1 // Scintilla source code edit control
3 ** Defines the main editor class.
5 // Copyright 1998-2011 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
21 enum {tickSize
= 100};
38 * When platform has a way to generate an event before painting,
39 * accumulate needed styling range and other work items in
40 * WorkNeeded to avoid unnecessary work inside paint handler
53 WorkNeeded() : active(false), items(workNone
), upTo(0) {}
59 void Need(workItems items_
, Position pos
) {
60 if ((items_
& workStyle
) && (upTo
< pos
))
62 items
= static_cast<workItems
>(items
| items_
);
67 * Hold a piece of text selected for copying or dragging, along with encoding and selection format information.
76 SelectionText() : rectangular(false), lineCopy(false), codePage(0), characterSet(0) {}
86 void Copy(const std::string
&s_
, int codePage_
, int characterSet_
, bool rectangular_
, bool lineCopy_
) {
89 characterSet
= characterSet_
;
90 rectangular
= rectangular_
;
92 FixSelectionForClipboard();
94 void Copy(const SelectionText
&other
) {
95 Copy(other
.s
, other
.codePage
, other
.characterSet
, other
.rectangular
, other
.lineCopy
);
97 const char *Data() const {
100 size_t Length() const {
103 size_t LengthWithTerminator() const {
104 return s
.length() + 1;
110 void FixSelectionForClipboard() {
111 // To avoid truncating the contents of the clipboard when pasted where the
112 // clipboard contains NUL characters, replace NUL characters by spaces.
113 std::replace(s
.begin(), s
.end(), '\0', ' ');
118 // The range of lines that need to be wrapped
119 enum { lineLarge
= 0x7ffffff };
120 int start
; // When there are wraps pending, will be in document range
121 int end
; // May be lineLarge to indicate all of document after start
130 void Wrapped(int line
) {
134 bool NeedsWrap() const {
137 bool AddRange(int lineStart
, int lineEnd
) {
138 const bool neededWrap
= NeedsWrap();
139 bool changed
= false;
140 if (start
> lineStart
) {
144 if ((end
< lineEnd
) || !neededWrap
) {
154 class Editor
: public EditModel
, public DocWatcher
{
155 // Private so Editor objects can not be copied
156 explicit Editor(const Editor
&);
157 Editor
&operator=(const Editor
&);
159 protected: // ScintillaBase subclass needs access to much of Editor
161 /** On GTK+, Scintilla is a container widget holding two scroll bars
162 * whereas on Windows there is just one window with both scroll bars turned on. */
163 Window wMain
; ///< The Scintilla parent window
164 Window wMargin
; ///< May be separate when using a scroll view for wMain
166 /** Style resources may be expensive to allocate so are cached between uses.
167 * When a style attribute is changed, this cache is flushed. */
172 float scaleRGBAImage
;
174 MarginView marginView
;
180 bool mouseDownCaptures
;
182 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
183 bool horizontalScrollBarVisible
;
185 bool verticalScrollBarVisible
;
189 bool mouseSelectionRectangularSwitch
;
190 bool multipleSelection
;
191 bool additionalSelectionTyping
;
194 int virtualSpaceOptions
;
199 Timer autoScrollTimer
;
200 enum { autoScrollDelay
= 200 };
205 unsigned int lastClickTime
;
206 Point doubleClickCloseThreshold
;
210 enum { selChar
, selWord
, selSubLine
, selWholeLine
} selectionType
;
212 enum { ddNone
, ddInitial
, ddDragging
} inDragDrop
;
213 bool dropWentOutside
;
214 SelectionPosition posDrop
;
218 int originalAnchorPos
;
219 int wordSelectAnchorStartPos
;
220 int wordSelectAnchorEndPos
;
221 int wordSelectInitialCaretPos
;
231 enum { notPainting
, painting
, paintAbandoned
} paintState
;
232 bool paintAbandonedByStyling
;
234 bool paintingAllText
;
236 WorkNeeded workNeeded
;
243 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
246 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
258 WrapPending wrapPending
;
264 virtual void Initialise() = 0;
265 virtual void Finalise();
267 void InvalidateStyleData();
268 void InvalidateStyleRedraw();
269 void RefreshStyleData();
270 void SetRepresentations();
271 void DropGraphics(bool freeObjects
);
272 void AllocateGraphics();
274 // The top left visible point in main window coordinates. Will be 0,0 except for
275 // scroll views where it will be equivalent to the current scroll position.
276 virtual Point
GetVisibleOriginInMain() const;
277 Point
DocumentPointFromView(Point ptView
) const; // Convert a point from view space to document
278 int TopLineOfMain() const; // Return the line at Main's y coordinate 0
279 virtual PRectangle
GetClientRectangle() const;
280 virtual PRectangle
GetClientDrawingRectangle();
281 PRectangle
GetTextRectangle() const;
283 virtual int LinesOnScreen() const;
284 int LinesToScroll() const;
285 int MaxScrollPos() const;
286 SelectionPosition
ClampPositionIntoDocument(SelectionPosition sp
) const;
287 Point
LocationFromPosition(SelectionPosition pos
);
288 Point
LocationFromPosition(int pos
);
289 int XFromPosition(int pos
);
290 int XFromPosition(SelectionPosition sp
);
291 SelectionPosition
SPositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false, bool virtualSpace
=true);
292 int PositionFromLocation(Point pt
, bool canReturnInvalid
= false, bool charPosition
= false);
293 SelectionPosition
SPositionFromLineX(int lineDoc
, int x
);
294 int PositionFromLineX(int line
, int x
);
295 int LineFromLocation(Point pt
) const;
296 void SetTopLine(int topLineNew
);
298 virtual bool AbandonPaint();
299 virtual void RedrawRect(PRectangle rc
);
300 virtual void DiscardOverdraw();
301 virtual void Redraw();
302 void RedrawSelMargin(int line
=-1, bool allAfter
=false);
303 PRectangle
RectangleFromRange(Range r
, int overlap
);
304 void InvalidateRange(int start
, int end
);
306 bool UserVirtualSpace() const {
307 return ((virtualSpaceOptions
& SCVS_USERACCESSIBLE
) != 0);
309 int CurrentPosition() const;
310 bool SelectionEmpty() const;
311 SelectionPosition
SelectionStart();
312 SelectionPosition
SelectionEnd();
313 void SetRectangularRange();
314 void ThinRectangularRange();
315 void InvalidateSelection(SelectionRange newMain
, bool invalidateWholeSelection
=false);
316 void InvalidateWholeSelection();
317 void SetSelection(SelectionPosition currentPos_
, SelectionPosition anchor_
);
318 void SetSelection(int currentPos_
, int anchor_
);
319 void SetSelection(SelectionPosition currentPos_
);
320 void SetSelection(int currentPos_
);
321 void SetEmptySelection(SelectionPosition currentPos_
);
322 void SetEmptySelection(int currentPos_
);
323 enum AddNumber
{ addOne
, addEach
};
324 void MultipleSelectAdd(AddNumber addNumber
);
325 bool RangeContainsProtected(int start
, int end
) const;
326 bool SelectionContainsProtected();
327 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true) const;
328 SelectionPosition
MovePositionOutsideChar(SelectionPosition pos
, int moveDir
, bool checkLineEnd
=true) const;
329 void MovedCaret(SelectionPosition newPos
, SelectionPosition previousPos
, bool ensureVisible
);
330 void MovePositionTo(SelectionPosition newPos
, Selection::selTypes selt
=Selection::noSel
, bool ensureVisible
=true);
331 void MovePositionTo(int newPos
, Selection::selTypes selt
=Selection::noSel
, bool ensureVisible
=true);
332 SelectionPosition
MovePositionSoVisible(SelectionPosition pos
, int moveDir
);
333 SelectionPosition
MovePositionSoVisible(int pos
, int moveDir
);
334 Point
PointMainCaret();
335 void SetLastXChosen();
337 void ScrollTo(int line
, bool moveThumb
=true);
338 virtual void ScrollText(int linesToMove
);
339 void HorizontalScrollTo(int xPos
);
340 void VerticalCentreCaret();
341 void MoveSelectedLines(int lineDelta
);
342 void MoveSelectedLinesUp();
343 void MoveSelectedLinesDown();
344 void MoveCaretInsideView(bool ensureVisible
=true);
345 int DisplayFromPosition(int pos
);
347 struct XYScrollPosition
{
350 XYScrollPosition(int xOffset_
, int topLine_
) : xOffset(xOffset_
), topLine(topLine_
) {}
351 bool operator==(const XYScrollPosition
&other
) const {
352 return (xOffset
== other
.xOffset
) && (topLine
== other
.topLine
);
355 enum XYScrollOptions
{
359 xysDefault
=xysUseMargin
|xysVertical
|xysHorizontal
};
360 XYScrollPosition
XYScrollToMakeVisible(const SelectionRange
&range
, const XYScrollOptions options
);
361 void SetXYScroll(XYScrollPosition newXY
);
362 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
363 void ScrollRange(SelectionRange range
);
364 void ShowCaretAtCurrentPosition();
366 void CaretSetPeriod(int period
);
367 void InvalidateCaret();
368 virtual void UpdateSystemCaret();
370 bool Wrapping() const;
371 void NeedWrapping(int docLineStart
=0, int docLineEnd
=WrapPending::lineLarge
);
372 bool WrapOneLine(Surface
*surface
, int lineToWrap
);
373 enum wrapScope
{wsAll
, wsVisible
, wsIdle
};
374 bool WrapLines(enum wrapScope ws
);
376 void LinesSplit(int pixelWidth
);
378 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
379 void RefreshPixMaps(Surface
*surfaceWindow
);
380 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
381 long FormatRange(bool draw
, Sci_RangeToFormat
*pfr
);
382 int TextWidth(int style
, const char *text
);
384 virtual void SetVerticalScrollPos() = 0;
385 virtual void SetHorizontalScrollPos() = 0;
386 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
387 virtual void ReconfigureScrollBars();
388 void SetScrollBars();
391 void FilterSelections();
392 int InsertSpace(int position
, unsigned int spaces
);
393 void AddChar(char ch
);
394 virtual void AddCharUTF(const char *s
, unsigned int len
, bool treatAsDBCS
=false);
395 void ClearBeforeTentativeStart();
396 void InsertPaste(const char *text
, int len
);
397 enum PasteShape
{ pasteStream
=0, pasteRectangular
= 1, pasteLine
= 2 };
398 void InsertPasteShape(const char *text
, int len
, PasteShape shape
);
399 void ClearSelection(bool retainMultipleSelections
= false);
401 void ClearDocumentStyle();
403 void PasteRectangular(SelectionPosition pos
, const char *ptr
, int len
);
404 virtual void Copy() = 0;
405 virtual void CopyAllowLine();
406 virtual bool CanPaste();
407 virtual void Paste() = 0;
412 void DelCharBack(bool allowLineStartDeletion
);
413 virtual void ClaimSelection() = 0;
415 static int ModifierFlags(bool shift
, bool ctrl
, bool alt
, bool meta
=false);
416 virtual void NotifyChange() = 0;
417 virtual void NotifyFocus(bool focus
);
418 virtual void SetCtrlID(int identifier
);
419 virtual int GetCtrlID() { return ctrlID
; }
420 virtual void NotifyParent(SCNotification scn
) = 0;
421 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
422 void NotifyChar(int ch
);
423 void NotifySavePoint(bool isSavePoint
);
424 void NotifyModifyAttempt();
425 virtual void NotifyDoubleClick(Point pt
, int modifiers
);
426 virtual void NotifyDoubleClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
427 void NotifyHotSpotClicked(int position
, int modifiers
);
428 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
429 void NotifyHotSpotDoubleClicked(int position
, int modifiers
);
430 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
431 void NotifyHotSpotReleaseClick(int position
, int modifiers
);
432 void NotifyHotSpotReleaseClick(int position
, bool shift
, bool ctrl
, bool alt
);
433 bool NotifyUpdateUI();
434 void NotifyPainted();
435 void NotifyIndicatorClick(bool click
, int position
, int modifiers
);
436 void NotifyIndicatorClick(bool click
, int position
, bool shift
, bool ctrl
, bool alt
);
437 bool NotifyMarginClick(Point pt
, int modifiers
);
438 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
439 void NotifyNeedShown(int pos
, int len
);
440 void NotifyDwelling(Point pt
, bool state
);
443 void NotifyModifyAttempt(Document
*document
, void *userData
);
444 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
445 void CheckModificationForWrap(DocModification mh
);
446 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
447 void NotifyDeleted(Document
*document
, void *userData
);
448 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
449 void NotifyLexerChanged(Document
*doc
, void *userData
);
450 void NotifyErrorOccurred(Document
*doc
, void *userData
, int status
);
451 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
453 void ContainerNeedsUpdate(int flags
);
454 void PageMove(int direction
, Selection::selTypes selt
=Selection::noSel
, bool stuttered
= false);
455 enum { cmSame
, cmUpper
, cmLower
};
456 virtual std::string
CaseMapString(const std::string
&s
, int caseMapping
);
457 void ChangeCaseOfSelection(int caseMapping
);
458 void LineTranspose();
459 void Duplicate(bool forLine
);
460 virtual void CancelModes();
462 SelectionPosition
PositionUpOrDown(SelectionPosition spStart
, int direction
, int lastX
);
463 void CursorUpOrDown(int direction
, Selection::selTypes selt
);
464 void ParaUpOrDown(int direction
, Selection::selTypes selt
);
465 int StartEndDisplayLine(int pos
, bool start
);
466 int VCHomeDisplayPosition(int position
);
467 int VCHomeWrapPosition(int position
);
468 int LineEndWrapPosition(int position
);
469 int HorizontalMove(unsigned int iMessage
);
470 int DelWordOrLine(unsigned int iMessage
);
471 virtual int KeyCommand(unsigned int iMessage
);
472 virtual int KeyDefault(int /* key */, int /*modifiers*/);
473 int KeyDownWithModifiers(int key
, int modifiers
, bool *consumed
);
474 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
476 void Indent(bool forwards
);
478 virtual CaseFolder
*CaseFolderForEncoding();
479 long FindText(uptr_t wParam
, sptr_t lParam
);
481 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
482 long SearchInTarget(const char *text
, int length
);
483 void GoToLine(int lineNo
);
485 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
486 std::string
RangeText(int start
, int end
) const;
487 void CopySelectionRange(SelectionText
*ss
, bool allowLineCopy
=false);
488 void CopyRangeToClipboard(int start
, int end
);
489 void CopyText(int length
, const char *text
);
490 void SetDragPosition(SelectionPosition newPos
);
491 virtual void DisplayCursor(Window::Cursor c
);
492 virtual bool DragThreshold(Point ptStart
, Point ptNow
);
493 virtual void StartDrag();
494 void DropAt(SelectionPosition position
, const char *value
, size_t lengthValue
, bool moving
, bool rectangular
);
495 void DropAt(SelectionPosition position
, const char *value
, bool moving
, bool rectangular
);
496 /** PositionInSelection returns true if position in selection. */
497 bool PositionInSelection(int pos
);
498 bool PointInSelection(Point pt
);
499 bool PointInSelMargin(Point pt
) const;
500 Window::Cursor
GetMarginCursor(Point pt
) const;
501 void TrimAndSetSelection(int currentPos_
, int anchor_
);
502 void LineSelection(int lineCurrentPos_
, int lineAnchorPos_
, bool wholeLine
);
503 void WordSelection(int pos
);
504 void DwellEnd(bool mouseMoved
);
506 virtual void ButtonDownWithModifiers(Point pt
, unsigned int curTime
, int modifiers
);
507 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
508 void ButtonMoveWithModifiers(Point pt
, int modifiers
);
509 void ButtonMove(Point pt
);
510 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
514 virtual void SetTicking(bool on
);
515 enum TickReason
{ tickCaret
, tickScroll
, tickWiden
, tickDwell
, tickPlatform
};
516 virtual void TickFor(TickReason reason
);
517 virtual bool FineTickerAvailable();
518 virtual bool FineTickerRunning(TickReason reason
);
519 virtual void FineTickerStart(TickReason reason
, int millis
, int tolerance
);
520 virtual void FineTickerCancel(TickReason reason
);
521 virtual bool SetIdle(bool) { return false; }
522 virtual void SetMouseCapture(bool on
) = 0;
523 virtual bool HaveMouseCapture() = 0;
524 void SetFocusState(bool focusState
);
526 int PositionAfterArea(PRectangle rcArea
) const;
527 void StyleToPositionInView(Position pos
);
528 virtual void IdleWork();
529 virtual void QueueIdleWork(WorkNeeded::workItems items
, int upTo
=0);
531 virtual bool PaintContains(PRectangle rc
);
532 bool PaintContainsMargin();
533 void CheckForChangeOutsidePaint(Range r
);
534 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
536 void SetAnnotationHeights(int start
, int end
);
537 virtual void SetDocPointer(Document
*document
);
539 void SetAnnotationVisible(int visible
);
541 int ExpandLine(int line
);
542 void SetFoldExpanded(int lineDoc
, bool expanded
);
543 void FoldLine(int line
, int action
);
544 void FoldExpand(int line
, int action
, int level
);
545 int ContractedFoldNext(int lineStart
) const;
546 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
547 void FoldChanged(int line
, int levelNow
, int levelPrev
);
548 void NeedShown(int pos
, int len
);
549 void FoldAll(int action
);
551 int GetTag(char *tagValue
, int tagNumber
);
552 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
554 bool PositionIsHotspot(int position
) const;
555 bool PointIsHotspot(Point pt
);
556 void SetHotSpotRange(Point
*pt
);
557 Range
GetHotSpotRange() const;
558 void SetHoverIndicatorPosition(int position
);
559 void SetHoverIndicatorPoint(Point pt
);
561 int CodePage() const;
562 virtual bool ValidCodePage(int /* codePage */) const { return true; }
563 int WrapCount(int line
);
564 void AddStyledText(char *buffer
, int appendLength
);
566 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
567 void StyleSetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
568 sptr_t
StyleGetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
570 static const char *StringFromEOLMode(int eolMode
);
572 static sptr_t
StringResult(sptr_t lParam
, const char *val
);
573 static sptr_t
BytesResult(sptr_t lParam
, const unsigned char *val
, size_t len
);
576 // Public so the COM thunks can access it.
577 bool IsUnicodeMode() const;
578 // Public so scintilla_send_message can use it.
579 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
580 // Public so scintilla_set_id can use it.
582 // Public so COM methods for drag and drop can set it.
584 friend class AutoSurface
;
585 friend class SelectionLineIterator
;
589 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
595 AutoSurface(Editor
*ed
, int technology
= -1) : surf(0) {
596 if (ed
->wMain
.GetID()) {
597 surf
= Surface::Allocate(technology
!= -1 ? technology
: ed
->technology
);
599 surf
->Init(ed
->wMain
.GetID());
600 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
601 surf
->SetDBCSMode(ed
->CodePage());
605 AutoSurface(SurfaceID sid
, Editor
*ed
, int technology
= -1) : surf(0) {
606 if (ed
->wMain
.GetID()) {
607 surf
= Surface::Allocate(technology
!= -1 ? technology
: ed
->technology
);
609 surf
->Init(sid
, ed
->wMain
.GetID());
610 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
611 surf
->SetDBCSMode(ed
->CodePage());
618 Surface
*operator->() const {
621 operator Surface
*() const {