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