1 /* Menu support for GNU Emacs on the Microsoft Windows API.
2 Copyright (C) 1986, 1988, 1993-1994, 1996, 1998-1999, 2001-2015 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/>. */
30 #include "termhooks.h"
32 #include "blockinput.h"
33 #include "character.h"
39 /* This may include sys/types.h, and that somehow loses
40 if this is not done before the other system files. */
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. */
49 #endif /* !NTGUI_UNICODE */
51 /* Load sys/types.h if not already loaded.
52 In some systems loading it twice is suicidal. */
54 #include <sys/types.h>
57 #include "dispextern.h"
59 #include "w32common.h" /* for osinfo_cache */
61 #undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
68 HMENU current_popup_menu
;
70 void syms_of_w32menu (void);
71 void globals_of_w32menu (void);
73 typedef BOOL (WINAPI
* GetMenuItemInfoA_Proc
) (
77 IN OUT LPMENUITEMINFOA
);
78 typedef BOOL (WINAPI
* SetMenuItemInfoA_Proc
) (
83 typedef int (WINAPI
* MessageBoxW_Proc
) (
86 IN
const WCHAR
*caption
,
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 void set_frame_menubar (struct frame
*, bool, bool);
104 static Lisp_Object
w32_dialog_show (struct frame
*, Lisp_Object
, Lisp_Object
, char **);
106 static int is_simple_dialog (Lisp_Object
);
107 static Lisp_Object
simple_dialog_show (struct frame
*, Lisp_Object
, Lisp_Object
);
110 static void utf8to16 (unsigned char *, int, WCHAR
*);
111 static int fill_in_menu (HMENU
, widget_value
*);
113 void w32_free_menu_strings (HWND
);
116 w32_popup_dialog (struct frame
*f
, Lisp_Object header
, Lisp_Object contents
)
119 check_window_system (f
);
123 /* Handle simple Yes/No choices as MessageBox popups. */
124 if (is_simple_dialog (contents
))
125 return simple_dialog_show (f
, contents
, header
);
127 return Qunsupported__w32_dialog
;
128 #else /* HAVE_DIALOGS */
132 Lisp_Object selection
;
134 /* Decode the dialog items from what was specified. */
135 title
= Fcar (contents
);
136 CHECK_STRING (title
);
138 list_of_panes (Fcons (contents
, Qnil
));
140 /* Display them in a dialog box. */
142 selection
= w32_dialog_show (f
, title
, header
, &error_name
);
145 discard_menu_items ();
146 FRAME_DISPLAY_INFO (f
)->grabbed
= 0;
148 if (error_name
) error (error_name
);
151 #endif /* HAVE_DIALOGS */
154 /* Activate the menu bar of frame F.
155 This is called from keyboard.c when it gets the
156 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
158 To activate the menu bar, we signal to the input thread that it can
159 return from the WM_INITMENU message, allowing the normal Windows
160 processing of the menus.
162 But first we recompute the menu bar contents (the whole tree).
164 This way we can safely execute Lisp code. */
167 x_activate_menubar (struct frame
*f
)
169 set_frame_menubar (f
, 0, 1);
171 /* Lock out further menubar changes while active. */
172 f
->output_data
.w32
->menubar_active
= 1;
174 /* Signal input thread to return from WM_INITMENU. */
175 complete_deferred_msg (FRAME_W32_WINDOW (f
), WM_INITMENU
, 0);
178 /* This callback is called from the menu bar pulldown menu
179 when the user makes a selection.
180 Figure out what the user chose
181 and put the appropriate events into the keyboard buffer. */
184 menubar_selection_callback (struct frame
*f
, void * client_data
)
186 Lisp_Object prefix
, entry
;
188 Lisp_Object
*subprefix_stack
;
189 int submenu_depth
= 0;
195 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* word_size
);
196 vector
= f
->menu_bar_vector
;
199 while (i
< f
->menu_bar_items_used
)
201 if (EQ (AREF (vector
, i
), Qnil
))
203 subprefix_stack
[submenu_depth
++] = prefix
;
207 else if (EQ (AREF (vector
, i
), Qlambda
))
209 prefix
= subprefix_stack
[--submenu_depth
];
212 else if (EQ (AREF (vector
, i
), Qt
))
214 prefix
= AREF (vector
, i
+ MENU_ITEMS_PANE_PREFIX
);
215 i
+= MENU_ITEMS_PANE_LENGTH
;
219 entry
= AREF (vector
, i
+ MENU_ITEMS_ITEM_VALUE
);
220 /* The EMACS_INT cast avoids a warning. There's no problem
221 as long as pointers have enough bits to hold small integers. */
222 if ((int) (EMACS_INT
) client_data
== i
)
225 struct input_event buf
;
229 XSETFRAME (frame
, f
);
230 buf
.kind
= MENU_BAR_EVENT
;
231 buf
.frame_or_window
= frame
;
233 kbd_buffer_store_event (&buf
);
235 for (j
= 0; j
< submenu_depth
; j
++)
236 if (!NILP (subprefix_stack
[j
]))
238 buf
.kind
= MENU_BAR_EVENT
;
239 buf
.frame_or_window
= frame
;
240 buf
.arg
= subprefix_stack
[j
];
241 kbd_buffer_store_event (&buf
);
246 buf
.kind
= MENU_BAR_EVENT
;
247 buf
.frame_or_window
= frame
;
249 kbd_buffer_store_event (&buf
);
252 buf
.kind
= MENU_BAR_EVENT
;
253 buf
.frame_or_window
= frame
;
255 /* Free memory used by owner-drawn and help-echo strings. */
256 w32_free_menu_strings (FRAME_W32_WINDOW (f
));
257 kbd_buffer_store_event (&buf
);
259 f
->output_data
.w32
->menubar_active
= 0;
262 i
+= MENU_ITEMS_ITEM_LENGTH
;
265 /* Free memory used by owner-drawn and help-echo strings. */
266 w32_free_menu_strings (FRAME_W32_WINDOW (f
));
267 f
->output_data
.w32
->menubar_active
= 0;
271 /* Set the contents of the menubar widgets of frame F.
272 The argument FIRST_TIME is currently ignored;
273 it is set the first time this is called, from initialize_frame_menubar. */
276 set_frame_menubar (struct frame
*f
, bool first_time
, bool deep_p
)
278 HMENU menubar_widget
= f
->output_data
.w32
->menubar_widget
;
280 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
282 int *submenu_start
, *submenu_end
;
283 int *submenu_top_level_items
, *submenu_n_panes
;
285 /* We must not change the menubar when actually in use. */
286 if (f
->output_data
.w32
->menubar_active
)
289 XSETFRAME (Vmenu_updating_frame
, f
);
291 if (! menubar_widget
)
296 /* Make a widget-value tree representing the entire menu trees. */
298 struct buffer
*prev
= current_buffer
;
300 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
301 int previous_menu_items_used
= f
->menu_bar_items_used
;
302 Lisp_Object
*previous_items
303 = (Lisp_Object
*) alloca (previous_menu_items_used
306 /* If we are making a new widget, its contents are empty,
307 do always reinitialize them. */
308 if (! menubar_widget
)
309 previous_menu_items_used
= 0;
311 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->contents
;
312 specbind (Qinhibit_quit
, Qt
);
313 /* Don't let the debugger step into this code
314 because it is not reentrant. */
315 specbind (Qdebug_on_next_call
, Qnil
);
317 record_unwind_save_match_data ();
319 if (NILP (Voverriding_local_map_menu_flag
))
321 specbind (Qoverriding_terminal_local_map
, Qnil
);
322 specbind (Qoverriding_local_map
, Qnil
);
325 set_buffer_internal_1 (XBUFFER (buffer
));
328 safe_run_hooks (Qactivate_menubar_hook
);
329 safe_run_hooks (Qmenu_bar_update_hook
);
330 fset_menu_bar_items (f
, menu_bar_items (FRAME_MENU_BAR_ITEMS (f
)));
332 items
= FRAME_MENU_BAR_ITEMS (f
);
334 /* Save the frame's previous menu bar contents data. */
335 if (previous_menu_items_used
)
336 memcpy (previous_items
, XVECTOR (f
->menu_bar_vector
)->contents
,
337 previous_menu_items_used
* word_size
);
339 /* Fill in menu_items with the current menu bar contents.
340 This can evaluate Lisp code. */
343 menu_items
= f
->menu_bar_vector
;
344 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
345 submenu_start
= (int *) alloca (ASIZE (items
) * sizeof (int));
346 submenu_end
= (int *) alloca (ASIZE (items
) * sizeof (int));
347 submenu_n_panes
= (int *) alloca (ASIZE (items
) * sizeof (int));
348 submenu_top_level_items
= (int *) alloca (ASIZE (items
) * sizeof (int));
350 for (i
= 0; i
< ASIZE (items
); i
+= 4)
352 Lisp_Object key
, string
, maps
;
356 key
= AREF (items
, i
);
357 string
= AREF (items
, i
+ 1);
358 maps
= AREF (items
, i
+ 2);
362 submenu_start
[i
] = menu_items_used
;
364 menu_items_n_panes
= 0;
365 submenu_top_level_items
[i
]
366 = parse_single_submenu (key
, string
, maps
);
367 submenu_n_panes
[i
] = menu_items_n_panes
;
369 submenu_end
[i
] = menu_items_used
;
372 finish_menu_items ();
374 /* Convert menu_items into widget_value trees
375 to display the menu. This cannot evaluate Lisp code. */
377 wv
= make_widget_value ("menubar", NULL
, true, Qnil
);
378 wv
->button_type
= BUTTON_TYPE_NONE
;
381 for (i
= 0; i
< last_i
; i
+= 4)
383 menu_items_n_panes
= submenu_n_panes
[i
];
384 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
385 submenu_top_level_items
[i
]);
389 first_wv
->contents
= wv
;
390 /* Don't set wv->name here; GC during the loop might relocate it. */
392 wv
->button_type
= BUTTON_TYPE_NONE
;
396 set_buffer_internal_1 (prev
);
398 /* If there has been no change in the Lisp-level contents
399 of the menu bar, skip redisplaying it. Just exit. */
401 for (i
= 0; i
< previous_menu_items_used
; i
++)
402 if (menu_items_used
== i
403 || (!EQ (previous_items
[i
], AREF (menu_items
, i
))))
405 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
407 free_menubar_widget_value_tree (first_wv
);
408 discard_menu_items ();
409 unbind_to (specpdl_count
, Qnil
);
413 fset_menu_bar_vector (f
, menu_items
);
414 f
->menu_bar_items_used
= menu_items_used
;
416 /* This undoes save_menu_items. */
417 unbind_to (specpdl_count
, Qnil
);
419 /* Now GC cannot happen during the lifetime of the widget_value,
420 so it's safe to store data from a Lisp_String, as long as
421 local copies are made when the actual menu is created.
422 Windows takes care of this for normal string items, but
423 not for owner-drawn items or additional item-info. */
424 wv
= first_wv
->contents
;
425 for (i
= 0; i
< ASIZE (items
); i
+= 4)
428 string
= AREF (items
, i
+ 1);
431 wv
->name
= SSDATA (string
);
432 update_submenu_strings (wv
->contents
);
438 /* Make a widget-value tree containing
439 just the top level menu bar strings. */
441 wv
= make_widget_value ("menubar", NULL
, true, Qnil
);
442 wv
->button_type
= BUTTON_TYPE_NONE
;
445 items
= FRAME_MENU_BAR_ITEMS (f
);
446 for (i
= 0; i
< ASIZE (items
); i
+= 4)
450 string
= AREF (items
, i
+ 1);
454 wv
= make_widget_value (SSDATA (string
), NULL
, true, Qnil
);
455 wv
->button_type
= BUTTON_TYPE_NONE
;
456 /* This prevents lwlib from assuming this
457 menu item is really supposed to be empty. */
458 /* The EMACS_INT cast avoids a warning.
459 This value just has to be different from small integers. */
460 wv
->call_data
= (void *) (EMACS_INT
) (-1);
465 first_wv
->contents
= wv
;
469 /* Forget what we thought we knew about what is in the
470 detailed contents of the menu bar menus.
471 Changing the top level always destroys the contents. */
472 f
->menu_bar_items_used
= 0;
475 /* Create or update the menu bar widget. */
481 /* Empty current menubar, rather than creating a fresh one. */
482 while (DeleteMenu (menubar_widget
, 0, MF_BYPOSITION
))
487 menubar_widget
= CreateMenu ();
489 fill_in_menu (menubar_widget
, first_wv
->contents
);
491 free_menubar_widget_value_tree (first_wv
);
494 HMENU old_widget
= f
->output_data
.w32
->menubar_widget
;
496 f
->output_data
.w32
->menubar_widget
= menubar_widget
;
497 SetMenu (FRAME_W32_WINDOW (f
), f
->output_data
.w32
->menubar_widget
);
498 /* Causes flicker when menu bar is updated
499 DrawMenuBar (FRAME_W32_WINDOW (f)); */
501 /* Force the window size to be recomputed so that the frame's text
502 area remains the same, if menubar has just been created. */
503 if (old_widget
== NULL
)
504 adjust_frame_size (f
, FRAME_TEXT_WIDTH (f
),
505 FRAME_TEXT_HEIGHT (f
), 2, 0, Qmenu_bar_lines
);
511 /* Called from Fx_create_frame to create the initial menubar of a frame
512 before it is mapped, so that the window is mapped with the menubar already
513 there instead of us tacking it on later and thrashing the window after it
517 initialize_frame_menubar (struct frame
*f
)
519 /* This function is called before the first chance to redisplay
520 the frame. It has to be, so the frame will have the right size. */
521 fset_menu_bar_items (f
, menu_bar_items (FRAME_MENU_BAR_ITEMS (f
)));
522 set_frame_menubar (f
, 1, 1);
525 /* Get rid of the menu bar of frame F, and free its storage.
526 This is used when deleting a frame, and when turning off the menu bar. */
529 free_frame_menubar (struct frame
*f
)
534 HMENU old
= GetMenu (FRAME_W32_WINDOW (f
));
535 SetMenu (FRAME_W32_WINDOW (f
), NULL
);
536 f
->output_data
.w32
->menubar_widget
= NULL
;
544 /* w32_menu_show actually displays a menu using the panes and items in
545 menu_items and returns the value selected from it; we assume input
546 is blocked by the caller. */
548 /* F is the frame the menu is for.
549 X and Y are the frame-relative specified position,
550 relative to the inside upper left corner of the frame F.
551 Bitfield MENUFLAGS bits are:
552 MENU_FOR_CLICK is set if this menu was invoked for a mouse click.
553 MENU_KEYMAPS is set if this menu was specified with keymaps;
554 in that case, we return a list containing the chosen item's value
555 and perhaps also the pane's prefix.
556 TITLE is the specified menu title.
557 ERROR is a place to store an error message string in case of failure.
558 (We return nil on failure, but the value doesn't actually matter.) */
561 w32_menu_show (struct frame
*f
, int x
, int y
, int menuflags
,
562 Lisp_Object title
, const char **error
)
565 int menu_item_selection
;
568 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
569 widget_value
**submenu_stack
570 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
571 Lisp_Object
*subprefix_stack
572 = (Lisp_Object
*) alloca (menu_items_used
* word_size
);
573 int submenu_depth
= 0;
578 if (menu_items_n_panes
== 0)
581 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
583 *error
= "Empty menu";
589 /* Create a tree of widget_value objects
590 representing the panes and their items. */
591 wv
= make_widget_value ("menu", NULL
, true, Qnil
);
592 wv
->button_type
= BUTTON_TYPE_NONE
;
596 /* Loop over all panes and items, filling in the tree. */
598 while (i
< menu_items_used
)
600 if (EQ (AREF (menu_items
, i
), Qnil
))
602 submenu_stack
[submenu_depth
++] = save_wv
;
608 else if (EQ (AREF (menu_items
, i
), Qlambda
))
611 save_wv
= submenu_stack
[--submenu_depth
];
615 else if (EQ (AREF (menu_items
, i
), Qt
)
616 && submenu_depth
!= 0)
617 i
+= MENU_ITEMS_PANE_LENGTH
;
618 /* Ignore a nil in the item list.
619 It's meaningful only for dialog boxes. */
620 else if (EQ (AREF (menu_items
, i
), Qquote
))
622 else if (EQ (AREF (menu_items
, i
), Qt
))
624 /* Create a new pane. */
625 Lisp_Object pane_name
, prefix
;
626 const char *pane_string
;
627 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
628 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
630 if (STRINGP (pane_name
))
632 if (unicode_append_menu
)
633 pane_name
= ENCODE_UTF_8 (pane_name
);
634 else if (STRING_MULTIBYTE (pane_name
))
635 pane_name
= ENCODE_SYSTEM (pane_name
);
637 ASET (menu_items
, i
+ MENU_ITEMS_PANE_NAME
, pane_name
);
640 pane_string
= (NILP (pane_name
)
641 ? "" : SSDATA (pane_name
));
642 /* If there is just one top-level pane, put all its items directly
643 under the top-level menu. */
644 if (menu_items_n_panes
== 1)
647 /* If the pane has a meaningful name,
648 make the pane a top-level menu item
649 with its items as a submenu beneath it. */
650 if (!(menuflags
& MENU_KEYMAPS
) && strcmp (pane_string
, ""))
652 wv
= make_widget_value (pane_string
, NULL
, true, Qnil
);
656 first_wv
->contents
= wv
;
657 if ((menuflags
& MENU_KEYMAPS
) && !NILP (prefix
))
659 wv
->button_type
= BUTTON_TYPE_NONE
;
669 i
+= MENU_ITEMS_PANE_LENGTH
;
673 /* Create a new item within current pane. */
674 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
676 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
677 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
678 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
679 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
680 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
681 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
682 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
684 if (STRINGP (item_name
))
686 if (unicode_append_menu
)
687 item_name
= ENCODE_UTF_8 (item_name
);
688 else if (STRING_MULTIBYTE (item_name
))
689 item_name
= ENCODE_SYSTEM (item_name
);
691 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
, item_name
);
694 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
696 descrip
= ENCODE_SYSTEM (descrip
);
697 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
, descrip
);
700 wv
= make_widget_value (SSDATA (item_name
), NULL
, !NILP (enable
),
701 STRINGP (help
) ? help
: Qnil
);
705 save_wv
->contents
= wv
;
707 wv
->key
= SSDATA (descrip
);
708 /* Use the contents index as call_data, since we are
709 restricted to 16-bits. */
710 wv
->call_data
= !NILP (def
) ? (void *) (EMACS_INT
) i
: 0;
713 wv
->button_type
= BUTTON_TYPE_NONE
;
714 else if (EQ (type
, QCtoggle
))
715 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
716 else if (EQ (type
, QCradio
))
717 wv
->button_type
= BUTTON_TYPE_RADIO
;
721 wv
->selected
= !NILP (selected
);
725 i
+= MENU_ITEMS_ITEM_LENGTH
;
729 /* Deal with the title, if it is non-nil. */
732 widget_value
*wv_title
;
733 widget_value
*wv_sep
= make_widget_value ("--", NULL
, false, Qnil
);
735 /* Maybe replace this separator with a bitmap or owner-draw item
736 so that it looks better. Having two separators looks odd. */
737 wv_sep
->next
= first_wv
->contents
;
739 if (unicode_append_menu
)
740 title
= ENCODE_UTF_8 (title
);
741 else if (STRING_MULTIBYTE (title
))
742 title
= ENCODE_SYSTEM (title
);
744 wv_title
= make_widget_value (SSDATA (title
), NULL
, true, Qnil
);
745 wv_title
->title
= TRUE
;
746 wv_title
->button_type
= BUTTON_TYPE_NONE
;
747 wv_title
->next
= wv_sep
;
748 first_wv
->contents
= wv_title
;
751 /* No selection has been chosen yet. */
752 menu_item_selection
= 0;
754 /* Actually create the menu. */
755 current_popup_menu
= menu
= CreatePopupMenu ();
756 fill_in_menu (menu
, first_wv
->contents
);
758 /* Adjust coordinates to be root-window-relative. */
761 ClientToScreen (FRAME_W32_WINDOW (f
), &pos
);
763 /* Display the menu. */
764 menu_item_selection
= SendMessage (FRAME_W32_WINDOW (f
),
765 WM_EMACS_TRACKPOPUPMENU
,
766 (WPARAM
)menu
, (LPARAM
)&pos
);
768 /* Clean up extraneous mouse events which might have been generated
770 discard_mouse_events ();
771 FRAME_DISPLAY_INFO (f
)->grabbed
= 0;
773 /* Free the widget_value objects we used to specify the contents. */
774 free_menubar_widget_value_tree (first_wv
);
778 /* Free the owner-drawn and help-echo menu strings. */
779 w32_free_menu_strings (FRAME_W32_WINDOW (f
));
780 f
->output_data
.w32
->menubar_active
= 0;
782 /* Find the selected item, and its pane, to return
784 if (menu_item_selection
!= 0)
786 Lisp_Object prefix
, entry
;
788 prefix
= entry
= Qnil
;
790 while (i
< menu_items_used
)
792 if (EQ (AREF (menu_items
, i
), Qnil
))
794 subprefix_stack
[submenu_depth
++] = prefix
;
798 else if (EQ (AREF (menu_items
, i
), Qlambda
))
800 prefix
= subprefix_stack
[--submenu_depth
];
803 else if (EQ (AREF (menu_items
, i
), Qt
))
805 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
806 i
+= MENU_ITEMS_PANE_LENGTH
;
808 /* Ignore a nil in the item list.
809 It's meaningful only for dialog boxes. */
810 else if (EQ (AREF (menu_items
, i
), Qquote
))
814 entry
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
815 if (menu_item_selection
== i
)
817 if (menuflags
& MENU_KEYMAPS
)
821 entry
= Fcons (entry
, Qnil
);
823 entry
= Fcons (prefix
, entry
);
824 for (j
= submenu_depth
- 1; j
>= 0; j
--)
825 if (!NILP (subprefix_stack
[j
]))
826 entry
= Fcons (subprefix_stack
[j
], entry
);
831 i
+= MENU_ITEMS_ITEM_LENGTH
;
835 else if (!(menuflags
& MENU_FOR_CLICK
))
838 /* Make "Cancel" equivalent to C-g. */
839 Fsignal (Qquit
, Qnil
);
848 /* TODO: On Windows, there are two ways of defining a dialog.
850 1. Create a predefined dialog resource and include it in nt/emacs.rc.
851 Using this method, we could then set the titles and make unneeded
852 buttons invisible before displaying the dialog. Everything would
853 be a fixed size though, so there is a risk that text does not
855 2. Create the dialog template in memory on the fly. This allows us
856 to size the dialog and buttons dynamically, probably giving more
857 natural looking results for dialogs with few buttons, and eliminating
858 the problem of text overflowing the buttons. But the API for this is
859 quite complex - structures have to be allocated in particular ways,
860 text content is tacked onto the end of structures in variable length
861 arrays with further structures tacked on after these, there are
862 certain alignment requirements for all this, and we have to
863 measure all the text and convert to "dialog coordinates" to figure
864 out how big to make everything.
866 For now, we'll just stick with menus for dialogs that are more
867 complicated than simple yes/no type questions for which we can use
868 the MessageBox function.
871 static char * button_names
[] = {
872 "button1", "button2", "button3", "button4", "button5",
873 "button6", "button7", "button8", "button9", "button10" };
876 w32_dialog_show (struct frame
*f
, Lisp_Object title
,
877 Lisp_Object header
, char **error
)
879 int i
, nb_buttons
= 0;
881 int menu_item_selection
;
883 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
885 /* Number of elements seen so far, before boundary. */
887 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
888 int boundary_seen
= 0;
892 if (menu_items_n_panes
> 1)
894 *error
= "Multiple panes in dialog box";
898 /* Create a tree of widget_value objects
899 representing the text label and buttons. */
901 Lisp_Object pane_name
;
903 pane_name
= AREF (menu_items
, MENU_ITEMS_PANE_NAME
);
904 pane_string
= (NILP (pane_name
)
905 ? "" : SSDATA (pane_name
));
906 prev_wv
= make_widget_value ("message", pane_string
, true, Qnil
);
909 /* Loop over all panes and items, filling in the tree. */
910 i
= MENU_ITEMS_PANE_LENGTH
;
911 while (i
< menu_items_used
)
914 /* Create a new item within current pane. */
915 Lisp_Object item_name
, enable
, descrip
, help
;
917 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
918 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
919 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
920 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
922 if (NILP (item_name
))
924 free_menubar_widget_value_tree (first_wv
);
925 *error
= "Submenu in dialog items";
928 if (EQ (item_name
, Qquote
))
930 /* This is the boundary between left-side elts
931 and right-side elts. Stop incrementing right_count. */
938 free_menubar_widget_value_tree (first_wv
);
939 *error
= "Too many dialog items";
943 wv
= make_widget_value (button_names
[nb_buttons
],
945 !NILP (enable
), Qnil
);
948 wv
->key
= SSDATA (descrip
);
949 wv
->call_data
= aref_addr (menu_items
, i
);
956 i
+= MENU_ITEMS_ITEM_LENGTH
;
959 /* If the boundary was not specified,
960 by default put half on the left and half on the right. */
962 left_count
= nb_buttons
- nb_buttons
/ 2;
964 wv
= make_widget_value (dialog_name
, NULL
, false, Qnil
);
966 /* Frame title: 'Q' = Question, 'I' = Information.
967 Can also have 'E' = Error if, one day, we want
968 a popup for errors. */
970 dialog_name
[0] = 'Q';
972 dialog_name
[0] = 'I';
974 /* Dialog boxes use a really stupid name encoding
975 which specifies how many buttons to use
976 and how many buttons are on the right. */
977 dialog_name
[1] = '0' + nb_buttons
;
978 dialog_name
[2] = 'B';
979 dialog_name
[3] = 'R';
980 /* Number of buttons to put on the right. */
981 dialog_name
[4] = '0' + nb_buttons
- left_count
;
983 wv
->contents
= first_wv
;
987 /* Actually create the dialog. */
988 dialog_id
= widget_id_tick
++;
989 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
990 f
->output_data
.w32
->widget
, 1, 0,
991 dialog_selection_callback
, 0);
992 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, TRUE
);
994 /* Free the widget_value objects we used to specify the contents. */
995 free_menubar_widget_value_tree (first_wv
);
997 /* No selection has been chosen yet. */
998 menu_item_selection
= 0;
1000 /* Display the menu. */
1001 lw_pop_up_all_widgets (dialog_id
);
1003 /* Process events that apply to the menu. */
1004 popup_get_selection ((XEvent
*) 0, FRAME_DISPLAY_INFO (f
), dialog_id
);
1006 lw_destroy_all_widgets (dialog_id
);
1008 /* Find the selected item, and its pane, to return
1009 the proper value. */
1010 if (menu_item_selection
!= 0)
1013 while (i
< menu_items_used
)
1017 if (EQ (AREF (menu_items
, i
), Qt
))
1018 i
+= MENU_ITEMS_PANE_LENGTH
;
1021 entry
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
1022 if (menu_item_selection
== i
)
1024 i
+= MENU_ITEMS_ITEM_LENGTH
;
1029 /* Make "Cancel" equivalent to C-g. */
1030 Fsignal (Qquit
, Qnil
);
1034 #else /* !HAVE_DIALOGS */
1036 /* Currently we only handle Yes No dialogs (y-or-n-p and yes-or-no-p) as
1037 simple dialogs. We could handle a few more, but I'm not aware of
1038 anywhere in Emacs that uses the other specific dialog choices that
1039 MessageBox provides. */
1042 is_simple_dialog (Lisp_Object contents
)
1044 Lisp_Object options
;
1045 Lisp_Object name
, yes
, no
, other
;
1047 if (!CONSP (contents
))
1049 options
= XCDR (contents
);
1051 yes
= build_string ("Yes");
1052 no
= build_string ("No");
1054 if (!CONSP (options
))
1057 name
= XCAR (options
);
1062 if (!NILP (Fstring_equal (name
, yes
)))
1064 else if (!NILP (Fstring_equal (name
, no
)))
1069 options
= XCDR (options
);
1070 if (!CONSP (options
))
1073 name
= XCAR (options
);
1077 if (NILP (Fstring_equal (name
, other
)))
1080 /* Check there are no more options. */
1081 options
= XCDR (options
);
1082 return !(CONSP (options
));
1086 simple_dialog_show (struct frame
*f
, Lisp_Object contents
, Lisp_Object header
)
1090 Lisp_Object lispy_answer
= Qnil
, temp
= XCAR (contents
);
1094 /* Since we only handle Yes/No dialogs, and we already checked
1095 is_simple_dialog, we don't need to worry about checking contents
1096 to see what type of dialog to use. */
1098 /* Use Unicode if possible, so any language can be displayed. */
1099 if (unicode_message_box
)
1107 char *utf8_text
= SDATA (ENCODE_UTF_8 (temp
));
1108 /* Be pessimistic about the number of characters needed.
1109 Remember characters outside the BMP will take more than
1110 one utf16 word, so we cannot simply use the character
1112 int utf8_len
= strlen (utf8_text
);
1113 text
= SAFE_ALLOCA ((utf8_len
+ 1) * sizeof (WCHAR
));
1114 utf8to16 (utf8_text
, utf8_len
, text
);
1123 title
= L
"Question";
1124 type
|= MB_ICONQUESTION
;
1128 title
= L
"Information";
1129 type
|= MB_ICONINFORMATION
;
1132 answer
= unicode_message_box (FRAME_W32_WINDOW (f
), text
, title
, type
);
1137 const char *text
, *title
;
1139 /* Fall back on ANSI message box, but at least use system
1140 encoding so questions representable by the system codepage
1141 are encoded properly. */
1143 text
= SDATA (ENCODE_SYSTEM (temp
));
1150 type
|= MB_ICONQUESTION
;
1154 title
= "Information";
1155 type
|= MB_ICONINFORMATION
;
1158 answer
= MessageBox (FRAME_W32_WINDOW (f
), text
, title
, type
);
1161 if (answer
== IDYES
)
1162 lispy_answer
= build_string ("Yes");
1163 else if (answer
== IDNO
)
1164 lispy_answer
= build_string ("No");
1166 Fsignal (Qquit
, Qnil
);
1168 for (temp
= XCDR (contents
); CONSP (temp
); temp
= XCDR (temp
))
1170 Lisp_Object item
, name
, value
;
1175 value
= XCDR (item
);
1183 if (!NILP (Fstring_equal (name
, lispy_answer
)))
1188 Fsignal (Qquit
, Qnil
);
1191 #endif /* !HAVE_DIALOGS */
1194 /* UTF8: 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx, 10xxxxxx, 10xxxxxx */
1196 utf8to16 (unsigned char * src
, int len
, WCHAR
* dest
)
1202 *dest
= (WCHAR
) *src
;
1203 dest
++; src
++; len
--;
1205 /* Since we might get >3 byte sequences which we don't handle, ignore the extra parts. */
1206 else if (*src
< 0xC0)
1210 /* 2 char UTF-8 sequence. */
1211 else if (*src
< 0xE0)
1213 *dest
= (WCHAR
) (((*src
& 0x1f) << 6)
1214 | (*(src
+ 1) & 0x3f));
1215 src
+= 2; len
-= 2; dest
++;
1217 else if (*src
< 0xF0)
1219 *dest
= (WCHAR
) (((*src
& 0x0f) << 12)
1220 | ((*(src
+ 1) & 0x3f) << 6)
1221 | (*(src
+ 2) & 0x3f));
1222 src
+= 3; len
-= 3; dest
++;
1224 else /* Not encodable. Insert Unicode Substitution char. */
1226 *dest
= (WCHAR
) 0xfffd;
1227 src
++; len
--; dest
++;
1234 add_menu_item (HMENU menu
, widget_value
*wv
, HMENU item
)
1237 char *out_string
, *p
, *q
;
1239 size_t nlen
, orig_len
;
1242 if (menu_separator_name_p (wv
->name
))
1244 fuFlags
= MF_SEPARATOR
;
1250 fuFlags
= MF_STRING
;
1252 fuFlags
= MF_STRING
| MF_GRAYED
;
1254 if (wv
->key
!= NULL
)
1256 out_string
= SAFE_ALLOCA (strlen (wv
->name
) + strlen (wv
->key
) + 2);
1257 p
= stpcpy (out_string
, wv
->name
);
1258 p
= stpcpy (p
, "\t");
1259 strcpy (p
, wv
->key
);
1262 out_string
= (char *)wv
->name
;
1264 /* Quote any special characters within the menu item's text and
1266 nlen
= orig_len
= strlen (out_string
);
1267 if (unicode_append_menu
)
1269 /* With UTF-8, & cannot be part of a multibyte character. */
1270 for (p
= out_string
; *p
; p
++)
1276 #ifndef NTGUI_UNICODE
1279 /* If encoded with the system codepage, use multibyte string
1280 functions in case of multibyte characters that contain '&'. */
1281 for (p
= out_string
; *p
; p
= _mbsinc (p
))
1283 if (_mbsnextc (p
) == '&')
1287 #endif /* !NTGUI_UNICODE */
1289 if (nlen
> orig_len
)
1292 out_string
= SAFE_ALLOCA (nlen
+ 1);
1296 if (unicode_append_menu
)
1302 #ifndef NTGUI_UNICODE
1305 if (_mbsnextc (p
) == '&')
1314 #endif /* !NTGUI_UNICODE */
1321 else if (wv
->title
|| wv
->call_data
== 0)
1323 /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since
1324 we can't deallocate the memory otherwise. */
1325 if (get_menu_item_info
)
1327 out_string
= (char *) local_alloc (strlen (wv
->name
) + 1);
1328 strcpy (out_string
, wv
->name
);
1330 DebPrint ("Menu: allocating %ld for owner-draw", out_string
);
1332 fuFlags
= MF_OWNERDRAW
| MF_DISABLED
;
1335 fuFlags
= MF_DISABLED
;
1338 /* Draw radio buttons and tickboxes. */
1339 else if (wv
->selected
&& (wv
->button_type
== BUTTON_TYPE_TOGGLE
||
1340 wv
->button_type
== BUTTON_TYPE_RADIO
))
1341 fuFlags
|= MF_CHECKED
;
1343 fuFlags
|= MF_UNCHECKED
;
1346 if (unicode_append_menu
&& out_string
)
1348 /* Convert out_string from UTF-8 to UTF-16-LE. */
1349 int utf8_len
= strlen (out_string
);
1350 WCHAR
* utf16_string
;
1351 if (fuFlags
& MF_OWNERDRAW
)
1352 utf16_string
= local_alloc ((utf8_len
+ 1) * sizeof (WCHAR
));
1354 utf16_string
= SAFE_ALLOCA ((utf8_len
+ 1) * sizeof (WCHAR
));
1356 utf8to16 (out_string
, utf8_len
, utf16_string
);
1357 return_value
= unicode_append_menu (menu
, fuFlags
,
1358 item
!= NULL
? (UINT_PTR
) item
1359 : (UINT_PTR
) wv
->call_data
,
1362 #ifndef NTGUI_UNICODE /* Fallback does not apply when always UNICODE */
1365 /* On W9x/ME, Unicode menus are not supported, though AppendMenuW
1366 apparently does exist at least in some cases and appears to be
1367 stubbed out to do nothing. out_string is UTF-8, but since
1368 our standard menus are in English and this is only going to
1369 happen the first time a menu is used, the encoding is
1370 of minor importance compared with menus not working at all. */
1372 AppendMenu (menu
, fuFlags
,
1373 item
!= NULL
? (UINT_PTR
) item
: (UINT_PTR
) wv
->call_data
,
1375 /* Don't use Unicode menus in future, unless this is Windows
1376 NT or later, where a failure of AppendMenuW does NOT mean
1377 Unicode menus are unsupported. */
1378 if (osinfo_cache
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
)
1379 unicode_append_menu
= NULL
;
1381 #endif /* NTGUI_UNICODE */
1383 if (unicode_append_menu
&& (fuFlags
& MF_OWNERDRAW
))
1384 local_free (out_string
);
1391 item
!= NULL
? (UINT_PTR
) item
: (UINT_PTR
) wv
->call_data
,
1395 /* This must be done after the menu item is created. */
1396 if (!wv
->title
&& wv
->call_data
!= 0)
1398 if (set_menu_item_info
)
1401 memset (&info
, 0, sizeof (info
));
1402 info
.cbSize
= sizeof (info
);
1403 info
.fMask
= MIIM_DATA
;
1405 /* Set help string for menu item. Leave it as a Lisp_Object
1406 until it is ready to be displayed, since GC can happen while
1407 menus are active. */
1408 if (!NILP (wv
->help
))
1410 /* As of Jul-2012, w32api headers say that dwItemData
1411 has DWORD type, but that's a bug: it should actually
1412 be ULONG_PTR, which is correct for 32-bit and 64-bit
1413 Windows alike. MSVC headers get it right; hopefully,
1414 MinGW headers will, too. */
1415 info
.dwItemData
= (ULONG_PTR
) XLI (wv
->help
);
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
,
1433 return return_value
;
1436 /* Construct native Windows menu(bar) based on widget_value tree. */
1438 fill_in_menu (HMENU menu
, widget_value
*wv
)
1440 for ( ; wv
!= NULL
; wv
= wv
->next
)
1444 HMENU sub_menu
= CreatePopupMenu ();
1446 if (sub_menu
== NULL
)
1449 if (!fill_in_menu (sub_menu
, wv
->contents
) ||
1450 !add_menu_item (menu
, wv
, sub_menu
))
1452 DestroyMenu (sub_menu
);
1458 if (!add_menu_item (menu
, wv
, NULL
))
1465 /* Display help string for currently pointed to menu item. Not
1466 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
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 is used
1477 to navigate the menus, since tooltips are distracting if they pop
1479 if (flags
& MF_OWNERDRAW
|| flags
& MF_POPUP
1480 || !(flags
& MF_MOUSESELECT
))
1486 memset (&info
, 0, sizeof (info
));
1487 info
.cbSize
= sizeof (info
);
1488 info
.fMask
= MIIM_DATA
;
1489 get_menu_item_info (menu
, item
, FALSE
, &info
);
1491 help
= info
.dwItemData
? XIL (info
.dwItemData
) : Qnil
;
1494 /* Store the help echo in the keyboard buffer as the X toolkit
1495 version does, rather than directly showing it. This seems to
1496 solve the GC problems that were present when we based the
1497 Windows code on the non-toolkit version. */
1500 XSETFRAME (frame
, f
);
1501 kbd_buffer_store_help_event (frame
, help
);
1504 /* X version has a loop through frames here, which doesn't
1505 appear to do anything, unless it has some side effect. */
1506 show_help_echo (help
, Qnil
, Qnil
, Qnil
);
1510 /* Free memory used by owner-drawn strings. */
1512 w32_free_submenu_strings (HMENU menu
)
1514 int i
, num
= GetMenuItemCount (menu
);
1515 for (i
= 0; i
< num
; i
++)
1518 memset (&info
, 0, sizeof (info
));
1519 info
.cbSize
= sizeof (info
);
1520 info
.fMask
= MIIM_DATA
| MIIM_TYPE
| MIIM_SUBMENU
;
1522 get_menu_item_info (menu
, i
, TRUE
, &info
);
1524 /* Owner-drawn names are held in dwItemData. */
1525 if ((info
.fType
& MF_OWNERDRAW
) && info
.dwItemData
)
1528 DebPrint ("Menu: freeing %ld for owner-draw", info
.dwItemData
);
1530 local_free (info
.dwItemData
);
1533 /* Recurse down submenus. */
1535 w32_free_submenu_strings (info
.hSubMenu
);
1540 w32_free_menu_strings (HWND hwnd
)
1542 HMENU menu
= current_popup_menu
;
1544 if (get_menu_item_info
)
1546 /* If there is no popup menu active, free the strings from the frame's
1549 menu
= GetMenu (hwnd
);
1552 w32_free_submenu_strings (menu
);
1555 current_popup_menu
= NULL
;
1558 /* The following is used by delayed window autoselection. */
1560 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p
, Smenu_or_popup_active_p
, 0, 0, 0,
1561 doc
: /* Return t if a menu or popup dialog is active on selected frame. */)
1565 f
= SELECTED_FRAME ();
1566 return (f
->output_data
.w32
->menubar_active
> 0) ? Qt
: Qnil
;
1570 syms_of_w32menu (void)
1572 globals_of_w32menu ();
1574 current_popup_menu
= NULL
;
1576 DEFSYM (Qdebug_on_next_call
, "debug-on-next-call");
1577 DEFSYM (Qunsupported__w32_dialog
, "unsupported--w32-dialog");
1579 defsubr (&Smenu_or_popup_active_p
);
1583 globals_of_w32menu is used to initialize those global variables that
1584 must always be initialized on startup even when the global variable
1585 initialized is non zero (see the function main in emacs.c).
1586 globals_of_w32menu is called from syms_of_w32menu when the global
1587 variable initialized is 0 and directly from main when initialized
1591 globals_of_w32menu (void)
1593 #ifndef NTGUI_UNICODE
1594 /* See if Get/SetMenuItemInfo functions are available. */
1595 HMODULE user32
= GetModuleHandle ("user32.dll");
1596 get_menu_item_info
= (GetMenuItemInfoA_Proc
) GetProcAddress (user32
, "GetMenuItemInfoA");
1597 set_menu_item_info
= (SetMenuItemInfoA_Proc
) GetProcAddress (user32
, "SetMenuItemInfoA");
1598 unicode_append_menu
= (AppendMenuW_Proc
) GetProcAddress (user32
, "AppendMenuW");
1599 unicode_message_box
= (MessageBoxW_Proc
) GetProcAddress (user32
, "MessageBoxW");
1600 #endif /* !NTGUI_UNICODE */