class CGXRichEditCtrl: public CGXStatic

The CGXRichEditCtrl lets you display and edit Rich Text in cells. Rich Text allows you to format individual characters or paragraphs with different fonts, colors or horizontal alignment.

CGXRichEditCtrl is only supported for MFC version 4.0 or higher.

The rich edit control determines attributes like font, horizontal alignment and text color information from the RTF string which is stored in the style value (see CGXStyle::GetValue).

In your application, you should implement toolbar buttons for outlining fonts (e.g., bold, italic, underline) or provide a menu option which lets the user format cells with the CGXStyleSheet dialog (as in RTFGrid). When the user formats cells with the Format|Cells dialog (or by pressing the “B”, “I”, “U” or other toolbar buttons), you should call CGXGridCore::SetStyleRange.

SetStyleRange calls CGXControl::StoreStyle for each cell. CGXRichEditCtrl provides its own version of StoreStyle. This method extracts font and text color information from the style object, inserts these attributes into the RTF string, and writes the RTF string back to the style object (with CGXStyle::SetValue).

This technique makes it possible for the user to format RTF cells in any way. StoreStyle even knows that only the selected text in the active RTF cell should be formatted. For example, if the user selects some text and presses the “Bold” toolbar button, only the selected text will become Bold.

The new CGXControl::LoadStyle method lets you extract style information from the current RTF cell. You can use this method to enable/disable the toolbar buttons for outlining the font. See the RTFGrid sample.

Additional features (and limitations) of the CGXRichEditCtrl are:

  • Support for autosize (bottomless) cells - Cells can automatically grow and shrink while the user is entering or removing text. Autosize is only possible for the cell height, not for the cell width.

  • CGXRichEditCtrl also supports resizing rows through the ResizeRowHeightsToFit() API. Columns cannot be resized. This is due to a limitation of the RICHEDIT control.

  • CGXRichEditCtrl supports Find and Replace. Find and Replace ignore any RTF statements. Therefore, you cannot search for “Bold” or “Italic” or other attributes but only for text regardless of its style.

  • CGXRichEditCtrl supports Cut, Copy and Paste for RTF strings. If the user copies formatted text from the RTF cell, the formatted text can be pasted into another RTF cell (or any other RTF application, e.g., WordPad).

  • The RichEdit is fully DBCS-enabled.

  • The RichEdit control does not support zooming.

  • Drag&Drop is only possible within a cell. You cannot drag selected text onto another cell.

Style attributes which specify the appearance of the cell are:

  • RTF Text with SetValue. Please note that RTF text has to be enclosed in brackets, e.g., “{ Hello World }”

  • Text color with SetColor.

  • Background color with SetInterior.

  • Cell frame 3d-effect (raised, inset, normal) with SetDraw3dFrame.

  • Borders with SetBorder.

  • Horizontal alignment with SetHorizontalAlignment.

  • Font with SetFont.

  • Read only with SetReadOnly.

  • Maximal text length with SetMaxTextLength.

  • Wordbreak with SetWrapText.

  • Auto size with SetAutoSize. When auto size is enabled, the cell will grow automatically when the user enters text and the text won’t fit into the cell.

  • Interpret ENTER with SetAllowEnter. If allow enter is TRUE, pressing ENTER will start a new line in the cell. If it is FALSE, the key will be processed by the grid to move the current cell.

The default id for CGXRichEditCtrl controls is GX_IDS_CTRL_RICHEDIT.

Use the CGXControl::GetControlText API if you want to determine the plain text without RTF statements from the cell and CGXControl::SetControlText if you want to specify the text for the cell without RTF statements. For example, if the RTF value is “{ Hello World! }”, GetControlText will return “Hello world!”.

RTF Statements

The easiest way to produce an RTF string is to format the text with WordPad and then open the RTF text with a text editor. You can then copy/paste the RTF string to your C++ code. Take care that you replace all ‘\’ characters with ‘\\’. Otherwise, Visual C++ will give you plenty of warnings.

Here is some information we have collected from Microsoft Developer Network on how to use RTF statements. See also “Creating Help Files” in MSDN.

RTF code always starts with

