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