Added some missing newlines to ends of files (necessary for linear RTF)
[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 You need to include {\tt <wx/richtext/richtextctrl.h>} in your source, and link
195 with the appropriate wxWidgets library with {\tt richtext} suffix. Put the rich text
196 library first in your link line to avoid unresolved symbols.
197
198 Then you can create a wxRichTextCtrl, with the wxWANT\_CHARS style if you want tabs to
199 be processed by the control rather than being used for navigation between controls.
200
201 It's helpful to have a model of how styling works. Any piece of text can have its
202 style changed, but there also two global notions of style. The control's {\it basic} style
203 is the fundamental style for the whole control, to which other character and paragraph styles are
204 applied. For example, you can change the control's overall font by either calling SetBasicStyle with
205 the appropriate font style, or by calling SetFont.
206
207 The {\it default} style, on the other hand, is applied to subsequently inserted
208 content. You might click on a Bold formatting tool, which sets bold as one of the default
209 attributes, and typing will appear in bold. Then when you select Italic, both
210 bold and italic attributes are applied as you type. The default attribute
211 is set with \helpref{SetDefaultStyle}{wxrichtextctrlsetdefaultstyle}.
212
213 (To be finished.)
214
215 \subsection{How wxRichTextCtrl is implemented}
216
217 Data representation is handled by wxRichTextBuffer, and a wxRichTextCtrl
218 always has one such buffer.
219
220 The content is represented by a hierarchy of objects, all derived from
221 wxRichTextObject. An object might be an image, a fragment of text, a paragraph,
222 or a whole buffer. Objects store a wxRichTextAttr containing style information;
223 although it contains both paragraph formatting and character style, the
224 paragraph style information is ignored by children of a paragraph (only
225 character style is relevant to these objects).
226
227 The top of the hierarchy is the buffer, a kind of wxRichTextParagraphLayoutBox.
228 containing further wxRichTextParagraph objects, each of which can include text and
229 images.
230
231 Each object maintains a range (start and end position) measured
232 from the start of the main parent box.
233
234 When Layout is called on an object, it is given a size which the object
235 must limit itself to, or one or more flexible directions (vertical
236 or horizontal). So, for example, a centred paragraph is given the page
237 width to play with (minus any margins), but can extend indefinitely
238 in the vertical direction. The implementation of Layout caches the calculated
239 size and position.
240
241 When the buffer is modified, a range is invalidated (marked as requiring
242 layout), so that only the minimum amount of layout is performed.
243
244 A paragraph of pure text with the same style contains just one further
245 object, a wxRichTextPlainText object. When styling is applied to part of
246 this object, the object is decomposed into separate objects, one object
247 for each different character style. So each object within a paragraph always has
248 just one wxRichTextAttr object to denote its character style. Of course, this can
249 lead to fragmentation after a lot of edit operations, potentially leading
250 to several objects with the same style where just one would do. So
251 a Defragment function is called when updating the control's display, to ensure that
252 the minimum number of objects is used.
253
254 (To be finished.)
255
256 \subsection{wxRichTextCtrl roadmap}
257
258 \wxheading{Bugs}
259
260 This is an incomplete list of bugs.
261
262 \begin{itemize}
263 \item Moving the caret up at the beginning of a line sometimes incorrectly positions the
264 caret.
265 \end{itemize}
266
267 \wxheading{Features}
268
269 This is a list of some of the features that have yet to be implemented. Help with them will be appreciated.
270
271 \begin{itemize}
272 \item Printing
273 \item RTF input and output
274 \item Floating images, with content wrapping around them
275 \item A ruler control
276 \item Standard editing toolbars
277 \item Automatic list numbering
278 \item Tables
279 \item Text frames
280 \item Add ability to show images in wxHTML output (currently uses embedded data suitable only for real browsers)
281 \item More complete stylesheet viewer, plus style sheet editing dialogs
282 \item Ability to read and write style sheets
283 \end{itemize}
284
285 There are also things that could be done to take advantage of the underlying text capabilities of the platform;
286 higher-level text formatting APIs are available on some platforms, such as Mac OS X, and some of translation from
287 high level to low level wxDC API is unnecessary. However this would require additions to the wxWidgets API.
288