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. */
137 int printMagnification
;
141 int controlCharSymbol
;
143 // Highlight current folding block
144 HighlightDelimiter highlightDelimiter
;
149 bool mouseDownCaptures
;
151 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
152 * the screen. This avoids flashing but is about 30% slower. */
154 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
155 * and then the foreground. This avoids chopping off characters that overlap the next run. */
158 int xOffset
; ///< Horizontal scrolled amount in pixels
159 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
160 bool horizontalScrollBarVisible
;
163 int lineWidthMaxSeen
;
164 bool verticalScrollBarVisible
;
168 bool multipleSelection
;
169 bool additionalSelectionTyping
;
171 bool additionalCaretsBlink
;
172 bool additionalCaretsVisible
;
174 int virtualSpaceOptions
;
177 Surface
*pixmapSelMargin
;
178 Surface
*pixmapSelPattern
;
179 Surface
*pixmapIndentGuide
;
180 Surface
*pixmapIndentGuideHighlight
;
183 PositionCache posCache
;
189 Timer autoScrollTimer
;
190 enum { autoScrollDelay
= 200 };
195 unsigned int lastClickTime
;
199 enum { selChar
, selWord
, selSubLine
, selWholeLine
} selectionType
;
201 enum { ddNone
, ddInitial
, ddDragging
} inDragDrop
;
202 bool dropWentOutside
;
203 SelectionPosition posDrag
;
204 SelectionPosition posDrop
;
208 int originalAnchorPos
;
209 int wordSelectAnchorStartPos
;
210 int wordSelectAnchorEndPos
;
211 int wordSelectInitialCaretPos
;
221 int bracesMatchStyle
;
222 int highlightGuideColumn
;
226 enum { notPainting
, painting
, paintAbandoned
} paintState
;
228 bool paintingAllText
;
230 StyleNeeded styleNeeded
;
236 bool primarySelection
;
239 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
242 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
259 enum { eWrapNone
, eWrapWord
, eWrapChar
} wrapState
;
260 enum { wrapLineLarge
= 0x7ffffff };
265 int wrapVisualFlagsLocation
;
266 int wrapVisualStartIndent
;
267 int wrapIndentMode
; // SC_WRAPINDENT_FIXED, _SAME, _INDENT
275 virtual void Initialise() = 0;
276 virtual void Finalise();
278 void InvalidateStyleData();
279 void InvalidateStyleRedraw();
280 void RefreshStyleData();
281 void DropGraphics(bool freeObjects
);
282 void AllocateGraphics();
284 virtual PRectangle
GetClientRectangle();
285 PRectangle
GetTextRectangle();
290 SelectionPosition
ClampPositionIntoDocument(SelectionPosition sp
) const;
291 Point
LocationFromPosition(SelectionPosition pos
);
292 Point
LocationFromPosition(int pos
);
293 int XFromPosition(int pos
);
294 int XFromPosition(SelectionPosition sp
);
295 SelectionPosition
SPositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false, bool virtualSpace
=true);
296 int PositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false);
297 SelectionPosition
SPositionFromLineX(int lineDoc
, int x
);
298 int PositionFromLineX(int line
, int x
);
299 int LineFromLocation(Point pt
);
300 void SetTopLine(int topLineNew
);
303 void RedrawRect(PRectangle rc
);
305 void RedrawSelMargin(int line
=-1, bool allAfter
=false);
306 PRectangle
RectangleFromRange(int start
, int end
);
307 void InvalidateRange(int start
, int end
);
309 bool UserVirtualSpace() const {
310 return ((virtualSpaceOptions
& SCVS_USERACCESSIBLE
) != 0);
312 int CurrentPosition();
313 bool SelectionEmpty();
314 SelectionPosition
SelectionStart();
315 SelectionPosition
SelectionEnd();
316 void SetRectangularRange();
317 void ThinRectangularRange();
318 void InvalidateSelection(SelectionRange newMain
, bool invalidateWholeSelection
=false);
319 void SetSelection(SelectionPosition currentPos_
, SelectionPosition anchor_
);
320 void SetSelection(int currentPos_
, int anchor_
);
321 void SetSelection(SelectionPosition currentPos_
);
322 void SetSelection(int currentPos_
);
323 void SetEmptySelection(SelectionPosition currentPos_
);
324 void SetEmptySelection(int currentPos_
);
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 int MovePositionTo(SelectionPosition newPos
, Selection::selTypes sel
=Selection::noSel
, bool ensureVisible
=true);
330 int MovePositionTo(int newPos
, Selection::selTypes sel
=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
{
349 XYScrollPosition(int xOffset_
, int topLine_
) : xOffset(xOffset_
), topLine(topLine_
) {}
351 XYScrollPosition
XYScrollToMakeVisible(const bool useMargin
, const bool vert
, const bool horiz
);
352 void SetXYScroll(XYScrollPosition newXY
);
353 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
354 void ShowCaretAtCurrentPosition();
356 void InvalidateCaret();
357 virtual void UpdateSystemCaret();
359 void NeedWrapping(int docLineStart
= 0, int docLineEnd
= wrapLineLarge
);
360 bool WrapOneLine(Surface
*surface
, int lineToWrap
);
361 bool WrapLines(bool fullWrap
, int priorityWrapLineStart
);
363 void LinesSplit(int pixelWidth
);
365 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
366 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
367 LineLayout
*RetrieveLineLayout(int lineNumber
);
368 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
369 int width
=LineLayout::wrapWidthInfinite
);
370 ColourDesired
SelectionBackground(ViewStyle
&vsDraw
, bool main
);
371 ColourDesired
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourDesired background
, int inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
372 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
373 void DrawWrapMarker(Surface
*surface
, PRectangle rcPlace
, bool isEndMarker
, ColourDesired wrapColour
);
374 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
375 int line
, int lineEnd
, int xStart
, int subLine
, XYACCUMULATOR subLineStart
,
376 bool overrideBackground
, ColourDesired background
,
377 bool drawWrapMark
, ColourDesired wrapColour
);
378 void DrawIndicator(int indicNum
, int startPos
, int endPos
, Surface
*surface
, ViewStyle
&vsDraw
,
379 int xStart
, PRectangle rcLine
, LineLayout
*ll
, int subLine
);
380 void DrawIndicators(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
381 PRectangle rcLine
, LineLayout
*ll
, int subLine
, int lineEnd
, bool under
);
382 void DrawAnnotation(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
383 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
384 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
385 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
386 void DrawBlockCaret(Surface
*surface
, ViewStyle
&vsDraw
, LineLayout
*ll
, int subLine
,
387 int xStart
, int offset
, int posCaret
, PRectangle rcCaret
, ColourDesired caretColour
);
388 void DrawCarets(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
389 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
390 void RefreshPixMaps(Surface
*surfaceWindow
);
391 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
392 long FormatRange(bool draw
, Sci_RangeToFormat
*pfr
);
393 int TextWidth(int style
, const char *text
);
395 virtual void SetVerticalScrollPos() = 0;
396 virtual void SetHorizontalScrollPos() = 0;
397 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
398 virtual void ReconfigureScrollBars();
399 void SetScrollBars();
402 void FilterSelections();
403 int InsertSpace(int position
, unsigned int spaces
);
404 void AddChar(char ch
);
405 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
406 void InsertPaste(SelectionPosition selStart
, const char *text
, int len
);
407 void ClearSelection(bool retainMultipleSelections
=false);
409 void ClearDocumentStyle();
411 void PasteRectangular(SelectionPosition pos
, const char *ptr
, int len
);
412 virtual void Copy() = 0;
413 virtual void CopyAllowLine();
414 virtual bool CanPaste();
415 virtual void Paste() = 0;
421 void DelCharBack(bool allowLineStartDeletion
);
422 virtual void ClaimSelection() = 0;
424 virtual void NotifyChange() = 0;
425 virtual void NotifyFocus(bool focus
);
426 virtual void SetCtrlID(int identifier
);
427 virtual int GetCtrlID() { return ctrlID
; }
428 virtual void NotifyParent(SCNotification scn
) = 0;
429 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
430 void NotifyChar(int ch
);
431 void NotifySavePoint(bool isSavePoint
);
432 void NotifyModifyAttempt();
433 virtual void NotifyDoubleClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
434 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
435 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
436 void NotifyHotSpotReleaseClick(int position
, bool shift
, bool ctrl
, bool alt
);
437 void NotifyUpdateUI();
438 void NotifyPainted();
439 void NotifyIndicatorClick(bool click
, int position
, bool shift
, bool ctrl
, bool alt
);
440 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
441 void NotifyNeedShown(int pos
, int len
);
442 void NotifyDwelling(Point pt
, bool state
);
445 void NotifyModifyAttempt(Document
*document
, void *userData
);
446 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
447 void CheckModificationForWrap(DocModification mh
);
448 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
449 void NotifyDeleted(Document
*document
, void *userData
);
450 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
451 void NotifyLexerChanged(Document
*doc
, void *userData
);
452 void NotifyErrorOccurred(Document
*doc
, void *userData
, int status
);
453 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
455 void ContainerNeedsUpdate(int flags
);
456 void PageMove(int direction
, Selection::selTypes sel
=Selection::noSel
, bool stuttered
= false);
457 enum { cmSame
, cmUpper
, cmLower
} caseMap
;
458 virtual std::string
CaseMapString(const std::string
&s
, int caseMapping
);
459 void ChangeCaseOfSelection(int caseMapping
);
460 void LineTranspose();
461 void Duplicate(bool forLine
);
462 virtual void CancelModes();
464 void CursorUpOrDown(int direction
, Selection::selTypes sel
=Selection::noSel
);
465 void ParaUpOrDown(int direction
, Selection::selTypes sel
=Selection::noSel
);
466 int StartEndDisplayLine(int pos
, bool start
);
467 virtual int KeyCommand(unsigned int iMessage
);
468 virtual int KeyDefault(int /* key */, int /*modifiers*/);
469 int KeyDownWithModifiers(int key
, int modifiers
, bool *consumed
);
470 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
472 void Indent(bool forwards
);
474 virtual CaseFolder
*CaseFolderForEncoding();
475 long FindText(uptr_t wParam
, sptr_t lParam
);
477 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
478 long SearchInTarget(const char *text
, int length
);
479 void GoToLine(int lineNo
);
481 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
482 char *CopyRange(int start
, int end
);
483 std::string
RangeText(int start
, int end
) const;
484 void CopySelectionRange(SelectionText
*ss
, bool allowLineCopy
=false);
485 void CopyRangeToClipboard(int start
, int end
);
486 void CopyText(int length
, const char *text
);
487 void SetDragPosition(SelectionPosition newPos
);
488 virtual void DisplayCursor(Window::Cursor c
);
489 virtual bool DragThreshold(Point ptStart
, Point ptNow
);
490 virtual void StartDrag();
491 void DropAt(SelectionPosition position
, const char *value
, bool moving
, bool rectangular
);
492 /** PositionInSelection returns true if position in selection. */
493 bool PositionInSelection(int pos
);
494 bool PointInSelection(Point pt
);
495 bool PointInSelMargin(Point pt
);
496 Window::Cursor
GetMarginCursor(Point pt
);
497 void TrimAndSetSelection(int currentPos_
, int anchor_
);
498 void LineSelection(int lineCurrentPos_
, int lineAnchorPos_
, bool wholeLine
);
499 void WordSelection(int pos
);
500 void DwellEnd(bool mouseMoved
);
502 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
503 void ButtonMove(Point pt
);
504 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
508 virtual void SetTicking(bool on
) = 0;
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
);
515 void StyleToPositionInView(Position pos
);
517 virtual void QueueStyling(int upTo
);
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 void SetDocPointer(Document
*document
);
527 void SetAnnotationVisible(int visible
);
529 void Expand(int &line
, bool doExpand
);
530 void ToggleContraction(int line
);
531 int ContractedFoldNext(int lineStart
);
532 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
533 int GetTag(char *tagValue
, int tagNumber
);
534 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
536 bool PositionIsHotspot(int position
);
537 bool PointIsHotspot(Point pt
);
538 void SetHotSpotRange(Point
*pt
);
539 void GetHotSpotRange(int &hsStart
, int &hsEnd
);
541 int CodePage() const;
542 virtual bool ValidCodePage(int /* codePage */) const { return true; }
543 int WrapCount(int line
);
544 void AddStyledText(char *buffer
, int appendLength
);
546 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
547 void StyleSetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
548 sptr_t
StyleGetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
550 static const char *StringFromEOLMode(int eolMode
);
552 static sptr_t
StringResult(sptr_t lParam
, const char *val
);
555 // Public so the COM thunks can access it.
556 bool IsUnicodeMode() const;
557 // Public so scintilla_send_message can use it.
558 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
559 // Public so scintilla_set_id can use it.
561 // Public so COM methods for drag and drop can set it.
563 friend class AutoSurface
;
564 friend class SelectionLineIterator
;
568 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
574 AutoSurface(Editor
*ed
, int technology
= -1) : surf(0) {
575 if (ed
->wMain
.GetID()) {
576 surf
= Surface::Allocate(technology
!= -1 ? technology
: ed
->technology
);
578 surf
->Init(ed
->wMain
.GetID());
579 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
580 surf
->SetDBCSMode(ed
->CodePage());
584 AutoSurface(SurfaceID sid
, Editor
*ed
, int technology
= -1) : surf(0) {
585 if (ed
->wMain
.GetID()) {
586 surf
= Surface::Allocate(technology
!= -1 ? technology
: ed
->technology
);
588 surf
->Init(sid
, ed
->wMain
.GetID());
589 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
590 surf
->SetDBCSMode(ed
->CodePage());
597 Surface
*operator->() const {
600 operator Surface
*() const {