1 /* Menu support for GNU Emacs on the Microsoft Windows API.
2 Copyright (C) 1986, 1988, 1993-1994, 1996, 1998-1999, 2001-2013 Free
3 Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
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
) (
90 #define get_menu_item_info GetMenuItemInfoA
91 #define set_menu_item_info SetMenuItemInfoA
92 #define unicode_append_menu AppendMenuW
93 #define unicode_message_box MessageBoxW
94 #else /* !NTGUI_UNICODE */
95 GetMenuItemInfoA_Proc get_menu_item_info
= NULL
;
96 SetMenuItemInfoA_Proc set_menu_item_info
= NULL
;
97 AppendMenuW_Proc unicode_append_menu
= NULL
;
98 MessageBoxW_Proc unicode_message_box
= NULL
;
99 #endif /* NTGUI_UNICODE */
101 Lisp_Object Qdebug_on_next_call
;
103 void set_frame_menubar (struct frame
*, bool, bool);
106 static Lisp_Object
w32_dialog_show (struct frame
*, int, 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
);
119 w32_popup_dialog (struct frame
*f
, Lisp_Object header
, Lisp_Object contents
)
123 Lisp_Object selection
;
125 check_window_system (f
);
127 /* Decode the dialog items from what was specified. */
128 title
= Fcar (contents
);
129 CHECK_STRING (title
);
131 list_of_panes (Fcons (contents
, Qnil
));
133 /* Display them in a dialog box. */
135 selection
= w32_dialog_show (f
, 0, title
, header
, &error_name
);
138 discard_menu_items ();
139 FRAME_DISPLAY_INFO (f
)->grabbed
= 0;
141 if (error_name
) error (error_name
);
144 #endif /* HAVE_DIALOGS */
146 /* Activate the menu bar of frame F.
147 This is called from keyboard.c when it gets the
148 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
150 To activate the menu bar, we signal to the input thread that it can
151 return from the WM_INITMENU message, allowing the normal Windows
152 processing of the menus.
154 But first we recompute the menu bar contents (the whole tree).
156 This way we can safely execute Lisp code. */
159 x_activate_menubar (struct frame
*f
)
161 set_frame_menubar (f
, 0, 1);
163 /* Lock out further menubar changes while active. */
164 f
->output_data
.w32
->menubar_active
= 1;
166 /* Signal input thread to return from WM_INITMENU. */
167 complete_deferred_msg (FRAME_W32_WINDOW (f
), WM_INITMENU
, 0);
170 /* This callback is called from the menu bar pulldown menu
171 when the user makes a selection.
172 Figure out what the user chose
173 and put the appropriate events into the keyboard buffer. */
176 menubar_selection_callback (struct frame
*f
, void * client_data
)
178 Lisp_Object prefix
, entry
;
180 Lisp_Object
*subprefix_stack
;
181 int submenu_depth
= 0;
187 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* word_size
);
188 vector
= f
->menu_bar_vector
;
191 while (i
< f
->menu_bar_items_used
)
193 if (EQ (AREF (vector
, i
), Qnil
))
195 subprefix_stack
[submenu_depth
++] = prefix
;
199 else if (EQ (AREF (vector
, i
), Qlambda
))
201 prefix
= subprefix_stack
[--submenu_depth
];
204 else if (EQ (AREF (vector
, i
), Qt
))
206 prefix
= AREF (vector
, i
+ MENU_ITEMS_PANE_PREFIX
);
207 i
+= MENU_ITEMS_PANE_LENGTH
;
211 entry
= AREF (vector
, i
+ MENU_ITEMS_ITEM_VALUE
);
212 /* The EMACS_INT cast avoids a warning. There's no problem
213 as long as pointers have enough bits to hold small integers. */
214 if ((int) (EMACS_INT
) client_data
== i
)
217 struct input_event buf
;
221 XSETFRAME (frame
, f
);
222 buf
.kind
= MENU_BAR_EVENT
;
223 buf
.frame_or_window
= frame
;
225 kbd_buffer_store_event (&buf
);
227 for (j
= 0; j
< submenu_depth
; j
++)
228 if (!NILP (subprefix_stack
[j
]))
230 buf
.kind
= MENU_BAR_EVENT
;
231 buf
.frame_or_window
= frame
;
232 buf
.arg
= subprefix_stack
[j
];
233 kbd_buffer_store_event (&buf
);
238 buf
.kind
= MENU_BAR_EVENT
;
239 buf
.frame_or_window
= frame
;
241 kbd_buffer_store_event (&buf
);
244 buf
.kind
= MENU_BAR_EVENT
;
245 buf
.frame_or_window
= frame
;
247 /* Free memory used by owner-drawn and help-echo strings. */
248 w32_free_menu_strings (FRAME_W32_WINDOW (f
));
249 kbd_buffer_store_event (&buf
);
251 f
->output_data
.w32
->menubar_active
= 0;
254 i
+= MENU_ITEMS_ITEM_LENGTH
;
257 /* Free memory used by owner-drawn and help-echo strings. */
258 w32_free_menu_strings (FRAME_W32_WINDOW (f
));
259 f
->output_data
.w32
->menubar_active
= 0;
263 /* Set the contents of the menubar widgets of frame F.
264 The argument FIRST_TIME is currently ignored;
265 it is set the first time this is called, from initialize_frame_menubar. */
268 set_frame_menubar (struct frame
*f
, bool first_time
, bool deep_p
)
270 HMENU menubar_widget
= f
->output_data
.w32
->menubar_widget
;
272 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
274 int *submenu_start
, *submenu_end
;
275 int *submenu_top_level_items
, *submenu_n_panes
;
277 /* We must not change the menubar when actually in use. */
278 if (f
->output_data
.w32
->menubar_active
)
281 XSETFRAME (Vmenu_updating_frame
, f
);
283 if (! menubar_widget
)
288 /* Make a widget-value tree representing the entire menu trees. */
290 struct buffer
*prev
= current_buffer
;
292 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
293 int previous_menu_items_used
= f
->menu_bar_items_used
;
294 Lisp_Object
*previous_items
295 = (Lisp_Object
*) alloca (previous_menu_items_used
298 /* If we are making a new widget, its contents are empty,
299 do always reinitialize them. */
300 if (! menubar_widget
)
301 previous_menu_items_used
= 0;
303 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->contents
;
304 specbind (Qinhibit_quit
, Qt
);
305 /* Don't let the debugger step into this code
306 because it is not reentrant. */
307 specbind (Qdebug_on_next_call
, Qnil
);
309 record_unwind_save_match_data ();
311 if (NILP (Voverriding_local_map_menu_flag
))
313 specbind (Qoverriding_terminal_local_map
, Qnil
);
314 specbind (Qoverriding_local_map
, Qnil
);
317 set_buffer_internal_1 (XBUFFER (buffer
));
320 safe_run_hooks (Qactivate_menubar_hook
);
321 safe_run_hooks (Qmenu_bar_update_hook
);
322 fset_menu_bar_items (f
, menu_bar_items (FRAME_MENU_BAR_ITEMS (f
)));
324 items
= FRAME_MENU_BAR_ITEMS (f
);
326 /* Save the frame's previous menu bar contents data. */
327 if (previous_menu_items_used
)
328 memcpy (previous_items
, XVECTOR (f
->menu_bar_vector
)->contents
,
329 previous_menu_items_used
* word_size
);
331 /* Fill in menu_items with the current menu bar contents.
332 This can evaluate Lisp code. */
335 menu_items
= f
->menu_bar_vector
;
336 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
337 submenu_start
= (int *) alloca (ASIZE (items
) * sizeof (int));
338 submenu_end
= (int *) alloca (ASIZE (items
) * sizeof (int));
339 submenu_n_panes
= (int *) alloca (ASIZE (items
) * sizeof (int));
340 submenu_top_level_items
= (int *) alloca (ASIZE (items
) * sizeof (int));
342 for (i
= 0; i
< ASIZE (items
); i
+= 4)
344 Lisp_Object key
, string
, maps
;
348 key
= AREF (items
, i
);
349 string
= AREF (items
, i
+ 1);
350 maps
= AREF (items
, i
+ 2);
354 submenu_start
[i
] = menu_items_used
;
356 menu_items_n_panes
= 0;
357 submenu_top_level_items
[i
]
358 = parse_single_submenu (key
, string
, maps
);
359 submenu_n_panes
[i
] = menu_items_n_panes
;
361 submenu_end
[i
] = menu_items_used
;
364 finish_menu_items ();
366 /* Convert menu_items into widget_value trees
367 to display the menu. This cannot evaluate Lisp code. */
369 wv
= xmalloc_widget_value ();
370 wv
->name
= "menubar";
373 wv
->button_type
= BUTTON_TYPE_NONE
;
377 for (i
= 0; i
< last_i
; i
+= 4)
379 menu_items_n_panes
= submenu_n_panes
[i
];
380 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
381 submenu_top_level_items
[i
]);
385 first_wv
->contents
= wv
;
386 /* Don't set wv->name here; GC during the loop might relocate it. */
388 wv
->button_type
= BUTTON_TYPE_NONE
;
392 set_buffer_internal_1 (prev
);
394 /* If there has been no change in the Lisp-level contents
395 of the menu bar, skip redisplaying it. Just exit. */
397 for (i
= 0; i
< previous_menu_items_used
; i
++)
398 if (menu_items_used
== i
399 || (!EQ (previous_items
[i
], AREF (menu_items
, i
))))
401 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
403 free_menubar_widget_value_tree (first_wv
);
404 discard_menu_items ();
405 unbind_to (specpdl_count
, Qnil
);
409 fset_menu_bar_vector (f
, menu_items
);
410 f
->menu_bar_items_used
= menu_items_used
;
412 /* This undoes save_menu_items. */
413 unbind_to (specpdl_count
, Qnil
);
415 /* Now GC cannot happen during the lifetime of the widget_value,
416 so it's safe to store data from a Lisp_String, as long as
417 local copies are made when the actual menu is created.
418 Windows takes care of this for normal string items, but
419 not for owner-drawn items or additional item-info. */
420 wv
= first_wv
->contents
;
421 for (i
= 0; i
< ASIZE (items
); i
+= 4)
424 string
= AREF (items
, i
+ 1);
427 wv
->name
= SSDATA (string
);
428 update_submenu_strings (wv
->contents
);
434 /* Make a widget-value tree containing
435 just the top level menu bar strings. */
437 wv
= xmalloc_widget_value ();
438 wv
->name
= "menubar";
441 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
= xmalloc_widget_value ();
455 wv
->name
= SSDATA (string
);
458 wv
->button_type
= BUTTON_TYPE_NONE
;
460 /* This prevents lwlib from assuming this
461 menu item is really supposed to be empty. */
462 /* The EMACS_INT cast avoids a warning.
463 This value just has to be different from small integers. */
464 wv
->call_data
= (void *) (EMACS_INT
) (-1);
469 first_wv
->contents
= wv
;
473 /* Forget what we thought we knew about what is in the
474 detailed contents of the menu bar menus.
475 Changing the top level always destroys the contents. */
476 f
->menu_bar_items_used
= 0;
479 /* Create or update the menu bar widget. */
485 /* Empty current menubar, rather than creating a fresh one. */
486 while (DeleteMenu (menubar_widget
, 0, MF_BYPOSITION
))
491 menubar_widget
= CreateMenu ();
493 fill_in_menu (menubar_widget
, first_wv
->contents
);
495 free_menubar_widget_value_tree (first_wv
);
498 HMENU old_widget
= f
->output_data
.w32
->menubar_widget
;
500 f
->output_data
.w32
->menubar_widget
= menubar_widget
;
501 SetMenu (FRAME_W32_WINDOW (f
), f
->output_data
.w32
->menubar_widget
);
502 /* Causes flicker when menu bar is updated
503 DrawMenuBar (FRAME_W32_WINDOW (f)); */
505 /* Force the window size to be recomputed so that the frame's text
506 area remains the same, if menubar has just been created. */
507 if (old_widget
== NULL
)
508 x_set_window_size (f
, 0, FRAME_TEXT_WIDTH (f
), FRAME_TEXT_HEIGHT (f
), 1);
514 /* Called from Fx_create_frame to create the initial menubar of a frame
515 before it is mapped, so that the window is mapped with the menubar already
516 there instead of us tacking it on later and thrashing the window after it
520 initialize_frame_menubar (struct frame
*f
)
522 /* This function is called before the first chance to redisplay
523 the frame. It has to be, so the frame will have the right size. */
524 fset_menu_bar_items (f
, menu_bar_items (FRAME_MENU_BAR_ITEMS (f
)));
525 set_frame_menubar (f
, 1, 1);
528 /* Get rid of the menu bar of frame F, and free its storage.
529 This is used when deleting a frame, and when turning off the menu bar. */
532 free_frame_menubar (struct frame
*f
)
537 HMENU old
= GetMenu (FRAME_W32_WINDOW (f
));
538 SetMenu (FRAME_W32_WINDOW (f
), NULL
);
539 f
->output_data
.w32
->menubar_widget
= NULL
;
547 /* w32_menu_show actually displays a menu using the panes and items in
548 menu_items and returns the value selected from it; we assume input
549 is blocked by the caller. */
551 /* F is the frame the menu is for.
552 X and Y are the frame-relative specified position,
553 relative to the inside upper left corner of the frame F.
554 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
555 KEYMAPS is 1 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 for_click
, int keymaps
,
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
= xmalloc_widget_value ();
597 wv
->button_type
= BUTTON_TYPE_NONE
;
602 /* Loop over all panes and items, filling in the tree. */
604 while (i
< menu_items_used
)
606 if (EQ (AREF (menu_items
, i
), Qnil
))
608 submenu_stack
[submenu_depth
++] = save_wv
;
614 else if (EQ (AREF (menu_items
, i
), Qlambda
))
617 save_wv
= submenu_stack
[--submenu_depth
];
621 else if (EQ (AREF (menu_items
, i
), Qt
)
622 && submenu_depth
!= 0)
623 i
+= MENU_ITEMS_PANE_LENGTH
;
624 /* Ignore a nil in the item list.
625 It's meaningful only for dialog boxes. */
626 else if (EQ (AREF (menu_items
, i
), Qquote
))
628 else if (EQ (AREF (menu_items
, i
), Qt
))
630 /* Create a new pane. */
631 Lisp_Object pane_name
, prefix
;
633 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
634 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
636 if (STRINGP (pane_name
))
638 if (unicode_append_menu
)
639 pane_name
= ENCODE_UTF_8 (pane_name
);
640 else if (STRING_MULTIBYTE (pane_name
))
641 pane_name
= ENCODE_SYSTEM (pane_name
);
643 ASET (menu_items
, i
+ MENU_ITEMS_PANE_NAME
, pane_name
);
646 pane_string
= (NILP (pane_name
)
647 ? "" : SSDATA (pane_name
));
648 /* If there is just one top-level pane, put all its items directly
649 under the top-level menu. */
650 if (menu_items_n_panes
== 1)
653 /* If the pane has a meaningful name,
654 make the pane a top-level menu item
655 with its items as a submenu beneath it. */
656 if (!keymaps
&& strcmp (pane_string
, ""))
658 wv
= xmalloc_widget_value ();
662 first_wv
->contents
= wv
;
663 wv
->name
= pane_string
;
664 if (keymaps
&& !NILP (prefix
))
668 wv
->button_type
= BUTTON_TYPE_NONE
;
679 i
+= MENU_ITEMS_PANE_LENGTH
;
683 /* Create a new item within current pane. */
684 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
686 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
687 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
688 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
689 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
690 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
691 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
692 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
694 if (STRINGP (item_name
))
696 if (unicode_append_menu
)
697 item_name
= ENCODE_UTF_8 (item_name
);
698 else if (STRING_MULTIBYTE (item_name
))
699 item_name
= ENCODE_SYSTEM (item_name
);
701 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
, item_name
);
704 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
706 descrip
= ENCODE_SYSTEM (descrip
);
707 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
, descrip
);
710 wv
= xmalloc_widget_value ();
714 save_wv
->contents
= wv
;
715 wv
->name
= SSDATA (item_name
);
717 wv
->key
= SSDATA (descrip
);
719 /* Use the contents index as call_data, since we are
720 restricted to 16-bits. */
721 wv
->call_data
= !NILP (def
) ? (void *) (EMACS_INT
) i
: 0;
722 wv
->enabled
= !NILP (enable
);
725 wv
->button_type
= BUTTON_TYPE_NONE
;
726 else if (EQ (type
, QCtoggle
))
727 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
728 else if (EQ (type
, QCradio
))
729 wv
->button_type
= BUTTON_TYPE_RADIO
;
733 wv
->selected
= !NILP (selected
);
742 i
+= MENU_ITEMS_ITEM_LENGTH
;
746 /* Deal with the title, if it is non-nil. */
749 widget_value
*wv_title
= xmalloc_widget_value ();
750 widget_value
*wv_sep
= xmalloc_widget_value ();
752 /* Maybe replace this separator with a bitmap or owner-draw item
753 so that it looks better. Having two separators looks odd. */
755 wv_sep
->next
= first_wv
->contents
;
758 if (unicode_append_menu
)
759 title
= ENCODE_UTF_8 (title
);
760 else if (STRING_MULTIBYTE (title
))
761 title
= ENCODE_SYSTEM (title
);
763 wv_title
->name
= SSDATA (title
);
764 wv_title
->enabled
= TRUE
;
765 wv_title
->title
= TRUE
;
766 wv_title
->button_type
= BUTTON_TYPE_NONE
;
767 wv_title
->help
= Qnil
;
768 wv_title
->next
= wv_sep
;
769 first_wv
->contents
= wv_title
;
772 /* No selection has been chosen yet. */
773 menu_item_selection
= 0;
775 /* Actually create the menu. */
776 current_popup_menu
= menu
= CreatePopupMenu ();
777 fill_in_menu (menu
, first_wv
->contents
);
779 /* Adjust coordinates to be root-window-relative. */
782 ClientToScreen (FRAME_W32_WINDOW (f
), &pos
);
784 /* Display the menu. */
785 menu_item_selection
= SendMessage (FRAME_W32_WINDOW (f
),
786 WM_EMACS_TRACKPOPUPMENU
,
787 (WPARAM
)menu
, (LPARAM
)&pos
);
789 /* Clean up extraneous mouse events which might have been generated
791 discard_mouse_events ();
792 FRAME_DISPLAY_INFO (f
)->grabbed
= 0;
794 /* Free the widget_value objects we used to specify the contents. */
795 free_menubar_widget_value_tree (first_wv
);
799 /* Free the owner-drawn and help-echo menu strings. */
800 w32_free_menu_strings (FRAME_W32_WINDOW (f
));
801 f
->output_data
.w32
->menubar_active
= 0;
803 /* Find the selected item, and its pane, to return
805 if (menu_item_selection
!= 0)
807 Lisp_Object prefix
, entry
;
809 prefix
= entry
= Qnil
;
811 while (i
< menu_items_used
)
813 if (EQ (AREF (menu_items
, i
), Qnil
))
815 subprefix_stack
[submenu_depth
++] = prefix
;
819 else if (EQ (AREF (menu_items
, i
), Qlambda
))
821 prefix
= subprefix_stack
[--submenu_depth
];
824 else if (EQ (AREF (menu_items
, i
), Qt
))
826 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
827 i
+= MENU_ITEMS_PANE_LENGTH
;
829 /* Ignore a nil in the item list.
830 It's meaningful only for dialog boxes. */
831 else if (EQ (AREF (menu_items
, i
), Qquote
))
835 entry
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
836 if (menu_item_selection
== i
)
842 entry
= Fcons (entry
, Qnil
);
844 entry
= Fcons (prefix
, entry
);
845 for (j
= submenu_depth
- 1; j
>= 0; j
--)
846 if (!NILP (subprefix_stack
[j
]))
847 entry
= Fcons (subprefix_stack
[j
], entry
);
852 i
+= MENU_ITEMS_ITEM_LENGTH
;
859 /* Make "Cancel" equivalent to C-g. */
860 Fsignal (Qquit
, Qnil
);
869 /* TODO: On Windows, there are two ways of defining a dialog.
871 1. Create a predefined dialog resource and include it in nt/emacs.rc.
872 Using this method, we could then set the titles and make unneeded
873 buttons invisible before displaying the dialog. Everything would
874 be a fixed size though, so there is a risk that text does not
876 2. Create the dialog template in memory on the fly. This allows us
877 to size the dialog and buttons dynamically, probably giving more
878 natural looking results for dialogs with few buttons, and eliminating
879 the problem of text overflowing the buttons. But the API for this is
880 quite complex - structures have to be allocated in particular ways,
881 text content is tacked onto the end of structures in variable length
882 arrays with further structures tacked on after these, there are
883 certain alignment requirements for all this, and we have to
884 measure all the text and convert to "dialog coordinates" to figure
885 out how big to make everything.
887 For now, we'll just stick with menus for dialogs that are more
888 complicated than simple yes/no type questions for which we can use
889 the MessageBox function.
892 static char * button_names
[] = {
893 "button1", "button2", "button3", "button4", "button5",
894 "button6", "button7", "button8", "button9", "button10" };
897 w32_dialog_show (struct frame
*f
, int keymaps
,
898 Lisp_Object title
, Lisp_Object header
,
901 int i
, nb_buttons
= 0;
903 int menu_item_selection
;
905 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
907 /* Number of elements seen so far, before boundary. */
909 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
910 int boundary_seen
= 0;
914 if (menu_items_n_panes
> 1)
916 *error
= "Multiple panes in dialog box";
920 /* Create a tree of widget_value objects
921 representing the text label and buttons. */
923 Lisp_Object pane_name
, prefix
;
925 pane_name
= AREF (menu_items
, MENU_ITEMS_PANE_NAME
);
926 prefix
= AREF (menu_items
, MENU_ITEMS_PANE_PREFIX
);
927 pane_string
= (NILP (pane_name
)
928 ? "" : SSDATA (pane_name
));
929 prev_wv
= xmalloc_widget_value ();
930 prev_wv
->value
= pane_string
;
931 if (keymaps
&& !NILP (prefix
))
933 prev_wv
->enabled
= 1;
934 prev_wv
->name
= "message";
935 prev_wv
->help
= Qnil
;
938 /* Loop over all panes and items, filling in the tree. */
939 i
= MENU_ITEMS_PANE_LENGTH
;
940 while (i
< menu_items_used
)
943 /* Create a new item within current pane. */
944 Lisp_Object item_name
, enable
, descrip
, help
;
946 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
947 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
948 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
949 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
951 if (NILP (item_name
))
953 free_menubar_widget_value_tree (first_wv
);
954 *error
= "Submenu in dialog items";
957 if (EQ (item_name
, Qquote
))
959 /* This is the boundary between left-side elts
960 and right-side elts. Stop incrementing right_count. */
967 free_menubar_widget_value_tree (first_wv
);
968 *error
= "Too many dialog items";
972 wv
= xmalloc_widget_value ();
974 wv
->name
= (char *) button_names
[nb_buttons
];
976 wv
->key
= SSDATA (descrip
);
977 wv
->value
= SSDATA (item_name
);
978 wv
->call_data
= aref_addr (menu_items
, i
);
979 wv
->enabled
= !NILP (enable
);
987 i
+= MENU_ITEMS_ITEM_LENGTH
;
990 /* If the boundary was not specified,
991 by default put half on the left and half on the right. */
993 left_count
= nb_buttons
- nb_buttons
/ 2;
995 wv
= xmalloc_widget_value ();
996 wv
->name
= dialog_name
;
999 /* Frame title: 'Q' = Question, 'I' = Information.
1000 Can also have 'E' = Error if, one day, we want
1001 a popup for errors. */
1003 dialog_name
[0] = 'Q';
1005 dialog_name
[0] = 'I';
1007 /* Dialog boxes use a really stupid name encoding
1008 which specifies how many buttons to use
1009 and how many buttons are on the right. */
1010 dialog_name
[1] = '0' + nb_buttons
;
1011 dialog_name
[2] = 'B';
1012 dialog_name
[3] = 'R';
1013 /* Number of buttons to put on the right. */
1014 dialog_name
[4] = '0' + nb_buttons
- left_count
;
1016 wv
->contents
= first_wv
;
1020 /* Actually create the dialog. */
1021 dialog_id
= widget_id_tick
++;
1022 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1023 f
->output_data
.w32
->widget
, 1, 0,
1024 dialog_selection_callback
, 0);
1025 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, TRUE
);
1027 /* Free the widget_value objects we used to specify the contents. */
1028 free_menubar_widget_value_tree (first_wv
);
1030 /* No selection has been chosen yet. */
1031 menu_item_selection
= 0;
1033 /* Display the menu. */
1034 lw_pop_up_all_widgets (dialog_id
);
1036 /* Process events that apply to the menu. */
1037 popup_get_selection ((XEvent
*) 0, FRAME_DISPLAY_INFO (f
), dialog_id
);
1039 lw_destroy_all_widgets (dialog_id
);
1041 /* Find the selected item, and its pane, to return
1042 the proper value. */
1043 if (menu_item_selection
!= 0)
1049 while (i
< menu_items_used
)
1053 if (EQ (AREF (menu_items
, i
), Qt
))
1055 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
1056 i
+= MENU_ITEMS_PANE_LENGTH
;
1060 entry
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
1061 if (menu_item_selection
== i
)
1065 entry
= Fcons (entry
, Qnil
);
1067 entry
= Fcons (prefix
, entry
);
1071 i
+= MENU_ITEMS_ITEM_LENGTH
;
1076 /* Make "Cancel" equivalent to C-g. */
1077 Fsignal (Qquit
, Qnil
);
1081 #else /* !HAVE_DIALOGS */
1083 /* Currently we only handle Yes No dialogs (y-or-n-p and yes-or-no-p) as
1084 simple dialogs. We could handle a few more, but I'm not aware of
1085 anywhere in Emacs that uses the other specific dialog choices that
1086 MessageBox provides. */
1089 is_simple_dialog (Lisp_Object contents
)
1091 Lisp_Object options
;
1092 Lisp_Object name
, yes
, no
, other
;
1094 if (!CONSP (contents
))
1096 options
= XCDR (contents
);
1098 yes
= build_string ("Yes");
1099 no
= build_string ("No");
1101 if (!CONSP (options
))
1104 name
= XCAR (options
);
1109 if (!NILP (Fstring_equal (name
, yes
)))
1111 else if (!NILP (Fstring_equal (name
, no
)))
1116 options
= XCDR (options
);
1117 if (!CONSP (options
))
1120 name
= XCAR (options
);
1124 if (NILP (Fstring_equal (name
, other
)))
1127 /* Check there are no more options. */
1128 options
= XCDR (options
);
1129 return !(CONSP (options
));
1133 simple_dialog_show (struct frame
*f
, Lisp_Object contents
, Lisp_Object header
)
1137 Lisp_Object lispy_answer
= Qnil
, temp
= XCAR (contents
);
1141 /* Since we only handle Yes/No dialogs, and we already checked
1142 is_simple_dialog, we don't need to worry about checking contents
1143 to see what type of dialog to use. */
1145 /* Use Unicode if possible, so any language can be displayed. */
1146 if (unicode_message_box
)
1148 WCHAR
*text
, *title
;
1153 char *utf8_text
= SDATA (ENCODE_UTF_8 (temp
));
1154 /* Be pessimistic about the number of characters needed.
1155 Remember characters outside the BMP will take more than
1156 one utf16 word, so we cannot simply use the character
1158 int utf8_len
= strlen (utf8_text
);
1159 text
= SAFE_ALLOCA ((utf8_len
+ 1) * sizeof (WCHAR
));
1160 utf8to16 (utf8_text
, utf8_len
, text
);
1169 title
= L
"Question";
1170 type
|= MB_ICONQUESTION
;
1174 title
= L
"Information";
1175 type
|= MB_ICONINFORMATION
;
1178 answer
= unicode_message_box (FRAME_W32_WINDOW (f
), text
, title
, type
);
1185 /* Fall back on ANSI message box, but at least use system
1186 encoding so questions representable by the system codepage
1187 are encoded properly. */
1189 text
= SDATA (ENCODE_SYSTEM (temp
));
1196 type
|= MB_ICONQUESTION
;
1200 title
= "Information";
1201 type
|= MB_ICONINFORMATION
;
1204 answer
= MessageBox (FRAME_W32_WINDOW (f
), text
, title
, type
);
1207 if (answer
== IDYES
)
1208 lispy_answer
= build_string ("Yes");
1209 else if (answer
== IDNO
)
1210 lispy_answer
= build_string ("No");
1212 Fsignal (Qquit
, Qnil
);
1214 for (temp
= XCDR (contents
); CONSP (temp
); temp
= XCDR (temp
))
1216 Lisp_Object item
, name
, value
;
1221 value
= XCDR (item
);
1229 if (!NILP (Fstring_equal (name
, lispy_answer
)))
1234 Fsignal (Qquit
, Qnil
);
1237 #endif /* !HAVE_DIALOGS */
1240 /* UTF8: 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx, 10xxxxxx, 10xxxxxx */
1242 utf8to16 (unsigned char * src
, int len
, WCHAR
* dest
)
1248 *dest
= (WCHAR
) *src
;
1249 dest
++; src
++; len
--;
1251 /* Since we might get >3 byte sequences which we don't handle, ignore the extra parts. */
1252 else if (*src
< 0xC0)
1256 /* 2 char UTF-8 sequence. */
1257 else if (*src
< 0xE0)
1259 *dest
= (WCHAR
) (((*src
& 0x1f) << 6)
1260 | (*(src
+ 1) & 0x3f));
1261 src
+= 2; len
-= 2; dest
++;
1263 else if (*src
< 0xF0)
1265 *dest
= (WCHAR
) (((*src
& 0x0f) << 12)
1266 | ((*(src
+ 1) & 0x3f) << 6)
1267 | (*(src
+ 2) & 0x3f));
1268 src
+= 3; len
-= 3; dest
++;
1270 else /* Not encodable. Insert Unicode Substitution char. */
1272 *dest
= (WCHAR
) 0xfffd;
1273 src
++; len
--; dest
++;
1280 add_menu_item (HMENU menu
, widget_value
*wv
, HMENU item
)
1283 char *out_string
, *p
, *q
;
1285 size_t nlen
, orig_len
;
1288 if (menu_separator_name_p (wv
->name
))
1290 fuFlags
= MF_SEPARATOR
;
1296 fuFlags
= MF_STRING
;
1298 fuFlags
= MF_STRING
| MF_GRAYED
;
1300 if (wv
->key
!= NULL
)
1302 out_string
= SAFE_ALLOCA (strlen (wv
->name
) + strlen (wv
->key
) + 2);
1303 strcpy (out_string
, wv
->name
);
1304 strcat (out_string
, "\t");
1305 strcat (out_string
, wv
->key
);
1308 out_string
= (char *)wv
->name
;
1310 /* Quote any special characters within the menu item's text and
1312 nlen
= orig_len
= strlen (out_string
);
1313 if (unicode_append_menu
)
1315 /* With UTF-8, & cannot be part of a multibyte character. */
1316 for (p
= out_string
; *p
; p
++)
1322 #ifndef NTGUI_UNICODE
1325 /* If encoded with the system codepage, use multibyte string
1326 functions in case of multibyte characters that contain '&'. */
1327 for (p
= out_string
; *p
; p
= _mbsinc (p
))
1329 if (_mbsnextc (p
) == '&')
1333 #endif /* !NTGUI_UNICODE */
1335 if (nlen
> orig_len
)
1338 out_string
= SAFE_ALLOCA (nlen
+ 1);
1342 if (unicode_append_menu
)
1348 #ifndef NTGUI_UNICODE
1351 if (_mbsnextc (p
) == '&')
1360 #endif /* !NTGUI_UNICODE */
1367 else if (wv
->title
|| wv
->call_data
== 0)
1369 /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since
1370 we can't deallocate the memory otherwise. */
1371 if (get_menu_item_info
)
1373 out_string
= (char *) local_alloc (strlen (wv
->name
) + 1);
1374 strcpy (out_string
, wv
->name
);
1376 DebPrint ("Menu: allocating %ld for owner-draw", out_string
);
1378 fuFlags
= MF_OWNERDRAW
| MF_DISABLED
;
1381 fuFlags
= MF_DISABLED
;
1384 /* Draw radio buttons and tickboxes. */
1385 else if (wv
->selected
&& (wv
->button_type
== BUTTON_TYPE_TOGGLE
||
1386 wv
->button_type
== BUTTON_TYPE_RADIO
))
1387 fuFlags
|= MF_CHECKED
;
1389 fuFlags
|= MF_UNCHECKED
;
1392 if (unicode_append_menu
&& out_string
)
1394 /* Convert out_string from UTF-8 to UTF-16-LE. */
1395 int utf8_len
= strlen (out_string
);
1396 WCHAR
* utf16_string
;
1397 if (fuFlags
& MF_OWNERDRAW
)
1398 utf16_string
= local_alloc ((utf8_len
+ 1) * sizeof (WCHAR
));
1400 utf16_string
= SAFE_ALLOCA ((utf8_len
+ 1) * sizeof (WCHAR
));
1402 utf8to16 (out_string
, utf8_len
, utf16_string
);
1403 return_value
= unicode_append_menu (menu
, fuFlags
,
1404 item
!= NULL
? (UINT_PTR
) item
1405 : (UINT_PTR
) wv
->call_data
,
1408 #ifndef NTGUI_UNICODE /* Fallback does not apply when always UNICODE */
1411 /* On W9x/ME, Unicode menus are not supported, though AppendMenuW
1412 apparently does exist at least in some cases and appears to be
1413 stubbed out to do nothing. out_string is UTF-8, but since
1414 our standard menus are in English and this is only going to
1415 happen the first time a menu is used, the encoding is
1416 of minor importance compared with menus not working at all. */
1418 AppendMenu (menu
, fuFlags
,
1419 item
!= NULL
? (UINT_PTR
) item
: (UINT_PTR
) wv
->call_data
,
1421 /* Don't use Unicode menus in future, unless this is Windows
1422 NT or later, where a failure of AppendMenuW does NOT mean
1423 Unicode menus are unsupported. */
1424 if (osinfo_cache
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
)
1425 unicode_append_menu
= NULL
;
1427 #endif /* NTGUI_UNICODE */
1429 if (unicode_append_menu
&& (fuFlags
& MF_OWNERDRAW
))
1430 local_free (out_string
);
1437 item
!= NULL
? (UINT_PTR
) item
: (UINT_PTR
) wv
->call_data
,
1441 /* This must be done after the menu item is created. */
1442 if (!wv
->title
&& wv
->call_data
!= 0)
1444 if (set_menu_item_info
)
1447 memset (&info
, 0, sizeof (info
));
1448 info
.cbSize
= sizeof (info
);
1449 info
.fMask
= MIIM_DATA
;
1451 /* Set help string for menu item. Leave it as a Lisp_Object
1452 until it is ready to be displayed, since GC can happen while
1453 menus are active. */
1454 if (!NILP (wv
->help
))
1456 /* As of Jul-2012, w32api headers say that dwItemData
1457 has DWORD type, but that's a bug: it should actually
1458 be ULONG_PTR, which is correct for 32-bit and 64-bit
1459 Windows alike. MSVC headers get it right; hopefully,
1460 MinGW headers will, too. */
1461 info
.dwItemData
= (ULONG_PTR
) XLI (wv
->help
);
1463 if (wv
->button_type
== BUTTON_TYPE_RADIO
)
1465 /* CheckMenuRadioItem allows us to differentiate TOGGLE and
1466 RADIO items, but is not available on NT 3.51 and earlier. */
1467 info
.fMask
|= MIIM_TYPE
| MIIM_STATE
;
1468 info
.fType
= MFT_RADIOCHECK
| MFT_STRING
;
1469 info
.dwTypeData
= out_string
;
1470 info
.fState
= wv
->selected
? MFS_CHECKED
: MFS_UNCHECKED
;
1473 set_menu_item_info (menu
,
1474 item
!= NULL
? (UINT_PTR
) item
: (UINT_PTR
) wv
->call_data
,
1479 return return_value
;
1482 /* Construct native Windows menu(bar) based on widget_value tree. */
1484 fill_in_menu (HMENU menu
, widget_value
*wv
)
1486 for ( ; wv
!= NULL
; wv
= wv
->next
)
1490 HMENU sub_menu
= CreatePopupMenu ();
1492 if (sub_menu
== NULL
)
1495 if (!fill_in_menu (sub_menu
, wv
->contents
) ||
1496 !add_menu_item (menu
, wv
, sub_menu
))
1498 DestroyMenu (sub_menu
);
1504 if (!add_menu_item (menu
, wv
, NULL
))
1511 /* Display help string for currently pointed to menu item. Not
1512 supported on NT 3.51 and earlier, as GetMenuItemInfo is not
1515 w32_menu_display_help (HWND owner
, HMENU menu
, UINT item
, UINT flags
)
1517 if (get_menu_item_info
)
1519 struct frame
*f
= x_window_to_frame (&one_w32_display_info
, owner
);
1520 Lisp_Object frame
, help
;
1522 /* No help echo on owner-draw menu items, or when the keyboard is used
1523 to navigate the menus, since tooltips are distracting if they pop
1525 if (flags
& MF_OWNERDRAW
|| flags
& MF_POPUP
1526 || !(flags
& MF_MOUSESELECT
))
1532 memset (&info
, 0, sizeof (info
));
1533 info
.cbSize
= sizeof (info
);
1534 info
.fMask
= MIIM_DATA
;
1535 get_menu_item_info (menu
, item
, FALSE
, &info
);
1537 help
= info
.dwItemData
? XIL (info
.dwItemData
) : Qnil
;
1540 /* Store the help echo in the keyboard buffer as the X toolkit
1541 version does, rather than directly showing it. This seems to
1542 solve the GC problems that were present when we based the
1543 Windows code on the non-toolkit version. */
1546 XSETFRAME (frame
, f
);
1547 kbd_buffer_store_help_event (frame
, help
);
1550 /* X version has a loop through frames here, which doesn't
1551 appear to do anything, unless it has some side effect. */
1552 show_help_echo (help
, Qnil
, Qnil
, Qnil
);
1556 /* Free memory used by owner-drawn strings. */
1558 w32_free_submenu_strings (HMENU menu
)
1560 int i
, num
= GetMenuItemCount (menu
);
1561 for (i
= 0; i
< num
; i
++)
1564 memset (&info
, 0, sizeof (info
));
1565 info
.cbSize
= sizeof (info
);
1566 info
.fMask
= MIIM_DATA
| MIIM_TYPE
| MIIM_SUBMENU
;
1568 get_menu_item_info (menu
, i
, TRUE
, &info
);
1570 /* Owner-drawn names are held in dwItemData. */
1571 if ((info
.fType
& MF_OWNERDRAW
) && info
.dwItemData
)
1574 DebPrint ("Menu: freeing %ld for owner-draw", info
.dwItemData
);
1576 local_free (info
.dwItemData
);
1579 /* Recurse down submenus. */
1581 w32_free_submenu_strings (info
.hSubMenu
);
1586 w32_free_menu_strings (HWND hwnd
)
1588 HMENU menu
= current_popup_menu
;
1590 if (get_menu_item_info
)
1592 /* If there is no popup menu active, free the strings from the frame's
1595 menu
= GetMenu (hwnd
);
1598 w32_free_submenu_strings (menu
);
1601 current_popup_menu
= NULL
;
1604 /* The following is used by delayed window autoselection. */
1606 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p
, Smenu_or_popup_active_p
, 0, 0, 0,
1607 doc
: /* Return t if a menu or popup dialog is active on selected frame. */)
1611 f
= SELECTED_FRAME ();
1612 return (f
->output_data
.w32
->menubar_active
> 0) ? Qt
: Qnil
;
1616 syms_of_w32menu (void)
1618 globals_of_w32menu ();
1620 current_popup_menu
= NULL
;
1622 DEFSYM (Qdebug_on_next_call
, "debug-on-next-call");
1624 defsubr (&Smenu_or_popup_active_p
);
1628 globals_of_w32menu is used to initialize those global variables that
1629 must always be initialized on startup even when the global variable
1630 initialized is non zero (see the function main in emacs.c).
1631 globals_of_w32menu is called from syms_of_w32menu when the global
1632 variable initialized is 0 and directly from main when initialized
1636 globals_of_w32menu (void)
1638 #ifndef NTGUI_UNICODE
1639 /* See if Get/SetMenuItemInfo functions are available. */
1640 HMODULE user32
= GetModuleHandle ("user32.dll");
1641 get_menu_item_info
= (GetMenuItemInfoA_Proc
) GetProcAddress (user32
, "GetMenuItemInfoA");
1642 set_menu_item_info
= (SetMenuItemInfoA_Proc
) GetProcAddress (user32
, "SetMenuItemInfoA");
1643 unicode_append_menu
= (AppendMenuW_Proc
) GetProcAddress (user32
, "AppendMenuW");
1644 unicode_message_box
= (MessageBoxW_Proc
) GetProcAddress (user32
, "MessageBoxW");
1645 #endif /* !NTGUI_UNICODE */