📄 richtextoverview.tex
字号:
\subsubsection{Starting to use wxRichTextCtrl}You need to include {\tt <wx/richtext/richtextctrl.h>} in your source, and linkwith the appropriate wxWidgets library with {\tt richtext} suffix. Put the rich textlibrary first in your link line to avoid unresolved symbols.Then you can create a wxRichTextCtrl, with the wxWANT\_CHARS style if you want tabs tobe processed by the control rather than being used for navigation between controls.\subsubsection{wxRichTextCtrl and styles}Styling attributes are represented by three classes: \helpref{wxTextAttr}{wxtextattr}, \helpref{wxTextAttrEx}{wxtextattrex} and \helpref{wxRichTextAttr}{wxrichtextattr}.wxTextAttr is shared across all controls that are derived from wxTextCtrlBase andcan store basic character and paragraph attributes. wxTextAttrEx derivesfrom wxTextAttr and adds some further attributes that are only supportedby wxRichTextCtrl. Finally, wxRichTextAttr is a more efficient versionof wxTextAttrEx that doesn't use a wxFont object and can be used toquery styles more quickly. wxTextAttrEx and wxRichTextAttr are largelyinterchangeable and have suitable conversion operators between them.When setting a style, the flags of the attribute object determine whichattributes are applied. When querying a style, the passed flags are ignoredexcept (optionally) to determine whether attributes should be retrieved fromcharacter content or from the paragraph object.wxRichTextCtrl takes a layered approach to styles, so that different parts ofthe content may be responsible for contributing different attributes to the finalstyle you see on the screen.There are four main notions of style within a control:\begin{enumerate}\itemsep=0pt\item {\bf Basic style:} the fundamental style of a control, onto which any otherstyles are layered. It provides default attributes, and changing the basic stylemay immediately change the look of the content depending on what other stylesthe content uses. Calling wxRichTextCtrl::SetFont changes the font for the basic style.The basic style is set with \helpref{wxRichTextCtrl::SetBasicStyle}{wxrichtextctrlsetbasicstyle}.\item {\bf Paragraph style:} each paragraph has attributes that are set independentlyfrom other paragraphs and independently from the content within the paragraph.Normally, these attributes are paragraph-related, such as alignment and indentation,but it is possible to set character attributes too.The paragraph style can be set independently of its content by passing wxRICHTEXT\_SETSTYLE\_PARAGRAPHS\_ONLYto \helpref{wxRichTextCtrl::SetStyleEx}{wxrichtextctrlsetstyleex}.\item {\bf Character style:} characters within each paragraph can have attributes.A single character, or a run of characters, can have a particular set of attributes.The character style can be with \helpref{wxRichTextCtrl::SetStyle}{wxrichtextctrlsetstyle} or \helpref{wxRichTextCtrl::SetStyleEx}{wxrichtextctrlsetstyleex}.\item {\bf Default style:} this is the `current' style that determines thestyle of content that is subsequently typed, pasted or programmatically inserted.The default style is set with \helpref{wxRichTextCtrl::SetDefaultStyle}{wxrichtextctrlsetdefaultstyle}.\end{enumerate}What you see on the screen is the dynamically {\it combined} style, found by mergingthe first three of the above style types (the fourth is only a guide for future contentinsertion and therefore does not affect the currently displayed content).To make all this more concrete, here are examples of where you might set these differentstyles:\begin{enumerate}\itemsep=0pt\item You might set the {\bf basic style} to have a Times Roman font in 12 point,left-aligned, with two millimetres of spacing after each paragraph.\item You might set the {\bf paragraph style} (for one particular paragraph) tobe centred.\item You might set the {\bf character style} of one particular word to bold.\item You might set the {\bf default style} to be underlined, for subsequentinserted text.\end{enumerate}Naturally you can do any of these things either using your own UI, or programmatically.The basic wxTextCtrl doesn't make the same distinctions as wxRichTextCtrl regardingattribute storage. So we need finer control when setting and retrievingattributes. \helpref{wxRichTextCtrl::SetStyleEx}{wxrichtextctrlsetstyleex} takes a {\it flags} parameter:\begin{itemize}\itemsep=0pt\item wxRICHTEXT\_SETSTYLE\_OPTIMIZE specifies that the style should be changed only ifthe combined attributes are different from the attributes for the current object. This is important whenapplying styling that has been edited by the user, because he has just edited the {\it combined} (visible)style, and wxRichTextCtrl wants to leave unchanged attributes associated with their original objectsinstead of applying them to both paragraph and content objects.\item wxRICHTEXT\_SETSTYLE\_PARAGRAPHS\_ONLY specifies that only paragraph objects within the given rangeshould take on the attributes.\item wxRICHTEXT\_SETSTYLE\_CHARACTERS\_ONLY specifies that only content objects (text or images) within the given rangeshould take on the attributes.\item wxRICHTEXT\_SETSTYLE\_WITH\_UNDO specifies that the operation should be undoable.\end{itemize}It's great to be able to change arbitrary attributes in a wxRichTextCtrl, butit can be unwieldy for the user or programmer to set attributes separately. Word processors have collectionsof styles that you can tailor or use as-is, and this means that you can set a heading with one clickinstead of marking text in bold, specifying a large font size, and applying a certainparagraph spacing and alignment for every such heading. Similarly,wxWidgets provides a class called \helpref{wxRichTextStyleSheet}{wxrichtextstylesheet} which manages style definitions(\helpref{wxRichTextParagraphStyleDefinition}{wxrichtextparagraphstyledefinition}, \helpref{wxRichTextListStyleDefinition}{wxrichtextliststyledefinition} and \helpref{wxRichTextCharacterStyleDefinition}{wxrichtextcharacterstyledefinition}).Once you have added definitions to a style sheet and associated it with a wxRichTextCtrl,you can apply a named definition to a range of text. The classes \helpref{wxRichTextStyleComboCtrl}{wxrichtextstylecomboctrl}\rtfspand \helpref{wxRichTextStyleListBox}{wxrichtextstylelistbox} can be used to present the user with a listof styles in a sheet, and apply them to the selected text.You can reapply a style sheet to the contents of the control, by calling \helpref{wxRichTextCtrl::ApplyStyleSheet}{wxrichtextctrlapplystylesheet}.This is useful if the style definitions have changed, and you want the content to reflect this.It relies on the fact that when you apply a named style, the style definition name is recorded in thecontent. So ApplyStyleSheet works by finding the paragraph attributes with style names and re-applying the definition'sattributes to the paragraph. Currently, this works with paragraph and list style definitions only.\subsection{wxRichTextCtrl dialogs}\label{wxrichtextctrldialogs}wxRichTextCtrl comes with standard dialogs to make it easier to implementtext editing functionality.\helpref{wxRichTextFormattingDialog}{wxrichtextformattingdialog} can be usedfor character or paragraph formatting, or a combination of both. It's a wxPropertySheetDialogwith the following available tabs: Font, Indents \& Spacing, Tabs, Bullets, Style, and List Style.You can select which pages will be shown by supplying flags to the dialog constructor.In a character formatting dialog, typically only the Font page will be shown.In a paragraph formatting dialog, you'll show the Indents \& Spacing, Tabs and Bulletspages. The Style tab is useful when editing a style definition.You can customize this dialog by providing your own wxRichTextFormattingDialogFactoryobject, which tells the formatting dialog how many pages are supported, what their identifiersare, and how to creates the pages.\helpref{wxRichTextStyleOrganiserDialog}{wxrichtextstyleorganiserdialog} is a multi-purpose dialogthat can be used for managing style definitions, browsing styles and applying them, or selecting list styles witha renumber option. See the sample for usage - it is used for the "Manage Styles" and "Bullets and Numbering"menu commands.\helpref{wxSymbolPickerDialog}{wxsymbolpickerdialog} lets the user insert a symbol froma specified font. It has no wxRichTextCtrl dependencies besides being included inthe rich text library.\subsection{How wxRichTextCtrl is implemented}Data representation is handled by wxRichTextBuffer, and a wxRichTextCtrlalways has one such buffer.The content is represented by a hierarchy of objects, all derived fromwxRichTextObject. An object might be an image, a fragment of text, a paragraph,or a whole buffer. Objects store a wxTextAttrEx containing style information;a paragraph object can contain both paragraph and character information, butcontent objects such as text can only store character information. The finalstyle displayed in the control or in a printout is a combination of basestyle, paragraph style and content (character) style.The top of the hierarchy is the buffer, a kind of wxRichTextParagraphLayoutBox.containing further wxRichTextParagraph objects, each of which can include text,images and potentially other types of object.Each object maintains a range (start and end position) measuredfrom the start of the main parent object.When Layout is called on an object, it is given a size which the objectmust limit itself to, or one or more flexible directions (verticalor horizontal). So, for example, a centred paragraph is given the pagewidth to play with (minus any margins), but can extend indefinitelyin the vertical direction. The implementation of Layout caches the calculatedsize and position.When the buffer is modified, a range is invalidated (marked as requiringlayout), so that only the minimum amount of layout is performed.A paragraph of pure text with the same style contains just one furtherobject, a wxRichTextPlainText object. When styling is applied to part ofthis object, the object is decomposed into separate objects, one objectfor each different character style. So each object within a paragraph always hasjust one wxTextAttrEx object to denote its character style. Of course, this canlead to fragmentation after a lot of edit operations, potentially leadingto several objects with the same style where just one would do. Soa Defragment function is called when updating the control's display, to ensure thatthe minimum number of objects is used.\subsection{wxRichTextCtrl roadmap}\wxheading{Bugs}This is an incomplete list of bugs.\begin{itemize}\itemsep=0pt\item Moving the caret up at the beginning of a line sometimes incorrectly positions thecaret.\item As the selection is expanded, the text jumps slightly due to kerning differences betweendrawing a single text string versus drawing several fragments separately. This couldbe improved by using wxDC::GetPartialTextExtents to calculate exactly where the separate fragmentsshould be drawn. Note that this problem also applies to separation of text fragments due to difference in their attributes.\end{itemize}\wxheading{Features}This is a list of some of the features that have yet to be implemented. Help with them will be appreciated.\begin{itemize}\itemsep=0pt\item RTF input and output\item Conversion from HTML\item Open Office input and output\item Floating images, with content wrapping around them\item A ruler control\item Standard editing toolbars\item Tables\item Bitmap bullets\item Borders\item Text frames\item Justified text, in print/preview at least\end{itemize}There are also things that could be done to take advantage of the underlying text capabilities of the platform;higher-level text formatting APIs are available on some platforms, such as Mac OS X, and some of translation fromhigh level to low level wxDC API is unnecessary. However this would require additions to the wxWidgets API.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -