* fileio.c (Fread_file_name): Pass Qt as fifth parameter to
[bpt/emacs.git] / src / gtkutil.c
1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003
3 Free 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 2, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23
24 #ifdef USE_GTK
25 #include <string.h>
26 #include <stdio.h>
27 #include "lisp.h"
28 #include "xterm.h"
29 #include "blockinput.h"
30 #include "window.h"
31 #include "atimer.h"
32 #include "gtkutil.h"
33 #include "termhooks.h"
34 #include "keyboard.h"
35 #include "charset.h"
36 #include "coding.h"
37 #include <gdk/gdkkeysyms.h>
38
39
40 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
41 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
42
43 \f
44 /***********************************************************************
45 Display handling functions
46 ***********************************************************************/
47
48 #ifdef HAVE_GTK_MULTIDISPLAY
49
50 /* Return the GdkDisplay that corresponds to the X display DPY. */
51
52 static GdkDisplay *
53 xg_get_gdk_display (dpy)
54 Display *dpy;
55 {
56 return gdk_x11_lookup_xdisplay (dpy);
57 }
58
59 /* When the GTK widget W is to be created on a display for F that
60 is not the default display, set the display for W.
61 W can be a GtkMenu or a GtkWindow widget. */
62
63 static void
64 xg_set_screen (w, f)
65 GtkWidget *w;
66 FRAME_PTR f;
67 {
68 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
69 {
70 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
71 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
72
73 if (GTK_IS_MENU (w))
74 gtk_menu_set_screen (GTK_MENU (w), gscreen);
75 else
76 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
77 }
78 }
79
80
81 #else /* not HAVE_GTK_MULTIDISPLAY */
82
83 /* Make some defines so we can use the GTK 2.2 functions when
84 compiling with GTK 2.0. */
85
86 #define xg_set_screen(w, f)
87 #define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
88 #define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
89 #define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
90 #define gdk_x11_lookup_xdisplay(dpy) 0
91 #define GdkDisplay void
92
93 #endif /* not HAVE_GTK_MULTIDISPLAY */
94
95 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
96 *DPY is set to NULL if the display can't be opened.
97
98 Returns non-zero if display could be opened, zero if display could not
99 be opened, and less than zero if the GTK version doesn't support
100 multipe displays. */
101
102 int
103 xg_display_open (display_name, dpy)
104 char *display_name;
105 Display **dpy;
106 {
107 #ifdef HAVE_GTK_MULTIDISPLAY
108 GdkDisplay *gdpy;
109
110 gdpy = gdk_display_open (display_name);
111 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
112
113 return gdpy != NULL;
114
115 #else /* not HAVE_GTK_MULTIDISPLAY */
116
117 return -1;
118 #endif /* not HAVE_GTK_MULTIDISPLAY */
119 }
120
121
122 /* Close display DPY. */
123
124 void
125 xg_display_close (Display *dpy)
126 {
127 #ifdef HAVE_GTK_MULTIDISPLAY
128 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
129
130 /* GTK 2.2 has a bug that makes gdk_display_close crash (bug
131 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way
132 we can continue running, but there will be memory leaks. */
133
134 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4
135
136 /* If this is the default display, we must change it before calling
137 dispose, otherwise it will crash. */
138 if (gdk_display_get_default () == gdpy)
139 {
140 struct x_display_info *dpyinfo;
141 Display *new_dpy = 0;
142 GdkDisplay *gdpy_new;
143
144 /* Find another display. */
145 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
146 if (dpyinfo->display != dpy)
147 {
148 new_dpy = dpyinfo->display;
149 break;
150 }
151
152 if (! new_dpy) return; /* Emacs will exit anyway. */
153
154 gdpy_new = gdk_x11_lookup_xdisplay (new_dpy);
155 gdk_display_manager_set_default_display (gdk_display_manager_get (),
156 gdpy_new);
157 }
158
159 g_object_run_dispose (G_OBJECT (gdpy));
160
161 #else
162 /* I hope this will be fixed in GTK 2.4. It is what bug 85715 says. */
163 gdk_display_close (gdpy);
164 #endif
165 #endif /* HAVE_GTK_MULTIDISPLAY */
166 }
167
168 \f
169 /***********************************************************************
170 Utility functions
171 ***********************************************************************/
172 /* The timer for scroll bar repetition and menu bar timeouts.
173 NULL if no timer is started. */
174 static struct atimer *xg_timer;
175
176
177 /* The next two variables and functions are taken from lwlib. */
178 static widget_value *widget_value_free_list;
179 static int malloc_cpt;
180
181 /* Allocate a widget_value structure, either by taking one from the
182 widget_value_free_list or by malloc:ing a new one.
183
184 Return a pointer to the allocated structure. */
185
186 widget_value *
187 malloc_widget_value ()
188 {
189 widget_value *wv;
190 if (widget_value_free_list)
191 {
192 wv = widget_value_free_list;
193 widget_value_free_list = wv->free_list;
194 wv->free_list = 0;
195 }
196 else
197 {
198 wv = (widget_value *) malloc (sizeof (widget_value));
199 malloc_cpt++;
200 }
201 memset (wv, 0, sizeof (widget_value));
202 return wv;
203 }
204
205 /* This is analogous to free. It frees only what was allocated
206 by malloc_widget_value, and no substructures. */
207
208 void
209 free_widget_value (wv)
210 widget_value *wv;
211 {
212 if (wv->free_list)
213 abort ();
214
215 if (malloc_cpt > 25)
216 {
217 /* When the number of already allocated cells is too big,
218 We free it. */
219 free (wv);
220 malloc_cpt--;
221 }
222 else
223 {
224 wv->free_list = widget_value_free_list;
225 widget_value_free_list = wv;
226 }
227 }
228
229
230 /* Create and return the cursor to be used for popup menus and
231 scroll bars on display DPY. */
232
233 GdkCursor *
234 xg_create_default_cursor (dpy)
235 Display *dpy;
236 {
237 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
238 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
239 }
240
241 /* For the image defined in IMG, make and return a GtkImage. For displays with
242 8 planes or less we must make a GdkPixbuf and apply the mask manually.
243 Otherwise the highlightning and dimming the tool bar code in GTK does
244 will look bad. For display with more than 8 planes we just use the
245 pixmap and mask directly. For monochrome displays, GTK doesn't seem
246 able to use external pixmaps, it looks bad whatever we do.
247 The image is defined on the display where frame F is.
248 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
249 If OLD_WIDGET is NULL, a new widget is constructed and returned.
250 If OLD_WIDGET is not NULL, that widget is modified. */
251
252 static GtkWidget *
253 xg_get_image_for_pixmap (f, img, widget, old_widget)
254 FRAME_PTR f;
255 struct image *img;
256 GtkWidget *widget;
257 GtkImage *old_widget;
258 {
259 GdkPixmap *gpix;
260 GdkPixmap *gmask;
261 GdkDisplay *gdpy;
262
263 /* If we are on a one bit display, let GTK do all the image handling.
264 This seems to be the only way to make insensitive and activated icons
265 look good. */
266 if (x_screen_planes (f) == 1)
267 {
268 Lisp_Object specified_file = Qnil;
269 Lisp_Object tail;
270 extern Lisp_Object QCfile;
271
272 for (tail = XCDR (img->spec);
273 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
274 tail = XCDR (XCDR (tail)))
275 if (EQ (XCAR (tail), QCfile))
276 specified_file = XCAR (XCDR (tail));
277
278 if (STRINGP (specified_file))
279 {
280
281 Lisp_Object file = Qnil;
282 struct gcpro gcpro1;
283 GCPRO1 (file);
284
285 file = x_find_image_file (specified_file);
286 /* We already loaded the image once before calling this
287 function, so this should not fail. */
288 xassert (STRINGP (file) != 0);
289
290 if (! old_widget)
291 old_widget = GTK_IMAGE (gtk_image_new_from_file (SDATA (file)));
292 else
293 gtk_image_set_from_file (old_widget, SDATA (file));
294
295 UNGCPRO;
296 return GTK_WIDGET (old_widget);
297 }
298 }
299
300 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
301 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
302 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
303
304 if (x_screen_planes (f) > 8 || x_screen_planes (f) == 1)
305 {
306 if (! old_widget)
307 old_widget = GTK_IMAGE (gtk_image_new_from_pixmap (gpix, gmask));
308 else
309 gtk_image_set_from_pixmap (old_widget, gpix, gmask);
310 }
311 else
312 {
313 /* This is a workaround to make icons look good on pseudo color
314 displays. Apparently GTK expects the images to have an alpha
315 channel. If they don't, insensitive and activated icons will
316 look bad. This workaround does not work on monochrome displays,
317 and is not needed on true color/static color displays (i.e.
318 16 bits and higher). */
319 int x, y, width, height, rowstride, mask_rowstride;
320 GdkPixbuf *icon_buf, *tmp_buf;
321 guchar *pixels;
322 guchar *mask_pixels;
323
324 gdk_drawable_get_size (gpix, &width, &height);
325 tmp_buf = gdk_pixbuf_get_from_drawable (NULL,
326 gpix,
327 gtk_widget_get_colormap (widget),
328 0, 0, 0, 0, width, height);
329 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
330 g_object_unref (G_OBJECT (tmp_buf));
331
332 if (gmask)
333 {
334 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
335 gmask,
336 NULL,
337 0, 0, 0, 0,
338 width, height);
339 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
340 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
341 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
342 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
343 int y;
344
345 for (y = 0; y < height; ++y)
346 {
347 guchar *iconptr, *maskptr;
348 int x;
349
350 iconptr = pixels + y * rowstride;
351 maskptr = mask_pixels + y * mask_rowstride;
352
353 for (x = 0; x < width; ++x)
354 {
355 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
356 just R is sufficient. */
357 if (maskptr[0] == 0)
358 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
359
360 iconptr += rowstride/width;
361 maskptr += mask_rowstride/width;
362 }
363 }
364
365 g_object_unref (G_OBJECT (mask_buf));
366 }
367
368 if (! old_widget)
369 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
370 else
371 gtk_image_set_from_pixbuf (old_widget, icon_buf);
372
373 g_object_unref (G_OBJECT (icon_buf));
374 }
375
376 g_object_unref (G_OBJECT (gpix));
377 if (gmask) g_object_unref (G_OBJECT (gmask));
378
379 return GTK_WIDGET (old_widget);
380 }
381
382
383 /* Set CURSOR on W and all widgets W contain. We must do like this
384 for scroll bars and menu because they create widgets internally,
385 and it is those widgets that are visible. */
386
387 static void
388 xg_set_cursor (w, cursor)
389 GtkWidget *w;
390 GdkCursor *cursor;
391 {
392 GList *children = gdk_window_peek_children (w->window);
393
394 gdk_window_set_cursor (w->window, cursor);
395
396 /* The scroll bar widget has more than one GDK window (had to look at
397 the source to figure this out), and there is no way to set cursor
398 on widgets in GTK. So we must set the cursor for all GDK windows.
399 Ditto for menus. */
400
401 for ( ; children; children = g_list_next (children))
402 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
403 }
404
405 /* Timer function called when a timeout occurs for xg_timer.
406 This function processes all GTK events in a recursive event loop.
407 This is done because GTK timer events are not seen by Emacs event
408 detection, Emacs only looks for X events. When a scroll bar has the
409 pointer (detected by button press/release events below) an Emacs
410 timer is started, and this function can then check if the GTK timer
411 has expired by calling the GTK event loop.
412 Also, when a menu is active, it has a small timeout before it
413 pops down the sub menu under it. */
414
415 static void
416 xg_process_timeouts (timer)
417 struct atimer *timer;
418 {
419 BLOCK_INPUT;
420 /* Ideally we would like to just handle timer events, like the Xt version
421 of this does in xterm.c, but there is no such feature in GTK. */
422 while (gtk_events_pending ())
423 gtk_main_iteration ();
424 UNBLOCK_INPUT;
425 }
426
427 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
428 xg_process_timeouts is called when the timer expires. The timer
429 started is continuous, i.e. runs until xg_stop_timer is called. */
430
431 static void
432 xg_start_timer ()
433 {
434 if (! xg_timer)
435 {
436 EMACS_TIME interval;
437 EMACS_SET_SECS_USECS (interval, 0, 100000);
438 xg_timer = start_atimer (ATIMER_CONTINUOUS,
439 interval,
440 xg_process_timeouts,
441 0);
442 }
443 }
444
445 /* Stop the xg_timer if started. */
446
447 static void
448 xg_stop_timer ()
449 {
450 if (xg_timer)
451 {
452 cancel_atimer (xg_timer);
453 xg_timer = 0;
454 }
455 }
456
457 /* Insert NODE into linked LIST. */
458
459 static void
460 xg_list_insert (xg_list_node *list, xg_list_node *node)
461 {
462 xg_list_node *list_start = list->next;
463
464 if (list_start) list_start->prev = node;
465 node->next = list_start;
466 node->prev = 0;
467 list->next = node;
468 }
469
470 /* Remove NODE from linked LIST. */
471
472 static void
473 xg_list_remove (xg_list_node *list, xg_list_node *node)
474 {
475 xg_list_node *list_start = list->next;
476 if (node == list_start)
477 {
478 list->next = node->next;
479 if (list->next) list->next->prev = 0;
480 }
481 else
482 {
483 node->prev->next = node->next;
484 if (node->next) node->next->prev = node->prev;
485 }
486 }
487
488 /* Allocate and return a utf8 version of STR. If STR is already
489 utf8 or NULL, just return STR.
490 If not, a new string is allocated and the caller must free the result
491 with g_free. */
492
493 static char *
494 get_utf8_string (str)
495 char *str;
496 {
497 char *utf8_str = str;
498
499 /* If not UTF-8, try current locale. */
500 if (str && !g_utf8_validate (str, -1, NULL))
501 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
502
503 return utf8_str;
504 }
505
506
507 \f
508 /***********************************************************************
509 General functions for creating widgets, resizing, events, e.t.c.
510 ***********************************************************************/
511
512 /* Make a geometry string and pass that to GTK. It seems this is the
513 only way to get geometry position right if the user explicitly
514 asked for a position when starting Emacs.
515 F is the frame we shall set geometry for. */
516
517 static void
518 xg_set_geometry (f)
519 FRAME_PTR f;
520 {
521 if (f->size_hint_flags & USPosition)
522 {
523 int left = f->left_pos;
524 int xneg = f->size_hint_flags & XNegative;
525 int top = f->top_pos;
526 int yneg = f->size_hint_flags & YNegative;
527 char geom_str[32];
528
529 if (xneg)
530 left = -left;
531 if (yneg)
532 top = -top;
533
534 sprintf (geom_str, "=%dx%d%c%d%c%d",
535 FRAME_PIXEL_WIDTH (f),
536 FRAME_TOTAL_PIXEL_HEIGHT (f),
537 (xneg ? '-' : '+'), left,
538 (yneg ? '-' : '+'), top);
539
540 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
541 geom_str))
542 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
543 }
544 }
545
546
547 /* Resize the outer window of frame F after chainging the height.
548 This happend when the menu bar or the tool bar is added or removed.
549 COLUMNS/ROWS is the size the edit area shall have after the resize. */
550
551 static void
552 xg_resize_outer_widget (f, columns, rows)
553 FRAME_PTR f;
554 int columns;
555 int rows;
556 {
557 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
558 FRAME_PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
559
560 /* base_height is now changed. */
561 x_wm_set_size_hint (f, 0, 0);
562
563 /* If we are not mapped yet, set geometry once again, as window
564 height now have changed. */
565 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
566 xg_set_geometry (f);
567
568 xg_frame_set_char_size (f, columns, rows);
569 gdk_window_process_all_updates ();
570 }
571
572 /* Function to handle resize of our widgets. Since Emacs has some layouts
573 that does not fit well with GTK standard containers, we do most layout
574 manually.
575 F is the frame to resize.
576 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
577
578 void
579 xg_resize_widgets (f, pixelwidth, pixelheight)
580 FRAME_PTR f;
581 int pixelwidth, pixelheight;
582 {
583 int mbheight = FRAME_MENUBAR_HEIGHT (f);
584 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
585 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight
586 - mbheight - tbheight));
587 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
588
589 if (FRAME_GTK_WIDGET (f)
590 && (columns != FRAME_COLS (f)
591 || rows != FRAME_LINES (f)
592 || pixelwidth != FRAME_PIXEL_WIDTH (f)
593 || pixelheight != FRAME_PIXEL_HEIGHT (f)))
594 {
595 struct x_output *x = f->output_data.x;
596 GtkAllocation all;
597
598 all.y = mbheight + tbheight;
599 all.x = 0;
600
601 all.width = pixelwidth;
602 all.height = pixelheight - mbheight - tbheight;
603
604 gtk_widget_size_allocate (x->edit_widget, &all);
605
606 change_frame_size (f, rows, columns, 0, 1, 0);
607 SET_FRAME_GARBAGED (f);
608 cancel_mouse_face (f);
609 }
610 }
611
612
613 /* Update our widget size to be COLS/ROWS characters for frame F. */
614
615 void
616 xg_frame_set_char_size (f, cols, rows)
617 FRAME_PTR f;
618 int cols;
619 int rows;
620 {
621 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
622 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
623 int pixelwidth;
624
625 /* Take into account the size of the scroll bar. Always use the
626 number of columns occupied by the scroll bar here otherwise we
627 might end up with a frame width that is not a multiple of the
628 frame's character width which is bad for vertically split
629 windows. */
630 f->scroll_bar_actual_width
631 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
632
633 compute_fringe_widths (f, 0);
634
635 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
636 after calculating that value. */
637 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
638
639 /* Must resize our top level widget. Font size may have changed,
640 but not rows/cols. */
641 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
642 pixelwidth, pixelheight);
643 xg_resize_widgets (f, pixelwidth, pixelheight);
644 x_wm_set_size_hint (f, 0, 0);
645 SET_FRAME_GARBAGED (f);
646 cancel_mouse_face (f);
647 }
648
649 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
650 Must be done like this, because GtkWidget:s can have "hidden"
651 X Window that aren't accessible.
652
653 Return 0 if no widget match WDESC. */
654
655 GtkWidget *
656 xg_win_to_widget (dpy, wdesc)
657 Display *dpy;
658 Window wdesc;
659 {
660 gpointer gdkwin;
661 GtkWidget *gwdesc = 0;
662
663 BLOCK_INPUT;
664
665 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
666 wdesc);
667 if (gdkwin)
668 {
669 GdkEvent event;
670 event.any.window = gdkwin;
671 gwdesc = gtk_get_event_widget (&event);
672 }
673
674 UNBLOCK_INPUT;
675 return gwdesc;
676 }
677
678 /* Fill in the GdkColor C so that it represents PIXEL.
679 W is the widget that color will be used for. Used to find colormap. */
680
681 static void
682 xg_pix_to_gcolor (w, pixel, c)
683 GtkWidget *w;
684 unsigned long pixel;
685 GdkColor *c;
686 {
687 GdkColormap *map = gtk_widget_get_colormap (w);
688 gdk_colormap_query_color (map, pixel, c);
689 }
690
691 /* Create and set up the GTK widgets for frame F.
692 Return 0 if creation failed, non-zero otherwise. */
693
694 int
695 xg_create_frame_widgets (f)
696 FRAME_PTR f;
697 {
698 GtkWidget *wtop;
699 GtkWidget *wvbox;
700 GtkWidget *wfixed;
701 GdkColor bg;
702 GtkRcStyle *style;
703 int i;
704 char *title = 0;
705
706 BLOCK_INPUT;
707
708 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
709 xg_set_screen (wtop, f);
710
711 wvbox = gtk_vbox_new (FALSE, 0);
712 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
713
714 if (! wtop || ! wvbox || ! wfixed)
715 {
716 if (wtop) gtk_widget_destroy (wtop);
717 if (wvbox) gtk_widget_destroy (wvbox);
718 if (wfixed) gtk_widget_destroy (wfixed);
719
720 return 0;
721 }
722
723 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
724 gtk_widget_set_name (wtop, EMACS_CLASS);
725 gtk_widget_set_name (wvbox, "pane");
726 gtk_widget_set_name (wfixed, SDATA (Vx_resource_name));
727
728 /* If this frame has a title or name, set it in the title bar. */
729 if (! NILP (f->title)) title = SDATA (ENCODE_UTF_8 (f->title));
730 else if (! NILP (f->name)) title = SDATA (ENCODE_UTF_8 (f->name));
731
732 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
733
734 FRAME_GTK_OUTER_WIDGET (f) = wtop;
735 FRAME_GTK_WIDGET (f) = wfixed;
736 f->output_data.x->vbox_widget = wvbox;
737
738 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
739
740 gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f),
741 FRAME_PIXEL_HEIGHT (f));
742
743 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
744 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
745
746 if (FRAME_EXTERNAL_TOOL_BAR (f))
747 update_frame_tool_bar (f);
748
749 /* The tool bar is created but first there are no items in it.
750 This causes it to be zero height. Later items are added, but then
751 the frame is already mapped, so there is a "jumping" resize.
752 This makes geometry handling difficult, for example -0-0 will end
753 up in the wrong place as tool bar height has not been taken into account.
754 So we cheat a bit by setting a height that is what it will have
755 later on when tool bar items are added. */
756 if (FRAME_EXTERNAL_TOOL_BAR (f) && f->n_tool_bar_items == 0)
757 FRAME_TOOLBAR_HEIGHT (f) = 34;
758
759
760 /* We don't want this widget double buffered, because we draw on it
761 with regular X drawing primitives, so from a GTK/GDK point of
762 view, the widget is totally blank. When an expose comes, this
763 will make the widget blank, and then Emacs redraws it. This flickers
764 a lot, so we turn off double buffering. */
765 gtk_widget_set_double_buffered (wfixed, FALSE);
766
767 /* GTK documents says use gtk_window_set_resizable. But then a user
768 can't shrink the window from its starting size. */
769 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
770 gtk_window_set_wmclass (GTK_WINDOW (wtop),
771 SDATA (Vx_resource_name),
772 SDATA (Vx_resource_class));
773
774 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
775 GTK is to destroy the widget. We want Emacs to do that instead. */
776 g_signal_connect (G_OBJECT (wtop), "delete-event",
777 G_CALLBACK (gtk_true), 0);
778
779 /* Convert our geometry parameters into a geometry string
780 and specify it.
781 GTK will itself handle calculating the real position this way. */
782 xg_set_geometry (f);
783
784 gtk_widget_add_events (wfixed,
785 GDK_POINTER_MOTION_MASK
786 | GDK_EXPOSURE_MASK
787 | GDK_BUTTON_PRESS_MASK
788 | GDK_BUTTON_RELEASE_MASK
789 | GDK_KEY_PRESS_MASK
790 | GDK_ENTER_NOTIFY_MASK
791 | GDK_LEAVE_NOTIFY_MASK
792 | GDK_FOCUS_CHANGE_MASK
793 | GDK_STRUCTURE_MASK
794 | GDK_VISIBILITY_NOTIFY_MASK);
795
796 /* Must realize the windows so the X window gets created. It is used
797 by callers of this function. */
798 gtk_widget_realize (wfixed);
799 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
800
801 /* Since GTK clears its window by filling with the background color,
802 we must keep X and GTK background in sync. */
803 xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg);
804 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
805
806 /* Also, do not let any background pixmap to be set, this looks very
807 bad as Emacs overwrites the background pixmap with its own idea
808 of background color. */
809 style = gtk_widget_get_modifier_style (wfixed);
810
811 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
812 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
813 gtk_widget_modify_style (wfixed, style);
814
815 /* GTK does not set any border, and they look bad with GTK. */
816 f->border_width = 0;
817 f->internal_border_width = 0;
818
819 UNBLOCK_INPUT;
820
821 return 1;
822 }
823
824 /* Set the normal size hints for the window manager, for frame F.
825 FLAGS is the flags word to use--or 0 meaning preserve the flags
826 that the window now has.
827 If USER_POSITION is nonzero, we set the User Position
828 flag (this is useful when FLAGS is 0). */
829
830 void
831 x_wm_set_size_hint (f, flags, user_position)
832 FRAME_PTR f;
833 long flags;
834 int user_position;
835 {
836 if (FRAME_GTK_OUTER_WIDGET (f))
837 {
838 /* Must use GTK routines here, otherwise GTK resets the size hints
839 to its own defaults. */
840 GdkGeometry size_hints;
841 gint hint_flags = 0;
842 int base_width, base_height;
843 int min_rows = 0, min_cols = 0;
844 int win_gravity = f->win_gravity;
845
846 if (flags)
847 {
848 memset (&size_hints, 0, sizeof (size_hints));
849 f->output_data.x->size_hints = size_hints;
850 f->output_data.x->hint_flags = hint_flags;
851 }
852 else
853 flags = f->size_hint_flags;
854
855 size_hints = f->output_data.x->size_hints;
856 hint_flags = f->output_data.x->hint_flags;
857
858 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
859 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
860 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
861
862 hint_flags |= GDK_HINT_BASE_SIZE;
863 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
864 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
865 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
866
867 check_frame_size (f, &min_rows, &min_cols);
868
869 size_hints.base_width = base_width;
870 size_hints.base_height = base_height;
871 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
872 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
873
874
875 /* These currently have a one to one mapping with the X values, but I
876 don't think we should rely on that. */
877 hint_flags |= GDK_HINT_WIN_GRAVITY;
878 size_hints.win_gravity = 0;
879 if (win_gravity == NorthWestGravity)
880 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
881 else if (win_gravity == NorthGravity)
882 size_hints.win_gravity = GDK_GRAVITY_NORTH;
883 else if (win_gravity == NorthEastGravity)
884 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
885 else if (win_gravity == WestGravity)
886 size_hints.win_gravity = GDK_GRAVITY_WEST;
887 else if (win_gravity == CenterGravity)
888 size_hints.win_gravity = GDK_GRAVITY_CENTER;
889 else if (win_gravity == EastGravity)
890 size_hints.win_gravity = GDK_GRAVITY_EAST;
891 else if (win_gravity == SouthWestGravity)
892 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
893 else if (win_gravity == SouthGravity)
894 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
895 else if (win_gravity == SouthEastGravity)
896 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
897 else if (win_gravity == StaticGravity)
898 size_hints.win_gravity = GDK_GRAVITY_STATIC;
899
900 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
901 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
902 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
903
904 if (user_position)
905 {
906 hint_flags &= ~GDK_HINT_POS;
907 hint_flags |= GDK_HINT_USER_POS;
908 }
909
910 BLOCK_INPUT;
911
912 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
913 FRAME_GTK_OUTER_WIDGET (f),
914 &size_hints,
915 hint_flags);
916
917 f->output_data.x->size_hints = size_hints;
918 f->output_data.x->hint_flags = hint_flags;
919 UNBLOCK_INPUT;
920 }
921 }
922
923 /* Change background color of a frame.
924 Since GTK uses the background colour to clear the window, we must
925 keep the GTK and X colors in sync.
926 F is the frame to change,
927 BG is the pixel value to change to. */
928
929 void
930 xg_set_background_color (f, bg)
931 FRAME_PTR f;
932 unsigned long bg;
933 {
934 if (FRAME_GTK_WIDGET (f))
935 {
936 GdkColor gdk_bg;
937
938 BLOCK_INPUT;
939 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
940 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
941 UNBLOCK_INPUT;
942 }
943 }
944
945
946 \f
947 /***********************************************************************
948 Dialog functions
949 ***********************************************************************/
950 /* Return the dialog title to use for a dialog of type KEY.
951 This is the encoding used by lwlib. We use the same for GTK. */
952
953 static char *
954 get_dialog_title (char key)
955 {
956 char *title = "";
957
958 switch (key) {
959 case 'E': case 'e':
960 title = "Error";
961 break;
962
963 case 'I': case 'i':
964 title = "Information";
965 break;
966
967 case 'L': case 'l':
968 title = "Prompt";
969 break;
970
971 case 'P': case 'p':
972 title = "Prompt";
973 break;
974
975 case 'Q': case 'q':
976 title = "Question";
977 break;
978 }
979
980 return title;
981 }
982
983 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
984 the dialog, but return TRUE so the event does not propagate further
985 in GTK. This prevents GTK from destroying the dialog widget automatically
986 and we can always destrou the widget manually, regardles of how
987 it was popped down (button press or WM_DELETE_WINDOW).
988 W is the dialog widget.
989 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
990 user_data is NULL (not used).
991
992 Returns TRUE to end propagation of event. */
993
994 static gboolean
995 dialog_delete_callback (w, event, user_data)
996 GtkWidget *w;
997 GdkEvent *event;
998 gpointer user_data;
999 {
1000 gtk_widget_unmap (w);
1001 return TRUE;
1002 }
1003
1004 /* Create a popup dialog window. See also xg_create_widget below.
1005 WV is a widget_value describing the dialog.
1006 SELECT_CB is the callback to use when a button has been pressed.
1007 DEACTIVATE_CB is the callback to use when the dialog pops down.
1008
1009 Returns the GTK dialog widget. */
1010
1011 static GtkWidget *
1012 create_dialog (wv, select_cb, deactivate_cb)
1013 widget_value *wv;
1014 GCallback select_cb;
1015 GCallback deactivate_cb;
1016 {
1017 char *title = get_dialog_title (wv->name[0]);
1018 int total_buttons = wv->name[1] - '0';
1019 int right_buttons = wv->name[4] - '0';
1020 int left_buttons;
1021 int button_nr = 0;
1022 int button_spacing = 10;
1023 GtkWidget *wdialog = gtk_dialog_new ();
1024 widget_value *item;
1025 GtkBox *cur_box;
1026 GtkWidget *wvbox;
1027 GtkWidget *whbox_up;
1028 GtkWidget *whbox_down;
1029
1030 /* If the number of buttons is greater than 4, make two rows of buttons
1031 instead. This looks better. */
1032 int make_two_rows = total_buttons > 4;
1033
1034 if (right_buttons == 0) right_buttons = total_buttons/2;
1035 left_buttons = total_buttons - right_buttons;
1036
1037 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1038 gtk_widget_set_name (wdialog, "emacs-dialog");
1039
1040 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
1041
1042 if (make_two_rows)
1043 {
1044 wvbox = gtk_vbox_new (TRUE, button_spacing);
1045 whbox_up = gtk_hbox_new (FALSE, 0);
1046 whbox_down = gtk_hbox_new (FALSE, 0);
1047
1048 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1049 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1050 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1051
1052 cur_box = GTK_BOX (whbox_up);
1053 }
1054
1055 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1056 G_CALLBACK (dialog_delete_callback), 0);
1057
1058 if (deactivate_cb)
1059 {
1060 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1061 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1062 }
1063
1064 for (item = wv->contents; item; item = item->next)
1065 {
1066 char *utf8_label = get_utf8_string (item->value);
1067 GtkWidget *w;
1068 GtkRequisition req;
1069
1070 if (item->name && strcmp (item->name, "message") == 0)
1071 {
1072 /* This is the text part of the dialog. */
1073 w = gtk_label_new (utf8_label);
1074 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1075 gtk_label_new (""),
1076 FALSE, FALSE, 0);
1077 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
1078 TRUE, TRUE, 0);
1079 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1080
1081 /* Try to make dialog look better. Must realize first so
1082 the widget can calculate the size it needs. */
1083 gtk_widget_realize (w);
1084 gtk_widget_size_request (w, &req);
1085 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1086 req.height);
1087 if (item->value && strlen (item->value) > 0)
1088 button_spacing = 2*req.width/strlen (item->value);
1089 }
1090 else
1091 {
1092 /* This is one button to add to the dialog. */
1093 w = gtk_button_new_with_label (utf8_label);
1094 if (! item->enabled)
1095 gtk_widget_set_sensitive (w, FALSE);
1096 if (select_cb)
1097 g_signal_connect (G_OBJECT (w), "clicked",
1098 select_cb, item->call_data);
1099
1100 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1101 if (++button_nr == left_buttons)
1102 {
1103 if (make_two_rows)
1104 cur_box = GTK_BOX (whbox_down);
1105 else
1106 gtk_box_pack_start (cur_box,
1107 gtk_label_new (""),
1108 TRUE, TRUE,
1109 button_spacing);
1110 }
1111 }
1112
1113 if (utf8_label && utf8_label != item->value)
1114 g_free (utf8_label);
1115 }
1116
1117 return wdialog;
1118 }
1119
1120
1121 \f
1122 /***********************************************************************
1123 File dialog functions
1124 ***********************************************************************/
1125 enum
1126 {
1127 XG_FILE_NOT_DONE,
1128 XG_FILE_OK,
1129 XG_FILE_CANCEL,
1130 XG_FILE_DESTROYED,
1131 };
1132
1133 #ifdef HAVE_GTK_FILE_BOTH
1134 static int use_old_gtk_file_dialog;
1135 #endif
1136
1137
1138 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1139 /* Read a file name from the user using a file chooser dialog.
1140 F is the current frame.
1141 PROMPT is a prompt to show to the user. May not be NULL.
1142 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1143 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1144 file.
1145
1146 Returns a file name or NULL if no file was selected.
1147 The returned string must be freed by the caller. */
1148
1149 static char *
1150 xg_get_file_with_chooser (f, prompt, default_filename, mustmatch_p, only_dir_p)
1151 FRAME_PTR f;
1152 char *prompt;
1153 char *default_filename;
1154 int mustmatch_p, only_dir_p;
1155 {
1156 GtkWidget *filewin;
1157 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1158
1159 char *fn = 0;
1160 GtkFileChooserAction action = (mustmatch_p ?
1161 GTK_FILE_CHOOSER_ACTION_OPEN :
1162 GTK_FILE_CHOOSER_ACTION_SAVE);
1163
1164 if (only_dir_p)
1165 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1166
1167 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1168 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1169 (mustmatch_p || only_dir_p ?
1170 GTK_STOCK_OPEN : GTK_STOCK_SAVE),
1171 GTK_RESPONSE_OK,
1172 NULL);
1173
1174 xg_set_screen (filewin, f);
1175 gtk_widget_set_name (filewin, "emacs-filedialog");
1176 gtk_window_set_transient_for (GTK_WINDOW (filewin), gwin);
1177 gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin), TRUE);
1178
1179
1180 if (default_filename)
1181 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1182 default_filename);
1183
1184 gtk_widget_show (filewin);
1185
1186 if (gtk_dialog_run (GTK_DIALOG (filewin)) == GTK_RESPONSE_OK)
1187 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filewin));
1188
1189 gtk_widget_destroy (filewin);
1190
1191 return fn;
1192 }
1193 #endif /* HAVE_GTK_FILE_CHOOSER_DIALOG_NEW */
1194
1195 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1196 /* Callback function invoked when the Ok button is pressed in
1197 a file dialog.
1198 W is the file dialog widget,
1199 ARG points to an integer where we record what has happend. */
1200
1201 static void
1202 xg_file_sel_ok (w, arg)
1203 GtkWidget *w;
1204 gpointer arg;
1205 {
1206 *(int*)arg = XG_FILE_OK;
1207 }
1208
1209 /* Callback function invoked when the Cancel button is pressed in
1210 a file dialog.
1211 W is the file dialog widget,
1212 ARG points to an integer where we record what has happend. */
1213
1214 static void
1215 xg_file_sel_cancel (w, arg)
1216 GtkWidget *w;
1217 gpointer arg;
1218 {
1219 *(int*)arg = XG_FILE_CANCEL;
1220 }
1221
1222 /* Callback function invoked when the file dialog is destroyed (i.e.
1223 popped down). We must keep track of this, because if this
1224 happens, GTK destroys the widget. But if for example, Ok is pressed,
1225 the dialog is popped down, but the dialog widget is not destroyed.
1226 W is the file dialog widget,
1227 ARG points to an integer where we record what has happend. */
1228
1229 static void
1230 xg_file_sel_destroy (w, arg)
1231 GtkWidget *w;
1232 gpointer arg;
1233 {
1234 *(int*)arg = XG_FILE_DESTROYED;
1235 }
1236
1237 /* Read a file name from the user using a file selection dialog.
1238 F is the current frame.
1239 PROMPT is a prompt to show to the user. May not be NULL.
1240 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1241 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1242 file.
1243
1244 Returns a file name or NULL if no file was selected.
1245 The returned string must be freed by the caller. */
1246
1247 static char *
1248 xg_get_file_with_selection (f, prompt, default_filename,
1249 mustmatch_p, only_dir_p)
1250 FRAME_PTR f;
1251 char *prompt;
1252 char *default_filename;
1253 int mustmatch_p, only_dir_p;
1254 {
1255 GtkWidget *filewin;
1256 GtkFileSelection *filesel;
1257 int filesel_done = XG_FILE_NOT_DONE;
1258 char *fn = 0;
1259
1260 filewin = gtk_file_selection_new (prompt);
1261 filesel = GTK_FILE_SELECTION (filewin);
1262
1263 xg_set_screen (filewin, f);
1264 gtk_widget_set_name (filewin, "emacs-filedialog");
1265 gtk_window_set_transient_for (GTK_WINDOW (filewin),
1266 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1267 gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin), TRUE);
1268
1269 g_signal_connect (G_OBJECT (filesel->ok_button),
1270 "clicked",
1271 G_CALLBACK (xg_file_sel_ok),
1272 &filesel_done);
1273 g_signal_connect (G_OBJECT (filesel->cancel_button),
1274 "clicked",
1275 G_CALLBACK (xg_file_sel_cancel),
1276 &filesel_done);
1277 g_signal_connect (G_OBJECT (filesel),
1278 "destroy",
1279 G_CALLBACK (xg_file_sel_destroy),
1280 &filesel_done);
1281
1282 if (default_filename)
1283 gtk_file_selection_set_filename (filesel, default_filename);
1284
1285 if (mustmatch_p)
1286 {
1287 /* The selection_entry part of filesel is not documented. */
1288 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1289 gtk_file_selection_hide_fileop_buttons (filesel);
1290 }
1291
1292
1293 gtk_widget_show_all (filewin);
1294
1295 while (filesel_done == XG_FILE_NOT_DONE)
1296 gtk_main_iteration ();
1297
1298 if (filesel_done == XG_FILE_OK)
1299 fn = xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1300
1301 if (filesel_done != XG_FILE_DESTROYED)
1302 gtk_widget_destroy (filewin);
1303
1304 return fn;
1305 }
1306 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1307
1308 /* Read a file name from the user using a file dialog, either the old
1309 file selection dialog, or the new file chooser dialog. Which to use
1310 depends on what the GTK version used has, and what the value of
1311 gtk-use-old-file-dialog.
1312 F is the current frame.
1313 PROMPT is a prompt to show to the user. May not be NULL.
1314 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1315 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1316 file.
1317
1318 Returns a file name or NULL if no file was selected.
1319 The returned string must be freed by the caller. */
1320
1321 char *
1322 xg_get_file_name (f, prompt, default_filename, mustmatch_p, only_dir_p)
1323 FRAME_PTR f;
1324 char *prompt;
1325 char *default_filename;
1326 int mustmatch_p, only_dir_p;
1327 {
1328 #ifdef HAVE_GTK_FILE_BOTH
1329 if (use_old_gtk_file_dialog)
1330 return xg_get_file_with_selection (f, prompt, default_filename,
1331 mustmatch_p, only_dir_p);
1332 return xg_get_file_with_chooser (f, prompt, default_filename,
1333 mustmatch_p, only_dir_p);
1334
1335 #else /* not HAVE_GTK_FILE_BOTH */
1336
1337 #ifdef HAVE_GTK_FILE_SELECTION_DIALOG_NEW
1338 return xg_get_file_with_selection (f, prompt, default_filename,
1339 mustmatch_p, only_dir_p);
1340 #endif
1341 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1342 return xg_get_file_with_chooser (f, prompt, default_filename,
1343 mustmatch_p, only_dir_p);
1344 #endif
1345
1346 #endif /* HAVE_GTK_FILE_BOTH */
1347 return 0;
1348 }
1349
1350 \f
1351 /***********************************************************************
1352 Menu functions.
1353 ***********************************************************************/
1354
1355 /* The name of menu items that can be used for citomization. Since GTK
1356 RC files are very crude and primitive, we have to set this on all
1357 menu item names so a user can easily cutomize menu items. */
1358
1359 #define MENU_ITEM_NAME "emacs-menuitem"
1360
1361
1362 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1363 during GC. The next member points to the items. */
1364 static xg_list_node xg_menu_cb_list;
1365
1366 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1367 during GC. The next member points to the items. */
1368 static xg_list_node xg_menu_item_cb_list;
1369
1370 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1371 F is the frame CL_DATA will be initialized for.
1372 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1373
1374 The menu bar and all sub menus under the menu bar in a frame
1375 share the same structure, hence the reference count.
1376
1377 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1378 allocated xg_menu_cb_data if CL_DATA is NULL. */
1379
1380 static xg_menu_cb_data *
1381 make_cl_data (cl_data, f, highlight_cb)
1382 xg_menu_cb_data *cl_data;
1383 FRAME_PTR f;
1384 GCallback highlight_cb;
1385 {
1386 if (! cl_data)
1387 {
1388 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1389 cl_data->f = f;
1390 cl_data->menu_bar_vector = f->menu_bar_vector;
1391 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1392 cl_data->highlight_cb = highlight_cb;
1393 cl_data->ref_count = 0;
1394
1395 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1396 }
1397
1398 cl_data->ref_count++;
1399
1400 return cl_data;
1401 }
1402
1403 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1404 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1405
1406 When the menu bar is updated, menu items may have been added and/or
1407 removed, so menu_bar_vector and menu_bar_items_used change. We must
1408 then update CL_DATA since it is used to determine which menu
1409 item that is invoked in the menu.
1410 HIGHLIGHT_CB could change, there is no check that the same
1411 function is given when modifying a menu bar as was given when
1412 creating the menu bar. */
1413
1414 static void
1415 update_cl_data (cl_data, f, highlight_cb)
1416 xg_menu_cb_data *cl_data;
1417 FRAME_PTR f;
1418 GCallback highlight_cb;
1419 {
1420 if (cl_data)
1421 {
1422 cl_data->f = f;
1423 cl_data->menu_bar_vector = f->menu_bar_vector;
1424 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1425 cl_data->highlight_cb = highlight_cb;
1426 }
1427 }
1428
1429 /* Decrease reference count for CL_DATA.
1430 If reference count is zero, free CL_DATA. */
1431
1432 static void
1433 unref_cl_data (cl_data)
1434 xg_menu_cb_data *cl_data;
1435 {
1436 if (cl_data && cl_data->ref_count > 0)
1437 {
1438 cl_data->ref_count--;
1439 if (cl_data->ref_count == 0)
1440 {
1441 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1442 xfree (cl_data);
1443 }
1444 }
1445 }
1446
1447 /* Function that marks all lisp data during GC. */
1448
1449 void
1450 xg_mark_data ()
1451 {
1452 xg_list_node *iter;
1453
1454 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1455 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1456
1457 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1458 {
1459 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1460
1461 if (! NILP (cb_data->help))
1462 mark_object (cb_data->help);
1463 }
1464 }
1465
1466
1467 /* Callback called when a menu item is destroyed. Used to free data.
1468 W is the widget that is being destroyed (not used).
1469 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1470
1471 static void
1472 menuitem_destroy_callback (w, client_data)
1473 GtkWidget *w;
1474 gpointer client_data;
1475 {
1476 if (client_data)
1477 {
1478 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1479 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1480 xfree (data);
1481 }
1482 }
1483
1484 /* Callback called when the pointer enters/leaves a menu item.
1485 W is the menu item.
1486 EVENT is either an enter event or leave event.
1487 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1488
1489 Returns FALSE to tell GTK to keep processing this event. */
1490
1491 static gboolean
1492 menuitem_highlight_callback (w, event, client_data)
1493 GtkWidget *w;
1494 GdkEventCrossing *event;
1495 gpointer client_data;
1496 {
1497 if (client_data)
1498 {
1499 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1500 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data;
1501
1502 if (! NILP (data->help) && data->cl_data->highlight_cb)
1503 {
1504 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1505 (*func) (w, call_data);
1506 }
1507 }
1508
1509 return FALSE;
1510 }
1511
1512 /* Callback called when a menu is destroyed. Used to free data.
1513 W is the widget that is being destroyed (not used).
1514 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1515
1516 static void
1517 menu_destroy_callback (w, client_data)
1518 GtkWidget *w;
1519 gpointer client_data;
1520 {
1521 unref_cl_data ((xg_menu_cb_data*) client_data);
1522 }
1523
1524 /* Callback called when a menu does a grab or ungrab. That means the
1525 menu has been activated or deactivated.
1526 Used to start a timer so the small timeout the menus in GTK uses before
1527 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1528 W is the widget that does the grab (not used).
1529 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1530 CLIENT_DATA is NULL (not used). */
1531
1532 static void
1533 menu_grab_callback (GtkWidget *widget,
1534 gboolean ungrab_p,
1535 gpointer client_data)
1536 {
1537 /* Keep track of total number of grabs. */
1538 static int cnt;
1539
1540 if (ungrab_p) cnt--;
1541 else cnt++;
1542
1543 if (cnt > 0 && ! xg_timer) xg_start_timer ();
1544 else if (cnt == 0 && xg_timer) xg_stop_timer ();
1545 }
1546
1547 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1548 must be non-NULL) and can be inserted into a menu item.
1549
1550 Returns the GtkHBox. */
1551
1552 static GtkWidget *
1553 make_widget_for_menu_item (utf8_label, utf8_key)
1554 char *utf8_label;
1555 char *utf8_key;
1556 {
1557 GtkWidget *wlbl;
1558 GtkWidget *wkey;
1559 GtkWidget *wbox;
1560
1561 wbox = gtk_hbox_new (FALSE, 0);
1562 wlbl = gtk_label_new (utf8_label);
1563 wkey = gtk_label_new (utf8_key);
1564
1565 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1566 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1567
1568 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1569 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1570
1571 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1572 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1573 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1574
1575 return wbox;
1576 }
1577
1578 /* Make and return a menu item widget with the key to the right.
1579 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1580 UTF8_KEY is the text representing the key binding.
1581 ITEM is the widget_value describing the menu item.
1582
1583 GROUP is an in/out parameter. If the menu item to be created is not
1584 part of any radio menu group, *GROUP contains NULL on entry and exit.
1585 If the menu item to be created is part of a radio menu group, on entry
1586 *GROUP contains the group to use, or NULL if this is the first item
1587 in the group. On exit, *GROUP contains the radio item group.
1588
1589 Unfortunately, keys don't line up as nicely as in Motif,
1590 but the MacOS X version doesn't either, so I guess that is OK. */
1591
1592 static GtkWidget *
1593 make_menu_item (utf8_label, utf8_key, item, group)
1594 char *utf8_label;
1595 char *utf8_key;
1596 widget_value *item;
1597 GSList **group;
1598 {
1599 GtkWidget *w;
1600 GtkWidget *wtoadd = 0;
1601
1602 /* It has been observed that some menu items have a NULL name field.
1603 This will lead to this function being called with a NULL utf8_label.
1604 GTK crashes on that so we set a blank label. Why there is a NULL
1605 name remains to be investigated. */
1606 if (! utf8_label) utf8_label = " ";
1607
1608 if (utf8_key)
1609 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1610
1611 if (item->button_type == BUTTON_TYPE_TOGGLE)
1612 {
1613 *group = NULL;
1614 if (utf8_key) w = gtk_check_menu_item_new ();
1615 else w = gtk_check_menu_item_new_with_label (utf8_label);
1616 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1617 }
1618 else if (item->button_type == BUTTON_TYPE_RADIO)
1619 {
1620 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1621 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1622 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1623 if (item->selected)
1624 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1625 }
1626 else
1627 {
1628 *group = NULL;
1629 if (utf8_key) w = gtk_menu_item_new ();
1630 else w = gtk_menu_item_new_with_label (utf8_label);
1631 }
1632
1633 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1634 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1635
1636 return w;
1637 }
1638
1639 /* Return non-zero if LABEL specifies a separator (GTK only has one
1640 separator type) */
1641
1642 static int
1643 xg_separator_p (char *label)
1644 {
1645 if (! label) return 0;
1646 else if (strlen (label) > 3
1647 && strncmp (label, "--", 2) == 0
1648 && label[2] != '-')
1649 {
1650 static char* separator_names[] = {
1651 "space",
1652 "no-line",
1653 "single-line",
1654 "double-line",
1655 "single-dashed-line",
1656 "double-dashed-line",
1657 "shadow-etched-in",
1658 "shadow-etched-out",
1659 "shadow-etched-in-dash",
1660 "shadow-etched-out-dash",
1661 "shadow-double-etched-in",
1662 "shadow-double-etched-out",
1663 "shadow-double-etched-in-dash",
1664 "shadow-double-etched-out-dash",
1665 0,
1666 };
1667
1668 int i;
1669
1670 label += 2;
1671 for (i = 0; separator_names[i]; ++i)
1672 if (strcmp (label, separator_names[i]) == 0)
1673 return 1;
1674 }
1675 else
1676 {
1677 /* Old-style separator, maybe. It's a separator if it contains
1678 only dashes. */
1679 while (*label == '-')
1680 ++label;
1681 if (*label == 0) return 1;
1682 }
1683
1684 return 0;
1685 }
1686
1687 static int xg_detached_menus;
1688
1689 /* Returns non-zero if there are detached menus. */
1690
1691 int
1692 xg_have_tear_offs ()
1693 {
1694 return xg_detached_menus > 0;
1695 }
1696
1697 /* Callback invoked when a detached menu window is removed. Here we
1698 decrease the xg_detached_menus count.
1699 WIDGET is the top level window that is removed (the parent of the menu).
1700 CLIENT_DATA is not used. */
1701
1702 static void
1703 tearoff_remove (widget, client_data)
1704 GtkWidget *widget;
1705 gpointer client_data;
1706 {
1707 if (xg_detached_menus > 0) --xg_detached_menus;
1708 }
1709
1710 /* Callback invoked when a menu is detached. It increases the
1711 xg_detached_menus count.
1712 WIDGET is the GtkTearoffMenuItem.
1713 CLIENT_DATA is not used. */
1714
1715 static void
1716 tearoff_activate (widget, client_data)
1717 GtkWidget *widget;
1718 gpointer client_data;
1719 {
1720 GtkWidget *menu = gtk_widget_get_parent (widget);
1721 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1722 {
1723 ++xg_detached_menus;
1724 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
1725 "destroy",
1726 G_CALLBACK (tearoff_remove), 0);
1727 }
1728 }
1729
1730
1731 /* Create a menu item widget, and connect the callbacks.
1732 ITEM decribes the menu item.
1733 F is the frame the created menu belongs to.
1734 SELECT_CB is the callback to use when a menu item is selected.
1735 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1736 CL_DATA points to the callback data to be used for this menu.
1737 GROUP is an in/out parameter. If the menu item to be created is not
1738 part of any radio menu group, *GROUP contains NULL on entry and exit.
1739 If the menu item to be created is part of a radio menu group, on entry
1740 *GROUP contains the group to use, or NULL if this is the first item
1741 in the group. On exit, *GROUP contains the radio item group.
1742
1743 Returns the created GtkWidget. */
1744
1745 static GtkWidget *
1746 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1747 widget_value *item;
1748 FRAME_PTR f;
1749 GCallback select_cb;
1750 GCallback highlight_cb;
1751 xg_menu_cb_data *cl_data;
1752 GSList **group;
1753 {
1754 char *utf8_label;
1755 char *utf8_key;
1756 GtkWidget *w;
1757 xg_menu_item_cb_data *cb_data;
1758
1759 utf8_label = get_utf8_string (item->name);
1760 utf8_key = get_utf8_string (item->key);
1761
1762 w = make_menu_item (utf8_label, utf8_key, item, group);
1763
1764 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1765 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1766
1767 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1768
1769 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1770
1771 cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0;
1772 cb_data->help = item->help;
1773 cb_data->cl_data = cl_data;
1774 cb_data->call_data = item->call_data;
1775
1776 g_signal_connect (G_OBJECT (w),
1777 "destroy",
1778 G_CALLBACK (menuitem_destroy_callback),
1779 cb_data);
1780
1781 /* Put cb_data in widget, so we can get at it when modifying menubar */
1782 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
1783
1784 /* final item, not a submenu */
1785 if (item->call_data && ! item->contents)
1786 {
1787 if (select_cb)
1788 cb_data->select_id
1789 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
1790 }
1791
1792 if (! NILP (item->help) && highlight_cb)
1793 {
1794 /* We use enter/leave notify instead of select/deselect because
1795 select/deselect doesn't go well with detached menus. */
1796 cb_data->highlight_id
1797 = g_signal_connect (G_OBJECT (w),
1798 "enter-notify-event",
1799 G_CALLBACK (menuitem_highlight_callback),
1800 cb_data);
1801 cb_data->unhighlight_id
1802 = g_signal_connect (G_OBJECT (w),
1803 "leave-notify-event",
1804 G_CALLBACK (menuitem_highlight_callback),
1805 cb_data);
1806 }
1807
1808 return w;
1809 }
1810
1811 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
1812 GCallback, GCallback, int, int, int,
1813 GtkWidget *, xg_menu_cb_data *, char *));
1814
1815 /* Create a full menu tree specified by DATA.
1816 F is the frame the created menu belongs to.
1817 SELECT_CB is the callback to use when a menu item is selected.
1818 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1819 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1820 POP_UP_P is non-zero if we shall create a popup menu.
1821 MENU_BAR_P is non-zero if we shall create a menu bar.
1822 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1823 if MENU_BAR_P is non-zero.
1824 TOPMENU is the topmost GtkWidget that others shall be placed under.
1825 It may be NULL, in that case we create the appropriate widget
1826 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1827 CL_DATA is the callback data we shall use for this menu, or NULL
1828 if we haven't set the first callback yet.
1829 NAME is the name to give to the top level menu if this function
1830 creates it. May be NULL to not set any name.
1831
1832 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1833 not NULL.
1834
1835 This function calls itself to create submenus. */
1836
1837 static GtkWidget *
1838 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
1839 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
1840 widget_value *data;
1841 FRAME_PTR f;
1842 GCallback select_cb;
1843 GCallback deactivate_cb;
1844 GCallback highlight_cb;
1845 int pop_up_p;
1846 int menu_bar_p;
1847 int add_tearoff_p;
1848 GtkWidget *topmenu;
1849 xg_menu_cb_data *cl_data;
1850 char *name;
1851 {
1852 widget_value *item;
1853 GtkWidget *wmenu = topmenu;
1854 GSList *group = NULL;
1855
1856 if (! topmenu)
1857 {
1858 if (! menu_bar_p)
1859 {
1860 wmenu = gtk_menu_new ();
1861 xg_set_screen (wmenu, f);
1862 }
1863 else wmenu = gtk_menu_bar_new ();
1864
1865 /* Put cl_data on the top menu for easier access. */
1866 cl_data = make_cl_data (cl_data, f, highlight_cb);
1867 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
1868 g_signal_connect (G_OBJECT (wmenu), "destroy",
1869 G_CALLBACK (menu_destroy_callback), cl_data);
1870
1871 if (name)
1872 gtk_widget_set_name (wmenu, name);
1873
1874 if (deactivate_cb)
1875 g_signal_connect (G_OBJECT (wmenu),
1876 "deactivate", deactivate_cb, 0);
1877
1878 g_signal_connect (G_OBJECT (wmenu),
1879 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
1880 }
1881
1882 if (! menu_bar_p && add_tearoff_p)
1883 {
1884 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1885 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
1886
1887 g_signal_connect (G_OBJECT (tearoff), "activate",
1888 G_CALLBACK (tearoff_activate), 0);
1889 }
1890
1891 for (item = data; item; item = item->next)
1892 {
1893 GtkWidget *w;
1894
1895 if (pop_up_p && !item->contents && !item->call_data
1896 && !xg_separator_p (item->name))
1897 {
1898 char *utf8_label;
1899 /* A title for a popup. We do the same as GTK does when
1900 creating titles, but it does not look good. */
1901 group = NULL;
1902 utf8_label = get_utf8_string (item->name);
1903
1904 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
1905 w = gtk_menu_item_new_with_label (utf8_label);
1906 gtk_widget_set_sensitive (w, FALSE);
1907 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1908 }
1909 else if (xg_separator_p (item->name))
1910 {
1911 group = NULL;
1912 /* GTK only have one separator type. */
1913 w = gtk_separator_menu_item_new ();
1914 }
1915 else
1916 {
1917 w = xg_create_one_menuitem (item,
1918 f,
1919 item->contents ? 0 : select_cb,
1920 highlight_cb,
1921 cl_data,
1922 &group);
1923
1924 if (item->contents)
1925 {
1926 GtkWidget *submenu = create_menus (item->contents,
1927 f,
1928 select_cb,
1929 deactivate_cb,
1930 highlight_cb,
1931 0,
1932 0,
1933 add_tearoff_p,
1934 0,
1935 cl_data,
1936 0);
1937 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
1938 }
1939 }
1940
1941 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
1942 gtk_widget_set_name (w, MENU_ITEM_NAME);
1943 }
1944
1945 return wmenu;
1946 }
1947
1948 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1949 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1950 with some text and buttons.
1951 F is the frame the created item belongs to.
1952 NAME is the name to use for the top widget.
1953 VAL is a widget_value structure describing items to be created.
1954 SELECT_CB is the callback to use when a menu item is selected or
1955 a dialog button is pressed.
1956 DEACTIVATE_CB is the callback to use when an item is deactivated.
1957 For a menu, when a sub menu is not shown anymore, for a dialog it is
1958 called when the dialog is popped down.
1959 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1960
1961 Returns the widget created. */
1962
1963 GtkWidget *
1964 xg_create_widget (type, name, f, val,
1965 select_cb, deactivate_cb, highlight_cb)
1966 char *type;
1967 char *name;
1968 FRAME_PTR f;
1969 widget_value *val;
1970 GCallback select_cb;
1971 GCallback deactivate_cb;
1972 GCallback highlight_cb;
1973 {
1974 GtkWidget *w = 0;
1975 int menu_bar_p = strcmp (type, "menubar") == 0;
1976 int pop_up_p = strcmp (type, "popup") == 0;
1977
1978 if (strcmp (type, "dialog") == 0)
1979 {
1980 w = create_dialog (val, select_cb, deactivate_cb);
1981 xg_set_screen (w, f);
1982 gtk_window_set_transient_for (GTK_WINDOW (w),
1983 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1984 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1985 gtk_widget_set_name (w, "emacs-dialog");
1986 }
1987 else if (menu_bar_p || pop_up_p)
1988 {
1989 w = create_menus (val->contents,
1990 f,
1991 select_cb,
1992 deactivate_cb,
1993 highlight_cb,
1994 pop_up_p,
1995 menu_bar_p,
1996 menu_bar_p,
1997 0,
1998 0,
1999 name);
2000
2001 /* Set the cursor to an arrow for popup menus when they are mapped.
2002 This is done by default for menu bar menus. */
2003 if (pop_up_p)
2004 {
2005 /* Must realize so the GdkWindow inside the widget is created. */
2006 gtk_widget_realize (w);
2007 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2008 }
2009 }
2010 else
2011 {
2012 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2013 type);
2014 }
2015
2016 return w;
2017 }
2018
2019 /* Return the label for menu item WITEM. */
2020
2021 static const char *
2022 xg_get_menu_item_label (witem)
2023 GtkMenuItem *witem;
2024 {
2025 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2026 return gtk_label_get_label (wlabel);
2027 }
2028
2029 /* Return non-zero if the menu item WITEM has the text LABEL. */
2030
2031 static int
2032 xg_item_label_same_p (witem, label)
2033 GtkMenuItem *witem;
2034 char *label;
2035 {
2036 int is_same = 0;
2037 char *utf8_label = get_utf8_string (label);
2038 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2039
2040 if (! old_label && ! utf8_label)
2041 is_same = 1;
2042 else if (old_label && utf8_label)
2043 is_same = strcmp (utf8_label, old_label) == 0;
2044
2045 if (utf8_label && utf8_label != label) g_free (utf8_label);
2046
2047 return is_same;
2048 }
2049
2050 /* Destroy widgets in LIST. */
2051
2052 static void
2053 xg_destroy_widgets (list)
2054 GList *list;
2055 {
2056 GList *iter;
2057
2058 for (iter = list; iter; iter = g_list_next (iter))
2059 {
2060 GtkWidget *w = GTK_WIDGET (iter->data);
2061
2062 /* Destroying the widget will remove it from the container it is in. */
2063 gtk_widget_destroy (w);
2064 }
2065 }
2066
2067 /* Update the top level names in MENUBAR (i.e. not submenus).
2068 F is the frame the menu bar belongs to.
2069 *LIST is a list with the current menu bar names (menu item widgets).
2070 ITER is the item within *LIST that shall be updated.
2071 POS is the numerical position, starting at 0, of ITER in *LIST.
2072 VAL describes what the menu bar shall look like after the update.
2073 SELECT_CB is the callback to use when a menu item is selected.
2074 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2075 CL_DATA points to the callback data to be used for this menu bar.
2076
2077 This function calls itself to walk through the menu bar names. */
2078
2079 static void
2080 xg_update_menubar (menubar, f, list, iter, pos, val,
2081 select_cb, highlight_cb, cl_data)
2082 GtkWidget *menubar;
2083 FRAME_PTR f;
2084 GList **list;
2085 GList *iter;
2086 int pos;
2087 widget_value *val;
2088 GCallback select_cb;
2089 GCallback highlight_cb;
2090 xg_menu_cb_data *cl_data;
2091 {
2092 if (! iter && ! val)
2093 return;
2094 else if (iter && ! val)
2095 {
2096 /* Item(s) have been removed. Remove all remaining items. */
2097 xg_destroy_widgets (iter);
2098
2099 /* All updated. */
2100 val = 0;
2101 iter = 0;
2102 }
2103 else if (! iter && val)
2104 {
2105 /* Item(s) added. Add all new items in one call. */
2106 create_menus (val, f, select_cb, 0, highlight_cb,
2107 0, 1, 0, menubar, cl_data, 0);
2108
2109 /* All updated. */
2110 val = 0;
2111 iter = 0;
2112 }
2113 /* Below this neither iter or val is NULL */
2114 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2115 {
2116 /* This item is still the same, check next item. */
2117 val = val->next;
2118 iter = g_list_next (iter);
2119 ++pos;
2120 }
2121 else /* This item is changed. */
2122 {
2123 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2124 GtkMenuItem *witem2 = 0;
2125 int val_in_menubar = 0;
2126 int iter_in_new_menubar = 0;
2127 GList *iter2;
2128 widget_value *cur;
2129
2130 /* See if the changed entry (val) is present later in the menu bar */
2131 for (iter2 = iter;
2132 iter2 && ! val_in_menubar;
2133 iter2 = g_list_next (iter2))
2134 {
2135 witem2 = GTK_MENU_ITEM (iter2->data);
2136 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2137 }
2138
2139 /* See if the current entry (iter) is present later in the
2140 specification for the new menu bar. */
2141 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2142 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2143
2144 if (val_in_menubar && ! iter_in_new_menubar)
2145 {
2146 int nr = pos;
2147
2148 /* This corresponds to:
2149 Current: A B C
2150 New: A C
2151 Remove B. */
2152
2153 gtk_widget_ref (GTK_WIDGET (witem));
2154 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2155 gtk_widget_destroy (GTK_WIDGET (witem));
2156
2157 /* Must get new list since the old changed. */
2158 g_list_free (*list);
2159 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2160 while (nr-- > 0) iter = g_list_next (iter);
2161 }
2162 else if (! val_in_menubar && ! iter_in_new_menubar)
2163 {
2164 /* This corresponds to:
2165 Current: A B C
2166 New: A X C
2167 Rename B to X. This might seem to be a strange thing to do,
2168 since if there is a menu under B it will be totally wrong for X.
2169 But consider editing a C file. Then there is a C-mode menu
2170 (corresponds to B above).
2171 If then doing C-x C-f the minibuf menu (X above) replaces the
2172 C-mode menu. When returning from the minibuffer, we get
2173 back the C-mode menu. Thus we do:
2174 Rename B to X (C-mode to minibuf menu)
2175 Rename X to B (minibuf to C-mode menu).
2176 If the X menu hasn't been invoked, the menu under B
2177 is up to date when leaving the minibuffer. */
2178 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2179 char *utf8_label = get_utf8_string (val->name);
2180 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2181
2182 gtk_label_set_text (wlabel, utf8_label);
2183
2184 /* If this item has a submenu that has been detached, change
2185 the title in the WM decorations also. */
2186 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2187 /* Set the title of the detached window. */
2188 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2189
2190 iter = g_list_next (iter);
2191 val = val->next;
2192 ++pos;
2193 }
2194 else if (! val_in_menubar && iter_in_new_menubar)
2195 {
2196 /* This corresponds to:
2197 Current: A B C
2198 New: A X B C
2199 Insert X. */
2200
2201 int nr = pos;
2202 GList *group = 0;
2203 GtkWidget *w = xg_create_one_menuitem (val,
2204 f,
2205 select_cb,
2206 highlight_cb,
2207 cl_data,
2208 &group);
2209
2210 gtk_widget_set_name (w, MENU_ITEM_NAME);
2211 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2212
2213 g_list_free (*list);
2214 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2215 while (nr-- > 0) iter = g_list_next (iter);
2216 iter = g_list_next (iter);
2217 val = val->next;
2218 ++pos;
2219 }
2220 else /* if (val_in_menubar && iter_in_new_menubar) */
2221 {
2222 int nr = pos;
2223 /* This corresponds to:
2224 Current: A B C
2225 New: A C B
2226 Move C before B */
2227
2228 gtk_widget_ref (GTK_WIDGET (witem2));
2229 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2230 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2231 GTK_WIDGET (witem2), pos);
2232 gtk_widget_unref (GTK_WIDGET (witem2));
2233
2234 g_list_free (*list);
2235 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2236 while (nr-- > 0) iter = g_list_next (iter);
2237 val = val->next;
2238 ++pos;
2239 }
2240 }
2241
2242 /* Update the rest of the menu bar. */
2243 xg_update_menubar (menubar, f, list, iter, pos, val,
2244 select_cb, highlight_cb, cl_data);
2245 }
2246
2247 /* Update the menu item W so it corresponds to VAL.
2248 SELECT_CB is the callback to use when a menu item is selected.
2249 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2250 CL_DATA is the data to set in the widget for menu invokation. */
2251
2252 static void
2253 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2254 widget_value *val;
2255 GtkWidget *w;
2256 GCallback select_cb;
2257 GCallback highlight_cb;
2258 xg_menu_cb_data *cl_data;
2259 {
2260 GtkWidget *wchild;
2261 GtkLabel *wlbl = 0;
2262 GtkLabel *wkey = 0;
2263 char *utf8_label;
2264 char *utf8_key;
2265 const char *old_label = 0;
2266 const char *old_key = 0;
2267 xg_menu_item_cb_data *cb_data;
2268
2269 wchild = gtk_bin_get_child (GTK_BIN (w));
2270 utf8_label = get_utf8_string (val->name);
2271 utf8_key = get_utf8_string (val->key);
2272
2273 /* See if W is a menu item with a key. See make_menu_item above. */
2274 if (GTK_IS_HBOX (wchild))
2275 {
2276 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2277
2278 wlbl = GTK_LABEL (list->data);
2279 wkey = GTK_LABEL (list->next->data);
2280 g_list_free (list);
2281
2282 if (! utf8_key)
2283 {
2284 /* Remove the key and keep just the label. */
2285 gtk_widget_ref (GTK_WIDGET (wlbl));
2286 gtk_container_remove (GTK_CONTAINER (w), wchild);
2287 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2288 wkey = 0;
2289 }
2290
2291 }
2292 else /* Just a label. */
2293 {
2294 wlbl = GTK_LABEL (wchild);
2295
2296 /* Check if there is now a key. */
2297 if (utf8_key)
2298 {
2299 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2300 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2301
2302 wlbl = GTK_LABEL (list->data);
2303 wkey = GTK_LABEL (list->next->data);
2304 g_list_free (list);
2305
2306 gtk_container_remove (GTK_CONTAINER (w), wchild);
2307 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2308 }
2309 }
2310
2311
2312 if (wkey) old_key = gtk_label_get_label (wkey);
2313 if (wlbl) old_label = gtk_label_get_label (wlbl);
2314
2315 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2316 gtk_label_set_text (wkey, utf8_key);
2317
2318 if (! old_label || strcmp (utf8_label, old_label) != 0)
2319 gtk_label_set_text (wlbl, utf8_label);
2320
2321 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2322 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2323
2324 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2325 gtk_widget_set_sensitive (w, FALSE);
2326 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2327 gtk_widget_set_sensitive (w, TRUE);
2328
2329 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2330 XG_ITEM_DATA);
2331 if (cb_data)
2332 {
2333 cb_data->call_data = val->call_data;
2334 cb_data->help = val->help;
2335 cb_data->cl_data = cl_data;
2336
2337 /* We assume the callback functions don't change. */
2338 if (val->call_data && ! val->contents)
2339 {
2340 /* This item shall have a select callback. */
2341 if (! cb_data->select_id)
2342 cb_data->select_id
2343 = g_signal_connect (G_OBJECT (w), "activate",
2344 select_cb, cb_data);
2345 }
2346 else if (cb_data->select_id)
2347 {
2348 g_signal_handler_disconnect (w, cb_data->select_id);
2349 cb_data->select_id = 0;
2350 }
2351
2352 if (NILP (cb_data->help))
2353 {
2354 /* Shall not have help. Remove if any existed previously. */
2355 if (cb_data->highlight_id)
2356 {
2357 g_signal_handler_disconnect (G_OBJECT (w),
2358 cb_data->highlight_id);
2359 cb_data->highlight_id = 0;
2360 }
2361 if (cb_data->unhighlight_id)
2362 {
2363 g_signal_handler_disconnect (G_OBJECT (w),
2364 cb_data->unhighlight_id);
2365 cb_data->unhighlight_id = 0;
2366 }
2367 }
2368 else if (! cb_data->highlight_id && highlight_cb)
2369 {
2370 /* Have help now, but didn't previously. Add callback. */
2371 cb_data->highlight_id
2372 = g_signal_connect (G_OBJECT (w),
2373 "enter-notify-event",
2374 G_CALLBACK (menuitem_highlight_callback),
2375 cb_data);
2376 cb_data->unhighlight_id
2377 = g_signal_connect (G_OBJECT (w),
2378 "leave-notify-event",
2379 G_CALLBACK (menuitem_highlight_callback),
2380 cb_data);
2381 }
2382 }
2383 }
2384
2385 /* Update the toggle menu item W so it corresponds to VAL. */
2386
2387 static void
2388 xg_update_toggle_item (val, w)
2389 widget_value *val;
2390 GtkWidget *w;
2391 {
2392 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2393 }
2394
2395 /* Update the radio menu item W so it corresponds to VAL. */
2396
2397 static void
2398 xg_update_radio_item (val, w)
2399 widget_value *val;
2400 GtkWidget *w;
2401 {
2402 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2403 }
2404
2405 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2406 SUBMENU may be NULL, in that case a new menu is created.
2407 F is the frame the menu bar belongs to.
2408 VAL describes the contents of the menu bar.
2409 SELECT_CB is the callback to use when a menu item is selected.
2410 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2411 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2412 CL_DATA is the call back data to use for any newly created items.
2413
2414 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2415 was NULL. */
2416
2417 static GtkWidget *
2418 xg_update_submenu (submenu, f, val,
2419 select_cb, deactivate_cb, highlight_cb, cl_data)
2420 GtkWidget *submenu;
2421 FRAME_PTR f;
2422 widget_value *val;
2423 GCallback select_cb;
2424 GCallback deactivate_cb;
2425 GCallback highlight_cb;
2426 xg_menu_cb_data *cl_data;
2427 {
2428 GtkWidget *newsub = submenu;
2429 GList *list = 0;
2430 GList *iter;
2431 widget_value *cur;
2432 int has_tearoff_p = 0;
2433 GList *first_radio = 0;
2434
2435 if (submenu)
2436 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2437
2438 for (cur = val, iter = list;
2439 cur && iter;
2440 iter = g_list_next (iter), cur = cur->next)
2441 {
2442 GtkWidget *w = GTK_WIDGET (iter->data);
2443
2444 /* Skip tearoff items, they have no counterpart in val. */
2445 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2446 {
2447 has_tearoff_p = 1;
2448 iter = g_list_next (iter);
2449 if (iter) w = GTK_WIDGET (iter->data);
2450 else break;
2451 }
2452
2453 /* Remember first radio button in a group. If we get a mismatch in
2454 a radio group we must rebuild the whole group so that the connections
2455 in GTK becomes correct. */
2456 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2457 first_radio = iter;
2458 else if (cur->button_type != BUTTON_TYPE_RADIO
2459 && ! GTK_IS_RADIO_MENU_ITEM (w))
2460 first_radio = 0;
2461
2462 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2463 {
2464 if (! xg_separator_p (cur->name))
2465 break;
2466 }
2467 else if (GTK_IS_CHECK_MENU_ITEM (w))
2468 {
2469 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2470 break;
2471 xg_update_toggle_item (cur, w);
2472 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2473 }
2474 else if (GTK_IS_RADIO_MENU_ITEM (w))
2475 {
2476 if (cur->button_type != BUTTON_TYPE_RADIO)
2477 break;
2478 xg_update_radio_item (cur, w);
2479 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2480 }
2481 else if (GTK_IS_MENU_ITEM (w))
2482 {
2483 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2484 GtkWidget *sub;
2485
2486 if (cur->button_type != BUTTON_TYPE_NONE ||
2487 xg_separator_p (cur->name))
2488 break;
2489
2490 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2491
2492 sub = gtk_menu_item_get_submenu (witem);
2493 if (sub && ! cur->contents)
2494 {
2495 /* Not a submenu anymore. */
2496 gtk_widget_ref (sub);
2497 gtk_menu_item_remove_submenu (witem);
2498 gtk_widget_destroy (sub);
2499 }
2500 else if (cur->contents)
2501 {
2502 GtkWidget *nsub;
2503
2504 nsub = xg_update_submenu (sub, f, cur->contents,
2505 select_cb, deactivate_cb,
2506 highlight_cb, cl_data);
2507
2508 /* If this item just became a submenu, we must set it. */
2509 if (nsub != sub)
2510 gtk_menu_item_set_submenu (witem, nsub);
2511 }
2512 }
2513 else
2514 {
2515 /* Structural difference. Remove everything from here and down
2516 in SUBMENU. */
2517 break;
2518 }
2519 }
2520
2521 /* Remove widgets from first structual change. */
2522 if (iter)
2523 {
2524 /* If we are adding new menu items below, we must remove from
2525 first radio button so that radio groups become correct. */
2526 if (cur && first_radio) xg_destroy_widgets (first_radio);
2527 else xg_destroy_widgets (iter);
2528 }
2529
2530 if (cur)
2531 {
2532 /* More items added. Create them. */
2533 newsub = create_menus (cur,
2534 f,
2535 select_cb,
2536 deactivate_cb,
2537 highlight_cb,
2538 0,
2539 0,
2540 ! has_tearoff_p,
2541 submenu,
2542 cl_data,
2543 0);
2544 }
2545
2546 if (list) g_list_free (list);
2547
2548 return newsub;
2549 }
2550
2551 /* Update the MENUBAR.
2552 F is the frame the menu bar belongs to.
2553 VAL describes the contents of the menu bar.
2554 If DEEP_P is non-zero, rebuild all but the top level menu names in
2555 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2556 SELECT_CB is the callback to use when a menu item is selected.
2557 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2558 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2559
2560 void
2561 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2562 select_cb, deactivate_cb, highlight_cb)
2563 GtkWidget *menubar;
2564 FRAME_PTR f;
2565 widget_value *val;
2566 int deep_p;
2567 GCallback select_cb;
2568 GCallback deactivate_cb;
2569 GCallback highlight_cb;
2570 {
2571 xg_menu_cb_data *cl_data;
2572 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2573
2574 if (! list) return;
2575
2576 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2577 XG_FRAME_DATA);
2578
2579 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2580 select_cb, highlight_cb, cl_data);
2581
2582 if (deep_p);
2583 {
2584 widget_value *cur;
2585
2586 /* Update all sub menus.
2587 We must keep the submenus (GTK menu item widgets) since the
2588 X Window in the XEvent that activates the menu are those widgets. */
2589
2590 /* Update cl_data, menu_item things in F may have changed. */
2591 update_cl_data (cl_data, f, highlight_cb);
2592
2593 for (cur = val->contents; cur; cur = cur->next)
2594 {
2595 GList *iter;
2596 GtkWidget *sub = 0;
2597 GtkWidget *newsub;
2598 GtkMenuItem *witem;
2599
2600 /* Find sub menu that corresponds to val and update it. */
2601 for (iter = list ; iter; iter = g_list_next (iter))
2602 {
2603 witem = GTK_MENU_ITEM (iter->data);
2604 if (xg_item_label_same_p (witem, cur->name))
2605 {
2606 sub = gtk_menu_item_get_submenu (witem);
2607 break;
2608 }
2609 }
2610
2611 newsub = xg_update_submenu (sub,
2612 f,
2613 cur->contents,
2614 select_cb,
2615 deactivate_cb,
2616 highlight_cb,
2617 cl_data);
2618 /* sub may still be NULL. If we just updated non deep and added
2619 a new menu bar item, it has no sub menu yet. So we set the
2620 newly created sub menu under witem. */
2621 if (newsub != sub)
2622 {
2623 xg_set_screen (newsub, f);
2624 gtk_menu_item_set_submenu (witem, newsub);
2625 }
2626 }
2627 }
2628
2629 g_list_free (list);
2630 gtk_widget_show_all (menubar);
2631 }
2632
2633 /* Recompute all the widgets of frame F, when the menu bar has been
2634 changed. Value is non-zero if widgets were updated. */
2635
2636 int
2637 xg_update_frame_menubar (f)
2638 FRAME_PTR f;
2639 {
2640 struct x_output *x = f->output_data.x;
2641 GtkRequisition req;
2642
2643 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2644 return 0;
2645
2646 BLOCK_INPUT;
2647
2648 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2649 FALSE, FALSE, 0);
2650 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2651
2652 gtk_widget_show_all (x->menubar_widget);
2653 gtk_widget_size_request (x->menubar_widget, &req);
2654
2655 FRAME_MENUBAR_HEIGHT (f) = req.height;
2656
2657 /* The height has changed, resize outer widget and set columns
2658 rows to what we had before adding the menu bar. */
2659 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2660
2661 SET_FRAME_GARBAGED (f);
2662 UNBLOCK_INPUT;
2663
2664 return 1;
2665 }
2666
2667 /* Get rid of the menu bar of frame F, and free its storage.
2668 This is used when deleting a frame, and when turning off the menu bar. */
2669
2670 void
2671 free_frame_menubar (f)
2672 FRAME_PTR f;
2673 {
2674 struct x_output *x = f->output_data.x;
2675
2676 if (x->menubar_widget)
2677 {
2678 BLOCK_INPUT;
2679
2680 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2681 /* The menubar and its children shall be deleted when removed from
2682 the container. */
2683 x->menubar_widget = 0;
2684 FRAME_MENUBAR_HEIGHT (f) = 0;
2685
2686 /* The height has changed, resize outer widget and set columns
2687 rows to what we had before removing the menu bar. */
2688 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2689
2690 SET_FRAME_GARBAGED (f);
2691 UNBLOCK_INPUT;
2692 }
2693 }
2694
2695
2696 \f
2697 /***********************************************************************
2698 Scroll bar functions
2699 ***********************************************************************/
2700
2701
2702 /* Setting scroll bar values invokes the callback. Use this variable
2703 to indicate that callback should do nothing. */
2704
2705 int xg_ignore_gtk_scrollbar;
2706
2707 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2708 32 bits. But we want to store pointers, and they may be larger
2709 than 32 bits. Keep a mapping from integer index to widget pointers
2710 to get around the 32 bit limitation. */
2711
2712 static struct
2713 {
2714 GtkWidget **widgets;
2715 int max_size;
2716 int used;
2717 } id_to_widget;
2718
2719 /* Grow this much every time we need to allocate more */
2720
2721 #define ID_TO_WIDGET_INCR 32
2722
2723 /* Store the widget pointer W in id_to_widget and return the integer index. */
2724
2725 static int
2726 xg_store_widget_in_map (w)
2727 GtkWidget *w;
2728 {
2729 int i;
2730
2731 if (id_to_widget.max_size == id_to_widget.used)
2732 {
2733 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2734
2735 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2736 sizeof (GtkWidget *)*new_size);
2737
2738 for (i = id_to_widget.max_size; i < new_size; ++i)
2739 id_to_widget.widgets[i] = 0;
2740 id_to_widget.max_size = new_size;
2741 }
2742
2743 /* Just loop over the array and find a free place. After all,
2744 how many scroll bars are we creating? Should be a small number.
2745 The check above guarantees we will find a free place. */
2746 for (i = 0; i < id_to_widget.max_size; ++i)
2747 {
2748 if (! id_to_widget.widgets[i])
2749 {
2750 id_to_widget.widgets[i] = w;
2751 ++id_to_widget.used;
2752
2753 return i;
2754 }
2755 }
2756
2757 /* Should never end up here */
2758 abort ();
2759 }
2760
2761 /* Remove pointer at IDX from id_to_widget.
2762 Called when scroll bar is destroyed. */
2763
2764 static void
2765 xg_remove_widget_from_map (idx)
2766 int idx;
2767 {
2768 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2769 {
2770 id_to_widget.widgets[idx] = 0;
2771 --id_to_widget.used;
2772 }
2773 }
2774
2775 /* Get the widget pointer at IDX from id_to_widget. */
2776
2777 static GtkWidget *
2778 xg_get_widget_from_map (idx)
2779 int idx;
2780 {
2781 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2782 return id_to_widget.widgets[idx];
2783
2784 return 0;
2785 }
2786
2787 /* Return the scrollbar id for X Window WID on display DPY.
2788 Return -1 if WID not in id_to_widget. */
2789
2790 int
2791 xg_get_scroll_id_for_window (dpy, wid)
2792 Display *dpy;
2793 Window wid;
2794 {
2795 int idx;
2796 GtkWidget *w;
2797
2798 w = xg_win_to_widget (dpy, wid);
2799
2800 if (w)
2801 {
2802 for (idx = 0; idx < id_to_widget.max_size; ++idx)
2803 if (id_to_widget.widgets[idx] == w)
2804 return idx;
2805 }
2806
2807 return -1;
2808 }
2809
2810 /* Callback invoked when scroll bar WIDGET is destroyed.
2811 DATA is the index into id_to_widget for WIDGET.
2812 We free pointer to last scroll bar values here and remove the index. */
2813
2814 static void
2815 xg_gtk_scroll_destroy (widget, data)
2816 GtkWidget *widget;
2817 gpointer data;
2818 {
2819 gpointer p;
2820 int id = (int)data;
2821
2822 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
2823 if (p) xfree (p);
2824 xg_remove_widget_from_map (id);
2825 }
2826
2827 /* Callback for button press/release events. Used to start timer so that
2828 the scroll bar repetition timer in GTK gets handeled.
2829 Also, sets bar->dragging to Qnil when dragging (button release) is done.
2830 WIDGET is the scroll bar widget the event is for (not used).
2831 EVENT contains the event.
2832 USER_DATA points to the struct scrollbar structure.
2833
2834 Returns FALSE to tell GTK that it shall continue propagate the event
2835 to widgets. */
2836
2837 static gboolean
2838 scroll_bar_button_cb (widget, event, user_data)
2839 GtkWidget *widget;
2840 GdkEventButton *event;
2841 gpointer user_data;
2842 {
2843 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
2844 xg_start_timer ();
2845 else if (event->type == GDK_BUTTON_RELEASE)
2846 {
2847 struct scroll_bar *bar = (struct scroll_bar *) user_data;
2848 if (xg_timer) xg_stop_timer ();
2849 bar->dragging = Qnil;
2850 }
2851
2852 return FALSE;
2853 }
2854
2855 /* Create a scroll bar widget for frame F. Store the scroll bar
2856 in BAR.
2857 SCROLL_CALLBACK is the callback to invoke when the value of the
2858 bar changes.
2859 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2860 to set resources for the widget. */
2861
2862 void
2863 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
2864 FRAME_PTR f;
2865 struct scroll_bar *bar;
2866 GCallback scroll_callback;
2867 char *scroll_bar_name;
2868 {
2869 GtkWidget *wscroll;
2870 GtkWidget *webox;
2871 GtkObject *vadj;
2872 int scroll_id;
2873
2874 /* Page, step increment values are not so important here, they
2875 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2876 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
2877 0.1, 0.1, 0.1);
2878
2879 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
2880 webox = gtk_event_box_new ();
2881 gtk_widget_set_name (wscroll, scroll_bar_name);
2882 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
2883
2884 scroll_id = xg_store_widget_in_map (wscroll);
2885
2886 g_signal_connect (G_OBJECT (wscroll),
2887 "value-changed",
2888 scroll_callback,
2889 (gpointer) bar);
2890 g_signal_connect (G_OBJECT (wscroll),
2891 "destroy",
2892 G_CALLBACK (xg_gtk_scroll_destroy),
2893 (gpointer) scroll_id);
2894
2895 /* Connect to button press and button release to detect if any scroll bar
2896 has the pointer. */
2897 g_signal_connect (G_OBJECT (wscroll),
2898 "button-press-event",
2899 G_CALLBACK (scroll_bar_button_cb),
2900 (gpointer) bar);
2901 g_signal_connect (G_OBJECT (wscroll),
2902 "button-release-event",
2903 G_CALLBACK (scroll_bar_button_cb),
2904 (gpointer) bar);
2905
2906 /* The scroll bar widget does not draw on a window of its own. Instead
2907 it draws on the parent window, in this case the edit widget. So
2908 whenever the edit widget is cleared, the scroll bar needs to redraw
2909 also, which causes flicker. Put an event box between the edit widget
2910 and the scroll bar, so the scroll bar instead draws itself on the
2911 event box window. */
2912 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
2913 gtk_container_add (GTK_CONTAINER (webox), wscroll);
2914
2915
2916 /* Set the cursor to an arrow. */
2917 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2918
2919 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
2920 }
2921
2922 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
2923
2924 void
2925 xg_show_scroll_bar (scrollbar_id)
2926 int scrollbar_id;
2927 {
2928 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2929 if (w)
2930 gtk_widget_show_all (gtk_widget_get_parent (w));
2931 }
2932
2933 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
2934
2935 void
2936 xg_remove_scroll_bar (f, scrollbar_id)
2937 FRAME_PTR f;
2938 int scrollbar_id;
2939 {
2940 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2941 if (w)
2942 {
2943 GtkWidget *wparent = gtk_widget_get_parent (w);
2944 gtk_widget_destroy (w);
2945 gtk_widget_destroy (wparent);
2946 SET_FRAME_GARBAGED (f);
2947 }
2948 }
2949
2950 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2951 in frame F.
2952 TOP/LEFT are the new pixel positions where the bar shall appear.
2953 WIDTH, HEIGHT is the size in pixels the bar shall have. */
2954
2955 void
2956 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
2957 FRAME_PTR f;
2958 int scrollbar_id;
2959 int top;
2960 int left;
2961 int width;
2962 int height;
2963 {
2964
2965 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
2966
2967 if (wscroll)
2968 {
2969 GtkWidget *wfixed = f->output_data.x->edit_widget;
2970 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
2971
2972 /* Move and resize to new values. */
2973 gtk_widget_set_size_request (wscroll, width, height);
2974 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
2975
2976 SET_FRAME_GARBAGED (f);
2977 cancel_mouse_face (f);
2978 }
2979 }
2980
2981 /* Set the thumb size and position of scroll bar BAR. We are currently
2982 displaying PORTION out of a whole WHOLE, and our position POSITION. */
2983
2984 void
2985 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
2986 struct scroll_bar *bar;
2987 int portion, position, whole;
2988 {
2989 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
2990
2991 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
2992
2993 if (wscroll && NILP (bar->dragging))
2994 {
2995 GtkAdjustment *adj;
2996 gdouble shown;
2997 gdouble top;
2998 int size, value;
2999 int new_step;
3000 int changed = 0;
3001
3002 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3003
3004 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3005 rather than the real portion value. This makes the thumb less likely
3006 to resize and that looks better. */
3007 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3008 /* When the thumb is at the bottom, position == whole.
3009 So we need to increase `whole' to make space for the thumb. */
3010 whole += portion;
3011
3012 if (whole <= 0)
3013 top = 0, shown = 1;
3014 else
3015 {
3016 top = (gdouble) position / whole;
3017 shown = (gdouble) portion / whole;
3018 }
3019
3020 size = shown * XG_SB_RANGE;
3021 size = min (size, XG_SB_RANGE);
3022 size = max (size, 1);
3023
3024 value = top * XG_SB_RANGE;
3025 value = min (value, XG_SB_MAX - size);
3026 value = max (value, XG_SB_MIN);
3027
3028 /* Assume all lines are of equal size. */
3029 new_step = size / max (1, FRAME_LINES (f));
3030
3031 if ((int) adj->page_size != size
3032 || (int) adj->step_increment != new_step)
3033 {
3034 adj->page_size = size;
3035 adj->step_increment = new_step;
3036 /* Assume a page increment is about 95% of the page size */
3037 adj->page_increment = (int) (0.95*adj->page_size);
3038 changed = 1;
3039 }
3040
3041 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3042 {
3043 GtkWidget *wfixed = f->output_data.x->edit_widget;
3044
3045 BLOCK_INPUT;
3046
3047 /* gtk_range_set_value invokes the callback. Set
3048 ignore_gtk_scrollbar to make the callback do nothing */
3049 xg_ignore_gtk_scrollbar = 1;
3050
3051 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3052 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3053 else if (changed)
3054 gtk_adjustment_changed (adj);
3055
3056 xg_ignore_gtk_scrollbar = 0;
3057
3058 UNBLOCK_INPUT;
3059 }
3060 }
3061 }
3062
3063 \f
3064 /***********************************************************************
3065 Tool bar functions
3066 ***********************************************************************/
3067 /* The key for the data we put in the GtkImage widgets. The data is
3068 the image used by Emacs. We use this to see if we need to update
3069 the GtkImage with a new image. */
3070 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3071
3072 /* Callback function invoked when a tool bar item is pressed.
3073 W is the button widget in the tool bar that got pressed,
3074 CLIENT_DATA is an integer that is the index of the button in the
3075 tool bar. 0 is the first button. */
3076
3077 static void
3078 xg_tool_bar_callback (w, client_data)
3079 GtkWidget *w;
3080 gpointer client_data;
3081 {
3082 int idx = (int)client_data;
3083 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3084 Lisp_Object key, frame;
3085 struct input_event event;
3086 EVENT_INIT (event);
3087
3088 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3089 return;
3090
3091 idx *= TOOL_BAR_ITEM_NSLOTS;
3092
3093 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3094 XSETFRAME (frame, f);
3095 event.kind = TOOL_BAR_EVENT;
3096 event.frame_or_window = frame;
3097 event.arg = frame;
3098 kbd_buffer_store_event (&event);
3099
3100 event.kind = TOOL_BAR_EVENT;
3101 event.frame_or_window = frame;
3102 event.arg = key;
3103 event.modifiers = 0; /* These are not available. */
3104 kbd_buffer_store_event (&event);
3105 }
3106
3107 /* This callback is called when a tool bar is detached. We must set
3108 the height of the tool bar to zero when this happens so frame sizes
3109 are correctly calculated.
3110 WBOX is the handle box widget that enables detach/attach of the tool bar.
3111 W is the tool bar widget.
3112 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3113
3114 static void
3115 xg_tool_bar_detach_callback (wbox, w, client_data)
3116 GtkHandleBox *wbox;
3117 GtkWidget *w;
3118 gpointer client_data;
3119 {
3120 FRAME_PTR f = (FRAME_PTR) client_data;
3121
3122 if (f)
3123 {
3124 /* When detaching a tool bar, not everything dissapear. There are
3125 a few pixels left that are used to drop the tool bar back into
3126 place. */
3127 int bw = gtk_container_get_border_width (GTK_CONTAINER (wbox));
3128 FRAME_TOOLBAR_HEIGHT (f) = 2;
3129
3130 /* The height has changed, resize outer widget and set columns
3131 rows to what we had before detaching the tool bar. */
3132 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3133 }
3134 }
3135
3136 /* This callback is called when a tool bar is reattached. We must set
3137 the height of the tool bar when this happens so frame sizes
3138 are correctly calculated.
3139 WBOX is the handle box widget that enables detach/attach of the tool bar.
3140 W is the tool bar widget.
3141 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3142
3143 static void
3144 xg_tool_bar_attach_callback (wbox, w, client_data)
3145 GtkHandleBox *wbox;
3146 GtkWidget *w;
3147 gpointer client_data;
3148 {
3149 FRAME_PTR f = (FRAME_PTR) client_data;
3150
3151 if (f)
3152 {
3153 GtkRequisition req;
3154
3155 gtk_widget_size_request (w, &req);
3156 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3157
3158 /* The height has changed, resize outer widget and set columns
3159 rows to what we had before detaching the tool bar. */
3160 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3161 }
3162 }
3163
3164 /* This callback is called when the mouse enters or leaves a tool bar item.
3165 It is used for displaying and hiding the help text.
3166 W is the tool bar item, a button.
3167 EVENT is either an enter event or leave event.
3168 CLIENT_DATA is an integer that is the index of the button in the
3169 tool bar. 0 is the first button.
3170
3171 Returns FALSE to tell GTK to keep processing this event. */
3172
3173 static gboolean
3174 xg_tool_bar_help_callback (w, event, client_data)
3175 GtkWidget *w;
3176 GdkEventCrossing *event;
3177 gpointer client_data;
3178 {
3179 int idx = (int)client_data;
3180 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3181 Lisp_Object help, frame;
3182
3183 if (! GTK_IS_BUTTON (w))
3184 {
3185 return FALSE;
3186 }
3187
3188 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3189 return FALSE;
3190
3191 if (event->type == GDK_ENTER_NOTIFY)
3192 {
3193 idx *= TOOL_BAR_ITEM_NSLOTS;
3194 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3195
3196 if (NILP (help))
3197 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3198 }
3199 else
3200 help = Qnil;
3201
3202 XSETFRAME (frame, f);
3203 kbd_buffer_store_help_event (frame, help);
3204
3205 return FALSE;
3206 }
3207
3208
3209 /* This callback is called when a tool bar item shall be redrawn.
3210 It modifies the expose event so that the GtkImage widget redraws the
3211 whole image. This to overcome a bug that makes GtkImage draw the image
3212 in the wrong place when it tries to redraw just a part of the image.
3213 W is the GtkImage to be redrawn.
3214 EVENT is the expose event for W.
3215 CLIENT_DATA is unused.
3216
3217 Returns FALSE to tell GTK to keep processing this event. */
3218
3219 static gboolean
3220 xg_tool_bar_item_expose_callback (w, event, client_data)
3221 GtkWidget *w;
3222 GdkEventExpose *event;
3223 gpointer client_data;
3224 {
3225 gint width, height;
3226
3227 gdk_drawable_get_size (event->window, &width, &height);
3228
3229 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3230 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3231
3232 event->area.x = max (0, event->area.x);
3233 event->area.y = max (0, event->area.y);
3234
3235 event->area.width = max (width, event->area.width);
3236 event->area.height = max (height, event->area.height);
3237
3238 return FALSE;
3239 }
3240
3241 /* This callback is called when a tool bar shall be redrawn.
3242 We need to update the tool bar from here in case the image cache
3243 has deleted the pixmaps used in the tool bar.
3244 W is the GtkToolbar to be redrawn.
3245 EVENT is the expose event for W.
3246 CLIENT_DATA is pointing to the frame for this tool bar.
3247
3248 Returns FALSE to tell GTK to keep processing this event. */
3249
3250 static gboolean
3251 xg_tool_bar_expose_callback (w, event, client_data)
3252 GtkWidget *w;
3253 GdkEventExpose *event;
3254 gpointer client_data;
3255 {
3256 update_frame_tool_bar ((FRAME_PTR) client_data);
3257 return FALSE;
3258 }
3259
3260 /* Create a tool bar for frame F. */
3261
3262 static void
3263 xg_create_tool_bar (f)
3264 FRAME_PTR f;
3265 {
3266 struct x_output *x = f->output_data.x;
3267 GtkRequisition req;
3268 int vbox_pos = x->menubar_widget ? 1 : 0;
3269
3270 x->toolbar_widget = gtk_toolbar_new ();
3271 x->handlebox_widget = gtk_handle_box_new ();
3272 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3273 x->toolbar_widget);
3274
3275 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3276 FALSE, FALSE, 0);
3277
3278 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3279 vbox_pos);
3280
3281 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3282
3283 /* We only have icons, so override any user setting. We could
3284 use the caption property of the toolbar item (see update_frame_tool_bar
3285 below), but some of those strings are long, making the toolbar so
3286 long it does not fit on the screen. The GtkToolbar widget makes every
3287 item equal size, so the longest caption determine the size of every
3288 tool bar item. I think the creators of the GtkToolbar widget
3289 counted on 4 or 5 character long strings. */
3290 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3291 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3292 GTK_ORIENTATION_HORIZONTAL);
3293
3294 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3295 G_CALLBACK (xg_tool_bar_detach_callback), f);
3296 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3297 G_CALLBACK (xg_tool_bar_attach_callback), f);
3298 g_signal_connect (G_OBJECT (x->toolbar_widget),
3299 "expose-event",
3300 G_CALLBACK (xg_tool_bar_expose_callback),
3301 f);
3302
3303 gtk_widget_show_all (x->handlebox_widget);
3304
3305 gtk_widget_size_request (x->toolbar_widget, &req);
3306 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3307
3308 /* The height has changed, resize outer widget and set columns
3309 rows to what we had before adding the tool bar. */
3310 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3311
3312 SET_FRAME_GARBAGED (f);
3313 }
3314
3315 /* Update the tool bar for frame F. Add new buttons and remove old. */
3316
3317 void
3318 update_frame_tool_bar (f)
3319 FRAME_PTR f;
3320 {
3321 int i;
3322 GtkRequisition old_req, new_req;
3323 GList *icon_list;
3324 GList *iter;
3325 struct x_output *x = f->output_data.x;
3326
3327 if (! FRAME_GTK_WIDGET (f))
3328 return;
3329
3330 BLOCK_INPUT;
3331
3332 if (! x->toolbar_widget)
3333 xg_create_tool_bar (f);
3334
3335 gtk_widget_size_request (x->toolbar_widget, &old_req);
3336
3337 icon_list = gtk_container_get_children (GTK_CONTAINER (x->toolbar_widget));
3338 iter = icon_list;
3339
3340 for (i = 0; i < f->n_tool_bar_items; ++i)
3341 {
3342 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3343
3344 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3345 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3346 int idx;
3347 int img_id;
3348 struct image *img;
3349 Lisp_Object image;
3350 GtkWidget *wicon = iter ? GTK_WIDGET (iter->data) : 0;
3351
3352 if (iter) iter = g_list_next (iter);
3353
3354 /* If image is a vector, choose the image according to the
3355 button state. */
3356 image = PROP (TOOL_BAR_ITEM_IMAGES);
3357 if (VECTORP (image))
3358 {
3359 if (enabled_p)
3360 idx = (selected_p
3361 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3362 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3363 else
3364 idx = (selected_p
3365 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3366 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3367
3368 xassert (ASIZE (image) >= idx);
3369 image = AREF (image, idx);
3370 }
3371 else
3372 idx = -1;
3373
3374 /* Ignore invalid image specifications. */
3375 if (!valid_image_p (image))
3376 {
3377 if (wicon) gtk_widget_hide (wicon);
3378 continue;
3379 }
3380
3381 img_id = lookup_image (f, image);
3382 img = IMAGE_FROM_ID (f, img_id);
3383 prepare_image_for_display (f, img);
3384
3385 if (img->load_failed_p || img->pixmap == None)
3386 {
3387 if (wicon) gtk_widget_hide (wicon);
3388 continue;
3389 }
3390
3391 if (! wicon)
3392 {
3393 GtkWidget *w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
3394
3395 gtk_toolbar_append_item (GTK_TOOLBAR (x->toolbar_widget),
3396 0, 0, 0,
3397 w,
3398 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
3399 (gpointer)i);
3400
3401 /* Save the image so we can see if an update is needed when
3402 this function is called again. */
3403 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3404 (gpointer)img->pixmap);
3405
3406 /* Catch expose events to overcome an annoying redraw bug, see
3407 comment for xg_tool_bar_item_expose_callback. */
3408 g_signal_connect (G_OBJECT (w),
3409 "expose-event",
3410 G_CALLBACK (xg_tool_bar_item_expose_callback),
3411 0);
3412
3413 /* We must set sensitive on the button that is the parent
3414 of the GtkImage parent. Go upwards until we find the button. */
3415 while (! GTK_IS_BUTTON (w))
3416 w = gtk_widget_get_parent (w);
3417
3418 if (w)
3419 {
3420 /* Save the frame in the button so the xg_tool_bar_callback
3421 can get at it. */
3422 g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
3423 gtk_widget_set_sensitive (w, enabled_p);
3424
3425 /* Use enter/leave notify to show help. We use the events
3426 rather than the GtkButton specific signals "enter" and
3427 "leave", so we can have only one callback. The event
3428 will tell us what kind of event it is. */
3429 g_signal_connect (G_OBJECT (w),
3430 "enter-notify-event",
3431 G_CALLBACK (xg_tool_bar_help_callback),
3432 (gpointer)i);
3433 g_signal_connect (G_OBJECT (w),
3434 "leave-notify-event",
3435 G_CALLBACK (xg_tool_bar_help_callback),
3436 (gpointer)i);
3437 }
3438 }
3439 else
3440 {
3441 /* The child of the tool bar is a button. Inside that button
3442 is a vbox. Inside that vbox is the GtkImage. */
3443 GtkWidget *wvbox = gtk_bin_get_child (GTK_BIN (wicon));
3444 GList *chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
3445 GtkImage *wimage = GTK_IMAGE (chlist->data);
3446 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
3447 XG_TOOL_BAR_IMAGE_DATA);
3448 g_list_free (chlist);
3449
3450 if (old_img != img->pixmap)
3451 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
3452
3453 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
3454 (gpointer)img->pixmap);
3455
3456 gtk_widget_set_sensitive (wicon, enabled_p);
3457 gtk_widget_show (wicon);
3458 }
3459
3460 #undef PROP
3461 }
3462
3463 /* Remove buttons not longer needed. We just hide them so they
3464 can be reused later on. */
3465 while (iter)
3466 {
3467 GtkWidget *w = GTK_WIDGET (iter->data);
3468 gtk_widget_hide (w);
3469 iter = g_list_next (iter);
3470 }
3471
3472 gtk_widget_size_request (x->toolbar_widget, &new_req);
3473 if (old_req.height != new_req.height)
3474 {
3475 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
3476 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3477 }
3478
3479 if (icon_list) g_list_free (icon_list);
3480
3481 UNBLOCK_INPUT;
3482 }
3483
3484 /* Deallocate all resources for the tool bar on frame F.
3485 Remove the tool bar. */
3486
3487 void
3488 free_frame_tool_bar (f)
3489 FRAME_PTR f;
3490 {
3491 struct x_output *x = f->output_data.x;
3492
3493 if (x->toolbar_widget)
3494 {
3495 BLOCK_INPUT;
3496 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
3497 x->handlebox_widget);
3498 x->toolbar_widget = 0;
3499 x->handlebox_widget = 0;
3500 FRAME_TOOLBAR_HEIGHT (f) = 0;
3501
3502 /* The height has changed, resize outer widget and set columns
3503 rows to what we had before removing the tool bar. */
3504 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3505
3506 SET_FRAME_GARBAGED (f);
3507 UNBLOCK_INPUT;
3508 }
3509 }
3510
3511
3512 \f
3513 /***********************************************************************
3514 Initializing
3515 ***********************************************************************/
3516 void
3517 xg_initialize ()
3518 {
3519 xg_ignore_gtk_scrollbar = 0;
3520 xg_detached_menus = 0;
3521 xg_menu_cb_list.prev = xg_menu_cb_list.next =
3522 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
3523
3524 id_to_widget.max_size = id_to_widget.used = 0;
3525 id_to_widget.widgets = 0;
3526
3527 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
3528 bindings. It doesn't seem to be any way to remove properties,
3529 so we set it to VoidSymbol which in X means "no key". */
3530 gtk_settings_set_string_property (gtk_settings_get_default (),
3531 "gtk-menu-bar-accel",
3532 "VoidSymbol",
3533 EMACS_CLASS);
3534
3535 /* Make GTK text input widgets use Emacs style keybindings. This is
3536 Emacs after all. */
3537 gtk_settings_set_string_property (gtk_settings_get_default (),
3538 "gtk-key-theme-name",
3539 "Emacs",
3540 EMACS_CLASS);
3541
3542 #ifdef HAVE_GTK_FILE_BOTH
3543 DEFVAR_BOOL ("use-old-gtk-file-dialog", &use_old_gtk_file_dialog,
3544 doc: /* *Non-nil means that the old GTK file selection dialog is used.
3545 If nil the new GTK file chooser is used instead. To turn off
3546 all file dialogs set the variable `use-file-dialog'. */);
3547 use_old_gtk_file_dialog = 0;
3548 #endif
3549 }
3550
3551 #endif /* USE_GTK */
3552
3553 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
3554 (do not change this comment) */