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