Debian Codegraph @

src/window/windows/castlewindow_winapi_menu.inc

raw
1 {%MainUnit ../castlewindow.pas}
2 {
3 Copyright 2004-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 { @abstract(Helper for CastleWindow implementation of menu-related things
18 under WinAPI.)
19
20 It converts menu structure from classes in castlewindowmenu.inc to WinAPI menu
21 (HMenu handle).
22
23 Let me elaborate here a little about how menu shortcuts are handled
24 when CastleWindow is implemented on top of WinAPI:
25
26 @orderedList(
27 @item(
28 1st thing to realize is that WinAPI is stupid and AFAIK you simply
29 can't explicitly say to WinAPI : this menu item's key shortcut
30 is xxx.
31
32 Instead you have to do two things:
33
34 @orderedList(
35 @item(
36 When creating menu item, specify it's caption as
37 "Real caption" + CharTab + "textual description of menu's key shortcut")
38
39 @item(
40 Create accelerator table (e.g. with CreateAcceleratorTable)
41 that specifies associations "key shortcuts" -> "command numbers to
42 be returned by WM_COMMAND".
43
44 Then you have to use that accelerator table in your message loop
45 using TranslateAccelerators. This will actually translate WM_KEYDOWN
46 messages to WM_COMMAND.)
47 )
48
49 What's stupid about this?
50 It's stupid because there actually need not be any connection
51 between what "textual description of menu's key shortcut"
52 you gave in 1 and what actual key shortcuts you associate
53 with which commands in 2.
54 You can easily specify none or inccorrect textual key descriptions for
55 some menu items. Moreover you can create "textual descriptions"
56 in non-standard way, e.g. some programs name Ctrl + O shortcut
57 as 'Ctrl+o' and some as 'Ctrl+O'.)
58
59 @item(
60 OK, so what I'm doing with it?
61
62 Well, in this particular case stupidity of WinAPI means that I have
63 less work. That's because I already implemented mechanism to
64 create textual key descriptions in TMenuItem.KeyString
65 and I already created mechanism to translate key downs to
66 appropriately translate key presses to menu items.
67 I had to do this because my CastleWindow unit must
68 work when implemented on top of Xlib, where I don't have
69 a concept of a "menu item with key shortcut" available.
70
71 So what I'm doing?
72 I'm doing 1.1 in this unit using my TMenuItem.KeyString, and
73 I'm ignoring 1.2 (i.e. I'm doing equivalent things myself in CastleWindow unit,
74 not using any WinAPI accelerator tables).)
75 )
76 }
77
78 { We use ParentAllowsEnabled trick to take into account main Menu.Enabled
79 state. WinAPI doesn't allow the menu bar or popup to be just disabled,
80 so instead we disable all direct children of main Manu
81 if Menu.Enabled = false.
82
83 In other words, when main menu creates it's children, it passes
84 ParentAllowsEnabled = main Menu.Enabled.
85 When other menu creates it's children, it passes
86 ParentAllowsEnabled = @true. }
87 function MakeWinapiMenuCore(Menu: TMenu;
88 MenuBar: boolean; ParentAllowsEnabled: boolean): HMenu;
89
90 function SMnemonicsToWin(const S: string): UnicodeString;
91 var
92 SPos, ResultPos: Integer;
93 begin
94 { I'm utlizing here the fact that Result for sure will be
95 shorter or equal to S }
96 SetLength(Result, Length(S));
97
98 ResultPos := 1;
99 SPos := 1;
100 while SPos <= Length(S) do
101 begin
102 if S[SPos] = '_' then
103 begin
104 if (SPos < Length(S)) and (S[SPos + 1] = '_') then
105 begin
106 { '__' replace with '_' }
107 Result[ResultPos] := '_';
108 Inc(ResultPos);
109 Inc(SPos, 2);
110 end else
111 begin
112 { '_' (not starting '__') replace with '&' }
113 Result[ResultPos] := '&';
114 Inc(ResultPos);
115 Inc(SPos);
116 end;
117 end else
118 begin
119 Result[ResultPos] := S[SPos];
120 Inc(ResultPos);
121 Inc(SPos);
122 end;
123 end;
124
125 SetLength(Result, ResultPos - 1);
126 end;
127
128 function EnabledFlag(Enabled: boolean): UInt;
129 begin
130 if Enabled then
131 Result := MF_ENABLED else
132 Result := MF_GRAYED;
133 end;
134
135 procedure AppendGLMenu(Menu: TMenu; ParentAllowsEnabled: boolean);
136 begin
137 { I'm casting MakeWinapiMenu result (:HMenu)
138 to UINT to avoid range check errors }
139 OSCheck( AppendMenuW(Result,
140 MF_STRING or MF_POPUP or EnabledFlag(Menu.Enabled and ParentAllowsEnabled),
141 UINT(MakeWinapiMenuCore(Menu, false, true)),
142 PWideChar(SMnemonicsToWin(Menu.Caption))) );
143 end;
144
145 procedure AppendGLMenuItem(MenuItem: TMenuItem; ParentAllowsEnabled: boolean);
146 var
147 Flags: UInt;
148 KeyStr: String;
149 WinMenuCaption: UnicodeString;
150 begin
151 Flags := MF_STRING or EnabledFlag(MenuItem.Enabled and ParentAllowsEnabled);
152
153 { If I understand docs properly, MF_UNCHECKED is actually meaningless
154 here as I don't use any customized bitmaps for menu check marks.
155 But I wrote it for consistency. }
156 if MenuItem is TMenuItemChecked then
157 begin
158 if TMenuItemChecked(MenuItem).Checked then
159 Flags := Flags or MF_CHECKED else
160 Flags := Flags or MF_UNCHECKED;
161 end;
162
163 { Don't use here MenuItem.CaptionWithKey.
164 Instead use #9 to delimit key names.
165 This way our key shortcuts will be drawn nicely. }
166 WinMenuCaption := SMnemonicsToWin(MenuItem.Caption);
167 if MenuItem.KeyToString(KeyStr) then
168 WinMenuCaption := WinMenuCaption + #9 + UnicodeString(KeyStr);
169
170 OSCheck( AppendMenuW(Result, Flags, MenuItem.SmallId, PWideChar(WinMenuCaption)) );
171
172 if (MenuItem is TMenuItemRadio) and TMenuItemRadio(MenuItem).Checked then
173 OSCheck( CheckMenuRadioItem(Result,
174 MenuItem.SmallId, MenuItem.SmallId, MenuItem.SmallId, MF_BYCOMMAND) );
175 end;
176
177 procedure AppendGLMenuSeparator;
178 begin
179 OSCheck( AppendMenu(Result, MF_SEPARATOR, 0, nil) );
180 end;
181
182 var
183 M: TMenuEntry;
184 i: Integer;
185 begin
186 if MenuBar then
187 begin
188 Result := CreateMenu;
189 OSCheck( Result <> 0, 'CreateMenu');
190 end else
191 begin
192 Result := CreatePopupMenu;
193 OSCheck( Result <> 0, 'CreatePopupMenu');
194 end;
195
196 for i := 0 to Menu.Count - 1 do
197 begin
198 M := Menu[i];
199 if M is TMenuItem then
200 AppendGLMenuItem(TMenuItem(M), ParentAllowsEnabled) else
201 if M is TMenuSeparator then
202 begin
203 if not MenuBar then AppendGLMenuSeparator;
204 end else
205 if M is TMenu then
206 AppendGLMenu(TMenu(M), ParentAllowsEnabled) else
207 raise EInternalError.Create('Not implemented TMenuEntry subclass');
208 end;
209 end;
210
211 { Convert Menu structure to a WinAPI menu. Resulting menu is menu bar
212 (obtained with CreateMenu) if MenuBar = true, else it's popup menu
213 (obtained with CreatePopupMenu).
214
215 Each TMenuItem will be added with identifier taken from it's SmallId.
216
217 If MenuBar then TMenuSeparator entries in Menu.Entries[] are ignored
218 (WinAPI toplevel menu bar cannot have a separator, that's quite sensible
219 actually). Of course separators at lower depths are not ignored. }
220 function MakeWinapiMenu(Menu: TMenu; MenuBar: boolean): HMenu;
221 begin
222 Result := MakeWinapiMenuCore(Menu, MenuBar, Menu.Enabled);
223 end;