]> git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/richtextctrl.h
The tree only sets the focus in response to a TVN_SELCHANGINGW event if the tree...
[wxWidgets.git] / docs / doxygen / overviews / richtextctrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtextctrl.h
3 // Purpose: topic overview
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10
11 @page overview_richtextctrl wxRichTextCtrl Overview
12
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_nested_object
21 @li @ref overview_richtextctrl_context_menus
22 @li @ref overview_richtextctrl_roadmap
23
24
25 <hr>
26
27
28 @section overview_richtextctrl_classes Related Classes
29
30 <b>Major classes:</b>
31 wxRichTextCtrl, wxRichTextBuffer, wxRichTextEvent
32
33 <b>Helper classes:</b>
34 wxTextAttr, wxRichTextRange
35
36 <b>File handler classes:</b>
37 wxRichTextFileHandler, wxRichTextHTMLHandler, wxRichTextXMLHandler
38
39 <b>Style classes:</b>
40 wxRichTextCharacterStyleDefinition, wxRichTextParagraphStyleDefinition,
41 wxRichTextListStyleDefinition, wxRichTextStyleSheet
42
43 <b>Additional controls:</b>
44 wxRichTextStyleComboCtrl, wxRichTextStyleListBox, wxRichTextStyleListCtrl
45
46 <b>Printing classes:</b>
47 wxRichTextPrinting, wxRichTextPrintout, wxRichTextHeaderFooterData
48
49 <b>Dialog classes:</b>
50 wxRichTextStyleOrganiserDialog, wxRichTextFormattingDialog,
51 wxSymbolPickerDialog
52
53
54 @section overview_richtextctrl_intro Introduction
55
56 wxRichTextCtrl provides a generic implementation of a rich text editor that can
57 handle different character styles, paragraph formatting, and images. It's aimed
58 at editing 'natural' language text - if you need an editor that supports code
59 editing, wxStyledTextCtrl is a better choice.
60
61 Despite its name, it cannot currently read or write RTF (rich text format)
62 files. Instead, it uses its own XML format, and can also read and write plain
63 text. In future we expect to provide RTF or OpenDocument file capabilities. Custom file formats
64 can be supported by creating additional file handlers and registering them with
65 the control.
66
67 wxRichTextCtrl is largely compatible with the wxTextCtrl API, but extends it
68 where necessary. The control can be used where the native rich text
69 capabilities of wxTextCtrl are not adequate (this is particularly true on
70 Windows) and where more direct access to the content representation is
71 required. It is difficult and inefficient to read the style information in a
72 wxTextCtrl, whereas this information is readily available in wxRichTextCtrl.
73 Since it's written in pure wxWidgets, any customizations you make to
74 wxRichTextCtrl will be reflected on all platforms.
75
76 wxRichTextCtrl supports basic printing via the easy-to-use wxRichTextPrinting
77 class. Creating applications with simple word processing features is simplified
78 with the inclusion of wxRichTextFormattingDialog, a tabbed dialog allowing
79 interactive tailoring of paragraph and character styling. Also provided is the
80 multi-purpose dialog wxRichTextStyleOrganiserDialog that can be used for
81 managing style definitions, browsing styles and applying them, or selecting
82 list styles with a renumber option.
83
84 There are a few disadvantages to using wxRichTextCtrl. It is not native, so
85 does not behave exactly as a native wxTextCtrl, although common editing
86 conventions are followed. Users may miss the built-in spelling correction on
87 Mac OS X, or any special character input that may be provided by the native
88 control. It would also be a poor choice if intended users rely on screen
89 readers that would be not work well with non-native text input implementation.
90 You might mitigate this by providing the choice between wxTextCtrl and
91 wxRichTextCtrl, with fewer features in the former case.
92
93 A good way to understand wxRichTextCtrl's capabilities is to compile and run
94 the sample, @c samples/richtext, and browse the code.
95
96
97 @section overview_richtextctrl_example Code Example
98
99 The following code is an example taken from the sample, and adds text and
100 styles to a rich text control programmatically.
101
102 @code
103 wxRichTextCtrl* richTextCtrl = new wxRichTextCtrl(
104 splitter, wxID_ANY, wxEmptyString, wxDefaultPosition,
105 wxSize(200, 200), wxVSCROLL | wxHSCROLL | wxBORDER_NONE | wxWANTS_CHARS);
106
107 wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL);
108 wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD);
109 wxFont italicFont = wxFont(12, wxROMAN, wxITALIC, wxNORMAL);
110
111 wxFont font(12, wxROMAN, wxNORMAL, wxNORMAL);
112
113 m_richTextCtrl->SetFont(font);
114
115 wxRichTextCtrl& r = richTextCtrl;
116
117 r.BeginSuppressUndo();
118
119 r.BeginParagraphSpacing(0, 20);
120
121 r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
122 r.BeginBold();
123
124 r.BeginFontSize(14);
125 r.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images"));
126 r.EndFontSize();
127 r.Newline();
128
129 r.BeginItalic();
130 r.WriteText(wxT("by Julian Smart"));
131 r.EndItalic();
132
133 r.EndBold();
134
135 r.Newline();
136 r.WriteImage(wxBitmap(zebra_xpm));
137
138 r.EndAlignment();
139
140 r.Newline();
141 r.Newline();
142
143 r.WriteText(wxT("What can you do with this thing? "));
144 r.WriteImage(wxBitmap(smiley_xpm));
145 r.WriteText(wxT(" Well, you can change text "));
146
147 r.BeginTextColour(wxColour(255, 0, 0));
148 r.WriteText(wxT("colour, like this red bit."));
149 r.EndTextColour();
150
151 r.BeginTextColour(wxColour(0, 0, 255));
152 r.WriteText(wxT(" And this blue bit."));
153 r.EndTextColour();
154
155 r.WriteText(wxT(" Naturally you can make things "));
156 r.BeginBold();
157 r.WriteText(wxT("bold "));
158 r.EndBold();
159 r.BeginItalic();
160 r.WriteText(wxT("or italic "));
161 r.EndItalic();
162 r.BeginUnderline();
163 r.WriteText(wxT("or underlined."));
164 r.EndUnderline();
165
166 r.BeginFontSize(14);
167 r.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
168 r.EndFontSize();
169
170 r.WriteText(wxT(" Next we'll show an indented paragraph."));
171
172 r.BeginLeftIndent(60);
173 r.Newline();
174
175 r.WriteText(wxT("Indented paragraph."));
176 r.EndLeftIndent();
177
178 r.Newline();
179
180 r.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
181
182 r.BeginLeftIndent(100, -40);
183 r.Newline();
184
185 r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter."));
186 r.EndLeftIndent();
187
188 r.Newline();
189
190 r.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
191
192 r.BeginNumberedBullet(1, 100, 60);
193 r.Newline();
194
195 r.WriteText(wxT("This is my first item. Note that wxRichTextCtrl doesn't automatically do numbering, but this will be added later."));
196 r.EndNumberedBullet();
197
198 r.BeginNumberedBullet(2, 100, 60);
199 r.Newline();
200
201 r.WriteText(wxT("This is my second item."));
202 r.EndNumberedBullet();
203
204 r.Newline();
205
206 r.WriteText(wxT("The following paragraph is right-indented:"));
207
208 r.BeginRightIndent(200);
209 r.Newline();
210
211 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."));
212 r.EndRightIndent();
213
214 r.Newline();
215
216 wxArrayInt tabs;
217 tabs.Add(400);
218 tabs.Add(600);
219 tabs.Add(800);
220 tabs.Add(1000);
221 wxTextAttr attr;
222 attr.SetFlags(wxTEXT_ATTR_TABS);
223 attr.SetTabs(tabs);
224 r.SetDefaultStyle(attr);
225
226 r.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
227
228 r.Newline();
229 r.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
230
231 r.BeginSymbolBullet(wxT('*'), 100, 60);
232 r.Newline();
233 r.WriteText(wxT("Compatibility with wxTextCtrl API"));
234 r.EndSymbolBullet();
235
236 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!"));
237
238 r.EndSuppressUndo();
239 @endcode
240
241
242 @section overview_richtextctrl_starting Starting to Use wxRichTextCtrl
243
244 You need to include @c @<wx/richtext/richtextctrl.h@> in your source, and link
245 with the appropriate wxWidgets library with @c richtext suffix. Put the rich
246 text library first in your link line to avoid unresolved symbols.
247
248 Then you can create a wxRichTextCtrl, with the wxWANT_CHARS style if you want
249 tabs to be processed by the control rather than being used for navigation
250 between controls.
251
252
253 @section overview_richtextctrl_styles Text Styles
254
255 Styling attributes are represented by wxTextAttr, or for more control over
256 attributes such as margins and size, the derived class wxRichTextAttr.
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, Borders,
358 Margins, Background, Size, and List Style.
359 You can select which pages will be shown by supplying flags to the dialog
360 constructor. In a character formatting dialog, typically only the Font page
361 will be shown. In a paragraph formatting dialog, you'll show the Indents @&
362 Spacing, Tabs and Bullets pages. The Style tab is useful when editing a style
363 definition.
364
365 You can customize this dialog by providing your own
366 wxRichTextFormattingDialogFactory object, which tells the formatting dialog how
367 many pages are supported, what their identifiers are, and how to creates the
368 pages.
369
370 wxRichTextStyleOrganiserDialog is a multi-purpose dialog that can be used for
371 managing style definitions, browsing styles and applying them, or selecting
372 list styles with a renumber option. See the sample for usage - it is used for
373 the "Manage Styles" and "Bullets and Numbering" menu commands.
374
375 wxSymbolPickerDialog lets the user insert a symbol from a specified font. It
376 has no wxRichTextCtrl dependencies besides being included in the rich text
377 library.
378
379
380 @section overview_richtextctrl_impl How wxRichTextCtrl is Implemented
381
382 Data representation is handled by wxRichTextBuffer, and a wxRichTextCtrl always
383 has one such buffer.
384
385 The content is represented by a hierarchy of objects, all derived from
386 wxRichTextObject. An object might be an image, a fragment of text, a paragraph,
387 or a further composite object. Objects store a wxRichTextAttr containing style information; a
388 paragraph object can contain both paragraph and character information, but
389 content objects such as text can only store character information. The final
390 style displayed in the control or in a printout is a combination of base style,
391 paragraph style and content (character) style.
392
393 The top of the hierarchy is the buffer, a kind of wxRichTextParagraphLayoutBox,
394 containing further wxRichTextParagraph objects, each of which can include text,
395 images and potentially other types of object.
396
397 Each object maintains a range (start and end position) measured from the start
398 of the main parent object.
399
400 When Layout is called on an object, it is given a size which the object must
401 limit itself to, or one or more flexible directions (vertical or horizontal).
402 So, for example, a centred paragraph is given the page width to play with
403 (minus any margins), but can extend indefinitely in the vertical direction.
404 The implementation of Layout caches the calculated size and position.
405
406 When the buffer is modified, a range is invalidated (marked as requiring
407 layout), so that only the minimum amount of layout is performed.
408
409 A paragraph of pure text with the same style contains just one further object,
410 a wxRichTextPlainText object. When styling is applied to part of this object,
411 the object is decomposed into separate objects, one object for each different
412 character style. So each object within a paragraph always has just one
413 wxTextAttr object to denote its character style. Of course, this can lead to
414 fragmentation after a lot of edit operations, potentially leading to several
415 objects with the same style where just one would do. So a Defragment function
416 is called when updating the control's display, to ensure that the minimum
417 number of objects is used.
418
419 @section overview_richtextctrl_nested_object Nested Objects
420
421 wxRichTextCtrl supports nested objects such as text boxes and tables. To achieve
422 compatibility with the existing API, there is the concept of @e object @e focus.
423 When the user clicks on a nested text box, the object focus is set to that
424 container object so all keyboard input and API functions apply to that
425 container. The application can change the focus using wxRichTextCtrl::SetObjectFocus.
426 Call this function with a @null parameter to set the focus back to the top-level
427 object.
428
429 An event will be sent to the control when the focus changes.
430
431 When the user clicks on the control, wxRichTextCtrl determines which container to set
432 as the current object focus by calling the found container's overrided wxRichTextObject::AcceptsFocus
433 function. For example, although a table is a container, it must not itself be
434 the object focus because there is no text editing at the table level. Instead, a cell
435 within the table must accept the focus.
436
437 Since with nested objects it is not possible to represent a section with merely
438 a start position and an end position, the class wxRichTextSelection is provided
439 which stores multiple ranges (for non-contiguous selections such as table cells) and
440 a pointer to the container object in question. You can pass wxRichTextSelection
441 to wxRichTextCtrl::SetSelection or get an instance of it from wxRichTextCtrl::GetSelection.
442
443 When selecting multiple objects, such as cell tables, the wxRichTextCtrl dragging handler code calls the
444 function wxRichTextObject::HandlesChildSelections to determine whether the children
445 can be individual selections. Currently only table cells can be multiply-selected
446 in this way.
447
448 @section overview_richtextctrl_context_menus Context menus and property dialogs
449
450 There are three ways you can make use of context menus: you can let wxRichTextCtrl handle everything and provide a basic menu;
451 you can set your own context menu using wxRichTextCtrl::SetContextMenu but let wxRichTextCtrl handle showing it and adding property items;
452 or you can override the default context menu behaviour by adding a context menu event handler
453 to your class in the normal way.
454
455 If you right-click over a text box in cell in a table, you may want to edit the properties of
456 one of these objects - but which properties will you be editing?
457
458 Well, the default behaviour allows up to three property-editing menu items simultaneously - for the object clicked on,
459 the container of that object, and the container's parent (depending on whether any of these
460 objects return @true from their wxRichTextObject::CanEditProperties functions).
461 If you supply a context menu, add a property command item using the wxID_RICHTEXT_PROPERTIES1 identifier,
462 so that wxRichTextCtrl can find the position to add command items. The object should
463 tell the control what label to use by returning a string from wxRichTextObject::GetPropertiesMenuLabel.
464
465 Since there may be several property-editing commands showing, it is recommended that you don't
466 include the word Properties - just the name of the object, such as Text Box or Table.
467
468 @section overview_richtextctrl_roadmap Development Roadmap
469
470 @subsection overview_richtextctrl_roadmap_bugs Bugs
471
472 This is an incomplete list of bugs.
473
474 @li Moving the caret up at the beginning of a line sometimes incorrectly
475 positions the caret.
476 @li As the selection is expanded, the text jumps slightly due to kerning
477 differences between drawing a single text string versus drawing several
478 fragments separately. This could be improved by using
479 wxDC::GetPartialTextExtents to calculate exactly where the separate
480 fragments should be drawn. Note that this problem also applies to
481 separation of text fragments due to difference in their attributes.
482
483 @subsection overview_richtextctrl_roadmap_features Features
484
485 This is a list of some of the features that have yet to be implemented. Help
486 with them will be appreciated.
487
488 @li support for composite objects in some functions where it's not yet implemented, for example ApplyStyleSheet
489 @li Table API enhancements and dialogs; improved table layout especially row spans and fitting
490 @li Conversion from HTML, and a rewrite of the HTML output handler that includes CSS,
491 tables, text boxes, and floating images, in addition to a simplified-HTML mode for wxHTML compatibility
492 @li Open Office input and output
493 @li RTF input and output
494 @li A ruler control
495 @li Standard editing toolbars
496 @li Bitmap bullets
497 @li Justified text, in print/preview at least
498 @li scaling: either everything scaled, or rendering using a custom reference point size and an optional dimension scale
499
500 There are also things that could be done to take advantage of the underlying
501 text capabilities of the platform; higher-level text formatting APIs are
502 available on some platforms, such as Mac OS X, and some of translation from
503 high level to low level wxDC API is unnecessary. However this would require
504 additions to the wxWidgets API.
505
506 */
507