| 1 |
{ |
| 2 |
Copyright 2022-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ This is a hack used in TProjectForm.FormKeyDown to |
| 17 |
- Have some menu items with simple shortcuts, like Home, F, Ctrl+Z |
| 18 |
(and *visible* in menu with these shortcuts, to be discoverable) |
| 19 |
- but still allow user to interact with text input (like TEdit) |
| 20 |
using Home, F, Ctrl+Z etc. |
| 21 |
|
| 22 |
The intuitive result should be: |
| 23 |
- using Home, F, Ctrl+Z etc. when TEdit is focused, makes normal text-editing |
| 24 |
operation |
| 25 |
- only if no text-editing is focused, |
| 26 |
it goes to the specific custom action we wanted to assign to it. |
| 27 |
|
| 28 |
Rejected workarounds: |
| 29 |
- use only shortcuts that avoid common text-editing. |
| 30 |
Like Ctrl+Home, Ctrl+F, Ctrl+Alt+Z. |
| 31 |
Rejected because these are important shortcuts. |
| 32 |
For ease of use, for consistency with other software -- |
| 33 |
we want to keep them easy. |
| 34 |
- use simple shortcuts, but not set them as menu item shortcuts. |
| 35 |
Just dispatch them yourself if e.g. TCastleControl is focused. |
| 36 |
Rejected: then UI sucks, we cannot show shortcuts in menu in standard way. |
| 37 |
|
| 38 |
This workaround depends on Lazarus behaviors (tested on WinAPI, GTK, Cocoa |
| 39 |
backends): |
| 40 |
- Key being used to activate menu item is still send to TForm1.FormKeyDown |
| 41 |
when form has KeyPreview |
| 42 |
- We can resign from "normal" key handling (which means running menu item |
| 43 |
for these special keys) by assigning Key := 0 |
| 44 |
- Our 2 important text-editing states are TEdit and editing in TTreeView, |
| 45 |
the latter is luckily just an internal TEdit too. |
| 46 |
- So we just interpret and implement TEdit operations manually. |
| 47 |
|
| 48 |
Note: This is still ugly and inherently unreliable workaround, because we |
| 49 |
interpret what some special built-in keys should do. So we need to |
| 50 |
capture all such keys, and implement same logic... |
| 51 |
E.g. pressing "f" key, with and without Shift, with and without CapsLock, |
| 52 |
should be consistent with system-handled "g" key. |
| 53 |
- What happens if we don't include special handling for some key, |
| 54 |
but we should? In the worst case it will not work with edit boxes, |
| 55 |
and always activate menu command. |
| 56 |
- What happens if we included unnecessarily some key in this treatment? |
| 57 |
Everything should be OK, if only we simulate default behavior OK. |
| 58 |
|
| 59 |
Tested: It is necessary, and good workaround, |
| 60 |
- (FPC 3.2.2, LCL 2.2) on Windows with WinAPI widgetset. |
| 61 |
- (FPC 3.2.2, LCL 2.2) on Linux with GTK widgetset. |
| 62 |
- (FPC 3.2.2, LCL 2.2) on macOS with Cocoa widgetset. |
| 63 |
|
| 64 |
See https://github.com/michaliskambi/lcl-test-menu-special-shortcuts |
| 65 |
for separated LCL application testing this. |
| 66 |
|
| 67 |
Note: TTreeView also handles Home / End. |
| 68 |
Strangely, it behaves like we want out-of-the-box: |
| 69 |
it intercepts Home / End when focused, regardless of if some menu item |
| 70 |
has such shortcut. |
| 71 |
Tested this is true on: |
| 72 |
- (FPC 3.2.2, LCL 2.2) on Windows with WinAPI widgetset. |
| 73 |
- (FPC 3.2.2, LCL 2.2) on Linux with GTK widgetset. |
| 74 |
- (FPC 3.2.2, LCL 2.2) on macOS with Cocoa widgetset. |
| 75 |
} |
| 76 |
unit CastleLclEditHack; |
| 77 |
|
| 78 |
interface |
| 79 |
|
| 80 |
uses {$ifdef MSWINDOWS} { for GetKeyState } Windows, {$endif} |
| 81 |
SysUtils, Classes, StdCtrls; |
| 82 |
|
| 83 |
type |
| 84 |
{ Abstact class to wrap capabilities of TEdit and (editable) TComboBox. } |
| 85 |
TEditBox = class |
| 86 |
protected |
| 87 |
function GetSelLength: Integer; virtual; abstract; |
| 88 |
function GetSelStart: Integer; virtual; abstract; |
| 89 |
function GetSelText: String; virtual; abstract; |
| 90 |
function GetText: String; virtual; abstract; |
| 91 |
procedure SetSelLength(const AValue: Integer); virtual; abstract; |
| 92 |
procedure SetSelStart(const AValue: Integer); virtual; abstract; |
| 93 |
procedure SetSelText(const AValue: String); virtual; abstract; |
| 94 |
procedure SetText(const AValue: String); virtual; abstract; |
| 95 |
|
| 96 |
property Text: String read GetText write SetText; |
| 97 |
property SelText: String read GetSelText write SetSelText; |
| 98 |
property SelStart: Integer read GetSelStart write SetSelStart; |
| 99 |
property SelLength: Integer read GetSelLength write SetSelLength; |
| 100 |
|
| 101 |
procedure CopyToClipboard; virtual; |
| 102 |
procedure CutToClipboard; virtual; |
| 103 |
procedure PasteFromClipboard; virtual; |
| 104 |
procedure Undo; virtual; |
| 105 |
function ReadOnly: Boolean; virtual; abstract; |
| 106 |
public |
| 107 |
{ Process the given key to realize default editing action on it. |
| 108 |
Processes only a subset of keys for which FormProject has possibly a menu shortcut. } |
| 109 |
procedure ProcessKey(var Key: Word; const Shift: TShiftState); |
| 110 |
end; |
| 111 |
|
| 112 |
{ Wrapper around TEdit. } |
| 113 |
TEditBoxForEdit = class(TEditBox) |
| 114 |
private |
| 115 |
E: TEdit; |
| 116 |
protected |
| 117 |
function GetSelLength: Integer; override; |
| 118 |
function GetSelStart: Integer; override; |
| 119 |
function GetSelText: String; override; |
| 120 |
function GetText: String; override; |
| 121 |
procedure SetSelLength(const AValue: Integer); override; |
| 122 |
procedure SetSelStart(const AValue: Integer); override; |
| 123 |
procedure SetSelText(const AValue: String); override; |
| 124 |
procedure SetText(const AValue: String); override; |
| 125 |
|
| 126 |
procedure CopyToClipboard; override; |
| 127 |
procedure CutToClipboard; override; |
| 128 |
procedure PasteFromClipboard; override; |
| 129 |
procedure Undo; override; |
| 130 |
function ReadOnly: Boolean; override; |
| 131 |
public |
| 132 |
constructor Create(const AEdit: TEdit); |
| 133 |
end; |
| 134 |
|
| 135 |
{ Wrapper around TComboBox. } |
| 136 |
TEditBoxForComboBox = class(TEditBox) |
| 137 |
private |
| 138 |
E: TComboBox; |
| 139 |
protected |
| 140 |
function GetSelLength: Integer; override; |
| 141 |
function GetSelStart: Integer; override; |
| 142 |
function GetSelText: String; override; |
| 143 |
function GetText: String; override; |
| 144 |
procedure SetSelLength(const AValue: Integer); override; |
| 145 |
procedure SetSelStart(const AValue: Integer); override; |
| 146 |
procedure SetSelText(const AValue: String); override; |
| 147 |
procedure SetText(const AValue: String); override; |
| 148 |
|
| 149 |
function ReadOnly: Boolean; override; |
| 150 |
public |
| 151 |
constructor Create(const AEdit: TComboBox); |
| 152 |
end; |
| 153 |
|
| 154 |
{ Process a key pressed with given ActiveControl being focused. |
| 155 |
|
| 156 |
If this was a key that typically interacts with TEdit controls, |
| 157 |
then perform the typical TEdit action directly |
| 158 |
(do not allow menu item to catch it) and set Key to 0. |
| 159 |
|
| 160 |
Does something only for keys that are used as some menu shortcut |
| 161 |
in CGE editor, and are also used to edit text. Like Delete or Ctrl+Z |
| 162 |
or 1. } |
| 163 |
procedure ProcessKeyToPerformEdit(const ActiveControl: TComponent; |
| 164 |
var Key: Word; const Shift: TShiftState); |
| 165 |
|
| 166 |
implementation |
| 167 |
|
| 168 |
uses LCLType, LCLVersion, StrUtils, Clipbrd, |
| 169 |
CastleStringUtils, CastleUnicode; |
| 170 |
|
| 171 |
{ TEditBox ------------------------------------------------------------------- } |
| 172 |
|
| 173 |
procedure TEditBox.ProcessKey(var Key: Word; const Shift: TShiftState); |
| 174 |
|
| 175 |
{ When pressing a letter, should we make it upper? |
| 176 |
It sucks, but in this workaround we cannot depend on OnKeyPress reaching us |
| 177 |
(it doesn't reach us when menu item intercepts the key) |
| 178 |
so we need to calculate key->char here. } |
| 179 |
function LettersUpCase: Boolean; |
| 180 |
begin |
| 181 |
Result := false; |
| 182 |
// TODO: This is Windows-only. |
| 183 |
{$ifdef MSWINDOWS} |
| 184 |
{ See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeystate . |
| 185 |
Read low-order bit of VK_CAPITAL (caps lock) state. } |
| 186 |
if (GetKeyState(VK_CAPITAL) and 1) <> 0 then |
| 187 |
Result := true; |
| 188 |
{$endif} |
| 189 |
if ssShift in Shift then |
| 190 |
Result := not Result; |
| 191 |
end; |
| 192 |
|
| 193 |
procedure InsertChar(const C: String); |
| 194 |
begin |
| 195 |
if ReadOnly then |
| 196 |
Beep |
| 197 |
else |
| 198 |
// TODO: in Overwrite mode, with SelText = '', delete next char first |
| 199 |
SelText := C; |
| 200 |
end; |
| 201 |
|
| 202 |
var |
| 203 |
SavedSelStart: Integer; |
| 204 |
begin |
| 205 |
case Key of |
| 206 |
VK_HOME: |
| 207 |
begin |
| 208 |
if Shift = [ssShift] then |
| 209 |
begin |
| 210 |
SavedSelStart := SelStart; |
| 211 |
SelStart := 0; |
| 212 |
SelLength := SavedSelStart; |
| 213 |
end else |
| 214 |
SelStart := 0 |
| 215 |
end; |
| 216 |
{.$ifdef LCLCocoa} |
| 217 |
{ If user didn't adjust Home/End system-wide, then actually |
| 218 |
by default Home/End do nothing in TEdit. |
| 219 |
So we could disable Home... |
| 220 |
but it seems more useful to make Home/End just work in CGE TEdit. |
| 221 |
|
| 222 |
For easier testing, we enable our End handling on all platforms. } |
| 223 |
VK_END: |
| 224 |
begin |
| 225 |
if Shift = [ssShift] then |
| 226 |
SelLength := StringLength(Text) - SelStart |
| 227 |
else |
| 228 |
SelStart := StringLength(Text); |
| 229 |
end; |
| 230 |
{.$endif} |
| 231 |
VK_0..VK_9: |
| 232 |
begin |
| 233 |
if ssShift in Shift then |
| 234 |
Exit; // resign from special handling, to allow to type !@#$... using alphanumeric keys |
| 235 |
InsertChar(Chr(Ord('0') + Key - VK_0)); |
| 236 |
end; |
| 237 |
VK_NUMPAD0..VK_NUMPAD9: |
| 238 |
begin |
| 239 |
if Shift <> [] then |
| 240 |
Exit; // resign from special handling |
| 241 |
InsertChar(Chr(Ord('0') + Key - VK_NUMPAD0)); |
| 242 |
end; |
| 243 |
VK_F: |
| 244 |
begin |
| 245 |
if (Shift = []) or (Shift = [ssShift]) then |
| 246 |
InsertChar(IfThen(LettersUpCase, 'F', 'f')) |
| 247 |
else |
| 248 |
Exit; // resign from special handling, e.g. let Ctrl+F always call menu item "Find Toggle" |
| 249 |
end; |
| 250 |
VK_Z: |
| 251 |
if Shift = [ssCtrl] then |
| 252 |
Undo |
| 253 |
else |
| 254 |
Exit; // resign from special handling |
| 255 |
//if Shift = [ssCtrl, ssShift] then |
| 256 |
// Redo |
| 257 |
//else |
| 258 |
VK_C: |
| 259 |
if Shift = [ssCtrl] then |
| 260 |
CopyToClipboard |
| 261 |
else |
| 262 |
Exit; // resign from special handling |
| 263 |
VK_V: |
| 264 |
if Shift = [ssCtrl] then |
| 265 |
begin |
| 266 |
if ReadOnly then |
| 267 |
Beep |
| 268 |
else |
| 269 |
PasteFromClipboard; |
| 270 |
end else |
| 271 |
Exit; // resign from special handling |
| 272 |
VK_X: |
| 273 |
if Shift = [ssCtrl] then |
| 274 |
begin |
| 275 |
if ReadOnly then |
| 276 |
Beep |
| 277 |
else |
| 278 |
CutToClipboard |
| 279 |
end else |
| 280 |
Exit; // resign from special handling |
| 281 |
VK_DELETE: |
| 282 |
if ReadOnly then |
| 283 |
Beep |
| 284 |
else |
| 285 |
begin |
| 286 |
if SelText <> '' then |
| 287 |
SelText := '' |
| 288 |
else |
| 289 |
begin |
| 290 |
SavedSelStart := SelStart; |
| 291 |
Text := |
| 292 |
StringCopy(Text, 1, SelStart) + |
| 293 |
StringEnding(Text, SelStart + 2); |
| 294 |
SelStart := SavedSelStart; |
| 295 |
end; |
| 296 |
end; |
| 297 |
else |
| 298 |
Exit; // resign from special handling |
| 299 |
end; |
| 300 |
|
| 301 |
Key := 0; // prevent key reaching normal menu/action event |
| 302 |
end; |
| 303 |
|
| 304 |
procedure TEditBox.CopyToClipboard; |
| 305 |
begin |
| 306 |
if SelText <> '' then |
| 307 |
Clipboard.AsText := SelText; |
| 308 |
end; |
| 309 |
|
| 310 |
procedure TEditBox.CutToClipboard; |
| 311 |
begin |
| 312 |
CopyToClipboard; |
| 313 |
SelText := ''; |
| 314 |
end; |
| 315 |
|
| 316 |
procedure TEditBox.PasteFromClipboard; |
| 317 |
begin |
| 318 |
if Clipboard.HasFormat(CF_TEXT) then |
| 319 |
SelText := Clipboard.AsText; |
| 320 |
end; |
| 321 |
|
| 322 |
procedure TEditBox.Undo; |
| 323 |
begin |
| 324 |
// do nothing |
| 325 |
end; |
| 326 |
|
| 327 |
{ TEditBoxForEdit ------------------------------------------------------------ } |
| 328 |
|
| 329 |
function TEditBoxForEdit.GetSelLength: Integer; |
| 330 |
begin |
| 331 |
Result := E.SelLength; |
| 332 |
end; |
| 333 |
|
| 334 |
function TEditBoxForEdit.GetSelStart: Integer; |
| 335 |
begin |
| 336 |
Result := E.SelStart; |
| 337 |
end; |
| 338 |
|
| 339 |
function TEditBoxForEdit.GetSelText: String; |
| 340 |
begin |
| 341 |
Result := E.SelText; |
| 342 |
end; |
| 343 |
|
| 344 |
function TEditBoxForEdit.GetText: String; |
| 345 |
begin |
| 346 |
Result := E.Text; |
| 347 |
end; |
| 348 |
|
| 349 |
procedure TEditBoxForEdit.SetSelLength(const AValue: Integer); |
| 350 |
begin |
| 351 |
E.SelLength := AValue; |
| 352 |
end; |
| 353 |
|
| 354 |
procedure TEditBoxForEdit.SetSelStart(const AValue: Integer); |
| 355 |
begin |
| 356 |
E.SelStart := AValue; |
| 357 |
end; |
| 358 |
|
| 359 |
procedure TEditBoxForEdit.SetSelText(const AValue: String); |
| 360 |
begin |
| 361 |
E.SelText := AValue; |
| 362 |
end; |
| 363 |
|
| 364 |
procedure TEditBoxForEdit.SetText(const AValue: String); |
| 365 |
begin |
| 366 |
E.Text := AValue; |
| 367 |
end; |
| 368 |
|
| 369 |
constructor TEditBoxForEdit.Create(const AEdit: TEdit); |
| 370 |
begin |
| 371 |
inherited Create; |
| 372 |
E := AEdit; |
| 373 |
end; |
| 374 |
|
| 375 |
procedure TEditBoxForEdit.CopyToClipboard; |
| 376 |
begin |
| 377 |
E.CopyToClipboard; |
| 378 |
end; |
| 379 |
|
| 380 |
procedure TEditBoxForEdit.CutToClipboard; |
| 381 |
begin |
| 382 |
E.CutToClipboard; |
| 383 |
end; |
| 384 |
|
| 385 |
procedure TEditBoxForEdit.PasteFromClipboard; |
| 386 |
begin |
| 387 |
E.PasteFromClipboard; |
| 388 |
end; |
| 389 |
|
| 390 |
procedure TEditBoxForEdit.Undo; |
| 391 |
begin |
| 392 |
E.Undo; |
| 393 |
end; |
| 394 |
|
| 395 |
function TEditBoxForEdit.ReadOnly: Boolean; |
| 396 |
begin |
| 397 |
Result := E.ReadOnly; |
| 398 |
end; |
| 399 |
|
| 400 |
{ TEditBoxForComboBox ------------------------------------------------------------ } |
| 401 |
|
| 402 |
function TEditBoxForComboBox.GetSelLength: Integer; |
| 403 |
begin |
| 404 |
Result := E.SelLength; |
| 405 |
end; |
| 406 |
|
| 407 |
function TEditBoxForComboBox.GetSelStart: Integer; |
| 408 |
begin |
| 409 |
Result := E.SelStart; |
| 410 |
end; |
| 411 |
|
| 412 |
function TEditBoxForComboBox.GetSelText: String; |
| 413 |
begin |
| 414 |
Result := E.SelText; |
| 415 |
end; |
| 416 |
|
| 417 |
function TEditBoxForComboBox.GetText: String; |
| 418 |
begin |
| 419 |
Result := E.Text; |
| 420 |
end; |
| 421 |
|
| 422 |
procedure TEditBoxForComboBox.SetSelLength(const AValue: Integer); |
| 423 |
begin |
| 424 |
E.SelLength := AValue; |
| 425 |
end; |
| 426 |
|
| 427 |
procedure TEditBoxForComboBox.SetSelStart(const AValue: Integer); |
| 428 |
begin |
| 429 |
E.SelStart := AValue; |
| 430 |
end; |
| 431 |
|
| 432 |
procedure TEditBoxForComboBox.SetSelText(const AValue: String); |
| 433 |
begin |
| 434 |
E.SelText := AValue; |
| 435 |
{ Without these lines, by default setting SelText keeps it selected, |
| 436 |
unlike on TEdit. } |
| 437 |
E.SelStart := E.SelStart + StringLength(AValue); |
| 438 |
E.SelLength := 0; |
| 439 |
end; |
| 440 |
|
| 441 |
procedure TEditBoxForComboBox.SetText(const AValue: String); |
| 442 |
begin |
| 443 |
E.Text := AValue; |
| 444 |
end; |
| 445 |
|
| 446 |
constructor TEditBoxForComboBox.Create(const AEdit: TComboBox); |
| 447 |
begin |
| 448 |
inherited Create; |
| 449 |
E := AEdit; |
| 450 |
end; |
| 451 |
|
| 452 |
//procedure TEditBoxForComboBox.CopyToClipboard; |
| 453 |
//begin |
| 454 |
// E.CopyToClipboard; |
| 455 |
//end; |
| 456 |
// |
| 457 |
//procedure TEditBoxForComboBox.CutToClipboard; |
| 458 |
//begin |
| 459 |
// E.CutToClipboard; |
| 460 |
//end; |
| 461 |
// |
| 462 |
//procedure TEditBoxForComboBox.PasteFromClipboard; |
| 463 |
//begin |
| 464 |
// E.PasteFromClipboard; |
| 465 |
//end; |
| 466 |
// |
| 467 |
//procedure TEditBoxForComboBox.Undo; |
| 468 |
//begin |
| 469 |
// E.Undo; |
| 470 |
//end; |
| 471 |
|
| 472 |
function TEditBoxForComboBox.ReadOnly: Boolean; |
| 473 |
begin |
| 474 |
Result := E.ReadOnly; |
| 475 |
end; |
| 476 |
|
| 477 |
{ global --------------------------------------------------------------------- } |
| 478 |
|
| 479 |
var |
| 480 |
CachedForComboBox: TEditBoxForComboBox; |
| 481 |
CachedForEdit: TEditBoxForEdit; |
| 482 |
|
| 483 |
procedure ProcessKeyToPerformEdit(const ActiveControl: TComponent; var Key: Word; const Shift: TShiftState); |
| 484 |
|
| 485 |
{$if LCL_FULLVERSION >= 2020000} |
| 486 |
{$define HAS_COMBO_EDIT_BOX} |
| 487 |
{$endif} |
| 488 |
|
| 489 |
{$ifndef HAS_COMBO_EDIT_BOX} |
| 490 |
// Adjusted from TComboBoxStyleHelper.HasEditBox in latest LCL. |
| 491 |
function ComboHasEditBox(const Style: TComboBoxStyle): Boolean; |
| 492 |
const |
| 493 |
ArrHasEditBox: array[TComboBoxStyle] of Boolean = ( |
| 494 |
True, // csDropDown |
| 495 |
True, // csSimple |
| 496 |
False, // csDropDownList |
| 497 |
False, // csOwnerDrawFixed |
| 498 |
False, // csOwnerDrawVariable |
| 499 |
True, // csOwnerDrawEditableFixed |
| 500 |
True // csOwnerDrawEditableVariable |
| 501 |
); |
| 502 |
begin |
| 503 |
Result := ArrHasEditBox[Style]; |
| 504 |
end; |
| 505 |
{$endif} |
| 506 |
|
| 507 |
var |
| 508 |
EditBox: TEditBox; |
| 509 |
begin |
| 510 |
if (ActiveControl is TComboBox) and |
| 511 |
{$ifdef HAS_COMBO_EDIT_BOX} |
| 512 |
(TComboBox(ActiveControl).Style.HasEditBox) |
| 513 |
{$else} |
| 514 |
ComboHasEditBox(TComboBox(ActiveControl).Style) |
| 515 |
{$endif} |
| 516 |
then |
| 517 |
begin |
| 518 |
{ Instead of creating new TEditBoxForComboBox instance at each ProcessKeyToPerformEdit |
| 519 |
call, we just reuse one cached instance. } |
| 520 |
if CachedForComboBox = nil then |
| 521 |
CachedForComboBox := TEditBoxForComboBox.Create(nil); |
| 522 |
CachedForComboBox.E := TComboBox(ActiveControl); |
| 523 |
EditBox := CachedForComboBox; |
| 524 |
end else |
| 525 |
|
| 526 |
if ActiveControl is TEdit then |
| 527 |
begin |
| 528 |
if CachedForEdit = nil then |
| 529 |
CachedForEdit := TEditBoxForEdit.Create(nil); |
| 530 |
CachedForEdit.E := TEdit(ActiveControl); |
| 531 |
EditBox := CachedForEdit; |
| 532 |
end else |
| 533 |
EditBox := nil; |
| 534 |
|
| 535 |
if EditBox <> nil then |
| 536 |
EditBox.ProcessKey(Key, Shift); |
| 537 |
end; |
| 538 |
|
| 539 |
finalization |
| 540 |
FreeAndNil(CachedForComboBox); |
| 541 |
FreeAndNil(CachedForEdit); |
| 542 |
end. |