WPCubed Product Support

How to change the fonttype properly? (use DevExpress Combo)

Hi,

I'm using a DBWPRichText with DevExpress-TdxBarComboBox.

To change the font of the selected text, I've added action "Fonttype" to the control.

1: When selecting some text with the same fonttype, the control is clearing the description of the fonttype in the dxBarComboBox (not showing the current fonttype).

2: The editor doesn't recognize the change of the font (nor fontsize) as a change in the database, so how do I set this up properly?

Regards, Stef

#1 ♥ 0
17k Views 3 users read 8 min
support

Hi,

The interface SelectedTextAttr will NOT report any font name if the selected text uses different fonts. In this case it is correct to do not show any font name.

But it will also not report any font name if the fonts are (partly) not defined. Since wptools works with attribute inheritance (similar to HTML/CSS) this can happen frequently.

The interface

CurrAttr

implements a workaround to report the default font in case of undefined fonts.

function TWPSetModeControl.GetFontName: TFontName;
begin
  Result := '';
  if Cursor.IsSelected then
    Cursor.SelectedTextAttr.GetFontName(Result)
  else Cursor.WritingTextAttr.GetFontName(Result);
{$IFNDEF DONT_REPORT_DEFAULT_ATTR} // also see DONT_REPORT_DEFAULT_ATTR_STYLE and DONT_REPORT_DEFAULT_ATTR_COLOR
  if (Result = '') and (Cursor.active_paragraph <> nil) and
    (Cursor.active_paragraph.ABaseStyle <> nil) then
    Result := Cursor.active_paragraph.ABaseStyle.AGetFontName;
  if Result = '' then
    FRichText.DefaultAttr.GetFontName(Result);
  if Result = '' then Result := DefaultFontName;
{$ENDIF}
end;

So if you use CurrAttr.FontName it should work for the current and the selected fonts to update the combo.

Julian

#2 ♥ 0

I've tried to implement the above, but didn't succeed till now.

Could you please tell me which events must be used (and how) to get the appropiate behaviour?
Please keep in mind that I use a DBWPRichText with DevExpress-TdxBarComboBox.

Regards,
Stef

#4 ♥ 0

Stef

I use the following to show the font in a TdxBarComboBox The code is in the OnChangeSelection event.

// Change the font combo and font size first
  frmReport.dxBarFontNameCombo1.Text :=  WPRichText1.CurrAttr.FontName;
  frmReport.dxBarComboFontSize.CurText := inttostr(Trunc(WPRichText1.CurrAttr.Size));

Rob.

#5 ♥ 0

Thank you.

Only the first line of code is sufficiënt.
When using the second line it is changing the size of selected text to one size just by selecting it (though the selected text contains two different fontsizes). Maybe that is because I use for both items a dxBarComboBox.

#6 ♥ 0

After some more testing I discovered that, the described behaviour also applies to selecing text with two different fonts.
It is changing the fonttype of selected text to one type just by selecting it (though the selected text contains two different fonttypes).

Could that be related to the dxBarComboBox?

#7 ♥ 0

What I need is the following:

  1. When selecting some text with the same fonttype, the control will show the description of the fonttype in the dxBarComboBox.
  2. When selecting some text with both defined and undefined fonttypes, the control will show the description of the defined fonttype in the dxBarComboBox.
  3. When selecting some text with two different defined fonttypes, the control will show no description in the dxBarComboBox.

Please keep in mind that I use a TDBWPRichText component.

One more question:
How to make sure that text in the editor will always be defined, aspecially when typing new text directly into the editor.

#8 ♥ 0
support

Hi,

WPRichText1.CurrAttr.FontName

is a simplified property. It will display a font even if currently there is a) no font defined (using default font) or b) different fonts are active in selection.

In my code I use a more complicated approach which uses the new WPTools 5 Attribute Interfaces (SelTextAttr and WritingTextAttr)

The code is in unit WPCtrRich.PAS:

if IsSelected then
begin
if not SelectedTextAttr.GetFontName(fnt) then
fnt := ' ';
FToolBar.UpdateSelection(wptName, fnt, 0);
end else FToolBar.UpdateSelection(wptName, CurrAttr.FontName, 0);

Here the WPToolBar is updated.

In this code the combo is updated using CurrAttr.FontName if no text is selected, othewise the SeletectedTextAttr.GetFontName function(!) is used. That function returns FALSE if the selection uses different fonts.

You can convert this code to DX as

if WPRichText.IsSelected then
    begin
        if not WPRichText.SelectedTextAttr.GetFontName(fnt) then
           fnt := '  ';
        dxBarFontNameCombo1.Text :=  := fnt;
    end 
    else dxBarFontNameCombo1.Text := WPRichText.CurrAttr.FontName;
  1. When selecting some text with both defined and undefined fonttypes, the control will show the description of the defined fonttype in the dxBarComboBox.

I don't see a chance to do that - undefined + defined = undefined in the SelectedTextAttr.

How to make sure that text in the editor will always be defined, aspecially when typing new text directly into the editor.

