Merge branch 'master' of https://github.com/konsolebox/geany into konsolebox-master
[geany-mirror.git] / scintilla / src / ViewStyle.cxx
blob7ece73b2eceb4ab8ac178ca038fcfdc2ea5fa166
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.
8 #include <string.h>
9 #include <assert.h>
11 #include <stdexcept>
12 #include <vector>
13 #include <map>
15 #include "Platform.h"
17 #include "Scintilla.h"
18 #include "Position.h"
19 #include "SplitVector.h"
20 #include "Partitioning.h"
21 #include "RunStyles.h"
22 #include "Indicator.h"
23 #include "XPM.h"
24 #include "LineMarker.h"
25 #include "Style.h"
26 #include "ViewStyle.h"
28 #ifdef SCI_NAMESPACE
29 using namespace Scintilla;
30 #endif
32 MarginStyle::MarginStyle() :
33 style(SC_MARGIN_SYMBOL), width(0), mask(0), sensitive(false), cursor(SC_CURSORREVERSEARROW) {
36 // A list of the fontnames - avoids wasting space in each style
37 FontNames::FontNames() {
40 FontNames::~FontNames() {
41 Clear();
44 void FontNames::Clear() {
45 for (std::vector<char *>::const_iterator it=names.begin(); it != names.end(); ++it) {
46 delete []*it;
48 names.clear();
51 const char *FontNames::Save(const char *name) {
52 if (!name)
53 return 0;
55 for (std::vector<char *>::const_iterator it=names.begin(); it != names.end(); ++it) {
56 if (strcmp(*it, name) == 0) {
57 return *it;
60 const size_t lenName = strlen(name) + 1;
61 char *nameSave = new char[lenName];
62 memcpy(nameSave, name, lenName);
63 names.push_back(nameSave);
64 return nameSave;
67 FontRealised::FontRealised() {
70 FontRealised::~FontRealised() {
71 font.Release();
74 void FontRealised::Realise(Surface &surface, int zoomLevel, int technology, const FontSpecification &fs) {
75 PLATFORM_ASSERT(fs.fontName);
76 sizeZoomed = fs.size + zoomLevel * SC_FONT_SIZE_MULTIPLIER;
77 if (sizeZoomed <= 2 * SC_FONT_SIZE_MULTIPLIER) // Hangs if sizeZoomed <= 1
78 sizeZoomed = 2 * SC_FONT_SIZE_MULTIPLIER;
80 float deviceHeight = static_cast<float>(surface.DeviceHeightFont(sizeZoomed));
81 FontParameters fp(fs.fontName, deviceHeight / SC_FONT_SIZE_MULTIPLIER, fs.weight, fs.italic, fs.extraFontFlag, technology, fs.characterSet);
82 font.Create(fp);
84 ascent = static_cast<unsigned int>(surface.Ascent(font));
85 descent = static_cast<unsigned int>(surface.Descent(font));
86 aveCharWidth = surface.AverageCharWidth(font);
87 spaceWidth = surface.WidthChar(font, ' ');
90 ViewStyle::ViewStyle() {
91 Init();
94 ViewStyle::ViewStyle(const ViewStyle &source) {
95 Init(source.styles.size());
96 for (unsigned int sty=0; sty<source.styles.size(); sty++) {
97 styles[sty] = source.styles[sty];
98 // Can't just copy fontname as its lifetime is relative to its owning ViewStyle
99 styles[sty].fontName = fontNames.Save(source.styles[sty].fontName);
101 nextExtendedStyle = source.nextExtendedStyle;
102 for (int mrk=0; mrk<=MARKER_MAX; mrk++) {
103 markers[mrk] = source.markers[mrk];
105 CalcLargestMarkerHeight();
106 indicatorsDynamic = 0;
107 indicatorsSetFore = 0;
108 for (int ind=0; ind<=INDIC_MAX; ind++) {
109 indicators[ind] = source.indicators[ind];
110 if (indicators[ind].IsDynamic())
111 indicatorsDynamic++;
112 if (indicators[ind].OverridesTextFore())
113 indicatorsSetFore++;
116 selColours = source.selColours;
117 selAdditionalForeground = source.selAdditionalForeground;
118 selAdditionalBackground = source.selAdditionalBackground;
119 selBackground2 = source.selBackground2;
120 selAlpha = source.selAlpha;
121 selAdditionalAlpha = source.selAdditionalAlpha;
122 selEOLFilled = source.selEOLFilled;
124 foldmarginColour = source.foldmarginColour;
125 foldmarginHighlightColour = source.foldmarginHighlightColour;
127 hotspotColours = source.hotspotColours;
128 hotspotUnderline = source.hotspotUnderline;
129 hotspotSingleLine = source.hotspotSingleLine;
131 whitespaceColours = source.whitespaceColours;
132 controlCharSymbol = source.controlCharSymbol;
133 controlCharWidth = source.controlCharWidth;
134 selbar = source.selbar;
135 selbarlight = source.selbarlight;
136 caretcolour = source.caretcolour;
137 additionalCaretColour = source.additionalCaretColour;
138 showCaretLineBackground = source.showCaretLineBackground;
139 alwaysShowCaretLineBackground = source.alwaysShowCaretLineBackground;
140 caretLineBackground = source.caretLineBackground;
141 caretLineAlpha = source.caretLineAlpha;
142 edgecolour = source.edgecolour;
143 edgeState = source.edgeState;
144 caretStyle = source.caretStyle;
145 caretWidth = source.caretWidth;
146 someStylesProtected = false;
147 someStylesForceCase = false;
148 leftMarginWidth = source.leftMarginWidth;
149 rightMarginWidth = source.rightMarginWidth;
150 for (int margin=0; margin <= SC_MAX_MARGIN; margin++) {
151 ms[margin] = source.ms[margin];
153 maskInLine = source.maskInLine;
154 fixedColumnWidth = source.fixedColumnWidth;
155 marginInside = source.marginInside;
156 textStart = source.textStart;
157 zoomLevel = source.zoomLevel;
158 viewWhitespace = source.viewWhitespace;
159 whitespaceSize = source.whitespaceSize;
160 viewIndentationGuides = source.viewIndentationGuides;
161 viewEOL = source.viewEOL;
162 extraFontFlag = source.extraFontFlag;
163 extraAscent = source.extraAscent;
164 extraDescent = source.extraDescent;
165 marginStyleOffset = source.marginStyleOffset;
166 annotationVisible = source.annotationVisible;
167 annotationStyleOffset = source.annotationStyleOffset;
168 braceHighlightIndicatorSet = source.braceHighlightIndicatorSet;
169 braceHighlightIndicator = source.braceHighlightIndicator;
170 braceBadLightIndicatorSet = source.braceBadLightIndicatorSet;
171 braceBadLightIndicator = source.braceBadLightIndicator;
173 theEdge = source.theEdge;
175 marginNumberPadding = source.marginNumberPadding;
176 ctrlCharPadding = source.ctrlCharPadding;
177 lastSegItalicsOffset = source.lastSegItalicsOffset;
179 wrapState = source.wrapState;
180 wrapVisualFlags = source.wrapVisualFlags;
181 wrapVisualFlagsLocation = source.wrapVisualFlagsLocation;
182 wrapVisualStartIndent = source.wrapVisualStartIndent;
183 wrapIndentMode = source.wrapIndentMode;
186 ViewStyle::~ViewStyle() {
187 styles.clear();
188 for (FontMap::iterator it = fonts.begin(); it != fonts.end(); ++it) {
189 delete it->second;
191 fonts.clear();
194 void ViewStyle::Init(size_t stylesSize_) {
195 AllocStyles(stylesSize_);
196 nextExtendedStyle = 256;
197 fontNames.Clear();
198 ResetDefaultStyle();
200 // There are no image markers by default, so no need for calling CalcLargestMarkerHeight()
201 largestMarkerHeight = 0;
203 indicators[0] = Indicator(INDIC_SQUIGGLE, ColourDesired(0, 0x7f, 0));
204 indicators[1] = Indicator(INDIC_TT, ColourDesired(0, 0, 0xff));
205 indicators[2] = Indicator(INDIC_PLAIN, ColourDesired(0xff, 0, 0));
207 technology = SC_TECHNOLOGY_DEFAULT;
208 indicatorsDynamic = 0;
209 indicatorsSetFore = 0;
210 lineHeight = 1;
211 lineOverlap = 0;
212 maxAscent = 1;
213 maxDescent = 1;
214 aveCharWidth = 8;
215 spaceWidth = 8;
216 tabWidth = spaceWidth * 8;
218 selColours.fore = ColourOptional(ColourDesired(0xff, 0, 0));
219 selColours.back = ColourOptional(ColourDesired(0xc0, 0xc0, 0xc0), true);
220 selAdditionalForeground = ColourDesired(0xff, 0, 0);
221 selAdditionalBackground = ColourDesired(0xd7, 0xd7, 0xd7);
222 selBackground2 = ColourDesired(0xb0, 0xb0, 0xb0);
223 selAlpha = SC_ALPHA_NOALPHA;
224 selAdditionalAlpha = SC_ALPHA_NOALPHA;
225 selEOLFilled = false;
227 foldmarginColour = ColourOptional(ColourDesired(0xff, 0, 0));
228 foldmarginHighlightColour = ColourOptional(ColourDesired(0xc0, 0xc0, 0xc0));
230 whitespaceColours.fore = ColourOptional();
231 whitespaceColours.back = ColourOptional(ColourDesired(0xff, 0xff, 0xff));
232 controlCharSymbol = 0; /* Draw the control characters */
233 controlCharWidth = 0;
234 selbar = Platform::Chrome();
235 selbarlight = Platform::ChromeHighlight();
236 styles[STYLE_LINENUMBER].fore = ColourDesired(0, 0, 0);
237 styles[STYLE_LINENUMBER].back = Platform::Chrome();
238 caretcolour = ColourDesired(0, 0, 0);
239 additionalCaretColour = ColourDesired(0x7f, 0x7f, 0x7f);
240 showCaretLineBackground = false;
241 alwaysShowCaretLineBackground = false;
242 caretLineBackground = ColourDesired(0xff, 0xff, 0);
243 caretLineAlpha = SC_ALPHA_NOALPHA;
244 edgecolour = ColourDesired(0xc0, 0xc0, 0xc0);
245 edgeState = EDGE_NONE;
246 caretStyle = CARETSTYLE_LINE;
247 caretWidth = 1;
248 someStylesProtected = false;
249 someStylesForceCase = false;
251 hotspotColours.fore = ColourOptional(ColourDesired(0, 0, 0xff));
252 hotspotColours.back = ColourOptional(ColourDesired(0xff, 0xff, 0xff));
253 hotspotUnderline = true;
254 hotspotSingleLine = true;
256 leftMarginWidth = 1;
257 rightMarginWidth = 1;
258 ms[0].style = SC_MARGIN_NUMBER;
259 ms[0].width = 0;
260 ms[0].mask = 0;
261 ms[1].style = SC_MARGIN_SYMBOL;
262 ms[1].width = 16;
263 ms[1].mask = ~SC_MASK_FOLDERS;
264 ms[2].style = SC_MARGIN_SYMBOL;
265 ms[2].width = 0;
266 ms[2].mask = 0;
267 marginInside = true;
268 fixedColumnWidth = marginInside ? leftMarginWidth : 0;
269 maskInLine = 0xffffffff;
270 for (int margin=0; margin <= SC_MAX_MARGIN; margin++) {
271 fixedColumnWidth += ms[margin].width;
272 if (ms[margin].width > 0)
273 maskInLine &= ~ms[margin].mask;
275 textStart = marginInside ? fixedColumnWidth : leftMarginWidth;
276 zoomLevel = 0;
277 viewWhitespace = wsInvisible;
278 whitespaceSize = 1;
279 viewIndentationGuides = ivNone;
280 viewEOL = false;
281 extraFontFlag = 0;
282 extraAscent = 0;
283 extraDescent = 0;
284 marginStyleOffset = 0;
285 annotationVisible = ANNOTATION_HIDDEN;
286 annotationStyleOffset = 0;
287 braceHighlightIndicatorSet = false;
288 braceHighlightIndicator = 0;
289 braceBadLightIndicatorSet = false;
290 braceBadLightIndicator = 0;
292 theEdge = 0;
294 marginNumberPadding = 3;
295 ctrlCharPadding = 3; // +3 For a blank on front and rounded edge each side
296 lastSegItalicsOffset = 2;
298 wrapState = eWrapNone;
299 wrapVisualFlags = 0;
300 wrapVisualFlagsLocation = 0;
301 wrapVisualStartIndent = 0;
302 wrapIndentMode = SC_WRAPINDENT_FIXED;
305 void ViewStyle::Refresh(Surface &surface, int tabInChars) {
306 for (FontMap::iterator it = fonts.begin(); it != fonts.end(); ++it) {
307 delete it->second;
309 fonts.clear();
311 selbar = Platform::Chrome();
312 selbarlight = Platform::ChromeHighlight();
314 for (unsigned int i=0; i<styles.size(); i++) {
315 styles[i].extraFontFlag = extraFontFlag;
318 CreateAndAddFont(styles[STYLE_DEFAULT]);
319 for (unsigned int j=0; j<styles.size(); j++) {
320 CreateAndAddFont(styles[j]);
323 for (FontMap::iterator it = fonts.begin(); it != fonts.end(); ++it) {
324 it->second->Realise(surface, zoomLevel, technology, it->first);
327 for (unsigned int k=0; k<styles.size(); k++) {
328 FontRealised *fr = Find(styles[k]);
329 styles[k].Copy(fr->font, *fr);
331 indicatorsDynamic = 0;
332 indicatorsSetFore = 0;
333 for (int ind = 0; ind <= INDIC_MAX; ind++) {
334 if (indicators[ind].IsDynamic())
335 indicatorsDynamic++;
336 if (indicators[ind].OverridesTextFore())
337 indicatorsSetFore++;
339 maxAscent = 1;
340 maxDescent = 1;
341 FindMaxAscentDescent();
342 maxAscent += extraAscent;
343 maxDescent += extraDescent;
344 lineHeight = maxAscent + maxDescent;
345 lineOverlap = lineHeight / 10;
346 if (lineOverlap < 2)
347 lineOverlap = 2;
348 if (lineOverlap > lineHeight)
349 lineOverlap = lineHeight;
351 someStylesProtected = false;
352 someStylesForceCase = false;
353 for (unsigned int l=0; l<styles.size(); l++) {
354 if (styles[l].IsProtected()) {
355 someStylesProtected = true;
357 if (styles[l].caseForce != Style::caseMixed) {
358 someStylesForceCase = true;
362 aveCharWidth = styles[STYLE_DEFAULT].aveCharWidth;
363 spaceWidth = styles[STYLE_DEFAULT].spaceWidth;
364 tabWidth = spaceWidth * tabInChars;
366 controlCharWidth = 0.0;
367 if (controlCharSymbol >= 32) {
368 controlCharWidth = surface.WidthChar(styles[STYLE_CONTROLCHAR].font, static_cast<char>(controlCharSymbol));
371 fixedColumnWidth = marginInside ? leftMarginWidth : 0;
372 maskInLine = 0xffffffff;
373 for (int margin=0; margin <= SC_MAX_MARGIN; margin++) {
374 fixedColumnWidth += ms[margin].width;
375 if (ms[margin].width > 0)
376 maskInLine &= ~ms[margin].mask;
378 textStart = marginInside ? fixedColumnWidth : leftMarginWidth;
381 void ViewStyle::ReleaseAllExtendedStyles() {
382 nextExtendedStyle = 256;
385 int ViewStyle::AllocateExtendedStyles(int numberStyles) {
386 int startRange = static_cast<int>(nextExtendedStyle);
387 nextExtendedStyle += numberStyles;
388 EnsureStyle(nextExtendedStyle);
389 for (size_t i=startRange; i<nextExtendedStyle; i++) {
390 styles[i].ClearTo(styles[STYLE_DEFAULT]);
392 return startRange;
395 void ViewStyle::EnsureStyle(size_t index) {
396 if (index >= styles.size()) {
397 AllocStyles(index+1);
401 void ViewStyle::ResetDefaultStyle() {
402 styles[STYLE_DEFAULT].Clear(ColourDesired(0,0,0),
403 ColourDesired(0xff,0xff,0xff),
404 Platform::DefaultFontSize() * SC_FONT_SIZE_MULTIPLIER, fontNames.Save(Platform::DefaultFont()),
405 SC_CHARSET_DEFAULT,
406 SC_WEIGHT_NORMAL, false, false, false, Style::caseMixed, true, true, false);
409 void ViewStyle::ClearStyles() {
410 // Reset all styles to be like the default style
411 for (unsigned int i=0; i<styles.size(); i++) {
412 if (i != STYLE_DEFAULT) {
413 styles[i].ClearTo(styles[STYLE_DEFAULT]);
416 styles[STYLE_LINENUMBER].back = Platform::Chrome();
418 // Set call tip fore/back to match the values previously set for call tips
419 styles[STYLE_CALLTIP].back = ColourDesired(0xff, 0xff, 0xff);
420 styles[STYLE_CALLTIP].fore = ColourDesired(0x80, 0x80, 0x80);
423 void ViewStyle::SetStyleFontName(int styleIndex, const char *name) {
424 styles[styleIndex].fontName = fontNames.Save(name);
427 bool ViewStyle::ProtectionActive() const {
428 return someStylesProtected;
431 int ViewStyle::ExternalMarginWidth() const {
432 return marginInside ? 0 : fixedColumnWidth;
435 bool ViewStyle::ValidStyle(size_t styleIndex) const {
436 return styleIndex < styles.size();
439 void ViewStyle::CalcLargestMarkerHeight() {
440 largestMarkerHeight = 0;
441 for (int m = 0; m <= MARKER_MAX; ++m) {
442 switch (markers[m].markType) {
443 case SC_MARK_PIXMAP:
444 if (markers[m].pxpm && markers[m].pxpm->GetHeight() > largestMarkerHeight)
445 largestMarkerHeight = markers[m].pxpm->GetHeight();
446 break;
447 case SC_MARK_RGBAIMAGE:
448 if (markers[m].image && markers[m].image->GetHeight() > largestMarkerHeight)
449 largestMarkerHeight = markers[m].image->GetHeight();
450 break;
455 // See if something overrides the line background color: Either if caret is on the line
456 // and background color is set for that, or if a marker is defined that forces its background
457 // color onto the line, or if a marker is defined but has no selection margin in which to
458 // display itself (as long as it's not an SC_MARK_EMPTY marker). These are checked in order
459 // with the earlier taking precedence. When multiple markers cause background override,
460 // the color for the highest numbered one is used.
461 ColourOptional ViewStyle::Background(int marksOfLine, bool caretActive, bool lineContainsCaret) const {
462 ColourOptional background;
463 if ((caretActive || alwaysShowCaretLineBackground) && showCaretLineBackground && (caretLineAlpha == SC_ALPHA_NOALPHA) && lineContainsCaret) {
464 background = ColourOptional(caretLineBackground, true);
466 if (!background.isSet && marksOfLine) {
467 int marks = marksOfLine;
468 for (int markBit = 0; (markBit < 32) && marks; markBit++) {
469 if ((marks & 1) && (markers[markBit].markType == SC_MARK_BACKGROUND) &&
470 (markers[markBit].alpha == SC_ALPHA_NOALPHA)) {
471 background = ColourOptional(markers[markBit].back, true);
473 marks >>= 1;
476 if (!background.isSet && maskInLine) {
477 int marksMasked = marksOfLine & maskInLine;
478 if (marksMasked) {
479 for (int markBit = 0; (markBit < 32) && marksMasked; markBit++) {
480 if ((marksMasked & 1) && (markers[markBit].markType != SC_MARK_EMPTY) &&
481 (markers[markBit].alpha == SC_ALPHA_NOALPHA)) {
482 background = ColourOptional(markers[markBit].back, true);
484 marksMasked >>= 1;
488 return background;
491 bool ViewStyle::SelectionBackgroundDrawn() const {
492 return selColours.back.isSet &&
493 ((selAlpha == SC_ALPHA_NOALPHA) || (selAdditionalAlpha == SC_ALPHA_NOALPHA));
496 bool ViewStyle::WhitespaceBackgroundDrawn() const {
497 return (viewWhitespace != wsInvisible) && (whitespaceColours.back.isSet);
500 ColourDesired ViewStyle::WrapColour() const {
501 if (whitespaceColours.fore.isSet)
502 return whitespaceColours.fore;
503 else
504 return styles[STYLE_DEFAULT].fore;
507 bool ViewStyle::SetWrapState(int wrapState_) {
508 WrapMode wrapStateWanted;
509 switch (wrapState_) {
510 case SC_WRAP_WORD:
511 wrapStateWanted = eWrapWord;
512 break;
513 case SC_WRAP_CHAR:
514 wrapStateWanted = eWrapChar;
515 break;
516 case SC_WRAP_WHITESPACE:
517 wrapStateWanted = eWrapWhitespace;
518 break;
519 default:
520 wrapStateWanted = eWrapNone;
521 break;
523 bool changed = wrapState != wrapStateWanted;
524 wrapState = wrapStateWanted;
525 return changed;
528 bool ViewStyle::SetWrapVisualFlags(int wrapVisualFlags_) {
529 bool changed = wrapVisualFlags != wrapVisualFlags_;
530 wrapVisualFlags = wrapVisualFlags_;
531 return changed;
534 bool ViewStyle::SetWrapVisualFlagsLocation(int wrapVisualFlagsLocation_) {
535 bool changed = wrapVisualFlagsLocation != wrapVisualFlagsLocation_;
536 wrapVisualFlagsLocation = wrapVisualFlagsLocation_;
537 return changed;
540 bool ViewStyle::SetWrapVisualStartIndent(int wrapVisualStartIndent_) {
541 bool changed = wrapVisualStartIndent != wrapVisualStartIndent_;
542 wrapVisualStartIndent = wrapVisualStartIndent_;
543 return changed;
546 bool ViewStyle::SetWrapIndentMode(int wrapIndentMode_) {
547 bool changed = wrapIndentMode != wrapIndentMode_;
548 wrapIndentMode = wrapIndentMode_;
549 return changed;
552 void ViewStyle::AllocStyles(size_t sizeNew) {
553 size_t i=styles.size();
554 styles.resize(sizeNew);
555 if (styles.size() > STYLE_DEFAULT) {
556 for (; i<sizeNew; i++) {
557 if (i != STYLE_DEFAULT) {
558 styles[i].ClearTo(styles[STYLE_DEFAULT]);
564 void ViewStyle::CreateAndAddFont(const FontSpecification &fs) {
565 if (fs.fontName) {
566 FontMap::iterator it = fonts.find(fs);
567 if (it == fonts.end()) {
568 fonts[fs] = new FontRealised();
573 FontRealised *ViewStyle::Find(const FontSpecification &fs) {
574 if (!fs.fontName) // Invalid specification so return arbitrary object
575 return fonts.begin()->second;
576 FontMap::iterator it = fonts.find(fs);
577 if (it != fonts.end()) {
578 // Should always reach here since map was just set for all styles
579 return it->second;
581 return 0;
584 void ViewStyle::FindMaxAscentDescent() {
585 for (FontMap::const_iterator it = fonts.begin(); it != fonts.end(); ++it) {
586 if (maxAscent < it->second->ascent)
587 maxAscent = it->second->ascent;
588 if (maxDescent < it->second->descent)
589 maxDescent = it->second->descent;