C++ Builder

La Isla Bonita



Extended Rich Edit
Extended Rich Edit


Description

This article describes the TRichEditEx control (Extended Rich Edit), which I created as a descendant of the standard VCL TRichEdit class. The new TRichEditEx class adds the following new functionality: several Write() methods and a CountLine() method
The code inside the Button1Click event handler illustrates the usage of the TRichEditEx control. All The components on the form are created dynamically.

RichEditEx.cpp

#include 
#pragma hdrstop

#include "RichEditExtended.h"
//-----------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TRichEditEx* RichEditEx1;
//-----------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    Width    = 800;
    Height   = 600;
    Color    = (TColor)0xfd2dfaa;
    Position = poScreenCenter;

    TButton* Button1 = new TButton( this );
    Button1->Parent  = this;
    Button1->Top     = 520;
    Button1->Left    = 300;
    Button1->Width   = 200;
    Button1->Height  =  25;
    Button1->Visible = true;
    Button1->Enabled = true;
    Button1->Caption = "Write to Extended Rich Edit";
    Button1->OnClick = Button1Click;

    RichEditEx1 = new TRichEditEx( this );
    RichEditEx1->Parent  = this;
    RichEditEx1->Top     = 10;
    RichEditEx1->Left    = 10;
    RichEditEx1->Width   = Width - 3*RichEditEx1->Left;
    RichEditEx1->Height  = 500;
    RichEditEx1->Visible = true;
    RichEditEx1->Enabled = true;
    RichEditEx1->Text    = "Extended Rich Edit";
}
//-----------------------------------------------------------------------

void TRichEditEx::Write( String text, String name, TColor color, int size,
                         int height, TFontPitch pitch, int charset,
                         int bold, int italic, int underline, int strikeout,
                         TAlignment alignment, TNumberingStyle numbering,
                         int first_indent, int left_indent, int right_indent )
{
//GENERAL WRITE METHOD WITH PARAGRAPH FORMATTING
//----------------------------------------------------------------
//Note 1: The possible values of Font Pitch are fpDefault, fpFixed,
//fpVariable
//Note 2: Height is dominant over size when both are greater than zero
//If you want to use only the font size, then set the Font height to zero.
//Note 3: If you want to make a bullet visible set first_indent,
//left_indent and  right_indent to zero
//Remember that the Numbering property also affects the left indentation.
//---------------------------------------------------------------------
    SelStart = GetTextLen();
    SelAttributes->Color   = color;
    //-------------------------------
    Paragraph->Numbering   = numbering;
    Paragraph->Alignment   = alignment;
    Paragraph->FirstIndent = first_indent;
    Paragraph->LeftIndent  = left_indent;
    Paragraph->RightIndent = right_indent;
    //-------------------------------
    SelAttributes->Name    = name;
    SelAttributes->Size    = size;
    SelAttributes->Height  = height;
    SelAttributes->Pitch   = pitch;
    SelAttributes->Charset = charset;

    switch ( bold )
    {
        case FS_BOLD:
             SelAttributes->Style = SelAttributes->Style << fsBold;
             break;
        case FS_NOT_BOLD:
             SelAttributes->Style = SelAttributes->Style >> fsBold;
             break;
        default: ShowMessage("Illegal Font Type: Using default value");
                 SelAttributes->Style = SelAttributes->Style >> fsBold;
    }

    switch ( italic )
    {
        case FS_ITALIC:
             SelAttributes->Style = SelAttributes->Style << fsItalic;
             break;
        case FS_NOT_ITALIC:
             SelAttributes->Style = SelAttributes->Style >> fsItalic;
             break;
        default: ShowMessage("Illegal Font Type: Using default value");
                 SelAttributes->Style = SelAttributes->Style >> fsItalic;        
    }

    switch ( underline )
    {
        case FS_UNDERLINE:
             SelAttributes->Style = SelAttributes->Style << fsUnderline;
             break;
        case FS_NOT_UNDERLINE:
             SelAttributes->Style = SelAttributes->Style >> fsUnderline;
             break;
        default: ShowMessage("Illegal Font Type: Using default value");
                 SelAttributes->Style = SelAttributes->Style >> fsUnderline;
    }

    switch ( strikeout )
    {
        case FS_STRIKEOUT:
             SelAttributes->Style = SelAttributes->Style << fsStrikeOut;
             break;
        case FS_NOT_STRIKEOUT:
             SelAttributes->Style = SelAttributes->Style >> fsStrikeOut;
             break;
        default: ShowMessage("Illegal Font Type: Using default value");
                 SelAttributes->Style = SelAttributes->Style >> fsStrikeOut;
    }
    SelText = text;
}
//-----------------------------------------------------------------------

