applied backgroundcolors.patch
[TortoiseGit.git] / ext / scintilla / src / Editor.h
blobb785c1f1be116c171c07a83f07fa362c70a570a0
1 // Scintilla source code edit control
2 /** @file Editor.h
3 ** Defines the main editor class.
4 **/
5 // Copyright 1998-2011 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #ifndef EDITOR_H
9 #define EDITOR_H
11 #ifdef SCI_NAMESPACE
12 namespace Scintilla {
13 #endif
15 /**
17 class Caret {
18 public:
19 bool active;
20 bool on;
21 int period;
23 Caret();
26 /**
28 class Timer {
29 public:
30 bool ticking;
31 int ticksToWait;
32 enum {tickSize = 100};
33 TickerID tickerID;
35 Timer();
38 /**
40 class Idler {
41 public:
42 bool state;
43 IdlerID idlerID;
45 Idler();
48 /**
49 * When platform has a way to generate an event before painting,
50 * accumulate needed styling range in StyleNeeded to avoid unnecessary work.
52 class StyleNeeded {
53 public:
54 bool active;
55 Position upTo;
57 StyleNeeded() : active(false), upTo(0) {}
58 void Reset() {
59 active = false;
60 upTo = 0;
62 void NeedUpTo(Position pos) {
63 if (upTo < pos)
64 upTo = pos;
68 /**
69 * Hold a piece of text selected for copying or dragging.
70 * The text is expected to hold a terminating '\0' and this is counted in len.
72 class SelectionText {
73 public:
74 char *s;
75 int len;
76 bool rectangular;
77 bool lineCopy;
78 int codePage;
79 int characterSet;
80 SelectionText() : s(0), len(0), rectangular(false), lineCopy(false), codePage(0), characterSet(0) {}
81 ~SelectionText() {
82 Free();
84 void Free() {
85 Set(0, 0, 0, 0, false, false);
87 void Set(char *s_, int len_, int codePage_, int characterSet_, bool rectangular_, bool lineCopy_) {
88 delete []s;
89 s = s_;
90 if (s)
91 len = len_;
92 else
93 len = 0;
94 codePage = codePage_;
95 characterSet = characterSet_;
96 rectangular = rectangular_;
97 lineCopy = lineCopy_;
99 void Copy(const char *s_, int len_, int codePage_, int characterSet_, bool rectangular_, bool lineCopy_) {
100 delete []s;
101 s = 0;
102 s = new char[len_];
103 len = len_;
104 for (int i = 0; i < len_; i++) {
105 s[i] = s_[i];
107 codePage = codePage_;
108 characterSet = characterSet_;
109 rectangular = rectangular_;
110 lineCopy = lineCopy_;
112 void Copy(const SelectionText &other) {
113 Copy(other.s, other.len, other.codePage, other.characterSet, other.rectangular, other.lineCopy);
119 class Editor : public DocWatcher {
120 // Private so Editor objects can not be copied
121 Editor(const Editor &);
122 Editor &operator=(const Editor &);
124 protected: // ScintillaBase subclass needs access to much of Editor
126 /** On GTK+, Scintilla is a container widget holding two scroll bars
127 * whereas on Windows there is just one window with both scroll bars turned on. */
128 Window wMain; ///< The Scintilla parent window
130 /** Style resources may be expensive to allocate so are cached between uses.
131 * When a style attribute is changed, this cache is flushed. */
132 bool stylesValid;
133 ViewStyle vs;
134 int technology;
135 Point sizeRGBAImage;
136 float scaleRGBAImage;
138 int printMagnification;
139 int printColourMode;
140 int printWrapState;
141 int cursorMode;
142 int controlCharSymbol;
144 // Highlight current folding block
145 HighlightDelimiter highlightDelimiter;
147 bool hasFocus;
148 bool hideSelection;
149 bool inOverstrike;
150 bool mouseDownCaptures;
152 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
153 * the screen. This avoids flashing but is about 30% slower. */
154 bool bufferedDraw;
155 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
156 * and then the foreground. This avoids chopping off characters that overlap the next run. */
157 bool twoPhaseDraw;
159 int xOffset; ///< Horizontal scrolled amount in pixels
160 int xCaretMargin; ///< Ensure this many pixels visible on both sides of caret
161 bool horizontalScrollBarVisible;
162 int scrollWidth;
163 bool trackLineWidth;
164 int lineWidthMaxSeen;
165 bool verticalScrollBarVisible;
166 bool endAtLastLine;
167 int caretSticky;
168 int marginOptions;
169 bool multipleSelection;
170 bool additionalSelectionTyping;
171 int multiPasteMode;
172 bool additionalCaretsBlink;
173 bool additionalCaretsVisible;
175 int virtualSpaceOptions;
177 Surface *pixmapLine;
178 Surface *pixmapSelMargin;
179 Surface *pixmapSelPattern;
180 Surface *pixmapIndentGuide;
181 Surface *pixmapIndentGuideHighlight;
183 LineLayoutCache llc;
184 PositionCache posCache;
186 KeyMap kmap;
188 Caret caret;
189 Timer timer;
190 Timer autoScrollTimer;
191 enum { autoScrollDelay = 200 };
193 Idler idler;
195 Point lastClick;
196 unsigned int lastClickTime;
197 int dwellDelay;
198 int ticksToDwell;
199 bool dwelling;
200 enum { selChar, selWord, selSubLine, selWholeLine } selectionType;
201 Point ptMouseLast;
202 enum { ddNone, ddInitial, ddDragging } inDragDrop;
203 bool dropWentOutside;
204 SelectionPosition posDrag;
205 SelectionPosition posDrop;
206 int hotSpotClickPos;
207 int lastXChosen;
208 int lineAnchorPos;
209 int originalAnchorPos;
210 int wordSelectAnchorStartPos;
211 int wordSelectAnchorEndPos;
212 int wordSelectInitialCaretPos;
213 int targetStart;
214 int targetEnd;
215 int searchFlags;
216 int topLine;
217 int posTopLine;
218 int lengthForEncode;
220 int needUpdateUI;
221 Position braces[2];
222 int bracesMatchStyle;
223 int highlightGuideColumn;
225 int theEdge;
227 enum { notPainting, painting, paintAbandoned } paintState;
228 PRectangle rcPaint;
229 bool paintingAllText;
230 bool willRedrawAll;
231 StyleNeeded styleNeeded;
233 int modEventMask;
235 SelectionText drag;
236 Selection sel;
237 bool primarySelection;
239 int caretXPolicy;
240 int caretXSlop; ///< Ensure this many pixels visible on both sides of caret
242 int caretYPolicy;
243 int caretYSlop; ///< Ensure this many lines visible on both sides of caret
245 int visiblePolicy;
246 int visibleSlop;
248 int searchAnchor;
250 bool recordingMacro;
252 int foldFlags;
253 ContractionState cs;
255 // Hotspot support
256 int hsStart;
257 int hsEnd;
259 // Wrapping support
260 enum { eWrapNone, eWrapWord, eWrapChar } wrapState;
261 enum { wrapLineLarge = 0x7ffffff };
262 int wrapWidth;
263 int wrapStart;
264 int wrapEnd;
265 int wrapVisualFlags;
266 int wrapVisualFlagsLocation;
267 int wrapVisualStartIndent;
268 int wrapIndentMode; // SC_WRAPINDENT_FIXED, _SAME, _INDENT
270 bool convertPastes;
272 int marginNumberPadding; // the right-side padding of the number margin
273 int ctrlCharPadding; // the padding around control character text blobs
274 int lastSegItalicsOffset; // the offset so as not to clip italic characters at EOLs
276 Document *pdoc;
278 Editor();
279 virtual ~Editor();
280 virtual void Initialise() = 0;
281 virtual void Finalise();
283 void InvalidateStyleData();
284 void InvalidateStyleRedraw();
285 void RefreshStyleData();
286 void DropGraphics(bool freeObjects);
287 void AllocateGraphics();
289 virtual PRectangle GetClientRectangle();
290 PRectangle GetTextRectangle();
292 int LinesOnScreen();
293 int LinesToScroll();
294 int MaxScrollPos();
295 SelectionPosition ClampPositionIntoDocument(SelectionPosition sp) const;
296 Point LocationFromPosition(SelectionPosition pos);
297 Point LocationFromPosition(int pos);
298 int XFromPosition(int pos);
299 int XFromPosition(SelectionPosition sp);
300 SelectionPosition SPositionFromLocation(Point pt, bool canReturnInvalid=false, bool charPosition=false, bool virtualSpace=true);
301 int PositionFromLocation(Point pt, bool canReturnInvalid=false, bool charPosition=false);
302 SelectionPosition SPositionFromLineX(int lineDoc, int x);
303 int PositionFromLineX(int line, int x);
304 int LineFromLocation(Point pt);
305 void SetTopLine(int topLineNew);
307 bool AbandonPaint();
308 void RedrawRect(PRectangle rc);
309 void Redraw();
310 void RedrawSelMargin(int line=-1, bool allAfter=false);
311 PRectangle RectangleFromRange(int start, int end);
312 void InvalidateRange(int start, int end);
314 bool UserVirtualSpace() const {
315 return ((virtualSpaceOptions & SCVS_USERACCESSIBLE) != 0);
317 int CurrentPosition();
318 bool SelectionEmpty();
319 SelectionPosition SelectionStart();
320 SelectionPosition SelectionEnd();
321 void SetRectangularRange();
322 void ThinRectangularRange();
323 void InvalidateSelection(SelectionRange newMain, bool invalidateWholeSelection=false);
324 void SetSelection(SelectionPosition currentPos_, SelectionPosition anchor_);
325 void SetSelection(int currentPos_, int anchor_);
326 void SetSelection(SelectionPosition currentPos_);
327 void SetSelection(int currentPos_);
328 void SetEmptySelection(SelectionPosition currentPos_);
329 void SetEmptySelection(int currentPos_);
330 bool RangeContainsProtected(int start, int end) const;
331 bool SelectionContainsProtected();
332 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true) const;
333 SelectionPosition MovePositionOutsideChar(SelectionPosition pos, int moveDir, bool checkLineEnd=true) const;
334 int MovePositionTo(SelectionPosition newPos, Selection::selTypes sel=Selection::noSel, bool ensureVisible=true);
335 int MovePositionTo(int newPos, Selection::selTypes sel=Selection::noSel, bool ensureVisible=true);
336 SelectionPosition MovePositionSoVisible(SelectionPosition pos, int moveDir);
337 SelectionPosition MovePositionSoVisible(int pos, int moveDir);
338 Point PointMainCaret();
339 void SetLastXChosen();
341 void ScrollTo(int line, bool moveThumb=true);
342 virtual void ScrollText(int linesToMove);
343 void HorizontalScrollTo(int xPos);
344 void VerticalCentreCaret();
345 void MoveSelectedLines(int lineDelta);
346 void MoveSelectedLinesUp();
347 void MoveSelectedLinesDown();
348 void MoveCaretInsideView(bool ensureVisible=true);
349 int DisplayFromPosition(int pos);
351 struct XYScrollPosition {
352 int xOffset;
353 int topLine;
354 XYScrollPosition(int xOffset_, int topLine_) : xOffset(xOffset_), topLine(topLine_) {}
356 XYScrollPosition XYScrollToMakeVisible(const bool useMargin, const bool vert, const bool horiz);
357 void SetXYScroll(XYScrollPosition newXY);
358 void EnsureCaretVisible(bool useMargin=true, bool vert=true, bool horiz=true);
359 void ShowCaretAtCurrentPosition();
360 void DropCaret();
361 void InvalidateCaret();
362 virtual void UpdateSystemCaret();
364 void NeedWrapping(int docLineStart = 0, int docLineEnd = wrapLineLarge);
365 bool WrapOneLine(Surface *surface, int lineToWrap);
366 bool WrapLines(bool fullWrap, int priorityWrapLineStart);
367 void LinesJoin();
368 void LinesSplit(int pixelWidth);
370 int SubstituteMarkerIfEmpty(int markerCheck, int markerDefault);
371 void PaintSelMargin(Surface *surface, PRectangle &rc);
372 LineLayout *RetrieveLineLayout(int lineNumber);
373 void LayoutLine(int line, Surface *surface, ViewStyle &vstyle, LineLayout *ll,
374 int width=LineLayout::wrapWidthInfinite);
375 ColourDesired SelectionBackground(ViewStyle &vsDraw, bool main);
376 ColourDesired TextBackground(ViewStyle &vsDraw, bool overrideBackground, ColourDesired background, int inSelection, bool inHotspot, int styleMain, int i, LineLayout *ll);
377 void DrawIndentGuide(Surface *surface, int lineVisible, int lineHeight, int start, PRectangle rcSegment, bool highlight);
378 void DrawWrapMarker(Surface *surface, PRectangle rcPlace, bool isEndMarker, ColourDesired wrapColour);
379 void DrawEOL(Surface *surface, ViewStyle &vsDraw, PRectangle rcLine, LineLayout *ll,
380 int line, int lineEnd, int xStart, int subLine, XYACCUMULATOR subLineStart,
381 bool overrideBackground, ColourDesired background,
382 bool drawWrapMark, ColourDesired wrapColour);
383 void DrawIndicator(int indicNum, int startPos, int endPos, Surface *surface, ViewStyle &vsDraw,
384 int xStart, PRectangle rcLine, LineLayout *ll, int subLine);
385 void DrawIndicators(Surface *surface, ViewStyle &vsDraw, int line, int xStart,
386 PRectangle rcLine, LineLayout *ll, int subLine, int lineEnd, bool under);
387 void DrawAnnotation(Surface *surface, ViewStyle &vsDraw, int line, int xStart,
388 PRectangle rcLine, LineLayout *ll, int subLine);
389 void DrawLine(Surface *surface, ViewStyle &vsDraw, int line, int lineVisible, int xStart,
390 PRectangle rcLine, LineLayout *ll, int subLine);
391 void DrawBlockCaret(Surface *surface, ViewStyle &vsDraw, LineLayout *ll, int subLine,
392 int xStart, int offset, int posCaret, PRectangle rcCaret, ColourDesired caretColour);
393 void DrawCarets(Surface *surface, ViewStyle &vsDraw, int line, int xStart,
394 PRectangle rcLine, LineLayout *ll, int subLine);
395 void RefreshPixMaps(Surface *surfaceWindow);
396 void Paint(Surface *surfaceWindow, PRectangle rcArea);
397 long FormatRange(bool draw, Sci_RangeToFormat *pfr);
398 int TextWidth(int style, const char *text);
400 virtual void SetVerticalScrollPos() = 0;
401 virtual void SetHorizontalScrollPos() = 0;
402 virtual bool ModifyScrollBars(int nMax, int nPage) = 0;
403 virtual void ReconfigureScrollBars();
404 void SetScrollBars();
405 void ChangeSize();
407 void FilterSelections();
408 int InsertSpace(int position, unsigned int spaces);
409 void AddChar(char ch);
410 virtual void AddCharUTF(char *s, unsigned int len, bool treatAsDBCS=false);
411 void InsertPaste(SelectionPosition selStart, const char *text, int len);
412 void ClearSelection(bool retainMultipleSelections=false);
413 void ClearAll();
414 void ClearDocumentStyle();
415 void Cut();
416 void PasteRectangular(SelectionPosition pos, const char *ptr, int len);
417 virtual void Copy() = 0;
418 virtual void CopyAllowLine();
419 virtual bool CanPaste();
420 virtual void Paste() = 0;
421 void Clear();
422 void SelectAll();
423 void Undo();
424 void Redo();
425 void DelChar();
426 void DelCharBack(bool allowLineStartDeletion);
427 virtual void ClaimSelection() = 0;
429 virtual void NotifyChange() = 0;
430 virtual void NotifyFocus(bool focus);
431 virtual void SetCtrlID(int identifier);
432 virtual int GetCtrlID() { return ctrlID; }
433 virtual void NotifyParent(SCNotification scn) = 0;
434 virtual void NotifyParent(SCNotification * scn) = 0;
435 virtual void NotifyStyleToNeeded(int endStyleNeeded);
436 void NotifyChar(int ch);
437 void NotifySavePoint(bool isSavePoint);
438 void NotifyModifyAttempt();
439 virtual void NotifyDoubleClick(Point pt, bool shift, bool ctrl, bool alt);
440 void NotifyHotSpotClicked(int position, bool shift, bool ctrl, bool alt);
441 void NotifyHotSpotDoubleClicked(int position, bool shift, bool ctrl, bool alt);
442 void NotifyHotSpotReleaseClick(int position, bool shift, bool ctrl, bool alt);
443 void NotifyUpdateUI();
444 void NotifyPainted();
445 void NotifyIndicatorClick(bool click, int position, bool shift, bool ctrl, bool alt);
446 bool NotifyMarginClick(Point pt, bool shift, bool ctrl, bool alt);
447 void NotifyNeedShown(int pos, int len);
448 void NotifyDwelling(Point pt, bool state);
449 void NotifyZoom();
451 void NotifyModifyAttempt(Document *document, void *userData);
452 void NotifySavePoint(Document *document, void *userData, bool atSavePoint);
453 void CheckModificationForWrap(DocModification mh);
454 void NotifyModified(Document *document, DocModification mh, void *userData);
455 void NotifyDeleted(Document *document, void *userData);
456 void NotifyStyleNeeded(Document *doc, void *userData, int endPos);
457 void NotifyLexerChanged(Document *doc, void *userData);
458 void NotifyErrorOccurred(Document *doc, void *userData, int status);
459 void NotifyMacroRecord(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
461 void ContainerNeedsUpdate(int flags);
462 void PageMove(int direction, Selection::selTypes sel=Selection::noSel, bool stuttered = false);
463 enum { cmSame, cmUpper, cmLower } caseMap;
464 virtual std::string CaseMapString(const std::string &s, int caseMapping);
465 void ChangeCaseOfSelection(int caseMapping);
466 void LineTranspose();
467 void Duplicate(bool forLine);
468 virtual void CancelModes();
469 void NewLine();
470 void CursorUpOrDown(int direction, Selection::selTypes sel=Selection::noSel);
471 void ParaUpOrDown(int direction, Selection::selTypes sel=Selection::noSel);
472 int StartEndDisplayLine(int pos, bool start);
473 virtual int KeyCommand(unsigned int iMessage);
474 virtual int KeyDefault(int /* key */, int /*modifiers*/);
475 int KeyDownWithModifiers(int key, int modifiers, bool *consumed);
476 int KeyDown(int key, bool shift, bool ctrl, bool alt, bool *consumed=0);
478 void Indent(bool forwards);
480 virtual CaseFolder *CaseFolderForEncoding();
481 long FindText(uptr_t wParam, sptr_t lParam);
482 void SearchAnchor();
483 long SearchText(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
484 long SearchInTarget(const char *text, int length);
485 void GoToLine(int lineNo);
487 virtual void CopyToClipboard(const SelectionText &selectedText) = 0;
488 char *CopyRange(int start, int end);
489 std::string RangeText(int start, int end) const;
490 void CopySelectionRange(SelectionText *ss, bool allowLineCopy=false);
491 void CopyRangeToClipboard(int start, int end);
492 void CopyText(int length, const char *text);
493 void SetDragPosition(SelectionPosition newPos);
494 virtual void DisplayCursor(Window::Cursor c);
495 virtual bool DragThreshold(Point ptStart, Point ptNow);
496 virtual void StartDrag();
497 void DropAt(SelectionPosition position, const char *value, bool moving, bool rectangular);
498 /** PositionInSelection returns true if position in selection. */
499 bool PositionInSelection(int pos);
500 bool PointInSelection(Point pt);
501 bool PointInSelMargin(Point pt);
502 Window::Cursor GetMarginCursor(Point pt);
503 void TrimAndSetSelection(int currentPos_, int anchor_);
504 void LineSelection(int lineCurrentPos_, int lineAnchorPos_, bool wholeLine);
505 void WordSelection(int pos);
506 void DwellEnd(bool mouseMoved);
507 void MouseLeave();
508 virtual void ButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt);
509 void ButtonMove(Point pt);
510 void ButtonUp(Point pt, unsigned int curTime, bool ctrl);
512 void Tick();
513 bool Idle();
514 virtual void SetTicking(bool on) = 0;
515 virtual bool SetIdle(bool) { return false; }
516 virtual void SetMouseCapture(bool on) = 0;
517 virtual bool HaveMouseCapture() = 0;
518 void SetFocusState(bool focusState);
520 int PositionAfterArea(PRectangle rcArea);
521 void StyleToPositionInView(Position pos);
522 void IdleStyling();
523 virtual void QueueStyling(int upTo);
525 virtual bool PaintContains(PRectangle rc);
526 bool PaintContainsMargin();
527 void CheckForChangeOutsidePaint(Range r);
528 void SetBraceHighlight(Position pos0, Position pos1, int matchStyle);
530 void SetAnnotationHeights(int start, int end);
531 void SetDocPointer(Document *document);
533 void SetAnnotationVisible(int visible);
535 void Expand(int &line, bool doExpand);
536 void ToggleContraction(int line);
537 int ContractedFoldNext(int lineStart);
538 void EnsureLineVisible(int lineDoc, bool enforcePolicy);
539 int GetTag(char *tagValue, int tagNumber);
540 int ReplaceTarget(bool replacePatterns, const char *text, int length=-1);
542 bool PositionIsHotspot(int position);
543 bool PointIsHotspot(Point pt);
544 void SetHotSpotRange(Point *pt);
545 void GetHotSpotRange(int &hsStart, int &hsEnd);
547 int CodePage() const;
548 virtual bool ValidCodePage(int /* codePage */) const { return true; }
549 int WrapCount(int line);
550 void AddStyledText(char *buffer, int appendLength);
552 virtual sptr_t DefWndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) = 0;
553 void StyleSetMessage(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
554 sptr_t StyleGetMessage(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
556 static const char *StringFromEOLMode(int eolMode);
558 static sptr_t StringResult(sptr_t lParam, const char *val);
560 public:
561 // Public so the COM thunks can access it.
562 bool IsUnicodeMode() const;
563 // Public so scintilla_send_message can use it.
564 virtual sptr_t WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
565 // Public so scintilla_set_id can use it.
566 int ctrlID;
567 // Public so COM methods for drag and drop can set it.
568 int errorStatus;
569 friend class AutoSurface;
570 friend class SelectionLineIterator;
574 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
576 class AutoSurface {
577 private:
578 Surface *surf;
579 public:
580 AutoSurface(Editor *ed, int technology = -1) : surf(0) {
581 if (ed->wMain.GetID()) {
582 surf = Surface::Allocate(technology != -1 ? technology : ed->technology);
583 if (surf) {
584 surf->Init(ed->wMain.GetID());
585 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
586 surf->SetDBCSMode(ed->CodePage());
590 AutoSurface(SurfaceID sid, Editor *ed, int technology = -1) : surf(0) {
591 if (ed->wMain.GetID()) {
592 surf = Surface::Allocate(technology != -1 ? technology : ed->technology);
593 if (surf) {
594 surf->Init(sid, ed->wMain.GetID());
595 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
596 surf->SetDBCSMode(ed->CodePage());
600 ~AutoSurface() {
601 delete surf;
603 Surface *operator->() const {
604 return surf;
606 operator Surface *() const {
607 return surf;
611 #ifdef SCI_NAMESPACE
613 #endif
615 #endif