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
;
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
{
137 SelectionText() : s(0), len(0), rectangular(false) {}
141 void Set(char *s_
, int len_
, bool rectangular_
=false) {
148 rectangular
= rectangular_
;
150 void Copy(const char *s_
, int len_
, bool rectangular_
=false) {
155 for (int i
= 0; i
< len_
; i
++) {
161 rectangular
= rectangular_
;
167 class Editor
: public DocWatcher
{
168 // Private so Editor objects can not be copied
169 Editor(const Editor
&) : DocWatcher() {}
170 Editor
&operator=(const Editor
&) { return *this; }
172 protected: // ScintillaBase subclass needs access to much of Editor
174 /** On GTK+, Scintilla is a container widget holding two scroll bars
175 * whereas on Windows there is just one window with both scroll bars turned on. */
176 Window wMain
; ///< The Scintilla parent window
178 /** Style resources may be expensive to allocate so are cached between uses.
179 * When a style attribute is changed, this cache is flushed. */
184 int printMagnification
;
188 int controlCharSymbol
;
194 bool mouseDownCaptures
;
196 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
197 * the screen. This avoids flashing but is about 30% slower. */
199 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
200 * and then the foreground. This avoids chopping off characters that overlap the next run. */
203 int xOffset
; ///< Horizontal scrolled amount in pixels
204 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
205 bool horizontalScrollBarVisible
;
207 bool verticalScrollBarVisible
;
211 Surface
*pixmapSelMargin
;
212 Surface
*pixmapSelPattern
;
213 Surface
*pixmapIndentGuide
;
214 Surface
*pixmapIndentGuideHighlight
;
222 Timer autoScrollTimer
;
223 enum { autoScrollDelay
= 200 };
228 unsigned int lastClickTime
;
232 enum { selChar
, selWord
, selLine
} selectionType
;
235 bool dropWentOutside
;
240 int originalAnchorPos
;
251 int bracesMatchStyle
;
252 int highlightGuideColumn
;
256 enum { notPainting
, painting
, paintAbandoned
} paintState
;
258 bool paintingAllText
;
263 enum selTypes
{ noSel
, selStream
, selRectangle
, selLines
};
265 bool moveExtendsSelection
;
266 int xStartSelect
; ///< x position of start of rectangular selection
267 int xEndSelect
; ///< x position of end of rectangular selection
268 bool primarySelection
;
271 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
274 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
291 enum { eWrapNone
, eWrapWord
} wrapState
;
292 bool backgroundWrapEnabled
;
294 int docLineLastWrapped
;
295 int docLastLineToWrap
;
301 virtual void Initialise() = 0;
302 virtual void Finalise();
304 void InvalidateStyleData();
305 void InvalidateStyleRedraw();
306 virtual void RefreshColourPalette(Palette
&pal
, bool want
);
307 void RefreshStyleData();
310 virtual PRectangle
GetClientRectangle();
311 PRectangle
GetTextRectangle();
316 Point
LocationFromPosition(int pos
);
317 int XFromPosition(int pos
);
318 int PositionFromLocation(Point pt
);
319 int PositionFromLocationClose(Point pt
);
320 int PositionFromLineX(int line
, int x
);
321 int LineFromLocation(Point pt
);
322 void SetTopLine(int topLineNew
);
325 void RedrawRect(PRectangle rc
);
327 void RedrawSelMargin();
328 PRectangle
RectangleFromRange(int start
, int end
);
329 void InvalidateRange(int start
, int end
);
331 int CurrentPosition();
332 bool SelectionEmpty();
333 int SelectionStart();
335 void InvalidateSelection(int currentPos_
, int anchor_
);
336 void SetSelection(int currentPos_
, int anchor_
);
337 void SetSelection(int currentPos_
);
338 void SetEmptySelection(int currentPos_
);
339 bool RangeContainsProtected(int start
, int end
) const;
340 bool SelectionContainsProtected();
341 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true);
342 int MovePositionTo(int newPos
, selTypes sel
=noSel
, bool ensureVisible
=true);
343 int MovePositionSoVisible(int pos
, int moveDir
);
344 void SetLastXChosen();
346 void ScrollTo(int line
, bool moveThumb
=true);
347 virtual void ScrollText(int linesToMove
);
348 void HorizontalScrollTo(int xPos
);
349 void MoveCaretInsideView(bool ensureVisible
=true);
350 int DisplayFromPosition(int pos
);
351 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
352 void ShowCaretAtCurrentPosition();
354 void InvalidateCaret();
356 void NeedWrapping(int docLineStartWrapping
= 0, int docLineEndWrapping
= 0x7ffffff);
357 bool WrapLines(bool fullWrap
, int priorityWrapLineStart
);
359 void LinesSplit(int pixelWidth
);
361 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
362 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
363 LineLayout
*RetrieveLineLayout(int lineNumber
);
364 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
365 int width
=LineLayout::wrapWidthInfinite
);
366 ColourAllocated
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourAllocated background
, bool inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
367 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
368 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
369 int line
, int lineEnd
, int xStart
, int subLine
, int subLineStart
,
370 bool overrideBackground
, ColourAllocated background
);
371 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
372 PRectangle rcLine
, LineLayout
*ll
, int subLine
=0);
373 void RefreshPixMaps(Surface
*surfaceWindow
);
374 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
375 long FormatRange(bool draw
, RangeToFormat
*pfr
);
376 int TextWidth(int style
, const char *text
);
378 virtual void SetVerticalScrollPos() = 0;
379 virtual void SetHorizontalScrollPos() = 0;
380 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
381 virtual void ReconfigureScrollBars();
382 void SetScrollBars();
385 void AddChar(char ch
);
386 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
387 void ClearSelection();
389 void ClearDocumentStyle();
391 void PasteRectangular(int pos
, const char *ptr
, int len
);
392 virtual void Copy() = 0;
393 virtual bool CanPaste();
394 virtual void Paste() = 0;
400 void DelCharBack(bool allowLineStartDeletion
);
401 virtual void ClaimSelection() = 0;
403 virtual void NotifyChange() = 0;
404 virtual void NotifyFocus(bool focus
);
405 virtual int GetCtrlID() { return ctrlID
; }
406 virtual void NotifyParent(SCNotification scn
) = 0;
407 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
408 void NotifyChar(int ch
);
409 void NotifyMove(int position
);
410 void NotifySavePoint(bool isSavePoint
);
411 void NotifyModifyAttempt();
412 virtual void NotifyDoubleClick(Point pt
, bool shift
);
413 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
414 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
415 void NotifyUpdateUI();
416 void NotifyPainted();
417 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
418 void NotifyNeedShown(int pos
, int len
);
419 void NotifyDwelling(Point pt
, bool state
);
422 void NotifyModifyAttempt(Document
*document
, void *userData
);
423 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
424 void CheckModificationForWrap(DocModification mh
);
425 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
426 void NotifyDeleted(Document
*document
, void *userData
);
427 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
428 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
430 void PageMove(int direction
, selTypes sel
=noSel
, bool stuttered
= false);
431 void ChangeCaseOfSelection(bool makeUpperCase
);
432 void LineTranspose();
433 void LineDuplicate();
434 virtual void CancelModes();
436 void CursorUpOrDown(int direction
, selTypes sel
=noSel
);
437 int StartEndDisplayLine(int pos
, bool start
);
438 virtual int KeyCommand(unsigned int iMessage
);
439 virtual int KeyDefault(int /* key */, int /*modifiers*/);
440 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
442 int GetWhitespaceVisible();
443 void SetWhitespaceVisible(int view
);
445 void Indent(bool forwards
);
447 long FindText(uptr_t wParam
, sptr_t lParam
);
449 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
450 long SearchInTarget(const char *text
, int length
);
451 void GoToLine(int lineNo
);
453 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
454 char *CopyRange(int start
, int end
);
455 void CopySelectionFromRange(SelectionText
*ss
, int start
, int end
);
456 void CopySelectionRange(SelectionText
*ss
);
457 void CopyRangeToClipboard(int start
, int end
);
458 void CopyText(int length
, const char *text
);
459 void SetDragPosition(int newPos
);
460 virtual void DisplayCursor(Window::Cursor c
);
461 virtual void StartDrag();
462 void DropAt(int position
, const char *value
, bool moving
, bool rectangular
);
463 /** PositionInSelection returns 0 if position in selection, -1 if position before selection, and 1 if after.
464 * Before means either before any line of selection or before selection on its line, with a similar meaning to after. */
465 int PositionInSelection(int pos
);
466 bool PointInSelection(Point pt
);
467 bool PointInSelMargin(Point pt
);
468 void LineSelection(int lineCurrent_
, int lineAnchor_
);
469 void DwellEnd(bool mouseMoved
);
470 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
471 void ButtonMove(Point pt
);
472 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
476 virtual void SetTicking(bool on
) = 0;
477 virtual bool SetIdle(bool) { return false; }
478 virtual void SetMouseCapture(bool on
) = 0;
479 virtual bool HaveMouseCapture() = 0;
480 void SetFocusState(bool focusState
);
482 void CheckForChangeOutsidePaint(Range r
);
483 int BraceMatch(int position
, int maxReStyle
);
484 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
486 void SetDocPointer(Document
*document
);
488 void Expand(int &line
, bool doExpand
);
489 void ToggleContraction(int line
);
490 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
491 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
493 bool PositionIsHotspot(int position
);
494 bool PointIsHotspot(Point pt
);
495 void SetHotSpotRange(Point
*pt
);
496 void GetHotSpotRange(int& hsStart
, int& hsEnd
);
498 int CodePage() const;
500 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
503 // Public so the COM thunks can access it.
504 bool IsUnicodeMode() const;
505 // Public so scintilla_send_message can use it.
506 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
507 // Public so scintilla_set_id can use it.
509 friend class AutoSurface
;
510 friend class SelectionLineIterator
;
514 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
520 AutoSurface(Editor
*ed
) : surf(0) {
521 if (ed
->wMain
.GetID()) {
522 surf
= Surface::Allocate();
524 surf
->Init(ed
->wMain
.GetID());
525 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
526 surf
->SetDBCSMode(ed
->CodePage());
530 AutoSurface(SurfaceID sid
, Editor
*ed
) : surf(0) {
531 if (ed
->wMain
.GetID()) {
532 surf
= Surface::Allocate();
534 surf
->Init(sid
, ed
->wMain
.GetID());
535 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
536 surf
->SetDBCSMode(ed
->CodePage());
543 Surface
*operator->() const {
546 operator Surface
*() const {