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};
38 friend class LineLayoutCache
;
41 /// Drawing is only performed for @a maxLineLength characters on each line.
45 enum { wrapWidthInfinite
= 0x7ffffff };
48 enum validLevel
{ llInvalid
, llCheckTextAndStyle
, llPositions
, llLines
} validity
;
59 char bracePreviousStyles
[2];
65 // Wrapped line support
69 LineLayout(int maxLineLength_
);
70 virtual ~LineLayout();
71 void Resize(int maxLineLength_
);
73 void Invalidate(validLevel validity_
);
74 int LineStart(int line
) {
77 } else if ((line
>= lines
) || !lineStarts
) {
78 return numCharsInLine
;
80 return lineStarts
[line
];
83 void SetLineStart(int line
, int start
);
84 void SetBracesHighlight(Range rangeLine
, Position braces
[],
85 char bracesMatchStyle
, int xHighlight
);
86 void RestoreBracesHighlight(Range rangeLine
, Position braces
[]);
91 class LineLayoutCache
{
98 void Allocate(int length_
);
99 void AllocateForLevel(int linesOnScreen
, int linesInDoc
);
102 virtual ~LineLayoutCache();
105 llcNone
=SC_CACHE_NONE
,
106 llcCaret
=SC_CACHE_CARET
,
107 llcPage
=SC_CACHE_PAGE
,
108 llcDocument
=SC_CACHE_DOCUMENT
110 void Invalidate(LineLayout::validLevel validity_
);
111 void SetLevel(int level_
);
112 int GetLevel() { return level
; }
113 LineLayout
*Retrieve(int lineNumber
, int lineCaret
, int maxChars
, int styleClock_
,
114 int linesOnScreen
, int linesInDoc
);
115 void Dispose(LineLayout
*ll
);
118 class SelectionText
{
123 SelectionText() : s(0), len(0), rectangular(false) {}
127 void Set(char *s_
, int len_
, bool rectangular_
=false) {
134 rectangular
= rectangular_
;
136 void Copy(const char *s_
, int len_
, bool rectangular_
=false) {
141 for (int i
= 0; i
< len_
; i
++) {
147 rectangular
= rectangular_
;
153 class Editor
: public DocWatcher
{
154 // Private so Editor objects can not be copied
155 Editor(const Editor
&) : DocWatcher() {}
156 Editor
&operator=(const Editor
&) { return *this; }
158 protected: // ScintillaBase subclass needs access to much of Editor
160 /** On GTK+, Scintilla is a container widget holding two scroll bars
161 * whereas on Windows there is just one window with both scroll bars turned on. */
162 Window wMain
; ///< The Scintilla parent window
164 /** Style resources may be expensive to allocate so are cached between uses.
165 * When a style attribute is changed, this cache is flushed. */
170 int printMagnification
;
174 int controlCharSymbol
;
180 bool mouseDownCaptures
;
182 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
183 * the screen. This avoids flashing but is about 30% slower. */
185 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
186 * and then the foreground. This avoids chopping off characters that overlap the next run. */
189 int xOffset
; ///< Horizontal scrolled amount in pixels
190 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
191 bool horizontalScrollBarVisible
;
193 bool verticalScrollBarVisible
;
197 Surface
*pixmapSelMargin
;
198 Surface
*pixmapSelPattern
;
199 Surface
*pixmapIndentGuide
;
200 Surface
*pixmapIndentGuideHighlight
;
208 Timer autoScrollTimer
;
209 enum { autoScrollDelay
= 200 };
212 unsigned int lastClickTime
;
216 enum { selChar
, selWord
, selLine
} selectionType
;
219 bool dropWentOutside
;
224 int originalAnchorPos
;
235 int bracesMatchStyle
;
236 int highlightGuideColumn
;
240 enum { notPainting
, painting
, paintAbandoned
} paintState
;
242 bool paintingAllText
;
247 enum { selStream
, selRectangle
, selRectangleFixed
} selType
;
250 bool primarySelection
;
253 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
256 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
273 enum { eWrapNone
, eWrapWord
} wrapState
;
275 int docLineLastWrapped
;
281 virtual void Initialise() = 0;
282 virtual void Finalise();
284 void InvalidateStyleData();
285 void InvalidateStyleRedraw();
286 virtual void RefreshColourPalette(Palette
&pal
, bool want
);
287 void RefreshStyleData();
290 virtual PRectangle
GetClientRectangle();
291 PRectangle
GetTextRectangle();
296 Point
LocationFromPosition(int pos
);
297 int XFromPosition(int pos
);
298 int PositionFromLocation(Point pt
);
299 int PositionFromLocationClose(Point pt
);
300 int PositionFromLineX(int line
, int x
);
301 int LineFromLocation(Point pt
);
302 void SetTopLine(int topLineNew
);
305 void RedrawRect(PRectangle rc
);
307 void RedrawSelMargin();
308 PRectangle
RectangleFromRange(int start
, int end
);
309 void InvalidateRange(int start
, int end
);
311 int CurrentPosition();
312 bool SelectionEmpty();
313 int SelectionStart(int line
=-1);
314 int SelectionEnd(int line
=-1);
315 void SetSelection(int currentPos_
, int anchor_
);
316 void SetSelection(int currentPos_
);
317 void SetEmptySelection(int currentPos_
);
318 bool RangeContainsProtected(int start
, int end
) const;
319 bool SelectionContainsProtected() const;
320 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true);
321 int MovePositionTo(int newPos
, bool extend
=false, bool ensureVisible
=true);
322 int MovePositionSoVisible(int pos
, int moveDir
);
323 void SetLastXChosen();
325 void ScrollTo(int line
, bool moveThumb
=true);
326 virtual void ScrollText(int linesToMove
);
327 void HorizontalScrollTo(int xPos
);
328 void MoveCaretInsideView(bool ensureVisible
=true);
329 int DisplayFromPosition(int pos
);
330 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
331 void ShowCaretAtCurrentPosition();
333 void InvalidateCaret();
335 void NeedWrapping(int docLineStartWrapping
=0);
338 void LinesSplit(int pixelWidth
);
340 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
341 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
342 LineLayout
*RetrieveLineLayout(int lineNumber
);
343 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
344 int width
=LineLayout::wrapWidthInfinite
);
345 ColourAllocated
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourAllocated background
, bool inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
346 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
347 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
348 int line
, int lineEnd
, int xStart
, int subLine
, int subLineStart
,
349 bool overrideBackground
, ColourAllocated background
);
350 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
351 PRectangle rcLine
, LineLayout
*ll
, int subLine
=0);
352 void RefreshPixMaps(Surface
*surfaceWindow
);
353 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
354 long FormatRange(bool draw
, RangeToFormat
*pfr
);
355 int TextWidth(int style
, const char *text
);
357 virtual void SetVerticalScrollPos() = 0;
358 virtual void SetHorizontalScrollPos() = 0;
359 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
360 virtual void ReconfigureScrollBars();
361 void SetScrollBars();
364 void AddChar(char ch
);
365 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
366 void ClearSelection();
368 void ClearDocumentStyle();
370 void PasteRectangular(int pos
, const char *ptr
, int len
);
371 virtual void Copy() = 0;
372 virtual bool CanPaste();
373 virtual void Paste() = 0;
379 void DelCharBack(bool allowLineStartDeletion
);
380 virtual void ClaimSelection() = 0;
382 virtual void NotifyChange() = 0;
383 virtual void NotifyFocus(bool focus
);
384 virtual int GetCtrlID() { return ctrlID
; }
385 virtual void NotifyParent(SCNotification scn
) = 0;
386 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
387 void NotifyChar(int ch
);
388 void NotifyMove(int position
);
389 void NotifySavePoint(bool isSavePoint
);
390 void NotifyModifyAttempt();
391 virtual void NotifyDoubleClick(Point pt
, bool shift
);
392 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
393 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
394 void NotifyUpdateUI();
395 void NotifyPainted();
396 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
397 void NotifyNeedShown(int pos
, int len
);
398 void NotifyDwelling(Point pt
, bool state
);
401 void NotifyModifyAttempt(Document
*document
, void *userData
);
402 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
403 void CheckModificationForWrap(DocModification mh
);
404 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
405 void NotifyDeleted(Document
*document
, void *userData
);
406 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
407 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
409 void PageMove(int direction
, bool extend
=false);
410 void ChangeCaseOfSelection(bool makeUpperCase
);
411 void LineTranspose();
412 void LineDuplicate();
413 virtual void CancelModes();
415 void CursorUpOrDown(int direction
, bool extend
=false);
416 int StartEndDisplayLine(int pos
, bool start
);
417 virtual int KeyCommand(unsigned int iMessage
);
418 virtual int KeyDefault(int /* key */, int /*modifiers*/);
419 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
421 int GetWhitespaceVisible();
422 void SetWhitespaceVisible(int view
);
424 void Indent(bool forwards
);
426 long FindText(uptr_t wParam
, sptr_t lParam
);
428 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
429 long SearchInTarget(const char *text
, int length
);
430 void GoToLine(int lineNo
);
432 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
433 char *CopyRange(int start
, int end
);
434 void CopySelectionFromRange(SelectionText
*ss
, int start
, int end
);
435 void CopySelectionRange(SelectionText
*ss
);
436 void CopyRangeToClipboard(int start
, int end
);
437 void CopyText(int length
, const char *text
);
438 void SetDragPosition(int newPos
);
439 virtual void DisplayCursor(Window::Cursor c
);
440 virtual void StartDrag();
441 void DropAt(int position
, const char *value
, bool moving
, bool rectangular
);
442 /** PositionInSelection returns 0 if position in selection, -1 if position before selection, and 1 if after.
443 * Before means either before any line of selection or before selection on its line, with a similar meaning to after. */
444 int PositionInSelection(int pos
);
445 bool PointInSelection(Point pt
);
446 bool PointInSelMargin(Point pt
);
447 void LineSelection(int lineCurrent_
, int lineAnchor_
);
448 void DwellEnd(bool mouseMoved
);
449 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
450 void ButtonMove(Point pt
);
451 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
454 virtual void SetTicking(bool on
) = 0;
455 virtual void SetMouseCapture(bool on
) = 0;
456 virtual bool HaveMouseCapture() = 0;
457 void SetFocusState(bool focusState
);
459 void CheckForChangeOutsidePaint(Range r
);
460 int BraceMatch(int position
, int maxReStyle
);
461 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
463 void SetDocPointer(Document
*document
);
465 void Expand(int &line
, bool doExpand
);
466 void ToggleContraction(int line
);
467 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
468 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
470 bool PositionIsHotspot(int position
);
471 bool PointIsHotspot(Point pt
);
472 void SetHotSpotRange(Point
*pt
);
473 void GetHotSpotRange(int& hsStart
, int& hsEnd
);
475 int CodePage() const;
477 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
480 // Public so the COM thunks can access it.
481 bool IsUnicodeMode() const;
482 // Public so scintilla_send_message can use it.
483 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
484 // Public so scintilla_set_id can use it.
486 friend class AutoSurface
;
490 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
496 AutoSurface(Editor
*ed
) : surf(0) {
497 if (ed
->wMain
.GetID()) {
498 surf
= Surface::Allocate();
500 surf
->Init(ed
->wMain
.GetID());
501 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
502 surf
->SetDBCSMode(ed
->CodePage());
506 AutoSurface(SurfaceID sid
, Editor
*ed
) : surf(0) {
507 if (ed
->wMain
.GetID()) {
508 surf
= Surface::Allocate();
510 surf
->Init(sid
, ed
->wMain
.GetID());
511 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
512 surf
->SetDBCSMode(ed
->CodePage());
519 Surface
*operator->() const {
522 operator Surface
*() const {