void TRichEditEx::Write( String text, String name, TColor color, int size,
                        int height, TFontPitch pitch, int charset,
                        int bold, int italic, int underline, int strikeout  )
{
//WRITE WITHOUT PARAGRAPH FORMATTING

    SelStart = GetTextLen();
    SelAttributes->Name    = name;
    SelAttributes->Color   = color;
    SelAttributes->Size    = size;
    SelAttributes->Height  = height;
    SelAttributes->Pitch   = pitch;
    SelAttributes->Charset = charset;

    switch ( bold )
    {
        case FS_BOLD:
             SelAttributes->Style = SelAttributes->Style << fsBold;
             break;
        case FS_NOT_BOLD:
             SelAttributes->Style = SelAttributes->Style >> fsBold;
             break;
        default: ShowMessage("Illegal Font Type: Using default value");
                 SelAttributes->Style = SelAttributes->Style >> fsBold;
    }

    switch ( italic )
    {
       case FS_ITALIC:
            SelAttributes->Style = SelAttributes->Style << fsItalic;
            break;
       case FS_NOT_ITALIC:
            SelAttributes->Style = SelAttributes->Style >> fsItalic;
            break;
       default: ShowMessage("Illegal Font Type: Using default value");
                SelAttributes->Style = SelAttributes->Style >> fsItalic;
    }

    switch ( underline )
    {
        case FS_UNDERLINE:
             SelAttributes->Style = SelAttributes->Style << fsUnderline;
             break;
        case FS_NOT_UNDERLINE:
             SelAttributes->Style = SelAttributes->Style >> fsUnderline;
             break;
        default: ShowMessage("Illegal Font Type: Using default value");
                 SelAttributes->Style = SelAttributes->Style >> fsUnderline;
    }

    switch ( strikeout )
    {
        case FS_STRIKEOUT:
             SelAttributes->Style = SelAttributes->Style << fsStrikeOut;
             break;
        case FS_NOT_STRIKEOUT:
             SelAttributes->Style = SelAttributes->Style >> fsStrikeOut;
             break;
        default: ShowMessage("Illegal Font Type: Using default value");
                 SelAttributes->Style = SelAttributes->Style >> fsStrikeOut;
    }
    SelText = text;
}
//-----------------------------------------------------------------------

void TRichEditEx::Write( String text, TColor color, int height )
{
//SIMPLE WRITE METHOD
    SelStart = GetTextLen();
    SelAttributes->Color = color;
    SelAttributes->Height = height;
    SelText = text;
}
//-----------------------------------------------------------------------

void TRichEditEx::Write( String text, TColor color )
{
//SIMPLE WRITE METHOD
    SelStart = GetTextLen();
    SelAttributes->Color = color;
    SelText = text;
}
//-----------------------------------------------------------------------

int TRichEditEx::CountLines()
{
//COUNT LINES IN RICH EDIT
    int PositionFound, PositionStart, PositionEnd;
    PositionStart = 0;
    // PositionEnd is the length from PositionStart
    // to the end of the entire text.
    PositionEnd   = Text.Length() - PositionStart;

    PositionFound = FindText( "\n", PositionStart, PositionEnd,
                                   TSearchTypes()<< stMatchCase );
    int linecount = 0;
    while( 1 )
    {
        PositionFound = FindText( "\n", PositionStart, PositionEnd,
                                  TSearchTypes()<< stMatchCase);
        if ( PositionFound == -1 )
        {
            if ( PositionEnd )
                 linecount++;
            break;
        }
        linecount++;
        PositionStart = PositionFound+1;
        PositionEnd   = Text.Length() - PositionStart;
    }
    return linecount;
}

