(keymap_panes): Pass the keymap's prompt as the pane name to
[bpt/emacs.git] / src / w32menu.c
1 /* Menu support for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1986, 88, 93, 94, 96, 98, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <config.h>
22 #include <signal.h>
23
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "termhooks.h"
27 #include "frame.h"
28 #include "window.h"
29 #include "keyboard.h"
30 #include "blockinput.h"
31 #include "buffer.h"
32 #include "charset.h"
33 #include "coding.h"
34
35 /* This may include sys/types.h, and that somehow loses
36 if this is not done before the other system files. */
37 #include "w32term.h"
38
39 /* Load sys/types.h if not already loaded.
40 In some systems loading it twice is suicidal. */
41 #ifndef makedev
42 #include <sys/types.h>
43 #endif
44
45 #include "dispextern.h"
46
47 #undef HAVE_MULTILINGUAL_MENU
48 #undef HAVE_DIALOGS /* NTEMACS_TODO: Implement native dialogs. */
49
50 /******************************************************************/
51 /* Definitions copied from lwlib.h */
52
53 typedef void * XtPointer;
54 typedef char Boolean;
55
56 #define True 1
57 #define False 0
58
59 enum button_type
60 {
61 BUTTON_TYPE_NONE,
62 BUTTON_TYPE_TOGGLE,
63 BUTTON_TYPE_RADIO
64 };
65
66 typedef struct _widget_value
67 {
68 /* name of widget */
69 char* name;
70 /* value (meaning depend on widget type) */
71 char* value;
72 /* keyboard equivalent. no implications for XtTranslations */
73 char* key;
74 /* Help string or null if none. */
75 char *help;
76 /* true if enabled */
77 Boolean enabled;
78 /* true if selected */
79 Boolean selected;
80 /* The type of a button. */
81 enum button_type button_type;
82 /* true if menu title */
83 Boolean title;
84 #if 0
85 /* true if was edited (maintained by get_value) */
86 Boolean edited;
87 /* true if has changed (maintained by lw library) */
88 change_type change;
89 /* true if this widget itself has changed,
90 but not counting the other widgets found in the `next' field. */
91 change_type this_one_change;
92 #endif
93 /* Contents of the sub-widgets, also selected slot for checkbox */
94 struct _widget_value* contents;
95 /* data passed to callback */
96 XtPointer call_data;
97 /* next one in the list */
98 struct _widget_value* next;
99 #if 0
100 /* slot for the toolkit dependent part. Always initialize to NULL. */
101 void* toolkit_data;
102 /* tell us if we should free the toolkit data slot when freeing the
103 widget_value itself. */
104 Boolean free_toolkit_data;
105
106 /* we resource the widget_value structures; this points to the next
107 one on the free list if this one has been deallocated.
108 */
109 struct _widget_value *free_list;
110 #endif
111 } widget_value;
112
113 /* LocalAlloc/Free is a reasonably good allocator. */
114 #define malloc_widget_value() (void*)LocalAlloc (LMEM_ZEROINIT, sizeof (widget_value))
115 #define free_widget_value(wv) LocalFree (wv)
116
117 /******************************************************************/
118
119 #define min(x,y) (((x) < (y)) ? (x) : (y))
120 #define max(x,y) (((x) > (y)) ? (x) : (y))
121
122 #ifndef TRUE
123 #define TRUE 1
124 #define FALSE 0
125 #endif /* no TRUE */
126
127 Lisp_Object Vmenu_updating_frame;
128
129 Lisp_Object Qdebug_on_next_call;
130
131 extern Lisp_Object Qmenu_bar;
132 extern Lisp_Object Qmouse_click, Qevent_kind;
133
134 extern Lisp_Object QCtoggle, QCradio;
135
136 extern Lisp_Object Voverriding_local_map;
137 extern Lisp_Object Voverriding_local_map_menu_flag;
138
139 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
140
141 extern Lisp_Object Qmenu_bar_update_hook;
142
143 void set_frame_menubar ();
144
145 static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
146 Lisp_Object, Lisp_Object, Lisp_Object,
147 Lisp_Object, Lisp_Object));
148 static Lisp_Object w32_dialog_show ();
149 static Lisp_Object w32_menu_show ();
150
151 static void keymap_panes ();
152 static void single_keymap_panes ();
153 static void single_menu_item ();
154 static void list_of_panes ();
155 static void list_of_items ();
156 \f
157 /* This holds a Lisp vector that holds the results of decoding
158 the keymaps or alist-of-alists that specify a menu.
159
160 It describes the panes and items within the panes.
161
162 Each pane is described by 3 elements in the vector:
163 t, the pane name, the pane's prefix key.
164 Then follow the pane's items, with 5 elements per item:
165 the item string, the enable flag, the item's value,
166 the definition, and the equivalent keyboard key's description string.
167
168 In some cases, multiple levels of menus may be described.
169 A single vector slot containing nil indicates the start of a submenu.
170 A single vector slot containing lambda indicates the end of a submenu.
171 The submenu follows a menu item which is the way to reach the submenu.
172
173 A single vector slot containing quote indicates that the
174 following items should appear on the right of a dialog box.
175
176 Using a Lisp vector to hold this information while we decode it
177 takes care of protecting all the data from GC. */
178
179 #define MENU_ITEMS_PANE_NAME 1
180 #define MENU_ITEMS_PANE_PREFIX 2
181 #define MENU_ITEMS_PANE_LENGTH 3
182
183 enum menu_item_idx
184 {
185 MENU_ITEMS_ITEM_NAME = 0,
186 MENU_ITEMS_ITEM_ENABLE,
187 MENU_ITEMS_ITEM_VALUE,
188 MENU_ITEMS_ITEM_EQUIV_KEY,
189 MENU_ITEMS_ITEM_DEFINITION,
190 MENU_ITEMS_ITEM_TYPE,
191 MENU_ITEMS_ITEM_SELECTED,
192 MENU_ITEMS_ITEM_HELP,
193 MENU_ITEMS_ITEM_LENGTH
194 };
195
196 static Lisp_Object menu_items;
197
198 /* Number of slots currently allocated in menu_items. */
199 static int menu_items_allocated;
200
201 /* This is the index in menu_items of the first empty slot. */
202 static int menu_items_used;
203
204 /* The number of panes currently recorded in menu_items,
205 excluding those within submenus. */
206 static int menu_items_n_panes;
207
208 /* Current depth within submenus. */
209 static int menu_items_submenu_depth;
210
211 /* Flag which when set indicates a dialog or menu has been posted by
212 Xt on behalf of one of the widget sets. */
213 static int popup_activated_flag;
214
215 static int next_menubar_widget_id;
216
217 /* This is set nonzero after the user activates the menu bar, and set
218 to zero again after the menu bars are redisplayed by prepare_menu_bar.
219 While it is nonzero, all calls to set_frame_menubar go deep.
220
221 I don't understand why this is needed, but it does seem to be
222 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
223
224 int pending_menu_activation;
225 \f
226
227 /* Return the frame whose ->output_data.w32->menubar_widget equals
228 ID, or 0 if none. */
229
230 static struct frame *
231 menubar_id_to_frame (id)
232 HMENU id;
233 {
234 Lisp_Object tail, frame;
235 FRAME_PTR f;
236
237 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
238 {
239 frame = XCAR (tail);
240 if (!GC_FRAMEP (frame))
241 continue;
242 f = XFRAME (frame);
243 if (!FRAME_WINDOW_P (f))
244 continue;
245 if (f->output_data.w32->menubar_widget == id)
246 return f;
247 }
248 return 0;
249 }
250 \f
251 /* Initialize the menu_items structure if we haven't already done so.
252 Also mark it as currently empty. */
253
254 static void
255 init_menu_items ()
256 {
257 if (NILP (menu_items))
258 {
259 menu_items_allocated = 60;
260 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
261 }
262
263 menu_items_used = 0;
264 menu_items_n_panes = 0;
265 menu_items_submenu_depth = 0;
266 }
267
268 /* Call at the end of generating the data in menu_items.
269 This fills in the number of items in the last pane. */
270
271 static void
272 finish_menu_items ()
273 {
274 }
275
276 /* Call when finished using the data for the current menu
277 in menu_items. */
278
279 static void
280 discard_menu_items ()
281 {
282 /* Free the structure if it is especially large.
283 Otherwise, hold on to it, to save time. */
284 if (menu_items_allocated > 200)
285 {
286 menu_items = Qnil;
287 menu_items_allocated = 0;
288 }
289 }
290
291 /* Make the menu_items vector twice as large. */
292
293 static void
294 grow_menu_items ()
295 {
296 Lisp_Object old;
297 int old_size = menu_items_allocated;
298 old = menu_items;
299
300 menu_items_allocated *= 2;
301 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
302 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
303 old_size * sizeof (Lisp_Object));
304 }
305
306 /* Begin a submenu. */
307
308 static void
309 push_submenu_start ()
310 {
311 if (menu_items_used + 1 > menu_items_allocated)
312 grow_menu_items ();
313
314 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
315 menu_items_submenu_depth++;
316 }
317
318 /* End a submenu. */
319
320 static void
321 push_submenu_end ()
322 {
323 if (menu_items_used + 1 > menu_items_allocated)
324 grow_menu_items ();
325
326 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
327 menu_items_submenu_depth--;
328 }
329
330 /* Indicate boundary between left and right. */
331
332 static void
333 push_left_right_boundary ()
334 {
335 if (menu_items_used + 1 > menu_items_allocated)
336 grow_menu_items ();
337
338 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
339 }
340
341 /* Start a new menu pane in menu_items..
342 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
343
344 static void
345 push_menu_pane (name, prefix_vec)
346 Lisp_Object name, prefix_vec;
347 {
348 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
349 grow_menu_items ();
350
351 if (menu_items_submenu_depth == 0)
352 menu_items_n_panes++;
353 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
354 XVECTOR (menu_items)->contents[menu_items_used++] = name;
355 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
356 }
357
358 /* Push one menu item into the current pane. NAME is the string to
359 display. ENABLE if non-nil means this item can be selected. KEY
360 is the key generated by choosing this item, or nil if this item
361 doesn't really have a definition. DEF is the definition of this
362 item. EQUIV is the textual description of the keyboard equivalent
363 for this item (or nil if none). TYPE is the type of this menu
364 item, one of nil, `toggle' or `radio'. */
365
366 static void
367 push_menu_item (name, enable, key, def, equiv, type, selected, help)
368 Lisp_Object name, enable, key, def, equiv, type, selected, help;
369 {
370 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
371 grow_menu_items ();
372
373 XVECTOR (menu_items)->contents[menu_items_used++] = name;
374 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
375 XVECTOR (menu_items)->contents[menu_items_used++] = key;
376 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
377 XVECTOR (menu_items)->contents[menu_items_used++] = def;
378 XVECTOR (menu_items)->contents[menu_items_used++] = type;
379 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
380 XVECTOR (menu_items)->contents[menu_items_used++] = help;
381 }
382 \f
383 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
384 and generate menu panes for them in menu_items.
385 If NOTREAL is nonzero,
386 don't bother really computing whether an item is enabled. */
387
388 static void
389 keymap_panes (keymaps, nmaps, notreal)
390 Lisp_Object *keymaps;
391 int nmaps;
392 int notreal;
393 {
394 int mapno;
395
396 init_menu_items ();
397
398 /* Loop over the given keymaps, making a pane for each map.
399 But don't make a pane that is empty--ignore that map instead.
400 P is the number of panes we have made so far. */
401 for (mapno = 0; mapno < nmaps; mapno++)
402 single_keymap_panes (keymaps[mapno],
403 map_prompt (keymaps[mapno]), Qnil, notreal, 10);
404
405 finish_menu_items ();
406 }
407
408 /* This is a recursive subroutine of keymap_panes.
409 It handles one keymap, KEYMAP.
410 The other arguments are passed along
411 or point to local variables of the previous function.
412 If NOTREAL is nonzero, only check for equivalent key bindings, don't
413 evaluate expressions in menu items and don't make any menu.
414
415 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
416
417 static void
418 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
419 Lisp_Object keymap;
420 Lisp_Object pane_name;
421 Lisp_Object prefix;
422 int notreal;
423 int maxdepth;
424 {
425 Lisp_Object pending_maps = Qnil;
426 Lisp_Object tail, item;
427 struct gcpro gcpro1, gcpro2;
428
429 if (maxdepth <= 0)
430 return;
431
432 push_menu_pane (pane_name, prefix);
433
434 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
435 {
436 GCPRO2 (keymap, pending_maps);
437 /* Look at each key binding, and if it is a menu item add it
438 to this menu. */
439 item = XCAR (tail);
440 if (CONSP (item))
441 single_menu_item (XCAR (item), XCDR (item),
442 &pending_maps, notreal, maxdepth);
443 else if (VECTORP (item))
444 {
445 /* Loop over the char values represented in the vector. */
446 int len = XVECTOR (item)->size;
447 int c;
448 for (c = 0; c < len; c++)
449 {
450 Lisp_Object character;
451 XSETFASTINT (character, c);
452 single_menu_item (character, XVECTOR (item)->contents[c],
453 &pending_maps, notreal, maxdepth);
454 }
455 }
456 UNGCPRO;
457 }
458
459 /* Process now any submenus which want to be panes at this level. */
460 while (!NILP (pending_maps))
461 {
462 Lisp_Object elt, eltcdr, string;
463 elt = Fcar (pending_maps);
464 eltcdr = XCDR (elt);
465 string = XCAR (eltcdr);
466 /* We no longer discard the @ from the beginning of the string here.
467 Instead, we do this in w32_menu_show. */
468 single_keymap_panes (Fcar (elt), string,
469 XCDR (eltcdr), notreal, maxdepth - 1);
470 pending_maps = Fcdr (pending_maps);
471 }
472 }
473 \f
474 /* This is a subroutine of single_keymap_panes that handles one
475 keymap entry.
476 KEY is a key in a keymap and ITEM is its binding.
477 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
478 separate panes.
479 If NOTREAL is nonzero, only check for equivalent key bindings, don't
480 evaluate expressions in menu items and don't make any menu.
481 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
482
483 static void
484 single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth)
485 Lisp_Object key, item;
486 Lisp_Object *pending_maps_ptr;
487 int maxdepth, notreal;
488 {
489 Lisp_Object map, item_string, enabled;
490 struct gcpro gcpro1, gcpro2;
491 int res;
492
493 /* Parse the menu item and leave the result in item_properties. */
494 GCPRO2 (key, item);
495 res = parse_menu_item (item, notreal, 0);
496 UNGCPRO;
497 if (!res)
498 return; /* Not a menu item. */
499
500 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
501
502 if (notreal)
503 {
504 /* We don't want to make a menu, just traverse the keymaps to
505 precompute equivalent key bindings. */
506 if (!NILP (map))
507 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
508 return;
509 }
510
511 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
512 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
513
514 if (!NILP (map) && XSTRING (item_string)->data[0] == '@')
515 {
516 if (!NILP (enabled))
517 /* An enabled separate pane. Remember this to handle it later. */
518 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
519 *pending_maps_ptr);
520 return;
521 }
522
523 push_menu_item (item_string, enabled, key,
524 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
525 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
526 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
527 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
528 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
529
530 /* Display a submenu using the toolkit. */
531 if (! (NILP (map) || NILP (enabled)))
532 {
533 push_submenu_start ();
534 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
535 push_submenu_end ();
536 }
537 }
538 \f
539 /* Push all the panes and items of a menu described by the
540 alist-of-alists MENU.
541 This handles old-fashioned calls to x-popup-menu. */
542
543 static void
544 list_of_panes (menu)
545 Lisp_Object menu;
546 {
547 Lisp_Object tail;
548
549 init_menu_items ();
550
551 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
552 {
553 Lisp_Object elt, pane_name, pane_data;
554 elt = Fcar (tail);
555 pane_name = Fcar (elt);
556 CHECK_STRING (pane_name, 0);
557 push_menu_pane (pane_name, Qnil);
558 pane_data = Fcdr (elt);
559 CHECK_CONS (pane_data, 0);
560 list_of_items (pane_data);
561 }
562
563 finish_menu_items ();
564 }
565
566 /* Push the items in a single pane defined by the alist PANE. */
567
568 static void
569 list_of_items (pane)
570 Lisp_Object pane;
571 {
572 Lisp_Object tail, item, item1;
573
574 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
575 {
576 item = Fcar (tail);
577 if (STRINGP (item))
578 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil);
579 else if (NILP (item))
580 push_left_right_boundary ();
581 else
582 {
583 CHECK_CONS (item, 0);
584 item1 = Fcar (item);
585 CHECK_STRING (item1, 1);
586 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil);
587 }
588 }
589 }
590 \f
591 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
592 "Pop up a deck-of-cards menu and return user's selection.\n\
593 POSITION is a position specification. This is either a mouse button event\n\
594 or a list ((XOFFSET YOFFSET) WINDOW)\n\
595 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
596 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
597 This controls the position of the center of the first line\n\
598 in the first pane of the menu, not the top left of the menu as a whole.\n\
599 If POSITION is t, it means to use the current mouse position.\n\
600 \n\
601 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
602 The menu items come from key bindings that have a menu string as well as\n\
603 a definition; actually, the \"definition\" in such a key binding looks like\n\
604 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
605 the keymap as a top-level element.\n\n\
606 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
607 Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
608 \n\
609 You can also use a list of keymaps as MENU.\n\
610 Then each keymap makes a separate pane.\n\
611 When MENU is a keymap or a list of keymaps, the return value\n\
612 is a list of events.\n\n\
613 \n\
614 Alternatively, you can specify a menu of multiple panes\n\
615 with a list of the form (TITLE PANE1 PANE2...),\n\
616 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
617 Each ITEM is normally a cons cell (STRING . VALUE);\n\
618 but a string can appear as an item--that makes a nonselectable line\n\
619 in the menu.\n\
620 With this form of menu, the return value is VALUE from the chosen item.\n\
621 \n\
622 If POSITION is nil, don't display the menu at all, just precalculate the\n\
623 cached information about equivalent key sequences.")
624 (position, menu)
625 Lisp_Object position, menu;
626 {
627 Lisp_Object keymap, tem;
628 int xpos, ypos;
629 Lisp_Object title;
630 char *error_name;
631 Lisp_Object selection;
632 FRAME_PTR f;
633 Lisp_Object x, y, window;
634 int keymaps = 0;
635 int for_click = 0;
636 struct gcpro gcpro1;
637
638 #ifdef HAVE_MENUS
639 if (! NILP (position))
640 {
641 check_w32 ();
642
643 /* Decode the first argument: find the window and the coordinates. */
644 if (EQ (position, Qt)
645 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
646 {
647 /* Use the mouse's current position. */
648 FRAME_PTR new_f = SELECTED_FRAME ();
649 Lisp_Object bar_window;
650 enum scroll_bar_part part;
651 unsigned long time;
652
653 if (mouse_position_hook)
654 (*mouse_position_hook) (&new_f, 1, &bar_window,
655 &part, &x, &y, &time);
656 if (new_f != 0)
657 XSETFRAME (window, new_f);
658 else
659 {
660 window = selected_window;
661 XSETFASTINT (x, 0);
662 XSETFASTINT (y, 0);
663 }
664 }
665 else
666 {
667 tem = Fcar (position);
668 if (CONSP (tem))
669 {
670 window = Fcar (Fcdr (position));
671 x = Fcar (tem);
672 y = Fcar (Fcdr (tem));
673 }
674 else
675 {
676 for_click = 1;
677 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
678 window = Fcar (tem); /* POSN_WINDOW (tem) */
679 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
680 x = Fcar (tem);
681 y = Fcdr (tem);
682 }
683 }
684
685 CHECK_NUMBER (x, 0);
686 CHECK_NUMBER (y, 0);
687
688 /* Decode where to put the menu. */
689
690 if (FRAMEP (window))
691 {
692 f = XFRAME (window);
693 xpos = 0;
694 ypos = 0;
695 }
696 else if (WINDOWP (window))
697 {
698 CHECK_LIVE_WINDOW (window, 0);
699 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
700
701 xpos = (FONT_WIDTH (FRAME_FONT (f))
702 * XFASTINT (XWINDOW (window)->left));
703 ypos = (FRAME_LINE_HEIGHT (f)
704 * XFASTINT (XWINDOW (window)->top));
705 }
706 else
707 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
708 but I don't want to make one now. */
709 CHECK_WINDOW (window, 0);
710
711 xpos += XINT (x);
712 ypos += XINT (y);
713
714 XSETFRAME (Vmenu_updating_frame, f);
715 }
716 Vmenu_updating_frame = Qnil;
717 #endif /* HAVE_MENUS */
718
719 title = Qnil;
720 GCPRO1 (title);
721
722 /* Decode the menu items from what was specified. */
723
724 keymap = Fkeymapp (menu);
725 tem = Qnil;
726 if (CONSP (menu))
727 tem = Fkeymapp (Fcar (menu));
728 if (!NILP (keymap))
729 {
730 /* We were given a keymap. Extract menu info from the keymap. */
731 Lisp_Object prompt;
732 keymap = get_keymap (menu);
733
734 /* Extract the detailed info to make one pane. */
735 keymap_panes (&menu, 1, NILP (position));
736
737 /* Search for a string appearing directly as an element of the keymap.
738 That string is the title of the menu. */
739 prompt = map_prompt (keymap);
740 if (NILP (title) && !NILP (prompt))
741 title = prompt;
742
743 /* Make that be the pane title of the first pane. */
744 if (!NILP (prompt) && menu_items_n_panes >= 0)
745 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
746
747 keymaps = 1;
748 }
749 else if (!NILP (tem))
750 {
751 /* We were given a list of keymaps. */
752 int nmaps = XFASTINT (Flength (menu));
753 Lisp_Object *maps
754 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
755 int i;
756
757 title = Qnil;
758
759 /* The first keymap that has a prompt string
760 supplies the menu title. */
761 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
762 {
763 Lisp_Object prompt;
764
765 maps[i++] = keymap = get_keymap (Fcar (tem));
766
767 prompt = map_prompt (keymap);
768 if (NILP (title) && !NILP (prompt))
769 title = prompt;
770 }
771
772 /* Extract the detailed info to make one pane. */
773 keymap_panes (maps, nmaps, NILP (position));
774
775 /* Make the title be the pane title of the first pane. */
776 if (!NILP (title) && menu_items_n_panes >= 0)
777 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
778
779 keymaps = 1;
780 }
781 else
782 {
783 /* We were given an old-fashioned menu. */
784 title = Fcar (menu);
785 CHECK_STRING (title, 1);
786
787 list_of_panes (Fcdr (menu));
788
789 keymaps = 0;
790 }
791
792 if (NILP (position))
793 {
794 discard_menu_items ();
795 UNGCPRO;
796 return Qnil;
797 }
798
799 #ifdef HAVE_MENUS
800 /* Display them in a menu. */
801 BLOCK_INPUT;
802
803 selection = w32_menu_show (f, xpos, ypos, for_click,
804 keymaps, title, &error_name);
805 UNBLOCK_INPUT;
806
807 discard_menu_items ();
808
809 UNGCPRO;
810 #endif /* HAVE_MENUS */
811
812 if (error_name) error (error_name);
813 return selection;
814 }
815
816 #ifdef HAVE_MENUS
817
818 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
819 "Pop up a dialog box and return user's selection.\n\
820 POSITION specifies which frame to use.\n\
821 This is normally a mouse button event or a window or frame.\n\
822 If POSITION is t, it means to use the frame the mouse is on.\n\
823 The dialog box appears in the middle of the specified frame.\n\
824 \n\
825 CONTENTS specifies the alternatives to display in the dialog box.\n\
826 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
827 Each ITEM is a cons cell (STRING . VALUE).\n\
828 The return value is VALUE from the chosen item.\n\n\
829 An ITEM may also be just a string--that makes a nonselectable item.\n\
830 An ITEM may also be nil--that means to put all preceding items\n\
831 on the left of the dialog box and all following items on the right.\n\
832 \(By default, approximately half appear on each side.)")
833 (position, contents)
834 Lisp_Object position, contents;
835 {
836 FRAME_PTR f;
837 Lisp_Object window;
838
839 check_w32 ();
840
841 /* Decode the first argument: find the window or frame to use. */
842 if (EQ (position, Qt)
843 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
844 {
845 #if 0 /* Using the frame the mouse is on may not be right. */
846 /* Use the mouse's current position. */
847 FRAME_PTR new_f = SELECTED_FRAME ();
848 Lisp_Object bar_window;
849 int part;
850 unsigned long time;
851 Lisp_Object x, y;
852
853 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
854
855 if (new_f != 0)
856 XSETFRAME (window, new_f);
857 else
858 window = selected_window;
859 #endif
860 window = selected_window;
861 }
862 else if (CONSP (position))
863 {
864 Lisp_Object tem;
865 tem = Fcar (position);
866 if (CONSP (tem))
867 window = Fcar (Fcdr (position));
868 else
869 {
870 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
871 window = Fcar (tem); /* POSN_WINDOW (tem) */
872 }
873 }
874 else if (WINDOWP (position) || FRAMEP (position))
875 window = position;
876 else
877 window = Qnil;
878
879 /* Decode where to put the menu. */
880
881 if (FRAMEP (window))
882 f = XFRAME (window);
883 else if (WINDOWP (window))
884 {
885 CHECK_LIVE_WINDOW (window, 0);
886 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
887 }
888 else
889 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
890 but I don't want to make one now. */
891 CHECK_WINDOW (window, 0);
892
893 #ifndef HAVE_DIALOGS
894 /* Display a menu with these alternatives
895 in the middle of frame F. */
896 {
897 Lisp_Object x, y, frame, newpos;
898 XSETFRAME (frame, f);
899 XSETINT (x, x_pixel_width (f) / 2);
900 XSETINT (y, x_pixel_height (f) / 2);
901 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
902
903 return Fx_popup_menu (newpos,
904 Fcons (Fcar (contents), Fcons (contents, Qnil)));
905 }
906 #else /* HAVE_DIALOGS */
907 {
908 Lisp_Object title;
909 char *error_name;
910 Lisp_Object selection;
911
912 /* Decode the dialog items from what was specified. */
913 title = Fcar (contents);
914 CHECK_STRING (title, 1);
915
916 list_of_panes (Fcons (contents, Qnil));
917
918 /* Display them in a dialog box. */
919 BLOCK_INPUT;
920 selection = w32_dialog_show (f, 0, title, &error_name);
921 UNBLOCK_INPUT;
922
923 discard_menu_items ();
924
925 if (error_name) error (error_name);
926 return selection;
927 }
928 #endif /* HAVE_DIALOGS */
929 }
930
931 /* Activate the menu bar of frame F.
932 This is called from keyboard.c when it gets the
933 menu_bar_activate_event out of the Emacs event queue.
934
935 To activate the menu bar, we signal to the input thread that it can
936 return from the WM_INITMENU message, allowing the normal Windows
937 processing of the menus.
938
939 But first we recompute the menu bar contents (the whole tree).
940
941 This way we can safely execute Lisp code. */
942
943 void
944 x_activate_menubar (f)
945 FRAME_PTR f;
946 {
947 set_frame_menubar (f, 0, 1);
948
949 /* Lock out further menubar changes while active. */
950 f->output_data.w32->menubar_active = 1;
951
952 /* Signal input thread to return from WM_INITMENU. */
953 complete_deferred_msg (FRAME_W32_WINDOW (f), WM_INITMENU, 0);
954 }
955
956 /* This callback is called from the menu bar pulldown menu
957 when the user makes a selection.
958 Figure out what the user chose
959 and put the appropriate events into the keyboard buffer. */
960
961 void
962 menubar_selection_callback (FRAME_PTR f, void * client_data)
963 {
964 Lisp_Object prefix, entry;
965 Lisp_Object vector;
966 Lisp_Object *subprefix_stack;
967 int submenu_depth = 0;
968 int i;
969
970 if (!f)
971 return;
972 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
973 vector = f->menu_bar_vector;
974 prefix = Qnil;
975 i = 0;
976 while (i < f->menu_bar_items_used)
977 {
978 if (EQ (XVECTOR (vector)->contents[i], Qnil))
979 {
980 subprefix_stack[submenu_depth++] = prefix;
981 prefix = entry;
982 i++;
983 }
984 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
985 {
986 prefix = subprefix_stack[--submenu_depth];
987 i++;
988 }
989 else if (EQ (XVECTOR (vector)->contents[i], Qt))
990 {
991 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
992 i += MENU_ITEMS_PANE_LENGTH;
993 }
994 else
995 {
996 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
997 /* The EMACS_INT cast avoids a warning. There's no problem
998 as long as pointers have enough bits to hold small integers. */
999 if ((int) (EMACS_INT) client_data == i)
1000 {
1001 int j;
1002 struct input_event buf;
1003 Lisp_Object frame;
1004
1005 XSETFRAME (frame, f);
1006 buf.kind = MENU_BAR_EVENT;
1007 buf.frame_or_window = frame;
1008 buf.arg = frame;
1009 kbd_buffer_store_event (&buf);
1010
1011 for (j = 0; j < submenu_depth; j++)
1012 if (!NILP (subprefix_stack[j]))
1013 {
1014 buf.kind = MENU_BAR_EVENT;
1015 buf.frame_or_window = frame;
1016 buf.arg = subprefix_stack[j];
1017 kbd_buffer_store_event (&buf);
1018 }
1019
1020 if (!NILP (prefix))
1021 {
1022 buf.kind = MENU_BAR_EVENT;
1023 buf.frame_or_window = frame;
1024 buf.arg = prefix;
1025 kbd_buffer_store_event (&buf);
1026 }
1027
1028 buf.kind = MENU_BAR_EVENT;
1029 buf.frame_or_window = frame;
1030 buf.arg = entry;
1031 kbd_buffer_store_event (&buf);
1032
1033 return;
1034 }
1035 i += MENU_ITEMS_ITEM_LENGTH;
1036 }
1037 }
1038 }
1039
1040 /* Allocate a widget_value, blocking input. */
1041
1042 widget_value *
1043 xmalloc_widget_value ()
1044 {
1045 widget_value *value;
1046
1047 BLOCK_INPUT;
1048 value = malloc_widget_value ();
1049 UNBLOCK_INPUT;
1050
1051 return value;
1052 }
1053
1054 /* This recursively calls free_widget_value on the tree of widgets.
1055 It must free all data that was malloc'ed for these widget_values.
1056 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1057 must be left alone. */
1058
1059 void
1060 free_menubar_widget_value_tree (wv)
1061 widget_value *wv;
1062 {
1063 if (! wv) return;
1064
1065 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1066
1067 if (wv->contents && (wv->contents != (widget_value*)1))
1068 {
1069 free_menubar_widget_value_tree (wv->contents);
1070 wv->contents = (widget_value *) 0xDEADBEEF;
1071 }
1072 if (wv->next)
1073 {
1074 free_menubar_widget_value_tree (wv->next);
1075 wv->next = (widget_value *) 0xDEADBEEF;
1076 }
1077 BLOCK_INPUT;
1078 free_widget_value (wv);
1079 UNBLOCK_INPUT;
1080 }
1081 \f
1082 /* Return a tree of widget_value structures for a menu bar item
1083 whose event type is ITEM_KEY (with string ITEM_NAME)
1084 and whose contents come from the list of keymaps MAPS. */
1085
1086 static widget_value *
1087 single_submenu (item_key, item_name, maps)
1088 Lisp_Object item_key, item_name, maps;
1089 {
1090 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1091 int i;
1092 int submenu_depth = 0;
1093 Lisp_Object length;
1094 int len;
1095 Lisp_Object *mapvec;
1096 widget_value **submenu_stack;
1097 int previous_items = menu_items_used;
1098 int top_level_items = 0;
1099
1100 length = Flength (maps);
1101 len = XINT (length);
1102
1103 /* Convert the list MAPS into a vector MAPVEC. */
1104 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1105 for (i = 0; i < len; i++)
1106 {
1107 mapvec[i] = Fcar (maps);
1108 maps = Fcdr (maps);
1109 }
1110
1111 menu_items_n_panes = 0;
1112
1113 /* Loop over the given keymaps, making a pane for each map.
1114 But don't make a pane that is empty--ignore that map instead. */
1115 for (i = 0; i < len; i++)
1116 {
1117 if (SYMBOLP (mapvec[i])
1118 || (CONSP (mapvec[i])
1119 && NILP (Fkeymapp (mapvec[i]))))
1120 {
1121 /* Here we have a command at top level in the menu bar
1122 as opposed to a submenu. */
1123 top_level_items = 1;
1124 push_menu_pane (Qnil, Qnil);
1125 push_menu_item (item_name, Qt, item_key, mapvec[i],
1126 Qnil, Qnil, Qnil, Qnil);
1127 }
1128 else
1129 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
1130 }
1131
1132 /* Create a tree of widget_value objects
1133 representing the panes and their items. */
1134
1135 submenu_stack
1136 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1137 wv = xmalloc_widget_value ();
1138 wv->name = "menu";
1139 wv->value = 0;
1140 wv->enabled = 1;
1141 wv->button_type = BUTTON_TYPE_NONE;
1142 first_wv = wv;
1143 save_wv = 0;
1144 prev_wv = 0;
1145
1146 /* Loop over all panes and items made during this call
1147 and construct a tree of widget_value objects.
1148 Ignore the panes and items made by previous calls to
1149 single_submenu, even though those are also in menu_items. */
1150 i = previous_items;
1151 while (i < menu_items_used)
1152 {
1153 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1154 {
1155 submenu_stack[submenu_depth++] = save_wv;
1156 save_wv = prev_wv;
1157 prev_wv = 0;
1158 i++;
1159 }
1160 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1161 {
1162 prev_wv = save_wv;
1163 save_wv = submenu_stack[--submenu_depth];
1164 i++;
1165 }
1166 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1167 && submenu_depth != 0)
1168 i += MENU_ITEMS_PANE_LENGTH;
1169 /* Ignore a nil in the item list.
1170 It's meaningful only for dialog boxes. */
1171 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1172 i += 1;
1173 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1174 {
1175 /* Create a new pane. */
1176 Lisp_Object pane_name, prefix;
1177 char *pane_string;
1178 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1179 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1180 #ifndef HAVE_MULTILINGUAL_MENU
1181 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1182 pane_name = ENCODE_SYSTEM (pane_name);
1183 #endif
1184 pane_string = (NILP (pane_name)
1185 ? "" : (char *) XSTRING (pane_name)->data);
1186 /* If there is just one top-level pane, put all its items directly
1187 under the top-level menu. */
1188 if (menu_items_n_panes == 1)
1189 pane_string = "";
1190
1191 /* If the pane has a meaningful name,
1192 make the pane a top-level menu item
1193 with its items as a submenu beneath it. */
1194 if (strcmp (pane_string, ""))
1195 {
1196 wv = xmalloc_widget_value ();
1197 if (save_wv)
1198 save_wv->next = wv;
1199 else
1200 first_wv->contents = wv;
1201 wv->name = pane_string;
1202 /* Ignore the @ that means "separate pane".
1203 This is a kludge, but this isn't worth more time. */
1204 if (!NILP (prefix) && wv->name[0] == '@')
1205 wv->name++;
1206 wv->value = 0;
1207 wv->enabled = 1;
1208 wv->button_type = BUTTON_TYPE_NONE;
1209 }
1210 save_wv = wv;
1211 prev_wv = 0;
1212 i += MENU_ITEMS_PANE_LENGTH;
1213 }
1214 else
1215 {
1216 /* Create a new item within current pane. */
1217 Lisp_Object item_name, enable, descrip, def, type, selected;
1218 Lisp_Object help;
1219
1220 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1221 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1222 descrip
1223 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1224 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1225 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
1226 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
1227 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
1228
1229 #ifndef HAVE_MULTILINGUAL_MENU
1230 if (STRING_MULTIBYTE (item_name))
1231 item_name = ENCODE_SYSTEM (item_name);
1232 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1233 descrip = ENCODE_SYSTEM (descrip);
1234 #endif
1235
1236 wv = xmalloc_widget_value ();
1237 if (prev_wv)
1238 prev_wv->next = wv;
1239 else
1240 save_wv->contents = wv;
1241
1242 wv->name = (char *) XSTRING (item_name)->data;
1243 if (!NILP (descrip))
1244 wv->key = (char *) XSTRING (descrip)->data;
1245 wv->value = 0;
1246 /* The EMACS_INT cast avoids a warning. There's no problem
1247 as long as pointers have enough bits to hold small integers. */
1248 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1249 wv->enabled = !NILP (enable);
1250
1251 if (NILP (type))
1252 wv->button_type = BUTTON_TYPE_NONE;
1253 else if (EQ (type, QCradio))
1254 wv->button_type = BUTTON_TYPE_RADIO;
1255 else if (EQ (type, QCtoggle))
1256 wv->button_type = BUTTON_TYPE_TOGGLE;
1257 else
1258 abort ();
1259
1260 wv->selected = !NILP (selected);
1261 if (STRINGP (help))
1262 wv->help = (char *) XSTRING (help)->data;
1263 else
1264 wv->help = NULL;
1265
1266 prev_wv = wv;
1267
1268 i += MENU_ITEMS_ITEM_LENGTH;
1269 }
1270 }
1271
1272 /* If we have just one "menu item"
1273 that was originally a button, return it by itself. */
1274 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1275 {
1276 wv = first_wv->contents;
1277 free_widget_value (first_wv);
1278 return wv;
1279 }
1280
1281 return first_wv;
1282 }
1283 \f
1284 /* Set the contents of the menubar widgets of frame F.
1285 The argument FIRST_TIME is currently ignored;
1286 it is set the first time this is called, from initialize_frame_menubar. */
1287
1288 void
1289 set_frame_menubar (f, first_time, deep_p)
1290 FRAME_PTR f;
1291 int first_time;
1292 int deep_p;
1293 {
1294 HMENU menubar_widget = f->output_data.w32->menubar_widget;
1295 Lisp_Object items;
1296 widget_value *wv, *first_wv, *prev_wv = 0;
1297 int i;
1298
1299 /* We must not change the menubar when actually in use. */
1300 if (f->output_data.w32->menubar_active)
1301 return;
1302
1303 XSETFRAME (Vmenu_updating_frame, f);
1304
1305 if (! menubar_widget)
1306 deep_p = 1;
1307 else if (pending_menu_activation && !deep_p)
1308 deep_p = 1;
1309
1310 wv = xmalloc_widget_value ();
1311 wv->name = "menubar";
1312 wv->value = 0;
1313 wv->enabled = 1;
1314 wv->button_type = BUTTON_TYPE_NONE;
1315 first_wv = wv;
1316
1317 if (deep_p)
1318 {
1319 /* Make a widget-value tree representing the entire menu trees. */
1320
1321 struct buffer *prev = current_buffer;
1322 Lisp_Object buffer;
1323 int specpdl_count = specpdl_ptr - specpdl;
1324 int previous_menu_items_used = f->menu_bar_items_used;
1325 Lisp_Object *previous_items
1326 = (Lisp_Object *) alloca (previous_menu_items_used
1327 * sizeof (Lisp_Object));
1328
1329 /* If we are making a new widget, its contents are empty,
1330 do always reinitialize them. */
1331 if (! menubar_widget)
1332 previous_menu_items_used = 0;
1333
1334 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1335 specbind (Qinhibit_quit, Qt);
1336 /* Don't let the debugger step into this code
1337 because it is not reentrant. */
1338 specbind (Qdebug_on_next_call, Qnil);
1339
1340 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1341 if (NILP (Voverriding_local_map_menu_flag))
1342 {
1343 specbind (Qoverriding_terminal_local_map, Qnil);
1344 specbind (Qoverriding_local_map, Qnil);
1345 }
1346
1347 set_buffer_internal_1 (XBUFFER (buffer));
1348
1349 /* Run the Lucid hook. */
1350 call1 (Vrun_hooks, Qactivate_menubar_hook);
1351 /* If it has changed current-menubar from previous value,
1352 really recompute the menubar from the value. */
1353 if (! NILP (Vlucid_menu_bar_dirty_flag))
1354 call0 (Qrecompute_lucid_menubar);
1355 safe_run_hooks (Qmenu_bar_update_hook);
1356 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1357
1358 items = FRAME_MENU_BAR_ITEMS (f);
1359
1360 inhibit_garbage_collection ();
1361
1362 /* Save the frame's previous menu bar contents data. */
1363 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1364 previous_menu_items_used * sizeof (Lisp_Object));
1365
1366 /* Fill in the current menu bar contents. */
1367 menu_items = f->menu_bar_vector;
1368 menu_items_allocated = XVECTOR (menu_items)->size;
1369 init_menu_items ();
1370 for (i = 0; i < XVECTOR (items)->size; i += 4)
1371 {
1372 Lisp_Object key, string, maps;
1373
1374 key = XVECTOR (items)->contents[i];
1375 string = XVECTOR (items)->contents[i + 1];
1376 maps = XVECTOR (items)->contents[i + 2];
1377 if (NILP (string))
1378 break;
1379
1380 wv = single_submenu (key, string, maps);
1381 if (prev_wv)
1382 prev_wv->next = wv;
1383 else
1384 first_wv->contents = wv;
1385 /* Don't set wv->name here; GC during the loop might relocate it. */
1386 wv->enabled = 1;
1387 wv->button_type = BUTTON_TYPE_NONE;
1388 prev_wv = wv;
1389 }
1390
1391 finish_menu_items ();
1392
1393 set_buffer_internal_1 (prev);
1394 unbind_to (specpdl_count, Qnil);
1395
1396 /* If there has been no change in the Lisp-level contents
1397 of the menu bar, skip redisplaying it. Just exit. */
1398
1399 for (i = 0; i < previous_menu_items_used; i++)
1400 if (menu_items_used == i
1401 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1402 break;
1403 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1404 {
1405 free_menubar_widget_value_tree (first_wv);
1406 menu_items = Qnil;
1407
1408 return;
1409 }
1410
1411 /* Now GC cannot happen during the lifetime of the widget_value,
1412 so it's safe to store data from a Lisp_String. */
1413 wv = first_wv->contents;
1414 for (i = 0; i < XVECTOR (items)->size; i += 4)
1415 {
1416 Lisp_Object string;
1417 string = XVECTOR (items)->contents[i + 1];
1418 if (NILP (string))
1419 break;
1420 wv->name = (char *) XSTRING (string)->data;
1421 wv = wv->next;
1422 }
1423
1424 f->menu_bar_vector = menu_items;
1425 f->menu_bar_items_used = menu_items_used;
1426 menu_items = Qnil;
1427 }
1428 else
1429 {
1430 /* Make a widget-value tree containing
1431 just the top level menu bar strings. */
1432
1433 items = FRAME_MENU_BAR_ITEMS (f);
1434 for (i = 0; i < XVECTOR (items)->size; i += 4)
1435 {
1436 Lisp_Object string;
1437
1438 string = XVECTOR (items)->contents[i + 1];
1439 if (NILP (string))
1440 break;
1441
1442 wv = xmalloc_widget_value ();
1443 wv->name = (char *) XSTRING (string)->data;
1444 wv->value = 0;
1445 wv->enabled = 1;
1446 wv->button_type = BUTTON_TYPE_NONE;
1447 /* This prevents lwlib from assuming this
1448 menu item is really supposed to be empty. */
1449 /* The EMACS_INT cast avoids a warning.
1450 This value just has to be different from small integers. */
1451 wv->call_data = (void *) (EMACS_INT) (-1);
1452
1453 if (prev_wv)
1454 prev_wv->next = wv;
1455 else
1456 first_wv->contents = wv;
1457 prev_wv = wv;
1458 }
1459
1460 /* Forget what we thought we knew about what is in the
1461 detailed contents of the menu bar menus.
1462 Changing the top level always destroys the contents. */
1463 f->menu_bar_items_used = 0;
1464 }
1465
1466 /* Create or update the menu bar widget. */
1467
1468 BLOCK_INPUT;
1469
1470 if (menubar_widget)
1471 {
1472 /* Empty current menubar, rather than creating a fresh one. */
1473 while (DeleteMenu (menubar_widget, 0, MF_BYPOSITION))
1474 ;
1475 }
1476 else
1477 {
1478 menubar_widget = CreateMenu ();
1479 }
1480 fill_in_menu (menubar_widget, first_wv->contents);
1481
1482 free_menubar_widget_value_tree (first_wv);
1483
1484 {
1485 HMENU old_widget = f->output_data.w32->menubar_widget;
1486
1487 f->output_data.w32->menubar_widget = menubar_widget;
1488 SetMenu (FRAME_W32_WINDOW (f), f->output_data.w32->menubar_widget);
1489 /* Causes flicker when menu bar is updated
1490 DrawMenuBar (FRAME_W32_WINDOW (f)); */
1491
1492 /* Force the window size to be recomputed so that the frame's text
1493 area remains the same, if menubar has just been created. */
1494 if (old_widget == NULL)
1495 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
1496 }
1497
1498 UNBLOCK_INPUT;
1499 }
1500
1501 /* Called from Fx_create_frame to create the initial menubar of a frame
1502 before it is mapped, so that the window is mapped with the menubar already
1503 there instead of us tacking it on later and thrashing the window after it
1504 is visible. */
1505
1506 void
1507 initialize_frame_menubar (f)
1508 FRAME_PTR f;
1509 {
1510 /* This function is called before the first chance to redisplay
1511 the frame. It has to be, so the frame will have the right size. */
1512 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1513 set_frame_menubar (f, 1, 1);
1514 }
1515
1516 /* Get rid of the menu bar of frame F, and free its storage.
1517 This is used when deleting a frame, and when turning off the menu bar. */
1518
1519 void
1520 free_frame_menubar (f)
1521 FRAME_PTR f;
1522 {
1523 BLOCK_INPUT;
1524
1525 {
1526 HMENU old = GetMenu (FRAME_W32_WINDOW (f));
1527 SetMenu (FRAME_W32_WINDOW (f), NULL);
1528 f->output_data.w32->menubar_widget = NULL;
1529 DestroyMenu (old);
1530 }
1531
1532 UNBLOCK_INPUT;
1533 }
1534
1535 \f
1536 /* w32_menu_show actually displays a menu using the panes and items in
1537 menu_items and returns the value selected from it; we assume input
1538 is blocked by the caller. */
1539
1540 /* F is the frame the menu is for.
1541 X and Y are the frame-relative specified position,
1542 relative to the inside upper left corner of the frame F.
1543 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1544 KEYMAPS is 1 if this menu was specified with keymaps;
1545 in that case, we return a list containing the chosen item's value
1546 and perhaps also the pane's prefix.
1547 TITLE is the specified menu title.
1548 ERROR is a place to store an error message string in case of failure.
1549 (We return nil on failure, but the value doesn't actually matter.) */
1550
1551 static Lisp_Object
1552 w32_menu_show (f, x, y, for_click, keymaps, title, error)
1553 FRAME_PTR f;
1554 int x;
1555 int y;
1556 int for_click;
1557 int keymaps;
1558 Lisp_Object title;
1559 char **error;
1560 {
1561 int i;
1562 int menu_item_selection;
1563 HMENU menu;
1564 POINT pos;
1565 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1566 widget_value **submenu_stack
1567 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1568 Lisp_Object *subprefix_stack
1569 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1570 int submenu_depth = 0;
1571 int first_pane;
1572 int next_release_must_exit = 0;
1573
1574 *error = NULL;
1575
1576 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1577 {
1578 *error = "Empty menu";
1579 return Qnil;
1580 }
1581
1582 /* Create a tree of widget_value objects
1583 representing the panes and their items. */
1584 wv = xmalloc_widget_value ();
1585 wv->name = "menu";
1586 wv->value = 0;
1587 wv->enabled = 1;
1588 wv->button_type = BUTTON_TYPE_NONE;
1589 first_wv = wv;
1590 first_pane = 1;
1591
1592 /* Loop over all panes and items, filling in the tree. */
1593 i = 0;
1594 while (i < menu_items_used)
1595 {
1596 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1597 {
1598 submenu_stack[submenu_depth++] = save_wv;
1599 save_wv = prev_wv;
1600 prev_wv = 0;
1601 first_pane = 1;
1602 i++;
1603 }
1604 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1605 {
1606 prev_wv = save_wv;
1607 save_wv = submenu_stack[--submenu_depth];
1608 first_pane = 0;
1609 i++;
1610 }
1611 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1612 && submenu_depth != 0)
1613 i += MENU_ITEMS_PANE_LENGTH;
1614 /* Ignore a nil in the item list.
1615 It's meaningful only for dialog boxes. */
1616 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1617 i += 1;
1618 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1619 {
1620 /* Create a new pane. */
1621 Lisp_Object pane_name, prefix;
1622 char *pane_string;
1623 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1624 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1625 #ifndef HAVE_MULTILINGUAL_MENU
1626 if (!NILP (pane_name) && STRING_MULTIBYTE (pane_name))
1627 pane_name = ENCODE_SYSTEM (pane_name);
1628 #endif
1629 pane_string = (NILP (pane_name)
1630 ? "" : (char *) XSTRING (pane_name)->data);
1631 /* If there is just one top-level pane, put all its items directly
1632 under the top-level menu. */
1633 if (menu_items_n_panes == 1)
1634 pane_string = "";
1635
1636 /* If the pane has a meaningful name,
1637 make the pane a top-level menu item
1638 with its items as a submenu beneath it. */
1639 if (!keymaps && strcmp (pane_string, ""))
1640 {
1641 wv = xmalloc_widget_value ();
1642 if (save_wv)
1643 save_wv->next = wv;
1644 else
1645 first_wv->contents = wv;
1646 wv->name = pane_string;
1647 if (keymaps && !NILP (prefix))
1648 wv->name++;
1649 wv->value = 0;
1650 wv->enabled = 1;
1651 wv->button_type = BUTTON_TYPE_NONE;
1652 save_wv = wv;
1653 prev_wv = 0;
1654 }
1655 else if (first_pane)
1656 {
1657 save_wv = wv;
1658 prev_wv = 0;
1659 }
1660 first_pane = 0;
1661 i += MENU_ITEMS_PANE_LENGTH;
1662 }
1663 else
1664 {
1665 /* Create a new item within current pane. */
1666 Lisp_Object item_name, enable, descrip, def, type, selected, help;
1667
1668 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1669 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1670 descrip
1671 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1672 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1673 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
1674 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
1675 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
1676
1677 #ifndef HAVE_MULTILINGUAL_MENU
1678 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
1679 item_name = ENCODE_SYSTEM (item_name);
1680 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1681 descrip = ENCODE_SYSTEM (descrip);
1682 #endif
1683
1684 wv = xmalloc_widget_value ();
1685 if (prev_wv)
1686 prev_wv->next = wv;
1687 else
1688 save_wv->contents = wv;
1689 wv->name = (char *) XSTRING (item_name)->data;
1690 if (!NILP (descrip))
1691 wv->key = (char *) XSTRING (descrip)->data;
1692 wv->value = 0;
1693 /* Use the contents index as call_data, since we are
1694 restricted to 16-bits.. */
1695 wv->call_data = !NILP (def) ? (void *) (EMACS_INT) i : 0;
1696 wv->enabled = !NILP (enable);
1697
1698 if (NILP (type))
1699 wv->button_type = BUTTON_TYPE_NONE;
1700 else if (EQ (type, QCtoggle))
1701 wv->button_type = BUTTON_TYPE_TOGGLE;
1702 else if (EQ (type, QCradio))
1703 wv->button_type = BUTTON_TYPE_RADIO;
1704 else
1705 abort ();
1706
1707 wv->selected = !NILP (selected);
1708
1709 if (STRINGP (help))
1710 wv->help = (char *) XSTRING (help)->data;
1711 else
1712 wv->help = NULL;
1713
1714 prev_wv = wv;
1715
1716 i += MENU_ITEMS_ITEM_LENGTH;
1717 }
1718 }
1719
1720 /* Deal with the title, if it is non-nil. */
1721 if (!NILP (title))
1722 {
1723 widget_value *wv_title = xmalloc_widget_value ();
1724 widget_value *wv_sep = xmalloc_widget_value ();
1725
1726 /* Maybe replace this separator with a bitmap or owner-draw item
1727 so that it looks better. Having two separators looks odd. */
1728 wv_sep->name = "--";
1729 wv_sep->next = first_wv->contents;
1730
1731 #ifndef HAVE_MULTILINGUAL_MENU
1732 if (STRING_MULTIBYTE (title))
1733 title = ENCODE_SYSTEM (title);
1734 #endif
1735 wv_title->name = (char *) XSTRING (title)->data;
1736 wv_title->enabled = True;
1737 wv_title->title = True;
1738 wv_title->button_type = BUTTON_TYPE_NONE;
1739 wv_title->next = wv_sep;
1740 first_wv->contents = wv_title;
1741 }
1742
1743 /* Actually create the menu. */
1744 menu = CreatePopupMenu ();
1745 fill_in_menu (menu, first_wv->contents);
1746
1747 /* Adjust coordinates to be root-window-relative. */
1748 pos.x = x;
1749 pos.y = y;
1750 ClientToScreen (FRAME_W32_WINDOW (f), &pos);
1751
1752 /* No selection has been chosen yet. */
1753 menu_item_selection = 0;
1754
1755 /* Display the menu. */
1756 menu_item_selection = SendMessage (FRAME_W32_WINDOW (f),
1757 WM_EMACS_TRACKPOPUPMENU,
1758 (WPARAM)menu, (LPARAM)&pos);
1759
1760 /* Clean up extraneous mouse events which might have been generated
1761 during the call. */
1762 discard_mouse_events ();
1763
1764 /* Free the widget_value objects we used to specify the contents. */
1765 free_menubar_widget_value_tree (first_wv);
1766
1767 DestroyMenu (menu);
1768
1769 /* Find the selected item, and its pane, to return
1770 the proper value. */
1771 if (menu_item_selection != 0)
1772 {
1773 Lisp_Object prefix, entry;
1774
1775 prefix = Qnil;
1776 i = 0;
1777 while (i < menu_items_used)
1778 {
1779 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1780 {
1781 subprefix_stack[submenu_depth++] = prefix;
1782 prefix = entry;
1783 i++;
1784 }
1785 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1786 {
1787 prefix = subprefix_stack[--submenu_depth];
1788 i++;
1789 }
1790 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1791 {
1792 prefix
1793 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1794 i += MENU_ITEMS_PANE_LENGTH;
1795 }
1796 /* Ignore a nil in the item list.
1797 It's meaningful only for dialog boxes. */
1798 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1799 i += 1;
1800 else
1801 {
1802 entry
1803 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1804 if (menu_item_selection == i)
1805 {
1806 if (keymaps != 0)
1807 {
1808 int j;
1809
1810 entry = Fcons (entry, Qnil);
1811 if (!NILP (prefix))
1812 entry = Fcons (prefix, entry);
1813 for (j = submenu_depth - 1; j >= 0; j--)
1814 if (!NILP (subprefix_stack[j]))
1815 entry = Fcons (subprefix_stack[j], entry);
1816 }
1817 return entry;
1818 }
1819 i += MENU_ITEMS_ITEM_LENGTH;
1820 }
1821 }
1822 }
1823
1824 return Qnil;
1825 }
1826 \f
1827
1828 static char * button_names [] = {
1829 "button1", "button2", "button3", "button4", "button5",
1830 "button6", "button7", "button8", "button9", "button10" };
1831
1832 static Lisp_Object
1833 w32_dialog_show (f, keymaps, title, error)
1834 FRAME_PTR f;
1835 int keymaps;
1836 Lisp_Object title;
1837 char **error;
1838 {
1839 int i, nb_buttons=0;
1840 char dialog_name[6];
1841 int menu_item_selection;
1842
1843 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1844
1845 /* Number of elements seen so far, before boundary. */
1846 int left_count = 0;
1847 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1848 int boundary_seen = 0;
1849
1850 *error = NULL;
1851
1852 if (menu_items_n_panes > 1)
1853 {
1854 *error = "Multiple panes in dialog box";
1855 return Qnil;
1856 }
1857
1858 /* Create a tree of widget_value objects
1859 representing the text label and buttons. */
1860 {
1861 Lisp_Object pane_name, prefix;
1862 char *pane_string;
1863 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
1864 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
1865 pane_string = (NILP (pane_name)
1866 ? "" : (char *) XSTRING (pane_name)->data);
1867 prev_wv = xmalloc_widget_value ();
1868 prev_wv->value = pane_string;
1869 if (keymaps && !NILP (prefix))
1870 prev_wv->name++;
1871 prev_wv->enabled = 1;
1872 prev_wv->name = "message";
1873 first_wv = prev_wv;
1874
1875 /* Loop over all panes and items, filling in the tree. */
1876 i = MENU_ITEMS_PANE_LENGTH;
1877 while (i < menu_items_used)
1878 {
1879
1880 /* Create a new item within current pane. */
1881 Lisp_Object item_name, enable, descrip, help;
1882
1883 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1884 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1885 descrip
1886 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1887 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
1888
1889 if (NILP (item_name))
1890 {
1891 free_menubar_widget_value_tree (first_wv);
1892 *error = "Submenu in dialog items";
1893 return Qnil;
1894 }
1895 if (EQ (item_name, Qquote))
1896 {
1897 /* This is the boundary between left-side elts
1898 and right-side elts. Stop incrementing right_count. */
1899 boundary_seen = 1;
1900 i++;
1901 continue;
1902 }
1903 if (nb_buttons >= 9)
1904 {
1905 free_menubar_widget_value_tree (first_wv);
1906 *error = "Too many dialog items";
1907 return Qnil;
1908 }
1909
1910 wv = xmalloc_widget_value ();
1911 prev_wv->next = wv;
1912 wv->name = (char *) button_names[nb_buttons];
1913 if (!NILP (descrip))
1914 wv->key = (char *) XSTRING (descrip)->data;
1915 wv->value = (char *) XSTRING (item_name)->data;
1916 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1917 wv->enabled = !NILP (enable);
1918 prev_wv = wv;
1919
1920 if (! boundary_seen)
1921 left_count++;
1922
1923 nb_buttons++;
1924 i += MENU_ITEMS_ITEM_LENGTH;
1925 }
1926
1927 /* If the boundary was not specified,
1928 by default put half on the left and half on the right. */
1929 if (! boundary_seen)
1930 left_count = nb_buttons - nb_buttons / 2;
1931
1932 wv = xmalloc_widget_value ();
1933 wv->name = dialog_name;
1934
1935 /* Dialog boxes use a really stupid name encoding
1936 which specifies how many buttons to use
1937 and how many buttons are on the right.
1938 The Q means something also. */
1939 dialog_name[0] = 'Q';
1940 dialog_name[1] = '0' + nb_buttons;
1941 dialog_name[2] = 'B';
1942 dialog_name[3] = 'R';
1943 /* Number of buttons to put on the right. */
1944 dialog_name[4] = '0' + nb_buttons - left_count;
1945 dialog_name[5] = 0;
1946 wv->contents = first_wv;
1947 first_wv = wv;
1948 }
1949
1950 /* Actually create the dialog. */
1951 #ifdef HAVE_DIALOGS
1952 dialog_id = widget_id_tick++;
1953 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1954 f->output_data.w32->widget, 1, 0,
1955 dialog_selection_callback, 0);
1956 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
1957 #endif
1958
1959 /* Free the widget_value objects we used to specify the contents. */
1960 free_menubar_widget_value_tree (first_wv);
1961
1962 /* No selection has been chosen yet. */
1963 menu_item_selection = 0;
1964
1965 /* Display the menu. */
1966 #ifdef HAVE_DIALOGS
1967 lw_pop_up_all_widgets (dialog_id);
1968 popup_activated_flag = 1;
1969
1970 /* Process events that apply to the menu. */
1971 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
1972
1973 lw_destroy_all_widgets (dialog_id);
1974 #endif
1975
1976 /* Find the selected item, and its pane, to return
1977 the proper value. */
1978 if (menu_item_selection != 0)
1979 {
1980 Lisp_Object prefix;
1981
1982 prefix = Qnil;
1983 i = 0;
1984 while (i < menu_items_used)
1985 {
1986 Lisp_Object entry;
1987
1988 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1989 {
1990 prefix
1991 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1992 i += MENU_ITEMS_PANE_LENGTH;
1993 }
1994 else
1995 {
1996 entry
1997 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1998 if (menu_item_selection == i)
1999 {
2000 if (keymaps != 0)
2001 {
2002 entry = Fcons (entry, Qnil);
2003 if (!NILP (prefix))
2004 entry = Fcons (prefix, entry);
2005 }
2006 return entry;
2007 }
2008 i += MENU_ITEMS_ITEM_LENGTH;
2009 }
2010 }
2011 }
2012
2013 return Qnil;
2014 }
2015 \f
2016
2017 /* Is this item a separator? */
2018 static int
2019 name_is_separator (name)
2020 char *name;
2021 {
2022 /* Check if name string consists of only dashes ('-') */
2023 while (*name == '-') name++;
2024 return (*name == '\0');
2025 }
2026
2027
2028 /* Indicate boundary between left and right. */
2029 static int
2030 add_left_right_boundary (HMENU menu)
2031 {
2032 return AppendMenu (menu, MF_MENUBARBREAK, 0, NULL);
2033 }
2034
2035 static int
2036 add_menu_item (HMENU menu, widget_value *wv, HMENU item)
2037 {
2038 UINT fuFlags;
2039 char *out_string;
2040 int return_value;
2041
2042 if (name_is_separator (wv->name))
2043 fuFlags = MF_SEPARATOR;
2044 else
2045 {
2046 if (wv->enabled)
2047 fuFlags = MF_STRING;
2048 else
2049 fuFlags = MF_STRING | MF_GRAYED;
2050
2051 if (wv->key != NULL)
2052 {
2053 out_string = alloca (strlen (wv->name) + strlen (wv->key) + 2);
2054 strcpy (out_string, wv->name);
2055 strcat (out_string, "\t");
2056 strcat (out_string, wv->key);
2057 }
2058 else
2059 out_string = wv->name;
2060
2061 if (wv->title)
2062 {
2063 #if 0 /* no GC while popup menu is active */
2064 out_string = LocalAlloc (0, strlen (wv->name) + 1);
2065 strcpy (out_string, wv->name);
2066 #endif
2067 fuFlags = MF_OWNERDRAW | MF_DISABLED;
2068 }
2069
2070 /* Draw radio buttons and tickboxes. */
2071 {
2072 if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
2073 wv->button_type == BUTTON_TYPE_RADIO))
2074 fuFlags |= MF_CHECKED;
2075 else
2076 fuFlags |= MF_UNCHECKED;
2077 }
2078 }
2079 if (item != NULL)
2080 fuFlags = MF_POPUP;
2081
2082 return_value =
2083 AppendMenu (menu,
2084 fuFlags,
2085 item != NULL ? (UINT) item : (UINT) wv->call_data,
2086 (fuFlags == MF_SEPARATOR) ? NULL: out_string );
2087
2088 /* This must be done after the menu item is created. */
2089 {
2090 HMODULE user32 = GetModuleHandle ("user32.dll");
2091 FARPROC set_menu_item_info = GetProcAddress (user32, "SetMenuItemInfoA");
2092
2093 if (set_menu_item_info)
2094 {
2095 MENUITEMINFO info;
2096 bzero (&info, sizeof (info));
2097 info.cbSize = sizeof (info);
2098 info.fMask = MIIM_DATA;
2099 /* Set help string for menu item. */
2100 info.dwItemData = (DWORD)wv->help;
2101
2102 if (wv->button_type == BUTTON_TYPE_RADIO)
2103 {
2104 /* CheckMenuRadioItem allows us to differentiate TOGGLE and
2105 RADIO items, but is not available on NT 3.51 and earlier. */
2106 info.fMask |= MIIM_TYPE | MIIM_STATE;
2107 info.fType = MFT_RADIOCHECK;
2108 info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED;
2109 }
2110 set_menu_item_info (menu,
2111 item != NULL ? (UINT) item : (UINT) wv->call_data,
2112 FALSE, &info);
2113 }
2114 }
2115 return return_value;
2116 }
2117
2118 /* Construct native Windows menu(bar) based on widget_value tree. */
2119 static int
2120 fill_in_menu (HMENU menu, widget_value *wv)
2121 {
2122 int items_added = 0;
2123
2124 for ( ; wv != NULL; wv = wv->next)
2125 {
2126 if (wv->contents)
2127 {
2128 HMENU sub_menu = CreatePopupMenu ();
2129
2130 if (sub_menu == NULL)
2131 return 0;
2132
2133 if (!fill_in_menu (sub_menu, wv->contents) ||
2134 !add_menu_item (menu, wv, sub_menu))
2135 {
2136 DestroyMenu (sub_menu);
2137 return 0;
2138 }
2139 }
2140 else
2141 {
2142 if (!add_menu_item (menu, wv, NULL))
2143 return 0;
2144 }
2145 }
2146 return 1;
2147 }
2148
2149 int
2150 popup_activated ()
2151 {
2152 /* popup_activated_flag not actually used on W32 */
2153 return 0;
2154 }
2155
2156 /* Display help string for currently pointed to menu item. Not
2157 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
2158 available. */
2159 void
2160 w32_menu_display_help (HMENU menu, UINT item, UINT flags)
2161 {
2162 int pane = 0; /* NTEMACS_TODO: Set this to pane number. */
2163
2164 HMODULE user32 = GetModuleHandle ("user32.dll");
2165 FARPROC get_menu_item_info = GetProcAddress (user32, "GetMenuItemInfoA");
2166
2167 if (get_menu_item_info)
2168 {
2169 extern Lisp_Object Qmenu_item;
2170 Lisp_Object *first_item;
2171 Lisp_Object pane_name;
2172 Lisp_Object menu_object;
2173 MENUITEMINFO info;
2174
2175 bzero (&info, sizeof (info));
2176 info.cbSize = sizeof (info);
2177 info.fMask = MIIM_DATA;
2178 get_menu_item_info (menu, item, FALSE, &info);
2179
2180 first_item = XVECTOR (menu_items)->contents;
2181 if (EQ (first_item[0], Qt))
2182 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2183 else if (EQ (first_item[0], Qquote))
2184 /* This shouldn't happen, see w32_menu_show. */
2185 pane_name = build_string ("");
2186 else
2187 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2188
2189 /* (menu-item MENU-NAME PANE-NUMBER) */
2190 menu_object = Fcons (Qmenu_item,
2191 Fcons (pane_name,
2192 Fcons (make_number (pane), Qnil)));
2193
2194 show_help_echo (info.dwItemData ?
2195 build_string ((char *) info.dwItemData) : Qnil,
2196 Qnil, menu_object, make_number (item), 1);
2197 }
2198 }
2199
2200
2201
2202 #endif /* HAVE_MENUS */
2203 \f
2204 syms_of_w32menu ()
2205 {
2206 staticpro (&menu_items);
2207 menu_items = Qnil;
2208
2209 Qdebug_on_next_call = intern ("debug-on-next-call");
2210 staticpro (&Qdebug_on_next_call);
2211
2212 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2213 "Frame for which we are updating a menu.\n\
2214 The enable predicate for a menu command should check this variable.");
2215 Vmenu_updating_frame = Qnil;
2216
2217 defsubr (&Sx_popup_menu);
2218 #ifdef HAVE_MENUS
2219 defsubr (&Sx_popup_dialog);
2220 #endif
2221 }