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