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