void TRichEditEx::SetParaAttributes( TAlignment alignment, TNumberingStyle numbering )
{
//SET PARAGRAPH ATTRIBUTES
    Paragraph->Numbering   = numbering;
    Paragraph->Alignment   = alignment;
}

void TRichEditEx::SetParaAttributes( TAlignment alignment, TNumberingStyle numbering,
                             int first_indent, int left_indent, int right_indent )
{
//SET PARAGRAPH ATTRIBUTES
    Paragraph->FirstIndent = first_indent;
    Paragraph->LeftIndent  = left_indent;
    Paragraph->RightIndent = right_indent;
    Paragraph->Numbering   = numbering;
    Paragraph->Alignment   = alignment;
}
//---------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

//The following code illustrates the usage of the TRichEditEx control:

    RichEditEx1->Lines->Clear();
    RichEditEx1->PlainText = false;
    RichEditEx1->WordWrap  = true;

    RichEditEx1->Write( "(1)This is a new line of text.\n",
           "Courrier New", (TColor)0xfaefd, 10, 30, fpFixed, ANSI_CHARSET,
           FS_BOLD, FS_NOT_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taCenter, nsNone, 50, 50, 50 );

    RichEditEx1->Write( "(2)This is a new line of text.\n",
           "Courrier New", (TColor)0xfaefd, 10, 30, fpFixed, ANSI_CHARSET,
           FS_NOT_BOLD, FS_NOT_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taCenter, nsNone, 50, 50, 50 );


    RichEditEx1->Write( "(3)This is a new line of text. \n",
           "Courrier New", clGreen, 10, 20, fpFixed, ANSI_CHARSET,
           FS_NOT_BOLD, FS_NOT_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taLeftJustify, nsNone, 5, 50, 50);

    RichEditEx1->Write( "(4)This is a new line of text. \n",
           "Courrier New", (TColor)0xfdaec12, 10, 25, fpFixed, ANSI_CHARSET,
           FS_NOT_BOLD, FS_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taLeftJustify, nsNone, 10, 50, 50 );

    RichEditEx1->Write( "(5)This is a new line of text. \n",
           "Courrier New", (TColor)(TColor)0xcd88a, 10, 30, fpFixed, ANSI_CHARSET,
           FS_NOT_BOLD, FS_NOT_ITALIC, FS_UNDERLINE, FS_NOT_STRIKEOUT,
           taLeftJustify, nsNone, 10, 50, 50 );

    RichEditEx1->Write( "(6)This is a new line of text. \n",
           "Courrier New", (TColor)0xbcbcbc, 10, 35, fpFixed, ANSI_CHARSET,
           FS_NOT_BOLD, FS_NOT_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taLeftJustify, nsBullet, 0, 0, 0 );

    RichEditEx1->Write( "(7)This is a new line of text. \n",
           "Courrier New", (TColor)0xeeeee, 10, 35, fpFixed, ANSI_CHARSET,
           FS_NOT_BOLD, FS_NOT_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taLeftJustify, nsBullet, 0, 0, 0 );

    RichEditEx1->Write( "(8)This is a new line of text. \n",
           "Courrier New", (TColor)0xfaefd, 10, 35, fpFixed, ANSI_CHARSET,
           FS_NOT_BOLD, FS_NOT_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taLeftJustify, nsNone, 10, 50, 50 );

    RichEditEx1->Write( "(9)This is a new line of text. \n",
           "Courrier New", (TColor)0x44ffd, 10, 35, fpFixed, ANSI_CHARSET,
           FS_NOT_BOLD, FS_NOT_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taLeftJustify, nsNone, 10, 50, 50 );

    RichEditEx1->Write( "(10)This is a new line of text. \n",
           "Courrier New", clRed, 10, 35, fpFixed, ANSI_CHARSET,
           FS_BOLD, FS_ITALIC, FS_UNDERLINE, FS_STRIKEOUT,
           taLeftJustify, nsNone, 10, 50, 50 );

    RichEditEx1->Write( "(1)This is a new line of text.\n",
           "Sans Serif", (TColor)0xfaefd, 50, 10, fpFixed, ANSI_CHARSET,
           FS_NOT_BOLD, FS_NOT_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taCenter, nsBullet, 10, 50, 50 );

    RichEditEx1->Write( "(2)This is a new line of text. \n",
           "Courrier New", clRed, 10, 20, fpFixed, ANSI_CHARSET,
           FS_BOLD, FS_NOT_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taCenter, nsNone, 10, 50, 50 );

    RichEditEx1->Write( "(3)This is a new line of text. \n",
           "Times New Roman", clGreen, 10, 20, fpFixed, ANSI_CHARSET,
           FS_NOT_BOLD, FS_NOT_ITALIC, FS_NOT_UNDERLINE, FS_NOT_STRIKEOUT,
           taCenter, nsBullet, 10, 50, 50 );

    //------------------------------------------------------------------

    RichEditEx1->SetParaAttributes( taLeftJustify, nsBullet );
    RichEditEx1->Write( "(Line 1)text. \n", clBlack );
    RichEditEx1->SetParaAttributes( taLeftJustify, nsBullet );
    RichEditEx1->Write( "(Line 2)text. \n", clGreen );
    RichEditEx1->SetParaAttributes( taLeftJustify, nsBullet );
    RichEditEx1->Write( "(Line 3)text. \n", clRed );

    RichEditEx1->SetParaAttributes( taCenter, nsBullet );
    RichEditEx1->Write( "(Line 1)text. \n", clBlack );
    RichEditEx1->SetParaAttributes( taCenter, nsBullet );
    RichEditEx1->Write( "(Line 2)text. \n", clGreen );
    RichEditEx1->SetParaAttributes( taCenter, nsBullet );
    RichEditEx1->Write( "(Line 3)text. \n", clRed );

    RichEditEx1->SetParaAttributes( taCenter, nsNone );
    RichEditEx1->Write( "(Line 1)text. \n", clBlack );
    RichEditEx1->SetParaAttributes( taCenter, nsNone );
    RichEditEx1->Write( "(Line 2)text. \n", clGreen );
    RichEditEx1->SetParaAttributes( taCenter, nsNone );
    RichEditEx1->Write( "(Line 3)text. \n", clRed );

    RichEditEx1->SetParaAttributes( taRightJustify, nsNone, 50, 50, 50 );
    RichEditEx1->Write( "(Line 1)text. \n", clBlack );
    RichEditEx1->SetParaAttributes( taRightJustify, nsNone, 20, 20, 20 );
    RichEditEx1->Write( "(Line 2)text. \n", clGreen );
    RichEditEx1->SetParaAttributes( taRightJustify, nsNone, 20, 20, 20 );
    RichEditEx1->Write( "(Line 3)text. \n", clRed );
}

