]> code.delx.au - gnu-emacs/blob - src/w32menu.c
Fix infinite loop in menu input due to block_input.
[gnu-emacs] / src / w32menu.c
1 /* Menu support for GNU Emacs on the Microsoft Windows API.
2 Copyright (C) 1986, 1988, 1993-1994, 1996, 1998-1999, 2001-2013 Free
3 Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21
22 #include <signal.h>
23 #include <stdio.h>
24 #include <setjmp.h>
25
26 #include "lisp.h"
27 #include "keyboard.h"
28 #include "keymap.h"
29 #include "frame.h"
30 #include "termhooks.h"
31 #include "window.h"
32 #include "blockinput.h"
33 #include "character.h"
34 #include "buffer.h"
35 #include "charset.h"
36 #include "coding.h"
37 #include "menu.h"
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 /* 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
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
59 #include "w32common.h" /* for osinfo_cache */
60
61 #undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
62
63 #ifndef TRUE
64 #define TRUE 1
65 #define FALSE 0
66 #endif /* no TRUE */
67
68 HMENU current_popup_menu;
69
70 void syms_of_w32menu (void);
71 void globals_of_w32menu (void);
72
73 typedef BOOL (WINAPI * GetMenuItemInfoA_Proc) (
74 IN HMENU,
75 IN UINT,
76 IN BOOL,
77 IN OUT LPMENUITEMINFOA);
78 typedef BOOL (WINAPI * SetMenuItemInfoA_Proc) (
79 IN HMENU,
80 IN UINT,
81 IN BOOL,
82 IN LPCMENUITEMINFOA);
83 typedef int (WINAPI * MessageBoxW_Proc) (
84 IN HWND window,
85 IN WCHAR *text,
86 IN WCHAR *caption,
87 IN UINT type);
88
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 */
95 GetMenuItemInfoA_Proc get_menu_item_info = NULL;
96 SetMenuItemInfoA_Proc set_menu_item_info = NULL;
97 AppendMenuW_Proc unicode_append_menu = NULL;
98 MessageBoxW_Proc unicode_message_box = NULL;
99 #endif /* NTGUI_UNICODE */
100
101 Lisp_Object Qdebug_on_next_call;
102
103 void set_frame_menubar (struct frame *, bool, bool);
104
105 #ifdef HAVE_DIALOGS
106 static Lisp_Object w32_dialog_show (struct frame *, int, Lisp_Object, char**);
107 #else
108 static int is_simple_dialog (Lisp_Object);
109 static Lisp_Object simple_dialog_show (struct frame *, Lisp_Object, Lisp_Object);
110 #endif
111
112 static void utf8to16 (unsigned char *, int, WCHAR *);
113 static int fill_in_menu (HMENU, widget_value *);
114
115 void w32_free_menu_strings (HWND);
116
117 #ifdef HAVE_MENUS
118
119 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
120 doc: /* Pop up a dialog box and return user's selection.
121 POSITION specifies which frame to use.
122 This is normally a mouse button event or a window or frame.
123 If POSITION is t, it means to use the frame the mouse is on.
124 The dialog box appears in the middle of the specified frame.
125
126 CONTENTS specifies the alternatives to display in the dialog box.
127 It is a list of the form (TITLE ITEM1 ITEM2...).
128 Each ITEM is a cons cell (STRING . VALUE).
129 The return value is VALUE from the chosen item.
130
131 An ITEM may also be just a string--that makes a nonselectable item.
132 An ITEM may also be nil--that means to put all preceding items
133 on the left of the dialog box and all following items on the right.
134 \(By default, approximately half appear on each side.)
135
136 If HEADER is non-nil, the frame title for the box is "Information",
137 otherwise it is "Question". */)
138 (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
139 {
140 struct frame *f = NULL;
141 Lisp_Object window;
142
143 /* Decode the first argument: find the window or frame to use. */
144 if (EQ (position, Qt)
145 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
146 || EQ (XCAR (position), Qtool_bar))))
147 {
148 #if 0 /* Using the frame the mouse is on may not be right. */
149 /* Use the mouse's current position. */
150 struct frame *new_f = SELECTED_FRAME ();
151 Lisp_Object bar_window;
152 enum scroll_bar_part part;
153 Time time;
154 Lisp_Object x, y;
155
156 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
157
158 if (new_f != 0)
159 XSETFRAME (window, new_f);
160 else
161 window = selected_window;
162 #endif
163 window = selected_window;
164 }
165 else if (CONSP (position))
166 {
167 Lisp_Object tem = XCAR (position);
168 if (CONSP (tem))
169 window = Fcar (XCDR (position));
170 else
171 {
172 tem = Fcar (XCDR (position)); /* EVENT_START (position) */
173 window = Fcar (tem); /* POSN_WINDOW (tem) */
174 }
175 }
176 else if (WINDOWP (position) || FRAMEP (position))
177 window = position;
178 else
179 window = Qnil;
180
181 /* Decode where to put the menu. */
182
183 if (FRAMEP (window))
184 f = XFRAME (window);
185 else if (WINDOWP (window))
186 {
187 CHECK_LIVE_WINDOW (window);
188 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
189 }
190 else
191 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
192 but I don't want to make one now. */
193 CHECK_WINDOW (window);
194
195 check_window_system (f);
196
197 #ifndef HAVE_DIALOGS
198
199 {
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, FRAME_PIXEL_WIDTH (f) / 2);
210 XSETINT (y, FRAME_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 }
215 }
216 #else /* HAVE_DIALOGS */
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);
224 CHECK_STRING (title);
225
226 list_of_panes (Fcons (contents, Qnil));
227
228 /* Display them in a dialog box. */
229 block_input ();
230 selection = w32_dialog_show (f, 0, title, header, &error_name);
231 unblock_input ();
232
233 discard_menu_items ();
234 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
235
236 if (error_name) error (error_name);
237 return selection;
238 }
239 #endif /* HAVE_DIALOGS */
240 }
241
242 /* Activate the menu bar of frame F.
243 This is called from keyboard.c when it gets the
244 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
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. */
253
254 void
255 x_activate_menubar (struct frame *f)
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
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
271 void
272 menubar_selection_callback (struct frame *f, void * client_data)
273 {
274 Lisp_Object prefix, entry;
275 Lisp_Object vector;
276 Lisp_Object *subprefix_stack;
277 int submenu_depth = 0;
278 int i;
279
280 if (!f)
281 return;
282 entry = Qnil;
283 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * word_size);
284 vector = f->menu_bar_vector;
285 prefix = Qnil;
286 i = 0;
287 while (i < f->menu_bar_items_used)
288 {
289 if (EQ (AREF (vector, i), Qnil))
290 {
291 subprefix_stack[submenu_depth++] = prefix;
292 prefix = entry;
293 i++;
294 }
295 else if (EQ (AREF (vector, i), Qlambda))
296 {
297 prefix = subprefix_stack[--submenu_depth];
298 i++;
299 }
300 else if (EQ (AREF (vector, i), Qt))
301 {
302 prefix = AREF (vector, i + MENU_ITEMS_PANE_PREFIX);
303 i += MENU_ITEMS_PANE_LENGTH;
304 }
305 else
306 {
307 entry = AREF (vector, i + MENU_ITEMS_ITEM_VALUE);
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;
315 EVENT_INIT (buf);
316
317 XSETFRAME (frame, f);
318 buf.kind = MENU_BAR_EVENT;
319 buf.frame_or_window = frame;
320 buf.arg = frame;
321 kbd_buffer_store_event (&buf);
322
323 for (j = 0; j < submenu_depth; j++)
324 if (!NILP (subprefix_stack[j]))
325 {
326 buf.kind = MENU_BAR_EVENT;
327 buf.frame_or_window = frame;
328 buf.arg = subprefix_stack[j];
329 kbd_buffer_store_event (&buf);
330 }
331
332 if (!NILP (prefix))
333 {
334 buf.kind = MENU_BAR_EVENT;
335 buf.frame_or_window = frame;
336 buf.arg = prefix;
337 kbd_buffer_store_event (&buf);
338 }
339
340 buf.kind = MENU_BAR_EVENT;
341 buf.frame_or_window = frame;
342 buf.arg = entry;
343 /* Free memory used by owner-drawn and help-echo strings. */
344 w32_free_menu_strings (FRAME_W32_WINDOW (f));
345 kbd_buffer_store_event (&buf);
346
347 f->output_data.w32->menubar_active = 0;
348 return;
349 }
350 i += MENU_ITEMS_ITEM_LENGTH;
351 }
352 }
353 /* Free memory used by owner-drawn and help-echo strings. */
354 w32_free_menu_strings (FRAME_W32_WINDOW (f));
355 f->output_data.w32->menubar_active = 0;
356 }
357
358 \f
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
363 void
364 set_frame_menubar (struct frame *f, bool first_time, bool deep_p)
365 {
366 HMENU menubar_widget = f->output_data.w32->menubar_widget;
367 Lisp_Object items;
368 widget_value *wv, *first_wv, *prev_wv = 0;
369 int i, last_i;
370 int *submenu_start, *submenu_end;
371 int *submenu_top_level_items, *submenu_n_panes;
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;
381
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;
388 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
389 int previous_menu_items_used = f->menu_bar_items_used;
390 Lisp_Object *previous_items
391 = (Lisp_Object *) alloca (previous_menu_items_used
392 * word_size);
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
399 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->contents;
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
405 record_unwind_save_match_data ();
406
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
415 /* Run the hooks. */
416 safe_run_hooks (Qactivate_menubar_hook);
417 safe_run_hooks (Qmenu_bar_update_hook);
418 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
419
420 items = FRAME_MENU_BAR_ITEMS (f);
421
422 /* Save the frame's previous menu bar contents data. */
423 if (previous_menu_items_used)
424 memcpy (previous_items, XVECTOR (f->menu_bar_vector)->contents,
425 previous_menu_items_used * word_size);
426
427 /* Fill in menu_items with the current menu bar contents.
428 This can evaluate Lisp code. */
429 save_menu_items ();
430
431 menu_items = f->menu_bar_vector;
432 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
433 submenu_start = (int *) alloca (ASIZE (items) * sizeof (int));
434 submenu_end = (int *) alloca (ASIZE (items) * sizeof (int));
435 submenu_n_panes = (int *) alloca (ASIZE (items) * sizeof (int));
436 submenu_top_level_items = (int *) alloca (ASIZE (items) * sizeof (int));
437 init_menu_items ();
438 for (i = 0; i < ASIZE (items); i += 4)
439 {
440 Lisp_Object key, string, maps;
441
442 last_i = i;
443
444 key = AREF (items, i);
445 string = AREF (items, i + 1);
446 maps = AREF (items, i + 2);
447 if (NILP (string))
448 break;
449
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);
455 submenu_n_panes[i] = menu_items_n_panes;
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 {
475 menu_items_n_panes = submenu_n_panes[i];
476 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
477 submenu_top_level_items[i]);
478 if (prev_wv)
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;
484 wv->button_type = BUTTON_TYPE_NONE;
485 prev_wv = wv;
486 }
487
488 set_buffer_internal_1 (prev);
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
495 || (!EQ (previous_items[i], AREF (menu_items, i))))
496 break;
497 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
498 {
499 free_menubar_widget_value_tree (first_wv);
500 discard_menu_items ();
501 unbind_to (specpdl_count, Qnil);
502 return;
503 }
504
505 fset_menu_bar_vector (f, menu_items);
506 f->menu_bar_items_used = menu_items_used;
507
508 /* This undoes save_menu_items. */
509 unbind_to (specpdl_count, Qnil);
510
511 /* Now GC cannot happen during the lifetime of the widget_value,
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. */
516 wv = first_wv->contents;
517 for (i = 0; i < ASIZE (items); i += 4)
518 {
519 Lisp_Object string;
520 string = AREF (items, i + 1);
521 if (NILP (string))
522 break;
523 wv->name = SSDATA (string);
524 update_submenu_strings (wv->contents);
525 wv = wv->next;
526 }
527 }
528 else
529 {
530 /* Make a widget-value tree containing
531 just the top level menu bar strings. */
532
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
541 items = FRAME_MENU_BAR_ITEMS (f);
542 for (i = 0; i < ASIZE (items); i += 4)
543 {
544 Lisp_Object string;
545
546 string = AREF (items, i + 1);
547 if (NILP (string))
548 break;
549
550 wv = xmalloc_widget_value ();
551 wv->name = SSDATA (string);
552 wv->value = 0;
553 wv->enabled = 1;
554 wv->button_type = BUTTON_TYPE_NONE;
555 wv->help = Qnil;
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
562 if (prev_wv)
563 prev_wv->next = wv;
564 else
565 first_wv->contents = wv;
566 prev_wv = wv;
567 }
568
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;
573 }
574
575 /* Create or update the menu bar widget. */
576
577 block_input ();
578
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);
590
591 free_menubar_widget_value_tree (first_wv);
592
593 {
594 HMENU old_widget = f->output_data.w32->menubar_widget;
595
596 f->output_data.w32->menubar_widget = menubar_widget;
597 SetMenu (FRAME_W32_WINDOW (f), f->output_data.w32->menubar_widget);
598 /* Causes flicker when menu bar is updated
599 DrawMenuBar (FRAME_W32_WINDOW (f)); */
600
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)
604 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
605 }
606
607 unblock_input ();
608 }
609
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
615 void
616 initialize_frame_menubar (struct frame *f)
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. */
620 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
621 set_frame_menubar (f, 1, 1);
622 }
623
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
627 void
628 free_frame_menubar (struct frame *f)
629 {
630 block_input ();
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 }
638
639 unblock_input ();
640 }
641
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. */
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.
650 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
651 KEYMAPS is 1 if this menu was specified with keymaps;
652 in that case, we return a list containing the chosen item's value
653 and perhaps also the pane's prefix.
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
658 Lisp_Object
659 w32_menu_show (struct frame *f, int x, int y, int for_click, int keymaps,
660 Lisp_Object title, const char **error)
661 {
662 int i;
663 int menu_item_selection;
664 HMENU menu;
665 POINT pos;
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
670 = (Lisp_Object *) alloca (menu_items_used * word_size);
671 int submenu_depth = 0;
672 int first_pane;
673
674 *error = NULL;
675
676 if (menu_items_n_panes == 0)
677 return Qnil;
678
679 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
680 {
681 *error = "Empty menu";
682 return Qnil;
683 }
684
685 block_input ();
686
687 /* Create a tree of widget_value objects
688 representing the panes and their items. */
689 wv = xmalloc_widget_value ();
690 wv->name = "menu";
691 wv->value = 0;
692 wv->enabled = 1;
693 wv->button_type = BUTTON_TYPE_NONE;
694 wv->help = Qnil;
695 first_wv = wv;
696 first_pane = 1;
697
698 /* Loop over all panes and items, filling in the tree. */
699 i = 0;
700 while (i < menu_items_used)
701 {
702 if (EQ (AREF (menu_items, i), Qnil))
703 {
704 submenu_stack[submenu_depth++] = save_wv;
705 save_wv = prev_wv;
706 prev_wv = 0;
707 first_pane = 1;
708 i++;
709 }
710 else if (EQ (AREF (menu_items, i), Qlambda))
711 {
712 prev_wv = save_wv;
713 save_wv = submenu_stack[--submenu_depth];
714 first_pane = 0;
715 i++;
716 }
717 else if (EQ (AREF (menu_items, i), Qt)
718 && submenu_depth != 0)
719 i += MENU_ITEMS_PANE_LENGTH;
720 /* Ignore a nil in the item list.
721 It's meaningful only for dialog boxes. */
722 else if (EQ (AREF (menu_items, i), Qquote))
723 i += 1;
724 else if (EQ (AREF (menu_items, i), Qt))
725 {
726 /* Create a new pane. */
727 Lisp_Object pane_name, prefix;
728 char *pane_string;
729 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
730 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
731
732 if (STRINGP (pane_name))
733 {
734 if (unicode_append_menu)
735 pane_name = ENCODE_UTF_8 (pane_name);
736 else if (STRING_MULTIBYTE (pane_name))
737 pane_name = ENCODE_SYSTEM (pane_name);
738
739 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
740 }
741
742 pane_string = (NILP (pane_name)
743 ? "" : SSDATA (pane_name));
744 /* If there is just one top-level pane, put all its items directly
745 under the top-level menu. */
746 if (menu_items_n_panes == 1)
747 pane_string = "";
748
749 /* If the pane has a meaningful name,
750 make the pane a top-level menu item
751 with its items as a submenu beneath it. */
752 if (!keymaps && strcmp (pane_string, ""))
753 {
754 wv = xmalloc_widget_value ();
755 if (save_wv)
756 save_wv->next = wv;
757 else
758 first_wv->contents = wv;
759 wv->name = pane_string;
760 if (keymaps && !NILP (prefix))
761 wv->name++;
762 wv->value = 0;
763 wv->enabled = 1;
764 wv->button_type = BUTTON_TYPE_NONE;
765 wv->help = Qnil;
766 save_wv = wv;
767 prev_wv = 0;
768 }
769 else if (first_pane)
770 {
771 save_wv = wv;
772 prev_wv = 0;
773 }
774 first_pane = 0;
775 i += MENU_ITEMS_PANE_LENGTH;
776 }
777 else
778 {
779 /* Create a new item within current pane. */
780 Lisp_Object item_name, enable, descrip, def, type, selected, help;
781
782 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
783 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
784 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
785 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
786 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
787 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
788 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
789
790 if (STRINGP (item_name))
791 {
792 if (unicode_append_menu)
793 item_name = ENCODE_UTF_8 (item_name);
794 else if (STRING_MULTIBYTE (item_name))
795 item_name = ENCODE_SYSTEM (item_name);
796
797 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
798 }
799
800 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
801 {
802 descrip = ENCODE_SYSTEM (descrip);
803 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
804 }
805
806 wv = xmalloc_widget_value ();
807 if (prev_wv)
808 prev_wv->next = wv;
809 else
810 save_wv->contents = wv;
811 wv->name = SSDATA (item_name);
812 if (!NILP (descrip))
813 wv->key = SSDATA (descrip);
814 wv->value = 0;
815 /* Use the contents index as call_data, since we are
816 restricted to 16-bits. */
817 wv->call_data = !NILP (def) ? (void *) (EMACS_INT) i : 0;
818 wv->enabled = !NILP (enable);
819
820 if (NILP (type))
821 wv->button_type = BUTTON_TYPE_NONE;
822 else if (EQ (type, QCtoggle))
823 wv->button_type = BUTTON_TYPE_TOGGLE;
824 else if (EQ (type, QCradio))
825 wv->button_type = BUTTON_TYPE_RADIO;
826 else
827 emacs_abort ();
828
829 wv->selected = !NILP (selected);
830
831 if (!STRINGP (help))
832 help = Qnil;
833
834 wv->help = help;
835
836 prev_wv = wv;
837
838 i += MENU_ITEMS_ITEM_LENGTH;
839 }
840 }
841
842 /* Deal with the title, if it is non-nil. */
843 if (!NILP (title))
844 {
845 widget_value *wv_title = xmalloc_widget_value ();
846 widget_value *wv_sep = xmalloc_widget_value ();
847
848 /* Maybe replace this separator with a bitmap or owner-draw item
849 so that it looks better. Having two separators looks odd. */
850 wv_sep->name = "--";
851 wv_sep->next = first_wv->contents;
852 wv_sep->help = Qnil;
853
854 if (unicode_append_menu)
855 title = ENCODE_UTF_8 (title);
856 else if (STRING_MULTIBYTE (title))
857 title = ENCODE_SYSTEM (title);
858
859 wv_title->name = SSDATA (title);
860 wv_title->enabled = TRUE;
861 wv_title->title = TRUE;
862 wv_title->button_type = BUTTON_TYPE_NONE;
863 wv_title->help = Qnil;
864 wv_title->next = wv_sep;
865 first_wv->contents = wv_title;
866 }
867
868 /* No selection has been chosen yet. */
869 menu_item_selection = 0;
870
871 /* Actually create the menu. */
872 current_popup_menu = menu = CreatePopupMenu ();
873 fill_in_menu (menu, first_wv->contents);
874
875 /* Adjust coordinates to be root-window-relative. */
876 pos.x = x;
877 pos.y = y;
878 ClientToScreen (FRAME_W32_WINDOW (f), &pos);
879
880 /* Display the menu. */
881 menu_item_selection = SendMessage (FRAME_W32_WINDOW (f),
882 WM_EMACS_TRACKPOPUPMENU,
883 (WPARAM)menu, (LPARAM)&pos);
884
885 /* Clean up extraneous mouse events which might have been generated
886 during the call. */
887 discard_mouse_events ();
888 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
889
890 /* Free the widget_value objects we used to specify the contents. */
891 free_menubar_widget_value_tree (first_wv);
892
893 DestroyMenu (menu);
894
895 /* Free the owner-drawn and help-echo menu strings. */
896 w32_free_menu_strings (FRAME_W32_WINDOW (f));
897 f->output_data.w32->menubar_active = 0;
898
899 /* Find the selected item, and its pane, to return
900 the proper value. */
901 if (menu_item_selection != 0)
902 {
903 Lisp_Object prefix, entry;
904
905 prefix = entry = Qnil;
906 i = 0;
907 while (i < menu_items_used)
908 {
909 if (EQ (AREF (menu_items, i), Qnil))
910 {
911 subprefix_stack[submenu_depth++] = prefix;
912 prefix = entry;
913 i++;
914 }
915 else if (EQ (AREF (menu_items, i), Qlambda))
916 {
917 prefix = subprefix_stack[--submenu_depth];
918 i++;
919 }
920 else if (EQ (AREF (menu_items, i), Qt))
921 {
922 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
923 i += MENU_ITEMS_PANE_LENGTH;
924 }
925 /* Ignore a nil in the item list.
926 It's meaningful only for dialog boxes. */
927 else if (EQ (AREF (menu_items, i), Qquote))
928 i += 1;
929 else
930 {
931 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
932 if (menu_item_selection == i)
933 {
934 if (keymaps != 0)
935 {
936 int j;
937
938 entry = Fcons (entry, Qnil);
939 if (!NILP (prefix))
940 entry = Fcons (prefix, entry);
941 for (j = submenu_depth - 1; j >= 0; j--)
942 if (!NILP (subprefix_stack[j]))
943 entry = Fcons (subprefix_stack[j], entry);
944 }
945 unblock_input ();
946 return entry;
947 }
948 i += MENU_ITEMS_ITEM_LENGTH;
949 }
950 }
951 }
952 else if (!for_click)
953 {
954 unblock_input ();
955 /* Make "Cancel" equivalent to C-g. */
956 Fsignal (Qquit, Qnil);
957 }
958
959 unblock_input ();
960 return Qnil;
961 }
962 \f
963
964 #ifdef HAVE_DIALOGS
965 /* TODO: On Windows, there are two ways of defining a dialog.
966
967 1. Create a predefined dialog resource and include it in nt/emacs.rc.
968 Using this method, we could then set the titles and make unneeded
969 buttons invisible before displaying the dialog. Everything would
970 be a fixed size though, so there is a risk that text does not
971 fit on a button.
972 2. Create the dialog template in memory on the fly. This allows us
973 to size the dialog and buttons dynamically, probably giving more
974 natural looking results for dialogs with few buttons, and eliminating
975 the problem of text overflowing the buttons. But the API for this is
976 quite complex - structures have to be allocated in particular ways,
977 text content is tacked onto the end of structures in variable length
978 arrays with further structures tacked on after these, there are
979 certain alignment requirements for all this, and we have to
980 measure all the text and convert to "dialog coordinates" to figure
981 out how big to make everything.
982
983 For now, we'll just stick with menus for dialogs that are more
984 complicated than simple yes/no type questions for which we can use
985 the MessageBox function.
986 */
987
988 static char * button_names [] = {
989 "button1", "button2", "button3", "button4", "button5",
990 "button6", "button7", "button8", "button9", "button10" };
991
992 static Lisp_Object
993 w32_dialog_show (struct frame *f, int keymaps,
994 Lisp_Object title, Lisp_Object header,
995 char **error)
996 {
997 int i, nb_buttons = 0;
998 char dialog_name[6];
999 int menu_item_selection;
1000
1001 widget_value *wv, *first_wv = 0, *prev_wv = 0;
1002
1003 /* Number of elements seen so far, before boundary. */
1004 int left_count = 0;
1005 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1006 int boundary_seen = 0;
1007
1008 *error = NULL;
1009
1010 if (menu_items_n_panes > 1)
1011 {
1012 *error = "Multiple panes in dialog box";
1013 return Qnil;
1014 }
1015
1016 /* Create a tree of widget_value objects
1017 representing the text label and buttons. */
1018 {
1019 Lisp_Object pane_name, prefix;
1020 char *pane_string;
1021 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
1022 prefix = AREF (menu_items, MENU_ITEMS_PANE_PREFIX);
1023 pane_string = (NILP (pane_name)
1024 ? "" : SSDATA (pane_name));
1025 prev_wv = xmalloc_widget_value ();
1026 prev_wv->value = pane_string;
1027 if (keymaps && !NILP (prefix))
1028 prev_wv->name++;
1029 prev_wv->enabled = 1;
1030 prev_wv->name = "message";
1031 prev_wv->help = Qnil;
1032 first_wv = prev_wv;
1033
1034 /* Loop over all panes and items, filling in the tree. */
1035 i = MENU_ITEMS_PANE_LENGTH;
1036 while (i < menu_items_used)
1037 {
1038
1039 /* Create a new item within current pane. */
1040 Lisp_Object item_name, enable, descrip, help;
1041
1042 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1043 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1044 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1045 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1046
1047 if (NILP (item_name))
1048 {
1049 free_menubar_widget_value_tree (first_wv);
1050 *error = "Submenu in dialog items";
1051 return Qnil;
1052 }
1053 if (EQ (item_name, Qquote))
1054 {
1055 /* This is the boundary between left-side elts
1056 and right-side elts. Stop incrementing right_count. */
1057 boundary_seen = 1;
1058 i++;
1059 continue;
1060 }
1061 if (nb_buttons >= 9)
1062 {
1063 free_menubar_widget_value_tree (first_wv);
1064 *error = "Too many dialog items";
1065 return Qnil;
1066 }
1067
1068 wv = xmalloc_widget_value ();
1069 prev_wv->next = wv;
1070 wv->name = (char *) button_names[nb_buttons];
1071 if (!NILP (descrip))
1072 wv->key = SSDATA (descrip);
1073 wv->value = SSDATA (item_name);
1074 wv->call_data = aref_addr (menu_items, i);
1075 wv->enabled = !NILP (enable);
1076 wv->help = Qnil;
1077 prev_wv = wv;
1078
1079 if (! boundary_seen)
1080 left_count++;
1081
1082 nb_buttons++;
1083 i += MENU_ITEMS_ITEM_LENGTH;
1084 }
1085
1086 /* If the boundary was not specified,
1087 by default put half on the left and half on the right. */
1088 if (! boundary_seen)
1089 left_count = nb_buttons - nb_buttons / 2;
1090
1091 wv = xmalloc_widget_value ();
1092 wv->name = dialog_name;
1093 wv->help = Qnil;
1094
1095 /* Frame title: 'Q' = Question, 'I' = Information.
1096 Can also have 'E' = Error if, one day, we want
1097 a popup for errors. */
1098 if (NILP (header))
1099 dialog_name[0] = 'Q';
1100 else
1101 dialog_name[0] = 'I';
1102
1103 /* Dialog boxes use a really stupid name encoding
1104 which specifies how many buttons to use
1105 and how many buttons are on the right. */
1106 dialog_name[1] = '0' + nb_buttons;
1107 dialog_name[2] = 'B';
1108 dialog_name[3] = 'R';
1109 /* Number of buttons to put on the right. */
1110 dialog_name[4] = '0' + nb_buttons - left_count;
1111 dialog_name[5] = 0;
1112 wv->contents = first_wv;
1113 first_wv = wv;
1114 }
1115
1116 /* Actually create the dialog. */
1117 dialog_id = widget_id_tick++;
1118 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1119 f->output_data.w32->widget, 1, 0,
1120 dialog_selection_callback, 0);
1121 lw_modify_all_widgets (dialog_id, first_wv->contents, TRUE);
1122
1123 /* Free the widget_value objects we used to specify the contents. */
1124 free_menubar_widget_value_tree (first_wv);
1125
1126 /* No selection has been chosen yet. */
1127 menu_item_selection = 0;
1128
1129 /* Display the menu. */
1130 lw_pop_up_all_widgets (dialog_id);
1131
1132 /* Process events that apply to the menu. */
1133 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
1134
1135 lw_destroy_all_widgets (dialog_id);
1136
1137 /* Find the selected item, and its pane, to return
1138 the proper value. */
1139 if (menu_item_selection != 0)
1140 {
1141 Lisp_Object prefix;
1142
1143 prefix = Qnil;
1144 i = 0;
1145 while (i < menu_items_used)
1146 {
1147 Lisp_Object entry;
1148
1149 if (EQ (AREF (menu_items, i), Qt))
1150 {
1151 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1152 i += MENU_ITEMS_PANE_LENGTH;
1153 }
1154 else
1155 {
1156 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
1157 if (menu_item_selection == i)
1158 {
1159 if (keymaps != 0)
1160 {
1161 entry = Fcons (entry, Qnil);
1162 if (!NILP (prefix))
1163 entry = Fcons (prefix, entry);
1164 }
1165 return entry;
1166 }
1167 i += MENU_ITEMS_ITEM_LENGTH;
1168 }
1169 }
1170 }
1171 else
1172 /* Make "Cancel" equivalent to C-g. */
1173 Fsignal (Qquit, Qnil);
1174
1175 return Qnil;
1176 }
1177 #else /* !HAVE_DIALOGS */
1178
1179 /* Currently we only handle Yes No dialogs (y-or-n-p and yes-or-no-p) as
1180 simple dialogs. We could handle a few more, but I'm not aware of
1181 anywhere in Emacs that uses the other specific dialog choices that
1182 MessageBox provides. */
1183
1184 static int
1185 is_simple_dialog (Lisp_Object contents)
1186 {
1187 Lisp_Object options;
1188 Lisp_Object name, yes, no, other;
1189
1190 if (!CONSP (contents))
1191 return 0;
1192 options = XCDR (contents);
1193
1194 yes = build_string ("Yes");
1195 no = build_string ("No");
1196
1197 if (!CONSP (options))
1198 return 0;
1199
1200 name = XCAR (options);
1201 if (!CONSP (name))
1202 return 0;
1203 name = XCAR (name);
1204
1205 if (!NILP (Fstring_equal (name, yes)))
1206 other = no;
1207 else if (!NILP (Fstring_equal (name, no)))
1208 other = yes;
1209 else
1210 return 0;
1211
1212 options = XCDR (options);
1213 if (!CONSP (options))
1214 return 0;
1215
1216 name = XCAR (options);
1217 if (!CONSP (name))
1218 return 0;
1219 name = XCAR (name);
1220 if (NILP (Fstring_equal (name, other)))
1221 return 0;
1222
1223 /* Check there are no more options. */
1224 options = XCDR (options);
1225 return !(CONSP (options));
1226 }
1227
1228 static Lisp_Object
1229 simple_dialog_show (struct frame *f, Lisp_Object contents, Lisp_Object header)
1230 {
1231 int answer;
1232 UINT type;
1233 Lisp_Object lispy_answer = Qnil, temp = XCAR (contents);
1234
1235 type = MB_YESNO;
1236
1237 /* Since we only handle Yes/No dialogs, and we already checked
1238 is_simple_dialog, we don't need to worry about checking contents
1239 to see what type of dialog to use. */
1240
1241 /* Use Unicode if possible, so any language can be displayed. */
1242 if (unicode_message_box)
1243 {
1244 WCHAR *text, *title;
1245 USE_SAFE_ALLOCA;
1246
1247 if (STRINGP (temp))
1248 {
1249 char *utf8_text = SDATA (ENCODE_UTF_8 (temp));
1250 /* Be pessimistic about the number of characters needed.
1251 Remember characters outside the BMP will take more than
1252 one utf16 word, so we cannot simply use the character
1253 length of temp. */
1254 int utf8_len = strlen (utf8_text);
1255 text = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR));
1256 utf8to16 (utf8_text, utf8_len, text);
1257 }
1258 else
1259 {
1260 text = L"";
1261 }
1262
1263 if (NILP (header))
1264 {
1265 title = L"Question";
1266 type |= MB_ICONQUESTION;
1267 }
1268 else
1269 {
1270 title = L"Information";
1271 type |= MB_ICONINFORMATION;
1272 }
1273
1274 answer = unicode_message_box (FRAME_W32_WINDOW (f), text, title, type);
1275 SAFE_FREE ();
1276 }
1277 else
1278 {
1279 char *text, *title;
1280
1281 /* Fall back on ANSI message box, but at least use system
1282 encoding so questions representable by the system codepage
1283 are encoded properly. */
1284 if (STRINGP (temp))
1285 text = SDATA (ENCODE_SYSTEM (temp));
1286 else
1287 text = "";
1288
1289 if (NILP (header))
1290 {
1291 title = "Question";
1292 type |= MB_ICONQUESTION;
1293 }
1294 else
1295 {
1296 title = "Information";
1297 type |= MB_ICONINFORMATION;
1298 }
1299
1300 answer = MessageBox (FRAME_W32_WINDOW (f), text, title, type);
1301 }
1302
1303 if (answer == IDYES)
1304 lispy_answer = build_string ("Yes");
1305 else if (answer == IDNO)
1306 lispy_answer = build_string ("No");
1307 else
1308 Fsignal (Qquit, Qnil);
1309
1310 for (temp = XCDR (contents); CONSP (temp); temp = XCDR (temp))
1311 {
1312 Lisp_Object item, name, value;
1313 item = XCAR (temp);
1314 if (CONSP (item))
1315 {
1316 name = XCAR (item);
1317 value = XCDR (item);
1318 }
1319 else
1320 {
1321 name = item;
1322 value = Qnil;
1323 }
1324
1325 if (!NILP (Fstring_equal (name, lispy_answer)))
1326 {
1327 return value;
1328 }
1329 }
1330 Fsignal (Qquit, Qnil);
1331 return Qnil;
1332 }
1333 #endif /* !HAVE_DIALOGS */
1334 \f
1335
1336 /* UTF8: 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx, 10xxxxxx, 10xxxxxx */
1337 static void
1338 utf8to16 (unsigned char * src, int len, WCHAR * dest)
1339 {
1340 while (len > 0)
1341 {
1342 if (*src < 0x80)
1343 {
1344 *dest = (WCHAR) *src;
1345 dest++; src++; len--;
1346 }
1347 /* Since we might get >3 byte sequences which we don't handle, ignore the extra parts. */
1348 else if (*src < 0xC0)
1349 {
1350 src++; len--;
1351 }
1352 /* 2 char UTF-8 sequence. */
1353 else if (*src < 0xE0)
1354 {
1355 *dest = (WCHAR) (((*src & 0x1f) << 6)
1356 | (*(src + 1) & 0x3f));
1357 src += 2; len -= 2; dest++;
1358 }
1359 else if (*src < 0xF0)
1360 {
1361 *dest = (WCHAR) (((*src & 0x0f) << 12)
1362 | ((*(src + 1) & 0x3f) << 6)
1363 | (*(src + 2) & 0x3f));
1364 src += 3; len -= 3; dest++;
1365 }
1366 else /* Not encodable. Insert Unicode Substitution char. */
1367 {
1368 *dest = (WCHAR) 0xfffd;
1369 src++; len--; dest++;
1370 }
1371 }
1372 *dest = 0;
1373 }
1374
1375 static int
1376 add_menu_item (HMENU menu, widget_value *wv, HMENU item)
1377 {
1378 UINT fuFlags;
1379 char *out_string, *p, *q;
1380 int return_value;
1381 size_t nlen, orig_len;
1382 USE_SAFE_ALLOCA;
1383
1384 if (menu_separator_name_p (wv->name))
1385 {
1386 fuFlags = MF_SEPARATOR;
1387 out_string = NULL;
1388 }
1389 else
1390 {
1391 if (wv->enabled)
1392 fuFlags = MF_STRING;
1393 else
1394 fuFlags = MF_STRING | MF_GRAYED;
1395
1396 if (wv->key != NULL)
1397 {
1398 out_string = SAFE_ALLOCA (strlen (wv->name) + strlen (wv->key) + 2);
1399 strcpy (out_string, wv->name);
1400 strcat (out_string, "\t");
1401 strcat (out_string, wv->key);
1402 }
1403 else
1404 out_string = (char *)wv->name;
1405
1406 /* Quote any special characters within the menu item's text and
1407 key binding. */
1408 nlen = orig_len = strlen (out_string);
1409 if (unicode_append_menu)
1410 {
1411 /* With UTF-8, & cannot be part of a multibyte character. */
1412 for (p = out_string; *p; p++)
1413 {
1414 if (*p == '&')
1415 nlen++;
1416 }
1417 }
1418 #ifndef NTGUI_UNICODE
1419 else
1420 {
1421 /* If encoded with the system codepage, use multibyte string
1422 functions in case of multibyte characters that contain '&'. */
1423 for (p = out_string; *p; p = _mbsinc (p))
1424 {
1425 if (_mbsnextc (p) == '&')
1426 nlen++;
1427 }
1428 }
1429 #endif /* !NTGUI_UNICODE */
1430
1431 if (nlen > orig_len)
1432 {
1433 p = out_string;
1434 out_string = SAFE_ALLOCA (nlen + 1);
1435 q = out_string;
1436 while (*p)
1437 {
1438 if (unicode_append_menu)
1439 {
1440 if (*p == '&')
1441 *q++ = *p;
1442 *q++ = *p++;
1443 }
1444 #ifndef NTGUI_UNICODE
1445 else
1446 {
1447 if (_mbsnextc (p) == '&')
1448 {
1449 _mbsncpy (q, p, 1);
1450 q = _mbsinc (q);
1451 }
1452 _mbsncpy (q, p, 1);
1453 p = _mbsinc (p);
1454 q = _mbsinc (q);
1455 }
1456 #endif /* !NTGUI_UNICODE */
1457 }
1458 *q = '\0';
1459 }
1460
1461 if (item != NULL)
1462 fuFlags = MF_POPUP;
1463 else if (wv->title || wv->call_data == 0)
1464 {
1465 /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since
1466 we can't deallocate the memory otherwise. */
1467 if (get_menu_item_info)
1468 {
1469 out_string = (char *) local_alloc (strlen (wv->name) + 1);
1470 strcpy (out_string, wv->name);
1471 #ifdef MENU_DEBUG
1472 DebPrint ("Menu: allocating %ld for owner-draw", out_string);
1473 #endif
1474 fuFlags = MF_OWNERDRAW | MF_DISABLED;
1475 }
1476 else
1477 fuFlags = MF_DISABLED;
1478 }
1479
1480 /* Draw radio buttons and tickboxes. */
1481 else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
1482 wv->button_type == BUTTON_TYPE_RADIO))
1483 fuFlags |= MF_CHECKED;
1484 else
1485 fuFlags |= MF_UNCHECKED;
1486 }
1487
1488 if (unicode_append_menu && out_string)
1489 {
1490 /* Convert out_string from UTF-8 to UTF-16-LE. */
1491 int utf8_len = strlen (out_string);
1492 WCHAR * utf16_string;
1493 if (fuFlags & MF_OWNERDRAW)
1494 utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR));
1495 else
1496 utf16_string = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR));
1497
1498 utf8to16 (out_string, utf8_len, utf16_string);
1499 return_value = unicode_append_menu (menu, fuFlags,
1500 item != NULL ? (UINT_PTR) item
1501 : (UINT_PTR) wv->call_data,
1502 utf16_string);
1503
1504 #ifndef NTGUI_UNICODE /* Fallback does not apply when always UNICODE */
1505 if (!return_value)
1506 {
1507 /* On W9x/ME, Unicode menus are not supported, though AppendMenuW
1508 apparently does exist at least in some cases and appears to be
1509 stubbed out to do nothing. out_string is UTF-8, but since
1510 our standard menus are in English and this is only going to
1511 happen the first time a menu is used, the encoding is
1512 of minor importance compared with menus not working at all. */
1513 return_value =
1514 AppendMenu (menu, fuFlags,
1515 item != NULL ? (UINT_PTR) item: (UINT_PTR) wv->call_data,
1516 out_string);
1517 /* Don't use Unicode menus in future, unless this is Windows
1518 NT or later, where a failure of AppendMenuW does NOT mean
1519 Unicode menus are unsupported. */
1520 if (osinfo_cache.dwPlatformId != VER_PLATFORM_WIN32_NT)
1521 unicode_append_menu = NULL;
1522 }
1523 #endif /* NTGUI_UNICODE */
1524
1525 if (unicode_append_menu && (fuFlags & MF_OWNERDRAW))
1526 local_free (out_string);
1527 }
1528 else
1529 {
1530 return_value =
1531 AppendMenu (menu,
1532 fuFlags,
1533 item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data,
1534 out_string );
1535 }
1536
1537 /* This must be done after the menu item is created. */
1538 if (!wv->title && wv->call_data != 0)
1539 {
1540 if (set_menu_item_info)
1541 {
1542 MENUITEMINFO info;
1543 memset (&info, 0, sizeof (info));
1544 info.cbSize = sizeof (info);
1545 info.fMask = MIIM_DATA;
1546
1547 /* Set help string for menu item. Leave it as a Lisp_Object
1548 until it is ready to be displayed, since GC can happen while
1549 menus are active. */
1550 if (!NILP (wv->help))
1551 {
1552 /* As of Jul-2012, w32api headers say that dwItemData
1553 has DWORD type, but that's a bug: it should actually
1554 be ULONG_PTR, which is correct for 32-bit and 64-bit
1555 Windows alike. MSVC headers get it right; hopefully,
1556 MinGW headers will, too. */
1557 info.dwItemData = (ULONG_PTR) XLI (wv->help);
1558 }
1559 if (wv->button_type == BUTTON_TYPE_RADIO)
1560 {
1561 /* CheckMenuRadioItem allows us to differentiate TOGGLE and
1562 RADIO items, but is not available on NT 3.51 and earlier. */
1563 info.fMask |= MIIM_TYPE | MIIM_STATE;
1564 info.fType = MFT_RADIOCHECK | MFT_STRING;
1565 info.dwTypeData = out_string;
1566 info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED;
1567 }
1568
1569 set_menu_item_info (menu,
1570 item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data,
1571 FALSE, &info);
1572 }
1573 }
1574 SAFE_FREE ();
1575 return return_value;
1576 }
1577
1578 /* Construct native Windows menu(bar) based on widget_value tree. */
1579 static int
1580 fill_in_menu (HMENU menu, widget_value *wv)
1581 {
1582 for ( ; wv != NULL; wv = wv->next)
1583 {
1584 if (wv->contents)
1585 {
1586 HMENU sub_menu = CreatePopupMenu ();
1587
1588 if (sub_menu == NULL)
1589 return 0;
1590
1591 if (!fill_in_menu (sub_menu, wv->contents) ||
1592 !add_menu_item (menu, wv, sub_menu))
1593 {
1594 DestroyMenu (sub_menu);
1595 return 0;
1596 }
1597 }
1598 else
1599 {
1600 if (!add_menu_item (menu, wv, NULL))
1601 return 0;
1602 }
1603 }
1604 return 1;
1605 }
1606
1607 /* Display help string for currently pointed to menu item. Not
1608 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
1609 available. */
1610 void
1611 w32_menu_display_help (HWND owner, HMENU menu, UINT item, UINT flags)
1612 {
1613 if (get_menu_item_info)
1614 {
1615 struct frame *f = x_window_to_frame (&one_w32_display_info, owner);
1616 Lisp_Object frame, help;
1617
1618 /* No help echo on owner-draw menu items, or when the keyboard is used
1619 to navigate the menus, since tooltips are distracting if they pop
1620 up elsewhere. */
1621 if (flags & MF_OWNERDRAW || flags & MF_POPUP
1622 || !(flags & MF_MOUSESELECT))
1623 help = Qnil;
1624 else
1625 {
1626 MENUITEMINFO info;
1627
1628 memset (&info, 0, sizeof (info));
1629 info.cbSize = sizeof (info);
1630 info.fMask = MIIM_DATA;
1631 get_menu_item_info (menu, item, FALSE, &info);
1632
1633 help = info.dwItemData ? XIL (info.dwItemData) : Qnil;
1634 }
1635
1636 /* Store the help echo in the keyboard buffer as the X toolkit
1637 version does, rather than directly showing it. This seems to
1638 solve the GC problems that were present when we based the
1639 Windows code on the non-toolkit version. */
1640 if (f)
1641 {
1642 XSETFRAME (frame, f);
1643 kbd_buffer_store_help_event (frame, help);
1644 }
1645 else
1646 /* X version has a loop through frames here, which doesn't
1647 appear to do anything, unless it has some side effect. */
1648 show_help_echo (help, Qnil, Qnil, Qnil);
1649 }
1650 }
1651
1652 /* Free memory used by owner-drawn strings. */
1653 static void
1654 w32_free_submenu_strings (HMENU menu)
1655 {
1656 int i, num = GetMenuItemCount (menu);
1657 for (i = 0; i < num; i++)
1658 {
1659 MENUITEMINFO info;
1660 memset (&info, 0, sizeof (info));
1661 info.cbSize = sizeof (info);
1662 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_SUBMENU;
1663
1664 get_menu_item_info (menu, i, TRUE, &info);
1665
1666 /* Owner-drawn names are held in dwItemData. */
1667 if ((info.fType & MF_OWNERDRAW) && info.dwItemData)
1668 {
1669 #ifdef MENU_DEBUG
1670 DebPrint ("Menu: freeing %ld for owner-draw", info.dwItemData);
1671 #endif
1672 local_free (info.dwItemData);
1673 }
1674
1675 /* Recurse down submenus. */
1676 if (info.hSubMenu)
1677 w32_free_submenu_strings (info.hSubMenu);
1678 }
1679 }
1680
1681 void
1682 w32_free_menu_strings (HWND hwnd)
1683 {
1684 HMENU menu = current_popup_menu;
1685
1686 if (get_menu_item_info)
1687 {
1688 /* If there is no popup menu active, free the strings from the frame's
1689 menubar. */
1690 if (!menu)
1691 menu = GetMenu (hwnd);
1692
1693 if (menu)
1694 w32_free_submenu_strings (menu);
1695 }
1696
1697 current_popup_menu = NULL;
1698 }
1699
1700 #endif /* HAVE_MENUS */
1701
1702 /* The following is used by delayed window autoselection. */
1703
1704 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
1705 doc: /* Return t if a menu or popup dialog is active on selected frame. */)
1706 (void)
1707 {
1708 #ifdef HAVE_MENUS
1709 struct frame *f;
1710 f = SELECTED_FRAME ();
1711 return (f->output_data.w32->menubar_active > 0) ? Qt : Qnil;
1712 #else
1713 return Qnil;
1714 #endif /* HAVE_MENUS */
1715 }
1716
1717 void
1718 syms_of_w32menu (void)
1719 {
1720 globals_of_w32menu ();
1721
1722 current_popup_menu = NULL;
1723
1724 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
1725
1726 defsubr (&Smenu_or_popup_active_p);
1727 #ifdef HAVE_MENUS
1728 defsubr (&Sx_popup_dialog);
1729 #endif
1730 }
1731
1732 /*
1733 globals_of_w32menu is used to initialize those global variables that
1734 must always be initialized on startup even when the global variable
1735 initialized is non zero (see the function main in emacs.c).
1736 globals_of_w32menu is called from syms_of_w32menu when the global
1737 variable initialized is 0 and directly from main when initialized
1738 is non zero.
1739 */
1740 void
1741 globals_of_w32menu (void)
1742 {
1743 #ifndef NTGUI_UNICODE
1744 /* See if Get/SetMenuItemInfo functions are available. */
1745 HMODULE user32 = GetModuleHandle ("user32.dll");
1746 get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA");
1747 set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA");
1748 unicode_append_menu = (AppendMenuW_Proc) GetProcAddress (user32, "AppendMenuW");
1749 unicode_message_box = (MessageBoxW_Proc) GetProcAddress (user32, "MessageBoxW");
1750 #endif /* !NTGUI_UNICODE */
1751 }