richtextctrl.h 21 KB

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