]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/Editor.h
Updated to Scintilla 1.62
[wxWidgets.git] / contrib / src / stc / scintilla / src / Editor.h
1 // Scintilla source code edit control
2 /** @file Editor.h
3 ** Defines the main editor class.
4 **/
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.
7
8 #ifndef EDITOR_H
9 #define EDITOR_H
10
11 /**
12 */
13 class Caret {
14 public:
15 bool active;
16 bool on;
17 int period;
18
19 Caret();
20 };
21
22 /**
23 */
24 class Timer {
25 public:
26 bool ticking;
27 int ticksToWait;
28 enum {tickSize = 100};
29 TickerID tickerID;
30
31 Timer();
32 };
33
34 /**
35 */
36 class Idler {
37 public:
38 bool state;
39 IdlerID idlerID;
40
41 Idler();
42 };
43
44 /**
45 */
46 class LineLayout {
47 private:
48 friend class LineLayoutCache;
49 int *lineStarts;
50 int lenLineStarts;
51 /// Drawing is only performed for @a maxLineLength characters on each line.
52 int lineNumber;
53 bool inCache;
54 public:
55 enum { wrapWidthInfinite = 0x7ffffff };
56 int maxLineLength;
57 int numCharsInLine;
58 enum validLevel { llInvalid, llCheckTextAndStyle, llPositions, llLines } validity;
59 int xHighlightGuide;
60 bool highlightColumn;
61 int selStart;
62 int selEnd;
63 bool containsCaret;
64 int edgeColumn;
65 char *chars;
66 unsigned char *styles;
67 char *indicators;
68 int *positions;
69 char bracePreviousStyles[2];
70
71 // Hotspot support
72 int hsStart;
73 int hsEnd;
74
75 // Wrapped line support
76 int widthLine;
77 int lines;
78
79 LineLayout(int maxLineLength_);
80 virtual ~LineLayout();
81 void Resize(int maxLineLength_);
82 void Free();
83 void Invalidate(validLevel validity_);
84 int LineStart(int line) {
85 if (line <= 0) {
86 return 0;
87 } else if ((line >= lines) || !lineStarts) {
88 return numCharsInLine;
89 } else {
90 return lineStarts[line];
91 }
92 }
93 void SetLineStart(int line, int start);
94 void SetBracesHighlight(Range rangeLine, Position braces[],
95 char bracesMatchStyle, int xHighlight);
96 void RestoreBracesHighlight(Range rangeLine, Position braces[]);
97 };
98
99 /**
100 */
101 class LineLayoutCache {
102 int level;
103 int length;
104 int size;
105 LineLayout **cache;
106 bool allInvalidated;
107 int styleClock;
108 void Allocate(int length_);
109 void AllocateForLevel(int linesOnScreen, int linesInDoc);
110 public:
111 LineLayoutCache();
112 virtual ~LineLayoutCache();
113 void Deallocate();
114 enum {
115 llcNone=SC_CACHE_NONE,
116 llcCaret=SC_CACHE_CARET,
117 llcPage=SC_CACHE_PAGE,
118 llcDocument=SC_CACHE_DOCUMENT
119 };
120 void Invalidate(LineLayout::validLevel validity_);
121 void SetLevel(int level_);
122 int GetLevel() { return level; }
123 LineLayout *Retrieve(int lineNumber, int lineCaret, int maxChars, int styleClock_,
124 int linesOnScreen, int linesInDoc);
125 void Dispose(LineLayout *ll);
126 };
127
128 /**
129 * Hold a piece of text selected for copying or dragging.
130 * The text is expected to hold a terminating '\0' and this is counted in len.
131 */
132 class SelectionText {
133 public:
134 char *s;
135 int len;
136 bool rectangular;
137 int codePage;
138 int characterSet;
139 SelectionText() : s(0), len(0), rectangular(false), codePage(0), characterSet(0) {}
140 ~SelectionText() {
141 Free();
142 }
143 void Free() {
144 Set(0, 0, 0, 0, false);
145 }
146 void Set(char *s_, int len_, int codePage_, int characterSet_, bool rectangular_) {
147 delete []s;
148 s = s_;
149 if (s)
150 len = len_;
151 else
152 len = 0;
153 codePage = codePage_;
154 characterSet = characterSet_;
155 rectangular = rectangular_;
156 }
157 void Copy(const char *s_, int len_, int codePage_, int characterSet_, bool rectangular_) {
158 delete []s;
159 s = new char[len_];
160 if (s) {
161 len = len_;
162 for (int i = 0; i < len_; i++) {
163 s[i] = s_[i];
164 }
165 } else {
166 len = 0;
167 }
168 codePage = codePage_;
169 characterSet = characterSet_;
170 rectangular = rectangular_;
171 }
172 void Copy(const SelectionText &other) {
173 Copy(other.s, other.len, other.codePage, other.characterSet, other.rectangular);
174 }
175 };
176
177 /**
178 */
179 class Editor : public DocWatcher {
180 // Private so Editor objects can not be copied
181 Editor(const Editor &) : DocWatcher() {}
182 Editor &operator=(const Editor &) { return *this; }
183
184 protected: // ScintillaBase subclass needs access to much of Editor
185
186 /** On GTK+, Scintilla is a container widget holding two scroll bars
187 * whereas on Windows there is just one window with both scroll bars turned on. */
188 Window wMain; ///< The Scintilla parent window
189
190 /** Style resources may be expensive to allocate so are cached between uses.
191 * When a style attribute is changed, this cache is flushed. */
192 bool stylesValid;
193 ViewStyle vs;
194 Palette palette;
195
196 int printMagnification;
197 int printColourMode;
198 int printWrapState;
199 int cursorMode;
200 int controlCharSymbol;
201
202 bool hasFocus;
203 bool hideSelection;
204 bool inOverstrike;
205 int errorStatus;
206 bool mouseDownCaptures;
207
208 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
209 * the screen. This avoids flashing but is about 30% slower. */
210 bool bufferedDraw;
211 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
212 * and then the foreground. This avoids chopping off characters that overlap the next run. */
213 bool twoPhaseDraw;
214
215 int xOffset; ///< Horizontal scrolled amount in pixels
216 int xCaretMargin; ///< Ensure this many pixels visible on both sides of caret
217 bool horizontalScrollBarVisible;
218 int scrollWidth;
219 bool verticalScrollBarVisible;
220 bool endAtLastLine;
221
222 Surface *pixmapLine;
223 Surface *pixmapSelMargin;
224 Surface *pixmapSelPattern;
225 Surface *pixmapIndentGuide;
226 Surface *pixmapIndentGuideHighlight;
227
228 LineLayoutCache llc;
229
230 KeyMap kmap;
231
232 Caret caret;
233 Timer timer;
234 Timer autoScrollTimer;
235 enum { autoScrollDelay = 200 };
236
237 Idler idler;
238
239 Point lastClick;
240 unsigned int lastClickTime;
241 int dwellDelay;
242 int ticksToDwell;
243 bool dwelling;
244 enum { selChar, selWord, selLine } selectionType;
245 Point ptMouseLast;
246 bool inDragDrop;
247 bool dropWentOutside;
248 int posDrag;
249 int posDrop;
250 int lastXChosen;
251 int lineAnchor;
252 int originalAnchorPos;
253 int currentPos;
254 int anchor;
255 int targetStart;
256 int targetEnd;
257 int searchFlags;
258 int topLine;
259 int posTopLine;
260 int lengthForEncode;
261
262 bool needUpdateUI;
263 Position braces[2];
264 int bracesMatchStyle;
265 int highlightGuideColumn;
266
267 int theEdge;
268
269 enum { notPainting, painting, paintAbandoned } paintState;
270 PRectangle rcPaint;
271 bool paintingAllText;
272
273 int modEventMask;
274
275 SelectionText drag;
276 enum selTypes { noSel, selStream, selRectangle, selLines };
277 selTypes selType;
278 bool moveExtendsSelection;
279 int xStartSelect; ///< x position of start of rectangular selection
280 int xEndSelect; ///< x position of end of rectangular selection
281 bool primarySelection;
282
283 int caretXPolicy;
284 int caretXSlop; ///< Ensure this many pixels visible on both sides of caret
285
286 int caretYPolicy;
287 int caretYSlop; ///< Ensure this many lines visible on both sides of caret
288
289 int visiblePolicy;
290 int visibleSlop;
291
292 int searchAnchor;
293
294 bool recordingMacro;
295
296 int foldFlags;
297 ContractionState cs;
298
299 // Hotspot support
300 int hsStart;
301 int hsEnd;
302
303 // Wrapping support
304 enum { eWrapNone, eWrapWord } wrapState;
305 bool backgroundWrapEnabled;
306 int wrapWidth;
307 int docLineLastWrapped;
308 int docLastLineToWrap;
309 int wrapVisualFlags;
310 int wrapVisualFlagsLocation;
311 int wrapVisualStartIndent;
312 int actualWrapVisualStartIndent;
313
314 Document *pdoc;
315
316 Editor();
317 virtual ~Editor();
318 virtual void Initialise() = 0;
319 virtual void Finalise();
320
321 void InvalidateStyleData();
322 void InvalidateStyleRedraw();
323 virtual void RefreshColourPalette(Palette &pal, bool want);
324 void RefreshStyleData();
325 void DropGraphics();
326
327 virtual PRectangle GetClientRectangle();
328 PRectangle GetTextRectangle();
329
330 int LinesOnScreen();
331 int LinesToScroll();
332 int MaxScrollPos();
333 Point LocationFromPosition(int pos);
334 int XFromPosition(int pos);
335 int PositionFromLocation(Point pt);
336 int PositionFromLocationClose(Point pt);
337 int PositionFromLineX(int line, int x);
338 int LineFromLocation(Point pt);
339 void SetTopLine(int topLineNew);
340
341 bool AbandonPaint();
342 void RedrawRect(PRectangle rc);
343 void Redraw();
344 void RedrawSelMargin();
345 PRectangle RectangleFromRange(int start, int end);
346 void InvalidateRange(int start, int end);
347
348 int CurrentPosition();
349 bool SelectionEmpty();
350 int SelectionStart();
351 int SelectionEnd();
352 void InvalidateSelection(int currentPos_, int anchor_);
353 void SetSelection(int currentPos_, int anchor_);
354 void SetSelection(int currentPos_);
355 void SetEmptySelection(int currentPos_);
356 bool RangeContainsProtected(int start, int end) const;
357 bool SelectionContainsProtected();
358 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true);
359 int MovePositionTo(int newPos, selTypes sel=noSel, bool ensureVisible=true);
360 int MovePositionSoVisible(int pos, int moveDir);
361 void SetLastXChosen();
362
363 void ScrollTo(int line, bool moveThumb=true);
364 virtual void ScrollText(int linesToMove);
365 void HorizontalScrollTo(int xPos);
366 void MoveCaretInsideView(bool ensureVisible=true);
367 int DisplayFromPosition(int pos);
368 void EnsureCaretVisible(bool useMargin=true, bool vert=true, bool horiz=true);
369 void ShowCaretAtCurrentPosition();
370 void DropCaret();
371 void InvalidateCaret();
372 virtual void UpdateSystemCaret();
373
374 void NeedWrapping(int docLineStartWrapping = 0, int docLineEndWrapping = 0x7ffffff);
375 bool WrapLines(bool fullWrap, int priorityWrapLineStart);
376 void LinesJoin();
377 void LinesSplit(int pixelWidth);
378
379 int SubstituteMarkerIfEmpty(int markerCheck, int markerDefault);
380 void PaintSelMargin(Surface *surface, PRectangle &rc);
381 LineLayout *RetrieveLineLayout(int lineNumber);
382 void LayoutLine(int line, Surface *surface, ViewStyle &vstyle, LineLayout *ll,
383 int width=LineLayout::wrapWidthInfinite);
384 ColourAllocated TextBackground(ViewStyle &vsDraw, bool overrideBackground, ColourAllocated background, bool inSelection, bool inHotspot, int styleMain, int i, LineLayout *ll);
385 void DrawIndentGuide(Surface *surface, int lineVisible, int lineHeight, int start, PRectangle rcSegment, bool highlight);
386 void DrawWrapMarker(Surface *surface, PRectangle rcPlace, bool isEndMarker, ColourAllocated wrapColour);
387 void DrawEOL(Surface *surface, ViewStyle &vsDraw, PRectangle rcLine, LineLayout *ll,
388 int line, int lineEnd, int xStart, int subLine, int subLineStart,
389 bool overrideBackground, ColourAllocated background,
390 bool drawWrapMark, ColourAllocated wrapColour);
391 void DrawLine(Surface *surface, ViewStyle &vsDraw, int line, int lineVisible, int xStart,
392 PRectangle rcLine, LineLayout *ll, int subLine=0);
393 void RefreshPixMaps(Surface *surfaceWindow);
394 void Paint(Surface *surfaceWindow, PRectangle rcArea);
395 long FormatRange(bool draw, RangeToFormat *pfr);
396 int TextWidth(int style, const char *text);
397
398 virtual void SetVerticalScrollPos() = 0;
399 virtual void SetHorizontalScrollPos() = 0;
400 virtual bool ModifyScrollBars(int nMax, int nPage) = 0;
401 virtual void ReconfigureScrollBars();
402 void SetScrollBars();
403 void ChangeSize();
404
405 void AddChar(char ch);
406 virtual void AddCharUTF(char *s, unsigned int len, bool treatAsDBCS=false);
407 void ClearSelection();
408 void ClearAll();
409 void ClearDocumentStyle();
410 void Cut();
411 void PasteRectangular(int pos, const char *ptr, int len);
412 virtual void Copy() = 0;
413 virtual bool CanPaste();
414 virtual void Paste() = 0;
415 void Clear();
416 void SelectAll();
417 void Undo();
418 void Redo();
419 void DelChar();
420 void DelCharBack(bool allowLineStartDeletion);
421 virtual void ClaimSelection() = 0;
422
423 virtual void NotifyChange() = 0;
424 virtual void NotifyFocus(bool focus);
425 virtual int GetCtrlID() { return ctrlID; }
426 virtual void NotifyParent(SCNotification scn) = 0;
427 virtual void NotifyStyleToNeeded(int endStyleNeeded);
428 void NotifyChar(int ch);
429 void NotifyMove(int position);
430 void NotifySavePoint(bool isSavePoint);
431 void NotifyModifyAttempt();
432 virtual void NotifyDoubleClick(Point pt, bool shift);
433 void NotifyHotSpotClicked(int position, bool shift, bool ctrl, bool alt);
434 void NotifyHotSpotDoubleClicked(int position, bool shift, bool ctrl, bool alt);
435 void NotifyUpdateUI();
436 void NotifyPainted();
437 bool NotifyMarginClick(Point pt, bool shift, bool ctrl, bool alt);
438 void NotifyNeedShown(int pos, int len);
439 void NotifyDwelling(Point pt, bool state);
440 void NotifyZoom();
441
442 void NotifyModifyAttempt(Document *document, void *userData);
443 void NotifySavePoint(Document *document, void *userData, bool atSavePoint);
444 void CheckModificationForWrap(DocModification mh);
445 void NotifyModified(Document *document, DocModification mh, void *userData);
446 void NotifyDeleted(Document *document, void *userData);
447 void NotifyStyleNeeded(Document *doc, void *userData, int endPos);
448 void NotifyMacroRecord(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
449
450 void PageMove(int direction, selTypes sel=noSel, bool stuttered = false);
451 void ChangeCaseOfSelection(bool makeUpperCase);
452 void LineTranspose();
453 void LineDuplicate();
454 virtual void CancelModes();
455 void NewLine();
456 void CursorUpOrDown(int direction, selTypes sel=noSel);
457 int StartEndDisplayLine(int pos, bool start);
458 virtual int KeyCommand(unsigned int iMessage);
459 virtual int KeyDefault(int /* key */, int /*modifiers*/);
460 int KeyDown(int key, bool shift, bool ctrl, bool alt, bool *consumed=0);
461
462 int GetWhitespaceVisible();
463 void SetWhitespaceVisible(int view);
464
465 void Indent(bool forwards);
466
467 long FindText(uptr_t wParam, sptr_t lParam);
468 void SearchAnchor();
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);
472
473 virtual void CopyToClipboard(const SelectionText &selectedText) = 0;
474 char *CopyRange(int start, int end);
475 void CopySelectionFromRange(SelectionText *ss, int start, int end);
476 void CopySelectionRange(SelectionText *ss);
477 void CopyRangeToClipboard(int start, int end);
478 void CopyText(int length, const char *text);
479 void SetDragPosition(int newPos);
480 virtual void DisplayCursor(Window::Cursor c);
481 virtual void StartDrag();
482 void DropAt(int position, const char *value, bool moving, bool rectangular);
483 /** PositionInSelection returns 0 if position in selection, -1 if position before selection, and 1 if after.
484 * Before means either before any line of selection or before selection on its line, with a similar meaning to after. */
485 int PositionInSelection(int pos);
486 bool PointInSelection(Point pt);
487 bool PointInSelMargin(Point pt);
488 void LineSelection(int lineCurrent_, int lineAnchor_);
489 void DwellEnd(bool mouseMoved);
490 virtual void ButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt);
491 void ButtonMove(Point pt);
492 void ButtonUp(Point pt, unsigned int curTime, bool ctrl);
493
494 void Tick();
495 bool Idle();
496 virtual void SetTicking(bool on) = 0;
497 virtual bool SetIdle(bool) { return false; }
498 virtual void SetMouseCapture(bool on) = 0;
499 virtual bool HaveMouseCapture() = 0;
500 void SetFocusState(bool focusState);
501
502 void CheckForChangeOutsidePaint(Range r);
503 int BraceMatch(int position, int maxReStyle);
504 void SetBraceHighlight(Position pos0, Position pos1, int matchStyle);
505
506 void SetDocPointer(Document *document);
507
508 void Expand(int &line, bool doExpand);
509 void ToggleContraction(int line);
510 void EnsureLineVisible(int lineDoc, bool enforcePolicy);
511 int ReplaceTarget(bool replacePatterns, const char *text, int length=-1);
512
513 bool PositionIsHotspot(int position);
514 bool PointIsHotspot(Point pt);
515 void SetHotSpotRange(Point *pt);
516 void GetHotSpotRange(int& hsStart, int& hsEnd);
517
518 int CodePage() const;
519
520 virtual sptr_t DefWndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) = 0;
521
522 public:
523 // Public so the COM thunks can access it.
524 bool IsUnicodeMode() const;
525 // Public so scintilla_send_message can use it.
526 virtual sptr_t WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
527 // Public so scintilla_set_id can use it.
528 int ctrlID;
529 friend class AutoSurface;
530 friend class SelectionLineIterator;
531 };
532
533 /**
534 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
535 */
536 class AutoSurface {
537 private:
538 Surface *surf;
539 public:
540 AutoSurface(Editor *ed) : surf(0) {
541 if (ed->wMain.GetID()) {
542 surf = Surface::Allocate();
543 if (surf) {
544 surf->Init(ed->wMain.GetID());
545 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
546 surf->SetDBCSMode(ed->CodePage());
547 }
548 }
549 }
550 AutoSurface(SurfaceID sid, Editor *ed) : surf(0) {
551 if (ed->wMain.GetID()) {
552 surf = Surface::Allocate();
553 if (surf) {
554 surf->Init(sid, ed->wMain.GetID());
555 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
556 surf->SetDBCSMode(ed->CodePage());
557 }
558 }
559 }
560 ~AutoSurface() {
561 delete surf;
562 }
563 Surface *operator->() const {
564 return surf;
565 }
566 operator Surface *() const {
567 return surf;
568 }
569 };
570
571 #endif