Better compatibility between wxRichTextCtrl and wxTextCtrlBase
[wxWidgets.git] / include / wx / richtext / richtextctrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/richtext/richtextctrl.h
3 // Purpose: A rich edit control
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2005-09-30
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_RICHTEXTCTRL_H_
13 #define _WX_RICHTEXTCTRL_H_
14
15 #include "wx/richtext/richtextbuffer.h"
16
17 #if wxUSE_RICHTEXT
18
19 #include "wx/scrolwin.h"
20 #include "wx/caret.h"
21
22 #if wxCHECK_VERSION(2,7,0)
23 #define wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE 1
24 #else
25 #define wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE 0
26 #endif
27
28 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
29 #include "wx/textctrl.h"
30 #endif
31
32 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleDefinition;
33
34 /*!
35 * Styles and flags
36 */
37
38 /* Styles
39 */
40
41 #define wxRE_READONLY 0x0010
42 #define wxRE_MULTILINE 0x0020
43
44 /* Flags
45 */
46
47 #define wxRICHTEXT_SHIFT_DOWN 0x01
48 #define wxRICHTEXT_CTRL_DOWN 0x02
49 #define wxRICHTEXT_ALT_DOWN 0x04
50
51 /* Defaults
52 */
53
54 #define wxRICHTEXT_DEFAULT_OVERALL_SIZE wxSize(-1, -1)
55 #define wxRICHTEXT_DEFAULT_IMAGE_SIZE wxSize(80, 80)
56 #define wxRICHTEXT_DEFAULT_SPACING 3
57 #define wxRICHTEXT_DEFAULT_MARGIN 3
58 #define wxRICHTEXT_DEFAULT_UNFOCUSSED_BACKGROUND wxColour(175, 175, 175)
59 #define wxRICHTEXT_DEFAULT_FOCUSSED_BACKGROUND wxColour(140, 140, 140)
60 #define wxRICHTEXT_DEFAULT_UNSELECTED_BACKGROUND wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)
61 #define wxRICHTEXT_DEFAULT_TYPE_COLOUR wxColour(0, 0, 200)
62 #define wxRICHTEXT_DEFAULT_FOCUS_RECT_COLOUR wxColour(100, 80, 80)
63 #define wxRICHTEXT_DEFAULT_CARET_WIDTH 2
64 // Minimum buffer size before delayed layout kicks in
65 #define wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD 20000
66 // Milliseconds before layout occurs after resize
67 #define wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL 50
68
69 /*!
70 * Forward declarations
71 */
72
73 /*!
74 * wxRichTextItem class declaration
75 */
76
77 // Drawing styles/states
78 #define wxRICHTEXT_SELECTED 0x01
79 #define wxRICHTEXT_TAGGED 0x02
80 // The control is focussed
81 #define wxRICHTEXT_FOCUSSED 0x04
82 // The item itself has the focus
83 #define wxRICHTEXT_IS_FOCUS 0x08
84
85 /*!
86 * wxRichTextCtrl class declaration
87 */
88
89 class WXDLLIMPEXP_RICHTEXT wxRichTextCtrl:
90 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
91 public wxTextCtrlBase, public wxScrollHelper
92 #else
93 public wxScrolledWindow
94 #endif
95 {
96 DECLARE_CLASS( wxRichTextCtrl )
97 DECLARE_EVENT_TABLE()
98
99 public:
100 // Constructors
101
102 wxRichTextCtrl( );
103 wxRichTextCtrl( wxWindow* parent, wxWindowID id = -1, const wxString& value = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
104 long style = wxRE_MULTILINE );
105 virtual ~wxRichTextCtrl( );
106
107 // Operations
108
109 /// Creation
110 bool Create( wxWindow* parent, wxWindowID id = -1, const wxString& value = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
111 long style = wxRE_MULTILINE );
112
113 /// Member initialisation
114 void Init();
115
116 ///// wxTextCtrl compatibility
117
118 // Accessors
119
120 virtual wxString GetValue() const;
121 virtual void SetValue(const wxString& value);
122
123 virtual wxString GetRange(long from, long to) const;
124
125 virtual int GetLineLength(long lineNo) const ;
126 virtual wxString GetLineText(long lineNo) const ;
127 virtual int GetNumberOfLines() const ;
128
129 virtual bool IsModified() const ;
130 virtual bool IsEditable() const ;
131
132 // more readable flag testing methods
133 bool IsSingleLine() const { return !HasFlag(wxRE_MULTILINE); }
134 bool IsMultiLine() const { return !IsSingleLine(); }
135
136 // If the return values from and to are the same, there is no selection.
137 virtual void GetSelection(long* from, long* to) const;
138
139 virtual wxString GetStringSelection() const;
140
141 /// Get filename
142 wxString GetFilename() const { return m_filename; }
143
144 /// Set filename
145 void SetFilename(const wxString& filename) { m_filename = filename; }
146
147 /// Set the threshold in character positions for doing layout optimization during sizing
148 void SetDelayedLayoutThreshold(long threshold) { m_delayedLayoutThreshold = threshold; }
149
150 /// Get the threshold in character positions for doing layout optimization during sizing
151 long GetDelayedLayoutThreshold() const { return m_delayedLayoutThreshold; }
152
153 // Operations
154
155 // editing
156 virtual void Clear();
157 virtual void Replace(long from, long to, const wxString& value);
158 virtual void Remove(long from, long to);
159
160 // load/save the controls contents from/to the file
161 virtual bool LoadFile(const wxString& file) { return LoadFile(file, wxRICHTEXT_TYPE_ANY); }
162 virtual bool LoadFile(const wxString& file, int type);
163 virtual bool SaveFile(const wxString& file = wxEmptyString) { return SaveFile(file, wxRICHTEXT_TYPE_ANY); }
164 virtual bool SaveFile(const wxString& file, int type);
165
166 // sets/clears the dirty flag
167 virtual void MarkDirty();
168 virtual void DiscardEdits();
169
170 // set the max number of characters which may be entered in a single line
171 // text control
172 virtual void SetMaxLength(unsigned long WXUNUSED(len)) { }
173
174 // writing text inserts it at the current position, appending always
175 // inserts it at the end
176 virtual void WriteText(const wxString& text);
177 virtual void AppendText(const wxString& text);
178
179 // text control under some platforms supports the text styles: these
180 // methods allow to apply the given text style to the given selection or to
181 // set/get the style which will be used for all appended text
182 virtual bool SetStyle(long start, long end, const wxTextAttr& style);
183 virtual bool SetStyle(long start, long end, const wxTextAttrEx& style);
184 virtual bool SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style);
185 virtual bool GetStyle(long position, wxTextAttr& style);
186 virtual bool GetStyle(long position, wxTextAttrEx& style);
187 virtual bool GetStyle(long position, wxRichTextAttr& style);
188 virtual bool SetDefaultStyle(const wxTextAttrEx& style);
189 virtual bool SetDefaultStyle(const wxTextAttr& style);
190
191 // TODO: change to GetDefaultStyle if we merge wxTextAttr and wxTextAttrEx
192 virtual const wxTextAttrEx& GetDefaultStyleEx() const;
193 virtual const wxTextAttr& GetDefaultStyle() const;
194
195 // translate between the position (which is just an index in the text ctrl
196 // considering all its contents as a single strings) and (x, y) coordinates
197 // which represent column and line.
198 virtual long XYToPosition(long x, long y) const;
199 virtual bool PositionToXY(long pos, long *x, long *y) const;
200
201 virtual void ShowPosition(long pos);
202
203 // find the character at position given in pixels
204 //
205 // NB: pt is in device coords (not adjusted for the client area origin nor
206 // scrolling)
207 virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, long *pos) const;
208 virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt,
209 wxTextCoord *col,
210 wxTextCoord *row) const;
211
212 // Clipboard operations
213 virtual void Copy();
214 virtual void Cut();
215 virtual void Paste();
216 virtual void DeleteSelection();
217
218 virtual bool CanCopy() const;
219 virtual bool CanCut() const;
220 virtual bool CanPaste() const;
221 virtual bool CanDeleteSelection() const;
222
223 // Undo/redo
224 virtual void Undo();
225 virtual void Redo();
226
227 virtual bool CanUndo() const;
228 virtual bool CanRedo() const;
229
230 // Insertion point
231 virtual void SetInsertionPoint(long pos);
232 virtual void SetInsertionPointEnd();
233 virtual long GetInsertionPoint() const;
234 virtual wxTextPos GetLastPosition() const;
235
236 virtual void SetSelection(long from, long to);
237 virtual void SelectAll();
238 virtual void SetEditable(bool editable);
239
240 /// Call Freeze to prevent refresh
241 virtual void Freeze();
242
243 /// Call Thaw to refresh
244 virtual void Thaw();
245
246 /// Call Thaw to refresh
247 virtual bool IsFrozen() const { return m_freezeCount > 0; }
248
249 virtual bool HasSelection() const;
250
251 ///// Functionality specific to wxRichTextCtrl
252
253 /// Write an image at the current insertion point. Supply optional type to use
254 /// for internal and file storage of the raw data.
255 virtual bool WriteImage(const wxImage& image, int bitmapType = wxBITMAP_TYPE_PNG);
256
257 /// Write a bitmap at the current insertion point. Supply optional type to use
258 /// for internal and file storage of the raw data.
259 virtual bool WriteImage(const wxBitmap& bitmap, int bitmapType = wxBITMAP_TYPE_PNG);
260
261 /// Load an image from file and write at the current insertion point.
262 virtual bool WriteImage(const wxString& filename, int bitmapType);
263
264 /// Write an image block at the current insertion point.
265 virtual bool WriteImage(const wxRichTextImageBlock& imageBlock);
266
267 /// Insert a newline (actually paragraph) at the current insertion point.
268 virtual bool Newline();
269
270 /// Set basic (overall) style
271 virtual void SetBasicStyle(const wxTextAttrEx& style) { GetBuffer().SetBasicStyle(style); }
272 virtual void SetBasicStyle(const wxRichTextAttr& style) { GetBuffer().SetBasicStyle(style); }
273
274 /// Get basic (overall) style
275 virtual const wxTextAttrEx& GetBasicStyle() const { return GetBuffer().GetBasicStyle(); }
276
277 /// Begin using a style
278 virtual bool BeginStyle(const wxTextAttrEx& style) { return GetBuffer().BeginStyle(style); }
279
280 /// End the style
281 virtual bool EndStyle() { return GetBuffer().EndStyle(); }
282
283 /// End all styles
284 virtual bool EndAllStyles() { return GetBuffer().EndAllStyles(); }
285
286 /// Begin using bold
287 bool BeginBold() { return GetBuffer().BeginBold(); }
288
289 /// End using bold
290 bool EndBold() { return GetBuffer().EndBold(); }
291
292 /// Begin using italic
293 bool BeginItalic() { return GetBuffer().BeginItalic(); }
294
295 /// End using italic
296 bool EndItalic() { return GetBuffer().EndItalic(); }
297
298 /// Begin using underline
299 bool BeginUnderline() { return GetBuffer().BeginUnderline(); }
300
301 /// End using underline
302 bool EndUnderline() { return GetBuffer().EndUnderline(); }
303
304 /// Begin using point size
305 bool BeginFontSize(int pointSize) { return GetBuffer().BeginFontSize(pointSize); }
306
307 /// End using point size
308 bool EndFontSize() { return GetBuffer().EndFontSize(); }
309
310 /// Begin using this font
311 bool BeginFont(const wxFont& font) { return GetBuffer().BeginFont(font); }
312
313 /// End using a font
314 bool EndFont() { return GetBuffer().EndFont(); }
315
316 /// Begin using this colour
317 bool BeginTextColour(const wxColour& colour) { return GetBuffer().BeginTextColour(colour); }
318
319 /// End using a colour
320 bool EndTextColour() { return GetBuffer().EndTextColour(); }
321
322 /// Begin using alignment
323 bool BeginAlignment(wxTextAttrAlignment alignment) { return GetBuffer().BeginAlignment(alignment); }
324
325 /// End alignment
326 bool EndAlignment() { return GetBuffer().EndAlignment(); }
327
328 /// Begin left indent
329 bool BeginLeftIndent(int leftIndent, int leftSubIndent = 0) { return GetBuffer().BeginLeftIndent(leftIndent, leftSubIndent); }
330
331 /// End left indent
332 bool EndLeftIndent() { return GetBuffer().EndLeftIndent(); }
333
334 /// Begin right indent
335 bool BeginRightIndent(int rightIndent) { return GetBuffer().BeginRightIndent(rightIndent); }
336
337 /// End right indent
338 bool EndRightIndent() { return GetBuffer().EndRightIndent(); }
339
340 /// Begin paragraph spacing
341 bool BeginParagraphSpacing(int before, int after) { return GetBuffer().BeginParagraphSpacing(before, after); }
342
343 /// End paragraph spacing
344 bool EndParagraphSpacing() { return GetBuffer().EndParagraphSpacing(); }
345
346 /// Begin line spacing
347 bool BeginLineSpacing(int lineSpacing) { return GetBuffer().BeginLineSpacing(lineSpacing); }
348
349 /// End line spacing
350 bool EndLineSpacing() { return GetBuffer().EndLineSpacing(); }
351
352 /// Begin numbered bullet
353 bool BeginNumberedBullet(int bulletNumber, int leftIndent, int leftSubIndent, int bulletStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD)
354 { return GetBuffer().BeginNumberedBullet(bulletNumber, leftIndent, leftSubIndent, bulletStyle); }
355
356 /// End numbered bullet
357 bool EndNumberedBullet() { return GetBuffer().EndNumberedBullet(); }
358
359 /// Begin symbol bullet
360 bool BeginSymbolBullet(wxChar symbol, int leftIndent, int leftSubIndent, int bulletStyle = wxTEXT_ATTR_BULLET_STYLE_SYMBOL)
361 { return GetBuffer().BeginSymbolBullet(symbol, leftIndent, leftSubIndent, bulletStyle); }
362
363 /// End symbol bullet
364 bool EndSymbolBullet() { return GetBuffer().EndSymbolBullet(); }
365
366 /// Begin named character style
367 bool BeginCharacterStyle(const wxString& characterStyle) { return GetBuffer().BeginCharacterStyle(characterStyle); }
368
369 /// End named character style
370 bool EndCharacterStyle() { return GetBuffer().EndCharacterStyle(); }
371
372 /// Begin named paragraph style
373 bool BeginParagraphStyle(const wxString& paragraphStyle) { return GetBuffer().BeginParagraphStyle(paragraphStyle); }
374
375 /// End named character style
376 bool EndParagraphStyle() { return GetBuffer().EndParagraphStyle(); }
377
378 /// Sets the default style to the style under the cursor
379 bool SetDefaultStyleToCursorStyle();
380
381 /// Clear the selection
382 virtual void SelectNone();
383
384 /// Get/set the selection range in character positions. -1, -1 means no selection.
385 /// The range is in API convention, i.e. a single character selection is denoted
386 /// by (n, n+1)
387 wxRichTextRange GetSelectionRange() const;
388 void SetSelectionRange(const wxRichTextRange& range);
389
390 /// Get/set the selection range in character positions. -1, -1 means no selection.
391 /// The range is in internal format, i.e. a single character selection is denoted
392 /// by (n, n)
393 const wxRichTextRange& GetInternalSelectionRange() const { return m_selectionRange; }
394 void SetInternalSelectionRange(const wxRichTextRange& range) { m_selectionRange = range; }
395
396 /// Add a new paragraph of text to the end of the buffer
397 virtual wxRichTextRange AddParagraph(const wxString& text);
398
399 /// Add an image
400 virtual wxRichTextRange AddImage(const wxImage& image);
401
402 /// Layout the buffer: which we must do before certain operations, such as
403 /// setting the caret position.
404 virtual bool LayoutContent(bool onlyVisibleRect = false);
405
406 /// Move the caret to the given character position
407 virtual bool MoveCaret(long pos, bool showAtLineStart = false);
408
409 /// Move right
410 virtual bool MoveRight(int noPositions = 1, int flags = 0);
411
412 /// Move left
413 virtual bool MoveLeft(int noPositions = 1, int flags = 0);
414
415 /// Move up
416 virtual bool MoveUp(int noLines = 1, int flags = 0);
417
418 /// Move up
419 virtual bool MoveDown(int noLines = 1, int flags = 0);
420
421 /// Move to the end of the line
422 virtual bool MoveToLineEnd(int flags = 0);
423
424 /// Move to the start of the line
425 virtual bool MoveToLineStart(int flags = 0);
426
427 /// Move to the end of the paragraph
428 virtual bool MoveToParagraphEnd(int flags = 0);
429
430 /// Move to the start of the paragraph
431 virtual bool MoveToParagraphStart(int flags = 0);
432
433 /// Move to the start of the buffer
434 virtual bool MoveHome(int flags = 0);
435
436 /// Move to the end of the buffer
437 virtual bool MoveEnd(int flags = 0);
438
439 /// Move n pages up
440 virtual bool PageUp(int noPages = 1, int flags = 0);
441
442 /// Move n pages down
443 virtual bool PageDown(int noPages = 1, int flags = 0);
444
445 /// Move n words left
446 virtual bool WordLeft(int noPages = 1, int flags = 0);
447
448 /// Move n words right
449 virtual bool WordRight(int noPages = 1, int flags = 0);
450
451 /// Returns the buffer associated with the control.
452 wxRichTextBuffer& GetBuffer() { return m_buffer; }
453 const wxRichTextBuffer& GetBuffer() const { return m_buffer; }
454
455 /// Start batching undo history for commands.
456 virtual bool BeginBatchUndo(const wxString& cmdName) { return m_buffer.BeginBatchUndo(cmdName); }
457
458 /// End batching undo history for commands.
459 virtual bool EndBatchUndo() { return m_buffer.EndBatchUndo(); }
460
461 /// Are we batching undo history for commands?
462 virtual bool BatchingUndo() const { return m_buffer.BatchingUndo(); }
463
464 /// Start suppressing undo history for commands.
465 virtual bool BeginSuppressUndo() { return m_buffer.BeginSuppressUndo(); }
466
467 /// End suppressing undo history for commands.
468 virtual bool EndSuppressUndo() { return m_buffer.EndSuppressUndo(); }
469
470 /// Are we suppressing undo history for commands?
471 virtual bool SuppressingUndo() const { return m_buffer.SuppressingUndo(); }
472
473 /// Test if this whole range has character attributes of the specified kind. If any
474 /// of the attributes are different within the range, the test fails. You
475 /// can use this to implement, for example, bold button updating. style must have
476 /// flags indicating which attributes are of interest.
477 virtual bool HasCharacterAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const
478 {
479 return GetBuffer().HasCharacterAttributes(range, style);
480 }
481 virtual bool HasCharacterAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const
482 {
483 return GetBuffer().HasCharacterAttributes(range, style);
484 }
485
486 /// Test if this whole range has paragraph attributes of the specified kind. If any
487 /// of the attributes are different within the range, the test fails. You
488 /// can use this to implement, for example, centering button updating. style must have
489 /// flags indicating which attributes are of interest.
490 virtual bool HasParagraphAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const
491 {
492 return GetBuffer().HasParagraphAttributes(range, style);
493 }
494 virtual bool HasParagraphAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const
495 {
496 return GetBuffer().HasParagraphAttributes(range, style);
497 }
498
499 /// Is all of the selection bold?
500 virtual bool IsSelectionBold();
501
502 /// Is all of the selection italics?
503 virtual bool IsSelectionItalics();
504
505 /// Is all of the selection underlined?
506 virtual bool IsSelectionUnderlined();
507
508 /// Is all of the selection aligned according to the specified flag?
509 virtual bool IsSelectionAligned(wxTextAttrAlignment alignment);
510
511 /// Apply bold to the selection
512 virtual bool ApplyBoldToSelection();
513
514 /// Apply italic to the selection
515 virtual bool ApplyItalicToSelection();
516
517 /// Apply underline to the selection
518 virtual bool ApplyUnderlineToSelection();
519
520 /// Apply alignment to the selection
521 virtual bool ApplyAlignmentToSelection(wxTextAttrAlignment alignment);
522
523 /// Apply a named style to the selection
524 virtual void ApplyStyle(wxRichTextStyleDefinition* def);
525
526 /// Set style sheet, if any.
527 void SetStyleSheet(wxRichTextStyleSheet* styleSheet) { GetBuffer().SetStyleSheet(styleSheet); }
528 wxRichTextStyleSheet* GetStyleSheet() const { return GetBuffer().GetStyleSheet(); }
529
530 // Command handlers
531
532 void Command(wxCommandEvent& event);
533 void OnDropFiles(wxDropFilesEvent& event);
534
535 void OnCut(wxCommandEvent& event);
536 void OnCopy(wxCommandEvent& event);
537 void OnPaste(wxCommandEvent& event);
538 void OnUndo(wxCommandEvent& event);
539 void OnRedo(wxCommandEvent& event);
540 void OnSelectAll(wxCommandEvent& event);
541 void OnClear(wxCommandEvent& event);
542
543 void OnUpdateCut(wxUpdateUIEvent& event);
544 void OnUpdateCopy(wxUpdateUIEvent& event);
545 void OnUpdatePaste(wxUpdateUIEvent& event);
546 void OnUpdateUndo(wxUpdateUIEvent& event);
547 void OnUpdateRedo(wxUpdateUIEvent& event);
548 void OnUpdateSelectAll(wxUpdateUIEvent& event);
549 void OnUpdateClear(wxUpdateUIEvent& event);
550
551 // Show a context menu for Rich Edit controls (the standard
552 // EDIT control has one already)
553 void OnContextMenu(wxContextMenuEvent& event);
554
555 // Event handlers
556
557 /// Painting
558 void OnPaint(wxPaintEvent& event);
559 void OnEraseBackground(wxEraseEvent& event);
560
561 /// Left-click
562 void OnLeftClick(wxMouseEvent& event);
563
564 /// Left-up
565 void OnLeftUp(wxMouseEvent& event);
566
567 /// Motion
568 void OnMoveMouse(wxMouseEvent& event);
569
570 /// Left-double-click
571 void OnLeftDClick(wxMouseEvent& event);
572
573 /// Middle-click
574 void OnMiddleClick(wxMouseEvent& event);
575
576 /// Right-click
577 void OnRightClick(wxMouseEvent& event);
578
579 /// Key press
580 void OnChar(wxKeyEvent& event);
581
582 /// Sizing
583 void OnSize(wxSizeEvent& event);
584
585 /// Setting/losing focus
586 void OnSetFocus(wxFocusEvent& event);
587 void OnKillFocus(wxFocusEvent& event);
588
589 /// Idle-time processing
590 void OnIdle(wxIdleEvent& event);
591
592 /// Scrolling
593 void OnScroll(wxScrollWinEvent& event);
594
595 // Implementation
596
597 /// Set font, and also default attributes
598 virtual bool SetFont(const wxFont& font);
599
600 /// Set up scrollbars, e.g. after a resize
601 virtual void SetupScrollbars(bool atTop = false);
602
603 /// Keyboard navigation
604 virtual bool KeyboardNavigate(int keyCode, int flags);
605
606 /// Paint the background
607 virtual void PaintBackground(wxDC& dc);
608
609 /// Recreate buffer bitmap if necessary
610 virtual bool RecreateBuffer(const wxSize& size = wxDefaultSize);
611
612 /// Set the selection
613 virtual void DoSetSelection(long from, long to, bool scrollCaret = true);
614
615 /// Write text
616 virtual void DoWriteText(const wxString& value, bool selectionOnly = true);
617
618 /// Send an update event
619 virtual bool SendUpdateEvent();
620
621 /// Init command event
622 void InitCommandEvent(wxCommandEvent& event) const;
623
624 /// do the window-specific processing after processing the update event
625 // (duplicated code from wxTextCtrlBase)
626 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
627 virtual void DoUpdateWindowUI(wxUpdateUIEvent& event);
628 #endif
629
630 /// Should we inherit colours?
631 virtual bool ShouldInheritColours() const { return false; }
632
633 /// Position the caret
634 virtual void PositionCaret();
635
636 /// Extend the selection, returning true if the selection was
637 /// changed. Selections are in caret positions.
638 virtual bool ExtendSelection(long oldPosition, long newPosition, int flags);
639
640 /// Scroll into view. This takes a _caret_ position.
641 virtual bool ScrollIntoView(long position, int keyCode);
642
643 /// The caret position is the character position just before the caret.
644 /// A value of -1 means the caret is at the start of the buffer.
645 void SetCaretPosition(long position, bool showAtLineStart = false) ;
646 long GetCaretPosition() const { return m_caretPosition; }
647
648 /// The adjusted caret position is the character position adjusted to take
649 /// into account whether we're at the start of a paragraph, in which case
650 /// style information should be taken from the next position, not current one.
651 long GetAdjustedCaretPosition(long caretPos) const;
652
653 /// Move caret one visual step forward: this may mean setting a flag
654 /// and keeping the same position if we're going from the end of one line
655 /// to the start of the next, which may be the exact same caret position.
656 void MoveCaretForward(long oldPosition) ;
657
658 /// Move caret one visual step forward: this may mean setting a flag
659 /// and keeping the same position if we're going from the end of one line
660 /// to the start of the next, which may be the exact same caret position.
661 void MoveCaretBack(long oldPosition) ;
662
663 /// Get the caret height and position for the given character position
664 bool GetCaretPositionForIndex(long position, wxRect& rect);
665
666 /// Gets the line for the visible caret position. If the caret is
667 /// shown at the very end of the line, it means the next character is actually
668 /// on the following line. So let's get the line we're expecting to find
669 /// if this is the case.
670 wxRichTextLine* GetVisibleLineForCaretPosition(long caretPosition) const;
671
672 /// Gets the command processor
673 wxCommandProcessor* GetCommandProcessor() const { return GetBuffer().GetCommandProcessor(); }
674
675 /// Delete content if there is a selection, e.g. when pressing a key.
676 /// Returns the new caret position in newPos, or leaves it if there
677 /// was no action.
678 bool DeleteSelectedContent(long* newPos= NULL);
679
680 /// Transform logical to physical
681 wxPoint GetPhysicalPoint(const wxPoint& ptLogical) const;
682
683 /// Transform physical to logical
684 wxPoint GetLogicalPoint(const wxPoint& ptPhysical) const;
685
686 /// Finds the caret position for the next word. Direction
687 /// is 1 (forward) or -1 (backwards).
688 virtual long FindNextWordPosition(int direction = 1) const;
689
690 /// Is the given position visible on the screen?
691 bool IsPositionVisible(long pos) const;
692
693 /// Returns the first visible position in the current view
694 long GetFirstVisiblePosition() const;
695
696 /// Returns the caret position since the default formatting was changed. As
697 /// soon as this position changes, we no longer reflect the default style
698 /// in the UI. A value of -2 means that we should only reflect the style of the
699 /// content under the caret.
700 long GetCaretPositionForDefaultStyle() const { return m_caretPositionForDefaultStyle; }
701
702 /// Set the caret position for the default style that the user is selecting.
703 void SetCaretPositionForDefaultStyle(long pos) { m_caretPositionForDefaultStyle = pos; }
704
705 /// Should the UI reflect the default style chosen by the user, rather than the style under
706 /// the caret?
707 bool IsDefaultStyleShowing() const { return m_caretPositionForDefaultStyle != -2; }
708
709 /// Convenience function that tells the control to start reflecting the default
710 /// style, since the user is changing it.
711 void SetAndShowDefaultStyle(const wxRichTextAttr& attr)
712 {
713 SetDefaultStyle(attr);
714 SetCaretPositionForDefaultStyle(GetCaretPosition());
715 }
716
717 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
718 WX_FORWARD_TO_SCROLL_HELPER()
719 #endif
720
721 // Overrides
722 protected:
723
724 virtual wxSize DoGetBestSize() const ;
725
726 // Data members
727 private:
728
729 /// Allows nested Freeze/Thaw
730 int m_freezeCount;
731
732 /// Buffer bitmap
733 wxBitmap m_bufferBitmap;
734
735 /// Text buffer
736 wxRichTextBuffer m_buffer;
737
738 /// Filename
739 wxString m_filename;
740
741 wxMenu* m_contextMenu;
742
743 /// Caret position (1 less than the character position, so -1 is the
744 /// first caret position).
745 long m_caretPosition;
746
747 /// Caret position when the default formatting has been changed. As
748 /// soon as this position changes, we no longer reflect the default style
749 /// in the UI.
750 long m_caretPositionForDefaultStyle;
751
752 /// Selection range in character positions. -2, -2 means no selection.
753 wxRichTextRange m_selectionRange;
754
755 /// Anchor so we know how to extend the selection
756 /// It's a caret position since it's between two characters.
757 long m_selectionAnchor;
758
759 /// Are we editable?
760 bool m_editable;
761
762 /// Are we showing the caret position at the start of a line
763 /// instead of at the end of the previous one?
764 bool m_caretAtLineStart;
765
766 /// Are we dragging a selection?
767 bool m_dragging;
768
769 /// Start position for drag
770 wxPoint m_dragStart;
771
772 /// Do we need full layout in idle?
773 bool m_fullLayoutRequired;
774 wxLongLong m_fullLayoutTime;
775 long m_fullLayoutSavedPosition;
776
777 /// Threshold for doing delayed layout
778 long m_delayedLayoutThreshold;
779 };
780
781 /*!
782 * wxRichTextEvent - the event class for wxRichTextCtrl notifications
783 */
784
785 class WXDLLIMPEXP_RICHTEXT wxRichTextEvent : public wxNotifyEvent
786 {
787 public:
788 wxRichTextEvent(wxEventType commandType = wxEVT_NULL, int winid = 0)
789 : wxNotifyEvent(commandType, winid),
790 m_itemIndex(-1), m_flags(0)
791 { }
792
793 wxRichTextEvent(const wxRichTextEvent& event)
794 : wxNotifyEvent(event),
795 m_itemIndex(event.m_itemIndex), m_flags(event.m_flags)
796 { }
797
798 int GetIndex() const { return m_itemIndex; }
799 void SetIndex(int n) { m_itemIndex = n; }
800
801 int GetFlags() const { return m_flags; }
802 void SetFlags(int flags) { m_flags = flags; }
803
804 virtual wxEvent *Clone() const { return new wxRichTextEvent(*this); }
805
806 protected:
807 int m_itemIndex;
808 int m_flags;
809
810 private:
811 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxRichTextEvent)
812 };
813
814 /*!
815 * wxRichTextCtrl event macros
816 */
817
818 BEGIN_DECLARE_EVENT_TYPES()
819 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_RICHTEXT, wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED, 2600)
820 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_RICHTEXT, wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED, 2601)
821 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_RICHTEXT, wxEVT_COMMAND_RICHTEXT_LEFT_CLICK, 2602)
822 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_RICHTEXT, wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK, 2603)
823 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_RICHTEXT, wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK, 2604)
824 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_RICHTEXT, wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK, 2605)
825 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_RICHTEXT, wxEVT_COMMAND_RICHTEXT_RETURN, 2606)
826 END_DECLARE_EVENT_TYPES()
827
828 typedef void (wxEvtHandler::*wxRichTextEventFunction)(wxRichTextEvent&);
829
830 #define EVT_RICHTEXT_ITEM_SELECTED(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED, id, -1, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
831 #define EVT_RICHTEXT_ITEM_DESELECTED(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED, id, -1, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
832 #define EVT_RICHTEXT_LEFT_CLICK(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK, id, -1, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
833 #define EVT_RICHTEXT_RIGHT_CLICK(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK, id, -1, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
834 #define EVT_RICHTEXT_MIDDLE_CLICK(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK, id, -1, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
835 #define EVT_RICHTEXT_LEFT_DCLICK(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK, id, -1, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
836 #define EVT_RICHTEXT_RETURN(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_RETURN, id, -1, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
837
838 #endif
839 // wxUSE_RICHTEXT
840
841 #endif
842 // _WX_RICHTEXTCTRL_H_
843