1 // Scintilla source code edit control
3 ** Defines the main editor class.
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
28 enum {tickSize
= 100};
48 friend class LineLayoutCache
;
51 /// Drawing is only performed for @a maxLineLength characters on each line.
55 enum { wrapWidthInfinite
= 0x7ffffff };
58 enum validLevel
{ llInvalid
, llCheckTextAndStyle
, llPositions
, llLines
} validity
;
66 unsigned char *styles
;
69 char bracePreviousStyles
[2];
75 // Wrapped line support
79 LineLayout(int maxLineLength_
);
80 virtual ~LineLayout();
81 void Resize(int maxLineLength_
);
83 void Invalidate(validLevel validity_
);
84 int LineStart(int line
) {
87 } else if ((line
>= lines
) || !lineStarts
) {
88 return numCharsInLine
;
90 return lineStarts
[line
];
93 void SetLineStart(int line
, int start
);
94 void SetBracesHighlight(Range rangeLine
, Position braces
[],
95 char bracesMatchStyle
, int xHighlight
);
96 void RestoreBracesHighlight(Range rangeLine
, Position braces
[]);
101 class LineLayoutCache
{
108 void Allocate(int length_
);
109 void AllocateForLevel(int linesOnScreen
, int linesInDoc
);
112 virtual ~LineLayoutCache();
115 llcNone
=SC_CACHE_NONE
,
116 llcCaret
=SC_CACHE_CARET
,
117 llcPage
=SC_CACHE_PAGE
,
118 llcDocument
=SC_CACHE_DOCUMENT
120 void Invalidate(LineLayout::validLevel validity_
);
121 void SetLevel(int level_
);
122 int GetLevel() { return level
; }
123 LineLayout
*Retrieve(int lineNumber
, int lineCaret
, int maxChars
, int styleClock_
,
124 int linesOnScreen
, int linesInDoc
);
125 void Dispose(LineLayout
*ll
);
129 * Hold a piece of text selected for copying or dragging.
130 * The text is expected to hold a terminating '\0'.
132 class SelectionText
{
139 SelectionText() : s(0), len(0), rectangular(false), codePage(0), characterSet(0) {}
144 Set(0, 0, 0, 0, false);
146 void Set(char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
) {
153 codePage
= codePage_
;
154 characterSet
= characterSet_
;
155 rectangular
= rectangular_
;
157 void Copy(const char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
) {
162 for (int i
= 0; i
< len_
; i
++) {
168 codePage
= codePage_
;
169 characterSet
= characterSet_
;
170 rectangular
= rectangular_
;
172 void Copy(const SelectionText
&other
) {
173 Copy(other
.s
, other
.len
, other
.codePage
, other
.characterSet
, other
.rectangular
);
179 class Editor
: public DocWatcher
{
180 // Private so Editor objects can not be copied
181 Editor(const Editor
&) : DocWatcher() {}
182 Editor
&operator=(const Editor
&) { return *this; }
184 protected: // ScintillaBase subclass needs access to much of Editor
186 /** On GTK+, Scintilla is a container widget holding two scroll bars
187 * whereas on Windows there is just one window with both scroll bars turned on. */
188 Window wMain
; ///< The Scintilla parent window
190 /** Style resources may be expensive to allocate so are cached between uses.
191 * When a style attribute is changed, this cache is flushed. */
196 int printMagnification
;
200 int controlCharSymbol
;
206 bool mouseDownCaptures
;
208 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
209 * the screen. This avoids flashing but is about 30% slower. */
211 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
212 * and then the foreground. This avoids chopping off characters that overlap the next run. */
215 int xOffset
; ///< Horizontal scrolled amount in pixels
216 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
217 bool horizontalScrollBarVisible
;
219 bool verticalScrollBarVisible
;
223 Surface
*pixmapSelMargin
;
224 Surface
*pixmapSelPattern
;
225 Surface
*pixmapIndentGuide
;
226 Surface
*pixmapIndentGuideHighlight
;
234 Timer autoScrollTimer
;
235 enum { autoScrollDelay
= 200 };
240 unsigned int lastClickTime
;
244 enum { selChar
, selWord
, selLine
} selectionType
;
247 bool dropWentOutside
;
252 int originalAnchorPos
;
263 int bracesMatchStyle
;
264 int highlightGuideColumn
;
268 enum { notPainting
, painting
, paintAbandoned
} paintState
;
270 bool paintingAllText
;
275 enum selTypes
{ noSel
, selStream
, selRectangle
, selLines
};
277 bool moveExtendsSelection
;
278 int xStartSelect
; ///< x position of start of rectangular selection
279 int xEndSelect
; ///< x position of end of rectangular selection
280 bool primarySelection
;
283 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
286 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
303 enum { eWrapNone
, eWrapWord
} wrapState
;
304 bool backgroundWrapEnabled
;
306 int docLineLastWrapped
;
307 int docLastLineToWrap
;
309 int wrapVisualFlagsLocation
;
310 int wrapVisualStartIndent
;
311 int actualWrapVisualStartIndent
;
317 virtual void Initialise() = 0;
318 virtual void Finalise();
320 void InvalidateStyleData();
321 void InvalidateStyleRedraw();
322 virtual void RefreshColourPalette(Palette
&pal
, bool want
);
323 void RefreshStyleData();
326 virtual PRectangle
GetClientRectangle();
327 PRectangle
GetTextRectangle();
332 Point
LocationFromPosition(int pos
);
333 int XFromPosition(int pos
);
334 int PositionFromLocation(Point pt
);
335 int PositionFromLocationClose(Point pt
);
336 int PositionFromLineX(int line
, int x
);
337 int LineFromLocation(Point pt
);
338 void SetTopLine(int topLineNew
);
341 void RedrawRect(PRectangle rc
);
343 void RedrawSelMargin();
344 PRectangle
RectangleFromRange(int start
, int end
);
345 void InvalidateRange(int start
, int end
);
347 int CurrentPosition();
348 bool SelectionEmpty();
349 int SelectionStart();
351 void InvalidateSelection(int currentPos_
, int anchor_
);
352 void SetSelection(int currentPos_
, int anchor_
);
353 void SetSelection(int currentPos_
);
354 void SetEmptySelection(int currentPos_
);
355 bool RangeContainsProtected(int start
, int end
) const;
356 bool SelectionContainsProtected();
357 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true);
358 int MovePositionTo(int newPos
, selTypes sel
=noSel
, bool ensureVisible
=true);
359 int MovePositionSoVisible(int pos
, int moveDir
);
360 void SetLastXChosen();
362 void ScrollTo(int line
, bool moveThumb
=true);
363 virtual void ScrollText(int linesToMove
);
364 void HorizontalScrollTo(int xPos
);
365 void MoveCaretInsideView(bool ensureVisible
=true);
366 int DisplayFromPosition(int pos
);
367 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
368 void ShowCaretAtCurrentPosition();
370 void InvalidateCaret();
372 void NeedWrapping(int docLineStartWrapping
= 0, int docLineEndWrapping
= 0x7ffffff);
373 bool WrapLines(bool fullWrap
, int priorityWrapLineStart
);
375 void LinesSplit(int pixelWidth
);
377 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
378 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
379 LineLayout
*RetrieveLineLayout(int lineNumber
);
380 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
381 int width
=LineLayout::wrapWidthInfinite
);
382 ColourAllocated
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourAllocated background
, bool inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
383 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
384 void DrawWrapMarker(Surface
*surface
, PRectangle rcPlace
, bool isEndMarker
, ColourAllocated wrapColour
);
385 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
386 int line
, int lineEnd
, int xStart
, int subLine
, int subLineStart
,
387 bool overrideBackground
, ColourAllocated background
,
388 bool drawWrapMark
, ColourAllocated wrapColour
);
389 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
390 PRectangle rcLine
, LineLayout
*ll
, int subLine
=0);
391 void RefreshPixMaps(Surface
*surfaceWindow
);
392 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
393 long FormatRange(bool draw
, RangeToFormat
*pfr
);
394 int TextWidth(int style
, const char *text
);
396 virtual void SetVerticalScrollPos() = 0;
397 virtual void SetHorizontalScrollPos() = 0;
398 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
399 virtual void ReconfigureScrollBars();
400 void SetScrollBars();
403 void AddChar(char ch
);
404 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
405 void ClearSelection();
407 void ClearDocumentStyle();
409 void PasteRectangular(int pos
, const char *ptr
, int len
);
410 virtual void Copy() = 0;
411 virtual bool CanPaste();
412 virtual void Paste() = 0;
418 void DelCharBack(bool allowLineStartDeletion
);
419 virtual void ClaimSelection() = 0;
421 virtual void NotifyChange() = 0;
422 virtual void NotifyFocus(bool focus
);
423 virtual int GetCtrlID() { return ctrlID
; }
424 virtual void NotifyParent(SCNotification scn
) = 0;
425 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
426 void NotifyChar(int ch
);
427 void NotifyMove(int position
);
428 void NotifySavePoint(bool isSavePoint
);
429 void NotifyModifyAttempt();
430 virtual void NotifyDoubleClick(Point pt
, bool shift
);
431 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
432 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
433 void NotifyUpdateUI();
434 void NotifyPainted();
435 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
436 void NotifyNeedShown(int pos
, int len
);
437 void NotifyDwelling(Point pt
, bool state
);
440 void NotifyModifyAttempt(Document
*document
, void *userData
);
441 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
442 void CheckModificationForWrap(DocModification mh
);
443 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
444 void NotifyDeleted(Document
*document
, void *userData
);
445 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
446 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
448 void PageMove(int direction
, selTypes sel
=noSel
, bool stuttered
= false);
449 void ChangeCaseOfSelection(bool makeUpperCase
);
450 void LineTranspose();
451 void LineDuplicate();
452 virtual void CancelModes();
454 void CursorUpOrDown(int direction
, selTypes sel
=noSel
);
455 int StartEndDisplayLine(int pos
, bool start
);
456 virtual int KeyCommand(unsigned int iMessage
);
457 virtual int KeyDefault(int /* key */, int /*modifiers*/);
458 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
460 int GetWhitespaceVisible();
461 void SetWhitespaceVisible(int view
);
463 void Indent(bool forwards
);
465 long FindText(uptr_t wParam
, sptr_t lParam
);
467 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
468 long SearchInTarget(const char *text
, int length
);
469 void GoToLine(int lineNo
);
471 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
472 char *CopyRange(int start
, int end
);
473 void CopySelectionFromRange(SelectionText
*ss
, int start
, int end
);
474 void CopySelectionRange(SelectionText
*ss
);
475 void CopyRangeToClipboard(int start
, int end
);
476 void CopyText(int length
, const char *text
);
477 void SetDragPosition(int newPos
);
478 virtual void DisplayCursor(Window::Cursor c
);
479 virtual void StartDrag();
480 void DropAt(int position
, const char *value
, bool moving
, bool rectangular
);
481 /** PositionInSelection returns 0 if position in selection, -1 if position before selection, and 1 if after.
482 * Before means either before any line of selection or before selection on its line, with a similar meaning to after. */
483 int PositionInSelection(int pos
);
484 bool PointInSelection(Point pt
);
485 bool PointInSelMargin(Point pt
);
486 void LineSelection(int lineCurrent_
, int lineAnchor_
);
487 void DwellEnd(bool mouseMoved
);
488 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
489 void ButtonMove(Point pt
);
490 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
494 virtual void SetTicking(bool on
) = 0;
495 virtual bool SetIdle(bool) { return false; }
496 virtual void SetMouseCapture(bool on
) = 0;
497 virtual bool HaveMouseCapture() = 0;
498 void SetFocusState(bool focusState
);
500 void CheckForChangeOutsidePaint(Range r
);
501 int BraceMatch(int position
, int maxReStyle
);
502 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
504 void SetDocPointer(Document
*document
);
506 void Expand(int &line
, bool doExpand
);
507 void ToggleContraction(int line
);
508 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
509 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
511 bool PositionIsHotspot(int position
);
512 bool PointIsHotspot(Point pt
);
513 void SetHotSpotRange(Point
*pt
);
514 void GetHotSpotRange(int& hsStart
, int& hsEnd
);
516 int CodePage() const;
518 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
521 // Public so the COM thunks can access it.
522 bool IsUnicodeMode() const;
523 // Public so scintilla_send_message can use it.
524 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
525 // Public so scintilla_set_id can use it.
527 friend class AutoSurface
;
528 friend class SelectionLineIterator
;
532 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
538 AutoSurface(Editor
*ed
) : surf(0) {
539 if (ed
->wMain
.GetID()) {
540 surf
= Surface::Allocate();
542 surf
->Init(ed
->wMain
.GetID());
543 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
544 surf
->SetDBCSMode(ed
->CodePage());
548 AutoSurface(SurfaceID sid
, Editor
*ed
) : surf(0) {
549 if (ed
->wMain
.GetID()) {
550 surf
= Surface::Allocate();
552 surf
->Init(sid
, ed
->wMain
.GetID());
553 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
554 surf
->SetDBCSMode(ed
->CodePage());
561 Surface
*operator->() const {
564 operator Surface
*() const {