1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtext/richtextbuffer.h
3 // Purpose: documentation for wxRichTextBuffer class
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
10 @class wxRichTextBuffer
11 @headerfile richtextbuffer.h wx/richtext/richtextbuffer.h
13 This class represents the whole buffer associated with a wxRichTextCtrl.
19 wxTextAttr, wxRichTextCtrl
21 class wxRichTextBuffer
28 wxRichTextBuffer(const wxRichTextBuffer
& obj
);
38 Adds an event handler to the buffer's list of handlers. A buffer associated with
39 a contol has the control as the only event handler, but the application is free
40 to add more if further notification is required. All handlers are notified
41 of an event originating from the buffer, such as the replacement of a style
43 during loading. The buffer never deletes any of the event handlers, unless
44 RemoveEventHandler() is
45 called with @true as the second argument.
47 bool AddEventHandler(wxEvtHandler
* handler
);
52 void AddHandler(wxRichTextFileHandler
* handler
);
55 Adds a paragraph of text.
57 wxRichTextRange
AddParagraph(const wxString
& text
);
60 Returns @true if the buffer is currently collapsing commands into a single
66 Begins using alignment.
68 bool BeginAlignment(wxTextAttrAlignment alignment
);
71 Begins collapsing undo/redo commands. Note that this may not work properly
72 if combining commands that delete or insert content, changing ranges for
75 @e cmdName should be the name of the combined command that will appear
76 next to Undo and Redo in the edit menu.
78 bool BeginBatchUndo(const wxString
& cmdName
);
86 Begins applying the named character style.
88 bool BeginCharacterStyle(const wxString
& characterStyle
);
91 Begins using this font.
93 bool BeginFont(const wxFont
& font
);
96 Begins using the given point size.
98 bool BeginFontSize(int pointSize
);
106 Begin using @e leftIndent for the left indent, and optionally @e leftSubIndent
108 the sub-indent. Both are expressed in tenths of a millimetre.
110 The sub-indent is an offset from the left of the paragraph, and is used for all
112 first line in a paragraph. A positive value will cause the first line to appear
114 of the subsequent lines, and a negative value will cause the first line to be
116 relative to the subsequent lines.
118 bool BeginLeftIndent(int leftIndent
, int leftSubIndent
= 0);
121 Begins line spacing using the specified value. @e spacing is a multiple, where
122 10 means single-spacing,
123 15 means 1.5 spacing, and 20 means double spacing. The following constants are
124 defined for convenience:
126 bool BeginLineSpacing(int lineSpacing
);
129 Begins using a specified list style. Optionally, you can also pass a level and
132 bool BeginListStyle(const wxString
& listStyle
, int level
=1,
136 Begins a numbered bullet. This call will be needed for each item in the list,
138 application should take care of incrementing the numbering.
140 @e bulletNumber is a number, usually starting with 1.
142 @e leftIndent and @e leftSubIndent are values in tenths of a millimetre.
144 @e bulletStyle is a bitlist of the following values:
147 wxRichTextBuffer uses indentation to render a bulleted item. The left indent is
149 the margin and the bullet. The content of the paragraph, including the first
151 at leftMargin + leftSubIndent. So the distance between the left edge of the
153 left of the actual paragraph is leftSubIndent.
155 bool BeginNumberedBullet(int bulletNumber
, int leftIndent
,
157 int bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
);
160 Begins paragraph spacing; pass the before-paragraph and after-paragraph spacing
164 bool BeginParagraphSpacing(int before
, int after
);
167 Begins applying the named paragraph style.
169 bool BeginParagraphStyle(const wxString
& paragraphStyle
);
172 Begins a right indent, specified in tenths of a millimetre.
174 bool BeginRightIndent(int rightIndent
);
177 Begins applying a standard bullet, using one of the standard bullet names
178 (currently @c standard/circle or @c standard/square.
179 See BeginNumberedBullet() for an explanation of how indentation is used to
180 render the bulleted paragraph.
182 bool BeginStandardBullet(const wxString
& bulletName
,
185 int bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_STANDARD
);
188 Begins using a specified style.
190 bool BeginStyle(const wxTextAttr
& style
);
193 Begins suppressing undo/redo commands. The way undo is suppressed may be
195 differently by each command. If not dealt with by a command implementation, then
196 it will be implemented automatically by not storing the command in the undo
198 when the action is submitted to the command processor.
200 bool BeginSuppressUndo();
203 Begins applying a symbol bullet, using a character from the current font. See
204 BeginNumberedBullet() for
205 an explanation of how indentation is used to render the bulleted paragraph.
207 bool BeginSymbolBullet(wxChar symbol
, int leftIndent
,
209 int bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_SYMBOL
);
212 Begins using the specified text foreground colour.
214 bool BeginTextColour(const wxColour
& colour
);
217 Begins applying wxTEXT_ATTR_URL to the content. Pass a URL and optionally, a
218 character style to apply,
219 since it is common to mark a URL with a familiar style such as blue text with
222 bool BeginURL(const wxString
& url
,
223 const wxString
& characterStyle
= wxEmptyString
);
226 Begins using underline.
228 bool BeginUnderline();
231 Returns @true if content can be pasted from the clipboard.
233 bool CanPasteFromClipboard();
236 Cleans up the file handlers.
238 void CleanUpHandlers();
247 Clears the list style from the given range, clearing list-related attributes
248 and applying any named paragraph style associated with each paragraph.
250 @e flags is a bit list of the following:
252 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
254 See also SetListStyle(), PromoteList(), NumberList().
256 bool ClearListStyle(const wxRichTextRange
& range
,
257 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
);
258 bool ClearListStyle(const wxRichTextRange
& range
,
259 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
);
263 Clears the style stack.
265 void ClearStyleStack();
270 wxRichTextObject
* Clone();
273 Copies the given buffer.
275 void Copy(const wxRichTextBuffer
& obj
);
278 Copy the given range to the clipboard.
280 bool CopyToClipboard(const wxRichTextRange
& range
);
283 Submits a command to delete the given range.
285 bool DeleteRangeWithUndo(const wxRichTextRange
& range
,
286 wxRichTextCtrl
* ctrl
);
290 Dumps the contents of the buffer for debugging purposes.
293 void Dump(wxTextOutputStream
& stream
);
302 Ends all styles that have been started with a Begin... command.
307 Ends collapsing undo/redo commands, and submits the combined command.
317 Ends using the named character style.
319 bool EndCharacterStyle();
327 Ends using a point size.
337 Ends using a left indent.
339 bool EndLeftIndent();
342 Ends using a line spacing.
344 bool EndLineSpacing();
347 Ends using a specified list style.
352 Ends a numbered bullet.
354 bool EndNumberedBullet();
357 Ends paragraph spacing.
359 bool EndParagraphSpacing();
362 Ends applying a named character style.
364 bool EndParagraphStyle();
367 Ends using a right indent.
369 bool EndRightIndent();
372 Ends using a standard bullet.
374 bool EndStandardBullet();
377 Ends the current style.
382 Ends suppressing undo/redo commands.
384 bool EndSuppressUndo();
387 Ends using a symbol bullet.
389 bool EndSymbolBullet();
392 Ends using a text foreground colour.
394 bool EndTextColour();
399 #define bool EndURL() /* implementation is private */
402 Ends using underline.
408 Finds a handler by name.
410 wxRichTextFileHandler
* FindHandler(int imageType
);
411 wxRichTextFileHandler
* FindHandler(const wxString
& extension
,
413 wxRichTextFileHandler
* FindHandler(const wxString
& name
);
417 Finds a handler by filename or, if supplied, type.
419 wxRichTextFileHandler
* FindHandlerFilenameOrType(const wxString
& filename
,
423 Gets the basic (overall) style. This is the style of the whole
424 buffer before further styles are applied, unlike the default style, which
425 only affects the style currently being applied (for example, setting the default
426 style to bold will cause subsequently inserted text to be bold).
428 const wxTextAttr
GetBasicStyle();
431 Gets the collapsed command.
433 wxRichTextCommand
* GetBatchedCommand();
436 Gets the command processor. A text buffer always creates its own command
440 wxCommandProcessor
* GetCommandProcessor();
443 Returns the current default style, affecting the style currently being applied
444 (for example, setting the default
445 style to bold will cause subsequently inserted text to be bold).
447 const wxTextAttr
GetDefaultStyle();
450 Gets a wildcard incorporating all visible handlers. If @e types is present,
451 it will be filled with the file type corresponding to each filter. This can be
452 used to determine the type to pass to @ref getextwildcard() LoadFile given a
455 wxString
GetExtWildcard(bool combine
= @
false, bool save
= @
false,
456 wxArrayInt
* types
= @NULL
);
459 Returns the list of file handlers.
461 wxList
GetHandlers();
464 Returns the object to be used to render certain aspects of the content, such as
467 static wxRichTextRenderer
* GetRenderer();
470 Gets the attributes at the given position.
472 This function gets the combined style - that is, the style you see on the
474 of combining base style, paragraph style and character style attributes. To get
476 or paragraph style alone, use GetUncombinedStyle().
478 bool GetStyle(long position
, wxTextAttr
& style
);
481 This function gets a style representing the common, combined attributes in the
483 Attributes which have different values within the specified range will not be
487 The function is used to get the attributes to display in the formatting dialog:
489 can edit the attributes common to the selection, and optionally specify the
491 attributes to be applied uniformly.
493 To apply the edited attributes, you can use SetStyle() specifying
494 the wxRICHTEXT_SETSTYLE_OPTIMIZE flag, which will only apply attributes that
496 from the @e combined attributes within the range. So, the user edits the
497 effective, displayed attributes
498 for the range, but his choice won't be applied unnecessarily to content. As an
500 say the style for a paragraph specifies bold, but the paragraph text doesn't
501 specify a weight. The
502 combined style is bold, and this is what the user will see on-screen and in the
504 dialog. The user now specifies red text, in addition to bold. When applying with
505 SetStyle, the content font weight attributes won't be changed to bold because
506 this is already specified
507 by the paragraph. However the text colour attributes @e will be changed to
510 bool GetStyleForRange(const wxRichTextRange
& range
,
514 Returns the current style sheet associated with the buffer, if any.
516 wxRichTextStyleSheet
* GetStyleSheet();
519 Get the size of the style stack, for example to check correct nesting.
521 size_t GetStyleStackSize();
524 Gets the attributes at the given position.
526 This function gets the @e uncombined style - that is, the attributes associated
528 paragraph or character content, and not necessarily the combined attributes you
530 screen. To get the combined attributes, use GetStyle().
532 If you specify (any) paragraph attribute in @e style's flags, this function
534 the paragraph attributes. Otherwise, it will return the character attributes.
536 bool GetUncombinedStyle(long position
, wxTextAttr
& style
);
539 Finds the text position for the given position, putting the position in @e
541 one is found. @e pt is in logical units (a zero y position is
542 at the beginning of the buffer).
544 The function returns one of the following values:
546 int HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
);
554 Initialises the standard handlers. Currently, only the plain text
555 loading/saving handler
556 is initialised by default.
558 void InitStandardHandlers();
561 Inserts a handler at the front of the list.
563 void InsertHandler(wxRichTextFileHandler
* handler
);
566 Submits a command to insert the given image.
568 bool InsertImageWithUndo(long pos
,
569 const wxRichTextImageBlock
& imageBlock
,
570 wxRichTextCtrl
* ctrl
);
573 Submits a command to insert a newline.
575 bool InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
);
578 Submits a command to insert the given text.
580 bool InsertTextWithUndo(long pos
, const wxString
& text
,
581 wxRichTextCtrl
* ctrl
);
584 Returns @true if the buffer has been modified.
590 Loads content from a file.
592 bool LoadFile(wxInputStream
& stream
,
593 int type
= wxRICHTEXT_TYPE_ANY
);
594 bool LoadFile(const wxString
& filename
,
595 int type
= wxRICHTEXT_TYPE_ANY
);
599 Marks the buffer as modified or unmodified.
601 void Modify(bool modify
= @
true);
605 Numbers the paragraphs in the given range. Pass flags to determine how the
607 Either the style definition or the name of the style definition (in the current
608 sheet) can be passed.
610 @e flags is a bit list of the following:
612 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
613 wxRICHTEXT_SETSTYLE_RENUMBER: specifies that numbering should start from @e
614 startFrom, otherwise existing attributes are used.
615 wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL: specifies that @e listLevel should be used
616 as the level for all paragraphs, otherwise the current indentation will be used.
618 See also SetListStyle(), PromoteList(), ClearListStyle().
620 bool NumberList(const wxRichTextRange
& range
,
621 const wxRichTextListStyleDefinition
* style
,
622 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
625 bool Number(const wxRichTextRange
& range
,
626 const wxString
& styleName
,
627 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
633 Pastes the clipboard content to the buffer at the given position.
635 bool PasteFromClipboard(long position
);
639 Promotes or demotes the paragraphs in the given range. A positive @e promoteBy
640 produces a smaller indent, and a negative number
641 produces a larger indent. Pass flags to determine how the attributes are set.
642 Either the style definition or the name of the style definition (in the current
643 sheet) can be passed.
645 @e flags is a bit list of the following:
647 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
648 wxRICHTEXT_SETSTYLE_RENUMBER: specifies that numbering should start from @e
649 startFrom, otherwise existing attributes are used.
650 wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL: specifies that @e listLevel should be used
651 as the level for all paragraphs, otherwise the current indentation will be used.
653 See also SetListStyle(), See also SetListStyle(), ClearListStyle().
655 bool PromoteList(int promoteBy
, const wxRichTextRange
& range
,
656 const wxRichTextListStyleDefinition
* style
,
657 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
659 bool PromoteList(int promoteBy
, const wxRichTextRange
& range
,
660 const wxString
& styleName
,
661 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
666 Removes an event handler from the buffer's list of handlers, deleting the
667 object if @e deleteHandler is @true.
669 bool RemoveEventHandler(wxEvtHandler
* handler
,
670 bool deleteHandler
= @
false);
675 bool RemoveHandler(const wxString
& name
);
678 Clears the buffer, adds a new blank paragraph, and clears the command history.
680 void ResetAndClearCommands();
684 Saves content to a file.
686 bool SaveFile(wxOutputStream
& stream
,
687 int type
= wxRICHTEXT_TYPE_ANY
);
688 bool SaveFile(const wxString
& filename
,
689 int type
= wxRICHTEXT_TYPE_ANY
);
693 Sets the basic (overall) style. This is the style of the whole
694 buffer before further styles are applied, unlike the default style, which
695 only affects the style currently being applied (for example, setting the default
696 style to bold will cause subsequently inserted text to be bold).
698 void SetBasicStyle(const wxTextAttr
& style
);
701 Sets the default style, affecting the style currently being applied (for
702 example, setting the default
703 style to bold will cause subsequently inserted text to be bold).
705 This is not cumulative - setting the default style will replace the previous
708 void SetDefaultStyle(const wxTextAttr
& style
);
712 Sets the list attributes for the given range, passing flags to determine how
713 the attributes are set.
714 Either the style definition or the name of the style definition (in the current
715 sheet) can be passed.
717 @e flags is a bit list of the following:
719 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
720 wxRICHTEXT_SETSTYLE_RENUMBER: specifies that numbering should start from @e
721 startFrom, otherwise existing attributes are used.
722 wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL: specifies that @e listLevel should be used
723 as the level for all paragraphs, otherwise the current indentation will be used.
725 See also NumberList(), PromoteList(), ClearListStyle().
727 bool SetListStyle(const wxRichTextRange
& range
,
728 const wxRichTextListStyleDefinition
* style
,
729 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
732 bool SetListStyle(const wxRichTextRange
& range
,
733 const wxString
& styleName
,
734 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
740 Sets @e renderer as the object to be used to render certain aspects of the
741 content, such as bullets.
742 You can override default rendering by deriving a new class from
743 wxRichTextRenderer or wxRichTextStdRenderer,
744 overriding one or more virtual functions, and setting an instance of the class
747 static void SetRenderer(wxRichTextRenderer
* renderer
);
750 Sets the attributes for the given range. Pass flags to determine how the
753 The end point of range is specified as the last character position of the span
755 So, for example, to set the style for a character at position 5, use the range
757 This differs from the wxRichTextCtrl API, where you would specify (5,6).
759 @e flags may contain a bit list of the following values:
761 wxRICHTEXT_SETSTYLE_NONE: no style flag.
762 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this operation should be
764 wxRICHTEXT_SETSTYLE_OPTIMIZE: specifies that the style should not be applied
766 combined style at this point is already the style in question.
767 wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY: specifies that the style should only be
768 applied to paragraphs,
769 and not the content. This allows content styling to be preserved independently
770 from that of e.g. a named paragraph style.
771 wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY: specifies that the style should only be
772 applied to characters,
773 and not the paragraph. This allows content styling to be preserved
774 independently from that of e.g. a named paragraph style.
775 wxRICHTEXT_SETSTYLE_RESET: resets (clears) the existing style before applying
777 wxRICHTEXT_SETSTYLE_REMOVE: removes the specified style. Only the style flags
778 are used in this operation.
780 bool SetStyle(const wxRichTextRange
& range
,
781 const wxTextAttr
& style
,
782 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
);
785 Sets the current style sheet, if any. This will allow the application to use
786 named character and paragraph styles found in the style sheet.
788 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
);
791 Submit an action immediately, or delay it according to whether collapsing is on.
793 bool SubmitAction(wxRichTextAction
* action
);
796 Returns @true if undo suppression is currently on.
798 bool SuppressingUndo();
803 @class wxRichTextFileHandler
804 @headerfile richtextbuffer.h wx/richtext/richtextbuffer.h
806 This is the base class for file handlers, for loading and/or saving content
807 associated with a wxRichTextBuffer.
812 class wxRichTextFileHandler
: public wxObject
818 wxRichTextFileHandler(const wxString
& name
= wxEmptyString
,
819 const wxString
& ext
= wxEmptyString
,
823 Override this function and return @true if this handler can we handle @e
824 filename. By default,
825 this function checks the extension.
827 bool CanHandle(const wxString
& filename
);
830 Override and return @true if this handler can load content.
835 Override and return @true if this handler can save content.
840 Override to load content from @e stream into @e buffer.
842 bool DoLoadFile(wxRichTextBuffer
* buffer
, wxInputStream
& stream
);
845 Override to save content to @e stream from @e buffer.
847 bool DoSaveFile(wxRichTextBuffer
* buffer
, wxOutputStream
& stream
);
850 Returns the encoding associated with the handler (if any).
852 const wxString
GetEncoding();
855 Returns the extension associated with the handler.
857 wxString
GetExtension();
860 Returns flags that change the behaviour of loading or saving. See the
861 documentation for each
862 handler class to see what flags are relevant for each handler.
867 Returns the name of the handler.
872 Returns the type of the handler.
877 Returns @true if this handler should be visible to the user.
883 Loads content from a stream or file. Not all handlers will implement file
886 bool LoadFile(wxRichTextBuffer
* buffer
, wxInputStream
& stream
);
887 bool LoadFile(wxRichTextBuffer
* buffer
,
888 const wxString
& filename
);
893 Saves content to a stream or file. Not all handlers will implement file saving.
895 bool SaveFile(wxRichTextBuffer
* buffer
, wxOutputStream
& stream
);
896 bool SaveFile(wxRichTextBuffer
* buffer
,
897 const wxString
& filename
);
901 Sets the encoding to use when saving a file. If empty, a suitable encoding is
904 void SetEncoding(const wxString
& encoding
);
907 Sets the default extension to recognise.
909 void SetExtension(const wxString
& ext
);
912 Sets flags that change the behaviour of loading or saving. See the
913 documentation for each
914 handler class to see what flags are relevant for each handler.
916 You call this function directly if you are using a file handler explicitly
918 going through the text control or buffer LoadFile/SaveFile API). Or, you can
919 call the control or buffer's SetHandlerFlags function to set the flags that will
920 be used for subsequent load and save operations.
922 void SetFlags(int flags
);
925 Sets the name of the handler.
927 void SetName(const wxString
& name
);
930 Sets the handler type.
932 void SetType(int type
);
935 Sets whether the handler should be visible to the user (via the application's
939 void SetVisible(bool visible
);
944 @class wxRichTextRange
945 @headerfile richtextbuffer.h wx/richtext/richtextbuffer.h
947 This class stores beginning and end positions for a range of data.
952 class wxRichTextRange
959 wxRichTextRange(long start
, long end
);
960 wxRichTextRange(const wxRichTextRange
& range
);
970 Returns @true if the given position is within this range. Does not
971 match if the range is empty.
973 bool Contains(long pos
);
976 Converts the internal range, which uses the first and last character positions
978 to the API-standard range, whose end is one past the last character in the
980 In other words, one is added to the end position.
982 wxRichTextRange
FromInternal();
985 Returns the end position.
990 Returns the length of the range.
995 Returns the start of the range.
1000 Returns @true if this range is completely outside @e range.
1002 bool IsOutside(const wxRichTextRange
& range
);
1005 Returns @true if this range is completely within @e range.
1007 bool IsWithin(const wxRichTextRange
& range
);
1010 Limits this range to be within @e range.
1012 bool LimitTo(const wxRichTextRange
& range
);
1015 Sets the end of the range.
1017 void SetEnd(long end
);
1022 void SetRange(long start
, long end
);
1025 Sets the start of the range.
1027 void SetStart(long start
);
1030 Swaps the start and end.
1035 Converts the API-standard range, whose end is one past the last character in
1037 to the internal form, which uses the first and last character positions of the
1039 In other words, one is subtracted from the end position.
1041 wxRichTextRange
ToInternal();
1044 Adds @e range to this range.
1046 wxRichTextRange
operator+(const wxRichTextRange
& range
);
1049 Subtracts @e range from this range.
1051 wxRichTextRange
operator-(const wxRichTextRange
& range
);
1054 Assigns @e range to this range.
1056 void operator=(const wxRichTextRange
& range
);
1059 Returns @true if @e range is the same as this range.
1061 bool operator==(const wxRichTextRange
& range
);