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