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