unit UFormKeyShortcuts;

{$mode delphi}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ComCtrls, ActnList, LCLProc, Menus;

type

  { TFormKeyShortcuts }

  TFormKeyShortcuts = class(TForm)
    ListView1: TListView;
    MenuItem1: TMenuItem;
    PopupMenu1: TPopupMenu;
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
  private
  public
    procedure LoadFromComponent(C: TComponent);
  end;

var
  FormKeyShortcuts: TFormKeyShortcuts;

implementation

uses
  UFormMain, UCore;

resourcestring
  SMainForm = 'Main window';
  SGlobal = 'Global';

{ TFormKeyShortcuts }

procedure TFormKeyShortcuts.FormShow(Sender: TObject);
begin
  Core.PersistentForm.Load(Self);
  Core.ThemeManager1.UseTheme(Self);
  try
    ListView1.BeginUpdate;
    ListView1.Clear;
    LoadFromComponent(Core);
    LoadFromComponent(FormMain);
  finally
    ListView1.EndUpdate;
  end;
end;

procedure TFormKeyShortcuts.MenuItem1Click(Sender: TObject);
begin
  if Assigned(ListView1.Selected) then
    TAction(ListView1.Selected.Data).Execute;
end;

procedure TFormKeyShortcuts.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  Core.PersistentForm.Save(Self);
end;

procedure TFormKeyShortcuts.FormCreate(Sender: TObject);
begin
  Core.CoolTranslator1.TranslateComponentRecursive(Self);
end;

procedure TFormKeyShortcuts.LoadFromComponent(C: TComponent);
var
  I: Integer;
  NewItem: TListItem;
begin
  if C is TActionList then
  with TActionList(C) do begin
    for I := 0 to ActionCount - 1 do
      if Actions[I] is TAction then
        with TAction(Actions[I]) do
        //if ShortCut > 0 then
        begin
          NewItem := ListView1.Items.Add;
          NewItem.Caption := Caption;
          NewItem.ImageIndex := ImageIndex;
          NewItem.Data := TAction(Actions[I]);
          with NewItem.SubItems do begin
            if C.Owner is TFormMain then Add(SMainForm)
            else if C.Owner is TDataModule then Add(SGlobal)
            else if C.Owner is TForm then Add(TForm(C.Owner).Caption)
            else Add(C.Name);
            Add(ShortCutToText(ShortCut));
          end;
        end;
  end;
  for I := 0 to C.ComponentCount - 1 do
    LoadFromComponent(C.Components[I]);
end;

initialization
  {$I UFormKeyShortcuts.lrs}

end.

