diff options
author | Graeme Geldenhuys <graeme@mastermaths.co.za> | 2011-08-24 12:06:37 +0200 |
---|---|---|
committer | Graeme Geldenhuys <graeme@mastermaths.co.za> | 2011-08-24 12:06:37 +0200 |
commit | 4d6aa18425807fc9a14455874fdc6800c563f76a (patch) | |
tree | 92704244ec15c1578f588dbc9ca60ee7b3a4cae5 | |
parent | bc0424f5251d927c9ca96d0ecb77bfd9ac5de341 (diff) | |
download | fpGUI-4d6aa18425807fc9a14455874fdc6800c563f76a.tar.xz |
menu: correctly handle keyboard shortcut events now
Before the menu only used to process the Alt+<key> events for the
mainmenu bar (top level menus). Now we recursively run through all menu
items looking for a hotkey match.
-rw-r--r-- | src/gui/fpg_menu.pas | 60 |
1 files changed, 55 insertions, 5 deletions
diff --git a/src/gui/fpg_menu.pas b/src/gui/fpg_menu.pas index 545d7581..a3fa0d2d 100644 --- a/src/gui/fpg_menu.pas +++ b/src/gui/fpg_menu.pas @@ -127,6 +127,7 @@ type procedure DrawRow(line: integer; const AItemFocused: boolean); virtual; function ItemHeight(mi: TfpgMenuItem): integer; virtual; procedure PrepareToShow; + procedure DoKeyShortcut(const AOrigin: TfpgWidget; const keycode: word; const shiftstate: TShiftState; var consumed: boolean; const IsChildOfOrigin: boolean = False); override; public OpenerPopup: TfpgPopupMenu; OpenerMenuBar: TfpgMenuBar; @@ -504,11 +505,11 @@ procedure TfpgMenuBar.HandleKeyPress(var keycode: word; var s: string; i: integer; + mi: TfpgMenuItem; begin -// writeln(Classname, '.Keypress'); s := KeycodeToText(keycode, shiftstate); -// writeln('s: ', s); - // handle MenuBar (Alt+?) shortcuts only - for now! + + // handle MenuBar (Alt+?) shortcuts only if (length(s) = 5) and (copy(s, 1, 4) = 'Alt+') then begin s := KeycodeToText(keycode, []); @@ -516,12 +517,28 @@ begin if i <> -1 then begin consumed := True; -// writeln('Selected ', VisibleItem(i).Text); FFocusItem := i; DoSelect; end; end; - inherited HandleKeyPress(keycode, shiftstate, consumed); + + { now process sub-menus off the MenuBar } + for i := 0 to ComponentCount-1 do + begin + if Components[i] is TfpgMenuItem then { these are main menu items } + begin + mi := TfpgMenuItem(Components[i]); + if mi.Visible and mi.Enabled then + begin + { process the sub menus } + if mi.SubMenu <> nil then + mi.SubMenu.DoKeyShortcut(nil, keycode, shiftstate, consumed); + if consumed then + Exit; + end; + end; + end; + end; procedure TfpgMenuBar.HandlePaint; @@ -1262,6 +1279,39 @@ begin uFocusedPopupMenu := self; end; +procedure TfpgPopupMenu.DoKeyShortcut(const AOrigin: TfpgWidget; + const keycode: word; const shiftstate: TShiftState; var consumed: boolean; const IsChildOfOrigin: boolean = False); +var + s: TfpgString; + i: integer; + mi: TfpgMenuItem; +begin + s := KeycodeToText(keycode, shiftstate); + for i := 0 to ComponentCount-1 do + begin + if Components[i] is TfpgMenuItem then + begin + mi := Components[i] as TfpgMenuItem; + if mi.Separator then + Continue; + if mi.Visible and mi.Enabled then + begin + { process the sub menus } + if mi.SubMenu <> nil then + mi.SubMenu.DoKeyShortcut(nil, keycode, shiftstate, consumed) + else if mi.HotKeyDef = s then + begin + consumed := True; + mi.Click; + end; + if consumed then + Exit; + end; + end; + end; + +end; + function TfpgPopupMenu.CalcMouseRow(y: integer): integer; var h: integer; |