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.
60 SelectionText() : s(0), len(0), rectangular(false), lineCopy(false), codePage(0), characterSet(0) {}
65 Set(0, 0, 0, 0, false, false);
67 void Set(char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
, bool lineCopy_
) {
75 characterSet
= characterSet_
;
76 rectangular
= rectangular_
;
79 void Copy(const char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
, bool lineCopy_
) {
84 for (int i
= 0; i
< len_
; i
++) {
88 characterSet
= characterSet_
;
89 rectangular
= rectangular_
;
92 void Copy(const SelectionText
&other
) {
93 Copy(other
.s
, other
.len
, other
.codePage
, other
.characterSet
, other
.rectangular
, other
.lineCopy
);
99 class Editor
: public DocWatcher
{
100 // Private so Editor objects can not be copied
101 Editor(const Editor
&);
102 Editor
&operator=(const Editor
&);
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
;
125 bool mouseDownCaptures
;
127 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
128 * the screen. This avoids flashing but is about 30% slower. */
130 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
131 * and then the foreground. This avoids chopping off characters that overlap the next run. */
134 int xOffset
; ///< Horizontal scrolled amount in pixels
135 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
136 bool horizontalScrollBarVisible
;
139 int lineWidthMaxSeen
;
140 bool verticalScrollBarVisible
;
143 bool multipleSelection
;
144 bool additionalSelectionTyping
;
145 bool additionalCaretsBlink
;
146 bool additionalCaretsVisible
;
148 int virtualSpaceOptions
;
151 Surface
*pixmapSelMargin
;
152 Surface
*pixmapSelPattern
;
153 Surface
*pixmapIndentGuide
;
154 Surface
*pixmapIndentGuideHighlight
;
157 PositionCache posCache
;
163 Timer autoScrollTimer
;
164 enum { autoScrollDelay
= 200 };
169 unsigned int lastClickTime
;
173 enum { selChar
, selWord
, selLine
} selectionType
;
175 enum { ddNone
, ddInitial
, ddDragging
} inDragDrop
;
176 bool dropWentOutside
;
177 SelectionPosition posDrag
;
178 SelectionPosition posDrop
;
181 int originalAnchorPos
;
191 int bracesMatchStyle
;
192 int highlightGuideColumn
;
196 enum { notPainting
, painting
, paintAbandoned
} paintState
;
198 bool paintingAllText
;
204 bool primarySelection
;
207 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
210 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
227 enum { eWrapNone
, eWrapWord
, eWrapChar
} wrapState
;
228 enum { wrapLineLarge
= 0x7ffffff };
233 int wrapVisualFlagsLocation
;
234 int wrapVisualStartIndent
;
235 int wrapAddIndent
; // This will be added to initial indent of line
236 int wrapIndentMode
; // SC_WRAPINDENT_FIXED, _SAME, _INDENT
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 SelectionPosition
ClampPositionIntoDocument(SelectionPosition sp
) const;
260 Point
LocationFromPosition(SelectionPosition pos
);
261 Point
LocationFromPosition(int pos
);
262 int XFromPosition(int pos
);
263 int XFromPosition(SelectionPosition sp
);
264 SelectionPosition
SPositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false, bool virtualSpace
=true);
265 int PositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false);
266 SelectionPosition
SPositionFromLineX(int lineDoc
, int x
);
267 int PositionFromLineX(int line
, int x
);
268 int LineFromLocation(Point pt
);
269 void SetTopLine(int topLineNew
);
272 void RedrawRect(PRectangle rc
);
274 void RedrawSelMargin(int line
=-1);
275 PRectangle
RectangleFromRange(int start
, int end
);
276 void InvalidateRange(int start
, int end
);
278 bool UserVirtualSpace() const {
279 return ((virtualSpaceOptions
& SCVS_USERACCESSIBLE
) != 0);
281 int CurrentPosition();
282 bool SelectionEmpty();
283 SelectionPosition
SelectionStart();
284 SelectionPosition
SelectionEnd();
285 void SetRectangularRange();
286 void ThinRectangularRange();
287 void InvalidateSelection(SelectionRange newMain
, bool invalidateWholeSelection
=false);
288 void SetSelection(SelectionPosition currentPos_
, SelectionPosition anchor_
);
289 void SetSelection(int currentPos_
, int anchor_
);
290 void SetSelection(SelectionPosition currentPos_
);
291 void SetSelection(int currentPos_
);
292 void SetEmptySelection(SelectionPosition currentPos_
);
293 void SetEmptySelection(int currentPos_
);
294 bool RangeContainsProtected(int start
, int end
) const;
295 bool SelectionContainsProtected();
296 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true) const;
297 SelectionPosition
MovePositionOutsideChar(SelectionPosition pos
, int moveDir
, bool checkLineEnd
=true) const;
298 int MovePositionTo(SelectionPosition newPos
, Selection::selTypes sel
=Selection::noSel
, bool ensureVisible
=true);
299 int MovePositionTo(int newPos
, Selection::selTypes sel
=Selection::noSel
, bool ensureVisible
=true);
300 SelectionPosition
MovePositionSoVisible(SelectionPosition pos
, int moveDir
);
301 SelectionPosition
MovePositionSoVisible(int pos
, int moveDir
);
302 Point
PointMainCaret();
303 void SetLastXChosen();
305 void ScrollTo(int line
, bool moveThumb
=true);
306 virtual void ScrollText(int linesToMove
);
307 void HorizontalScrollTo(int xPos
);
308 void MoveCaretInsideView(bool ensureVisible
=true);
309 int DisplayFromPosition(int pos
);
310 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
311 void ShowCaretAtCurrentPosition();
313 void InvalidateCaret();
314 virtual void UpdateSystemCaret();
316 void NeedWrapping(int docLineStart
= 0, int docLineEnd
= wrapLineLarge
);
317 bool WrapOneLine(Surface
*surface
, int lineToWrap
);
318 bool WrapLines(bool fullWrap
, int priorityWrapLineStart
);
320 void LinesSplit(int pixelWidth
);
322 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
323 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
324 LineLayout
*RetrieveLineLayout(int lineNumber
);
325 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
326 int width
=LineLayout::wrapWidthInfinite
);
327 ColourAllocated
SelectionBackground(ViewStyle
&vsDraw
, bool main
);
328 ColourAllocated
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourAllocated background
, int inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
329 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
330 void DrawWrapMarker(Surface
*surface
, PRectangle rcPlace
, bool isEndMarker
, ColourAllocated wrapColour
);
331 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
332 int line
, int lineEnd
, int xStart
, int subLine
, int subLineStart
,
333 bool overrideBackground
, ColourAllocated background
,
334 bool drawWrapMark
, ColourAllocated wrapColour
);
335 void DrawIndicators(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
336 PRectangle rcLine
, LineLayout
*ll
, int subLine
, int lineEnd
, bool under
);
337 void DrawAnnotation(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
338 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
339 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
340 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
341 void DrawBlockCaret(Surface
*surface
, ViewStyle
&vsDraw
, LineLayout
*ll
, int subLine
,
342 int xStart
, int offset
, int posCaret
, PRectangle rcCaret
, ColourAllocated caretColour
);
343 void DrawCarets(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
344 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
345 void RefreshPixMaps(Surface
*surfaceWindow
);
346 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
347 long FormatRange(bool draw
, Sci_RangeToFormat
*pfr
);
348 int TextWidth(int style
, const char *text
);
350 virtual void SetVerticalScrollPos() = 0;
351 virtual void SetHorizontalScrollPos() = 0;
352 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
353 virtual void ReconfigureScrollBars();
354 void SetScrollBars();
357 void FilterSelections();
358 int InsertSpace(int position
, unsigned int spaces
);
359 void AddChar(char ch
);
360 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
361 void ClearSelection();
363 void ClearDocumentStyle();
365 void PasteRectangular(SelectionPosition pos
, const char *ptr
, int len
);
366 virtual void Copy() = 0;
367 virtual void CopyAllowLine();
368 virtual bool CanPaste();
369 virtual void Paste() = 0;
375 void DelCharBack(bool allowLineStartDeletion
);
376 virtual void ClaimSelection() = 0;
378 virtual void NotifyChange() = 0;
379 virtual void NotifyFocus(bool focus
);
380 virtual int GetCtrlID() { return ctrlID
; }
381 virtual void NotifyParent(SCNotification scn
) = 0;
382 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
383 void NotifyChar(int ch
);
384 void NotifySavePoint(bool isSavePoint
);
385 void NotifyModifyAttempt();
386 virtual void NotifyDoubleClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
387 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
388 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
389 void NotifyUpdateUI();
390 void NotifyPainted();
391 void NotifyIndicatorClick(bool click
, int position
, bool shift
, bool ctrl
, bool alt
);
392 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
393 void NotifyNeedShown(int pos
, int len
);
394 void NotifyDwelling(Point pt
, bool state
);
397 void NotifyModifyAttempt(Document
*document
, void *userData
);
398 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
399 void CheckModificationForWrap(DocModification mh
);
400 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
401 void NotifyDeleted(Document
*document
, void *userData
);
402 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
403 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
405 void PageMove(int direction
, Selection::selTypes sel
=Selection::noSel
, bool stuttered
= false);
406 void ChangeCaseOfSelection(bool makeUpperCase
);
407 void LineTranspose();
408 void Duplicate(bool forLine
);
409 virtual void CancelModes();
411 void CursorUpOrDown(int direction
, Selection::selTypes sel
=Selection::noSel
);
412 void ParaUpOrDown(int direction
, Selection::selTypes sel
=Selection::noSel
);
413 int StartEndDisplayLine(int pos
, bool start
);
414 virtual int KeyCommand(unsigned int iMessage
);
415 virtual int KeyDefault(int /* key */, int /*modifiers*/);
416 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
418 int GetWhitespaceVisible();
419 void SetWhitespaceVisible(int view
);
421 void Indent(bool forwards
);
423 long FindText(uptr_t wParam
, sptr_t lParam
);
425 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
426 long SearchInTarget(const char *text
, int length
);
427 void GoToLine(int lineNo
);
429 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
430 char *CopyRange(int start
, int end
);
431 void CopySelectionRange(SelectionText
*ss
, bool allowLineCopy
=false);
432 void CopyRangeToClipboard(int start
, int end
);
433 void CopyText(int length
, const char *text
);
434 void SetDragPosition(SelectionPosition newPos
);
435 virtual void DisplayCursor(Window::Cursor c
);
436 virtual bool DragThreshold(Point ptStart
, Point ptNow
);
437 virtual void StartDrag();
438 void DropAt(SelectionPosition position
, const char *value
, bool moving
, bool rectangular
);
439 /** PositionInSelection returns true if position in selection. */
440 bool PositionInSelection(int pos
);
441 bool PointInSelection(Point pt
);
442 bool PointInSelMargin(Point pt
);
443 void LineSelection(int lineCurrent_
, int lineAnchor_
);
444 void DwellEnd(bool mouseMoved
);
445 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
446 void ButtonMove(Point pt
);
447 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
451 virtual void SetTicking(bool on
) = 0;
452 virtual bool SetIdle(bool) { return false; }
453 virtual void SetMouseCapture(bool on
) = 0;
454 virtual bool HaveMouseCapture() = 0;
455 void SetFocusState(bool focusState
);
457 virtual bool PaintContains(PRectangle rc
);
458 bool PaintContainsMargin();
459 void CheckForChangeOutsidePaint(Range r
);
460 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
462 void SetAnnotationHeights(int start
, int end
);
463 void SetDocPointer(Document
*document
);
465 void SetAnnotationVisible(int visible
);
467 void Expand(int &line
, bool doExpand
);
468 void ToggleContraction(int line
);
469 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
470 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
472 bool PositionIsHotspot(int position
);
473 bool PointIsHotspot(Point pt
);
474 void SetHotSpotRange(Point
*pt
);
475 void GetHotSpotRange(int& hsStart
, int& hsEnd
);
477 int CodePage() const;
478 virtual bool ValidCodePage(int /* codePage */) const { return true; }
479 int WrapCount(int line
);
480 void AddStyledText(char *buffer
, int appendLength
);
482 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
483 void StyleSetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
484 sptr_t
StyleGetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
486 static const char *StringFromEOLMode(int eolMode
);
488 static sptr_t
StringResult(sptr_t lParam
, const char *val
);
491 // Public so the COM thunks can access it.
492 bool IsUnicodeMode() const;
493 // Public so scintilla_send_message can use it.
494 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
495 // Public so scintilla_set_id can use it.
497 // Public so COM methods for drag and drop can set it.
499 friend class AutoSurface
;
500 friend class SelectionLineIterator
;
504 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
510 AutoSurface(Editor
*ed
) : surf(0) {
511 if (ed
->wMain
.GetID()) {
512 surf
= Surface::Allocate();
514 surf
->Init(ed
->wMain
.GetID());
515 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
516 surf
->SetDBCSMode(ed
->CodePage());
520 AutoSurface(SurfaceID sid
, Editor
*ed
) : surf(0) {
521 if (ed
->wMain
.GetID()) {
522 surf
= Surface::Allocate();
524 surf
->Init(sid
, ed
->wMain
.GetID());
525 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
526 surf
->SetDBCSMode(ed
->CodePage());
533 Surface
*operator->() const {
536 operator Surface
*() const {