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