Update Scintilla to 3.5.1 pre-release
[geany-mirror.git] / scintilla / src / Editor.h
bloba1f39e7c8c11edfeac9e4258256ee18ac40a0cf4
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 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 int dwellDelay;
207 int ticksToDwell;
208 bool dwelling;
209 enum { selChar, selWord, selSubLine, selWholeLine } selectionType;
210 Point ptMouseLast;
211 enum { ddNone, ddInitial, ddDragging } inDragDrop;
212 bool dropWentOutside;
213 SelectionPosition posDrop;
214 int hotSpotClickPos;
215 int lastXChosen;
216 int lineAnchorPos;
217 int originalAnchorPos;
218 int wordSelectAnchorStartPos;
219 int wordSelectAnchorEndPos;
220 int wordSelectInitialCaretPos;
221 int targetStart;
222 int targetEnd;
223 int searchFlags;
224 int topLine;
225 int posTopLine;
226 int lengthForEncode;
228 int needUpdateUI;
230 enum { notPainting, painting, paintAbandoned } paintState;
231 bool paintAbandonedByStyling;
232 PRectangle rcPaint;
233 bool paintingAllText;
234 bool willRedrawAll;
235 WorkNeeded workNeeded;
237 int modEventMask;
239 SelectionText drag;
241 int caretXPolicy;
242 int caretXSlop; ///< Ensure this many pixels visible on both sides of caret
244 int caretYPolicy;
245 int caretYSlop; ///< Ensure this many lines visible on both sides of caret
247 int visiblePolicy;
248 int visibleSlop;
250 int searchAnchor;
252 bool recordingMacro;
254 int foldAutomatic;
256 // Wrapping support
257 WrapPending wrapPending;
259 bool convertPastes;
261 Editor();
262 virtual ~Editor();
263 virtual void Initialise() = 0;
264 virtual void Finalise();
266 void InvalidateStyleData();
267 void InvalidateStyleRedraw();
268 void RefreshStyleData();
269 void SetRepresentations();
270 void DropGraphics(bool freeObjects);
271 void AllocateGraphics();
273 // The top left visible point in main window coordinates. Will be 0,0 except for
274 // scroll views where it will be equivalent to the current scroll position.
275 virtual Point GetVisibleOriginInMain() const;
276 Point DocumentPointFromView(Point ptView) const; // Convert a point from view space to document
277 int TopLineOfMain() const; // Return the line at Main's y coordinate 0
278 virtual PRectangle GetClientRectangle() const;
279 virtual PRectangle GetClientDrawingRectangle();
280 PRectangle GetTextRectangle() const;
282 virtual int LinesOnScreen() const;
283 int LinesToScroll() const;
284 int MaxScrollPos() const;
285 SelectionPosition ClampPositionIntoDocument(SelectionPosition sp) const;
286 Point LocationFromPosition(SelectionPosition pos);
287 Point LocationFromPosition(int pos);
288 int XFromPosition(int pos);
289 int XFromPosition(SelectionPosition sp);
290 SelectionPosition SPositionFromLocation(Point pt, bool canReturnInvalid=false, bool charPosition=false, bool virtualSpace=true);
291 int PositionFromLocation(Point pt, bool canReturnInvalid = false, bool charPosition = false);
292 SelectionPosition SPositionFromLineX(int lineDoc, int x);
293 int PositionFromLineX(int line, int x);
294 int LineFromLocation(Point pt) const;
295 void SetTopLine(int topLineNew);
297 virtual bool AbandonPaint();
298 virtual void RedrawRect(PRectangle rc);
299 virtual void DiscardOverdraw();
300 virtual void Redraw();
301 void RedrawSelMargin(int line=-1, bool allAfter=false);
302 PRectangle RectangleFromRange(Range r, int overlap);
303 void InvalidateRange(int start, int end);
305 bool UserVirtualSpace() const {
306 return ((virtualSpaceOptions & SCVS_USERACCESSIBLE) != 0);
308 int CurrentPosition() const;
309 bool SelectionEmpty() const;
310 SelectionPosition SelectionStart();
311 SelectionPosition SelectionEnd();
312 void SetRectangularRange();
313 void ThinRectangularRange();
314 void InvalidateSelection(SelectionRange newMain, bool invalidateWholeSelection=false);
315 void SetSelection(SelectionPosition currentPos_, SelectionPosition anchor_);
316 void SetSelection(int currentPos_, int anchor_);
317 void SetSelection(SelectionPosition currentPos_);
318 void SetSelection(int currentPos_);
319 void SetEmptySelection(SelectionPosition currentPos_);
320 void SetEmptySelection(int currentPos_);
321 bool RangeContainsProtected(int start, int end) const;
322 bool SelectionContainsProtected();
323 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true) const;
324 SelectionPosition MovePositionOutsideChar(SelectionPosition pos, int moveDir, bool checkLineEnd=true) const;
325 int MovePositionTo(SelectionPosition newPos, Selection::selTypes selt=Selection::noSel, bool ensureVisible=true);
326 int MovePositionTo(int newPos, Selection::selTypes selt=Selection::noSel, bool ensureVisible=true);
327 SelectionPosition MovePositionSoVisible(SelectionPosition pos, int moveDir);
328 SelectionPosition MovePositionSoVisible(int pos, int moveDir);
329 Point PointMainCaret();
330 void SetLastXChosen();
332 void ScrollTo(int line, bool moveThumb=true);
333 virtual void ScrollText(int linesToMove);
334 void HorizontalScrollTo(int xPos);
335 void VerticalCentreCaret();
336 void MoveSelectedLines(int lineDelta);
337 void MoveSelectedLinesUp();
338 void MoveSelectedLinesDown();
339 void MoveCaretInsideView(bool ensureVisible=true);
340 int DisplayFromPosition(int pos);
342 struct XYScrollPosition {
343 int xOffset;
344 int topLine;
345 XYScrollPosition(int xOffset_, int topLine_) : xOffset(xOffset_), topLine(topLine_) {}
346 bool operator==(const XYScrollPosition &other) const {
347 return (xOffset == other.xOffset) && (topLine == other.topLine);
350 enum XYScrollOptions {
351 xysUseMargin=0x1,
352 xysVertical=0x2,
353 xysHorizontal=0x4,
354 xysDefault=xysUseMargin|xysVertical|xysHorizontal};
355 XYScrollPosition XYScrollToMakeVisible(const SelectionRange &range, const XYScrollOptions options);
356 void SetXYScroll(XYScrollPosition newXY);
357 void EnsureCaretVisible(bool useMargin=true, bool vert=true, bool horiz=true);
358 void ScrollRange(SelectionRange range);
359 void ShowCaretAtCurrentPosition();
360 void DropCaret();
361 void CaretSetPeriod(int period);
362 void InvalidateCaret();
363 virtual void UpdateSystemCaret();
365 bool Wrapping() const;
366 void NeedWrapping(int docLineStart=0, int docLineEnd=WrapPending::lineLarge);
367 bool WrapOneLine(Surface *surface, int lineToWrap);
368 enum wrapScope {wsAll, wsVisible, wsIdle};
369 bool WrapLines(enum wrapScope ws);
370 void LinesJoin();
371 void LinesSplit(int pixelWidth);
373 void PaintSelMargin(Surface *surface, PRectangle &rc);
374 void RefreshPixMaps(Surface *surfaceWindow);
375 void Paint(Surface *surfaceWindow, PRectangle rcArea);
376 long FormatRange(bool draw, Sci_RangeToFormat *pfr);
377 int TextWidth(int style, const char *text);
379 virtual void SetVerticalScrollPos() = 0;
380 virtual void SetHorizontalScrollPos() = 0;
381 virtual bool ModifyScrollBars(int nMax, int nPage) = 0;
382 virtual void ReconfigureScrollBars();
383 void SetScrollBars();
384 void ChangeSize();
386 void FilterSelections();
387 int InsertSpace(int position, unsigned int spaces);
388 void AddChar(char ch);
389 virtual void AddCharUTF(const char *s, unsigned int len, bool treatAsDBCS=false);
390 void InsertPaste(const char *text, int len);
391 enum PasteShape { pasteStream=0, pasteRectangular = 1, pasteLine = 2 };
392 void InsertPasteShape(const char *text, int len, PasteShape shape);
393 void ClearSelection(bool retainMultipleSelections = false);
394 void ClearAll();
395 void ClearDocumentStyle();
396 void Cut();
397 void PasteRectangular(SelectionPosition pos, const char *ptr, int len);
398 virtual void Copy() = 0;
399 virtual void CopyAllowLine();
400 virtual bool CanPaste();
401 virtual void Paste() = 0;
402 void Clear();
403 void SelectAll();
404 void Undo();
405 void Redo();
406 void DelCharBack(bool allowLineStartDeletion);
407 virtual void ClaimSelection() = 0;
409 static int ModifierFlags(bool shift, bool ctrl, bool alt, bool meta=false);
410 virtual void NotifyChange() = 0;
411 virtual void NotifyFocus(bool focus);
412 virtual void SetCtrlID(int identifier);
413 virtual int GetCtrlID() { return ctrlID; }
414 virtual void NotifyParent(SCNotification scn) = 0;
415 virtual void NotifyStyleToNeeded(int endStyleNeeded);
416 void NotifyChar(int ch);
417 void NotifySavePoint(bool isSavePoint);
418 void NotifyModifyAttempt();
419 virtual void NotifyDoubleClick(Point pt, int modifiers);
420 virtual void NotifyDoubleClick(Point pt, bool shift, bool ctrl, bool alt);
421 void NotifyHotSpotClicked(int position, int modifiers);
422 void NotifyHotSpotClicked(int position, bool shift, bool ctrl, bool alt);
423 void NotifyHotSpotDoubleClicked(int position, int modifiers);
424 void NotifyHotSpotDoubleClicked(int position, bool shift, bool ctrl, bool alt);
425 void NotifyHotSpotReleaseClick(int position, int modifiers);
426 void NotifyHotSpotReleaseClick(int position, bool shift, bool ctrl, bool alt);
427 bool NotifyUpdateUI();
428 void NotifyPainted();
429 void NotifyIndicatorClick(bool click, int position, int modifiers);
430 void NotifyIndicatorClick(bool click, int position, bool shift, bool ctrl, bool alt);
431 bool NotifyMarginClick(Point pt, int modifiers);
432 bool NotifyMarginClick(Point pt, bool shift, bool ctrl, bool alt);
433 void NotifyNeedShown(int pos, int len);
434 void NotifyDwelling(Point pt, bool state);
435 void NotifyZoom();
437 void NotifyModifyAttempt(Document *document, void *userData);
438 void NotifySavePoint(Document *document, void *userData, bool atSavePoint);
439 void CheckModificationForWrap(DocModification mh);
440 void NotifyModified(Document *document, DocModification mh, void *userData);
441 void NotifyDeleted(Document *document, void *userData);
442 void NotifyStyleNeeded(Document *doc, void *userData, int endPos);
443 void NotifyLexerChanged(Document *doc, void *userData);
444 void NotifyErrorOccurred(Document *doc, void *userData, int status);
445 void NotifyMacroRecord(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
447 void ContainerNeedsUpdate(int flags);
448 void PageMove(int direction, Selection::selTypes selt=Selection::noSel, bool stuttered = false);
449 enum { cmSame, cmUpper, cmLower };
450 virtual std::string CaseMapString(const std::string &s, int caseMapping);
451 void ChangeCaseOfSelection(int caseMapping);
452 void LineTranspose();
453 void Duplicate(bool forLine);
454 virtual void CancelModes();
455 void NewLine();
456 void CursorUpOrDown(int direction, Selection::selTypes selt=Selection::noSel);
457 void ParaUpOrDown(int direction, Selection::selTypes selt=Selection::noSel);
458 int StartEndDisplayLine(int pos, bool start);
459 virtual int KeyCommand(unsigned int iMessage);
460 virtual int KeyDefault(int /* key */, int /*modifiers*/);
461 int KeyDownWithModifiers(int key, int modifiers, bool *consumed);
462 int KeyDown(int key, bool shift, bool ctrl, bool alt, bool *consumed=0);
464 void Indent(bool forwards);
466 virtual CaseFolder *CaseFolderForEncoding();
467 long FindText(uptr_t wParam, sptr_t lParam);
468 void SearchAnchor();
469 long SearchText(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
470 long SearchInTarget(const char *text, int length);
471 void GoToLine(int lineNo);
473 virtual void CopyToClipboard(const SelectionText &selectedText) = 0;
474 std::string RangeText(int start, int end) const;
475 void CopySelectionRange(SelectionText *ss, bool allowLineCopy=false);
476 void CopyRangeToClipboard(int start, int end);
477 void CopyText(int length, const char *text);
478 void SetDragPosition(SelectionPosition newPos);
479 virtual void DisplayCursor(Window::Cursor c);
480 virtual bool DragThreshold(Point ptStart, Point ptNow);
481 virtual void StartDrag();
482 void DropAt(SelectionPosition position, const char *value, size_t lengthValue, bool moving, bool rectangular);
483 void DropAt(SelectionPosition position, const char *value, bool moving, bool rectangular);
484 /** PositionInSelection returns true if position in selection. */
485 bool PositionInSelection(int pos);
486 bool PointInSelection(Point pt);
487 bool PointInSelMargin(Point pt) const;
488 Window::Cursor GetMarginCursor(Point pt) const;
489 void TrimAndSetSelection(int currentPos_, int anchor_);
490 void LineSelection(int lineCurrentPos_, int lineAnchorPos_, bool wholeLine);
491 void WordSelection(int pos);
492 void DwellEnd(bool mouseMoved);
493 void MouseLeave();
494 virtual void ButtonDownWithModifiers(Point pt, unsigned int curTime, int modifiers);
495 virtual void ButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt);
496 void ButtonMoveWithModifiers(Point pt, int modifiers);
497 void ButtonMove(Point pt);
498 void ButtonUp(Point pt, unsigned int curTime, bool ctrl);
500 void Tick();
501 bool Idle();
502 virtual void SetTicking(bool on);
503 enum TickReason { tickCaret, tickScroll, tickWiden, tickDwell, tickPlatform };
504 virtual void TickFor(TickReason reason);
505 virtual bool FineTickerAvailable();
506 virtual bool FineTickerRunning(TickReason reason);
507 virtual void FineTickerStart(TickReason reason, int millis, int tolerance);
508 virtual void FineTickerCancel(TickReason reason);
509 virtual bool SetIdle(bool) { return false; }
510 virtual void SetMouseCapture(bool on) = 0;
511 virtual bool HaveMouseCapture() = 0;
512 void SetFocusState(bool focusState);
514 int PositionAfterArea(PRectangle rcArea) const;
515 void StyleToPositionInView(Position pos);
516 virtual void IdleWork();
517 virtual void QueueIdleWork(WorkNeeded::workItems items, int upTo=0);
519 virtual bool PaintContains(PRectangle rc);
520 bool PaintContainsMargin();
521 void CheckForChangeOutsidePaint(Range r);
522 void SetBraceHighlight(Position pos0, Position pos1, int matchStyle);
524 void SetAnnotationHeights(int start, int end);
525 virtual void SetDocPointer(Document *document);
527 void SetAnnotationVisible(int visible);
529 int ExpandLine(int line);
530 void SetFoldExpanded(int lineDoc, bool expanded);
531 void FoldLine(int line, int action);
532 void FoldExpand(int line, int action, int level);
533 int ContractedFoldNext(int lineStart) const;
534 void EnsureLineVisible(int lineDoc, bool enforcePolicy);
535 void FoldChanged(int line, int levelNow, int levelPrev);
536 void NeedShown(int pos, int len);
537 void FoldAll(int action);
539 int GetTag(char *tagValue, int tagNumber);
540 int ReplaceTarget(bool replacePatterns, const char *text, int length=-1);
542 bool PositionIsHotspot(int position) const;
543 bool PointIsHotspot(Point pt);
544 void SetHotSpotRange(Point *pt);
545 Range GetHotSpotRange() const;
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);
559 static sptr_t BytesResult(sptr_t lParam, const unsigned char *val, size_t len);
561 public:
562 // Public so the COM thunks can access it.
563 bool IsUnicodeMode() const;
564 // Public so scintilla_send_message can use it.
565 virtual sptr_t WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
566 // Public so scintilla_set_id can use it.
567 int ctrlID;
568 // Public so COM methods for drag and drop can set it.
569 int errorStatus;
570 friend class AutoSurface;
571 friend class SelectionLineIterator;
575 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
577 class AutoSurface {
578 private:
579 Surface *surf;
580 public:
581 AutoSurface(Editor *ed, int technology = -1) : surf(0) {
582 if (ed->wMain.GetID()) {
583 surf = Surface::Allocate(technology != -1 ? technology : ed->technology);
584 if (surf) {
585 surf->Init(ed->wMain.GetID());
586 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
587 surf->SetDBCSMode(ed->CodePage());
591 AutoSurface(SurfaceID sid, Editor *ed, int technology = -1) : surf(0) {
592 if (ed->wMain.GetID()) {
593 surf = Surface::Allocate(technology != -1 ? technology : ed->technology);
594 if (surf) {
595 surf->Init(sid, ed->wMain.GetID());
596 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
597 surf->SetDBCSMode(ed->CodePage());
601 ~AutoSurface() {
602 delete surf;
604 Surface *operator->() const {
605 return surf;
607 operator Surface *() const {
608 return surf;
612 #ifdef SCI_NAMESPACE
614 #endif
616 #endif