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 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
;
209 enum { selChar
, selWord
, selSubLine
, selWholeLine
} selectionType
;
211 enum { ddNone
, ddInitial
, ddDragging
} inDragDrop
;
212 bool dropWentOutside
;
213 SelectionPosition posDrop
;
217 int originalAnchorPos
;
218 int wordSelectAnchorStartPos
;
219 int wordSelectAnchorEndPos
;
220 int wordSelectInitialCaretPos
;
230 enum { notPainting
, painting
, paintAbandoned
} paintState
;
231 bool paintAbandonedByStyling
;
233 bool paintingAllText
;
235 WorkNeeded workNeeded
;
242 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
245 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
257 WrapPending wrapPending
;
263 virtual void Initialise() = 0;
264 virtual void Finalise();
266 void InvalidateStyleData();
267 void InvalidateStyleRedraw();
268 void RefreshStyleData();
269 void SetRepresentations();
270 void DropGraphics(bool freeObjects
);
271 void AllocateGraphics();
273 // The top left visible point in main window coordinates. Will be 0,0 except for
274 // scroll views where it will be equivalent to the current scroll position.
275 virtual Point
GetVisibleOriginInMain() const;
276 Point
DocumentPointFromView(Point ptView
) const; // Convert a point from view space to document
277 int TopLineOfMain() const; // Return the line at Main's y coordinate 0
278 virtual PRectangle
GetClientRectangle() const;
279 virtual PRectangle
GetClientDrawingRectangle();
280 PRectangle
GetTextRectangle() const;
282 virtual int LinesOnScreen() const;
283 int LinesToScroll() const;
284 int MaxScrollPos() const;
285 SelectionPosition
ClampPositionIntoDocument(SelectionPosition sp
) const;
286 Point
LocationFromPosition(SelectionPosition pos
);
287 Point
LocationFromPosition(int pos
);
288 int XFromPosition(int pos
);
289 int XFromPosition(SelectionPosition sp
);
290 SelectionPosition
SPositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false, bool virtualSpace
=true);
291 int PositionFromLocation(Point pt
, bool canReturnInvalid
= false, bool charPosition
= false);
292 SelectionPosition
SPositionFromLineX(int lineDoc
, int x
);
293 int PositionFromLineX(int line
, int x
);
294 int LineFromLocation(Point pt
) const;
295 void SetTopLine(int topLineNew
);
297 virtual bool AbandonPaint();
298 virtual void RedrawRect(PRectangle rc
);
299 virtual void DiscardOverdraw();
300 virtual void Redraw();
301 void RedrawSelMargin(int line
=-1, bool allAfter
=false);
302 PRectangle
RectangleFromRange(Range r
, int overlap
);
303 void InvalidateRange(int start
, int end
);
305 bool UserVirtualSpace() const {
306 return ((virtualSpaceOptions
& SCVS_USERACCESSIBLE
) != 0);
308 int CurrentPosition() const;
309 bool SelectionEmpty() const;
310 SelectionPosition
SelectionStart();
311 SelectionPosition
SelectionEnd();
312 void SetRectangularRange();
313 void ThinRectangularRange();
314 void InvalidateSelection(SelectionRange newMain
, bool invalidateWholeSelection
=false);
315 void SetSelection(SelectionPosition currentPos_
, SelectionPosition anchor_
);
316 void SetSelection(int currentPos_
, int anchor_
);
317 void SetSelection(SelectionPosition currentPos_
);
318 void SetSelection(int currentPos_
);
319 void SetEmptySelection(SelectionPosition currentPos_
);
320 void SetEmptySelection(int currentPos_
);
321 bool RangeContainsProtected(int start
, int end
) const;
322 bool SelectionContainsProtected();
323 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true) const;
324 SelectionPosition
MovePositionOutsideChar(SelectionPosition pos
, int moveDir
, bool checkLineEnd
=true) const;
325 int MovePositionTo(SelectionPosition newPos
, Selection::selTypes selt
=Selection::noSel
, bool ensureVisible
=true);
326 int MovePositionTo(int newPos
, Selection::selTypes selt
=Selection::noSel
, bool ensureVisible
=true);
327 SelectionPosition
MovePositionSoVisible(SelectionPosition pos
, int moveDir
);
328 SelectionPosition
MovePositionSoVisible(int pos
, int moveDir
);
329 Point
PointMainCaret();
330 void SetLastXChosen();
332 void ScrollTo(int line
, bool moveThumb
=true);
333 virtual void ScrollText(int linesToMove
);
334 void HorizontalScrollTo(int xPos
);
335 void VerticalCentreCaret();
336 void MoveSelectedLines(int lineDelta
);
337 void MoveSelectedLinesUp();
338 void MoveSelectedLinesDown();
339 void MoveCaretInsideView(bool ensureVisible
=true);
340 int DisplayFromPosition(int pos
);
342 struct XYScrollPosition
{
345 XYScrollPosition(int xOffset_
, int topLine_
) : xOffset(xOffset_
), topLine(topLine_
) {}
346 bool operator==(const XYScrollPosition
&other
) const {
347 return (xOffset
== other
.xOffset
) && (topLine
== other
.topLine
);
350 enum XYScrollOptions
{
354 xysDefault
=xysUseMargin
|xysVertical
|xysHorizontal
};
355 XYScrollPosition
XYScrollToMakeVisible(const SelectionRange
&range
, const XYScrollOptions options
);
356 void SetXYScroll(XYScrollPosition newXY
);
357 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
358 void ScrollRange(SelectionRange range
);
359 void ShowCaretAtCurrentPosition();
361 void CaretSetPeriod(int period
);
362 void InvalidateCaret();
363 virtual void UpdateSystemCaret();
365 bool Wrapping() const;
366 void NeedWrapping(int docLineStart
=0, int docLineEnd
=WrapPending::lineLarge
);
367 bool WrapOneLine(Surface
*surface
, int lineToWrap
);
368 enum wrapScope
{wsAll
, wsVisible
, wsIdle
};
369 bool WrapLines(enum wrapScope ws
);
371 void LinesSplit(int pixelWidth
);
373 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
374 void RefreshPixMaps(Surface
*surfaceWindow
);
375 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
376 long FormatRange(bool draw
, Sci_RangeToFormat
*pfr
);
377 int TextWidth(int style
, const char *text
);
379 virtual void SetVerticalScrollPos() = 0;
380 virtual void SetHorizontalScrollPos() = 0;
381 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
382 virtual void ReconfigureScrollBars();
383 void SetScrollBars();
386 void FilterSelections();
387 int InsertSpace(int position
, unsigned int spaces
);
388 void AddChar(char ch
);
389 virtual void AddCharUTF(const char *s
, unsigned int len
, bool treatAsDBCS
=false);
390 void InsertPaste(const char *text
, int len
);
391 enum PasteShape
{ pasteStream
=0, pasteRectangular
= 1, pasteLine
= 2 };
392 void InsertPasteShape(const char *text
, int len
, PasteShape shape
);
393 void ClearSelection(bool retainMultipleSelections
= false);
395 void ClearDocumentStyle();
397 void PasteRectangular(SelectionPosition pos
, const char *ptr
, int len
);
398 virtual void Copy() = 0;
399 virtual void CopyAllowLine();
400 virtual bool CanPaste();
401 virtual void Paste() = 0;
406 void DelCharBack(bool allowLineStartDeletion
);
407 virtual void ClaimSelection() = 0;
409 static int ModifierFlags(bool shift
, bool ctrl
, bool alt
, bool meta
=false);
410 virtual void NotifyChange() = 0;
411 virtual void NotifyFocus(bool focus
);
412 virtual void SetCtrlID(int identifier
);
413 virtual int GetCtrlID() { return ctrlID
; }
414 virtual void NotifyParent(SCNotification scn
) = 0;
415 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
416 void NotifyChar(int ch
);
417 void NotifySavePoint(bool isSavePoint
);
418 void NotifyModifyAttempt();
419 virtual void NotifyDoubleClick(Point pt
, int modifiers
);
420 virtual void NotifyDoubleClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
421 void NotifyHotSpotClicked(int position
, int modifiers
);
422 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
423 void NotifyHotSpotDoubleClicked(int position
, int modifiers
);
424 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
425 void NotifyHotSpotReleaseClick(int position
, int modifiers
);
426 void NotifyHotSpotReleaseClick(int position
, bool shift
, bool ctrl
, bool alt
);
427 bool NotifyUpdateUI();
428 void NotifyPainted();
429 void NotifyIndicatorClick(bool click
, int position
, int modifiers
);
430 void NotifyIndicatorClick(bool click
, int position
, bool shift
, bool ctrl
, bool alt
);
431 bool NotifyMarginClick(Point pt
, int modifiers
);
432 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
433 void NotifyNeedShown(int pos
, int len
);
434 void NotifyDwelling(Point pt
, bool state
);
437 void NotifyModifyAttempt(Document
*document
, void *userData
);
438 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
439 void CheckModificationForWrap(DocModification mh
);
440 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
441 void NotifyDeleted(Document
*document
, void *userData
);
442 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
443 void NotifyLexerChanged(Document
*doc
, void *userData
);
444 void NotifyErrorOccurred(Document
*doc
, void *userData
, int status
);
445 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
447 void ContainerNeedsUpdate(int flags
);
448 void PageMove(int direction
, Selection::selTypes selt
=Selection::noSel
, bool stuttered
= false);
449 enum { cmSame
, cmUpper
, cmLower
};
450 virtual std::string
CaseMapString(const std::string
&s
, int caseMapping
);
451 void ChangeCaseOfSelection(int caseMapping
);
452 void LineTranspose();
453 void Duplicate(bool forLine
);
454 virtual void CancelModes();
456 void CursorUpOrDown(int direction
, Selection::selTypes selt
=Selection::noSel
);
457 void ParaUpOrDown(int direction
, Selection::selTypes selt
=Selection::noSel
);
458 int StartEndDisplayLine(int pos
, bool start
);
459 virtual int KeyCommand(unsigned int iMessage
);
460 virtual int KeyDefault(int /* key */, int /*modifiers*/);
461 int KeyDownWithModifiers(int key
, int modifiers
, bool *consumed
);
462 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
464 void Indent(bool forwards
);
466 virtual CaseFolder
*CaseFolderForEncoding();
467 long FindText(uptr_t wParam
, sptr_t lParam
);
469 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
470 long SearchInTarget(const char *text
, int length
);
471 void GoToLine(int lineNo
);
473 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
474 std::string
RangeText(int start
, int end
) const;
475 void CopySelectionRange(SelectionText
*ss
, bool allowLineCopy
=false);
476 void CopyRangeToClipboard(int start
, int end
);
477 void CopyText(int length
, const char *text
);
478 void SetDragPosition(SelectionPosition newPos
);
479 virtual void DisplayCursor(Window::Cursor c
);
480 virtual bool DragThreshold(Point ptStart
, Point ptNow
);
481 virtual void StartDrag();
482 void DropAt(SelectionPosition position
, const char *value
, size_t lengthValue
, bool moving
, bool rectangular
);
483 void DropAt(SelectionPosition position
, const char *value
, bool moving
, bool rectangular
);
484 /** PositionInSelection returns true if position in selection. */
485 bool PositionInSelection(int pos
);
486 bool PointInSelection(Point pt
);
487 bool PointInSelMargin(Point pt
) const;
488 Window::Cursor
GetMarginCursor(Point pt
) const;
489 void TrimAndSetSelection(int currentPos_
, int anchor_
);
490 void LineSelection(int lineCurrentPos_
, int lineAnchorPos_
, bool wholeLine
);
491 void WordSelection(int pos
);
492 void DwellEnd(bool mouseMoved
);
494 virtual void ButtonDownWithModifiers(Point pt
, unsigned int curTime
, int modifiers
);
495 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
496 void ButtonMoveWithModifiers(Point pt
, int modifiers
);
497 void ButtonMove(Point pt
);
498 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
502 virtual void SetTicking(bool on
);
503 enum TickReason
{ tickCaret
, tickScroll
, tickWiden
, tickDwell
, tickPlatform
};
504 virtual void TickFor(TickReason reason
);
505 virtual bool FineTickerAvailable();
506 virtual bool FineTickerRunning(TickReason reason
);
507 virtual void FineTickerStart(TickReason reason
, int millis
, int tolerance
);
508 virtual void FineTickerCancel(TickReason reason
);
509 virtual bool SetIdle(bool) { return false; }
510 virtual void SetMouseCapture(bool on
) = 0;
511 virtual bool HaveMouseCapture() = 0;
512 void SetFocusState(bool focusState
);
514 int PositionAfterArea(PRectangle rcArea
) const;
515 void StyleToPositionInView(Position pos
);
516 virtual void IdleWork();
517 virtual void QueueIdleWork(WorkNeeded::workItems items
, int upTo
=0);
519 virtual bool PaintContains(PRectangle rc
);
520 bool PaintContainsMargin();
521 void CheckForChangeOutsidePaint(Range r
);
522 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
524 void SetAnnotationHeights(int start
, int end
);
525 virtual void SetDocPointer(Document
*document
);
527 void SetAnnotationVisible(int visible
);
529 int ExpandLine(int line
);
530 void SetFoldExpanded(int lineDoc
, bool expanded
);
531 void FoldLine(int line
, int action
);
532 void FoldExpand(int line
, int action
, int level
);
533 int ContractedFoldNext(int lineStart
) const;
534 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
535 void FoldChanged(int line
, int levelNow
, int levelPrev
);
536 void NeedShown(int pos
, int len
);
537 void FoldAll(int action
);
539 int GetTag(char *tagValue
, int tagNumber
);
540 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
542 bool PositionIsHotspot(int position
) const;
543 bool PointIsHotspot(Point pt
);
544 void SetHotSpotRange(Point
*pt
);
545 Range
GetHotSpotRange() const;
547 int CodePage() const;
548 virtual bool ValidCodePage(int /* codePage */) const { return true; }
549 int WrapCount(int line
);
550 void AddStyledText(char *buffer
, int appendLength
);
552 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
553 void StyleSetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
554 sptr_t
StyleGetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
556 static const char *StringFromEOLMode(int eolMode
);
558 static sptr_t
StringResult(sptr_t lParam
, const char *val
);
559 static sptr_t
BytesResult(sptr_t lParam
, const unsigned char *val
, size_t len
);
562 // Public so the COM thunks can access it.
563 bool IsUnicodeMode() const;
564 // Public so scintilla_send_message can use it.
565 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
566 // Public so scintilla_set_id can use it.
568 // Public so COM methods for drag and drop can set it.
570 friend class AutoSurface
;
571 friend class SelectionLineIterator
;
575 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
581 AutoSurface(Editor
*ed
, int technology
= -1) : surf(0) {
582 if (ed
->wMain
.GetID()) {
583 surf
= Surface::Allocate(technology
!= -1 ? technology
: ed
->technology
);
585 surf
->Init(ed
->wMain
.GetID());
586 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
587 surf
->SetDBCSMode(ed
->CodePage());
591 AutoSurface(SurfaceID sid
, Editor
*ed
, int technology
= -1) : surf(0) {
592 if (ed
->wMain
.GetID()) {
593 surf
= Surface::Allocate(technology
!= -1 ? technology
: ed
->technology
);
595 surf
->Init(sid
, ed
->wMain
.GetID());
596 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
597 surf
->SetDBCSMode(ed
->CodePage());
604 Surface
*operator->() const {
607 operator Surface
*() const {