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