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 int printMagnification
;
140 int controlCharSymbol
;
142 // Highlight current folding block
143 HighlightDelimiter highlightDelimiter
;
148 bool mouseDownCaptures
;
150 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
151 * the screen. This avoids flashing but is about 30% slower. */
153 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
154 * and then the foreground. This avoids chopping off characters that overlap the next run. */
157 int xOffset
; ///< Horizontal scrolled amount in pixels
158 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
159 bool horizontalScrollBarVisible
;
162 int lineWidthMaxSeen
;
163 bool verticalScrollBarVisible
;
166 bool multipleSelection
;
167 bool additionalSelectionTyping
;
169 bool additionalCaretsBlink
;
170 bool additionalCaretsVisible
;
172 int virtualSpaceOptions
;
175 Surface
*pixmapSelMargin
;
176 Surface
*pixmapSelPattern
;
177 Surface
*pixmapIndentGuide
;
178 Surface
*pixmapIndentGuideHighlight
;
181 PositionCache posCache
;
187 Timer autoScrollTimer
;
188 enum { autoScrollDelay
= 200 };
193 unsigned int lastClickTime
;
197 enum { selChar
, selWord
, selLine
} selectionType
;
199 enum { ddNone
, ddInitial
, ddDragging
} inDragDrop
;
200 bool dropWentOutside
;
201 SelectionPosition posDrag
;
202 SelectionPosition posDrop
;
206 int originalAnchorPos
;
207 int wordSelectAnchorStartPos
;
208 int wordSelectAnchorEndPos
;
209 int wordSelectInitialCaretPos
;
219 int bracesMatchStyle
;
220 int highlightGuideColumn
;
224 enum { notPainting
, painting
, paintAbandoned
} paintState
;
226 bool paintingAllText
;
227 StyleNeeded styleNeeded
;
233 bool primarySelection
;
236 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
239 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
256 enum { eWrapNone
, eWrapWord
, eWrapChar
} wrapState
;
257 enum { wrapLineLarge
= 0x7ffffff };
262 int wrapVisualFlagsLocation
;
263 int wrapVisualStartIndent
;
264 int wrapAddIndent
; // This will be added to initial indent of line
265 int wrapIndentMode
; // SC_WRAPINDENT_FIXED, _SAME, _INDENT
273 virtual void Initialise() = 0;
274 virtual void Finalise();
276 void InvalidateStyleData();
277 void InvalidateStyleRedraw();
278 virtual void RefreshColourPalette(Palette
&pal
, bool want
);
279 void RefreshStyleData();
282 virtual PRectangle
GetClientRectangle();
283 PRectangle
GetTextRectangle();
288 SelectionPosition
ClampPositionIntoDocument(SelectionPosition sp
) const;
289 Point
LocationFromPosition(SelectionPosition pos
);
290 Point
LocationFromPosition(int pos
);
291 int XFromPosition(int pos
);
292 int XFromPosition(SelectionPosition sp
);
293 SelectionPosition
SPositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false, bool virtualSpace
=true);
294 int PositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false);
295 SelectionPosition
SPositionFromLineX(int lineDoc
, int x
);
296 int PositionFromLineX(int line
, int x
);
297 int LineFromLocation(Point pt
);
298 void SetTopLine(int topLineNew
);
301 void RedrawRect(PRectangle rc
);
303 void RedrawSelMargin(int line
=-1, bool allAfter
=false);
304 PRectangle
RectangleFromRange(int start
, int end
);
305 void InvalidateRange(int start
, int end
);
307 bool UserVirtualSpace() const {
308 return ((virtualSpaceOptions
& SCVS_USERACCESSIBLE
) != 0);
310 int CurrentPosition();
311 bool SelectionEmpty();
312 SelectionPosition
SelectionStart();
313 SelectionPosition
SelectionEnd();
314 void SetRectangularRange();
315 void ThinRectangularRange();
316 void InvalidateSelection(SelectionRange newMain
, bool invalidateWholeSelection
=false);
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 bool RangeContainsProtected(int start
, int end
) const;
324 bool SelectionContainsProtected();
325 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true) const;
326 SelectionPosition
MovePositionOutsideChar(SelectionPosition pos
, int moveDir
, bool checkLineEnd
=true) const;
327 int MovePositionTo(SelectionPosition newPos
, Selection::selTypes sel
=Selection::noSel
, bool ensureVisible
=true);
328 int MovePositionTo(int newPos
, Selection::selTypes sel
=Selection::noSel
, bool ensureVisible
=true);
329 SelectionPosition
MovePositionSoVisible(SelectionPosition pos
, int moveDir
);
330 SelectionPosition
MovePositionSoVisible(int pos
, int moveDir
);
331 Point
PointMainCaret();
332 void SetLastXChosen();
334 void ScrollTo(int line
, bool moveThumb
=true);
335 virtual void ScrollText(int linesToMove
);
336 void HorizontalScrollTo(int xPos
);
337 void VerticalCentreCaret();
338 void MoveSelectedLines(int lineDelta
);
339 void MoveSelectedLinesUp();
340 void MoveSelectedLinesDown();
341 void MoveCaretInsideView(bool ensureVisible
=true);
342 int DisplayFromPosition(int pos
);
344 struct XYScrollPosition
{
347 XYScrollPosition(int xOffset_
, int topLine_
) : xOffset(xOffset_
), topLine(topLine_
) {}
349 XYScrollPosition
XYScrollToMakeVisible(const bool useMargin
, const bool vert
, const bool horiz
);
350 void SetXYScroll(XYScrollPosition newXY
);
351 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
352 void ShowCaretAtCurrentPosition();
354 void InvalidateCaret();
355 virtual void UpdateSystemCaret();
357 void NeedWrapping(int docLineStart
= 0, int docLineEnd
= wrapLineLarge
);
358 bool WrapOneLine(Surface
*surface
, int lineToWrap
);
359 bool WrapLines(bool fullWrap
, int priorityWrapLineStart
);
361 void LinesSplit(int pixelWidth
);
363 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
364 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
365 LineLayout
*RetrieveLineLayout(int lineNumber
);
366 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
367 int width
=LineLayout::wrapWidthInfinite
);
368 ColourAllocated
SelectionBackground(ViewStyle
&vsDraw
, bool main
);
369 ColourAllocated
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourAllocated background
, int inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
370 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
371 void DrawWrapMarker(Surface
*surface
, PRectangle rcPlace
, bool isEndMarker
, ColourAllocated wrapColour
);
372 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
373 int line
, int lineEnd
, int xStart
, int subLine
, int subLineStart
,
374 bool overrideBackground
, ColourAllocated background
,
375 bool drawWrapMark
, ColourAllocated wrapColour
);
376 void DrawIndicator(int indicNum
, int startPos
, int endPos
, Surface
*surface
, ViewStyle
&vsDraw
,
377 int xStart
, PRectangle rcLine
, LineLayout
*ll
, int subLine
);
378 void DrawIndicators(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
379 PRectangle rcLine
, LineLayout
*ll
, int subLine
, int lineEnd
, bool under
);
380 void DrawAnnotation(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
381 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
382 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
383 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
384 void DrawBlockCaret(Surface
*surface
, ViewStyle
&vsDraw
, LineLayout
*ll
, int subLine
,
385 int xStart
, int offset
, int posCaret
, PRectangle rcCaret
, ColourAllocated caretColour
);
386 void DrawCarets(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
387 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
388 void RefreshPixMaps(Surface
*surfaceWindow
);
389 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
390 long FormatRange(bool draw
, Sci_RangeToFormat
*pfr
);
391 int TextWidth(int style
, const char *text
);
393 virtual void SetVerticalScrollPos() = 0;
394 virtual void SetHorizontalScrollPos() = 0;
395 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
396 virtual void ReconfigureScrollBars();
397 void SetScrollBars();
400 void FilterSelections();
401 int InsertSpace(int position
, unsigned int spaces
);
402 void AddChar(char ch
);
403 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
404 void InsertPaste(SelectionPosition selStart
, const char *text
, int len
);
405 void ClearSelection(bool retainMultipleSelections
=false);
407 void ClearDocumentStyle();
409 void PasteRectangular(SelectionPosition pos
, const char *ptr
, int len
);
410 virtual void Copy() = 0;
411 virtual void CopyAllowLine();
412 virtual bool CanPaste();
413 virtual void Paste() = 0;
419 void DelCharBack(bool allowLineStartDeletion
);
420 virtual void ClaimSelection() = 0;
422 virtual void NotifyChange() = 0;
423 virtual void NotifyFocus(bool focus
);
424 virtual int GetCtrlID() { return ctrlID
; }
425 virtual void NotifyParent(SCNotification scn
) = 0;
426 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
427 void NotifyChar(int ch
);
428 void NotifySavePoint(bool isSavePoint
);
429 void NotifyModifyAttempt();
430 virtual void NotifyDoubleClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
431 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
432 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
433 void NotifyHotSpotReleaseClick(int position
, bool shift
, bool ctrl
, bool alt
);
434 void NotifyUpdateUI();
435 void NotifyPainted();
436 void NotifyIndicatorClick(bool click
, int position
, bool shift
, bool ctrl
, bool alt
);
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
);
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 sel
=Selection::noSel
, bool stuttered
= false);
454 enum { cmSame
, cmUpper
, cmLower
} caseMap
;
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();
461 void CursorUpOrDown(int direction
, Selection::selTypes sel
=Selection::noSel
);
462 void ParaUpOrDown(int direction
, Selection::selTypes sel
=Selection::noSel
);
463 int StartEndDisplayLine(int pos
, bool start
);
464 virtual int KeyCommand(unsigned int iMessage
);
465 virtual int KeyDefault(int /* key */, int /*modifiers*/);
466 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
468 int GetWhitespaceVisible();
469 void SetWhitespaceVisible(int view
);
471 void Indent(bool forwards
);
473 virtual CaseFolder
*CaseFolderForEncoding();
474 long FindText(uptr_t wParam
, sptr_t lParam
);
476 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
477 long SearchInTarget(const char *text
, int length
);
478 void GoToLine(int lineNo
);
480 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
481 char *CopyRange(int start
, int end
);
482 void CopySelectionRange(SelectionText
*ss
, bool allowLineCopy
=false);
483 void CopyRangeToClipboard(int start
, int end
);
484 void CopyText(int length
, const char *text
);
485 void SetDragPosition(SelectionPosition newPos
);
486 virtual void DisplayCursor(Window::Cursor c
);
487 virtual bool DragThreshold(Point ptStart
, Point ptNow
);
488 virtual void StartDrag();
489 void DropAt(SelectionPosition position
, const char *value
, bool moving
, bool rectangular
);
490 /** PositionInSelection returns true if position in selection. */
491 bool PositionInSelection(int pos
);
492 bool PointInSelection(Point pt
);
493 bool PointInSelMargin(Point pt
);
494 Window::Cursor
GetMarginCursor(Point pt
);
495 void LineSelection(int lineCurrent_
, int lineAnchor_
);
496 void WordSelection(int pos
);
497 void DwellEnd(bool mouseMoved
);
499 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
500 void ButtonMove(Point pt
);
501 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
505 virtual void SetTicking(bool on
) = 0;
506 virtual bool SetIdle(bool) { return false; }
507 virtual void SetMouseCapture(bool on
) = 0;
508 virtual bool HaveMouseCapture() = 0;
509 void SetFocusState(bool focusState
);
511 int PositionAfterArea(PRectangle rcArea
);
512 void StyleToPositionInView(Position pos
);
514 virtual void QueueStyling(int upTo
);
516 virtual bool PaintContains(PRectangle rc
);
517 bool PaintContainsMargin();
518 void CheckForChangeOutsidePaint(Range r
);
519 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
521 void SetAnnotationHeights(int start
, int end
);
522 void SetDocPointer(Document
*document
);
524 void SetAnnotationVisible(int visible
);
526 void Expand(int &line
, bool doExpand
);
527 void ToggleContraction(int line
);
528 int ContractedFoldNext(int lineStart
);
529 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
530 int GetTag(char *tagValue
, int tagNumber
);
531 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
533 bool PositionIsHotspot(int position
);
534 bool PointIsHotspot(Point pt
);
535 void SetHotSpotRange(Point
*pt
);
536 void GetHotSpotRange(int &hsStart
, int &hsEnd
);
538 int CodePage() const;
539 virtual bool ValidCodePage(int /* codePage */) const { return true; }
540 int WrapCount(int line
);
541 void AddStyledText(char *buffer
, int appendLength
);
543 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
544 void StyleSetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
545 sptr_t
StyleGetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
547 static const char *StringFromEOLMode(int eolMode
);
549 static sptr_t
StringResult(sptr_t lParam
, const char *val
);
552 // Public so the COM thunks can access it.
553 bool IsUnicodeMode() const;
554 // Public so scintilla_send_message can use it.
555 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
556 // Public so scintilla_set_id can use it.
558 // Public so COM methods for drag and drop can set it.
560 friend class AutoSurface
;
561 friend class SelectionLineIterator
;
565 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
571 AutoSurface(Editor
*ed
) : surf(0) {
572 if (ed
->wMain
.GetID()) {
573 surf
= Surface::Allocate();
575 surf
->Init(ed
->wMain
.GetID());
576 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
577 surf
->SetDBCSMode(ed
->CodePage());
581 AutoSurface(SurfaceID sid
, Editor
*ed
) : surf(0) {
582 if (ed
->wMain
.GetID()) {
583 surf
= Surface::Allocate();
585 surf
->Init(sid
, ed
->wMain
.GetID());
586 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
587 surf
->SetDBCSMode(ed
->CodePage());
594 Surface
*operator->() const {
597 operator Surface
*() const {