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