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