]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/richtextoverview.tex
remove C++ comment
[wxWidgets.git] / docs / latex / wx / richtextoverview.tex
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
27 wxRichTextCtrl provides a generic implementation of a rich text editor that can handle different character
28 styles, paragraph formatting, and images. It's aimed at editing 'natural' language text - if you need an editor
29 that supports code editing, wxStyledTextCtrl is a better choice.
30
31 Despite its name, it cannot currently read or write RTF (rich text format) files. Instead, it
32 uses its own XML format, and can also read and write plain text. In future we expect to provide
33 RTF file capabilities. Custom file formats can be supported by creating additional
34 file handlers and registering them with the control.
35
36 wxRichTextCtrl is largely compatible with the wxTextCtrl API, but extends it where necessary.
37 The control can be used where the native rich text capabilities of wxTextCtrl are not
38 adequate (this is particularly true on Windows) and where more direct access to
39 the content representation is required. It is difficult and inefficient to read
40 the style information in a wxTextCtrl, whereas this information is readily
41 available in wxRichTextCtrl. Since it's written in pure wxWidgets, any customizations
42 you make to wxRichTextCtrl will be reflected on all platforms.
43
44 wxRichTextCtrl supports basic printing via the easy-to-use \helpref{wxRichTextPrinting}{wxrichtextprinting} class.
45 Creating applications with simple word processing features is simplified with the inclusion of\rtfsp
46 \helpref{wxRichTextFormattingDialog}{wxrichtextformattingdialog}, a tabbed dialog allowing
47 interactive tailoring of paragraph and character styling. Also provided is the multi-purpose dialog\rtfsp
48 \helpref{wxRichTextStyleOrganiserDialog}{wxrichtextstyleorganiserdialog} that can be used for
49 managing style definitions, browsing styles and applying them, or selecting list styles with
50 a renumber option.
51
52 There are a few disadvantages to using wxRichTextCtrl. It is not native,
53 so does not behave exactly as a native wxTextCtrl, although common editing conventions
54 are followed. Users may miss the built-in spelling correction on Mac OS X, or any
55 special character input that may be provided by the native control. It would also
56 be a poor choice if intended users rely on screen readers that would be not work well
57 with non-native text input implementation. You might mitigate this by providing
58 the choice between wxTextCtrl and wxRichTextCtrl, with fewer features in the
59 former case.
60
61 A good way to understand wxRichTextCtrl's capabilities is to compile and run the
62 sample, {\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
68 The 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
213 You need to include {\tt <wx/richtext/richtextctrl.h>} in your source, and link
214 with the appropriate wxWidgets library with {\tt richtext} suffix. Put the rich text
215 library first in your link line to avoid unresolved symbols.
216
217 Then you can create a wxRichTextCtrl, with the wxWANT\_CHARS style if you want tabs to
218 be processed by the control rather than being used for navigation between controls.
219
220 \subsubsection{wxRichTextCtrl and styles}
221
222 Styling attributes are represented by \helpref{wxTextAttr}{wxtextattr}.
223
224 When setting a style, the flags of the attribute object determine which
225 attributes are applied. When querying a style, the passed flags are ignored
226 except (optionally) to determine whether attributes should be retrieved from
227 character content or from the paragraph object.
228
229 wxRichTextCtrl takes a layered approach to styles, so that different parts of
230 the content may be responsible for contributing different attributes to the final
231 style you see on the screen.
232
233 There 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
237 styles are layered. It provides default attributes, and changing the basic style
238 may immediately change the look of the content depending on what other styles
239 the content uses. Calling wxRichTextCtrl::SetFont changes the font for the basic style.
240 The basic style is set with \helpref{wxRichTextCtrl::SetBasicStyle}{wxrichtextctrlsetbasicstyle}.
241 \item {\bf Paragraph style:} each paragraph has attributes that are set independently
242 from other paragraphs and independently from the content within the paragraph.
243 Normally, these attributes are paragraph-related, such as alignment and indentation,
244 but it is possible to set character attributes too.
245 The paragraph style can be set independently of its content by passing wxRICHTEXT\_SETSTYLE\_PARAGRAPHS\_ONLY
246 to \helpref{wxRichTextCtrl::SetStyleEx}{wxrichtextctrlsetstyleex}.
247 \item {\bf Character style:} characters within each paragraph can have attributes.
248 A single character, or a run of characters, can have a particular set of attributes.
249 The 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
252 style of content that is subsequently typed, pasted or programmatically inserted.
253 The default style is set with \helpref{wxRichTextCtrl::SetDefaultStyle}{wxrichtextctrlsetdefaultstyle}.
254 \end{enumerate}
255
256 What you see on the screen is the dynamically {\it combined} style, found by merging
257 the first three of the above style types (the fourth is only a guide for future content
258 insertion and therefore does not affect the currently displayed content).
259
260 To make all this more concrete, here are examples of where you might set these different
261 styles:
262
263 \begin{enumerate}\itemsep=0pt
264 \item You might set the {\bf basic style} to have a Times Roman font in 12 point,
265 left-aligned, with two millimetres of spacing after each paragraph.
266 \item You might set the {\bf paragraph style} (for one particular paragraph) to
267 be 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
270 inserted text.
271 \end{enumerate}
272
273 Naturally you can do any of these things either using your own UI, or programmatically.
274
275 The basic wxTextCtrl doesn't make the same distinctions as wxRichTextCtrl regarding
276 attribute storage. So we need finer control when setting and retrieving
277 attributes. \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
281 the combined attributes are different from the attributes for the current object. This is important when
282 applying styling that has been edited by the user, because he has just edited the {\it combined} (visible)
283 style, and wxRichTextCtrl wants to leave unchanged attributes associated with their original objects
284 instead of applying them to both paragraph and content objects.
285 \item wxRICHTEXT\_SETSTYLE\_PARAGRAPHS\_ONLY specifies that only paragraph objects within the given range
286 should take on the attributes.
287 \item wxRICHTEXT\_SETSTYLE\_CHARACTERS\_ONLY specifies that only content objects (text or images) within the given range
288 should take on the attributes.
289 \item wxRICHTEXT\_SETSTYLE\_WITH\_UNDO specifies that the operation should be undoable.
290 \end{itemize}
291
292 It's great to be able to change arbitrary attributes in a wxRichTextCtrl, but
293 it can be unwieldy for the user or programmer to set attributes separately. Word processors have collections
294 of styles that you can tailor or use as-is, and this means that you can set a heading with one click
295 instead of marking text in bold, specifying a large font size, and applying a certain
296 paragraph spacing and alignment for every such heading. Similarly,
297 wxWidgets provides a class called \helpref{wxRichTextStyleSheet}{wxrichtextstylesheet} which manages style definitions
298 (\helpref{wxRichTextParagraphStyleDefinition}{wxrichtextparagraphstyledefinition}, \helpref{wxRichTextListStyleDefinition}{wxrichtextliststyledefinition} and \helpref{wxRichTextCharacterStyleDefinition}{wxrichtextcharacterstyledefinition}).
299 Once you have added definitions to a style sheet and associated it with a wxRichTextCtrl,
300 you can apply a named definition to a range of text. The classes \helpref{wxRichTextStyleComboCtrl}{wxrichtextstylecomboctrl}\rtfsp
301 and \helpref{wxRichTextStyleListBox}{wxrichtextstylelistbox} can be used to present the user with a list
302 of styles in a sheet, and apply them to the selected text.
303
304 You can reapply a style sheet to the contents of the control, by calling \helpref{wxRichTextCtrl::ApplyStyleSheet}{wxrichtextctrlapplystylesheet}.
305 This is useful if the style definitions have changed, and you want the content to reflect this.
306 It relies on the fact that when you apply a named style, the style definition name is recorded in the
307 content. So ApplyStyleSheet works by finding the paragraph attributes with style names and re-applying the definition's
308 attributes to the paragraph. Currently, this works with paragraph and list style definitions only.
309
310 \subsection{wxRichTextCtrl dialogs}\label{wxrichtextctrldialogs}
311
312 wxRichTextCtrl comes with standard dialogs to make it easier to implement
313 text editing functionality.
314
315 \helpref{wxRichTextFormattingDialog}{wxrichtextformattingdialog} can be used
316 for character or paragraph formatting, or a combination of both. It's a wxPropertySheetDialog
317 with the following available tabs: Font, Indents \& Spacing, Tabs, Bullets, Style, and List Style.
318 You can select which pages will be shown by supplying flags to the dialog constructor.
319 In a character formatting dialog, typically only the Font page will be shown.
320 In a paragraph formatting dialog, you'll show the Indents \& Spacing, Tabs and Bullets
321 pages. The Style tab is useful when editing a style definition.
322
323 You can customize this dialog by providing your own wxRichTextFormattingDialogFactory
324 object, which tells the formatting dialog how many pages are supported, what their identifiers
325 are, and how to creates the pages.
326
327 \helpref{wxRichTextStyleOrganiserDialog}{wxrichtextstyleorganiserdialog} is a multi-purpose dialog
328 that can be used for managing style definitions, browsing styles and applying them, or selecting list styles with
329 a renumber option. See the sample for usage - it is used for the "Manage Styles" and "Bullets and Numbering"
330 menu commands.
331
332 \helpref{wxSymbolPickerDialog}{wxsymbolpickerdialog} lets the user insert a symbol from
333 a specified font. It has no wxRichTextCtrl dependencies besides being included in
334 the rich text library.
335
336 \subsection{How wxRichTextCtrl is implemented}
337
338 Data representation is handled by wxRichTextBuffer, and a wxRichTextCtrl
339 always has one such buffer.
340
341 The content is represented by a hierarchy of objects, all derived from
342 wxRichTextObject. An object might be an image, a fragment of text, a paragraph,
343 or a whole buffer. Objects store a wxTextAttr containing style information;
344 a paragraph object can contain both paragraph and character information, but
345 content objects such as text can only store character information. The final
346 style displayed in the control or in a printout is a combination of base
347 style, paragraph style and content (character) style.
348
349 The top of the hierarchy is the buffer, a kind of wxRichTextParagraphLayoutBox.
350 containing further wxRichTextParagraph objects, each of which can include text,
351 images and potentially other types of object.
352
353 Each object maintains a range (start and end position) measured
354 from the start of the main parent object.
355
356 When Layout is called on an object, it is given a size which the object
357 must limit itself to, or one or more flexible directions (vertical
358 or horizontal). So, for example, a centred paragraph is given the page
359 width to play with (minus any margins), but can extend indefinitely
360 in the vertical direction. The implementation of Layout caches the calculated
361 size and position.
362
363 When the buffer is modified, a range is invalidated (marked as requiring
364 layout), so that only the minimum amount of layout is performed.
365
366 A paragraph of pure text with the same style contains just one further
367 object, a wxRichTextPlainText object. When styling is applied to part of
368 this object, the object is decomposed into separate objects, one object
369 for each different character style. So each object within a paragraph always has
370 just one wxTextAttr object to denote its character style. Of course, this can
371 lead to fragmentation after a lot of edit operations, potentially leading
372 to several objects with the same style where just one would do. So
373 a Defragment function is called when updating the control's display, to ensure that
374 the minimum number of objects is used.
375
376 \subsection{wxRichTextCtrl roadmap}
377
378 \wxheading{Bugs}
379
380 This 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
384 caret.
385 \item As the selection is expanded, the text jumps slightly due to kerning differences between
386 drawing a single text string versus drawing several fragments separately. This could
387 be improved by using wxDC::GetPartialTextExtents to calculate exactly where the separate fragments
388 should 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
393 This 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
409 There are also things that could be done to take advantage of the underlying text capabilities of the platform;
410 higher-level text formatting APIs are available on some platforms, such as Mac OS X, and some of translation from
411 high level to low level wxDC API is unnecessary. However this would require additions to the wxWidgets API.
412