1 // Scintilla source code edit control
3 ** Defines the main editor class.
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.
32 enum {tickSize
= 100};
49 * When platform has a way to generate an event before painting,
50 * accumulate needed styling range in StyleNeeded to avoid unnecessary work.
57 StyleNeeded() : active(false), upTo(0) {}
62 void NeedUpTo(Position pos
) {
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.
80 SelectionText() : s(0), len(0), rectangular(false), lineCopy(false), codePage(0), characterSet(0) {}
85 Set(0, 0, 0, 0, false, false);
87 void Set(char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
, bool lineCopy_
) {
95 characterSet
= characterSet_
;
96 rectangular
= rectangular_
;
98 FixSelectionForClipboard();
100 void Copy(const char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
, bool lineCopy_
) {
105 for (int i
= 0; i
< len_
; i
++) {
108 codePage
= codePage_
;
109 characterSet
= characterSet_
;
110 rectangular
= rectangular_
;
111 lineCopy
= lineCopy_
;
112 FixSelectionForClipboard();
114 void Copy(const SelectionText
&other
) {
115 Copy(other
.s
, other
.len
, other
.codePage
, other
.characterSet
, other
.rectangular
, other
.lineCopy
);
119 void FixSelectionForClipboard() {
120 // Replace null characters by spaces.
121 // To avoid that the content of the clipboard is truncated in the paste operation
122 // when the clipboard contains null characters.
123 for (int i
= 0; i
< len
- 1; ++i
) {
132 class Editor
: public DocWatcher
{
133 // Private so Editor objects can not be copied
134 Editor(const Editor
&);
135 Editor
&operator=(const Editor
&);
137 protected: // ScintillaBase subclass needs access to much of Editor
139 /** On GTK+, Scintilla is a container widget holding two scroll bars
140 * whereas on Windows there is just one window with both scroll bars turned on. */
141 Window wMain
; ///< The Scintilla parent window
143 /** Style resources may be expensive to allocate so are cached between uses.
144 * When a style attribute is changed, this cache is flushed. */
149 float scaleRGBAImage
;
151 int printMagnification
;
155 int controlCharSymbol
;
157 // Highlight current folding block
158 HighlightDelimiter highlightDelimiter
;
163 bool mouseDownCaptures
;
165 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
166 * the screen. This avoids flashing but is about 30% slower. */
168 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
169 * and then the foreground. This avoids chopping off characters that overlap the next run. */
172 int xOffset
; ///< Horizontal scrolled amount in pixels
173 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
174 bool horizontalScrollBarVisible
;
177 int lineWidthMaxSeen
;
178 bool verticalScrollBarVisible
;
182 bool multipleSelection
;
183 bool additionalSelectionTyping
;
185 bool additionalCaretsBlink
;
186 bool additionalCaretsVisible
;
188 int virtualSpaceOptions
;
191 Surface
*pixmapSelMargin
;
192 Surface
*pixmapSelPattern
;
193 Surface
*pixmapIndentGuide
;
194 Surface
*pixmapIndentGuideHighlight
;
197 PositionCache posCache
;
203 Timer autoScrollTimer
;
204 enum { autoScrollDelay
= 200 };
209 unsigned int lastClickTime
;
213 enum { selChar
, selWord
, selSubLine
, selWholeLine
} selectionType
;
215 enum { ddNone
, ddInitial
, ddDragging
} inDragDrop
;
216 bool dropWentOutside
;
217 SelectionPosition posDrag
;
218 SelectionPosition posDrop
;
222 int originalAnchorPos
;
223 int wordSelectAnchorStartPos
;
224 int wordSelectAnchorEndPos
;
225 int wordSelectInitialCaretPos
;
235 int bracesMatchStyle
;
236 int highlightGuideColumn
;
240 enum { notPainting
, painting
, paintAbandoned
} paintState
;
242 bool paintingAllText
;
244 StyleNeeded styleNeeded
;
250 bool primarySelection
;
253 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
256 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
273 enum { eWrapNone
, eWrapWord
, eWrapChar
} wrapState
;
274 enum { wrapLineLarge
= 0x7ffffff };
279 int wrapVisualFlagsLocation
;
280 int wrapVisualStartIndent
;
281 int wrapIndentMode
; // SC_WRAPINDENT_FIXED, _SAME, _INDENT
285 int marginNumberPadding
; // the right-side padding of the number margin
286 int ctrlCharPadding
; // the padding around control character text blobs
287 int lastSegItalicsOffset
; // the offset so as not to clip italic characters at EOLs
293 virtual void Initialise() = 0;
294 virtual void Finalise();
296 void InvalidateStyleData();
297 void InvalidateStyleRedraw();
298 void RefreshStyleData();
299 void DropGraphics(bool freeObjects
);
300 void AllocateGraphics();
302 virtual PRectangle
GetClientRectangle();
303 PRectangle
GetTextRectangle();
308 SelectionPosition
ClampPositionIntoDocument(SelectionPosition sp
) const;
309 Point
LocationFromPosition(SelectionPosition pos
);
310 Point
LocationFromPosition(int pos
);
311 int XFromPosition(int pos
);
312 int XFromPosition(SelectionPosition sp
);
313 SelectionPosition
SPositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false, bool virtualSpace
=true);
314 int PositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false);
315 SelectionPosition
SPositionFromLineX(int lineDoc
, int x
);
316 int PositionFromLineX(int line
, int x
);
317 int LineFromLocation(Point pt
);
318 void SetTopLine(int topLineNew
);
321 void RedrawRect(PRectangle rc
);
323 void RedrawSelMargin(int line
=-1, bool allAfter
=false);
324 PRectangle
RectangleFromRange(int start
, int end
);
325 void InvalidateRange(int start
, int end
);
327 bool UserVirtualSpace() const {
328 return ((virtualSpaceOptions
& SCVS_USERACCESSIBLE
) != 0);
330 int CurrentPosition();
331 bool SelectionEmpty();
332 SelectionPosition
SelectionStart();
333 SelectionPosition
SelectionEnd();
334 void SetRectangularRange();
335 void ThinRectangularRange();
336 void InvalidateSelection(SelectionRange newMain
, bool invalidateWholeSelection
=false);
337 void SetSelection(SelectionPosition currentPos_
, SelectionPosition anchor_
);
338 void SetSelection(int currentPos_
, int anchor_
);
339 void SetSelection(SelectionPosition currentPos_
);
340 void SetSelection(int currentPos_
);
341 void SetEmptySelection(SelectionPosition currentPos_
);
342 void SetEmptySelection(int currentPos_
);
343 bool RangeContainsProtected(int start
, int end
) const;
344 bool SelectionContainsProtected();
345 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true) const;
346 SelectionPosition
MovePositionOutsideChar(SelectionPosition pos
, int moveDir
, bool checkLineEnd
=true) const;
347 int MovePositionTo(SelectionPosition newPos
, Selection::selTypes sel
=Selection::noSel
, bool ensureVisible
=true);
348 int MovePositionTo(int newPos
, Selection::selTypes sel
=Selection::noSel
, bool ensureVisible
=true);
349 SelectionPosition
MovePositionSoVisible(SelectionPosition pos
, int moveDir
);
350 SelectionPosition
MovePositionSoVisible(int pos
, int moveDir
);
351 Point
PointMainCaret();
352 void SetLastXChosen();
354 void ScrollTo(int line
, bool moveThumb
=true);
355 virtual void ScrollText(int linesToMove
);
356 void HorizontalScrollTo(int xPos
);
357 void VerticalCentreCaret();
358 void MoveSelectedLines(int lineDelta
);
359 void MoveSelectedLinesUp();
360 void MoveSelectedLinesDown();
361 void MoveCaretInsideView(bool ensureVisible
=true);
362 int DisplayFromPosition(int pos
);
364 struct XYScrollPosition
{
367 XYScrollPosition(int xOffset_
, int topLine_
) : xOffset(xOffset_
), topLine(topLine_
) {}
369 XYScrollPosition
XYScrollToMakeVisible(const bool useMargin
, const bool vert
, const bool horiz
);
370 void SetXYScroll(XYScrollPosition newXY
);
371 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
372 void ShowCaretAtCurrentPosition();
374 void InvalidateCaret();
375 virtual void UpdateSystemCaret();
377 void NeedWrapping(int docLineStart
= 0, int docLineEnd
= wrapLineLarge
);
378 bool WrapOneLine(Surface
*surface
, int lineToWrap
);
379 bool WrapLines(bool fullWrap
, int priorityWrapLineStart
);
381 void LinesSplit(int pixelWidth
);
383 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
384 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
385 LineLayout
*RetrieveLineLayout(int lineNumber
);
386 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
387 int width
=LineLayout::wrapWidthInfinite
);
388 ColourDesired
SelectionBackground(ViewStyle
&vsDraw
, bool main
);
389 ColourDesired
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourDesired background
, int inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
390 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
391 void DrawWrapMarker(Surface
*surface
, PRectangle rcPlace
, bool isEndMarker
, ColourDesired wrapColour
);
392 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
393 int line
, int lineEnd
, int xStart
, int subLine
, XYACCUMULATOR subLineStart
,
394 bool overrideBackground
, ColourDesired background
,
395 bool drawWrapMark
, ColourDesired wrapColour
);
396 void DrawIndicator(int indicNum
, int startPos
, int endPos
, Surface
*surface
, ViewStyle
&vsDraw
,
397 int xStart
, PRectangle rcLine
, LineLayout
*ll
, int subLine
);
398 void DrawIndicators(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
399 PRectangle rcLine
, LineLayout
*ll
, int subLine
, int lineEnd
, bool under
);
400 void DrawAnnotation(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
401 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
402 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
403 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
404 void DrawBlockCaret(Surface
*surface
, ViewStyle
&vsDraw
, LineLayout
*ll
, int subLine
,
405 int xStart
, int offset
, int posCaret
, PRectangle rcCaret
, ColourDesired caretColour
);
406 void DrawCarets(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
407 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
408 void RefreshPixMaps(Surface
*surfaceWindow
);
409 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
410 long FormatRange(bool draw
, Sci_RangeToFormat
*pfr
);
411 int TextWidth(int style
, const char *text
);
413 virtual void SetVerticalScrollPos() = 0;
414 virtual void SetHorizontalScrollPos() = 0;
415 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
416 virtual void ReconfigureScrollBars();
417 void SetScrollBars();
420 void FilterSelections();
421 int InsertSpace(int position
, unsigned int spaces
);
422 void AddChar(char ch
);
423 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
424 void InsertPaste(SelectionPosition selStart
, const char *text
, int len
);
425 void ClearSelection(bool retainMultipleSelections
=false);
427 void ClearDocumentStyle();
429 void PasteRectangular(SelectionPosition pos
, const char *ptr
, int len
);
430 virtual void Copy() = 0;
431 virtual void CopyAllowLine();
432 virtual bool CanPaste();
433 virtual void Paste() = 0;
439 void DelCharBack(bool allowLineStartDeletion
);
440 virtual void ClaimSelection() = 0;
442 virtual void NotifyChange() = 0;
443 virtual void NotifyFocus(bool focus
);
444 virtual void SetCtrlID(int identifier
);
445 virtual int GetCtrlID() { return ctrlID
; }
446 virtual void NotifyParent(SCNotification scn
) = 0;
447 virtual void NotifyParent(SCNotification
* scn
) = 0;
448 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
449 void NotifyChar(int ch
);
450 void NotifySavePoint(bool isSavePoint
);
451 void NotifyModifyAttempt();
452 virtual void NotifyDoubleClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
453 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
454 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
455 void NotifyHotSpotReleaseClick(int position
, bool shift
, bool ctrl
, bool alt
);
456 void NotifyUpdateUI();
457 void NotifyPainted();
458 void NotifyIndicatorClick(bool click
, int position
, bool shift
, bool ctrl
, bool alt
);
459 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
460 void NotifyNeedShown(int pos
, int len
);
461 void NotifyDwelling(Point pt
, bool state
);
464 void NotifyModifyAttempt(Document
*document
, void *userData
);
465 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
466 void CheckModificationForWrap(DocModification mh
);
467 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
468 void NotifyDeleted(Document
*document
, void *userData
);
469 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
470 void NotifyLexerChanged(Document
*doc
, void *userData
);
471 void NotifyErrorOccurred(Document
*doc
, void *userData
, int status
);
472 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
474 void ContainerNeedsUpdate(int flags
);
475 void PageMove(int direction
, Selection::selTypes sel
=Selection::noSel
, bool stuttered
= false);
476 enum { cmSame
, cmUpper
, cmLower
} caseMap
;
477 virtual std::string
CaseMapString(const std::string
&s
, int caseMapping
);
478 void ChangeCaseOfSelection(int caseMapping
);
479 void LineTranspose();
480 void Duplicate(bool forLine
);
481 virtual void CancelModes();
483 void CursorUpOrDown(int direction
, Selection::selTypes sel
=Selection::noSel
);
484 void ParaUpOrDown(int direction
, Selection::selTypes sel
=Selection::noSel
);
485 int StartEndDisplayLine(int pos
, bool start
);
486 virtual int KeyCommand(unsigned int iMessage
);
487 virtual int KeyDefault(int /* key */, int /*modifiers*/);
488 int KeyDownWithModifiers(int key
, int modifiers
, bool *consumed
);
489 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
491 void Indent(bool forwards
);
493 virtual CaseFolder
*CaseFolderForEncoding();
494 long FindText(uptr_t wParam
, sptr_t lParam
);
496 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
497 long SearchInTarget(const char *text
, int length
);
498 void GoToLine(int lineNo
);
500 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
501 char *CopyRange(int start
, int end
);
502 std::string
RangeText(int start
, int end
) const;
503 void CopySelectionRange(SelectionText
*ss
, bool allowLineCopy
=false);
504 void CopyRangeToClipboard(int start
, int end
);
505 void CopyText(int length
, const char *text
);
506 void SetDragPosition(SelectionPosition newPos
);
507 virtual void DisplayCursor(Window::Cursor c
);
508 virtual bool DragThreshold(Point ptStart
, Point ptNow
);
509 virtual void StartDrag();
510 void DropAt(SelectionPosition position
, const char *value
, bool moving
, bool rectangular
);
511 /** PositionInSelection returns true if position in selection. */
512 bool PositionInSelection(int pos
);
513 bool PointInSelection(Point pt
);
514 bool PointInSelMargin(Point pt
);
515 Window::Cursor
GetMarginCursor(Point pt
);
516 void TrimAndSetSelection(int currentPos_
, int anchor_
);
517 void LineSelection(int lineCurrentPos_
, int lineAnchorPos_
, bool wholeLine
);
518 void WordSelection(int pos
);
519 void DwellEnd(bool mouseMoved
);
521 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
522 void ButtonMove(Point pt
);
523 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
527 virtual void SetTicking(bool on
) = 0;
528 virtual bool SetIdle(bool) { return false; }
529 virtual void SetMouseCapture(bool on
) = 0;
530 virtual bool HaveMouseCapture() = 0;
531 void SetFocusState(bool focusState
);
533 int PositionAfterArea(PRectangle rcArea
);
534 void StyleToPositionInView(Position pos
);
536 virtual void QueueStyling(int upTo
);
538 virtual bool PaintContains(PRectangle rc
);
539 bool PaintContainsMargin();
540 void CheckForChangeOutsidePaint(Range r
);
541 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
543 void SetAnnotationHeights(int start
, int end
);
544 void SetDocPointer(Document
*document
);
546 void SetAnnotationVisible(int visible
);
548 void Expand(int &line
, bool doExpand
);
549 void ToggleContraction(int line
);
550 int ContractedFoldNext(int lineStart
);
551 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
552 int GetTag(char *tagValue
, int tagNumber
);
553 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
555 bool PositionIsHotspot(int position
);
556 bool PointIsHotspot(Point pt
);
557 void SetHotSpotRange(Point
*pt
);
558 void GetHotSpotRange(int &hsStart
, int &hsEnd
);
560 int CodePage() const;
561 virtual bool ValidCodePage(int /* codePage */) const { return true; }
562 int WrapCount(int line
);
563 void AddStyledText(char *buffer
, int appendLength
);
565 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
566 void StyleSetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
567 sptr_t
StyleGetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
569 static const char *StringFromEOLMode(int eolMode
);
571 static sptr_t
StringResult(sptr_t lParam
, const char *val
);
574 // Public so the COM thunks can access it.
575 bool IsUnicodeMode() const;
576 // Public so scintilla_send_message can use it.
577 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
578 // Public so scintilla_set_id can use it.
580 // Public so COM methods for drag and drop can set it.
582 friend class AutoSurface
;
583 friend class SelectionLineIterator
;
587 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
593 AutoSurface(Editor
*ed
, int technology
= -1) : surf(0) {
594 if (ed
->wMain
.GetID()) {
595 surf
= Surface::Allocate(technology
!= -1 ? technology
: ed
->technology
);
597 surf
->Init(ed
->wMain
.GetID());
598 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
599 surf
->SetDBCSMode(ed
->CodePage());
603 AutoSurface(SurfaceID sid
, Editor
*ed
, int technology
= -1) : surf(0) {
604 if (ed
->wMain
.GetID()) {
605 surf
= Surface::Allocate(technology
!= -1 ? technology
: ed
->technology
);
607 surf
->Init(sid
, ed
->wMain
.GetID());
608 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
609 surf
->SetDBCSMode(ed
->CodePage());
616 Surface
*operator->() const {
619 operator Surface
*() const {