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