When loading RTF text the fonts which equal the default fonts are removed. If you dio not like this handling (it is required for proper paragraph style operation) you can disable this using the compiler define DONT_FIX_DEFSTYLE (used by WPIOReadRTF)

When typing RTF text the property WriteTextAttr is used. Here you can check with GetFontName if one is defined. OnClear is a good event to initialize this interface.

Regards,

Julian Ziersch

#9 ♥ 0

Can you tell me where I can set: define DONT_FIX_DEFSTYLE ?
And also how to use the property WriteTextAttr with checking GetFontName. OnClear is a good event to initialize this interface???

#10 ♥ 0
support

Can you tell me where I can set: define DONT_FIX_DEFSTYLE ?

IDE Menu, Project Options, Conditionals,
entere there and do a BuildAll

And also how to use the property WriteTextAttr with checking GetFontName.

var f : string;
if not WP.WritingTextAttr.GetFontName(f) then
WP.WritingTextAttr.SetFontName('Arial');

OnClear is a good event to initialize this interface???

OnClear is a published event of TWPRichText

#11 ♥ 0

Hi Julian,

Below you find some code that I've added to the OnChangeSelection of a TDBWPRichText (as you suggested).

When text is selected and it is the default font, then the FontNameCombo will not show this font.
Is there a way to show the default font also when it is selected? And at the same time NOT show any font in the FontNameCombo, when the selected text contains multiple fonts.

procedure TFDossierTypes.WPTEditorTypesChangeSelection(Sender: TObject);
var fnt : TFontname;
begin
if WPTEditorTypes.IsSelected then
begin
if not WPTEditorTypes.SelectedTextAttr.GetFontName(fnt) then
fnt := '';
FontNameCombo.Text := fnt;
end else
FontNameCombo.Text := WPTEditorTypes.CurrAttr.FontName;
end;

#12 ♥ 0
support

To change this behavior a change in the cor unit is required. I have implemented this change as default in upcoming release - it will be possible to switch it off.

Julian

#13 ♥ 0

Thank you very much.
When do you plan to release the new version?

#14 ♥ 0
support

You're welcome,

the new release is now online.

Julian

#15 ♥ 0

Hi Julian,

My problem is still not solved with version 5.20.5.

When I select text only the default font is showing in the fontnamecombobox, other fonts are always blank.
Also the fontsize is never mentioned when I select text.

procedure TFDossierTypes.WPTEditorTypesChangeSelection(Sender: TObject);
var fnt : TFontname;
begin
  if WPTEditorTypes.IsSelected then
  begin
    if not WPTEditorTypes.SelectedTextAttr.GetFontName(fnt) then // Fontname bestaat niet of is leeg
      fnt := '';
    mbLettertype.Text := fnt;
  end else
    mbLettertype.Text := WPTEditorTypes.CurrAttr.FontName;
end;

Please solve this problem a.s.a.p. as it is such basic functionality for any wordprocessor.

Regards,
Stef

#16 ♥ 0
support

Hi,

I made a mistake when testing the change. Infact it worked - sometimes - but qwhen developing a test app I found out that selected text which used a font always reported a NULL font. I had to reports V5.20.5 - also to the chance to include a change to make it work with C++Builder 2006 Update 2. (I had to wait for German release of the update.)

My demo is using a simple combo box "mbLettertype".

Its uses this code:

procedure TForm1.FormCreate(Sender: TObject);
begin
  mbLettertype.Items := Screen.Fonts;
  WPRichText1Clear(nil);
end;

// Button: Load Text
procedure TForm1.Button1Click(Sender: TObject);
begin
  WPRichText1.Load();
end;

// Button: Clear Text
procedure TForm1.Button2Click(Sender: TObject);
begin
  WPRichText1.Clear();
end;

// Update the "Toolbar"
procedure TForm1.mbLettertypeChange(Sender: TObject);
begin
   // We do not want to assign fonts which do not exist!
   if (mbLettertype.Text='') or
      (mbLettertype.Items.IndexOf(mbLettertype.Text)>=0) then
      WPRichText1.CurrAttr.FontName := mbLettertype.Text;
end;

// Apply change in "Toolbar" to text
procedure TForm1.WPRichText1CharacterAttrChange(Sender: TObject;
  Attribute: TWPSetModeControl);
var fnt : TFontName;
begin
  if WPRichText1.IsSelected then
  begin 
    if not WPRichText1.SelectedTextAttr.GetFontName(fnt) then fnt := '';
    mbLettertype.Text := fnt;
  end else 
    mbLettertype.Text := WPRichText1.CurrAttr.FontName; 
end;

// This is executed after "clear". 
procedure TForm1.WPRichText1Clear(Sender: TObject);
begin
  WPRichText1.DefaultAttr.Clear;
  WPRichText1.DefaultAttr.SetFontName('Courier New');
  WPRichText1.DefaultAttr.SetFontSize(10);
  WPRichText1.WritingAttr.Clear;
end;

Now it appears to work.

Julian

#17 ♥ 0
This category is archived. No new posts are possible.