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