]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/latex/wx/richtextoverview.tex
fix LaTeX error (escape underscores); don't abuse bold face
[wxWidgets.git] / docs / latex / wx / richtextoverview.tex
... / ...
CommitLineData
1\section{wxRichTextCtrl overview}\label{wxrichtextctrloverview}
2
3{\bf Major classes:} \helpref{wxRichTextCtrl}{wxrichtextctrl}, \helpref{wxRichTextBuffer}{wxrichtextbuffer}, \helpref{wxRichTextEvent}{wxrichtextevent}
4
5{\bf Helper classes:} \helpref{wxTextAttr}{wxtextattr}, \helpref{wxRichTextRange}{wxrichtextrange}
6
7{\bf File handler classes:} \helpref{wxRichTextFileHandler}{wxrichtextfilehandler}, \helpref{wxRichTextHTMLHandler}{wxrichtexthtmlhandler},
8\helpref{wxRichTextXMLHandler}{wxrichtextxmlhandler}
9
10{\bf Style classes:} \helpref{wxRichTextCharacterStyleDefinition}{wxrichtextcharacterstyledefinition},
11\helpref{wxRichTextParagraphStyleDefinition}{wxrichtextparagraphstyledefinition},
12\helpref{wxRichTextListStyleDefinition}{wxrichtextliststyledefinition},
13\helpref{wxRichTextStyleSheet}{wxrichtextstylesheet}
14
15{\bf Additional controls:} \helpref{wxRichTextStyleComboCtrl}{wxrichtextstylecomboctrl},
16\helpref{wxRichTextStyleListBox}{wxrichtextstylelistbox},
17\helpref{wxRichTextStyleListCtrl}{wxrichtextstylelistctrl}
18
19{\bf Printing classes:} \helpref{wxRichTextPrinting}{wxrichtextprinting},
20\helpref{wxRichTextPrintout}{wxrichtextprintout},
21\helpref{wxRichTextHeaderFooterData}{wxrichtextheaderfooterdata}
22
23{\bf Dialog classes:} \helpref{wxRichTextStyleOrganiserDialog}{wxrichtextstyleorganiserdialog},
24\helpref{wxRichTextFormattingDialog}{wxrichtextformattingdialog},
25\helpref{wxSymbolPickerDialog}{wxsymbolpickerdialog}
26
27wxRichTextCtrl provides a generic implementation of a rich text editor that can handle different character
28styles, paragraph formatting, and images. It's aimed at editing 'natural' language text - if you need an editor
29that supports code editing, wxStyledTextCtrl is a better choice.
30
31Despite its name, it cannot currently read or write RTF (rich text format) files. Instead, it
32uses its own XML format, and can also read and write plain text. In future we expect to provide
33RTF file capabilities. Custom file formats can be supported by creating additional
34file handlers and registering them with the control.
35
36wxRichTextCtrl is largely compatible with the wxTextCtrl API, but extends it where necessary.
37The control can be used where the native rich text capabilities of wxTextCtrl are not
38adequate (this is particularly true on Windows) and where more direct access to
39the content representation is required. It is difficult and inefficient to read
40the style information in a wxTextCtrl, whereas this information is readily
41available in wxRichTextCtrl. Since it's written in pure wxWidgets, any customizations
42you make to wxRichTextCtrl will be reflected on all platforms.
43
44wxRichTextCtrl supports basic printing via the easy-to-use \helpref{wxRichTextPrinting}{wxrichtextprinting} class.
45Creating applications with simple word processing features is simplified with the inclusion of\rtfsp
46\helpref{wxRichTextFormattingDialog}{wxrichtextformattingdialog}, a tabbed dialog allowing
47interactive tailoring of paragraph and character styling. Also provided is the multi-purpose dialog\rtfsp
48\helpref{wxRichTextStyleOrganiserDialog}{wxrichtextstyleorganiserdialog} that can be used for
49managing style definitions, browsing styles and applying them, or selecting list styles with
50a renumber option.
51
52There are a few disadvantages to using wxRichTextCtrl. It is not native,
53so does not behave exactly as a native wxTextCtrl, although common editing conventions
54are followed. Users may miss the built-in spelling correction on Mac OS X, or any
55special character input that may be provided by the native control. It would also
56be a poor choice if intended users rely on screen readers that would be not work well
57with non-native text input implementation. You might mitigate this by providing
58the choice between wxTextCtrl and wxRichTextCtrl, with fewer features in the
59former case.
60
61A good way to understand wxRichTextCtrl's capabilities is to compile and run the
62sample, {\tt samples/richtext}, and browse the code. The following screenshot shows the sample in action:
63
64$$\image{8cm;0cm}{richtextctrl.gif}$$
65
66\wxheading{Example}\label{wxrichtextctrlexample}
67
68The following code is taken from the sample, and adds text and styles to a rich text control programmatically.
69
70{\small
71\begin{verbatim}
72 wxRichTextCtrl* richTextCtrl = new wxRichTextCtrl(splitter, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, 200), wxVSCROLL|wxHSCROLL|wxBORDER_NONE|wxWANTS_CHARS);
73
74 wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL);
75 wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD);
76 wxFont italicFont = wxFont(12, wxROMAN, wxITALIC, wxNORMAL);
77
78 wxFont font(12, wxROMAN, wxNORMAL, wxNORMAL);
79
80 m_richTextCtrl->SetFont(font);
81
82 wxRichTextCtrl& r = richTextCtrl;
83
84 r.BeginSuppressUndo();
85
86 r.BeginParagraphSpacing(0, 20);
87
88 r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
89 r.BeginBold();
90
91 r.BeginFontSize(14);
92 r.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images"));
93 r.EndFontSize();
94 r.Newline();
95
96 r.BeginItalic();
97 r.WriteText(wxT("by Julian Smart"));
98 r.EndItalic();
99
100 r.EndBold();
101
102 r.Newline();
103 r.WriteImage(wxBitmap(zebra_xpm));
104
105 r.EndAlignment();
106
107 r.Newline();
108 r.Newline();
109
110 r.WriteText(wxT("What can you do with this thing? "));
111 r.WriteImage(wxBitmap(smiley_xpm));
112 r.WriteText(wxT(" Well, you can change text "));
113
114 r.BeginTextColour(wxColour(255, 0, 0));
115 r.WriteText(wxT("colour, like this red bit."));
116 r.EndTextColour();
117
118 r.BeginTextColour(wxColour(0, 0, 255));
119 r.WriteText(wxT(" And this blue bit."));
120 r.EndTextColour();
121
122 r.WriteText(wxT(" Naturally you can make things "));
123 r.BeginBold();
124 r.WriteText(wxT("bold "));
125 r.EndBold();
126 r.BeginItalic();
127 r.WriteText(wxT("or italic "));
128 r.EndItalic();
129 r.BeginUnderline();
130 r.WriteText(wxT("or underlined."));
131 r.EndUnderline();
132
133 r.BeginFontSize(14);
134 r.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
135 r.EndFontSize();
136
137 r.WriteText(wxT(" Next we'll show an indented paragraph."));
138
139 r.BeginLeftIndent(60);
140 r.Newline();
141
142 r.WriteText(wxT("Indented paragraph."));
143 r.EndLeftIndent();
144
145 r.Newline();
146
147 r.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
148
149 r.BeginLeftIndent(100, -40);
150 r.Newline();
151
152 r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter."));
153 r.EndLeftIndent();
154
155 r.Newline();
156
157 r.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
158
159 r.BeginNumberedBullet(1, 100, 60);
160 r.Newline();
161
162 r.WriteText(wxT("This is my first item. Note that wxRichTextCtrl doesn't automatically do numbering, but this will be added later."));
163 r.EndNumberedBullet();
164
165 r.BeginNumberedBullet(2, 100, 60);
166 r.Newline();
167
168 r.WriteText(wxT("This is my second item."));
169 r.EndNumberedBullet();
170
171 r.Newline();
172
173 r.WriteText(wxT("The following paragraph is right-indented:"));
174
175 r.BeginRightIndent(200);
176 r.Newline();
177
178 r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable."));
179 r.EndRightIndent();
180
181 r.Newline();
182
183 wxArrayInt tabs;
184 tabs.Add(400);
185 tabs.Add(600);
186 tabs.Add(800);
187 tabs.Add(1000);
188 wxTextAttr attr;
189 attr.SetFlags(wxTEXT_ATTR_TABS);
190 attr.SetTabs(tabs);
191 r.SetDefaultStyle(attr);
192
193 r.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
194
195 r.Newline();
196 r.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
197
198 r.BeginSymbolBullet(wxT('*'), 100, 60);
199 r.Newline();
200 r.WriteText(wxT("Compatibility with wxTextCtrl API"));
201 r.EndSymbolBullet();
202
203 r.WriteText(wxT("Note: this sample content was generated programmatically from within the MyFrame constructor in the demo. The images were loaded from inline XPMs. Enjoy wxRichTextCtrl!"));
204
205 r.EndSuppressUndo();
206\end{verbatim}
207}
208
209\subsection{Programming with wxRichTextCtrl}
210
211\subsubsection{Starting to use wxRichTextCtrl}
212
213You need to include {\tt <wx/richtext/richtextctrl.h>} in your source, and link
214with the appropriate wxWidgets library with {\tt richtext} suffix. Put the rich text
215library first in your link line to avoid unresolved symbols.
216
217Then you can create a wxRichTextCtrl, with the wxWANT\_CHARS style if you want tabs to
218be processed by the control rather than being used for navigation between controls.
219
220\subsubsection{wxRichTextCtrl and styles}
221
222Styling attributes are represented by \helpref{wxTextAttr}{wxtextattr}.
223
224When setting a style, the flags of the attribute object determine which
225attributes are applied. When querying a style, the passed flags are ignored
226except (optionally) to determine whether attributes should be retrieved from
227character content or from the paragraph object.
228
229wxRichTextCtrl takes a layered approach to styles, so that different parts of
230the content may be responsible for contributing different attributes to the final
231style you see on the screen.
232
233There are four main notions of style within a control:
234
235\begin{enumerate}\itemsep=0pt
236\item {\bf Basic style:} the fundamental style of a control, onto which any other
237styles are layered. It provides default attributes, and changing the basic style
238may immediately change the look of the content depending on what other styles
239the content uses. Calling wxRichTextCtrl::SetFont changes the font for the basic style.
240The basic style is set with \helpref{wxRichTextCtrl::SetBasicStyle}{wxrichtextctrlsetbasicstyle}.
241\item {\bf Paragraph style:} each paragraph has attributes that are set independently
242from other paragraphs and independently from the content within the paragraph.
243Normally, these attributes are paragraph-related, such as alignment and indentation,
244but it is possible to set character attributes too.
245The paragraph style can be set independently of its content by passing wxRICHTEXT\_SETSTYLE\_PARAGRAPHS\_ONLY
246to \helpref{wxRichTextCtrl::SetStyleEx}{wxrichtextctrlsetstyleex}.
247\item {\bf Character style:} characters within each paragraph can have attributes.
248A single character, or a run of characters, can have a particular set of attributes.
249The character style can be with \helpref{wxRichTextCtrl::SetStyle}{wxrichtextctrlsetstyle} or
250\helpref{wxRichTextCtrl::SetStyleEx}{wxrichtextctrlsetstyleex}.
251\item {\bf Default style:} this is the `current' style that determines the
252style of content that is subsequently typed, pasted or programmatically inserted.
253The default style is set with \helpref{wxRichTextCtrl::SetDefaultStyle}{wxrichtextctrlsetdefaultstyle}.
254\end{enumerate}
255
256What you see on the screen is the dynamically {\it combined} style, found by merging
257the first three of the above style types (the fourth is only a guide for future content
258insertion and therefore does not affect the currently displayed content).
259
260To make all this more concrete, here are examples of where you might set these different
261styles:
262
263\begin{enumerate}\itemsep=0pt
264\item You might set the {\bf basic style} to have a Times Roman font in 12 point,
265left-aligned, with two millimetres of spacing after each paragraph.
266\item You might set the {\bf paragraph style} (for one particular paragraph) to
267be centred.
268\item You might set the {\bf character style} of one particular word to bold.
269\item You might set the {\bf default style} to be underlined, for subsequent
270inserted text.
271\end{enumerate}
272
273Naturally you can do any of these things either using your own UI, or programmatically.
274
275The basic wxTextCtrl doesn't make the same distinctions as wxRichTextCtrl regarding
276attribute storage. So we need finer control when setting and retrieving
277attributes. \helpref{wxRichTextCtrl::SetStyleEx}{wxrichtextctrlsetstyleex} takes a {\it flags} parameter:
278
279\begin{itemize}\itemsep=0pt
280\item wxRICHTEXT\_SETSTYLE\_OPTIMIZE specifies that the style should be changed only if
281the combined attributes are different from the attributes for the current object. This is important when
282applying styling that has been edited by the user, because he has just edited the {\it combined} (visible)
283style, and wxRichTextCtrl wants to leave unchanged attributes associated with their original objects
284instead of applying them to both paragraph and content objects.
285\item wxRICHTEXT\_SETSTYLE\_PARAGRAPHS\_ONLY specifies that only paragraph objects within the given range
286should take on the attributes.
287\item wxRICHTEXT\_SETSTYLE\_CHARACTERS\_ONLY specifies that only content objects (text or images) within the given range
288should take on the attributes.
289\item wxRICHTEXT\_SETSTYLE\_WITH\_UNDO specifies that the operation should be undoable.
290\end{itemize}
291
292It's great to be able to change arbitrary attributes in a wxRichTextCtrl, but
293it can be unwieldy for the user or programmer to set attributes separately. Word processors have collections
294of styles that you can tailor or use as-is, and this means that you can set a heading with one click
295instead of marking text in bold, specifying a large font size, and applying a certain
296paragraph spacing and alignment for every such heading. Similarly,
297wxWidgets provides a class called \helpref{wxRichTextStyleSheet}{wxrichtextstylesheet} which manages style definitions
298(\helpref{wxRichTextParagraphStyleDefinition}{wxrichtextparagraphstyledefinition}, \helpref{wxRichTextListStyleDefinition}{wxrichtextliststyledefinition} and \helpref{wxRichTextCharacterStyleDefinition}{wxrichtextcharacterstyledefinition}).
299Once you have added definitions to a style sheet and associated it with a wxRichTextCtrl,
300you can apply a named definition to a range of text. The classes \helpref{wxRichTextStyleComboCtrl}{wxrichtextstylecomboctrl}\rtfsp
301and \helpref{wxRichTextStyleListBox}{wxrichtextstylelistbox} can be used to present the user with a list
302of styles in a sheet, and apply them to the selected text.
303
304You can reapply a style sheet to the contents of the control, by calling \helpref{wxRichTextCtrl::ApplyStyleSheet}{wxrichtextctrlapplystylesheet}.
305This is useful if the style definitions have changed, and you want the content to reflect this.
306It relies on the fact that when you apply a named style, the style definition name is recorded in the
307content. So ApplyStyleSheet works by finding the paragraph attributes with style names and re-applying the definition's
308attributes to the paragraph. Currently, this works with paragraph and list style definitions only.
309
310\subsection{wxRichTextCtrl dialogs}\label{wxrichtextctrldialogs}
311
312wxRichTextCtrl comes with standard dialogs to make it easier to implement
313text editing functionality.
314
315\helpref{wxRichTextFormattingDialog}{wxrichtextformattingdialog} can be used
316for character or paragraph formatting, or a combination of both. It's a wxPropertySheetDialog
317with the following available tabs: Font, Indents \& Spacing, Tabs, Bullets, Style, and List Style.
318You can select which pages will be shown by supplying flags to the dialog constructor.
319In a character formatting dialog, typically only the Font page will be shown.
320In a paragraph formatting dialog, you'll show the Indents \& Spacing, Tabs and Bullets
321pages. The Style tab is useful when editing a style definition.
322
323You can customize this dialog by providing your own wxRichTextFormattingDialogFactory
324object, which tells the formatting dialog how many pages are supported, what their identifiers
325are, and how to creates the pages.
326
327\helpref{wxRichTextStyleOrganiserDialog}{wxrichtextstyleorganiserdialog} is a multi-purpose dialog
328that can be used for managing style definitions, browsing styles and applying them, or selecting list styles with
329a renumber option. See the sample for usage - it is used for the "Manage Styles" and "Bullets and Numbering"
330menu commands.
331
332\helpref{wxSymbolPickerDialog}{wxsymbolpickerdialog} lets the user insert a symbol from
333a specified font. It has no wxRichTextCtrl dependencies besides being included in
334the rich text library.
335
336\subsection{How wxRichTextCtrl is implemented}
337
338Data representation is handled by wxRichTextBuffer, and a wxRichTextCtrl
339always has one such buffer.
340
341The content is represented by a hierarchy of objects, all derived from
342wxRichTextObject. An object might be an image, a fragment of text, a paragraph,
343or a whole buffer. Objects store a wxTextAttr containing style information;
344a paragraph object can contain both paragraph and character information, but
345content objects such as text can only store character information. The final
346style displayed in the control or in a printout is a combination of base
347style, paragraph style and content (character) style.
348
349The top of the hierarchy is the buffer, a kind of wxRichTextParagraphLayoutBox.
350containing further wxRichTextParagraph objects, each of which can include text,
351images and potentially other types of object.
352
353Each object maintains a range (start and end position) measured
354from the start of the main parent object.
355
356When Layout is called on an object, it is given a size which the object
357must limit itself to, or one or more flexible directions (vertical
358or horizontal). So, for example, a centred paragraph is given the page
359width to play with (minus any margins), but can extend indefinitely
360in the vertical direction. The implementation of Layout caches the calculated
361size and position.
362
363When the buffer is modified, a range is invalidated (marked as requiring
364layout), so that only the minimum amount of layout is performed.
365
366A paragraph of pure text with the same style contains just one further
367object, a wxRichTextPlainText object. When styling is applied to part of
368this object, the object is decomposed into separate objects, one object
369for each different character style. So each object within a paragraph always has
370just one wxTextAttr object to denote its character style. Of course, this can
371lead to fragmentation after a lot of edit operations, potentially leading
372to several objects with the same style where just one would do. So
373a Defragment function is called when updating the control's display, to ensure that
374the minimum number of objects is used.
375
376\subsection{wxRichTextCtrl roadmap}
377
378\wxheading{Bugs}
379
380This is an incomplete list of bugs.
381
382\begin{itemize}\itemsep=0pt
383\item Moving the caret up at the beginning of a line sometimes incorrectly positions the
384caret.
385\item As the selection is expanded, the text jumps slightly due to kerning differences between
386drawing a single text string versus drawing several fragments separately. This could
387be improved by using wxDC::GetPartialTextExtents to calculate exactly where the separate fragments
388should be drawn. Note that this problem also applies to separation of text fragments due to difference in their attributes.
389\end{itemize}
390
391\wxheading{Features}
392
393This is a list of some of the features that have yet to be implemented. Help with them will be appreciated.
394
395\begin{itemize}\itemsep=0pt
396\item RTF input and output
397\item Conversion from HTML
398\item Open Office input and output
399\item Floating images, with content wrapping around them
400\item A ruler control
401\item Standard editing toolbars
402\item Tables
403\item Bitmap bullets
404\item Borders
405\item Text frames
406\item Justified text, in print/preview at least
407\end{itemize}
408
409There are also things that could be done to take advantage of the underlying text capabilities of the platform;
410higher-level text formatting APIs are available on some platforms, such as Mac OS X, and some of translation from
411high level to low level wxDC API is unnecessary. However this would require additions to the wxWidgets API.
412