<commit_before>(*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

Copyright (c) <NAME>
*)
unit formkeys;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ButtonPanel,
  StdCtrls, Menus, ExtCtrls, IniFiles,
  LclType, LclProc, LazUTF8, LazFileUtils,
  ATSynEdit_Keymap,
  proc_globdata,
  proc_msg;

type
  { TfmKeys }

  TfmKeys = class(TForm)
    bAdd1: TButton;
    bAdd2: TButton;
    bClear1: TButton;
    bClear2: TButton;
    chkForLexer: TCheckBox;
    labelKey1: TLabel;
    labelKey2: TLabel;
    panelInput: TPanel;
    panelBtn: TButtonPanel;
    panelPress: TPanel;
    procedure bAdd1Click(Sender: TObject);
    procedure bAdd2Click(Sender: TObject);
    procedure bClear1Click(Sender: TObject);
    procedure bClear2Click(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure HelpButtonClick(Sender: TObject);
  private
    { private declarations }
    FKeyPressed: integer;
    procedure DoAddKey(var K: TATKeyArray);
    procedure DoClearKey(var K: TATKeyArray);
    procedure DoUpdate;
    function GetHotkey: integer;
  public
    { public declarations }
    Keys1, Keys2: TATKeyArray;
  end;

var
  fmKeys: TfmKeys;

procedure DoLocalize_FormKeys(F: TfmKeys);


implementation

{$R *.lfm}

procedure DoLocalize_FormKeys(F: TfmKeys);
const
  section = 'd_keys';
var
  ini: TIniFile;
  fn: string;
begin
  fn:= GetAppLangFilename;
  if not FileExists(fn) then exit;
  ini:= TIniFile.Create(fn);
  try
    with F do Caption:= ini.ReadString(section, '_', Caption);
    with F.panelBtn.OKButton do Caption:= msgButtonOk;
    with F.panelBtn.CancelButton do Caption:= msgButtonCancel;

    with F.bClear1 do Caption:= ini.ReadString(section, 'clr', Caption);
    with F.bAdd1 do Caption:= ini.ReadString(section, 'add', Caption);
    F.bClear2.Caption:= F.bClear1.Caption;
    F.bAdd2.Caption:= F.bAdd1.Caption;

    with F.panelPress do Caption:= ini.ReadString(section, 'wait', Caption);
    with F.chkForLexer do Caption:= ini.ReadString(section, 'lex', Caption);
  finally
    FreeAndNil(ini);
  end;
end;

{ TfmKeys }

procedure TfmKeys.FormShow(Sender: TObject);
begin
  DoUpdate;
end;

procedure TfmKeys.HelpButtonClick(Sender: TObject);
begin
  Modalresult:= mrNo;
end;

procedure TfmKeys.bClear1Click(Sender: TObject);
begin
  DoClearKey(Keys1);
  DoUpdate;
end;

procedure TfmKeys.bClear2Click(Sender: TObject);
begin
  DoClearKey(Keys2);
  DoUpdate;
end;

procedure TfmKeys.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
  if not panelPress.Visible then exit;

  if
    (key=VK_MENU) or
    (key=VK_LMENU) or
    (key=VK_RMENU) or
    (key=VK_CONTROL) or
    (key=VK_LCONTROL) or
    (key=VK_RCONTROL) or
    (key=VK_SHIFT) or
    (key=VK_LSHIFT) or
    (key=VK_RSHIFT) or
    (key=VK_LWIN) or
    (key=VK_RWIN) then
      begin key:= 0; exit end;

  FKeyPressed:= ShortCut(Key, Shift);
  key:= 0;
end;

procedure TfmKeys.DoClearKey(var K: TATKeyArray);
var
  i: integer;
begin
  for i:= 0 to High(K) do K[i]:= 0;
end;

procedure TfmKeys.bAdd1Click(Sender: TObject);
begin
  DoAddKey(Keys1);
  DoUpdate;
end;

procedure TfmKeys.bAdd2Click(Sender: TObject);
begin
  DoAddKey(Keys2);
  DoUpdate;
end;

procedure TfmKeys.DoAddKey(var K: TATKeyArray);
var
  newkey, index, i: integer;
begin
  newkey:= GetHotkey;
  if newkey=0 then exit;

  index:= -1;
  for i:= 0 to High(K) do
    if K[i]=0 then
      begin index:= i; break end;
  if index<0 then exit;

  K[index]:= newkey;
end;

function TfmKeys.GetHotkey: integer;
begin
  Result:= 0;

  panelPress.Align:= alClient;
  panelInput.Hide;
  panelPress.Show;

  FKeyPressed:= 0;
  repeat
    Application.ProcessMessages;
  until FKeyPressed<>0;
  Result:= FKeyPressed;

  panelPress.Hide;
  panelInput.Show;
end;

procedure TfmKeys.DoUpdate;
begin
  labelKey1.caption:= '1) '+ KeyArrayToString(Keys1);
  labelKey2.caption:= '2) '+ KeyArrayToString(Keys2);

  bAdd1.Enabled:= KeyArrayLength(Keys1)<cMaxKeyCombo;
  bAdd2.Enabled:= KeyArrayLength(Keys2)<cMaxKeyCombo;

  if bAdd1.Enabled then
    ActiveControl:= bAdd1
  else
  if bClear1.Enabled then
    ActiveControl:= bClear1;
end;
<commit_msg>fix: show form to input hotkey<commit_after>(*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

Copyright (c) <NAME>
*)
unit formkeys;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ButtonPanel,
  StdCtrls, Menus, ExtCtrls, IniFiles,
  LclType, LclProc, LazUTF8, LazFileUtils,
  ATSynEdit_Keymap,
  proc_globdata,
  proc_msg;

type
  { TfmKeys }

  TfmKeys = class(TForm)
    bAdd1: TButton;
    bAdd2: TButton;
    bClear1: TButton;
    bClear2: TButton;
    chkForLexer: TCheckBox;
    labelKey1: TLabel;
    labelKey2: TLabel;
    panelInput: TPanel;
    panelBtn: TButtonPanel;
    panelPress: TPanel;
    procedure bAdd1Click(Sender: TObject);
    procedure bAdd2Click(Sender: TObject);
    procedure bClear1Click(Sender: TObject);
    procedure bClear2Click(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure HelpButtonClick(Sender: TObject);
  private
    { private declarations }
    FKeyPressed: integer;
    procedure DoAddKey(var K: TATKeyArray);
    procedure DoClearKey(var K: TATKeyArray);
    procedure DoUpdate;
    function GetHotkey: integer;
  public
    { public declarations }
    Keys1, Keys2: TATKeyArray;
  end;

var
  fmKeys: TfmKeys;

procedure DoLocalize_FormKeys(F: TfmKeys);


implementation

{$R *.lfm}

procedure DoLocalize_FormKeys(F: TfmKeys);
const
  section = 'd_keys';
var
  ini: TIniFile;
  fn: string;
begin
  fn:= GetAppLangFilename;
  if not FileExists(fn) then exit;
  ini:= TIniFile.Create(fn);
  try
    with F do Caption:= ini.ReadString(section, '_', Caption);
    with F.panelBtn.OKButton do Caption:= msgButtonOk;
    with F.panelBtn.CancelButton do Caption:= msgButtonCancel;

    with F.bClear1 do Caption:= ini.ReadString(section, 'clr', Caption);
    with F.bAdd1 do Caption:= ini.ReadString(section, 'add', Caption);
    F.bClear2.Caption:= F.bClear1.Caption;
    F.bAdd2.Caption:= F.bAdd1.Caption;

    with F.panelPress do Caption:= ini.ReadString(section, 'wait', Caption);
    with F.chkForLexer do Caption:= ini.ReadString(section, 'lex', Caption);
  finally
    FreeAndNil(ini);
  end;
end;

{ TfmKeys }

procedure TfmKeys.FormShow(Sender: TObject);
begin
  DoUpdate;
end;

procedure TfmKeys.HelpButtonClick(Sender: TObject);
begin
  Modalresult:= mrNo;
end;

procedure TfmKeys.bClear1Click(Sender: TObject);
begin
  DoClearKey(Keys1);
  DoUpdate;
end;

procedure TfmKeys.bClear2Click(Sender: TObject);
begin
  DoClearKey(Keys2);
  DoUpdate;
end;

procedure TfmKeys.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
  if not panelPress.Visible then exit;

  if
    (key=VK_MENU) or
    (key=VK_LMENU) or
    (key=VK_RMENU) or
    (key=VK_CONTROL) or
    (key=VK_LCONTROL) or
    (key=VK_RCONTROL) or
    (key=VK_SHIFT) or
    (key=VK_LSHIFT) or
    (key=VK_RSHIFT) or
    (key=VK_LWIN) or
    (key=VK_RWIN) then
      begin key:= 0; exit end;

  FKeyPressed:= ShortCut(Key, Shift);
  key:= 0;
end;

procedure TfmKeys.DoClearKey(var K: TATKeyArray);
var
  i: integer;
begin
  for i:= 0 to High(K) do K[i]:= 0;
end;

procedure TfmKeys.bAdd1Click(Sender: TObject);
begin
  DoAddKey(Keys1);
  DoUpdate;
end;

procedure TfmKeys.bAdd2Click(Sender: TObject);
begin
  DoAddKey(Keys2);
  DoUpdate;
end;

procedure TfmKeys.DoAddKey(var K: TATKeyArray);
var
  newkey, index, i: integer;
begin
  newkey:= GetHotkey;
  if newkey=0 then exit;

  index:= -1;
  for i:= 0 to High(K) do
    if K[i]=0 then
      begin index:= i; break end;
  if index<0 then exit;

  K[index]:= newkey;
end;

function TfmKeys.GetHotkey: integer;
begin
  Result:= 0;

  panelPress.Align:= alClient;
  panelInput.Hide;
  panelBtn.Hide;
  panelPress.Show;

  FKeyPressed:= 0;
  repeat
    Application.ProcessMessages;
  until FKeyPressed<>0;
  Result:= FKeyPressed;

  panelPress.Hide;
  panelBtn.Show;
  panelInput.Show;
end;

procedure TfmKeys.DoUpdate;
begin
  labelKey1.caption:= '1) '+ KeyArrayToString(Keys1);
  labelKey2.caption:= '2) '+ KeyArrayToString(Keys2);

  bAdd1.Enabled:= KeyArrayLength(Keys1)<cMaxKeyCombo;
  bAdd2.Enabled:= KeyArrayLength(Keys2)<cMaxKeyCombo;

  if bAdd1.Enabled then
    ActiveControl:= bAdd1
  else
  if bClear1.Enabled then
    ActiveControl:= bClear1;
end;

{'fraction_non_alphanumeric': 0.11189207572837546, 'fraction_numerical': 0.012923704795374674, 'mean_word_length': 4.105324074074074, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1949969', 'n_tokens_mistral': 3448, 'n_tokens_neox': 3257, 'n_words': 831}