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