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