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"
|
|
80
|
|
81 #include "buffer.h"
|
|
82 #include "commands.h"
|
|
83 #include "console-msw.h"
|
|
84 #include "emacsfns.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
|
|
95 #define EMPTY_ITEM_ID ((UINT)LISP_TO_VOID (Qunbound))
|
|
96 #define EMPTY_ITEM_NAME "(empty)"
|
|
97
|
233
|
98 /* Current menu (bar or popup) descriptor. gcpro'ed */
|
|
99 static Lisp_Object current_menudesc;
|
231
|
100
|
233
|
101 /* Current menubar or popup hashtable. gcpro'ed */
|
|
102 static Lisp_Object current_hashtable;
|
231
|
103
|
|
104 /* This is used to allocate unique ids to menu items.
|
|
105 Items ids are in MENU_ITEM_ID_MIN to MENU_ITEM_ID_MAX.
|
|
106 Allocation checks that the item is not already in
|
|
107 the TOP_LEVEL_MENU */
|
251
|
108
|
|
109 /* #### defines go to gui-msw.h, as the range is shared with toolbars
|
|
110 (If only toolbars will be implemented as common controls) */
|
231
|
111 #define MENU_ITEM_ID_MIN 0x8000
|
|
112 #define MENU_ITEM_ID_MAX 0xFFFF
|
|
113 #define MENU_ITEM_ID_BITS(x) ((x) & 0x7FFF | 0x8000)
|
|
114 static HMENU top_level_menu;
|
|
115
|
251
|
116 #define MAX_MENUITEM_LENGTH 128
|
231
|
117
|
|
118 /*
|
|
119 * This returns Windows-style menu item string:
|
|
120 * "Left Flush\tRight Flush"
|
|
121 */
|
251
|
122 static char*
|
|
123 displayable_menu_item (struct gui_item* pgui_item)
|
231
|
124 {
|
|
125 /* We construct the name in a static buffer. That's fine, beause
|
|
126 menu items longer than 128 chars are probably programming errors,
|
|
127 and better be caught than displayed! */
|
|
128
|
251
|
129 static char buf[MAX_MENUITEM_LENGTH+2];
|
|
130 unsigned int ll, lr;
|
231
|
131
|
251
|
132 /* Left flush part of the string */
|
|
133 ll = gui_item_display_flush_left (pgui_item, buf, MAX_MENUITEM_LENGTH);
|
231
|
134
|
251
|
135 /* Right flush part */
|
|
136 assert (MAX_MENUITEM_LENGTH > ll + 1);
|
|
137 lr = gui_item_display_flush_right (pgui_item, buf + ll + 1,
|
|
138 MAX_MENUITEM_LENGTH - ll - 1);
|
|
139 if (lr)
|
|
140 buf [ll] = '\t';
|
231
|
141
|
|
142 return buf;
|
|
143 }
|
|
144
|
|
145 /*
|
|
146 * hmenu_to_lisp_object() returns an opaque ptr given menu handle.
|
|
147 */
|
|
148 static Lisp_Object
|
|
149 hmenu_to_lisp_object (HMENU hmenu)
|
|
150 {
|
|
151 return make_opaque_ptr (hmenu);
|
|
152 }
|
|
153
|
|
154 /*
|
|
155 * Allocation tries a hash based on item's path and name first. This
|
|
156 * almost guarantees that the same item will override its old value in
|
|
157 * the hashtable rather than abandon it.
|
|
158 */
|
|
159 static Lisp_Object
|
251
|
160 allocate_menu_item_id (Lisp_Object path, Lisp_Object name, Lisp_Object suffix)
|
231
|
161 {
|
251
|
162 UINT id = MENU_ITEM_ID_BITS (HASH3 (internal_hash (path, 0),
|
|
163 internal_hash (name, 0),
|
|
164 internal_hash (suffix, 0)));
|
231
|
165 do {
|
|
166 id = MENU_ITEM_ID_BITS (id + 1);
|
|
167 } while (GetMenuState (top_level_menu, id, MF_BYCOMMAND) != 0xFFFFFFFF);
|
|
168 return make_int (id);
|
|
169 }
|
|
170
|
|
171 static HMENU
|
|
172 create_empty_popup_menu (void)
|
|
173 {
|
251
|
174 return CreatePopupMenu ();
|
231
|
175 }
|
|
176
|
|
177 static void
|
|
178 empty_menu (HMENU menu, int add_empty_p)
|
|
179 {
|
|
180 while (DeleteMenu (menu, 0, MF_BYPOSITION));
|
|
181 if (add_empty_p)
|
|
182 AppendMenu (menu, MF_STRING | MF_GRAYED, EMPTY_ITEM_ID, EMPTY_ITEM_NAME);
|
|
183 }
|
|
184
|
233
|
185 /*
|
|
186 * The idea of checksumming is that we must hash minimal object
|
|
187 * which is neccessarily changes when the item changes. For separator
|
|
188 * this is a constant, for grey strings and submenus these are hashes
|
|
189 * of names, since sumbenus are unpopulated until opened so always
|
|
190 * equal otherwise. For items, this is a full hash value of a callback,
|
|
191 * because a callback may me a form which can be changed only somewhere
|
|
192 * in depth.
|
|
193 */
|
|
194 static unsigned long
|
|
195 checksum_menu_item (Lisp_Object item)
|
|
196 {
|
|
197 if (STRINGP (item))
|
|
198 {
|
|
199 /* Separator or unselectable text - hash as a string + 13 */
|
|
200 if (separator_string_p (XSTRING_DATA (item)))
|
|
201 return 13;
|
|
202 else
|
|
203 return internal_hash (item, 0) + 13;
|
|
204 }
|
|
205 else if (CONSP (item))
|
|
206 {
|
|
207 /* Submenu - hash by its string name + 0 */
|
|
208 return internal_hash (XCAR(item), 0);
|
|
209 }
|
|
210 else if (VECTORP (item))
|
|
211 {
|
|
212 /* An ordinary item - hash its name and callback form. */
|
251
|
213 return HASH2 (internal_hash (XVECTOR_DATA(item)[0], 0),
|
|
214 internal_hash (XVECTOR_DATA(item)[1], 0));
|
233
|
215 }
|
|
216
|
|
217 /* An error - will be caught later */
|
|
218 return 0;
|
|
219 }
|
|
220
|
231
|
221 static void
|
|
222 populate_menu_add_item (HMENU menu, Lisp_Object path,
|
|
223 Lisp_Object hash_tab, Lisp_Object item, int flush_right)
|
|
224 {
|
|
225 MENUITEMINFO item_info;
|
|
226
|
|
227 item_info.cbSize = sizeof (item_info);
|
|
228 item_info.fMask = MIIM_TYPE | MIIM_STATE | MIIM_ID;
|
|
229 item_info.fState = 0;
|
|
230 item_info.wID = 0;
|
|
231 item_info.fType = 0;
|
|
232
|
|
233 if (STRINGP (item))
|
|
234 {
|
|
235 /* Separator or unselectable text */
|
|
236 if (separator_string_p (XSTRING_DATA (item)))
|
|
237 item_info.fType = MFT_SEPARATOR;
|
|
238 else
|
|
239 {
|
|
240 item_info.fType = MFT_STRING;
|
|
241 item_info.fState = MFS_DISABLED;
|
|
242 item_info.dwTypeData = XSTRING_DATA (item);
|
|
243 }
|
|
244 }
|
|
245 else if (CONSP (item))
|
|
246 {
|
|
247 /* Submenu */
|
|
248 HMENU submenu;
|
251
|
249 struct gui_item gui_item;
|
|
250 struct gcpro gcpro1;
|
|
251
|
|
252 gui_item_init (&gui_item);
|
|
253 GCPRO1 (gui_item);
|
|
254 gcpro1.nvars = GUI_ITEM_GCPRO_COUNT;
|
|
255
|
|
256 menu_parse_submenu_keywords (item, &gui_item);
|
|
257
|
|
258 if (!STRINGP (gui_item.name))
|
233
|
259 signal_simple_error ("Menu name (first element) must be a string", item);
|
231
|
260
|
251
|
261 if (!gui_item_included_p (&gui_item, Vmenubar_configuration))
|
231
|
262 return;
|
|
263
|
251
|
264 if (!gui_item_active_p (&gui_item))
|
231
|
265 item_info.fState = MFS_GRAYED;
|
|
266 /* Temptation is to put 'else' right here. Although, the
|
|
267 displayed item won't have an arrow indicating that it is a
|
|
268 popup. So we go ahead a little bit more and create a popup */
|
|
269 submenu = create_empty_popup_menu();
|
|
270
|
|
271 item_info.fMask |= MIIM_SUBMENU;
|
251
|
272 item_info.dwTypeData = displayable_menu_item (&gui_item);
|
231
|
273 item_info.hSubMenu = submenu;
|
|
274
|
|
275 if (!(item_info.fState & MFS_GRAYED))
|
|
276 {
|
|
277 /* Now add the full submenu path as a value to the hash table,
|
|
278 keyed by menu handle */
|
|
279 if (NILP(path))
|
251
|
280 /* list1 cannot GC */
|
|
281 path = list1 (gui_item.name);
|
|
282 else
|
|
283 {
|
|
284 Lisp_Object arg[2] = { path, list1 (gui_item.name) };
|
|
285 /* Fappend gcpro'es its arg */
|
|
286 path = Fappend (2, arg);
|
|
287 }
|
231
|
288
|
251
|
289 /* Fputhash GCPRO'es PATH */
|
231
|
290 Fputhash (hmenu_to_lisp_object (submenu), path, hash_tab);
|
|
291 }
|
251
|
292 UNGCPRO; /* gui_item */
|
231
|
293 }
|
|
294 else if (VECTORP (item))
|
|
295 {
|
|
296 /* An ordinary item */
|
251
|
297 Lisp_Object style, id;
|
|
298 struct gui_item gui_item;
|
|
299 struct gcpro gcpro1;
|
231
|
300
|
251
|
301 gui_item_init (&gui_item);
|
|
302 GCPRO1 (gui_item);
|
|
303 gcpro1.nvars = GUI_ITEM_GCPRO_COUNT;
|
|
304
|
|
305 gui_parse_item_keywords (item, &gui_item);
|
|
306
|
|
307 if (!gui_item_included_p (&gui_item, Vmenubar_configuration))
|
231
|
308 return;
|
|
309
|
251
|
310 if (!gui_item_active_p (&gui_item))
|
|
311 item_info.fState = MFS_GRAYED;
|
231
|
312
|
251
|
313 style = (NILP (gui_item.selected) || NILP (Feval (gui_item.selected))
|
|
314 ? Qnil : gui_item.style);
|
|
315
|
231
|
316 if (EQ (style, Qradio))
|
|
317 {
|
|
318 item_info.fType |= MFT_RADIOCHECK;
|
|
319 item_info.fState |= MFS_CHECKED;
|
|
320 }
|
|
321 else if (EQ (style, Qtoggle))
|
|
322 {
|
|
323 item_info.fState |= MFS_CHECKED;
|
|
324 }
|
|
325
|
251
|
326 id = allocate_menu_item_id (path, gui_item.name,
|
|
327 gui_item.suffix);
|
|
328 Fputhash (id, gui_item.callback, hash_tab);
|
231
|
329
|
|
330 item_info.wID = (UINT) XINT(id);
|
|
331 item_info.fType |= MFT_STRING;
|
251
|
332 item_info.dwTypeData = displayable_menu_item (&gui_item);
|
|
333
|
|
334 UNGCPRO; /* gui_item */
|
231
|
335 }
|
|
336 else
|
|
337 {
|
251
|
338 signal_simple_error ("Mailformed menu item descriptor", item);
|
231
|
339 }
|
|
340
|
|
341 if (flush_right)
|
|
342 item_info.fType |= MFT_RIGHTJUSTIFY;
|
|
343
|
|
344 InsertMenuItem (menu, UINT_MAX, TRUE, &item_info);
|
|
345 }
|
|
346
|
233
|
347 /*
|
|
348 * This function is called from populate_menu and checksum_menu.
|
|
349 * When called to populate, MENU is a menu handle, PATH is a
|
|
350 * list of strings representing menu path from root to this submenu,
|
|
351 * DESCRIPTOR is a menu descriptor, HASH_TAB is a hashtable associated
|
|
352 * with root menu, BAR_P indicates whether this called for a menubar or
|
|
353 * a popup, and POPULATE_P is non-zero. Return value must be ignored.
|
|
354 * When called to checksum, DESCRIPTOR has the same meaning, POPULATE_P
|
|
355 * is zero, PATH must be Qnil, and the rest of parameters is ignored.
|
|
356 * Return value is the menu checksum.
|
|
357 */
|
|
358 static unsigned long
|
251
|
359 populate_or_checksum_helper (HMENU menu, Lisp_Object path, Lisp_Object desc,
|
233
|
360 Lisp_Object hash_tab, int bar_p, int populate_p)
|
231
|
361 {
|
251
|
362 Lisp_Object item_desc;
|
231
|
363 int deep_p, flush_right;
|
|
364 struct gcpro gcpro1;
|
233
|
365 unsigned long checksum = 0;
|
251
|
366 struct gui_item gui_item;
|
|
367
|
|
368 gui_item_init (&gui_item);
|
|
369 GCPRO1 (gui_item);
|
|
370 gcpro1.nvars = GUI_ITEM_GCPRO_COUNT;
|
231
|
371
|
|
372 /* Will initially contain only "(empty)" */
|
233
|
373 if (populate_p)
|
|
374 empty_menu (menu, 1);
|
231
|
375
|
|
376 /* PATH set to nil indicates top-level popup or menubar */
|
|
377 deep_p = !NILP (path);
|
|
378
|
251
|
379 /* Fetch keywords prepending the item list */
|
|
380 desc = menu_parse_submenu_keywords (desc, &gui_item);
|
231
|
381
|
251
|
382 /* Check that menu name is specified when expected */
|
|
383 if (NILP (gui_item.name) && deep_p)
|
|
384 signal_simple_error ("Menu must have a name", desc);
|
231
|
385
|
251
|
386 /* Apply filter if specified */
|
|
387 if (!NILP (gui_item.filter))
|
|
388 desc = call1 (gui_item.filter, desc);
|
231
|
389
|
251
|
390 /* Loop thru the desc's CDR and add items for each entry */
|
231
|
391 flush_right = 0;
|
251
|
392 EXTERNAL_LIST_LOOP (item_desc, desc)
|
231
|
393 {
|
|
394 if (NILP (XCAR (item_desc)))
|
|
395 {
|
|
396 if (bar_p)
|
|
397 flush_right = 1;
|
233
|
398 if (!populate_p)
|
|
399 checksum = HASH2 (checksum, Qnil);
|
231
|
400 }
|
233
|
401 else if (populate_p)
|
231
|
402 populate_menu_add_item (menu, path, hash_tab,
|
|
403 XCAR (item_desc), flush_right);
|
233
|
404 else
|
|
405 checksum = HASH2 (checksum,
|
|
406 checksum_menu_item (XCAR (item_desc)));
|
231
|
407 }
|
|
408
|
233
|
409 if (populate_p)
|
|
410 {
|
|
411 /* Remove the "(empty)" item, if there are other ones */
|
|
412 if (GetMenuItemCount (menu) > 1)
|
|
413 RemoveMenu (menu, EMPTY_ITEM_ID, MF_BYCOMMAND);
|
231
|
414
|
233
|
415 /* Add the header to the popup, if told so. The same as in X - an
|
|
416 insensitive item, and a separator (Seems to me, there were
|
|
417 two separators in X... In Windows this looks ugly, anywats. */
|
251
|
418 if (!bar_p && !deep_p && popup_menu_titles && !NILP(gui_item.name))
|
233
|
419 {
|
251
|
420 CHECK_STRING (gui_item.name);
|
233
|
421 InsertMenu (menu, 0, MF_BYPOSITION | MF_STRING | MF_DISABLED,
|
251
|
422 0, XSTRING_DATA(gui_item.name));
|
233
|
423 InsertMenu (menu, 1, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
|
|
424 SetMenuDefaultItem (menu, 0, MF_BYPOSITION);
|
|
425 }
|
231
|
426 }
|
251
|
427 UNGCPRO; /* gui_item */
|
233
|
428 return checksum;
|
|
429 }
|
|
430
|
|
431 static void
|
251
|
432 populate_menu (HMENU menu, Lisp_Object path, Lisp_Object desc,
|
233
|
433 Lisp_Object hash_tab, int bar_p)
|
|
434 {
|
251
|
435 populate_or_checksum_helper (menu, path, desc, hash_tab, bar_p, 1);
|
233
|
436 }
|
|
437
|
|
438 static unsigned long
|
251
|
439 checksum_menu (Lisp_Object desc)
|
231
|
440 {
|
251
|
441 return populate_or_checksum_helper (NULL, Qnil, desc, Qunbound, 0, 0);
|
231
|
442 }
|
|
443
|
|
444 static void
|
|
445 update_frame_menubar_maybe (struct frame* f)
|
|
446 {
|
|
447 HMENU menubar = GetMenu (FRAME_MSWINDOWS_HANDLE (f));
|
|
448 struct window *w = XWINDOW (FRAME_LAST_NONMINIBUF_WINDOW (f));
|
|
449 Lisp_Object desc = (!NILP (w->menubar_visible_p)
|
|
450 ? symbol_value_in_buffer (Qcurrent_menubar, w->buffer)
|
|
451 : Qnil);
|
|
452
|
251
|
453 top_level_menu = menubar;
|
|
454
|
231
|
455 if (NILP (desc) && menubar != NULL)
|
|
456 {
|
|
457 /* Menubar has gone */
|
|
458 FRAME_MSWINDOWS_MENU_HASHTABLE(f) = Qnil;
|
233
|
459 SetMenu (FRAME_MSWINDOWS_HANDLE (f), NULL);
|
231
|
460 DestroyMenu (menubar);
|
|
461 DrawMenuBar (FRAME_MSWINDOWS_HANDLE (f));
|
|
462 return;
|
|
463 }
|
|
464
|
|
465 if (!NILP (desc) && menubar == NULL)
|
|
466 {
|
|
467 /* Menubar has appeared */
|
|
468 menubar = CreateMenu ();
|
|
469 goto populate;
|
|
470 }
|
|
471
|
|
472 if (NILP (desc))
|
|
473 {
|
|
474 /* We did not have the bar and are not going to */
|
|
475 return;
|
|
476 }
|
|
477
|
233
|
478 /* Now we bail out if the menubar has not changed */
|
|
479 if (FRAME_MSWINDOWS_MENU_CHECKSUM(f) == checksum_menu (desc))
|
|
480 return;
|
231
|
481
|
|
482 populate:
|
|
483 /* Come with empty hash table */
|
|
484 if (NILP (FRAME_MSWINDOWS_MENU_HASHTABLE(f)))
|
|
485 FRAME_MSWINDOWS_MENU_HASHTABLE(f) = Fmake_hashtable (make_int (50), Qequal);
|
|
486 else
|
|
487 Fclrhash (FRAME_MSWINDOWS_MENU_HASHTABLE(f));
|
|
488
|
|
489 Fputhash (hmenu_to_lisp_object (menubar), Qnil,
|
|
490 FRAME_MSWINDOWS_MENU_HASHTABLE(f));
|
|
491 populate_menu (menubar, Qnil, desc,
|
|
492 FRAME_MSWINDOWS_MENU_HASHTABLE(f), 1);
|
|
493 SetMenu (FRAME_MSWINDOWS_HANDLE (f), menubar);
|
|
494 DrawMenuBar (FRAME_MSWINDOWS_HANDLE (f));
|
233
|
495
|
|
496 FRAME_MSWINDOWS_MENU_CHECKSUM(f) = checksum_menu (desc);
|
231
|
497 }
|
|
498
|
|
499 static void
|
|
500 prune_menubar (struct frame *f)
|
|
501 {
|
|
502 HMENU menubar = GetMenu (FRAME_MSWINDOWS_HANDLE (f));
|
|
503 Lisp_Object desc = current_frame_menubar (f);
|
|
504 if (menubar == NULL)
|
|
505 return;
|
|
506
|
|
507 /* #### If a filter function has set desc to Qnil, this abort()
|
233
|
508 triggers. To resolve, we must prevent filters explicitely from
|
|
509 mangling with the active menu. In apply_filter probably?
|
231
|
510 Is copy-tree on the whole menu too expensive? */
|
|
511 if (NILP(desc))
|
|
512 /* abort(); */
|
|
513 return;
|
|
514
|
|
515 /* We do the trick by removing all items and re-populating top level */
|
|
516 empty_menu (menubar, 0);
|
|
517
|
|
518 assert (HASHTABLEP (FRAME_MSWINDOWS_MENU_HASHTABLE(f)));
|
|
519 Fclrhash (FRAME_MSWINDOWS_MENU_HASHTABLE(f));
|
|
520
|
|
521 Fputhash (hmenu_to_lisp_object (menubar), Qnil,
|
|
522 FRAME_MSWINDOWS_MENU_HASHTABLE(f));
|
|
523 populate_menu (menubar, Qnil, desc,
|
|
524 FRAME_MSWINDOWS_MENU_HASHTABLE(f), 1);
|
|
525 }
|
|
526
|
|
527 /*
|
|
528 * This is called when cleanup is possible. It is better not to
|
|
529 * clean things up at all than do it too earaly!
|
|
530 */
|
|
531 static void
|
|
532 menu_cleanup (struct frame *f)
|
|
533 {
|
|
534 /* This function can GC */
|
233
|
535 current_menudesc = Qnil;
|
|
536 current_hashtable = Qnil;
|
|
537 prune_menubar (f);
|
231
|
538 }
|
|
539
|
|
540
|
|
541 /*------------------------------------------------------------------------*/
|
|
542 /* Message handlers */
|
|
543 /*------------------------------------------------------------------------*/
|
|
544 static Lisp_Object
|
|
545 unsafe_handle_wm_initmenupopup_1 (HMENU menu, struct frame* f)
|
|
546 {
|
|
547 /* This function can call lisp, beat dogs and stick chewing gum to
|
|
548 everything! */
|
|
549
|
233
|
550 Lisp_Object path, desc;
|
231
|
551 struct gcpro gcpro1;
|
|
552
|
|
553 /* Find which guy is going to explode */
|
233
|
554 path = Fgethash (hmenu_to_lisp_object (menu), current_hashtable, Qunbound);
|
231
|
555 assert (!UNBOUNDP (path));
|
233
|
556 #ifdef DEBUG_XEMACS
|
|
557 /* Allow to continue in a debugger after assert - not so fatal */
|
|
558 if (UNBOUNDP (path))
|
|
559 error ("internal menu error");
|
|
560 #endif
|
231
|
561
|
|
562 /* Now find a desc chunk for it. If none, then probably menu open
|
|
563 hook has played too much games around stuff */
|
251
|
564 desc = Fmenu_find_real_submenu (current_menudesc, path);
|
|
565 if (NILP (desc))
|
|
566 signal_simple_error ("This menu does not exist any more", path);
|
231
|
567
|
|
568 /* Now, stuff it */
|
|
569 /* DESC may be generated by filter, so we have to gcpro it */
|
|
570 GCPRO1 (desc);
|
233
|
571 populate_menu (menu, path, desc, current_hashtable, 0);
|
231
|
572 UNGCPRO;
|
|
573 return Qt;
|
|
574 }
|
|
575
|
|
576 static Lisp_Object
|
|
577 unsafe_handle_wm_initmenu_1 (struct frame* f)
|
|
578 {
|
|
579 /* This function can call lisp */
|
251
|
580
|
|
581 /* NOTE: This is called for the bar only, WM_INITMENU
|
|
582 for popups is filtered out */
|
|
583
|
231
|
584 /* #### - this menubar update mechanism is expensively anti-social and
|
|
585 the activate-menubar-hook is now mostly obsolete. */
|
|
586
|
|
587 /* We simply ignore return value. In any case, we construct the bar
|
|
588 on the fly */
|
|
589 run_hook (Vactivate_menubar_hook);
|
233
|
590
|
231
|
591 update_frame_menubar_maybe (f);
|
233
|
592
|
|
593 current_menudesc = current_frame_menubar (f);
|
|
594 current_hashtable = FRAME_MSWINDOWS_MENU_HASHTABLE(f);
|
|
595 assert (HASHTABLEP (current_hashtable));
|
|
596
|
231
|
597 return Qt;
|
|
598 }
|
|
599
|
|
600 /*
|
|
601 * Return value is Qt if we have dispatched the command,
|
|
602 * or Qnil if id has not been mapped to a callback.
|
|
603 * Window procedure may try other targets to route the
|
|
604 * command if we return nil
|
|
605 */
|
|
606 Lisp_Object
|
|
607 mswindows_handle_wm_command (struct frame* f, WORD id)
|
|
608 {
|
|
609 /* Try to map the command id through the proper hash table */
|
233
|
610 Lisp_Object command, funcsym, frame;
|
231
|
611 struct gcpro gcpro1;
|
|
612
|
233
|
613 command = Fgethash (make_int (id), current_hashtable, Qunbound);
|
231
|
614 if (UNBOUNDP (command))
|
|
615 {
|
|
616 menu_cleanup (f);
|
|
617 return Qnil;
|
|
618 }
|
|
619
|
|
620 /* Need to gcpro because the hashtable may get destroyed
|
|
621 by menu_cleanup(), and will not gcpro the command
|
|
622 any more */
|
|
623 GCPRO1 (command);
|
|
624 menu_cleanup (f);
|
|
625
|
|
626 /* Ok, this is our one. Enqueue it. */
|
|
627 if (SYMBOLP (command))
|
|
628 funcsym = Qcall_interactively;
|
|
629 else if (CONSP (command))
|
|
630 funcsym = Qeval;
|
|
631 else
|
251
|
632 signal_simple_error ("Callback must be either evallable form or a symbol",
|
|
633 command);
|
231
|
634
|
|
635 XSETFRAME (frame, f);
|
|
636 enqueue_misc_user_event (frame, funcsym, command);
|
233
|
637
|
|
638 /* Needs good bump also, for WM_COMMAND may have been dispatched from
|
|
639 mswindows_need_event, which will block again despite new command
|
|
640 event has arrived */
|
|
641 mswindows_enqueue_magic_event (FRAME_MSWINDOWS_HANDLE(f),
|
|
642 XM_BUMPQUEUE);
|
231
|
643
|
|
644 UNGCPRO; /* command */
|
|
645 return Qt;
|
|
646 }
|
|
647
|
|
648
|
|
649 /*------------------------------------------------------------------------*/
|
|
650 /* Message handling proxies */
|
|
651 /*------------------------------------------------------------------------*/
|
|
652
|
|
653 static HMENU wm_initmenu_menu;
|
|
654 static struct frame* wm_initmenu_frame;
|
|
655
|
|
656 static Lisp_Object
|
|
657 unsafe_handle_wm_initmenupopup (Lisp_Object u_n_u_s_e_d)
|
|
658 {
|
|
659 return unsafe_handle_wm_initmenupopup_1 (wm_initmenu_menu, wm_initmenu_frame);
|
|
660 }
|
|
661
|
|
662 static Lisp_Object
|
|
663 unsafe_handle_wm_initmenu (Lisp_Object u_n_u_s_e_d)
|
|
664 {
|
|
665 return unsafe_handle_wm_initmenu_1 (wm_initmenu_frame);
|
|
666 }
|
|
667
|
|
668 Lisp_Object
|
|
669 mswindows_handle_wm_initmenupopup (HMENU hmenu, struct frame* frm)
|
|
670 {
|
|
671 /* We cannot pass hmenu as a lisp object. Use static var */
|
|
672 wm_initmenu_menu = hmenu;
|
|
673 wm_initmenu_frame = frm;
|
|
674 return mswindows_protect_modal_loop (unsafe_handle_wm_initmenupopup, Qnil);
|
|
675 }
|
|
676
|
|
677 Lisp_Object
|
233
|
678 mswindows_handle_wm_initmenu (HMENU hmenu, struct frame* f)
|
231
|
679 {
|
233
|
680 /* Handle only frame menubar, ignore if from popup or system menu */
|
|
681 if (GetMenu (FRAME_MSWINDOWS_HANDLE(f)) == hmenu)
|
|
682 {
|
|
683 wm_initmenu_frame = f;
|
|
684 return mswindows_protect_modal_loop (unsafe_handle_wm_initmenu, Qnil);
|
|
685 }
|
|
686 return Qt;
|
231
|
687 }
|
|
688
|
251
|
689 /* #### This function goes away. Removing it now may
|
|
690 interfere with pending patch 980128-jhar */
|
231
|
691 Lisp_Object
|
|
692 mswindows_handle_wm_exitmenuloop (struct frame* f)
|
|
693 {
|
|
694 return Qt;
|
|
695 }
|
|
696
|
|
697
|
|
698 /*------------------------------------------------------------------------*/
|
|
699 /* Methods */
|
|
700 /*------------------------------------------------------------------------*/
|
|
701
|
|
702 static void
|
|
703 mswindows_update_frame_menubars (struct frame* f)
|
|
704 {
|
251
|
705 /* #### KLUDGE. menubar.c calls us when the following
|
|
706 condition is true:
|
|
707 (f->menubar_changed || f->windows_changed)
|
|
708 Is that much really necessary?
|
|
709 */
|
|
710 if (f->menubar_changed)
|
|
711 update_frame_menubar_maybe (f);
|
231
|
712 }
|
|
713
|
|
714 static void
|
|
715 mswindows_free_frame_menubars (struct frame* f)
|
|
716 {
|
|
717 FRAME_MSWINDOWS_MENU_HASHTABLE(f) = Qnil;
|
|
718 }
|
|
719
|
|
720 static void
|
|
721 mswindows_popup_menu (Lisp_Object menu_desc, Lisp_Object event)
|
|
722 {
|
|
723 struct frame *f = selected_frame ();
|
|
724 struct Lisp_Event *eev = NULL;
|
|
725 HMENU menu;
|
|
726 POINT pt;
|
|
727 int ok;
|
|
728
|
|
729 if (!NILP (event))
|
|
730 {
|
|
731 CHECK_LIVE_EVENT (event);
|
|
732 eev = XEVENT (event);
|
|
733 if (eev->event_type != button_press_event
|
|
734 && eev->event_type != button_release_event)
|
|
735 wrong_type_argument (Qmouse_event_p, event);
|
|
736 }
|
|
737 else if (!NILP (Vthis_command_keys))
|
|
738 {
|
|
739 /* if an event wasn't passed, use the last event of the event sequence
|
|
740 currently being executed, if that event is a mouse event */
|
|
741 eev = XEVENT (Vthis_command_keys); /* last event first */
|
|
742 if (eev->event_type != button_press_event
|
|
743 && eev->event_type != button_release_event)
|
|
744 eev = NULL;
|
|
745 }
|
|
746
|
|
747 /* Default is to put the menu at the point (10, 10) in frame */
|
|
748 if (eev)
|
|
749 {
|
|
750 pt.x = eev->event.button.x;
|
|
751 pt.y = eev->event.button.y;
|
|
752 ClientToScreen (FRAME_MSWINDOWS_HANDLE (f), &pt);
|
|
753 }
|
|
754 else
|
|
755 pt.x = pt.y = 10;
|
|
756
|
|
757 if (SYMBOLP (menu_desc))
|
|
758 menu_desc = Fsymbol_value (menu_desc);
|
|
759
|
233
|
760 current_menudesc = menu_desc;
|
|
761 current_hashtable = Fmake_hashtable (make_int(10), Qequal);
|
231
|
762 menu = create_empty_popup_menu();
|
233
|
763 Fputhash (hmenu_to_lisp_object (menu), Qnil, current_hashtable);
|
251
|
764 top_level_menu = menu;
|
231
|
765
|
233
|
766 ok = TrackPopupMenu (menu,
|
|
767 TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
|
231
|
768 pt.x, pt.y, 0,
|
|
769 FRAME_MSWINDOWS_HANDLE (f), NULL);
|
|
770
|
|
771 DestroyMenu (menu);
|
|
772
|
|
773 /* Signal a signal if caught by Track...() modal loop */
|
|
774 mswindows_unmodalize_signal_maybe ();
|
|
775
|
|
776 /* This is probably the only real reason for failure */
|
|
777 if (!ok) {
|
|
778 menu_cleanup (f);
|
233
|
779 signal_simple_error ("Cannot track popup menu while in menu",
|
231
|
780 menu_desc);
|
|
781 }
|
|
782 }
|
|
783
|
|
784
|
|
785 /*------------------------------------------------------------------------*/
|
|
786 /* Initialization */
|
|
787 /*------------------------------------------------------------------------*/
|
|
788 void
|
|
789 syms_of_menubar_mswindows (void)
|
|
790 {
|
|
791 }
|
|
792
|
|
793 void
|
|
794 console_type_create_menubar_mswindows (void)
|
|
795 {
|
|
796 CONSOLE_HAS_METHOD (mswindows, update_frame_menubars);
|
|
797 CONSOLE_HAS_METHOD (mswindows, free_frame_menubars);
|
|
798 CONSOLE_HAS_METHOD (mswindows, popup_menu);
|
|
799 }
|
|
800
|
|
801 void
|
|
802 vars_of_menubar_mswindows (void)
|
|
803 {
|
233
|
804 current_menudesc = Qnil;
|
|
805 current_hashtable = Qnil;
|
231
|
806
|
233
|
807 staticpro (¤t_menudesc);
|
|
808 staticpro (¤t_hashtable);
|
231
|
809
|