Page 1 of 1

OnImportPicture

Posted: Wed Mar 22, 2023 8:38 am
by ravn
Why won't this work.
If I use format I don't get the error message but filename is still empty.

Graphic := RVGraphicHandler.LoadFromStream(Stream);
Rve.SetCurrentItemExtraStrProperty(rvespImageFileName,RVU_AnsiToUnicode(CP_ACP,'test.png'),True);
// I Get error her: EListError with message 'List index out of bounds (-1)'.


Thanks in advance

Re: OnImportPicture

Posted: Wed Mar 22, 2023 12:59 pm
by Sergey Tkachenko
1. SetCurrent*** methods work with the item at the caret position.
The caret position is defined only when the editor is formatted, so formatting is necessary before calling this method.

2. SetCurrentItemExtraStrProperty works only if the current item supports the specified property, otherwise it does nothing. For rvespImageFileName, the item must be a picture.

3. Delphi automatically converts string literals to Unicode when necessary, so you can simply write
Rve.SetCurrentItemExtraStrProperty(rvespImageFileName, 'test.png' ,True);

4. You create a graphic but do not insert it. If you want to insert a picture at the caret position and assign its file name, write

Code: Select all

Graphic := RVGraphicHandler.LoadFromStream(Stream);
if Graphic = nil then
  exit;
if Rve.InsertPicture('', Graphic, rvvaBaseline) then
  Rve.SetCurrentItemExtraStrProperty(rvespImageFileName, 'test.png', True);
However, the code above contains two editing operations (insertion and assigning a file name), so they can be undone by the user in two steps.
Solution:

Code: Select all

Graphic := RVGraphicHandler.LoadFromStream(Stream);
if Graphic = nil then
  exit;
rve.TopLevelEditor.BeginUndoGroup(rvutInsert);
rve.TopLevelEditor.SetUndoGroupMode(True);
if Rve.InsertPicture('', Graphic, rvvaBaseline) then
  Rve.SetCurrentItemExtraStrProperty(rvespImageFileName, 'test.png', True);
rve.TopLevelEditor.SetUndoGroupMode(False);