home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Runimage / Delphi50 / Source / Vcl / APPEVNTS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  13.3 KB  |  511 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {                                                       }
  6. {       Copyright (c) 1995,99 Inprise Corporation       }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit AppEvnts;
  11.  
  12. interface
  13.  
  14. uses Windows, Messages, SysUtils, Classes, Forms, ActnList;
  15.  
  16. type
  17.   TCustomApplicationEvents = class(TComponent)
  18.   private
  19.     FOnActionExecute: TActionEvent;
  20.     FOnActionUpdate: TActionEvent;
  21.     FOnException: TExceptionEvent;
  22.     FOnMessage: TMessageEvent;
  23.     FOnHelp: THelpEvent;
  24.     FOnHint: TNotifyEvent;
  25.     FOnIdle: TIdleEvent;
  26.     FOnDeactivate: TNotifyEvent;
  27.     FOnActivate: TNotifyEvent;
  28.     FOnMinimize: TNotifyEvent;
  29.     FOnRestore: TNotifyEvent;
  30.     FOnShortCut: TShortCutEvent;
  31.     FOnShowHint: TShowHintEvent;
  32.     procedure DoActionExecute(Action: TBasicAction; var Handled: Boolean);
  33.     procedure DoActionUpdate(Action: TBasicAction; var Handled: Boolean);
  34.     procedure DoActivate(Sender: TObject);
  35.     procedure DoDeactivate(Sender: TObject);
  36.     procedure DoException(Sender: TObject; E: Exception);
  37.     procedure DoIdle(Sender: TObject; var Done: Boolean);
  38.     function DoHelp(Command: Word; Data: Longint; var CallHelp: Boolean): Boolean;
  39.     procedure DoHint(Sender: TObject);
  40.     procedure DoMessage(var Msg: TMsg; var Handled: Boolean);
  41.     procedure DoMinimize(Sender: TObject);
  42.     procedure DoRestore(Sender: TObject);
  43.     procedure DoShowHint(var HintStr: string; var CanShow: Boolean;
  44.       var HintInfo: THintInfo);
  45.     procedure DoShortcut(var Msg: TWMKey; var Handled: Boolean);
  46.   protected
  47.     property OnActionExecute: TActionEvent read FOnActionExecute write FOnActionExecute;
  48.     property OnActionUpdate: TActionEvent read FOnActionUpdate write FOnActionUpdate;
  49.     property OnActivate: TNotifyEvent read FOnActivate write FOnActivate;
  50.     property OnDeactivate: TNotifyEvent read FOnDeactivate write FOnDeactivate;
  51.     property OnException: TExceptionEvent read FOnException write FOnException;
  52.     property OnIdle: TIdleEvent read FOnIdle write FOnIdle;
  53.     property OnHelp: THelpEvent read FOnHelp write FOnHelp;
  54.     property OnHint: TNotifyEvent read FOnHint write FOnHint;
  55.     property OnMessage: TMessageEvent read FOnMessage write FOnMessage;
  56.     property OnMinimize: TNotifyEvent read FOnMinimize write FOnMinimize;
  57.     property OnRestore: TNotifyEvent read FOnRestore write FOnRestore;
  58.     property OnShowHint: TShowHintEvent read FOnShowHint write FOnShowHint;
  59.     property OnShortCut: TShortCutEvent read FOnShortCut write FOnShortCut;
  60.   public
  61.     constructor Create(AOwner: TComponent); override;
  62.     procedure Activate;
  63.     procedure CancelDispatch;
  64.   end;
  65.  
  66.   TApplicationEvents = class(TCustomApplicationEvents)
  67.   published
  68.     property OnActionExecute;
  69.     property OnActionUpdate;
  70.     property OnActivate;
  71.     property OnDeactivate;
  72.     property OnException;
  73.     property OnIdle;
  74.     property OnHelp;
  75.     property OnHint;
  76.     property OnMessage;
  77.     property OnMinimize;
  78.     property OnRestore;
  79.     property OnShowHint;
  80.     property OnShortCut;
  81.   end;
  82.  
  83. implementation
  84.  
  85. uses Contnrs, Consts;
  86.  
  87. type
  88.   TMultiCaster = class(TComponent)
  89.   private
  90.     FAppEvents: TComponentList;
  91.     FDispatching: Boolean;
  92.     procedure DoActionExecute(Action: TBasicAction; var Handled: Boolean);
  93.     procedure DoActionUpdate(Action: TBasicAction; var Handled: Boolean);
  94.     procedure DoActivate(Sender: TObject);
  95.     procedure DoDeactivate(Sender: TObject);
  96.     procedure DoException(Sender: TObject; E: Exception);
  97.     procedure DoIdle(Sender: TObject; var Done: Boolean);
  98.     function DoHelp(Command: Word; Data: Longint; var CallHelp: Boolean): Boolean;
  99.     procedure DoHint(Sender: TObject);
  100.     procedure DoMessage(var Msg: TMsg; var Handled: Boolean);
  101.     procedure DoMinimize(Sender: TObject);
  102.     procedure DoRestore(Sender: TObject);
  103.     procedure DoShowHint(var HintStr: string; var CanShow: Boolean;
  104.       var HintInfo: THintInfo);
  105.     procedure DoShortcut(var Msg: TWMKey; var Handled: Boolean);
  106.     function GetCount: Integer;
  107.     function GetAppEvents(Index: Integer): TCustomApplicationEvents;
  108.   public
  109.     constructor Create(AOwner: TComponent); override;
  110.     destructor Destroy; override;
  111.     procedure Activate(AppEvent: TCustomApplicationEvents);
  112.     procedure AddAppEvent(AppEvent: TCustomApplicationEvents);
  113.     procedure CancelDispatch;
  114.     procedure CheckDispatching;
  115.  
  116.     property AppEvents[Index: Integer]: TCustomApplicationEvents
  117.       read GetAppEvents; default;
  118.     property Count: Integer read GetCount;
  119.   end;
  120.  
  121. var
  122.   MultiCaster: TMultiCaster = nil;
  123.  
  124. { TCustomApplicationEvents }
  125.  
  126. procedure TCustomApplicationEvents.Activate;
  127. begin
  128.   MultiCaster.Activate(Self);
  129. end;
  130.  
  131. procedure TCustomApplicationEvents.CancelDispatch;
  132. begin
  133.   MultiCaster.CancelDispatch;
  134. end;
  135.  
  136. constructor TCustomApplicationEvents.Create(AOwner: TComponent);
  137. begin
  138.   inherited Create(AOwner);
  139.   MultiCaster.AddAppEvent(Self);
  140. end;
  141.  
  142. procedure TCustomApplicationEvents.DoActionExecute(Action: TBasicAction;
  143.   var Handled: Boolean);
  144. begin
  145.   if Assigned(FOnActionExecute) then FOnActionExecute(Action, Handled);
  146. end;
  147.  
  148. procedure TCustomApplicationEvents.DoActionUpdate(Action: TBasicAction;
  149.   var Handled: Boolean);
  150. begin
  151.   if Assigned(FOnActionUpdate) then FOnActionUpdate(Action, Handled);
  152. end;
  153.  
  154. procedure TCustomApplicationEvents.DoActivate(Sender: TObject);
  155. begin
  156.   if Assigned(FOnActivate) then FOnActivate(Sender);
  157. end;
  158.  
  159. procedure TCustomApplicationEvents.DoDeactivate(Sender: TObject);
  160. begin
  161.   if Assigned(FOnDeactivate) then FOnDeactivate(Sender);
  162. end;
  163.  
  164. procedure TCustomApplicationEvents.DoException(Sender: TObject;
  165.   E: Exception);
  166. begin
  167.   if Assigned(FOnException) then FOnException(Sender, E);
  168. end;
  169.  
  170. function TCustomApplicationEvents.DoHelp(Command: Word; Data: Integer;
  171.   var CallHelp: Boolean): Boolean;
  172. begin
  173.   if Assigned(FOnHelp) then
  174.     Result := FOnHelp(Command, Data, CallHelp)
  175.   else Result := False;  
  176. end;
  177.  
  178. procedure TCustomApplicationEvents.DoHint(Sender: TObject);
  179. begin
  180.   if Assigned(FOnHint) then FOnHint(Sender);
  181. end;
  182.  
  183. procedure TCustomApplicationEvents.DoIdle(Sender: TObject; var Done: Boolean);
  184. begin
  185.   if Assigned(FOnIdle) then FOnIdle(Sender, Done);
  186. end;
  187.  
  188. procedure TCustomApplicationEvents.DoMessage(var Msg: TMsg; var Handled: Boolean);
  189. begin
  190.   if Assigned(FOnMessage) then FOnMessage(Msg, Handled);
  191. end;
  192.  
  193. procedure TCustomApplicationEvents.DoMinimize(Sender: TObject);
  194. begin
  195.   if Assigned(FOnMinimize) then FOnMinimize(Sender);
  196. end;
  197.  
  198. procedure TCustomApplicationEvents.DoRestore(Sender: TObject);
  199. begin
  200.   if Assigned(FOnRestore) then FOnRestore(Sender);
  201. end;
  202.  
  203. procedure TCustomApplicationEvents.DoShortcut(var Msg: TWMKey;
  204.   var Handled: Boolean);
  205. begin
  206.   if Assigned(FOnShortcut) then FOnShortcut(Msg, Handled);
  207. end;
  208.  
  209. procedure TCustomApplicationEvents.DoShowHint(var HintStr: string;
  210.   var CanShow: Boolean; var HintInfo: THintInfo);
  211. begin
  212.   if Assigned(FOnShowHint) then FOnShowHint(HintStr, CanShow, HintInfo);
  213. end;
  214.  
  215. { TMultiCaster }
  216.  
  217. procedure TMultiCaster.Activate(AppEvent: TCustomApplicationEvents);
  218. begin
  219.   CheckDispatching;
  220.   if FAppEvents.IndexOf(AppEvent) < FAppEvents.Count - 1 then
  221.   begin
  222.     FAppEvents.Remove(AppEvent);
  223.     FAppEvents.Add(AppEvent);
  224.   end;
  225. end;
  226.  
  227. procedure TMultiCaster.AddAppEvent(AppEvent: TCustomApplicationEvents);
  228. begin
  229.   if FAppEvents.IndexOf(AppEvent) = -1 then
  230.     FAppEvents.Add(AppEvent);
  231. end;
  232.  
  233. procedure TMultiCaster.CancelDispatch;
  234. begin
  235.   FDispatching := False;
  236. end;
  237.  
  238. procedure TMultiCaster.CheckDispatching;
  239. begin
  240.   if FDispatching then
  241.     raise Exception.CreateRes(@sOperationNotAllowed);
  242. end;
  243.  
  244. constructor TMultiCaster.Create(AOwner: TComponent);
  245. begin
  246.   inherited Create(AOwner);
  247.   FAppEvents := TComponentList.Create(False);
  248.   with Application do
  249.   begin
  250.     OnActionExecute := DoActionExecute;
  251.     OnActionUpdate := DoActionUpdate;
  252.     OnActivate := DoActivate;
  253.     OnDeactivate := DoDeactivate;
  254.     OnException := DoException;
  255.     OnHelp := DoHelp;
  256.     OnHint := DoHint;
  257.     OnIdle := DoIdle;
  258.     OnMessage := DoMessage;
  259.     OnMinimize := DoMinimize;
  260.     OnRestore := DoRestore;
  261.     OnShowHint := DoShowHint;
  262.     OnShortCut := DoShortcut;
  263.   end;
  264. end;
  265.  
  266. destructor TMultiCaster.Destroy;
  267. begin
  268.   with Application do
  269.   begin
  270.     OnActionExecute := nil;
  271.     OnActionUpdate := nil;
  272.     OnActivate := nil;
  273.     OnDeactivate := nil;
  274.     OnException := nil;
  275.     OnHelp := nil;
  276.     OnHint := nil;
  277.     OnIdle := nil;
  278.     OnMessage := nil;
  279.     OnMinimize := nil;
  280.     OnRestore := nil;
  281.     OnShowHint := nil;
  282.     OnShortCut := nil;
  283.   end;
  284.   FAppEvents.Free;
  285.   inherited Destroy;
  286. end;
  287.  
  288. procedure TMultiCaster.DoActionExecute(Action: TBasicAction; var Handled: Boolean);
  289. var
  290.   I: Integer;
  291. begin
  292.   FDispatching := True;
  293.   try
  294.     for I := Count - 1 downto 0 do
  295.     begin
  296.       AppEvents[I].DoActionExecute(Action, Handled);
  297.       if not FDispatching then Break;
  298.     end;
  299.   finally
  300.     FDispatching := False;
  301.   end;
  302. end;
  303.  
  304. procedure TMultiCaster.DoActionUpdate(Action: TBasicAction; var Handled: Boolean);
  305. var
  306.   I: Integer;
  307. begin
  308.   FDispatching := True;
  309.   try
  310.     for I := Count - 1 downto 0 do
  311.     begin
  312.       AppEvents[I].DoActionUpdate(Action, Handled);
  313.       if not FDispatching then Break;
  314.     end;
  315.   finally
  316.     FDispatching := False;
  317.   end;
  318. end;
  319.  
  320. procedure TMultiCaster.DoActivate(Sender: TObject);
  321. var
  322.   I: Integer;
  323. begin
  324.   FDispatching := True;
  325.   try
  326.     for I := Count - 1 downto 0 do
  327.     begin
  328.       AppEvents[I].DoActivate(Sender);
  329.       if not FDispatching then Break;
  330.     end;
  331.   finally
  332.     FDispatching := False;
  333.   end;
  334. end;
  335.  
  336. procedure TMultiCaster.DoDeactivate(Sender: TObject);
  337. var
  338.   I: Integer;
  339. begin
  340.   FDispatching := True;
  341.   try
  342.     for I := Count - 1 downto 0 do
  343.     begin
  344.       AppEvents[I].DoDeactivate(Sender);
  345.       if not FDispatching then Break;
  346.     end;
  347.   finally
  348.     FDispatching := False;
  349.   end;
  350. end;
  351.  
  352. procedure TMultiCaster.DoException(Sender: TObject; E: Exception);
  353. var
  354.   I: Integer;
  355. begin
  356.   FDispatching := True;
  357.   try
  358.     for I := Count - 1 downto 0 do
  359.     begin
  360.       AppEvents[I].DoException(Sender, E);
  361.       if not FDispatching then Break;
  362.     end;
  363.   finally
  364.     FDispatching := False;
  365.   end;
  366. end;
  367.  
  368. function TMultiCaster.DoHelp(Command: Word; Data: Integer; var CallHelp: Boolean): Boolean;
  369. var
  370.   I: Integer;
  371. begin
  372.   FDispatching := True;
  373.   try
  374.     Result := False;
  375.     for I := Count - 1 downto 0 do
  376.     begin
  377.       Result := Result or AppEvents[I].DoHelp(Command, Data, CallHelp);
  378.       if not FDispatching then Break;
  379.     end;
  380.   finally
  381.     FDispatching := False;
  382.   end;
  383. end;
  384.  
  385. procedure TMultiCaster.DoHint(Sender: TObject);
  386. var
  387.   I: Integer;
  388. begin
  389.   FDispatching := True;
  390.   try
  391.     for I := Count - 1 downto 0 do
  392.     begin
  393.       AppEvents[I].DoHint(Sender);
  394.       if not FDispatching then Break;
  395.     end;
  396.   finally
  397.     FDispatching := False;
  398.   end;
  399. end;
  400.  
  401. procedure TMultiCaster.DoIdle(Sender: TObject; var Done: Boolean);
  402. var
  403.   I: Integer;
  404. begin
  405.   FDispatching := True;
  406.   try
  407.     for I := Count - 1 downto 0 do
  408.     begin
  409.       AppEvents[I].DoIdle(Sender, Done);
  410.       if not FDispatching then Break;
  411.     end;
  412.   finally
  413.     FDispatching := False;
  414.   end;
  415. end;
  416.  
  417. procedure TMultiCaster.DoMessage(var Msg: TMsg; var Handled: Boolean);
  418. var
  419.   I: Integer;
  420. begin
  421.   FDispatching := True;
  422.   try
  423.     for I := Count - 1 downto 0 do
  424.     begin
  425.       AppEvents[I].DoMessage(Msg, Handled);
  426.       if not FDispatching then Break;
  427.     end;
  428.   finally
  429.     FDispatching := False;
  430.   end;
  431. end;
  432.  
  433. procedure TMultiCaster.DoMinimize(Sender: TObject);
  434. var
  435.   I: Integer;
  436. begin
  437.   FDispatching := True;
  438.   try
  439.     for I := Count - 1 downto 0 do
  440.     begin
  441.       AppEvents[I].DoMinimize(Sender);
  442.       if not FDispatching then Break;
  443.     end;
  444.   finally
  445.     FDispatching := False;
  446.   end;
  447. end;
  448.  
  449. procedure TMultiCaster.DoRestore(Sender: TObject);
  450. var
  451.   I: Integer;
  452. begin
  453.   FDispatching := True;
  454.   try
  455.     for I := Count - 1 downto 0 do
  456.     begin
  457.       AppEvents[I].DoRestore(Sender);
  458.       if not FDispatching then Break;
  459.     end;
  460.   finally
  461.     FDispatching := False;
  462.   end;
  463. end;
  464.  
  465. procedure TMultiCaster.DoShortcut(var Msg: TWMKey; var Handled: Boolean);
  466. var
  467.   I: Integer;
  468. begin
  469.   FDispatching := True;
  470.   try
  471.     for I := Count - 1 downto 0 do
  472.     begin
  473.       AppEvents[I].DoShortcut(Msg, Handled);
  474.       if not FDispatching then Break;
  475.     end;
  476.   finally
  477.     FDispatching := False;
  478.   end;
  479. end;
  480.  
  481. procedure TMultiCaster.DoShowHint(var HintStr: string;
  482.   var CanShow: Boolean; var HintInfo: THintInfo);
  483. var
  484.   I: Integer;
  485. begin
  486.   FDispatching := True;
  487.   try
  488.     for I := Count - 1 downto 0 do
  489.     begin
  490.       AppEvents[I].DoShowHint(HintStr, CanShow, HintInfo);
  491.       if not FDispatching then Break;
  492.     end;
  493.   finally
  494.     FDispatching := False;
  495.   end;
  496. end;
  497.  
  498. function TMultiCaster.GetAppEvents(Index: Integer): TCustomApplicationEvents;
  499. begin
  500.   Result := TCustomApplicationEvents(FAppEvents[Index]);
  501. end;
  502.  
  503. function TMultiCaster.GetCount: Integer;
  504. begin
  505.   Result := FAppEvents.Count;
  506. end;
  507.  
  508. initialization
  509.   MultiCaster := TMultiCaster.Create(Application);
  510. end.
  511.