1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtext/richtextbuffer.h
3 // Purpose: interface of wxRichTextBuffer
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.
18 @see wxTextAttr, wxRichTextCtrl
20 class wxRichTextBuffer
27 wxRichTextBuffer(const wxRichTextBuffer
& obj
);
37 Adds an event handler to the buffer's list of handlers. A buffer associated with
38 a contol has the control as the only event handler, but the application is free
39 to add more if further notification is required. All handlers are notified
40 of an event originating from the buffer, such as the replacement of a style
42 during loading. The buffer never deletes any of the event handlers, unless
43 RemoveEventHandler() is
44 called with @true as the second argument.
46 bool AddEventHandler(wxEvtHandler
* handler
);
51 void AddHandler(wxRichTextFileHandler
* handler
);
54 Adds a paragraph of text.
56 wxRichTextRange
AddParagraph(const wxString
& text
);
59 Returns @true if the buffer is currently collapsing commands into a single
62 bool BatchingUndo() const;
65 Begins using alignment.
67 bool BeginAlignment(wxTextAttrAlignment alignment
);
70 Begins collapsing undo/redo commands. Note that this may not work properly
71 if combining commands that delete or insert content, changing ranges for
73 @a cmdName should be the name of the combined command that will appear
74 next to Undo and Redo in the edit menu.
76 bool BeginBatchUndo(const wxString
& cmdName
);
84 Begins applying the named character style.
86 bool BeginCharacterStyle(const wxString
& characterStyle
);
89 Begins using this font.
91 bool BeginFont(const wxFont
& font
);
94 Begins using the given point size.
96 bool BeginFontSize(int pointSize
);
104 Begin using @a leftIndent for the left indent, and optionally @a leftSubIndent
106 the sub-indent. Both are expressed in tenths of a millimetre.
107 The sub-indent is an offset from the left of the paragraph, and is used for all
109 first line in a paragraph. A positive value will cause the first line to appear
111 of the subsequent lines, and a negative value will cause the first line to be
113 relative to the subsequent lines.
115 bool BeginLeftIndent(int leftIndent
, int leftSubIndent
= 0);
118 Begins line spacing using the specified value. @e spacing is a multiple, where
119 10 means single-spacing,
120 15 means 1.5 spacing, and 20 means double spacing. The following constants are
121 defined for convenience:
123 bool BeginLineSpacing(int lineSpacing
);
126 Begins using a specified list style. Optionally, you can also pass a level and
129 bool BeginListStyle(const wxString
& listStyle
, int level
= 1,
133 Begins a numbered bullet. This call will be needed for each item in the list,
135 application should take care of incrementing the numbering.
136 @a bulletNumber is a number, usually starting with 1.
137 @a leftIndent and @a leftSubIndent are values in tenths of a millimetre.
138 @a bulletStyle is a bitlist of the following values:
140 wxRichTextBuffer uses indentation to render a bulleted item. The left indent is
142 the margin and the bullet. The content of the paragraph, including the first
144 at leftMargin + leftSubIndent. So the distance between the left edge of the
146 left of the actual paragraph is leftSubIndent.
148 bool BeginNumberedBullet(int bulletNumber
, int leftIndent
,
150 int bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
);
153 Begins paragraph spacing; pass the before-paragraph and after-paragraph spacing
157 bool BeginParagraphSpacing(int before
, int after
);
160 Begins applying the named paragraph style.
162 bool BeginParagraphStyle(const wxString
& paragraphStyle
);
165 Begins a right indent, specified in tenths of a millimetre.
167 bool BeginRightIndent(int rightIndent
);
170 Begins applying a standard bullet, using one of the standard bullet names
171 (currently @c standard/circle or @c standard/square.
172 See BeginNumberedBullet() for an explanation of how indentation is used to
173 render the bulleted paragraph.
175 bool BeginStandardBullet(const wxString
& bulletName
,
178 int bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_STANDARD
);
181 Begins using a specified style.
183 bool BeginStyle(const wxTextAttr
& style
);
186 Begins suppressing undo/redo commands. The way undo is suppressed may be
188 differently by each command. If not dealt with by a command implementation, then
189 it will be implemented automatically by not storing the command in the undo
191 when the action is submitted to the command processor.
193 bool BeginSuppressUndo();
196 Begins applying a symbol bullet, using a character from the current font. See
197 BeginNumberedBullet() for
198 an explanation of how indentation is used to render the bulleted paragraph.
200 bool BeginSymbolBullet(wxChar symbol
, int leftIndent
,
202 int bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_SYMBOL
);
205 Begins using the specified text foreground colour.
207 bool BeginTextColour(const wxColour
& colour
);
210 Begins applying wxTEXT_ATTR_URL to the content. Pass a URL and optionally, a
211 character style to apply,
212 since it is common to mark a URL with a familiar style such as blue text with
215 bool BeginURL(const wxString
& url
,
216 const wxString
& characterStyle
= wxEmptyString
);
219 Begins using underline.
221 bool BeginUnderline();
224 Returns @true if content can be pasted from the clipboard.
226 bool CanPasteFromClipboard() const;
229 Cleans up the file handlers.
231 void CleanUpHandlers();
240 Clears the list style from the given range, clearing list-related attributes
241 and applying any named paragraph style associated with each paragraph.
242 @a flags is a bit list of the following:
243 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
244 See also SetListStyle(), PromoteList(), NumberList().
246 bool ClearListStyle(const wxRichTextRange
& range
,
247 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
);
248 bool ClearListStyle(const wxRichTextRange
& range
,
249 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
);
253 Clears the style stack.
255 void ClearStyleStack();
260 wxRichTextObject
* Clone() const;
263 Copies the given buffer.
265 void Copy(const wxRichTextBuffer
& obj
);
268 Copy the given range to the clipboard.
270 bool CopyToClipboard(const wxRichTextRange
& range
);
273 Submits a command to delete the given range.
275 bool DeleteRangeWithUndo(const wxRichTextRange
& range
,
276 wxRichTextCtrl
* ctrl
);
280 Dumps the contents of the buffer for debugging purposes.
283 void Dump(wxTextOutputStream
& stream
);
292 Ends all styles that have been started with a Begin... command.
297 Ends collapsing undo/redo commands, and submits the combined command.
307 Ends using the named character style.
309 bool EndCharacterStyle();
317 Ends using a point size.
327 Ends using a left indent.
329 bool EndLeftIndent();
332 Ends using a line spacing.
334 bool EndLineSpacing();
337 Ends using a specified list style.
342 Ends a numbered bullet.
344 bool EndNumberedBullet();
347 Ends paragraph spacing.
349 bool EndParagraphSpacing();
352 Ends applying a named character style.
354 bool EndParagraphStyle();
357 Ends using a right indent.
359 bool EndRightIndent();
362 Ends using a standard bullet.
364 bool EndStandardBullet();
367 Ends the current style.
372 Ends suppressing undo/redo commands.
374 bool EndSuppressUndo();
377 Ends using a symbol bullet.
379 bool EndSymbolBullet();
382 Ends using a text foreground colour.
384 bool EndTextColour();
392 Ends using underline.
398 Finds a handler by name.
400 wxRichTextFileHandler
* FindHandler(int imageType
);
401 wxRichTextFileHandler
* FindHandler(const wxString
& extension
,
403 wxRichTextFileHandler
* FindHandler(const wxString
& name
);
407 Finds a handler by filename or, if supplied, type.
409 wxRichTextFileHandler
* FindHandlerFilenameOrType(const wxString
& filename
,
413 Gets the basic (overall) style. This is the style of the whole
414 buffer before further styles are applied, unlike the default style, which
415 only affects the style currently being applied (for example, setting the default
416 style to bold will cause subsequently inserted text to be bold).
418 const wxTextAttr
GetBasicStyle() const;
421 Gets the collapsed command.
423 wxRichTextCommand
* GetBatchedCommand() const;
426 Gets the command processor. A text buffer always creates its own command
430 wxCommandProcessor
* GetCommandProcessor() const;
433 Returns the current default style, affecting the style currently being applied
434 (for example, setting the default
435 style to bold will cause subsequently inserted text to be bold).
437 const wxTextAttr
GetDefaultStyle() const;
440 Gets a wildcard incorporating all visible handlers. If @a types is present,
441 it will be filled with the file type corresponding to each filter. This can be
442 used to determine the type to pass to @ref getextwildcard() LoadFile given a
445 wxString
GetExtWildcard(bool combine
= false, bool save
= false,
446 wxArrayInt
* types
= NULL
);
449 Returns the list of file handlers.
451 wxList
GetHandlers();
454 Returns the object to be used to render certain aspects of the content, such as
457 static wxRichTextRenderer
* GetRenderer();
460 Gets the attributes at the given position.
461 This function gets the combined style - that is, the style you see on the
463 of combining base style, paragraph style and character style attributes. To get
465 or paragraph style alone, use GetUncombinedStyle().
467 bool GetStyle(long position
, wxTextAttr
& style
);
470 This function gets a style representing the common, combined attributes in the
472 Attributes which have different values within the specified range will not be
475 The function is used to get the attributes to display in the formatting dialog:
477 can edit the attributes common to the selection, and optionally specify the
479 attributes to be applied uniformly.
480 To apply the edited attributes, you can use SetStyle() specifying
481 the wxRICHTEXT_SETSTYLE_OPTIMIZE flag, which will only apply attributes that
483 from the @e combined attributes within the range. So, the user edits the
484 effective, displayed attributes
485 for the range, but his choice won't be applied unnecessarily to content. As an
487 say the style for a paragraph specifies bold, but the paragraph text doesn't
488 specify a weight. The
489 combined style is bold, and this is what the user will see on-screen and in the
491 dialog. The user now specifies red text, in addition to bold. When applying with
492 SetStyle, the content font weight attributes won't be changed to bold because
493 this is already specified
494 by the paragraph. However the text colour attributes @e will be changed to
497 bool GetStyleForRange(const wxRichTextRange
& range
,
501 Returns the current style sheet associated with the buffer, if any.
503 wxRichTextStyleSheet
* GetStyleSheet() const;
506 Get the size of the style stack, for example to check correct nesting.
508 size_t GetStyleStackSize() const;
511 Gets the attributes at the given position.
512 This function gets the @e uncombined style - that is, the attributes associated
514 paragraph or character content, and not necessarily the combined attributes you
516 screen. To get the combined attributes, use GetStyle().
517 If you specify (any) paragraph attribute in @e style's flags, this function
519 the paragraph attributes. Otherwise, it will return the character attributes.
521 bool GetUncombinedStyle(long position
, wxTextAttr
& style
);
524 Finds the text position for the given position, putting the position in @a
526 one is found. @a pt is in logical units (a zero y position is
527 at the beginning of the buffer).
528 The function returns one of the following values:
530 int HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
);
538 Initialises the standard handlers. Currently, only the plain text
539 loading/saving handler
540 is initialised by default.
542 void InitStandardHandlers();
545 Inserts a handler at the front of the list.
547 void InsertHandler(wxRichTextFileHandler
* handler
);
550 Submits a command to insert the given image.
552 bool InsertImageWithUndo(long pos
,
553 const wxRichTextImageBlock
& imageBlock
,
554 wxRichTextCtrl
* ctrl
);
557 Submits a command to insert a newline.
559 bool InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
);
562 Submits a command to insert the given text.
564 bool InsertTextWithUndo(long pos
, const wxString
& text
,
565 wxRichTextCtrl
* ctrl
);
568 Returns @true if the buffer has been modified.
570 bool IsModified() const;
574 Loads content from a file.
576 bool LoadFile(wxInputStream
& stream
,
577 int type
= wxRICHTEXT_TYPE_ANY
);
578 bool LoadFile(const wxString
& filename
,
579 int type
= wxRICHTEXT_TYPE_ANY
);
583 Marks the buffer as modified or unmodified.
585 void Modify(bool modify
= true);
589 Numbers the paragraphs in the given range. Pass flags to determine how the
591 Either the style definition or the name of the style definition (in the current
592 sheet) can be passed.
593 @a flags is a bit list of the following:
594 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
595 wxRICHTEXT_SETSTYLE_RENUMBER: specifies that numbering should start from @e
596 startFrom, otherwise existing attributes are used.
597 wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL: specifies that @a listLevel should be used
598 as the level for all paragraphs, otherwise the current indentation will be used.
599 See also SetListStyle(), PromoteList(), ClearListStyle().
601 bool NumberList(const wxRichTextRange
& range
,
602 const wxRichTextListStyleDefinition
* style
,
603 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
606 bool Number(const wxRichTextRange
& range
,
607 const wxString
& styleName
,
608 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
614 Pastes the clipboard content to the buffer at the given position.
616 bool PasteFromClipboard(long position
);
620 Promotes or demotes the paragraphs in the given range. A positive @a promoteBy
621 produces a smaller indent, and a negative number
622 produces a larger indent. Pass flags to determine how the attributes are set.
623 Either the style definition or the name of the style definition (in the current
624 sheet) can be passed.
625 @a flags is a bit list of the following:
626 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
627 wxRICHTEXT_SETSTYLE_RENUMBER: specifies that numbering should start from @e
628 startFrom, otherwise existing attributes are used.
629 wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL: specifies that @a listLevel should be used
630 as the level for all paragraphs, otherwise the current indentation will be used.
631 See also SetListStyle(), See also SetListStyle(), ClearListStyle().
633 bool PromoteList(int promoteBy
, const wxRichTextRange
& range
,
634 const wxRichTextListStyleDefinition
* style
,
635 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
637 bool PromoteList(int promoteBy
, const wxRichTextRange
& range
,
638 const wxString
& styleName
,
639 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
644 Removes an event handler from the buffer's list of handlers, deleting the
645 object if @a deleteHandler is @true.
647 bool RemoveEventHandler(wxEvtHandler
* handler
,
648 bool deleteHandler
= false);
653 bool RemoveHandler(const wxString
& name
);
656 Clears the buffer, adds a new blank paragraph, and clears the command history.
658 void ResetAndClearCommands();
662 Saves content to a file.
664 bool SaveFile(wxOutputStream
& stream
,
665 int type
= wxRICHTEXT_TYPE_ANY
);
666 bool SaveFile(const wxString
& filename
,
667 int type
= wxRICHTEXT_TYPE_ANY
);
671 Sets the basic (overall) style. This is the style of the whole
672 buffer before further styles are applied, unlike the default style, which
673 only affects the style currently being applied (for example, setting the default
674 style to bold will cause subsequently inserted text to be bold).
676 void SetBasicStyle(const wxTextAttr
& style
);
679 Sets the default style, affecting the style currently being applied (for
680 example, setting the default
681 style to bold will cause subsequently inserted text to be bold).
682 This is not cumulative - setting the default style will replace the previous
685 void SetDefaultStyle(const wxTextAttr
& style
);
689 Sets the list attributes for the given range, passing flags to determine how
690 the attributes are set.
691 Either the style definition or the name of the style definition (in the current
692 sheet) can be passed.
693 @a flags is a bit list of the following:
694 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
695 wxRICHTEXT_SETSTYLE_RENUMBER: specifies that numbering should start from @e
696 startFrom, otherwise existing attributes are used.
697 wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL: specifies that @a listLevel should be used
698 as the level for all paragraphs, otherwise the current indentation will be used.
699 See also NumberList(), PromoteList(), ClearListStyle().
701 bool SetListStyle(const wxRichTextRange
& range
,
702 const wxRichTextListStyleDefinition
* style
,
703 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
706 bool SetListStyle(const wxRichTextRange
& range
,
707 const wxString
& styleName
,
708 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
714 Sets @a renderer as the object to be used to render certain aspects of the
715 content, such as bullets.
716 You can override default rendering by deriving a new class from
717 wxRichTextRenderer or wxRichTextStdRenderer,
718 overriding one or more virtual functions, and setting an instance of the class
721 static void SetRenderer(wxRichTextRenderer
* renderer
);
724 Sets the attributes for the given range. Pass flags to determine how the
726 The end point of range is specified as the last character position of the span
728 So, for example, to set the style for a character at position 5, use the range
730 This differs from the wxRichTextCtrl API, where you would specify (5,6).
731 @a flags may contain a bit list of the following values:
732 wxRICHTEXT_SETSTYLE_NONE: no style flag.
733 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this operation should be
735 wxRICHTEXT_SETSTYLE_OPTIMIZE: specifies that the style should not be applied
737 combined style at this point is already the style in question.
738 wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY: specifies that the style should only be
739 applied to paragraphs,
740 and not the content. This allows content styling to be preserved independently
741 from that of e.g. a named paragraph style.
742 wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY: specifies that the style should only be
743 applied to characters,
744 and not the paragraph. This allows content styling to be preserved
745 independently from that of e.g. a named paragraph style.
746 wxRICHTEXT_SETSTYLE_RESET: resets (clears) the existing style before applying
748 wxRICHTEXT_SETSTYLE_REMOVE: removes the specified style. Only the style flags
749 are used in this operation.
751 bool SetStyle(const wxRichTextRange
& range
,
752 const wxTextAttr
& style
,
753 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
);
756 Sets the current style sheet, if any. This will allow the application to use
757 named character and paragraph styles found in the style sheet.
759 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
);
762 Submit an action immediately, or delay it according to whether collapsing is on.
764 bool SubmitAction(wxRichTextAction
* action
);
767 Returns @true if undo suppression is currently on.
769 bool SuppressingUndo() const;
775 @class wxRichTextFileHandler
776 @headerfile richtextbuffer.h wx/richtext/richtextbuffer.h
778 This is the base class for file handlers, for loading and/or saving content
779 associated with a wxRichTextBuffer.
784 class wxRichTextFileHandler
: public wxObject
790 wxRichTextFileHandler(const wxString
& name
= wxEmptyString
,
791 const wxString
& ext
= wxEmptyString
,
795 Override this function and return @true if this handler can we handle @e
796 filename. By default,
797 this function checks the extension.
799 bool CanHandle(const wxString
& filename
) const;
802 Override and return @true if this handler can load content.
804 bool CanLoad() const;
807 Override and return @true if this handler can save content.
809 bool CanSave() const;
812 Override to load content from @a stream into @e buffer.
814 bool DoLoadFile(wxRichTextBuffer
* buffer
, wxInputStream
& stream
);
817 Override to save content to @a stream from @e buffer.
819 bool DoSaveFile(wxRichTextBuffer
* buffer
, wxOutputStream
& stream
);
822 Returns the encoding associated with the handler (if any).
824 const wxString
GetEncoding() const;
827 Returns the extension associated with the handler.
829 wxString
GetExtension() const;
832 Returns flags that change the behaviour of loading or saving. See the
833 documentation for each
834 handler class to see what flags are relevant for each handler.
836 int GetFlags() const;
839 Returns the name of the handler.
841 wxString
GetName() const;
844 Returns the type of the handler.
849 Returns @true if this handler should be visible to the user.
851 bool IsVisible() const;
855 Loads content from a stream or file. Not all handlers will implement file
858 bool LoadFile(wxRichTextBuffer
* buffer
, wxInputStream
& stream
);
859 bool LoadFile(wxRichTextBuffer
* buffer
,
860 const wxString
& filename
);
865 Saves content to a stream or file. Not all handlers will implement file saving.
867 bool SaveFile(wxRichTextBuffer
* buffer
, wxOutputStream
& stream
);
868 bool SaveFile(wxRichTextBuffer
* buffer
,
869 const wxString
& filename
);
873 Sets the encoding to use when saving a file. If empty, a suitable encoding is
876 void SetEncoding(const wxString
& encoding
);
879 Sets the default extension to recognise.
881 void SetExtension(const wxString
& ext
);
884 Sets flags that change the behaviour of loading or saving. See the
885 documentation for each
886 handler class to see what flags are relevant for each handler.
887 You call this function directly if you are using a file handler explicitly
889 going through the text control or buffer LoadFile/SaveFile API). Or, you can
890 call the control or buffer's SetHandlerFlags function to set the flags that will
891 be used for subsequent load and save operations.
893 void SetFlags(int flags
);
896 Sets the name of the handler.
898 void SetName(const wxString
& name
);
901 Sets the handler type.
903 void SetType(int type
);
906 Sets whether the handler should be visible to the user (via the application's
910 void SetVisible(bool visible
);
916 @class wxRichTextRange
917 @headerfile richtextbuffer.h wx/richtext/richtextbuffer.h
919 This class stores beginning and end positions for a range of data.
924 class wxRichTextRange
931 wxRichTextRange(long start
, long end
);
932 wxRichTextRange(const wxRichTextRange
& range
);
942 Returns @true if the given position is within this range. Does not
943 match if the range is empty.
945 bool Contains(long pos
) const;
948 Converts the internal range, which uses the first and last character positions
950 to the API-standard range, whose end is one past the last character in the
952 In other words, one is added to the end position.
954 wxRichTextRange
FromInternal() const;
957 Returns the end position.
962 Returns the length of the range.
964 long GetLength() const;
967 Returns the start of the range.
969 long GetStart() const;
972 Returns @true if this range is completely outside @e range.
974 bool IsOutside(const wxRichTextRange
& range
) const;
977 Returns @true if this range is completely within @e range.
979 bool IsWithin(const wxRichTextRange
& range
) const;
982 Limits this range to be within @e range.
984 bool LimitTo(const wxRichTextRange
& range
);
987 Sets the end of the range.
989 void SetEnd(long end
);
994 void SetRange(long start
, long end
);
997 Sets the start of the range.
999 void SetStart(long start
);
1002 Swaps the start and end.
1007 Converts the API-standard range, whose end is one past the last character in
1009 to the internal form, which uses the first and last character positions of the
1011 In other words, one is subtracted from the end position.
1013 wxRichTextRange
ToInternal() const;
1016 Adds @a range to this range.
1018 wxRichTextRange
operator+(const wxRichTextRange
& range
) const;
1021 Subtracts @a range from this range.
1023 wxRichTextRange
operator-(const wxRichTextRange
& range
) const;
1026 Assigns @a range to this range.
1028 void operator=(const wxRichTextRange
& range
);
1031 Returns @true if @a range is the same as this range.
1033 bool operator==(const wxRichTextRange
& range
) const;