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