| 1 |
{%MainUnit castlewindow.pas} |
| 2 |
{ |
| 3 |
Copyright 2022-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Implementation of TCastleWindow.MainMenu for Cocoa. |
| 18 |
|
| 19 |
Lots of inspiration from LCL Cocoa widgetset code. |
| 20 |
} |
| 21 |
|
| 22 |
type |
| 23 |
TCocoaMenuItem = objcclass(NSMenuItem) |
| 24 |
ParentWindow: TCastleWindow; |
| 25 |
MenuItem: TMenuItem; |
| 26 |
procedure castleClick(sender: id); message 'castleClick:'; |
| 27 |
end; |
| 28 |
|
| 29 |
procedure TCocoaMenuItem.castleClick(sender: id); |
| 30 |
begin |
| 31 |
//WritelnLog('DoMenuClick ', MenuItem.Caption); |
| 32 |
ParentWindow.DoMenuClick(MenuItem); |
| 33 |
end; |
| 34 |
|
| 35 |
const |
| 36 |
CocoaMenuState: array [Boolean] of NSInteger = (NSOffState, NSOnState); |
| 37 |
|
| 38 |
procedure TCastleWindow.BackendMenuInitialize; |
| 39 |
|
| 40 |
{ Cocoa menu item keyEquivalent which represents menu item key shortcut. } |
| 41 |
procedure CocoaMenuKey(const KeyString: String; const Key: TKey; const Modifiers: TModifierKeys; |
| 42 |
out CocoaKeyEquivalent: String; |
| 43 |
out CocoaKeyModifier: NSUInteger); |
| 44 |
var |
| 45 |
// KeyString expressed as a single 8-bit character, or 0 if not possible |
| 46 |
KeyString1: AnsiChar; |
| 47 |
begin |
| 48 |
if Length(KeyString) = 1 then |
| 49 |
KeyString1 := KeyString[1] |
| 50 |
else |
| 51 |
KeyString1 := #0; |
| 52 |
|
| 53 |
case KeyString1 of |
| 54 |
CtrlA .. CtrlZ: |
| 55 |
begin |
| 56 |
CocoaKeyEquivalent := Chr(Ord(KeyString1) - Ord(CtrlA) + Ord('a')); |
| 57 |
CocoaKeyModifier := NSCommandKeyMask {NSControlKeyMask}; |
| 58 |
end; |
| 59 |
else |
| 60 |
begin |
| 61 |
CocoaKeyEquivalent := KeyString; |
| 62 |
CocoaKeyModifier := 0; |
| 63 |
end; |
| 64 |
end; |
| 65 |
|
| 66 |
if mkShift in Modifiers then |
| 67 |
CocoaKeyModifier := CocoaKeyModifier or NSShiftKeyMask; |
| 68 |
if mkCtrl in Modifiers then |
| 69 |
CocoaKeyModifier := CocoaKeyModifier or NSCommandKeyMask {NSControlKeyMask}; |
| 70 |
if mkAlt in Modifiers then |
| 71 |
CocoaKeyModifier := CocoaKeyModifier or NSAlternateKeyMask; |
| 72 |
|
| 73 |
// TODO: handle Key |
| 74 |
end; |
| 75 |
|
| 76 |
procedure AddMenuItem(const ParentMenu: NSMenu; const Entry: TMenuEntry); |
| 77 |
var |
| 78 |
I: Integer; |
| 79 |
EntryItem: TMenuItem; |
| 80 |
EntryMenu: TMenu; |
| 81 |
CocoaSubMenu: NSMenu; |
| 82 |
CocoaMenuItem: NSMenuItem; // sometimes, but not always, TCocoaMenuItem |
| 83 |
CocoaKeyEquivalent: String; |
| 84 |
CocoaKeyModifier: NSUInteger; |
| 85 |
begin |
| 86 |
if Entry is TMenuItem then |
| 87 |
begin |
| 88 |
EntryItem := TMenuItem(Entry); |
| 89 |
CocoaMenuKey(EntryItem.KeyString, EntryItem.Key, EntryItem.Modifiers, |
| 90 |
CocoaKeyEquivalent, CocoaKeyModifier); |
| 91 |
CocoaMenuItem := TCocoaMenuItem.alloc.initWithTitle_action_keyEquivalent( |
| 92 |
NSSTR(SRemoveMnemonics(EntryItem.Caption)), |
| 93 |
ObjCSelector('castleClick:'), |
| 94 |
NSSTR(CocoaKeyEquivalent) |
| 95 |
); |
| 96 |
TCocoaMenuItem(CocoaMenuItem).MenuItem := EntryItem; |
| 97 |
TCocoaMenuItem(CocoaMenuItem).ParentWindow := Self; |
| 98 |
CocoaMenuItem.SetEnabled(EntryItem.Enabled and (not MenuForcefullyDisabled)); |
| 99 |
CocoaMenuItem.SetKeyEquivalentModifierMask(CocoaKeyModifier); |
| 100 |
CocoaMenuItem.setTarget(CocoaMenuItem); // determines where do we look for castleClick: selector |
| 101 |
if EntryItem is TMenuItemChecked then |
| 102 |
CocoaMenuItem.setState(CocoaMenuState[TMenuItemChecked(EntryItem).Checked]); |
| 103 |
end else |
| 104 |
if Entry is TMenuSeparator then |
| 105 |
begin |
| 106 |
CocoaMenuItem := NSMenuItem.SeparatorItem; |
| 107 |
end else |
| 108 |
if Entry is TMenu then |
| 109 |
begin |
| 110 |
EntryMenu := TMenu(Entry); |
| 111 |
CocoaSubMenu := NSMenu.alloc.initWithTitle( |
| 112 |
NSSTR(SRemoveMnemonics(EntryMenu.Caption))); |
| 113 |
CocoaSubMenu.setAutoenablesItems(false); |
| 114 |
for I := 0 to EntryMenu.Count - 1 do |
| 115 |
AddMenuItem(CocoaSubMenu, EntryMenu.Entries[I]); |
| 116 |
CocoaMenuItem := NSMenuItem.alloc.initWithTitle_action_keyEquivalent( |
| 117 |
NSSTR(SRemoveMnemonics(EntryMenu.Caption)), nil, |
| 118 |
NSSTR('')).autorelease; |
| 119 |
CocoaMenuItem.SetEnabled(EntryMenu.Enabled and (not MenuForcefullyDisabled)); |
| 120 |
CocoaMenuItem.setSubmenu(CocoaSubMenu); |
| 121 |
end else |
| 122 |
raise EInternalError.Create('Not implemented TMenuEntry subclass'); |
| 123 |
|
| 124 |
ParentMenu.AddItem(CocoaMenuItem); |
| 125 |
Entry.Handle := CocoaMenuItem; |
| 126 |
end; |
| 127 |
|
| 128 |
var |
| 129 |
I: Integer; |
| 130 |
begin |
| 131 |
NSApp.MainMenu.setAutoenablesItems(false); |
| 132 |
|
| 133 |
for I := 0 to MainMenu.Count - 1 do |
| 134 |
AddMenuItem(NSApp.MainMenu, MainMenu.Entries[I]); |
| 135 |
end; |
| 136 |
|
| 137 |
procedure TCastleWindow.BackendMenuFinalize; |
| 138 |
var |
| 139 |
I: Integer; |
| 140 |
begin |
| 141 |
for I := 0 to MainMenu.Count - 1 do |
| 142 |
NSApp.mainMenu.RemoveItem(NSMenuItem(MainMenu.Entries[I].Handle)); |
| 143 |
end; |
| 144 |
|
| 145 |
procedure TCastleWindow.MenuUpdateCaption(Entry: TMenuEntryWithCaption); |
| 146 |
var |
| 147 |
CocoaMenuItem: NSMenuItem; |
| 148 |
begin |
| 149 |
if Entry.Handle <> nil then |
| 150 |
begin |
| 151 |
CocoaMenuItem := NSMenuItem(Entry.Handle); |
| 152 |
CocoaMenuItem.SetTitle(NSSTR(Entry.Caption)); |
| 153 |
end; |
| 154 |
end; |
| 155 |
|
| 156 |
procedure TCastleWindow.MenuUpdateEnabled(Entry: TMenuEntryWithCaption); |
| 157 |
var |
| 158 |
CocoaMenuItem: NSMenuItem; |
| 159 |
begin |
| 160 |
if Entry.Handle <> nil then |
| 161 |
begin |
| 162 |
CocoaMenuItem := NSMenuItem(Entry.Handle); |
| 163 |
CocoaMenuItem.SetEnabled(Entry.Enabled and (not MenuForcefullyDisabled)); |
| 164 |
end; |
| 165 |
end; |
| 166 |
|
| 167 |
procedure TCastleWindow.MenuUpdateChecked(Entry: TMenuItemChecked); |
| 168 |
var |
| 169 |
CocoaMenuItem: NSMenuItem; |
| 170 |
begin |
| 171 |
if Entry.Handle <> nil then |
| 172 |
begin |
| 173 |
CocoaMenuItem := NSMenuItem(Entry.Handle); |
| 174 |
CocoaMenuItem.setState(CocoaMenuState[Entry.Checked]); |
| 175 |
end; |
| 176 |
end; |
| 177 |
|
| 178 |
function TCastleWindow.MenuUpdateCheckedFast: boolean; |
| 179 |
begin |
| 180 |
Result := true; |
| 181 |
end; |
| 182 |
|
| 183 |
procedure TCastleWindow.MenuInsert(const Parent: TMenu; |
| 184 |
const ParentPosition: Integer; const Entry: TMenuEntry); |
| 185 |
begin |
| 186 |
{ TODO: implement menu on Cocoa faster, without reinitialize on update } |
| 187 |
MenuFinalize; |
| 188 |
MenuInitialize; |
| 189 |
end; |
| 190 |
|
| 191 |
procedure TCastleWindow.MenuDelete(const Parent: TMenu; |
| 192 |
const ParentPosition: Integer; const Entry: TMenuEntry); |
| 193 |
begin |
| 194 |
{ TODO: implement menu on Cocoa faster, without reinitialize on update } |
| 195 |
MenuFinalize; |
| 196 |
MenuInitialize; |
| 197 |
end; |
| 198 |
|
| 199 |
procedure TCastleWindow.SetMenuForcefullyDisabled(const Value: Boolean); |
| 200 |
|
| 201 |
procedure UpdateForcefullyDisabled(const Menu: TMenu); |
| 202 |
var |
| 203 |
I: Integer; |
| 204 |
begin |
| 205 |
MenuUpdateEnabled(Menu); |
| 206 |
for I := 0 to Menu.Count - 1 do |
| 207 |
if Menu[I] is TMenu then |
| 208 |
UpdateForcefullyDisabled(TMenu(Menu[I])) |
| 209 |
else |
| 210 |
if Menu[I] is TMenuEntryWithCaption then |
| 211 |
MenuUpdateEnabled(TMenuEntryWithCaption(Menu[I])); |
| 212 |
end; |
| 213 |
|
| 214 |
begin |
| 215 |
if FMenuForcefullyDisabled <> Value then |
| 216 |
begin |
| 217 |
FMenuForcefullyDisabled := Value; |
| 218 |
if MainMenu <> nil then |
| 219 |
UpdateForcefullyDisabled(MainMenu); |
| 220 |
end; |
| 221 |
end; |