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' and this is counted in len.
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
;
264 int bracesMatchStyle
;
265 int highlightGuideColumn
;
269 enum { notPainting
, painting
, paintAbandoned
} paintState
;
271 bool paintingAllText
;
276 enum selTypes
{ noSel
, selStream
, selRectangle
, selLines
};
278 bool moveExtendsSelection
;
279 int xStartSelect
; ///< x position of start of rectangular selection
280 int xEndSelect
; ///< x position of end of rectangular selection
281 bool primarySelection
;
284 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
287 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
304 enum { eWrapNone
, eWrapWord
} wrapState
;
305 bool backgroundWrapEnabled
;
307 int docLineLastWrapped
;
308 int docLastLineToWrap
;
310 int wrapVisualFlagsLocation
;
311 int wrapVisualStartIndent
;
312 int actualWrapVisualStartIndent
;
318 virtual void Initialise() = 0;
319 virtual void Finalise();
321 void InvalidateStyleData();
322 void InvalidateStyleRedraw();
323 virtual void RefreshColourPalette(Palette
&pal
, bool want
);
324 void RefreshStyleData();
327 virtual PRectangle
GetClientRectangle();
328 PRectangle
GetTextRectangle();
333 Point
LocationFromPosition(int pos
);
334 int XFromPosition(int pos
);
335 int PositionFromLocation(Point pt
);
336 int PositionFromLocationClose(Point pt
);
337 int PositionFromLineX(int line
, int x
);
338 int LineFromLocation(Point pt
);
339 void SetTopLine(int topLineNew
);
342 void RedrawRect(PRectangle rc
);
344 void RedrawSelMargin();
345 PRectangle
RectangleFromRange(int start
, int end
);
346 void InvalidateRange(int start
, int end
);
348 int CurrentPosition();
349 bool SelectionEmpty();
350 int SelectionStart();
352 void InvalidateSelection(int currentPos_
, int anchor_
);
353 void SetSelection(int currentPos_
, int anchor_
);
354 void SetSelection(int currentPos_
);
355 void SetEmptySelection(int currentPos_
);
356 bool RangeContainsProtected(int start
, int end
) const;
357 bool SelectionContainsProtected();
358 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true);
359 int MovePositionTo(int newPos
, selTypes sel
=noSel
, bool ensureVisible
=true);
360 int MovePositionSoVisible(int pos
, int moveDir
);
361 void SetLastXChosen();
363 void ScrollTo(int line
, bool moveThumb
=true);
364 virtual void ScrollText(int linesToMove
);
365 void HorizontalScrollTo(int xPos
);
366 void MoveCaretInsideView(bool ensureVisible
=true);
367 int DisplayFromPosition(int pos
);
368 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
369 void ShowCaretAtCurrentPosition();
371 void InvalidateCaret();
372 virtual void UpdateSystemCaret();
374 void NeedWrapping(int docLineStartWrapping
= 0, int docLineEndWrapping
= 0x7ffffff);
375 bool WrapLines(bool fullWrap
, int priorityWrapLineStart
);
377 void LinesSplit(int pixelWidth
);
379 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
380 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
381 LineLayout
*RetrieveLineLayout(int lineNumber
);
382 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
383 int width
=LineLayout::wrapWidthInfinite
);
384 ColourAllocated
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourAllocated background
, bool inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
385 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
386 void DrawWrapMarker(Surface
*surface
, PRectangle rcPlace
, bool isEndMarker
, ColourAllocated wrapColour
);
387 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
388 int line
, int lineEnd
, int xStart
, int subLine
, int subLineStart
,
389 bool overrideBackground
, ColourAllocated background
,
390 bool drawWrapMark
, ColourAllocated wrapColour
);
391 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
392 PRectangle rcLine
, LineLayout
*ll
, int subLine
=0);
393 void RefreshPixMaps(Surface
*surfaceWindow
);
394 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
395 long FormatRange(bool draw
, RangeToFormat
*pfr
);
396 int TextWidth(int style
, const char *text
);
398 virtual void SetVerticalScrollPos() = 0;
399 virtual void SetHorizontalScrollPos() = 0;
400 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
401 virtual void ReconfigureScrollBars();
402 void SetScrollBars();
405 void AddChar(char ch
);
406 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
407 void ClearSelection();
409 void ClearDocumentStyle();
411 void PasteRectangular(int pos
, const char *ptr
, int len
);
412 virtual void Copy() = 0;
413 virtual bool CanPaste();
414 virtual void Paste() = 0;
420 void DelCharBack(bool allowLineStartDeletion
);
421 virtual void ClaimSelection() = 0;
423 virtual void NotifyChange() = 0;
424 virtual void NotifyFocus(bool focus
);
425 virtual int GetCtrlID() { return ctrlID
; }
426 virtual void NotifyParent(SCNotification scn
) = 0;
427 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
428 void NotifyChar(int ch
);
429 void NotifyMove(int position
);
430 void NotifySavePoint(bool isSavePoint
);
431 void NotifyModifyAttempt();
432 virtual void NotifyDoubleClick(Point pt
, bool shift
);
433 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
434 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
435 void NotifyUpdateUI();
436 void NotifyPainted();
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 NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
450 void PageMove(int direction
, selTypes sel
=noSel
, bool stuttered
= false);
451 void ChangeCaseOfSelection(bool makeUpperCase
);
452 void LineTranspose();
453 void LineDuplicate();
454 virtual void CancelModes();
456 void CursorUpOrDown(int direction
, selTypes sel
=noSel
);
457 int StartEndDisplayLine(int pos
, bool start
);
458 virtual int KeyCommand(unsigned int iMessage
);
459 virtual int KeyDefault(int /* key */, int /*modifiers*/);
460 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
462 int GetWhitespaceVisible();
463 void SetWhitespaceVisible(int view
);
465 void Indent(bool forwards
);
467 long FindText(uptr_t wParam
, sptr_t lParam
);
469 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
470 long SearchInTarget(const char *text
, int length
);
471 void GoToLine(int lineNo
);
473 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
474 char *CopyRange(int start
, int end
);
475 void CopySelectionFromRange(SelectionText
*ss
, int start
, int end
);
476 void CopySelectionRange(SelectionText
*ss
);
477 void CopyRangeToClipboard(int start
, int end
);
478 void CopyText(int length
, const char *text
);
479 void SetDragPosition(int newPos
);
480 virtual void DisplayCursor(Window::Cursor c
);
481 virtual void StartDrag();
482 void DropAt(int position
, const char *value
, bool moving
, bool rectangular
);
483 /** PositionInSelection returns 0 if position in selection, -1 if position before selection, and 1 if after.
484 * Before means either before any line of selection or before selection on its line, with a similar meaning to after. */
485 int PositionInSelection(int pos
);
486 bool PointInSelection(Point pt
);
487 bool PointInSelMargin(Point pt
);
488 void LineSelection(int lineCurrent_
, int lineAnchor_
);
489 void DwellEnd(bool mouseMoved
);
490 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
491 void ButtonMove(Point pt
);
492 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
496 virtual void SetTicking(bool on
) = 0;
497 virtual bool SetIdle(bool) { return false; }
498 virtual void SetMouseCapture(bool on
) = 0;
499 virtual bool HaveMouseCapture() = 0;
500 void SetFocusState(bool focusState
);
502 void CheckForChangeOutsidePaint(Range r
);
503 int BraceMatch(int position
, int maxReStyle
);
504 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
506 void SetDocPointer(Document
*document
);
508 void Expand(int &line
, bool doExpand
);
509 void ToggleContraction(int line
);
510 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
511 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
513 bool PositionIsHotspot(int position
);
514 bool PointIsHotspot(Point pt
);
515 void SetHotSpotRange(Point
*pt
);
516 void GetHotSpotRange(int& hsStart
, int& hsEnd
);
518 int CodePage() const;
520 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
523 // Public so the COM thunks can access it.
524 bool IsUnicodeMode() const;
525 // Public so scintilla_send_message can use it.
526 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
527 // Public so scintilla_set_id can use it.
529 friend class AutoSurface
;
530 friend class SelectionLineIterator
;
534 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
540 AutoSurface(Editor
*ed
) : surf(0) {
541 if (ed
->wMain
.GetID()) {
542 surf
= Surface::Allocate();
544 surf
->Init(ed
->wMain
.GetID());
545 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
546 surf
->SetDBCSMode(ed
->CodePage());
550 AutoSurface(SurfaceID sid
, Editor
*ed
) : surf(0) {
551 if (ed
->wMain
.GetID()) {
552 surf
= Surface::Allocate();
554 surf
->Init(sid
, ed
->wMain
.GetID());
555 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
556 surf
->SetDBCSMode(ed
->CodePage());
563 Surface
*operator->() const {
566 operator Surface
*() const {