1 // Scintilla source code edit control
2 /** @file ViewStyle.cxx
3  ** Store information on how the document is to be viewed.
4  **/
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7 
8 #include <string.h>
9 
10 #include "Platform.h"
11 
12 #include "Scintilla.h"
13 #include "Indicator.h"
14 #include "XPM.h"
15 #include "LineMarker.h"
16 #include "Style.h"
17 #include "ViewStyle.h"
18 
MarginStyle()19 MarginStyle::MarginStyle() :
20 	style(SC_MARGIN_SYMBOL), width(0), mask(0), sensitive(false) {
21 }
22 
23 // A list of the fontnames - avoids wasting space in each style
FontNames()24 FontNames::FontNames() {
25 	max = 0;
26 }
27 
~FontNames()28 FontNames::~FontNames() {
29 	Clear();
30 }
31 
Clear()32 void FontNames::Clear() {
33 	for (int i=0;i<max;i++) {
34 		delete []names[i];
35 	}
36 	max = 0;
37 }
38 
Save(const char * name)39 const char *FontNames::Save(const char *name) {
40 	if (!name)
41 		return 0;
42 	for (int i=0;i<max;i++) {
43 		if (strcmp(names[i], name) == 0) {
44 			return names[i];
45 		}
46 	}
47 	names[max] = new char[strlen(name) + 1];
48 	strcpy(names[max], name);
49 	max++;
50 	return names[max-1];
51 }
52 
ViewStyle()53 ViewStyle::ViewStyle() {
54 	Init();
55 }
56 
ViewStyle(const ViewStyle & source)57 ViewStyle::ViewStyle(const ViewStyle &source) {
58 	Init();
59 	for (unsigned int sty=0;sty<(sizeof(styles)/sizeof(styles[0]));sty++) {
60 		styles[sty] = source.styles[sty];
61 		// Can't just copy fontname as its lifetime is relative to its owning ViewStyle
62 		styles[sty].fontName = fontNames.Save(source.styles[sty].fontName);
63 	}
64 	for (int mrk=0;mrk<=MARKER_MAX;mrk++) {
65 		markers[mrk] = source.markers[mrk];
66 	}
67 	for (int ind=0;ind<=INDIC_MAX;ind++) {
68 		indicators[ind] = source.indicators[ind];
69 	}
70 
71 	selforeset = source.selforeset;
72 	selforeground.desired = source.selforeground.desired;
73 	selbackset = source.selbackset;
74 	selbackground.desired = source.selbackground.desired;
75 	selbackground2.desired = source.selbackground2.desired;
76 	selAlpha = source.selAlpha;
77 
78 	foldmarginColourSet = source.foldmarginColourSet;
79 	foldmarginColour.desired = source.foldmarginColour.desired;
80 	foldmarginHighlightColourSet = source.foldmarginHighlightColourSet;
81 	foldmarginHighlightColour.desired = source.foldmarginHighlightColour.desired;
82 
83 	hotspotForegroundSet = source.hotspotForegroundSet;
84 	hotspotForeground.desired = source.hotspotForeground.desired;
85 	hotspotBackgroundSet = source.hotspotBackgroundSet;
86 	hotspotBackground.desired = source.hotspotBackground.desired;
87 	hotspotUnderline = source.hotspotUnderline;
88 	hotspotSingleLine = source.hotspotSingleLine;
89 
90 	whitespaceForegroundSet = source.whitespaceForegroundSet;
91 	whitespaceForeground.desired = source.whitespaceForeground.desired;
92 	whitespaceBackgroundSet = source.whitespaceBackgroundSet;
93 	whitespaceBackground.desired = source.whitespaceBackground.desired;
94 	selbar.desired = source.selbar.desired;
95 	selbarlight.desired = source.selbarlight.desired;
96 	caretcolour.desired = source.caretcolour.desired;
97 	showCaretLineBackground = source.showCaretLineBackground;
98 	caretLineBackground.desired = source.caretLineBackground.desired;
99 	caretLineAlpha = source.caretLineAlpha;
100 	edgecolour.desired = source.edgecolour.desired;
101 	edgeState = source.edgeState;
102 	caretWidth = source.caretWidth;
103 	someStylesProtected = false;
104 	leftMarginWidth = source.leftMarginWidth;
105 	rightMarginWidth = source.rightMarginWidth;
106 	for (int i=0;i < margins; i++) {
107 		ms[i] = source.ms[i];
108 	}
109 	symbolMargin = source.symbolMargin;
110 	maskInLine = source.maskInLine;
111 	fixedColumnWidth = source.fixedColumnWidth;
112 	zoomLevel = source.zoomLevel;
113 	viewWhitespace = source.viewWhitespace;
114 	viewIndentationGuides = source.viewIndentationGuides;
115 	viewEOL = source.viewEOL;
116 	showMarkedLines = source.showMarkedLines;
117 	extraFontFlag = source.extraFontFlag;
118 }
119 
~ViewStyle()120 ViewStyle::~ViewStyle() {
121 }
122 
Init()123 void ViewStyle::Init() {
124 	fontNames.Clear();
125 	ResetDefaultStyle();
126 
127 	indicators[0].style = INDIC_SQUIGGLE;
128 	indicators[0].fore = ColourDesired(0, 0x7f, 0);
129 	indicators[1].style = INDIC_TT;
130 	indicators[1].fore = ColourDesired(0, 0, 0xff);
131 	indicators[2].style = INDIC_PLAIN;
132 	indicators[2].fore = ColourDesired(0xff, 0, 0);
133 
134 	lineHeight = 1;
135 	maxAscent = 1;
136 	maxDescent = 1;
137 	aveCharWidth = 8;
138 	spaceWidth = 8;
139 
140 	selforeset = false;
141 	selforeground.desired = ColourDesired(0xff, 0, 0);
142 	selbackset = true;
143 	selbackground.desired = ColourDesired(0xc0, 0xc0, 0xc0);
144 	selbackground2.desired = ColourDesired(0xb0, 0xb0, 0xb0);
145 	selAlpha = SC_ALPHA_NOALPHA;
146 
147 	foldmarginColourSet = false;
148 	foldmarginColour.desired = ColourDesired(0xff, 0, 0);
149 	foldmarginHighlightColourSet = false;
150 	foldmarginHighlightColour.desired = ColourDesired(0xc0, 0xc0, 0xc0);
151 
152 	whitespaceForegroundSet = false;
153 	whitespaceForeground.desired = ColourDesired(0, 0, 0);
154 	whitespaceBackgroundSet = false;
155 	whitespaceBackground.desired = ColourDesired(0xff, 0xff, 0xff);
156 	selbar.desired = Platform::Chrome();
157 	selbarlight.desired = Platform::ChromeHighlight();
158 	styles[STYLE_LINENUMBER].fore.desired = ColourDesired(0, 0, 0);
159 	styles[STYLE_LINENUMBER].back.desired = Platform::Chrome();
160 	caretcolour.desired = ColourDesired(0, 0, 0);
161 	showCaretLineBackground = false;
162 	caretLineBackground.desired = ColourDesired(0xff, 0xff, 0);
163 	caretLineAlpha = SC_ALPHA_NOALPHA;
164 	edgecolour.desired = ColourDesired(0xc0, 0xc0, 0xc0);
165 	edgeState = EDGE_NONE;
166 	caretWidth = 1;
167 	someStylesProtected = false;
168 
169 	hotspotForegroundSet = false;
170 	hotspotForeground.desired = ColourDesired(0, 0, 0xff);
171 	hotspotBackgroundSet = false;
172 	hotspotBackground.desired = ColourDesired(0xff, 0xff, 0xff);
173 	hotspotUnderline = true;
174 	hotspotSingleLine = true;
175 
176 	leftMarginWidth = 1;
177 	rightMarginWidth = 1;
178 	ms[0].style = SC_MARGIN_NUMBER;
179 	ms[0].width = 0;
180 	ms[0].mask = 0;
181 	ms[1].style = SC_MARGIN_SYMBOL;
182 	ms[1].width = 16;
183 	ms[1].mask = ~SC_MASK_FOLDERS;
184 	ms[2].style = SC_MARGIN_SYMBOL;
185 	ms[2].width = 0;
186 	ms[2].mask = 0;
187 	fixedColumnWidth = leftMarginWidth;
188 	symbolMargin = false;
189 	maskInLine = 0xffffffff;
190 	for (int margin=0; margin < margins; margin++) {
191 		fixedColumnWidth += ms[margin].width;
192 		symbolMargin = symbolMargin || (ms[margin].style != SC_MARGIN_NUMBER);
193 		if (ms[margin].width > 0)
194 			maskInLine &= ~ms[margin].mask;
195 	}
196 	zoomLevel = 0;
197 	viewWhitespace = wsInvisible;
198 	viewIndentationGuides = false;
199 	viewEOL = false;
200 	showMarkedLines = true;
201 	extraFontFlag = false;
202 }
203 
RefreshColourPalette(Palette & pal,bool want)204 void ViewStyle::RefreshColourPalette(Palette &pal, bool want) {
205 	unsigned int i;
206 	for (i=0;i<(sizeof(styles)/sizeof(styles[0]));i++) {
207 		pal.WantFind(styles[i].fore, want);
208 		pal.WantFind(styles[i].back, want);
209 	}
210 	for (i=0;i<(sizeof(indicators)/sizeof(indicators[0]));i++) {
211 		pal.WantFind(indicators[i].fore, want);
212 	}
213 	for (i=0;i<(sizeof(markers)/sizeof(markers[0]));i++) {
214 		markers[i].RefreshColourPalette(pal, want);
215 	}
216 	pal.WantFind(selforeground, want);
217 	pal.WantFind(selbackground, want);
218 	pal.WantFind(selbackground2, want);
219 
220 	pal.WantFind(foldmarginColour, want);
221 	pal.WantFind(foldmarginHighlightColour, want);
222 
223 	pal.WantFind(whitespaceForeground, want);
224 	pal.WantFind(whitespaceBackground, want);
225 	pal.WantFind(selbar, want);
226 	pal.WantFind(selbarlight, want);
227 	pal.WantFind(caretcolour, want);
228 	pal.WantFind(caretLineBackground, want);
229 	pal.WantFind(edgecolour, want);
230 	pal.WantFind(hotspotForeground, want);
231 	pal.WantFind(hotspotBackground, want);
232 }
233 
Refresh(Surface & surface)234 void ViewStyle::Refresh(Surface &surface) {
235 	selbar.desired = Platform::Chrome();
236 	selbarlight.desired = Platform::ChromeHighlight();
237 	styles[STYLE_DEFAULT].Realise(surface, zoomLevel, NULL, extraFontFlag);
238 	maxAscent = styles[STYLE_DEFAULT].ascent;
239 	maxDescent = styles[STYLE_DEFAULT].descent;
240 	someStylesProtected = false;
241 	for (unsigned int i=0;i<(sizeof(styles)/sizeof(styles[0]));i++) {
242 		if (i != STYLE_DEFAULT) {
243 			styles[i].Realise(surface, zoomLevel, &styles[STYLE_DEFAULT], extraFontFlag);
244 			if (maxAscent < styles[i].ascent)
245 				maxAscent = styles[i].ascent;
246 			if (maxDescent < styles[i].descent)
247 				maxDescent = styles[i].descent;
248 		}
249 		if (styles[i].IsProtected()) {
250 			someStylesProtected = true;
251 		}
252 	}
253 
254 	lineHeight = maxAscent + maxDescent;
255 	aveCharWidth = styles[STYLE_DEFAULT].aveCharWidth;
256 	spaceWidth = styles[STYLE_DEFAULT].spaceWidth;
257 
258 	fixedColumnWidth = leftMarginWidth;
259 	symbolMargin = false;
260 	maskInLine = 0xffffffff;
261 	for (int margin=0; margin < margins; margin++) {
262 		fixedColumnWidth += ms[margin].width;
263 		symbolMargin = symbolMargin || (ms[margin].style != SC_MARGIN_NUMBER);
264 		if (ms[margin].width > 0)
265 			maskInLine &= ~ms[margin].mask;
266 	}
267 }
268 
ResetDefaultStyle()269 void ViewStyle::ResetDefaultStyle() {
270 	styles[STYLE_DEFAULT].Clear(ColourDesired(0,0,0),
271 		ColourDesired(0xff,0xff,0xff),
272 	        Platform::DefaultFontSize(), fontNames.Save(Platform::DefaultFont()),
273 		SC_CHARSET_DEFAULT,
274 		false, false, false, false, Style::caseMixed, true, true, false);
275 }
276 
ClearStyles()277 void ViewStyle::ClearStyles() {
278 	// Reset all styles to be like the default style
279 	for (unsigned int i=0;i<(sizeof(styles)/sizeof(styles[0]));i++) {
280 		if (i != STYLE_DEFAULT) {
281 			styles[i].ClearTo(styles[STYLE_DEFAULT]);
282 		}
283 	}
284 	styles[STYLE_LINENUMBER].back.desired = Platform::Chrome();
285 
286 	// Set call tip fore/back to match the values previously set for call tips
287 	styles[STYLE_CALLTIP].back.desired = ColourDesired(0xff, 0xff, 0xff);
288 	styles[STYLE_CALLTIP].fore.desired = ColourDesired(0x80, 0x80, 0x80);
289 }
290 
SetStyleFontName(int styleIndex,const char * name)291 void ViewStyle::SetStyleFontName(int styleIndex, const char *name) {
292 	styles[styleIndex].fontName = fontNames.Save(name);
293 }
294 
ProtectionActive() const295 bool ViewStyle::ProtectionActive() const {
296     return someStylesProtected;
297 }
298