Add Keywords header.
[bpt/emacs.git] / src / w32menu.c
CommitLineData
e9e23e23 1/* Menu support for GNU Emacs on the Microsoft W32 API.
68c45bf0 2 Copyright (C) 1986, 88, 93, 94, 96, 98, 1999 Free Software Foundation, Inc.
ee78dc32
GV
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
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
ee78dc32 20
ee78dc32 21#include <config.h>
68c45bf0 22#include <signal.h>
162d2499 23
ee78dc32
GV
24#include <stdio.h>
25#include "lisp.h"
26#include "termhooks.h"
15d36dee 27#include "keyboard.h"
ee78dc32
GV
28#include "frame.h"
29#include "window.h"
ee78dc32 30#include "blockinput.h"
fdc12c4d 31#include "buffer.h"
6915ded0
JR
32#include "charset.h"
33#include "coding.h"
ee78dc32
GV
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
162d2499 47#undef HAVE_MULTILINGUAL_MENU
222456a1 48#undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
162d2499 49
485015ca
GV
50/******************************************************************/
51/* Definitions copied from lwlib.h */
ee78dc32 52
485015ca
GV
53typedef void * XtPointer;
54typedef char Boolean;
55
162d2499
JR
56enum button_type
57{
58 BUTTON_TYPE_NONE,
59 BUTTON_TYPE_TOGGLE,
60 BUTTON_TYPE_RADIO
61};
c8869655 62
485015ca 63typedef struct _widget_value
ee78dc32 64{
485015ca
GV
65 /* name of widget */
66 char* name;
67 /* value (meaning depend on widget type) */
68 char* value;
69 /* keyboard equivalent. no implications for XtTranslations */
70 char* key;
6915ded0
JR
71 /* Help string or null if none. */
72 char *help;
485015ca
GV
73 /* true if enabled */
74 Boolean enabled;
75 /* true if selected */
76 Boolean selected;
162d2499
JR
77 /* The type of a button. */
78 enum button_type button_type;
485015ca
GV
79 /* true if menu title */
80 Boolean title;
81#if 0
82 /* true if was edited (maintained by get_value) */
83 Boolean edited;
84 /* true if has changed (maintained by lw library) */
85 change_type change;
86 /* true if this widget itself has changed,
87 but not counting the other widgets found in the `next' field. */
88 change_type this_one_change;
89#endif
90 /* Contents of the sub-widgets, also selected slot for checkbox */
91 struct _widget_value* contents;
92 /* data passed to callback */
93 XtPointer call_data;
94 /* next one in the list */
95 struct _widget_value* next;
96#if 0
97 /* slot for the toolkit dependent part. Always initialize to NULL. */
98 void* toolkit_data;
99 /* tell us if we should free the toolkit data slot when freeing the
100 widget_value itself. */
101 Boolean free_toolkit_data;
102
103 /* we resource the widget_value structures; this points to the next
104 one on the free list if this one has been deallocated.
105 */
106 struct _widget_value *free_list;
107#endif
108} widget_value;
109
110/* LocalAlloc/Free is a reasonably good allocator. */
111#define malloc_widget_value() (void*)LocalAlloc (LMEM_ZEROINIT, sizeof (widget_value))
112#define free_widget_value(wv) LocalFree (wv)
113
114/******************************************************************/
115
116#define min(x,y) (((x) < (y)) ? (x) : (y))
117#define max(x,y) (((x) > (y)) ? (x) : (y))
118
119#ifndef TRUE
120#define TRUE 1
121#define FALSE 0
122#endif /* no TRUE */
123
124Lisp_Object Vmenu_updating_frame;
ee78dc32 125
fdc12c4d
RS
126Lisp_Object Qdebug_on_next_call;
127
ee78dc32 128extern Lisp_Object Qmenu_bar;
485015ca
GV
129extern Lisp_Object Qmouse_click, Qevent_kind;
130
131extern Lisp_Object QCtoggle, QCradio;
ee78dc32 132
fdc12c4d
RS
133extern Lisp_Object Voverriding_local_map;
134extern Lisp_Object Voverriding_local_map_menu_flag;
135
136extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
137
138extern Lisp_Object Qmenu_bar_update_hook;
139
014510b0
GV
140void set_frame_menubar ();
141
162d2499
JR
142static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
143 Lisp_Object, Lisp_Object, Lisp_Object,
144 Lisp_Object, Lisp_Object));
fbd6baed 145static Lisp_Object w32_dialog_show ();
162d2499 146static Lisp_Object w32_menu_show ();
ee78dc32 147
485015ca
GV
148static void keymap_panes ();
149static void single_keymap_panes ();
150static void single_menu_item ();
151static void list_of_panes ();
152static void list_of_items ();
153\f
154/* This holds a Lisp vector that holds the results of decoding
155 the keymaps or alist-of-alists that specify a menu.
156
157 It describes the panes and items within the panes.
158
159 Each pane is described by 3 elements in the vector:
160 t, the pane name, the pane's prefix key.
161 Then follow the pane's items, with 5 elements per item:
162 the item string, the enable flag, the item's value,
163 the definition, and the equivalent keyboard key's description string.
164
165 In some cases, multiple levels of menus may be described.
166 A single vector slot containing nil indicates the start of a submenu.
167 A single vector slot containing lambda indicates the end of a submenu.
168 The submenu follows a menu item which is the way to reach the submenu.
169
170 A single vector slot containing quote indicates that the
171 following items should appear on the right of a dialog box.
172
173 Using a Lisp vector to hold this information while we decode it
174 takes care of protecting all the data from GC. */
175
176#define MENU_ITEMS_PANE_NAME 1
177#define MENU_ITEMS_PANE_PREFIX 2
178#define MENU_ITEMS_PANE_LENGTH 3
179
162d2499
JR
180enum menu_item_idx
181{
182 MENU_ITEMS_ITEM_NAME = 0,
183 MENU_ITEMS_ITEM_ENABLE,
184 MENU_ITEMS_ITEM_VALUE,
185 MENU_ITEMS_ITEM_EQUIV_KEY,
186 MENU_ITEMS_ITEM_DEFINITION,
187 MENU_ITEMS_ITEM_TYPE,
188 MENU_ITEMS_ITEM_SELECTED,
189 MENU_ITEMS_ITEM_HELP,
190 MENU_ITEMS_ITEM_LENGTH
191};
485015ca
GV
192
193static Lisp_Object menu_items;
194
195/* Number of slots currently allocated in menu_items. */
196static int menu_items_allocated;
197
198/* This is the index in menu_items of the first empty slot. */
199static int menu_items_used;
200
201/* The number of panes currently recorded in menu_items,
202 excluding those within submenus. */
203static int menu_items_n_panes;
204
205/* Current depth within submenus. */
206static int menu_items_submenu_depth;
207
208/* Flag which when set indicates a dialog or menu has been posted by
209 Xt on behalf of one of the widget sets. */
210static int popup_activated_flag;
211
162d2499
JR
212static int next_menubar_widget_id;
213
485015ca
GV
214/* This is set nonzero after the user activates the menu bar, and set
215 to zero again after the menu bars are redisplayed by prepare_menu_bar.
216 While it is nonzero, all calls to set_frame_menubar go deep.
217
218 I don't understand why this is needed, but it does seem to be
219 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
ee78dc32 220
485015ca
GV
221int pending_menu_activation;
222\f
223
224/* Return the frame whose ->output_data.w32->menubar_widget equals
162d2499 225 ID, or 0 if none. */
485015ca
GV
226
227static struct frame *
162d2499
JR
228menubar_id_to_frame (id)
229 HMENU id;
485015ca
GV
230{
231 Lisp_Object tail, frame;
232 FRAME_PTR f;
ee78dc32 233
8e713be6 234 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
485015ca 235 {
8e713be6 236 frame = XCAR (tail);
485015ca
GV
237 if (!GC_FRAMEP (frame))
238 continue;
239 f = XFRAME (frame);
162d2499 240 if (!FRAME_WINDOW_P (f))
485015ca 241 continue;
162d2499 242 if (f->output_data.w32->menubar_widget == id)
485015ca
GV
243 return f;
244 }
245 return 0;
246}
247\f
ee78dc32
GV
248/* Initialize the menu_items structure if we haven't already done so.
249 Also mark it as currently empty. */
250
485015ca
GV
251static void
252init_menu_items ()
ee78dc32 253{
485015ca 254 if (NILP (menu_items))
ee78dc32 255 {
485015ca
GV
256 menu_items_allocated = 60;
257 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
ee78dc32 258 }
485015ca
GV
259
260 menu_items_used = 0;
261 menu_items_n_panes = 0;
262 menu_items_submenu_depth = 0;
ee78dc32
GV
263}
264
485015ca
GV
265/* Call at the end of generating the data in menu_items.
266 This fills in the number of items in the last pane. */
ee78dc32 267
485015ca
GV
268static void
269finish_menu_items ()
ee78dc32 270{
ee78dc32 271}
014510b0
GV
272
273/* Call when finished using the data for the current menu
274 in menu_items. */
275
485015ca
GV
276static void
277discard_menu_items ()
014510b0 278{
485015ca
GV
279 /* Free the structure if it is especially large.
280 Otherwise, hold on to it, to save time. */
281 if (menu_items_allocated > 200)
014510b0 282 {
485015ca
GV
283 menu_items = Qnil;
284 menu_items_allocated = 0;
014510b0 285 }
014510b0
GV
286}
287
485015ca 288/* Make the menu_items vector twice as large. */
ee78dc32 289
485015ca
GV
290static void
291grow_menu_items ()
ee78dc32 292{
485015ca
GV
293 Lisp_Object old;
294 int old_size = menu_items_allocated;
295 old = menu_items;
296
297 menu_items_allocated *= 2;
298 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
299 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
300 old_size * sizeof (Lisp_Object));
ee78dc32
GV
301}
302
485015ca 303/* Begin a submenu. */
014510b0 304
485015ca
GV
305static void
306push_submenu_start ()
307{
308 if (menu_items_used + 1 > menu_items_allocated)
309 grow_menu_items ();
014510b0 310
485015ca
GV
311 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
312 menu_items_submenu_depth++;
ee78dc32 313}
485015ca
GV
314
315/* End a submenu. */
316
317static void
318push_submenu_end ()
ee78dc32 319{
485015ca
GV
320 if (menu_items_used + 1 > menu_items_allocated)
321 grow_menu_items ();
ee78dc32 322
485015ca
GV
323 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
324 menu_items_submenu_depth--;
ee78dc32
GV
325}
326
485015ca 327/* Indicate boundary between left and right. */
ee78dc32 328
485015ca
GV
329static void
330push_left_right_boundary ()
ee78dc32 331{
485015ca
GV
332 if (menu_items_used + 1 > menu_items_allocated)
333 grow_menu_items ();
334
335 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
ee78dc32
GV
336}
337
485015ca
GV
338/* Start a new menu pane in menu_items..
339 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
ee78dc32 340
485015ca
GV
341static void
342push_menu_pane (name, prefix_vec)
343 Lisp_Object name, prefix_vec;
ee78dc32 344{
485015ca
GV
345 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
346 grow_menu_items ();
347
348 if (menu_items_submenu_depth == 0)
349 menu_items_n_panes++;
350 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
351 XVECTOR (menu_items)->contents[menu_items_used++] = name;
352 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
353}
ee78dc32 354
162d2499
JR
355/* Push one menu item into the current pane. NAME is the string to
356 display. ENABLE if non-nil means this item can be selected. KEY
357 is the key generated by choosing this item, or nil if this item
358 doesn't really have a definition. DEF is the definition of this
359 item. EQUIV is the textual description of the keyboard equivalent
360 for this item (or nil if none). TYPE is the type of this menu
361 item, one of nil, `toggle' or `radio'. */
485015ca
GV
362
363static void
162d2499
JR
364push_menu_item (name, enable, key, def, equiv, type, selected, help)
365 Lisp_Object name, enable, key, def, equiv, type, selected, help;
485015ca
GV
366{
367 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
368 grow_menu_items ();
369
370 XVECTOR (menu_items)->contents[menu_items_used++] = name;
371 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
372 XVECTOR (menu_items)->contents[menu_items_used++] = key;
373 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
374 XVECTOR (menu_items)->contents[menu_items_used++] = def;
162d2499
JR
375 XVECTOR (menu_items)->contents[menu_items_used++] = type;
376 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
377 XVECTOR (menu_items)->contents[menu_items_used++] = help;
ee78dc32
GV
378}
379\f
380/* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
381 and generate menu panes for them in menu_items.
382 If NOTREAL is nonzero,
383 don't bother really computing whether an item is enabled. */
384
485015ca
GV
385static void
386keymap_panes (keymaps, nmaps, notreal)
ee78dc32
GV
387 Lisp_Object *keymaps;
388 int nmaps;
389 int notreal;
390{
391 int mapno;
014510b0 392
485015ca 393 init_menu_items ();
014510b0 394
485015ca
GV
395 /* Loop over the given keymaps, making a pane for each map.
396 But don't make a pane that is empty--ignore that map instead.
397 P is the number of panes we have made so far. */
398 for (mapno = 0; mapno < nmaps; mapno++)
e5cd3d11
JR
399 single_keymap_panes (keymaps[mapno],
400 map_prompt (keymaps[mapno]), Qnil, notreal, 10);
ee78dc32 401
485015ca 402 finish_menu_items ();
ee78dc32
GV
403}
404
405/* This is a recursive subroutine of keymap_panes.
406 It handles one keymap, KEYMAP.
407 The other arguments are passed along
408 or point to local variables of the previous function.
485015ca
GV
409 If NOTREAL is nonzero, only check for equivalent key bindings, don't
410 evaluate expressions in menu items and don't make any menu.
ee78dc32 411
485015ca
GV
412 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
413
414static void
415single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
ee78dc32
GV
416 Lisp_Object keymap;
417 Lisp_Object pane_name;
418 Lisp_Object prefix;
419 int notreal;
485015ca 420 int maxdepth;
ee78dc32 421{
485015ca
GV
422 Lisp_Object pending_maps = Qnil;
423 Lisp_Object tail, item;
424 struct gcpro gcpro1, gcpro2;
485015ca
GV
425
426 if (maxdepth <= 0)
427 return;
428
429 push_menu_pane (pane_name, prefix);
430
8e713be6 431 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
ee78dc32 432 {
485015ca
GV
433 GCPRO2 (keymap, pending_maps);
434 /* Look at each key binding, and if it is a menu item add it
435 to this menu. */
8e713be6 436 item = XCAR (tail);
ee78dc32 437 if (CONSP (item))
8e713be6 438 single_menu_item (XCAR (item), XCDR (item),
37ad8b63 439 &pending_maps, notreal, maxdepth);
ee78dc32
GV
440 else if (VECTORP (item))
441 {
442 /* Loop over the char values represented in the vector. */
443 int len = XVECTOR (item)->size;
444 int c;
445 for (c = 0; c < len; c++)
446 {
447 Lisp_Object character;
448 XSETFASTINT (character, c);
485015ca 449 single_menu_item (character, XVECTOR (item)->contents[c],
37ad8b63 450 &pending_maps, notreal, maxdepth);
ee78dc32
GV
451 }
452 }
485015ca 453 UNGCPRO;
ee78dc32
GV
454 }
455
456 /* Process now any submenus which want to be panes at this level. */
457 while (!NILP (pending_maps))
458 {
459 Lisp_Object elt, eltcdr, string;
460 elt = Fcar (pending_maps);
8e713be6
KR
461 eltcdr = XCDR (elt);
462 string = XCAR (eltcdr);
ee78dc32 463 /* We no longer discard the @ from the beginning of the string here.
485015ca
GV
464 Instead, we do this in w32_menu_show. */
465 single_keymap_panes (Fcar (elt), string,
8e713be6 466 XCDR (eltcdr), notreal, maxdepth - 1);
485015ca
GV
467 pending_maps = Fcdr (pending_maps);
468 }
469}
470\f
471/* This is a subroutine of single_keymap_panes that handles one
472 keymap entry.
473 KEY is a key in a keymap and ITEM is its binding.
474 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
475 separate panes.
476 If NOTREAL is nonzero, only check for equivalent key bindings, don't
477 evaluate expressions in menu items and don't make any menu.
37ad8b63 478 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
485015ca
GV
479
480static void
37ad8b63 481single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth)
485015ca
GV
482 Lisp_Object key, item;
483 Lisp_Object *pending_maps_ptr;
484 int maxdepth, notreal;
485015ca 485{
162d2499 486 Lisp_Object map, item_string, enabled;
485015ca
GV
487 struct gcpro gcpro1, gcpro2;
488 int res;
489
490 /* Parse the menu item and leave the result in item_properties. */
491 GCPRO2 (key, item);
492 res = parse_menu_item (item, notreal, 0);
493 UNGCPRO;
494 if (!res)
495 return; /* Not a menu item. */
496
497 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
498
499 if (notreal)
500 {
501 /* We don't want to make a menu, just traverse the keymaps to
502 precompute equivalent key bindings. */
503 if (!NILP (map))
504 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
505 return;
506 }
507
508 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
509 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
510
511 if (!NILP (map) && XSTRING (item_string)->data[0] == '@')
512 {
513 if (!NILP (enabled))
514 /* An enabled separate pane. Remember this to handle it later. */
515 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
516 *pending_maps_ptr);
517 return;
518 }
519
485015ca
GV
520 push_menu_item (item_string, enabled, key,
521 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
162d2499
JR
522 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
523 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
524 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
525 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
485015ca 526
485015ca
GV
527 /* Display a submenu using the toolkit. */
528 if (! (NILP (map) || NILP (enabled)))
529 {
530 push_submenu_start ();
531 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
532 push_submenu_end ();
ee78dc32 533 }
ee78dc32
GV
534}
535\f
8e6208c5 536/* Push all the panes and items of a menu described by the
ee78dc32
GV
537 alist-of-alists MENU.
538 This handles old-fashioned calls to x-popup-menu. */
539
485015ca
GV
540static void
541list_of_panes (menu)
ee78dc32
GV
542 Lisp_Object menu;
543{
544 Lisp_Object tail;
014510b0 545
485015ca 546 init_menu_items ();
014510b0 547
485015ca 548 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
ee78dc32
GV
549 {
550 Lisp_Object elt, pane_name, pane_data;
485015ca 551 elt = Fcar (tail);
ee78dc32
GV
552 pane_name = Fcar (elt);
553 CHECK_STRING (pane_name, 0);
485015ca 554 push_menu_pane (pane_name, Qnil);
ee78dc32
GV
555 pane_data = Fcdr (elt);
556 CHECK_CONS (pane_data, 0);
485015ca 557 list_of_items (pane_data);
ee78dc32 558 }
485015ca
GV
559
560 finish_menu_items ();
ee78dc32
GV
561}
562
563/* Push the items in a single pane defined by the alist PANE. */
564
485015ca
GV
565static void
566list_of_items (pane)
ee78dc32
GV
567 Lisp_Object pane;
568{
569 Lisp_Object tail, item, item1;
ee78dc32 570
ee78dc32
GV
571 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
572 {
573 item = Fcar (tail);
574 if (STRINGP (item))
162d2499 575 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil);
ee78dc32 576 else if (NILP (item))
485015ca 577 push_left_right_boundary ();
ee78dc32
GV
578 else
579 {
580 CHECK_CONS (item, 0);
581 item1 = Fcar (item);
582 CHECK_STRING (item1, 1);
162d2499 583 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil);
ee78dc32
GV
584 }
585 }
ee78dc32
GV
586}
587\f
485015ca
GV
588DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
589 "Pop up a deck-of-cards menu and return user's selection.\n\
590POSITION is a position specification. This is either a mouse button event\n\
591or a list ((XOFFSET YOFFSET) WINDOW)\n\
592where XOFFSET and YOFFSET are positions in pixels from the top left\n\
593corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
594This controls the position of the center of the first line\n\
595in the first pane of the menu, not the top left of the menu as a whole.\n\
596If POSITION is t, it means to use the current mouse position.\n\
597\n\
598MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
599The menu items come from key bindings that have a menu string as well as\n\
600a definition; actually, the \"definition\" in such a key binding looks like\n\
601\(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
602the keymap as a top-level element.\n\n\
603If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
604Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
605\n\
606You can also use a list of keymaps as MENU.\n\
607 Then each keymap makes a separate pane.\n\
608When MENU is a keymap or a list of keymaps, the return value\n\
609is a list of events.\n\n\
610\n\
611Alternatively, you can specify a menu of multiple panes\n\
612 with a list of the form (TITLE PANE1 PANE2...),\n\
613where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
614Each ITEM is normally a cons cell (STRING . VALUE);\n\
615but a string can appear as an item--that makes a nonselectable line\n\
616in the menu.\n\
617With this form of menu, the return value is VALUE from the chosen item.\n\
618\n\
619If POSITION is nil, don't display the menu at all, just precalculate the\n\
620cached information about equivalent key sequences.")
621 (position, menu)
622 Lisp_Object position, menu;
ee78dc32 623{
ee78dc32
GV
624 Lisp_Object keymap, tem;
625 int xpos, ypos;
626 Lisp_Object title;
627 char *error_name;
628 Lisp_Object selection;
ee78dc32
GV
629 FRAME_PTR f;
630 Lisp_Object x, y, window;
631 int keymaps = 0;
485015ca 632 int for_click = 0;
ee78dc32 633 struct gcpro gcpro1;
485015ca
GV
634
635#ifdef HAVE_MENUS
ee78dc32
GV
636 if (! NILP (position))
637 {
485015ca
GV
638 check_w32 ();
639
ee78dc32 640 /* Decode the first argument: find the window and the coordinates. */
ef06882d 641 if (EQ (position, Qt)
0b6bb670
JR
642 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
643 || EQ (XCAR (position), Qtool_bar))))
ee78dc32
GV
644 {
645 /* Use the mouse's current position. */
162d2499 646 FRAME_PTR new_f = SELECTED_FRAME ();
ee78dc32 647 Lisp_Object bar_window;
162d2499 648 enum scroll_bar_part part;
ee78dc32 649 unsigned long time;
485015ca 650
ee78dc32 651 if (mouse_position_hook)
485015ca
GV
652 (*mouse_position_hook) (&new_f, 1, &bar_window,
653 &part, &x, &y, &time);
ee78dc32
GV
654 if (new_f != 0)
655 XSETFRAME (window, new_f);
656 else
657 {
658 window = selected_window;
659 XSETFASTINT (x, 0);
660 XSETFASTINT (y, 0);
661 }
662 }
663 else
664 {
665 tem = Fcar (position);
666 if (CONSP (tem))
667 {
668 window = Fcar (Fcdr (position));
669 x = Fcar (tem);
670 y = Fcar (Fcdr (tem));
671 }
672 else
673 {
485015ca
GV
674 for_click = 1;
675 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
676 window = Fcar (tem); /* POSN_WINDOW (tem) */
ee78dc32
GV
677 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
678 x = Fcar (tem);
679 y = Fcdr (tem);
ee78dc32
GV
680 }
681 }
485015ca 682
ee78dc32
GV
683 CHECK_NUMBER (x, 0);
684 CHECK_NUMBER (y, 0);
685
686 /* Decode where to put the menu. */
485015ca 687
ee78dc32
GV
688 if (FRAMEP (window))
689 {
690 f = XFRAME (window);
ee78dc32
GV
691 xpos = 0;
692 ypos = 0;
693 }
694 else if (WINDOWP (window))
695 {
696 CHECK_LIVE_WINDOW (window, 0);
697 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
485015ca 698
162d2499 699 xpos = (FONT_WIDTH (FRAME_FONT (f))
485015ca 700 * XFASTINT (XWINDOW (window)->left));
162d2499 701 ypos = (FRAME_LINE_HEIGHT (f)
485015ca 702 * XFASTINT (XWINDOW (window)->top));
ee78dc32
GV
703 }
704 else
705 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
706 but I don't want to make one now. */
707 CHECK_WINDOW (window, 0);
485015ca 708
ee78dc32
GV
709 xpos += XINT (x);
710 ypos += XINT (y);
c8869655
GV
711
712 XSETFRAME (Vmenu_updating_frame, f);
ee78dc32 713 }
c8869655 714 Vmenu_updating_frame = Qnil;
485015ca 715#endif /* HAVE_MENUS */
c8869655 716
ee78dc32
GV
717 title = Qnil;
718 GCPRO1 (title);
485015ca
GV
719
720 /* Decode the menu items from what was specified. */
721
02067692
SM
722 keymap = get_keymap (menu, 0, 0);
723 if (CONSP (keymap))
485015ca
GV
724 {
725 /* We were given a keymap. Extract menu info from the keymap. */
726 Lisp_Object prompt;
485015ca
GV
727
728 /* Extract the detailed info to make one pane. */
729 keymap_panes (&menu, 1, NILP (position));
730
731 /* Search for a string appearing directly as an element of the keymap.
732 That string is the title of the menu. */
733 prompt = map_prompt (keymap);
734 if (NILP (title) && !NILP (prompt))
735 title = prompt;
736
737 /* Make that be the pane title of the first pane. */
738 if (!NILP (prompt) && menu_items_n_panes >= 0)
739 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
740
741 keymaps = 1;
742 }
02067692 743 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
485015ca
GV
744 {
745 /* We were given a list of keymaps. */
746 int nmaps = XFASTINT (Flength (menu));
747 Lisp_Object *maps
748 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
749 int i;
750
751 title = Qnil;
752
753 /* The first keymap that has a prompt string
754 supplies the menu title. */
755 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
756 {
757 Lisp_Object prompt;
758
02067692 759 maps[i++] = keymap = get_keymap (Fcar (tem), 1, 0);
485015ca
GV
760
761 prompt = map_prompt (keymap);
762 if (NILP (title) && !NILP (prompt))
763 title = prompt;
764 }
765
766 /* Extract the detailed info to make one pane. */
767 keymap_panes (maps, nmaps, NILP (position));
768
769 /* Make the title be the pane title of the first pane. */
770 if (!NILP (title) && menu_items_n_panes >= 0)
771 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
772
773 keymaps = 1;
774 }
775 else
776 {
777 /* We were given an old-fashioned menu. */
778 title = Fcar (menu);
779 CHECK_STRING (title, 1);
780
781 list_of_panes (Fcdr (menu));
782
783 keymaps = 0;
784 }
ee78dc32
GV
785
786 if (NILP (position))
787 {
485015ca 788 discard_menu_items ();
ee78dc32
GV
789 UNGCPRO;
790 return Qnil;
791 }
485015ca
GV
792
793#ifdef HAVE_MENUS
ee78dc32
GV
794 /* Display them in a menu. */
795 BLOCK_INPUT;
485015ca
GV
796
797 selection = w32_menu_show (f, xpos, ypos, for_click,
798 keymaps, title, &error_name);
ee78dc32 799 UNBLOCK_INPUT;
485015ca
GV
800
801 discard_menu_items ();
802
ee78dc32 803 UNGCPRO;
485015ca
GV
804#endif /* HAVE_MENUS */
805
ee78dc32
GV
806 if (error_name) error (error_name);
807 return selection;
808}
809
485015ca
GV
810#ifdef HAVE_MENUS
811
ee78dc32 812DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
485015ca 813 "Pop up a dialog box and return user's selection.\n\
ee78dc32
GV
814POSITION specifies which frame to use.\n\
815This is normally a mouse button event or a window or frame.\n\
816If POSITION is t, it means to use the frame the mouse is on.\n\
817The dialog box appears in the middle of the specified frame.\n\
818\n\
819CONTENTS specifies the alternatives to display in the dialog box.\n\
820It is a list of the form (TITLE ITEM1 ITEM2...).\n\
821Each ITEM is a cons cell (STRING . VALUE).\n\
822The return value is VALUE from the chosen item.\n\n\
823An ITEM may also be just a string--that makes a nonselectable item.\n\
824An ITEM may also be nil--that means to put all preceding items\n\
825on the left of the dialog box and all following items on the right.\n\
826\(By default, approximately half appear on each side.)")
485015ca
GV
827 (position, contents)
828 Lisp_Object position, contents;
ee78dc32
GV
829{
830 FRAME_PTR f;
831 Lisp_Object window;
485015ca
GV
832
833 check_w32 ();
834
ee78dc32 835 /* Decode the first argument: find the window or frame to use. */
485015ca 836 if (EQ (position, Qt)
0b6bb670
JR
837 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
838 || EQ (XCAR (position), Qtool_bar))))
ee78dc32 839 {
485015ca
GV
840#if 0 /* Using the frame the mouse is on may not be right. */
841 /* Use the mouse's current position. */
162d2499 842 FRAME_PTR new_f = SELECTED_FRAME ();
485015ca 843 Lisp_Object bar_window;
15d36dee 844 enum scroll_bar_part part;
485015ca
GV
845 unsigned long time;
846 Lisp_Object x, y;
847
848 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
849
850 if (new_f != 0)
851 XSETFRAME (window, new_f);
852 else
ee78dc32 853 window = selected_window;
485015ca
GV
854#endif
855 window = selected_window;
ee78dc32
GV
856 }
857 else if (CONSP (position))
858 {
859 Lisp_Object tem;
860 tem = Fcar (position);
485015ca 861 if (CONSP (tem))
ee78dc32
GV
862 window = Fcar (Fcdr (position));
863 else
864 {
485015ca
GV
865 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
866 window = Fcar (tem); /* POSN_WINDOW (tem) */
ee78dc32
GV
867 }
868 }
869 else if (WINDOWP (position) || FRAMEP (position))
870 window = position;
485015ca
GV
871 else
872 window = Qnil;
873
ee78dc32 874 /* Decode where to put the menu. */
485015ca 875
ee78dc32
GV
876 if (FRAMEP (window))
877 f = XFRAME (window);
878 else if (WINDOWP (window))
879 {
880 CHECK_LIVE_WINDOW (window, 0);
881 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
882 }
883 else
884 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
885 but I don't want to make one now. */
886 CHECK_WINDOW (window, 0);
485015ca 887
162d2499 888#ifndef HAVE_DIALOGS
ee78dc32
GV
889 /* Display a menu with these alternatives
890 in the middle of frame F. */
891 {
892 Lisp_Object x, y, frame, newpos;
893 XSETFRAME (frame, f);
894 XSETINT (x, x_pixel_width (f) / 2);
895 XSETINT (y, x_pixel_height (f) / 2);
896 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
897
898 return Fx_popup_menu (newpos,
899 Fcons (Fcar (contents), Fcons (contents, Qnil)));
900 }
162d2499 901#else /* HAVE_DIALOGS */
ee78dc32
GV
902 {
903 Lisp_Object title;
904 char *error_name;
905 Lisp_Object selection;
906
907 /* Decode the dialog items from what was specified. */
908 title = Fcar (contents);
909 CHECK_STRING (title, 1);
910
911 list_of_panes (Fcons (contents, Qnil));
912
913 /* Display them in a dialog box. */
914 BLOCK_INPUT;
485015ca 915 selection = w32_dialog_show (f, 0, title, &error_name);
ee78dc32
GV
916 UNBLOCK_INPUT;
917
918 discard_menu_items ();
919
920 if (error_name) error (error_name);
921 return selection;
922 }
162d2499 923#endif /* HAVE_DIALOGS */
ee78dc32
GV
924}
925
014510b0
GV
926/* Activate the menu bar of frame F.
927 This is called from keyboard.c when it gets the
928 menu_bar_activate_event out of the Emacs event queue.
929
930 To activate the menu bar, we signal to the input thread that it can
931 return from the WM_INITMENU message, allowing the normal Windows
932 processing of the menus.
933
934 But first we recompute the menu bar contents (the whole tree).
935
936 This way we can safely execute Lisp code. */
937
162d2499 938void
014510b0
GV
939x_activate_menubar (f)
940 FRAME_PTR f;
941{
942 set_frame_menubar (f, 0, 1);
943
944 /* Lock out further menubar changes while active. */
945 f->output_data.w32->menubar_active = 1;
946
947 /* Signal input thread to return from WM_INITMENU. */
948 complete_deferred_msg (FRAME_W32_WINDOW (f), WM_INITMENU, 0);
949}
950
485015ca
GV
951/* This callback is called from the menu bar pulldown menu
952 when the user makes a selection.
953 Figure out what the user chose
954 and put the appropriate events into the keyboard buffer. */
955
956void
957menubar_selection_callback (FRAME_PTR f, void * client_data)
ee78dc32 958{
485015ca
GV
959 Lisp_Object prefix, entry;
960 Lisp_Object vector;
961 Lisp_Object *subprefix_stack;
962 int submenu_depth = 0;
ee78dc32 963 int i;
fdc12c4d 964
485015ca 965 if (!f)
014510b0 966 return;
fe5a0162 967 entry = Qnil;
485015ca
GV
968 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
969 vector = f->menu_bar_vector;
970 prefix = Qnil;
971 i = 0;
972 while (i < f->menu_bar_items_used)
973 {
974 if (EQ (XVECTOR (vector)->contents[i], Qnil))
975 {
976 subprefix_stack[submenu_depth++] = prefix;
977 prefix = entry;
978 i++;
979 }
980 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
981 {
982 prefix = subprefix_stack[--submenu_depth];
983 i++;
984 }
985 else if (EQ (XVECTOR (vector)->contents[i], Qt))
986 {
987 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
988 i += MENU_ITEMS_PANE_LENGTH;
989 }
990 else
991 {
992 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
993 /* The EMACS_INT cast avoids a warning. There's no problem
994 as long as pointers have enough bits to hold small integers. */
995 if ((int) (EMACS_INT) client_data == i)
996 {
997 int j;
998 struct input_event buf;
999 Lisp_Object frame;
014510b0 1000
485015ca 1001 XSETFRAME (frame, f);
20501278
GM
1002 buf.kind = MENU_BAR_EVENT;
1003 buf.frame_or_window = frame;
1004 buf.arg = frame;
485015ca 1005 kbd_buffer_store_event (&buf);
014510b0 1006
485015ca
GV
1007 for (j = 0; j < submenu_depth; j++)
1008 if (!NILP (subprefix_stack[j]))
1009 {
20501278
GM
1010 buf.kind = MENU_BAR_EVENT;
1011 buf.frame_or_window = frame;
1012 buf.arg = subprefix_stack[j];
485015ca
GV
1013 kbd_buffer_store_event (&buf);
1014 }
fdc12c4d 1015
485015ca
GV
1016 if (!NILP (prefix))
1017 {
20501278
GM
1018 buf.kind = MENU_BAR_EVENT;
1019 buf.frame_or_window = frame;
1020 buf.arg = prefix;
485015ca
GV
1021 kbd_buffer_store_event (&buf);
1022 }
fdc12c4d 1023
20501278
GM
1024 buf.kind = MENU_BAR_EVENT;
1025 buf.frame_or_window = frame;
1026 buf.arg = entry;
485015ca 1027 kbd_buffer_store_event (&buf);
fdc12c4d 1028
485015ca
GV
1029 return;
1030 }
1031 i += MENU_ITEMS_ITEM_LENGTH;
1032 }
014510b0 1033 }
485015ca 1034}
014510b0 1035
485015ca 1036/* Allocate a widget_value, blocking input. */
014510b0 1037
485015ca
GV
1038widget_value *
1039xmalloc_widget_value ()
1040{
1041 widget_value *value;
014510b0 1042
fdc12c4d 1043 BLOCK_INPUT;
485015ca 1044 value = malloc_widget_value ();
ee78dc32 1045 UNBLOCK_INPUT;
485015ca
GV
1046
1047 return value;
ee78dc32
GV
1048}
1049
485015ca
GV
1050/* This recursively calls free_widget_value on the tree of widgets.
1051 It must free all data that was malloc'ed for these widget_values.
1052 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1053 must be left alone. */
1054
1055void
1056free_menubar_widget_value_tree (wv)
1057 widget_value *wv;
ee78dc32 1058{
485015ca 1059 if (! wv) return;
ee78dc32 1060
485015ca
GV
1061 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1062
1063 if (wv->contents && (wv->contents != (widget_value*)1))
1064 {
1065 free_menubar_widget_value_tree (wv->contents);
1066 wv->contents = (widget_value *) 0xDEADBEEF;
1067 }
1068 if (wv->next)
1069 {
1070 free_menubar_widget_value_tree (wv->next);
1071 wv->next = (widget_value *) 0xDEADBEEF;
1072 }
1073 BLOCK_INPUT;
1074 free_widget_value (wv);
ee78dc32
GV
1075 UNBLOCK_INPUT;
1076}
ee78dc32 1077\f
485015ca
GV
1078/* Return a tree of widget_value structures for a menu bar item
1079 whose event type is ITEM_KEY (with string ITEM_NAME)
1080 and whose contents come from the list of keymaps MAPS. */
ee78dc32 1081
485015ca
GV
1082static widget_value *
1083single_submenu (item_key, item_name, maps)
1084 Lisp_Object item_key, item_name, maps;
ee78dc32 1085{
485015ca
GV
1086 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1087 int i;
1088 int submenu_depth = 0;
1089 Lisp_Object length;
1090 int len;
1091 Lisp_Object *mapvec;
1092 widget_value **submenu_stack;
485015ca
GV
1093 int previous_items = menu_items_used;
1094 int top_level_items = 0;
ee78dc32 1095
485015ca
GV
1096 length = Flength (maps);
1097 len = XINT (length);
ee78dc32 1098
485015ca
GV
1099 /* Convert the list MAPS into a vector MAPVEC. */
1100 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1101 for (i = 0; i < len; i++)
ee78dc32 1102 {
485015ca
GV
1103 mapvec[i] = Fcar (maps);
1104 maps = Fcdr (maps);
ee78dc32
GV
1105 }
1106
485015ca 1107 menu_items_n_panes = 0;
ee78dc32 1108
485015ca
GV
1109 /* Loop over the given keymaps, making a pane for each map.
1110 But don't make a pane that is empty--ignore that map instead. */
1111 for (i = 0; i < len; i++)
ee78dc32 1112 {
485015ca 1113 if (SYMBOLP (mapvec[i])
02067692 1114 || (CONSP (mapvec[i]) && !KEYMAPP (mapvec[i])))
485015ca
GV
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);
162d2499
JR
1120 push_menu_item (item_name, Qt, item_key, mapvec[i],
1121 Qnil, Qnil, Qnil, Qnil);
485015ca
GV
1122 }
1123 else
1124 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
ee78dc32 1125 }
485015ca
GV
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;
162d2499 1136 wv->button_type = BUTTON_TYPE_NONE;
485015ca
GV
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;
ee78dc32
GV
1146 while (i < menu_items_used)
1147 {
1148 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1149 {
485015ca
GV
1150 submenu_stack[submenu_depth++] = save_wv;
1151 save_wv = prev_wv;
1152 prev_wv = 0;
ee78dc32
GV
1153 i++;
1154 }
1155 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1156 {
485015ca
GV
1157 prev_wv = save_wv;
1158 save_wv = submenu_stack[--submenu_depth];
ee78dc32
GV
1159 i++;
1160 }
485015ca
GV
1161 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1162 && submenu_depth != 0)
1163 i += MENU_ITEMS_PANE_LENGTH;
ee78dc32
GV
1164 /* Ignore a nil in the item list.
1165 It's meaningful only for dialog boxes. */
485015ca
GV
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];
162d2499
JR
1175#ifndef HAVE_MULTILINGUAL_MENU
1176 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
6915ded0 1177 pane_name = ENCODE_SYSTEM (pane_name);
162d2499 1178#endif
485015ca
GV
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;
162d2499 1203 wv->button_type = BUTTON_TYPE_NONE;
485015ca
GV
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. */
162d2499
JR
1212 Lisp_Object item_name, enable, descrip, def, type, selected;
1213 Lisp_Object help;
7d8e57da 1214
485015ca
GV
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];
162d2499
JR
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))
6915ded0 1226 item_name = ENCODE_SYSTEM (item_name);
162d2499 1227 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
6915ded0 1228 descrip = ENCODE_SYSTEM (descrip);
162d2499 1229#endif
485015ca
GV
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);
162d2499
JR
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))
7d8e57da
JR
1257 wv->help = (char *) XSTRING (help)->data;
1258 else
1259 wv->help = NULL;
1260
485015ca
GV
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;
162d2499 1290 Lisp_Object items;
485015ca
GV
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;
162d2499 1309 wv->button_type = BUTTON_TYPE_NONE;
485015ca
GV
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. */
c53b6500 1345 safe_run_hooks (Qactivate_menubar_hook);
485015ca
GV
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;
162d2499 1382 wv->button_type = BUTTON_TYPE_NONE;
485015ca
GV
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
162d2499 1426 just the top level menu bar strings. */
485015ca
GV
1427
1428 items = FRAME_MENU_BAR_ITEMS (f);
485015ca
GV
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;
162d2499 1441 wv->button_type = BUTTON_TYPE_NONE;
485015ca
GV
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
162d2499
JR
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;
485015ca
GV
1459 }
1460
1461 /* Create or update the menu bar widget. */
ee78dc32 1462
485015ca 1463 BLOCK_INPUT;
ee78dc32 1464
485015ca
GV
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);
ee78dc32 1476
485015ca 1477 free_menubar_widget_value_tree (first_wv);
ee78dc32 1478
485015ca
GV
1479 {
1480 HMENU old_widget = f->output_data.w32->menubar_widget;
ee78dc32 1481
485015ca
GV
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)); */
ee78dc32 1486
485015ca
GV
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));
ee78dc32 1491 }
485015ca
GV
1492
1493 UNBLOCK_INPUT;
ee78dc32
GV
1494}
1495
485015ca
GV
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);
ee78dc32
GV
1509}
1510
485015ca
GV
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}
ee78dc32 1529
485015ca
GV
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. */
ee78dc32
GV
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.
485015ca 1538 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
ee78dc32 1539 KEYMAPS is 1 if this menu was specified with keymaps;
485015ca
GV
1540 in that case, we return a list containing the chosen item's value
1541 and perhaps also the pane's prefix.
ee78dc32
GV
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
485015ca
GV
1546static Lisp_Object
1547w32_menu_show (f, x, y, for_click, keymaps, title, error)
ee78dc32
GV
1548 FRAME_PTR f;
1549 int x;
1550 int y;
485015ca
GV
1551 int for_click;
1552 int keymaps;
1553 Lisp_Object title;
ee78dc32
GV
1554 char **error;
1555{
485015ca
GV
1556 int i;
1557 int menu_item_selection;
1558 HMENU menu;
ee78dc32 1559 POINT pos;
485015ca
GV
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;
485015ca 1566 int first_pane;
485015ca 1567
ee78dc32 1568 *error = NULL;
485015ca
GV
1569
1570 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
ee78dc32
GV
1571 {
1572 *error = "Empty menu";
1573 return Qnil;
1574 }
485015ca
GV
1575
1576 /* Create a tree of widget_value objects
1577 representing the panes and their items. */
1578 wv = xmalloc_widget_value ();
1579 wv->name = "menu";
1580 wv->value = 0;
1581 wv->enabled = 1;
162d2499 1582 wv->button_type = BUTTON_TYPE_NONE;
485015ca
GV
1583 first_wv = wv;
1584 first_pane = 1;
1585
1586 /* Loop over all panes and items, filling in the tree. */
1587 i = 0;
1588 while (i < menu_items_used)
1589 {
1590 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1591 {
1592 submenu_stack[submenu_depth++] = save_wv;
1593 save_wv = prev_wv;
1594 prev_wv = 0;
1595 first_pane = 1;
1596 i++;
1597 }
1598 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1599 {
1600 prev_wv = save_wv;
1601 save_wv = submenu_stack[--submenu_depth];
1602 first_pane = 0;
1603 i++;
1604 }
1605 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1606 && submenu_depth != 0)
1607 i += MENU_ITEMS_PANE_LENGTH;
1608 /* Ignore a nil in the item list.
1609 It's meaningful only for dialog boxes. */
1610 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1611 i += 1;
1612 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1613 {
1614 /* Create a new pane. */
1615 Lisp_Object pane_name, prefix;
1616 char *pane_string;
1617 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1618 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
162d2499
JR
1619#ifndef HAVE_MULTILINGUAL_MENU
1620 if (!NILP (pane_name) && STRING_MULTIBYTE (pane_name))
6915ded0 1621 pane_name = ENCODE_SYSTEM (pane_name);
162d2499 1622#endif
485015ca
GV
1623 pane_string = (NILP (pane_name)
1624 ? "" : (char *) XSTRING (pane_name)->data);
1625 /* If there is just one top-level pane, put all its items directly
1626 under the top-level menu. */
1627 if (menu_items_n_panes == 1)
1628 pane_string = "";
1629
1630 /* If the pane has a meaningful name,
1631 make the pane a top-level menu item
1632 with its items as a submenu beneath it. */
1633 if (!keymaps && strcmp (pane_string, ""))
1634 {
1635 wv = xmalloc_widget_value ();
1636 if (save_wv)
1637 save_wv->next = wv;
1638 else
1639 first_wv->contents = wv;
1640 wv->name = pane_string;
1641 if (keymaps && !NILP (prefix))
1642 wv->name++;
1643 wv->value = 0;
1644 wv->enabled = 1;
162d2499 1645 wv->button_type = BUTTON_TYPE_NONE;
485015ca
GV
1646 save_wv = wv;
1647 prev_wv = 0;
1648 }
1649 else if (first_pane)
1650 {
1651 save_wv = wv;
1652 prev_wv = 0;
1653 }
1654 first_pane = 0;
1655 i += MENU_ITEMS_PANE_LENGTH;
1656 }
1657 else
1658 {
1659 /* Create a new item within current pane. */
b65a2f83 1660 Lisp_Object item_name, enable, descrip, def, type, selected, help;
b65a2f83 1661
485015ca
GV
1662 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1663 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1664 descrip
1665 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1666 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
162d2499
JR
1667 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
1668 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
b65a2f83 1669 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
162d2499
JR
1670
1671#ifndef HAVE_MULTILINGUAL_MENU
1672 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
6915ded0 1673 item_name = ENCODE_SYSTEM (item_name);
162d2499 1674 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
6915ded0 1675 descrip = ENCODE_SYSTEM (descrip);
162d2499 1676#endif
485015ca
GV
1677
1678 wv = xmalloc_widget_value ();
1679 if (prev_wv)
1680 prev_wv->next = wv;
1681 else
1682 save_wv->contents = wv;
1683 wv->name = (char *) XSTRING (item_name)->data;
1684 if (!NILP (descrip))
1685 wv->key = (char *) XSTRING (descrip)->data;
1686 wv->value = 0;
1687 /* Use the contents index as call_data, since we are
1688 restricted to 16-bits.. */
e59fe83b 1689 wv->call_data = !NILP (def) ? (void *) (EMACS_INT) i : 0;
485015ca 1690 wv->enabled = !NILP (enable);
162d2499
JR
1691
1692 if (NILP (type))
1693 wv->button_type = BUTTON_TYPE_NONE;
1694 else if (EQ (type, QCtoggle))
1695 wv->button_type = BUTTON_TYPE_TOGGLE;
1696 else if (EQ (type, QCradio))
1697 wv->button_type = BUTTON_TYPE_RADIO;
1698 else
1699 abort ();
1700
1701 wv->selected = !NILP (selected);
7d8e57da
JR
1702
1703 if (STRINGP (help))
1704 wv->help = (char *) XSTRING (help)->data;
1705 else
1706 wv->help = NULL;
1707
485015ca
GV
1708 prev_wv = wv;
1709
1710 i += MENU_ITEMS_ITEM_LENGTH;
1711 }
1712 }
1713
1714 /* Deal with the title, if it is non-nil. */
1715 if (!NILP (title))
1716 {
1717 widget_value *wv_title = xmalloc_widget_value ();
1718 widget_value *wv_sep = xmalloc_widget_value ();
1719
1720 /* Maybe replace this separator with a bitmap or owner-draw item
1721 so that it looks better. Having two separators looks odd. */
1722 wv_sep->name = "--";
1723 wv_sep->next = first_wv->contents;
1724
162d2499
JR
1725#ifndef HAVE_MULTILINGUAL_MENU
1726 if (STRING_MULTIBYTE (title))
6915ded0 1727 title = ENCODE_SYSTEM (title);
162d2499 1728#endif
485015ca 1729 wv_title->name = (char *) XSTRING (title)->data;
02067692
SM
1730 wv_title->enabled = TRUE;
1731 wv_title->title = TRUE;
162d2499 1732 wv_title->button_type = BUTTON_TYPE_NONE;
485015ca
GV
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);
162d2499 1740
485015ca 1741 /* Adjust coordinates to be root-window-relative. */
ee78dc32
GV
1742 pos.x = x;
1743 pos.y = y;
fbd6baed 1744 ClientToScreen (FRAME_W32_WINDOW (f), &pos);
485015ca 1745
485015ca
GV
1746 /* No selection has been chosen yet. */
1747 menu_item_selection = 0;
014510b0 1748
ee78dc32 1749 /* Display the menu. */
485015ca
GV
1750 menu_item_selection = SendMessage (FRAME_W32_WINDOW (f),
1751 WM_EMACS_TRACKPOPUPMENU,
1752 (WPARAM)menu, (LPARAM)&pos);
014510b0
GV
1753
1754 /* Clean up extraneous mouse events which might have been generated
1755 during the call. */
1756 discard_mouse_events ();
1757
86fb1a79
JR
1758 /* Free the widget_value objects we used to specify the contents. */
1759 free_menubar_widget_value_tree (first_wv);
1760
485015ca
GV
1761 DestroyMenu (menu);
1762
ee78dc32
GV
1763 /* Find the selected item, and its pane, to return
1764 the proper value. */
485015ca 1765 if (menu_item_selection != 0)
ee78dc32 1766 {
485015ca
GV
1767 Lisp_Object prefix, entry;
1768
fe5a0162 1769 prefix = entry = Qnil;
485015ca
GV
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 }
ee78dc32 1816 }
014510b0 1817
ee78dc32
GV
1818 return Qnil;
1819}
485015ca 1820\f
ee78dc32 1821
485015ca 1822static char * button_names [] = {
ee78dc32 1823 "button1", "button2", "button3", "button4", "button5",
485015ca 1824 "button6", "button7", "button8", "button9", "button10" };
ee78dc32
GV
1825
1826static Lisp_Object
485015ca 1827w32_dialog_show (f, keymaps, title, error)
ee78dc32 1828 FRAME_PTR f;
ee78dc32
GV
1829 int keymaps;
1830 Lisp_Object title;
1831 char **error;
1832{
1833 int i, nb_buttons=0;
ee78dc32 1834 char dialog_name[6];
485015ca
GV
1835 int menu_item_selection;
1836
fe5a0162 1837 widget_value *wv, *first_wv = 0, *prev_wv = 0;
485015ca 1838
ee78dc32
GV
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;
485015ca 1843
ee78dc32 1844 *error = NULL;
485015ca 1845
ee78dc32
GV
1846 if (menu_items_n_panes > 1)
1847 {
1848 *error = "Multiple panes in dialog box";
1849 return Qnil;
1850 }
485015ca 1851
ee78dc32
GV
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);
485015ca 1861 prev_wv = xmalloc_widget_value ();
ee78dc32
GV
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;
485015ca 1868
ee78dc32
GV
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. */
b65a2f83 1875 Lisp_Object item_name, enable, descrip, help;
b65a2f83 1876
ee78dc32
GV
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];
b65a2f83 1881 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
ee78dc32
GV
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 }
485015ca 1897 if (nb_buttons >= 9)
ee78dc32
GV
1898 {
1899 free_menubar_widget_value_tree (first_wv);
1900 *error = "Too many dialog items";
1901 return Qnil;
1902 }
485015ca
GV
1903
1904 wv = xmalloc_widget_value ();
ee78dc32
GV
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;
485015ca 1913
ee78dc32
GV
1914 if (! boundary_seen)
1915 left_count++;
485015ca 1916
ee78dc32
GV
1917 nb_buttons++;
1918 i += MENU_ITEMS_ITEM_LENGTH;
1919 }
485015ca 1920
ee78dc32
GV
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;
485015ca
GV
1925
1926 wv = xmalloc_widget_value ();
ee78dc32 1927 wv->name = dialog_name;
485015ca 1928
ee78dc32
GV
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 }
485015ca 1943
ee78dc32 1944 /* Actually create the dialog. */
162d2499 1945#ifdef HAVE_DIALOGS
485015ca 1946 dialog_id = widget_id_tick++;
ee78dc32 1947 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
fbd6baed 1948 f->output_data.w32->widget, 1, 0,
ee78dc32 1949 dialog_selection_callback, 0);
02067692 1950 lw_modify_all_widgets (dialog_id, first_wv->contents, TRUE);
485015ca
GV
1951#endif
1952
ee78dc32
GV
1953 /* Free the widget_value objects we used to specify the contents. */
1954 free_menubar_widget_value_tree (first_wv);
485015ca 1955
ee78dc32
GV
1956 /* No selection has been chosen yet. */
1957 menu_item_selection = 0;
485015ca 1958
ee78dc32 1959 /* Display the menu. */
162d2499 1960#ifdef HAVE_DIALOGS
ee78dc32 1961 lw_pop_up_all_widgets (dialog_id);
485015ca
GV
1962 popup_activated_flag = 1;
1963
ee78dc32 1964 /* Process events that apply to the menu. */
485015ca 1965 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
ee78dc32 1966
485015ca
GV
1967 lw_destroy_all_widgets (dialog_id);
1968#endif
ee78dc32 1969
ee78dc32
GV
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;
485015ca 1975
ee78dc32
GV
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];
485015ca 1992 if (menu_item_selection == i)
ee78dc32
GV
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 }
485015ca 2006
ee78dc32
GV
2007 return Qnil;
2008}
485015ca
GV
2009\f
2010
2011/* Is this item a separator? */
2012static int
2013name_is_separator (name)
2014 char *name;
2015{
fe5a0162
SM
2016 char *start = name;
2017
2018 /* Check if name string consists of only dashes ('-'). */
485015ca 2019 while (*name == '-') name++;
fe5a0162
SM
2020 /* Separators can also be of the form "--:TripleSuperMegaEtched"
2021 or "--deep-shadow". We don't implement them yet, se we just treat
2022 them like normal separators. */
2023 return (*name == '\0' || start + 2 == name);
485015ca
GV
2024}
2025
2026
2027/* Indicate boundary between left and right. */
2028static int
2029add_left_right_boundary (HMENU menu)
2030{
2031 return AppendMenu (menu, MF_MENUBARBREAK, 0, NULL);
2032}
2033
2034static int
2035add_menu_item (HMENU menu, widget_value *wv, HMENU item)
2036{
2037 UINT fuFlags;
2038 char *out_string;
37ad8b63 2039 int return_value;
485015ca
GV
2040
2041 if (name_is_separator (wv->name))
222456a1
JR
2042 {
2043 fuFlags = MF_SEPARATOR;
2044 out_string = NULL;
2045 }
485015ca
GV
2046 else
2047 {
070d1949 2048 if (wv->enabled)
485015ca
GV
2049 fuFlags = MF_STRING;
2050 else
2051 fuFlags = MF_STRING | MF_GRAYED;
2052
2053 if (wv->key != NULL)
2054 {
2055 out_string = alloca (strlen (wv->name) + strlen (wv->key) + 2);
2056 strcpy (out_string, wv->name);
2057 strcat (out_string, "\t");
2058 strcat (out_string, wv->key);
2059 }
2060 else
2061 out_string = wv->name;
2062
4cdf6a6c 2063 if (wv->title || wv->call_data == 0)
485015ca
GV
2064 {
2065#if 0 /* no GC while popup menu is active */
2066 out_string = LocalAlloc (0, strlen (wv->name) + 1);
2067 strcpy (out_string, wv->name);
ee78dc32 2068#endif
4cdf6a6c 2069 fuFlags = MF_OWNERDRAW | MF_DISABLED;
485015ca 2070 }
162d2499 2071 /* Draw radio buttons and tickboxes. */
4cdf6a6c 2072 else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
37ad8b63 2073 wv->button_type == BUTTON_TYPE_RADIO))
4cdf6a6c 2074 fuFlags |= MF_CHECKED;
37ad8b63 2075 else
4cdf6a6c 2076 fuFlags |= MF_UNCHECKED;
485015ca 2077 }
4cdf6a6c 2078
485015ca
GV
2079 if (item != NULL)
2080 fuFlags = MF_POPUP;
2081
37ad8b63
JR
2082 return_value =
2083 AppendMenu (menu,
2084 fuFlags,
2085 item != NULL ? (UINT) item : (UINT) wv->call_data,
222456a1 2086 out_string );
37ad8b63
JR
2087
2088 /* This must be done after the menu item is created. */
4cdf6a6c
AI
2089 if ((fuFlags & MF_STRING) != 0)
2090 {
2091 HMODULE user32 = GetModuleHandle ("user32.dll");
2092 FARPROC set_menu_item_info = GetProcAddress (user32, "SetMenuItemInfoA");
37ad8b63 2093
4cdf6a6c
AI
2094 if (set_menu_item_info)
2095 {
2096 MENUITEMINFO info;
2097 bzero (&info, sizeof (info));
2098 info.cbSize = sizeof (info);
2099 info.fMask = MIIM_DATA;
2100
2101 /* Set help string for menu item. */
2102 info.dwItemData = (DWORD)wv->help;
2103
2104 if (wv->button_type == BUTTON_TYPE_RADIO)
2105 {
2106 /* CheckMenuRadioItem allows us to differentiate TOGGLE and
2107 RADIO items, but is not available on NT 3.51 and earlier. */
2108 info.fMask |= MIIM_TYPE | MIIM_STATE;
2109 info.fType = MFT_RADIOCHECK | MFT_STRING;
2110 info.dwTypeData = out_string;
2111 info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED;
2112 }
2113
2114 set_menu_item_info (menu,
2115 item != NULL ? (UINT) item : (UINT) wv->call_data,
2116 FALSE, &info);
2117 }
2118 }
37ad8b63 2119 return return_value;
485015ca
GV
2120}
2121
2122/* Construct native Windows menu(bar) based on widget_value tree. */
15d36dee 2123int
485015ca
GV
2124fill_in_menu (HMENU menu, widget_value *wv)
2125{
2126 int items_added = 0;
ee78dc32 2127
485015ca
GV
2128 for ( ; wv != NULL; wv = wv->next)
2129 {
2130 if (wv->contents)
2131 {
2132 HMENU sub_menu = CreatePopupMenu ();
2133
2134 if (sub_menu == NULL)
2135 return 0;
2136
2137 if (!fill_in_menu (sub_menu, wv->contents) ||
2138 !add_menu_item (menu, wv, sub_menu))
2139 {
2140 DestroyMenu (sub_menu);
2141 return 0;
2142 }
2143 }
2144 else
2145 {
2146 if (!add_menu_item (menu, wv, NULL))
2147 return 0;
2148 }
2149 }
2150 return 1;
2151}
2152
162d2499
JR
2153int
2154popup_activated ()
2155{
2156 /* popup_activated_flag not actually used on W32 */
2157 return 0;
2158}
2159
7d8e57da
JR
2160/* Display help string for currently pointed to menu item. Not
2161 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
2162 available. */
2163void
2164w32_menu_display_help (HMENU menu, UINT item, UINT flags)
2165{
222456a1 2166 int pane = 0; /* TODO: Set this to pane number. */
e5cd3d11 2167
7d8e57da
JR
2168 HMODULE user32 = GetModuleHandle ("user32.dll");
2169 FARPROC get_menu_item_info = GetProcAddress (user32, "GetMenuItemInfoA");
2170
2171 if (get_menu_item_info)
2172 {
e5cd3d11
JR
2173 extern Lisp_Object Qmenu_item;
2174 Lisp_Object *first_item;
2175 Lisp_Object pane_name;
2176 Lisp_Object menu_object;
7d8e57da
JR
2177 MENUITEMINFO info;
2178
2179 bzero (&info, sizeof (info));
2180 info.cbSize = sizeof (info);
2181 info.fMask = MIIM_DATA;
2182 get_menu_item_info (menu, item, FALSE, &info);
2183
e5cd3d11
JR
2184 first_item = XVECTOR (menu_items)->contents;
2185 if (EQ (first_item[0], Qt))
2186 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2187 else if (EQ (first_item[0], Qquote))
2188 /* This shouldn't happen, see w32_menu_show. */
2189 pane_name = build_string ("");
2190 else
2191 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2192
2193 /* (menu-item MENU-NAME PANE-NUMBER) */
2194 menu_object = Fcons (Qmenu_item,
2195 Fcons (pane_name,
2196 Fcons (make_number (pane), Qnil)));
2197
f4d19545 2198 show_help_echo (info.dwItemData ?
86fb1a79 2199 build_string ((char *) info.dwItemData) : Qnil,
e5cd3d11 2200 Qnil, menu_object, make_number (item), 1);
7d8e57da
JR
2201 }
2202}
2203
2204
2205
485015ca
GV
2206#endif /* HAVE_MENUS */
2207\f
fbd6baed 2208syms_of_w32menu ()
ee78dc32 2209{
485015ca
GV
2210 staticpro (&menu_items);
2211 menu_items = Qnil;
2212
fdc12c4d
RS
2213 Qdebug_on_next_call = intern ("debug-on-next-call");
2214 staticpro (&Qdebug_on_next_call);
2215
c8869655 2216 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
485015ca 2217 "Frame for which we are updating a menu.\n\
c8869655
GV
2218The enable predicate for a menu command should check this variable.");
2219 Vmenu_updating_frame = Qnil;
2220
ee78dc32 2221 defsubr (&Sx_popup_menu);
485015ca 2222#ifdef HAVE_MENUS
ee78dc32 2223 defsubr (&Sx_popup_dialog);
485015ca 2224#endif
ee78dc32 2225}