unit VisPlugServ;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, ActiveX, Classes, ComObj, CM2VisInterface, SysUtils, CM2EditorSupportInterface;

type
  TMyPlugin = class(TComObject, ICM2VisPlug, ICM2EditorSupportVis20)
  protected
   Function Name(OUT Name: Pchar): HResult; stdcall;
   Function Version(OUT Version: word): HResult; stdcall; // Uses MMRB Notation
   Function RequiredCoreVersion(OUT RCV: Word): HResult; stdcall; // Uses MMRB Notation - Please refer to CM2Plugtypes for details
  // Declare Error commands
   Function GetLastErrorMsg(OUT Msg: PChar): HResult; stdcall;
  // Declare Info system
   Function GetInfoCount(OUT InfoCount: Integer): HResult; stdcall;
   Function GetInfo(Index: Integer; OUT InfoName: PChar; OUT InfoVar: PChar): HResult; stdcall;
  // Declare Visual Basics
   Function SetDC(CONST NewDC: THandle): HResult; stdcall;
   Function SetDimensions(Left, Top, Width, Height : Integer): HResult; stdcall;
   Function SetConfigString(NewConfigString: PChar): HResult; stdcall;
  // Declare "Event" functions
   Function Redraw: HResult; stdcall;
   Function Update(SensorStyle: Byte; NewString: PChar; NewValue: Double): HResult; stdcall;
   Function Init(PaintDC: THandle): HResult; stdcall;
   Function Terminating: HResult; stdcall;
   Function Setup: HResult; stdcall;
  // Declare Support types
   // Default (Required)
   Function GetDefaultCount(OUT DefaultCount: Integer): HResult; stdcall;
   Function GetDefault(Index: Integer; OUT Name: Pchar; OUT Value: PChar; OUT Description: PChar): HResult; stdcall;
   // Optional
   Function GetOptionalCount(OUT OptionalCount: Integer): HResult; stdcall;
   Function GetOptional(Index: Integer; OUT Name: Pchar; OUT Value: PChar; OUT Description: PChar): HResult; stdcall;
   // Editor Support
   Function ObjIns(ParameterName: PChar; OUT ValueType: Byte; OUT ValueRange: PChar): HResult; stdcall;
   Function OnEditor(ParameterName: PChar; OUT Value): HResult; stdcall;
   Function GetMenuCount(OUT MenuCount: Byte): HResult; stdcall;
   Function GetMenuItem(Index: Byte; OUT MenuTitle: PChar; OUT MenuHint: PChar): HResult; stdcall;
   Function OnMenu(Index: Byte; OUT ResultString: PChar): HResult; stdcall;
  end;

const
  Class_MyPlugin: TGUID = '{823B31A3-9174-4844-9E4A-CBCD2EDED533}';

implementation

uses ComServ, MySetupDialog;

{ TMyPlugin }

function TMyPlugin.GetDefault(Index: Integer; out Name, Value,
  Description: PChar): HResult;
begin
 result := S_OK;
 Case Index of
  0: Begin Name := 'About'; Value := 'Click Me'; Description := 'Look at the About dialog for the plugin'; end;
 end;
end;

function TMyPlugin.GetDefaultCount(out DefaultCount: Integer): HResult;
begin
 DefaultCount := 1;
 Result := S_OK;
end;

function TMyPlugin.GetInfo(Index: Integer; out InfoName,
  InfoVar: PChar): HResult;
begin
 Case Index of
  0: Begin InfoName := 'Programmed By'; InfoVar := 'C. Aaangel'; End;
  1: Begin InfoName := 'Created'; InfoVar := '28th Nov 2005'; End;
  2: Begin InfoName := 'Copyright'; InfoVar := 'C. Aaangel'; end;
  3: Begin InfoName := 'Updated'; InfoVar := '28th Nov 2005'; end;
 end;
 Result := S_OK;
end;

