1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtext/richtextbuffer.h
3 // Purpose: interface of wxRichTextBuffer
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
10 @class wxRichTextBuffer
12 This class represents the whole buffer associated with a wxRichTextCtrl.
17 @see wxTextAttr, wxRichTextCtrl
19 class wxRichTextBuffer
26 wxRichTextBuffer(const wxRichTextBuffer
& obj
);
33 virtual ~wxRichTextBuffer();
36 Adds an event handler to the buffer's list of handlers. A buffer associated with
37 a contol has the control as the only event handler, but the application is free
38 to add more if further notification is required. All handlers are notified
39 of an event originating from the buffer, such as the replacement of a style
41 during loading. The buffer never deletes any of the event handlers, unless
42 RemoveEventHandler() is
43 called with @true as the second argument.
45 bool AddEventHandler(wxEvtHandler
* handler
);
50 static void AddHandler(wxRichTextFileHandler
* handler
);
53 Adds a paragraph of text.
55 wxRichTextRange
AddParagraph(const wxString
& text
);
58 Returns @true if the buffer is currently collapsing commands into a single
61 virtual bool BatchingUndo() const;
64 Begins using alignment.
66 bool BeginAlignment(wxTextAttrAlignment alignment
);
69 Begins collapsing undo/redo commands. Note that this may not work properly
70 if combining commands that delete or insert content, changing ranges for
72 @a cmdName should be the name of the combined command that will appear
73 next to Undo and Redo in the edit menu.
75 virtual bool BeginBatchUndo(const wxString
& cmdName
);
83 Begins applying the named character style.
85 bool BeginCharacterStyle(const wxString
& characterStyle
);
88 Begins using this font.
90 bool BeginFont(const wxFont
& font
);
93 Begins using the given point size.
95 bool BeginFontSize(int pointSize
);
103 Begin using @a leftIndent for the left indent, and optionally @a leftSubIndent
105 the sub-indent. Both are expressed in tenths of a millimetre.
106 The sub-indent is an offset from the left of the paragraph, and is used for all
108 first line in a paragraph. A positive value will cause the first line to appear
110 of the subsequent lines, and a negative value will cause the first line to be
112 relative to the subsequent lines.
114 bool BeginLeftIndent(int leftIndent
, int leftSubIndent
= 0);
117 Begins line spacing using the specified value. @e spacing is a multiple, where
118 10 means single-spacing,
119 15 means 1.5 spacing, and 20 means double spacing. The following constants are
120 defined for convenience:
122 bool BeginLineSpacing(int lineSpacing
);
125 Begins using a specified list style. Optionally, you can also pass a level and
128 bool BeginListStyle(const wxString
& listStyle
, int level
= 1,
132 Begins a numbered bullet. This call will be needed for each item in the list,
134 application should take care of incrementing the numbering.
135 @a bulletNumber is a number, usually starting with 1.
136 @a leftIndent and @a leftSubIndent are values in tenths of a millimetre.
137 @a bulletStyle is a bitlist of the following values:
139 wxRichTextBuffer uses indentation to render a bulleted item. The left indent is
141 the margin and the bullet. The content of the paragraph, including the first
143 at leftMargin + leftSubIndent. So the distance between the left edge of the
145 left of the actual paragraph is leftSubIndent.
147 bool BeginNumberedBullet(int bulletNumber
, int leftIndent
,
149 int bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
);
152 Begins paragraph spacing; pass the before-paragraph and after-paragraph spacing
156 bool BeginParagraphSpacing(int before
, int after
);
159 Begins applying the named paragraph style.
161 bool BeginParagraphStyle(const wxString
& paragraphStyle
);
164 Begins a right indent, specified in tenths of a millimetre.
166 bool BeginRightIndent(int rightIndent
);
169 Begins applying a standard bullet, using one of the standard bullet names
170 (currently @c standard/circle or @c standard/square.
171 See BeginNumberedBullet() for an explanation of how indentation is used to
172 render the bulleted paragraph.
174 bool BeginStandardBullet(const wxString
& bulletName
,
177 int bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_STANDARD
);
180 Begins using a specified style.
182 virtual bool BeginStyle(const wxTextAttr
& style
);
185 Begins suppressing undo/redo commands. The way undo is suppressed may be
187 differently by each command. If not dealt with by a command implementation, then
188 it will be implemented automatically by not storing the command in the undo
190 when the action is submitted to the command processor.
192 virtual bool BeginSuppressUndo();
195 Begins applying a symbol bullet, using a character from the current font. See
196 BeginNumberedBullet() for
197 an explanation of how indentation is used to render the bulleted paragraph.
199 bool BeginSymbolBullet(wxChar symbol
, int leftIndent
,
201 int bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_SYMBOL
);
204 Begins using the specified text foreground colour.
206 bool BeginTextColour(const wxColour
& colour
);
209 Begins applying wxTEXT_ATTR_URL to the content. Pass a URL and optionally, a
210 character style to apply,
211 since it is common to mark a URL with a familiar style such as blue text with
214 bool BeginURL(const wxString
& url
,
215 const wxString
& characterStyle
= wxEmptyString
);
218 Begins using underline.
220 bool BeginUnderline();
223 Returns @true if content can be pasted from the clipboard.
225 virtual bool CanPasteFromClipboard() const;
228 Cleans up the file handlers.
230 static void CleanUpHandlers();
239 Clears the list style from the given range, clearing list-related attributes
240 and applying any named paragraph style associated with each paragraph.
241 @a flags is a bit list of the following:
242 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
243 See also SetListStyle(), PromoteList(), NumberList().
245 bool ClearListStyle(const wxRichTextRange
& range
,
246 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
);
247 bool ClearListStyle(const wxRichTextRange
& range
,
248 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
);
252 Clears the style stack.
254 virtual void ClearStyleStack();
259 virtual wxRichTextObject
* Clone() const;
262 Copies the given buffer.
264 void Copy(const wxRichTextBuffer
& obj
);
267 Copy the given range to the clipboard.
269 virtual bool CopyToClipboard(const wxRichTextRange
& range
);
272 Submits a command to delete the given range.
274 bool DeleteRangeWithUndo(const wxRichTextRange
& range
,
275 wxRichTextCtrl
* ctrl
);
279 Dumps the contents of the buffer for debugging purposes.
282 void Dump(wxTextOutputStream
& stream
);
291 Ends all styles that have been started with a Begin... command.
293 virtual bool EndAllStyles();
296 Ends collapsing undo/redo commands, and submits the combined command.
298 virtual bool EndBatchUndo();
306 Ends using the named character style.
308 bool EndCharacterStyle();
316 Ends using a point size.
326 Ends using a left indent.
328 bool EndLeftIndent();
331 Ends using a line spacing.
333 bool EndLineSpacing();
336 Ends using a specified list style.
341 Ends a numbered bullet.
343 bool EndNumberedBullet();
346 Ends paragraph spacing.
348 bool EndParagraphSpacing();
351 Ends applying a named character style.
353 bool EndParagraphStyle();
356 Ends using a right indent.
358 bool EndRightIndent();
361 Ends using a standard bullet.
363 bool EndStandardBullet();
366 Ends the current style.
368 virtual bool EndStyle();
371 Ends suppressing undo/redo commands.
373 virtual bool EndSuppressUndo();
376 Ends using a symbol bullet.
378 bool EndSymbolBullet();
381 Ends using a text foreground colour.
383 bool EndTextColour();
391 Ends using underline.
397 Finds a handler by name.
399 wxRichTextFileHandler
* FindHandler(int imageType
);
400 wxRichTextFileHandler
* FindHandler(const wxString
& extension
,
402 wxRichTextFileHandler
* FindHandler(const wxString
& name
);
406 Finds a handler by filename or, if supplied, type.
408 wxRichTextFileHandler
* FindHandlerFilenameOrType(const wxString
& filename
,
412 Gets the basic (overall) style. This is the style of the whole
413 buffer before further styles are applied, unlike the default style, which
414 only affects the style currently being applied (for example, setting the default
415 style to bold will cause subsequently inserted text to be bold).
417 const wxTextAttr
GetBasicStyle() const;
420 Gets the collapsed command.
422 virtual wxRichTextCommand
* GetBatchedCommand() const;
425 Gets the command processor. A text buffer always creates its own command
429 wxCommandProcessor
* GetCommandProcessor() const;
432 Returns the current default style, affecting the style currently being applied
433 (for example, setting the default
434 style to bold will cause subsequently inserted text to be bold).
436 const wxTextAttr
GetDefaultStyle() const;
439 Gets a wildcard incorporating all visible handlers. If @a types is present,
440 it will be filled with the file type corresponding to each filter. This can be
441 used to determine the type to pass to @ref getextwildcard() LoadFile given a
444 wxString
GetExtWildcard(bool combine
= false, bool save
= false,
445 wxArrayInt
* types
= NULL
);
448 Returns the list of file handlers.
450 wxList
GetHandlers();
453 Returns the object to be used to render certain aspects of the content, such as
456 static wxRichTextRenderer
* GetRenderer();
459 Gets the attributes at the given position.
460 This function gets the combined style - that is, the style you see on the
462 of combining base style, paragraph style and character style attributes. To get
464 or paragraph style alone, use GetUncombinedStyle().
466 bool GetStyle(long position
, wxTextAttr
& style
);
469 This function gets a style representing the common, combined attributes in the
471 Attributes which have different values within the specified range will not be
474 The function is used to get the attributes to display in the formatting dialog:
476 can edit the attributes common to the selection, and optionally specify the
478 attributes to be applied uniformly.
479 To apply the edited attributes, you can use SetStyle() specifying
480 the wxRICHTEXT_SETSTYLE_OPTIMIZE flag, which will only apply attributes that
482 from the @e combined attributes within the range. So, the user edits the
483 effective, displayed attributes
484 for the range, but his choice won't be applied unnecessarily to content. As an
486 say the style for a paragraph specifies bold, but the paragraph text doesn't
487 specify a weight. The
488 combined style is bold, and this is what the user will see on-screen and in the
490 dialog. The user now specifies red text, in addition to bold. When applying with
491 SetStyle, the content font weight attributes won't be changed to bold because
492 this is already specified
493 by the paragraph. However the text colour attributes @e will be changed to
496 bool GetStyleForRange(const wxRichTextRange
& range
,
500 Returns the current style sheet associated with the buffer, if any.
502 virtual wxRichTextStyleSheet
* GetStyleSheet() const;
505 Get the size of the style stack, for example to check correct nesting.
507 virtual size_t GetStyleStackSize() const;
510 Gets the attributes at the given position.
511 This function gets the @e uncombined style - that is, the attributes associated
513 paragraph or character content, and not necessarily the combined attributes you
515 screen. To get the combined attributes, use GetStyle().
516 If you specify (any) paragraph attribute in @e style's flags, this function
518 the paragraph attributes. Otherwise, it will return the character attributes.
520 bool GetUncombinedStyle(long position
, wxTextAttr
& style
);
523 Finds the text position for the given position, putting the position in @a
525 one is found. @a pt is in logical units (a zero y position is
526 at the beginning of the buffer).
527 The function returns one of the following values:
529 int HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
);
537 Initialises the standard handlers. Currently, only the plain text
538 loading/saving handler
539 is initialised by default.
541 static void InitStandardHandlers();
544 Inserts a handler at the front of the list.
546 static void InsertHandler(wxRichTextFileHandler
* handler
);
549 Submits a command to insert the given image.
551 bool InsertImageWithUndo(long pos
,
552 const wxRichTextImageBlock
& imageBlock
,
553 wxRichTextCtrl
* ctrl
);
556 Submits a command to insert a newline.
558 bool InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
);
561 Submits a command to insert the given text.
563 bool InsertTextWithUndo(long pos
, const wxString
& text
,
564 wxRichTextCtrl
* ctrl
);
567 Returns @true if the buffer has been modified.
569 bool IsModified() const;
573 Loads content from a file.
575 bool LoadFile(wxInputStream
& stream
,
576 int type
= wxRICHTEXT_TYPE_ANY
);
577 bool LoadFile(const wxString
& filename
,
578 int type
= wxRICHTEXT_TYPE_ANY
);
582 Marks the buffer as modified or unmodified.
584 void Modify(bool modify
= true);
588 Numbers the paragraphs in the given range. Pass flags to determine how the
590 Either the style definition or the name of the style definition (in the current
591 sheet) can be passed.
592 @a flags is a bit list of the following:
593 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
594 wxRICHTEXT_SETSTYLE_RENUMBER: specifies that numbering should start from @e
595 startFrom, otherwise existing attributes are used.
596 wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL: specifies that @a listLevel should be used
597 as the level for all paragraphs, otherwise the current indentation will be used.
598 See also SetListStyle(), PromoteList(), ClearListStyle().
600 bool NumberList(const wxRichTextRange
& range
,
601 const wxRichTextListStyleDefinition
* style
,
602 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
605 bool Number(const wxRichTextRange
& range
,
606 const wxString
& styleName
,
607 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
613 Pastes the clipboard content to the buffer at the given position.
615 virtual bool PasteFromClipboard(long position
);
619 Promotes or demotes the paragraphs in the given range. A positive @a promoteBy
620 produces a smaller indent, and a negative number
621 produces a larger indent. Pass flags to determine how the attributes are set.
622 Either the style definition or the name of the style definition (in the current
623 sheet) can be passed.
624 @a flags is a bit list of the following:
625 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
626 wxRICHTEXT_SETSTYLE_RENUMBER: specifies that numbering should start from @e
627 startFrom, otherwise existing attributes are used.
628 wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL: specifies that @a listLevel should be used
629 as the level for all paragraphs, otherwise the current indentation will be used.
630 See also SetListStyle(), See also SetListStyle(), ClearListStyle().
632 bool PromoteList(int promoteBy
, const wxRichTextRange
& range
,
633 const wxRichTextListStyleDefinition
* style
,
634 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
636 bool PromoteList(int promoteBy
, const wxRichTextRange
& range
,
637 const wxString
& styleName
,
638 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
643 Removes an event handler from the buffer's list of handlers, deleting the
644 object if @a deleteHandler is @true.
646 bool RemoveEventHandler(wxEvtHandler
* handler
,
647 bool deleteHandler
= false);
652 static bool RemoveHandler(const wxString
& name
);
655 Clears the buffer, adds a new blank paragraph, and clears the command history.
657 virtual void ResetAndClearCommands();
661 Saves content to a file.
663 bool SaveFile(wxOutputStream
& stream
,
664 int type
= wxRICHTEXT_TYPE_ANY
);
665 bool SaveFile(const wxString
& filename
,
666 int type
= wxRICHTEXT_TYPE_ANY
);
670 Sets the basic (overall) style. This is the style of the whole
671 buffer before further styles are applied, unlike the default style, which
672 only affects the style currently being applied (for example, setting the default
673 style to bold will cause subsequently inserted text to be bold).
675 void SetBasicStyle(const wxTextAttr
& style
);
678 Sets the default style, affecting the style currently being applied (for
679 example, setting the default
680 style to bold will cause subsequently inserted text to be bold).
681 This is not cumulative - setting the default style will replace the previous
684 void SetDefaultStyle(const wxTextAttr
& style
);
688 Sets the list attributes for the given range, passing flags to determine how
689 the attributes are set.
690 Either the style definition or the name of the style definition (in the current
691 sheet) can be passed.
692 @a flags is a bit list of the following:
693 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this command will be undoable.
694 wxRICHTEXT_SETSTYLE_RENUMBER: specifies that numbering should start from @e
695 startFrom, otherwise existing attributes are used.
696 wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL: specifies that @a listLevel should be used
697 as the level for all paragraphs, otherwise the current indentation will be used.
698 See also NumberList(), PromoteList(), ClearListStyle().
700 bool SetListStyle(const wxRichTextRange
& range
,
701 const wxRichTextListStyleDefinition
* style
,
702 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
705 bool SetListStyle(const wxRichTextRange
& range
,
706 const wxString
& styleName
,
707 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
,
713 Sets @a renderer as the object to be used to render certain aspects of the
714 content, such as bullets.
715 You can override default rendering by deriving a new class from
716 wxRichTextRenderer or wxRichTextStdRenderer,
717 overriding one or more virtual functions, and setting an instance of the class
720 static void SetRenderer(wxRichTextRenderer
* renderer
);
723 Sets the attributes for the given range. Pass flags to determine how the
725 The end point of range is specified as the last character position of the span
727 So, for example, to set the style for a character at position 5, use the range
729 This differs from the wxRichTextCtrl API, where you would specify (5,6).
730 @a flags may contain a bit list of the following values:
731 wxRICHTEXT_SETSTYLE_NONE: no style flag.
732 wxRICHTEXT_SETSTYLE_WITH_UNDO: specifies that this operation should be
734 wxRICHTEXT_SETSTYLE_OPTIMIZE: specifies that the style should not be applied
736 combined style at this point is already the style in question.
737 wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY: specifies that the style should only be
738 applied to paragraphs,
739 and not the content. This allows content styling to be preserved independently
740 from that of e.g. a named paragraph style.
741 wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY: specifies that the style should only be
742 applied to characters,
743 and not the paragraph. This allows content styling to be preserved
744 independently from that of e.g. a named paragraph style.
745 wxRICHTEXT_SETSTYLE_RESET: resets (clears) the existing style before applying
747 wxRICHTEXT_SETSTYLE_REMOVE: removes the specified style. Only the style flags
748 are used in this operation.
750 bool SetStyle(const wxRichTextRange
& range
,
751 const wxTextAttr
& style
,
752 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
);
755 Sets the current style sheet, if any. This will allow the application to use
756 named character and paragraph styles found in the style sheet.
758 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
);
761 Submit an action immediately, or delay it according to whether collapsing is on.
763 virtual bool SubmitAction(wxRichTextAction
* action
);
766 Returns @true if undo suppression is currently on.
768 virtual bool SuppressingUndo() const;
774 @class wxRichTextFileHandler
776 This is the base class for file handlers, for loading and/or saving content
777 associated with a wxRichTextBuffer.
782 class wxRichTextFileHandler
: public wxObject
788 wxRichTextFileHandler(const wxString
& name
= wxEmptyString
,
789 const wxString
& ext
= wxEmptyString
,
793 Override this function and return @true if this handler can we handle @e
794 filename. By default,
795 this function checks the extension.
797 virtual bool CanHandle(const wxString
& filename
) const;
800 Override and return @true if this handler can load content.
802 virtual bool CanLoad() const;
805 Override and return @true if this handler can save content.
807 virtual bool CanSave() const;
810 Override to load content from @a stream into @e buffer.
812 bool DoLoadFile(wxRichTextBuffer
* buffer
, wxInputStream
& stream
);
815 Override to save content to @a stream from @e buffer.
817 bool DoSaveFile(wxRichTextBuffer
* buffer
, wxOutputStream
& stream
);
820 Returns the encoding associated with the handler (if any).
822 const wxString
GetEncoding() const;
825 Returns the extension associated with the handler.
827 wxString
GetExtension() const;
830 Returns flags that change the behaviour of loading or saving. See the
831 documentation for each
832 handler class to see what flags are relevant for each handler.
834 int GetFlags() const;
837 Returns the name of the handler.
839 wxString
GetName() const;
842 Returns the type of the handler.
847 Returns @true if this handler should be visible to the user.
849 virtual bool IsVisible() const;
853 Loads content from a stream or file. Not all handlers will implement file
856 bool LoadFile(wxRichTextBuffer
* buffer
, wxInputStream
& stream
);
857 bool LoadFile(wxRichTextBuffer
* buffer
,
858 const wxString
& filename
);
863 Saves content to a stream or file. Not all handlers will implement file saving.
865 bool SaveFile(wxRichTextBuffer
* buffer
, wxOutputStream
& stream
);
866 bool SaveFile(wxRichTextBuffer
* buffer
,
867 const wxString
& filename
);
871 Sets the encoding to use when saving a file. If empty, a suitable encoding is
874 void SetEncoding(const wxString
& encoding
);
877 Sets the default extension to recognise.
879 void SetExtension(const wxString
& ext
);
882 Sets flags that change the behaviour of loading or saving. See the
883 documentation for each
884 handler class to see what flags are relevant for each handler.
885 You call this function directly if you are using a file handler explicitly
887 going through the text control or buffer LoadFile/SaveFile API). Or, you can
888 call the control or buffer's SetHandlerFlags function to set the flags that will
889 be used for subsequent load and save operations.
891 void SetFlags(int flags
);
894 Sets the name of the handler.
896 void SetName(const wxString
& name
);
899 Sets the handler type.
901 void SetType(int type
);
904 Sets whether the handler should be visible to the user (via the application's
908 virtual void SetVisible(bool visible
);
914 @class wxRichTextRange
916 This class stores beginning and end positions for a range of data.
921 class wxRichTextRange
928 wxRichTextRange(long start
, long end
);
929 wxRichTextRange(const wxRichTextRange
& range
);
939 Returns @true if the given position is within this range. Does not
940 match if the range is empty.
942 bool Contains(long pos
) const;
945 Converts the internal range, which uses the first and last character positions
947 to the API-standard range, whose end is one past the last character in the
949 In other words, one is added to the end position.
951 wxRichTextRange
FromInternal() const;
954 Returns the end position.
959 Returns the length of the range.
961 long GetLength() const;
964 Returns the start of the range.
966 long GetStart() const;
969 Returns @true if this range is completely outside @e range.
971 bool IsOutside(const wxRichTextRange
& range
) const;
974 Returns @true if this range is completely within @e range.
976 bool IsWithin(const wxRichTextRange
& range
) const;
979 Limits this range to be within @e range.
981 bool LimitTo(const wxRichTextRange
& range
);
984 Sets the end of the range.
986 void SetEnd(long end
);
991 void SetRange(long start
, long end
);
994 Sets the start of the range.
996 void SetStart(long start
);
999 Swaps the start and end.
1004 Converts the API-standard range, whose end is one past the last character in
1006 to the internal form, which uses the first and last character positions of the
1008 In other words, one is subtracted from the end position.
1010 wxRichTextRange
ToInternal() const;
1013 Adds @a range to this range.
1015 wxRichTextRange
operator+(const wxRichTextRange
& range
) const;
1018 Subtracts @a range from this range.
1020 wxRichTextRange
operator-(const wxRichTextRange
& range
) const;
1023 Assigns @a range to this range.
1025 void operator=(const wxRichTextRange
& range
);
1028 Returns @true if @a range is the same as this range.
1030 bool operator==(const wxRichTextRange
& range
) const;