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;
|
|
280
|
|
281 gui_item_init (&gui_item);
|
269
|
282 GCPRO_GUI_ITEM (&gui_item);
|
251
|
283
|
|
284 menu_parse_submenu_keywords (item, &gui_item);
|
|
285
|
|
286 if (!STRINGP (gui_item.name))
|
233
|
287 signal_simple_error ("Menu name (first element) must be a string", item);
|
231
|
288
|
251
|
289 if (!gui_item_included_p (&gui_item, Vmenubar_configuration))
|
231
|
290 return;
|
|
291
|
251
|
292 if (!gui_item_active_p (&gui_item))
|
231
|
293 item_info.fState = MFS_GRAYED;
|
|
294 /* Temptation is to put 'else' right here. Although, the
|
|
295 displayed item won't have an arrow indicating that it is a
|
|
296 popup. So we go ahead a little bit more and create a popup */
|
|
297 submenu = create_empty_popup_menu();
|
|
298
|
|
299 item_info.fMask |= MIIM_SUBMENU;
|
251
|
300 item_info.dwTypeData = displayable_menu_item (&gui_item);
|
231
|
301 item_info.hSubMenu = submenu;
|
|
302
|
|
303 if (!(item_info.fState & MFS_GRAYED))
|
|
304 {
|
|
305 /* Now add the full submenu path as a value to the hash table,
|
|
306 keyed by menu handle */
|
|
307 if (NILP(path))
|
251
|
308 /* list1 cannot GC */
|
|
309 path = list1 (gui_item.name);
|
|
310 else
|
|
311 {
|
282
|
312 Lisp_Object arg[2];
|
|
313 arg[0] = path;
|
|
314 arg[1] = list1 (gui_item.name);
|
251
|
315 /* Fappend gcpro'es its arg */
|
|
316 path = Fappend (2, arg);
|
|
317 }
|
231
|
318
|
251
|
319 /* Fputhash GCPRO'es PATH */
|
231
|
320 Fputhash (hmenu_to_lisp_object (submenu), path, hash_tab);
|
|
321 }
|
251
|
322 UNGCPRO; /* gui_item */
|
231
|
323 }
|
|
324 else if (VECTORP (item))
|
|
325 {
|
|
326 /* An ordinary item */
|
251
|
327 Lisp_Object style, id;
|
|
328 struct gui_item gui_item;
|
|
329 struct gcpro gcpro1;
|
231
|
330
|
251
|
331 gui_item_init (&gui_item);
|
269
|
332 GCPRO_GUI_ITEM (&gui_item);
|
251
|
333
|
|
334 gui_parse_item_keywords (item, &gui_item);
|
|
335
|
|
336 if (!gui_item_included_p (&gui_item, Vmenubar_configuration))
|
231
|
337 return;
|
|
338
|
251
|
339 if (!gui_item_active_p (&gui_item))
|
|
340 item_info.fState = MFS_GRAYED;
|
231
|
341
|
251
|
342 style = (NILP (gui_item.selected) || NILP (Feval (gui_item.selected))
|
|
343 ? Qnil : gui_item.style);
|
|
344
|
231
|
345 if (EQ (style, Qradio))
|
|
346 {
|
|
347 item_info.fType |= MFT_RADIOCHECK;
|
|
348 item_info.fState |= MFS_CHECKED;
|
|
349 }
|
|
350 else if (EQ (style, Qtoggle))
|
|
351 {
|
|
352 item_info.fState |= MFS_CHECKED;
|
|
353 }
|
|
354
|
251
|
355 id = allocate_menu_item_id (path, gui_item.name,
|
|
356 gui_item.suffix);
|
|
357 Fputhash (id, gui_item.callback, hash_tab);
|
231
|
358
|
|
359 item_info.wID = (UINT) XINT(id);
|
|
360 item_info.fType |= MFT_STRING;
|
251
|
361 item_info.dwTypeData = displayable_menu_item (&gui_item);
|
|
362
|
|
363 UNGCPRO; /* gui_item */
|
231
|
364 }
|
|
365 else
|
|
366 {
|
251
|
367 signal_simple_error ("Mailformed menu item descriptor", item);
|
231
|
368 }
|
|
369
|
|
370 if (flush_right)
|
|
371 item_info.fType |= MFT_RIGHTJUSTIFY;
|
|
372
|
|
373 InsertMenuItem (menu, UINT_MAX, TRUE, &item_info);
|
|
374 }
|
|
375
|
233
|
376 /*
|
|
377 * This function is called from populate_menu and checksum_menu.
|
|
378 * When called to populate, MENU is a menu handle, PATH is a
|
|
379 * list of strings representing menu path from root to this submenu,
|
|
380 * DESCRIPTOR is a menu descriptor, HASH_TAB is a hashtable associated
|
|
381 * with root menu, BAR_P indicates whether this called for a menubar or
|
|
382 * a popup, and POPULATE_P is non-zero. Return value must be ignored.
|
|
383 * When called to checksum, DESCRIPTOR has the same meaning, POPULATE_P
|
|
384 * is zero, PATH must be Qnil, and the rest of parameters is ignored.
|
|
385 * Return value is the menu checksum.
|
|
386 */
|
|
387 static unsigned long
|
251
|
388 populate_or_checksum_helper (HMENU menu, Lisp_Object path, Lisp_Object desc,
|
233
|
389 Lisp_Object hash_tab, int bar_p, int populate_p)
|
231
|
390 {
|
251
|
391 Lisp_Object item_desc;
|
231
|
392 int deep_p, flush_right;
|
|
393 struct gcpro gcpro1;
|
269
|
394 unsigned long checksum;
|
251
|
395 struct gui_item gui_item;
|
|
396
|
|
397 gui_item_init (&gui_item);
|
269
|
398 GCPRO_GUI_ITEM (&gui_item);
|
|
399
|
|
400 /* We are sometimes called with the menubar unchanged, and with changed
|
|
401 right flush. We have to update the menubar in ths case,
|
|
402 so account for the compliance setting in the hash value */
|
|
403 checksum = REPLACE_ME_WITH_GLOBAL_VARIABLE_WHICH_CONTROLS_RIHGT_FLUSH;
|
231
|
404
|
|
405 /* Will initially contain only "(empty)" */
|
233
|
406 if (populate_p)
|
|
407 empty_menu (menu, 1);
|
231
|
408
|
|
409 /* PATH set to nil indicates top-level popup or menubar */
|
|
410 deep_p = !NILP (path);
|
|
411
|
251
|
412 /* Fetch keywords prepending the item list */
|
|
413 desc = menu_parse_submenu_keywords (desc, &gui_item);
|
231
|
414
|
251
|
415 /* Check that menu name is specified when expected */
|
|
416 if (NILP (gui_item.name) && deep_p)
|
|
417 signal_simple_error ("Menu must have a name", desc);
|
231
|
418
|
251
|
419 /* Apply filter if specified */
|
|
420 if (!NILP (gui_item.filter))
|
|
421 desc = call1 (gui_item.filter, desc);
|
231
|
422
|
251
|
423 /* Loop thru the desc's CDR and add items for each entry */
|
231
|
424 flush_right = 0;
|
251
|
425 EXTERNAL_LIST_LOOP (item_desc, desc)
|
231
|
426 {
|
|
427 if (NILP (XCAR (item_desc)))
|
|
428 {
|
269
|
429 /* Do not flush right menubar items when MS style compiant */
|
|
430 if (bar_p && !REPLACE_ME_WITH_GLOBAL_VARIABLE_WHICH_CONTROLS_RIHGT_FLUSH)
|
231
|
431 flush_right = 1;
|
233
|
432 if (!populate_p)
|
278
|
433 checksum = HASH2 (checksum, LISP_HASH (Qnil));
|
231
|
434 }
|
233
|
435 else if (populate_p)
|
231
|
436 populate_menu_add_item (menu, path, hash_tab,
|
|
437 XCAR (item_desc), flush_right);
|
233
|
438 else
|
|
439 checksum = HASH2 (checksum,
|
|
440 checksum_menu_item (XCAR (item_desc)));
|
231
|
441 }
|
|
442
|
233
|
443 if (populate_p)
|
|
444 {
|
|
445 /* Remove the "(empty)" item, if there are other ones */
|
|
446 if (GetMenuItemCount (menu) > 1)
|
|
447 RemoveMenu (menu, EMPTY_ITEM_ID, MF_BYCOMMAND);
|
231
|
448
|
233
|
449 /* Add the header to the popup, if told so. The same as in X - an
|
|
450 insensitive item, and a separator (Seems to me, there were
|
|
451 two separators in X... In Windows this looks ugly, anywats. */
|
251
|
452 if (!bar_p && !deep_p && popup_menu_titles && !NILP(gui_item.name))
|
233
|
453 {
|
251
|
454 CHECK_STRING (gui_item.name);
|
233
|
455 InsertMenu (menu, 0, MF_BYPOSITION | MF_STRING | MF_DISABLED,
|
251
|
456 0, XSTRING_DATA(gui_item.name));
|
233
|
457 InsertMenu (menu, 1, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
|
|
458 SetMenuDefaultItem (menu, 0, MF_BYPOSITION);
|
|
459 }
|
231
|
460 }
|
251
|
461 UNGCPRO; /* gui_item */
|
233
|
462 return checksum;
|
|
463 }
|
|
464
|
|
465 static void
|
251
|
466 populate_menu (HMENU menu, Lisp_Object path, Lisp_Object desc,
|
233
|
467 Lisp_Object hash_tab, int bar_p)
|
|
468 {
|
251
|
469 populate_or_checksum_helper (menu, path, desc, hash_tab, bar_p, 1);
|
233
|
470 }
|
|
471
|
|
472 static unsigned long
|
251
|
473 checksum_menu (Lisp_Object desc)
|
231
|
474 {
|
251
|
475 return populate_or_checksum_helper (NULL, Qnil, desc, Qunbound, 0, 0);
|
231
|
476 }
|
|
477
|
|
478 static void
|
|
479 update_frame_menubar_maybe (struct frame* f)
|
|
480 {
|
|
481 HMENU menubar = GetMenu (FRAME_MSWINDOWS_HANDLE (f));
|
|
482 struct window *w = XWINDOW (FRAME_LAST_NONMINIBUF_WINDOW (f));
|
|
483 Lisp_Object desc = (!NILP (w->menubar_visible_p)
|
|
484 ? symbol_value_in_buffer (Qcurrent_menubar, w->buffer)
|
|
485 : Qnil);
|
|
486
|
251
|
487 top_level_menu = menubar;
|
|
488
|
231
|
489 if (NILP (desc) && menubar != NULL)
|
|
490 {
|
|
491 /* Menubar has gone */
|
|
492 FRAME_MSWINDOWS_MENU_HASHTABLE(f) = Qnil;
|
233
|
493 SetMenu (FRAME_MSWINDOWS_HANDLE (f), NULL);
|
231
|
494 DestroyMenu (menubar);
|
|
495 DrawMenuBar (FRAME_MSWINDOWS_HANDLE (f));
|
|
496 return;
|
|
497 }
|
|
498
|
|
499 if (!NILP (desc) && menubar == NULL)
|
|
500 {
|
|
501 /* Menubar has appeared */
|
|
502 menubar = CreateMenu ();
|
|
503 goto populate;
|
|
504 }
|
|
505
|
|
506 if (NILP (desc))
|
|
507 {
|
|
508 /* We did not have the bar and are not going to */
|
|
509 return;
|
|
510 }
|
|
511
|
233
|
512 /* Now we bail out if the menubar has not changed */
|
|
513 if (FRAME_MSWINDOWS_MENU_CHECKSUM(f) == checksum_menu (desc))
|
|
514 return;
|
231
|
515
|
|
516 populate:
|
|
517 /* Come with empty hash table */
|
|
518 if (NILP (FRAME_MSWINDOWS_MENU_HASHTABLE(f)))
|
|
519 FRAME_MSWINDOWS_MENU_HASHTABLE(f) = Fmake_hashtable (make_int (50), Qequal);
|
|
520 else
|
|
521 Fclrhash (FRAME_MSWINDOWS_MENU_HASHTABLE(f));
|
|
522
|
|
523 Fputhash (hmenu_to_lisp_object (menubar), Qnil,
|
|
524 FRAME_MSWINDOWS_MENU_HASHTABLE(f));
|
|
525 populate_menu (menubar, Qnil, desc,
|
|
526 FRAME_MSWINDOWS_MENU_HASHTABLE(f), 1);
|
|
527 SetMenu (FRAME_MSWINDOWS_HANDLE (f), menubar);
|
|
528 DrawMenuBar (FRAME_MSWINDOWS_HANDLE (f));
|
233
|
529
|
|
530 FRAME_MSWINDOWS_MENU_CHECKSUM(f) = checksum_menu (desc);
|
231
|
531 }
|
|
532
|
|
533 static void
|
|
534 prune_menubar (struct frame *f)
|
|
535 {
|
|
536 HMENU menubar = GetMenu (FRAME_MSWINDOWS_HANDLE (f));
|
|
537 Lisp_Object desc = current_frame_menubar (f);
|
|
538 if (menubar == NULL)
|
|
539 return;
|
|
540
|
|
541 /* #### If a filter function has set desc to Qnil, this abort()
|
233
|
542 triggers. To resolve, we must prevent filters explicitely from
|
|
543 mangling with the active menu. In apply_filter probably?
|
231
|
544 Is copy-tree on the whole menu too expensive? */
|
|
545 if (NILP(desc))
|
|
546 /* abort(); */
|
|
547 return;
|
|
548
|
|
549 /* We do the trick by removing all items and re-populating top level */
|
|
550 empty_menu (menubar, 0);
|
|
551
|
|
552 assert (HASHTABLEP (FRAME_MSWINDOWS_MENU_HASHTABLE(f)));
|
|
553 Fclrhash (FRAME_MSWINDOWS_MENU_HASHTABLE(f));
|
|
554
|
|
555 Fputhash (hmenu_to_lisp_object (menubar), Qnil,
|
|
556 FRAME_MSWINDOWS_MENU_HASHTABLE(f));
|
|
557 populate_menu (menubar, Qnil, desc,
|
|
558 FRAME_MSWINDOWS_MENU_HASHTABLE(f), 1);
|
|
559 }
|
|
560
|
|
561 /*
|
|
562 * This is called when cleanup is possible. It is better not to
|
|
563 * clean things up at all than do it too earaly!
|
|
564 */
|
|
565 static void
|
|
566 menu_cleanup (struct frame *f)
|
|
567 {
|
|
568 /* This function can GC */
|
233
|
569 current_menudesc = Qnil;
|
|
570 current_hashtable = Qnil;
|
|
571 prune_menubar (f);
|
231
|
572 }
|
|
573
|
|
574
|
|
575 /*------------------------------------------------------------------------*/
|
|
576 /* Message handlers */
|
|
577 /*------------------------------------------------------------------------*/
|
|
578 static Lisp_Object
|
|
579 unsafe_handle_wm_initmenupopup_1 (HMENU menu, struct frame* f)
|
|
580 {
|
|
581 /* This function can call lisp, beat dogs and stick chewing gum to
|
|
582 everything! */
|
|
583
|
233
|
584 Lisp_Object path, desc;
|
231
|
585 struct gcpro gcpro1;
|
|
586
|
|
587 /* Find which guy is going to explode */
|
233
|
588 path = Fgethash (hmenu_to_lisp_object (menu), current_hashtable, Qunbound);
|
231
|
589 assert (!UNBOUNDP (path));
|
233
|
590 #ifdef DEBUG_XEMACS
|
|
591 /* Allow to continue in a debugger after assert - not so fatal */
|
|
592 if (UNBOUNDP (path))
|
|
593 error ("internal menu error");
|
|
594 #endif
|
231
|
595
|
|
596 /* Now find a desc chunk for it. If none, then probably menu open
|
|
597 hook has played too much games around stuff */
|
251
|
598 desc = Fmenu_find_real_submenu (current_menudesc, path);
|
|
599 if (NILP (desc))
|
|
600 signal_simple_error ("This menu does not exist any more", path);
|
231
|
601
|
|
602 /* Now, stuff it */
|
|
603 /* DESC may be generated by filter, so we have to gcpro it */
|
|
604 GCPRO1 (desc);
|
233
|
605 populate_menu (menu, path, desc, current_hashtable, 0);
|
231
|
606 UNGCPRO;
|
|
607 return Qt;
|
|
608 }
|
|
609
|
|
610 static Lisp_Object
|
|
611 unsafe_handle_wm_initmenu_1 (struct frame* f)
|
|
612 {
|
|
613 /* This function can call lisp */
|
251
|
614
|
|
615 /* NOTE: This is called for the bar only, WM_INITMENU
|
|
616 for popups is filtered out */
|
|
617
|
231
|
618 /* #### - this menubar update mechanism is expensively anti-social and
|
|
619 the activate-menubar-hook is now mostly obsolete. */
|
|
620
|
|
621 /* We simply ignore return value. In any case, we construct the bar
|
|
622 on the fly */
|
288
|
623 run_hook (Qactivate_menubar_hook);
|
233
|
624
|
231
|
625 update_frame_menubar_maybe (f);
|
233
|
626
|
|
627 current_menudesc = current_frame_menubar (f);
|
|
628 current_hashtable = FRAME_MSWINDOWS_MENU_HASHTABLE(f);
|
|
629 assert (HASHTABLEP (current_hashtable));
|
|
630
|
231
|
631 return Qt;
|
|
632 }
|
|
633
|
|
634 /*
|
|
635 * Return value is Qt if we have dispatched the command,
|
|
636 * or Qnil if id has not been mapped to a callback.
|
|
637 * Window procedure may try other targets to route the
|
|
638 * command if we return nil
|
|
639 */
|
|
640 Lisp_Object
|
|
641 mswindows_handle_wm_command (struct frame* f, WORD id)
|
|
642 {
|
|
643 /* Try to map the command id through the proper hash table */
|
286
|
644 Lisp_Object data, fn, arg, frame;
|
231
|
645 struct gcpro gcpro1;
|
|
646
|
286
|
647 data = Fgethash (make_int (id), current_hashtable, Qunbound);
|
|
648 if (UNBOUNDP (data))
|
231
|
649 {
|
|
650 menu_cleanup (f);
|
|
651 return Qnil;
|
|
652 }
|
|
653
|
286
|
654 /* Need to gcpro because the hashtable may get destroyed by
|
|
655 menu_cleanup(), and will not gcpro the data any more */
|
|
656 GCPRO1 (data);
|
231
|
657 menu_cleanup (f);
|
|
658
|
|
659 /* Ok, this is our one. Enqueue it. */
|
288
|
660 get_gui_callback (data, &fn, &arg);
|
231
|
661 XSETFRAME (frame, f);
|
298
|
662 /* this used to call mswindows_enqueue_misc_user_event but that
|
|
663 breaks customize because the misc_event gets eval'ed in some
|
|
664 cicumstances. Don't change it back unless you can fix the
|
|
665 customize problem also.*/
|
|
666 enqueue_misc_user_event (frame, fn, arg);
|
|
667 mswindows_enqueue_magic_event (NULL, XM_BUMPQUEUE);
|
276
|
668
|
286
|
669 UNGCPRO; /* data */
|
231
|
670 return Qt;
|
|
671 }
|
|
672
|
|
673
|
|
674 /*------------------------------------------------------------------------*/
|
|
675 /* Message handling proxies */
|
|
676 /*------------------------------------------------------------------------*/
|
|
677
|
|
678 static HMENU wm_initmenu_menu;
|
|
679 static struct frame* wm_initmenu_frame;
|
|
680
|
|
681 static Lisp_Object
|
|
682 unsafe_handle_wm_initmenupopup (Lisp_Object u_n_u_s_e_d)
|
|
683 {
|
|
684 return unsafe_handle_wm_initmenupopup_1 (wm_initmenu_menu, wm_initmenu_frame);
|
|
685 }
|
|
686
|
|
687 static Lisp_Object
|
|
688 unsafe_handle_wm_initmenu (Lisp_Object u_n_u_s_e_d)
|
|
689 {
|
|
690 return unsafe_handle_wm_initmenu_1 (wm_initmenu_frame);
|
|
691 }
|
|
692
|
|
693 Lisp_Object
|
|
694 mswindows_handle_wm_initmenupopup (HMENU hmenu, struct frame* frm)
|
|
695 {
|
|
696 /* We cannot pass hmenu as a lisp object. Use static var */
|
|
697 wm_initmenu_menu = hmenu;
|
|
698 wm_initmenu_frame = frm;
|
|
699 return mswindows_protect_modal_loop (unsafe_handle_wm_initmenupopup, Qnil);
|
|
700 }
|
|
701
|
|
702 Lisp_Object
|
233
|
703 mswindows_handle_wm_initmenu (HMENU hmenu, struct frame* f)
|
231
|
704 {
|
233
|
705 /* Handle only frame menubar, ignore if from popup or system menu */
|
|
706 if (GetMenu (FRAME_MSWINDOWS_HANDLE(f)) == hmenu)
|
|
707 {
|
|
708 wm_initmenu_frame = f;
|
|
709 return mswindows_protect_modal_loop (unsafe_handle_wm_initmenu, Qnil);
|
|
710 }
|
|
711 return Qt;
|
231
|
712 }
|
|
713
|
|
714
|
|
715 /*------------------------------------------------------------------------*/
|
|
716 /* Methods */
|
|
717 /*------------------------------------------------------------------------*/
|
|
718
|
|
719 static void
|
|
720 mswindows_update_frame_menubars (struct frame* f)
|
|
721 {
|
269
|
722 update_frame_menubar_maybe (f);
|
231
|
723 }
|
|
724
|
|
725 static void
|
|
726 mswindows_free_frame_menubars (struct frame* f)
|
|
727 {
|
|
728 FRAME_MSWINDOWS_MENU_HASHTABLE(f) = Qnil;
|
|
729 }
|
|
730
|
|
731 static void
|
|
732 mswindows_popup_menu (Lisp_Object menu_desc, Lisp_Object event)
|
|
733 {
|
|
734 struct frame *f = selected_frame ();
|
|
735 struct Lisp_Event *eev = NULL;
|
|
736 HMENU menu;
|
|
737 POINT pt;
|
|
738 int ok;
|
|
739
|
|
740 if (!NILP (event))
|
|
741 {
|
|
742 CHECK_LIVE_EVENT (event);
|
|
743 eev = XEVENT (event);
|
|
744 if (eev->event_type != button_press_event
|
|
745 && eev->event_type != button_release_event)
|
|
746 wrong_type_argument (Qmouse_event_p, event);
|
|
747 }
|
|
748 else if (!NILP (Vthis_command_keys))
|
|
749 {
|
|
750 /* if an event wasn't passed, use the last event of the event sequence
|
|
751 currently being executed, if that event is a mouse event */
|
|
752 eev = XEVENT (Vthis_command_keys); /* last event first */
|
|
753 if (eev->event_type != button_press_event
|
|
754 && eev->event_type != button_release_event)
|
|
755 eev = NULL;
|
|
756 }
|
|
757
|
|
758 /* Default is to put the menu at the point (10, 10) in frame */
|
|
759 if (eev)
|
|
760 {
|
|
761 pt.x = eev->event.button.x;
|
|
762 pt.y = eev->event.button.y;
|
|
763 ClientToScreen (FRAME_MSWINDOWS_HANDLE (f), &pt);
|
|
764 }
|
|
765 else
|
|
766 pt.x = pt.y = 10;
|
|
767
|
|
768 if (SYMBOLP (menu_desc))
|
|
769 menu_desc = Fsymbol_value (menu_desc);
|
298
|
770 CHECK_CONS (menu_desc);
|
|
771 CHECK_STRING (XCAR (menu_desc));
|
231
|
772
|
233
|
773 current_menudesc = menu_desc;
|
|
774 current_hashtable = Fmake_hashtable (make_int(10), Qequal);
|
231
|
775 menu = create_empty_popup_menu();
|
233
|
776 Fputhash (hmenu_to_lisp_object (menu), Qnil, current_hashtable);
|
251
|
777 top_level_menu = menu;
|
231
|
778
|
298
|
779 /* see comments in menubar-x.c */
|
|
780 if (zmacs_regions)
|
|
781 zmacs_region_stays = 1;
|
|
782
|
233
|
783 ok = TrackPopupMenu (menu,
|
|
784 TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
|
231
|
785 pt.x, pt.y, 0,
|
|
786 FRAME_MSWINDOWS_HANDLE (f), NULL);
|
|
787
|
|
788 DestroyMenu (menu);
|
|
789
|
|
790 /* Signal a signal if caught by Track...() modal loop */
|
|
791 mswindows_unmodalize_signal_maybe ();
|
|
792
|
|
793 /* This is probably the only real reason for failure */
|
|
794 if (!ok) {
|
|
795 menu_cleanup (f);
|
233
|
796 signal_simple_error ("Cannot track popup menu while in menu",
|
231
|
797 menu_desc);
|
|
798 }
|
|
799 }
|
|
800
|
|
801
|
|
802 /*------------------------------------------------------------------------*/
|
|
803 /* Initialization */
|
|
804 /*------------------------------------------------------------------------*/
|
|
805 void
|
|
806 syms_of_menubar_mswindows (void)
|
|
807 {
|
|
808 }
|
|
809
|
|
810 void
|
|
811 console_type_create_menubar_mswindows (void)
|
|
812 {
|
|
813 CONSOLE_HAS_METHOD (mswindows, update_frame_menubars);
|
|
814 CONSOLE_HAS_METHOD (mswindows, free_frame_menubars);
|
|
815 CONSOLE_HAS_METHOD (mswindows, popup_menu);
|
|
816 }
|
|
817
|
|
818 void
|
|
819 vars_of_menubar_mswindows (void)
|
|
820 {
|
233
|
821 current_menudesc = Qnil;
|
|
822 current_hashtable = Qnil;
|
231
|
823
|
233
|
824 staticpro (¤t_menudesc);
|
|
825 staticpro (¤t_hashtable);
|
253
|
826 }
|