1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
{
***************************************************************************
* *
* This source 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 code 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. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
TShortCutGrabBox - a control to edit a shortcut
TShortCutDialog - a dialog to edit ide shortcuts
}
unit KeyMapShortCutDlg;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LCLProc, Forms, Controls, Graphics, Dialogs,
ExtCtrls, StdCtrls, LCLType,
PropEdits, IDECommands, IDEWindowIntf, IDEDialogs,
KeyMapping, LazarusIDEStrConsts, Buttons, ButtonPanel;
type
{ TShortCutDialog }
TShortCutDialog = class(TForm)
BtnPanel: TButtonPanel;
PrimaryGroupBox: TGroupBox;
SecondaryGroupBox: TGroupBox;
procedure CancelButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure OkButtonClick(Sender: TObject);
private
FKeyCommandRelationList: TKeyCommandRelationList;
FPrimaryKey1Box: TShortCutGrabBox;
FPrimaryKey2Box: TShortCutGrabBox;
FRelationIndex: integer;
FSecondaryKey1Box: TShortCutGrabBox;
FSecondaryKey2Box: TShortCutGrabBox;
FShowSecondary: boolean;
FShowSequence: boolean;
function GetPrimaryShortCut: TIDEShortCut;
function GetSecondaryShortCut: TIDEShortCut;
procedure SetPrimaryShortCut(const AValue: TIDEShortCut);
procedure SetSecondaryShortCut(const AValue: TIDEShortCut);
procedure SetShowSecondary(const AValue: boolean);
procedure SetShowSequence(const AValue: boolean);
function ResolveConflicts(Key: TIDEShortCut; Scope: TIDECommandScope): TModalResult;
procedure UpdateCaptions;
public
procedure ClearKeys;
procedure SetRelation(AKeyCommandRelationList: TKeyCommandRelationList;
Index: integer);
property KeyCommandRelationList: TKeyCommandRelationList
read FKeyCommandRelationList write FKeyCommandRelationList;
property RelationIndex: integer read FRelationIndex write FRelationIndex;
property ShowSecondary: boolean read FShowSecondary write SetShowSecondary;
property ShowSequence: boolean read FShowSequence write SetShowSequence;
property PrimaryKey1Box: TShortCutGrabBox read FPrimaryKey1Box;
property PrimaryKey2Box: TShortCutGrabBox read FPrimaryKey2Box;
property SecondaryKey1Box: TShortCutGrabBox read FSecondaryKey1Box;
property SecondaryKey2Box: TShortCutGrabBox read FSecondaryKey2Box;
property PrimaryShortCut: TIDEShortCut read GetPrimaryShortCut write SetPrimaryShortCut;
property SecondaryShortCut: TIDEShortCut read GetSecondaryShortCut write SetSecondaryShortCut;
end;
function ShowKeyMappingEditForm(Index: integer;
AKeyCommandRelationList: TKeyCommandRelationList): TModalResult;
implementation
{$R *.lfm}
function ShowKeyMappingEditForm(Index: integer;
AKeyCommandRelationList: TKeyCommandRelationList): TModalResult;
var
ShortCutDialog: TShortCutDialog;
begin
ShortCutDialog:=TShortCutDialog.Create(nil);
try
ShortCutDialog.ShowSecondary:=true;
ShortCutDialog.ShowSequence:=true;
ShortCutDialog.SetRelation(AKeyCommandRelationList,Index);
Result:=ShortCutDialog.ShowModal;
finally
ShortCutDialog.Free;
end;
end;
{ TShortCutDialog }
procedure TShortCutDialog.FormCreate(Sender: TObject);
begin
Caption := srkmEditForCmd;
BtnPanel.OKButton.OnClick := @OkButtonClick;
BtnPanel.CancelButton.OnClick := @CancelButtonClick;
IDEDialogLayoutList.ApplyLayout(Self, 480, 480);
FShowSecondary:=true;
FShowSequence:=true;
FPrimaryKey1Box:=TShortCutGrabBox.Create(Self);
with FPrimaryKey1Box do begin
Name:='FPrimaryKey1Box';
Align:=alClient;
AutoSize:=true;
BorderSpacing.Around:=6;
Parent:=PrimaryGroupBox;
end;
FPrimaryKey2Box:=TShortCutGrabBox.Create(Self);
with FPrimaryKey2Box do begin
Name:='FPrimaryKey2Box';
Align:=alBottom;
AutoSize:=true;
BorderSpacing.Around:=6;
Parent:=PrimaryGroupBox;
end;
PrimaryGroupBox.AutoSize:=true;
FSecondaryKey1Box:=TShortCutGrabBox.Create(Self);
with FSecondaryKey1Box do begin
Name:='FSecondaryKey1Box';
Align:=alClient;
AutoSize:=true;
BorderSpacing.Around:=6;
Parent:=SecondaryGroupBox;
end;
FSecondaryKey2Box:=TShortCutGrabBox.Create(Self);
with FSecondaryKey2Box do begin
Name:='FSecondaryKey2Box';
Align:=alBottom;
AutoSize:=true;
BorderSpacing.Around:=6;
Parent:=SecondaryGroupBox;
end;
SecondaryGroupBox.AutoSize:=true;
UpdateCaptions;
ClearKeys;
end;
procedure TShortCutDialog.FormShow(Sender: TObject);
begin
FPrimaryKey1Box.GrabButton.SetFocus;
end;
procedure TShortCutDialog.CancelButtonClick(Sender: TObject);
begin
IDEDialogLayoutList.SaveLayout(Self);
end;
procedure TShortCutDialog.OkButtonClick(Sender: TObject);
var
NewKeyA: TIDEShortCut;
NewKeyB: TIDEShortCut;
CurRelation: TKeyCommandRelation;
begin
IDEDialogLayoutList.SaveLayout(Self);
if KeyCommandRelationList=nil then begin
ModalResult:=mrOk;
exit;
end;
// set defaults
NewKeyA:=PrimaryShortCut;
NewKeyB:=SecondaryShortCut;
//debugln('TShortCutDialog.OkButtonClick A ShortcutA=',KeyAndShiftStateToEditorKeyString(NewKeyA),' ShortcutB=',KeyAndShiftStateToEditorKeyString(NewKeyB));
// get old relation
CurRelation:=KeyCommandRelationList.Relations[RelationIndex];
case ResolveConflicts(NewKeyA,CurRelation.Category.Scope) of
mrCancel: begin
debugln('TShortCutDialog.OkButtonClick ResolveConflicts failed for key1');
exit;
end;
mrRetry: begin
ModalResult:=mrNone;
exit;
end;
end;
//debugln('TShortCutDialog.OkButtonClick B ShortcutA=',KeyAndShiftStateToEditorKeyString(NewKeyA),' ShortcutB=',KeyAndShiftStateToEditorKeyString(NewKeyB));
if (NewKeyA.Key1=NewKeyB.Key1) and (NewKeyA.Shift1=NewKeyB.Shift1) and
(NewKeyA.Key2=NewKeyB.Key2) and (NewKeyA.Shift2=NewKeyB.Shift2) then
begin
NewKeyB.Key1:=VK_UNKNOWN;
NewKeyB.Shift1:=[];
NewKeyB.Key2:=VK_UNKNOWN;
NewKeyB.Shift2:=[];
end
else
case ResolveConflicts(NewKeyB,CurRelation.Category.Scope) of
mrCancel: begin
debugln('TShortCutDialog.OkButtonClick ResolveConflicts failed for key1');
exit;
end;
mrRetry: begin
ModalResult:=mrNone;
exit;
end;
end;
//debugln('TShortCutDialog.OkButtonClick C ShortcutA=',KeyAndShiftStateToEditorKeyString(NewKeyA),' ShortcutB=',KeyAndShiftStateToEditorKeyString(NewKeyB));
if NewKeyA.Key1=VK_UNKNOWN then
begin
NewKeyA:=NewKeyB;
NewKeyB.Key1:=VK_UNKNOWN;
NewKeyB.Shift1:=[];
NewKeyB.Key2:=VK_UNKNOWN;
NewKeyB.Shift2:=[];
end;
//debugln('TShortCutDialog.OkButtonClick D ShortcutA=',KeyAndShiftStateToEditorKeyString(NewKeyA),' ShortcutB=',KeyAndShiftStateToEditorKeyString(NewKeyB));
CurRelation.ShortcutA:=NewKeyA;
CurRelation.ShortcutB:=NewKeyB;
//debugln('TShortCutDialog.OkButtonClick B ShortcutA=',KeyAndShiftStateToEditorKeyString(NewKeyA),' ShortcutB=',KeyAndShiftStateToEditorKeyString(NewKeyB));
ModalResult:=mrOk;
end;
procedure TShortCutDialog.SetShowSecondary(const AValue: boolean);
begin
if FShowSecondary=AValue then exit;
FShowSecondary:=AValue;
SecondaryGroupBox.Visible:=FShowSecondary;
end;
procedure TShortCutDialog.SetShowSequence(const AValue: boolean);
begin
if FShowSequence=AValue then exit;
FShowSequence:=AValue;
FPrimaryKey2Box.Visible:=FShowSequence;
FSecondaryKey2Box.Visible:=FShowSequence;
// With a single key GrabBox focus OK button after keypress.
if not (FShowSecondary or FShowSequence) then
FPrimaryKey1Box.MainOkButton:=BtnPanel.OKButton;
UpdateCaptions;
end;
procedure TShortCutDialog.SetPrimaryShortCut(const AValue: TIDEShortCut);
var
APrimaryShortCut: TIDEShortCut;
begin
APrimaryShortCut:=GetPrimaryShortCut;
if CompareIDEShortCuts(@APrimaryShortCut,@AValue)=0 then exit;
PrimaryKey1Box.Key:=AValue.Key1;
PrimaryKey1Box.ShiftState:=AValue.Shift1;
PrimaryKey2Box.Key:=AValue.Key2;
PrimaryKey2Box.ShiftState:=AValue.Shift2;
end;
function TShortCutDialog.GetPrimaryShortCut: TIDEShortCut;
begin
Result.Key1:=PrimaryKey1Box.Key;
Result.Shift1:=PrimaryKey1Box.ShiftState;
Result.Key2:=PrimaryKey2Box.Key;
Result.Shift2:=PrimaryKey2Box.ShiftState;
end;
function TShortCutDialog.GetSecondaryShortCut: TIDEShortCut;
begin
Result.Key1:=SecondaryKey1Box.Key;
Result.Shift1:=SecondaryKey1Box.ShiftState;
Result.Key2:=SecondaryKey2Box.Key;
Result.Shift2:=SecondaryKey2Box.ShiftState;
end;
procedure TShortCutDialog.SetSecondaryShortCut(const AValue: TIDEShortCut);
var
ASecondaryShortCut: TIDEShortCut;
begin
ASecondaryShortCut:=SecondaryShortCut;
if CompareIDEShortCuts(@ASecondaryShortCut,@AValue)=0 then exit;
SecondaryKey1Box.Key:=AValue.Key1;
SecondaryKey1Box.ShiftState:=AValue.Shift1;
SecondaryKey2Box.Key:=AValue.Key2;
SecondaryKey2Box.ShiftState:=AValue.Shift2;
end;
function TShortCutDialog.ResolveConflicts(Key: TIDEShortCut;
Scope: TIDECommandScope): TModalResult;
type
TConflictType = (ctNone,ctConflictKeyA,ctConflictKeyB);
var
ConflictRelation: TKeyCommandRelation;
ConflictName: String;
CurRelation: TKeyCommandRelation;
CurName: String;
j: integer;
conflictType: TConflictType;
begin
Result:=mrOK;
// search for conflict
CurRelation:=KeyCommandRelationList.Relations[RelationIndex];
if Key.Key1=VK_UNKNOWN then
exit;
//Try to find an IDE command that conflicts
for j:=0 to KeyCommandRelationList.RelationCount-1 do begin
conflictType:=ctNone;
ConflictRelation:=KeyCommandRelationList.Relations[j];
with ConflictRelation do
begin
if (j=RelationIndex) then continue;
if not Category.ScopeIntersects(Scope) then continue;
if ((Key.Key1=ShortcutA.Key1) and (Key.Shift1=ShortcutA.Shift1))
and (((Key.Key2=ShortcutA.Key2) and (Key.Shift2=ShortcutA.Shift2))
or (Key.Key2=VK_UNKNOWN) or (ShortcutA.Key2=VK_UNKNOWN))
then begin
conflictType:=ctConflictKeyA; // ShortcutA bites
end
else if ((Key.Key1=ShortcutB.Key1) and (Key.Shift1=ShortcutB.Shift1))
and (((Key.Key2=ShortcutB.Key2) and (Key.Shift2=ShortcutB.Shift2))
or (Key.Key2=VK_UNKNOWN) or (ShortcutB.Key2=VK_UNKNOWN))
then begin
conflictType:=ctConflictKeyB; // ShortcutB bites
end;
end;
if (conflictType<>ctNone) then begin
CurName:=CurRelation.GetCategoryAndName;
ConflictName:=ConflictRelation.GetCategoryAndName;
if conflictType=ctConflictKeyA then
ConflictName:=ConflictName
+' ('+KeyAndShiftStateToEditorKeyString(ConflictRelation.ShortcutA)+')'
else
ConflictName:=ConflictName
+' ('+KeyAndShiftStateToEditorKeyString(ConflictRelation.ShortcutB)+')';
case IDEMessageDialog(lisPEConflictFound,
Format(lisTheKeyIsAlreadyAssignedToRemoveTheOldAssignmentAnd,
[KeyAndShiftStateToEditorKeyString(Key),
LineEnding, ConflictName, LineEnding, LineEnding, CurName]),
mtConfirmation, [mbYes, mbNo, mbCancel])
of
mrYes: Result:=mrOK;
mrCancel: Result:=mrCancel;
else Result:=mrRetry;
end;
if Result=mrOK then begin
if (conflictType=ctConflictKeyA) then
ConflictRelation.ShortcutA:=ConflictRelation.ShortcutB;
ConflictRelation.ClearShortcutB;
end
else
break;
end;
end;
end;
procedure TShortCutDialog.UpdateCaptions;
begin
if ShowSequence then begin
PrimaryGroupBox.Caption:=lisKeyOr2KeySequence;
SecondaryGroupBox.Caption:=lisAlternativeKeyOr2KeySequence;
end else begin
PrimaryGroupBox.Caption:=lisEdtExtToolKey;
SecondaryGroupBox.Caption:=lisAlternativeKey;
end;
end;
procedure TShortCutDialog.ClearKeys;
begin
PrimaryShortCut:=CleanIDEShortCut;
SecondaryShortCut:=CleanIDEShortCut;
end;
procedure TShortCutDialog.SetRelation(
AKeyCommandRelationList: TKeyCommandRelationList; Index: integer);
var
CurRelation: TKeyCommandRelation;
begin
KeyCommandRelationList:=AKeyCommandRelationList;
RelationIndex:=Index;
CurRelation:=AKeyCommandRelationList.Relations[RelationIndex];
PrimaryShortCut:=CurRelation.ShortcutA;
SecondaryShortCut:=CurRelation.ShortcutB;
Caption:=srkmCommand+' "'+CurRelation.LocalizedName+'"';
end;
end.
|