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