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