{\rtf1\ansi

Next, you must declare the names of the fonts you use in the file by using a \fonttbl statement. The \fonttbl statement, enclosed in braces, contains a list of font and family names and specifies a unique number for each font. You use these numbers with \f statements later to set specific fonts. The following \fonttbl statement assigns font numbers 0, 1, and 2 to the TrueType fonts Times New Roman, Courier New, and Arial, respectively:

{\fonttbl
\f0\froman Times New Roman;
\f1\fdecor Courier New;
\f2\fswiss Arial;}

You should also use the \deff statement to set the default font for the file. Windows Help uses this default font if no other font is specified. The following example sets the default font number to zero, corresponding to the Times New Roman font specified in the previous \fonttbl statement:

\deff0 

 If you use specific text colors or choose not to rely on the default text colors set by Windows, you must define your colors by using a \colortbl statement. The \colortbl statement, enclosed in braces, defines each color by specifying the amount of each primary color (red, green, and blue) used in it. The statement implicitly numbers the colors consecutively starting from zero. You use these color numbers with \cf statements later to set the color. The following example creates four colors (black, red, green, and blue):

{\colortbl
\red0\green0\blue0;
\red255\green0\blue0;
\red0\green128\blue0;
\red0\green0\blue255;}

Although it is not shown here, you can put a semicolon immediately after the \colortbl statement to define the default color as 0.

You can set the font name and size by using the \f and \fs statements. The name is set by using a font number specified in the \fonttbl statement. The size of the font is specified in half-points. The following example sets the text to 10-point Times New Roman (if the \fonttbl statement matches the example in Declaring Character Set, Fonts, and Colors):

\f0\fs20
\b is for bold font. \i italic font. \ul underlined font.

Once you set the font name and size, the settings apply to all subsequent text up to the next \plain statement or until you change the name or size by using the \f or \fs statement again. The \plain statement resets the name and font to the defaults. The default font name is as set by the \deff statement; the default font size is 12 points.

Horizontal alignment can be specified with \qc (centered), \qr (right aligned) and \ql (left aligned). Each paragraph is separated with \par.

Don’t forget the closing bracket at the end of the RTF string.

Example:

We want to display the following text:

10 point, Times New Roman, red, centered
14 point, Courier New, blue, left aligned
18 point, Arial, black, right aligned
Default font and color, Bold, Underline, Italic

Here is the RTF string

{\rtf1\ansi
{\fonttbl
\f0\froman Times New Roman;
\f1\fdecor Courier New;
\f2\fswiss Arial;}
\deff0
{\colortbl
\red0\green0\blue0;
\red255\green0\blue0;
\red0\green128\blue0;
\red0\green0\blue255;}
\qc\cf1\f0\fs20 10 point, Times New Roman, red, centered\plain\par
\ql\cf3\f1\fs28 14 point, Courier New, blue, left aligned\plain\par
\qr\cf0\f2\fs36 18 point, Arial, black, right aligned\plain\par
\qc Default font and color, \b Bold\plain , \ul Underline\plain , \i Italic \plain\par
}

See the example below for how to call SetValueRange.


                                            

#include <gxall.h>

Example

This example initializes a rich edit cell:

SetStyleRange(CGXRange(nRow, nCol),
   CGXStyle()
      .SetControl(GX_IDS_CTRL_RICHEDIT)
      .SetAutoSize(TRUE)
      .SetWrapText(TRUE)
      .SetAllowEnter(TRUE)
   );
SetValueRange(CGXRange(nRow, nCol),
   _T("{\\rtf1\\ansi")
   // font table
   _T("{\\fonttbl")
   _T("\\f0\\froman Times New Roman; ")
   _T("\\f1\\fdecor Courier New; ")
   _T("\\f2\\fswiss Arial;} ")
   _T("\\deff0 ")
   // color table
   _T("{\\colortbl")
   _T("\\red0\\green0\\blue0; ")
   _T("\\red255\\green0\\blue0; ")
   _T("\\red0\\green128\\blue0; ")
   _T("\\red0\\green0\\blue255;} ")
   // first line
   _T("\\qc\\cf1\\f0\\fs20 10 point, Times New Roman, ")
   _T("red, centered\\plain\\par")
   // second line
   _T("\\ql\\cf3\\f1\\fs28 14 point, Courier New, blue, ")
   _T("left aligned\\plain\\par")
   // third line
   _T("\\qr\\cf0\\f2\\fs36 18 point, Arial, black, ")
   _T("right aligned\\plain\\par")
   // fourth line
   _T("\\qc Default font and color, \\b Bold\\plain , \\ul Underline\\plain , \\i Italic \\plain\\par")
   // closing bracket
   _T("}")
);

See Also

 CGXStyle::GetValue  CGXGridCore::SetStyleRange  CGXControl::StoreStyle  CGXControl::LoadStyle  CGXControl::GetControlText  CGXControl::SetControlText

CGXRichEditCtrl

 Class Members