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