428
|
1 /* Implements an elisp-programmable menubar -- Win32
|
|
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
|
442
|
4 Copyright (C) 1997 Kirill M. Katsnelson <kkm@kis.ru>.
|
1333
|
5 Copyright (C) 2000, 2001, 2002, 2003 Ben Wing.
|
428
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
9 XEmacs is free software; you can redistribute it and/or modify it
|
|
10 under the terms of the GNU General Public License as published by the
|
|
11 Free Software Foundation; either version 2, or (at your option) any
|
|
12 later version.
|
|
13
|
|
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
17 for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with XEmacs; see the file COPYING. If not, write to
|
|
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 Boston, MA 02111-1307, USA. */
|
|
23
|
|
24 /* Synched up with: Not in FSF. */
|
|
25
|
771
|
26 /* This function mostly Mule-ized (except perhaps some Unicode splitting).
|
|
27 5-2000. */
|
|
28
|
428
|
29 /* Author:
|
|
30 Initially written by kkm 12/24/97,
|
|
31 peeking into and copying stuff from menubar-x.c
|
|
32 */
|
|
33
|
|
34 /* Algorithm for handling menus is as follows. When window's menubar
|
|
35 * is created, current-menubar is not traversed in depth. Rather, only
|
|
36 * top level items, both items and pulldowns, are added to the
|
|
37 * menubar. Each pulldown is initially empty. When a pulldown is
|
|
38 * selected and about to open, corresponding element of
|
|
39 * current-menubar is found, and the newly open pulldown is
|
|
40 * populated. This is made again in the same non-recursive manner.
|
|
41 *
|
|
42 * This algorithm uses hash tables to find out element of the menu
|
|
43 * descriptor list given menu handle. The key is an opaque ptr data
|
|
44 * type, keeping menu handle, and the value is a list of strings
|
|
45 * representing the path from the root of the menu to the item
|
|
46 * descriptor. Each frame has an associated hash table.
|
|
47 *
|
|
48 * Leaf items are assigned a unique id based on item's hash. When an
|
|
49 * item is selected, Windows sends back the id. Unfortunately, only
|
|
50 * low 16 bit of the ID are sent, and there's no way to get the 32-bit
|
|
51 * value. Yes, Win32 is just a different set of bugs than X! Aside
|
|
52 * from this blame, another hashing mechanism is required to map menu
|
|
53 * ids to commands (which are actually Lisp_Object's). This mapping is
|
|
54 * performed in the same hash table, as the lifetime of both maps is
|
|
55 * exactly the same. This is unambigous, as menu handles are
|
|
56 * represented by lisp opaques, while command ids are by lisp
|
|
57 * integers. The additional advantage for this is that command forms
|
|
58 * are automatically GC-protected, which is important because these
|
|
59 * may be transient forms generated by :filter functions.
|
|
60 *
|
|
61 * The hash table is not allowed to grow too much; it is pruned
|
|
62 * whenever this is safe to do. This is done by re-creating the menu
|
|
63 * bar, and clearing and refilling the hash table from scratch.
|
|
64 *
|
|
65 * Popup menus are handled identically to pulldowns. A static hash
|
|
66 * table is used for popup menus, and lookup is made not in
|
|
67 * current-menubar but in a lisp form supplied to the `popup'
|
|
68 * function.
|
|
69 *
|
|
70 * Another Windows weirdness is that there's no way to tell that a
|
|
71 * popup has been dismissed without making selection. We need to know
|
|
72 * that to cleanup the popup menu hash table, but this is not honestly
|
|
73 * doable using *documented* sequence of messages. Sticking to
|
|
74 * particular knowledge is bad because this may break in Windows NT
|
|
75 * 5.0, or Windows 98, or other future version. Instead, I allow the
|
|
76 * hash tables to hang around, and not clear them, unless WM_COMMAND is
|
442
|
77 * received. This is worth some memory but more safe. Hacks welcome,
|
428
|
78 * anyways!
|
|
79 *
|
|
80 */
|
|
81
|
|
82 #include <config.h>
|
|
83 #include "lisp.h"
|
|
84
|
|
85 #include "buffer.h"
|
|
86 #include "commands.h"
|
872
|
87 #include "console-msw-impl.h"
|
428
|
88 #include "elhash.h"
|
|
89 #include "events.h"
|
872
|
90 #include "frame-impl.h"
|
428
|
91 #include "gui.h"
|
|
92 #include "lisp.h"
|
|
93 #include "menubar.h"
|
|
94 #include "opaque.h"
|
872
|
95 #include "window-impl.h"
|
428
|
96
|
|
97 /* #### */
|
442
|
98 #define REPLACE_ME_WITH_GLOBAL_VARIABLE_WHICH_CONTROLS_RIGHT_FLUSH 0
|
428
|
99
|
|
100 #define EMPTY_ITEM_ID ((UINT)LISP_TO_VOID (Qunbound))
|
771
|
101 #define EMPTY_ITEM_NAME "(empty)" /* WARNING: uses of this need XETEXT */
|
428
|
102
|
|
103 /* Current menu (bar or popup) descriptor. gcpro'ed */
|
|
104 static Lisp_Object current_menudesc;
|
|
105
|
|
106 /* Current menubar or popup hash table. gcpro'ed */
|
|
107 static Lisp_Object current_hash_table;
|
|
108
|
|
109 /* This is used to allocate unique ids to menu items.
|
|
110 Items ids are in MENU_ITEM_ID_MIN to MENU_ITEM_ID_MAX.
|
|
111 Allocation checks that the item is not already in
|
|
112 the TOP_LEVEL_MENU */
|
|
113
|
|
114 /* #### defines go to gui-msw.h, as the range is shared with toolbars
|
|
115 (If only toolbars will be implemented as common controls) */
|
|
116 #define MENU_ITEM_ID_MIN 0x8000
|
|
117 #define MENU_ITEM_ID_MAX 0xFFFF
|
|
118 #define MENU_ITEM_ID_BITS(x) (((x) & 0x7FFF) | 0x8000)
|
|
119 static HMENU top_level_menu;
|
|
120
|
|
121 /*
|
|
122 * This returns Windows-style menu item string:
|
|
123 * "Left Flush\tRight Flush"
|
|
124 */
|
442
|
125
|
771
|
126 static Lisp_Object
|
867
|
127 displayable_menu_item (Lisp_Object gui_item, int bar_p, Ichar *accel)
|
428
|
128 {
|
771
|
129 Lisp_Object left, right = Qnil;
|
428
|
130
|
|
131 /* Left flush part of the string */
|
771
|
132 left = gui_item_display_flush_left (gui_item);
|
428
|
133
|
771
|
134 left = mswindows_translate_menu_or_dialog_item (left, accel);
|
428
|
135
|
|
136 /* Right flush part, unless we're at the top-level where it's not allowed */
|
|
137 if (!bar_p)
|
771
|
138 right = gui_item_display_flush_right (gui_item);
|
442
|
139
|
771
|
140 if (!NILP (right))
|
|
141 return concat3 (left, build_string ("\t"), right);
|
|
142 else
|
|
143 return left;
|
428
|
144 }
|
|
145
|
|
146 /*
|
|
147 * hmenu_to_lisp_object() returns an opaque ptr given menu handle.
|
|
148 */
|
|
149 static Lisp_Object
|
|
150 hmenu_to_lisp_object (HMENU hmenu)
|
|
151 {
|
|
152 return make_opaque_ptr (hmenu);
|
|
153 }
|
|
154
|
|
155 /*
|
|
156 * Allocation tries a hash based on item's path and name first. This
|
|
157 * almost guarantees that the same item will override its old value in
|
|
158 * the hash table rather than abandon it.
|
|
159 */
|
|
160 static Lisp_Object
|
|
161 allocate_menu_item_id (Lisp_Object path, Lisp_Object name, Lisp_Object suffix)
|
|
162 {
|
|
163 UINT id = MENU_ITEM_ID_BITS (HASH3 (internal_hash (path, 0),
|
|
164 internal_hash (name, 0),
|
|
165 internal_hash (suffix, 0)));
|
|
166 do {
|
|
167 id = MENU_ITEM_ID_BITS (id + 1);
|
|
168 } while (GetMenuState (top_level_menu, id, MF_BYCOMMAND) != 0xFFFFFFFF);
|
|
169 return make_int (id);
|
|
170 }
|
|
171
|
|
172 static HMENU
|
|
173 create_empty_popup_menu (void)
|
|
174 {
|
|
175 return CreatePopupMenu ();
|
|
176 }
|
|
177
|
|
178 static void
|
|
179 empty_menu (HMENU menu, int add_empty_p)
|
|
180 {
|
|
181 while (DeleteMenu (menu, 0, MF_BYPOSITION));
|
|
182 if (add_empty_p)
|
771
|
183 qxeAppendMenu (menu, MF_STRING | MF_GRAYED, EMPTY_ITEM_ID,
|
|
184 XETEXT (EMPTY_ITEM_NAME));
|
428
|
185 }
|
|
186
|
|
187 /*
|
|
188 * The idea of checksumming is that we must hash minimal object
|
|
189 * which is necessarily changes when the item changes. For separator
|
|
190 * this is a constant, for grey strings and submenus these are hashes
|
|
191 * of names, since submenus are unpopulated until opened so always
|
|
192 * equal otherwise. For items, this is a full hash value of a callback,
|
|
193 * because a callback may me a form which can be changed only somewhere
|
|
194 * in depth.
|
|
195 */
|
|
196 static unsigned long
|
|
197 checksum_menu_item (Lisp_Object item)
|
|
198 {
|
|
199 if (STRINGP (item))
|
|
200 {
|
|
201 /* Separator or unselectable text - hash as a string + 13 */
|
|
202 if (separator_string_p (XSTRING_DATA (item)))
|
|
203 return 13;
|
|
204 else
|
|
205 return internal_hash (item, 0) + 13;
|
|
206 }
|
|
207 else if (CONSP (item))
|
|
208 {
|
|
209 /* Submenu - hash by its string name + 0 */
|
771
|
210 return internal_hash (XCAR (item), 0);
|
428
|
211 }
|
|
212 else if (VECTORP (item))
|
|
213 {
|
|
214 /* An ordinary item - hash its name and callback form. */
|
|
215 return HASH2 (internal_hash (XVECTOR_DATA(item)[0], 0),
|
|
216 internal_hash (XVECTOR_DATA(item)[1], 0));
|
|
217 }
|
442
|
218
|
428
|
219 /* An error - will be caught later */
|
|
220 return 0;
|
|
221 }
|
|
222
|
|
223 static void
|
|
224 populate_menu_add_item (HMENU menu, Lisp_Object path,
|
|
225 Lisp_Object hash_tab, Lisp_Object item,
|
442
|
226 Lisp_Object *accel_list,
|
428
|
227 int flush_right, int bar_p)
|
|
228 {
|
771
|
229 MENUITEMINFOW item_info;
|
428
|
230
|
|
231 item_info.cbSize = sizeof (item_info);
|
|
232 item_info.fMask = MIIM_TYPE | MIIM_STATE | MIIM_ID;
|
|
233 item_info.fState = 0;
|
|
234 item_info.wID = 0;
|
|
235 item_info.fType = 0;
|
|
236
|
|
237 if (STRINGP (item))
|
|
238 {
|
|
239 /* Separator or unselectable text */
|
|
240 if (separator_string_p (XSTRING_DATA (item)))
|
771
|
241 item_info.fType = MFT_SEPARATOR;
|
428
|
242 else
|
|
243 {
|
771
|
244 Extbyte *itemext;
|
|
245
|
428
|
246 item_info.fType = MFT_STRING;
|
|
247 item_info.fState = MFS_DISABLED;
|
771
|
248 LISP_STRING_TO_TSTR (item, itemext);
|
|
249 item_info.dwTypeData = (XELPTSTR) itemext;
|
428
|
250 }
|
|
251 }
|
|
252 else if (CONSP (item))
|
|
253 {
|
|
254 /* Submenu */
|
|
255 HMENU submenu;
|
|
256 Lisp_Object gui_item = allocate_gui_item ();
|
442
|
257 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
|
|
258 struct gcpro gcpro1, gcpro2, gcpro3;
|
867
|
259 Ichar accel;
|
771
|
260 Extbyte *itemext;
|
428
|
261
|
442
|
262 GCPRO3 (gui_item, path, *accel_list);
|
428
|
263
|
|
264 menu_parse_submenu_keywords (item, gui_item);
|
|
265
|
|
266 if (!STRINGP (pgui_item->name))
|
563
|
267 invalid_argument ("Menu name (first element) must be a string",
|
442
|
268 item);
|
428
|
269
|
|
270 if (!gui_item_included_p (gui_item, Vmenubar_configuration))
|
442
|
271 {
|
|
272 UNGCPRO;
|
|
273 goto done;
|
|
274 }
|
428
|
275
|
1913
|
276 if (!gui_item_active_p (gui_item))
|
771
|
277 item_info.fState = MFS_GRAYED;
|
428
|
278 /* Temptation is to put 'else' right here. Although, the
|
|
279 displayed item won't have an arrow indicating that it is a
|
|
280 popup. So we go ahead a little bit more and create a popup */
|
442
|
281 submenu = create_empty_popup_menu ();
|
428
|
282
|
|
283 item_info.fMask |= MIIM_SUBMENU;
|
771
|
284 LISP_STRING_TO_TSTR (displayable_menu_item (gui_item, bar_p, &accel),
|
|
285 itemext);
|
|
286 item_info.dwTypeData = (XELPTSTR) itemext;
|
428
|
287 item_info.hSubMenu = submenu;
|
442
|
288
|
|
289 if (accel && bar_p)
|
|
290 *accel_list = Fcons (make_char (accel), *accel_list);
|
428
|
291
|
|
292 if (!(item_info.fState & MFS_GRAYED))
|
|
293 {
|
|
294 /* Now add the full submenu path as a value to the hash table,
|
|
295 keyed by menu handle */
|
|
296 if (NILP(path))
|
|
297 path = list1 (pgui_item->name);
|
|
298 else
|
|
299 {
|
|
300 Lisp_Object arg[2];
|
|
301 arg[0] = path;
|
|
302 arg[1] = list1 (pgui_item->name);
|
|
303 path = Fappend (2, arg);
|
|
304 }
|
|
305
|
|
306 Fputhash (hmenu_to_lisp_object (submenu), path, hash_tab);
|
|
307 }
|
442
|
308 UNGCPRO;
|
|
309 }
|
428
|
310 else if (VECTORP (item))
|
|
311 {
|
|
312 /* An ordinary item */
|
|
313 Lisp_Object style, id;
|
|
314 Lisp_Object gui_item = gui_parse_item_keywords (item);
|
442
|
315 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
|
|
316 struct gcpro gcpro1, gcpro2;
|
867
|
317 Ichar accel;
|
771
|
318 Extbyte *itemext;
|
428
|
319
|
442
|
320 GCPRO2 (gui_item, *accel_list);
|
428
|
321
|
|
322 if (!gui_item_included_p (gui_item, Vmenubar_configuration))
|
442
|
323 {
|
|
324 UNGCPRO;
|
|
325 goto done;
|
|
326 }
|
|
327
|
|
328 if (!STRINGP (pgui_item->name))
|
|
329 pgui_item->name = Feval (pgui_item->name);
|
428
|
330
|
1913
|
331 if (!gui_item_active_p (gui_item))
|
771
|
332 item_info.fState = MFS_GRAYED;
|
428
|
333
|
|
334 style = (NILP (pgui_item->selected) || NILP (Feval (pgui_item->selected))
|
|
335 ? Qnil : pgui_item->style);
|
|
336
|
|
337 if (EQ (style, Qradio))
|
|
338 {
|
|
339 item_info.fType |= MFT_RADIOCHECK;
|
|
340 item_info.fState |= MFS_CHECKED;
|
|
341 }
|
|
342 else if (EQ (style, Qtoggle))
|
771
|
343 item_info.fState |= MFS_CHECKED;
|
428
|
344
|
|
345 id = allocate_menu_item_id (path, pgui_item->name,
|
|
346 pgui_item->suffix);
|
|
347 Fputhash (id, pgui_item->callback, hash_tab);
|
|
348
|
442
|
349 item_info.wID = (UINT) XINT (id);
|
428
|
350 item_info.fType |= MFT_STRING;
|
771
|
351 LISP_STRING_TO_TSTR (displayable_menu_item (gui_item, bar_p, &accel),
|
|
352 itemext);
|
|
353 item_info.dwTypeData = (XELPTSTR) itemext;
|
428
|
354
|
442
|
355 if (accel && bar_p)
|
|
356 *accel_list = Fcons (make_char (accel), *accel_list);
|
|
357
|
|
358 UNGCPRO;
|
428
|
359 }
|
|
360 else
|
563
|
361 sferror ("Malformed menu item descriptor", item);
|
428
|
362
|
|
363 if (flush_right)
|
771
|
364 item_info.fType |= MFT_RIGHTJUSTIFY;
|
428
|
365
|
771
|
366 qxeInsertMenuItem (menu, UINT_MAX, TRUE, &item_info);
|
442
|
367
|
|
368 done:;
|
|
369 }
|
428
|
370
|
|
371 /*
|
|
372 * This function is called from populate_menu and checksum_menu.
|
|
373 * When called to populate, MENU is a menu handle, PATH is a
|
|
374 * list of strings representing menu path from root to this submenu,
|
|
375 * DESCRIPTOR is a menu descriptor, HASH_TAB is a hash table associated
|
|
376 * with root menu, BAR_P indicates whether this called for a menubar or
|
|
377 * a popup, and POPULATE_P is non-zero. Return value must be ignored.
|
|
378 * When called to checksum, DESCRIPTOR has the same meaning, POPULATE_P
|
|
379 * is zero, PATH must be Qnil, and the rest of parameters is ignored.
|
|
380 * Return value is the menu checksum.
|
|
381 */
|
|
382 static unsigned long
|
|
383 populate_or_checksum_helper (HMENU menu, Lisp_Object path, Lisp_Object desc,
|
|
384 Lisp_Object hash_tab, int bar_p, int populate_p)
|
|
385 {
|
|
386 Lisp_Object item_desc;
|
|
387 int deep_p, flush_right;
|
442
|
388 struct gcpro gcpro1, gcpro2, gcpro3;
|
428
|
389 unsigned long checksum;
|
|
390 Lisp_Object gui_item = allocate_gui_item ();
|
442
|
391 Lisp_Object accel_list = Qnil;
|
|
392 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
|
|
393
|
|
394 GCPRO3 (gui_item, accel_list, desc);
|
428
|
395
|
|
396 /* We are sometimes called with the menubar unchanged, and with changed
|
|
397 right flush. We have to update the menubar in this case,
|
|
398 so account for the compliance setting in the hash value */
|
442
|
399 checksum = REPLACE_ME_WITH_GLOBAL_VARIABLE_WHICH_CONTROLS_RIGHT_FLUSH;
|
428
|
400
|
|
401 /* Will initially contain only "(empty)" */
|
|
402 if (populate_p)
|
|
403 empty_menu (menu, 1);
|
|
404
|
|
405 /* PATH set to nil indicates top-level popup or menubar */
|
|
406 deep_p = !NILP (path);
|
|
407
|
|
408 /* Fetch keywords prepending the item list */
|
|
409 desc = menu_parse_submenu_keywords (desc, gui_item);
|
|
410
|
|
411 /* Check that menu name is specified when expected */
|
|
412 if (NILP (pgui_item->name) && deep_p)
|
563
|
413 sferror ("Menu must have a name", desc);
|
428
|
414
|
|
415 /* Apply filter if specified */
|
|
416 if (!NILP (pgui_item->filter))
|
|
417 desc = call1 (pgui_item->filter, desc);
|
|
418
|
|
419 /* Loop thru the desc's CDR and add items for each entry */
|
|
420 flush_right = 0;
|
|
421 EXTERNAL_LIST_LOOP (item_desc, desc)
|
|
422 {
|
|
423 if (NILP (XCAR (item_desc)))
|
|
424 {
|
|
425 /* Do not flush right menubar items when MS style compliant */
|
442
|
426 if (bar_p && !REPLACE_ME_WITH_GLOBAL_VARIABLE_WHICH_CONTROLS_RIGHT_FLUSH)
|
428
|
427 flush_right = 1;
|
|
428 if (!populate_p)
|
|
429 checksum = HASH2 (checksum, LISP_HASH (Qnil));
|
|
430 }
|
|
431 else if (populate_p)
|
|
432 populate_menu_add_item (menu, path, hash_tab,
|
442
|
433 XCAR (item_desc), &accel_list,
|
|
434 flush_right, bar_p);
|
428
|
435 else
|
|
436 checksum = HASH2 (checksum,
|
|
437 checksum_menu_item (XCAR (item_desc)));
|
|
438 }
|
442
|
439
|
428
|
440 if (populate_p)
|
|
441 {
|
|
442 /* Remove the "(empty)" item, if there are other ones */
|
|
443 if (GetMenuItemCount (menu) > 1)
|
|
444 RemoveMenu (menu, EMPTY_ITEM_ID, MF_BYCOMMAND);
|
|
445
|
|
446 /* Add the header to the popup, if told so. The same as in X - an
|
|
447 insensitive item, and a separator (Seems to me, there were
|
442
|
448 two separators in X... In Windows this looks ugly, anyways.) */
|
|
449 if (!bar_p && !deep_p && popup_menu_titles && !NILP (pgui_item->name))
|
428
|
450 {
|
771
|
451 Extbyte *nameext;
|
|
452
|
|
453 LISP_STRING_TO_TSTR (displayable_menu_item (gui_item, bar_p, NULL),
|
|
454 nameext);
|
|
455 qxeInsertMenu (menu, 0, MF_BYPOSITION | MF_STRING | MF_DISABLED,
|
|
456 0, nameext);
|
|
457 qxeInsertMenu (menu, 1, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
|
|
458 SetMenuDefaultItem (menu, 0, MF_BYPOSITION);
|
428
|
459 }
|
|
460 }
|
442
|
461
|
|
462 if (bar_p)
|
|
463 Fputhash (Qt, accel_list, hash_tab);
|
|
464
|
|
465 UNGCPRO;
|
428
|
466 return checksum;
|
|
467 }
|
|
468
|
|
469 static void
|
|
470 populate_menu (HMENU menu, Lisp_Object path, Lisp_Object desc,
|
442
|
471 Lisp_Object hash_tab, int bar_p)
|
428
|
472 {
|
|
473 populate_or_checksum_helper (menu, path, desc, hash_tab, bar_p, 1);
|
|
474 }
|
|
475
|
|
476 static unsigned long
|
|
477 checksum_menu (Lisp_Object desc)
|
|
478 {
|
|
479 return populate_or_checksum_helper (NULL, Qnil, desc, Qunbound, 0, 0);
|
|
480 }
|
|
481
|
|
482 static void
|
442
|
483 update_frame_menubar_maybe (struct frame *f)
|
428
|
484 {
|
|
485 HMENU menubar = GetMenu (FRAME_MSWINDOWS_HANDLE (f));
|
|
486 struct window *w = XWINDOW (FRAME_LAST_NONMINIBUF_WINDOW (f));
|
|
487 Lisp_Object desc = (!NILP (w->menubar_visible_p)
|
|
488 ? symbol_value_in_buffer (Qcurrent_menubar, w->buffer)
|
|
489 : Qnil);
|
442
|
490 struct gcpro gcpro1;
|
|
491
|
|
492 GCPRO1 (desc); /* it's safest to do this, just in case some filter
|
|
493 or something changes the value of current-menubar */
|
428
|
494
|
|
495 top_level_menu = menubar;
|
|
496
|
|
497 if (NILP (desc) && menubar != NULL)
|
|
498 {
|
|
499 /* Menubar has gone */
|
442
|
500 FRAME_MSWINDOWS_MENU_HASH_TABLE (f) = Qnil;
|
428
|
501 SetMenu (FRAME_MSWINDOWS_HANDLE (f), NULL);
|
|
502 DestroyMenu (menubar);
|
|
503 DrawMenuBar (FRAME_MSWINDOWS_HANDLE (f));
|
442
|
504 UNGCPRO;
|
428
|
505 return;
|
|
506 }
|
|
507
|
|
508 if (!NILP (desc) && menubar == NULL)
|
|
509 {
|
|
510 /* Menubar has appeared */
|
|
511 menubar = CreateMenu ();
|
|
512 goto populate;
|
|
513 }
|
|
514
|
|
515 if (NILP (desc))
|
|
516 {
|
|
517 /* We did not have the bar and are not going to */
|
442
|
518 UNGCPRO;
|
428
|
519 return;
|
|
520 }
|
|
521
|
|
522 /* Now we bail out if the menubar has not changed */
|
442
|
523 if (FRAME_MSWINDOWS_MENU_CHECKSUM (f) == checksum_menu (desc))
|
|
524 {
|
|
525 UNGCPRO;
|
|
526 return;
|
|
527 }
|
428
|
528
|
|
529 populate:
|
|
530 /* Come with empty hash table */
|
442
|
531 if (NILP (FRAME_MSWINDOWS_MENU_HASH_TABLE (f)))
|
|
532 FRAME_MSWINDOWS_MENU_HASH_TABLE (f) =
|
428
|
533 make_lisp_hash_table (50, HASH_TABLE_NON_WEAK, HASH_TABLE_EQUAL);
|
|
534 else
|
442
|
535 Fclrhash (FRAME_MSWINDOWS_MENU_HASH_TABLE (f));
|
428
|
536
|
|
537 Fputhash (hmenu_to_lisp_object (menubar), Qnil,
|
442
|
538 FRAME_MSWINDOWS_MENU_HASH_TABLE (f));
|
428
|
539 populate_menu (menubar, Qnil, desc,
|
442
|
540 FRAME_MSWINDOWS_MENU_HASH_TABLE (f), 1);
|
428
|
541 SetMenu (FRAME_MSWINDOWS_HANDLE (f), menubar);
|
|
542 DrawMenuBar (FRAME_MSWINDOWS_HANDLE (f));
|
|
543
|
442
|
544 FRAME_MSWINDOWS_MENU_CHECKSUM (f) = checksum_menu (desc);
|
|
545
|
|
546 UNGCPRO;
|
428
|
547 }
|
|
548
|
|
549 static void
|
|
550 prune_menubar (struct frame *f)
|
|
551 {
|
|
552 HMENU menubar = GetMenu (FRAME_MSWINDOWS_HANDLE (f));
|
|
553 Lisp_Object desc = current_frame_menubar (f);
|
442
|
554 struct gcpro gcpro1;
|
|
555
|
428
|
556 if (menubar == NULL)
|
|
557 return;
|
|
558
|
|
559 /* #### If a filter function has set desc to Qnil, this abort()
|
|
560 triggers. To resolve, we must prevent filters explicitly from
|
|
561 mangling with the active menu. In apply_filter probably?
|
|
562 Is copy-tree on the whole menu too expensive? */
|
442
|
563 if (NILP (desc))
|
428
|
564 /* abort(); */
|
|
565 return;
|
|
566
|
442
|
567 GCPRO1 (desc); /* just to be safe -- see above */
|
428
|
568 /* We do the trick by removing all items and re-populating top level */
|
|
569 empty_menu (menubar, 0);
|
|
570
|
442
|
571 assert (HASH_TABLEP (FRAME_MSWINDOWS_MENU_HASH_TABLE (f)));
|
|
572 Fclrhash (FRAME_MSWINDOWS_MENU_HASH_TABLE (f));
|
428
|
573
|
|
574 Fputhash (hmenu_to_lisp_object (menubar), Qnil,
|
442
|
575 FRAME_MSWINDOWS_MENU_HASH_TABLE (f));
|
|
576 populate_menu (menubar, Qnil, desc,
|
|
577 FRAME_MSWINDOWS_MENU_HASH_TABLE (f), 1);
|
|
578 UNGCPRO;
|
428
|
579 }
|
|
580
|
|
581 /*
|
|
582 * This is called when cleanup is possible. It is better not to
|
|
583 * clean things up at all than do it too early!
|
|
584 */
|
|
585 static void
|
|
586 menu_cleanup (struct frame *f)
|
|
587 {
|
|
588 /* This function can GC */
|
|
589 current_menudesc = Qnil;
|
|
590 current_hash_table = Qnil;
|
|
591 prune_menubar (f);
|
|
592 }
|
442
|
593
|
|
594 int
|
867
|
595 mswindows_char_is_accelerator (struct frame *f, Ichar ch)
|
442
|
596 {
|
|
597 Lisp_Object hash = FRAME_MSWINDOWS_MENU_HASH_TABLE (f);
|
|
598
|
|
599 if (NILP (hash))
|
|
600 return 0;
|
771
|
601 return !NILP (memq_no_quit
|
|
602 (make_char
|
|
603 (DOWNCASE (WINDOW_XBUFFER (FRAME_SELECTED_XWINDOW (f)), ch)),
|
|
604 Fgethash (Qt, hash, Qnil)));
|
442
|
605 }
|
|
606
|
428
|
607
|
|
608 /*------------------------------------------------------------------------*/
|
|
609 /* Message handlers */
|
|
610 /*------------------------------------------------------------------------*/
|
|
611 static Lisp_Object
|
442
|
612 unsafe_handle_wm_initmenupopup_1 (HMENU menu, struct frame *f)
|
428
|
613 {
|
|
614 /* This function can call lisp, beat dogs and stick chewing gum to
|
|
615 everything! */
|
|
616
|
|
617 Lisp_Object path, desc;
|
|
618 struct gcpro gcpro1;
|
707
|
619
|
428
|
620 /* Find which guy is going to explode */
|
|
621 path = Fgethash (hmenu_to_lisp_object (menu), current_hash_table, Qunbound);
|
|
622 assert (!UNBOUNDP (path));
|
|
623 #ifdef DEBUG_XEMACS
|
|
624 /* Allow to continue in a debugger after assert - not so fatal */
|
|
625 if (UNBOUNDP (path))
|
563
|
626 signal_error (Qinternal_error, "internal menu error", Qunbound);
|
428
|
627 #endif
|
|
628
|
|
629 /* Now find a desc chunk for it. If none, then probably menu open
|
|
630 hook has played too much games around stuff */
|
|
631 desc = Fmenu_find_real_submenu (current_menudesc, path);
|
|
632 if (NILP (desc))
|
563
|
633 invalid_state ("This menu does not exist any more", path);
|
428
|
634
|
|
635 /* Now, stuff it */
|
|
636 /* DESC may be generated by filter, so we have to gcpro it */
|
|
637 GCPRO1 (desc);
|
|
638 populate_menu (menu, path, desc, current_hash_table, 0);
|
|
639 UNGCPRO;
|
|
640 return Qt;
|
|
641 }
|
|
642
|
|
643 static Lisp_Object
|
442
|
644 unsafe_handle_wm_initmenu_1 (struct frame *f)
|
428
|
645 {
|
|
646 /* This function can call lisp */
|
|
647
|
|
648 /* NOTE: This is called for the bar only, WM_INITMENU
|
|
649 for popups is filtered out */
|
|
650
|
|
651 /* #### - this menubar update mechanism is expensively anti-social and
|
|
652 the activate-menubar-hook is now mostly obsolete. */
|
|
653
|
|
654 /* We simply ignore return value. In any case, we construct the bar
|
|
655 on the fly */
|
853
|
656 run_hook_trapping_problems
|
1333
|
657 (Qmenubar, Qactivate_menubar_hook,
|
853
|
658 INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION);
|
428
|
659
|
|
660 update_frame_menubar_maybe (f);
|
|
661
|
|
662 current_menudesc = current_frame_menubar (f);
|
442
|
663 current_hash_table = FRAME_MSWINDOWS_MENU_HASH_TABLE (f);
|
428
|
664 assert (HASH_TABLEP (current_hash_table));
|
|
665
|
|
666 return Qt;
|
|
667 }
|
|
668
|
|
669 /*
|
|
670 * Return value is Qt if we have dispatched the command,
|
|
671 * or Qnil if id has not been mapped to a callback.
|
|
672 * Window procedure may try other targets to route the
|
|
673 * command if we return nil
|
|
674 */
|
|
675 Lisp_Object
|
442
|
676 mswindows_handle_wm_command (struct frame *f, WORD id)
|
428
|
677 {
|
|
678 /* Try to map the command id through the proper hash table */
|
|
679 Lisp_Object data, fn, arg, frame;
|
|
680 struct gcpro gcpro1;
|
|
681
|
|
682 if (NILP (current_hash_table))
|
|
683 return Qnil;
|
|
684
|
|
685 data = Fgethash (make_int (id), current_hash_table, Qunbound);
|
|
686
|
|
687 if (UNBOUNDP (data))
|
|
688 {
|
|
689 menu_cleanup (f);
|
|
690 return Qnil;
|
|
691 }
|
|
692
|
|
693 /* Need to gcpro because the hash table may get destroyed by
|
|
694 menu_cleanup(), and will not gcpro the data any more */
|
|
695 GCPRO1 (data);
|
|
696 menu_cleanup (f);
|
|
697
|
|
698 /* Ok, this is our one. Enqueue it. */
|
|
699 get_gui_callback (data, &fn, &arg);
|
793
|
700 frame = wrap_frame (f);
|
428
|
701 /* this used to call mswindows_enqueue_misc_user_event but that
|
|
702 breaks customize because the misc_event gets eval'ed in some
|
442
|
703 circumstances. Don't change it back unless you can fix the
|
771
|
704 customize problem also. */
|
707
|
705 mswindows_enqueue_misc_user_event (frame, fn, arg);
|
428
|
706
|
|
707 UNGCPRO; /* data */
|
|
708 return Qt;
|
|
709 }
|
|
710
|
|
711
|
|
712 /*------------------------------------------------------------------------*/
|
|
713 /* Message handling proxies */
|
|
714 /*------------------------------------------------------------------------*/
|
|
715
|
1268
|
716 struct handle_wm_initmenu
|
|
717 {
|
|
718 HMENU menu;
|
|
719 struct frame *frame;
|
|
720 };
|
428
|
721
|
|
722 static Lisp_Object
|
1268
|
723 unsafe_handle_wm_initmenupopup (void *arg)
|
428
|
724 {
|
1268
|
725 struct handle_wm_initmenu *z = (struct handle_wm_initmenu *) arg;
|
|
726 return unsafe_handle_wm_initmenupopup_1 (z->menu, z->frame);
|
428
|
727 }
|
|
728
|
|
729 static Lisp_Object
|
1268
|
730 unsafe_handle_wm_initmenu (void *arg)
|
428
|
731 {
|
1268
|
732 struct handle_wm_initmenu *z = (struct handle_wm_initmenu *) arg;
|
|
733 return unsafe_handle_wm_initmenu_1 (z->frame);
|
428
|
734 }
|
|
735
|
|
736 Lisp_Object
|
442
|
737 mswindows_handle_wm_initmenupopup (HMENU hmenu, struct frame *frm)
|
428
|
738 {
|
1268
|
739 struct handle_wm_initmenu z;
|
1279
|
740 int depth = internal_bind_int (&in_menu_callback, 1);
|
|
741 Lisp_Object retval;
|
1268
|
742
|
|
743 z.menu = hmenu;
|
|
744 z.frame = frm;
|
|
745
|
|
746 /* [[ Allow runaway filter code, e.g. custom, to be aborted. We are
|
853
|
747 usually called from next_event_internal(), which has turned off
|
1268
|
748 quit checking to read the C-g as an event.]]
|
|
749
|
|
750 #### This is bogus because by the very act of calling
|
|
751 event_stream_protect_modal_loop(), we disable event retrieval! */
|
1279
|
752 retval = event_stream_protect_modal_loop ("Error during menu handling",
|
|
753 unsafe_handle_wm_initmenupopup, &z,
|
|
754 UNINHIBIT_QUIT);
|
|
755 unbind_to (depth);
|
|
756
|
|
757 return retval;
|
428
|
758 }
|
|
759
|
|
760 Lisp_Object
|
442
|
761 mswindows_handle_wm_initmenu (HMENU hmenu, struct frame *f)
|
428
|
762 {
|
|
763 /* Handle only frame menubar, ignore if from popup or system menu */
|
442
|
764 if (GetMenu (FRAME_MSWINDOWS_HANDLE (f)) == hmenu)
|
428
|
765 {
|
1268
|
766 struct handle_wm_initmenu z;
|
|
767
|
|
768 z.frame = f;
|
|
769 return event_stream_protect_modal_loop ("Error during menu handling",
|
|
770 unsafe_handle_wm_initmenu, &z,
|
|
771 UNINHIBIT_QUIT);
|
428
|
772 }
|
|
773 return Qt;
|
|
774 }
|
|
775
|
|
776
|
|
777 /*------------------------------------------------------------------------*/
|
|
778 /* Methods */
|
|
779 /*------------------------------------------------------------------------*/
|
|
780
|
|
781 static void
|
442
|
782 mswindows_update_frame_menubars (struct frame *f)
|
428
|
783 {
|
|
784 update_frame_menubar_maybe (f);
|
|
785 }
|
|
786
|
|
787 static void
|
442
|
788 mswindows_free_frame_menubars (struct frame *f)
|
428
|
789 {
|
442
|
790 FRAME_MSWINDOWS_MENU_HASH_TABLE (f) = Qnil;
|
428
|
791 }
|
|
792
|
|
793 static void
|
|
794 mswindows_popup_menu (Lisp_Object menu_desc, Lisp_Object event)
|
|
795 {
|
|
796 struct frame *f = selected_frame ();
|
440
|
797 Lisp_Event *eev = NULL;
|
428
|
798 HMENU menu;
|
|
799 POINT pt;
|
|
800 int ok;
|
442
|
801 struct gcpro gcpro1;
|
|
802
|
|
803 GCPRO1 (menu_desc); /* to be safe -- see above */
|
428
|
804
|
|
805 if (!NILP (event))
|
|
806 {
|
|
807 CHECK_LIVE_EVENT (event);
|
|
808 eev = XEVENT (event);
|
|
809 if (eev->event_type != button_press_event
|
|
810 && eev->event_type != button_release_event)
|
|
811 wrong_type_argument (Qmouse_event_p, event);
|
|
812 }
|
|
813 else if (!NILP (Vthis_command_keys))
|
|
814 {
|
|
815 /* if an event wasn't passed, use the last event of the event sequence
|
|
816 currently being executed, if that event is a mouse event */
|
|
817 eev = XEVENT (Vthis_command_keys); /* last event first */
|
|
818 if (eev->event_type != button_press_event
|
|
819 && eev->event_type != button_release_event)
|
|
820 eev = NULL;
|
|
821 }
|
|
822
|
707
|
823 popup_up_p++;
|
|
824
|
428
|
825 /* Default is to put the menu at the point (10, 10) in frame */
|
|
826 if (eev)
|
|
827 {
|
1204
|
828 pt.x = EVENT_BUTTON_X (eev);
|
|
829 pt.y = EVENT_BUTTON_Y (eev);
|
428
|
830 ClientToScreen (FRAME_MSWINDOWS_HANDLE (f), &pt);
|
|
831 }
|
|
832 else
|
|
833 pt.x = pt.y = 10;
|
|
834
|
|
835 if (SYMBOLP (menu_desc))
|
|
836 menu_desc = Fsymbol_value (menu_desc);
|
|
837 CHECK_CONS (menu_desc);
|
|
838 CHECK_STRING (XCAR (menu_desc));
|
|
839
|
707
|
840 menu_cleanup (f);
|
|
841
|
428
|
842 current_menudesc = menu_desc;
|
|
843 current_hash_table =
|
|
844 make_lisp_hash_table (10, HASH_TABLE_NON_WEAK, HASH_TABLE_EQUAL);
|
442
|
845 menu = create_empty_popup_menu ();
|
428
|
846 Fputhash (hmenu_to_lisp_object (menu), Qnil, current_hash_table);
|
|
847 top_level_menu = menu;
|
442
|
848
|
428
|
849 /* see comments in menubar-x.c */
|
|
850 if (zmacs_regions)
|
|
851 zmacs_region_stays = 1;
|
442
|
852
|
428
|
853 ok = TrackPopupMenu (menu,
|
|
854 TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
|
|
855 pt.x, pt.y, 0,
|
|
856 FRAME_MSWINDOWS_HANDLE (f), NULL);
|
|
857
|
|
858 DestroyMenu (menu);
|
|
859
|
707
|
860 /* A WM_COMMAND is not issued until TrackPopupMenu returns. This
|
|
861 makes setting popup_up_p fairly pointless since we cannot keep
|
|
862 the menu up and dispatch events. Furthermore, we seem to have
|
|
863 little control over what happens to the menu when we click. */
|
|
864 popup_up_p--;
|
|
865
|
|
866 /* Signal a signal if caught by Track...() modal loop. */
|
|
867 /* I think this is pointless, the code hasn't actually put us in a
|
|
868 modal loop at this time -- andyp. */
|
428
|
869 mswindows_unmodalize_signal_maybe ();
|
|
870
|
|
871 /* This is probably the only real reason for failure */
|
442
|
872 if (!ok)
|
|
873 {
|
|
874 menu_cleanup (f);
|
563
|
875 invalid_operation ("Cannot track popup menu while in menu",
|
|
876 menu_desc);
|
442
|
877 }
|
|
878 UNGCPRO;
|
428
|
879 }
|
|
880
|
|
881
|
|
882 /*------------------------------------------------------------------------*/
|
|
883 /* Initialization */
|
|
884 /*------------------------------------------------------------------------*/
|
|
885 void
|
|
886 syms_of_menubar_mswindows (void)
|
|
887 {
|
|
888 }
|
|
889
|
|
890 void
|
|
891 console_type_create_menubar_mswindows (void)
|
|
892 {
|
|
893 CONSOLE_HAS_METHOD (mswindows, update_frame_menubars);
|
|
894 CONSOLE_HAS_METHOD (mswindows, free_frame_menubars);
|
|
895 CONSOLE_HAS_METHOD (mswindows, popup_menu);
|
|
896 }
|
|
897
|
|
898 void
|
|
899 vars_of_menubar_mswindows (void)
|
|
900 {
|
|
901 current_menudesc = Qnil;
|
|
902 current_hash_table = Qnil;
|
|
903
|
|
904 staticpro (¤t_menudesc);
|
|
905 staticpro (¤t_hash_table);
|
|
906 }
|