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