Set release date.
[geany-mirror.git] / scintilla / Editor.h
blob54411fad5c2d5811481caf1a3ac1a21f0d8cf70d
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.
8 #ifndef EDITOR_H
9 #define EDITOR_H
11 #ifdef SCI_NAMESPACE
12 namespace Scintilla {
13 #endif
15 /**
17 class Caret {
18 public:
19 bool active;
20 bool on;
21 int period;
23 Caret();
26 /**
28 class Timer {
29 public:
30 bool ticking;
31 int ticksToWait;
32 enum {tickSize = 100};
33 TickerID tickerID;
35 Timer();
38 /**
40 class Idler {
41 public:
42 bool state;
43 IdlerID idlerID;
45 Idler();
48 /**
49 * When platform has a way to generate an event before painting,
50 * accumulate needed styling range in StyleNeeded to avoid unnecessary work.
52 class StyleNeeded {
53 public:
54 bool active;
55 Position upTo;
57 StyleNeeded() : active(false), upTo(0) {}
58 void Reset() {
59 active = false;
60 upTo = 0;
62 void NeedUpTo(Position pos) {
63 if (upTo < pos)
64 upTo = pos;
68 /**
69 * Hold a piece of text selected for copying or dragging.
70 * The text is expected to hold a terminating '\0' and this is counted in len.
72 class SelectionText {
73 public:
74 char *s;
75 int len;
76 bool rectangular;
77 bool lineCopy;
78 int codePage;
79 int characterSet;
80 SelectionText() : s(0), len(0), rectangular(false), lineCopy(false), codePage(0), characterSet(0) {}
81 ~SelectionText() {
82 Free();
84 void Free() {
85 Set(0, 0, 0, 0, false, false);
87 void Set(char *s_, int len_, int codePage_, int characterSet_, bool rectangular_, bool lineCopy_) {
88 delete []s;
89 s = s_;
90 if (s)
91 len = len_;
92 else
93 len = 0;
94 codePage = codePage_;
95 characterSet = characterSet_;
96 rectangular = rectangular_;
97 lineCopy = lineCopy_;
99 void Copy(const char *s_, int len_, int codePage_, int characterSet_, bool rectangular_, bool lineCopy_) {
100 delete []s;
101 s = 0;
102 s = new char[len_];
103 len = len_;
104 for (int i = 0; i < len_; i++) {
105 s[i] = s_[i];
107 codePage = codePage_;
108 characterSet = characterSet_;
109 rectangular = rectangular_;
110 lineCopy = lineCopy_;
112 void Copy(const SelectionText &other) {
113 Copy(other.s, other.len, other.codePage, other.characterSet, other.rectangular, other.lineCopy);
119 class Editor : public DocWatcher {
120 // Private so Editor objects can not be copied
121 Editor(const Editor &);
122 Editor &operator=(const Editor &);
124 protected: // ScintillaBase subclass needs access to much of Editor
126 /** On GTK+, Scintilla is a container widget holding two scroll bars
127 * whereas on Windows there is just one window with both scroll bars turned on. */
128 Window wMain; ///< The Scintilla parent window
130 /** Style resources may be expensive to allocate so are cached between uses.
131 * When a style attribute is changed, this cache is flushed. */
132 bool stylesValid;
133 ViewStyle vs;
134 Palette palette;
136 int printMagnification;
137 int printColourMode;
138 int printWrapState;
139 int cursorMode;
140 int controlCharSymbol;
142 bool hasFocus;
143 bool hideSelection;
144 bool inOverstrike;
145 bool mouseDownCaptures;
147 /** In bufferedDraw mode, graphics operations are drawn to a pixmap and then copied to
148 * the screen. This avoids flashing but is about 30% slower. */
149 bool bufferedDraw;
150 /** In twoPhaseDraw mode, drawing is performed in two phases, first the background
151 * and then the foreground. This avoids chopping off characters that overlap the next run. */
152 bool twoPhaseDraw;
154 int xOffset; ///< Horizontal scrolled amount in pixels
155 int xCaretMargin; ///< Ensure this many pixels visible on both sides of caret
156 bool horizontalScrollBarVisible;
157 int scrollWidth;
158 bool trackLineWidth;
159 int lineWidthMaxSeen;
160 bool verticalScrollBarVisible;
161 bool endAtLastLine;
162 bool caretSticky;
163 bool multipleSelection;
164 bool additionalSelectionTyping;
165 int multiPasteMode;
166 bool additionalCaretsBlink;
167 bool additionalCaretsVisible;
169 int virtualSpaceOptions;
171 Surface *pixmapLine;
172 Surface *pixmapSelMargin;
173 Surface *pixmapSelPattern;
174 Surface *pixmapIndentGuide;
175 Surface *pixmapIndentGuideHighlight;
177 LineLayoutCache llc;
178 PositionCache posCache;
180 KeyMap kmap;
182 Caret caret;
183 Timer timer;
184 Timer autoScrollTimer;
185 enum { autoScrollDelay = 200 };
187 Idler idler;
189 Point lastClick;
190 unsigned int lastClickTime;
191 int dwellDelay;
192 int ticksToDwell;
193 bool dwelling;
194 enum { selChar, selWord, selLine } selectionType;
195 Point ptMouseLast;
196 enum { ddNone, ddInitial, ddDragging } inDragDrop;
197 bool dropWentOutside;
198 SelectionPosition posDrag;
199 SelectionPosition posDrop;
200 int lastXChosen;
201 int lineAnchor;
202 int originalAnchorPos;
203 int targetStart;
204 int targetEnd;
205 int searchFlags;
206 int topLine;
207 int posTopLine;
208 int lengthForEncode;
210 bool needUpdateUI;
211 Position braces[2];
212 int bracesMatchStyle;
213 int highlightGuideColumn;
215 int theEdge;
217 enum { notPainting, painting, paintAbandoned } paintState;
218 PRectangle rcPaint;
219 bool paintingAllText;
220 StyleNeeded styleNeeded;
222 int modEventMask;
224 SelectionText drag;
225 Selection sel;
226 bool primarySelection;
228 int caretXPolicy;
229 int caretXSlop; ///< Ensure this many pixels visible on both sides of caret
231 int caretYPolicy;
232 int caretYSlop; ///< Ensure this many lines visible on both sides of caret
234 int visiblePolicy;
235 int visibleSlop;
237 int searchAnchor;
239 bool recordingMacro;
241 int foldFlags;
242 ContractionState cs;
244 // Hotspot support
245 int hsStart;
246 int hsEnd;
248 // Wrapping support
249 enum { eWrapNone, eWrapWord, eWrapChar } wrapState;
250 enum { wrapLineLarge = 0x7ffffff };
251 int wrapWidth;
252 int wrapStart;
253 int wrapEnd;
254 int wrapVisualFlags;
255 int wrapVisualFlagsLocation;
256 int wrapVisualStartIndent;
257 int wrapAddIndent; // This will be added to initial indent of line
258 int wrapIndentMode; // SC_WRAPINDENT_FIXED, _SAME, _INDENT
260 bool convertPastes;
262 Document *pdoc;
264 Editor();
265 virtual ~Editor();
266 virtual void Initialise() = 0;
267 virtual void Finalise();
269 void InvalidateStyleData();
270 void InvalidateStyleRedraw();
271 virtual void RefreshColourPalette(Palette &pal, bool want);
272 void RefreshStyleData();
273 void DropGraphics();
275 virtual PRectangle GetClientRectangle();
276 PRectangle GetTextRectangle();
278 int LinesOnScreen();
279 int LinesToScroll();
280 int MaxScrollPos();
281 SelectionPosition ClampPositionIntoDocument(SelectionPosition sp) const;
282 Point LocationFromPosition(SelectionPosition pos);
283 Point LocationFromPosition(int pos);
284 int XFromPosition(int pos);
285 int XFromPosition(SelectionPosition sp);
286 SelectionPosition SPositionFromLocation(Point pt, bool canReturnInvalid=false, bool charPosition=false, bool virtualSpace=true);
287 int PositionFromLocation(Point pt, bool canReturnInvalid=false, bool charPosition=false);
288 SelectionPosition SPositionFromLineX(int lineDoc, int x);
289 int PositionFromLineX(int line, int x);
290 int LineFromLocation(Point pt);
291 void SetTopLine(int topLineNew);
293 bool AbandonPaint();
294 void RedrawRect(PRectangle rc);
295 void Redraw();
296 void RedrawSelMargin(int line=-1, bool allAfter=false);
297 PRectangle RectangleFromRange(int start, int end);
298 void InvalidateRange(int start, int end);
300 bool UserVirtualSpace() const {
301 return ((virtualSpaceOptions & SCVS_USERACCESSIBLE) != 0);
303 int CurrentPosition();
304 bool SelectionEmpty();
305 SelectionPosition SelectionStart();
306 SelectionPosition SelectionEnd();
307 void SetRectangularRange();
308 void ThinRectangularRange();
309 void InvalidateSelection(SelectionRange newMain, bool invalidateWholeSelection=false);
310 void SetSelection(SelectionPosition currentPos_, SelectionPosition anchor_);
311 void SetSelection(int currentPos_, int anchor_);
312 void SetSelection(SelectionPosition currentPos_);
313 void SetSelection(int currentPos_);
314 void SetEmptySelection(SelectionPosition currentPos_);
315 void SetEmptySelection(int currentPos_);
316 bool RangeContainsProtected(int start, int end) const;
317 bool SelectionContainsProtected();
318 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true) const;
319 SelectionPosition MovePositionOutsideChar(SelectionPosition pos, int moveDir, bool checkLineEnd=true) const;
320 int MovePositionTo(SelectionPosition newPos, Selection::selTypes sel=Selection::noSel, bool ensureVisible=true);
321 int MovePositionTo(int newPos, Selection::selTypes sel=Selection::noSel, bool ensureVisible=true);
322 SelectionPosition MovePositionSoVisible(SelectionPosition pos, int moveDir);
323 SelectionPosition MovePositionSoVisible(int pos, int moveDir);
324 Point PointMainCaret();
325 void SetLastXChosen();
327 void ScrollTo(int line, bool moveThumb=true);
328 virtual void ScrollText(int linesToMove);
329 void HorizontalScrollTo(int xPos);
330 void MoveCaretInsideView(bool ensureVisible=true);
331 int DisplayFromPosition(int pos);
333 struct XYScrollPosition {
334 int xOffset;
335 int topLine;
336 XYScrollPosition(int xOffset_, int topLine_) : xOffset(xOffset_), topLine(topLine_) {}
338 XYScrollPosition XYScrollToMakeVisible(const bool useMargin, const bool vert, const bool horiz);
339 void SetXYScroll(XYScrollPosition newXY);
340 void EnsureCaretVisible(bool useMargin=true, bool vert=true, bool horiz=true);
341 void ShowCaretAtCurrentPosition();
342 void DropCaret();
343 void InvalidateCaret();
344 virtual void UpdateSystemCaret();
346 void NeedWrapping(int docLineStart = 0, int docLineEnd = wrapLineLarge);
347 bool WrapOneLine(Surface *surface, int lineToWrap);
348 bool WrapLines(bool fullWrap, int priorityWrapLineStart);
349 void LinesJoin();
350 void LinesSplit(int pixelWidth);
352 int SubstituteMarkerIfEmpty(int markerCheck, int markerDefault);
353 void PaintSelMargin(Surface *surface, PRectangle &rc);
354 LineLayout *RetrieveLineLayout(int lineNumber);
355 void LayoutLine(int line, Surface *surface, ViewStyle &vstyle, LineLayout *ll,
356 int width=LineLayout::wrapWidthInfinite);
357 ColourAllocated SelectionBackground(ViewStyle &vsDraw, bool main);
358 ColourAllocated TextBackground(ViewStyle &vsDraw, bool overrideBackground, ColourAllocated background, int inSelection, bool inHotspot, int styleMain, int i, LineLayout *ll);
359 void DrawIndentGuide(Surface *surface, int lineVisible, int lineHeight, int start, PRectangle rcSegment, bool highlight);
360 void DrawWrapMarker(Surface *surface, PRectangle rcPlace, bool isEndMarker, ColourAllocated wrapColour);
361 void DrawEOL(Surface *surface, ViewStyle &vsDraw, PRectangle rcLine, LineLayout *ll,
362 int line, int lineEnd, int xStart, int subLine, int subLineStart,
363 bool overrideBackground, ColourAllocated background,
364 bool drawWrapMark, ColourAllocated wrapColour);
365 void DrawIndicators(Surface *surface, ViewStyle &vsDraw, int line, int xStart,
366 PRectangle rcLine, LineLayout *ll, int subLine, int lineEnd, bool under);
367 void DrawAnnotation(Surface *surface, ViewStyle &vsDraw, int line, int xStart,
368 PRectangle rcLine, LineLayout *ll, int subLine);
369 void DrawLine(Surface *surface, ViewStyle &vsDraw, int line, int lineVisible, int xStart,
370 PRectangle rcLine, LineLayout *ll, int subLine);
371 void DrawBlockCaret(Surface *surface, ViewStyle &vsDraw, LineLayout *ll, int subLine,
372 int xStart, int offset, int posCaret, PRectangle rcCaret, ColourAllocated caretColour);
373 void DrawCarets(Surface *surface, ViewStyle &vsDraw, int line, int xStart,
374 PRectangle rcLine, LineLayout *ll, int subLine);
375 void RefreshPixMaps(Surface *surfaceWindow);
376 void Paint(Surface *surfaceWindow, PRectangle rcArea);
377 long FormatRange(bool draw, Sci_RangeToFormat *pfr);
378 int TextWidth(int style, const char *text);
380 virtual void SetVerticalScrollPos() = 0;
381 virtual void SetHorizontalScrollPos() = 0;
382 virtual bool ModifyScrollBars(int nMax, int nPage) = 0;
383 virtual void ReconfigureScrollBars();
384 void SetScrollBars();
385 void ChangeSize();
387 void FilterSelections();
388 int InsertSpace(int position, unsigned int spaces);
389 void AddChar(char ch);
390 virtual void AddCharUTF(char *s, unsigned int len, bool treatAsDBCS=false);
391 void InsertPaste(SelectionPosition selStart, const char *text, int len);
392 void ClearSelection();
393 void ClearAll();
394 void ClearDocumentStyle();
395 void Cut();
396 void PasteRectangular(SelectionPosition pos, const char *ptr, int len);
397 virtual void Copy() = 0;
398 virtual void CopyAllowLine();
399 virtual bool CanPaste();
400 virtual void Paste() = 0;
401 void Clear();
402 void SelectAll();
403 void Undo();
404 void Redo();
405 void DelChar();
406 void DelCharBack(bool allowLineStartDeletion);
407 virtual void ClaimSelection() = 0;
409 virtual void NotifyChange() = 0;
410 virtual void NotifyFocus(bool focus);
411 virtual int GetCtrlID() { return ctrlID; }
412 virtual void NotifyParent(SCNotification scn) = 0;
413 virtual void NotifyStyleToNeeded(int endStyleNeeded);
414 void NotifyChar(int ch);
415 void NotifySavePoint(bool isSavePoint);
416 void NotifyModifyAttempt();
417 virtual void NotifyDoubleClick(Point pt, bool shift, bool ctrl, bool alt);
418 void NotifyHotSpotClicked(int position, bool shift, bool ctrl, bool alt);
419 void NotifyHotSpotDoubleClicked(int position, bool shift, bool ctrl, bool alt);
420 void NotifyUpdateUI();
421 void NotifyPainted();
422 void NotifyIndicatorClick(bool click, int position, bool shift, bool ctrl, bool alt);
423 bool NotifyMarginClick(Point pt, bool shift, bool ctrl, bool alt);
424 void NotifyNeedShown(int pos, int len);
425 void NotifyDwelling(Point pt, bool state);
426 void NotifyZoom();
428 void NotifyModifyAttempt(Document *document, void *userData);
429 void NotifySavePoint(Document *document, void *userData, bool atSavePoint);
430 void CheckModificationForWrap(DocModification mh);
431 void NotifyModified(Document *document, DocModification mh, void *userData);
432 void NotifyDeleted(Document *document, void *userData);
433 void NotifyStyleNeeded(Document *doc, void *userData, int endPos);
434 void NotifyMacroRecord(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
436 void PageMove(int direction, Selection::selTypes sel=Selection::noSel, bool stuttered = false);
437 enum { cmSame, cmUpper, cmLower } caseMap;
438 virtual std::string CaseMapString(const std::string &s, int caseMapping);
439 void ChangeCaseOfSelection(int caseMapping);
440 void LineTranspose();
441 void Duplicate(bool forLine);
442 virtual void CancelModes();
443 void NewLine();
444 void CursorUpOrDown(int direction, Selection::selTypes sel=Selection::noSel);
445 void ParaUpOrDown(int direction, Selection::selTypes sel=Selection::noSel);
446 int StartEndDisplayLine(int pos, bool start);
447 virtual int KeyCommand(unsigned int iMessage);
448 virtual int KeyDefault(int /* key */, int /*modifiers*/);
449 int KeyDown(int key, bool shift, bool ctrl, bool alt, bool *consumed=0);
451 int GetWhitespaceVisible();
452 void SetWhitespaceVisible(int view);
454 void Indent(bool forwards);
456 virtual CaseFolder *CaseFolderForEncoding();
457 long FindText(uptr_t wParam, sptr_t lParam);
458 void SearchAnchor();
459 long SearchText(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
460 long SearchInTarget(const char *text, int length);
461 void GoToLine(int lineNo);
463 virtual void CopyToClipboard(const SelectionText &selectedText) = 0;
464 char *CopyRange(int start, int end);
465 void CopySelectionRange(SelectionText *ss, bool allowLineCopy=false);
466 void CopyRangeToClipboard(int start, int end);
467 void CopyText(int length, const char *text);
468 void SetDragPosition(SelectionPosition newPos);
469 virtual void DisplayCursor(Window::Cursor c);
470 virtual bool DragThreshold(Point ptStart, Point ptNow);
471 virtual void StartDrag();
472 void DropAt(SelectionPosition position, const char *value, bool moving, bool rectangular);
473 /** PositionInSelection returns true if position in selection. */
474 bool PositionInSelection(int pos);
475 bool PointInSelection(Point pt);
476 bool PointInSelMargin(Point pt);
477 void LineSelection(int lineCurrent_, int lineAnchor_);
478 void DwellEnd(bool mouseMoved);
479 void MouseLeave();
480 virtual void ButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt);
481 void ButtonMove(Point pt);
482 void ButtonUp(Point pt, unsigned int curTime, bool ctrl);
484 void Tick();
485 bool Idle();
486 virtual void SetTicking(bool on) = 0;
487 virtual bool SetIdle(bool) { return false; }
488 virtual void SetMouseCapture(bool on) = 0;
489 virtual bool HaveMouseCapture() = 0;
490 void SetFocusState(bool focusState);
492 int PositionAfterArea(PRectangle rcArea);
493 void StyleToPositionInView(Position pos);
494 void IdleStyling();
495 virtual void QueueStyling(int upTo);
497 virtual bool PaintContains(PRectangle rc);
498 bool PaintContainsMargin();
499 void CheckForChangeOutsidePaint(Range r);
500 void SetBraceHighlight(Position pos0, Position pos1, int matchStyle);
502 void SetAnnotationHeights(int start, int end);
503 void SetDocPointer(Document *document);
505 void SetAnnotationVisible(int visible);
507 void Expand(int &line, bool doExpand);
508 void ToggleContraction(int line);
509 void EnsureLineVisible(int lineDoc, bool enforcePolicy);
510 int GetTag(char *tagValue, int tagNumber);
511 int ReplaceTarget(bool replacePatterns, const char *text, int length=-1);
513 bool PositionIsHotspot(int position);
514 bool PointIsHotspot(Point pt);
515 void SetHotSpotRange(Point *pt);
516 void GetHotSpotRange(int &hsStart, int &hsEnd);
518 int CodePage() const;
519 virtual bool ValidCodePage(int /* codePage */) const { return true; }
520 int WrapCount(int line);
521 void AddStyledText(char *buffer, int appendLength);
523 virtual sptr_t DefWndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) = 0;
524 void StyleSetMessage(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
525 sptr_t StyleGetMessage(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
527 static const char *StringFromEOLMode(int eolMode);
529 static sptr_t StringResult(sptr_t lParam, const char *val);
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 // Public so COM methods for drag and drop can set it.
539 int errorStatus;
540 friend class AutoSurface;
541 friend class SelectionLineIterator;
545 * A smart pointer class to ensure Surfaces are set up and deleted correctly.
547 class AutoSurface {
548 private:
549 Surface *surf;
550 public:
551 AutoSurface(Editor *ed) : surf(0) {
552 if (ed->wMain.GetID()) {
553 surf = Surface::Allocate();
554 if (surf) {
555 surf->Init(ed->wMain.GetID());
556 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
557 surf->SetDBCSMode(ed->CodePage());
561 AutoSurface(SurfaceID sid, Editor *ed) : surf(0) {
562 if (ed->wMain.GetID()) {
563 surf = Surface::Allocate();
564 if (surf) {
565 surf->Init(sid, ed->wMain.GetID());
566 surf->SetUnicodeMode(SC_CP_UTF8 == ed->CodePage());
567 surf->SetDBCSMode(ed->CodePage());
571 ~AutoSurface() {
572 delete surf;
574 Surface *operator->() const {
575 return surf;
577 operator Surface *() const {
578 return surf;
582 #ifdef SCI_NAMESPACE
584 #endif
586 #endif