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