RichEditEx.h

#ifndef RichEditExtendedH
#define RichEditExtendedH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------

enum
{
    FS_BOLD,
    FS_NOT_BOLD,
    FS_ITALIC,
    FS_NOT_ITALIC,
    FS_UNDERLINE,
    FS_NOT_UNDERLINE,
    FS_STRIKEOUT,
    FS_NOT_STRIKEOUT
};

class TRichEditEx : public TRichEdit
{
  private:

  public:
     __fastcall TRichEditEx(TComponent* Owner) : TRichEdit(Owner)
     {
     }

     void Write( String text, String name, TColor color, int size,
                 int height, TFontPitch pitch, int charset,
                 int bold, int italic, int underline, int strikeout,
                 TAlignment alignment, TNumberingStyle numbering,
                 int first_indent, int left_indent, int right_indent );

     void Write( String text, String name, TColor color, int size,
                 int height, TFontPitch pitch, int charset,
                 int bold, int italic, int underline, int strikeout  );

     void Write( String text, TColor color, int height );

     void Write( String text, TColor color );

     int CountLines();

     void SetParaAttributes( TAlignment alignment, TNumberingStyle numbering );

     void SetParaAttributes( TAlignment alignment, TNumberingStyle numbering,
                             int first_indent, int left_indent,
                             int right_indent );
};


class TForm1 : public TForm
{
__published:	// IDE-managed Components

        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.