(Fx_popup_dialog): Add a third boolean argument to select frame title
[bpt/emacs.git] / src / w32menu.c
CommitLineData
e9e23e23 1/* Menu support for GNU Emacs on the Microsoft W32 API.
f456401b 2 Copyright (C) 1986,88,93,94,96,98,1999,2003 Free Software Foundation, Inc.
ee78dc32
GV
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
ee78dc32 20
ee78dc32 21#include <config.h>
68c45bf0 22#include <signal.h>
162d2499 23
ee78dc32
GV
24#include <stdio.h>
25#include "lisp.h"
26#include "termhooks.h"
15d36dee 27#include "keyboard.h"
5ee707b8 28#include "keymap.h"
ee78dc32
GV
29#include "frame.h"
30#include "window.h"
ee78dc32 31#include "blockinput.h"
fdc12c4d 32#include "buffer.h"
6915ded0
JR
33#include "charset.h"
34#include "coding.h"
ee78dc32
GV
35
36/* This may include sys/types.h, and that somehow loses
37 if this is not done before the other system files. */
38#include "w32term.h"
39
40/* Load sys/types.h if not already loaded.
41 In some systems loading it twice is suicidal. */
42#ifndef makedev
43#include <sys/types.h>
44#endif
45
46#include "dispextern.h"
47
222456a1 48#undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
162d2499 49
485015ca
GV
50/******************************************************************/
51/* Definitions copied from lwlib.h */
ee78dc32 52
485015ca
GV
53typedef void * XtPointer;
54typedef char Boolean;
55
162d2499
JR
56enum button_type
57{
58 BUTTON_TYPE_NONE,
59 BUTTON_TYPE_TOGGLE,
60 BUTTON_TYPE_RADIO
61};
c8869655 62
d1fffd1d
JR
63/* This structure is based on the one in ../lwlib/lwlib.h, modified
64 for Windows. */
485015ca 65typedef struct _widget_value
ee78dc32 66{
485015ca 67 /* name of widget */
1f06d367 68 Lisp_Object lname;
485015ca
GV
69 char* name;
70 /* value (meaning depend on widget type) */
71 char* value;
177c0ea7 72 /* keyboard equivalent. no implications for XtTranslations */
1f06d367 73 Lisp_Object lkey;
485015ca 74 char* key;
d1fffd1d
JR
75 /* Help string or nil if none.
76 GC finds this string through the frame's menu_bar_vector
77 or through menu_items. */
78 Lisp_Object help;
485015ca
GV
79 /* true if enabled */
80 Boolean enabled;
81 /* true if selected */
82 Boolean selected;
162d2499
JR
83 /* The type of a button. */
84 enum button_type button_type;
485015ca
GV
85 /* true if menu title */
86 Boolean title;
87#if 0
88 /* true if was edited (maintained by get_value) */
89 Boolean edited;
90 /* true if has changed (maintained by lw library) */
91 change_type change;
92 /* true if this widget itself has changed,
93 but not counting the other widgets found in the `next' field. */
94 change_type this_one_change;
95#endif
96 /* Contents of the sub-widgets, also selected slot for checkbox */
97 struct _widget_value* contents;
98 /* data passed to callback */
99 XtPointer call_data;
100 /* next one in the list */
101 struct _widget_value* next;
102#if 0
103 /* slot for the toolkit dependent part. Always initialize to NULL. */
104 void* toolkit_data;
105 /* tell us if we should free the toolkit data slot when freeing the
106 widget_value itself. */
107 Boolean free_toolkit_data;
108
109 /* we resource the widget_value structures; this points to the next
110 one on the free list if this one has been deallocated.
111 */
112 struct _widget_value *free_list;
113#endif
114} widget_value;
115
6ed09cf0
RS
116/* Local memory management */
117#define local_heap (GetProcessHeap ())
118#define local_alloc(n) (HeapAlloc (local_heap, HEAP_ZERO_MEMORY, (n)))
119#define local_free(p) (HeapFree (local_heap, 0, ((LPVOID) (p))))
120
121#define malloc_widget_value() ((widget_value *) local_alloc (sizeof (widget_value)))
122#define free_widget_value(wv) (local_free ((wv)))
485015ca
GV
123
124/******************************************************************/
125
485015ca
GV
126#ifndef TRUE
127#define TRUE 1
128#define FALSE 0
129#endif /* no TRUE */
130
ace9b298
JR
131static HMENU current_popup_menu;
132
f60ae425 133void syms_of_w32menu ();
9785d95b 134void globals_of_w32menu ();
f60ae425
BK
135
136typedef BOOL (WINAPI * GetMenuItemInfoA_Proc) (
137 IN HMENU,
138 IN UINT,
139 IN BOOL,
1f06d367 140 IN OUT LPMENUITEMINFOA);
f60ae425
BK
141typedef BOOL (WINAPI * SetMenuItemInfoA_Proc) (
142 IN HMENU,
143 IN UINT,
144 IN BOOL,
1f06d367 145 IN LPCMENUITEMINFOA);
f60ae425 146
1f06d367
JR
147GetMenuItemInfoA_Proc get_menu_item_info = NULL;
148SetMenuItemInfoA_Proc set_menu_item_info = NULL;
149AppendMenuW_Proc unicode_append_menu = NULL;
ace9b298 150
485015ca 151Lisp_Object Vmenu_updating_frame;
ee78dc32 152
fdc12c4d
RS
153Lisp_Object Qdebug_on_next_call;
154
ee78dc32 155extern Lisp_Object Qmenu_bar;
485015ca
GV
156
157extern Lisp_Object QCtoggle, QCradio;
ee78dc32 158
fdc12c4d
RS
159extern Lisp_Object Voverriding_local_map;
160extern Lisp_Object Voverriding_local_map_menu_flag;
161
162extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
163
164extern Lisp_Object Qmenu_bar_update_hook;
165
014510b0
GV
166void set_frame_menubar ();
167
162d2499
JR
168static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
169 Lisp_Object, Lisp_Object, Lisp_Object,
170 Lisp_Object, Lisp_Object));
1c5acb8c 171#ifdef HAVE_DIALOGS
fbd6baed 172static Lisp_Object w32_dialog_show ();
1c5acb8c 173#endif
162d2499 174static Lisp_Object w32_menu_show ();
ee78dc32 175
485015ca
GV
176static void keymap_panes ();
177static void single_keymap_panes ();
178static void single_menu_item ();
179static void list_of_panes ();
180static void list_of_items ();
71f6b295 181void w32_free_menu_strings (HWND);
485015ca
GV
182\f
183/* This holds a Lisp vector that holds the results of decoding
184 the keymaps or alist-of-alists that specify a menu.
185
186 It describes the panes and items within the panes.
187
188 Each pane is described by 3 elements in the vector:
189 t, the pane name, the pane's prefix key.
190 Then follow the pane's items, with 5 elements per item:
191 the item string, the enable flag, the item's value,
192 the definition, and the equivalent keyboard key's description string.
193
194 In some cases, multiple levels of menus may be described.
195 A single vector slot containing nil indicates the start of a submenu.
196 A single vector slot containing lambda indicates the end of a submenu.
197 The submenu follows a menu item which is the way to reach the submenu.
198
199 A single vector slot containing quote indicates that the
200 following items should appear on the right of a dialog box.
201
202 Using a Lisp vector to hold this information while we decode it
203 takes care of protecting all the data from GC. */
204
205#define MENU_ITEMS_PANE_NAME 1
206#define MENU_ITEMS_PANE_PREFIX 2
207#define MENU_ITEMS_PANE_LENGTH 3
208
162d2499
JR
209enum menu_item_idx
210{
211 MENU_ITEMS_ITEM_NAME = 0,
212 MENU_ITEMS_ITEM_ENABLE,
213 MENU_ITEMS_ITEM_VALUE,
214 MENU_ITEMS_ITEM_EQUIV_KEY,
215 MENU_ITEMS_ITEM_DEFINITION,
216 MENU_ITEMS_ITEM_TYPE,
217 MENU_ITEMS_ITEM_SELECTED,
218 MENU_ITEMS_ITEM_HELP,
219 MENU_ITEMS_ITEM_LENGTH
220};
485015ca
GV
221
222static Lisp_Object menu_items;
223
224/* Number of slots currently allocated in menu_items. */
225static int menu_items_allocated;
226
227/* This is the index in menu_items of the first empty slot. */
228static int menu_items_used;
229
230/* The number of panes currently recorded in menu_items,
231 excluding those within submenus. */
232static int menu_items_n_panes;
233
234/* Current depth within submenus. */
235static int menu_items_submenu_depth;
236
237/* Flag which when set indicates a dialog or menu has been posted by
238 Xt on behalf of one of the widget sets. */
239static int popup_activated_flag;
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
8e713be6 263 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
485015ca 264 {
8e713be6 265 frame = XCAR (tail);
485015ca
GV
266 if (!GC_FRAMEP (frame))
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
ee78dc32 679 if (mouse_position_hook)
485015ca
GV
680 (*mouse_position_hook) (&new_f, 1, &bar_window,
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 }
c8869655 740 Vmenu_updating_frame = Qnil;
485015ca 741#endif /* HAVE_MENUS */
c8869655 742
ee78dc32
GV
743 title = Qnil;
744 GCPRO1 (title);
485015ca
GV
745
746 /* Decode the menu items from what was specified. */
747
02067692
SM
748 keymap = get_keymap (menu, 0, 0);
749 if (CONSP (keymap))
485015ca
GV
750 {
751 /* We were given a keymap. Extract menu info from the keymap. */
752 Lisp_Object prompt;
485015ca
GV
753
754 /* Extract the detailed info to make one pane. */
755 keymap_panes (&menu, 1, NILP (position));
756
757 /* Search for a string appearing directly as an element of the keymap.
758 That string is the title of the menu. */
5ee707b8 759 prompt = Fkeymap_prompt (keymap);
485015ca
GV
760 if (NILP (title) && !NILP (prompt))
761 title = prompt;
762
763 /* Make that be the pane title of the first pane. */
764 if (!NILP (prompt) && menu_items_n_panes >= 0)
6ed09cf0 765 ASET (menu_items, MENU_ITEMS_PANE_NAME, prompt);
485015ca
GV
766
767 keymaps = 1;
768 }
02067692 769 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
485015ca
GV
770 {
771 /* We were given a list of keymaps. */
772 int nmaps = XFASTINT (Flength (menu));
773 Lisp_Object *maps
774 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
775 int i;
776
777 title = Qnil;
778
779 /* The first keymap that has a prompt string
780 supplies the menu title. */
781 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
782 {
783 Lisp_Object prompt;
784
02067692 785 maps[i++] = keymap = get_keymap (Fcar (tem), 1, 0);
485015ca 786
5ee707b8 787 prompt = Fkeymap_prompt (keymap);
485015ca
GV
788 if (NILP (title) && !NILP (prompt))
789 title = prompt;
790 }
791
792 /* Extract the detailed info to make one pane. */
793 keymap_panes (maps, nmaps, NILP (position));
794
795 /* Make the title be the pane title of the first pane. */
796 if (!NILP (title) && menu_items_n_panes >= 0)
6ed09cf0 797 ASET (menu_items, MENU_ITEMS_PANE_NAME, title);
485015ca
GV
798
799 keymaps = 1;
800 }
801 else
802 {
803 /* We were given an old-fashioned menu. */
804 title = Fcar (menu);
b7826503 805 CHECK_STRING (title);
485015ca
GV
806
807 list_of_panes (Fcdr (menu));
808
809 keymaps = 0;
810 }
177c0ea7 811
ee78dc32
GV
812 if (NILP (position))
813 {
485015ca 814 discard_menu_items ();
ee78dc32
GV
815 UNGCPRO;
816 return Qnil;
817 }
485015ca
GV
818
819#ifdef HAVE_MENUS
6ed09cf0
RS
820 /* If resources from a previous popup menu exist yet, does nothing
821 until the `menu_free_timer' has freed them (see w32fns.c).
822 */
823 if (current_popup_menu)
824 {
825 discard_menu_items ();
826 UNGCPRO;
827 return Qnil;
177c0ea7
JB
828 }
829
ee78dc32
GV
830 /* Display them in a menu. */
831 BLOCK_INPUT;
485015ca
GV
832
833 selection = w32_menu_show (f, xpos, ypos, for_click,
834 keymaps, title, &error_name);
ee78dc32 835 UNBLOCK_INPUT;
485015ca
GV
836
837 discard_menu_items ();
6ed09cf0 838#endif /* HAVE_MENUS */
485015ca 839
ee78dc32 840 UNGCPRO;
485015ca 841
ee78dc32
GV
842 if (error_name) error (error_name);
843 return selection;
844}
845
485015ca
GV
846#ifdef HAVE_MENUS
847
ee78dc32 848DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
33f09670
JR
849 doc: /* Pop up a dialog box and return user's selection.
850POSITION specifies which frame to use.
851This is normally a mouse button event or a window or frame.
852If POSITION is t, it means to use the frame the mouse is on.
853The dialog box appears in the middle of the specified frame.
854
855CONTENTS specifies the alternatives to display in the dialog box.
856It is a list of the form (TITLE ITEM1 ITEM2...).
857Each ITEM is a cons cell (STRING . VALUE).
858The return value is VALUE from the chosen item.
859
860An ITEM may also be just a string--that makes a nonselectable item.
861An ITEM may also be nil--that means to put all preceding items
862on the left of the dialog box and all following items on the right.
d23928c7
NR
863\(By default, approximately half appear on each side.)
864
865If HEADER is non-nil, the frame title for the box is "Information",
866otherwise it is "Question". */)
867 (position, contents, header)
868 Lisp_Object position, contents, header;
ee78dc32 869{
b2123f81 870 FRAME_PTR f = NULL;
ee78dc32 871 Lisp_Object window;
485015ca
GV
872
873 check_w32 ();
874
ee78dc32 875 /* Decode the first argument: find the window or frame to use. */
485015ca 876 if (EQ (position, Qt)
0b6bb670
JR
877 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
878 || EQ (XCAR (position), Qtool_bar))))
ee78dc32 879 {
485015ca
GV
880#if 0 /* Using the frame the mouse is on may not be right. */
881 /* Use the mouse's current position. */
162d2499 882 FRAME_PTR new_f = SELECTED_FRAME ();
485015ca 883 Lisp_Object bar_window;
15d36dee 884 enum scroll_bar_part part;
485015ca
GV
885 unsigned long time;
886 Lisp_Object x, y;
887
888 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
889
890 if (new_f != 0)
891 XSETFRAME (window, new_f);
892 else
ee78dc32 893 window = selected_window;
485015ca
GV
894#endif
895 window = selected_window;
ee78dc32
GV
896 }
897 else if (CONSP (position))
898 {
899 Lisp_Object tem;
900 tem = Fcar (position);
485015ca 901 if (CONSP (tem))
ee78dc32
GV
902 window = Fcar (Fcdr (position));
903 else
904 {
485015ca
GV
905 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
906 window = Fcar (tem); /* POSN_WINDOW (tem) */
ee78dc32
GV
907 }
908 }
909 else if (WINDOWP (position) || FRAMEP (position))
910 window = position;
485015ca
GV
911 else
912 window = Qnil;
913
ee78dc32 914 /* Decode where to put the menu. */
485015ca 915
ee78dc32
GV
916 if (FRAMEP (window))
917 f = XFRAME (window);
918 else if (WINDOWP (window))
919 {
b7826503 920 CHECK_LIVE_WINDOW (window);
ee78dc32
GV
921 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
922 }
923 else
924 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
925 but I don't want to make one now. */
b7826503 926 CHECK_WINDOW (window);
485015ca 927
162d2499 928#ifndef HAVE_DIALOGS
ee78dc32
GV
929 /* Display a menu with these alternatives
930 in the middle of frame F. */
931 {
932 Lisp_Object x, y, frame, newpos;
933 XSETFRAME (frame, f);
934 XSETINT (x, x_pixel_width (f) / 2);
935 XSETINT (y, x_pixel_height (f) / 2);
936 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
937
938 return Fx_popup_menu (newpos,
939 Fcons (Fcar (contents), Fcons (contents, Qnil)));
940 }
162d2499 941#else /* HAVE_DIALOGS */
ee78dc32
GV
942 {
943 Lisp_Object title;
944 char *error_name;
945 Lisp_Object selection;
946
947 /* Decode the dialog items from what was specified. */
948 title = Fcar (contents);
b7826503 949 CHECK_STRING (title);
ee78dc32
GV
950
951 list_of_panes (Fcons (contents, Qnil));
952
953 /* Display them in a dialog box. */
954 BLOCK_INPUT;
d23928c7 955 selection = w32_dialog_show (f, 0, title, header, &error_name);
ee78dc32
GV
956 UNBLOCK_INPUT;
957
958 discard_menu_items ();
959
960 if (error_name) error (error_name);
961 return selection;
962 }
162d2499 963#endif /* HAVE_DIALOGS */
ee78dc32
GV
964}
965
014510b0
GV
966/* Activate the menu bar of frame F.
967 This is called from keyboard.c when it gets the
3b8f9651 968 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
014510b0
GV
969
970 To activate the menu bar, we signal to the input thread that it can
971 return from the WM_INITMENU message, allowing the normal Windows
972 processing of the menus.
973
974 But first we recompute the menu bar contents (the whole tree).
975
976 This way we can safely execute Lisp code. */
177c0ea7 977
162d2499 978void
014510b0
GV
979x_activate_menubar (f)
980 FRAME_PTR f;
981{
982 set_frame_menubar (f, 0, 1);
983
984 /* Lock out further menubar changes while active. */
985 f->output_data.w32->menubar_active = 1;
986
987 /* Signal input thread to return from WM_INITMENU. */
988 complete_deferred_msg (FRAME_W32_WINDOW (f), WM_INITMENU, 0);
989}
990
485015ca
GV
991/* This callback is called from the menu bar pulldown menu
992 when the user makes a selection.
993 Figure out what the user chose
994 and put the appropriate events into the keyboard buffer. */
995
996void
997menubar_selection_callback (FRAME_PTR f, void * client_data)
ee78dc32 998{
485015ca
GV
999 Lisp_Object prefix, entry;
1000 Lisp_Object vector;
1001 Lisp_Object *subprefix_stack;
1002 int submenu_depth = 0;
ee78dc32 1003 int i;
fdc12c4d 1004
485015ca 1005 if (!f)
014510b0 1006 return;
fe5a0162 1007 entry = Qnil;
485015ca
GV
1008 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1009 vector = f->menu_bar_vector;
1010 prefix = Qnil;
1011 i = 0;
1012 while (i < f->menu_bar_items_used)
1013 {
6ed09cf0 1014 if (EQ (AREF (vector, i), Qnil))
485015ca
GV
1015 {
1016 subprefix_stack[submenu_depth++] = prefix;
1017 prefix = entry;
1018 i++;
1019 }
6ed09cf0 1020 else if (EQ (AREF (vector, i), Qlambda))
485015ca
GV
1021 {
1022 prefix = subprefix_stack[--submenu_depth];
1023 i++;
1024 }
6ed09cf0 1025 else if (EQ (AREF (vector, i), Qt))
485015ca 1026 {
6ed09cf0 1027 prefix = AREF (vector, i + MENU_ITEMS_PANE_PREFIX);
485015ca
GV
1028 i += MENU_ITEMS_PANE_LENGTH;
1029 }
1030 else
1031 {
6ed09cf0 1032 entry = AREF (vector, i + MENU_ITEMS_ITEM_VALUE);
485015ca
GV
1033 /* The EMACS_INT cast avoids a warning. There's no problem
1034 as long as pointers have enough bits to hold small integers. */
1035 if ((int) (EMACS_INT) client_data == i)
1036 {
1037 int j;
1038 struct input_event buf;
1039 Lisp_Object frame;
f456401b 1040 EVENT_INIT (buf);
014510b0 1041
485015ca 1042 XSETFRAME (frame, f);
20501278
GM
1043 buf.kind = MENU_BAR_EVENT;
1044 buf.frame_or_window = frame;
1045 buf.arg = frame;
485015ca 1046 kbd_buffer_store_event (&buf);
014510b0 1047
485015ca
GV
1048 for (j = 0; j < submenu_depth; j++)
1049 if (!NILP (subprefix_stack[j]))
1050 {
20501278
GM
1051 buf.kind = MENU_BAR_EVENT;
1052 buf.frame_or_window = frame;
1053 buf.arg = subprefix_stack[j];
485015ca
GV
1054 kbd_buffer_store_event (&buf);
1055 }
fdc12c4d 1056
485015ca
GV
1057 if (!NILP (prefix))
1058 {
20501278
GM
1059 buf.kind = MENU_BAR_EVENT;
1060 buf.frame_or_window = frame;
1061 buf.arg = prefix;
485015ca
GV
1062 kbd_buffer_store_event (&buf);
1063 }
fdc12c4d 1064
20501278
GM
1065 buf.kind = MENU_BAR_EVENT;
1066 buf.frame_or_window = frame;
1067 buf.arg = entry;
485015ca 1068 kbd_buffer_store_event (&buf);
fdc12c4d 1069
71f6b295
JR
1070 /* Free memory used by owner-drawn and help-echo strings. */
1071 w32_free_menu_strings (FRAME_W32_WINDOW (f));
1072 f->output_data.w32->menu_command_in_progress = 0;
1073 f->output_data.w32->menubar_active = 0;
485015ca
GV
1074 return;
1075 }
1076 i += MENU_ITEMS_ITEM_LENGTH;
1077 }
014510b0 1078 }
71f6b295
JR
1079 /* Free memory used by owner-drawn and help-echo strings. */
1080 w32_free_menu_strings (FRAME_W32_WINDOW (f));
1081 f->output_data.w32->menu_command_in_progress = 0;
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
1446 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1447 if (NILP (Voverriding_local_map_menu_flag))
1448 {
1449 specbind (Qoverriding_terminal_local_map, Qnil);
1450 specbind (Qoverriding_local_map, Qnil);
1451 }
1452
1453 set_buffer_internal_1 (XBUFFER (buffer));
1454
1455 /* Run the Lucid hook. */
c53b6500 1456 safe_run_hooks (Qactivate_menubar_hook);
485015ca
GV
1457 /* If it has changed current-menubar from previous value,
1458 really recompute the menubar from the value. */
1459 if (! NILP (Vlucid_menu_bar_dirty_flag))
1460 call0 (Qrecompute_lucid_menubar);
1461 safe_run_hooks (Qmenu_bar_update_hook);
1462 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1463
1464 items = FRAME_MENU_BAR_ITEMS (f);
1465
485015ca 1466 /* Save the frame's previous menu bar contents data. */
c30dce3d
AI
1467 if (previous_menu_items_used)
1468 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1469 previous_menu_items_used * sizeof (Lisp_Object));
485015ca 1470
6ed09cf0
RS
1471 /* Fill in menu_items with the current menu bar contents.
1472 This can evaluate Lisp code. */
485015ca 1473 menu_items = f->menu_bar_vector;
c30dce3d 1474 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
6ed09cf0
RS
1475 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1476 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
fe04b8c8 1477 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
6ed09cf0
RS
1478 submenu_top_level_items
1479 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
485015ca 1480 init_menu_items ();
6ed09cf0 1481 for (i = 0; i < ASIZE (items); i += 4)
485015ca
GV
1482 {
1483 Lisp_Object key, string, maps;
1484
6ed09cf0
RS
1485 last_i = i;
1486
1487 key = AREF (items, i);
1488 string = AREF (items, i + 1);
1489 maps = AREF (items, i + 2);
485015ca
GV
1490 if (NILP (string))
1491 break;
1492
6ed09cf0
RS
1493 submenu_start[i] = menu_items_used;
1494
1495 menu_items_n_panes = 0;
1496 submenu_top_level_items[i]
1497 = parse_single_submenu (key, string, maps);
fe04b8c8 1498 submenu_n_panes[i] = menu_items_n_panes;
6ed09cf0
RS
1499
1500 submenu_end[i] = menu_items_used;
1501 }
1502
1503 finish_menu_items ();
1504
1505 /* Convert menu_items into widget_value trees
1506 to display the menu. This cannot evaluate Lisp code. */
1507
1508 wv = xmalloc_widget_value ();
1509 wv->name = "menubar";
1510 wv->value = 0;
1511 wv->enabled = 1;
1512 wv->button_type = BUTTON_TYPE_NONE;
1513 wv->help = Qnil;
1514 first_wv = wv;
1515
1516 for (i = 0; i < last_i; i += 4)
1517 {
fe04b8c8 1518 menu_items_n_panes = submenu_n_panes[i];
6ed09cf0
RS
1519 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
1520 submenu_top_level_items[i]);
177c0ea7 1521 if (prev_wv)
485015ca
GV
1522 prev_wv->next = wv;
1523 else
1524 first_wv->contents = wv;
1525 /* Don't set wv->name here; GC during the loop might relocate it. */
1526 wv->enabled = 1;
162d2499 1527 wv->button_type = BUTTON_TYPE_NONE;
485015ca
GV
1528 prev_wv = wv;
1529 }
1530
485015ca
GV
1531 set_buffer_internal_1 (prev);
1532 unbind_to (specpdl_count, Qnil);
1533
1534 /* If there has been no change in the Lisp-level contents
1535 of the menu bar, skip redisplaying it. Just exit. */
1536
1537 for (i = 0; i < previous_menu_items_used; i++)
1538 if (menu_items_used == i
6ed09cf0 1539 || (!EQ (previous_items[i], AREF (menu_items, i))))
485015ca
GV
1540 break;
1541 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1542 {
1543 free_menubar_widget_value_tree (first_wv);
1544 menu_items = Qnil;
1545
1546 return;
1547 }
1548
1549 /* Now GC cannot happen during the lifetime of the widget_value,
ace9b298
JR
1550 so it's safe to store data from a Lisp_String, as long as
1551 local copies are made when the actual menu is created.
1552 Windows takes care of this for normal string items, but
1553 not for owner-drawn items or additional item-info. */
485015ca 1554 wv = first_wv->contents;
6ed09cf0 1555 for (i = 0; i < ASIZE (items); i += 4)
485015ca
GV
1556 {
1557 Lisp_Object string;
6ed09cf0 1558 string = AREF (items, i + 1);
485015ca
GV
1559 if (NILP (string))
1560 break;
d5db4077 1561 wv->name = (char *) SDATA (string);
1f06d367 1562 update_submenu_strings (wv->contents);
485015ca
GV
1563 wv = wv->next;
1564 }
1565
1566 f->menu_bar_vector = menu_items;
1567 f->menu_bar_items_used = menu_items_used;
1568 menu_items = Qnil;
1569 }
1570 else
1571 {
1572 /* Make a widget-value tree containing
162d2499 1573 just the top level menu bar strings. */
485015ca 1574
6ed09cf0
RS
1575 wv = xmalloc_widget_value ();
1576 wv->name = "menubar";
1577 wv->value = 0;
1578 wv->enabled = 1;
1579 wv->button_type = BUTTON_TYPE_NONE;
1580 wv->help = Qnil;
1581 first_wv = wv;
1582
485015ca 1583 items = FRAME_MENU_BAR_ITEMS (f);
6ed09cf0 1584 for (i = 0; i < ASIZE (items); i += 4)
485015ca
GV
1585 {
1586 Lisp_Object string;
1587
6ed09cf0 1588 string = AREF (items, i + 1);
485015ca
GV
1589 if (NILP (string))
1590 break;
1591
1592 wv = xmalloc_widget_value ();
d5db4077 1593 wv->name = (char *) SDATA (string);
485015ca
GV
1594 wv->value = 0;
1595 wv->enabled = 1;
162d2499 1596 wv->button_type = BUTTON_TYPE_NONE;
2d10309f 1597 wv->help = Qnil;
485015ca
GV
1598 /* This prevents lwlib from assuming this
1599 menu item is really supposed to be empty. */
1600 /* The EMACS_INT cast avoids a warning.
1601 This value just has to be different from small integers. */
1602 wv->call_data = (void *) (EMACS_INT) (-1);
1603
177c0ea7 1604 if (prev_wv)
485015ca
GV
1605 prev_wv->next = wv;
1606 else
1607 first_wv->contents = wv;
1608 prev_wv = wv;
1609 }
1610
162d2499
JR
1611 /* Forget what we thought we knew about what is in the
1612 detailed contents of the menu bar menus.
1613 Changing the top level always destroys the contents. */
1614 f->menu_bar_items_used = 0;
485015ca
GV
1615 }
1616
1617 /* Create or update the menu bar widget. */
ee78dc32 1618
485015ca 1619 BLOCK_INPUT;
ee78dc32 1620
485015ca
GV
1621 if (menubar_widget)
1622 {
1623 /* Empty current menubar, rather than creating a fresh one. */
1624 while (DeleteMenu (menubar_widget, 0, MF_BYPOSITION))
1625 ;
1626 }
1627 else
1628 {
1629 menubar_widget = CreateMenu ();
1630 }
1631 fill_in_menu (menubar_widget, first_wv->contents);
ee78dc32 1632
485015ca 1633 free_menubar_widget_value_tree (first_wv);
ee78dc32 1634
485015ca
GV
1635 {
1636 HMENU old_widget = f->output_data.w32->menubar_widget;
ee78dc32 1637
485015ca
GV
1638 f->output_data.w32->menubar_widget = menubar_widget;
1639 SetMenu (FRAME_W32_WINDOW (f), f->output_data.w32->menubar_widget);
177c0ea7 1640 /* Causes flicker when menu bar is updated
485015ca 1641 DrawMenuBar (FRAME_W32_WINDOW (f)); */
ee78dc32 1642
485015ca
GV
1643 /* Force the window size to be recomputed so that the frame's text
1644 area remains the same, if menubar has just been created. */
1645 if (old_widget == NULL)
90022f5a 1646 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
ee78dc32 1647 }
485015ca
GV
1648
1649 UNBLOCK_INPUT;
ee78dc32
GV
1650}
1651
485015ca
GV
1652/* Called from Fx_create_frame to create the initial menubar of a frame
1653 before it is mapped, so that the window is mapped with the menubar already
1654 there instead of us tacking it on later and thrashing the window after it
1655 is visible. */
1656
1657void
1658initialize_frame_menubar (f)
1659 FRAME_PTR f;
1660{
1661 /* This function is called before the first chance to redisplay
1662 the frame. It has to be, so the frame will have the right size. */
1663 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1664 set_frame_menubar (f, 1, 1);
ee78dc32
GV
1665}
1666
485015ca
GV
1667/* Get rid of the menu bar of frame F, and free its storage.
1668 This is used when deleting a frame, and when turning off the menu bar. */
1669
1670void
1671free_frame_menubar (f)
1672 FRAME_PTR f;
1673{
1674 BLOCK_INPUT;
1675
1676 {
1677 HMENU old = GetMenu (FRAME_W32_WINDOW (f));
1678 SetMenu (FRAME_W32_WINDOW (f), NULL);
1679 f->output_data.w32->menubar_widget = NULL;
1680 DestroyMenu (old);
1681 }
29250851 1682
485015ca
GV
1683 UNBLOCK_INPUT;
1684}
ee78dc32 1685
485015ca
GV
1686\f
1687/* w32_menu_show actually displays a menu using the panes and items in
1688 menu_items and returns the value selected from it; we assume input
1689 is blocked by the caller. */
ee78dc32
GV
1690
1691/* F is the frame the menu is for.
1692 X and Y are the frame-relative specified position,
1693 relative to the inside upper left corner of the frame F.
485015ca 1694 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
ee78dc32 1695 KEYMAPS is 1 if this menu was specified with keymaps;
485015ca
GV
1696 in that case, we return a list containing the chosen item's value
1697 and perhaps also the pane's prefix.
ee78dc32
GV
1698 TITLE is the specified menu title.
1699 ERROR is a place to store an error message string in case of failure.
1700 (We return nil on failure, but the value doesn't actually matter.) */
1701
485015ca
GV
1702static Lisp_Object
1703w32_menu_show (f, x, y, for_click, keymaps, title, error)
ee78dc32
GV
1704 FRAME_PTR f;
1705 int x;
1706 int y;
485015ca
GV
1707 int for_click;
1708 int keymaps;
1709 Lisp_Object title;
ee78dc32
GV
1710 char **error;
1711{
485015ca
GV
1712 int i;
1713 int menu_item_selection;
1714 HMENU menu;
ee78dc32 1715 POINT pos;
485015ca
GV
1716 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1717 widget_value **submenu_stack
1718 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1719 Lisp_Object *subprefix_stack
1720 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1721 int submenu_depth = 0;
485015ca 1722 int first_pane;
485015ca 1723
ee78dc32 1724 *error = NULL;
485015ca
GV
1725
1726 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
ee78dc32
GV
1727 {
1728 *error = "Empty menu";
1729 return Qnil;
1730 }
485015ca
GV
1731
1732 /* Create a tree of widget_value objects
1733 representing the panes and their items. */
1734 wv = xmalloc_widget_value ();
1735 wv->name = "menu";
1736 wv->value = 0;
1737 wv->enabled = 1;
162d2499 1738 wv->button_type = BUTTON_TYPE_NONE;
e6bb685b 1739 wv->help = Qnil;
485015ca
GV
1740 first_wv = wv;
1741 first_pane = 1;
177c0ea7 1742
485015ca
GV
1743 /* Loop over all panes and items, filling in the tree. */
1744 i = 0;
1745 while (i < menu_items_used)
1746 {
6ed09cf0 1747 if (EQ (AREF (menu_items, i), Qnil))
485015ca
GV
1748 {
1749 submenu_stack[submenu_depth++] = save_wv;
1750 save_wv = prev_wv;
1751 prev_wv = 0;
1752 first_pane = 1;
1753 i++;
1754 }
6ed09cf0 1755 else if (EQ (AREF (menu_items, i), Qlambda))
485015ca
GV
1756 {
1757 prev_wv = save_wv;
1758 save_wv = submenu_stack[--submenu_depth];
1759 first_pane = 0;
1760 i++;
1761 }
6ed09cf0 1762 else if (EQ (AREF (menu_items, i), Qt)
485015ca
GV
1763 && submenu_depth != 0)
1764 i += MENU_ITEMS_PANE_LENGTH;
1765 /* Ignore a nil in the item list.
1766 It's meaningful only for dialog boxes. */
6ed09cf0 1767 else if (EQ (AREF (menu_items, i), Qquote))
485015ca 1768 i += 1;
6ed09cf0 1769 else if (EQ (AREF (menu_items, i), Qt))
485015ca
GV
1770 {
1771 /* Create a new pane. */
1772 Lisp_Object pane_name, prefix;
1773 char *pane_string;
29250851
JR
1774 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
1775 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1f06d367
JR
1776
1777 if (STRINGP (pane_name))
29250851 1778 {
1f06d367
JR
1779 if (unicode_append_menu)
1780 pane_name = ENCODE_UTF_8 (pane_name);
1781 else if (STRING_MULTIBYTE (pane_name))
1782 pane_name = ENCODE_SYSTEM (pane_name);
1783
6ed09cf0 1784 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
29250851 1785 }
1f06d367 1786
485015ca 1787 pane_string = (NILP (pane_name)
d5db4077 1788 ? "" : (char *) SDATA (pane_name));
485015ca
GV
1789 /* If there is just one top-level pane, put all its items directly
1790 under the top-level menu. */
1791 if (menu_items_n_panes == 1)
1792 pane_string = "";
1793
1794 /* If the pane has a meaningful name,
1795 make the pane a top-level menu item
1796 with its items as a submenu beneath it. */
1797 if (!keymaps && strcmp (pane_string, ""))
1798 {
1799 wv = xmalloc_widget_value ();
1800 if (save_wv)
1801 save_wv->next = wv;
1802 else
1803 first_wv->contents = wv;
1804 wv->name = pane_string;
1805 if (keymaps && !NILP (prefix))
1806 wv->name++;
1807 wv->value = 0;
1808 wv->enabled = 1;
162d2499 1809 wv->button_type = BUTTON_TYPE_NONE;
2d10309f 1810 wv->help = Qnil;
485015ca
GV
1811 save_wv = wv;
1812 prev_wv = 0;
1813 }
1814 else if (first_pane)
1815 {
1816 save_wv = wv;
1817 prev_wv = 0;
1818 }
1819 first_pane = 0;
1820 i += MENU_ITEMS_PANE_LENGTH;
1821 }
1822 else
1823 {
1824 /* Create a new item within current pane. */
b65a2f83 1825 Lisp_Object item_name, enable, descrip, def, type, selected, help;
b65a2f83 1826
29250851
JR
1827 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1828 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1829 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1830 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1831 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1832 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1833 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
162d2499 1834
1f06d367 1835 if (STRINGP (item_name))
29250851 1836 {
1f06d367
JR
1837 if (unicode_append_menu)
1838 item_name = ENCODE_UTF_8 (item_name);
1839 else if (STRING_MULTIBYTE (item_name))
1840 item_name = ENCODE_SYSTEM (item_name);
1841
6ed09cf0 1842 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
29250851 1843 }
1f06d367
JR
1844
1845 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
29250851
JR
1846 {
1847 descrip = ENCODE_SYSTEM (descrip);
6ed09cf0 1848 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
29250851 1849 }
485015ca
GV
1850
1851 wv = xmalloc_widget_value ();
177c0ea7 1852 if (prev_wv)
485015ca 1853 prev_wv->next = wv;
177c0ea7 1854 else
485015ca 1855 save_wv->contents = wv;
d5db4077 1856 wv->name = (char *) SDATA (item_name);
485015ca 1857 if (!NILP (descrip))
d5db4077 1858 wv->key = (char *) SDATA (descrip);
485015ca
GV
1859 wv->value = 0;
1860 /* Use the contents index as call_data, since we are
2cd23960 1861 restricted to 16-bits. */
e59fe83b 1862 wv->call_data = !NILP (def) ? (void *) (EMACS_INT) i : 0;
485015ca 1863 wv->enabled = !NILP (enable);
162d2499
JR
1864
1865 if (NILP (type))
1866 wv->button_type = BUTTON_TYPE_NONE;
1867 else if (EQ (type, QCtoggle))
1868 wv->button_type = BUTTON_TYPE_TOGGLE;
1869 else if (EQ (type, QCradio))
1870 wv->button_type = BUTTON_TYPE_RADIO;
1871 else
1872 abort ();
1873
1874 wv->selected = !NILP (selected);
d1fffd1d
JR
1875 if (!STRINGP (help))
1876 help = Qnil;
1877
1878 wv->help = help;
7d8e57da 1879
485015ca
GV
1880 prev_wv = wv;
1881
1882 i += MENU_ITEMS_ITEM_LENGTH;
1883 }
1884 }
1885
1886 /* Deal with the title, if it is non-nil. */
1887 if (!NILP (title))
1888 {
1889 widget_value *wv_title = xmalloc_widget_value ();
1890 widget_value *wv_sep = xmalloc_widget_value ();
1891
1892 /* Maybe replace this separator with a bitmap or owner-draw item
1893 so that it looks better. Having two separators looks odd. */
1894 wv_sep->name = "--";
1895 wv_sep->next = first_wv->contents;
2d10309f 1896 wv_sep->help = Qnil;
485015ca 1897
1f06d367
JR
1898 if (unicode_append_menu)
1899 title = ENCODE_UTF_8 (title);
1900 else if (STRING_MULTIBYTE (title))
6915ded0 1901 title = ENCODE_SYSTEM (title);
1f06d367 1902
d5db4077 1903 wv_title->name = (char *) SDATA (title);
02067692
SM
1904 wv_title->enabled = TRUE;
1905 wv_title->title = TRUE;
162d2499 1906 wv_title->button_type = BUTTON_TYPE_NONE;
2d10309f 1907 wv_title->help = Qnil;
485015ca
GV
1908 wv_title->next = wv_sep;
1909 first_wv->contents = wv_title;
1910 }
1911
1912 /* Actually create the menu. */
ace9b298 1913 current_popup_menu = menu = CreatePopupMenu ();
485015ca 1914 fill_in_menu (menu, first_wv->contents);
162d2499 1915
485015ca 1916 /* Adjust coordinates to be root-window-relative. */
ee78dc32
GV
1917 pos.x = x;
1918 pos.y = y;
fbd6baed 1919 ClientToScreen (FRAME_W32_WINDOW (f), &pos);
485015ca 1920
485015ca
GV
1921 /* No selection has been chosen yet. */
1922 menu_item_selection = 0;
014510b0 1923
ee78dc32 1924 /* Display the menu. */
177c0ea7 1925 menu_item_selection = SendMessage (FRAME_W32_WINDOW (f),
485015ca
GV
1926 WM_EMACS_TRACKPOPUPMENU,
1927 (WPARAM)menu, (LPARAM)&pos);
014510b0
GV
1928
1929 /* Clean up extraneous mouse events which might have been generated
1930 during the call. */
1931 discard_mouse_events ();
1932
86fb1a79
JR
1933 /* Free the widget_value objects we used to specify the contents. */
1934 free_menubar_widget_value_tree (first_wv);
1935
485015ca
GV
1936 DestroyMenu (menu);
1937
ee78dc32
GV
1938 /* Find the selected item, and its pane, to return
1939 the proper value. */
485015ca 1940 if (menu_item_selection != 0)
ee78dc32 1941 {
485015ca
GV
1942 Lisp_Object prefix, entry;
1943
fe5a0162 1944 prefix = entry = Qnil;
485015ca
GV
1945 i = 0;
1946 while (i < menu_items_used)
1947 {
6ed09cf0 1948 if (EQ (AREF (menu_items, i), Qnil))
485015ca
GV
1949 {
1950 subprefix_stack[submenu_depth++] = prefix;
1951 prefix = entry;
1952 i++;
1953 }
6ed09cf0 1954 else if (EQ (AREF (menu_items, i), Qlambda))
485015ca
GV
1955 {
1956 prefix = subprefix_stack[--submenu_depth];
1957 i++;
1958 }
6ed09cf0 1959 else if (EQ (AREF (menu_items, i), Qt))
485015ca 1960 {
6ed09cf0 1961 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
485015ca
GV
1962 i += MENU_ITEMS_PANE_LENGTH;
1963 }
1964 /* Ignore a nil in the item list.
1965 It's meaningful only for dialog boxes. */
6ed09cf0 1966 else if (EQ (AREF (menu_items, i), Qquote))
485015ca
GV
1967 i += 1;
1968 else
1969 {
6ed09cf0 1970 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
485015ca
GV
1971 if (menu_item_selection == i)
1972 {
1973 if (keymaps != 0)
1974 {
1975 int j;
1976
1977 entry = Fcons (entry, Qnil);
1978 if (!NILP (prefix))
1979 entry = Fcons (prefix, entry);
1980 for (j = submenu_depth - 1; j >= 0; j--)
1981 if (!NILP (subprefix_stack[j]))
1982 entry = Fcons (subprefix_stack[j], entry);
1983 }
1984 return entry;
1985 }
1986 i += MENU_ITEMS_ITEM_LENGTH;
1987 }
1988 }
ee78dc32 1989 }
014510b0 1990
ee78dc32
GV
1991 return Qnil;
1992}
485015ca 1993\f
ee78dc32 1994
ace9b298 1995#ifdef HAVE_DIALOGS
485015ca 1996static char * button_names [] = {
ee78dc32 1997 "button1", "button2", "button3", "button4", "button5",
485015ca 1998 "button6", "button7", "button8", "button9", "button10" };
ee78dc32
GV
1999
2000static Lisp_Object
d23928c7 2001w32_dialog_show (f, keymaps, title, header, error)
ee78dc32 2002 FRAME_PTR f;
ee78dc32 2003 int keymaps;
d23928c7 2004 Lisp_Object title, header;
ee78dc32
GV
2005 char **error;
2006{
2007 int i, nb_buttons=0;
ee78dc32 2008 char dialog_name[6];
485015ca
GV
2009 int menu_item_selection;
2010
fe5a0162 2011 widget_value *wv, *first_wv = 0, *prev_wv = 0;
485015ca 2012
ee78dc32
GV
2013 /* Number of elements seen so far, before boundary. */
2014 int left_count = 0;
2015 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2016 int boundary_seen = 0;
485015ca 2017
ee78dc32 2018 *error = NULL;
485015ca 2019
ee78dc32
GV
2020 if (menu_items_n_panes > 1)
2021 {
2022 *error = "Multiple panes in dialog box";
2023 return Qnil;
2024 }
485015ca 2025
ee78dc32
GV
2026 /* Create a tree of widget_value objects
2027 representing the text label and buttons. */
2028 {
2029 Lisp_Object pane_name, prefix;
2030 char *pane_string;
6ed09cf0
RS
2031 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
2032 prefix = AREF (menu_items, MENU_ITEMS_PANE_PREFIX);
ee78dc32 2033 pane_string = (NILP (pane_name)
177c0ea7 2034 ? "" : (char *) SDATA (pane_name));
485015ca 2035 prev_wv = xmalloc_widget_value ();
ee78dc32
GV
2036 prev_wv->value = pane_string;
2037 if (keymaps && !NILP (prefix))
2038 prev_wv->name++;
2039 prev_wv->enabled = 1;
2040 prev_wv->name = "message";
2d10309f 2041 prev_wv->help = Qnil;
ee78dc32 2042 first_wv = prev_wv;
177c0ea7 2043
ee78dc32
GV
2044 /* Loop over all panes and items, filling in the tree. */
2045 i = MENU_ITEMS_PANE_LENGTH;
2046 while (i < menu_items_used)
2047 {
177c0ea7 2048
ee78dc32 2049 /* Create a new item within current pane. */
b65a2f83 2050 Lisp_Object item_name, enable, descrip, help;
b65a2f83 2051
6ed09cf0
RS
2052 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2053 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2054 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2055 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
177c0ea7 2056
ee78dc32
GV
2057 if (NILP (item_name))
2058 {
2059 free_menubar_widget_value_tree (first_wv);
2060 *error = "Submenu in dialog items";
2061 return Qnil;
2062 }
2063 if (EQ (item_name, Qquote))
2064 {
2065 /* This is the boundary between left-side elts
2066 and right-side elts. Stop incrementing right_count. */
2067 boundary_seen = 1;
2068 i++;
2069 continue;
2070 }
485015ca 2071 if (nb_buttons >= 9)
ee78dc32
GV
2072 {
2073 free_menubar_widget_value_tree (first_wv);
2074 *error = "Too many dialog items";
2075 return Qnil;
2076 }
485015ca
GV
2077
2078 wv = xmalloc_widget_value ();
ee78dc32
GV
2079 prev_wv->next = wv;
2080 wv->name = (char *) button_names[nb_buttons];
2081 if (!NILP (descrip))
d5db4077
KR
2082 wv->key = (char *) SDATA (descrip);
2083 wv->value = (char *) SDATA (item_name);
6ed09cf0 2084 wv->call_data = (void *) &AREF (menu_items, i);
ee78dc32 2085 wv->enabled = !NILP (enable);
2d10309f 2086 wv->help = Qnil;
ee78dc32 2087 prev_wv = wv;
485015ca 2088
ee78dc32
GV
2089 if (! boundary_seen)
2090 left_count++;
485015ca 2091
ee78dc32
GV
2092 nb_buttons++;
2093 i += MENU_ITEMS_ITEM_LENGTH;
2094 }
485015ca 2095
ee78dc32
GV
2096 /* If the boundary was not specified,
2097 by default put half on the left and half on the right. */
2098 if (! boundary_seen)
2099 left_count = nb_buttons - nb_buttons / 2;
485015ca
GV
2100
2101 wv = xmalloc_widget_value ();
ee78dc32 2102 wv->name = dialog_name;
2d10309f 2103 wv->help = Qnil;
485015ca 2104
d23928c7
NR
2105 /* Frame title: 'Q' = Question, 'I' = Information.
2106 Can also have 'E' = Error if, one day, we want
2107 a popup for errors. */
2108 if (NILP(header))
2109 dialog_name[0] = 'Q';
2110 else
2111 dialog_name[0] = 'I';
2112
ee78dc32
GV
2113 /* Dialog boxes use a really stupid name encoding
2114 which specifies how many buttons to use
d23928c7 2115 and how many buttons are on the right. */
ee78dc32
GV
2116 dialog_name[1] = '0' + nb_buttons;
2117 dialog_name[2] = 'B';
2118 dialog_name[3] = 'R';
2119 /* Number of buttons to put on the right. */
2120 dialog_name[4] = '0' + nb_buttons - left_count;
2121 dialog_name[5] = 0;
2122 wv->contents = first_wv;
2123 first_wv = wv;
2124 }
485015ca 2125
ee78dc32 2126 /* Actually create the dialog. */
485015ca 2127 dialog_id = widget_id_tick++;
ee78dc32 2128 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
fbd6baed 2129 f->output_data.w32->widget, 1, 0,
ee78dc32 2130 dialog_selection_callback, 0);
02067692 2131 lw_modify_all_widgets (dialog_id, first_wv->contents, TRUE);
485015ca 2132
ee78dc32
GV
2133 /* Free the widget_value objects we used to specify the contents. */
2134 free_menubar_widget_value_tree (first_wv);
485015ca 2135
ee78dc32
GV
2136 /* No selection has been chosen yet. */
2137 menu_item_selection = 0;
485015ca 2138
ee78dc32
GV
2139 /* Display the menu. */
2140 lw_pop_up_all_widgets (dialog_id);
485015ca
GV
2141 popup_activated_flag = 1;
2142
ee78dc32 2143 /* Process events that apply to the menu. */
485015ca 2144 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
ee78dc32 2145
177c0ea7 2146 lw_destroy_all_widgets (dialog_id);
ee78dc32 2147
ee78dc32
GV
2148 /* Find the selected item, and its pane, to return
2149 the proper value. */
2150 if (menu_item_selection != 0)
2151 {
2152 Lisp_Object prefix;
485015ca 2153
ee78dc32
GV
2154 prefix = Qnil;
2155 i = 0;
2156 while (i < menu_items_used)
2157 {
2158 Lisp_Object entry;
2159
6ed09cf0 2160 if (EQ (AREF (menu_items, i), Qt))
ee78dc32 2161 {
6ed09cf0 2162 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
ee78dc32
GV
2163 i += MENU_ITEMS_PANE_LENGTH;
2164 }
2165 else
2166 {
6ed09cf0 2167 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
485015ca 2168 if (menu_item_selection == i)
ee78dc32
GV
2169 {
2170 if (keymaps != 0)
2171 {
2172 entry = Fcons (entry, Qnil);
2173 if (!NILP (prefix))
2174 entry = Fcons (prefix, entry);
2175 }
2176 return entry;
2177 }
2178 i += MENU_ITEMS_ITEM_LENGTH;
2179 }
2180 }
2181 }
485015ca 2182
ee78dc32
GV
2183 return Qnil;
2184}
ace9b298 2185#endif /* HAVE_DIALOGS */
485015ca
GV
2186\f
2187
2188/* Is this item a separator? */
2189static int
2190name_is_separator (name)
2191 char *name;
2192{
fe5a0162
SM
2193 char *start = name;
2194
2195 /* Check if name string consists of only dashes ('-'). */
485015ca 2196 while (*name == '-') name++;
fe5a0162
SM
2197 /* Separators can also be of the form "--:TripleSuperMegaEtched"
2198 or "--deep-shadow". We don't implement them yet, se we just treat
2199 them like normal separators. */
2200 return (*name == '\0' || start + 2 == name);
485015ca
GV
2201}
2202
2203
2204/* Indicate boundary between left and right. */
2205static int
2206add_left_right_boundary (HMENU menu)
2207{
2208 return AppendMenu (menu, MF_MENUBARBREAK, 0, NULL);
2209}
2210
1f06d367
JR
2211/* UTF8: 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx, 10xxxxxx, 10xxxxxx */
2212static void
2213utf8to16 (unsigned char * src, int len, WCHAR * dest)
2214{
2215 while (len > 0)
2216 {
2217 int utf16;
2218 if (*src < 0x80)
2219 {
2220 *dest = (WCHAR) *src;
2221 dest++; src++; len--;
2222 }
2223 /* Since we might get >3 byte sequences which we don't handle, ignore the extra parts. */
2224 else if (*src < 0xC0)
2225 {
2226 src++; len--;
2227 }
2228 /* 2 char UTF-8 sequence. */
2229 else if (*src < 0xE0)
2230 {
2231 *dest = (WCHAR) (((*src & 0x1f) << 6)
2232 | (*(src + 1) & 0x3f));
2233 src += 2; len -= 2; dest++;
2234 }
2235 else if (*src < 0xF0)
2236 {
2237 *dest = (WCHAR) (((*src & 0x0f) << 12)
2238 | ((*(src + 1) & 0x3f) << 6)
2239 | (*(src + 2) & 0x3f));
2240 src += 3; len -= 3; dest++;
2241 }
2242 else /* Not encodable. Insert Unicode Substitution char. */
2243 {
2244 *dest = (WCHAR) 0xfffd;
2245 src++; len--; dest++;
2246 }
2247 }
2248 *dest = 0;
2249}
2250
485015ca
GV
2251static int
2252add_menu_item (HMENU menu, widget_value *wv, HMENU item)
2253{
2254 UINT fuFlags;
2255 char *out_string;
37ad8b63 2256 int return_value;
485015ca
GV
2257
2258 if (name_is_separator (wv->name))
222456a1
JR
2259 {
2260 fuFlags = MF_SEPARATOR;
2261 out_string = NULL;
2262 }
177c0ea7 2263 else
485015ca 2264 {
070d1949 2265 if (wv->enabled)
485015ca
GV
2266 fuFlags = MF_STRING;
2267 else
2268 fuFlags = MF_STRING | MF_GRAYED;
2269
2270 if (wv->key != NULL)
2271 {
2272 out_string = alloca (strlen (wv->name) + strlen (wv->key) + 2);
2273 strcpy (out_string, wv->name);
2274 strcat (out_string, "\t");
2275 strcat (out_string, wv->key);
2276 }
2277 else
2278 out_string = wv->name;
2279
d1fffd1d
JR
2280 if (item != NULL)
2281 fuFlags = MF_POPUP;
2282 else if (wv->title || wv->call_data == 0)
485015ca 2283 {
ace9b298
JR
2284 /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since
2285 we can't deallocate the memory otherwise. */
2286 if (get_menu_item_info)
2287 {
6ed09cf0
RS
2288 out_string = (char *) local_alloc (strlen (wv->name) + 1);
2289 strcpy (out_string, wv->name);
d1fffd1d 2290#ifdef MENU_DEBUG
6ed09cf0 2291 DebPrint ("Menu: allocing %ld for owner-draw", out_string);
d1fffd1d 2292#endif
ace9b298
JR
2293 fuFlags = MF_OWNERDRAW | MF_DISABLED;
2294 }
2295 else
2296 fuFlags = MF_DISABLED;
485015ca 2297 }
1c5acb8c 2298
162d2499 2299 /* Draw radio buttons and tickboxes. */
4cdf6a6c 2300 else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
6f665da9 2301 wv->button_type == BUTTON_TYPE_RADIO))
4cdf6a6c 2302 fuFlags |= MF_CHECKED;
37ad8b63 2303 else
4cdf6a6c 2304 fuFlags |= MF_UNCHECKED;
485015ca 2305 }
4cdf6a6c 2306
1f06d367
JR
2307 if (unicode_append_menu && out_string)
2308 {
2309 /* Convert out_string from UTF-8 to UTF-16-LE. */
2310 int utf8_len = strlen (out_string);
2311 WCHAR * utf16_string;
2312 if (fuFlags & MF_OWNERDRAW)
2313 utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR));
2314 else
2315 utf16_string = alloca ((utf8_len + 1) * sizeof (WCHAR));
2316
2317 utf8to16 (out_string, utf8_len, utf16_string);
2318 return_value = unicode_append_menu (menu, fuFlags,
2319 item != NULL ? (UINT) item
2320 : (UINT) wv->call_data,
2321 utf16_string);
2322 if (fuFlags & MF_OWNERDRAW)
2323 local_free (out_string);
2324 }
2325 else
2326 {
2327 return_value =
2328 AppendMenu (menu,
2329 fuFlags,
2330 item != NULL ? (UINT) item : (UINT) wv->call_data,
2331 out_string );
2332 }
37ad8b63
JR
2333
2334 /* This must be done after the menu item is created. */
6f665da9 2335 if (!wv->title && wv->call_data != 0)
4cdf6a6c 2336 {
4cdf6a6c
AI
2337 if (set_menu_item_info)
2338 {
2339 MENUITEMINFO info;
2340 bzero (&info, sizeof (info));
2341 info.cbSize = sizeof (info);
2342 info.fMask = MIIM_DATA;
2343
d1fffd1d
JR
2344 /* Set help string for menu item. Leave it as a Lisp_Object
2345 until it is ready to be displayed, since GC can happen while
2346 menus are active. */
24ee9763
JR
2347 if (!NILP (wv->help))
2348#ifdef USE_LISP_UNION_TYPE
2349 info.dwItemData = (DWORD) (wv->help).i;
2350#else
2351 info.dwItemData = (DWORD) (wv->help);
2352#endif
4cdf6a6c
AI
2353 if (wv->button_type == BUTTON_TYPE_RADIO)
2354 {
2355 /* CheckMenuRadioItem allows us to differentiate TOGGLE and
2356 RADIO items, but is not available on NT 3.51 and earlier. */
2357 info.fMask |= MIIM_TYPE | MIIM_STATE;
2358 info.fType = MFT_RADIOCHECK | MFT_STRING;
2359 info.dwTypeData = out_string;
2360 info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED;
2361 }
2362
2363 set_menu_item_info (menu,
2364 item != NULL ? (UINT) item : (UINT) wv->call_data,
2365 FALSE, &info);
2366 }
2367 }
37ad8b63 2368 return return_value;
485015ca
GV
2369}
2370
2371/* Construct native Windows menu(bar) based on widget_value tree. */
15d36dee 2372int
485015ca
GV
2373fill_in_menu (HMENU menu, widget_value *wv)
2374{
2375 int items_added = 0;
ee78dc32 2376
485015ca
GV
2377 for ( ; wv != NULL; wv = wv->next)
2378 {
2379 if (wv->contents)
2380 {
2381 HMENU sub_menu = CreatePopupMenu ();
2382
2383 if (sub_menu == NULL)
2384 return 0;
2385
2386 if (!fill_in_menu (sub_menu, wv->contents) ||
2387 !add_menu_item (menu, wv, sub_menu))
2388 {
2389 DestroyMenu (sub_menu);
2390 return 0;
2391 }
2392 }
2393 else
2394 {
2395 if (!add_menu_item (menu, wv, NULL))
2396 return 0;
2397 }
2398 }
2399 return 1;
2400}
2401
162d2499
JR
2402int
2403popup_activated ()
2404{
2405 /* popup_activated_flag not actually used on W32 */
2406 return 0;
2407}
2408
7d8e57da
JR
2409/* Display help string for currently pointed to menu item. Not
2410 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
2411 available. */
2412void
59a86c99 2413w32_menu_display_help (HWND owner, HMENU menu, UINT item, UINT flags)
7d8e57da 2414{
7d8e57da
JR
2415 if (get_menu_item_info)
2416 {
1c5acb8c
JR
2417 struct frame *f = x_window_to_frame (&one_w32_display_info, owner);
2418 Lisp_Object frame, help;
7d8e57da 2419
1f06d367 2420 /* No help echo on owner-draw menu items. */
ace9b298
JR
2421 if (flags & MF_OWNERDRAW || flags & MF_POPUP)
2422 help = Qnil;
2423 else
2424 {
2425 MENUITEMINFO info;
7d8e57da 2426
ace9b298
JR
2427 bzero (&info, sizeof (info));
2428 info.cbSize = sizeof (info);
2429 info.fMask = MIIM_DATA;
2430 get_menu_item_info (menu, item, FALSE, &info);
2431
24ee9763
JR
2432#ifdef USE_LISP_UNION_TYPE
2433 help = info.dwItemData ? (Lisp_Object) ((EMACS_INT) info.dwItemData)
2434 : Qnil;
2435#else
d1fffd1d 2436 help = info.dwItemData ? (Lisp_Object) info.dwItemData : Qnil;
24ee9763 2437#endif
ace9b298 2438 }
e5cd3d11 2439
1c5acb8c
JR
2440 /* Store the help echo in the keyboard buffer as the X toolkit
2441 version does, rather than directly showing it. This seems to
2442 solve the GC problems that were present when we based the
2443 Windows code on the non-toolkit version. */
2444 if (f)
2445 {
2446 XSETFRAME (frame, f);
2447 kbd_buffer_store_help_event (frame, help);
2448 }
2449 else
2450 /* X version has a loop through frames here, which doesn't
2451 appear to do anything, unless it has some side effect. */
2452 show_help_echo (help, Qnil, Qnil, Qnil, 1);
7d8e57da
JR
2453 }
2454}
2455
d1fffd1d 2456/* Free memory used by owner-drawn strings. */
ace9b298
JR
2457static void
2458w32_free_submenu_strings (menu)
2459 HMENU menu;
2460{
2461 int i, num = GetMenuItemCount (menu);
2462 for (i = 0; i < num; i++)
2463 {
2464 MENUITEMINFO info;
37647b2d 2465 bzero (&info, sizeof (info));
ace9b298 2466 info.cbSize = sizeof (info);
d1fffd1d 2467 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_SUBMENU;
7d8e57da 2468
ace9b298
JR
2469 get_menu_item_info (menu, i, TRUE, &info);
2470
d1fffd1d
JR
2471 /* Owner-drawn names are held in dwItemData. */
2472 if ((info.fType & MF_OWNERDRAW) && info.dwItemData)
2473 {
2474#ifdef MENU_DEBUG
2475 DebPrint ("Menu: freeing %ld for owner-draw", info.dwItemData);
2476#endif
6ed09cf0 2477 local_free (info.dwItemData);
d1fffd1d 2478 }
ace9b298
JR
2479
2480 /* Recurse down submenus. */
2481 if (info.hSubMenu)
2482 w32_free_submenu_strings (info.hSubMenu);
2483 }
2484}
2485
2486void
2487w32_free_menu_strings (hwnd)
2488 HWND hwnd;
2489{
2490 HMENU menu = current_popup_menu;
2491
2492 if (get_menu_item_info)
2493 {
2494 /* If there is no popup menu active, free the strings from the frame's
2495 menubar. */
2496 if (!menu)
2497 menu = GetMenu (hwnd);
2498
2499 if (menu)
2500 w32_free_submenu_strings (menu);
2501 }
2502
2503 current_popup_menu = NULL;
2504}
7d8e57da 2505
485015ca 2506#endif /* HAVE_MENUS */
ace9b298 2507
f60ae425 2508void syms_of_w32menu ()
ee78dc32 2509{
9785d95b 2510 globals_of_w32menu ();
485015ca
GV
2511 staticpro (&menu_items);
2512 menu_items = Qnil;
2513
ace9b298
JR
2514 current_popup_menu = NULL;
2515
fdc12c4d
RS
2516 Qdebug_on_next_call = intern ("debug-on-next-call");
2517 staticpro (&Qdebug_on_next_call);
2518
c8869655 2519 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
33f09670
JR
2520 doc: /* Frame for which we are updating a menu.
2521The enable predicate for a menu command should check this variable. */);
c8869655
GV
2522 Vmenu_updating_frame = Qnil;
2523
ee78dc32 2524 defsubr (&Sx_popup_menu);
485015ca 2525#ifdef HAVE_MENUS
ee78dc32 2526 defsubr (&Sx_popup_dialog);
485015ca 2527#endif
ee78dc32 2528}
9785d95b
BK
2529
2530/*
2531 globals_of_w32menu is used to initialize those global variables that
2532 must always be initialized on startup even when the global variable
2533 initialized is non zero (see the function main in emacs.c).
2534 globals_of_w32menu is called from syms_of_w32menu when the global
2535 variable initialized is 0 and directly from main when initialized
2536 is non zero.
2537 */
2538void globals_of_w32menu ()
2539{
2540 /* See if Get/SetMenuItemInfo functions are available. */
2541 HMODULE user32 = GetModuleHandle ("user32.dll");
2542 get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA");
2543 set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA");
1f06d367 2544 unicode_append_menu = (AppendMenuW_Proc) GetProcAddress (user32, "AppendMenuW");
9785d95b 2545}
ab5796a9
MB
2546
2547/* arch-tag: 0eaed431-bb4e-4aac-a527-95a1b4f1fed0
2548 (do not change this comment) */