Attribute Interfaces

<< Click to Display Table of Contents >>

Navigation:  Programming > Move cursor / select text > TWPRTFDataCursor >

Attribute Interfaces

 

The following references are accessible through the TWPRTFDataCursor class:

 

The interface to change the selected text. It is also available as WPRichText.SelectedTextAttr.

property SelectedTextAttr: TWPSelectedTextAttrInterface;

 

The interface to change the current writing mode. It is also available as WPRichText.WritingAttr.

property WritingTextAttr: TWPCurrentWritingmodeAttrInterface;

 

This interfaces changes the character atr the cursor position, similar to WPRichText.CPAttr.

property CurrentCharAttr: TWPCursorCharAttrInterface;

 

This function returns SelectedTextAttr if text is selected, otherwise WritingTextAttr:

function CurrAttribute: TWPSelectedTextAttrInterface;

 

Also available is an interface in the finder class to modify the found text.

property Finder.FoundAttr: TWPFoundTextAttrInterface.

 

 

The TWPAttrInterface group contains classes to change attributes of different text parts. This chart shows which classes there are and how you can access each of them.

 

clip0155

 

 

 

The most important interface is TWPAbstractCharParAttrInterface. It defines the methods which are implemented by TWPSelectedTextAttrInterface and TWPCursorCharAttrInterface. It also inherits of TWPAbstractCharAttrInterface.

 

Please note that the 'Get' functions return a boolean value. This value will be false if the property was not set. In this case the passed variable will not be modified.

 

If you work with colors you need to know that colors are saved as index values in the TextColors array. The methods with "Color" in their names automatically do the conversion.

 

