1 /* Menu support for GNU Emacs on the Microsoft Windows API.
2 Copyright (C) 1986, 1988, 1993-1994, 1996, 1998-1999, 2001-2014 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 Lisp_Object Qdebug_on_next_call
, Qunsupported__w32_dialog
;
103 void set_frame_menubar (struct frame
*, bool, bool);
106 static Lisp_Object
w32_dialog_show (struct frame
*, Lisp_Object
, Lisp_Object
, char **);
108 static int is_simple_dialog (Lisp_Object
);
109 static Lisp_Object
simple_dialog_show (struct frame
*, Lisp_Object
, Lisp_Object
);
112 static void utf8to16 (unsigned char *, int, WCHAR
*);
113 static int fill_in_menu (HMENU
, widget_value
*);
115 void w32_free_menu_strings (HWND
);
118 w32_popup_dialog (struct frame
*f
, Lisp_Object header
, Lisp_Object contents
)
121 check_window_system (f
);
125 /* Handle simple Yes/No choices as MessageBox popups. */
126 if (is_simple_dialog (contents
))
127 return simple_dialog_show (f
, contents
, header
);
129 return Qunsupported__w32_dialog
;
130 #else /* HAVE_DIALOGS */
134 Lisp_Object selection
;
136 /* Decode the dialog items from what was specified. */
137 title
= Fcar (contents
);
138 CHECK_STRING (title
);
140 list_of_panes (Fcons (contents
, Qnil
));
142 /* Display them in a dialog box. */
144 selection
= w32_dialog_show (f
, title
, header
, &error_name
);
147 discard_menu_items ();
148 FRAME_DISPLAY_INFO (f
)->grabbed
= 0;
150 if (error_name
) error (error_name
);
153 #endif /* HAVE_DIALOGS */
156 /* Activate the menu bar of frame F.
157 This is called from keyboard.c when it gets the
158 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
160 To activate the menu bar, we signal to the input thread that it can
161 return from the WM_INITMENU message, allowing the normal Windows
162 processing of the menus.
164 But first we recompute the menu bar contents (the whole tree).
166 This way we can safely execute Lisp code. */
169 x_activate_menubar (struct frame
*f
)
171 set_frame_menubar (f
, 0, 1);
173 /* Lock out further menubar changes while active. */
174 f
->output_data
.w32
->menubar_active
= 1;
176 /* Signal input thread to return from WM_INITMENU. */
177 complete_deferred_msg (FRAME_W32_WINDOW (f
), WM_INITMENU
, 0);
180 /* This callback is called from the menu bar pulldown menu
181 when the user makes a selection.
182 Figure out what the user chose
183 and put the appropriate events into the keyboard buffer. */
186 menubar_selection_callback (struct frame
*f
, void * client_data
)
188 Lisp_Object prefix
, entry
;
190 Lisp_Object
*subprefix_stack
;
191 int submenu_depth
= 0;
197 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* word_size
);
198 vector
= f
->menu_bar_vector
;
201 while (i
< f
->menu_bar_items_used
)
203 if (EQ (AREF (vector
, i
), Qnil
))
205 subprefix_stack
[submenu_depth
++] = prefix
;
209 else if (EQ (AREF (vector
, i
), Qlambda
))
211 prefix
= subprefix_stack
[--submenu_depth
];
214 else if (EQ (AREF (vector
, i
), Qt
))
216 prefix
= AREF (vector
, i
+ MENU_ITEMS_PANE_PREFIX
);
217 i
+= MENU_ITEMS_PANE_LENGTH
;
221 entry
= AREF (vector
, i
+ MENU_ITEMS_ITEM_VALUE
);
222 /* The EMACS_INT cast avoids a warning. There's no problem
223 as long as pointers have enough bits to hold small integers. */
224 if ((int) (EMACS_INT
) client_data
== i
)
227 struct input_event buf
;
231 XSETFRAME (frame
, f
);
232 buf
.kind
= MENU_BAR_EVENT
;
233 buf
.frame_or_window
= frame
;
235 kbd_buffer_store_event (&buf
);
237 for (j
= 0; j
< submenu_depth
; j
++)
238 if (!NILP (subprefix_stack
[j
]))
240 buf
.kind
= MENU_BAR_EVENT
;
241 buf
.frame_or_window
= frame
;
242 buf
.arg
= subprefix_stack
[j
];
243 kbd_buffer_store_event (&buf
);
248 buf
.kind
= MENU_BAR_EVENT
;
249 buf
.frame_or_window
= frame
;
251 kbd_buffer_store_event (&buf
);
254 buf
.kind
= MENU_BAR_EVENT
;
255 buf
.frame_or_window
= frame
;
257 /* Free memory used by owner-drawn and help-echo strings. */
258 w32_free_menu_strings (FRAME_W32_WINDOW (f
));
259 kbd_buffer_store_event (&buf
);
261 f
->output_data
.w32
->menubar_active
= 0;
264 i
+= MENU_ITEMS_ITEM_LENGTH
;
267 /* Free memory used by owner-drawn and help-echo strings. */
268 w32_free_menu_strings (FRAME_W32_WINDOW (f
));
269 f
->output_data
.w32
->menubar_active
= 0;
273 /* Set the contents of the menubar widgets of frame F.
274 The argument FIRST_TIME is currently ignored;
275 it is set the first time this is called, from initialize_frame_menubar. */
278 set_frame_menubar (struct frame
*f
, bool first_time
, bool deep_p
)
280 HMENU menubar_widget
= f
->output_data
.w32
->menubar_widget
;
282 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
284 int *submenu_start
, *submenu_end
;
285 int *submenu_top_level_items
, *submenu_n_panes
;
287 /* We must not change the menubar when actually in use. */
288 if (f
->output_data
.w32
->menubar_active
)
291 XSETFRAME (Vmenu_updating_frame
, f
);
293 if (! menubar_widget
)
298 /* Make a widget-value tree representing the entire menu trees. */
300 struct buffer
*prev
= current_buffer
;
302 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
303 int previous_menu_items_used
= f
->menu_bar_items_used
;
304 Lisp_Object
*previous_items
305 = (Lisp_Object
*) alloca (previous_menu_items_used
308 /* If we are making a new widget, its contents are empty,
309 do always reinitialize them. */
310 if (! menubar_widget
)
311 previous_menu_items_used
= 0;
313 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->contents
;
314 specbind (Qinhibit_quit
, Qt
);
315 /* Don't let the debugger step into this code
316 because it is not reentrant. */
317 specbind (Qdebug_on_next_call
, Qnil
);
319 record_unwind_save_match_data ();
321 if (NILP (Voverriding_local_map_menu_flag
))
323 specbind (Qoverriding_terminal_local_map
, Qnil
);
324 specbind (Qoverriding_local_map
, Qnil
);
327 set_buffer_internal_1 (XBUFFER (buffer
));
330 safe_run_hooks (Qactivate_menubar_hook
);
331 safe_run_hooks (Qmenu_bar_update_hook
);
332 fset_menu_bar_items (f
, menu_bar_items (FRAME_MENU_BAR_ITEMS (f
)));
334 items
= FRAME_MENU_BAR_ITEMS (f
);
336 /* Save the frame's previous menu bar contents data. */
337 if (previous_menu_items_used
)
338 memcpy (previous_items
, XVECTOR (f
->menu_bar_vector
)->contents
,
339 previous_menu_items_used
* word_size
);
341 /* Fill in menu_items with the current menu bar contents.
342 This can evaluate Lisp code. */
345 menu_items
= f
->menu_bar_vector
;
346 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
347 submenu_start
= (int *) alloca (ASIZE (items
) * sizeof (int));
348 submenu_end
= (int *) alloca (ASIZE (items
) * sizeof (int));
349 submenu_n_panes
= (int *) alloca (ASIZE (items
) * sizeof (int));
350 submenu_top_level_items
= (int *) alloca (ASIZE (items
) * sizeof (int));
352 for (i
= 0; i
< ASIZE (items
); i
+= 4)
354 Lisp_Object key
, string
, maps
;
358 key
= AREF (items
, i
);
359 string
= AREF (items
, i
+ 1);
360 maps
= AREF (items
, i
+ 2);
364 submenu_start
[i
] = menu_items_used
;
366 menu_items_n_panes
= 0;
367 submenu_top_level_items
[i
]
368 = parse_single_submenu (key
, string
, maps
);
369 submenu_n_panes
[i
] = menu_items_n_panes
;
371 submenu_end
[i
] = menu_items_used
;
374 finish_menu_items ();
376 /* Convert menu_items into widget_value trees
377 to display the menu. This cannot evaluate Lisp code. */
379 wv
= make_widget_value ("menubar", NULL
, true, Qnil
);
380 wv
->button_type
= BUTTON_TYPE_NONE
;
383 for (i
= 0; i
< last_i
; i
+= 4)
385 menu_items_n_panes
= submenu_n_panes
[i
];
386 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
387 submenu_top_level_items
[i
]);
391 first_wv
->contents
= wv
;
392 /* Don't set wv->name here; GC during the loop might relocate it. */
394 wv
->button_type
= BUTTON_TYPE_NONE
;
398 set_buffer_internal_1 (prev
);
400 /* If there has been no change in the Lisp-level contents
401 of the menu bar, skip redisplaying it. Just exit. */
403 for (i
= 0; i
< previous_menu_items_used
; i
++)
404 if (menu_items_used
== i
405 || (!EQ (previous_items
[i
], AREF (menu_items
, i
))))
407 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
409 free_menubar_widget_value_tree (first_wv
);
410 discard_menu_items ();
411 unbind_to (specpdl_count
, Qnil
);
415 fset_menu_bar_vector (f
, menu_items
);
416 f
->menu_bar_items_used
= menu_items_used
;
418 /* This undoes save_menu_items. */
419 unbind_to (specpdl_count
, Qnil
);
421 /* Now GC cannot happen during the lifetime of the widget_value,
422 so it's safe to store data from a Lisp_String, as long as
423 local copies are made when the actual menu is created.
424 Windows takes care of this for normal string items, but
425 not for owner-drawn items or additional item-info. */
426 wv
= first_wv
->contents
;
427 for (i
= 0; i
< ASIZE (items
); i
+= 4)
430 string
= AREF (items
, i
+ 1);
433 wv
->name
= SSDATA (string
);
434 update_submenu_strings (wv
->contents
);
440 /* Make a widget-value tree containing
441 just the top level menu bar strings. */
443 wv
= make_widget_value ("menubar", NULL
, true, Qnil
);
444 wv
->button_type
= BUTTON_TYPE_NONE
;
447 items
= FRAME_MENU_BAR_ITEMS (f
);
448 for (i
= 0; i
< ASIZE (items
); i
+= 4)
452 string
= AREF (items
, i
+ 1);
456 wv
= make_widget_value (SSDATA (string
), NULL
, true, Qnil
);
457 wv
->button_type
= BUTTON_TYPE_NONE
;
458 /* This prevents lwlib from assuming this
459 menu item is really supposed to be empty. */
460 /* The EMACS_INT cast avoids a warning.
461 This value just has to be different from small integers. */
462 wv
->call_data
= (void *) (EMACS_INT
) (-1);
467 first_wv
->contents
= wv
;
471 /* Forget what we thought we knew about what is in the
472 detailed contents of the menu bar menus.
473 Changing the top level always destroys the contents. */
474 f
->menu_bar_items_used
= 0;
477 /* Create or update the menu bar widget. */
483 /* Empty current menubar, rather than creating a fresh one. */
484 while (DeleteMenu (menubar_widget
, 0, MF_BYPOSITION
))
489 menubar_widget
= CreateMenu ();
491 fill_in_menu (menubar_widget
, first_wv
->contents
);
493 free_menubar_widget_value_tree (first_wv
);
496 HMENU old_widget
= f
->output_data
.w32
->menubar_widget
;
498 f
->output_data
.w32
->menubar_widget
= menubar_widget
;
499 SetMenu (FRAME_W32_WINDOW (f
), f
->output_data
.w32
->menubar_widget
);
500 /* Causes flicker when menu bar is updated
501 DrawMenuBar (FRAME_W32_WINDOW (f)); */
503 /* Force the window size to be recomputed so that the frame's text
504 area remains the same, if menubar has just been created. */
505 if (old_widget
== NULL
)
506 adjust_frame_size (f
, FRAME_TEXT_WIDTH (f
),
507 FRAME_TEXT_HEIGHT (f
), 2, 0, Qmenu_bar_lines
);
513 /* Called from Fx_create_frame to create the initial menubar of a frame
514 before it is mapped, so that the window is mapped with the menubar already
515 there instead of us tacking it on later and thrashing the window after it
519 initialize_frame_menubar (struct frame
*f
)
521 /* This function is called before the first chance to redisplay
522 the frame. It has to be, so the frame will have the right size. */
523 fset_menu_bar_items (f
, menu_bar_items (FRAME_MENU_BAR_ITEMS (f
)));
524 set_frame_menubar (f
, 1, 1);
527 /* Get rid of the menu bar of frame F, and free its storage.
528 This is used when deleting a frame, and when turning off the menu bar. */
531 free_frame_menubar (struct frame
*f
)
536 HMENU old
= GetMenu (FRAME_W32_WINDOW (f
));
537 SetMenu (FRAME_W32_WINDOW (f
), NULL
);
538 f
->output_data
.w32
->menubar_widget
= NULL
;
546 /* w32_menu_show actually displays a menu using the panes and items in
547 menu_items and returns the value selected from it; we assume input
548 is blocked by the caller. */
550 /* F is the frame the menu is for.
551 X and Y are the frame-relative specified position,
552 relative to the inside upper left corner of the frame F.
553 Bitfield MENUFLAGS bits are:
554 MENU_FOR_CLICK is set if this menu was invoked for a mouse click.
555 MENU_KEYMAPS is set if this menu was specified with keymaps;
556 in that case, we return a list containing the chosen item's value
557 and perhaps also the pane's prefix.
558 TITLE is the specified menu title.
559 ERROR is a place to store an error message string in case of failure.
560 (We return nil on failure, but the value doesn't actually matter.) */
563 w32_menu_show (struct frame
*f
, int x
, int y
, int menuflags
,
564 Lisp_Object title
, const char **error
)
567 int menu_item_selection
;
570 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
571 widget_value
**submenu_stack
572 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
573 Lisp_Object
*subprefix_stack
574 = (Lisp_Object
*) alloca (menu_items_used
* word_size
);
575 int submenu_depth
= 0;
580 if (menu_items_n_panes
== 0)
583 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
585 *error
= "Empty menu";
591 /* Create a tree of widget_value objects
592 representing the panes and their items. */
593 wv
= make_widget_value ("menu", NULL
, true, Qnil
);
594 wv
->button_type
= BUTTON_TYPE_NONE
;
598 /* Loop over all panes and items, filling in the tree. */
600 while (i
< menu_items_used
)
602 if (EQ (AREF (menu_items
, i
), Qnil
))
604 submenu_stack
[submenu_depth
++] = save_wv
;
610 else if (EQ (AREF (menu_items
, i
), Qlambda
))
613 save_wv
= submenu_stack
[--submenu_depth
];
617 else if (EQ (AREF (menu_items
, i
), Qt
)
618 && submenu_depth
!= 0)
619 i
+= MENU_ITEMS_PANE_LENGTH
;
620 /* Ignore a nil in the item list.
621 It's meaningful only for dialog boxes. */
622 else if (EQ (AREF (menu_items
, i
), Qquote
))
624 else if (EQ (AREF (menu_items
, i
), Qt
))
626 /* Create a new pane. */
627 Lisp_Object pane_name
, prefix
;
628 const char *pane_string
;
629 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
630 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
632 if (STRINGP (pane_name
))
634 if (unicode_append_menu
)
635 pane_name
= ENCODE_UTF_8 (pane_name
);
636 else if (STRING_MULTIBYTE (pane_name
))
637 pane_name
= ENCODE_SYSTEM (pane_name
);
639 ASET (menu_items
, i
+ MENU_ITEMS_PANE_NAME
, pane_name
);
642 pane_string
= (NILP (pane_name
)
643 ? "" : SSDATA (pane_name
));
644 /* If there is just one top-level pane, put all its items directly
645 under the top-level menu. */
646 if (menu_items_n_panes
== 1)
649 /* If the pane has a meaningful name,
650 make the pane a top-level menu item
651 with its items as a submenu beneath it. */
652 if (!(menuflags
& MENU_KEYMAPS
) && strcmp (pane_string
, ""))
654 wv
= make_widget_value (pane_string
, NULL
, true, Qnil
);
658 first_wv
->contents
= wv
;
659 if ((menuflags
& MENU_KEYMAPS
) && !NILP (prefix
))
661 wv
->button_type
= BUTTON_TYPE_NONE
;
671 i
+= MENU_ITEMS_PANE_LENGTH
;
675 /* Create a new item within current pane. */
676 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
678 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
679 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
680 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
681 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
682 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
683 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
684 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
686 if (STRINGP (item_name
))
688 if (unicode_append_menu
)
689 item_name
= ENCODE_UTF_8 (item_name
);
690 else if (STRING_MULTIBYTE (item_name
))
691 item_name
= ENCODE_SYSTEM (item_name
);
693 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
, item_name
);
696 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
698 descrip
= ENCODE_SYSTEM (descrip
);
699 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
, descrip
);
702 wv
= make_widget_value (SSDATA (item_name
), NULL
, !NILP (enable
),
703 STRINGP (help
) ? help
: Qnil
);
707 save_wv
->contents
= wv
;
709 wv
->key
= SSDATA (descrip
);
710 /* Use the contents index as call_data, since we are
711 restricted to 16-bits. */
712 wv
->call_data
= !NILP (def
) ? (void *) (EMACS_INT
) i
: 0;
715 wv
->button_type
= BUTTON_TYPE_NONE
;
716 else if (EQ (type
, QCtoggle
))
717 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
718 else if (EQ (type
, QCradio
))
719 wv
->button_type
= BUTTON_TYPE_RADIO
;
723 wv
->selected
= !NILP (selected
);
727 i
+= MENU_ITEMS_ITEM_LENGTH
;
731 /* Deal with the title, if it is non-nil. */
734 widget_value
*wv_title
;
735 widget_value
*wv_sep
= make_widget_value ("--", NULL
, false, Qnil
);
737 /* Maybe replace this separator with a bitmap or owner-draw item
738 so that it looks better. Having two separators looks odd. */
739 wv_sep
->next
= first_wv
->contents
;
741 if (unicode_append_menu
)
742 title
= ENCODE_UTF_8 (title
);
743 else if (STRING_MULTIBYTE (title
))
744 title
= ENCODE_SYSTEM (title
);
746 wv_title
= make_widget_value (SSDATA (title
), NULL
, true, Qnil
);
747 wv_title
->title
= TRUE
;
748 wv_title
->button_type
= BUTTON_TYPE_NONE
;
749 wv_title
->next
= wv_sep
;
750 first_wv
->contents
= wv_title
;
753 /* No selection has been chosen yet. */
754 menu_item_selection
= 0;
756 /* Actually create the menu. */
757 current_popup_menu
= menu
= CreatePopupMenu ();
758 fill_in_menu (menu
, first_wv
->contents
);
760 /* Adjust coordinates to be root-window-relative. */
763 ClientToScreen (FRAME_W32_WINDOW (f
), &pos
);
765 /* Display the menu. */
766 menu_item_selection
= SendMessage (FRAME_W32_WINDOW (f
),
767 WM_EMACS_TRACKPOPUPMENU
,
768 (WPARAM
)menu
, (LPARAM
)&pos
);
770 /* Clean up extraneous mouse events which might have been generated
772 discard_mouse_events ();
773 FRAME_DISPLAY_INFO (f
)->grabbed
= 0;
775 /* Free the widget_value objects we used to specify the contents. */
776 free_menubar_widget_value_tree (first_wv
);
780 /* Free the owner-drawn and help-echo menu strings. */
781 w32_free_menu_strings (FRAME_W32_WINDOW (f
));
782 f
->output_data
.w32
->menubar_active
= 0;
784 /* Find the selected item, and its pane, to return
786 if (menu_item_selection
!= 0)
788 Lisp_Object prefix
, entry
;
790 prefix
= entry
= Qnil
;
792 while (i
< menu_items_used
)
794 if (EQ (AREF (menu_items
, i
), Qnil
))
796 subprefix_stack
[submenu_depth
++] = prefix
;
800 else if (EQ (AREF (menu_items
, i
), Qlambda
))
802 prefix
= subprefix_stack
[--submenu_depth
];
805 else if (EQ (AREF (menu_items
, i
), Qt
))
807 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
808 i
+= MENU_ITEMS_PANE_LENGTH
;
810 /* Ignore a nil in the item list.
811 It's meaningful only for dialog boxes. */
812 else if (EQ (AREF (menu_items
, i
), Qquote
))
816 entry
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
817 if (menu_item_selection
== i
)
819 if (menuflags
& MENU_KEYMAPS
)
823 entry
= Fcons (entry
, Qnil
);
825 entry
= Fcons (prefix
, entry
);
826 for (j
= submenu_depth
- 1; j
>= 0; j
--)
827 if (!NILP (subprefix_stack
[j
]))
828 entry
= Fcons (subprefix_stack
[j
], entry
);
833 i
+= MENU_ITEMS_ITEM_LENGTH
;
837 else if (!(menuflags
& MENU_FOR_CLICK
))
840 /* Make "Cancel" equivalent to C-g. */
841 Fsignal (Qquit
, Qnil
);
850 /* TODO: On Windows, there are two ways of defining a dialog.
852 1. Create a predefined dialog resource and include it in nt/emacs.rc.
853 Using this method, we could then set the titles and make unneeded
854 buttons invisible before displaying the dialog. Everything would
855 be a fixed size though, so there is a risk that text does not
857 2. Create the dialog template in memory on the fly. This allows us
858 to size the dialog and buttons dynamically, probably giving more
859 natural looking results for dialogs with few buttons, and eliminating
860 the problem of text overflowing the buttons. But the API for this is
861 quite complex - structures have to be allocated in particular ways,
862 text content is tacked onto the end of structures in variable length
863 arrays with further structures tacked on after these, there are
864 certain alignment requirements for all this, and we have to
865 measure all the text and convert to "dialog coordinates" to figure
866 out how big to make everything.
868 For now, we'll just stick with menus for dialogs that are more
869 complicated than simple yes/no type questions for which we can use
870 the MessageBox function.
873 static char * button_names
[] = {
874 "button1", "button2", "button3", "button4", "button5",
875 "button6", "button7", "button8", "button9", "button10" };
878 w32_dialog_show (struct frame
*f
, Lisp_Object title
,
879 Lisp_Object header
, char **error
)
881 int i
, nb_buttons
= 0;
883 int menu_item_selection
;
885 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
887 /* Number of elements seen so far, before boundary. */
889 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
890 int boundary_seen
= 0;
894 if (menu_items_n_panes
> 1)
896 *error
= "Multiple panes in dialog box";
900 /* Create a tree of widget_value objects
901 representing the text label and buttons. */
903 Lisp_Object pane_name
;
905 pane_name
= AREF (menu_items
, MENU_ITEMS_PANE_NAME
);
906 pane_string
= (NILP (pane_name
)
907 ? "" : SSDATA (pane_name
));
908 prev_wv
= make_widget_value ("message", pane_string
, true, Qnil
);
911 /* Loop over all panes and items, filling in the tree. */
912 i
= MENU_ITEMS_PANE_LENGTH
;
913 while (i
< menu_items_used
)
916 /* Create a new item within current pane. */
917 Lisp_Object item_name
, enable
, descrip
, help
;
919 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
920 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
921 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
922 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
924 if (NILP (item_name
))
926 free_menubar_widget_value_tree (first_wv
);
927 *error
= "Submenu in dialog items";
930 if (EQ (item_name
, Qquote
))
932 /* This is the boundary between left-side elts
933 and right-side elts. Stop incrementing right_count. */
940 free_menubar_widget_value_tree (first_wv
);
941 *error
= "Too many dialog items";
945 wv
= make_widget_value (button_names
[nb_buttons
],
947 !NILP (enable
), Qnil
);
950 wv
->key
= SSDATA (descrip
);
951 wv
->call_data
= aref_addr (menu_items
, i
);
958 i
+= MENU_ITEMS_ITEM_LENGTH
;
961 /* If the boundary was not specified,
962 by default put half on the left and half on the right. */
964 left_count
= nb_buttons
- nb_buttons
/ 2;
966 wv
= make_widget_value (dialog_name
, NULL
, false, Qnil
);
968 /* Frame title: 'Q' = Question, 'I' = Information.
969 Can also have 'E' = Error if, one day, we want
970 a popup for errors. */
972 dialog_name
[0] = 'Q';
974 dialog_name
[0] = 'I';
976 /* Dialog boxes use a really stupid name encoding
977 which specifies how many buttons to use
978 and how many buttons are on the right. */
979 dialog_name
[1] = '0' + nb_buttons
;
980 dialog_name
[2] = 'B';
981 dialog_name
[3] = 'R';
982 /* Number of buttons to put on the right. */
983 dialog_name
[4] = '0' + nb_buttons
- left_count
;
985 wv
->contents
= first_wv
;
989 /* Actually create the dialog. */
990 dialog_id
= widget_id_tick
++;
991 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
992 f
->output_data
.w32
->widget
, 1, 0,
993 dialog_selection_callback
, 0);
994 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, TRUE
);
996 /* Free the widget_value objects we used to specify the contents. */
997 free_menubar_widget_value_tree (first_wv
);
999 /* No selection has been chosen yet. */
1000 menu_item_selection
= 0;
1002 /* Display the menu. */
1003 lw_pop_up_all_widgets (dialog_id
);
1005 /* Process events that apply to the menu. */
1006 popup_get_selection ((XEvent
*) 0, FRAME_DISPLAY_INFO (f
), dialog_id
);
1008 lw_destroy_all_widgets (dialog_id
);
1010 /* Find the selected item, and its pane, to return
1011 the proper value. */
1012 if (menu_item_selection
!= 0)
1015 while (i
< menu_items_used
)
1019 if (EQ (AREF (menu_items
, i
), Qt
))
1020 i
+= MENU_ITEMS_PANE_LENGTH
;
1023 entry
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
1024 if (menu_item_selection
== i
)
1026 i
+= MENU_ITEMS_ITEM_LENGTH
;
1031 /* Make "Cancel" equivalent to C-g. */
1032 Fsignal (Qquit
, Qnil
);
1036 #else /* !HAVE_DIALOGS */
1038 /* Currently we only handle Yes No dialogs (y-or-n-p and yes-or-no-p) as
1039 simple dialogs. We could handle a few more, but I'm not aware of
1040 anywhere in Emacs that uses the other specific dialog choices that
1041 MessageBox provides. */
1044 is_simple_dialog (Lisp_Object contents
)
1046 Lisp_Object options
;
1047 Lisp_Object name
, yes
, no
, other
;
1049 if (!CONSP (contents
))
1051 options
= XCDR (contents
);
1053 yes
= build_string ("Yes");
1054 no
= build_string ("No");
1056 if (!CONSP (options
))
1059 name
= XCAR (options
);
1064 if (!NILP (Fstring_equal (name
, yes
)))
1066 else if (!NILP (Fstring_equal (name
, no
)))
1071 options
= XCDR (options
);
1072 if (!CONSP (options
))
1075 name
= XCAR (options
);
1079 if (NILP (Fstring_equal (name
, other
)))
1082 /* Check there are no more options. */
1083 options
= XCDR (options
);
1084 return !(CONSP (options
));
1088 simple_dialog_show (struct frame
*f
, Lisp_Object contents
, Lisp_Object header
)
1092 Lisp_Object lispy_answer
= Qnil
, temp
= XCAR (contents
);
1096 /* Since we only handle Yes/No dialogs, and we already checked
1097 is_simple_dialog, we don't need to worry about checking contents
1098 to see what type of dialog to use. */
1100 /* Use Unicode if possible, so any language can be displayed. */
1101 if (unicode_message_box
)
1109 char *utf8_text
= SDATA (ENCODE_UTF_8 (temp
));
1110 /* Be pessimistic about the number of characters needed.
1111 Remember characters outside the BMP will take more than
1112 one utf16 word, so we cannot simply use the character
1114 int utf8_len
= strlen (utf8_text
);
1115 text
= SAFE_ALLOCA ((utf8_len
+ 1) * sizeof (WCHAR
));
1116 utf8to16 (utf8_text
, utf8_len
, text
);
1125 title
= L
"Question";
1126 type
|= MB_ICONQUESTION
;
1130 title
= L
"Information";
1131 type
|= MB_ICONINFORMATION
;
1134 answer
= unicode_message_box (FRAME_W32_WINDOW (f
), text
, title
, type
);
1139 const char *text
, *title
;
1141 /* Fall back on ANSI message box, but at least use system
1142 encoding so questions representable by the system codepage
1143 are encoded properly. */
1145 text
= SDATA (ENCODE_SYSTEM (temp
));
1152 type
|= MB_ICONQUESTION
;
1156 title
= "Information";
1157 type
|= MB_ICONINFORMATION
;
1160 answer
= MessageBox (FRAME_W32_WINDOW (f
), text
, title
, type
);
1163 if (answer
== IDYES
)
1164 lispy_answer
= build_string ("Yes");
1165 else if (answer
== IDNO
)
1166 lispy_answer
= build_string ("No");
1168 Fsignal (Qquit
, Qnil
);
1170 for (temp
= XCDR (contents
); CONSP (temp
); temp
= XCDR (temp
))
1172 Lisp_Object item
, name
, value
;
1177 value
= XCDR (item
);
1185 if (!NILP (Fstring_equal (name
, lispy_answer
)))
1190 Fsignal (Qquit
, Qnil
);
1193 #endif /* !HAVE_DIALOGS */
1196 /* UTF8: 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx, 10xxxxxx, 10xxxxxx */
1198 utf8to16 (unsigned char * src
, int len
, WCHAR
* dest
)
1204 *dest
= (WCHAR
) *src
;
1205 dest
++; src
++; len
--;
1207 /* Since we might get >3 byte sequences which we don't handle, ignore the extra parts. */
1208 else if (*src
< 0xC0)
1212 /* 2 char UTF-8 sequence. */
1213 else if (*src
< 0xE0)
1215 *dest
= (WCHAR
) (((*src
& 0x1f) << 6)
1216 | (*(src
+ 1) & 0x3f));
1217 src
+= 2; len
-= 2; dest
++;
1219 else if (*src
< 0xF0)
1221 *dest
= (WCHAR
) (((*src
& 0x0f) << 12)
1222 | ((*(src
+ 1) & 0x3f) << 6)
1223 | (*(src
+ 2) & 0x3f));
1224 src
+= 3; len
-= 3; dest
++;
1226 else /* Not encodable. Insert Unicode Substitution char. */
1228 *dest
= (WCHAR
) 0xfffd;
1229 src
++; len
--; dest
++;
1236 add_menu_item (HMENU menu
, widget_value
*wv
, HMENU item
)
1239 char *out_string
, *p
, *q
;
1241 size_t nlen
, orig_len
;
1244 if (menu_separator_name_p (wv
->name
))
1246 fuFlags
= MF_SEPARATOR
;
1252 fuFlags
= MF_STRING
;
1254 fuFlags
= MF_STRING
| MF_GRAYED
;
1256 if (wv
->key
!= NULL
)
1258 out_string
= SAFE_ALLOCA (strlen (wv
->name
) + strlen (wv
->key
) + 2);
1259 strcpy (out_string
, wv
->name
);
1260 strcat (out_string
, "\t");
1261 strcat (out_string
, wv
->key
);
1264 out_string
= (char *)wv
->name
;
1266 /* Quote any special characters within the menu item's text and
1268 nlen
= orig_len
= strlen (out_string
);
1269 if (unicode_append_menu
)
1271 /* With UTF-8, & cannot be part of a multibyte character. */
1272 for (p
= out_string
; *p
; p
++)
1278 #ifndef NTGUI_UNICODE
1281 /* If encoded with the system codepage, use multibyte string
1282 functions in case of multibyte characters that contain '&'. */
1283 for (p
= out_string
; *p
; p
= _mbsinc (p
))
1285 if (_mbsnextc (p
) == '&')
1289 #endif /* !NTGUI_UNICODE */
1291 if (nlen
> orig_len
)
1294 out_string
= SAFE_ALLOCA (nlen
+ 1);
1298 if (unicode_append_menu
)
1304 #ifndef NTGUI_UNICODE
1307 if (_mbsnextc (p
) == '&')
1316 #endif /* !NTGUI_UNICODE */
1323 else if (wv
->title
|| wv
->call_data
== 0)
1325 /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since
1326 we can't deallocate the memory otherwise. */
1327 if (get_menu_item_info
)
1329 out_string
= (char *) local_alloc (strlen (wv
->name
) + 1);
1330 strcpy (out_string
, wv
->name
);
1332 DebPrint ("Menu: allocating %ld for owner-draw", out_string
);
1334 fuFlags
= MF_OWNERDRAW
| MF_DISABLED
;
1337 fuFlags
= MF_DISABLED
;
1340 /* Draw radio buttons and tickboxes. */
1341 else if (wv
->selected
&& (wv
->button_type
== BUTTON_TYPE_TOGGLE
||
1342 wv
->button_type
== BUTTON_TYPE_RADIO
))
1343 fuFlags
|= MF_CHECKED
;
1345 fuFlags
|= MF_UNCHECKED
;
1348 if (unicode_append_menu
&& out_string
)
1350 /* Convert out_string from UTF-8 to UTF-16-LE. */
1351 int utf8_len
= strlen (out_string
);
1352 WCHAR
* utf16_string
;
1353 if (fuFlags
& MF_OWNERDRAW
)
1354 utf16_string
= local_alloc ((utf8_len
+ 1) * sizeof (WCHAR
));
1356 utf16_string
= SAFE_ALLOCA ((utf8_len
+ 1) * sizeof (WCHAR
));
1358 utf8to16 (out_string
, utf8_len
, utf16_string
);
1359 return_value
= unicode_append_menu (menu
, fuFlags
,
1360 item
!= NULL
? (UINT_PTR
) item
1361 : (UINT_PTR
) wv
->call_data
,
1364 #ifndef NTGUI_UNICODE /* Fallback does not apply when always UNICODE */
1367 /* On W9x/ME, Unicode menus are not supported, though AppendMenuW
1368 apparently does exist at least in some cases and appears to be
1369 stubbed out to do nothing. out_string is UTF-8, but since
1370 our standard menus are in English and this is only going to
1371 happen the first time a menu is used, the encoding is
1372 of minor importance compared with menus not working at all. */
1374 AppendMenu (menu
, fuFlags
,
1375 item
!= NULL
? (UINT_PTR
) item
: (UINT_PTR
) wv
->call_data
,
1377 /* Don't use Unicode menus in future, unless this is Windows
1378 NT or later, where a failure of AppendMenuW does NOT mean
1379 Unicode menus are unsupported. */
1380 if (osinfo_cache
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
)
1381 unicode_append_menu
= NULL
;
1383 #endif /* NTGUI_UNICODE */
1385 if (unicode_append_menu
&& (fuFlags
& MF_OWNERDRAW
))
1386 local_free (out_string
);
1393 item
!= NULL
? (UINT_PTR
) item
: (UINT_PTR
) wv
->call_data
,
1397 /* This must be done after the menu item is created. */
1398 if (!wv
->title
&& wv
->call_data
!= 0)
1400 if (set_menu_item_info
)
1403 memset (&info
, 0, sizeof (info
));
1404 info
.cbSize
= sizeof (info
);
1405 info
.fMask
= MIIM_DATA
;
1407 /* Set help string for menu item. Leave it as a Lisp_Object
1408 until it is ready to be displayed, since GC can happen while
1409 menus are active. */
1410 if (!NILP (wv
->help
))
1412 /* As of Jul-2012, w32api headers say that dwItemData
1413 has DWORD type, but that's a bug: it should actually
1414 be ULONG_PTR, which is correct for 32-bit and 64-bit
1415 Windows alike. MSVC headers get it right; hopefully,
1416 MinGW headers will, too. */
1417 info
.dwItemData
= (ULONG_PTR
) XLI (wv
->help
);
1419 if (wv
->button_type
== BUTTON_TYPE_RADIO
)
1421 /* CheckMenuRadioItem allows us to differentiate TOGGLE and
1422 RADIO items, but is not available on NT 3.51 and earlier. */
1423 info
.fMask
|= MIIM_TYPE
| MIIM_STATE
;
1424 info
.fType
= MFT_RADIOCHECK
| MFT_STRING
;
1425 info
.dwTypeData
= out_string
;
1426 info
.fState
= wv
->selected
? MFS_CHECKED
: MFS_UNCHECKED
;
1429 set_menu_item_info (menu
,
1430 item
!= NULL
? (UINT_PTR
) item
: (UINT_PTR
) wv
->call_data
,
1435 return return_value
;
1438 /* Construct native Windows menu(bar) based on widget_value tree. */
1440 fill_in_menu (HMENU menu
, widget_value
*wv
)
1442 for ( ; wv
!= NULL
; wv
= wv
->next
)
1446 HMENU sub_menu
= CreatePopupMenu ();
1448 if (sub_menu
== NULL
)
1451 if (!fill_in_menu (sub_menu
, wv
->contents
) ||
1452 !add_menu_item (menu
, wv
, sub_menu
))
1454 DestroyMenu (sub_menu
);
1460 if (!add_menu_item (menu
, wv
, NULL
))
1467 /* Display help string for currently pointed to menu item. Not
1468 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
1471 w32_menu_display_help (HWND owner
, HMENU menu
, UINT item
, UINT flags
)
1473 if (get_menu_item_info
)
1475 struct frame
*f
= x_window_to_frame (&one_w32_display_info
, owner
);
1476 Lisp_Object frame
, help
;
1478 /* No help echo on owner-draw menu items, or when the keyboard is used
1479 to navigate the menus, since tooltips are distracting if they pop
1481 if (flags
& MF_OWNERDRAW
|| flags
& MF_POPUP
1482 || !(flags
& MF_MOUSESELECT
))
1488 memset (&info
, 0, sizeof (info
));
1489 info
.cbSize
= sizeof (info
);
1490 info
.fMask
= MIIM_DATA
;
1491 get_menu_item_info (menu
, item
, FALSE
, &info
);
1493 help
= info
.dwItemData
? XIL (info
.dwItemData
) : Qnil
;
1496 /* Store the help echo in the keyboard buffer as the X toolkit
1497 version does, rather than directly showing it. This seems to
1498 solve the GC problems that were present when we based the
1499 Windows code on the non-toolkit version. */
1502 XSETFRAME (frame
, f
);
1503 kbd_buffer_store_help_event (frame
, help
);
1506 /* X version has a loop through frames here, which doesn't
1507 appear to do anything, unless it has some side effect. */
1508 show_help_echo (help
, Qnil
, Qnil
, Qnil
);
1512 /* Free memory used by owner-drawn strings. */
1514 w32_free_submenu_strings (HMENU menu
)
1516 int i
, num
= GetMenuItemCount (menu
);
1517 for (i
= 0; i
< num
; i
++)
1520 memset (&info
, 0, sizeof (info
));
1521 info
.cbSize
= sizeof (info
);
1522 info
.fMask
= MIIM_DATA
| MIIM_TYPE
| MIIM_SUBMENU
;
1524 get_menu_item_info (menu
, i
, TRUE
, &info
);
1526 /* Owner-drawn names are held in dwItemData. */
1527 if ((info
.fType
& MF_OWNERDRAW
) && info
.dwItemData
)
1530 DebPrint ("Menu: freeing %ld for owner-draw", info
.dwItemData
);
1532 local_free (info
.dwItemData
);
1535 /* Recurse down submenus. */
1537 w32_free_submenu_strings (info
.hSubMenu
);
1542 w32_free_menu_strings (HWND hwnd
)
1544 HMENU menu
= current_popup_menu
;
1546 if (get_menu_item_info
)
1548 /* If there is no popup menu active, free the strings from the frame's
1551 menu
= GetMenu (hwnd
);
1554 w32_free_submenu_strings (menu
);
1557 current_popup_menu
= NULL
;
1560 /* The following is used by delayed window autoselection. */
1562 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p
, Smenu_or_popup_active_p
, 0, 0, 0,
1563 doc
: /* Return t if a menu or popup dialog is active on selected frame. */)
1567 f
= SELECTED_FRAME ();
1568 return (f
->output_data
.w32
->menubar_active
> 0) ? Qt
: Qnil
;
1572 syms_of_w32menu (void)
1574 globals_of_w32menu ();
1576 current_popup_menu
= NULL
;
1578 DEFSYM (Qdebug_on_next_call
, "debug-on-next-call");
1579 DEFSYM (Qunsupported__w32_dialog
, "unsupported--w32-dialog");
1581 defsubr (&Smenu_or_popup_active_p
);
1585 globals_of_w32menu is used to initialize those global variables that
1586 must always be initialized on startup even when the global variable
1587 initialized is non zero (see the function main in emacs.c).
1588 globals_of_w32menu is called from syms_of_w32menu when the global
1589 variable initialized is 0 and directly from main when initialized
1593 globals_of_w32menu (void)
1595 #ifndef NTGUI_UNICODE
1596 /* See if Get/SetMenuItemInfo functions are available. */
1597 HMODULE user32
= GetModuleHandle ("user32.dll");
1598 get_menu_item_info
= (GetMenuItemInfoA_Proc
) GetProcAddress (user32
, "GetMenuItemInfoA");
1599 set_menu_item_info
= (SetMenuItemInfoA_Proc
) GetProcAddress (user32
, "SetMenuItemInfoA");
1600 unicode_append_menu
= (AppendMenuW_Proc
) GetProcAddress (user32
, "AppendMenuW");
1601 unicode_message_box
= (MessageBoxW_Proc
) GetProcAddress (user32
, "MessageBoxW");
1602 #endif /* !NTGUI_UNICODE */