home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d56 / RMCTL.ZIP / rmKeyBindings.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-22  |  11KB  |  425 lines

  1. {================================================================================
  2. Copyright (C) 1997-2001 Mills Enterprise
  3.  
  4. Unit     : rmKeyBindings
  5. Purpose  : To allow the end user to assign or change the hot keys assigned to
  6.            actions in an action list.
  7. Date     : 05-03-2000
  8. Author   : Ryan J. Mills
  9. Version  : 1.80
  10. ================================================================================}
  11.  
  12. unit rmKeyBindings;
  13.  
  14. interface
  15.  
  16. {$I CompilerDefines.INC}
  17.  
  18. uses classes, ActnList;
  19.  
  20. type
  21.   TrmKeyBindingItem = class(TCollectionItem)
  22.   private
  23.    { Private }
  24.     fDesignLocked : boolean;
  25.     fCategory : string;
  26.     fActionCaption : string;
  27.     fActionName : string;
  28.     fShortCut : TShortCut;
  29.     fDescription : string;
  30.     fImageIndex : integer;
  31.     procedure SetShortcut(const Value: TShortCut);
  32.     procedure setDesignLocked(const Value: boolean);
  33.   public
  34.     constructor Create(Collection: TCollection); override;
  35.     destructor Destroy; override;
  36.     procedure Assign(Source: TPersistent); override;
  37.   published
  38.     property ActionName : string read fActionName write fACtionName;
  39.     property ActionCaption : string read fActionCaption write fActionCaption;
  40.     property Category : string read fCategory write fCategory;
  41.     property Description : String read fDescription write fDescription;
  42.     property ImageIndex : integer read fImageIndex write fImageIndex default -1;
  43.     property KeyBinding : TShortCut read fShortCut write SetShortcut default scNone;
  44.     property DesignLocked : boolean read fDesignLocked write setDesignLocked default false;
  45.   end;
  46.  
  47.   TrmKeyBindingCollection = class(TCollection)
  48.   private
  49.    { Private }
  50.     FOwner: TPersistent;
  51.     function GetItem(Index: Integer): TrmKeyBindingItem;
  52.     procedure SetItem(Index: Integer; Value: TrmKeyBindingItem);
  53.   protected
  54.    { Protected }
  55.     function GetOwner: TPersistent; override;
  56.   public
  57.    { Public }
  58.     constructor Create(AOwner: TPersistent);
  59.     function Add: TrmKeyBindingItem;
  60.     property Items[Index: Integer]: TrmKeyBindingItem read GetItem write SetItem; default;
  61.   end;
  62.  
  63.   TrmBindingStorage = class(TComponent)
  64.   private
  65.      fItems: TrmKeyBindingCollection;
  66.      procedure SetItem(const Value: TrmKeyBindingCollection);
  67.   public
  68.      constructor create(AOwner:TComponent); override;
  69.      destructor destroy; override;
  70.   published
  71.      property Items: TrmKeyBindingCollection read fItems write SetItem;
  72.   end;
  73.  
  74.   TrmKeyBindings = class(TComponent)
  75.   private
  76.    { Private }
  77.     fActions : TCustomActionList;
  78.     fItems : TrmKeyBindingCollection;
  79.     fDisplayName: boolean;
  80.     fMultiBinds: boolean;
  81.   protected
  82.    { Protected }
  83.     procedure SetActionList(const Value: TCustomActionList); Virtual;
  84.   public
  85.    { Public }
  86.     constructor create(AOwner:TComponent); override;
  87.     destructor destroy; override;
  88.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  89.  
  90.     function EditBindings:boolean;
  91.     procedure ApplyBindings;
  92.     procedure ClearBindings;
  93.  
  94.     procedure LoadBindingsFromFile(fileName:string; Binary:Boolean);
  95.     procedure LoadBindingsFromStream(Strm:TStream; Binary:Boolean);
  96.     procedure SaveBindingsToFile(FileName:string; Binary:Boolean);
  97.     procedure SaveBindingsToStream(Strm:TStream; Binary:Boolean);
  98.   published
  99.     { Published }
  100.     property Actions : TCustomActionList read fActions write SetActionList;
  101.     property DisplayActionName:boolean read fDisplayName write fDisplayName default false;
  102.     property AllowMultiBinds:boolean read fMultiBinds write fMultiBinds default true;
  103.   end;
  104.  
  105. implementation
  106.  
  107. uses Forms, Controls, sysutils, rmKeyBindingsEditForm;
  108.  
  109. { TrmKeyBindings }
  110.  
  111. procedure TrmKeyBindings.ApplyBindings;
  112. var
  113.    loop, loop1 : integer;
  114.    wAction : TCustomAction;
  115.    wCursor : TCursor;
  116. begin
  117.    wCursor := screen.cursor;
  118.    try
  119.       screen.Cursor := crHourGlass;
  120.       for loop := 0 to fItems.Count-1 do
  121.       begin
  122.          wAction := TCustomAction(fActions.Actions[loop]);
  123.          if wAction.Name = fItems[loop].ActionName then
  124.          begin
  125.             wAction.shortcut := fItems[loop].KeyBinding;
  126.             fActions.UpdateAction(wAction);
  127.          end
  128.          else
  129.          begin
  130.             for loop1 := 0 to fActions.ActionCount-1 do
  131.             begin
  132.                wAction := TCustomAction(fActions.Actions[loop1]);
  133.                if wAction.Name = fItems[loop].ActionName then
  134.                begin
  135.                   wAction.shortcut := fItems[loop].KeyBinding;
  136.                   fActions.UpdateAction(wAction);
  137.                   break;
  138.                end;
  139.             end;
  140.          end
  141.       end;
  142.    finally
  143.       screen.cursor := wCursor;
  144.    end;
  145. end;
  146.  
  147. procedure TrmKeyBindings.ClearBindings;
  148. begin
  149.    fItems.Clear;
  150. end;
  151.  
  152. constructor TrmKeyBindings.create(AOwner: TComponent);
  153. begin
  154.   inherited;
  155.   fItems := TrmKeyBindingCollection.create(self);
  156.   fDisplayName := false;
  157.   fMultiBinds := true;
  158. end;
  159.  
  160. destructor TrmKeyBindings.destroy;
  161. begin
  162.   fItems.Clear;   
  163.   fItems.free;   
  164.   inherited;
  165. end;
  166.  
  167. function TrmKeyBindings.EditBindings:boolean;
  168. var
  169.    frmEditor : TFrmEditKeyBindings;
  170. begin
  171.    frmEditor := TFrmEditKeyBindings.create(nil);
  172.    try
  173.       if assigned(Actions) and assigned(Actions.images) then
  174.          frmEditor.images := Actions.images;
  175.       frmEditor.Items := Self.fItems;
  176.       frmEditor.DisplayName := fDisplayName;
  177.       frmEditor.MultiBinding := fMultiBinds;
  178.       frmEditor.Designing := (csDesigning in ComponentState);
  179.       if (frmEditor.ShowModal = mrOK) then
  180.       begin
  181.          result := true;
  182.          Self.fItems.assign(frmEditor.Items);
  183.       end
  184.       else
  185.         result := false;
  186.    finally
  187.       frmEditor.free;
  188.    end;
  189. end;
  190.  
  191. procedure TrmKeyBindings.LoadBindingsFromFile(fileName: string; Binary:Boolean);
  192. var
  193.    wFile : TFileStream;
  194. begin
  195.    if fileexists(filename) then
  196.    begin
  197.       wFile := TFileStream.create(filename, fmOpenRead);
  198.       try
  199.          LoadBindingsFromStream(wFile, Binary);
  200.       finally
  201.          wFile.free;
  202.       end;
  203.    end;
  204. end;
  205.  
  206. procedure TrmKeyBindings.LoadBindingsFromStream(Strm: TStream; Binary:Boolean);
  207. var
  208.    wStorage : TComponent;
  209.    wTemp : TMemoryStream;
  210. begin
  211.    Strm.Position := 0;
  212.  
  213.    if Binary then
  214.       wStorage := TrmBindingStorage(Strm.ReadComponent(nil))
  215.    else
  216.    begin
  217.       wTemp := TMemoryStream.create;
  218.       try
  219.          ObjectTextToBinary(Strm, wTemp);
  220.          wTemp.position := 0;
  221.          wStorage := TrmBindingStorage(wTemp.ReadComponent(nil));
  222.       finally
  223.          wTemp.free;
  224.       end;
  225.    end;
  226.  
  227.    try
  228.       fItems.Assign(TrmBindingStorage(wStorage).items);
  229.    finally
  230.       wStorage.free;
  231.    end;
  232.    ApplyBindings;
  233. end;
  234.  
  235. procedure TrmKeyBindings.Notification(AComponent: TComponent; Operation: TOperation);
  236. begin
  237.   if (Operation = opRemove) then
  238.   begin
  239.        if (aComponent = fActions) then
  240.           fActions := nil;
  241.   end;
  242.  
  243.   inherited;
  244. end;
  245.  
  246. procedure TrmKeyBindings.SaveBindingsToFile(FileName: string; Binary:Boolean);
  247. var
  248.    wFile : TFileStream;
  249.    {$ifdef BD5}
  250.    wAttr : integer;
  251.    {$endif}
  252. begin
  253.    {$ifdef BD5}
  254.    if fileexists(filename) then
  255.    begin
  256.       wAttr := filegetAttr(filename);
  257.       if (wAttr and faReadonly <> 0) or (wAttr and faSysFile <> 0) then
  258.          Raise Exception.create('Unable to open file for writing');
  259.    end;
  260.    {$endif}
  261.    wFile := TFileStream.create(filename, fmCreate);
  262.    try
  263.       SaveBindingsToStream(wFile, Binary);
  264.    finally
  265.       wFile.free;
  266.    end;
  267. end;
  268.  
  269. procedure TrmKeyBindings.SaveBindingsToStream(Strm: TStream; Binary:Boolean);
  270. var
  271.    wStorage : TrmBindingStorage;
  272.    wTemp : TMemoryStream;
  273. begin
  274.    wStorage := TrmBindingStorage.create(self);
  275.    try
  276.       Strm.Position := 0;
  277.       wStorage.Items := fItems;
  278.       if Binary then
  279.          Strm.WriteComponent(wStorage)
  280.       else
  281.       begin
  282.          wTemp := TMemoryStream.create;
  283.          try
  284.             wTemp.WriteComponent(wStorage);
  285.             wTemp.Position := 0;
  286.             ObjectBinaryToText(wTemp, Strm)
  287.          finally
  288.             wTemp.free;
  289.          end;
  290.       end;
  291.    finally
  292.       wStorage.free;
  293.    end;
  294. end;
  295.  
  296. procedure TrmKeyBindings.SetActionList(const Value: TCustomActionList);
  297. var
  298.    loop : integer;
  299.    wAction : TCustomAction;
  300. begin
  301.    fActions := Value;
  302.    if assigned(fActions) then
  303.    begin
  304.       fActions.FreeNotification(self);
  305.       fItems.Clear;
  306.       loop := 0;
  307.       while loop < fActions.ActionCount do
  308.       begin
  309.          if fActions[loop] is TCustomAction then
  310.          begin
  311.             wAction := TCustomAction(factions[loop]);
  312.             with fItems.Add do
  313.             begin
  314.                DesignLocked := false;
  315.                ActionCaption := wAction.Caption;
  316.                ActionName := wAction.Name;
  317.                Category := wAction.Category;
  318.                KeyBinding := wAction.Shortcut;
  319.                ImageIndex := wAction.ImageIndex;
  320.                Description := wAction.Hint;
  321.             end;
  322.          end;
  323.          inc(loop);
  324.       end;
  325.    end;
  326. end;
  327.  
  328. { TrmKeyBindingItem }
  329.  
  330. procedure TrmKeyBindingItem.Assign(Source: TPersistent);
  331. begin
  332.   if Source is TrmKeyBindingItem then
  333.   begin
  334.     fActionCaption := TrmKeyBindingItem(Source).ActionCaption;
  335.     fActionName := TrmKeyBindingItem(Source).ActionName;
  336.     fCategory := TrmKeyBindingItem(Source).Category;
  337.     fDesignLocked := TrmKeyBindingItem(Source).DesignLocked;
  338.     fShortCut := TrmKeyBindingItem(Source).KeyBinding;
  339.     fDescription := TrmKeyBindingItem(Source).Description;
  340.     fImageIndex := TrmKeyBindingItem(Source).ImageIndex;
  341.   end
  342.   else
  343.   inherited Assign(Source);
  344. end;
  345.  
  346. constructor TrmKeyBindingItem.Create(Collection: TCollection);
  347. begin
  348.   inherited;
  349.   fShortCut := scNone;
  350.   fActionCaption := '';
  351.   fActionName := '';
  352.   fCategory := '';
  353.   fDesignLocked := false;
  354.   fDescription := '';
  355.   fImageIndex := -1;
  356. end;
  357.  
  358. destructor TrmKeyBindingItem.Destroy;
  359. begin
  360.   inherited;
  361. end;
  362.  
  363. procedure TrmKeyBindingItem.setDesignLocked(const Value: boolean);
  364. begin
  365.   fDesignLocked := Value;
  366. end;
  367.  
  368. procedure TrmKeyBindingItem.SetShortcut(const Value: TShortCut);
  369. begin
  370.   fShortCut := Value;
  371. end;
  372.  
  373. { TrmKeyBindingCollection }
  374.  
  375. function TrmKeyBindingCollection.Add: TrmKeyBindingItem;
  376. begin
  377.   Result := TrmKeyBindingItem(inherited Add);
  378. end;
  379.  
  380. constructor TrmKeyBindingCollection.Create(AOwner: TPersistent);
  381. begin
  382.   inherited Create(TrmKeyBindingItem);
  383.   fOwner := AOwner;
  384. end;
  385.  
  386. function TrmKeyBindingCollection.GetItem(Index: Integer): TrmKeyBindingItem;
  387. begin
  388.   Result := TrmKeyBindingItem(inherited GetItem(Index));
  389. end;
  390.  
  391. function TrmKeyBindingCollection.GetOwner: TPersistent;
  392. begin
  393.    Result := FOwner;
  394. end;
  395.  
  396. procedure TrmKeyBindingCollection.SetItem(Index: Integer; Value: TrmKeyBindingItem);
  397. begin
  398.   inherited SetItem(Index, Value);
  399. end;
  400.  
  401. { TrmBindingStorage }
  402.  
  403. constructor TrmBindingStorage.create(AOwner: TComponent);
  404. begin
  405.   inherited;
  406.   fItems := TrmKeyBindingCollection.Create(self);
  407. end;
  408.  
  409. destructor TrmBindingStorage.destroy;
  410. begin
  411.   fItems.Clear;
  412.   fItems.Free;
  413.   inherited;
  414. end;
  415.  
  416. procedure TrmBindingStorage.SetItem(const Value: TrmKeyBindingCollection);
  417. begin
  418.    fItems.Assign(Value);
  419. end;
  420.  
  421. initialization
  422.  RegisterClass(TrmBindingStorage);
  423.  
  424. end.
  425.