In TWPAbstractCharAttrInterface the following methods are available to access the attributes of a character:

 

  procedure Clear;

  procedure Clear(const FontName: string;

        FontSize: Single; FontColor: TColor = clBlack); overload;

  procedure BeginUpdate;

  { Saves the font name, color and background color and all other

   character attributes. Use AttrLoad to load.

   Note: The stored information survives a RTFProps.Clear! }

  procedure AttrSave(var store: TWPAbstractCharAttrStore);

  { Loads the font name, color and background color and all other character

   attributes which were stored with AttrSave. If there

   are no stored attributes the result value is FALSE. }

  function AttrLoad(var store: TWPAbstractCharAttrStore): Boolean;

 

  { This procedure assigns the character attributes stored in a certain text

    style or TParagraph to this attribute interface }

  procedure Assign(SourceStyle: TWPTextStyle);

  function EndUpdate: Boolean;

  { Applies style flags (such as WPSTY_BOLD..) to the character style. It is

     possible to switch a style explizietly off by setting the style bit

     in the "mask" and unsetting it in the "on" parameters. }

     

    The following mask bits are available:

       WPSTY_BOLD = 1; // Bit 1 bold

       WPSTY_ITALIC = 2; // Bit 2 italic

       WPSTY_UNDERLINE = 4; // Bit 3 underlined (solod)

       WPSTY_STRIKEOUT = 8; // Bit 4 strikeout

       WPSTY_SUPERSCRIPT = 16; // Bit 5 superscript

       WPSTY_SUBSCRIPT = 32; // Bit 6 subscript

       WPSTY_HIDDEN = 64; // Bit 7 hidden text

       WPSTY_UPPERCASE = 128; // Bit 8 all uppercase

       WPSTY_SMALLCAPS = 256; // Bit 9 all uppercase but non-captitals are 20% larger

       WPSTY_LOWERCASE = 512; // Bit 10 all lowercase

       xxx =  1024; // reserved bit

       WPSTY_DBLSTRIKEOUT = 2048; // Bit 12 strikeout - double solid line

       xxx = 4096; // reserved bit

       WPSTY_PROTECTED = 8192; // protected text  

 

  procedure SetCharStyles(WPSTY_mask, WPSTY_on: Integer);

  // switches a style on and off

  procedure ToggleCharstyle(WPSTY_code: Integer);

 

  { TOneWrtStyle is an enum which can be used to identify character styles.

     The following values are defined:

       afsBold, afsItalic,afsUnderline, afsStrikeOut,

         afsSuper,afsSub,afsHidden,afsUppercaseStyle,

         afsSmallCaps,afsLowercaseStyle, afsNoProof,

         afsDoubleStrikeOut, afsButton, afsProtected, afsUserdefine.

       Note: afsNoProof and afsButton are reserved for future versions.

  WrtStyle = set of TOneWrtStyle. }

 

  function GetStyles(var styles: WrtStyle): Boolean;

  procedure SetStyles(Value: WrtStyle);

  procedure IncludeStyle(Element: TOneWrtStyle);

  procedure ExcludeStyle(Element: TOneWrtStyle);

  procedure UndefineStyle(Element: TOneWrtStyle);

  procedure IncludeStyles(Elements: WrtStyle);

  procedure ExcludeStyles(Elements: WrtStyle);

  function HasStyle(Element: TOneWrtStyle; var Yes: Boolean): Boolean;

  function HasAStyle(Element: TOneWrtStyle): Boolean;

  procedure SetFont(const FontNr: Integer);

  procedure SetFontName(const FontName: string);

  { reads the font name as index in the FontNames arra yof the TWPRTFdataProps }

  function GetFont(var FontNr: Integer): Boolean;

  function GetFontName(var FontName: TFontName): Boolean;

  function GetFontCharset(var Charset: Integer): Boolean;

  { Assign the VCL style font flags. Better use IncludeStyle() and

      ExcludeStyle() }

  procedure SetFontStyle(const FontStyle: TFontStyles);

  { Read the VCL style font flags. }

  function GetFontStyle(var FontStyle: TFontStyles): Boolean;

  procedure SetFontCharSet(const Charset: Integer);

  function GetCharSpacing(var DistanceTW: Integer): Boolean;

  procedure SetCharSpacing(const DistanceTW: Integer);

  function GetCharLevel(var CharlevelPC: Integer): Boolean;

  procedure SetCharLevel(const CharlevelPC: Integer);

  function GetCharWidth(var CharwidthPC: Integer): Boolean;

  procedure SetCharWidth(const CharwidthPC: Integer);

  procedure SetFontSize(Size: Single);

  function GetFontSize(var FontSize: Single): Boolean;

  procedure SetColorNr(const ColorNr: Integer);

  procedure SetColor(const Color: TColor);

  procedure SetColorString(const ColorName: string);

  function GetColorNr(var ColorNr: Integer): Boolean;

  function GetColor(var Color: TColor): Boolean;

  procedure SetBGColorNr(const ColorNr: Integer);

  procedure SetBGColor(const Color: TColor);

  procedure SetBGColorString(const ColorName: string);

  function GetBGColorNr(var ColorNr: Integer): Boolean;

  function GetBGColor(var Color: TColor): Boolean;

  function GetTextLanguage(var Mode: Integer): Boolean;

  procedure SetTextLanguage(const Mode: Integer);

  function GetHighlightMode(var Mode: Integer): Boolean;

  procedure SetHighlightMode(const mode: Integer);

  function GetTextEffect(var Mode: Integer): Boolean;

  procedure SetTextEffect(const mode: Integer);

  function GetUnderlineMode(var Mode: Integer): Boolean;

  procedure SetUnderlineMode(const mode: Integer);

  procedure SetUnderlineColorNr(const ColorNr: Integer);

  procedure SetUnderlineColor(const Color: TColor);

  function GetUnderlineColorNr(var ColorNr: Integer): Boolean;

  function GetUnderlineColor(var Color: TColor): Boolean;

  function GetCharEffect(var Effect: Integer): Boolean;

  procedure SetCharEffect(const Effect: Integer);

  function GetCharStyleSheet(var StyleNr: Integer): Boolean;

  { WPTools can assign paragraph styles also to character attributes }

  procedure SetCharStyleSheet(const StyleNr: Integer);

  { This function creates a CSS like style sheet using wptools special names.

       <br>If 'OnlyUsePTag' is set to true, only generic

       'P' tags are created: The created string can then only be used for text

       which uses the same RTFProps object in the same instance of the application.  }

  function AGetWPSS(OnlyUsePTag: Boolean = FALSE;

     Abbreviated: Boolean = FALSE): AnsiString;

  { This procedure applies the properties defined in the given string. They must

   have been created using the AGetWPSS function.

   Please note that white space characters are not expected in the string.<br>

   If the optional parameter "Merge" is set to TRUE no properties in the

   style are initialized before the new properties are applied. }

  procedure ASetWPSS(const WPCSSString: AnsiString;

     Merge: Boolean = false;

     Abbreviated: Boolean = FALSE);

 

  { This function returns true if the provided character attributes

       are contained completely in this character attributes.}

  function Contains(var OtherCA: TWPCharAttr): Boolean;

  function Contains(CharAttrInterface: TWPStoredCharAttrInterface): Boolean;

  { This function returns true if the provided character attributes

       containes all attributes which are defined here. }

  function ContainedIn(var OtherCA: TWPCharAttr): Boolean;

  function ContainedIn(CharAttrInterface: TWPStoredCharAttrInterface): Boolean;

 

