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_
;
99 void Copy(const char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
, bool lineCopy_
) {
104 for (int i
= 0; i
< len_
; 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. */
136 float scaleRGBAImage
;
138 int printMagnification
;
142 int controlCharSymbol
;
144 // Highlight current folding block
145 HighlightDelimiter highlightDelimiter
;
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. */
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. */
159 int xOffset
; ///< Horizontal scrolled amount in pixels
160 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
161 bool horizontalScrollBarVisible
;
164 int lineWidthMaxSeen
;
165 bool verticalScrollBarVisible
;
169 bool multipleSelection
;
170 bool additionalSelectionTyping
;
172 bool additionalCaretsBlink
;
173 bool additionalCaretsVisible
;
175 int virtualSpaceOptions
;
178 Surface
*pixmapSelMargin
;
179 Surface
*pixmapSelPattern
;
180 Surface
*pixmapIndentGuide
;
181 Surface
*pixmapIndentGuideHighlight
;
184 PositionCache posCache
;
190 Timer autoScrollTimer
;
191 enum { autoScrollDelay
= 200 };
196 unsigned int lastClickTime
;
200 enum { selChar
, selWord
, selSubLine
, selWholeLine
} selectionType
;
202 enum { ddNone
, ddInitial
, ddDragging
} inDragDrop
;
203 bool dropWentOutside
;
204 SelectionPosition posDrag
;
205 SelectionPosition posDrop
;
209 int originalAnchorPos
;
210 int wordSelectAnchorStartPos
;
211 int wordSelectAnchorEndPos
;
212 int wordSelectInitialCaretPos
;
222 int bracesMatchStyle
;
223 int highlightGuideColumn
;
227 enum { notPainting
, painting
, paintAbandoned
} paintState
;
229 bool paintingAllText
;
231 StyleNeeded styleNeeded
;
237 bool primarySelection
;
240 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
243 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
260 enum { eWrapNone
, eWrapWord
, eWrapChar
} wrapState
;
261 enum { wrapLineLarge
= 0x7ffffff };
266 int wrapVisualFlagsLocation
;
267 int wrapVisualStartIndent
;
268 int wrapIndentMode
; // SC_WRAPINDENT_FIXED, _SAME, _INDENT
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
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();
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
);
308 void RedrawRect(PRectangle rc
);
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
{
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();
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
);
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();
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);
414 void ClearDocumentStyle();
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;
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
);
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();
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
);
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
);
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
);
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
);
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
);
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.
566 // Public so COM methods for drag and drop can set it.
568 friend class AutoSurface
;
569 friend class SelectionLineIterator
;
573 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
579 AutoSurface(Editor
*ed
, int technology
= -1) : surf(0) {
580 if (ed
->wMain
.GetID()) {
581 surf
= Surface::Allocate(technology
!= -1 ? technology
: ed
->technology
);
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
);
593 surf
->Init(sid
, ed
->wMain
.GetID());
594 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
595 surf
->SetDBCSMode(ed
->CodePage());
602 Surface
*operator->() const {
605 operator Surface
*() const {