RichViewActions, how to get the executing richedit?

General TRichView support forum. Please post your questions here
SpoQSI
Posts: 16
Joined: Wed Sep 02, 2015 11:48 am

RichViewActions, how to get the executing richedit?

Post by SpoQSI »

Hello,

in the RichViewActions Demo, which i costumize, i need costum buttons in the toolbar.
Normal OnClick-Events don´t work, as i get exceptions with buttons without any action applied.

Therefor i wanted to create custom Actions, but i don´t know how to access the richview (scalerichview) which is on the executing form.

Here i want to open a Template-RTF which i created.

Code: Select all

procedure TsrvActionsResource.OpenPlainTemplateActionExecute(Sender: TObject);
begin

end;
In examples i found this:

Code: Select all

procedure TrvActionsResource.rvActionEvent1Execute(Sender: TObject;
  Editor: TCustomRichViewEdit);
var gr: TGraphic;
    Tag: TRVTag;
    Align: TRVVAlign;
    s: TRVAnsiString;
    info: String;
begin
  Editor.GetCurrentPictureInfo(s, gr, Align, Tag);
  info := Format('%s %dx%d', [gr.ClassName, gr.Width, gr.Height]);
  Application.MessageBox(PChar(info), 'Image Info', MB_OK or MB_ICONINFORMATION);
end;

procedure TrvActionsResource.rvActionEvent1Update(Sender: TObject;
  Editor: TCustomRichViewEdit);
var CurStyleNo: Integer;
begin
  CurStyleNo := Editor.CurItemStyle;
  (Sender as TrvActionEvent).Enabled := (CurStyleNo=rvsPicture) or
    (CurStyleNo=rvsHotPicture);
end;
But my Action-Events don´t have the "Editor: TCustomRichViewEdit".

How can i accomplish this?

Greetings, Spo
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Simplest way:

Do not create your own action classes. Use TrvActionEvent actions directly, assigning their OnExecute and OnUpdate events. If you want to know how to get TSRichViewEdit from Editor (TCustomRichViewEdit) parameter of these events, let me know.

---

If you create your own actions inherited from TrvAction, override Execute and UpdateTarget methods. Use GetControl(Target) to get the editor from the Target parameter (where GetControl is a method inherited from TrvAction)

---

If you create your own actions inherited from TsrvAction, everything is the same, but GetControl returns TSRichViewEdit.

---

Assigning OnClick of toolbar buttons must work as well.
SpoQSI
Posts: 16
Joined: Wed Sep 02, 2015 11:48 am

Post by SpoQSI »

I don´t understand what i have to do now. in your example in http://www.trichview.com/forums/viewtopic.php?t=4981 you say to use the Action Editor to add a new action. This is what i did. I have:

