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