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