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.
32 enum {tickSize
= 100};
49 * Hold a piece of text selected for copying or dragging.
50 * The text is expected to hold a terminating '\0' and this is counted in len.
59 SelectionText() : s(0), len(0), rectangular(false), codePage(0), characterSet(0) {}
64 Set(0, 0, 0, 0, false);
66 void Set(char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
) {
74 characterSet
= characterSet_
;
75 rectangular
= rectangular_
;
77 void Copy(const char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
) {
82 for (int i
= 0; i
< len_
; i
++) {
89 characterSet
= characterSet_
;
90 rectangular
= rectangular_
;
92 void Copy(const SelectionText
&other
) {
93 Copy(other
.s
, other
.len
, other
.codePage
, other
.characterSet
, other
.rectangular
);
99 class Editor
: public DocWatcher
{
100 // Private so Editor objects can not be copied
101 Editor(const Editor
&) : DocWatcher() {}
102 Editor
&operator=(const Editor
&) { return *this; }
104 protected: // ScintillaBase subclass needs access to much of Editor
106 /** On GTK+, Scintilla is a container widget holding two scroll bars
107 * whereas on Windows there is just one window with both scroll bars turned on. */
108 Window wMain
; ///< The Scintilla parent window
110 /** Style resources may be expensive to allocate so are cached between uses.
111 * When a style attribute is changed, this cache is flushed. */
116 int printMagnification
;
120 int controlCharSymbol
;
126 bool mouseDownCaptures
;
128 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
129 * the screen. This avoids flashing but is about 30% slower. */
131 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
132 * and then the foreground. This avoids chopping off characters that overlap the next run. */
135 int xOffset
; ///< Horizontal scrolled amount in pixels
136 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
137 bool horizontalScrollBarVisible
;
140 int lineWidthMaxSeen
;
141 bool verticalScrollBarVisible
;
146 Surface
*pixmapSelMargin
;
147 Surface
*pixmapSelPattern
;
148 Surface
*pixmapIndentGuide
;
149 Surface
*pixmapIndentGuideHighlight
;
152 PositionCache posCache
;
158 Timer autoScrollTimer
;
159 enum { autoScrollDelay
= 200 };
164 unsigned int lastClickTime
;
168 enum { selChar
, selWord
, selLine
} selectionType
;
170 enum { ddNone
, ddInitial
, ddDragging
} inDragDrop
;
171 bool dropWentOutside
;
176 int originalAnchorPos
;
188 int bracesMatchStyle
;
189 int highlightGuideColumn
;
193 enum { notPainting
, painting
, paintAbandoned
} paintState
;
195 bool paintingAllText
;
200 enum selTypes
{ noSel
, selStream
, selRectangle
, selLines
};
202 bool moveExtendsSelection
;
203 int xStartSelect
; ///< x position of start of rectangular selection
204 int xEndSelect
; ///< x position of end of rectangular selection
205 bool primarySelection
;
208 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
211 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
228 enum { eWrapNone
, eWrapWord
, eWrapChar
} wrapState
;
229 enum { wrapLineLarge
= 0x7ffffff };
234 int wrapVisualFlagsLocation
;
235 int wrapVisualStartIndent
;
236 int actualWrapVisualStartIndent
;
244 virtual void Initialise() = 0;
245 virtual void Finalise();
247 void InvalidateStyleData();
248 void InvalidateStyleRedraw();
249 virtual void RefreshColourPalette(Palette
&pal
, bool want
);
250 void RefreshStyleData();
253 virtual PRectangle
GetClientRectangle();
254 PRectangle
GetTextRectangle();
259 Point
LocationFromPosition(int pos
);
260 int XFromPosition(int pos
);
261 int PositionFromLocation(Point pt
);
262 int PositionFromLocationClose(Point pt
);
263 int PositionFromLineX(int line
, int x
);
264 int LineFromLocation(Point pt
);
265 void SetTopLine(int topLineNew
);
268 void RedrawRect(PRectangle rc
);
270 void RedrawSelMargin(int line
=-1);
271 PRectangle
RectangleFromRange(int start
, int end
);
272 void InvalidateRange(int start
, int end
);
274 int CurrentPosition();
275 bool SelectionEmpty();
276 int SelectionStart();
278 void SetRectangularRange();
279 void InvalidateSelection(int currentPos_
, int anchor_
);
280 void SetSelection(int currentPos_
, int anchor_
);
281 void SetSelection(int currentPos_
);
282 void SetEmptySelection(int currentPos_
);
283 bool RangeContainsProtected(int start
, int end
) const;
284 bool SelectionContainsProtected();
285 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true);
286 int MovePositionTo(int newPos
, selTypes sel
=noSel
, bool ensureVisible
=true);
287 int MovePositionSoVisible(int pos
, int moveDir
);
288 void SetLastXChosen();
290 void ScrollTo(int line
, bool moveThumb
=true);
291 virtual void ScrollText(int linesToMove
);
292 void HorizontalScrollTo(int xPos
);
293 void MoveCaretInsideView(bool ensureVisible
=true);
294 int DisplayFromPosition(int pos
);
295 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
296 void ShowCaretAtCurrentPosition();
298 void InvalidateCaret();
299 virtual void UpdateSystemCaret();
301 void NeedWrapping(int docLineStart
= 0, int docLineEnd
= wrapLineLarge
);
302 bool WrapOneLine(Surface
*surface
, int lineToWrap
);
303 bool WrapLines(bool fullWrap
, int priorityWrapLineStart
);
305 void LinesSplit(int pixelWidth
);
307 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
308 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
309 LineLayout
*RetrieveLineLayout(int lineNumber
);
310 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
311 int width
=LineLayout::wrapWidthInfinite
);
312 ColourAllocated
SelectionBackground(ViewStyle
&vsDraw
);
313 ColourAllocated
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourAllocated background
, bool inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
314 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
315 void DrawWrapMarker(Surface
*surface
, PRectangle rcPlace
, bool isEndMarker
, ColourAllocated wrapColour
);
316 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
317 int line
, int lineEnd
, int xStart
, int subLine
, int subLineStart
,
318 bool overrideBackground
, ColourAllocated background
,
319 bool drawWrapMark
, ColourAllocated wrapColour
);
320 void DrawIndicators(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
321 PRectangle rcLine
, LineLayout
*ll
, int subLine
, int lineEnd
, bool under
);
322 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
323 PRectangle rcLine
, LineLayout
*ll
, int subLine
=0);
324 void DrawBlockCaret(Surface
*surface
, ViewStyle
&vsDraw
, LineLayout
*ll
, int subLine
, int xStart
, int offset
, int posCaret
, PRectangle rcCaret
);
325 void RefreshPixMaps(Surface
*surfaceWindow
);
326 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
327 long FormatRange(bool draw
, RangeToFormat
*pfr
);
328 int TextWidth(int style
, const char *text
);
330 virtual void SetVerticalScrollPos() = 0;
331 virtual void SetHorizontalScrollPos() = 0;
332 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
333 virtual void ReconfigureScrollBars();
334 void SetScrollBars();
337 void AddChar(char ch
);
338 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
339 void ClearSelection();
341 void ClearDocumentStyle();
343 void PasteRectangular(int pos
, const char *ptr
, int len
);
344 virtual void Copy() = 0;
345 virtual bool CanPaste();
346 virtual void Paste() = 0;
352 void DelCharBack(bool allowLineStartDeletion
);
353 virtual void ClaimSelection() = 0;
355 virtual void NotifyChange() = 0;
356 virtual void NotifyFocus(bool focus
);
357 virtual int GetCtrlID() { return ctrlID
; }
358 virtual void NotifyParent(SCNotification scn
) = 0;
359 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
360 void NotifyChar(int ch
);
361 void NotifyMove(int position
);
362 void NotifySavePoint(bool isSavePoint
);
363 void NotifyModifyAttempt();
364 virtual void NotifyDoubleClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
365 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
366 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
367 void NotifyUpdateUI();
368 void NotifyPainted();
369 void NotifyIndicatorClick(bool click
, int position
, bool shift
, bool ctrl
, bool alt
);
370 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
371 void NotifyNeedShown(int pos
, int len
);
372 void NotifyDwelling(Point pt
, bool state
);
375 void NotifyModifyAttempt(Document
*document
, void *userData
);
376 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
377 void CheckModificationForWrap(DocModification mh
);
378 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
379 void NotifyDeleted(Document
*document
, void *userData
);
380 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
381 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
383 void PageMove(int direction
, selTypes sel
=noSel
, bool stuttered
= false);
384 void ChangeCaseOfSelection(bool makeUpperCase
);
385 void LineTranspose();
386 void Duplicate(bool forLine
);
387 virtual void CancelModes();
389 void CursorUpOrDown(int direction
, selTypes sel
=noSel
);
390 void ParaUpOrDown(int direction
, selTypes sel
=noSel
);
391 int StartEndDisplayLine(int pos
, bool start
);
392 virtual int KeyCommand(unsigned int iMessage
);
393 virtual int KeyDefault(int /* key */, int /*modifiers*/);
394 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
396 int GetWhitespaceVisible();
397 void SetWhitespaceVisible(int view
);
399 void Indent(bool forwards
);
401 long FindText(uptr_t wParam
, sptr_t lParam
);
403 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
404 long SearchInTarget(const char *text
, int length
);
405 void GoToLine(int lineNo
);
407 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
408 char *CopyRange(int start
, int end
);
409 void CopySelectionFromRange(SelectionText
*ss
, int start
, int end
);
410 void CopySelectionRange(SelectionText
*ss
);
411 void CopyRangeToClipboard(int start
, int end
);
412 void CopyText(int length
, const char *text
);
413 void SetDragPosition(int newPos
);
414 virtual void DisplayCursor(Window::Cursor c
);
415 virtual bool DragThreshold(Point ptStart
, Point ptNow
);
416 virtual void StartDrag();
417 void DropAt(int position
, const char *value
, bool moving
, bool rectangular
);
418 /** PositionInSelection returns 0 if position in selection, -1 if position before selection, and 1 if after.
419 * Before means either before any line of selection or before selection on its line, with a similar meaning to after. */
420 int PositionInSelection(int pos
);
421 bool PointInSelection(Point pt
);
422 bool PointInSelMargin(Point pt
);
423 void LineSelection(int lineCurrent_
, int lineAnchor_
);
424 void DwellEnd(bool mouseMoved
);
425 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
426 void ButtonMove(Point pt
);
427 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
431 virtual void SetTicking(bool on
) = 0;
432 virtual bool SetIdle(bool) { return false; }
433 virtual void SetMouseCapture(bool on
) = 0;
434 virtual bool HaveMouseCapture() = 0;
435 void SetFocusState(bool focusState
);
437 virtual bool PaintContains(PRectangle rc
);
438 bool PaintContainsMargin();
439 void CheckForChangeOutsidePaint(Range r
);
440 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
442 void SetDocPointer(Document
*document
);
444 void Expand(int &line
, bool doExpand
);
445 void ToggleContraction(int line
);
446 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
447 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
449 bool PositionIsHotspot(int position
);
450 bool PointIsHotspot(Point pt
);
451 void SetHotSpotRange(Point
*pt
);
452 void GetHotSpotRange(int& hsStart
, int& hsEnd
);
454 int CodePage() const;
455 virtual bool ValidCodePage(int /* codePage */) const { return true; }
456 int WrapCount(int line
);
457 void AddStyledText(char *buffer
, int appendLength
);
459 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
460 void StyleSetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
461 sptr_t
StyleGetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
464 // Public so the COM thunks can access it.
465 bool IsUnicodeMode() const;
466 // Public so scintilla_send_message can use it.
467 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
468 // Public so scintilla_set_id can use it.
470 friend class AutoSurface
;
471 friend class SelectionLineIterator
;
475 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
481 AutoSurface(Editor
*ed
) : surf(0) {
482 if (ed
->wMain
.GetID()) {
483 surf
= Surface::Allocate();
485 surf
->Init(ed
->wMain
.GetID());
486 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
487 surf
->SetDBCSMode(ed
->CodePage());
491 AutoSurface(SurfaceID sid
, Editor
*ed
) : surf(0) {
492 if (ed
->wMain
.GetID()) {
493 surf
= Surface::Allocate();
495 surf
->Init(sid
, ed
->wMain
.GetID());
496 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
497 surf
->SetDBCSMode(ed
->CodePage());
504 Surface
*operator->() const {
507 operator Surface
*() const {