TWPAbstractCharParAttrInterface defines this methods:

 

  { This procedure is used to set the paragraph style for all paragraphs }

  function AGetBaseStyle(var StyleNr: Integer): Boolean;

  { This procedure is used to set the paragraph style for all paragraphs }

  procedure ASetBaseStyle(StyleNr: Integer);

  { Increments a property (with offset>0) or decrements a property (with offset<0).

      In any case the value is checked for the minimum value }

  procedure AInc(WPAT_Code: Byte; Offset: Integer; MinValue: Integer = 0);

  {:: Assigns a color value - to reset to default use clNone }

  procedure ASetColor(WPAT_Code: Byte; Value: TColor);

  { Executes a bitwise OR operation with the current value and the passed

      value and assign the result }

  procedure ASetAdd(WPAT_Code: Byte; Value: Cardinal);

  { Executes a bitwise AND NOT operation with the current value and the passed

      value and assign the result }

  procedure ASetDel(WPAT_Code: Byte; Value: Cardinal);

  { This procedure is used to write most of the paragraph properties

     If the value is 0 it will delete an existing entry unless the 0 value is

     required to override an inherited value.

     Please note that you may only use 23 bits of Value }

  procedure ASetNeutral(WPAT_Code: Byte; Value: Integer);

   { This procedure is used to reset a property to the default value }

  procedure ADel(WPAT_Code: Byte);

  { This function returns the count of defined TabStops }

  function TabstopCount: Integer;

  { This function returns all defined tabstops one by one }

  procedure TabstopGet(nr: Integer; var Value: Integer;

      var Kind: TTabKind; var FillMode: TTabFill;

      var FillColor: Integer);

  { Adds a tabstop to the style. The value has to be specified in

     twips. If the function returns TRUE the tab was added, if

     it is false an existing was modified. }

  function TabstopAdd(Value: Integer; Kind: TTabKind;

     Fill: TTabFill; ColorNr: Integer): Boolean;

  { Deletes the tabstop with the giben value in twips + - the value RTFProps.TabPlusMinus) }

  function TabstopDelete(Value: Integer): Boolean;

  { Moves a tabstop specified by position }

  procedure TabstopMove(OldValue, NewValue: Integer);

  { Deletes all defined tabstops }

  procedure TabstopClear;

  { Selects a base style for this paragraph. }

  procedure SetStyle(ParStyleNr: Integer;

     ClearParStyles: Boolean = FALSE;

     ClearCharStyles: Boolean = FALSE);

 

  { Retrieves the number of the base style for this paragraph }

  function GetStyle: Integer;

  { If this property is true all paragraph manipulations (ASet, AGet, ASetAdd, ASetDel, ASetColor, SetStyle, GetStyle but NOT the tabstop procedures)

will be executed with the current cell instead of the current or selected paragraph. This makes it easier to change the color and border attributes in table cells.

 

The class TWPSelectedTextAttrInterface does not use this property, only TWPCursorCharAttrInterface.   }

  property ModifyCellsOnly: Boolean;

 

 

Examples:

 

a) Create formatted text under program control:

 

WPRichText1.AttrHelper.Clear;

WPRichText1.AttrHelper.SetFontName('Courier New');

WPRichText1.AttrHelper.SetColor(clGreen);

WPRichText1.ActiveParagraph.SetText(

 'Some green text',

 WPRichText1.AttrHelper.CharAttr);

WPRichText1.DelayedReformat;

 

b) Set the default writing font of a DBWPRichText. We use the OnClear event:

 

procedure TForm1.DBWPRichText1Clear(Sender: TObject);

begin

  DBWPRichText1.WritingAttr.Clear;

  DBWPRichText1.WritingAttr.SetFontName('Courier New');

  DBWPRichText1.WritingAttr.SetFontSize(18);

