////////////////////////////////////////////////////////////////////////////////
// CM2EventTypes.pas
// -----------------
// This unit contains the event system types for CoolMon 2.
//
// Last Edit: September, 7th 2004
// Certified: -
//
// This unit is coded in : Delphi 7
//
// Unit is copyright (c) 2004, The CoolMon Project
////////////////////////////////////////////////////////////////////////////////
unit CM2EventTypes;

interface

Uses Windows, CM2PlugInTypes, CM2FormManager;

Procedure ExecuteEvent(Sender: String; x, y: Integer; Trigger: TTrigger; FormMgr: TCM2FormMgr);
Function StrToEventType(Str: String): TEventType;
Function EventTypeToStr(et: TEventType): String;
Function StrToModState(Str: String): TModifierKeyState;
Function ModStateToStr(ms: TModifierKeyState): String;
function CtrlDown: Boolean;
function ShiftDown: Boolean;
function AltDown: Boolean;

implementation

uses SysUtils, Filter, lpl_SysUtils, CM2Tree, Dialogs, CM2VisPlugTypes, CM2API; //Unit1,

Procedure ExecuteEvent(Sender: String; x, y: Integer; Trigger: TTrigger; FormMgr: TCM2FormMgr);
Var
 S, Param, RunCMD: String;
 I: Integer;
 pVO: PVISObject;
Begin
 If Uppercase(Copy(Trigger.Execute, 1, length('VISUAL:'))) = 'VISUAL:' then begin
  RunCMD := DelFilt(Trigger.execute, 1, ':');
  For I := 0 to FormMgr.Count - 1 do begin
   pVO := FormMgr[I].VISObjectList.GetVOByVI(RunCMD);
   If pVO <> nil then begin
    pVO^.OnEvent(Sender, X, Y, Trigger);
   end;
  end;
  exit;
 end;
 If uppercase(copy(Trigger.Execute, 1, length('API:'))) = 'API:' then begin
  RunCMD := delFilt(Trigger.Execute, 1, ':');
  CM2API.API(nil, PAnsiChar(RunCMD), S);
  exit;
 end;
 RunCMD := Trigger.Execute;
 // Take it that it's a simple run command
 Param := '';
 For I := 1 to CountFilt(Trigger.Parameters, '|') do begin
  S := GetFilt(Trigger.Parameters, I, '|');
  Param := Param + ' "' + GetFilt(S, 2, '=') + '"';
 end;
 ExecApp(RunCMD, Param);
End;

Function StrToEventType(Str: String): TEventType;
Begin
 Str := Uppercase(Str);
 Result := etUnknown;
 If (Str = 'DBLCLICK') or (Str = 'DOUBLECLICK') Then Result := etDblClick;
 If (Str = 'ONMOUSELEFTDOWN') or (Str = 'ONMOUSEDOWNLEFT') then result := etMouseDownLeft;
 If (Str = 'ONMOUSEMIDDLEDOWN') or (Str = 'ONMOUSEDOWNMIDDLE') then result := etMouseDownMiddle;
 If (Str = 'ONMOUSERIGHTDOWN') or (Str = 'ONMOUSERIGHTDOWN')  then result := etMouseDownRight;
 If (Str = 'ONMOUSELEFTUP') or (Str = 'ONMOUSEUPLEFT')  then Result := etMouseUpLeft;
 If (Str = 'ONMOUSEMIDDLEUP') or (Str = 'ONMOUSEUPMIDDLE')  then result := etMouseUpMiddle;
 If (Str = 'ONMOUSERIGHTUP') or (Str = 'ONMOUSEUPRIGHT')  then result := etMouseUpRight;
 If (Str = 'ONMOUSEWHEELANY') or (Str = 'ONMOUSEANYWHEEL')  then result := etMouseWheelAny;
 If (Str = 'ONMOUSEWHEELUP') or (Str = 'ONMOUSEUPWHEEL')  then Result := etMouseWheelUp;
 If (Str = 'ONMOUSEWHEELDOWN') or (Str = 'ONMOUSEDOWNWHEEL')  then Result := etMouseWheelDown;
 If (Str = 'ONMOUSEMOVE') or (Str = 'ONMOVEMOUSE')  then Result := etMouseMove;
End;

Function EventTypeToStr(et: TEventType): String;
Begin
 Result := 'Unknown';
 Case et of
  etDblClick: Result := 'OnDoubleClick';
  etMouseDownLeft: Result := 'OnMouseLeftDown';
  etMouseDownMiddle: Result := 'OnMouseMiddleDown';
  etMouseDownRight: Result := 'OnMouseRightDown';
  etMouseUpLeft: Result := 'OnMouseLeftUp';
  etMouseUpMiddle: Result := 'OnMouseMiddleUp';
  etMouseUpRight: Result := 'OnMouseRightUp';
  etMouseWheelAny: Result := 'OnMouseWheelAny';
  etMouseWheelDown: Result := 'OnMouseWheelDown';
  etMouseWheelUp: Result := 'OnMouseWheelUp';
  etMouseMove: Result := 'OnMouseMove';
 end;
end;

Function StrToModState(Str: String): TModifierKeyState;
Var
 I: Integer;
 Any, A, C, S: Boolean;
Begin
 Result := mksUnknown;
 A := False; C := False; S := False; Any := False;
 For I := 1 to CountFilt(Str, '+') do begin
  If uppercase(GetFilt(Str, I, '+')) = 'ALT' then A := True;
  If Uppercase(GetFilt(Str, I, '+')) = 'CTRL' then C := True;
  If Uppercase(GetFilt(Str, I, '+')) = 'SHIFT' then S := True;
  If Uppercase(GetFilt(Str, I, '+')) = 'ANY' then Any := true;
 end;
 If Not(A) and Not(C) and Not(S) then Result := mksNone;
 If Not(A) and Not(C) and S Then Result := mksShift;
 If A and Not(C) and Not(S) Then Result := mksAlt;
 If Not(A) and C and Not(S) Then Result := mksCtrl;
 If A and Not(C) and S Then Result := mksAltShift;
 If A and C and Not(S) Then Result := mksAltCtrl;
 If Not(A) and C and S then Result := mksCtrlShift;
 If A and C and S Then Result := mksAltCtrlShift;
 If Any then Result := mksAny;
end;

Function ModStateToStr(ms: TModifierKeyState): String;
Begin
 Result := 'Unknown';
 Case ms of
  mksNone: Result := 'None';
  mksShift: Result := 'Shift';
  mksAlt: Result := 'Alt';
  mksCtrl: Result := 'Ctrl';
  mksAltShift: Result := 'Alt+Shift';
  mksAltCtrl: Result := 'Alt+Ctrl';
  mksCtrlShift: Result := 'Ctrl+Shift';
  mksAltCtrlShift: Result := 'Alt+Ctrl+Shift';
  mksAny: Result := 'Any';
 End;
end;

function CtrlDown: Boolean;
var
  State: TKeyboardState;
begin
  GetKeyboardState(State);
  Result := ((State[VK_CONTROL] and 128) <> 0);
end;

function ShiftDown: Boolean;
var
  State: TKeyboardState;
begin
  GetKeyboardState(State);
  Result := ((State[VK_SHIFT] and 128) <> 0);
end;

function AltDown: Boolean;
var
  State: TKeyboardState;
begin
  // GetAsyncKeyState
  GetKeyboardState(State);
  Result := ((State[VK_MENU] and 128) <> 0);
end;

end.
