Fix documentation of `window-combination-resize'
[emacs.git] / src / w32menu.c
blobd9ab8f5e518dea1e6bb52033ba79e41848176b61
1 /* Menu support for GNU Emacs on the Microsoft Windows API.
2 Copyright (C) 1986, 1988, 1993-1994, 1996, 1998-1999, 2001-2016 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 (at
10 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 "frame.h"
29 #include "blockinput.h"
30 #include "buffer.h"
31 #include "coding.h" /* for ENCODE_SYSTEM */
32 #include "menu.h"
34 /* This may include sys/types.h, and that somehow loses
35 if this is not done before the other system files. */
36 #include "w32term.h"
38 /* Cygwin does not support the multibyte string functions declared in
39 * mbstring.h below --- but that's okay: because Cygwin is
40 * UNICODE-only, we don't need to use these functions anyway. */
42 #ifndef NTGUI_UNICODE
43 #include <mbstring.h>
44 #endif /* !NTGUI_UNICODE */
46 /* Load sys/types.h if not already loaded.
47 In some systems loading it twice is suicidal. */
48 #ifndef makedev
49 #include <sys/types.h>
50 #endif
52 #include "w32common.h" /* for osinfo_cache */
54 #undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
56 #ifndef TRUE
57 #define TRUE 1
58 #define FALSE 0
59 #endif /* no TRUE */
61 HMENU current_popup_menu;
63 void syms_of_w32menu (void);
64 void globals_of_w32menu (void);
66 typedef BOOL (WINAPI * GetMenuItemInfoA_Proc) (
67 IN HMENU,
68 IN UINT,
69 IN BOOL,
70 IN OUT LPMENUITEMINFOA);
71 typedef BOOL (WINAPI * SetMenuItemInfoA_Proc) (
72 IN HMENU,
73 IN UINT,
74 IN BOOL,
75 IN LPCMENUITEMINFOA);
76 typedef int (WINAPI * MessageBoxW_Proc) (
77 IN HWND window,
78 IN const WCHAR *text,
79 IN const WCHAR *caption,
80 IN UINT type);
82 #ifdef NTGUI_UNICODE
83 #define get_menu_item_info GetMenuItemInfoA
84 #define set_menu_item_info SetMenuItemInfoA
85 #define unicode_append_menu AppendMenuW
86 #define unicode_message_box MessageBoxW
87 #else /* !NTGUI_UNICODE */
88 GetMenuItemInfoA_Proc get_menu_item_info = NULL;
89 SetMenuItemInfoA_Proc set_menu_item_info = NULL;
90 AppendMenuW_Proc unicode_append_menu = NULL;
91 MessageBoxW_Proc unicode_message_box = NULL;
92 #endif /* NTGUI_UNICODE */
94 void set_frame_menubar (struct frame *, bool, bool);
96 #ifdef HAVE_DIALOGS
97 static Lisp_Object w32_dialog_show (struct frame *, Lisp_Object, Lisp_Object, char **);
98 #else
99 static bool is_simple_dialog (Lisp_Object);
100 static Lisp_Object simple_dialog_show (struct frame *, Lisp_Object, Lisp_Object);
101 #endif
103 static void utf8to16 (unsigned char *, int, WCHAR *);
104 static int fill_in_menu (HMENU, widget_value *);
106 void w32_free_menu_strings (HWND);
108 Lisp_Object
109 w32_popup_dialog (struct frame *f, Lisp_Object header, Lisp_Object contents)
112 check_window_system (f);
114 #ifndef HAVE_DIALOGS
116 /* Handle simple Yes/No choices as MessageBox popups. */
117 if (is_simple_dialog (contents))
118 return simple_dialog_show (f, contents, header);
119 else
120 return Qunsupported__w32_dialog;
121 #else /* HAVE_DIALOGS */
123 Lisp_Object title;
124 char *error_name;
125 Lisp_Object selection;
127 /* Decode the dialog items from what was specified. */
128 title = Fcar (contents);
129 CHECK_STRING (title);
131 list_of_panes (Fcons (contents, Qnil));
133 /* Display them in a dialog box. */
134 block_input ();
135 selection = w32_dialog_show (f, title, header, &error_name);
136 unblock_input ();
138 discard_menu_items ();
139 FRAME_DISPLAY_INFO (f)->grabbed = 0;
141 if (error_name) error (error_name);
142 return selection;
144 #endif /* HAVE_DIALOGS */
147 /* Activate the menu bar of frame F.
148 This is called from keyboard.c when it gets the
149 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
151 To activate the menu bar, we signal to the input thread that it can
152 return from the WM_INITMENU message, allowing the normal Windows
153 processing of the menus.
155 But first we recompute the menu bar contents (the whole tree).
157 This way we can safely execute Lisp code. */
159 void
160 x_activate_menubar (struct frame *f)
162 set_frame_menubar (f, false, true);
164 /* Lock out further menubar changes while active. */
165 f->output_data.w32->menubar_active = 1;
167 /* Signal input thread to return from WM_INITMENU. */
168 complete_deferred_msg (FRAME_W32_WINDOW (f), WM_INITMENU, 0);
171 /* This callback is called from the menu bar pulldown menu
172 when the user makes a selection.
173 Figure out what the user chose
174 and put the appropriate events into the keyboard buffer. */
176 void
177 menubar_selection_callback (struct frame *f, void * client_data)
179 Lisp_Object prefix, entry;
180 Lisp_Object vector;
181 Lisp_Object *subprefix_stack;
182 int submenu_depth = 0;
183 int i;
185 if (!f)
186 return;
187 entry = Qnil;
188 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * word_size);
189 vector = f->menu_bar_vector;
190 prefix = Qnil;
191 i = 0;
192 while (i < f->menu_bar_items_used)
194 if (EQ (AREF (vector, i), Qnil))
196 subprefix_stack[submenu_depth++] = prefix;
197 prefix = entry;
198 i++;
200 else if (EQ (AREF (vector, i), Qlambda))
202 prefix = subprefix_stack[--submenu_depth];
203 i++;
205 else if (EQ (AREF (vector, i), Qt))
207 prefix = AREF (vector, i + MENU_ITEMS_PANE_PREFIX);
208 i += MENU_ITEMS_PANE_LENGTH;
210 else
212 entry = AREF (vector, i + MENU_ITEMS_ITEM_VALUE);
213 /* The UINT_PTR cast avoids a warning. There's no problem
214 as long as pointers have enough bits to hold small integers. */
215 if ((int) (UINT_PTR) client_data == i)
217 int j;
218 struct input_event buf;
219 Lisp_Object frame;
220 EVENT_INIT (buf);
222 XSETFRAME (frame, f);
223 buf.kind = MENU_BAR_EVENT;
224 buf.frame_or_window = frame;
225 buf.arg = frame;
226 kbd_buffer_store_event (&buf);
228 for (j = 0; j < submenu_depth; j++)
229 if (!NILP (subprefix_stack[j]))
231 buf.kind = MENU_BAR_EVENT;
232 buf.frame_or_window = frame;
233 buf.arg = subprefix_stack[j];
234 kbd_buffer_store_event (&buf);
237 if (!NILP (prefix))
239 buf.kind = MENU_BAR_EVENT;
240 buf.frame_or_window = frame;
241 buf.arg = prefix;
242 kbd_buffer_store_event (&buf);
245 buf.kind = MENU_BAR_EVENT;
246 buf.frame_or_window = frame;
247 buf.arg = entry;
248 /* Free memory used by owner-drawn and help-echo strings. */
249 w32_free_menu_strings (FRAME_W32_WINDOW (f));
250 kbd_buffer_store_event (&buf);
252 f->output_data.w32->menubar_active = 0;
253 return;
255 i += MENU_ITEMS_ITEM_LENGTH;
258 /* Free memory used by owner-drawn and help-echo strings. */
259 w32_free_menu_strings (FRAME_W32_WINDOW (f));
260 f->output_data.w32->menubar_active = 0;
264 /* Set the contents of the menubar widgets of frame F.
265 The argument FIRST_TIME is currently ignored;
266 it is set the first time this is called, from initialize_frame_menubar. */
268 void
269 set_frame_menubar (struct frame *f, bool first_time, bool deep_p)
271 HMENU menubar_widget = f->output_data.w32->menubar_widget;
272 Lisp_Object items;
273 widget_value *wv, *first_wv, *prev_wv = 0;
274 int i, last_i;
275 int *submenu_start, *submenu_end;
276 int *submenu_top_level_items, *submenu_n_panes;
278 /* We must not change the menubar when actually in use. */
279 if (f->output_data.w32->menubar_active)
280 return;
282 XSETFRAME (Vmenu_updating_frame, f);
284 if (! menubar_widget)
285 deep_p = true;
287 if (deep_p)
289 /* Make a widget-value tree representing the entire menu trees. */
291 struct buffer *prev = current_buffer;
292 Lisp_Object buffer;
293 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
294 int previous_menu_items_used = f->menu_bar_items_used;
295 Lisp_Object *previous_items
296 = (Lisp_Object *) alloca (previous_menu_items_used
297 * word_size);
299 /* If we are making a new widget, its contents are empty,
300 do always reinitialize them. */
301 if (! menubar_widget)
302 previous_menu_items_used = 0;
304 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->contents;
305 specbind (Qinhibit_quit, Qt);
306 /* Don't let the debugger step into this code
307 because it is not reentrant. */
308 specbind (Qdebug_on_next_call, Qnil);
310 record_unwind_save_match_data ();
312 if (NILP (Voverriding_local_map_menu_flag))
314 specbind (Qoverriding_terminal_local_map, Qnil);
315 specbind (Qoverriding_local_map, Qnil);
318 set_buffer_internal_1 (XBUFFER (buffer));
320 /* Run the hooks. */
321 safe_run_hooks (Qactivate_menubar_hook);
322 safe_run_hooks (Qmenu_bar_update_hook);
323 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
325 items = FRAME_MENU_BAR_ITEMS (f);
327 /* Save the frame's previous menu bar contents data. */
328 if (previous_menu_items_used)
329 memcpy (previous_items, XVECTOR (f->menu_bar_vector)->contents,
330 previous_menu_items_used * word_size);
332 /* Fill in menu_items with the current menu bar contents.
333 This can evaluate Lisp code. */
334 save_menu_items ();
336 menu_items = f->menu_bar_vector;
337 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
338 submenu_start = (int *) alloca (ASIZE (items) * sizeof (int));
339 submenu_end = (int *) alloca (ASIZE (items) * sizeof (int));
340 submenu_n_panes = (int *) alloca (ASIZE (items) * sizeof (int));
341 submenu_top_level_items = (int *) alloca (ASIZE (items) * sizeof (int));
342 init_menu_items ();
343 for (i = 0; i < ASIZE (items); i += 4)
345 Lisp_Object key, string, maps;
347 last_i = i;
349 key = AREF (items, i);
350 string = AREF (items, i + 1);
351 maps = AREF (items, i + 2);
352 if (NILP (string))
353 break;
355 submenu_start[i] = menu_items_used;
357 menu_items_n_panes = 0;
358 submenu_top_level_items[i]
359 = parse_single_submenu (key, string, maps);
360 submenu_n_panes[i] = menu_items_n_panes;
362 submenu_end[i] = menu_items_used;
365 finish_menu_items ();
367 /* Convert menu_items into widget_value trees
368 to display the menu. This cannot evaluate Lisp code. */
370 wv = make_widget_value ("menubar", NULL, true, Qnil);
371 wv->button_type = BUTTON_TYPE_NONE;
372 first_wv = wv;
374 for (i = 0; i < last_i; i += 4)
376 menu_items_n_panes = submenu_n_panes[i];
377 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
378 submenu_top_level_items[i]);
379 if (prev_wv)
380 prev_wv->next = wv;
381 else
382 first_wv->contents = wv;
383 /* Don't set wv->name here; GC during the loop might relocate it. */
384 wv->enabled = true;
385 wv->button_type = BUTTON_TYPE_NONE;
386 prev_wv = wv;
389 set_buffer_internal_1 (prev);
391 /* If there has been no change in the Lisp-level contents
392 of the menu bar, skip redisplaying it. Just exit. */
394 for (i = 0; i < previous_menu_items_used; i++)
395 if (menu_items_used == i
396 || (!EQ (previous_items[i], AREF (menu_items, i))))
397 break;
398 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
400 free_menubar_widget_value_tree (first_wv);
401 discard_menu_items ();
402 unbind_to (specpdl_count, Qnil);
403 return;
406 fset_menu_bar_vector (f, menu_items);
407 f->menu_bar_items_used = menu_items_used;
409 /* This undoes save_menu_items. */
410 unbind_to (specpdl_count, Qnil);
412 /* Now GC cannot happen during the lifetime of the widget_value,
413 so it's safe to store data from a Lisp_String, as long as
414 local copies are made when the actual menu is created.
415 Windows takes care of this for normal string items, but
416 not for owner-drawn items or additional item-info. */
417 wv = first_wv->contents;
418 for (i = 0; i < ASIZE (items); i += 4)
420 Lisp_Object string;
421 string = AREF (items, i + 1);
422 if (NILP (string))
423 break;
424 wv->name = SSDATA (string);
425 update_submenu_strings (wv->contents);
426 wv = wv->next;
429 else
431 /* Make a widget-value tree containing
432 just the top level menu bar strings. */
434 wv = make_widget_value ("menubar", NULL, true, Qnil);
435 wv->button_type = BUTTON_TYPE_NONE;
436 first_wv = wv;
438 items = FRAME_MENU_BAR_ITEMS (f);
439 for (i = 0; i < ASIZE (items); i += 4)
441 Lisp_Object string;
443 string = AREF (items, i + 1);
444 if (NILP (string))
445 break;
447 wv = make_widget_value (SSDATA (string), NULL, true, Qnil);
448 wv->button_type = BUTTON_TYPE_NONE;
449 /* This prevents lwlib from assuming this
450 menu item is really supposed to be empty. */
451 /* The EMACS_INT cast avoids a warning.
452 This value just has to be different from small integers. */
453 wv->call_data = (void *) (EMACS_INT) (-1);
455 if (prev_wv)
456 prev_wv->next = wv;
457 else
458 first_wv->contents = wv;
459 prev_wv = wv;
462 /* Forget what we thought we knew about what is in the
463 detailed contents of the menu bar menus.
464 Changing the top level always destroys the contents. */
465 f->menu_bar_items_used = 0;
468 /* Create or update the menu bar widget. */
470 block_input ();
472 if (menubar_widget)
474 /* Empty current menubar, rather than creating a fresh one. */
475 while (DeleteMenu (menubar_widget, 0, MF_BYPOSITION))
478 else
480 menubar_widget = CreateMenu ();
482 fill_in_menu (menubar_widget, first_wv->contents);
484 free_menubar_widget_value_tree (first_wv);
487 HMENU old_widget = f->output_data.w32->menubar_widget;
489 f->output_data.w32->menubar_widget = menubar_widget;
490 SetMenu (FRAME_W32_WINDOW (f), f->output_data.w32->menubar_widget);
491 /* Causes flicker when menu bar is updated
492 DrawMenuBar (FRAME_W32_WINDOW (f)); */
494 /* Force the window size to be recomputed so that the frame's text
495 area remains the same, if menubar has just been created. */
496 if (old_widget == NULL)
498 windows_or_buffers_changed = 23;
499 adjust_frame_size (f, -1, -1, 2, false, Qmenu_bar_lines);
503 unblock_input ();
506 /* Called from Fx_create_frame to create the initial menubar of a frame
507 before it is mapped, so that the window is mapped with the menubar already
508 there instead of us tacking it on later and thrashing the window after it
509 is visible. */
511 void
512 initialize_frame_menubar (struct frame *f)
514 /* This function is called before the first chance to redisplay
515 the frame. It has to be, so the frame will have the right size. */
516 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
517 set_frame_menubar (f, true, true);
520 /* Get rid of the menu bar of frame F, and free its storage.
521 This is used when deleting a frame, and when turning off the menu bar. */
523 void
524 free_frame_menubar (struct frame *f)
526 block_input ();
529 HMENU old = GetMenu (FRAME_W32_WINDOW (f));
530 SetMenu (FRAME_W32_WINDOW (f), NULL);
531 f->output_data.w32->menubar_widget = NULL;
532 DestroyMenu (old);
535 unblock_input ();
539 /* w32_menu_show actually displays a menu using the panes and items in
540 menu_items and returns the value selected from it; we assume input
541 is blocked by the caller. */
543 /* F is the frame the menu is for.
544 X and Y are the frame-relative specified position,
545 relative to the inside upper left corner of the frame F.
546 Bitfield MENUFLAGS bits are:
547 MENU_FOR_CLICK is set if this menu was invoked for a mouse click.
548 MENU_KEYMAPS is set if this menu was specified with keymaps;
549 in that case, we return a list containing the chosen item's value
550 and perhaps also the pane's prefix.
551 TITLE is the specified menu title.
552 ERROR is a place to store an error message string in case of failure.
553 (We return nil on failure, but the value doesn't actually matter.) */
555 Lisp_Object
556 w32_menu_show (struct frame *f, int x, int y, int menuflags,
557 Lisp_Object title, const char **error)
559 int i;
560 int menu_item_selection;
561 HMENU menu;
562 POINT pos;
563 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
564 widget_value **submenu_stack
565 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
566 Lisp_Object *subprefix_stack
567 = (Lisp_Object *) alloca (menu_items_used * word_size);
568 int submenu_depth = 0;
569 bool first_pane;
571 *error = NULL;
573 if (menu_items_n_panes == 0)
574 return Qnil;
576 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
578 *error = "Empty menu";
579 return Qnil;
582 block_input ();
584 /* Create a tree of widget_value objects
585 representing the panes and their items. */
586 wv = make_widget_value ("menu", NULL, true, Qnil);
587 wv->button_type = BUTTON_TYPE_NONE;
588 first_wv = wv;
589 first_pane = true;
591 /* Loop over all panes and items, filling in the tree. */
592 i = 0;
593 while (i < menu_items_used)
595 if (EQ (AREF (menu_items, i), Qnil))
597 submenu_stack[submenu_depth++] = save_wv;
598 save_wv = prev_wv;
599 prev_wv = 0;
600 first_pane = false;
601 i++;
603 else if (EQ (AREF (menu_items, i), Qlambda))
605 prev_wv = save_wv;
606 save_wv = submenu_stack[--submenu_depth];
607 first_pane = false;
608 i++;
610 else if (EQ (AREF (menu_items, i), Qt)
611 && submenu_depth != 0)
612 i += MENU_ITEMS_PANE_LENGTH;
613 /* Ignore a nil in the item list.
614 It's meaningful only for dialog boxes. */
615 else if (EQ (AREF (menu_items, i), Qquote))
616 i += 1;
617 else if (EQ (AREF (menu_items, i), Qt))
619 /* Create a new pane. */
620 Lisp_Object pane_name, prefix;
621 const char *pane_string;
622 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
623 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
625 if (STRINGP (pane_name))
627 if (unicode_append_menu)
628 pane_name = ENCODE_UTF_8 (pane_name);
629 else if (STRING_MULTIBYTE (pane_name))
630 pane_name = ENCODE_SYSTEM (pane_name);
632 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
635 pane_string = (NILP (pane_name)
636 ? "" : SSDATA (pane_name));
637 /* If there is just one top-level pane, put all its items directly
638 under the top-level menu. */
639 if (menu_items_n_panes == 1)
640 pane_string = "";
642 /* If the pane has a meaningful name,
643 make the pane a top-level menu item
644 with its items as a submenu beneath it. */
645 if (!(menuflags & MENU_KEYMAPS) && strcmp (pane_string, ""))
647 wv = make_widget_value (pane_string, NULL, true, Qnil);
648 if (save_wv)
649 save_wv->next = wv;
650 else
651 first_wv->contents = wv;
652 if ((menuflags & MENU_KEYMAPS) && !NILP (prefix))
653 wv->name++;
654 wv->button_type = BUTTON_TYPE_NONE;
655 save_wv = wv;
656 prev_wv = 0;
658 else if (first_pane)
660 save_wv = wv;
661 prev_wv = 0;
663 first_pane = false;
664 i += MENU_ITEMS_PANE_LENGTH;
666 else
668 /* Create a new item within current pane. */
669 Lisp_Object item_name, enable, descrip, def, type, selected, help;
671 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
672 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
673 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
674 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
675 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
676 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
677 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
679 if (STRINGP (item_name))
681 if (unicode_append_menu)
682 item_name = ENCODE_UTF_8 (item_name);
683 else if (STRING_MULTIBYTE (item_name))
684 item_name = ENCODE_SYSTEM (item_name);
686 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
689 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
691 descrip = ENCODE_SYSTEM (descrip);
692 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
695 wv = make_widget_value (SSDATA (item_name), NULL, !NILP (enable),
696 STRINGP (help) ? help : Qnil);
697 if (prev_wv)
698 prev_wv->next = wv;
699 else
700 save_wv->contents = wv;
701 if (!NILP (descrip))
702 wv->key = SSDATA (descrip);
703 /* Use the contents index as call_data, since we are
704 restricted to 16-bits. */
705 wv->call_data = !NILP (def) ? (void *) (UINT_PTR) i : 0;
707 if (NILP (type))
708 wv->button_type = BUTTON_TYPE_NONE;
709 else if (EQ (type, QCtoggle))
710 wv->button_type = BUTTON_TYPE_TOGGLE;
711 else if (EQ (type, QCradio))
712 wv->button_type = BUTTON_TYPE_RADIO;
713 else
714 emacs_abort ();
716 wv->selected = !NILP (selected);
718 prev_wv = wv;
720 i += MENU_ITEMS_ITEM_LENGTH;
724 /* Deal with the title, if it is non-nil. */
725 if (!NILP (title))
727 widget_value *wv_title;
728 widget_value *wv_sep = make_widget_value ("--", NULL, false, Qnil);
730 /* Maybe replace this separator with a bitmap or owner-draw item
731 so that it looks better. Having two separators looks odd. */
732 wv_sep->next = first_wv->contents;
734 if (unicode_append_menu)
735 title = ENCODE_UTF_8 (title);
736 else if (STRING_MULTIBYTE (title))
737 title = ENCODE_SYSTEM (title);
739 wv_title = make_widget_value (SSDATA (title), NULL, true, Qnil);
740 wv_title->title = TRUE;
741 wv_title->button_type = BUTTON_TYPE_NONE;
742 wv_title->next = wv_sep;
743 first_wv->contents = wv_title;
746 /* No selection has been chosen yet. */
747 menu_item_selection = 0;
749 /* Actually create the menu. */
750 current_popup_menu = menu = CreatePopupMenu ();
751 fill_in_menu (menu, first_wv->contents);
753 /* Adjust coordinates to be root-window-relative. */
754 pos.x = x;
755 pos.y = y;
756 ClientToScreen (FRAME_W32_WINDOW (f), &pos);
758 /* Display the menu. */
759 menu_item_selection = SendMessage (FRAME_W32_WINDOW (f),
760 WM_EMACS_TRACKPOPUPMENU,
761 (WPARAM)menu, (LPARAM)&pos);
763 /* Clean up extraneous mouse events which might have been generated
764 during the call. */
765 discard_mouse_events ();
766 FRAME_DISPLAY_INFO (f)->grabbed = 0;
768 /* Free the widget_value objects we used to specify the contents. */
769 free_menubar_widget_value_tree (first_wv);
771 DestroyMenu (menu);
773 /* Free the owner-drawn and help-echo menu strings. */
774 w32_free_menu_strings (FRAME_W32_WINDOW (f));
775 f->output_data.w32->menubar_active = 0;
777 /* Find the selected item, and its pane, to return
778 the proper value. */
779 if (menu_item_selection != 0)
781 Lisp_Object prefix, entry;
783 prefix = entry = Qnil;
784 i = 0;
785 while (i < menu_items_used)
787 if (EQ (AREF (menu_items, i), Qnil))
789 subprefix_stack[submenu_depth++] = prefix;
790 prefix = entry;
791 i++;
793 else if (EQ (AREF (menu_items, i), Qlambda))
795 prefix = subprefix_stack[--submenu_depth];
796 i++;
798 else if (EQ (AREF (menu_items, i), Qt))
800 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
801 i += MENU_ITEMS_PANE_LENGTH;
803 /* Ignore a nil in the item list.
804 It's meaningful only for dialog boxes. */
805 else if (EQ (AREF (menu_items, i), Qquote))
806 i += 1;
807 else
809 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
810 if (menu_item_selection == i)
812 if (menuflags & MENU_KEYMAPS)
814 int j;
816 entry = Fcons (entry, Qnil);
817 if (!NILP (prefix))
818 entry = Fcons (prefix, entry);
819 for (j = submenu_depth - 1; j >= 0; j--)
820 if (!NILP (subprefix_stack[j]))
821 entry = Fcons (subprefix_stack[j], entry);
823 unblock_input ();
824 return entry;
826 i += MENU_ITEMS_ITEM_LENGTH;
830 else if (!(menuflags & MENU_FOR_CLICK))
832 unblock_input ();
833 /* Make "Cancel" equivalent to C-g. */
834 Fsignal (Qquit, Qnil);
837 unblock_input ();
838 return Qnil;
842 #ifdef HAVE_DIALOGS
843 /* TODO: On Windows, there are two ways of defining a dialog.
845 1. Create a predefined dialog resource and include it in nt/emacs.rc.
846 Using this method, we could then set the titles and make unneeded
847 buttons invisible before displaying the dialog. Everything would
848 be a fixed size though, so there is a risk that text does not
849 fit on a button.
850 2. Create the dialog template in memory on the fly. This allows us
851 to size the dialog and buttons dynamically, probably giving more
852 natural looking results for dialogs with few buttons, and eliminating
853 the problem of text overflowing the buttons. But the API for this is
854 quite complex - structures have to be allocated in particular ways,
855 text content is tacked onto the end of structures in variable length
856 arrays with further structures tacked on after these, there are
857 certain alignment requirements for all this, and we have to
858 measure all the text and convert to "dialog coordinates" to figure
859 out how big to make everything.
861 For now, we'll just stick with menus for dialogs that are more
862 complicated than simple yes/no type questions for which we can use
863 the MessageBox function.
866 static char * button_names [] = {
867 "button1", "button2", "button3", "button4", "button5",
868 "button6", "button7", "button8", "button9", "button10" };
870 static Lisp_Object
871 w32_dialog_show (struct frame *f, Lisp_Object title,
872 Lisp_Object header, char **error)
874 int i, nb_buttons = 0;
875 char dialog_name[6];
876 int menu_item_selection;
878 widget_value *wv, *first_wv = 0, *prev_wv = 0;
880 /* Number of elements seen so far, before boundary. */
881 int left_count = 0;
882 /* true means we've seen the boundary between left-hand elts and
883 right-hand. */
884 bool boundary_seen = false;
886 *error = NULL;
888 if (menu_items_n_panes > 1)
890 *error = "Multiple panes in dialog box";
891 return Qnil;
894 /* Create a tree of widget_value objects
895 representing the text label and buttons. */
897 Lisp_Object pane_name;
898 char *pane_string;
899 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
900 pane_string = (NILP (pane_name)
901 ? "" : SSDATA (pane_name));
902 prev_wv = make_widget_value ("message", pane_string, true, Qnil);
903 first_wv = prev_wv;
905 /* Loop over all panes and items, filling in the tree. */
906 i = MENU_ITEMS_PANE_LENGTH;
907 while (i < menu_items_used)
910 /* Create a new item within current pane. */
911 Lisp_Object item_name, enable, descrip, help;
913 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
914 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
915 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
916 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
918 if (NILP (item_name))
920 free_menubar_widget_value_tree (first_wv);
921 *error = "Submenu in dialog items";
922 return Qnil;
924 if (EQ (item_name, Qquote))
926 /* This is the boundary between left-side elts
927 and right-side elts. Stop incrementing right_count. */
928 boundary_seen = true;
929 i++;
930 continue;
932 if (nb_buttons >= 9)
934 free_menubar_widget_value_tree (first_wv);
935 *error = "Too many dialog items";
936 return Qnil;
939 wv = make_widget_value (button_names[nb_buttons],
940 SSDATA (item_name),
941 !NILP (enable), Qnil);
942 prev_wv->next = wv;
943 if (!NILP (descrip))
944 wv->key = SSDATA (descrip);
945 wv->call_data = aref_addr (menu_items, i);
946 prev_wv = wv;
948 if (! boundary_seen)
949 left_count++;
951 nb_buttons++;
952 i += MENU_ITEMS_ITEM_LENGTH;
955 /* If the boundary was not specified,
956 by default put half on the left and half on the right. */
957 if (! boundary_seen)
958 left_count = nb_buttons - nb_buttons / 2;
960 wv = make_widget_value (dialog_name, NULL, false, Qnil);
962 /* Frame title: 'Q' = Question, 'I' = Information.
963 Can also have 'E' = Error if, one day, we want
964 a popup for errors. */
965 if (NILP (header))
966 dialog_name[0] = 'Q';
967 else
968 dialog_name[0] = 'I';
970 /* Dialog boxes use a really stupid name encoding
971 which specifies how many buttons to use
972 and how many buttons are on the right. */
973 dialog_name[1] = '0' + nb_buttons;
974 dialog_name[2] = 'B';
975 dialog_name[3] = 'R';
976 /* Number of buttons to put on the right. */
977 dialog_name[4] = '0' + nb_buttons - left_count;
978 dialog_name[5] = 0;
979 wv->contents = first_wv;
980 first_wv = wv;
983 /* Actually create the dialog. */
984 dialog_id = widget_id_tick++;
985 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
986 f->output_data.w32->widget, true, 0,
987 dialog_selection_callback, 0);
988 lw_modify_all_widgets (dialog_id, first_wv->contents, TRUE);
990 /* Free the widget_value objects we used to specify the contents. */
991 free_menubar_widget_value_tree (first_wv);
993 /* No selection has been chosen yet. */
994 menu_item_selection = 0;
996 /* Display the menu. */
997 lw_pop_up_all_widgets (dialog_id);
999 /* Process events that apply to the menu. */
1000 popup_get_selection ((XEvent *) 0, FRAME_DISPLAY_INFO (f), dialog_id);
1002 lw_destroy_all_widgets (dialog_id);
1004 /* Find the selected item, and its pane, to return
1005 the proper value. */
1006 if (menu_item_selection != 0)
1008 i = 0;
1009 while (i < menu_items_used)
1011 Lisp_Object entry;
1013 if (EQ (AREF (menu_items, i), Qt))
1014 i += MENU_ITEMS_PANE_LENGTH;
1015 else
1017 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
1018 if (menu_item_selection == i)
1019 return entry;
1020 i += MENU_ITEMS_ITEM_LENGTH;
1024 else
1025 /* Make "Cancel" equivalent to C-g. */
1026 Fsignal (Qquit, Qnil);
1028 return Qnil;
1030 #else /* !HAVE_DIALOGS */
1032 /* Currently we only handle Yes No dialogs (y-or-n-p and yes-or-no-p) as
1033 simple dialogs. We could handle a few more, but I'm not aware of
1034 anywhere in Emacs that uses the other specific dialog choices that
1035 MessageBox provides. */
1037 static bool
1038 is_simple_dialog (Lisp_Object contents)
1040 Lisp_Object options;
1041 Lisp_Object name, yes, no, other;
1043 if (!CONSP (contents))
1044 return false;
1045 options = XCDR (contents);
1047 yes = build_string ("Yes");
1048 no = build_string ("No");
1050 if (!CONSP (options))
1051 return false;
1053 name = XCAR (options);
1054 if (!CONSP (name))
1055 return false;
1056 name = XCAR (name);
1058 if (!NILP (Fstring_equal (name, yes)))
1059 other = no;
1060 else if (!NILP (Fstring_equal (name, no)))
1061 other = yes;
1062 else
1063 return false;
1065 options = XCDR (options);
1066 if (!CONSP (options))
1067 return false;
1069 name = XCAR (options);
1070 if (!CONSP (name))
1071 return false;
1072 name = XCAR (name);
1073 if (NILP (Fstring_equal (name, other)))
1074 return false;
1076 /* Check there are no more options. */
1077 options = XCDR (options);
1078 return !(CONSP (options));
1081 static Lisp_Object
1082 simple_dialog_show (struct frame *f, Lisp_Object contents, Lisp_Object header)
1084 int answer;
1085 UINT type;
1086 Lisp_Object lispy_answer = Qnil, temp = XCAR (contents);
1088 type = MB_YESNO;
1090 /* Since we only handle Yes/No dialogs, and we already checked
1091 is_simple_dialog, we don't need to worry about checking contents
1092 to see what type of dialog to use. */
1094 /* Use Unicode if possible, so any language can be displayed. */
1095 if (unicode_message_box)
1097 WCHAR *text;
1098 const WCHAR *title;
1099 USE_SAFE_ALLOCA;
1101 if (STRINGP (temp))
1103 char *utf8_text = SSDATA (ENCODE_UTF_8 (temp));
1104 /* Be pessimistic about the number of characters needed.
1105 Remember characters outside the BMP will take more than
1106 one utf16 word, so we cannot simply use the character
1107 length of temp. */
1108 int utf8_len = strlen (utf8_text);
1109 text = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR));
1110 utf8to16 ((unsigned char *)utf8_text, utf8_len, text);
1112 else
1114 text = L"";
1117 if (NILP (header))
1119 title = L"Question";
1120 type |= MB_ICONQUESTION;
1122 else
1124 title = L"Information";
1125 type |= MB_ICONINFORMATION;
1128 answer = unicode_message_box (FRAME_W32_WINDOW (f), text, title, type);
1129 SAFE_FREE ();
1131 else
1133 const char *text, *title;
1135 /* Fall back on ANSI message box, but at least use system
1136 encoding so questions representable by the system codepage
1137 are encoded properly. */
1138 if (STRINGP (temp))
1139 text = SSDATA (ENCODE_SYSTEM (temp));
1140 else
1141 text = "";
1143 if (NILP (header))
1145 title = "Question";
1146 type |= MB_ICONQUESTION;
1148 else
1150 title = "Information";
1151 type |= MB_ICONINFORMATION;
1154 answer = MessageBox (FRAME_W32_WINDOW (f), text, title, type);
1157 if (answer == IDYES)
1158 lispy_answer = build_string ("Yes");
1159 else if (answer == IDNO)
1160 lispy_answer = build_string ("No");
1161 else
1162 Fsignal (Qquit, Qnil);
1164 for (temp = XCDR (contents); CONSP (temp); temp = XCDR (temp))
1166 Lisp_Object item, name, value;
1167 item = XCAR (temp);
1168 if (CONSP (item))
1170 name = XCAR (item);
1171 value = XCDR (item);
1173 else
1175 name = item;
1176 value = Qnil;
1179 if (!NILP (Fstring_equal (name, lispy_answer)))
1181 return value;
1184 Fsignal (Qquit, Qnil);
1185 return Qnil;
1187 #endif /* !HAVE_DIALOGS */
1190 /* UTF8: 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx, 10xxxxxx, 10xxxxxx */
1191 static void
1192 utf8to16 (unsigned char * src, int len, WCHAR * dest)
1194 while (len > 0)
1196 if (*src < 0x80)
1198 *dest = (WCHAR) *src;
1199 dest++; src++; len--;
1201 /* Since we might get >3 byte sequences which we don't handle, ignore the extra parts. */
1202 else if (*src < 0xC0)
1204 src++; len--;
1206 /* 2 char UTF-8 sequence. */
1207 else if (*src < 0xE0)
1209 *dest = (WCHAR) (((*src & 0x1f) << 6)
1210 | (*(src + 1) & 0x3f));
1211 src += 2; len -= 2; dest++;
1213 else if (*src < 0xF0)
1215 *dest = (WCHAR) (((*src & 0x0f) << 12)
1216 | ((*(src + 1) & 0x3f) << 6)
1217 | (*(src + 2) & 0x3f));
1218 src += 3; len -= 3; dest++;
1220 else /* Not encodable. Insert Unicode Substitution char. */
1222 *dest = (WCHAR) 0xfffd;
1223 src++; len--; dest++;
1226 *dest = 0;
1229 static int
1230 add_menu_item (HMENU menu, widget_value *wv, HMENU item)
1232 UINT fuFlags;
1233 char *out_string, *p, *q;
1234 int return_value;
1235 size_t nlen, orig_len;
1236 USE_SAFE_ALLOCA;
1238 if (menu_separator_name_p (wv->name))
1240 fuFlags = MF_SEPARATOR;
1241 out_string = NULL;
1243 else
1245 if (wv->enabled)
1246 fuFlags = MF_STRING;
1247 else
1248 fuFlags = MF_STRING | MF_GRAYED;
1250 if (wv->key != NULL)
1252 out_string = SAFE_ALLOCA (strlen (wv->name) + strlen (wv->key) + 2);
1253 p = stpcpy (out_string, wv->name);
1254 p = stpcpy (p, "\t");
1255 strcpy (p, wv->key);
1257 else
1258 out_string = (char *)wv->name;
1260 /* Quote any special characters within the menu item's text and
1261 key binding. */
1262 nlen = orig_len = strlen (out_string);
1263 if (unicode_append_menu)
1265 /* With UTF-8, & cannot be part of a multibyte character. */
1266 for (p = out_string; *p; p++)
1268 if (*p == '&')
1269 nlen++;
1272 #ifndef NTGUI_UNICODE
1273 else
1275 /* If encoded with the system codepage, use multibyte string
1276 functions in case of multibyte characters that contain '&'. */
1277 for (p = out_string; *p; p = _mbsinc (p))
1279 if (_mbsnextc (p) == '&')
1280 nlen++;
1283 #endif /* !NTGUI_UNICODE */
1285 if (nlen > orig_len)
1287 p = out_string;
1288 out_string = SAFE_ALLOCA (nlen + 1);
1289 q = out_string;
1290 while (*p)
1292 if (unicode_append_menu)
1294 if (*p == '&')
1295 *q++ = *p;
1296 *q++ = *p++;
1298 #ifndef NTGUI_UNICODE
1299 else
1301 if (_mbsnextc (p) == '&')
1303 _mbsncpy (q, p, 1);
1304 q = _mbsinc (q);
1306 _mbsncpy (q, p, 1);
1307 p = _mbsinc (p);
1308 q = _mbsinc (q);
1310 #endif /* !NTGUI_UNICODE */
1312 *q = '\0';
1315 if (item != NULL)
1316 fuFlags = MF_POPUP;
1317 else if (wv->title || wv->call_data == 0)
1319 /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since
1320 we can't deallocate the memory otherwise. */
1321 if (get_menu_item_info)
1323 out_string = (char *) local_alloc (strlen (wv->name) + 1);
1324 strcpy (out_string, wv->name);
1325 #ifdef MENU_DEBUG
1326 DebPrint ("Menu: allocating %ld for owner-draw", out_string);
1327 #endif
1328 fuFlags = MF_OWNERDRAW | MF_DISABLED;
1330 else
1331 fuFlags = MF_DISABLED;
1334 /* Draw radio buttons and tickboxes. */
1335 else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
1336 wv->button_type == BUTTON_TYPE_RADIO))
1337 fuFlags |= MF_CHECKED;
1338 else
1339 fuFlags |= MF_UNCHECKED;
1342 if (unicode_append_menu && out_string)
1344 /* Convert out_string from UTF-8 to UTF-16-LE. */
1345 int utf8_len = strlen (out_string);
1346 WCHAR * utf16_string;
1347 if (fuFlags & MF_OWNERDRAW)
1348 utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR));
1349 else
1350 utf16_string = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR));
1352 utf8to16 ((unsigned char *)out_string, utf8_len, utf16_string);
1353 return_value = unicode_append_menu (menu, fuFlags,
1354 item != NULL ? (UINT_PTR) item
1355 : (UINT_PTR) wv->call_data,
1356 utf16_string);
1358 #ifndef NTGUI_UNICODE /* Fallback does not apply when always UNICODE */
1359 if (!return_value)
1361 /* On W9x/ME, Unicode menus are not supported, though AppendMenuW
1362 apparently does exist at least in some cases and appears to be
1363 stubbed out to do nothing. out_string is UTF-8, but since
1364 our standard menus are in English and this is only going to
1365 happen the first time a menu is used, the encoding is
1366 of minor importance compared with menus not working at all. */
1367 return_value =
1368 AppendMenu (menu, fuFlags,
1369 item != NULL ? (UINT_PTR) item: (UINT_PTR) wv->call_data,
1370 out_string);
1371 /* Don't use Unicode menus in future, unless this is Windows
1372 NT or later, where a failure of AppendMenuW does NOT mean
1373 Unicode menus are unsupported. */
1374 if (osinfo_cache.dwPlatformId != VER_PLATFORM_WIN32_NT)
1375 unicode_append_menu = NULL;
1377 #endif /* NTGUI_UNICODE */
1379 if (unicode_append_menu && (fuFlags & MF_OWNERDRAW))
1380 local_free (out_string);
1382 else
1384 return_value =
1385 AppendMenu (menu,
1386 fuFlags,
1387 item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data,
1388 out_string );
1391 /* This must be done after the menu item is created. */
1392 if (!wv->title && wv->call_data != 0)
1394 if (set_menu_item_info)
1396 MENUITEMINFO info;
1397 memset (&info, 0, sizeof (info));
1398 info.cbSize = sizeof (info);
1399 info.fMask = MIIM_DATA;
1401 /* Set help string for menu item. Leave it as a pointer to
1402 a Lisp_String until it is ready to be displayed, since GC
1403 can happen while menus are active. */
1404 if (!NILP (wv->help))
1406 /* We use XUNTAG below because in a 32-bit build
1407 --with-wide-int we cannot pass a Lisp_Object
1408 via a DWORD member of MENUITEMINFO. */
1409 /* As of Jul-2012, w32api headers say that dwItemData
1410 has DWORD type, but that's a bug: it should actually
1411 be ULONG_PTR, which is correct for 32-bit and 64-bit
1412 Windows alike. MSVC headers get it right; hopefully,
1413 MinGW headers will, too. */
1414 eassert (STRINGP (wv->help));
1415 info.dwItemData = (ULONG_PTR) XUNTAG (wv->help, Lisp_String);
1417 if (wv->button_type == BUTTON_TYPE_RADIO)
1419 /* CheckMenuRadioItem allows us to differentiate TOGGLE and
1420 RADIO items, but is not available on NT 3.51 and earlier. */
1421 info.fMask |= MIIM_TYPE | MIIM_STATE;
1422 info.fType = MFT_RADIOCHECK | MFT_STRING;
1423 info.dwTypeData = out_string;
1424 info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED;
1427 set_menu_item_info (menu,
1428 item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data,
1429 FALSE, &info);
1432 SAFE_FREE ();
1433 return return_value;
1436 /* Construct native Windows menu(bar) based on widget_value tree. */
1437 static int
1438 fill_in_menu (HMENU menu, widget_value *wv)
1440 for ( ; wv != NULL; wv = wv->next)
1442 if (wv->contents)
1444 HMENU sub_menu = CreatePopupMenu ();
1446 if (sub_menu == NULL)
1447 return 0;
1449 if (!fill_in_menu (sub_menu, wv->contents) ||
1450 !add_menu_item (menu, wv, sub_menu))
1452 DestroyMenu (sub_menu);
1453 return 0;
1456 else
1458 if (!add_menu_item (menu, wv, NULL))
1459 return 0;
1462 return 1;
1465 /* Display help string for currently pointed to menu item. Not
1466 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
1467 available. */
1468 void
1469 w32_menu_display_help (HWND owner, HMENU menu, UINT item, UINT flags)
1471 if (get_menu_item_info)
1473 struct frame *f = x_window_to_frame (&one_w32_display_info, owner);
1474 Lisp_Object frame, help;
1476 /* No help echo on owner-draw menu items, or when the keyboard
1477 is used to navigate the menus, since tooltips are distracting
1478 if they pop up elsewhere. */
1479 if ((flags & MF_OWNERDRAW) || (flags & MF_POPUP)
1480 || !(flags & MF_MOUSESELECT)
1481 /* Ignore any dwItemData for menu items whose flags don't
1482 have the MF_HILITE bit set. These are dwItemData that
1483 Windows sends our way, but they aren't pointers to our
1484 Lisp_String objects, so trying to create Lisp_Strings out
1485 of them below and pass that to the keyboard queue will
1486 crash Emacs when we try to display those "strings". It
1487 is unclear why we get these dwItemData, or what they are:
1488 sometimes they point to a wchar_t string that is the menu
1489 title, sometimes to someting that doesn't look like text
1490 at all. (The problematic data also comes with the 0x0800
1491 bit set, but this bit is not documented, so we don't want
1492 to depend on it.) */
1493 || !(flags & MF_HILITE))
1494 help = Qnil;
1495 else
1497 MENUITEMINFO info;
1499 memset (&info, 0, sizeof (info));
1500 info.cbSize = sizeof (info);
1501 info.fMask = MIIM_DATA;
1502 get_menu_item_info (menu, item, FALSE, &info);
1504 help =
1505 info.dwItemData
1506 ? make_lisp_ptr ((void *) info.dwItemData, Lisp_String)
1507 : Qnil;
1510 /* Store the help echo in the keyboard buffer as the X toolkit
1511 version does, rather than directly showing it. This seems to
1512 solve the GC problems that were present when we based the
1513 Windows code on the non-toolkit version. */
1514 if (f)
1516 XSETFRAME (frame, f);
1517 kbd_buffer_store_help_event (frame, help);
1519 else
1520 /* X version has a loop through frames here, which doesn't
1521 appear to do anything, unless it has some side effect. */
1522 show_help_echo (help, Qnil, Qnil, Qnil);
1526 /* Free memory used by owner-drawn strings. */
1527 static void
1528 w32_free_submenu_strings (HMENU menu)
1530 int i, num = GetMenuItemCount (menu);
1531 for (i = 0; i < num; i++)
1533 MENUITEMINFO info;
1534 memset (&info, 0, sizeof (info));
1535 info.cbSize = sizeof (info);
1536 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_SUBMENU;
1538 get_menu_item_info (menu, i, TRUE, &info);
1540 /* Owner-drawn names are held in dwItemData. */
1541 if ((info.fType & MF_OWNERDRAW) && info.dwItemData)
1543 #ifdef MENU_DEBUG
1544 DebPrint ("Menu: freeing %ld for owner-draw", info.dwItemData);
1545 #endif
1546 local_free (info.dwItemData);
1549 /* Recurse down submenus. */
1550 if (info.hSubMenu)
1551 w32_free_submenu_strings (info.hSubMenu);
1555 void
1556 w32_free_menu_strings (HWND hwnd)
1558 HMENU menu = current_popup_menu;
1560 if (get_menu_item_info)
1562 /* If there is no popup menu active, free the strings from the frame's
1563 menubar. */
1564 if (!menu)
1565 menu = GetMenu (hwnd);
1567 if (menu)
1568 w32_free_submenu_strings (menu);
1571 current_popup_menu = NULL;
1574 /* The following is used by delayed window autoselection. */
1576 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
1577 doc: /* Return t if a menu or popup dialog is active on selected frame. */)
1578 (void)
1580 struct frame *f;
1581 f = SELECTED_FRAME ();
1582 return (f->output_data.w32->menubar_active > 0) ? Qt : Qnil;
1585 void
1586 syms_of_w32menu (void)
1588 globals_of_w32menu ();
1590 current_popup_menu = NULL;
1592 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
1593 DEFSYM (Qunsupported__w32_dialog, "unsupported--w32-dialog");
1595 defsubr (&Smenu_or_popup_active_p);
1599 globals_of_w32menu is used to initialize those global variables that
1600 must always be initialized on startup even when the global variable
1601 initialized is non zero (see the function main in emacs.c).
1602 globals_of_w32menu is called from syms_of_w32menu when the global
1603 variable initialized is 0 and directly from main when initialized
1604 is non zero.
1606 void
1607 globals_of_w32menu (void)
1609 #ifndef NTGUI_UNICODE
1610 /* See if Get/SetMenuItemInfo functions are available. */
1611 HMODULE user32 = GetModuleHandle ("user32.dll");
1612 get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA");
1613 set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA");
1614 unicode_append_menu = (AppendMenuW_Proc) GetProcAddress (user32, "AppendMenuW");
1615 unicode_message_box = (MessageBoxW_Proc) GetProcAddress (user32, "MessageBoxW");
1616 #endif /* !NTGUI_UNICODE */