In the Package Menu, indicate packages that are newly-available.
[emacs.git] / src / w32menu.c
blob2bc4c208956613027c91adbb060555cabb960699
1 /* Menu support for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1986, 1988, 1993-1994, 1996, 1998-1999, 2001-2012
3 Free 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 <mbstring.h>
25 #include <setjmp.h>
27 #include "lisp.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "frame.h"
31 #include "termhooks.h"
32 #include "window.h"
33 #include "blockinput.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 /* Load sys/types.h if not already loaded.
44 In some systems loading it twice is suicidal. */
45 #ifndef makedev
46 #include <sys/types.h>
47 #endif
49 #include "dispextern.h"
51 #include "w32heap.h" /* for osinfo_cache */
53 #undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
55 #ifndef TRUE
56 #define TRUE 1
57 #define FALSE 0
58 #endif /* no TRUE */
60 HMENU current_popup_menu;
62 void syms_of_w32menu (void);
63 void globals_of_w32menu (void);
65 typedef BOOL (WINAPI * GetMenuItemInfoA_Proc) (
66 IN HMENU,
67 IN UINT,
68 IN BOOL,
69 IN OUT LPMENUITEMINFOA);
70 typedef BOOL (WINAPI * SetMenuItemInfoA_Proc) (
71 IN HMENU,
72 IN UINT,
73 IN BOOL,
74 IN LPCMENUITEMINFOA);
75 typedef int (WINAPI * MessageBoxW_Proc) (
76 IN HWND window,
77 IN WCHAR *text,
78 IN WCHAR *caption,
79 IN UINT type);
81 GetMenuItemInfoA_Proc get_menu_item_info = NULL;
82 SetMenuItemInfoA_Proc set_menu_item_info = NULL;
83 AppendMenuW_Proc unicode_append_menu = NULL;
84 MessageBoxW_Proc unicode_message_box = NULL;
86 Lisp_Object Qdebug_on_next_call;
88 void set_frame_menubar (FRAME_PTR, int, int);
90 #ifdef HAVE_DIALOGS
91 static Lisp_Object w32_dialog_show (FRAME_PTR, int, Lisp_Object, char**);
92 #else
93 static int is_simple_dialog (Lisp_Object);
94 static Lisp_Object simple_dialog_show (FRAME_PTR, Lisp_Object, Lisp_Object);
95 #endif
97 static void utf8to16 (unsigned char *, int, WCHAR *);
98 static int fill_in_menu (HMENU, widget_value *);
100 void w32_free_menu_strings (HWND);
103 /* This is set nonzero after the user activates the menu bar, and set
104 to zero again after the menu bars are redisplayed by prepare_menu_bar.
105 While it is nonzero, all calls to set_frame_menubar go deep.
107 I don't understand why this is needed, but it does seem to be
108 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
110 int pending_menu_activation;
112 #ifdef HAVE_MENUS
114 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
115 doc: /* Pop up a dialog box and return user's selection.
116 POSITION specifies which frame to use.
117 This is normally a mouse button event or a window or frame.
118 If POSITION is t, it means to use the frame the mouse is on.
119 The dialog box appears in the middle of the specified frame.
121 CONTENTS specifies the alternatives to display in the dialog box.
122 It is a list of the form (TITLE ITEM1 ITEM2...).
123 Each ITEM is a cons cell (STRING . VALUE).
124 The return value is VALUE from the chosen item.
126 An ITEM may also be just a string--that makes a nonselectable item.
127 An ITEM may also be nil--that means to put all preceding items
128 on the left of the dialog box and all following items on the right.
129 \(By default, approximately half appear on each side.)
131 If HEADER is non-nil, the frame title for the box is "Information",
132 otherwise it is "Question". */)
133 (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
135 FRAME_PTR f = NULL;
136 Lisp_Object window;
138 check_w32 ();
140 /* Decode the first argument: find the window or frame to use. */
141 if (EQ (position, Qt)
142 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
143 || EQ (XCAR (position), Qtool_bar))))
145 #if 0 /* Using the frame the mouse is on may not be right. */
146 /* Use the mouse's current position. */
147 FRAME_PTR new_f = SELECTED_FRAME ();
148 Lisp_Object bar_window;
149 enum scroll_bar_part part;
150 Time time;
151 Lisp_Object x, y;
153 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
155 if (new_f != 0)
156 XSETFRAME (window, new_f);
157 else
158 window = selected_window;
159 #endif
160 window = selected_window;
162 else if (CONSP (position))
164 Lisp_Object tem;
165 tem = Fcar (position);
166 if (CONSP (tem))
167 window = Fcar (Fcdr (position));
168 else
170 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
171 window = Fcar (tem); /* POSN_WINDOW (tem) */
174 else if (WINDOWP (position) || FRAMEP (position))
175 window = position;
176 else
177 window = Qnil;
179 /* Decode where to put the menu. */
181 if (FRAMEP (window))
182 f = XFRAME (window);
183 else if (WINDOWP (window))
185 CHECK_LIVE_WINDOW (window);
186 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
188 else
189 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
190 but I don't want to make one now. */
191 CHECK_WINDOW (window);
193 #ifndef HAVE_DIALOGS
196 /* Handle simple Yes/No choices as MessageBox popups. */
197 if (is_simple_dialog (contents))
198 return simple_dialog_show (f, contents, header);
199 else
201 /* Display a menu with these alternatives
202 in the middle of frame F. */
203 Lisp_Object x, y, frame, newpos;
204 XSETFRAME (frame, f);
205 XSETINT (x, x_pixel_width (f) / 2);
206 XSETINT (y, x_pixel_height (f) / 2);
207 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
208 return Fx_popup_menu (newpos,
209 Fcons (Fcar (contents), Fcons (contents, Qnil)));
212 #else /* HAVE_DIALOGS */
214 Lisp_Object title;
215 char *error_name;
216 Lisp_Object selection;
218 /* Decode the dialog items from what was specified. */
219 title = Fcar (contents);
220 CHECK_STRING (title);
222 list_of_panes (Fcons (contents, Qnil));
224 /* Display them in a dialog box. */
225 BLOCK_INPUT;
226 selection = w32_dialog_show (f, 0, title, header, &error_name);
227 UNBLOCK_INPUT;
229 discard_menu_items ();
230 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
232 if (error_name) error (error_name);
233 return selection;
235 #endif /* HAVE_DIALOGS */
238 /* Activate the menu bar of frame F.
239 This is called from keyboard.c when it gets the
240 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
242 To activate the menu bar, we signal to the input thread that it can
243 return from the WM_INITMENU message, allowing the normal Windows
244 processing of the menus.
246 But first we recompute the menu bar contents (the whole tree).
248 This way we can safely execute Lisp code. */
250 void
251 x_activate_menubar (FRAME_PTR f)
253 set_frame_menubar (f, 0, 1);
255 /* Lock out further menubar changes while active. */
256 f->output_data.w32->menubar_active = 1;
258 /* Signal input thread to return from WM_INITMENU. */
259 complete_deferred_msg (FRAME_W32_WINDOW (f), WM_INITMENU, 0);
262 /* This callback is called from the menu bar pulldown menu
263 when the user makes a selection.
264 Figure out what the user chose
265 and put the appropriate events into the keyboard buffer. */
267 void
268 menubar_selection_callback (FRAME_PTR f, void * client_data)
270 Lisp_Object prefix, entry;
271 Lisp_Object vector;
272 Lisp_Object *subprefix_stack;
273 int submenu_depth = 0;
274 int i;
276 if (!f)
277 return;
278 entry = Qnil;
279 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
280 vector = f->menu_bar_vector;
281 prefix = Qnil;
282 i = 0;
283 while (i < f->menu_bar_items_used)
285 if (EQ (AREF (vector, i), Qnil))
287 subprefix_stack[submenu_depth++] = prefix;
288 prefix = entry;
289 i++;
291 else if (EQ (AREF (vector, i), Qlambda))
293 prefix = subprefix_stack[--submenu_depth];
294 i++;
296 else if (EQ (AREF (vector, i), Qt))
298 prefix = AREF (vector, i + MENU_ITEMS_PANE_PREFIX);
299 i += MENU_ITEMS_PANE_LENGTH;
301 else
303 entry = AREF (vector, i + MENU_ITEMS_ITEM_VALUE);
304 /* The EMACS_INT cast avoids a warning. There's no problem
305 as long as pointers have enough bits to hold small integers. */
306 if ((int) (EMACS_INT) client_data == i)
308 int j;
309 struct input_event buf;
310 Lisp_Object frame;
311 EVENT_INIT (buf);
313 XSETFRAME (frame, f);
314 buf.kind = MENU_BAR_EVENT;
315 buf.frame_or_window = frame;
316 buf.arg = frame;
317 kbd_buffer_store_event (&buf);
319 for (j = 0; j < submenu_depth; j++)
320 if (!NILP (subprefix_stack[j]))
322 buf.kind = MENU_BAR_EVENT;
323 buf.frame_or_window = frame;
324 buf.arg = subprefix_stack[j];
325 kbd_buffer_store_event (&buf);
328 if (!NILP (prefix))
330 buf.kind = MENU_BAR_EVENT;
331 buf.frame_or_window = frame;
332 buf.arg = prefix;
333 kbd_buffer_store_event (&buf);
336 buf.kind = MENU_BAR_EVENT;
337 buf.frame_or_window = frame;
338 buf.arg = entry;
339 /* Free memory used by owner-drawn and help-echo strings. */
340 w32_free_menu_strings (FRAME_W32_WINDOW (f));
341 kbd_buffer_store_event (&buf);
343 f->output_data.w32->menubar_active = 0;
344 return;
346 i += MENU_ITEMS_ITEM_LENGTH;
349 /* Free memory used by owner-drawn and help-echo strings. */
350 w32_free_menu_strings (FRAME_W32_WINDOW (f));
351 f->output_data.w32->menubar_active = 0;
355 /* Set the contents of the menubar widgets of frame F.
356 The argument FIRST_TIME is currently ignored;
357 it is set the first time this is called, from initialize_frame_menubar. */
359 void
360 set_frame_menubar (FRAME_PTR f, int first_time, int deep_p)
362 HMENU menubar_widget = f->output_data.w32->menubar_widget;
363 Lisp_Object items;
364 widget_value *wv, *first_wv, *prev_wv = 0;
365 int i, last_i;
366 int *submenu_start, *submenu_end;
367 int *submenu_top_level_items, *submenu_n_panes;
369 /* We must not change the menubar when actually in use. */
370 if (f->output_data.w32->menubar_active)
371 return;
373 XSETFRAME (Vmenu_updating_frame, f);
375 if (! menubar_widget)
376 deep_p = 1;
377 else if (pending_menu_activation && !deep_p)
378 deep_p = 1;
380 if (deep_p)
382 /* Make a widget-value tree representing the entire menu trees. */
384 struct buffer *prev = current_buffer;
385 Lisp_Object buffer;
386 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
387 int previous_menu_items_used = f->menu_bar_items_used;
388 Lisp_Object *previous_items
389 = (Lisp_Object *) alloca (previous_menu_items_used
390 * sizeof (Lisp_Object));
392 /* If we are making a new widget, its contents are empty,
393 do always reinitialize them. */
394 if (! menubar_widget)
395 previous_menu_items_used = 0;
397 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
398 specbind (Qinhibit_quit, Qt);
399 /* Don't let the debugger step into this code
400 because it is not reentrant. */
401 specbind (Qdebug_on_next_call, Qnil);
403 record_unwind_save_match_data ();
405 if (NILP (Voverriding_local_map_menu_flag))
407 specbind (Qoverriding_terminal_local_map, Qnil);
408 specbind (Qoverriding_local_map, Qnil);
411 set_buffer_internal_1 (XBUFFER (buffer));
413 /* Run the hooks. */
414 safe_run_hooks (Qactivate_menubar_hook);
415 safe_run_hooks (Qmenu_bar_update_hook);
416 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
418 items = FRAME_MENU_BAR_ITEMS (f);
420 /* Save the frame's previous menu bar contents data. */
421 if (previous_menu_items_used)
422 memcpy (previous_items, XVECTOR (f->menu_bar_vector)->contents,
423 previous_menu_items_used * sizeof (Lisp_Object));
425 /* Fill in menu_items with the current menu bar contents.
426 This can evaluate Lisp code. */
427 save_menu_items ();
429 menu_items = f->menu_bar_vector;
430 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
431 submenu_start = (int *) alloca (ASIZE (items) * sizeof (int));
432 submenu_end = (int *) alloca (ASIZE (items) * sizeof (int));
433 submenu_n_panes = (int *) alloca (ASIZE (items) * sizeof (int));
434 submenu_top_level_items = (int *) alloca (ASIZE (items) * sizeof (int));
435 init_menu_items ();
436 for (i = 0; i < ASIZE (items); i += 4)
438 Lisp_Object key, string, maps;
440 last_i = i;
442 key = AREF (items, i);
443 string = AREF (items, i + 1);
444 maps = AREF (items, i + 2);
445 if (NILP (string))
446 break;
448 submenu_start[i] = menu_items_used;
450 menu_items_n_panes = 0;
451 submenu_top_level_items[i]
452 = parse_single_submenu (key, string, maps);
453 submenu_n_panes[i] = menu_items_n_panes;
455 submenu_end[i] = menu_items_used;
458 finish_menu_items ();
460 /* Convert menu_items into widget_value trees
461 to display the menu. This cannot evaluate Lisp code. */
463 wv = xmalloc_widget_value ();
464 wv->name = "menubar";
465 wv->value = 0;
466 wv->enabled = 1;
467 wv->button_type = BUTTON_TYPE_NONE;
468 wv->help = Qnil;
469 first_wv = wv;
471 for (i = 0; i < last_i; i += 4)
473 menu_items_n_panes = submenu_n_panes[i];
474 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
475 submenu_top_level_items[i]);
476 if (prev_wv)
477 prev_wv->next = wv;
478 else
479 first_wv->contents = wv;
480 /* Don't set wv->name here; GC during the loop might relocate it. */
481 wv->enabled = 1;
482 wv->button_type = BUTTON_TYPE_NONE;
483 prev_wv = wv;
486 set_buffer_internal_1 (prev);
488 /* If there has been no change in the Lisp-level contents
489 of the menu bar, skip redisplaying it. Just exit. */
491 for (i = 0; i < previous_menu_items_used; i++)
492 if (menu_items_used == i
493 || (!EQ (previous_items[i], AREF (menu_items, i))))
494 break;
495 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
497 free_menubar_widget_value_tree (first_wv);
498 discard_menu_items ();
499 unbind_to (specpdl_count, Qnil);
500 return;
503 f->menu_bar_vector = menu_items;
504 f->menu_bar_items_used = menu_items_used;
506 /* This undoes save_menu_items. */
507 unbind_to (specpdl_count, Qnil);
509 /* Now GC cannot happen during the lifetime of the widget_value,
510 so it's safe to store data from a Lisp_String, as long as
511 local copies are made when the actual menu is created.
512 Windows takes care of this for normal string items, but
513 not for owner-drawn items or additional item-info. */
514 wv = first_wv->contents;
515 for (i = 0; i < ASIZE (items); i += 4)
517 Lisp_Object string;
518 string = AREF (items, i + 1);
519 if (NILP (string))
520 break;
521 wv->name = SSDATA (string);
522 update_submenu_strings (wv->contents);
523 wv = wv->next;
526 else
528 /* Make a widget-value tree containing
529 just the top level menu bar strings. */
531 wv = xmalloc_widget_value ();
532 wv->name = "menubar";
533 wv->value = 0;
534 wv->enabled = 1;
535 wv->button_type = BUTTON_TYPE_NONE;
536 wv->help = Qnil;
537 first_wv = wv;
539 items = FRAME_MENU_BAR_ITEMS (f);
540 for (i = 0; i < ASIZE (items); i += 4)
542 Lisp_Object string;
544 string = AREF (items, i + 1);
545 if (NILP (string))
546 break;
548 wv = xmalloc_widget_value ();
549 wv->name = SSDATA (string);
550 wv->value = 0;
551 wv->enabled = 1;
552 wv->button_type = BUTTON_TYPE_NONE;
553 wv->help = Qnil;
554 /* This prevents lwlib from assuming this
555 menu item is really supposed to be empty. */
556 /* The EMACS_INT cast avoids a warning.
557 This value just has to be different from small integers. */
558 wv->call_data = (void *) (EMACS_INT) (-1);
560 if (prev_wv)
561 prev_wv->next = wv;
562 else
563 first_wv->contents = wv;
564 prev_wv = wv;
567 /* Forget what we thought we knew about what is in the
568 detailed contents of the menu bar menus.
569 Changing the top level always destroys the contents. */
570 f->menu_bar_items_used = 0;
573 /* Create or update the menu bar widget. */
575 BLOCK_INPUT;
577 if (menubar_widget)
579 /* Empty current menubar, rather than creating a fresh one. */
580 while (DeleteMenu (menubar_widget, 0, MF_BYPOSITION))
583 else
585 menubar_widget = CreateMenu ();
587 fill_in_menu (menubar_widget, first_wv->contents);
589 free_menubar_widget_value_tree (first_wv);
592 HMENU old_widget = f->output_data.w32->menubar_widget;
594 f->output_data.w32->menubar_widget = menubar_widget;
595 SetMenu (FRAME_W32_WINDOW (f), f->output_data.w32->menubar_widget);
596 /* Causes flicker when menu bar is updated
597 DrawMenuBar (FRAME_W32_WINDOW (f)); */
599 /* Force the window size to be recomputed so that the frame's text
600 area remains the same, if menubar has just been created. */
601 if (old_widget == NULL)
602 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
605 UNBLOCK_INPUT;
608 /* Called from Fx_create_frame to create the initial menubar of a frame
609 before it is mapped, so that the window is mapped with the menubar already
610 there instead of us tacking it on later and thrashing the window after it
611 is visible. */
613 void
614 initialize_frame_menubar (FRAME_PTR f)
616 /* This function is called before the first chance to redisplay
617 the frame. It has to be, so the frame will have the right size. */
618 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
619 set_frame_menubar (f, 1, 1);
622 /* Get rid of the menu bar of frame F, and free its storage.
623 This is used when deleting a frame, and when turning off the menu bar. */
625 void
626 free_frame_menubar (FRAME_PTR f)
628 BLOCK_INPUT;
631 HMENU old = GetMenu (FRAME_W32_WINDOW (f));
632 SetMenu (FRAME_W32_WINDOW (f), NULL);
633 f->output_data.w32->menubar_widget = NULL;
634 DestroyMenu (old);
637 UNBLOCK_INPUT;
641 /* w32_menu_show actually displays a menu using the panes and items in
642 menu_items and returns the value selected from it; we assume input
643 is blocked by the caller. */
645 /* F is the frame the menu is for.
646 X and Y are the frame-relative specified position,
647 relative to the inside upper left corner of the frame F.
648 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
649 KEYMAPS is 1 if this menu was specified with keymaps;
650 in that case, we return a list containing the chosen item's value
651 and perhaps also the pane's prefix.
652 TITLE is the specified menu title.
653 ERROR is a place to store an error message string in case of failure.
654 (We return nil on failure, but the value doesn't actually matter.) */
656 Lisp_Object
657 w32_menu_show (FRAME_PTR f, int x, int y, int for_click, int keymaps,
658 Lisp_Object title, const char **error)
660 int i;
661 int menu_item_selection;
662 HMENU menu;
663 POINT pos;
664 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
665 widget_value **submenu_stack
666 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
667 Lisp_Object *subprefix_stack
668 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
669 int submenu_depth = 0;
670 int first_pane;
672 *error = NULL;
674 if (menu_items_n_panes == 0)
675 return Qnil;
677 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
679 *error = "Empty menu";
680 return Qnil;
683 /* Create a tree of widget_value objects
684 representing the panes and their items. */
685 wv = xmalloc_widget_value ();
686 wv->name = "menu";
687 wv->value = 0;
688 wv->enabled = 1;
689 wv->button_type = BUTTON_TYPE_NONE;
690 wv->help = Qnil;
691 first_wv = wv;
692 first_pane = 1;
694 /* Loop over all panes and items, filling in the tree. */
695 i = 0;
696 while (i < menu_items_used)
698 if (EQ (AREF (menu_items, i), Qnil))
700 submenu_stack[submenu_depth++] = save_wv;
701 save_wv = prev_wv;
702 prev_wv = 0;
703 first_pane = 1;
704 i++;
706 else if (EQ (AREF (menu_items, i), Qlambda))
708 prev_wv = save_wv;
709 save_wv = submenu_stack[--submenu_depth];
710 first_pane = 0;
711 i++;
713 else if (EQ (AREF (menu_items, i), Qt)
714 && submenu_depth != 0)
715 i += MENU_ITEMS_PANE_LENGTH;
716 /* Ignore a nil in the item list.
717 It's meaningful only for dialog boxes. */
718 else if (EQ (AREF (menu_items, i), Qquote))
719 i += 1;
720 else if (EQ (AREF (menu_items, i), Qt))
722 /* Create a new pane. */
723 Lisp_Object pane_name, prefix;
724 char *pane_string;
725 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
726 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
728 if (STRINGP (pane_name))
730 if (unicode_append_menu)
731 pane_name = ENCODE_UTF_8 (pane_name);
732 else if (STRING_MULTIBYTE (pane_name))
733 pane_name = ENCODE_SYSTEM (pane_name);
735 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
738 pane_string = (NILP (pane_name)
739 ? "" : SSDATA (pane_name));
740 /* If there is just one top-level pane, put all its items directly
741 under the top-level menu. */
742 if (menu_items_n_panes == 1)
743 pane_string = "";
745 /* If the pane has a meaningful name,
746 make the pane a top-level menu item
747 with its items as a submenu beneath it. */
748 if (!keymaps && strcmp (pane_string, ""))
750 wv = xmalloc_widget_value ();
751 if (save_wv)
752 save_wv->next = wv;
753 else
754 first_wv->contents = wv;
755 wv->name = pane_string;
756 if (keymaps && !NILP (prefix))
757 wv->name++;
758 wv->value = 0;
759 wv->enabled = 1;
760 wv->button_type = BUTTON_TYPE_NONE;
761 wv->help = Qnil;
762 save_wv = wv;
763 prev_wv = 0;
765 else if (first_pane)
767 save_wv = wv;
768 prev_wv = 0;
770 first_pane = 0;
771 i += MENU_ITEMS_PANE_LENGTH;
773 else
775 /* Create a new item within current pane. */
776 Lisp_Object item_name, enable, descrip, def, type, selected, help;
778 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
779 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
780 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
781 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
782 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
783 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
784 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
786 if (STRINGP (item_name))
788 if (unicode_append_menu)
789 item_name = ENCODE_UTF_8 (item_name);
790 else if (STRING_MULTIBYTE (item_name))
791 item_name = ENCODE_SYSTEM (item_name);
793 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
796 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
798 descrip = ENCODE_SYSTEM (descrip);
799 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
802 wv = xmalloc_widget_value ();
803 if (prev_wv)
804 prev_wv->next = wv;
805 else
806 save_wv->contents = wv;
807 wv->name = SSDATA (item_name);
808 if (!NILP (descrip))
809 wv->key = SSDATA (descrip);
810 wv->value = 0;
811 /* Use the contents index as call_data, since we are
812 restricted to 16-bits. */
813 wv->call_data = !NILP (def) ? (void *) (EMACS_INT) i : 0;
814 wv->enabled = !NILP (enable);
816 if (NILP (type))
817 wv->button_type = BUTTON_TYPE_NONE;
818 else if (EQ (type, QCtoggle))
819 wv->button_type = BUTTON_TYPE_TOGGLE;
820 else if (EQ (type, QCradio))
821 wv->button_type = BUTTON_TYPE_RADIO;
822 else
823 abort ();
825 wv->selected = !NILP (selected);
827 if (!STRINGP (help))
828 help = Qnil;
830 wv->help = help;
832 prev_wv = wv;
834 i += MENU_ITEMS_ITEM_LENGTH;
838 /* Deal with the title, if it is non-nil. */
839 if (!NILP (title))
841 widget_value *wv_title = xmalloc_widget_value ();
842 widget_value *wv_sep = xmalloc_widget_value ();
844 /* Maybe replace this separator with a bitmap or owner-draw item
845 so that it looks better. Having two separators looks odd. */
846 wv_sep->name = "--";
847 wv_sep->next = first_wv->contents;
848 wv_sep->help = Qnil;
850 if (unicode_append_menu)
851 title = ENCODE_UTF_8 (title);
852 else if (STRING_MULTIBYTE (title))
853 title = ENCODE_SYSTEM (title);
855 wv_title->name = SSDATA (title);
856 wv_title->enabled = TRUE;
857 wv_title->title = TRUE;
858 wv_title->button_type = BUTTON_TYPE_NONE;
859 wv_title->help = Qnil;
860 wv_title->next = wv_sep;
861 first_wv->contents = wv_title;
864 /* No selection has been chosen yet. */
865 menu_item_selection = 0;
867 /* Actually create the menu. */
868 current_popup_menu = menu = CreatePopupMenu ();
869 fill_in_menu (menu, first_wv->contents);
871 /* Adjust coordinates to be root-window-relative. */
872 pos.x = x;
873 pos.y = y;
874 ClientToScreen (FRAME_W32_WINDOW (f), &pos);
876 /* Display the menu. */
877 menu_item_selection = SendMessage (FRAME_W32_WINDOW (f),
878 WM_EMACS_TRACKPOPUPMENU,
879 (WPARAM)menu, (LPARAM)&pos);
881 /* Clean up extraneous mouse events which might have been generated
882 during the call. */
883 discard_mouse_events ();
884 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
886 /* Free the widget_value objects we used to specify the contents. */
887 free_menubar_widget_value_tree (first_wv);
889 DestroyMenu (menu);
891 /* Free the owner-drawn and help-echo menu strings. */
892 w32_free_menu_strings (FRAME_W32_WINDOW (f));
893 f->output_data.w32->menubar_active = 0;
895 /* Find the selected item, and its pane, to return
896 the proper value. */
897 if (menu_item_selection != 0)
899 Lisp_Object prefix, entry;
901 prefix = entry = Qnil;
902 i = 0;
903 while (i < menu_items_used)
905 if (EQ (AREF (menu_items, i), Qnil))
907 subprefix_stack[submenu_depth++] = prefix;
908 prefix = entry;
909 i++;
911 else if (EQ (AREF (menu_items, i), Qlambda))
913 prefix = subprefix_stack[--submenu_depth];
914 i++;
916 else if (EQ (AREF (menu_items, i), Qt))
918 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
919 i += MENU_ITEMS_PANE_LENGTH;
921 /* Ignore a nil in the item list.
922 It's meaningful only for dialog boxes. */
923 else if (EQ (AREF (menu_items, i), Qquote))
924 i += 1;
925 else
927 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
928 if (menu_item_selection == i)
930 if (keymaps != 0)
932 int j;
934 entry = Fcons (entry, Qnil);
935 if (!NILP (prefix))
936 entry = Fcons (prefix, entry);
937 for (j = submenu_depth - 1; j >= 0; j--)
938 if (!NILP (subprefix_stack[j]))
939 entry = Fcons (subprefix_stack[j], entry);
941 return entry;
943 i += MENU_ITEMS_ITEM_LENGTH;
947 else if (!for_click)
948 /* Make "Cancel" equivalent to C-g. */
949 Fsignal (Qquit, Qnil);
951 return Qnil;
955 #ifdef HAVE_DIALOGS
956 /* TODO: On Windows, there are two ways of defining a dialog.
958 1. Create a predefined dialog resource and include it in nt/emacs.rc.
959 Using this method, we could then set the titles and make unneeded
960 buttons invisible before displaying the dialog. Everything would
961 be a fixed size though, so there is a risk that text does not
962 fit on a button.
963 2. Create the dialog template in memory on the fly. This allows us
964 to size the dialog and buttons dynamically, probably giving more
965 natural looking results for dialogs with few buttons, and eliminating
966 the problem of text overflowing the buttons. But the API for this is
967 quite complex - structures have to be allocated in particular ways,
968 text content is tacked onto the end of structures in variable length
969 arrays with further structures tacked on after these, there are
970 certain alignment requirements for all this, and we have to
971 measure all the text and convert to "dialog coordinates" to figure
972 out how big to make everything.
974 For now, we'll just stick with menus for dialogs that are more
975 complicated than simple yes/no type questions for which we can use
976 the MessageBox function.
979 static char * button_names [] = {
980 "button1", "button2", "button3", "button4", "button5",
981 "button6", "button7", "button8", "button9", "button10" };
983 static Lisp_Object
984 w32_dialog_show (FRAME_PTR f, int keymaps,
985 Lisp_Object title, Lisp_Object header,
986 char **error)
988 int i, nb_buttons = 0;
989 char dialog_name[6];
990 int menu_item_selection;
992 widget_value *wv, *first_wv = 0, *prev_wv = 0;
994 /* Number of elements seen so far, before boundary. */
995 int left_count = 0;
996 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
997 int boundary_seen = 0;
999 *error = NULL;
1001 if (menu_items_n_panes > 1)
1003 *error = "Multiple panes in dialog box";
1004 return Qnil;
1007 /* Create a tree of widget_value objects
1008 representing the text label and buttons. */
1010 Lisp_Object pane_name, prefix;
1011 char *pane_string;
1012 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
1013 prefix = AREF (menu_items, MENU_ITEMS_PANE_PREFIX);
1014 pane_string = (NILP (pane_name)
1015 ? "" : SSDATA (pane_name));
1016 prev_wv = xmalloc_widget_value ();
1017 prev_wv->value = pane_string;
1018 if (keymaps && !NILP (prefix))
1019 prev_wv->name++;
1020 prev_wv->enabled = 1;
1021 prev_wv->name = "message";
1022 prev_wv->help = Qnil;
1023 first_wv = prev_wv;
1025 /* Loop over all panes and items, filling in the tree. */
1026 i = MENU_ITEMS_PANE_LENGTH;
1027 while (i < menu_items_used)
1030 /* Create a new item within current pane. */
1031 Lisp_Object item_name, enable, descrip, help;
1033 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1034 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1035 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1036 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1038 if (NILP (item_name))
1040 free_menubar_widget_value_tree (first_wv);
1041 *error = "Submenu in dialog items";
1042 return Qnil;
1044 if (EQ (item_name, Qquote))
1046 /* This is the boundary between left-side elts
1047 and right-side elts. Stop incrementing right_count. */
1048 boundary_seen = 1;
1049 i++;
1050 continue;
1052 if (nb_buttons >= 9)
1054 free_menubar_widget_value_tree (first_wv);
1055 *error = "Too many dialog items";
1056 return Qnil;
1059 wv = xmalloc_widget_value ();
1060 prev_wv->next = wv;
1061 wv->name = (char *) button_names[nb_buttons];
1062 if (!NILP (descrip))
1063 wv->key = SSDATA (descrip);
1064 wv->value = SSDATA (item_name);
1065 wv->call_data = (void *) &AREF (menu_items, i);
1066 wv->enabled = !NILP (enable);
1067 wv->help = Qnil;
1068 prev_wv = wv;
1070 if (! boundary_seen)
1071 left_count++;
1073 nb_buttons++;
1074 i += MENU_ITEMS_ITEM_LENGTH;
1077 /* If the boundary was not specified,
1078 by default put half on the left and half on the right. */
1079 if (! boundary_seen)
1080 left_count = nb_buttons - nb_buttons / 2;
1082 wv = xmalloc_widget_value ();
1083 wv->name = dialog_name;
1084 wv->help = Qnil;
1086 /* Frame title: 'Q' = Question, 'I' = Information.
1087 Can also have 'E' = Error if, one day, we want
1088 a popup for errors. */
1089 if (NILP (header))
1090 dialog_name[0] = 'Q';
1091 else
1092 dialog_name[0] = 'I';
1094 /* Dialog boxes use a really stupid name encoding
1095 which specifies how many buttons to use
1096 and how many buttons are on the right. */
1097 dialog_name[1] = '0' + nb_buttons;
1098 dialog_name[2] = 'B';
1099 dialog_name[3] = 'R';
1100 /* Number of buttons to put on the right. */
1101 dialog_name[4] = '0' + nb_buttons - left_count;
1102 dialog_name[5] = 0;
1103 wv->contents = first_wv;
1104 first_wv = wv;
1107 /* Actually create the dialog. */
1108 dialog_id = widget_id_tick++;
1109 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1110 f->output_data.w32->widget, 1, 0,
1111 dialog_selection_callback, 0);
1112 lw_modify_all_widgets (dialog_id, first_wv->contents, TRUE);
1114 /* Free the widget_value objects we used to specify the contents. */
1115 free_menubar_widget_value_tree (first_wv);
1117 /* No selection has been chosen yet. */
1118 menu_item_selection = 0;
1120 /* Display the menu. */
1121 lw_pop_up_all_widgets (dialog_id);
1123 /* Process events that apply to the menu. */
1124 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
1126 lw_destroy_all_widgets (dialog_id);
1128 /* Find the selected item, and its pane, to return
1129 the proper value. */
1130 if (menu_item_selection != 0)
1132 Lisp_Object prefix;
1134 prefix = Qnil;
1135 i = 0;
1136 while (i < menu_items_used)
1138 Lisp_Object entry;
1140 if (EQ (AREF (menu_items, i), Qt))
1142 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1143 i += MENU_ITEMS_PANE_LENGTH;
1145 else
1147 entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
1148 if (menu_item_selection == i)
1150 if (keymaps != 0)
1152 entry = Fcons (entry, Qnil);
1153 if (!NILP (prefix))
1154 entry = Fcons (prefix, entry);
1156 return entry;
1158 i += MENU_ITEMS_ITEM_LENGTH;
1162 else
1163 /* Make "Cancel" equivalent to C-g. */
1164 Fsignal (Qquit, Qnil);
1166 return Qnil;
1168 #else /* !HAVE_DIALOGS */
1170 /* Currently we only handle Yes No dialogs (y-or-n-p and yes-or-no-p) as
1171 simple dialogs. We could handle a few more, but I'm not aware of
1172 anywhere in Emacs that uses the other specific dialog choices that
1173 MessageBox provides. */
1175 static int
1176 is_simple_dialog (Lisp_Object contents)
1178 Lisp_Object options;
1179 Lisp_Object name, yes, no, other;
1181 if (!CONSP (contents))
1182 return 0;
1183 options = XCDR (contents);
1185 yes = build_string ("Yes");
1186 no = build_string ("No");
1188 if (!CONSP (options))
1189 return 0;
1191 name = XCAR (options);
1192 if (!CONSP (name))
1193 return 0;
1194 name = XCAR (name);
1196 if (!NILP (Fstring_equal (name, yes)))
1197 other = no;
1198 else if (!NILP (Fstring_equal (name, no)))
1199 other = yes;
1200 else
1201 return 0;
1203 options = XCDR (options);
1204 if (!CONSP (options))
1205 return 0;
1207 name = XCAR (options);
1208 if (!CONSP (name))
1209 return 0;
1210 name = XCAR (name);
1211 if (NILP (Fstring_equal (name, other)))
1212 return 0;
1214 /* Check there are no more options. */
1215 options = XCDR (options);
1216 return !(CONSP (options));
1219 static Lisp_Object
1220 simple_dialog_show (FRAME_PTR f, Lisp_Object contents, Lisp_Object header)
1222 int answer;
1223 UINT type;
1224 Lisp_Object lispy_answer = Qnil, temp = XCAR (contents);
1226 type = MB_YESNO;
1228 /* Since we only handle Yes/No dialogs, and we already checked
1229 is_simple_dialog, we don't need to worry about checking contents
1230 to see what type of dialog to use. */
1232 /* Use Unicode if possible, so any language can be displayed. */
1233 if (unicode_message_box)
1235 WCHAR *text, *title;
1236 USE_SAFE_ALLOCA;
1238 if (STRINGP (temp))
1240 char *utf8_text = SDATA (ENCODE_UTF_8 (temp));
1241 /* Be pessimistic about the number of characters needed.
1242 Remember characters outside the BMP will take more than
1243 one utf16 word, so we cannot simply use the character
1244 length of temp. */
1245 int utf8_len = strlen (utf8_text);
1246 SAFE_ALLOCA (text, WCHAR *, (utf8_len + 1) * sizeof (WCHAR));
1247 utf8to16 (utf8_text, utf8_len, text);
1249 else
1251 text = L"";
1254 if (NILP (header))
1256 title = L"Question";
1257 type |= MB_ICONQUESTION;
1259 else
1261 title = L"Information";
1262 type |= MB_ICONINFORMATION;
1265 answer = unicode_message_box (FRAME_W32_WINDOW (f), text, title, type);
1266 SAFE_FREE ();
1268 else
1270 char *text, *title;
1272 /* Fall back on ANSI message box, but at least use system
1273 encoding so questions representable by the system codepage
1274 are encoded properly. */
1275 if (STRINGP (temp))
1276 text = SDATA (ENCODE_SYSTEM (temp));
1277 else
1278 text = "";
1280 if (NILP (header))
1282 title = "Question";
1283 type |= MB_ICONQUESTION;
1285 else
1287 title = "Information";
1288 type |= MB_ICONINFORMATION;
1291 answer = MessageBox (FRAME_W32_WINDOW (f), text, title, type);
1294 if (answer == IDYES)
1295 lispy_answer = build_string ("Yes");
1296 else if (answer == IDNO)
1297 lispy_answer = build_string ("No");
1298 else
1299 Fsignal (Qquit, Qnil);
1301 for (temp = XCDR (contents); CONSP (temp); temp = XCDR (temp))
1303 Lisp_Object item, name, value;
1304 item = XCAR (temp);
1305 if (CONSP (item))
1307 name = XCAR (item);
1308 value = XCDR (item);
1310 else
1312 name = item;
1313 value = Qnil;
1316 if (!NILP (Fstring_equal (name, lispy_answer)))
1318 return value;
1321 Fsignal (Qquit, Qnil);
1322 return Qnil;
1324 #endif /* !HAVE_DIALOGS */
1327 /* UTF8: 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx, 10xxxxxx, 10xxxxxx */
1328 static void
1329 utf8to16 (unsigned char * src, int len, WCHAR * dest)
1331 while (len > 0)
1333 if (*src < 0x80)
1335 *dest = (WCHAR) *src;
1336 dest++; src++; len--;
1338 /* Since we might get >3 byte sequences which we don't handle, ignore the extra parts. */
1339 else if (*src < 0xC0)
1341 src++; len--;
1343 /* 2 char UTF-8 sequence. */
1344 else if (*src < 0xE0)
1346 *dest = (WCHAR) (((*src & 0x1f) << 6)
1347 | (*(src + 1) & 0x3f));
1348 src += 2; len -= 2; dest++;
1350 else if (*src < 0xF0)
1352 *dest = (WCHAR) (((*src & 0x0f) << 12)
1353 | ((*(src + 1) & 0x3f) << 6)
1354 | (*(src + 2) & 0x3f));
1355 src += 3; len -= 3; dest++;
1357 else /* Not encodable. Insert Unicode Substitution char. */
1359 *dest = (WCHAR) 0xfffd;
1360 src++; len--; dest++;
1363 *dest = 0;
1366 static int
1367 add_menu_item (HMENU menu, widget_value *wv, HMENU item)
1369 UINT fuFlags;
1370 char *out_string, *p, *q;
1371 int return_value;
1372 size_t nlen, orig_len;
1373 USE_SAFE_ALLOCA;
1375 if (menu_separator_name_p (wv->name))
1377 fuFlags = MF_SEPARATOR;
1378 out_string = NULL;
1380 else
1382 if (wv->enabled)
1383 fuFlags = MF_STRING;
1384 else
1385 fuFlags = MF_STRING | MF_GRAYED;
1387 if (wv->key != NULL)
1389 SAFE_ALLOCA (out_string, char *,
1390 strlen (wv->name) + strlen (wv->key) + 2);
1391 strcpy (out_string, wv->name);
1392 strcat (out_string, "\t");
1393 strcat (out_string, wv->key);
1395 else
1396 out_string = (char *)wv->name;
1398 /* Quote any special characters within the menu item's text and
1399 key binding. */
1400 nlen = orig_len = strlen (out_string);
1401 if (unicode_append_menu)
1403 /* With UTF-8, & cannot be part of a multibyte character. */
1404 for (p = out_string; *p; p++)
1406 if (*p == '&')
1407 nlen++;
1410 else
1412 /* If encoded with the system codepage, use multibyte string
1413 functions in case of multibyte characters that contain '&'. */
1414 for (p = out_string; *p; p = _mbsinc (p))
1416 if (_mbsnextc (p) == '&')
1417 nlen++;
1421 if (nlen > orig_len)
1423 p = out_string;
1424 SAFE_ALLOCA (out_string, char *, nlen + 1);
1425 q = out_string;
1426 while (*p)
1428 if (unicode_append_menu)
1430 if (*p == '&')
1431 *q++ = *p;
1432 *q++ = *p++;
1434 else
1436 if (_mbsnextc (p) == '&')
1438 _mbsncpy (q, p, 1);
1439 q = _mbsinc (q);
1441 _mbsncpy (q, p, 1);
1442 p = _mbsinc (p);
1443 q = _mbsinc (q);
1446 *q = '\0';
1449 if (item != NULL)
1450 fuFlags = MF_POPUP;
1451 else if (wv->title || wv->call_data == 0)
1453 /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since
1454 we can't deallocate the memory otherwise. */
1455 if (get_menu_item_info)
1457 out_string = (char *) local_alloc (strlen (wv->name) + 1);
1458 strcpy (out_string, wv->name);
1459 #ifdef MENU_DEBUG
1460 DebPrint ("Menu: allocating %ld for owner-draw", out_string);
1461 #endif
1462 fuFlags = MF_OWNERDRAW | MF_DISABLED;
1464 else
1465 fuFlags = MF_DISABLED;
1468 /* Draw radio buttons and tickboxes. */
1469 else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
1470 wv->button_type == BUTTON_TYPE_RADIO))
1471 fuFlags |= MF_CHECKED;
1472 else
1473 fuFlags |= MF_UNCHECKED;
1476 if (unicode_append_menu && out_string)
1478 /* Convert out_string from UTF-8 to UTF-16-LE. */
1479 int utf8_len = strlen (out_string);
1480 WCHAR * utf16_string;
1481 if (fuFlags & MF_OWNERDRAW)
1482 utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR));
1483 else
1484 SAFE_ALLOCA (utf16_string, WCHAR *, (utf8_len + 1) * sizeof (WCHAR));
1486 utf8to16 (out_string, utf8_len, utf16_string);
1487 return_value = unicode_append_menu (menu, fuFlags,
1488 item != NULL ? (UINT) item
1489 : (UINT) wv->call_data,
1490 utf16_string);
1491 if (!return_value)
1493 /* On W9x/ME, Unicode menus are not supported, though AppendMenuW
1494 apparently does exist at least in some cases and appears to be
1495 stubbed out to do nothing. out_string is UTF-8, but since
1496 our standard menus are in English and this is only going to
1497 happen the first time a menu is used, the encoding is
1498 of minor importance compared with menus not working at all. */
1499 return_value =
1500 AppendMenu (menu, fuFlags,
1501 item != NULL ? (UINT) item: (UINT) wv->call_data,
1502 out_string);
1503 /* Don't use Unicode menus in future, unless this is Windows
1504 NT or later, where a failure of AppendMenuW does NOT mean
1505 Unicode menus are unsupported. */
1506 if (osinfo_cache.dwPlatformId != VER_PLATFORM_WIN32_NT)
1507 unicode_append_menu = NULL;
1510 if (unicode_append_menu && (fuFlags & MF_OWNERDRAW))
1511 local_free (out_string);
1513 else
1515 return_value =
1516 AppendMenu (menu,
1517 fuFlags,
1518 item != NULL ? (UINT) item : (UINT) wv->call_data,
1519 out_string );
1522 /* This must be done after the menu item is created. */
1523 if (!wv->title && wv->call_data != 0)
1525 if (set_menu_item_info)
1527 MENUITEMINFO info;
1528 memset (&info, 0, sizeof (info));
1529 info.cbSize = sizeof (info);
1530 info.fMask = MIIM_DATA;
1532 /* Set help string for menu item. Leave it as a Lisp_Object
1533 until it is ready to be displayed, since GC can happen while
1534 menus are active. */
1535 if (!NILP (wv->help))
1536 #ifdef USE_LISP_UNION_TYPE
1537 info.dwItemData = (DWORD) (wv->help).i;
1538 #else
1539 info.dwItemData = (DWORD) (wv->help);
1540 #endif
1541 if (wv->button_type == BUTTON_TYPE_RADIO)
1543 /* CheckMenuRadioItem allows us to differentiate TOGGLE and
1544 RADIO items, but is not available on NT 3.51 and earlier. */
1545 info.fMask |= MIIM_TYPE | MIIM_STATE;
1546 info.fType = MFT_RADIOCHECK | MFT_STRING;
1547 info.dwTypeData = out_string;
1548 info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED;
1551 set_menu_item_info (menu,
1552 item != NULL ? (UINT) item : (UINT) wv->call_data,
1553 FALSE, &info);
1556 SAFE_FREE ();
1557 return return_value;
1560 /* Construct native Windows menu(bar) based on widget_value tree. */
1561 static int
1562 fill_in_menu (HMENU menu, widget_value *wv)
1564 for ( ; wv != NULL; wv = wv->next)
1566 if (wv->contents)
1568 HMENU sub_menu = CreatePopupMenu ();
1570 if (sub_menu == NULL)
1571 return 0;
1573 if (!fill_in_menu (sub_menu, wv->contents) ||
1574 !add_menu_item (menu, wv, sub_menu))
1576 DestroyMenu (sub_menu);
1577 return 0;
1580 else
1582 if (!add_menu_item (menu, wv, NULL))
1583 return 0;
1586 return 1;
1589 /* Display help string for currently pointed to menu item. Not
1590 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
1591 available. */
1592 void
1593 w32_menu_display_help (HWND owner, HMENU menu, UINT item, UINT flags)
1595 if (get_menu_item_info)
1597 struct frame *f = x_window_to_frame (&one_w32_display_info, owner);
1598 Lisp_Object frame, help;
1600 /* No help echo on owner-draw menu items, or when the keyboard is used
1601 to navigate the menus, since tooltips are distracting if they pop
1602 up elsewhere. */
1603 if (flags & MF_OWNERDRAW || flags & MF_POPUP
1604 || !(flags & MF_MOUSESELECT))
1605 help = Qnil;
1606 else
1608 MENUITEMINFO info;
1610 memset (&info, 0, sizeof (info));
1611 info.cbSize = sizeof (info);
1612 info.fMask = MIIM_DATA;
1613 get_menu_item_info (menu, item, FALSE, &info);
1615 #ifdef USE_LISP_UNION_TYPE
1616 help = info.dwItemData ? (Lisp_Object) ((EMACS_INT) info.dwItemData)
1617 : Qnil;
1618 #else
1619 help = info.dwItemData ? (Lisp_Object) info.dwItemData : Qnil;
1620 #endif
1623 /* Store the help echo in the keyboard buffer as the X toolkit
1624 version does, rather than directly showing it. This seems to
1625 solve the GC problems that were present when we based the
1626 Windows code on the non-toolkit version. */
1627 if (f)
1629 XSETFRAME (frame, f);
1630 kbd_buffer_store_help_event (frame, help);
1632 else
1633 /* X version has a loop through frames here, which doesn't
1634 appear to do anything, unless it has some side effect. */
1635 show_help_echo (help, Qnil, Qnil, Qnil);
1639 /* Free memory used by owner-drawn strings. */
1640 static void
1641 w32_free_submenu_strings (HMENU menu)
1643 int i, num = GetMenuItemCount (menu);
1644 for (i = 0; i < num; i++)
1646 MENUITEMINFO info;
1647 memset (&info, 0, sizeof (info));
1648 info.cbSize = sizeof (info);
1649 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_SUBMENU;
1651 get_menu_item_info (menu, i, TRUE, &info);
1653 /* Owner-drawn names are held in dwItemData. */
1654 if ((info.fType & MF_OWNERDRAW) && info.dwItemData)
1656 #ifdef MENU_DEBUG
1657 DebPrint ("Menu: freeing %ld for owner-draw", info.dwItemData);
1658 #endif
1659 local_free (info.dwItemData);
1662 /* Recurse down submenus. */
1663 if (info.hSubMenu)
1664 w32_free_submenu_strings (info.hSubMenu);
1668 void
1669 w32_free_menu_strings (HWND hwnd)
1671 HMENU menu = current_popup_menu;
1673 if (get_menu_item_info)
1675 /* If there is no popup menu active, free the strings from the frame's
1676 menubar. */
1677 if (!menu)
1678 menu = GetMenu (hwnd);
1680 if (menu)
1681 w32_free_submenu_strings (menu);
1684 current_popup_menu = NULL;
1687 #endif /* HAVE_MENUS */
1689 /* The following is used by delayed window autoselection. */
1691 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
1692 doc: /* Return t if a menu or popup dialog is active on selected frame. */)
1693 (void)
1695 #ifdef HAVE_MENUS
1696 FRAME_PTR f;
1697 f = SELECTED_FRAME ();
1698 return (f->output_data.w32->menubar_active > 0) ? Qt : Qnil;
1699 #else
1700 return Qnil;
1701 #endif /* HAVE_MENUS */
1704 void
1705 syms_of_w32menu (void)
1707 globals_of_w32menu ();
1709 current_popup_menu = NULL;
1711 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
1713 defsubr (&Smenu_or_popup_active_p);
1714 #ifdef HAVE_MENUS
1715 defsubr (&Sx_popup_dialog);
1716 #endif
1720 globals_of_w32menu is used to initialize those global variables that
1721 must always be initialized on startup even when the global variable
1722 initialized is non zero (see the function main in emacs.c).
1723 globals_of_w32menu is called from syms_of_w32menu when the global
1724 variable initialized is 0 and directly from main when initialized
1725 is non zero.
1727 void
1728 globals_of_w32menu (void)
1730 /* See if Get/SetMenuItemInfo functions are available. */
1731 HMODULE user32 = GetModuleHandle ("user32.dll");
1732 get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA");
1733 set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA");
1734 unicode_append_menu = (AppendMenuW_Proc) GetProcAddress (user32, "AppendMenuW");
1735 unicode_message_box = (MessageBoxW_Proc) GetProcAddress (user32, "MessageBoxW");