Fixes: debbugs:17865
[bpt/emacs.git] / src / w32menu.c
CommitLineData
b46a6a83 1/* Menu support for GNU Emacs on the Microsoft Windows API.
ba318903 2 Copyright (C) 1986, 1988, 1993-1994, 1996, 1998-1999, 2001-2014 Free
ab422c4d 3 Software Foundation, Inc.
ee78dc32
GV
4
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
ee78dc32 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
ee78dc32
GV
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
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
ee78dc32 19
ee78dc32 20#include <config.h>
162d2499 21
7ab7006c 22#include <signal.h>
ee78dc32 23#include <stdio.h>
d7306fe6 24#include <setjmp.h>
7ab7006c 25
ee78dc32 26#include "lisp.h"
15d36dee 27#include "keyboard.h"
5ee707b8 28#include "keymap.h"
ee78dc32 29#include "frame.h"
7ab7006c 30#include "termhooks.h"
ee78dc32 31#include "window.h"
ee78dc32 32#include "blockinput.h"
e5560ff7 33#include "character.h"
fdc12c4d 34#include "buffer.h"
6915ded0
JR
35#include "charset.h"
36#include "coding.h"
ef7417fd 37#include "menu.h"
ee78dc32
GV
38
39/* This may include sys/types.h, and that somehow loses
40 if this is not done before the other system files. */
41#include "w32term.h"
42
0fda9b75
DC
43/* Cygwin does not support the multibyte string functions declared in
44 * mbstring.h below --- but that's okay: because Cygwin is
45 * UNICODE-only, we don't need to use these functions anyway. */
46
47#ifndef NTGUI_UNICODE
48#include <mbstring.h>
49#endif /* !NTGUI_UNICODE */
50
ee78dc32
GV
51/* Load sys/types.h if not already loaded.
52 In some systems loading it twice is suicidal. */
53#ifndef makedev
54#include <sys/types.h>
55#endif
56
57#include "dispextern.h"
58
501199a3 59#include "w32common.h" /* for osinfo_cache */
1c6900d9 60
222456a1 61#undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
162d2499 62
485015ca
GV
63#ifndef TRUE
64#define TRUE 1
65#define FALSE 0
66#endif /* no TRUE */
67
b2a916a0 68HMENU current_popup_menu;
ace9b298 69
b56ceb92
JB
70void syms_of_w32menu (void);
71void globals_of_w32menu (void);
f60ae425
BK
72
73typedef BOOL (WINAPI * GetMenuItemInfoA_Proc) (
74 IN HMENU,
75 IN UINT,
76 IN BOOL,
1f06d367 77 IN OUT LPMENUITEMINFOA);
f60ae425
BK
78typedef BOOL (WINAPI * SetMenuItemInfoA_Proc) (
79 IN HMENU,
80 IN UINT,
81 IN BOOL,
1f06d367 82 IN LPCMENUITEMINFOA);
1c9b4129
JR
83typedef int (WINAPI * MessageBoxW_Proc) (
84 IN HWND window,
af025ae8
EZ
85 IN const WCHAR *text,
86 IN const WCHAR *caption,
1c9b4129 87 IN UINT type);
f60ae425 88
0fda9b75
DC
89#ifdef NTGUI_UNICODE
90#define get_menu_item_info GetMenuItemInfoA
91#define set_menu_item_info SetMenuItemInfoA
92#define unicode_append_menu AppendMenuW
93#define unicode_message_box MessageBoxW
94#else /* !NTGUI_UNICODE */
1f06d367
JR
95GetMenuItemInfoA_Proc get_menu_item_info = NULL;
96SetMenuItemInfoA_Proc set_menu_item_info = NULL;
97AppendMenuW_Proc unicode_append_menu = NULL;
1c9b4129 98MessageBoxW_Proc unicode_message_box = NULL;
0fda9b75 99#endif /* NTGUI_UNICODE */
ace9b298 100
e139a33c 101Lisp_Object Qdebug_on_next_call, Qunsupported__w32_dialog;
fdc12c4d 102
a10c8269 103void set_frame_menubar (struct frame *, bool, bool);
014510b0 104
1c5acb8c 105#ifdef HAVE_DIALOGS
a10c8269 106static Lisp_Object w32_dialog_show (struct frame *, int, Lisp_Object, char**);
31403b24 107#else
f57e2426 108static int is_simple_dialog (Lisp_Object);
a10c8269 109static Lisp_Object simple_dialog_show (struct frame *, Lisp_Object, Lisp_Object);
1c5acb8c 110#endif
31403b24 111
1c9b4129 112static void utf8to16 (unsigned char *, int, WCHAR *);
24f981c9 113static int fill_in_menu (HMENU, widget_value *);
1c9b4129 114
f57e2426 115void w32_free_menu_strings (HWND);
0fda9b75 116
0afa0aab
EZ
117Lisp_Object
118w32_popup_dialog (struct frame *f, Lisp_Object header, Lisp_Object contents)
ee78dc32 119{
485015ca 120
7452b7bd
DA
121 check_window_system (f);
122
e139a33c 123#ifndef HAVE_DIALOGS
ee78dc32 124
e139a33c
EZ
125 /* Handle simple Yes/No choices as MessageBox popups. */
126 if (is_simple_dialog (contents))
127 return simple_dialog_show (f, contents, header);
128 else
129 return Qunsupported__w32_dialog;
130#else /* HAVE_DIALOGS */
131 {
132 Lisp_Object title;
133 char *error_name;
134 Lisp_Object selection;
ee78dc32 135
e139a33c
EZ
136 /* Decode the dialog items from what was specified. */
137 title = Fcar (contents);
138 CHECK_STRING (title);
ee78dc32 139
e139a33c 140 list_of_panes (Fcons (contents, Qnil));
ee78dc32 141
e139a33c
EZ
142 /* Display them in a dialog box. */
143 block_input ();
144 selection = w32_dialog_show (f, 0, title, header, &error_name);
145 unblock_input ();
146
147 discard_menu_items ();
148 FRAME_DISPLAY_INFO (f)->grabbed = 0;
149
150 if (error_name) error (error_name);
151 return selection;
152 }
0afa0aab 153#endif /* HAVE_DIALOGS */
e139a33c 154}
ee78dc32 155
014510b0
GV
156/* Activate the menu bar of frame F.
157 This is called from keyboard.c when it gets the
3b8f9651 158 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
014510b0
GV
159
160 To activate the menu bar, we signal to the input thread that it can
161 return from the WM_INITMENU message, allowing the normal Windows
162 processing of the menus.
163
164 But first we recompute the menu bar contents (the whole tree).
165
166 This way we can safely execute Lisp code. */
177c0ea7 167
162d2499 168void
a10c8269 169x_activate_menubar (struct frame *f)
014510b0
GV
170{
171 set_frame_menubar (f, 0, 1);
172
173 /* Lock out further menubar changes while active. */
174 f->output_data.w32->menubar_active = 1;
175
176 /* Signal input thread to return from WM_INITMENU. */
177 complete_deferred_msg (FRAME_W32_WINDOW (f), WM_INITMENU, 0);
178}
179
485015ca
GV
180/* This callback is called from the menu bar pulldown menu
181 when the user makes a selection.
182 Figure out what the user chose
183 and put the appropriate events into the keyboard buffer. */
184
185void
a10c8269 186menubar_selection_callback (struct frame *f, void * client_data)
ee78dc32 187{
485015ca
GV
188 Lisp_Object prefix, entry;
189 Lisp_Object vector;
190 Lisp_Object *subprefix_stack;
191 int submenu_depth = 0;
ee78dc32 192 int i;
fdc12c4d 193
485015ca 194 if (!f)
014510b0 195 return;
fe5a0162 196 entry = Qnil;
663e2b3f 197 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * word_size);
e69b0960 198 vector = f->menu_bar_vector;
485015ca
GV
199 prefix = Qnil;
200 i = 0;
201 while (i < f->menu_bar_items_used)
202 {
6ed09cf0 203 if (EQ (AREF (vector, i), Qnil))
485015ca
GV
204 {
205 subprefix_stack[submenu_depth++] = prefix;
206 prefix = entry;
207 i++;
208 }
6ed09cf0 209 else if (EQ (AREF (vector, i), Qlambda))
485015ca
GV
210 {
211 prefix = subprefix_stack[--submenu_depth];
212 i++;
213 }
6ed09cf0 214 else if (EQ (AREF (vector, i), Qt))
485015ca 215 {
6ed09cf0 216 prefix = AREF (vector, i + MENU_ITEMS_PANE_PREFIX);
485015ca
GV
217 i += MENU_ITEMS_PANE_LENGTH;
218 }
219 else
220 {
6ed09cf0 221 entry = AREF (vector, i + MENU_ITEMS_ITEM_VALUE);
485015ca
GV
222 /* The EMACS_INT cast avoids a warning. There's no problem
223 as long as pointers have enough bits to hold small integers. */
224 if ((int) (EMACS_INT) client_data == i)
225 {
226 int j;
227 struct input_event buf;
228 Lisp_Object frame;
f456401b 229 EVENT_INIT (buf);
014510b0 230
485015ca 231 XSETFRAME (frame, f);
20501278
GM
232 buf.kind = MENU_BAR_EVENT;
233 buf.frame_or_window = frame;
234 buf.arg = frame;
485015ca 235 kbd_buffer_store_event (&buf);
014510b0 236
485015ca
GV
237 for (j = 0; j < submenu_depth; j++)
238 if (!NILP (subprefix_stack[j]))
239 {
20501278
GM
240 buf.kind = MENU_BAR_EVENT;
241 buf.frame_or_window = frame;
242 buf.arg = subprefix_stack[j];
485015ca
GV
243 kbd_buffer_store_event (&buf);
244 }
fdc12c4d 245
485015ca
GV
246 if (!NILP (prefix))
247 {
20501278
GM
248 buf.kind = MENU_BAR_EVENT;
249 buf.frame_or_window = frame;
250 buf.arg = prefix;
485015ca
GV
251 kbd_buffer_store_event (&buf);
252 }
fdc12c4d 253
20501278
GM
254 buf.kind = MENU_BAR_EVENT;
255 buf.frame_or_window = frame;
256 buf.arg = entry;
71f6b295
JR
257 /* Free memory used by owner-drawn and help-echo strings. */
258 w32_free_menu_strings (FRAME_W32_WINDOW (f));
b2a916a0
JR
259 kbd_buffer_store_event (&buf);
260
71f6b295 261 f->output_data.w32->menubar_active = 0;
485015ca
GV
262 return;
263 }
264 i += MENU_ITEMS_ITEM_LENGTH;
265 }
014510b0 266 }
71f6b295
JR
267 /* Free memory used by owner-drawn and help-echo strings. */
268 w32_free_menu_strings (FRAME_W32_WINDOW (f));
71f6b295 269 f->output_data.w32->menubar_active = 0;
485015ca 270}
014510b0 271
ee78dc32 272\f
485015ca
GV
273/* Set the contents of the menubar widgets of frame F.
274 The argument FIRST_TIME is currently ignored;
275 it is set the first time this is called, from initialize_frame_menubar. */
276
277void
a10c8269 278set_frame_menubar (struct frame *f, bool first_time, bool deep_p)
485015ca
GV
279{
280 HMENU menubar_widget = f->output_data.w32->menubar_widget;
162d2499 281 Lisp_Object items;
485015ca 282 widget_value *wv, *first_wv, *prev_wv = 0;
6ed09cf0
RS
283 int i, last_i;
284 int *submenu_start, *submenu_end;
fe04b8c8 285 int *submenu_top_level_items, *submenu_n_panes;
485015ca
GV
286
287 /* We must not change the menubar when actually in use. */
288 if (f->output_data.w32->menubar_active)
289 return;
290
291 XSETFRAME (Vmenu_updating_frame, f);
292
293 if (! menubar_widget)
294 deep_p = 1;
485015ca 295
485015ca
GV
296 if (deep_p)
297 {
298 /* Make a widget-value tree representing the entire menu trees. */
299
300 struct buffer *prev = current_buffer;
301 Lisp_Object buffer;
d311d28c 302 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
485015ca
GV
303 int previous_menu_items_used = f->menu_bar_items_used;
304 Lisp_Object *previous_items
305 = (Lisp_Object *) alloca (previous_menu_items_used
663e2b3f 306 * word_size);
485015ca
GV
307
308 /* If we are making a new widget, its contents are empty,
309 do always reinitialize them. */
310 if (! menubar_widget)
311 previous_menu_items_used = 0;
312
e74aeda8 313 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->contents;
485015ca
GV
314 specbind (Qinhibit_quit, Qt);
315 /* Don't let the debugger step into this code
316 because it is not reentrant. */
317 specbind (Qdebug_on_next_call, Qnil);
318
89f2614d
KS
319 record_unwind_save_match_data ();
320
485015ca
GV
321 if (NILP (Voverriding_local_map_menu_flag))
322 {
323 specbind (Qoverriding_terminal_local_map, Qnil);
324 specbind (Qoverriding_local_map, Qnil);
325 }
326
327 set_buffer_internal_1 (XBUFFER (buffer));
328
cc477da7 329 /* Run the hooks. */
c53b6500 330 safe_run_hooks (Qactivate_menubar_hook);
485015ca 331 safe_run_hooks (Qmenu_bar_update_hook);
f00af5b1 332 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
485015ca
GV
333
334 items = FRAME_MENU_BAR_ITEMS (f);
335
485015ca 336 /* Save the frame's previous menu bar contents data. */
c30dce3d 337 if (previous_menu_items_used)
91f2d272 338 memcpy (previous_items, XVECTOR (f->menu_bar_vector)->contents,
663e2b3f 339 previous_menu_items_used * word_size);
485015ca 340
6ed09cf0
RS
341 /* Fill in menu_items with the current menu bar contents.
342 This can evaluate Lisp code. */
df80fd9d
JR
343 save_menu_items ();
344
e69b0960 345 menu_items = f->menu_bar_vector;
c30dce3d 346 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
57679c86
AS
347 submenu_start = (int *) alloca (ASIZE (items) * sizeof (int));
348 submenu_end = (int *) alloca (ASIZE (items) * sizeof (int));
77b37c05 349 submenu_n_panes = (int *) alloca (ASIZE (items) * sizeof (int));
57679c86 350 submenu_top_level_items = (int *) alloca (ASIZE (items) * sizeof (int));
485015ca 351 init_menu_items ();
6ed09cf0 352 for (i = 0; i < ASIZE (items); i += 4)
485015ca
GV
353 {
354 Lisp_Object key, string, maps;
355
6ed09cf0
RS
356 last_i = i;
357
358 key = AREF (items, i);
359 string = AREF (items, i + 1);
360 maps = AREF (items, i + 2);
485015ca
GV
361 if (NILP (string))
362 break;
363
6ed09cf0
RS
364 submenu_start[i] = menu_items_used;
365
366 menu_items_n_panes = 0;
367 submenu_top_level_items[i]
368 = parse_single_submenu (key, string, maps);
fe04b8c8 369 submenu_n_panes[i] = menu_items_n_panes;
6ed09cf0
RS
370
371 submenu_end[i] = menu_items_used;
372 }
373
374 finish_menu_items ();
375
376 /* Convert menu_items into widget_value trees
377 to display the menu. This cannot evaluate Lisp code. */
378
379 wv = xmalloc_widget_value ();
380 wv->name = "menubar";
381 wv->value = 0;
382 wv->enabled = 1;
383 wv->button_type = BUTTON_TYPE_NONE;
384 wv->help = Qnil;
385 first_wv = wv;
386
387 for (i = 0; i < last_i; i += 4)
388 {
fe04b8c8 389 menu_items_n_panes = submenu_n_panes[i];
6ed09cf0
RS
390 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
391 submenu_top_level_items[i]);
177c0ea7 392 if (prev_wv)
485015ca
GV
393 prev_wv->next = wv;
394 else
395 first_wv->contents = wv;
396 /* Don't set wv->name here; GC during the loop might relocate it. */
397 wv->enabled = 1;
162d2499 398 wv->button_type = BUTTON_TYPE_NONE;
485015ca
GV
399 prev_wv = wv;
400 }
401
485015ca 402 set_buffer_internal_1 (prev);
485015ca
GV
403
404 /* If there has been no change in the Lisp-level contents
405 of the menu bar, skip redisplaying it. Just exit. */
406
407 for (i = 0; i < previous_menu_items_used; i++)
408 if (menu_items_used == i
6ed09cf0 409 || (!EQ (previous_items[i], AREF (menu_items, i))))
485015ca
GV
410 break;
411 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
412 {
413 free_menubar_widget_value_tree (first_wv);
df80fd9d
JR
414 discard_menu_items ();
415 unbind_to (specpdl_count, Qnil);
485015ca
GV
416 return;
417 }
418
f00af5b1 419 fset_menu_bar_vector (f, menu_items);
df80fd9d
JR
420 f->menu_bar_items_used = menu_items_used;
421
422 /* This undoes save_menu_items. */
423 unbind_to (specpdl_count, Qnil);
424
485015ca 425 /* Now GC cannot happen during the lifetime of the widget_value,
ace9b298
JR
426 so it's safe to store data from a Lisp_String, as long as
427 local copies are made when the actual menu is created.
428 Windows takes care of this for normal string items, but
429 not for owner-drawn items or additional item-info. */
485015ca 430 wv = first_wv->contents;
6ed09cf0 431 for (i = 0; i < ASIZE (items); i += 4)
485015ca
GV
432 {
433 Lisp_Object string;
6ed09cf0 434 string = AREF (items, i + 1);
485015ca
GV
435 if (NILP (string))
436 break;
51b59d79 437 wv->name = SSDATA (string);
1f06d367 438 update_submenu_strings (wv->contents);
485015ca
GV
439 wv = wv->next;
440 }
485015ca
GV
441 }
442 else
443 {
444 /* Make a widget-value tree containing
162d2499 445 just the top level menu bar strings. */
485015ca 446
6ed09cf0
RS
447 wv = xmalloc_widget_value ();
448 wv->name = "menubar";
449 wv->value = 0;
450 wv->enabled = 1;
451 wv->button_type = BUTTON_TYPE_NONE;
452 wv->help = Qnil;
453 first_wv = wv;
454
485015ca 455 items = FRAME_MENU_BAR_ITEMS (f);
6ed09cf0 456 for (i = 0; i < ASIZE (items); i += 4)
485015ca
GV
457 {
458 Lisp_Object string;
459
6ed09cf0 460 string = AREF (items, i + 1);
485015ca
GV
461 if (NILP (string))
462 break;
463
464 wv = xmalloc_widget_value ();
51b59d79 465 wv->name = SSDATA (string);
485015ca
GV
466 wv->value = 0;
467 wv->enabled = 1;
162d2499 468 wv->button_type = BUTTON_TYPE_NONE;
2d10309f 469 wv->help = Qnil;
485015ca
GV
470 /* This prevents lwlib from assuming this
471 menu item is really supposed to be empty. */
472 /* The EMACS_INT cast avoids a warning.
473 This value just has to be different from small integers. */
474 wv->call_data = (void *) (EMACS_INT) (-1);
475
177c0ea7 476 if (prev_wv)
485015ca
GV
477 prev_wv->next = wv;
478 else
479 first_wv->contents = wv;
480 prev_wv = wv;
481 }
482
162d2499
JR
483 /* Forget what we thought we knew about what is in the
484 detailed contents of the menu bar menus.
485 Changing the top level always destroys the contents. */
486 f->menu_bar_items_used = 0;
485015ca
GV
487 }
488
489 /* Create or update the menu bar widget. */
ee78dc32 490
4d7e6e51 491 block_input ();
ee78dc32 492
485015ca
GV
493 if (menubar_widget)
494 {
495 /* Empty current menubar, rather than creating a fresh one. */
496 while (DeleteMenu (menubar_widget, 0, MF_BYPOSITION))
497 ;
498 }
499 else
500 {
501 menubar_widget = CreateMenu ();
502 }
503 fill_in_menu (menubar_widget, first_wv->contents);
ee78dc32 504
485015ca 505 free_menubar_widget_value_tree (first_wv);
ee78dc32 506
485015ca
GV
507 {
508 HMENU old_widget = f->output_data.w32->menubar_widget;
ee78dc32 509
485015ca
GV
510 f->output_data.w32->menubar_widget = menubar_widget;
511 SetMenu (FRAME_W32_WINDOW (f), f->output_data.w32->menubar_widget);
177c0ea7 512 /* Causes flicker when menu bar is updated
485015ca 513 DrawMenuBar (FRAME_W32_WINDOW (f)); */
ee78dc32 514
485015ca
GV
515 /* Force the window size to be recomputed so that the frame's text
516 area remains the same, if menubar has just been created. */
517 if (old_widget == NULL)
880e6158 518 x_set_window_size (f, 0, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 1);
ee78dc32 519 }
485015ca 520
4d7e6e51 521 unblock_input ();
ee78dc32
GV
522}
523
485015ca
GV
524/* Called from Fx_create_frame to create the initial menubar of a frame
525 before it is mapped, so that the window is mapped with the menubar already
526 there instead of us tacking it on later and thrashing the window after it
527 is visible. */
528
529void
a10c8269 530initialize_frame_menubar (struct frame *f)
485015ca
GV
531{
532 /* This function is called before the first chance to redisplay
533 the frame. It has to be, so the frame will have the right size. */
f00af5b1 534 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
485015ca 535 set_frame_menubar (f, 1, 1);
ee78dc32
GV
536}
537
485015ca
GV
538/* Get rid of the menu bar of frame F, and free its storage.
539 This is used when deleting a frame, and when turning off the menu bar. */
540
541void
a10c8269 542free_frame_menubar (struct frame *f)
485015ca 543{
4d7e6e51 544 block_input ();
485015ca
GV
545
546 {
547 HMENU old = GetMenu (FRAME_W32_WINDOW (f));
548 SetMenu (FRAME_W32_WINDOW (f), NULL);
549 f->output_data.w32->menubar_widget = NULL;
550 DestroyMenu (old);
551 }
29250851 552
4d7e6e51 553 unblock_input ();
485015ca 554}
ee78dc32 555
485015ca
GV
556\f
557/* w32_menu_show actually displays a menu using the panes and items in
558 menu_items and returns the value selected from it; we assume input
559 is blocked by the caller. */
ee78dc32
GV
560
561/* F is the frame the menu is for.
562 X and Y are the frame-relative specified position,
563 relative to the inside upper left corner of the frame F.
485015ca 564 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
ee78dc32 565 KEYMAPS is 1 if this menu was specified with keymaps;
485015ca
GV
566 in that case, we return a list containing the chosen item's value
567 and perhaps also the pane's prefix.
ee78dc32
GV
568 TITLE is the specified menu title.
569 ERROR is a place to store an error message string in case of failure.
570 (We return nil on failure, but the value doesn't actually matter.) */
571
ef7417fd 572Lisp_Object
a10c8269 573w32_menu_show (struct frame *f, int x, int y, int for_click, int keymaps,
42ca4633 574 Lisp_Object title, const char **error)
ee78dc32 575{
485015ca
GV
576 int i;
577 int menu_item_selection;
578 HMENU menu;
ee78dc32 579 POINT pos;
485015ca
GV
580 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
581 widget_value **submenu_stack
582 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
583 Lisp_Object *subprefix_stack
663e2b3f 584 = (Lisp_Object *) alloca (menu_items_used * word_size);
485015ca 585 int submenu_depth = 0;
485015ca 586 int first_pane;
485015ca 587
ee78dc32 588 *error = NULL;
485015ca 589
df80fd9d
JR
590 if (menu_items_n_panes == 0)
591 return Qnil;
592
485015ca 593 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
ee78dc32
GV
594 {
595 *error = "Empty menu";
596 return Qnil;
597 }
485015ca 598
f0177f86
EZ
599 block_input ();
600
485015ca
GV
601 /* Create a tree of widget_value objects
602 representing the panes and their items. */
603 wv = xmalloc_widget_value ();
604 wv->name = "menu";
605 wv->value = 0;
606 wv->enabled = 1;
162d2499 607 wv->button_type = BUTTON_TYPE_NONE;
e6bb685b 608 wv->help = Qnil;
485015ca
GV
609 first_wv = wv;
610 first_pane = 1;
177c0ea7 611
485015ca
GV
612 /* Loop over all panes and items, filling in the tree. */
613 i = 0;
614 while (i < menu_items_used)
615 {
6ed09cf0 616 if (EQ (AREF (menu_items, i), Qnil))
485015ca
GV
617 {
618 submenu_stack[submenu_depth++] = save_wv;
619 save_wv = prev_wv;
620 prev_wv = 0;
621 first_pane = 1;
622 i++;
623 }
6ed09cf0 624 else if (EQ (AREF (menu_items, i), Qlambda))
485015ca
GV
625 {
626 prev_wv = save_wv;
627 save_wv = submenu_stack[--submenu_depth];
628 first_pane = 0;
629 i++;
630 }
6ed09cf0 631 else if (EQ (AREF (menu_items, i), Qt)
485015ca
GV
632 && submenu_depth != 0)
633 i += MENU_ITEMS_PANE_LENGTH;
634 /* Ignore a nil in the item list.
635 It's meaningful only for dialog boxes. */
6ed09cf0 636 else if (EQ (AREF (menu_items, i), Qquote))
485015ca 637 i += 1;
6ed09cf0 638 else if (EQ (AREF (menu_items, i), Qt))
485015ca
GV
639 {
640 /* Create a new pane. */
641 Lisp_Object pane_name, prefix;
af025ae8 642 const char *pane_string;
29250851
JR
643 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
644 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1f06d367
JR
645
646 if (STRINGP (pane_name))
29250851 647 {
1f06d367
JR
648 if (unicode_append_menu)
649 pane_name = ENCODE_UTF_8 (pane_name);
650 else if (STRING_MULTIBYTE (pane_name))
651 pane_name = ENCODE_SYSTEM (pane_name);
652
6ed09cf0 653 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
29250851 654 }
1f06d367 655
485015ca 656 pane_string = (NILP (pane_name)
51b59d79 657 ? "" : SSDATA (pane_name));
485015ca
GV
658 /* If there is just one top-level pane, put all its items directly
659 under the top-level menu. */
660 if (menu_items_n_panes == 1)
661 pane_string = "";
662
663 /* If the pane has a meaningful name,
664 make the pane a top-level menu item
665 with its items as a submenu beneath it. */
666 if (!keymaps && strcmp (pane_string, ""))
667 {
668 wv = xmalloc_widget_value ();
669 if (save_wv)
670 save_wv->next = wv;
671 else
672 first_wv->contents = wv;
673 wv->name = pane_string;
674 if (keymaps && !NILP (prefix))
675 wv->name++;
676 wv->value = 0;
677 wv->enabled = 1;
162d2499 678 wv->button_type = BUTTON_TYPE_NONE;
2d10309f 679 wv->help = Qnil;
485015ca
GV
680 save_wv = wv;
681 prev_wv = 0;
682 }
683 else if (first_pane)
684 {
685 save_wv = wv;
686 prev_wv = 0;
687 }
688 first_pane = 0;
689 i += MENU_ITEMS_PANE_LENGTH;
690 }
691 else
692 {
693 /* Create a new item within current pane. */
b65a2f83 694 Lisp_Object item_name, enable, descrip, def, type, selected, help;
b65a2f83 695
29250851
JR
696 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
697 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
698 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
699 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
700 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
701 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
702 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
162d2499 703
1f06d367 704 if (STRINGP (item_name))
29250851 705 {
1f06d367
JR
706 if (unicode_append_menu)
707 item_name = ENCODE_UTF_8 (item_name);
708 else if (STRING_MULTIBYTE (item_name))
709 item_name = ENCODE_SYSTEM (item_name);
710
6ed09cf0 711 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
29250851 712 }
1f06d367
JR
713
714 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
29250851
JR
715 {
716 descrip = ENCODE_SYSTEM (descrip);
6ed09cf0 717 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
29250851 718 }
485015ca
GV
719
720 wv = xmalloc_widget_value ();
177c0ea7 721 if (prev_wv)
485015ca 722 prev_wv->next = wv;
177c0ea7 723 else
485015ca 724 save_wv->contents = wv;
51b59d79 725 wv->name = SSDATA (item_name);
485015ca 726 if (!NILP (descrip))
51b59d79 727 wv->key = SSDATA (descrip);
485015ca
GV
728 wv->value = 0;
729 /* Use the contents index as call_data, since we are
2cd23960 730 restricted to 16-bits. */
e59fe83b 731 wv->call_data = !NILP (def) ? (void *) (EMACS_INT) i : 0;
485015ca 732 wv->enabled = !NILP (enable);
162d2499
JR
733
734 if (NILP (type))
735 wv->button_type = BUTTON_TYPE_NONE;
736 else if (EQ (type, QCtoggle))
737 wv->button_type = BUTTON_TYPE_TOGGLE;
738 else if (EQ (type, QCradio))
739 wv->button_type = BUTTON_TYPE_RADIO;
740 else
1088b922 741 emacs_abort ();
162d2499
JR
742
743 wv->selected = !NILP (selected);
df80fd9d 744
d1fffd1d
JR
745 if (!STRINGP (help))
746 help = Qnil;
747
748 wv->help = help;
7d8e57da 749
485015ca
GV
750 prev_wv = wv;
751
752 i += MENU_ITEMS_ITEM_LENGTH;
753 }
754 }
755
756 /* Deal with the title, if it is non-nil. */
757 if (!NILP (title))
758 {
759 widget_value *wv_title = xmalloc_widget_value ();
760 widget_value *wv_sep = xmalloc_widget_value ();
761
762 /* Maybe replace this separator with a bitmap or owner-draw item
763 so that it looks better. Having two separators looks odd. */
764 wv_sep->name = "--";
765 wv_sep->next = first_wv->contents;
2d10309f 766 wv_sep->help = Qnil;
485015ca 767
1f06d367
JR
768 if (unicode_append_menu)
769 title = ENCODE_UTF_8 (title);
770 else if (STRING_MULTIBYTE (title))
6915ded0 771 title = ENCODE_SYSTEM (title);
1f06d367 772
51b59d79 773 wv_title->name = SSDATA (title);
02067692 774 wv_title->enabled = TRUE;
5b20b3cc 775 wv_title->title = TRUE;
162d2499 776 wv_title->button_type = BUTTON_TYPE_NONE;
2d10309f 777 wv_title->help = Qnil;
485015ca
GV
778 wv_title->next = wv_sep;
779 first_wv->contents = wv_title;
780 }
781
df80fd9d
JR
782 /* No selection has been chosen yet. */
783 menu_item_selection = 0;
784
485015ca 785 /* Actually create the menu. */
ace9b298 786 current_popup_menu = menu = CreatePopupMenu ();
485015ca 787 fill_in_menu (menu, first_wv->contents);
162d2499 788
485015ca 789 /* Adjust coordinates to be root-window-relative. */
ee78dc32
GV
790 pos.x = x;
791 pos.y = y;
fbd6baed 792 ClientToScreen (FRAME_W32_WINDOW (f), &pos);
485015ca 793
ee78dc32 794 /* Display the menu. */
177c0ea7 795 menu_item_selection = SendMessage (FRAME_W32_WINDOW (f),
485015ca
GV
796 WM_EMACS_TRACKPOPUPMENU,
797 (WPARAM)menu, (LPARAM)&pos);
014510b0
GV
798
799 /* Clean up extraneous mouse events which might have been generated
800 during the call. */
801 discard_mouse_events ();
aad3612f 802 FRAME_DISPLAY_INFO (f)->grabbed = 0;
014510b0 803
86fb1a79
JR
804 /* Free the widget_value objects we used to specify the contents. */
805 free_menubar_widget_value_tree (first_wv);
806
485015ca
GV
807 DestroyMenu (menu);
808
563cd4c0
JR
809 /* Free the owner-drawn and help-echo menu strings. */
810 w32_free_menu_strings (FRAME_W32_WINDOW (f));
5339f50c 811 f->output_data.w32->menubar_active = 0;
563cd4c0 812
ee78dc32
GV
813 /* Find the selected item, and its pane, to return
814 the proper value. */
485015ca 815 if (menu_item_selection != 0)
ee78dc32 816 {
485015ca
GV
817 Lisp_Object prefix, entry;
818
fe5a0162 819 prefix = entry = Qnil;
485015ca
GV
820 i = 0;
821 while (i < menu_items_used)
822 {
6ed09cf0 823 if (EQ (AREF (menu_items, i), Qnil))
485015ca
GV
824 {
825 subprefix_stack[submenu_depth++] = prefix;
826 prefix = entry;
827 i++;
828 }
6ed09cf0 829 else if (EQ (AREF (menu_items, i), Qlambda))
485015ca
GV
830 {
831 prefix = subprefix_stack[--submenu_depth];
832 i++;
833 }
6ed09cf0 834 else if (EQ (AREF (menu_items, i), Qt))
485015ca 835 {
6ed09cf0 836 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
485015ca
GV
837 i += MENU_ITEMS_PANE_LENGTH;
838 }
839 /* Ignore a nil in the item list.
840 It's meaningful only for dialog boxes. */
6ed09cf0 841 else if (EQ (AREF (menu_items, i), Qquote))
485015ca
GV
842 i += 1;
843 else
844 {
6ed09cf0 845 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
485015ca
GV
846 if (menu_item_selection == i)
847 {
848 if (keymaps != 0)
849 {
850 int j;
851
852 entry = Fcons (entry, Qnil);
853 if (!NILP (prefix))
854 entry = Fcons (prefix, entry);
855 for (j = submenu_depth - 1; j >= 0; j--)
856 if (!NILP (subprefix_stack[j]))
857 entry = Fcons (subprefix_stack[j], entry);
858 }
f0177f86 859 unblock_input ();
485015ca
GV
860 return entry;
861 }
862 i += MENU_ITEMS_ITEM_LENGTH;
863 }
864 }
ee78dc32 865 }
f3e0a6de 866 else if (!for_click)
f0177f86
EZ
867 {
868 unblock_input ();
869 /* Make "Cancel" equivalent to C-g. */
870 Fsignal (Qquit, Qnil);
871 }
014510b0 872
f0177f86 873 unblock_input ();
ee78dc32
GV
874 return Qnil;
875}
485015ca 876\f
ee78dc32 877
ace9b298 878#ifdef HAVE_DIALOGS
31403b24
JR
879/* TODO: On Windows, there are two ways of defining a dialog.
880
881 1. Create a predefined dialog resource and include it in nt/emacs.rc.
882 Using this method, we could then set the titles and make unneeded
883 buttons invisible before displaying the dialog. Everything would
884 be a fixed size though, so there is a risk that text does not
885 fit on a button.
886 2. Create the dialog template in memory on the fly. This allows us
887 to size the dialog and buttons dynamically, probably giving more
888 natural looking results for dialogs with few buttons, and eliminating
889 the problem of text overflowing the buttons. But the API for this is
890 quite complex - structures have to be allocated in particular ways,
891 text content is tacked onto the end of structures in variable length
892 arrays with further structures tacked on after these, there are
893 certain alignment requirements for all this, and we have to
894 measure all the text and convert to "dialog coordinates" to figure
895 out how big to make everything.
896
897 For now, we'll just stick with menus for dialogs that are more
898 complicated than simple yes/no type questions for which we can use
899 the MessageBox function.
900*/
38fecd52 901
485015ca 902static char * button_names [] = {
ee78dc32 903 "button1", "button2", "button3", "button4", "button5",
485015ca 904 "button6", "button7", "button8", "button9", "button10" };
ee78dc32
GV
905
906static Lisp_Object
a10c8269 907w32_dialog_show (struct frame *f, int keymaps,
b56ceb92
JB
908 Lisp_Object title, Lisp_Object header,
909 char **error)
ee78dc32 910{
9d4f32e8 911 int i, nb_buttons = 0;
ee78dc32 912 char dialog_name[6];
485015ca
GV
913 int menu_item_selection;
914
fe5a0162 915 widget_value *wv, *first_wv = 0, *prev_wv = 0;
485015ca 916
ee78dc32
GV
917 /* Number of elements seen so far, before boundary. */
918 int left_count = 0;
919 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
920 int boundary_seen = 0;
485015ca 921
ee78dc32 922 *error = NULL;
485015ca 923
ee78dc32
GV
924 if (menu_items_n_panes > 1)
925 {
926 *error = "Multiple panes in dialog box";
927 return Qnil;
928 }
485015ca 929
ee78dc32
GV
930 /* Create a tree of widget_value objects
931 representing the text label and buttons. */
932 {
933 Lisp_Object pane_name, prefix;
934 char *pane_string;
6ed09cf0
RS
935 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
936 prefix = AREF (menu_items, MENU_ITEMS_PANE_PREFIX);
ee78dc32 937 pane_string = (NILP (pane_name)
51b59d79 938 ? "" : SSDATA (pane_name));
485015ca 939 prev_wv = xmalloc_widget_value ();
ee78dc32
GV
940 prev_wv->value = pane_string;
941 if (keymaps && !NILP (prefix))
942 prev_wv->name++;
943 prev_wv->enabled = 1;
944 prev_wv->name = "message";
2d10309f 945 prev_wv->help = Qnil;
ee78dc32 946 first_wv = prev_wv;
177c0ea7 947
ee78dc32
GV
948 /* Loop over all panes and items, filling in the tree. */
949 i = MENU_ITEMS_PANE_LENGTH;
950 while (i < menu_items_used)
951 {
177c0ea7 952
ee78dc32 953 /* Create a new item within current pane. */
b65a2f83 954 Lisp_Object item_name, enable, descrip, help;
b65a2f83 955
6ed09cf0
RS
956 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
957 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
958 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
959 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
177c0ea7 960
ee78dc32
GV
961 if (NILP (item_name))
962 {
963 free_menubar_widget_value_tree (first_wv);
964 *error = "Submenu in dialog items";
965 return Qnil;
966 }
967 if (EQ (item_name, Qquote))
968 {
969 /* This is the boundary between left-side elts
970 and right-side elts. Stop incrementing right_count. */
971 boundary_seen = 1;
972 i++;
973 continue;
974 }
485015ca 975 if (nb_buttons >= 9)
ee78dc32
GV
976 {
977 free_menubar_widget_value_tree (first_wv);
978 *error = "Too many dialog items";
979 return Qnil;
980 }
485015ca
GV
981
982 wv = xmalloc_widget_value ();
ee78dc32
GV
983 prev_wv->next = wv;
984 wv->name = (char *) button_names[nb_buttons];
985 if (!NILP (descrip))
51b59d79
PE
986 wv->key = SSDATA (descrip);
987 wv->value = SSDATA (item_name);
4939150c 988 wv->call_data = aref_addr (menu_items, i);
ee78dc32 989 wv->enabled = !NILP (enable);
2d10309f 990 wv->help = Qnil;
ee78dc32 991 prev_wv = wv;
485015ca 992
ee78dc32
GV
993 if (! boundary_seen)
994 left_count++;
485015ca 995
ee78dc32
GV
996 nb_buttons++;
997 i += MENU_ITEMS_ITEM_LENGTH;
998 }
485015ca 999
ee78dc32
GV
1000 /* If the boundary was not specified,
1001 by default put half on the left and half on the right. */
1002 if (! boundary_seen)
1003 left_count = nb_buttons - nb_buttons / 2;
485015ca
GV
1004
1005 wv = xmalloc_widget_value ();
ee78dc32 1006 wv->name = dialog_name;
2d10309f 1007 wv->help = Qnil;
485015ca 1008
d23928c7
NR
1009 /* Frame title: 'Q' = Question, 'I' = Information.
1010 Can also have 'E' = Error if, one day, we want
1011 a popup for errors. */
ed3751c8 1012 if (NILP (header))
d23928c7
NR
1013 dialog_name[0] = 'Q';
1014 else
1015 dialog_name[0] = 'I';
1016
ee78dc32
GV
1017 /* Dialog boxes use a really stupid name encoding
1018 which specifies how many buttons to use
d23928c7 1019 and how many buttons are on the right. */
ee78dc32
GV
1020 dialog_name[1] = '0' + nb_buttons;
1021 dialog_name[2] = 'B';
1022 dialog_name[3] = 'R';
1023 /* Number of buttons to put on the right. */
1024 dialog_name[4] = '0' + nb_buttons - left_count;
1025 dialog_name[5] = 0;
1026 wv->contents = first_wv;
1027 first_wv = wv;
1028 }
485015ca 1029
ee78dc32 1030 /* Actually create the dialog. */
485015ca 1031 dialog_id = widget_id_tick++;
ee78dc32 1032 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
fbd6baed 1033 f->output_data.w32->widget, 1, 0,
ee78dc32 1034 dialog_selection_callback, 0);
02067692 1035 lw_modify_all_widgets (dialog_id, first_wv->contents, TRUE);
485015ca 1036
ee78dc32
GV
1037 /* Free the widget_value objects we used to specify the contents. */
1038 free_menubar_widget_value_tree (first_wv);
485015ca 1039
ee78dc32
GV
1040 /* No selection has been chosen yet. */
1041 menu_item_selection = 0;
485015ca 1042
ee78dc32
GV
1043 /* Display the menu. */
1044 lw_pop_up_all_widgets (dialog_id);
485015ca 1045
ee78dc32 1046 /* Process events that apply to the menu. */
aad3612f 1047 popup_get_selection ((XEvent *) 0, FRAME_DISPLAY_INFO (f), dialog_id);
ee78dc32 1048
177c0ea7 1049 lw_destroy_all_widgets (dialog_id);
ee78dc32 1050
ee78dc32
GV
1051 /* Find the selected item, and its pane, to return
1052 the proper value. */
1053 if (menu_item_selection != 0)
1054 {
1055 Lisp_Object prefix;
485015ca 1056
ee78dc32
GV
1057 prefix = Qnil;
1058 i = 0;
1059 while (i < menu_items_used)
1060 {
1061 Lisp_Object entry;
1062
6ed09cf0 1063 if (EQ (AREF (menu_items, i), Qt))
ee78dc32 1064 {
6ed09cf0 1065 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
ee78dc32
GV
1066 i += MENU_ITEMS_PANE_LENGTH;
1067 }
1068 else
1069 {
6ed09cf0 1070 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
485015ca 1071 if (menu_item_selection == i)
ee78dc32
GV
1072 {
1073 if (keymaps != 0)
1074 {
1075 entry = Fcons (entry, Qnil);
1076 if (!NILP (prefix))
1077 entry = Fcons (prefix, entry);
1078 }
1079 return entry;
1080 }
1081 i += MENU_ITEMS_ITEM_LENGTH;
1082 }
1083 }
1084 }
f3e0a6de
JR
1085 else
1086 /* Make "Cancel" equivalent to C-g. */
1087 Fsignal (Qquit, Qnil);
485015ca 1088
ee78dc32
GV
1089 return Qnil;
1090}
31403b24
JR
1091#else /* !HAVE_DIALOGS */
1092
1093/* Currently we only handle Yes No dialogs (y-or-n-p and yes-or-no-p) as
1094 simple dialogs. We could handle a few more, but I'm not aware of
1095 anywhere in Emacs that uses the other specific dialog choices that
1096 MessageBox provides. */
1097
b56ceb92
JB
1098static int
1099is_simple_dialog (Lisp_Object contents)
31403b24 1100{
3b0512a3 1101 Lisp_Object options;
31403b24
JR
1102 Lisp_Object name, yes, no, other;
1103
3b0512a3
AS
1104 if (!CONSP (contents))
1105 return 0;
1106 options = XCDR (contents);
1107
31403b24
JR
1108 yes = build_string ("Yes");
1109 no = build_string ("No");
1110
1111 if (!CONSP (options))
1112 return 0;
1113
3b0512a3
AS
1114 name = XCAR (options);
1115 if (!CONSP (name))
31403b24 1116 return 0;
3b0512a3 1117 name = XCAR (name);
31403b24
JR
1118
1119 if (!NILP (Fstring_equal (name, yes)))
1120 other = no;
1121 else if (!NILP (Fstring_equal (name, no)))
1122 other = yes;
1123 else
1124 return 0;
1125
1126 options = XCDR (options);
1127 if (!CONSP (options))
1128 return 0;
1129
3b0512a3
AS
1130 name = XCAR (options);
1131 if (!CONSP (name))
1132 return 0;
1133 name = XCAR (name);
31403b24
JR
1134 if (NILP (Fstring_equal (name, other)))
1135 return 0;
1136
1137 /* Check there are no more options. */
1138 options = XCDR (options);
1139 return !(CONSP (options));
1140}
1141
b56ceb92 1142static Lisp_Object
a10c8269 1143simple_dialog_show (struct frame *f, Lisp_Object contents, Lisp_Object header)
31403b24
JR
1144{
1145 int answer;
1146 UINT type;
31403b24
JR
1147 Lisp_Object lispy_answer = Qnil, temp = XCAR (contents);
1148
1c9b4129
JR
1149 type = MB_YESNO;
1150
1151 /* Since we only handle Yes/No dialogs, and we already checked
1152 is_simple_dialog, we don't need to worry about checking contents
1153 to see what type of dialog to use. */
31403b24 1154
fe7a3057 1155 /* Use Unicode if possible, so any language can be displayed. */
1c9b4129 1156 if (unicode_message_box)
31403b24 1157 {
af025ae8
EZ
1158 WCHAR *text;
1159 const WCHAR *title;
8bc53d00 1160 USE_SAFE_ALLOCA;
1c9b4129
JR
1161
1162 if (STRINGP (temp))
1163 {
1164 char *utf8_text = SDATA (ENCODE_UTF_8 (temp));
1165 /* Be pessimistic about the number of characters needed.
1166 Remember characters outside the BMP will take more than
1167 one utf16 word, so we cannot simply use the character
1168 length of temp. */
1169 int utf8_len = strlen (utf8_text);
98c6f1e3 1170 text = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR));
1c9b4129
JR
1171 utf8to16 (utf8_text, utf8_len, text);
1172 }
1173 else
1174 {
1175 text = L"";
1176 }
1177
1178 if (NILP (header))
1179 {
1180 title = L"Question";
1181 type |= MB_ICONQUESTION;
1182 }
1183 else
1184 {
1185 title = L"Information";
1186 type |= MB_ICONINFORMATION;
1187 }
1188
1189 answer = unicode_message_box (FRAME_W32_WINDOW (f), text, title, type);
8bc53d00 1190 SAFE_FREE ();
31403b24
JR
1191 }
1192 else
1193 {
af025ae8 1194 const char *text, *title;
31403b24 1195
1c9b4129
JR
1196 /* Fall back on ANSI message box, but at least use system
1197 encoding so questions representable by the system codepage
1198 are encoded properly. */
1199 if (STRINGP (temp))
1200 text = SDATA (ENCODE_SYSTEM (temp));
1201 else
1202 text = "";
1203
1204 if (NILP (header))
1205 {
1206 title = "Question";
1207 type |= MB_ICONQUESTION;
1208 }
1209 else
1210 {
1211 title = "Information";
1212 type |= MB_ICONINFORMATION;
1213 }
1214
1215 answer = MessageBox (FRAME_W32_WINDOW (f), text, title, type);
1216 }
31403b24
JR
1217
1218 if (answer == IDYES)
1219 lispy_answer = build_string ("Yes");
1220 else if (answer == IDNO)
1221 lispy_answer = build_string ("No");
1222 else
1223 Fsignal (Qquit, Qnil);
1224
1225 for (temp = XCDR (contents); CONSP (temp); temp = XCDR (temp))
1226 {
1227 Lisp_Object item, name, value;
1228 item = XCAR (temp);
1229 if (CONSP (item))
1230 {
1231 name = XCAR (item);
1232 value = XCDR (item);
1233 }
1234 else
1235 {
1236 name = item;
1237 value = Qnil;
1238 }
1239
1240 if (!NILP (Fstring_equal (name, lispy_answer)))
1241 {
1242 return value;
1243 }
1244 }
1245 Fsignal (Qquit, Qnil);
1246 return Qnil;
1247}
1248#endif /* !HAVE_DIALOGS */
485015ca
GV
1249\f
1250
1f06d367
JR
1251/* UTF8: 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx, 10xxxxxx, 10xxxxxx */
1252static void
1253utf8to16 (unsigned char * src, int len, WCHAR * dest)
1254{
1255 while (len > 0)
1256 {
1f06d367
JR
1257 if (*src < 0x80)
1258 {
1259 *dest = (WCHAR) *src;
1260 dest++; src++; len--;
1261 }
1262 /* Since we might get >3 byte sequences which we don't handle, ignore the extra parts. */
1263 else if (*src < 0xC0)
1264 {
1265 src++; len--;
1266 }
1267 /* 2 char UTF-8 sequence. */
1268 else if (*src < 0xE0)
1269 {
1270 *dest = (WCHAR) (((*src & 0x1f) << 6)
1271 | (*(src + 1) & 0x3f));
1272 src += 2; len -= 2; dest++;
1273 }
1274 else if (*src < 0xF0)
1275 {
1276 *dest = (WCHAR) (((*src & 0x0f) << 12)
1277 | ((*(src + 1) & 0x3f) << 6)
1278 | (*(src + 2) & 0x3f));
1279 src += 3; len -= 3; dest++;
1280 }
1281 else /* Not encodable. Insert Unicode Substitution char. */
1282 {
1283 *dest = (WCHAR) 0xfffd;
1284 src++; len--; dest++;
1285 }
1286 }
1287 *dest = 0;
1288}
1289
485015ca
GV
1290static int
1291add_menu_item (HMENU menu, widget_value *wv, HMENU item)
1292{
1293 UINT fuFlags;
af41f8a8 1294 char *out_string, *p, *q;
37ad8b63 1295 int return_value;
af41f8a8 1296 size_t nlen, orig_len;
8bc53d00 1297 USE_SAFE_ALLOCA;
485015ca 1298
4039c786 1299 if (menu_separator_name_p (wv->name))
222456a1
JR
1300 {
1301 fuFlags = MF_SEPARATOR;
1302 out_string = NULL;
1303 }
177c0ea7 1304 else
485015ca 1305 {
070d1949 1306 if (wv->enabled)
485015ca
GV
1307 fuFlags = MF_STRING;
1308 else
1309 fuFlags = MF_STRING | MF_GRAYED;
1310
1311 if (wv->key != NULL)
1312 {
98c6f1e3 1313 out_string = SAFE_ALLOCA (strlen (wv->name) + strlen (wv->key) + 2);
485015ca
GV
1314 strcpy (out_string, wv->name);
1315 strcat (out_string, "\t");
1316 strcat (out_string, wv->key);
1317 }
1318 else
94c97d85 1319 out_string = (char *)wv->name;
485015ca 1320
af41f8a8
EZ
1321 /* Quote any special characters within the menu item's text and
1322 key binding. */
1323 nlen = orig_len = strlen (out_string);
ff25d115
JR
1324 if (unicode_append_menu)
1325 {
1326 /* With UTF-8, & cannot be part of a multibyte character. */
1327 for (p = out_string; *p; p++)
1328 {
1329 if (*p == '&')
1330 nlen++;
1331 }
1332 }
0fda9b75 1333#ifndef NTGUI_UNICODE
ff25d115
JR
1334 else
1335 {
1336 /* If encoded with the system codepage, use multibyte string
1337 functions in case of multibyte characters that contain '&'. */
1338 for (p = out_string; *p; p = _mbsinc (p))
1339 {
1340 if (_mbsnextc (p) == '&')
1341 nlen++;
1342 }
1343 }
0fda9b75 1344#endif /* !NTGUI_UNICODE */
ff25d115 1345
af41f8a8 1346 if (nlen > orig_len)
ff25d115
JR
1347 {
1348 p = out_string;
98c6f1e3 1349 out_string = SAFE_ALLOCA (nlen + 1);
ff25d115
JR
1350 q = out_string;
1351 while (*p)
1352 {
1353 if (unicode_append_menu)
1354 {
1355 if (*p == '&')
1356 *q++ = *p;
1357 *q++ = *p++;
1358 }
0fda9b75 1359#ifndef NTGUI_UNICODE
ff25d115
JR
1360 else
1361 {
1362 if (_mbsnextc (p) == '&')
1363 {
1364 _mbsncpy (q, p, 1);
1365 q = _mbsinc (q);
1366 }
1367 _mbsncpy (q, p, 1);
1368 p = _mbsinc (p);
1369 q = _mbsinc (q);
1370 }
0fda9b75 1371#endif /* !NTGUI_UNICODE */
ff25d115
JR
1372 }
1373 *q = '\0';
1374 }
af41f8a8 1375
d1fffd1d
JR
1376 if (item != NULL)
1377 fuFlags = MF_POPUP;
1378 else if (wv->title || wv->call_data == 0)
485015ca 1379 {
ace9b298
JR
1380 /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since
1381 we can't deallocate the memory otherwise. */
1382 if (get_menu_item_info)
1383 {
6ed09cf0
RS
1384 out_string = (char *) local_alloc (strlen (wv->name) + 1);
1385 strcpy (out_string, wv->name);
d1fffd1d 1386#ifdef MENU_DEBUG
91af3942 1387 DebPrint ("Menu: allocating %ld for owner-draw", out_string);
d1fffd1d 1388#endif
ace9b298
JR
1389 fuFlags = MF_OWNERDRAW | MF_DISABLED;
1390 }
1391 else
1392 fuFlags = MF_DISABLED;
485015ca 1393 }
1c5acb8c 1394
162d2499 1395 /* Draw radio buttons and tickboxes. */
4cdf6a6c 1396 else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
6f665da9 1397 wv->button_type == BUTTON_TYPE_RADIO))
4cdf6a6c 1398 fuFlags |= MF_CHECKED;
37ad8b63 1399 else
4cdf6a6c 1400 fuFlags |= MF_UNCHECKED;
485015ca 1401 }
4cdf6a6c 1402
1f06d367
JR
1403 if (unicode_append_menu && out_string)
1404 {
1405 /* Convert out_string from UTF-8 to UTF-16-LE. */
1406 int utf8_len = strlen (out_string);
1407 WCHAR * utf16_string;
1408 if (fuFlags & MF_OWNERDRAW)
1409 utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR));
1410 else
98c6f1e3 1411 utf16_string = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR));
1f06d367
JR
1412
1413 utf8to16 (out_string, utf8_len, utf16_string);
1414 return_value = unicode_append_menu (menu, fuFlags,
62aba0d4
FP
1415 item != NULL ? (UINT_PTR) item
1416 : (UINT_PTR) wv->call_data,
1f06d367 1417 utf16_string);
0fda9b75
DC
1418
1419#ifndef NTGUI_UNICODE /* Fallback does not apply when always UNICODE */
d749b6fa
JR
1420 if (!return_value)
1421 {
fe7a3057 1422 /* On W9x/ME, Unicode menus are not supported, though AppendMenuW
d749b6fa
JR
1423 apparently does exist at least in some cases and appears to be
1424 stubbed out to do nothing. out_string is UTF-8, but since
1425 our standard menus are in English and this is only going to
1426 happen the first time a menu is used, the encoding is
1427 of minor importance compared with menus not working at all. */
1428 return_value =
1429 AppendMenu (menu, fuFlags,
62aba0d4 1430 item != NULL ? (UINT_PTR) item: (UINT_PTR) wv->call_data,
d749b6fa 1431 out_string);
1c6900d9
EZ
1432 /* Don't use Unicode menus in future, unless this is Windows
1433 NT or later, where a failure of AppendMenuW does NOT mean
1434 Unicode menus are unsupported. */
1435 if (osinfo_cache.dwPlatformId != VER_PLATFORM_WIN32_NT)
1436 unicode_append_menu = NULL;
d749b6fa 1437 }
0fda9b75 1438#endif /* NTGUI_UNICODE */
d749b6fa
JR
1439
1440 if (unicode_append_menu && (fuFlags & MF_OWNERDRAW))
1f06d367
JR
1441 local_free (out_string);
1442 }
1443 else
1444 {
1445 return_value =
1446 AppendMenu (menu,
1447 fuFlags,
62aba0d4 1448 item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data,
1f06d367
JR
1449 out_string );
1450 }
37ad8b63
JR
1451
1452 /* This must be done after the menu item is created. */
6f665da9 1453 if (!wv->title && wv->call_data != 0)
4cdf6a6c 1454 {
4cdf6a6c
AI
1455 if (set_menu_item_info)
1456 {
1457 MENUITEMINFO info;
72af86bd 1458 memset (&info, 0, sizeof (info));
4cdf6a6c
AI
1459 info.cbSize = sizeof (info);
1460 info.fMask = MIIM_DATA;
1461
d1fffd1d
JR
1462 /* Set help string for menu item. Leave it as a Lisp_Object
1463 until it is ready to be displayed, since GC can happen while
1464 menus are active. */
24ee9763 1465 if (!NILP (wv->help))
2d5c5f7d
EZ
1466 {
1467 /* As of Jul-2012, w32api headers say that dwItemData
1468 has DWORD type, but that's a bug: it should actually
37a9eac8 1469 be ULONG_PTR, which is correct for 32-bit and 64-bit
2d5c5f7d
EZ
1470 Windows alike. MSVC headers get it right; hopefully,
1471 MinGW headers will, too. */
37a9eac8 1472 info.dwItemData = (ULONG_PTR) XLI (wv->help);
2d5c5f7d 1473 }
4cdf6a6c
AI
1474 if (wv->button_type == BUTTON_TYPE_RADIO)
1475 {
1476 /* CheckMenuRadioItem allows us to differentiate TOGGLE and
1477 RADIO items, but is not available on NT 3.51 and earlier. */
1478 info.fMask |= MIIM_TYPE | MIIM_STATE;
1479 info.fType = MFT_RADIOCHECK | MFT_STRING;
1480 info.dwTypeData = out_string;
1481 info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED;
1482 }
1483
1484 set_menu_item_info (menu,
62aba0d4 1485 item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data,
4cdf6a6c
AI
1486 FALSE, &info);
1487 }
1488 }
8bc53d00 1489 SAFE_FREE ();
37ad8b63 1490 return return_value;
485015ca
GV
1491}
1492
1493/* Construct native Windows menu(bar) based on widget_value tree. */
24f981c9 1494static int
485015ca
GV
1495fill_in_menu (HMENU menu, widget_value *wv)
1496{
485015ca
GV
1497 for ( ; wv != NULL; wv = wv->next)
1498 {
1499 if (wv->contents)
1500 {
1501 HMENU sub_menu = CreatePopupMenu ();
1502
1503 if (sub_menu == NULL)
1504 return 0;
1505
1506 if (!fill_in_menu (sub_menu, wv->contents) ||
1507 !add_menu_item (menu, wv, sub_menu))
1508 {
1509 DestroyMenu (sub_menu);
1510 return 0;
1511 }
1512 }
1513 else
1514 {
1515 if (!add_menu_item (menu, wv, NULL))
1516 return 0;
1517 }
1518 }
1519 return 1;
1520}
1521
7d8e57da
JR
1522/* Display help string for currently pointed to menu item. Not
1523 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
1524 available. */
1525void
59a86c99 1526w32_menu_display_help (HWND owner, HMENU menu, UINT item, UINT flags)
7d8e57da 1527{
7d8e57da
JR
1528 if (get_menu_item_info)
1529 {
1c5acb8c
JR
1530 struct frame *f = x_window_to_frame (&one_w32_display_info, owner);
1531 Lisp_Object frame, help;
7d8e57da 1532
94c7f257
JR
1533 /* No help echo on owner-draw menu items, or when the keyboard is used
1534 to navigate the menus, since tooltips are distracting if they pop
1535 up elsewhere. */
1536 if (flags & MF_OWNERDRAW || flags & MF_POPUP
1537 || !(flags & MF_MOUSESELECT))
ace9b298
JR
1538 help = Qnil;
1539 else
1540 {
1541 MENUITEMINFO info;
7d8e57da 1542
72af86bd 1543 memset (&info, 0, sizeof (info));
ace9b298
JR
1544 info.cbSize = sizeof (info);
1545 info.fMask = MIIM_DATA;
1546 get_menu_item_info (menu, item, FALSE, &info);
1547
646b5f55 1548 help = info.dwItemData ? XIL (info.dwItemData) : Qnil;
ace9b298 1549 }
e5cd3d11 1550
1c5acb8c
JR
1551 /* Store the help echo in the keyboard buffer as the X toolkit
1552 version does, rather than directly showing it. This seems to
1553 solve the GC problems that were present when we based the
1554 Windows code on the non-toolkit version. */
1555 if (f)
1556 {
1557 XSETFRAME (frame, f);
1558 kbd_buffer_store_help_event (frame, help);
1559 }
1560 else
1561 /* X version has a loop through frames here, which doesn't
1562 appear to do anything, unless it has some side effect. */
f868cd8a 1563 show_help_echo (help, Qnil, Qnil, Qnil);
7d8e57da
JR
1564 }
1565}
1566
d1fffd1d 1567/* Free memory used by owner-drawn strings. */
ace9b298 1568static void
b56ceb92 1569w32_free_submenu_strings (HMENU menu)
ace9b298
JR
1570{
1571 int i, num = GetMenuItemCount (menu);
1572 for (i = 0; i < num; i++)
1573 {
1574 MENUITEMINFO info;
72af86bd 1575 memset (&info, 0, sizeof (info));
ace9b298 1576 info.cbSize = sizeof (info);
d1fffd1d 1577 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_SUBMENU;
7d8e57da 1578
ace9b298
JR
1579 get_menu_item_info (menu, i, TRUE, &info);
1580
d1fffd1d
JR
1581 /* Owner-drawn names are held in dwItemData. */
1582 if ((info.fType & MF_OWNERDRAW) && info.dwItemData)
1583 {
1584#ifdef MENU_DEBUG
1585 DebPrint ("Menu: freeing %ld for owner-draw", info.dwItemData);
1586#endif
6ed09cf0 1587 local_free (info.dwItemData);
d1fffd1d 1588 }
ace9b298
JR
1589
1590 /* Recurse down submenus. */
1591 if (info.hSubMenu)
1592 w32_free_submenu_strings (info.hSubMenu);
1593 }
1594}
1595
1596void
b56ceb92 1597w32_free_menu_strings (HWND hwnd)
ace9b298
JR
1598{
1599 HMENU menu = current_popup_menu;
1600
1601 if (get_menu_item_info)
1602 {
1603 /* If there is no popup menu active, free the strings from the frame's
1604 menubar. */
1605 if (!menu)
1606 menu = GetMenu (hwnd);
1607
1608 if (menu)
1609 w32_free_submenu_strings (menu);
1610 }
1611
1612 current_popup_menu = NULL;
1613}
7d8e57da 1614
e3135734
CY
1615/* The following is used by delayed window autoselection. */
1616
1617DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
1618 doc: /* Return t if a menu or popup dialog is active on selected frame. */)
5842a27b 1619 (void)
e3135734 1620{
a10c8269 1621 struct frame *f;
e3135734
CY
1622 f = SELECTED_FRAME ();
1623 return (f->output_data.w32->menubar_active > 0) ? Qt : Qnil;
e3135734
CY
1624}
1625
b56ceb92
JB
1626void
1627syms_of_w32menu (void)
ee78dc32 1628{
019e3c1a 1629 globals_of_w32menu ();
485015ca 1630
ace9b298
JR
1631 current_popup_menu = NULL;
1632
019e3c1a 1633 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
e139a33c 1634 DEFSYM (Qunsupported__w32_dialog, "unsupported--w32-dialog");
fdc12c4d 1635
e5d3d18d 1636 defsubr (&Smenu_or_popup_active_p);
ee78dc32 1637}
9785d95b
BK
1638
1639/*
1640 globals_of_w32menu is used to initialize those global variables that
1641 must always be initialized on startup even when the global variable
1642 initialized is non zero (see the function main in emacs.c).
1643 globals_of_w32menu is called from syms_of_w32menu when the global
1644 variable initialized is 0 and directly from main when initialized
1645 is non zero.
1646 */
b56ceb92
JB
1647void
1648globals_of_w32menu (void)
9785d95b 1649{
0fda9b75 1650#ifndef NTGUI_UNICODE
9d4f32e8 1651 /* See if Get/SetMenuItemInfo functions are available. */
9785d95b
BK
1652 HMODULE user32 = GetModuleHandle ("user32.dll");
1653 get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA");
1654 set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA");
1f06d367 1655 unicode_append_menu = (AppendMenuW_Proc) GetProcAddress (user32, "AppendMenuW");
1c9b4129 1656 unicode_message_box = (MessageBoxW_Proc) GetProcAddress (user32, "MessageBoxW");
0fda9b75 1657#endif /* !NTGUI_UNICODE */
9785d95b 1658}