Update Scintilla to version 3.5.7
[TortoiseGit.git] / ext / scintilla / src / Editor.h
blob909c82acead348e7069a8b5ccb8b545ea6253fb8
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 Timer {
18 public:
19 bool ticking;
20 int ticksToWait;
21 enum {tickSize = 100};
22 TickerID tickerID;
24 Timer();
27 /**
29 class Idler {
30 public:
31 bool state;
32 IdlerID idlerID;
34 Idler();
37 /**
38 * When platform has a way to generate an event before painting,
39 * accumulate needed styling range and other work items in
40 * WorkNeeded to avoid unnecessary work inside paint handler
42 class WorkNeeded {
43 public:
44 enum workItems {
45 workNone=0,
46 workStyle=1,
47 workUpdateUI=2
49 bool active;
50 enum workItems items;
51 Position upTo;
53 WorkNeeded() : active(false), items(workNone), upTo(0) {}
54 void Reset() {
55 active = false;
56 items = workNone;
57 upTo = 0;
59 void Need(workItems items_, Position pos) {
60 if ((items_ & workStyle) && (upTo < pos))
61 upTo = pos;
62 items = static_cast<workItems>(items | items_);
66 /**
67 * Hold a piece of text selected for copying or dragging, along with encoding and selection format information.
69 class SelectionText {
70 std::string s;
71 public:
72 bool rectangular;
73 bool lineCopy;
74 int codePage;
75 int characterSet;
76 SelectionText() : rectangular(false), lineCopy(false), codePage(0), characterSet(0) {}
77 ~SelectionText() {
79 void Clear() {
80 s.clear();
81 rectangular = false;
82 lineCopy = false;
83 codePage = 0;
84 characterSet = 0;
86 void Copy(const std::string &s_, int codePage_, int characterSet_, bool rectangular_, bool lineCopy_) {
87 s = s_;
88 codePage = codePage_;
89 characterSet = characterSet_;
90 rectangular = rectangular_;
91 lineCopy = lineCopy_;
92 FixSelectionForClipboard();
94 void Copy(const SelectionText &other) {
95 Copy(other.s, other.codePage, other.characterSet, other.rectangular, other.lineCopy);
97 const char *Data() const {
98 return s.c_str();
100 size_t Length() const {
101 return s.length();
103 size_t LengthWithTerminator() const {
104 return s.length() + 1;
106 bool Empty() const {
107 return s.empty();
109 private:
110 void FixSelectionForClipboard() {
111 // To avoid truncating the contents of the clipboard when pasted where the
112 // clipboard contains NUL characters, replace NUL characters by spaces.
113 std::replace(s.begin(), s.end(), '\0', ' ');
117 struct WrapPending {
118 // The range of lines that need to be wrapped
119 enum { lineLarge = 0x7ffffff };
120 int start; // When there are wraps pending, will be in document range
121 int end; // May be lineLarge to indicate all of document after start
122 WrapPending() {
123 start = lineLarge;
124 end = lineLarge;
126 void Reset() {
127 start = lineLarge;
128 end = lineLarge;
130 void Wrapped(int line) {
131 if (start == line)
132 start++;
134 bool NeedsWrap() const {
135 return start < end;
137 bool AddRange(int lineStart, int lineEnd) {
138 const bool neededWrap = NeedsWrap();
139 bool changed = false;
140 if (start > lineStart) {
141 start = lineStart;
142 changed = true;
144 if ((end < lineEnd) || !neededWrap) {
145 end = lineEnd;
146 changed = true;
148 return changed;
154 class Editor : public EditModel, public DocWatcher {
155 // Private so Editor objects can not be copied
156 explicit Editor(const Editor &);
157 Editor &operator=(const Editor &);
159 protected: // ScintillaBase subclass needs access to much of Editor
161 /** On GTK+, Scintilla is a container widget holding two scroll bars
162 * whereas on Windows there is just one window with both scroll bars turned on. */
163 Window wMain; ///< The Scintilla parent window
164 Window wMargin; ///< May be separate when using a scroll view for wMain
166 /** Style resources may be expensive to allocate so are cached between uses.
167 * When a style attribute is changed, this cache is flushed. */
168 bool stylesValid;
169 ViewStyle vs;
170 int technology;
171 Point sizeRGBAImage;
172 float scaleRGBAImage;
174 MarginView marginView;
175 EditView view;
177 int cursorMode;
179 bool hasFocus;
180 bool mouseDownCaptures;
182 int xCaretMargin; ///< Ensure this many pixels visible on both sides of caret
183 bool horizontalScrollBarVisible;
184 int scrollWidth;
185 bool verticalScrollBarVisible;
186 bool endAtLastLine;
187 int caretSticky;
188 int marginOptions;
189 bool mouseSelectionRectangularSwitch;
190 bool multipleSelection;
191 bool additionalSelectionTyping;
192 int multiPasteMode;
194 int virtualSpaceOptions;
196 KeyMap kmap;
198 Timer timer;
199 Timer autoScrollTimer;
200 enum { autoScrollDelay = 200 };
202 Idler idler;
204 Point lastClick;
205 unsigned int lastClickTime;
206 Point doubleClickCloseThreshold;
207 int dwellDelay;
208 int ticksToDwell;
209 bool dwelling;
210 enum { selChar, selWord, selSubLine, selWholeLine } selectionType;
211 Point ptMouseLast;
212 enum { ddNone, ddInitial, ddDragging } inDragDrop;
213 bool dropWentOutside;
214 SelectionPosition posDrop;
215 int hotSpotClickPos;
216 int lastXChosen;
217 int lineAnchorPos;
218 int originalAnchorPos;
219 int wordSelectAnchorStartPos;
220 int wordSelectAnchorEndPos;
221 int wordSelectInitialCaretPos;
222 int targetStart;
223 int targetEnd;
224 int searchFlags;
225 int topLine;
226 int posTopLine;
227 int lengthForEncode;
229 int needUpdateUI;
231 enum { notPainting, painting, paintAbandoned } paintState;
232 bool paintAbandonedByStyling;
233 PRectangle rcPaint;
234 bool paintingAllText;
235 bool willRedrawAll;
236 WorkNeeded workNeeded;
238 int modEventMask;
240 SelectionText drag;
242 int caretXPolicy;
243 int caretXSlop; ///< Ensure this many pixels visible on both sides of caret
245 int caretYPolicy;
246 int caretYSlop; ///< Ensure this many lines visible on both sides of caret
248 int visiblePolicy;
249 int visibleSlop;
251 int searchAnchor;
253 bool recordingMacro;
255 int foldAutomatic;
257 // Wrapping support
258 WrapPending wrapPending;
260 bool convertPastes;
262 Editor();
263 virtual ~Editor();
264 virtual void Initialise() = 0;
265 virtual void Finalise();
267 void InvalidateStyleData();
268 void InvalidateStyleRedraw();
269 void RefreshStyleData();
270 void SetRepresentations();
271 void DropGraphics(bool freeObjects);
272 void AllocateGraphics();
274 // The top left visible point in main window coordinates. Will be 0,0 except for
275 // scroll views where it will be equivalent to the current scroll position.
276 virtual Point GetVisibleOriginInMain() const;
277 Point DocumentPointFromView(Point ptView) const; // Convert a point from view space to document
278 int TopLineOfMain() const; // Return the line at Main's y coordinate 0
279 virtual PRectangle GetClientRectangle() const;
280 virtual PRectangle GetClientDrawingRectangle();
281 PRectangle GetTextRectangle() const;
283 virtual int LinesOnScreen() const;
284 int LinesToScroll() const;
285 int MaxScrollPos() const;
286 SelectionPosition ClampPositionIntoDocument(SelectionPosition sp) const;
287 Point LocationFromPosition(SelectionPosition pos);
288 Point LocationFromPosition(int pos);
289 int XFromPosition(int pos);
290 int XFromPosition(SelectionPosition sp);
291 SelectionPosition SPositionFromLocation(Point pt, bool canReturnInvalid=false, bool charPosition=false, bool virtualSpace=true);
292 int PositionFromLocation(Point pt, bool canReturnInvalid = false, bool charPosition = false);
293 SelectionPosition SPositionFromLineX(int lineDoc, int x);
294 int PositionFromLineX(int line, int x);
295 int LineFromLocation(Point pt) const;
296 void SetTopLine(int topLineNew);
298 virtual bool AbandonPaint();
299 virtual void RedrawRect(PRectangle rc);
300 virtual void DiscardOverdraw();
301 virtual void Redraw();
302 void RedrawSelMargin(int line=-1, bool allAfter=false);
303 PRectangle RectangleFromRange(Range r, int overlap);
304 void InvalidateRange(int start, int end);
306 bool UserVirtualSpace() const {
307 return ((virtualSpaceOptions & SCVS_USERACCESSIBLE) != 0);
309 int CurrentPosition() const;
310 bool SelectionEmpty() const;
311 SelectionPosition SelectionStart();
312 SelectionPosition SelectionEnd();
313 void SetRectangularRange();
314 void ThinRectangularRange();
315 void InvalidateSelection(SelectionRange newMain, bool invalidateWholeSelection=false);
316 void InvalidateWholeSelection();
317 void SetSelection(SelectionPosition currentPos_, SelectionPosition anchor_);
318 void SetSelection(int currentPos_, int anchor_);
319 void SetSelection(SelectionPosition currentPos_);
320 void SetSelection(int currentPos_);
321 void SetEmptySelection(SelectionPosition currentPos_);
322 void SetEmptySelection(int currentPos_);
323 enum AddNumber { addOne, addEach };
324 void MultipleSelectAdd(AddNumber addNumber);
325 bool RangeContainsProtected(int start, int end) const;
326 bool SelectionContainsProtected();
327 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true) const;
328 SelectionPosition MovePositionOutsideChar(SelectionPosition pos, int moveDir, bool checkLineEnd=true) const;
329 void MovePositionTo(SelectionPosition newPos, Selection::selTypes selt=Selection::noSel, bool ensureVisible=true);
330 void MovePositionTo(int newPos, Selection::selTypes selt=Selection::noSel, bool ensureVisible=true);
331 SelectionPosition MovePositionSoVisible(SelectionPosition pos, int moveDir);
332 SelectionPosition MovePositionSoVisible(int pos, int moveDir);
333 Point PointMainCaret();
334 void SetLastXChosen();
336 void ScrollTo(int line, bool moveThumb=true);
337 virtual void ScrollText(int linesToMove);
338 void HorizontalScrollTo(int xPos);
339 void VerticalCentreCaret();
340 void MoveSelectedLines(int lineDelta);
341 void MoveSelectedLinesUp();
342 void MoveSelectedLinesDown();
343 void MoveCaretInsideView(bool ensureVisible=true);
344 int DisplayFromPosition(int pos);
346 struct XYScrollPosition {
347 int xOffset;
348 int topLine;
349 XYScrollPosition(int xOffset_, int topLine_) : xOffset(xOffset_), topLine(topLine_) {}
350 bool operator==(const XYScrollPosition &other) const {
351 return (xOffset == other.xOffset) && (topLine == other.topLine);
354 enum XYScrollOptions {
355 xysUseMargin=0x1,
356 xysVertical=0x2,
357 xysHorizontal=0x4,
358 xysDefault=xysUseMargin|xysVertical|xysHorizontal};
359 XYScrollPosition XYScrollToMakeVisible(const SelectionRange &range, const XYScrollOptions options);
360 void SetXYScroll(XYScrollPosition newXY);
361 void EnsureCaretVisible(bool useMargin=true, bool vert=true, bool horiz=true);
362 void ScrollRange(SelectionRange range);
363 void ShowCaretAtCurrentPosition();
364 void DropCaret();
365 void CaretSetPeriod(int period);
366 void InvalidateCaret();
367 virtual void UpdateSystemCaret();
369 bool Wrapping() const;
370 void NeedWrapping(int docLineStart=0, int docLineEnd=WrapPending::lineLarge);
371 bool WrapOneLine(Surface *surface, int lineToWrap);
372 enum wrapScope {wsAll, wsVisible, wsIdle};
373 bool WrapLines(enum wrapScope ws);
374 void LinesJoin();
375 void LinesSplit(int pixelWidth);
377 void PaintSelMargin(Surface *surface, PRectangle &rc);
378 void RefreshPixMaps(Surface *surfaceWindow);
379 void Paint(Surface *surfaceWindow, PRectangle rcArea);
380 long FormatRange(bool draw, Sci_RangeToFormat *pfr);
381 int TextWidth(int style, const char *text);
383 virtual void SetVerticalScrollPos() = 0;
384 virtual void SetHorizontalScrollPos() = 0;
385 virtual bool ModifyScrollBars(int nMax, int nPage) = 0;
386 virtual void ReconfigureScrollBars();
387 void SetScrollBars();
388 void ChangeSize();
390 void FilterSelections();
391 int InsertSpace(int position, unsigned int spaces);
392 void AddChar(char ch);
393 virtual void AddCharUTF(const char *s, unsigned int len, bool treatAsDBCS=false);
394 void FillVirtualSpace();
395 void InsertPaste(const char *text, int len);
396 enum PasteShape { pasteStream=0, pasteRectangular = 1, pasteLine = 2 };
397 void InsertPasteShape(const char *text, int len, PasteShape shape);
398 void ClearSelection(bool retainMultipleSelections = false);
399 void ClearAll();
400 void ClearDocumentStyle();
401 void Cut();
402 void PasteRectangular(SelectionPosition pos, const char *ptr, int len);
403 virtual void Copy() = 0;
404 virtual void CopyAllowLine();
405 virtual bool CanPaste();
406 virtual void Paste() = 0;
407 void Clear();
408 void SelectAll();
409 void Undo();
410 void Redo();
411 void DelCharBack(bool allowLineStartDeletion);
412 virtual void ClaimSelection() = 0;
414 static int ModifierFlags(bool shift, bool ctrl, bool alt, bool meta=false);
415 virtual void NotifyChange() = 0;
416 virtual void NotifyFocus(bool focus);
417 virtual void SetCtrlID(int identifier);
418 virtual int GetCtrlID() { return ctrlID; }
419 virtual void NotifyParent(SCNotification scn) = 0;
420 virtual void NotifyStyleToNeeded(int endStyleNeeded);
421 void NotifyChar(int ch);
422 void NotifySavePoint(bool isSavePoint);
423 void NotifyModifyAttempt();
424 virtual void NotifyDoubleClick(Point pt, int modifiers);
425 virtual void NotifyDoubleClick(Point pt, bool shift, bool ctrl, bool alt);
426 void NotifyHotSpotClicked(int position, int modifiers);
427 void NotifyHotSpotClicked(int position, bool shift, bool ctrl, bool alt);
428 void NotifyHotSpotDoubleClicked(int position, int modifiers);
429 void NotifyHotSpotDoubleClicked(int position, bool shift, bool ctrl, bool alt);
430 void NotifyHotSpotReleaseClick(int position, int modifiers);
431 void NotifyHotSpotReleaseClick(int position, bool shift, bool ctrl, bool alt);
432 bool NotifyUpdateUI();
433 void NotifyPainted();
434 void NotifyIndicatorClick(bool click, int position, int modifiers);
435 void NotifyIndicatorClick(bool click, int position, bool shift, bool ctrl, bool alt);
436 bool NotifyMarginClick(Point pt, int modifiers);
437 bool NotifyMarginClick(Point pt, bool shift, bool ctrl, bool alt);
438 void NotifyNeedShown(int pos, int len);
439 void NotifyDwelling(Point pt, bool state);
440 void NotifyZoom();
442 void NotifyModifyAttempt(Document *document, void *userData);
443 void NotifySavePoint(Document *document, void *userData, bool atSavePoint);
444 void CheckModificationForWrap(DocModification mh);
445 void NotifyModified(Document *document, DocModification mh, void *userData);
446 void NotifyDeleted(Document *document, void *userData);
447 void NotifyStyleNeeded(Document *doc, void *userData, int endPos);
448 void NotifyLexerChanged(Document *doc, void *userData);
449 void NotifyErrorOccurred(Document *doc, void *userData, int status);
450 void NotifyMacroRecord(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
452 void ContainerNeedsUpdate(int flags);
453 void PageMove(int direction, Selection::selTypes selt=Selection::noSel, bool stuttered = false);
454 enum { cmSame, cmUpper, cmLower };
455 virtual std::string CaseMapString(const std::string &s, int caseMapping);
456 void ChangeCaseOfSelection(int caseMapping);
457 void LineTranspose();
458 void Duplicate(bool forLine);
459 virtual void CancelModes();
460 void NewLine();
461 void CursorUpOrDown(int direction, Selection::selTypes selt);
462 void ParaUpOrDown(int direction, Selection::selTypes selt);
463 int StartEndDisplayLine(int pos, bool start);
464 virtual int KeyCommand(unsigned int iMessage);
465 virtual int KeyDefault(int /* key */, int /*modifiers*/);
466 int KeyDownWithModifiers(int key, int modifiers, bool *consumed);
467 int KeyDown(int key, bool shift, bool ctrl, bool alt, bool *consumed=0);
469 void Indent(bool forwards);
471 virtual CaseFolder *CaseFolderForEncoding();
472 long FindText(uptr_t wParam, sptr_t lParam);
473 void SearchAnchor();
474 long SearchText(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
475 long SearchInTarget(const char *text, int length);
476 void GoToLine(int lineNo);
478 virtual void CopyToClipboard(const SelectionText &selectedText) = 0;
479 std::string RangeText(int start, int end) const;
480 void CopySelectionRange(SelectionText *ss, bool allowLineCopy=false);
481 void CopyRangeToClipboard(int start, int end);
482 void CopyText(int length, const char *text);
483 void SetDragPosition(SelectionPosition newPos);
484 virtual void DisplayCursor(Window::Cursor c);
485 virtual bool DragThreshold(Point ptStart, Point ptNow);
486 virtual void StartDrag();
487 void DropAt(SelectionPosition position, const char *value, size_t lengthValue, bool moving, bool rectangular);
488 void DropAt(SelectionPosition position, const char *value, bool moving, bool rectangular);
489 /** PositionInSelection returns true if position in selection. */
490 bool PositionInSelection(int pos);
491 bool PointInSelection(Point pt);
492 bool PointInSelMargin(Point pt) const;
493 Window::Cursor GetMarginCursor(Point pt) const;
494 void TrimAndSetSelection(int currentPos_, int anchor_);
495 void LineSelection(int lineCurrentPos_, int lineAnchorPos_, bool wholeLine);
496 void WordSelection(int pos);
497 void DwellEnd(bool mouseMoved);
498 void MouseLeave();
499 virtual void ButtonDownWithModifiers(Point pt, unsigned int curTime, int modifiers);
500 virtual void ButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt);
501 void ButtonMoveWithModifiers(Point pt, int modifiers);
502 void ButtonMove(Point pt);
503 void ButtonUp(Point pt, unsigned int curTime, bool ctrl);
505 void Tick();
506 bool Idle();
507 virtual void SetTicking(bool on);
508 enum TickReason { tickCaret, tickScroll, tickWiden, tickDwell, tickPlatform };
509 virtual void TickFor(TickReason reason);
510 virtual bool FineTickerAvailable();
511 virtual bool FineTickerRunning(TickReason reason);
512 virtual void FineTickerStart(TickReason reason, int millis, int tolerance);
513 virtual void FineTickerCancel(TickReason reason);
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) const;
520 void StyleToPositionInView(Position pos);
521 virtual void IdleWork();
522 virtual void QueueIdleWork(WorkNeeded::workItems items, int upTo=0);
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 virtual void SetDocPointer(Document *document);
532 void SetAnnotationVisible(int visible);
534 int ExpandLine(int line);
535 void SetFoldExpanded(int lineDoc, bool expanded);
536 void FoldLine(int line, int action);
537 void FoldExpand(int line, int action, int level);
538 int ContractedFoldNext(int lineStart) const;
539 void EnsureLineVisible(int lineDoc, bool enforcePolicy);
540 void FoldChanged(int line, int levelNow, int levelPrev);
541 void NeedShown(int pos, int len);
542 void FoldAll(int action);
544 int GetTag(char *tagValue, int tagNumber);
545 int ReplaceTarget(bool replacePatterns, const char *text, int length=-1);
547 bool PositionIsHotspot(int position) const;
548 bool PointIsHotspot(Point pt);
549 void SetHotSpotRange(Point *pt);
550 Range GetHotSpotRange() const;
551 void SetHoverIndicatorPosition(int position);
552 void SetHoverIndicatorPoint(Point pt);
554 int CodePage() const;
555 virtual bool ValidCodePage(int /* codePage */) const { return true; }
556 int WrapCount(int line);
557 void AddStyledText(char *buffer, int appendLength);
559 virtual sptr_t DefWndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) = 0;
560 void StyleSetMessage(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
561 sptr_t StyleGetMessage(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
563 static const char *StringFromEOLMode(int eolMode);
565 static sptr_t StringResult(sptr_t lParam, const char *val);
566 static sptr_t BytesResult(sptr_t lParam, const unsigned char *val, size_t len);
568 public:
569 // Public so the COM thunks can access it.
570 bool IsUnicodeMode() const;
571 // Public so scintilla_send_message can use it.
572 virtual sptr_t WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
573 // Public so scintilla_set_id can use it.
574 int ctrlID;
575 // Public so COM methods for drag and drop can set it.
576 int errorStatus;
577 friend class AutoSurface;
578 friend class SelectionLineIterator;
582 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
584 class AutoSurface {
585 private:
586 Surface *surf;
587 public:
588 AutoSurface(Editor *ed, int technology = -1) : surf(0) {
589 if (ed->wMain.GetID()) {
590 surf = Surface::Allocate(technology != -1 ? technology : ed->technology);
591 if (surf) {
592 surf->Init(ed->wMain.GetID());
593 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
594 surf->SetDBCSMode(ed->CodePage());
598 AutoSurface(SurfaceID sid, Editor *ed, int technology = -1) : surf(0) {
599 if (ed->wMain.GetID()) {
600 surf = Surface::Allocate(technology != -1 ? technology : ed->technology);
601 if (surf) {
602 surf->Init(sid, ed->wMain.GetID());
603 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
604 surf->SetDBCSMode(ed->CodePage());
608 ~AutoSurface() {
609 delete surf;
611 Surface *operator->() const {
612 return surf;
614 operator Surface *() const {
615 return surf;
619 #ifdef SCI_NAMESPACE
621 #endif
623 #endif