* net/tramp-sh.el (tramp-sh-handle-insert-directory): Check,
[emacs.git] / src / w32menu.c
blob03904cf20b86236204a1a287b828998864ee155b
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.
5 This file is part of GNU Emacs.
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.
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.
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/>. */
20 #include <config.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <setjmp.h>
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"
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"
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. */
47 #ifndef NTGUI_UNICODE
48 #include <mbstring.h>
49 #endif /* !NTGUI_UNICODE */
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
57 #include "dispextern.h"
59 #include "w32common.h" /* for osinfo_cache */
61 #undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
63 #ifndef TRUE
64 #define TRUE 1
65 #define FALSE 0
66 #endif /* no TRUE */
68 HMENU current_popup_menu;
70 void syms_of_w32menu (void);
71 void globals_of_w32menu (void);
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);
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 */
101 Lisp_Object Qdebug_on_next_call;
103 void set_frame_menubar (FRAME_PTR, bool, bool);
105 #ifdef HAVE_DIALOGS
106 static Lisp_Object w32_dialog_show (FRAME_PTR, int, Lisp_Object, char**);
107 #else
108 static int is_simple_dialog (Lisp_Object);
109 static Lisp_Object simple_dialog_show (FRAME_PTR, Lisp_Object, Lisp_Object);
110 #endif
112 static void utf8to16 (unsigned char *, int, WCHAR *);
113 static int fill_in_menu (HMENU, widget_value *);
115 void w32_free_menu_strings (HWND);
117 #ifdef HAVE_MENUS
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.
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.
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.)
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)
140 FRAME_PTR f = NULL;
141 Lisp_Object window;
143 check_w32 ();
145 /* Decode the first argument: find the window or frame to use. */
146 if (EQ (position, Qt)
147 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
148 || EQ (XCAR (position), Qtool_bar))))
150 #if 0 /* Using the frame the mouse is on may not be right. */
151 /* Use the mouse's current position. */
152 FRAME_PTR new_f = SELECTED_FRAME ();
153 Lisp_Object bar_window;
154 enum scroll_bar_part part;
155 Time time;
156 Lisp_Object x, y;
158 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
160 if (new_f != 0)
161 XSETFRAME (window, new_f);
162 else
163 window = selected_window;
164 #endif
165 window = selected_window;
167 else if (CONSP (position))
169 Lisp_Object tem = XCAR (position);
170 if (CONSP (tem))
171 window = Fcar (XCDR (position));
172 else
174 tem = Fcar (XCDR (position)); /* EVENT_START (position) */
175 window = Fcar (tem); /* POSN_WINDOW (tem) */
178 else if (WINDOWP (position) || FRAMEP (position))
179 window = position;
180 else
181 window = Qnil;
183 /* Decode where to put the menu. */
185 if (FRAMEP (window))
186 f = XFRAME (window);
187 else if (WINDOWP (window))
189 CHECK_LIVE_WINDOW (window);
190 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
192 else
193 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
194 but I don't want to make one now. */
195 CHECK_WINDOW (window);
197 #ifndef HAVE_DIALOGS
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
205 /* Display a menu with these alternatives
206 in the middle of frame F. */
207 Lisp_Object x, y, frame, newpos;
208 XSETFRAME (frame, f);
209 XSETINT (x, x_pixel_width (f) / 2);
210 XSETINT (y, x_pixel_height (f) / 2);
211 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
212 return Fx_popup_menu (newpos,
213 Fcons (Fcar (contents), Fcons (contents, Qnil)));
216 #else /* HAVE_DIALOGS */
218 Lisp_Object title;
219 char *error_name;
220 Lisp_Object selection;
222 /* Decode the dialog items from what was specified. */
223 title = Fcar (contents);
224 CHECK_STRING (title);
226 list_of_panes (Fcons (contents, Qnil));
228 /* Display them in a dialog box. */
229 block_input ();
230 selection = w32_dialog_show (f, 0, title, header, &error_name);
231 unblock_input ();
233 discard_menu_items ();
234 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
236 if (error_name) error (error_name);
237 return selection;
239 #endif /* HAVE_DIALOGS */
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.
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.
250 But first we recompute the menu bar contents (the whole tree).
252 This way we can safely execute Lisp code. */
254 void
255 x_activate_menubar (FRAME_PTR f)
257 set_frame_menubar (f, 0, 1);
259 /* Lock out further menubar changes while active. */
260 f->output_data.w32->menubar_active = 1;
262 /* Signal input thread to return from WM_INITMENU. */
263 complete_deferred_msg (FRAME_W32_WINDOW (f), WM_INITMENU, 0);
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. */
271 void
272 menubar_selection_callback (FRAME_PTR f, void * client_data)
274 Lisp_Object prefix, entry;
275 Lisp_Object vector;
276 Lisp_Object *subprefix_stack;
277 int submenu_depth = 0;
278 int i;
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)
289 if (EQ (AREF (vector, i), Qnil))
291 subprefix_stack[submenu_depth++] = prefix;
292 prefix = entry;
293 i++;
295 else if (EQ (AREF (vector, i), Qlambda))
297 prefix = subprefix_stack[--submenu_depth];
298 i++;
300 else if (EQ (AREF (vector, i), Qt))
302 prefix = AREF (vector, i + MENU_ITEMS_PANE_PREFIX);
303 i += MENU_ITEMS_PANE_LENGTH;
305 else
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)
312 int j;
313 struct input_event buf;
314 Lisp_Object frame;
315 EVENT_INIT (buf);
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);
323 for (j = 0; j < submenu_depth; j++)
324 if (!NILP (subprefix_stack[j]))
326 buf.kind = MENU_BAR_EVENT;
327 buf.frame_or_window = frame;
328 buf.arg = subprefix_stack[j];
329 kbd_buffer_store_event (&buf);
332 if (!NILP (prefix))
334 buf.kind = MENU_BAR_EVENT;
335 buf.frame_or_window = frame;
336 buf.arg = prefix;
337 kbd_buffer_store_event (&buf);
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);
347 f->output_data.w32->menubar_active = 0;
348 return;
350 i += MENU_ITEMS_ITEM_LENGTH;
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;
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. */
363 void
364 set_frame_menubar (FRAME_PTR f, bool first_time, bool deep_p)
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;
373 /* We must not change the menubar when actually in use. */
374 if (f->output_data.w32->menubar_active)
375 return;
377 XSETFRAME (Vmenu_updating_frame, f);
379 if (! menubar_widget)
380 deep_p = 1;
382 if (deep_p)
384 /* Make a widget-value tree representing the entire menu trees. */
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);
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;
399 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
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);
405 record_unwind_save_match_data ();
407 if (NILP (Voverriding_local_map_menu_flag))
409 specbind (Qoverriding_terminal_local_map, Qnil);
410 specbind (Qoverriding_local_map, Qnil);
413 set_buffer_internal_1 (XBUFFER (buffer));
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)));
420 items = FRAME_MENU_BAR_ITEMS (f);
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);
427 /* Fill in menu_items with the current menu bar contents.
428 This can evaluate Lisp code. */
429 save_menu_items ();
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)
440 Lisp_Object key, string, maps;
442 last_i = i;
444 key = AREF (items, i);
445 string = AREF (items, i + 1);
446 maps = AREF (items, i + 2);
447 if (NILP (string))
448 break;
450 submenu_start[i] = menu_items_used;
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;
457 submenu_end[i] = menu_items_used;
460 finish_menu_items ();
462 /* Convert menu_items into widget_value trees
463 to display the menu. This cannot evaluate Lisp code. */
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;
473 for (i = 0; i < last_i; i += 4)
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;
488 set_buffer_internal_1 (prev);
490 /* If there has been no change in the Lisp-level contents
491 of the menu bar, skip redisplaying it. Just exit. */
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)
499 free_menubar_widget_value_tree (first_wv);
500 discard_menu_items ();
501 unbind_to (specpdl_count, Qnil);
502 return;
505 fset_menu_bar_vector (f, menu_items);
506 f->menu_bar_items_used = menu_items_used;
508 /* This undoes save_menu_items. */
509 unbind_to (specpdl_count, Qnil);
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)
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;
528 else
530 /* Make a widget-value tree containing
531 just the top level menu bar strings. */
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;
541 items = FRAME_MENU_BAR_ITEMS (f);
542 for (i = 0; i < ASIZE (items); i += 4)
544 Lisp_Object string;
546 string = AREF (items, i + 1);
547 if (NILP (string))
548 break;
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);
562 if (prev_wv)
563 prev_wv->next = wv;
564 else
565 first_wv->contents = wv;
566 prev_wv = wv;
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;
575 /* Create or update the menu bar widget. */
577 block_input ();
579 if (menubar_widget)
581 /* Empty current menubar, rather than creating a fresh one. */
582 while (DeleteMenu (menubar_widget, 0, MF_BYPOSITION))
585 else
587 menubar_widget = CreateMenu ();
589 fill_in_menu (menubar_widget, first_wv->contents);
591 free_menubar_widget_value_tree (first_wv);
594 HMENU old_widget = f->output_data.w32->menubar_widget;
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)); */
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));
607 unblock_input ();
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. */
615 void
616 initialize_frame_menubar (FRAME_PTR f)
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);
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. */
627 void
628 free_frame_menubar (FRAME_PTR f)
630 block_input ();
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);
639 unblock_input ();
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. */
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.) */
658 Lisp_Object
659 w32_menu_show (FRAME_PTR f, int x, int y, int for_click, int keymaps,
660 Lisp_Object title, const char **error)
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;
674 *error = NULL;
676 if (menu_items_n_panes == 0)
677 return Qnil;
679 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
681 *error = "Empty menu";
682 return Qnil;
685 /* Create a tree of widget_value objects
686 representing the panes and their items. */
687 wv = xmalloc_widget_value ();
688 wv->name = "menu";
689 wv->value = 0;
690 wv->enabled = 1;
691 wv->button_type = BUTTON_TYPE_NONE;
692 wv->help = Qnil;
693 first_wv = wv;
694 first_pane = 1;
696 /* Loop over all panes and items, filling in the tree. */
697 i = 0;
698 while (i < menu_items_used)
700 if (EQ (AREF (menu_items, i), Qnil))
702 submenu_stack[submenu_depth++] = save_wv;
703 save_wv = prev_wv;
704 prev_wv = 0;
705 first_pane = 1;
706 i++;
708 else if (EQ (AREF (menu_items, i), Qlambda))
710 prev_wv = save_wv;
711 save_wv = submenu_stack[--submenu_depth];
712 first_pane = 0;
713 i++;
715 else if (EQ (AREF (menu_items, i), Qt)
716 && submenu_depth != 0)
717 i += MENU_ITEMS_PANE_LENGTH;
718 /* Ignore a nil in the item list.
719 It's meaningful only for dialog boxes. */
720 else if (EQ (AREF (menu_items, i), Qquote))
721 i += 1;
722 else if (EQ (AREF (menu_items, i), Qt))
724 /* Create a new pane. */
725 Lisp_Object pane_name, prefix;
726 char *pane_string;
727 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
728 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
730 if (STRINGP (pane_name))
732 if (unicode_append_menu)
733 pane_name = ENCODE_UTF_8 (pane_name);
734 else if (STRING_MULTIBYTE (pane_name))
735 pane_name = ENCODE_SYSTEM (pane_name);
737 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
740 pane_string = (NILP (pane_name)
741 ? "" : SSDATA (pane_name));
742 /* If there is just one top-level pane, put all its items directly
743 under the top-level menu. */
744 if (menu_items_n_panes == 1)
745 pane_string = "";
747 /* If the pane has a meaningful name,
748 make the pane a top-level menu item
749 with its items as a submenu beneath it. */
750 if (!keymaps && strcmp (pane_string, ""))
752 wv = xmalloc_widget_value ();
753 if (save_wv)
754 save_wv->next = wv;
755 else
756 first_wv->contents = wv;
757 wv->name = pane_string;
758 if (keymaps && !NILP (prefix))
759 wv->name++;
760 wv->value = 0;
761 wv->enabled = 1;
762 wv->button_type = BUTTON_TYPE_NONE;
763 wv->help = Qnil;
764 save_wv = wv;
765 prev_wv = 0;
767 else if (first_pane)
769 save_wv = wv;
770 prev_wv = 0;
772 first_pane = 0;
773 i += MENU_ITEMS_PANE_LENGTH;
775 else
777 /* Create a new item within current pane. */
778 Lisp_Object item_name, enable, descrip, def, type, selected, help;
780 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
781 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
782 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
783 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
784 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
785 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
786 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
788 if (STRINGP (item_name))
790 if (unicode_append_menu)
791 item_name = ENCODE_UTF_8 (item_name);
792 else if (STRING_MULTIBYTE (item_name))
793 item_name = ENCODE_SYSTEM (item_name);
795 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
798 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
800 descrip = ENCODE_SYSTEM (descrip);
801 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
804 wv = xmalloc_widget_value ();
805 if (prev_wv)
806 prev_wv->next = wv;
807 else
808 save_wv->contents = wv;
809 wv->name = SSDATA (item_name);
810 if (!NILP (descrip))
811 wv->key = SSDATA (descrip);
812 wv->value = 0;
813 /* Use the contents index as call_data, since we are
814 restricted to 16-bits. */
815 wv->call_data = !NILP (def) ? (void *) (EMACS_INT) i : 0;
816 wv->enabled = !NILP (enable);
818 if (NILP (type))
819 wv->button_type = BUTTON_TYPE_NONE;
820 else if (EQ (type, QCtoggle))
821 wv->button_type = BUTTON_TYPE_TOGGLE;
822 else if (EQ (type, QCradio))
823 wv->button_type = BUTTON_TYPE_RADIO;
824 else
825 emacs_abort ();
827 wv->selected = !NILP (selected);
829 if (!STRINGP (help))
830 help = Qnil;
832 wv->help = help;
834 prev_wv = wv;
836 i += MENU_ITEMS_ITEM_LENGTH;
840 /* Deal with the title, if it is non-nil. */
841 if (!NILP (title))
843 widget_value *wv_title = xmalloc_widget_value ();
844 widget_value *wv_sep = xmalloc_widget_value ();
846 /* Maybe replace this separator with a bitmap or owner-draw item
847 so that it looks better. Having two separators looks odd. */
848 wv_sep->name = "--";
849 wv_sep->next = first_wv->contents;
850 wv_sep->help = Qnil;
852 if (unicode_append_menu)
853 title = ENCODE_UTF_8 (title);
854 else if (STRING_MULTIBYTE (title))
855 title = ENCODE_SYSTEM (title);
857 wv_title->name = SSDATA (title);
858 wv_title->enabled = TRUE;
859 wv_title->title = TRUE;
860 wv_title->button_type = BUTTON_TYPE_NONE;
861 wv_title->help = Qnil;
862 wv_title->next = wv_sep;
863 first_wv->contents = wv_title;
866 /* No selection has been chosen yet. */
867 menu_item_selection = 0;
869 /* Actually create the menu. */
870 current_popup_menu = menu = CreatePopupMenu ();
871 fill_in_menu (menu, first_wv->contents);
873 /* Adjust coordinates to be root-window-relative. */
874 pos.x = x;
875 pos.y = y;
876 ClientToScreen (FRAME_W32_WINDOW (f), &pos);
878 /* Display the menu. */
879 menu_item_selection = SendMessage (FRAME_W32_WINDOW (f),
880 WM_EMACS_TRACKPOPUPMENU,
881 (WPARAM)menu, (LPARAM)&pos);
883 /* Clean up extraneous mouse events which might have been generated
884 during the call. */
885 discard_mouse_events ();
886 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
888 /* Free the widget_value objects we used to specify the contents. */
889 free_menubar_widget_value_tree (first_wv);
891 DestroyMenu (menu);
893 /* Free the owner-drawn and help-echo menu strings. */
894 w32_free_menu_strings (FRAME_W32_WINDOW (f));
895 f->output_data.w32->menubar_active = 0;
897 /* Find the selected item, and its pane, to return
898 the proper value. */
899 if (menu_item_selection != 0)
901 Lisp_Object prefix, entry;
903 prefix = entry = Qnil;
904 i = 0;
905 while (i < menu_items_used)
907 if (EQ (AREF (menu_items, i), Qnil))
909 subprefix_stack[submenu_depth++] = prefix;
910 prefix = entry;
911 i++;
913 else if (EQ (AREF (menu_items, i), Qlambda))
915 prefix = subprefix_stack[--submenu_depth];
916 i++;
918 else if (EQ (AREF (menu_items, i), Qt))
920 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
921 i += MENU_ITEMS_PANE_LENGTH;
923 /* Ignore a nil in the item list.
924 It's meaningful only for dialog boxes. */
925 else if (EQ (AREF (menu_items, i), Qquote))
926 i += 1;
927 else
929 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
930 if (menu_item_selection == i)
932 if (keymaps != 0)
934 int j;
936 entry = Fcons (entry, Qnil);
937 if (!NILP (prefix))
938 entry = Fcons (prefix, entry);
939 for (j = submenu_depth - 1; j >= 0; j--)
940 if (!NILP (subprefix_stack[j]))
941 entry = Fcons (subprefix_stack[j], entry);
943 return entry;
945 i += MENU_ITEMS_ITEM_LENGTH;
949 else if (!for_click)
950 /* Make "Cancel" equivalent to C-g. */
951 Fsignal (Qquit, Qnil);
953 return Qnil;
957 #ifdef HAVE_DIALOGS
958 /* TODO: On Windows, there are two ways of defining a dialog.
960 1. Create a predefined dialog resource and include it in nt/emacs.rc.
961 Using this method, we could then set the titles and make unneeded
962 buttons invisible before displaying the dialog. Everything would
963 be a fixed size though, so there is a risk that text does not
964 fit on a button.
965 2. Create the dialog template in memory on the fly. This allows us
966 to size the dialog and buttons dynamically, probably giving more
967 natural looking results for dialogs with few buttons, and eliminating
968 the problem of text overflowing the buttons. But the API for this is
969 quite complex - structures have to be allocated in particular ways,
970 text content is tacked onto the end of structures in variable length
971 arrays with further structures tacked on after these, there are
972 certain alignment requirements for all this, and we have to
973 measure all the text and convert to "dialog coordinates" to figure
974 out how big to make everything.
976 For now, we'll just stick with menus for dialogs that are more
977 complicated than simple yes/no type questions for which we can use
978 the MessageBox function.
981 static char * button_names [] = {
982 "button1", "button2", "button3", "button4", "button5",
983 "button6", "button7", "button8", "button9", "button10" };
985 static Lisp_Object
986 w32_dialog_show (FRAME_PTR f, int keymaps,
987 Lisp_Object title, Lisp_Object header,
988 char **error)
990 int i, nb_buttons = 0;
991 char dialog_name[6];
992 int menu_item_selection;
994 widget_value *wv, *first_wv = 0, *prev_wv = 0;
996 /* Number of elements seen so far, before boundary. */
997 int left_count = 0;
998 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
999 int boundary_seen = 0;
1001 *error = NULL;
1003 if (menu_items_n_panes > 1)
1005 *error = "Multiple panes in dialog box";
1006 return Qnil;
1009 /* Create a tree of widget_value objects
1010 representing the text label and buttons. */
1012 Lisp_Object pane_name, prefix;
1013 char *pane_string;
1014 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
1015 prefix = AREF (menu_items, MENU_ITEMS_PANE_PREFIX);
1016 pane_string = (NILP (pane_name)
1017 ? "" : SSDATA (pane_name));
1018 prev_wv = xmalloc_widget_value ();
1019 prev_wv->value = pane_string;
1020 if (keymaps && !NILP (prefix))
1021 prev_wv->name++;
1022 prev_wv->enabled = 1;
1023 prev_wv->name = "message";
1024 prev_wv->help = Qnil;
1025 first_wv = prev_wv;
1027 /* Loop over all panes and items, filling in the tree. */
1028 i = MENU_ITEMS_PANE_LENGTH;
1029 while (i < menu_items_used)
1032 /* Create a new item within current pane. */
1033 Lisp_Object item_name, enable, descrip, help;
1035 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1036 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1037 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1038 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1040 if (NILP (item_name))
1042 free_menubar_widget_value_tree (first_wv);
1043 *error = "Submenu in dialog items";
1044 return Qnil;
1046 if (EQ (item_name, Qquote))
1048 /* This is the boundary between left-side elts
1049 and right-side elts. Stop incrementing right_count. */
1050 boundary_seen = 1;
1051 i++;
1052 continue;
1054 if (nb_buttons >= 9)
1056 free_menubar_widget_value_tree (first_wv);
1057 *error = "Too many dialog items";
1058 return Qnil;
1061 wv = xmalloc_widget_value ();
1062 prev_wv->next = wv;
1063 wv->name = (char *) button_names[nb_buttons];
1064 if (!NILP (descrip))
1065 wv->key = SSDATA (descrip);
1066 wv->value = SSDATA (item_name);
1067 wv->call_data = aref_addr (menu_items, i);
1068 wv->enabled = !NILP (enable);
1069 wv->help = Qnil;
1070 prev_wv = wv;
1072 if (! boundary_seen)
1073 left_count++;
1075 nb_buttons++;
1076 i += MENU_ITEMS_ITEM_LENGTH;
1079 /* If the boundary was not specified,
1080 by default put half on the left and half on the right. */
1081 if (! boundary_seen)
1082 left_count = nb_buttons - nb_buttons / 2;
1084 wv = xmalloc_widget_value ();
1085 wv->name = dialog_name;
1086 wv->help = Qnil;
1088 /* Frame title: 'Q' = Question, 'I' = Information.
1089 Can also have 'E' = Error if, one day, we want
1090 a popup for errors. */
1091 if (NILP (header))
1092 dialog_name[0] = 'Q';
1093 else
1094 dialog_name[0] = 'I';
1096 /* Dialog boxes use a really stupid name encoding
1097 which specifies how many buttons to use
1098 and how many buttons are on the right. */
1099 dialog_name[1] = '0' + nb_buttons;
1100 dialog_name[2] = 'B';
1101 dialog_name[3] = 'R';
1102 /* Number of buttons to put on the right. */
1103 dialog_name[4] = '0' + nb_buttons - left_count;
1104 dialog_name[5] = 0;
1105 wv->contents = first_wv;
1106 first_wv = wv;
1109 /* Actually create the dialog. */
1110 dialog_id = widget_id_tick++;
1111 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1112 f->output_data.w32->widget, 1, 0,
1113 dialog_selection_callback, 0);
1114 lw_modify_all_widgets (dialog_id, first_wv->contents, TRUE);
1116 /* Free the widget_value objects we used to specify the contents. */
1117 free_menubar_widget_value_tree (first_wv);
1119 /* No selection has been chosen yet. */
1120 menu_item_selection = 0;
1122 /* Display the menu. */
1123 lw_pop_up_all_widgets (dialog_id);
1125 /* Process events that apply to the menu. */
1126 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
1128 lw_destroy_all_widgets (dialog_id);
1130 /* Find the selected item, and its pane, to return
1131 the proper value. */
1132 if (menu_item_selection != 0)
1134 Lisp_Object prefix;
1136 prefix = Qnil;
1137 i = 0;
1138 while (i < menu_items_used)
1140 Lisp_Object entry;
1142 if (EQ (AREF (menu_items, i), Qt))
1144 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1145 i += MENU_ITEMS_PANE_LENGTH;
1147 else
1149 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
1150 if (menu_item_selection == i)
1152 if (keymaps != 0)
1154 entry = Fcons (entry, Qnil);
1155 if (!NILP (prefix))
1156 entry = Fcons (prefix, entry);
1158 return entry;
1160 i += MENU_ITEMS_ITEM_LENGTH;
1164 else
1165 /* Make "Cancel" equivalent to C-g. */
1166 Fsignal (Qquit, Qnil);
1168 return Qnil;
1170 #else /* !HAVE_DIALOGS */
1172 /* Currently we only handle Yes No dialogs (y-or-n-p and yes-or-no-p) as
1173 simple dialogs. We could handle a few more, but I'm not aware of
1174 anywhere in Emacs that uses the other specific dialog choices that
1175 MessageBox provides. */
1177 static int
1178 is_simple_dialog (Lisp_Object contents)
1180 Lisp_Object options;
1181 Lisp_Object name, yes, no, other;
1183 if (!CONSP (contents))
1184 return 0;
1185 options = XCDR (contents);
1187 yes = build_string ("Yes");
1188 no = build_string ("No");
1190 if (!CONSP (options))
1191 return 0;
1193 name = XCAR (options);
1194 if (!CONSP (name))
1195 return 0;
1196 name = XCAR (name);
1198 if (!NILP (Fstring_equal (name, yes)))
1199 other = no;
1200 else if (!NILP (Fstring_equal (name, no)))
1201 other = yes;
1202 else
1203 return 0;
1205 options = XCDR (options);
1206 if (!CONSP (options))
1207 return 0;
1209 name = XCAR (options);
1210 if (!CONSP (name))
1211 return 0;
1212 name = XCAR (name);
1213 if (NILP (Fstring_equal (name, other)))
1214 return 0;
1216 /* Check there are no more options. */
1217 options = XCDR (options);
1218 return !(CONSP (options));
1221 static Lisp_Object
1222 simple_dialog_show (FRAME_PTR f, Lisp_Object contents, Lisp_Object header)
1224 int answer;
1225 UINT type;
1226 Lisp_Object lispy_answer = Qnil, temp = XCAR (contents);
1228 type = MB_YESNO;
1230 /* Since we only handle Yes/No dialogs, and we already checked
1231 is_simple_dialog, we don't need to worry about checking contents
1232 to see what type of dialog to use. */
1234 /* Use Unicode if possible, so any language can be displayed. */
1235 if (unicode_message_box)
1237 WCHAR *text, *title;
1238 USE_SAFE_ALLOCA;
1240 if (STRINGP (temp))
1242 char *utf8_text = SDATA (ENCODE_UTF_8 (temp));
1243 /* Be pessimistic about the number of characters needed.
1244 Remember characters outside the BMP will take more than
1245 one utf16 word, so we cannot simply use the character
1246 length of temp. */
1247 int utf8_len = strlen (utf8_text);
1248 text = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR));
1249 utf8to16 (utf8_text, utf8_len, text);
1251 else
1253 text = L"";
1256 if (NILP (header))
1258 title = L"Question";
1259 type |= MB_ICONQUESTION;
1261 else
1263 title = L"Information";
1264 type |= MB_ICONINFORMATION;
1267 answer = unicode_message_box (FRAME_W32_WINDOW (f), text, title, type);
1268 SAFE_FREE ();
1270 else
1272 char *text, *title;
1274 /* Fall back on ANSI message box, but at least use system
1275 encoding so questions representable by the system codepage
1276 are encoded properly. */
1277 if (STRINGP (temp))
1278 text = SDATA (ENCODE_SYSTEM (temp));
1279 else
1280 text = "";
1282 if (NILP (header))
1284 title = "Question";
1285 type |= MB_ICONQUESTION;
1287 else
1289 title = "Information";
1290 type |= MB_ICONINFORMATION;
1293 answer = MessageBox (FRAME_W32_WINDOW (f), text, title, type);
1296 if (answer == IDYES)
1297 lispy_answer = build_string ("Yes");
1298 else if (answer == IDNO)
1299 lispy_answer = build_string ("No");
1300 else
1301 Fsignal (Qquit, Qnil);
1303 for (temp = XCDR (contents); CONSP (temp); temp = XCDR (temp))
1305 Lisp_Object item, name, value;
1306 item = XCAR (temp);
1307 if (CONSP (item))
1309 name = XCAR (item);
1310 value = XCDR (item);
1312 else
1314 name = item;
1315 value = Qnil;
1318 if (!NILP (Fstring_equal (name, lispy_answer)))
1320 return value;
1323 Fsignal (Qquit, Qnil);
1324 return Qnil;
1326 #endif /* !HAVE_DIALOGS */
1329 /* UTF8: 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx, 10xxxxxx, 10xxxxxx */
1330 static void
1331 utf8to16 (unsigned char * src, int len, WCHAR * dest)
1333 while (len > 0)
1335 if (*src < 0x80)
1337 *dest = (WCHAR) *src;
1338 dest++; src++; len--;
1340 /* Since we might get >3 byte sequences which we don't handle, ignore the extra parts. */
1341 else if (*src < 0xC0)
1343 src++; len--;
1345 /* 2 char UTF-8 sequence. */
1346 else if (*src < 0xE0)
1348 *dest = (WCHAR) (((*src & 0x1f) << 6)
1349 | (*(src + 1) & 0x3f));
1350 src += 2; len -= 2; dest++;
1352 else if (*src < 0xF0)
1354 *dest = (WCHAR) (((*src & 0x0f) << 12)
1355 | ((*(src + 1) & 0x3f) << 6)
1356 | (*(src + 2) & 0x3f));
1357 src += 3; len -= 3; dest++;
1359 else /* Not encodable. Insert Unicode Substitution char. */
1361 *dest = (WCHAR) 0xfffd;
1362 src++; len--; dest++;
1365 *dest = 0;
1368 static int
1369 add_menu_item (HMENU menu, widget_value *wv, HMENU item)
1371 UINT fuFlags;
1372 char *out_string, *p, *q;
1373 int return_value;
1374 size_t nlen, orig_len;
1375 USE_SAFE_ALLOCA;
1377 if (menu_separator_name_p (wv->name))
1379 fuFlags = MF_SEPARATOR;
1380 out_string = NULL;
1382 else
1384 if (wv->enabled)
1385 fuFlags = MF_STRING;
1386 else
1387 fuFlags = MF_STRING | MF_GRAYED;
1389 if (wv->key != NULL)
1391 out_string = SAFE_ALLOCA (strlen (wv->name) + strlen (wv->key) + 2);
1392 strcpy (out_string, wv->name);
1393 strcat (out_string, "\t");
1394 strcat (out_string, wv->key);
1396 else
1397 out_string = (char *)wv->name;
1399 /* Quote any special characters within the menu item's text and
1400 key binding. */
1401 nlen = orig_len = strlen (out_string);
1402 if (unicode_append_menu)
1404 /* With UTF-8, & cannot be part of a multibyte character. */
1405 for (p = out_string; *p; p++)
1407 if (*p == '&')
1408 nlen++;
1411 #ifndef NTGUI_UNICODE
1412 else
1414 /* If encoded with the system codepage, use multibyte string
1415 functions in case of multibyte characters that contain '&'. */
1416 for (p = out_string; *p; p = _mbsinc (p))
1418 if (_mbsnextc (p) == '&')
1419 nlen++;
1422 #endif /* !NTGUI_UNICODE */
1424 if (nlen > orig_len)
1426 p = out_string;
1427 out_string = SAFE_ALLOCA (nlen + 1);
1428 q = out_string;
1429 while (*p)
1431 if (unicode_append_menu)
1433 if (*p == '&')
1434 *q++ = *p;
1435 *q++ = *p++;
1437 #ifndef NTGUI_UNICODE
1438 else
1440 if (_mbsnextc (p) == '&')
1442 _mbsncpy (q, p, 1);
1443 q = _mbsinc (q);
1445 _mbsncpy (q, p, 1);
1446 p = _mbsinc (p);
1447 q = _mbsinc (q);
1449 #endif /* !NTGUI_UNICODE */
1451 *q = '\0';
1454 if (item != NULL)
1455 fuFlags = MF_POPUP;
1456 else if (wv->title || wv->call_data == 0)
1458 /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since
1459 we can't deallocate the memory otherwise. */
1460 if (get_menu_item_info)
1462 out_string = (char *) local_alloc (strlen (wv->name) + 1);
1463 strcpy (out_string, wv->name);
1464 #ifdef MENU_DEBUG
1465 DebPrint ("Menu: allocating %ld for owner-draw", out_string);
1466 #endif
1467 fuFlags = MF_OWNERDRAW | MF_DISABLED;
1469 else
1470 fuFlags = MF_DISABLED;
1473 /* Draw radio buttons and tickboxes. */
1474 else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
1475 wv->button_type == BUTTON_TYPE_RADIO))
1476 fuFlags |= MF_CHECKED;
1477 else
1478 fuFlags |= MF_UNCHECKED;
1481 if (unicode_append_menu && out_string)
1483 /* Convert out_string from UTF-8 to UTF-16-LE. */
1484 int utf8_len = strlen (out_string);
1485 WCHAR * utf16_string;
1486 if (fuFlags & MF_OWNERDRAW)
1487 utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR));
1488 else
1489 utf16_string = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR));
1491 utf8to16 (out_string, utf8_len, utf16_string);
1492 return_value = unicode_append_menu (menu, fuFlags,
1493 item != NULL ? (UINT_PTR) item
1494 : (UINT_PTR) wv->call_data,
1495 utf16_string);
1497 #ifndef NTGUI_UNICODE /* Fallback does not apply when always UNICODE */
1498 if (!return_value)
1500 /* On W9x/ME, Unicode menus are not supported, though AppendMenuW
1501 apparently does exist at least in some cases and appears to be
1502 stubbed out to do nothing. out_string is UTF-8, but since
1503 our standard menus are in English and this is only going to
1504 happen the first time a menu is used, the encoding is
1505 of minor importance compared with menus not working at all. */
1506 return_value =
1507 AppendMenu (menu, fuFlags,
1508 item != NULL ? (UINT_PTR) item: (UINT_PTR) wv->call_data,
1509 out_string);
1510 /* Don't use Unicode menus in future, unless this is Windows
1511 NT or later, where a failure of AppendMenuW does NOT mean
1512 Unicode menus are unsupported. */
1513 if (osinfo_cache.dwPlatformId != VER_PLATFORM_WIN32_NT)
1514 unicode_append_menu = NULL;
1516 #endif /* NTGUI_UNICODE */
1518 if (unicode_append_menu && (fuFlags & MF_OWNERDRAW))
1519 local_free (out_string);
1521 else
1523 return_value =
1524 AppendMenu (menu,
1525 fuFlags,
1526 item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data,
1527 out_string );
1530 /* This must be done after the menu item is created. */
1531 if (!wv->title && wv->call_data != 0)
1533 if (set_menu_item_info)
1535 MENUITEMINFO info;
1536 memset (&info, 0, sizeof (info));
1537 info.cbSize = sizeof (info);
1538 info.fMask = MIIM_DATA;
1540 /* Set help string for menu item. Leave it as a Lisp_Object
1541 until it is ready to be displayed, since GC can happen while
1542 menus are active. */
1543 if (!NILP (wv->help))
1545 /* As of Jul-2012, w32api headers say that dwItemData
1546 has DWORD type, but that's a bug: it should actually
1547 be ULONG_PTR, which is correct for 32-bit and 64-bit
1548 Windows alike. MSVC headers get it right; hopefully,
1549 MinGW headers will, too. */
1550 info.dwItemData = (ULONG_PTR) XLI (wv->help);
1552 if (wv->button_type == BUTTON_TYPE_RADIO)
1554 /* CheckMenuRadioItem allows us to differentiate TOGGLE and
1555 RADIO items, but is not available on NT 3.51 and earlier. */
1556 info.fMask |= MIIM_TYPE | MIIM_STATE;
1557 info.fType = MFT_RADIOCHECK | MFT_STRING;
1558 info.dwTypeData = out_string;
1559 info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED;
1562 set_menu_item_info (menu,
1563 item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data,
1564 FALSE, &info);
1567 SAFE_FREE ();
1568 return return_value;
1571 /* Construct native Windows menu(bar) based on widget_value tree. */
1572 static int
1573 fill_in_menu (HMENU menu, widget_value *wv)
1575 for ( ; wv != NULL; wv = wv->next)
1577 if (wv->contents)
1579 HMENU sub_menu = CreatePopupMenu ();
1581 if (sub_menu == NULL)
1582 return 0;
1584 if (!fill_in_menu (sub_menu, wv->contents) ||
1585 !add_menu_item (menu, wv, sub_menu))
1587 DestroyMenu (sub_menu);
1588 return 0;
1591 else
1593 if (!add_menu_item (menu, wv, NULL))
1594 return 0;
1597 return 1;
1600 /* Display help string for currently pointed to menu item. Not
1601 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
1602 available. */
1603 void
1604 w32_menu_display_help (HWND owner, HMENU menu, UINT item, UINT flags)
1606 if (get_menu_item_info)
1608 struct frame *f = x_window_to_frame (&one_w32_display_info, owner);
1609 Lisp_Object frame, help;
1611 /* No help echo on owner-draw menu items, or when the keyboard is used
1612 to navigate the menus, since tooltips are distracting if they pop
1613 up elsewhere. */
1614 if (flags & MF_OWNERDRAW || flags & MF_POPUP
1615 || !(flags & MF_MOUSESELECT))
1616 help = Qnil;
1617 else
1619 MENUITEMINFO info;
1621 memset (&info, 0, sizeof (info));
1622 info.cbSize = sizeof (info);
1623 info.fMask = MIIM_DATA;
1624 get_menu_item_info (menu, item, FALSE, &info);
1626 help = info.dwItemData ? XIL (info.dwItemData) : Qnil;
1629 /* Store the help echo in the keyboard buffer as the X toolkit
1630 version does, rather than directly showing it. This seems to
1631 solve the GC problems that were present when we based the
1632 Windows code on the non-toolkit version. */
1633 if (f)
1635 XSETFRAME (frame, f);
1636 kbd_buffer_store_help_event (frame, help);
1638 else
1639 /* X version has a loop through frames here, which doesn't
1640 appear to do anything, unless it has some side effect. */
1641 show_help_echo (help, Qnil, Qnil, Qnil);
1645 /* Free memory used by owner-drawn strings. */
1646 static void
1647 w32_free_submenu_strings (HMENU menu)
1649 int i, num = GetMenuItemCount (menu);
1650 for (i = 0; i < num; i++)
1652 MENUITEMINFO info;
1653 memset (&info, 0, sizeof (info));
1654 info.cbSize = sizeof (info);
1655 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_SUBMENU;
1657 get_menu_item_info (menu, i, TRUE, &info);
1659 /* Owner-drawn names are held in dwItemData. */
1660 if ((info.fType & MF_OWNERDRAW) && info.dwItemData)
1662 #ifdef MENU_DEBUG
1663 DebPrint ("Menu: freeing %ld for owner-draw", info.dwItemData);
1664 #endif
1665 local_free (info.dwItemData);
1668 /* Recurse down submenus. */
1669 if (info.hSubMenu)
1670 w32_free_submenu_strings (info.hSubMenu);
1674 void
1675 w32_free_menu_strings (HWND hwnd)
1677 HMENU menu = current_popup_menu;
1679 if (get_menu_item_info)
1681 /* If there is no popup menu active, free the strings from the frame's
1682 menubar. */
1683 if (!menu)
1684 menu = GetMenu (hwnd);
1686 if (menu)
1687 w32_free_submenu_strings (menu);
1690 current_popup_menu = NULL;
1693 #endif /* HAVE_MENUS */
1695 /* The following is used by delayed window autoselection. */
1697 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
1698 doc: /* Return t if a menu or popup dialog is active on selected frame. */)
1699 (void)
1701 #ifdef HAVE_MENUS
1702 FRAME_PTR f;
1703 f = SELECTED_FRAME ();
1704 return (f->output_data.w32->menubar_active > 0) ? Qt : Qnil;
1705 #else
1706 return Qnil;
1707 #endif /* HAVE_MENUS */
1710 void
1711 syms_of_w32menu (void)
1713 globals_of_w32menu ();
1715 current_popup_menu = NULL;
1717 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
1719 defsubr (&Smenu_or_popup_active_p);
1720 #ifdef HAVE_MENUS
1721 defsubr (&Sx_popup_dialog);
1722 #endif
1726 globals_of_w32menu is used to initialize those global variables that
1727 must always be initialized on startup even when the global variable
1728 initialized is non zero (see the function main in emacs.c).
1729 globals_of_w32menu is called from syms_of_w32menu when the global
1730 variable initialized is 0 and directly from main when initialized
1731 is non zero.
1733 void
1734 globals_of_w32menu (void)
1736 #ifndef NTGUI_UNICODE
1737 /* See if Get/SetMenuItemInfo functions are available. */
1738 HMODULE user32 = GetModuleHandle ("user32.dll");
1739 get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA");
1740 set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA");
1741 unicode_append_menu = (AppendMenuW_Proc) GetProcAddress (user32, "AppendMenuW");
1742 unicode_message_box = (MessageBoxW_Proc) GetProcAddress (user32, "MessageBoxW");
1743 #endif /* !NTGUI_UNICODE */