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