/
xmenu.c
2583 lines (2162 loc) · 72 KB
/
xmenu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* X Communication module for terminals which understand the X protocol.
Copyright (C) 1986, 1988, 1993-1994, 1996, 1999-2011
Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
/* X pop-up deck-of-cards menu facility for GNU Emacs.
*
* Written by Jon Arnold and Roman Budzianowski
* Mods and rewrite by Robert Krawitz
*
*/
/* Modified by Fred Pierresteguy on December 93
to make the popup menus and menubar use the Xt. */
/* Rewritten for clarity and GC protection by rms in Feb 94. */
#include <config.h>
#if 0 /* Why was this included? And without syssignal.h? */
/* On 4.3 this loses if it comes after xterm.h. */
#include <signal.h>
#endif
#include <stdio.h>
#include <setjmp.h>
#include "lisp.h"
#include "keyboard.h"
#include "keymap.h"
#include "frame.h"
#include "termhooks.h"
#include "window.h"
#include "blockinput.h"
#include "buffer.h"
#include "charset.h"
#include "coding.h"
#include "sysselect.h"
#ifdef MSDOS
#include "msdos.h"
#endif
#ifdef HAVE_X_WINDOWS
/* This may include sys/types.h, and that somehow loses
if this is not done before the other system files. */
#include "xterm.h"
#endif
/* Load sys/types.h if not already loaded.
In some systems loading it twice is suicidal. */
#ifndef makedev
#include <sys/types.h>
#endif
#include "dispextern.h"
#ifdef HAVE_X_WINDOWS
/* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
code accepts the Emacs internal encoding. */
#undef HAVE_MULTILINGUAL_MENU
#ifdef USE_X_TOOLKIT
#include "widget.h"
#include <X11/Xlib.h>
#include <X11/IntrinsicP.h>
#include <X11/CoreP.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#ifdef USE_LUCID
#include "xsettings.h"
#include "../lwlib/xlwmenu.h"
#ifdef HAVE_XAW3D
#include <X11/Xaw3d/Paned.h>
#else /* !HAVE_XAW3D */
#include <X11/Xaw/Paned.h>
#endif /* HAVE_XAW3D */
#endif /* USE_LUCID */
#ifdef USE_MOTIF
#include "../lwlib/lwlib.h"
#endif
#else /* not USE_X_TOOLKIT */
#ifndef USE_GTK
#include "../oldXMenu/XMenu.h"
#endif
#endif /* not USE_X_TOOLKIT */
#endif /* HAVE_X_WINDOWS */
#ifdef USE_GTK
#include "gtkutil.h"
#endif
#include "menu.h"
#ifndef TRUE
#define TRUE 1
#endif /* no TRUE */
static Lisp_Object Qdebug_on_next_call;
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
static Lisp_Object xdialog_show (FRAME_PTR, int, Lisp_Object, Lisp_Object,
const char **);
#endif
/* Flag which when set indicates a dialog or menu has been posted by
Xt on behalf of one of the widget sets. */
static int popup_activated_flag;
#ifdef USE_X_TOOLKIT
static int next_menubar_widget_id;
/* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
static struct frame *
menubar_id_to_frame (LWLIB_ID id)
{
Lisp_Object tail, frame;
FRAME_PTR f;
for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
{
frame = XCAR (tail);
if (!FRAMEP (frame))
continue;
f = XFRAME (frame);
if (!FRAME_WINDOW_P (f))
continue;
if (f->output_data.x->id == id)
return f;
}
return 0;
}
#endif
#ifdef HAVE_X_WINDOWS
/* Return the mouse position in *X and *Y. The coordinates are window
relative for the edit window in frame F.
This is for Fx_popup_menu. The mouse_position_hook can not
be used for X, as it returns window relative coordinates
for the window where the mouse is in. This could be the menu bar,
the scroll bar or the edit window. Fx_popup_menu needs to be
sure it is the edit window. */
void
mouse_position_for_popup (FRAME_PTR f, int *x, int *y)
{
Window root, dummy_window;
int dummy;
if (! FRAME_X_P (f))
abort ();
BLOCK_INPUT;
XQueryPointer (FRAME_X_DISPLAY (f),
DefaultRootWindow (FRAME_X_DISPLAY (f)),
/* The root window which contains the pointer. */
&root,
/* Window pointer is on, not used */
&dummy_window,
/* The position on that root window. */
x, y,
/* x/y in dummy_window coordinates, not used. */
&dummy, &dummy,
/* Modifier keys and pointer buttons, about which
we don't care. */
(unsigned int *) &dummy);
UNBLOCK_INPUT;
/* xmenu_show expects window coordinates, not root window
coordinates. Translate. */
*x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
*y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
}
#endif /* HAVE_X_WINDOWS */
#ifdef HAVE_MENUS
DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
doc: /* Pop up a dialog box and return user's selection.
POSITION specifies which frame to use.
This is normally a mouse button event or a window or frame.
If POSITION is t, it means to use the frame the mouse is on.
The dialog box appears in the middle of the specified frame.
CONTENTS specifies the alternatives to display in the dialog box.
It is a list of the form (DIALOG ITEM1 ITEM2...).
Each ITEM is a cons cell (STRING . VALUE).
The return value is VALUE from the chosen item.
An ITEM may also be just a string--that makes a nonselectable item.
An ITEM may also be nil--that means to put all preceding items
on the left of the dialog box and all following items on the right.
\(By default, approximately half appear on each side.)
If HEADER is non-nil, the frame title for the box is "Information",
otherwise it is "Question".
If the user gets rid of the dialog box without making a valid choice,
for instance using the window manager, then this produces a quit and
`x-popup-dialog' does not return. */)
(Lisp_Object position, Lisp_Object contents, Lisp_Object header)
{
FRAME_PTR f = NULL;
Lisp_Object window;
check_x ();
/* Decode the first argument: find the window or frame to use. */
if (EQ (position, Qt)
|| (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
|| EQ (XCAR (position), Qtool_bar))))
{
#if 0 /* Using the frame the mouse is on may not be right. */
/* Use the mouse's current position. */
FRAME_PTR new_f = SELECTED_FRAME ();
Lisp_Object bar_window;
enum scroll_bar_part part;
Time time;
Lisp_Object x, y;
(*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
if (new_f != 0)
XSETFRAME (window, new_f);
else
window = selected_window;
#endif
window = selected_window;
}
else if (CONSP (position))
{
Lisp_Object tem;
tem = Fcar (position);
if (CONSP (tem))
window = Fcar (Fcdr (position));
else
{
tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
window = Fcar (tem); /* POSN_WINDOW (tem) */
}
}
else if (WINDOWP (position) || FRAMEP (position))
window = position;
else
window = Qnil;
/* Decode where to put the menu. */
if (FRAMEP (window))
f = XFRAME (window);
else if (WINDOWP (window))
{
CHECK_LIVE_WINDOW (window);
f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
}
else
/* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
but I don't want to make one now. */
CHECK_WINDOW (window);
if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
error ("Can not put X dialog on this terminal");
/* Force a redisplay before showing the dialog. If a frame is created
just before showing the dialog, its contents may not have been fully
drawn, as this depends on timing of events from the X server. Redisplay
is not done when a dialog is shown. If redisplay could be done in the
X event loop (i.e. the X event loop does not run in a signal handler)
this would not be needed.
Do this before creating the widget value that points to Lisp
string contents, because Fredisplay may GC and relocate them. */
Fredisplay (Qt);
#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
/* Display a menu with these alternatives
in the middle of frame F. */
{
Lisp_Object x, y, frame, newpos;
XSETFRAME (frame, f);
XSETINT (x, x_pixel_width (f) / 2);
XSETINT (y, x_pixel_height (f) / 2);
newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
return Fx_popup_menu (newpos,
Fcons (Fcar (contents), Fcons (contents, Qnil)));
}
#else
{
Lisp_Object title;
const char *error_name;
Lisp_Object selection;
int specpdl_count = SPECPDL_INDEX ();
/* Decode the dialog items from what was specified. */
title = Fcar (contents);
CHECK_STRING (title);
record_unwind_protect (unuse_menu_items, Qnil);
if (NILP (Fcar (Fcdr (contents))))
/* No buttons specified, add an "Ok" button so users can pop down
the dialog. Also, the lesstif/motif version crashes if there are
no buttons. */
contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
list_of_panes (Fcons (contents, Qnil));
/* Display them in a dialog box. */
BLOCK_INPUT;
selection = xdialog_show (f, 0, title, header, &error_name);
UNBLOCK_INPUT;
unbind_to (specpdl_count, Qnil);
discard_menu_items ();
if (error_name) error ("%s", error_name);
return selection;
}
#endif
}
#ifndef MSDOS
#if defined USE_GTK || defined USE_MOTIF
/* Set menu_items_inuse so no other popup menu or dialog is created. */
void
x_menu_set_in_use (int in_use)
{
menu_items_inuse = in_use ? Qt : Qnil;
popup_activated_flag = in_use;
#ifdef USE_X_TOOLKIT
if (popup_activated_flag)
x_activate_timeout_atimer ();
#endif
}
#endif
/* Wait for an X event to arrive or for a timer to expire. */
#ifndef USE_MOTIF
static
#endif
void
x_menu_wait_for_event (void *data)
{
/* Another way to do this is to register a timer callback, that can be
done in GTK and Xt. But we have to do it like this when using only X
anyway, and with callbacks we would have three variants for timer handling
instead of the small ifdefs below. */
while (
#ifdef USE_X_TOOLKIT
! XtAppPending (Xt_app_con)
#elif defined USE_GTK
! gtk_events_pending ()
#else
! XPending ((Display*) data)
#endif
)
{
EMACS_TIME next_time = timer_check (), *ntp;
long secs = EMACS_SECS (next_time);
long usecs = EMACS_USECS (next_time);
SELECT_TYPE read_fds;
struct x_display_info *dpyinfo;
int n = 0;
FD_ZERO (&read_fds);
for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
{
int fd = ConnectionNumber (dpyinfo->display);
FD_SET (fd, &read_fds);
if (fd > n) n = fd;
XFlush (dpyinfo->display);
}
if (secs < 0 && usecs < 0)
ntp = 0;
else
ntp = &next_time;
select (n + 1, &read_fds, (SELECT_TYPE *)0, (SELECT_TYPE *)0, ntp);
}
}
#endif /* ! MSDOS */
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
#ifdef USE_X_TOOLKIT
/* Loop in Xt until the menu pulldown or dialog popup has been
popped down (deactivated). This is used for x-popup-menu
and x-popup-dialog; it is not used for the menu bar.
NOTE: All calls to popup_get_selection should be protected
with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
static void
popup_get_selection (XEvent *initial_event, struct x_display_info *dpyinfo, LWLIB_ID id, int do_timers)
{
XEvent event;
while (popup_activated_flag)
{
if (initial_event)
{
event = *initial_event;
initial_event = 0;
}
else
{
if (do_timers) x_menu_wait_for_event (0);
XtAppNextEvent (Xt_app_con, &event);
}
/* Make sure we don't consider buttons grabbed after menu goes.
And make sure to deactivate for any ButtonRelease,
even if XtDispatchEvent doesn't do that. */
if (event.type == ButtonRelease
&& dpyinfo->display == event.xbutton.display)
{
dpyinfo->grabbed &= ~(1 << event.xbutton.button);
#ifdef USE_MOTIF /* Pretending that the event came from a
Btn1Down seems the only way to convince Motif to
activate its callbacks; setting the XmNmenuPost
isn't working. --marcus@sysc.pdx.edu. */
event.xbutton.button = 1;
/* Motif only pops down menus when no Ctrl, Alt or Mod
key is pressed and the button is released. So reset key state
so Motif thinks this is the case. */
event.xbutton.state = 0;
#endif
}
/* Pop down on C-g and Escape. */
else if (event.type == KeyPress
&& dpyinfo->display == event.xbutton.display)
{
KeySym keysym = XLookupKeysym (&event.xkey, 0);
if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
|| keysym == XK_Escape) /* Any escape, ignore modifiers. */
popup_activated_flag = 0;
}
x_dispatch_event (&event, event.xany.display);
}
}
DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
doc: /* Start key navigation of the menu bar in FRAME.
This initially opens the first menu bar item and you can then navigate with the
arrow keys, select a menu entry with the return key or cancel with the
escape key. If FRAME has no menu bar this function does nothing.
If FRAME is nil or not given, use the selected frame. */)
(Lisp_Object frame)
{
XEvent ev;
FRAME_PTR f = check_x_frame (frame);
Widget menubar;
BLOCK_INPUT;
if (FRAME_EXTERNAL_MENU_BAR (f))
set_frame_menubar (f, 0, 1);
menubar = FRAME_X_OUTPUT (f)->menubar_widget;
if (menubar)
{
Window child;
int error_p = 0;
x_catch_errors (FRAME_X_DISPLAY (f));
memset (&ev, 0, sizeof ev);
ev.xbutton.display = FRAME_X_DISPLAY (f);
ev.xbutton.window = XtWindow (menubar);
ev.xbutton.root = FRAME_X_DISPLAY_INFO (f)->root_window;
ev.xbutton.time = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
ev.xbutton.button = Button1;
ev.xbutton.x = ev.xbutton.y = FRAME_MENUBAR_HEIGHT (f) / 2;
ev.xbutton.same_screen = True;
#ifdef USE_MOTIF
{
Arg al[2];
WidgetList list;
Cardinal nr;
XtSetArg (al[0], XtNchildren, &list);
XtSetArg (al[1], XtNnumChildren, &nr);
XtGetValues (menubar, al, 2);
ev.xbutton.window = XtWindow (list[0]);
}
#endif
XTranslateCoordinates (FRAME_X_DISPLAY (f),
/* From-window, to-window. */
ev.xbutton.window, ev.xbutton.root,
/* From-position, to-position. */
ev.xbutton.x, ev.xbutton.y,
&ev.xbutton.x_root, &ev.xbutton.y_root,
/* Child of win. */
&child);
error_p = x_had_errors_p (FRAME_X_DISPLAY (f));
x_uncatch_errors ();
if (! error_p)
{
ev.type = ButtonPress;
ev.xbutton.state = 0;
XtDispatchEvent (&ev);
ev.xbutton.type = ButtonRelease;
ev.xbutton.state = Button1Mask;
XtDispatchEvent (&ev);
}
}
UNBLOCK_INPUT;
return Qnil;
}
#endif /* USE_X_TOOLKIT */
#ifdef USE_GTK
DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
doc: /* Start key navigation of the menu bar in FRAME.
This initially opens the first menu bar item and you can then navigate with the
arrow keys, select a menu entry with the return key or cancel with the
escape key. If FRAME has no menu bar this function does nothing.
If FRAME is nil or not given, use the selected frame. */)
(Lisp_Object frame)
{
GtkWidget *menubar;
FRAME_PTR f;
/* gcc 2.95 doesn't accept the FRAME_PTR declaration after
BLOCK_INPUT. */
BLOCK_INPUT;
f = check_x_frame (frame);
if (FRAME_EXTERNAL_MENU_BAR (f))
set_frame_menubar (f, 0, 1);
menubar = FRAME_X_OUTPUT (f)->menubar_widget;
if (menubar)
{
/* Activate the first menu. */
GList *children = gtk_container_get_children (GTK_CONTAINER (menubar));
if (children)
{
g_signal_emit_by_name (children->data, "activate_item");
popup_activated_flag = 1;
g_list_free (children);
}
}
UNBLOCK_INPUT;
return Qnil;
}
/* Loop util popup_activated_flag is set to zero in a callback.
Used for popup menus and dialogs. */
static void
popup_widget_loop (int do_timers, GtkWidget *widget)
{
++popup_activated_flag;
/* Process events in the Gtk event loop until done. */
while (popup_activated_flag)
{
if (do_timers) x_menu_wait_for_event (0);
gtk_main_iteration ();
}
}
#endif
/* Activate the menu bar of frame F.
This is called from keyboard.c when it gets the
MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
To activate the menu bar, we use the X button-press event
that was saved in saved_menu_event.
That makes the toolkit do its thing.
But first we recompute the menu bar contents (the whole tree).
The reason for saving the button event until here, instead of
passing it to the toolkit right away, is that we can safely
execute Lisp code. */
void
x_activate_menubar (FRAME_PTR f)
{
if (! FRAME_X_P (f))
abort ();
if (!f->output_data.x->saved_menu_event->type)
return;
#ifdef USE_GTK
if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
f->output_data.x->saved_menu_event->xany.window))
return;
#endif
set_frame_menubar (f, 0, 1);
BLOCK_INPUT;
popup_activated_flag = 1;
#ifdef USE_GTK
XPutBackEvent (f->output_data.x->display_info->display,
f->output_data.x->saved_menu_event);
#else
XtDispatchEvent (f->output_data.x->saved_menu_event);
#endif
UNBLOCK_INPUT;
/* Ignore this if we get it a second time. */
f->output_data.x->saved_menu_event->type = 0;
}
/* This callback is invoked when the user selects a menubar cascade
pushbutton, but before the pulldown menu is posted. */
#ifndef USE_GTK
static void
popup_activate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
{
popup_activated_flag = 1;
#ifdef USE_X_TOOLKIT
x_activate_timeout_atimer ();
#endif
}
#endif
/* This callback is invoked when a dialog or menu is finished being
used and has been unposted. */
#ifdef USE_GTK
static void
popup_deactivate_callback (GtkWidget *widget, gpointer client_data)
{
popup_activated_flag = 0;
}
#else
static void
popup_deactivate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
{
popup_activated_flag = 0;
}
#endif
/* Function that finds the frame for WIDGET and shows the HELP text
for that widget.
F is the frame if known, or NULL if not known. */
static void
show_help_event (FRAME_PTR f, xt_or_gtk_widget widget, Lisp_Object help)
{
Lisp_Object frame;
if (f)
{
XSETFRAME (frame, f);
kbd_buffer_store_help_event (frame, help);
}
else
{
#if 0 /* This code doesn't do anything useful. ++kfs */
/* WIDGET is the popup menu. It's parent is the frame's
widget. See which frame that is. */
xt_or_gtk_widget frame_widget = XtParent (widget);
Lisp_Object tail;
for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
{
frame = XCAR (tail);
if (FRAMEP (frame)
&& (f = XFRAME (frame),
FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
break;
}
#endif
show_help_echo (help, Qnil, Qnil, Qnil);
}
}
/* Callback called when menu items are highlighted/unhighlighted
while moving the mouse over them. WIDGET is the menu bar or menu
popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
the data structure for the menu item, or null in case of
unhighlighting. */
#ifdef USE_GTK
static void
menu_highlight_callback (GtkWidget *widget, gpointer call_data)
{
xg_menu_item_cb_data *cb_data;
Lisp_Object help;
cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
XG_ITEM_DATA);
if (! cb_data) return;
help = call_data ? cb_data->help : Qnil;
/* If popup_activated_flag is greater than 1 we are in a popup menu.
Don't pass the frame to show_help_event for those.
Passing frame creates an Emacs event. As we are looping in
popup_widget_loop, it won't be handeled. Passing NULL shows the tip
directly without using an Emacs event. This is what the Lucid code
does below. */
show_help_event (popup_activated_flag <= 1 ? cb_data->cl_data->f : NULL,
widget, help);
}
#else
static void
menu_highlight_callback (Widget widget, LWLIB_ID id, void *call_data)
{
struct frame *f;
Lisp_Object help;
widget_value *wv = (widget_value *) call_data;
help = wv ? wv->help : Qnil;
/* Determine the frame for the help event. */
f = menubar_id_to_frame (id);
show_help_event (f, widget, help);
}
#endif
#ifdef USE_GTK
/* Gtk calls callbacks just because we tell it what item should be
selected in a radio group. If this variable is set to a non-zero
value, we are creating menus and don't want callbacks right now.
*/
static int xg_crazy_callback_abort;
/* This callback is called from the menu bar pulldown menu
when the user makes a selection.
Figure out what the user chose
and put the appropriate events into the keyboard buffer. */
static void
menubar_selection_callback (GtkWidget *widget, gpointer client_data)
{
xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
if (xg_crazy_callback_abort)
return;
if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
return;
/* For a group of radio buttons, GTK calls the selection callback first
for the item that was active before the selection and then for the one that
is active after the selection. For C-h k this means we get the help on
the deselected item and then the selected item is executed. Prevent that
by ignoring the non-active item. */
if (GTK_IS_RADIO_MENU_ITEM (widget)
&& ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
return;
/* When a menu is popped down, X generates a focus event (i.e. focus
goes back to the frame below the menu). Since GTK buffers events,
we force it out here before the menu selection event. Otherwise
sit-for will exit at once if the focus event follows the menu selection
event. */
BLOCK_INPUT;
while (gtk_events_pending ())
gtk_main_iteration ();
UNBLOCK_INPUT;
find_and_call_menu_selection (cb_data->cl_data->f,
cb_data->cl_data->menu_bar_items_used,
cb_data->cl_data->menu_bar_vector,
cb_data->call_data);
}
#else /* not USE_GTK */
/* This callback is called from the menu bar pulldown menu
when the user makes a selection.
Figure out what the user chose
and put the appropriate events into the keyboard buffer. */
static void
menubar_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
{
FRAME_PTR f;
f = menubar_id_to_frame (id);
if (!f)
return;
find_and_call_menu_selection (f, f->menu_bar_items_used,
f->menu_bar_vector, client_data);
}
#endif /* not USE_GTK */
/* Recompute all the widgets of frame F, when the menu bar has been
changed. Value is non-zero if widgets were updated. */
static int
update_frame_menubar (FRAME_PTR f)
{
#ifdef USE_GTK
return xg_update_frame_menubar (f);
#else
struct x_output *x;
int columns, rows;
if (! FRAME_X_P (f))
abort ();
x = f->output_data.x;
if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
return 0;
BLOCK_INPUT;
/* Save the size of the frame because the pane widget doesn't accept
to resize itself. So force it. */
columns = FRAME_COLS (f);
rows = FRAME_LINES (f);
/* Do the voodoo which means "I'm changing lots of things, don't try
to refigure sizes until I'm done." */
lw_refigure_widget (x->column_widget, False);
/* The order in which children are managed is the top to bottom
order in which they are displayed in the paned window. First,
remove the text-area widget. */
XtUnmanageChild (x->edit_widget);
/* Remove the menubar that is there now, and put up the menubar that
should be there. */
XtManageChild (x->menubar_widget);
XtMapWidget (x->menubar_widget);
XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
/* Re-manage the text-area widget, and then thrash the sizes. */
XtManageChild (x->edit_widget);
lw_refigure_widget (x->column_widget, True);
/* Force the pane widget to resize itself with the right values. */
EmacsFrameSetCharSize (x->edit_widget, columns, rows);
UNBLOCK_INPUT;
#endif
return 1;
}
#ifdef USE_LUCID
static void
apply_systemfont_to_dialog (Widget w)
{
const char *fn = xsettings_get_system_normal_font ();
if (fn)
{
XrmDatabase db = XtDatabase (XtDisplay (w));
if (db)
XrmPutStringResource (&db, "*dialog.font", fn);
}
}
static void
apply_systemfont_to_menu (struct frame *f, Widget w)
{
const char *fn = xsettings_get_system_normal_font ();
if (fn)
{
XrmDatabase db = XtDatabase (XtDisplay (w));
if (db)
{
XrmPutStringResource (&db, "*menubar*font", fn);
XrmPutStringResource (&db, "*popup*font", fn);
}
}
}
#endif
/* Set the contents of the menubar widgets of frame F.
The argument FIRST_TIME is currently ignored;
it is set the first time this is called, from initialize_frame_menubar. */
void
set_frame_menubar (FRAME_PTR f, int first_time, int deep_p)
{
xt_or_gtk_widget menubar_widget;
#ifdef USE_X_TOOLKIT
LWLIB_ID id;
#endif
Lisp_Object items;
widget_value *wv, *first_wv, *prev_wv = 0;
int i;
int *submenu_start, *submenu_end;
int *submenu_top_level_items, *submenu_n_panes;
if (! FRAME_X_P (f))
abort ();
menubar_widget = f->output_data.x->menubar_widget;
XSETFRAME (Vmenu_updating_frame, f);
#ifdef USE_X_TOOLKIT
if (f->output_data.x->id == 0)
f->output_data.x->id = next_menubar_widget_id++;
id = f->output_data.x->id;
#endif
if (! menubar_widget)
deep_p = 1;
/* Make the first call for any given frame always go deep. */
else if (!f->output_data.x->saved_menu_event && !deep_p)
{
deep_p = 1;
f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
f->output_data.x->saved_menu_event->type = 0;
}
#ifdef USE_GTK
/* If we have detached menus, we must update deep so detached menus
also gets updated. */
deep_p = deep_p || xg_have_tear_offs ();
#endif
if (deep_p)
{
/* Make a widget-value tree representing the entire menu trees. */
struct buffer *prev = current_buffer;
Lisp_Object buffer;
int specpdl_count = SPECPDL_INDEX ();
int previous_menu_items_used = f->menu_bar_items_used;
Lisp_Object *previous_items
= (Lisp_Object *) alloca (previous_menu_items_used
* sizeof (Lisp_Object));
int subitems;
/* If we are making a new widget, its contents are empty,
do always reinitialize them. */
if (! menubar_widget)
previous_menu_items_used = 0;
buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
specbind (Qinhibit_quit, Qt);
/* Don't let the debugger step into this code
because it is not reentrant. */
specbind (Qdebug_on_next_call, Qnil);
record_unwind_save_match_data ();
if (NILP (Voverriding_local_map_menu_flag))
{
specbind (Qoverriding_terminal_local_map, Qnil);
specbind (Qoverriding_local_map, Qnil);
}
set_buffer_internal_1 (XBUFFER (buffer));
/* Run the Lucid hook. */
safe_run_hooks (Qactivate_menubar_hook);