@@ -13,7 +13,7 @@ interface
1313 SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
1414 StdCtrls, ComCtrls, SynEditHighlighter, SynHighlighterSQL,
1515 SynEdit, laz.VirtualTrees, SynEditKeyCmds, ActnList, Menus,
16- dbstructures, RegExpr, Generics.Collections, EditBtn, LCLType,
16+ dbstructures, RegExpr, Generics.Collections, EditBtn, LCLType, StrUtils,
1717 extra_controls, reformatter, Buttons, ColorBox, LCLProc, LCLIntf, lazaruscompat, FileUtil,
1818 vktable;
1919
@@ -217,8 +217,9 @@ TfrmPreferences = class(TExtForm)
217217 Column: TColumnIndex; var Ghosted: Boolean; var ImageIndex: Integer);
218218 procedure TreeShortcutItemsFocusChanged (Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex);
219219 procedure TreeShortcutItemsGetNodeDataSize (Sender: TBaseVirtualTree; var NodeDataSize: Integer);
220- procedure HotKeyEnter (Sender: TObject);
221- procedure HotKeyExit (Sender: TObject);
220+ procedure TreeShortcutItemsFocusChanging (Sender: TBaseVirtualTree; OldNode,
221+ NewNode: PVirtualNode; OldColumn, NewColumn: TColumnIndex;
222+ var Allowed: Boolean);
222223 procedure comboGridTextColorsSelect (Sender: TObject);
223224 procedure colorBoxGridTextColorsSelect (Sender: TObject);
224225 procedure editMySQLBinariesRightButtonClick (Sender: TObject);
@@ -243,6 +244,7 @@ TfrmPreferences = class(TExtForm)
243244 procedure InitLanguages ;
244245 procedure SelectDirectory (Sender: TObject; NewFolderButton: Boolean);
245246 function EnsureShortcutIsUnused (RequestShortcut: TShortCut): Boolean;
247+ procedure ShowShortCut (Index: Integer);
246248 public
247249 { Public declarations }
248250 end ;
@@ -405,6 +407,7 @@ procedure TfrmPreferences.Apply(Sender: TObject);
405407 AppSettings.WriteBool(asTabsToSpaces, chkTabsToSpaces.Checked);
406408
407409 // Shortcuts
410+ TreeShortcutItems.FocusedNode := nil ; // Triggers OnFocusCanging and applies current changes
408411 CatNode := TreeShortcutItems.GetFirst;
409412 while Assigned(CatNode) do begin
410413 ItemNode := TreeShortcutItems.GetFirstChild(CatNode);
@@ -633,12 +636,8 @@ procedure TfrmPreferences.FormCreate(Sender: TObject);
633636 FShortcutCategories.Add(_(' SQL editing' ));
634637 TreeShortcutItems.RootNodeCount := FShortcutCategories.Count;
635638 comboLineBreakStyle.Items := Explode(' ,' , _(' Windows linebreaks' )+' ,' +_(' UNIX linebreaks' )+' ,' +_(' Mac OS linebreaks' ));
636- comboShortcut1Key.Clear;
637- comboShortcut2Key.Clear;
638- for i:=Low(VKcodes) to High(VKcodes) do begin
639- comboShortcut1Key.Items.Add(VKcodes[i].Name );
640- comboShortcut2Key.Items.Add(VKcodes[i].Name );
641- end ;
639+ GetVKNames(comboShortcut1Key.Items);
640+ GetVKNames(comboShortcut2Key.Items);
642641
643642 comboReformatter.Items.Add(_(' Always ask' ));
644643 Reformatter := TfrmReformatter.Create(Self);
@@ -1087,18 +1086,18 @@ procedure TfrmPreferences.SynMemoSQLSampleClick(Sender: TObject);
10871086
10881087procedure TfrmPreferences.btnRemoveHotKeyClick (Sender: TObject);
10891088begin
1090- // Clear current shortcut
1089+ // Clear shortcut controls, and let the OnFocusChanging event store the removed value.
10911090 if Sender = btnRemoveHotKey1 then begin
10921091 chkShortcut1Shift.Checked := False;
10931092 chkShortcut1Alt.Checked := False;
10941093 chkShortcut1Control.Checked := False;
1095- comboShortcut1Key.ItemIndex := - 1 ;
1094+ comboShortcut1Key.ItemIndex := GetVKIndexByCode(VK_UNKNOWN) ;
10961095 end
10971096 else if Sender = btnRemoveHotKey2 then begin
10981097 chkShortcut2Shift.Checked := False;
10991098 chkShortcut2Alt.Checked := False;
11001099 chkShortcut2Control.Checked := False;
1101- comboShortcut2Key.ItemIndex := - 1 ;
1100+ comboShortcut2Key.ItemIndex := GetVKIndexByCode(VK_UNKNOWN) ;
11021101 end
11031102 else
11041103 Beep;
@@ -1127,8 +1126,6 @@ procedure TfrmPreferences.TreeShortcutItemsFocusChanged(Sender: TBaseVirtualTree
11271126var
11281127 ShortcutFocused: Boolean;
11291128 Data: PShortcutItemData;
1130- Key: Word;
1131- Shift: TShiftState;
11321129begin
11331130 // Shortcut item focus change in tree
11341131 ShortcutFocused := Assigned(Node) and (Sender.GetNodeLevel(Node) = 1 );
@@ -1148,13 +1145,9 @@ procedure TfrmPreferences.TreeShortcutItemsFocusChanged(Sender: TBaseVirtualTree
11481145 if MainForm.ActionList1DefaultHints[Data.Action.Index] <> ' ' then
11491146 lblShortcutHint.Caption := MainForm.ActionList1DefaultHints[Data.Action.Index];
11501147 end ;
1151- // FHotKey1.HotKey := Data.ShortCut1;
1152- // FHotKey2.HotKey := Data.ShortCut2;
1153- ShortCutToKey(Data.ShortCut1, Key, Shift);
1154- chkShortcut1Shift.Checked := ssShift in Shift;
1155- chkShortcut1Alt.Checked := ssAlt in Shift;
1156- chkShortcut1Control.Checked := ssCtrl in Shift;
1157- comboShortcut1Key.ItemIndex := GetVKIndexByCode(Key);
1148+
1149+ ShowShortCut(1 );
1150+ ShowShortCut(2 );
11581151 end ;
11591152 chkShortcut2Shift.Enabled := lblShortcut2.Enabled;
11601153 chkShortcut2Alt.Enabled := lblShortcut2.Enabled;
@@ -1163,6 +1156,30 @@ procedure TfrmPreferences.TreeShortcutItemsFocusChanged(Sender: TBaseVirtualTree
11631156 btnRemoveHotKey2.Enabled := lblShortcut2.Enabled;
11641157end ;
11651158
1159+ procedure TfrmPreferences.ShowShortCut (Index: Integer);
1160+ var
1161+ Data: PShortcutItemData;
1162+ Key: Word;
1163+ Shift: TShiftState;
1164+ begin
1165+ Data := TreeShortcutItems.GetNodeData(TreeShortcutItems.FocusedNode);
1166+ case Index of
1167+ 1 : begin
1168+ ShortCutToKey(Data.ShortCut1, Key, Shift);
1169+ chkShortcut1Shift.Checked := ssShift in Shift;
1170+ chkShortcut1Alt.Checked := ssAlt in Shift;
1171+ chkShortcut1Control.Checked := ssCtrl in Shift;
1172+ comboShortcut1Key.ItemIndex := GetVKIndexByCode(Key);
1173+ end ;
1174+ 2 : begin
1175+ ShortCutToKey(Data.ShortCut2, Key, Shift);
1176+ chkShortcut2Shift.Checked := ssShift in Shift;
1177+ chkShortcut2Alt.Checked := ssAlt in Shift;
1178+ chkShortcut2Control.Checked := ssCtrl in Shift;
1179+ comboShortcut2Key.ItemIndex := GetVKIndexByCode(Key);
1180+ end ;
1181+ end ;
1182+ end ;
11661183
11671184procedure TfrmPreferences.TreeShortcutItemsGetImageIndex (Sender: TBaseVirtualTree; Node: PVirtualNode;
11681185 Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean; var ImageIndex: Integer);
@@ -1323,23 +1340,51 @@ function TfrmPreferences.EnsureShortcutIsUnused(RequestShortcut: TShortCut): Boo
13231340
13241341end ;
13251342
1326-
1327- procedure TfrmPreferences.HotKeyEnter (Sender: TObject);
1343+ procedure TfrmPreferences.TreeShortcutItemsFocusChanging (
1344+ Sender: TBaseVirtualTree; OldNode, NewNode: PVirtualNode; OldColumn,
1345+ NewColumn: TColumnIndex; var Allowed: Boolean);
1346+ var
1347+ Data: PShortcutItemData;
1348+ NewShortCut: TShortCut;
1349+ Shift: TShiftState;
13281350begin
1329- // Remove Esc and Enter shortcuts from buttons
1330- btnOk.Default := False;
1331- btnCancel.Cancel := False;
1332- end ;
1333-
1351+ // Check if shortcut 1 or 2 changed
1352+ if (not Assigned(OldNode)) or (TreeShortcutItems.GetNodeLevel(OldNode) = 0 ) then
1353+ Exit;
1354+ Data := TreeShortcutItems.GetNodeData(OldNode);
1355+ Allowed := True;
1356+
1357+ Shift := [];
1358+ if chkShortcut1Shift.Checked then Include(Shift, ssShift);
1359+ if chkShortcut1Alt.Checked then Include(Shift, ssAlt);
1360+ if chkShortcut1Control.Checked then Include(Shift, ssCtrl);
1361+ NewShortCut := KeyToShortCut(VKcodes[comboShortcut1Key.ItemIndex].Code, Shift);
1362+ if NewShortCut <> Data.ShortCut1 then begin
1363+ if EnsureShortcutIsUnused(NewShortCut) then begin
1364+ Data.ShortCut1 := NewShortCut;
1365+ Modified(Sender);
1366+ end
1367+ else
1368+ ShowShortCut(1 );
1369+ end ;
13341370
1335- procedure TfrmPreferences.HotKeyExit (Sender: TObject);
1336- begin
1337- // Readd Esc and Enter shortcuts to buttons
1338- btnOk.Default := True;
1339- btnCancel.Cancel := True;
1371+ if chkShortcut2Shift.Enabled then begin
1372+ Shift := [];
1373+ if chkShortcut2Shift.Checked then Include(Shift, ssShift);
1374+ if chkShortcut2Alt.Checked then Include(Shift, ssAlt);
1375+ if chkShortcut2Control.Checked then Include(Shift, ssCtrl);
1376+ NewShortCut := KeyToShortCut(VKcodes[comboShortcut2Key.ItemIndex].Code, Shift);
1377+ if NewShortCut <> Data.ShortCut2 then begin
1378+ if EnsureShortcutIsUnused(NewShortCut) then begin
1379+ Data.ShortCut2 := NewShortCut;
1380+ Modified(Sender);
1381+ end
1382+ else
1383+ ShowShortCut(2 );
1384+ end ;
1385+ end ;
13401386end ;
13411387
1342-
13431388procedure TfrmPreferences.InitLanguages ;
13441389var
13451390 LangNames: String;
0 commit comments