end;

 

c) Implement the hotkeys Ctrl+B, I and U to toggle the bold, italic and underline character style in the current writing mode or, if text is selected, the selected text. We use the OnKeyPress event.

 

procedure TForm1.WPRichText1KeyPress(Sender: TObject; var Key: Char);

begin

if Integer(Key) = Integer('B') - 64 then

begin

   WPRichText1.TextCursor.CurrAttribute.ToggleCharstyle(WPSTY_BOLD);

   Key := #0;

end

else if Integer(Key) = Integer('I') - 64 then

begin

   WPRichText1.TextCursor.CurrAttribute.ToggleCharstyle(WPSTY_ITALIC);

   Key := #0;

end

else if Integer(Key) = Integer('U') - 64 then

begin

   WPRichText1.TextCursor.CurrAttribute.ToggleCharstyle(WPSTY_UNDERLINE);

   Key := #0;

end;

end;

 

d) Change font size of one paragraph:

 

WPRichText1.SelectParagraph(); // You can pass a 'par' here!

  if WPRichText1.SelectedTextAttr.GetFontSize(aSize) and (aSize<11) then

       WPRichText1.SelectedTextAttr.SetFontSize(12)

  else WPRichText1.SelectedTextAttr.SetFontSize(9);

  WPRichText1.HideSelection;

 

e) Show all supported underline modes:

 

procedure TForm1.ShowPossibleULClick(Sender: TObject);

const

 cnames: array[1..18] of string =

 (   'WPUND_Standard'

   , 'WPUND_Dotted'

   , 'WPUND_Dashed'

   , 'WPUND_Dashdotted'

   , 'WPUND_Dashdotdotted'

   , 'WPUND_Double'

   , 'WPUND_Heavywave'

   , 'WPUND_Longdashed'

   , 'WPUND_Thick'

   , 'WPUND_Thickdotted'

   , 'WPUND_Thickdashed'

   , 'WPUND_Thickdashdotted'

   , 'WPUND_Thickdashdotdotted'

   , 'WPUND_Thicklongdashed'

   , 'WPUND_Doublewave'

   , 'WPUND_WordUnderline'

   , 'WPUND_wave'

   , 'WPUND_curlyunderline');

var i: Integer;

begin

 WPRichText1.Clear;

 WPRichText1.CheckHasBody;

 

 WPRichText1.WritingAttr.Clear('Verdana', 10);

 

 WPRichText1.InputString('The modes are applied with' + #10 +

  'WPRichText1.WritingAttr.SetUnderlineMode(x)' + #13 + #13);

 

 WPRichText1.WritingAttr.ASet(WPAT_BorderFlags, WPBRD_DRAW_All4);

 WPRichText1.WritingAttr.LockBorder;

 WPRichText1.WritingAttr.ASet(WPAT_IndentLeft, 720);

 WPRichText1.WritingAttr.ASet(WPAT_IndentFirst, 288);

 WPRichText1.WritingAttr.ASet(WPAT_Indentright, 3600);

 WPRichText1.InputString(#13);

 

 WPRichText1.WritingAttr.SetColor(clGray);

 WPRichText1.WritingAttr.SetUnderlineColor(clRed);

 

for i := 1 to 18 do

begin

   WPRichText1.WritingAttr.SetUnderlineMode(i);

   WPRichText1.InputString(cnames[i] + #13);

end;

 

 WPRichText1.WritingAttr.SetUnderlineMode(-1);

 WPRichText1.WritingAttr.SetUnderlineColor(clNone);

 

// After UnlockBorder the border will be closed with the next

// new paragraph

 WPRichText1.WritingAttr.UnlockBorder;

 WPRichText1.InputString(#13);

 

 WPRichText1.WritingAttr.ADel(WPAT_IndentLeft);

 WPRichText1.WritingAttr.ADel(WPAT_IndentFirst);

 WPRichText1.WritingAttr.ADel(WPAT_Indentright);

 

 WPRichText1.WritingAttr.SetColor(clBlack);

 WPRichText1.InputString(#13 + 'The colors have been changed with' + #10 +

  'WPRichText1.WritingAttr.SetColor(clGray);' + #10 +

  'WPRichText1.WritingAttr.SetUnderlineColor(clRed);' + #13 + #13);

end;