Upgraded to scintilla 3.2.3
[TortoiseGit.git] / ext / scintilla / src / Editor.h
blob8d300d6aa9cd7d571c749b7d45377affbf18ea26
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 NotifyStyleToNeeded(int endStyleNeeded);
435 void NotifyChar(int ch);
436 void NotifySavePoint(bool isSavePoint);
437 void NotifyModifyAttempt();
438 virtual void NotifyDoubleClick(Point pt, bool shift, bool ctrl, bool alt);
439 void NotifyHotSpotClicked(int position, bool shift, bool ctrl, bool alt);
440 void NotifyHotSpotDoubleClicked(int position, bool shift, bool ctrl, bool alt);
441 void NotifyHotSpotReleaseClick(int position, bool shift, bool ctrl, bool alt);
442 void NotifyUpdateUI();
443 void NotifyPainted();
444 void NotifyIndicatorClick(bool click, int position, bool shift, bool ctrl, bool alt);
445 bool NotifyMarginClick(Point pt, bool shift, bool ctrl, bool alt);
446 void NotifyNeedShown(int pos, int len);
447 void NotifyDwelling(Point pt, bool state);
448 void NotifyZoom();
450 void NotifyModifyAttempt(Document *document, void *userData);
451 void NotifySavePoint(Document *document, void *userData, bool atSavePoint);
452 void CheckModificationForWrap(DocModification mh);
453 void NotifyModified(Document *document, DocModification mh, void *userData);
454 void NotifyDeleted(Document *document, void *userData);
455 void NotifyStyleNeeded(Document *doc, void *userData, int endPos);
456 void NotifyLexerChanged(Document *doc, void *userData);
457 void NotifyErrorOccurred(Document *doc, void *userData, int status);
458 void NotifyMacroRecord(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
460 void ContainerNeedsUpdate(int flags);
461 void PageMove(int direction, Selection::selTypes sel=Selection::noSel, bool stuttered = false);
462 enum { cmSame, cmUpper, cmLower } caseMap;
463 virtual std::string CaseMapString(const std::string &s, int caseMapping);
464 void ChangeCaseOfSelection(int caseMapping);
465 void LineTranspose();
466 void Duplicate(bool forLine);
467 virtual void CancelModes();
468 void NewLine();
469 void CursorUpOrDown(int direction, Selection::selTypes sel=Selection::noSel);
470 void ParaUpOrDown(int direction, Selection::selTypes sel=Selection::noSel);
471 int StartEndDisplayLine(int pos, bool start);
472 virtual int KeyCommand(unsigned int iMessage);
473 virtual int KeyDefault(int /* key */, int /*modifiers*/);
474 int KeyDownWithModifiers(int key, int modifiers, bool *consumed);
475 int KeyDown(int key, bool shift, bool ctrl, bool alt, bool *consumed=0);
477 void Indent(bool forwards);
479 virtual CaseFolder *CaseFolderForEncoding();
480 long FindText(uptr_t wParam, sptr_t lParam);
481 void SearchAnchor();
482 long SearchText(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
483 long SearchInTarget(const char *text, int length);
484 void GoToLine(int lineNo);
486 virtual void CopyToClipboard(const SelectionText &selectedText) = 0;
487 char *CopyRange(int start, int end);
488 std::string RangeText(int start, int end) const;
489 void CopySelectionRange(SelectionText *ss, bool allowLineCopy=false);
490 void CopyRangeToClipboard(int start, int end);
491 void CopyText(int length, const char *text);
492 void SetDragPosition(SelectionPosition newPos);
493 virtual void DisplayCursor(Window::Cursor c);
494 virtual bool DragThreshold(Point ptStart, Point ptNow);
495 virtual void StartDrag();
496 void DropAt(SelectionPosition position, const char *value, bool moving, bool rectangular);
497 /** PositionInSelection returns true if position in selection. */
498 bool PositionInSelection(int pos);
499 bool PointInSelection(Point pt);
500 bool PointInSelMargin(Point pt);
501 Window::Cursor GetMarginCursor(Point pt);
502 void TrimAndSetSelection(int currentPos_, int anchor_);
503 void LineSelection(int lineCurrentPos_, int lineAnchorPos_, bool wholeLine);
504 void WordSelection(int pos);
505 void DwellEnd(bool mouseMoved);
506 void MouseLeave();
507 virtual void ButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt);
508 void ButtonMove(Point pt);
509 void ButtonUp(Point pt, unsigned int curTime, bool ctrl);
511 void Tick();
512 bool Idle();
513 virtual void SetTicking(bool on) = 0;
514 virtual bool SetIdle(bool) { return false; }
515 virtual void SetMouseCapture(bool on) = 0;
516 virtual bool HaveMouseCapture() = 0;
517 void SetFocusState(bool focusState);
519 int PositionAfterArea(PRectangle rcArea);
520 void StyleToPositionInView(Position pos);
521 void IdleStyling();
522 virtual void QueueStyling(int upTo);
524 virtual bool PaintContains(PRectangle rc);
525 bool PaintContainsMargin();
526 void CheckForChangeOutsidePaint(Range r);
527 void SetBraceHighlight(Position pos0, Position pos1, int matchStyle);
529 void SetAnnotationHeights(int start, int end);
530 void SetDocPointer(Document *document);
532 void SetAnnotationVisible(int visible);
534 void Expand(int &line, bool doExpand);
535 void ToggleContraction(int line);
536 int ContractedFoldNext(int lineStart);
537 void EnsureLineVisible(int lineDoc, bool enforcePolicy);
538 int GetTag(char *tagValue, int tagNumber);
539 int ReplaceTarget(bool replacePatterns, const char *text, int length=-1);
541 bool PositionIsHotspot(int position);
542 bool PointIsHotspot(Point pt);
543 void SetHotSpotRange(Point *pt);
544 void GetHotSpotRange(int &hsStart, int &hsEnd);
546 int CodePage() const;
547 virtual bool ValidCodePage(int /* codePage */) const { return true; }
548 int WrapCount(int line);
549 void AddStyledText(char *buffer, int appendLength);
551 virtual sptr_t DefWndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) = 0;
552 void StyleSetMessage(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
553 sptr_t StyleGetMessage(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
555 static const char *StringFromEOLMode(int eolMode);
557 static sptr_t StringResult(sptr_t lParam, const char *val);
559 public:
560 // Public so the COM thunks can access it.
561 bool IsUnicodeMode() const;
562 // Public so scintilla_send_message can use it.
563 virtual sptr_t WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
564 // Public so scintilla_set_id can use it.
565 int ctrlID;
566 // Public so COM methods for drag and drop can set it.
567 int errorStatus;
568 friend class AutoSurface;
569 friend class SelectionLineIterator;
573 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
575 class AutoSurface {
576 private:
577 Surface *surf;
578 public:
579 AutoSurface(Editor *ed, int technology = -1) : surf(0) {
580 if (ed->wMain.GetID()) {
581 surf = Surface::Allocate(technology != -1 ? technology : ed->technology);
582 if (surf) {
583 surf->Init(ed->wMain.GetID());
584 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
585 surf->SetDBCSMode(ed->CodePage());
589 AutoSurface(SurfaceID sid, Editor *ed, int technology = -1) : surf(0) {
590 if (ed->wMain.GetID()) {
591 surf = Surface::Allocate(technology != -1 ? technology : ed->technology);
592 if (surf) {
593 surf->Init(sid, ed->wMain.GetID());
594 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
595 surf->SetDBCSMode(ed->CodePage());
599 ~AutoSurface() {
600 delete surf;
602 Surface *operator->() const {
603 return surf;
605 operator Surface *() const {
606 return surf;
610 #ifdef SCI_NAMESPACE
612 #endif
614 #endif