{ Double Commander ------------------------------------------------------------------------- Implements custom commands for a component Copyright (C) 2011-2012 Przemyslaw Nagay (cobines@gmail.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA } unit uFormCommands; {$mode objfpc}{$H+} interface uses Classes, SysUtils, StringHashList, ActnList; type TCommandFuncResult = (cfrSuccess, cfrDisabled, cfrNotFound); TCommandFunc = procedure(const Params: array of string) of object; TCommandCaptionType = (cctShort, cctLong); (* The commands are 'user' functions which can be assigned to toolbar button, hotkey, menu item, executed by scripts, etc. Only published functions and procedures can by found by MethodAddress. How to set up a form to handle hotkeys: 1. Specify that the form class implements IFormCommands (class (TForm, IFormCommands)). 2. Add private FCommands: TFormCommands that will implement the interface. 3. Add property that will specify that FCommands implements the interface in place of the form. property Commands: TFormCommands read FCommands{$IF FPC_FULLVERSION >= 020501} implements IFormCommands{$ENDIF}; For FPC < 2.5.1 "implements" does not work correctly so the form must implement the interface itself. For example see fViewer. {$IF FPC_FULLVERSION < 020501} // "implements" does not work in FPC < 2.5.1 function ExecuteCommand(Command: string; Param: String=''): TCommandFuncResult; function GetCommandCaption(Command: String; CaptionType: TCommandCaptionType): String; procedure GetCommandsList(List: TStrings); {$ENDIF} 4. Make sure a default constructor Create(TheOwner: TComponent) is present which will create the FCommands on demand when it is needed to read the hotkeys when the form is not currently created. 5. Register the form and action list in HotkeyManager somewhere in constructor: const HotkeysCategory = ; HMForm := HotMan.Register(Self, HotkeysCategory); HMForm.RegisterActionList(actionList); And unregister in destructor: HotMan.UnRegister(Self); 6. Register form as commands form so that it is displayed in Options: initialization TFormCommands.RegisterCommandsForm(Tfrm..., HotkeysCategory, @rsHotkeyCategory...); *) { IFormCommands } {$interfaces corba} // If a form/object implements this interface then it can execute custom // commands with parameters. IFormCommands = interface ['{0464B1C0-BA98-4258-A286-F0F726FF66C4}'] function ExecuteCommand(Command: String; const Params: array of string): TCommandFuncResult; function GetCommandCaption(Command: String; CaptionType: TCommandCaptionType = cctShort): String; procedure GetCommandsList(List: TStrings); end; {$interfaces default} // Used to filter out commands. If this function returns True the command // is not included in the commands list. TCommandFilterFunc = function (Command: String): Boolean of object; TCommandRec = record Address: Pointer; //