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 * When platform has a way to generate an event before painting,
50 * accumulate needed styling range in StyleNeeded to avoid unnecessary work.
57 StyleNeeded() : active(false), upTo(0) {}
62 void NeedUpTo(Position pos
) {
69 * Hold a piece of text selected for copying or dragging.
70 * The text is expected to hold a terminating '\0' and this is counted in len.
80 SelectionText() : s(0), len(0), rectangular(false), lineCopy(false), codePage(0), characterSet(0) {}
85 Set(0, 0, 0, 0, false, false);
87 void Set(char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
, bool lineCopy_
) {
95 characterSet
= characterSet_
;
96 rectangular
= rectangular_
;
99 void Copy(const char *s_
, int len_
, int codePage_
, int characterSet_
, bool rectangular_
, bool lineCopy_
) {
104 for (int i
= 0; i
< len_
; i
++) {
107 codePage
= codePage_
;
108 characterSet
= characterSet_
;
109 rectangular
= rectangular_
;
110 lineCopy
= lineCopy_
;
112 void Copy(const SelectionText
&other
) {
113 Copy(other
.s
, other
.len
, other
.codePage
, other
.characterSet
, other
.rectangular
, other
.lineCopy
);
119 class Editor
: public DocWatcher
{
120 // Private so Editor objects can not be copied
121 Editor(const Editor
&);
122 Editor
&operator=(const Editor
&);
124 protected: // ScintillaBase subclass needs access to much of Editor
126 /** On GTK+, Scintilla is a container widget holding two scroll bars
127 * whereas on Windows there is just one window with both scroll bars turned on. */
128 Window wMain
; ///< The Scintilla parent window
130 /** Style resources may be expensive to allocate so are cached between uses.
131 * When a style attribute is changed, this cache is flushed. */
136 int printMagnification
;
140 int controlCharSymbol
;
145 bool mouseDownCaptures
;
147 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
148 * the screen. This avoids flashing but is about 30% slower. */
150 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
151 * and then the foreground. This avoids chopping off characters that overlap the next run. */
154 int xOffset
; ///< Horizontal scrolled amount in pixels
155 int xCaretMargin
; ///< Ensure this many pixels visible on both sides of caret
156 bool horizontalScrollBarVisible
;
159 int lineWidthMaxSeen
;
160 bool verticalScrollBarVisible
;
163 bool multipleSelection
;
164 bool additionalSelectionTyping
;
166 bool additionalCaretsBlink
;
167 bool additionalCaretsVisible
;
169 int virtualSpaceOptions
;
172 Surface
*pixmapSelMargin
;
173 Surface
*pixmapSelPattern
;
174 Surface
*pixmapIndentGuide
;
175 Surface
*pixmapIndentGuideHighlight
;
178 PositionCache posCache
;
184 Timer autoScrollTimer
;
185 enum { autoScrollDelay
= 200 };
190 unsigned int lastClickTime
;
194 enum { selChar
, selWord
, selLine
} selectionType
;
196 enum { ddNone
, ddInitial
, ddDragging
} inDragDrop
;
197 bool dropWentOutside
;
198 SelectionPosition posDrag
;
199 SelectionPosition posDrop
;
202 int originalAnchorPos
;
212 int bracesMatchStyle
;
213 int highlightGuideColumn
;
217 enum { notPainting
, painting
, paintAbandoned
} paintState
;
219 bool paintingAllText
;
220 StyleNeeded styleNeeded
;
226 bool primarySelection
;
229 int caretXSlop
; ///< Ensure this many pixels visible on both sides of caret
232 int caretYSlop
; ///< Ensure this many lines visible on both sides of caret
249 enum { eWrapNone
, eWrapWord
, eWrapChar
} wrapState
;
250 enum { wrapLineLarge
= 0x7ffffff };
255 int wrapVisualFlagsLocation
;
256 int wrapVisualStartIndent
;
257 int wrapAddIndent
; // This will be added to initial indent of line
258 int wrapIndentMode
; // SC_WRAPINDENT_FIXED, _SAME, _INDENT
266 virtual void Initialise() = 0;
267 virtual void Finalise();
269 void InvalidateStyleData();
270 void InvalidateStyleRedraw();
271 virtual void RefreshColourPalette(Palette
&pal
, bool want
);
272 void RefreshStyleData();
275 virtual PRectangle
GetClientRectangle();
276 PRectangle
GetTextRectangle();
281 SelectionPosition
ClampPositionIntoDocument(SelectionPosition sp
) const;
282 Point
LocationFromPosition(SelectionPosition pos
);
283 Point
LocationFromPosition(int pos
);
284 int XFromPosition(int pos
);
285 int XFromPosition(SelectionPosition sp
);
286 SelectionPosition
SPositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false, bool virtualSpace
=true);
287 int PositionFromLocation(Point pt
, bool canReturnInvalid
=false, bool charPosition
=false);
288 SelectionPosition
SPositionFromLineX(int lineDoc
, int x
);
289 int PositionFromLineX(int line
, int x
);
290 int LineFromLocation(Point pt
);
291 void SetTopLine(int topLineNew
);
294 void RedrawRect(PRectangle rc
);
296 void RedrawSelMargin(int line
=-1, bool allAfter
=false);
297 PRectangle
RectangleFromRange(int start
, int end
);
298 void InvalidateRange(int start
, int end
);
300 bool UserVirtualSpace() const {
301 return ((virtualSpaceOptions
& SCVS_USERACCESSIBLE
) != 0);
303 int CurrentPosition();
304 bool SelectionEmpty();
305 SelectionPosition
SelectionStart();
306 SelectionPosition
SelectionEnd();
307 void SetRectangularRange();
308 void ThinRectangularRange();
309 void InvalidateSelection(SelectionRange newMain
, bool invalidateWholeSelection
=false);
310 void SetSelection(SelectionPosition currentPos_
, SelectionPosition anchor_
);
311 void SetSelection(int currentPos_
, int anchor_
);
312 void SetSelection(SelectionPosition currentPos_
);
313 void SetSelection(int currentPos_
);
314 void SetEmptySelection(SelectionPosition currentPos_
);
315 void SetEmptySelection(int currentPos_
);
316 bool RangeContainsProtected(int start
, int end
) const;
317 bool SelectionContainsProtected();
318 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true) const;
319 SelectionPosition
MovePositionOutsideChar(SelectionPosition pos
, int moveDir
, bool checkLineEnd
=true) const;
320 int MovePositionTo(SelectionPosition newPos
, Selection::selTypes sel
=Selection::noSel
, bool ensureVisible
=true);
321 int MovePositionTo(int newPos
, Selection::selTypes sel
=Selection::noSel
, bool ensureVisible
=true);
322 SelectionPosition
MovePositionSoVisible(SelectionPosition pos
, int moveDir
);
323 SelectionPosition
MovePositionSoVisible(int pos
, int moveDir
);
324 Point
PointMainCaret();
325 void SetLastXChosen();
327 void ScrollTo(int line
, bool moveThumb
=true);
328 virtual void ScrollText(int linesToMove
);
329 void HorizontalScrollTo(int xPos
);
330 void MoveCaretInsideView(bool ensureVisible
=true);
331 int DisplayFromPosition(int pos
);
333 struct XYScrollPosition
{
336 XYScrollPosition(int xOffset_
, int topLine_
) : xOffset(xOffset_
), topLine(topLine_
) {}
338 XYScrollPosition
XYScrollToMakeVisible(const bool useMargin
, const bool vert
, const bool horiz
);
339 void SetXYScroll(XYScrollPosition newXY
);
340 void EnsureCaretVisible(bool useMargin
=true, bool vert
=true, bool horiz
=true);
341 void ShowCaretAtCurrentPosition();
343 void InvalidateCaret();
344 virtual void UpdateSystemCaret();
346 void NeedWrapping(int docLineStart
= 0, int docLineEnd
= wrapLineLarge
);
347 bool WrapOneLine(Surface
*surface
, int lineToWrap
);
348 bool WrapLines(bool fullWrap
, int priorityWrapLineStart
);
350 void LinesSplit(int pixelWidth
);
352 int SubstituteMarkerIfEmpty(int markerCheck
, int markerDefault
);
353 void PaintSelMargin(Surface
*surface
, PRectangle
&rc
);
354 LineLayout
*RetrieveLineLayout(int lineNumber
);
355 void LayoutLine(int line
, Surface
*surface
, ViewStyle
&vstyle
, LineLayout
*ll
,
356 int width
=LineLayout::wrapWidthInfinite
);
357 ColourAllocated
SelectionBackground(ViewStyle
&vsDraw
, bool main
);
358 ColourAllocated
TextBackground(ViewStyle
&vsDraw
, bool overrideBackground
, ColourAllocated background
, int inSelection
, bool inHotspot
, int styleMain
, int i
, LineLayout
*ll
);
359 void DrawIndentGuide(Surface
*surface
, int lineVisible
, int lineHeight
, int start
, PRectangle rcSegment
, bool highlight
);
360 void DrawWrapMarker(Surface
*surface
, PRectangle rcPlace
, bool isEndMarker
, ColourAllocated wrapColour
);
361 void DrawEOL(Surface
*surface
, ViewStyle
&vsDraw
, PRectangle rcLine
, LineLayout
*ll
,
362 int line
, int lineEnd
, int xStart
, int subLine
, int subLineStart
,
363 bool overrideBackground
, ColourAllocated background
,
364 bool drawWrapMark
, ColourAllocated wrapColour
);
365 void DrawIndicators(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
366 PRectangle rcLine
, LineLayout
*ll
, int subLine
, int lineEnd
, bool under
);
367 void DrawAnnotation(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
368 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
369 void DrawLine(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int lineVisible
, int xStart
,
370 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
371 void DrawBlockCaret(Surface
*surface
, ViewStyle
&vsDraw
, LineLayout
*ll
, int subLine
,
372 int xStart
, int offset
, int posCaret
, PRectangle rcCaret
, ColourAllocated caretColour
);
373 void DrawCarets(Surface
*surface
, ViewStyle
&vsDraw
, int line
, int xStart
,
374 PRectangle rcLine
, LineLayout
*ll
, int subLine
);
375 void RefreshPixMaps(Surface
*surfaceWindow
);
376 void Paint(Surface
*surfaceWindow
, PRectangle rcArea
);
377 long FormatRange(bool draw
, Sci_RangeToFormat
*pfr
);
378 int TextWidth(int style
, const char *text
);
380 virtual void SetVerticalScrollPos() = 0;
381 virtual void SetHorizontalScrollPos() = 0;
382 virtual bool ModifyScrollBars(int nMax
, int nPage
) = 0;
383 virtual void ReconfigureScrollBars();
384 void SetScrollBars();
387 void FilterSelections();
388 int InsertSpace(int position
, unsigned int spaces
);
389 void AddChar(char ch
);
390 virtual void AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
=false);
391 void InsertPaste(SelectionPosition selStart
, const char *text
, int len
);
392 void ClearSelection();
394 void ClearDocumentStyle();
396 void PasteRectangular(SelectionPosition pos
, const char *ptr
, int len
);
397 virtual void Copy() = 0;
398 virtual void CopyAllowLine();
399 virtual bool CanPaste();
400 virtual void Paste() = 0;
406 void DelCharBack(bool allowLineStartDeletion
);
407 virtual void ClaimSelection() = 0;
409 virtual void NotifyChange() = 0;
410 virtual void NotifyFocus(bool focus
);
411 virtual int GetCtrlID() { return ctrlID
; }
412 virtual void NotifyParent(SCNotification scn
) = 0;
413 virtual void NotifyStyleToNeeded(int endStyleNeeded
);
414 void NotifyChar(int ch
);
415 void NotifySavePoint(bool isSavePoint
);
416 void NotifyModifyAttempt();
417 virtual void NotifyDoubleClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
418 void NotifyHotSpotClicked(int position
, bool shift
, bool ctrl
, bool alt
);
419 void NotifyHotSpotDoubleClicked(int position
, bool shift
, bool ctrl
, bool alt
);
420 void NotifyUpdateUI();
421 void NotifyPainted();
422 void NotifyIndicatorClick(bool click
, int position
, bool shift
, bool ctrl
, bool alt
);
423 bool NotifyMarginClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
424 void NotifyNeedShown(int pos
, int len
);
425 void NotifyDwelling(Point pt
, bool state
);
428 void NotifyModifyAttempt(Document
*document
, void *userData
);
429 void NotifySavePoint(Document
*document
, void *userData
, bool atSavePoint
);
430 void CheckModificationForWrap(DocModification mh
);
431 void NotifyModified(Document
*document
, DocModification mh
, void *userData
);
432 void NotifyDeleted(Document
*document
, void *userData
);
433 void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
);
434 void NotifyMacroRecord(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
436 void PageMove(int direction
, Selection::selTypes sel
=Selection::noSel
, bool stuttered
= false);
437 enum { cmSame
, cmUpper
, cmLower
} caseMap
;
438 virtual std::string
CaseMapString(const std::string
&s
, int caseMapping
);
439 void ChangeCaseOfSelection(int caseMapping
);
440 void LineTranspose();
441 void Duplicate(bool forLine
);
442 virtual void CancelModes();
444 void CursorUpOrDown(int direction
, Selection::selTypes sel
=Selection::noSel
);
445 void ParaUpOrDown(int direction
, Selection::selTypes sel
=Selection::noSel
);
446 int StartEndDisplayLine(int pos
, bool start
);
447 virtual int KeyCommand(unsigned int iMessage
);
448 virtual int KeyDefault(int /* key */, int /*modifiers*/);
449 int KeyDown(int key
, bool shift
, bool ctrl
, bool alt
, bool *consumed
=0);
451 int GetWhitespaceVisible();
452 void SetWhitespaceVisible(int view
);
454 void Indent(bool forwards
);
456 virtual CaseFolder
*CaseFolderForEncoding();
457 long FindText(uptr_t wParam
, sptr_t lParam
);
459 long SearchText(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
460 long SearchInTarget(const char *text
, int length
);
461 void GoToLine(int lineNo
);
463 virtual void CopyToClipboard(const SelectionText
&selectedText
) = 0;
464 char *CopyRange(int start
, int end
);
465 void CopySelectionRange(SelectionText
*ss
, bool allowLineCopy
=false);
466 void CopyRangeToClipboard(int start
, int end
);
467 void CopyText(int length
, const char *text
);
468 void SetDragPosition(SelectionPosition newPos
);
469 virtual void DisplayCursor(Window::Cursor c
);
470 virtual bool DragThreshold(Point ptStart
, Point ptNow
);
471 virtual void StartDrag();
472 void DropAt(SelectionPosition position
, const char *value
, bool moving
, bool rectangular
);
473 /** PositionInSelection returns true if position in selection. */
474 bool PositionInSelection(int pos
);
475 bool PointInSelection(Point pt
);
476 bool PointInSelMargin(Point pt
);
477 void LineSelection(int lineCurrent_
, int lineAnchor_
);
478 void DwellEnd(bool mouseMoved
);
480 virtual void ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
);
481 void ButtonMove(Point pt
);
482 void ButtonUp(Point pt
, unsigned int curTime
, bool ctrl
);
486 virtual void SetTicking(bool on
) = 0;
487 virtual bool SetIdle(bool) { return false; }
488 virtual void SetMouseCapture(bool on
) = 0;
489 virtual bool HaveMouseCapture() = 0;
490 void SetFocusState(bool focusState
);
492 int PositionAfterArea(PRectangle rcArea
);
493 void StyleToPositionInView(Position pos
);
495 virtual void QueueStyling(int upTo
);
497 virtual bool PaintContains(PRectangle rc
);
498 bool PaintContainsMargin();
499 void CheckForChangeOutsidePaint(Range r
);
500 void SetBraceHighlight(Position pos0
, Position pos1
, int matchStyle
);
502 void SetAnnotationHeights(int start
, int end
);
503 void SetDocPointer(Document
*document
);
505 void SetAnnotationVisible(int visible
);
507 void Expand(int &line
, bool doExpand
);
508 void ToggleContraction(int line
);
509 void EnsureLineVisible(int lineDoc
, bool enforcePolicy
);
510 int GetTag(char *tagValue
, int tagNumber
);
511 int ReplaceTarget(bool replacePatterns
, const char *text
, int length
=-1);
513 bool PositionIsHotspot(int position
);
514 bool PointIsHotspot(Point pt
);
515 void SetHotSpotRange(Point
*pt
);
516 void GetHotSpotRange(int &hsStart
, int &hsEnd
);
518 int CodePage() const;
519 virtual bool ValidCodePage(int /* codePage */) const { return true; }
520 int WrapCount(int line
);
521 void AddStyledText(char *buffer
, int appendLength
);
523 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) = 0;
524 void StyleSetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
525 sptr_t
StyleGetMessage(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
527 static const char *StringFromEOLMode(int eolMode
);
529 static sptr_t
StringResult(sptr_t lParam
, const char *val
);
532 // Public so the COM thunks can access it.
533 bool IsUnicodeMode() const;
534 // Public so scintilla_send_message can use it.
535 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
536 // Public so scintilla_set_id can use it.
538 // Public so COM methods for drag and drop can set it.
540 friend class AutoSurface
;
541 friend class SelectionLineIterator
;
545 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
551 AutoSurface(Editor
*ed
) : surf(0) {
552 if (ed
->wMain
.GetID()) {
553 surf
= Surface::Allocate();
555 surf
->Init(ed
->wMain
.GetID());
556 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
557 surf
->SetDBCSMode(ed
->CodePage());
561 AutoSurface(SurfaceID sid
, Editor
*ed
) : surf(0) {
562 if (ed
->wMain
.GetID()) {
563 surf
= Surface::Allocate();
565 surf
->Init(sid
, ed
->wMain
.GetID());
566 surf
->SetUnicodeMode(SC_CP_UTF8
== ed
->CodePage());
567 surf
->SetDBCSMode(ed
->CodePage());
574 Surface
*operator->() const {
577 operator Surface
*() const {