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
;
70 char bracePreviousStyles
[2];
76 // Wrapped line support
80 LineLayout(int maxLineLength_
);
81 virtual ~LineLayout();
82 void Resize(int maxLineLength_
);
84 void Invalidate(validLevel validity_
);
85 int LineStart(int line
) {
88 } else if ((line
>= lines
) || !lineStarts
) {
89 return numCharsInLine
;
91 return lineStarts
[line
];
94 void SetLineStart(int line
, int start
);
95 void SetBracesHighlight(Range rangeLine
, Position braces
[],
96 char bracesMatchStyle
, int xHighlight
);
97 void RestoreBracesHighlight(Range rangeLine
, Position braces
[]);
102 class LineLayoutCache
{
110 void Allocate(int length_
);
111 void AllocateForLevel(int linesOnScreen
, int linesInDoc
);
114 virtual ~LineLayoutCache();
117 llcNone
=SC_CACHE_NONE
,
118 llcCaret
=SC_CACHE_CARET
,
119 llcPage
=SC_CACHE_PAGE
,
120 llcDocument
=SC_CACHE_DOCUMENT
122 void Invalidate(LineLayout::validLevel validity_
);
123 void SetLevel(int level_
);
124 int GetLevel() { return level
; }
125 LineLayout
*Retrieve(int lineNumber
, int lineCaret
, int maxChars
, int styleClock_
,
126 int linesOnScreen
, int linesInDoc
);
127 void Dispose(LineLayout
*ll
);
131 * Hold a piece of text selected for copying or dragging.
132 * The text is expected to hold a terminating '\0' and this is counted in len.
134 class SelectionText
{
141 SelectionText() : s(0), len(0), rectangular(false), codePage(0), characterSet(0) {}
146 Set(0, 0, 0, 0, false);
148 void Set(char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
) {
155 codePage
= codePage_
;
156 characterSet
= characterSet_
;
157 rectangular
= rectangular_
;
159 void Copy(const char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
) {
164 for (int i
= 0; i
< len_
; i
++) {
170 codePage
= codePage_
;
171 characterSet
= characterSet_
;
172 rectangular
= rectangular_
;
174 void Copy(const SelectionText
&other
) {
175 Copy(other
.s
, other
.len
, other
.codePage
, other
.characterSet
, other
.rectangular
);
181 class Editor
: public DocWatcher
{
182 // Private so Editor objects can not be copied
183 Editor(const Editor
&) : DocWatcher() {}
184 Editor
&operator=(const Editor
&) { return *this; }
186 protected: // ScintillaBase subclass needs access to much of Editor
188 /** On GTK+, Scintilla is a container widget holding two scroll bars
189 * whereas on Windows there is just one window with both scroll bars turned on. */
190 Window wMain
; ///< The Scintilla parent window
192 /** Style resources may be expensive to allocate so are cached between uses.
193 * When a style attribute is changed, this cache is flushed. */
198 int printMagnification
;
202 int controlCharSymbol
;
208 bool mouseDownCaptures
;
210 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
211 * the screen. This avoids flashing but is about 30% slower. */
213 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
214 * and then the foreground. This avoids chopping off characters that overlap the next run. */
217 int xOffset
; ///< Horizontal scrolled amount in pixels
218 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
219 bool horizontalScrollBarVisible
;
221 bool verticalScrollBarVisible
;
226 Surface
*pixmapSelMargin
;
227 Surface
*pixmapSelPattern
;
228 Surface
*pixmapIndentGuide
;
229 Surface
*pixmapIndentGuideHighlight
;
237 Timer autoScrollTimer
;
238 enum { autoScrollDelay
= 200 };
243 unsigned int lastClickTime
;
247 enum { selChar
, selWord
, selLine
} selectionType
;
250 bool dropWentOutside
;
255 int originalAnchorPos
;
267 int bracesMatchStyle
;
268 int highlightGuideColumn
;
272 enum { notPainting
, painting
, paintAbandoned
} paintState
;
274 bool paintingAllText
;
279 enum selTypes
{ noSel
, selStream
, selRectangle
, selLines
};
281 bool moveExtendsSelection
;
282 int xStartSelect
; ///< x position of start of rectangular selection
283 int xEndSelect
; ///< x position of end of rectangular selection
284 bool primarySelection
;
287 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
290 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
307 enum { eWrapNone
, eWrapWord
, eWrapChar
} wrapState
;
308 bool backgroundWrapEnabled
;
310 int docLineLastWrapped
;
311 int docLastLineToWrap
;
313 int wrapVisualFlagsLocation
;
314 int wrapVisualStartIndent
;
315 int actualWrapVisualStartIndent
;
323 virtual void Initialise() = 0;
324 virtual void Finalise();
326 void InvalidateStyleData();
327 void InvalidateStyleRedraw();
328 virtual void RefreshColourPalette(Palette
&pal
, bool want
);
329 void RefreshStyleData();
332 virtual PRectangle
GetClientRectangle();
333 PRectangle
GetTextRectangle();
338 Point
LocationFromPosition(int pos
);
339 int XFromPosition(int pos
);
340 int PositionFromLocation(Point pt
);
341 int PositionFromLocationClose(Point pt
);
342 int PositionFromLineX(int line
, int x
);
343 int LineFromLocation(Point pt
);
344 void SetTopLine(int topLineNew
);
347 void RedrawRect(PRectangle rc
);
349 void RedrawSelMargin(int line
=-1);
350 PRectangle
RectangleFromRange(int start
, int end
);
351 void InvalidateRange(int start
, int end
);
353 int CurrentPosition();
354 bool SelectionEmpty();
355 int SelectionStart();
357 void SetRectangularRange();
358 void InvalidateSelection(int currentPos_
, int anchor_
);
359 void SetSelection(int currentPos_
, int anchor_
);
360 void SetSelection(int currentPos_
);
361 void SetEmptySelection(int currentPos_
);
362 bool RangeContainsProtected(int start
, int end
) const;
363 bool SelectionContainsProtected();
364 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true);
365 int MovePositionTo(int newPos
, selTypes sel
=noSel
, bool ensureVisible
=true);
366 int MovePositionSoVisible(int pos
, int moveDir
);
367 void SetLastXChosen();
369 void ScrollTo(int line
, bool moveThumb
=true);
370 virtual void ScrollText(int linesToMove
);
371 void HorizontalScrollTo(int xPos
);
372 void MoveCaretInsideView(bool ensureVisible
=true);
373 int DisplayFromPosition(int pos
);
374 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
375 void ShowCaretAtCurrentPosition();
377 void InvalidateCaret();
378 virtual void UpdateSystemCaret();
380 void NeedWrapping(int docLineStartWrapping
= 0, int docLineEndWrapping
= 0x7ffffff);
381 bool WrapLines(bool fullWrap
, int priorityWrapLineStart
);
383 void LinesSplit(int pixelWidth
);
385 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
386 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
387 LineLayout
*RetrieveLineLayout(int lineNumber
);
388 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
389 int width
=LineLayout::wrapWidthInfinite
);
390 ColourAllocated
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourAllocated background
, bool inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
391 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
392 void DrawWrapMarker(Surface
*surface
, PRectangle rcPlace
, bool isEndMarker
, ColourAllocated wrapColour
);
393 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
394 int line
, int lineEnd
, int xStart
, int subLine
, int subLineStart
,
395 bool overrideBackground
, ColourAllocated background
,
396 bool drawWrapMark
, ColourAllocated wrapColour
);
397 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
398 PRectangle rcLine
, LineLayout
*ll
, int subLine
=0);
399 void RefreshPixMaps(Surface
*surfaceWindow
);
400 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
401 long FormatRange(bool draw
, RangeToFormat
*pfr
);
402 int TextWidth(int style
, const char *text
);
404 virtual void SetVerticalScrollPos() = 0;
405 virtual void SetHorizontalScrollPos() = 0;
406 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
407 virtual void ReconfigureScrollBars();
408 void SetScrollBars();
411 void AddChar(char ch
);
412 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
413 void ClearSelection();
415 void ClearDocumentStyle();
417 void PasteRectangular(int pos
, const char *ptr
, int len
);
418 virtual void Copy() = 0;
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 int GetCtrlID() { return ctrlID
; }
432 virtual void NotifyParent(SCNotification scn
) = 0;
433 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
434 void NotifyChar(int ch
);
435 void NotifyMove(int position
);
436 void NotifySavePoint(bool isSavePoint
);
437 void NotifyModifyAttempt();
438 virtual void NotifyDoubleClick(Point pt
, bool shift
);
439 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
440 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
441 void NotifyUpdateUI();
442 void NotifyPainted();
443 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
444 void NotifyNeedShown(int pos
, int len
);
445 void NotifyDwelling(Point pt
, bool state
);
448 void NotifyModifyAttempt(Document
*document
, void *userData
);
449 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
450 void CheckModificationForWrap(DocModification mh
);
451 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
452 void NotifyDeleted(Document
*document
, void *userData
);
453 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
454 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
456 void PageMove(int direction
, selTypes sel
=noSel
, bool stuttered
= false);
457 void ChangeCaseOfSelection(bool makeUpperCase
);
458 void LineTranspose();
459 void Duplicate(bool forLine
);
460 virtual void CancelModes();
462 void CursorUpOrDown(int direction
, selTypes sel
=noSel
);
463 void ParaUpOrDown(int direction
, selTypes sel
=noSel
);
464 int StartEndDisplayLine(int pos
, bool start
);
465 virtual int KeyCommand(unsigned int iMessage
);
466 virtual int KeyDefault(int /* key */, int /*modifiers*/);
467 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
469 int GetWhitespaceVisible();
470 void SetWhitespaceVisible(int view
);
472 void Indent(bool forwards
);
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 CopySelectionFromRange(SelectionText
*ss
, int start
, int end
);
483 void CopySelectionRange(SelectionText
*ss
);
484 void CopyRangeToClipboard(int start
, int end
);
485 void CopyText(int length
, const char *text
);
486 void SetDragPosition(int newPos
);
487 virtual void DisplayCursor(Window::Cursor c
);
488 virtual void StartDrag();
489 void DropAt(int position
, const char *value
, bool moving
, bool rectangular
);
490 /** PositionInSelection returns 0 if position in selection, -1 if position before selection, and 1 if after.
491 * Before means either before any line of selection or before selection on its line, with a similar meaning to after. */
492 int PositionInSelection(int pos
);
493 bool PointInSelection(Point pt
);
494 bool PointInSelMargin(Point pt
);
495 void LineSelection(int lineCurrent_
, int lineAnchor_
);
496 void DwellEnd(bool mouseMoved
);
497 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
498 void ButtonMove(Point pt
);
499 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
503 virtual void SetTicking(bool on
) = 0;
504 virtual bool SetIdle(bool) { return false; }
505 virtual void SetMouseCapture(bool on
) = 0;
506 virtual bool HaveMouseCapture() = 0;
507 void SetFocusState(bool focusState
);
509 virtual bool PaintContains(PRectangle rc
);
510 bool PaintContainsMargin();
511 void CheckForChangeOutsidePaint(Range r
);
512 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
514 void SetDocPointer(Document
*document
);
516 void Expand(int &line
, bool doExpand
);
517 void ToggleContraction(int line
);
518 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
519 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
521 bool PositionIsHotspot(int position
);
522 bool PointIsHotspot(Point pt
);
523 void SetHotSpotRange(Point
*pt
);
524 void GetHotSpotRange(int& hsStart
, int& hsEnd
);
526 int CodePage() const;
527 int WrapCount(int line
);
529 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
532 // Public so the COM thunks can access it.
533 bool IsUnicodeMode() const;
534 // Public so scintilla_send_message can use it.
535 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
536 // Public so scintilla_set_id can use it.
538 friend class AutoSurface
;
539 friend class SelectionLineIterator
;
543 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
549 AutoSurface(Editor
*ed
) : surf(0) {
550 if (ed
->wMain
.GetID()) {
551 surf
= Surface::Allocate();
553 surf
->Init(ed
->wMain
.GetID());
554 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
555 surf
->SetDBCSMode(ed
->CodePage());
559 AutoSurface(SurfaceID sid
, Editor
*ed
) : surf(0) {
560 if (ed
->wMain
.GetID()) {
561 surf
= Surface::Allocate();
563 surf
->Init(sid
, ed
->wMain
.GetID());
564 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
565 surf
->SetDBCSMode(ed
->CodePage());
572 Surface
*operator->() const {
575 operator Surface
*() const {