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;
203
int originalAnchorPos;
204
int wordSelectAnchorStartPos;
205
int wordSelectAnchorEndPos;
206
int wordSelectInitialCaretPos;
216
int bracesMatchStyle;
217
int highlightGuideColumn;
221
enum { notPainting, painting, paintAbandoned } paintState;
223
bool paintingAllText;
224
StyleNeeded styleNeeded;
230
bool primarySelection;
233
int caretXSlop; ///< Ensure this many pixels visible on both sides of caret
236
int caretYSlop; ///< Ensure this many lines visible on both sides of caret
253
enum { eWrapNone, eWrapWord, eWrapChar } wrapState;
254
enum { wrapLineLarge = 0x7ffffff };
259
int wrapVisualFlagsLocation;
260
int wrapVisualStartIndent;
261
int wrapAddIndent; // This will be added to initial indent of line
262
int wrapIndentMode; // SC_WRAPINDENT_FIXED, _SAME, _INDENT
270
virtual void Initialise() = 0;
271
virtual void Finalise();
273
void InvalidateStyleData();
274
void InvalidateStyleRedraw();
275
virtual void RefreshColourPalette(Palette &pal, bool want);
276
void RefreshStyleData();
279
virtual PRectangle GetClientRectangle();
280
PRectangle GetTextRectangle();
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);
295
void SetTopLine(int topLineNew);
298
void RedrawRect(PRectangle rc);
300
void RedrawSelMargin(int line=-1, bool allAfter=false);
301
PRectangle RectangleFromRange(int start, int end);
302
void InvalidateRange(int start, int end);
304
bool UserVirtualSpace() const {
305
return ((virtualSpaceOptions & SCVS_USERACCESSIBLE) != 0);
307
int CurrentPosition();
308
bool SelectionEmpty();
309
SelectionPosition SelectionStart();
310
SelectionPosition SelectionEnd();
311
void SetRectangularRange();
312
void ThinRectangularRange();
313
void InvalidateSelection(SelectionRange newMain, bool invalidateWholeSelection=false);
314
void SetSelection(SelectionPosition currentPos_, SelectionPosition anchor_);
315
void SetSelection(int currentPos_, int anchor_);
316
void SetSelection(SelectionPosition currentPos_);
317
void SetSelection(int currentPos_);
318
void SetEmptySelection(SelectionPosition currentPos_);
319
void SetEmptySelection(int currentPos_);
320
bool RangeContainsProtected(int start, int end) const;
321
bool SelectionContainsProtected();
322
int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true) const;
323
SelectionPosition MovePositionOutsideChar(SelectionPosition pos, int moveDir, bool checkLineEnd=true) const;
324
int MovePositionTo(SelectionPosition newPos, Selection::selTypes sel=Selection::noSel, bool ensureVisible=true);
325
int MovePositionTo(int newPos, Selection::selTypes sel=Selection::noSel, bool ensureVisible=true);
326
SelectionPosition MovePositionSoVisible(SelectionPosition pos, int moveDir);
327
SelectionPosition MovePositionSoVisible(int pos, int moveDir);
328
Point PointMainCaret();
329
void SetLastXChosen();
331
void ScrollTo(int line, bool moveThumb=true);
332
virtual void ScrollText(int linesToMove);
333
void HorizontalScrollTo(int xPos);
334
void VerticalCentreCaret();
335
void MoveCaretInsideView(bool ensureVisible=true);
336
int DisplayFromPosition(int pos);
338
struct XYScrollPosition {
341
XYScrollPosition(int xOffset_, int topLine_) : xOffset(xOffset_), topLine(topLine_) {}
343
XYScrollPosition XYScrollToMakeVisible(const bool useMargin, const bool vert, const bool horiz);
344
void SetXYScroll(XYScrollPosition newXY);
345
void EnsureCaretVisible(bool useMargin=true, bool vert=true, bool horiz=true);
346
void ShowCaretAtCurrentPosition();
348
void InvalidateCaret();
349
virtual void UpdateSystemCaret();
351
void NeedWrapping(int docLineStart = 0, int docLineEnd = wrapLineLarge);
352
bool WrapOneLine(Surface *surface, int lineToWrap);
353
bool WrapLines(bool fullWrap, int priorityWrapLineStart);
355
void LinesSplit(int pixelWidth);
357
int SubstituteMarkerIfEmpty(int markerCheck, int markerDefault);
358
void PaintSelMargin(Surface *surface, PRectangle &rc);
359
LineLayout *RetrieveLineLayout(int lineNumber);
360
void LayoutLine(int line, Surface *surface, ViewStyle &vstyle, LineLayout *ll,
361
int width=LineLayout::wrapWidthInfinite);
362
ColourAllocated SelectionBackground(ViewStyle &vsDraw, bool main);
363
ColourAllocated TextBackground(ViewStyle &vsDraw, bool overrideBackground, ColourAllocated background, int inSelection, bool inHotspot, int styleMain, int i, LineLayout *ll);
364
void DrawIndentGuide(Surface *surface, int lineVisible, int lineHeight, int start, PRectangle rcSegment, bool highlight);
365
void DrawWrapMarker(Surface *surface, PRectangle rcPlace, bool isEndMarker, ColourAllocated wrapColour);
366
void DrawEOL(Surface *surface, ViewStyle &vsDraw, PRectangle rcLine, LineLayout *ll,
367
int line, int lineEnd, int xStart, int subLine, int subLineStart,
368
bool overrideBackground, ColourAllocated background,
369
bool drawWrapMark, ColourAllocated wrapColour);
370
void DrawIndicators(Surface *surface, ViewStyle &vsDraw, int line, int xStart,
371
PRectangle rcLine, LineLayout *ll, int subLine, int lineEnd, bool under);
372
void DrawAnnotation(Surface *surface, ViewStyle &vsDraw, int line, int xStart,
373
PRectangle rcLine, LineLayout *ll, int subLine);
374
void DrawLine(Surface *surface, ViewStyle &vsDraw, int line, int lineVisible, int xStart,
375
PRectangle rcLine, LineLayout *ll, int subLine);
376
void DrawBlockCaret(Surface *surface, ViewStyle &vsDraw, LineLayout *ll, int subLine,
377
int xStart, int offset, int posCaret, PRectangle rcCaret, ColourAllocated caretColour);
378
void DrawCarets(Surface *surface, ViewStyle &vsDraw, int line, int xStart,
379
PRectangle rcLine, LineLayout *ll, int subLine);
380
void RefreshPixMaps(Surface *surfaceWindow);
381
void Paint(Surface *surfaceWindow, PRectangle rcArea);
382
long FormatRange(bool draw, Sci_RangeToFormat *pfr);
383
int TextWidth(int style, const char *text);
385
virtual void SetVerticalScrollPos() = 0;
386
virtual void SetHorizontalScrollPos() = 0;
387
virtual bool ModifyScrollBars(int nMax, int nPage) = 0;
388
virtual void ReconfigureScrollBars();
389
void SetScrollBars();
392
void FilterSelections();
393
int InsertSpace(int position, unsigned int spaces);
394
void AddChar(char ch);
395
virtual void AddCharUTF(char *s, unsigned int len, bool treatAsDBCS=false);
396
void InsertPaste(SelectionPosition selStart, const char *text, int len);
397
void ClearSelection(bool retainMultipleSelections=false);
399
void ClearDocumentStyle();
401
void PasteRectangular(SelectionPosition pos, const char *ptr, int len);
402
virtual void Copy() = 0;
403
virtual void CopyAllowLine();
404
virtual bool CanPaste();
405
virtual void Paste() = 0;
411
void DelCharBack(bool allowLineStartDeletion);
412
virtual void ClaimSelection() = 0;
414
virtual void NotifyChange() = 0;
415
virtual void NotifyFocus(bool focus);
416
virtual int GetCtrlID() { return ctrlID; }
417
virtual void NotifyParent(SCNotification scn) = 0;
418
virtual void NotifyStyleToNeeded(int endStyleNeeded);
419
void NotifyChar(int ch);
420
void NotifySavePoint(bool isSavePoint);
421
void NotifyModifyAttempt();
422
virtual void NotifyDoubleClick(Point pt, bool shift, bool ctrl, bool alt);
423
void NotifyHotSpotClicked(int position, bool shift, bool ctrl, bool alt);
424
void NotifyHotSpotDoubleClicked(int position, bool shift, bool ctrl, bool alt);
425
void NotifyHotSpotReleaseClick(int position, bool shift, bool ctrl, bool alt);
426
void NotifyUpdateUI();
427
void NotifyPainted();
428
void NotifyIndicatorClick(bool click, int position, bool shift, bool ctrl, bool alt);
429
bool NotifyMarginClick(Point pt, bool shift, bool ctrl, bool alt);
430
void NotifyNeedShown(int pos, int len);
431
void NotifyDwelling(Point pt, bool state);
434
void NotifyModifyAttempt(Document *document, void *userData);
435
void NotifySavePoint(Document *document, void *userData, bool atSavePoint);
436
void CheckModificationForWrap(DocModification mh);
437
void NotifyModified(Document *document, DocModification mh, void *userData);
438
void NotifyDeleted(Document *document, void *userData);
439
void NotifyStyleNeeded(Document *doc, void *userData, int endPos);
440
void NotifyLexerChanged(Document *doc, void *userData);
441
void NotifyErrorOccurred(Document *doc, void *userData, int status);
442
void NotifyMacroRecord(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
444
void ContainerNeedsUpdate(int flags);
445
void PageMove(int direction, Selection::selTypes sel=Selection::noSel, bool stuttered = false);
446
enum { cmSame, cmUpper, cmLower } caseMap;
447
virtual std::string CaseMapString(const std::string &s, int caseMapping);
448
void ChangeCaseOfSelection(int caseMapping);
449
void LineTranspose();
450
void Duplicate(bool forLine);
451
virtual void CancelModes();
453
void CursorUpOrDown(int direction, Selection::selTypes sel=Selection::noSel);
454
void ParaUpOrDown(int direction, Selection::selTypes sel=Selection::noSel);
455
int StartEndDisplayLine(int pos, bool start);
456
virtual int KeyCommand(unsigned int iMessage);
457
virtual int KeyDefault(int /* key */, int /*modifiers*/);
458
int KeyDown(int key, bool shift, bool ctrl, bool alt, bool *consumed=0);
460
int GetWhitespaceVisible();
461
void SetWhitespaceVisible(int view);
463
void Indent(bool forwards);
465
virtual CaseFolder *CaseFolderForEncoding();
466
long FindText(uptr_t wParam, sptr_t lParam);
468
long SearchText(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
469
long SearchInTarget(const char *text, int length);
470
void GoToLine(int lineNo);
472
virtual void CopyToClipboard(const SelectionText &selectedText) = 0;
473
char *CopyRange(int start, int end);
474
void CopySelectionRange(SelectionText *ss, bool allowLineCopy=false);
475
void CopyRangeToClipboard(int start, int end);
476
void CopyText(int length, const char *text);
477
void SetDragPosition(SelectionPosition newPos);
478
virtual void DisplayCursor(Window::Cursor c);
479
virtual bool DragThreshold(Point ptStart, Point ptNow);
480
virtual void StartDrag();
481
void DropAt(SelectionPosition position, const char *value, bool moving, bool rectangular);
482
/** PositionInSelection returns true if position in selection. */
483
bool PositionInSelection(int pos);
484
bool PointInSelection(Point pt);
485
bool PointInSelMargin(Point pt);
486
Window::Cursor GetMarginCursor(Point pt);
487
void LineSelection(int lineCurrent_, int lineAnchor_);
488
void WordSelection(int pos);
489
void DwellEnd(bool mouseMoved);
491
virtual void ButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt);
492
void ButtonMove(Point pt);
493
void ButtonUp(Point pt, unsigned int curTime, bool ctrl);
497
virtual void SetTicking(bool on) = 0;
498
virtual bool SetIdle(bool) { return false; }
499
virtual void SetMouseCapture(bool on) = 0;
500
virtual bool HaveMouseCapture() = 0;
501
void SetFocusState(bool focusState);
503
int PositionAfterArea(PRectangle rcArea);
504
void StyleToPositionInView(Position pos);
506
virtual void QueueStyling(int upTo);
508
virtual bool PaintContains(PRectangle rc);
509
bool PaintContainsMargin();
510
void CheckForChangeOutsidePaint(Range r);
511
void SetBraceHighlight(Position pos0, Position pos1, int matchStyle);
513
void SetAnnotationHeights(int start, int end);
514
void SetDocPointer(Document *document);
516
void SetAnnotationVisible(int visible);
518
void Expand(int &line, bool doExpand);
519
void ToggleContraction(int line);
520
int ContractedFoldNext(int lineStart);
521
void EnsureLineVisible(int lineDoc, bool enforcePolicy);
522
int GetTag(char *tagValue, int tagNumber);
523
int ReplaceTarget(bool replacePatterns, const char *text, int length=-1);
525
bool PositionIsHotspot(int position);
526
bool PointIsHotspot(Point pt);
527
void SetHotSpotRange(Point *pt);
528
void GetHotSpotRange(int &hsStart, int &hsEnd);
530
int CodePage() const;
531
virtual bool ValidCodePage(int /* codePage */) const { return true; }
532
int WrapCount(int line);
533
void AddStyledText(char *buffer, int appendLength);
535
virtual sptr_t DefWndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) = 0;
536
void StyleSetMessage(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
537
sptr_t StyleGetMessage(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
539
static const char *StringFromEOLMode(int eolMode);
541
static sptr_t StringResult(sptr_t lParam, const char *val);
544
// Public so the COM thunks can access it.
545
bool IsUnicodeMode() const;
546
// Public so scintilla_send_message can use it.
547
virtual sptr_t WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
548
// Public so scintilla_set_id can use it.
550
// Public so COM methods for drag and drop can set it.
552
friend class AutoSurface;
553
friend class SelectionLineIterator;
557
* A smart pointer class to ensure Surfaces are set up and deleted correctly.
563
AutoSurface(Editor *ed) : surf(0) {
564
if (ed->wMain.GetID()) {
565
surf = Surface::Allocate();
567
surf->Init(ed->wMain.GetID());
568
surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
569
surf->SetDBCSMode(ed->CodePage());
573
AutoSurface(SurfaceID sid, Editor *ed) : surf(0) {
574
if (ed->wMain.GetID()) {
575
surf = Surface::Allocate();
577
surf->Init(sid, ed->wMain.GetID());
578
surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
579
surf->SetDBCSMode(ed->CodePage());
586
Surface *operator->() const {
589
operator Surface *() const {