]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/Editor.h
b9bda04ab4678c4ec19ed761a5bdfd4f688bc436
[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'.
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
261 bool needUpdateUI;
262 Position braces[2];
263 int bracesMatchStyle;
264 int highlightGuideColumn;
265
266 int theEdge;
267
268 enum { notPainting, painting, paintAbandoned } paintState;
269 PRectangle rcPaint;
270 bool paintingAllText;
271
272 int modEventMask;
273
274 SelectionText drag;
275 enum selTypes { noSel, selStream, selRectangle, selLines };
276 selTypes selType;
277 bool moveExtendsSelection;
278 int xStartSelect; ///< x position of start of rectangular selection
279 int xEndSelect; ///< x position of end of rectangular selection
280 bool primarySelection;
281
282 int caretXPolicy;
283 int caretXSlop; ///< Ensure this many pixels visible on both sides of caret
284
285 int caretYPolicy;
286 int caretYSlop; ///< Ensure this many lines visible on both sides of caret
287
288 int visiblePolicy;
289 int visibleSlop;
290
291 int searchAnchor;
292
293 bool recordingMacro;
294
295 int foldFlags;
296 ContractionState cs;
297
298 // Hotspot support
299 int hsStart;
300 int hsEnd;
301
302 // Wrapping support
303 enum { eWrapNone, eWrapWord } wrapState;
304 bool backgroundWrapEnabled;
305 int wrapWidth;
306 int docLineLastWrapped;
307 int docLastLineToWrap;
308 int wrapVisualFlags;
309 int wrapVisualFlagsLocation;
310 int wrapVisualStartIndent;
311 int actualWrapVisualStartIndent;
312
313 Document *pdoc;
314
315 Editor();
316 virtual ~Editor();
317 virtual void Initialise() = 0;
318 virtual void Finalise();
319
320 void InvalidateStyleData();
321 void InvalidateStyleRedraw();
322 virtual void RefreshColourPalette(Palette &pal, bool want);
323 void RefreshStyleData();
324 void DropGraphics();
325
326 virtual PRectangle GetClientRectangle();
327 PRectangle GetTextRectangle();
328
329 int LinesOnScreen();
330 int LinesToScroll();
331 int MaxScrollPos();
332 Point LocationFromPosition(int pos);
333 int XFromPosition(int pos);
334 int PositionFromLocation(Point pt);
335 int PositionFromLocationClose(Point pt);
336 int PositionFromLineX(int line, int x);
337 int LineFromLocation(Point pt);
338 void SetTopLine(int topLineNew);
339
340 bool AbandonPaint();
341 void RedrawRect(PRectangle rc);
342 void Redraw();
343 void RedrawSelMargin();
344 PRectangle RectangleFromRange(int start, int end);
345 void InvalidateRange(int start, int end);
346
347 int CurrentPosition();
348 bool SelectionEmpty();
349 int SelectionStart();
350 int SelectionEnd();
351 void InvalidateSelection(int currentPos_, int anchor_);
352 void SetSelection(int currentPos_, int anchor_);
353 void SetSelection(int currentPos_);
354 void SetEmptySelection(int currentPos_);
355 bool RangeContainsProtected(int start, int end) const;
356 bool SelectionContainsProtected();
357 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true);
358 int MovePositionTo(int newPos, selTypes sel=noSel, bool ensureVisible=true);
359 int MovePositionSoVisible(int pos, int moveDir);
360 void SetLastXChosen();
361
362 void ScrollTo(int line, bool moveThumb=true);
363 virtual void ScrollText(int linesToMove);
364 void HorizontalScrollTo(int xPos);
365 void MoveCaretInsideView(bool ensureVisible=true);
366 int DisplayFromPosition(int pos);
367 void EnsureCaretVisible(bool useMargin=true, bool vert=true, bool horiz=true);
368 void ShowCaretAtCurrentPosition();
369 void DropCaret();
370 void InvalidateCaret();
371
372 void NeedWrapping(int docLineStartWrapping = 0, int docLineEndWrapping = 0x7ffffff);
373 bool WrapLines(bool fullWrap, int priorityWrapLineStart);
374 void LinesJoin();
375 void LinesSplit(int pixelWidth);
376
377 int SubstituteMarkerIfEmpty(int markerCheck, int markerDefault);
378 void PaintSelMargin(Surface *surface, PRectangle &rc);
379 LineLayout *RetrieveLineLayout(int lineNumber);
380 void LayoutLine(int line, Surface *surface, ViewStyle &vstyle, LineLayout *ll,
381 int width=LineLayout::wrapWidthInfinite);
382 ColourAllocated TextBackground(ViewStyle &vsDraw, bool overrideBackground, ColourAllocated background, bool inSelection, bool inHotspot, int styleMain, int i, LineLayout *ll);
383 void DrawIndentGuide(Surface *surface, int lineVisible, int lineHeight, int start, PRectangle rcSegment, bool highlight);
384 void DrawWrapMarker(Surface *surface, PRectangle rcPlace, bool isEndMarker, ColourAllocated wrapColour);
385 void DrawEOL(Surface *surface, ViewStyle &vsDraw, PRectangle rcLine, LineLayout *ll,
386 int line, int lineEnd, int xStart, int subLine, int subLineStart,
387 bool overrideBackground, ColourAllocated background,
388 bool drawWrapMark, ColourAllocated wrapColour);
389 void DrawLine(Surface *surface, ViewStyle &vsDraw, int line, int lineVisible, int xStart,
390 PRectangle rcLine, LineLayout *ll, int subLine=0);
391 void RefreshPixMaps(Surface *surfaceWindow);
392 void Paint(Surface *surfaceWindow, PRectangle rcArea);
393 long FormatRange(bool draw, RangeToFormat *pfr);
394 int TextWidth(int style, const char *text);
395
396 virtual void SetVerticalScrollPos() = 0;
397 virtual void SetHorizontalScrollPos() = 0;
398 virtual bool ModifyScrollBars(int nMax, int nPage) = 0;
399 virtual void ReconfigureScrollBars();
400 void SetScrollBars();
401 void ChangeSize();
402
403 void AddChar(char ch);
404 virtual void AddCharUTF(char *s, unsigned int len, bool treatAsDBCS=false);
405 void ClearSelection();
406 void ClearAll();
407 void ClearDocumentStyle();
408 void Cut();
409 void PasteRectangular(int pos, const char *ptr, int len);
410 virtual void Copy() = 0;
411 virtual bool CanPaste();
412 virtual void Paste() = 0;
413 void Clear();
414 void SelectAll();
415 void Undo();
416 void Redo();
417 void DelChar();
418 void DelCharBack(bool allowLineStartDeletion);
419 virtual void ClaimSelection() = 0;
420
421 virtual void NotifyChange() = 0;
422 virtual void NotifyFocus(bool focus);
423 virtual int GetCtrlID() { return ctrlID; }
424 virtual void NotifyParent(SCNotification scn) = 0;
425 virtual void NotifyStyleToNeeded(int endStyleNeeded);
426 void NotifyChar(int ch);
427 void NotifyMove(int position);
428 void NotifySavePoint(bool isSavePoint);
429 void NotifyModifyAttempt();
430 virtual void NotifyDoubleClick(Point pt, bool shift);
431 void NotifyHotSpotClicked(int position, bool shift, bool ctrl, bool alt);
432 void NotifyHotSpotDoubleClicked(int position, bool shift, bool ctrl, bool alt);
433 void NotifyUpdateUI();
434 void NotifyPainted();
435 bool NotifyMarginClick(Point pt, bool shift, bool ctrl, bool alt);
436 void NotifyNeedShown(int pos, int len);
437 void NotifyDwelling(Point pt, bool state);
438 void NotifyZoom();
439
440 void NotifyModifyAttempt(Document *document, void *userData);
441 void NotifySavePoint(Document *document, void *userData, bool atSavePoint);
442 void CheckModificationForWrap(DocModification mh);
443 void NotifyModified(Document *document, DocModification mh, void *userData);
444 void NotifyDeleted(Document *document, void *userData);
445 void NotifyStyleNeeded(Document *doc, void *userData, int endPos);
446 void NotifyMacroRecord(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
447
448 void PageMove(int direction, selTypes sel=noSel, bool stuttered = false);
449 void ChangeCaseOfSelection(bool makeUpperCase);
450 void LineTranspose();
451 void LineDuplicate();
452 virtual void CancelModes();
453 void NewLine();
454 void CursorUpOrDown(int direction, selTypes sel=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);
459
460 int GetWhitespaceVisible();
461 void SetWhitespaceVisible(int view);
462
463 void Indent(bool forwards);
464
465 long FindText(uptr_t wParam, sptr_t lParam);
466 void SearchAnchor();
467 long SearchText(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
468 long SearchInTarget(const char *text, int length);
469 void GoToLine(int lineNo);
470
471 virtual void CopyToClipboard(const SelectionText &selectedText) = 0;
472 char *CopyRange(int start, int end);
473 void CopySelectionFromRange(SelectionText *ss, int start, int end);
474 void CopySelectionRange(SelectionText *ss);
475 void CopyRangeToClipboard(int start, int end);
476 void CopyText(int length, const char *text);
477 void SetDragPosition(int newPos);
478 virtual void DisplayCursor(Window::Cursor c);
479 virtual void StartDrag();
480 void DropAt(int position, const char *value, bool moving, bool rectangular);
481 /** PositionInSelection returns 0 if position in selection, -1 if position before selection, and 1 if after.
482 * Before means either before any line of selection or before selection on its line, with a similar meaning to after. */
483 int PositionInSelection(int pos);
484 bool PointInSelection(Point pt);
485 bool PointInSelMargin(Point pt);
486 void LineSelection(int lineCurrent_, int lineAnchor_);
487 void DwellEnd(bool mouseMoved);
488 virtual void ButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt);
489 void ButtonMove(Point pt);
490 void ButtonUp(Point pt, unsigned int curTime, bool ctrl);
491
492 void Tick();
493 bool Idle();
494 virtual void SetTicking(bool on) = 0;
495 virtual bool SetIdle(bool) { return false; }
496 virtual void SetMouseCapture(bool on) = 0;
497 virtual bool HaveMouseCapture() = 0;
498 void SetFocusState(bool focusState);
499
500 void CheckForChangeOutsidePaint(Range r);
501 int BraceMatch(int position, int maxReStyle);
502 void SetBraceHighlight(Position pos0, Position pos1, int matchStyle);
503
504 void SetDocPointer(Document *document);
505
506 void Expand(int &line, bool doExpand);
507 void ToggleContraction(int line);
508 void EnsureLineVisible(int lineDoc, bool enforcePolicy);
509 int ReplaceTarget(bool replacePatterns, const char *text, int length=-1);
510
511 bool PositionIsHotspot(int position);
512 bool PointIsHotspot(Point pt);
513 void SetHotSpotRange(Point *pt);
514 void GetHotSpotRange(int& hsStart, int& hsEnd);
515
516 int CodePage() const;
517
518 virtual sptr_t DefWndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) = 0;
519
520 public:
521 // Public so the COM thunks can access it.
522 bool IsUnicodeMode() const;
523 // Public so scintilla_send_message can use it.
524 virtual sptr_t WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
525 // Public so scintilla_set_id can use it.
526 int ctrlID;
527 friend class AutoSurface;
528 friend class SelectionLineIterator;
529 };
530
531 /**
532 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
533 */
534 class AutoSurface {
535 private:
536 Surface *surf;
537 public:
538 AutoSurface(Editor *ed) : surf(0) {
539 if (ed->wMain.GetID()) {
540 surf = Surface::Allocate();
541 if (surf) {
542 surf->Init(ed->wMain.GetID());
543 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
544 surf->SetDBCSMode(ed->CodePage());
545 }
546 }
547 }
548 AutoSurface(SurfaceID sid, Editor *ed) : surf(0) {
549 if (ed->wMain.GetID()) {
550 surf = Surface::Allocate();
551 if (surf) {
552 surf->Init(sid, ed->wMain.GetID());
553 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
554 surf->SetDBCSMode(ed->CodePage());
555 }
556 }
557 }
558 ~AutoSurface() {
559 delete surf;
560 }
561 Surface *operator->() const {
562 return surf;
563 }
564 operator Surface *() const {
565 return surf;
566 }
567 };
568
569 #endif