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