Generalizing a TRVAPopup Event

General TRichView support forum. Please post your questions here
Post Reply
DickBryant
Posts: 148
Joined: Wed Dec 07, 2005 2:02 pm
Contact:

Generalizing a TRVAPopup Event

Post by DickBryant »

Hi Sergey,

I got the RVAPopupMenu.OnPopup working fine with Live Spelling but only if I hard-code the rve involved - as it is in the Demo. I'm trying to generalize this so that it will work with multiple .rve's (I have many in my multi-tab editor) using the code below, but I get an Invalid Class Typecast error on the line

Result := Target as TCustomRichViewEdit;

in the code below. I THOUGHT I was emulating the way you generalize the actions in TRVActions in this code, but I'm obviously missing something.

procedure TFEditor.RVAPopupMenu1Popup(Sender: TObject);
var
index: Integer;
NewItem: TMenuItem;
rve: TCustomRichViewEdit;

wrd: String;
stl, i: Integer;
sl: TStringList;
mi: TMenuItem;

function GetControl(Target: TObject): TCustomRichViewEdit;
var Control: TCustomRichViewEdit;
begin
if Control<>nil then
Target := Control
else if RVA_DefaultControl<>nil then
Target := RVA_DefaultControl;
{if Target<>nil then
while Target is TRVTableInplaceEdit do
Target := TRVTableInplaceEdit(Target).Parent; }
Result := Target as TCustomRichViewEdit;
end;

begin
inherited;
rve := GetControl(Sender);

if rve.GetCurrentMisspelling(True, wrd, stl) then
begin
while RVAPopupMenu1.Items.Count>0 do
RVAPopupMenu1.Items[0].Free;
sl := TStringList.Create;
RVASpell31.Suggest(wrd, sl);
for i := 0 to sl.Count-1 do begin
mi := TMenuItem.Create(PopupMenu1);
mi.Caption := sl;
mi.OnClick := PopupMenuItemClick;
RVAPopupMenu1.Items.Add(mi);
end;
if sl.Count=0 then begin
mi := TMenuItem.Create(RVAPopupMenu1);
mi.Caption := '(no suggestions)';
mi.Enabled := False;
RVAPopupMenu1.Items.Add(mi);
end;

mi := TMenuItem.Create(RVAPopupMenu1);
mi.Caption := '-';
RVAPopupMenu1.Items.Add(mi);

mi := TMenuItem.Create(RVAPopupMenu1);
mi.Caption := '&Add to Dictionary';
mi.OnClick := PopupMenuAddToDictionaryClick;
RVAPopupMenu1.Items.Add(mi);

mi := TMenuItem.Create(PopupMenu1);
mi.Caption := '&Ignore All';
mi.OnClick := PopupMenuIgnoreAllClick;
RVAPopupMenu1.Items.Add(mi);

sl.Free;
end
else
begin
NewItem := TMenuItem.Create(RVAPopupMenu1); // create the new item
RVAPopupMenu1.Items.Add(NewItem);// add it to the Popupmenu
NewItem.Caption := 'Paste Special';
NewItem.ImageIndex := 9;
NewItem.OnClick := FEditor.PasteSpecial1Click;
{$IFDEF EDUCATOR}
NewItem := TMenuItem.Create(RVAPopupMenu1); // create the new item
RVAPopupMenu1.Items.Add(NewItem);// add it to the Popupmenu
NewItem.Caption := 'Thesaurus';
NewItem.ImageIndex := 4;
NewItem.OnClick := FEditor.Thesaurus1Click;
{$ENDIF}
end;
end;
Post Reply