Code: Select all

    OpenPlainTemplateAction: TAction;
    procedure OpenPlainTemplateActionExecute(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  srvActionsResource: TsrvActionsResource;

implementation

{$R *.dfm}

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

procedure TsrvActionsResource.OpenPlainTemplateActionExecute(Sender: TObject);
begin
  // Here i want to access the RichEdit to load a Template RTF <---------------
end;

If i try to use the OnClick Events of a TToolButton in the MainUnit i get a EListError-Exception in "TCustomrichViewEdit.UpdateAction" where "Result := inherited UpdateAction(Action);".
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I cannot reproduce this bug.
I took the ScaleRichView demo in Demos\ActionTest.
I added a new TAction in dmActionsSRV.pas, assigned its OnExecute event. Then I added a button on the toolbar and assigned this action to it.

By the way, you added TAction, not TrvActionEvent like it was explained in the topic.

--

Please send me a sample project reproducing this bug. Also, tell me which version of Delphi and TRichView you use.
SpoQSI
Posts: 16
Joined: Wed Sep 02, 2015 11:48 am

Post by SpoQSI »

I have no bug assigning an Action to a Toolbutton. The bug, as described, occurs when i do something in the OnClick Event of a Button in the Toolbar.

I have Delphi 6 and the latest Non-Beta TRichView Version.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Please send a sample project (as simple as possible) reproducing this problem to email richviewgmailcom
SpoQSI
Posts: 16
Joined: Wed Sep 02, 2015 11:48 am

Post by SpoQSI »

A sample project is not needed. Open ActionTest (The Demo), add a button to the upper left corner (or somewhere else) on the toolbar and set a Click-Event.

Add this code to the button:

Code: Select all

procedure TForm3.Button2Click(Sender: TObject);
var appPath : string;
begin
        appPath := ExtractFilePath(Application.ExeName);
        SRichViewEdit1.RichViewEdit.Clear;
        SRichViewEdit1.RichViewEdit.LoadRTF(appPath + 'TemplateNew.rtf');
end;
And then you get an Exception.
SpoQSI
Posts: 16
Joined: Wed Sep 02, 2015 11:48 am

Post by SpoQSI »

Also, if i try it the TsrvAction-Way you mentioned i cannot compile the code as TCustomRichViewEdit is not know.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Actions cannot work with unformatted editor.
Add

Code: Select all

procedure TForm3.Button2Click(Sender: TObject); 
var appPath : string; 
begin 
        appPath := ExtractFilePath(Application.ExeName); 
        SRichViewEdit1.RichViewEdit.Clear; 
        SRichViewEdit1.RichViewEdit.LoadRTF(appPath + 'TemplateNew.rtf'); 
        [color=red]SRichViewEdit1.Format;[/color]
end; 
Also, if i try it the TsrvAction-Way you mentioned i cannot compile the code as TCustomRichViewEdit is not know.
Add RVEdit in uses of this unit.
SpoQSI
Posts: 16
Joined: Wed Sep 02, 2015 11:48 am

Post by SpoQSI »

Great, the two suggestions solved my problems with that. Thank you very much.

One more Question:
How exactly can i get the current Documents RTF-Text as string without saving it to an rtf file?

I need to save and load the documents from and to a Database.
SpoQSI
Posts: 16
Joined: Wed Sep 02, 2015 11:48 am

Post by SpoQSI »

Oh i noticed the problem is not solved completely. When i load an RTF now this way:

Code: Select all

procedure TsrvActionsResource.OpenNewPlainTemplateActionEventExecute(
  Sender: TObject; Editor: TCustomRichViewEdit);
var appPath : string;
begin
        appPath := ExtractFilePath(Application.ExeName);
        Editor.Clear;
        Editor.LoadRTF(appPath + 'TemplateNew.rtf');
        Editor.Format;
end;
There the RTF Documents Borders/Header/Footer-Preferences arent loaded properly, which results in Header/Footer images and Tables in the Document itself are cut.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

As for getting RTF:

Code: Select all

function GetRTFString(rv: TCustomRichView): AnsiString;
var Stream: TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  rv.SaveRTFToStream(Stream, False);
  SetLength(Result, Stream.Size);
  Stream.Position := 0;
  Stream.ReadBuffer(PAnsiChar(Result)^, Length(Result));
  Stream.Free;
end;
As for the error, I do not understand what exactly happens. Can you explain how to reproduce?
SpoQSI
Posts: 16
Joined: Wed Sep 02, 2015 11:48 am

Post by SpoQSI »

Correct:
This is what it should look like and what it looks, if i Load the RTF via the standard open button in the ActionTest-Demo
Image



Incorrect:
This is what it looks like when loaded with this code:

Code: Select all

procedure TsrvActionsResource.OpenNewPlainTemplateActionEventExecute(
  Sender: TObject; Editor: TCustomRichViewEdit);
var appPath : string;
begin
        appPath := ExtractFilePath(Application.ExeName);
        Editor.Clear;
        Editor.LoadRTF(appPath + 'TemplateNew.rtf');
        Editor.Format;
end;
Image
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Well, I understand.
Yes, when you load a document with headers and footers in SRichViewEdit.RichViewEdit, it's not enough to call SRichViewEdit.RichViewEdit.Format, you need to call SRichViewEdit.Format.

Use this procedure:

Code: Select all

procedure CompleteFormat(rve: TCustomRichViewEdit);
var
  Ifc: IRVScaleRichViewInterface;
begin
  Ifc := rve.RVData.GetScaleRichViewInterface;
  if Ifc <> nil then
    Ifc.Format
  else
    rve.FormatAll;
end;

Code: Select all

procedure TsrvActionsResource.OpenNewPlainTemplateActionEventExecute( 
  Sender: TObject; Editor: TCustomRichViewEdit); 
var appPath : string; 
begin 
        appPath := ExtractFilePath(Application.ExeName); 
        Editor.Clear; 
        Editor.LoadRTF(appPath + 'TemplateNew.rtf'); 
        [color=red]CompleteFormat(Editor);[/color]
end;
SpoQSI
Posts: 16
Joined: Wed Sep 02, 2015 11:48 am

Post by SpoQSI »

Thank you for the fast response, but i can´t find the unit i have to add to the usings in which CompleteFormat is held.
Post Reply