Page 1 of 1

Adding a hyperlink to a picture in code

Posted: Mon Jul 09, 2012 5:43 pm
by Vandy1Fan
I would like to add a hyperlink to an image in code. I'm going to setup an action where the user will select an image for use as "Add To Calendar". They will choose the image and my program will add the hyperlink behind it.

How would I do that?

Thanks for all your help. This is a great product!

Posted: Wed Jul 11, 2012 11:23 pm
by gavind
+1. I'm looking for the same answer. Image

Posted: Thu Jul 12, 2012 12:08 pm
by Sergey Tkachenko
Actually, both RichViewActions (TrvActionInsertHyperlink) and the demo in Demos\*\Assorted\Hypertext\CreateHyperlink\ support pictures. If pictures are in the selection, the action or the demo make them hyperlinks.

Ok, if you need a special command for pictures.

First, you need to enable/disable your command. The simplest way to check if there is a single picture selected is RichViewEdit1.GetSelectedImage<>nil. You can do this check in RichViewEdit1.OnSelect (or in action's update method).

As for the command itself.
The selected item is not necessary the current item (the current item is an item in the position of the caret; however, if the image is selected from its end to its beginning, not the image, but the previous item may be current). So you cannot use GetCurrent***/SetCurrent*** methods.
We can use RichViewEdit1.TopLevelEditor.GetSelectionBounds to get the selected item. But for simplification, the code below uses undocumeted RichViewEdit1.TopLevelEditor.RVData.GetSingleSelectedItem method.

Code: Select all

var rve: TCustomRichViewEdit;
    RVData: TCustomRVFormattedData;
    ItemNo: Integer;
begin
  rve := RichViewEdit1.TopLevelEditor;
  if not rve.RVData.GetSingleSelectedItem(RVData, ItemNo) then
    exit;
  // in our case, GetSingleSelectedItem returns RVData = rve.RVData
  {
  // uncomment if you do not want to change a picture that is already a hyperlink
  if rve.GetItemStyle(ItemNo)=rvsHotPicture then
    exit;
  }
  rve.BeginUndoGroup(rvutTag);
  rve.SetUndoGroupMode(True);
  try
    rve.ConvertToHotPicture(ItemNo);
    rve.SetItemTagEd(ItemNo, 'http://www.trichview.com');
  finally
    rve.SetUndoGroupMode(False);
  end;
end;