function TMyPlugin.GetInfoCount(out InfoCount: Integer): HResult;
begin
 InfoCount := 4;
 Result := S_OK;
end;

function TMyPlugin.GetLastErrorMsg(out Msg: PChar): HResult;
begin
 Msg := 'GetLastErrorMSG Not implemented';
 Result := S_OK;
end;

function TMyPlugin.GetMenuCount(out MenuCount: Byte): HResult;
begin
 Result := S_OK;
 MenuCount := 1;
end;

function TMyPlugin.GetMenuItem(Index: Byte; out MenuTitle,
  MenuHint: PChar): HResult;
begin
 Case Index of
  0: Begin MenuTitle := 'Setup'; MenuHint := 'Launch the Setup Dialog'; end;
 end;
 Result := S_OK;
end;

function TMyPlugin.GetOptional(Index: Integer; out Name, Value,
  Description: PChar): HResult;
begin
 Result := S_OK;
end;

function TMyPlugin.GetOptionalCount(out OptionalCount: Integer): HResult;
begin
 OptionalCount := 0;
 Result := S_OK;
end;

function TMyPlugin.Init(PaintDC: THandle): HResult;
begin
 Result := S_OK;
end;

function TMyPlugin.Name(out Name: Pchar): HResult;
begin
 Name := 'SetupDialog';
 result := S_OK;
end;

function TMyPlugin.ObjIns(ParameterName: PChar; out ValueType: Byte;
  out ValueRange: PChar): HResult;
begin
 ValueType := vtUnknown;
 ValueRange := '';
 If uppercase(ParameterName) = 'ABOUT' then begin
  ValueType := vtEditor;
  ValueRange := '';
 end;
 Result := S_OK;
end;

function TMyPlugin.OnEditor(ParameterName: PChar; out Value): HResult;
begin
 Result := S_OK;
 If uppercase(ParameterName) = 'ABOUT' then begin
  Result := Setup;
 end;
end;

function TMyPlugin.OnMenu(Index: Byte; out ResultString: PChar): HResult;
begin
 Result := S_OK;
 Case Index of
  0: Begin
      Result := Setup;
      ResultString := '';
     end;
 end;
end;

function TMyPlugin.Redraw: HResult;
begin
 Result := S_OK;
end;

function TMyPlugin.RequiredCoreVersion(out RCV: Word): HResult;
begin
 RCV := 0400;
 Result := S_OK;
end;

function TMyPlugin.SetConfigString(NewConfigString: PChar): HResult;
begin
 Result := S_OK;
end;

function TMyPlugin.SetDC(const NewDC: THandle): HResult;
begin
 Result := S_OK;
end;

function TMyPlugin.SetDimensions(Left, Top, Width,
  Height: Integer): HResult;
begin
 Result := S_OK;
end;

function TMyPlugin.Setup: HResult;
Var
 SetupDlg: TSetupDlg;
begin
 // This is where you want to write in the magic ;-)
 // You can create your form just like you normally do, then you create and load
 // it dynamicly from within this function.
 // this function is ran when setup is chosen in the Plugin Manager.
 // CM2 must be restarted (if running) to adopt changes
 SetupDlg := TSetupDlg.Create(nil); // Alternatively you can use the DC handle to allow Z order.
 SetupDlg.Execute(3);
 FreeAndNil(SetupDlg);
 Result := S_OK;
end;

function TMyPlugin.Terminating: HResult;
begin
 Result := S_OK;
end;

function TMyPlugin.Update(SensorStyle: Byte; NewString: PChar;
  NewValue: Double): HResult;
begin
 Result := S_OK;
end;

function TMyPlugin.Version(out Version: word): HResult;
begin
 Version := 0200;
 Result := S_OK;
end;

initialization
  TComObjectFactory.Create(ComServer, TMyPlugin, Class_MyPlugin,
    'MyPlugin', '', ciMultiInstance, tmApartment);
end.
