library PowerPaint;

{ A Power Plugin DLL should simply have 3 Procedures.
  1 - Init
      GetVerbs will have a parameter of pointer.The application will call this
      init procedure by passing its instance to it. This is a standart procedure
      and should be implemeted same in all plugins.

  2 - GetVerbs
      GetVerbs will return a pchar including the name of the Verbs called from the
      application. Note that these verb names will be taken by the application to
      the menu, and each menu click will call the third exported procedure (DoVerb)
      with the verb index which is in fact the index of the verb in the string list.

  3 - DoVerb
      DoVerb has one parameter. It stands for the index of the verb to be
      called. In this procedure you will do your actions by using the procedure
      and functions that are defined in the interface of the PCPlgLib unit that
      you should include in the uses clause of this dll dpr.

 }

uses
  SysUtils,
  Classes,
  Windows,
  Messages,
  Graphics,
  Controls,
  Forms,
  StdCtrls,
  Buttons,
  Grids,
  Dialogs,
  PCPlgLib in '../PCPlgLib.pas',
  PaintForm in 'PaintForm.pas' {frmPaint};

procedure Init(Owner: Integer); stdcall;
begin
  GetAdresses(Owner);
  // Plugin specific intialization code
end;

Function GetVerbs:PChar;stdcall;
var res: pchar;
Begin
  res := 'Open PowerPaint';
  result := res;
End;

Procedure OpenPaint;
var i,f,cnt:Integer;
    cName: String;
    xBmp: Tbitmap;
    fBmp: Integer;
    frm: TFrmPaint;
    fig,fHandle: Integer;
    mStream: TmemoryStream;
    buf:Pbyte;
    xSize: Integer;
    fName: String;
    xByte: Byte;
Begin

  xBmp := nil;
  i := 0;
  fig := 0;
  fName := 'c:\ppbmp.bmp';
  cnt := pcGetPropSelectedCount;
  if cnt > 0 then begin
    repeat
      fHandle := pcGetSelectedHandle(i);
      cName := String(pcFigureGetClass(fHandle));
      if Copy(uppercase(cName) ,1,4) = 'TBMP' then
      begin
        fig := fHandle;
      end;
      inc(i);
    until (fig > 0) or (i >= Cnt);
  end;
  buf := nil;
  mStream := nil;

  if fig > 0 then begin

    pcFigureSaveBitmapToFile(fig,pchar(fName));
    Sleep(250);
    xBmp := Tbitmap.Create;
    xBmp.LoadFromFile(fName);
    buf := PByte(pcGetFigureCustomStream(fig,xSize));
    if assigned(buf) and (xSize > 0) then begin
      mStream := TMemoryStream.Create;
      mStream.Write(buf^,xsize);
      mStream.Position := 0;
      //FreeMem(Buf,xSize);
    end;
  end else begin

  end;

  frm := TFrmPaint.Create(nil);

  if frm.EditBitmap(xBmp,mStream) then
  begin
    xBmp.SaveToFile(fName);
    if fig > 0 then begin
      pcFigureLoadBitmapFromFile(fig,pchar(fName));
    end else begin
      fig := pcInsertBitmap(0,0,0,pchar(fName),0,1);
    end;
    if frm.Layers.Count > 1 then begin
      mStream := TmemoryStream.Create;
      frm.SaveToStream(mStream);
      GetMem(buf,mStream.Size);
      mStream.Position := 0;
      mStream.Read(buf^,mStream.Size);
      pcSetFigureCustomStream(fig,mStream.Size,buf^);
      mStream.Free;
      FreeMem(buf);
    end else begin
      buf := nil;
      xByte := 0;
      pcSetFigureCustomStream(fig,0,xByte);
    end;
  end;
  frm.free;
End;

Procedure DoVerb(VerbIndex: integer);stdcall;
Begin
  if VerbIndex = 0 then OpenPaint;
end;

  exports
    Init name 'Init',
    GetVerbs name 'GetVerbs',
    DoVerb name 'DoVerb';

begin

end.
