Remove experimental code inadvertently introduced when reverting the
[emacs.git] / src / gtkutil.c
blob1ce091bcbd724ee6809e4138d192d181ac5e3423
1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 #ifdef USE_GTK
23 #include <string.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include "lisp.h"
27 #include "xterm.h"
28 #include "blockinput.h"
29 #include "syssignal.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>
39 #ifdef HAVE_XFT
40 #include <X11/Xft/Xft.h>
41 #endif
43 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
44 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
46 /* Avoid "differ in sign" warnings */
47 #define SSDATA(x) ((char *) SDATA (x))
50 /***********************************************************************
51 Display handling functions
52 ***********************************************************************/
54 #ifdef HAVE_GTK_MULTIDISPLAY
56 /* Keep track of the default display, or NULL if there is none. Emacs
57 may close all its displays. */
59 static GdkDisplay *gdpy_def;
61 /* Return the GdkDisplay that corresponds to the X display DPY. */
63 static GdkDisplay *
64 xg_get_gdk_display (dpy)
65 Display *dpy;
67 return gdk_x11_lookup_xdisplay (dpy);
70 /* When the GTK widget W is to be created on a display for F that
71 is not the default display, set the display for W.
72 W can be a GtkMenu or a GtkWindow widget. */
74 static void
75 xg_set_screen (w, f)
76 GtkWidget *w;
77 FRAME_PTR f;
79 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
81 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
82 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
84 if (GTK_IS_MENU (w))
85 gtk_menu_set_screen (GTK_MENU (w), gscreen);
86 else
87 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
92 #else /* not HAVE_GTK_MULTIDISPLAY */
94 /* Make some defines so we can use the GTK 2.2 functions when
95 compiling with GTK 2.0. */
97 #define xg_set_screen(w, f)
98 #define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
99 #define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
100 #define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
101 #define gdk_x11_lookup_xdisplay(dpy) 0
102 #define GdkDisplay void
104 #endif /* not HAVE_GTK_MULTIDISPLAY */
106 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
107 *DPY is set to NULL if the display can't be opened.
109 Returns non-zero if display could be opened, zero if display could not
110 be opened, and less than zero if the GTK version doesn't support
111 multipe displays. */
114 xg_display_open (display_name, dpy)
115 char *display_name;
116 Display **dpy;
118 #ifdef HAVE_GTK_MULTIDISPLAY
119 GdkDisplay *gdpy;
121 gdpy = gdk_display_open (display_name);
122 if (!gdpy_def)
123 gdk_display_manager_set_default_display (gdk_display_manager_get (),
124 gdpy);
126 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
127 return gdpy != NULL;
129 #else /* not HAVE_GTK_MULTIDISPLAY */
131 return -1;
132 #endif /* not HAVE_GTK_MULTIDISPLAY */
136 /* Close display DPY. */
138 void
139 xg_display_close (Display *dpy)
141 #ifdef HAVE_GTK_MULTIDISPLAY
142 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
144 /* If this is the default display, try to change it before closing.
145 If there is no other display to use, gdpy_def is set to NULL, and
146 the next call to xg_display_open resets the default display. */
147 if (gdk_display_get_default () == gdpy)
149 struct x_display_info *dpyinfo;
150 GdkDisplay *gdpy_new = NULL;
152 /* Find another display. */
153 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
154 if (dpyinfo->display != dpy)
156 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
157 gdk_display_manager_set_default_display (gdk_display_manager_get (),
158 gdpy_new);
159 break;
161 gdpy_def = gdpy_new;
164 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
165 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
166 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
167 can continue running, but there will be memory leaks. */
168 g_object_run_dispose (G_OBJECT (gdpy));
169 #else
170 /* This seems to be fixed in GTK 2.10. */
171 gdk_display_close (gdpy);
172 #endif
173 #endif /* HAVE_GTK_MULTIDISPLAY */
177 /***********************************************************************
178 Utility functions
179 ***********************************************************************/
180 /* The timer for scroll bar repetition and menu bar timeouts.
181 NULL if no timer is started. */
182 static struct atimer *xg_timer;
185 /* The next two variables and functions are taken from lwlib. */
186 static widget_value *widget_value_free_list;
187 static int malloc_cpt;
189 /* Allocate a widget_value structure, either by taking one from the
190 widget_value_free_list or by malloc:ing a new one.
192 Return a pointer to the allocated structure. */
194 widget_value *
195 malloc_widget_value ()
197 widget_value *wv;
198 if (widget_value_free_list)
200 wv = widget_value_free_list;
201 widget_value_free_list = wv->free_list;
202 wv->free_list = 0;
204 else
206 wv = (widget_value *) xmalloc (sizeof (widget_value));
207 malloc_cpt++;
209 memset (wv, 0, sizeof (widget_value));
210 return wv;
213 /* This is analogous to free. It frees only what was allocated
214 by malloc_widget_value, and no substructures. */
216 void
217 free_widget_value (wv)
218 widget_value *wv;
220 if (wv->free_list)
221 abort ();
223 if (malloc_cpt > 25)
225 /* When the number of already allocated cells is too big,
226 We free it. */
227 xfree (wv);
228 malloc_cpt--;
230 else
232 wv->free_list = widget_value_free_list;
233 widget_value_free_list = wv;
238 /* Create and return the cursor to be used for popup menus and
239 scroll bars on display DPY. */
241 GdkCursor *
242 xg_create_default_cursor (dpy)
243 Display *dpy;
245 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
246 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
249 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
251 static GdkPixbuf *
252 xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap)
253 GdkPixmap *gpix;
254 GdkPixmap *gmask;
255 GdkColormap *cmap;
257 int x, y, width, height, rowstride, mask_rowstride;
258 GdkPixbuf *icon_buf, *tmp_buf;
259 guchar *pixels;
260 guchar *mask_pixels;
262 gdk_drawable_get_size (gpix, &width, &height);
263 tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
264 0, 0, 0, 0, width, height);
265 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
266 g_object_unref (G_OBJECT (tmp_buf));
268 if (gmask)
270 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
271 gmask,
272 NULL,
273 0, 0, 0, 0,
274 width, height);
275 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
276 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
277 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
278 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
279 int y;
281 for (y = 0; y < height; ++y)
283 guchar *iconptr, *maskptr;
284 int x;
286 iconptr = pixels + y * rowstride;
287 maskptr = mask_pixels + y * mask_rowstride;
289 for (x = 0; x < width; ++x)
291 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
292 just R is sufficient. */
293 if (maskptr[0] == 0)
294 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
296 iconptr += rowstride/width;
297 maskptr += mask_rowstride/width;
301 g_object_unref (G_OBJECT (mask_buf));
304 return icon_buf;
307 static Lisp_Object
308 file_for_image(image)
309 Lisp_Object image;
311 Lisp_Object specified_file = Qnil;
312 Lisp_Object tail;
313 extern Lisp_Object QCfile;
315 for (tail = XCDR (image);
316 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
317 tail = XCDR (XCDR (tail)))
318 if (EQ (XCAR (tail), QCfile))
319 specified_file = XCAR (XCDR (tail));
321 return specified_file;
324 /* For the image defined in IMG, make and return a GtkImage. For displays with
325 8 planes or less we must make a GdkPixbuf and apply the mask manually.
326 Otherwise the highlightning and dimming the tool bar code in GTK does
327 will look bad. For display with more than 8 planes we just use the
328 pixmap and mask directly. For monochrome displays, GTK doesn't seem
329 able to use external pixmaps, it looks bad whatever we do.
330 The image is defined on the display where frame F is.
331 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
332 If OLD_WIDGET is NULL, a new widget is constructed and returned.
333 If OLD_WIDGET is not NULL, that widget is modified. */
335 static GtkWidget *
336 xg_get_image_for_pixmap (f, img, widget, old_widget)
337 FRAME_PTR f;
338 struct image *img;
339 GtkWidget *widget;
340 GtkImage *old_widget;
342 GdkPixmap *gpix;
343 GdkPixmap *gmask;
344 GdkDisplay *gdpy;
345 GdkColormap *cmap;
346 GdkPixbuf *icon_buf;
348 /* If we have a file, let GTK do all the image handling.
349 This seems to be the only way to make insensitive and activated icons
350 look good in all cases. */
351 Lisp_Object specified_file = file_for_image (img->spec);
352 Lisp_Object file;
354 /* We already loaded the image once before calling this
355 function, so this only fails if the image file has been removed.
356 In that case, use the pixmap already loaded. */
358 if (STRINGP (specified_file)
359 && STRINGP (file = x_find_image_file (specified_file)))
361 if (! old_widget)
362 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
363 else
364 gtk_image_set_from_file (old_widget, SSDATA (file));
366 return GTK_WIDGET (old_widget);
369 /* No file, do the image handling ourselves. This will look very bad
370 on a monochrome display, and sometimes bad on all displays with
371 certain themes. */
373 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
374 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
375 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
377 /* This is a workaround to make icons look good on pseudo color
378 displays. Apparently GTK expects the images to have an alpha
379 channel. If they don't, insensitive and activated icons will
380 look bad. This workaround does not work on monochrome displays,
381 and is strictly not needed on true color/static color displays (i.e.
382 16 bits and higher). But we do it anyway so we get a pixbuf that is
383 not associated with the img->pixmap. The img->pixmap may be removed
384 by clearing the image cache and then the tool bar redraw fails, since
385 Gtk+ assumes the pixmap is always there. */
386 cmap = gtk_widget_get_colormap (widget);
387 icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);
389 if (! old_widget)
390 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
391 else
392 gtk_image_set_from_pixbuf (old_widget, icon_buf);
394 g_object_unref (G_OBJECT (icon_buf));
396 g_object_unref (G_OBJECT (gpix));
397 if (gmask) g_object_unref (G_OBJECT (gmask));
399 return GTK_WIDGET (old_widget);
403 /* Set CURSOR on W and all widgets W contain. We must do like this
404 for scroll bars and menu because they create widgets internally,
405 and it is those widgets that are visible. */
407 static void
408 xg_set_cursor (w, cursor)
409 GtkWidget *w;
410 GdkCursor *cursor;
412 GList *children = gdk_window_peek_children (w->window);
414 gdk_window_set_cursor (w->window, cursor);
416 /* The scroll bar widget has more than one GDK window (had to look at
417 the source to figure this out), and there is no way to set cursor
418 on widgets in GTK. So we must set the cursor for all GDK windows.
419 Ditto for menus. */
421 for ( ; children; children = g_list_next (children))
422 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
425 /* Timer function called when a timeout occurs for xg_timer.
426 This function processes all GTK events in a recursive event loop.
427 This is done because GTK timer events are not seen by Emacs event
428 detection, Emacs only looks for X events. When a scroll bar has the
429 pointer (detected by button press/release events below) an Emacs
430 timer is started, and this function can then check if the GTK timer
431 has expired by calling the GTK event loop.
432 Also, when a menu is active, it has a small timeout before it
433 pops down the sub menu under it. */
435 static void
436 xg_process_timeouts (timer)
437 struct atimer *timer;
439 BLOCK_INPUT;
440 /* Ideally we would like to just handle timer events, like the Xt version
441 of this does in xterm.c, but there is no such feature in GTK. */
442 while (gtk_events_pending ())
443 gtk_main_iteration ();
444 UNBLOCK_INPUT;
447 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
448 xg_process_timeouts is called when the timer expires. The timer
449 started is continuous, i.e. runs until xg_stop_timer is called. */
451 static void
452 xg_start_timer ()
454 if (! xg_timer)
456 EMACS_TIME interval;
457 EMACS_SET_SECS_USECS (interval, 0, 100000);
458 xg_timer = start_atimer (ATIMER_CONTINUOUS,
459 interval,
460 xg_process_timeouts,
465 /* Stop the xg_timer if started. */
467 static void
468 xg_stop_timer ()
470 if (xg_timer)
472 cancel_atimer (xg_timer);
473 xg_timer = 0;
477 /* Insert NODE into linked LIST. */
479 static void
480 xg_list_insert (xg_list_node *list, xg_list_node *node)
482 xg_list_node *list_start = list->next;
484 if (list_start) list_start->prev = node;
485 node->next = list_start;
486 node->prev = 0;
487 list->next = node;
490 /* Remove NODE from linked LIST. */
492 static void
493 xg_list_remove (xg_list_node *list, xg_list_node *node)
495 xg_list_node *list_start = list->next;
496 if (node == list_start)
498 list->next = node->next;
499 if (list->next) list->next->prev = 0;
501 else
503 node->prev->next = node->next;
504 if (node->next) node->next->prev = node->prev;
508 /* Allocate and return a utf8 version of STR. If STR is already
509 utf8 or NULL, just return STR.
510 If not, a new string is allocated and the caller must free the result
511 with g_free. */
513 static char *
514 get_utf8_string (str)
515 char *str;
517 char *utf8_str = str;
519 if (!str) return NULL;
521 /* If not UTF-8, try current locale. */
522 if (!g_utf8_validate (str, -1, NULL))
523 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
525 if (!utf8_str)
527 /* Probably some control characters in str. Escape them. */
528 size_t nr_bad = 0;
529 gsize bytes_read;
530 gsize bytes_written;
531 unsigned char *p = (unsigned char *)str;
532 char *cp, *up;
533 GError *error = NULL;
535 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
536 &bytes_written, &error))
537 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
539 ++nr_bad;
540 p += bytes_written+1;
541 g_error_free (error);
542 error = NULL;
545 if (error)
547 g_error_free (error);
548 error = NULL;
550 if (cp) g_free (cp);
552 up = utf8_str = xmalloc (strlen (str) + nr_bad * 4 + 1);
553 p = (unsigned char *)str;
555 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
556 &bytes_written, &error))
557 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
559 strncpy (up, (char *)p, bytes_written);
560 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
561 up[bytes_written+4] = '\0';
562 up += bytes_written+4;
563 p += bytes_written+1;
564 g_error_free (error);
565 error = NULL;
568 if (cp)
570 strcat (utf8_str, cp);
571 g_free (cp);
573 if (error)
575 g_error_free (error);
576 error = NULL;
579 return utf8_str;
584 /***********************************************************************
585 General functions for creating widgets, resizing, events, e.t.c.
586 ***********************************************************************/
588 /* Make a geometry string and pass that to GTK. It seems this is the
589 only way to get geometry position right if the user explicitly
590 asked for a position when starting Emacs.
591 F is the frame we shall set geometry for. */
593 static void
594 xg_set_geometry (f)
595 FRAME_PTR f;
597 if (f->size_hint_flags & USPosition)
599 int left = f->left_pos;
600 int xneg = f->size_hint_flags & XNegative;
601 int top = f->top_pos;
602 int yneg = f->size_hint_flags & YNegative;
603 char geom_str[32];
605 if (xneg)
606 left = -left;
607 if (yneg)
608 top = -top;
610 sprintf (geom_str, "=%dx%d%c%d%c%d",
611 FRAME_PIXEL_WIDTH (f),
612 FRAME_TOTAL_PIXEL_HEIGHT (f),
613 (xneg ? '-' : '+'), left,
614 (yneg ? '-' : '+'), top);
616 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
617 geom_str))
618 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
620 else if (f->size_hint_flags & PPosition)
621 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
622 f->left_pos, f->top_pos);
626 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
627 and a Gtk+ menu bar, we get resize events for the edit part of the
628 frame only. We let Gtk+ deal with the Gtk+ parts.
629 F is the frame to resize.
630 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
632 void
633 xg_frame_resized (f, pixelwidth, pixelheight)
634 FRAME_PTR f;
635 int pixelwidth, pixelheight;
637 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelheight);
638 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
640 if (FRAME_GTK_WIDGET (f)
641 && (columns != FRAME_COLS (f)
642 || rows != FRAME_LINES (f)
643 || pixelwidth != FRAME_PIXEL_WIDTH (f)
644 || pixelheight != FRAME_PIXEL_HEIGHT (f)))
646 FRAME_PIXEL_WIDTH (f) = pixelwidth;
647 FRAME_PIXEL_HEIGHT (f) = pixelheight;
649 change_frame_size (f, rows, columns, 0, 1, 0);
650 SET_FRAME_GARBAGED (f);
651 cancel_mouse_face (f);
655 /* Process all pending events on the display for frame F. */
657 static void
658 flush_and_sync (f)
659 FRAME_PTR f;
661 gdk_window_process_all_updates ();
662 x_sync (f);
663 while (gtk_events_pending ())
665 gtk_main_iteration ();
666 gdk_window_process_all_updates ();
667 x_sync (f);
671 /* Turn wm hints for resize off on frame F */
673 static void
674 x_wm_size_hint_off (f)
675 FRAME_PTR f;
677 GdkGeometry size_hints;
678 gint hint_flags = 0;
679 memset (&size_hints, 0, sizeof (size_hints));
680 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
681 size_hints.width_inc = 1;
682 size_hints.height_inc = 1;
683 hint_flags |= GDK_HINT_BASE_SIZE;
684 size_hints.base_width = 1;
685 size_hints.base_height = 1;
686 size_hints.min_width = 1;
687 size_hints.min_height = 1;
688 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
689 NULL,
690 &size_hints,
691 hint_flags);
692 /* Make sure these get set again in next call to x_wm_set_size_hint. */
693 f->output_data.x->hint_flags &= ~hint_flags;
694 flush_and_sync (f);
697 /* Resize the outer window of frame F after chainging the height.
698 This happend when the menu bar or the tool bar is added or removed.
699 COLUMNS/ROWS is the size the edit area shall have after the resize. */
701 void
702 xg_frame_set_char_size (f, cols, rows)
703 FRAME_PTR f;
704 int cols;
705 int rows;
707 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
708 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
709 int pixelwidth;
711 if (FRAME_PIXEL_HEIGHT (f) == 0)
712 return;
714 /* Take into account the size of the scroll bar. Always use the
715 number of columns occupied by the scroll bar here otherwise we
716 might end up with a frame width that is not a multiple of the
717 frame's character width which is bad for vertically split
718 windows. */
719 f->scroll_bar_actual_width
720 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
722 compute_fringe_widths (f, 0);
724 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
725 after calculating that value. */
726 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
728 /* Must resize our top level widget. Font size may have changed,
729 but not rows/cols.
730 Turn wm hints (min/max size and size increments) of temporarly.
731 It interferes too much, when for example adding or removing the
732 menu/tool bar. */
733 x_wm_size_hint_off (f);
734 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
735 pixelwidth, pixelheight);
736 flush_and_sync (f);
737 x_wm_set_size_hint (f, 0, 0);
740 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
741 Must be done like this, because GtkWidget:s can have "hidden"
742 X Window that aren't accessible.
744 Return 0 if no widget match WDESC. */
746 GtkWidget *
747 xg_win_to_widget (dpy, wdesc)
748 Display *dpy;
749 Window wdesc;
751 gpointer gdkwin;
752 GtkWidget *gwdesc = 0;
754 BLOCK_INPUT;
756 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
757 wdesc);
758 if (gdkwin)
760 GdkEvent event;
761 event.any.window = gdkwin;
762 gwdesc = gtk_get_event_widget (&event);
765 UNBLOCK_INPUT;
766 return gwdesc;
769 /* Fill in the GdkColor C so that it represents PIXEL.
770 W is the widget that color will be used for. Used to find colormap. */
772 static void
773 xg_pix_to_gcolor (w, pixel, c)
774 GtkWidget *w;
775 unsigned long pixel;
776 GdkColor *c;
778 GdkColormap *map = gtk_widget_get_colormap (w);
779 gdk_colormap_query_color (map, pixel, c);
782 /* Create and set up the GTK widgets for frame F.
783 Return 0 if creation failed, non-zero otherwise. */
786 xg_create_frame_widgets (f)
787 FRAME_PTR f;
789 GtkWidget *wtop;
790 GtkWidget *wvbox;
791 GtkWidget *wfixed;
792 GdkColor bg;
793 GtkRcStyle *style;
794 int i;
795 char *title = 0;
797 BLOCK_INPUT;
799 if (FRAME_X_EMBEDDED_P (f))
800 wtop = gtk_plug_new (f->output_data.x->parent_desc);
801 else
802 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
804 xg_set_screen (wtop, f);
806 wvbox = gtk_vbox_new (FALSE, 0);
807 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
809 if (! wtop || ! wvbox || ! wfixed)
811 if (wtop) gtk_widget_destroy (wtop);
812 if (wvbox) gtk_widget_destroy (wvbox);
813 if (wfixed) gtk_widget_destroy (wfixed);
815 UNBLOCK_INPUT;
816 return 0;
819 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
820 gtk_widget_set_name (wtop, EMACS_CLASS);
821 gtk_widget_set_name (wvbox, "pane");
822 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
824 /* If this frame has a title or name, set it in the title bar. */
825 if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
826 else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
828 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
830 FRAME_GTK_OUTER_WIDGET (f) = wtop;
831 FRAME_GTK_WIDGET (f) = wfixed;
832 f->output_data.x->vbox_widget = wvbox;
834 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
836 gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f),
837 FRAME_PIXEL_HEIGHT (f));
839 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
840 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
842 if (FRAME_EXTERNAL_TOOL_BAR (f))
843 update_frame_tool_bar (f);
845 /* We don't want this widget double buffered, because we draw on it
846 with regular X drawing primitives, so from a GTK/GDK point of
847 view, the widget is totally blank. When an expose comes, this
848 will make the widget blank, and then Emacs redraws it. This flickers
849 a lot, so we turn off double buffering. */
850 gtk_widget_set_double_buffered (wfixed, FALSE);
852 /* GTK documents says use gtk_window_set_resizable. But then a user
853 can't shrink the window from its starting size. */
854 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
855 gtk_window_set_wmclass (GTK_WINDOW (wtop),
856 SSDATA (Vx_resource_name),
857 SSDATA (Vx_resource_class));
859 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
860 GTK is to destroy the widget. We want Emacs to do that instead. */
861 g_signal_connect (G_OBJECT (wtop), "delete-event",
862 G_CALLBACK (gtk_true), 0);
864 /* Convert our geometry parameters into a geometry string
865 and specify it.
866 GTK will itself handle calculating the real position this way. */
867 xg_set_geometry (f);
869 gtk_widget_add_events (wfixed,
870 GDK_POINTER_MOTION_MASK
871 | GDK_EXPOSURE_MASK
872 | GDK_BUTTON_PRESS_MASK
873 | GDK_BUTTON_RELEASE_MASK
874 | GDK_KEY_PRESS_MASK
875 | GDK_ENTER_NOTIFY_MASK
876 | GDK_LEAVE_NOTIFY_MASK
877 | GDK_FOCUS_CHANGE_MASK
878 | GDK_STRUCTURE_MASK
879 | GDK_VISIBILITY_NOTIFY_MASK);
881 /* Must realize the windows so the X window gets created. It is used
882 by callers of this function. */
883 gtk_widget_realize (wfixed);
884 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
886 /* Since GTK clears its window by filling with the background color,
887 we must keep X and GTK background in sync. */
888 xg_pix_to_gcolor (wfixed, FRAME_BACKGROUND_PIXEL (f), &bg);
889 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
891 /* Also, do not let any background pixmap to be set, this looks very
892 bad as Emacs overwrites the background pixmap with its own idea
893 of background color. */
894 style = gtk_widget_get_modifier_style (wfixed);
896 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
897 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
898 gtk_widget_modify_style (wfixed, style);
900 /* GTK does not set any border, and they look bad with GTK. */
901 /* That they look bad is no excuse for imposing this here. --Stef
902 It should be done by providing the proper default in Fx_create_Frame.
903 f->border_width = 0;
904 f->internal_border_width = 0; */
906 UNBLOCK_INPUT;
908 return 1;
911 /* Set the normal size hints for the window manager, for frame F.
912 FLAGS is the flags word to use--or 0 meaning preserve the flags
913 that the window now has.
914 If USER_POSITION is nonzero, we set the User Position
915 flag (this is useful when FLAGS is 0). */
917 void
918 x_wm_set_size_hint (f, flags, user_position)
919 FRAME_PTR f;
920 long flags;
921 int user_position;
923 /* Don't set size hints during initialization; that apparently leads
924 to a race condition. See the thread at
925 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
926 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
927 return;
929 /* Must use GTK routines here, otherwise GTK resets the size hints
930 to its own defaults. */
931 GdkGeometry size_hints;
932 gint hint_flags = 0;
933 int base_width, base_height;
934 int min_rows = 0, min_cols = 0;
935 int win_gravity = f->win_gravity;
937 if (flags)
939 memset (&size_hints, 0, sizeof (size_hints));
940 f->output_data.x->size_hints = size_hints;
941 f->output_data.x->hint_flags = hint_flags;
943 else
944 flags = f->size_hint_flags;
946 size_hints = f->output_data.x->size_hints;
947 hint_flags = f->output_data.x->hint_flags;
949 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
950 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
951 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
953 hint_flags |= GDK_HINT_BASE_SIZE;
954 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
955 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
956 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
958 check_frame_size (f, &min_rows, &min_cols);
960 size_hints.base_width = base_width;
961 size_hints.base_height = base_height;
962 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
963 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
965 /* These currently have a one to one mapping with the X values, but I
966 don't think we should rely on that. */
967 hint_flags |= GDK_HINT_WIN_GRAVITY;
968 size_hints.win_gravity = 0;
969 if (win_gravity == NorthWestGravity)
970 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
971 else if (win_gravity == NorthGravity)
972 size_hints.win_gravity = GDK_GRAVITY_NORTH;
973 else if (win_gravity == NorthEastGravity)
974 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
975 else if (win_gravity == WestGravity)
976 size_hints.win_gravity = GDK_GRAVITY_WEST;
977 else if (win_gravity == CenterGravity)
978 size_hints.win_gravity = GDK_GRAVITY_CENTER;
979 else if (win_gravity == EastGravity)
980 size_hints.win_gravity = GDK_GRAVITY_EAST;
981 else if (win_gravity == SouthWestGravity)
982 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
983 else if (win_gravity == SouthGravity)
984 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
985 else if (win_gravity == SouthEastGravity)
986 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
987 else if (win_gravity == StaticGravity)
988 size_hints.win_gravity = GDK_GRAVITY_STATIC;
990 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
991 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
992 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
994 if (user_position)
996 hint_flags &= ~GDK_HINT_POS;
997 hint_flags |= GDK_HINT_USER_POS;
1000 if (hint_flags != f->output_data.x->hint_flags
1001 || memcmp (&size_hints,
1002 &f->output_data.x->size_hints,
1003 sizeof (size_hints)) != 0)
1005 BLOCK_INPUT;
1006 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1007 NULL, &size_hints, hint_flags);
1008 f->output_data.x->size_hints = size_hints;
1009 f->output_data.x->hint_flags = hint_flags;
1010 UNBLOCK_INPUT;
1014 /* Change background color of a frame.
1015 Since GTK uses the background color to clear the window, we must
1016 keep the GTK and X colors in sync.
1017 F is the frame to change,
1018 BG is the pixel value to change to. */
1020 void
1021 xg_set_background_color (f, bg)
1022 FRAME_PTR f;
1023 unsigned long bg;
1025 if (FRAME_GTK_WIDGET (f))
1027 GdkColor gdk_bg;
1029 BLOCK_INPUT;
1030 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
1031 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
1032 UNBLOCK_INPUT;
1037 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1038 functions so GTK does not overwrite the icon. */
1040 void
1041 xg_set_frame_icon (f, icon_pixmap, icon_mask)
1042 FRAME_PTR f;
1043 Pixmap icon_pixmap;
1044 Pixmap icon_mask;
1046 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1047 GdkPixmap *gpix = gdk_pixmap_foreign_new_for_display (gdpy, icon_pixmap);
1048 GdkPixmap *gmask = gdk_pixmap_foreign_new_for_display (gdpy, icon_mask);
1049 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, NULL);
1051 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1056 /***********************************************************************
1057 Dialog functions
1058 ***********************************************************************/
1059 /* Return the dialog title to use for a dialog of type KEY.
1060 This is the encoding used by lwlib. We use the same for GTK. */
1062 static char *
1063 get_dialog_title (char key)
1065 char *title = "";
1067 switch (key) {
1068 case 'E': case 'e':
1069 title = "Error";
1070 break;
1072 case 'I': case 'i':
1073 title = "Information";
1074 break;
1076 case 'L': case 'l':
1077 title = "Prompt";
1078 break;
1080 case 'P': case 'p':
1081 title = "Prompt";
1082 break;
1084 case 'Q': case 'q':
1085 title = "Question";
1086 break;
1089 return title;
1092 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1093 the dialog, but return TRUE so the event does not propagate further
1094 in GTK. This prevents GTK from destroying the dialog widget automatically
1095 and we can always destrou the widget manually, regardles of how
1096 it was popped down (button press or WM_DELETE_WINDOW).
1097 W is the dialog widget.
1098 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1099 user_data is NULL (not used).
1101 Returns TRUE to end propagation of event. */
1103 static gboolean
1104 dialog_delete_callback (w, event, user_data)
1105 GtkWidget *w;
1106 GdkEvent *event;
1107 gpointer user_data;
1109 gtk_widget_unmap (w);
1110 return TRUE;
1113 /* Create a popup dialog window. See also xg_create_widget below.
1114 WV is a widget_value describing the dialog.
1115 SELECT_CB is the callback to use when a button has been pressed.
1116 DEACTIVATE_CB is the callback to use when the dialog pops down.
1118 Returns the GTK dialog widget. */
1120 static GtkWidget *
1121 create_dialog (wv, select_cb, deactivate_cb)
1122 widget_value *wv;
1123 GCallback select_cb;
1124 GCallback deactivate_cb;
1126 char *title = get_dialog_title (wv->name[0]);
1127 int total_buttons = wv->name[1] - '0';
1128 int right_buttons = wv->name[4] - '0';
1129 int left_buttons;
1130 int button_nr = 0;
1131 int button_spacing = 10;
1132 GtkWidget *wdialog = gtk_dialog_new ();
1133 widget_value *item;
1134 GtkBox *cur_box;
1135 GtkWidget *wvbox;
1136 GtkWidget *whbox_up;
1137 GtkWidget *whbox_down;
1139 /* If the number of buttons is greater than 4, make two rows of buttons
1140 instead. This looks better. */
1141 int make_two_rows = total_buttons > 4;
1143 if (right_buttons == 0) right_buttons = total_buttons/2;
1144 left_buttons = total_buttons - right_buttons;
1146 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1147 gtk_widget_set_name (wdialog, "emacs-dialog");
1149 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
1151 if (make_two_rows)
1153 wvbox = gtk_vbox_new (TRUE, button_spacing);
1154 whbox_up = gtk_hbox_new (FALSE, 0);
1155 whbox_down = gtk_hbox_new (FALSE, 0);
1157 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1158 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1159 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1161 cur_box = GTK_BOX (whbox_up);
1164 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1165 G_CALLBACK (dialog_delete_callback), 0);
1167 if (deactivate_cb)
1169 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1170 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1173 for (item = wv->contents; item; item = item->next)
1175 char *utf8_label = get_utf8_string (item->value);
1176 GtkWidget *w;
1177 GtkRequisition req;
1179 if (item->name && strcmp (item->name, "message") == 0)
1181 /* This is the text part of the dialog. */
1182 w = gtk_label_new (utf8_label);
1183 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1184 gtk_label_new (""),
1185 FALSE, FALSE, 0);
1186 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
1187 TRUE, TRUE, 0);
1188 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1190 /* Try to make dialog look better. Must realize first so
1191 the widget can calculate the size it needs. */
1192 gtk_widget_realize (w);
1193 gtk_widget_size_request (w, &req);
1194 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1195 req.height);
1196 if (item->value && strlen (item->value) > 0)
1197 button_spacing = 2*req.width/strlen (item->value);
1199 else
1201 /* This is one button to add to the dialog. */
1202 w = gtk_button_new_with_label (utf8_label);
1203 if (! item->enabled)
1204 gtk_widget_set_sensitive (w, FALSE);
1205 if (select_cb)
1206 g_signal_connect (G_OBJECT (w), "clicked",
1207 select_cb, item->call_data);
1209 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1210 if (++button_nr == left_buttons)
1212 if (make_two_rows)
1213 cur_box = GTK_BOX (whbox_down);
1214 else
1215 gtk_box_pack_start (cur_box,
1216 gtk_label_new (""),
1217 TRUE, TRUE,
1218 button_spacing);
1222 if (utf8_label && utf8_label != item->value)
1223 g_free (utf8_label);
1226 return wdialog;
1231 /***********************************************************************
1232 File dialog functions
1233 ***********************************************************************/
1234 /* Return non-zero if the old file selection dialog is being used.
1235 Return zero if not. */
1238 xg_uses_old_file_dialog ()
1240 #ifdef HAVE_GTK_FILE_BOTH
1241 extern int x_gtk_use_old_file_dialog;
1242 return x_gtk_use_old_file_dialog;
1243 #else /* ! HAVE_GTK_FILE_BOTH */
1245 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1246 return 1;
1247 #else
1248 return 0;
1249 #endif
1251 #endif /* ! HAVE_GTK_FILE_BOTH */
1255 /* Function that is called when the file or font dialogs pop down.
1256 W is the dialog widget, RESPONSE is the response code.
1257 USER_DATA is what we passed in to g_signal_connect (pointer to int). */
1259 static void
1260 xg_dialog_response_cb (w,
1261 response,
1262 user_data)
1263 GtkDialog *w;
1264 gint response;
1265 gpointer user_data;
1267 int *ptr = (int *) user_data;
1268 *ptr = response;
1272 /* Destroy the dialog. This makes it pop down. */
1274 static Lisp_Object
1275 pop_down_dialog (arg)
1276 Lisp_Object arg;
1278 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1279 BLOCK_INPUT;
1280 gtk_widget_destroy (GTK_WIDGET (p->pointer));
1281 UNBLOCK_INPUT;
1282 return Qnil;
1285 typedef char * (*xg_get_file_func) P_ ((GtkWidget *));
1287 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1289 /* Return the selected file for file chooser dialog W.
1290 The returned string must be free:d. */
1292 static char *
1293 xg_get_file_name_from_chooser (w)
1294 GtkWidget *w;
1296 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1299 /* Callback called when the "Show hidden files" toggle is pressed.
1300 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1302 static void
1303 xg_toggle_visibility_cb (widget, data)
1304 GtkWidget *widget;
1305 gpointer data;
1307 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1308 gboolean visible;
1309 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1310 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1314 /* Callback called when a property changes in a file chooser.
1315 GOBJECT is the file chooser dialog, ARG1 describes the property.
1316 USER_DATA is the toggle widget in the file chooser dialog.
1317 We use this to update the "Show hidden files" toggle when the user
1318 changes that property by right clicking in the file list. */
1320 static void
1321 xg_toggle_notify_cb (gobject, arg1, user_data)
1322 GObject *gobject;
1323 GParamSpec *arg1;
1324 gpointer user_data;
1326 extern int x_gtk_show_hidden_files;
1328 if (strcmp (arg1->name, "show-hidden") == 0)
1330 GtkFileChooser *dialog = GTK_FILE_CHOOSER (gobject);
1331 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1332 gboolean visible, toggle_on;
1334 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1335 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1337 if (!!visible != !!toggle_on)
1339 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1340 G_CALLBACK (xg_toggle_visibility_cb),
1341 gobject);
1342 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1343 g_signal_handlers_unblock_by_func
1344 (G_OBJECT (wtoggle),
1345 G_CALLBACK (xg_toggle_visibility_cb),
1346 gobject);
1348 x_gtk_show_hidden_files = visible;
1352 /* Read a file name from the user using a file chooser dialog.
1353 F is the current frame.
1354 PROMPT is a prompt to show to the user. May not be NULL.
1355 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1356 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1357 file. *FUNC is set to a function that can be used to retrieve the
1358 selected file name from the returned widget.
1360 Returns the created widget. */
1362 static GtkWidget *
1363 xg_get_file_with_chooser (f, prompt, default_filename,
1364 mustmatch_p, only_dir_p, func)
1365 FRAME_PTR f;
1366 char *prompt;
1367 char *default_filename;
1368 int mustmatch_p, only_dir_p;
1369 xg_get_file_func *func;
1371 char message[1024];
1373 GtkWidget *filewin, *wtoggle, *wbox, *wmessage;
1374 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1375 GtkFileChooserAction action = (mustmatch_p ?
1376 GTK_FILE_CHOOSER_ACTION_OPEN :
1377 GTK_FILE_CHOOSER_ACTION_SAVE);
1378 extern int x_gtk_show_hidden_files;
1379 extern int x_gtk_file_dialog_help_text;
1382 if (only_dir_p)
1383 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1385 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1386 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1387 (mustmatch_p || only_dir_p ?
1388 GTK_STOCK_OPEN : GTK_STOCK_OK),
1389 GTK_RESPONSE_OK,
1390 NULL);
1391 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1393 wbox = gtk_vbox_new (FALSE, 0);
1394 gtk_widget_show (wbox);
1395 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1397 if (x_gtk_show_hidden_files)
1399 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1400 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1402 gtk_widget_show (wtoggle);
1403 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1404 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1405 g_signal_connect (G_OBJECT (filewin), "notify",
1406 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1408 if (x_gtk_file_dialog_help_text)
1410 message[0] = '\0';
1411 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1412 Show the C-l help text only for versions < 2.10. */
1413 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1414 strcat (message, "\nType C-l to display a file name text entry box.\n");
1415 strcat (message, "\nIf you don't like this file selector, use the "
1416 "corresponding\nkey binding or customize "
1417 "use-file-dialog to turn it off.");
1419 wmessage = gtk_label_new (message);
1420 gtk_widget_show (wmessage);
1423 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1424 if (x_gtk_file_dialog_help_text)
1425 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1426 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1428 if (default_filename)
1430 Lisp_Object file;
1431 struct gcpro gcpro1;
1432 char *utf8_filename;
1433 GCPRO1 (file);
1435 file = build_string (default_filename);
1437 /* File chooser does not understand ~/... in the file name. It must be
1438 an absolute name starting with /. */
1439 if (default_filename[0] != '/')
1440 file = Fexpand_file_name (file, Qnil);
1442 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1443 if (! NILP (Ffile_directory_p (file)))
1444 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1445 utf8_filename);
1446 else
1448 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1449 utf8_filename);
1450 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1452 char *cp = strrchr (utf8_filename, '/');
1453 if (cp) ++cp;
1454 else cp = utf8_filename;
1455 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1459 UNGCPRO;
1462 *func = xg_get_file_name_from_chooser;
1463 return filewin;
1465 #endif /* HAVE_GTK_FILE_CHOOSER_DIALOG_NEW */
1467 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1469 /* Return the selected file for file selector dialog W.
1470 The returned string must be free:d. */
1472 static char *
1473 xg_get_file_name_from_selector (w)
1474 GtkWidget *w;
1476 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1477 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1480 /* Create a file selection dialog.
1481 F is the current frame.
1482 PROMPT is a prompt to show to the user. May not be NULL.
1483 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1484 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1485 file. *FUNC is set to a function that can be used to retrieve the
1486 selected file name from the returned widget.
1488 Returns the created widget. */
1490 static GtkWidget *
1491 xg_get_file_with_selection (f, prompt, default_filename,
1492 mustmatch_p, only_dir_p, func)
1493 FRAME_PTR f;
1494 char *prompt;
1495 char *default_filename;
1496 int mustmatch_p, only_dir_p;
1497 xg_get_file_func *func;
1499 GtkWidget *filewin;
1500 GtkFileSelection *filesel;
1502 filewin = gtk_file_selection_new (prompt);
1503 filesel = GTK_FILE_SELECTION (filewin);
1505 if (default_filename)
1506 gtk_file_selection_set_filename (filesel, default_filename);
1508 if (mustmatch_p)
1510 /* The selection_entry part of filesel is not documented. */
1511 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1512 gtk_file_selection_hide_fileop_buttons (filesel);
1515 *func = xg_get_file_name_from_selector;
1517 return filewin;
1519 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1521 /* Read a file name from the user using a file dialog, either the old
1522 file selection dialog, or the new file chooser dialog. Which to use
1523 depends on what the GTK version used has, and what the value of
1524 gtk-use-old-file-dialog.
1525 F is the current frame.
1526 PROMPT is a prompt to show to the user. May not be NULL.
1527 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1528 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1529 file.
1531 Returns a file name or NULL if no file was selected.
1532 The returned string must be freed by the caller. */
1534 char *
1535 xg_get_file_name (f, prompt, default_filename, mustmatch_p, only_dir_p)
1536 FRAME_PTR f;
1537 char *prompt;
1538 char *default_filename;
1539 int mustmatch_p, only_dir_p;
1541 GtkWidget *w = 0;
1542 int count = SPECPDL_INDEX ();
1543 char *fn = 0;
1544 int filesel_done = 0;
1545 xg_get_file_func func;
1547 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1548 /* I really don't know why this is needed, but without this the GLIBC add on
1549 library linuxthreads hangs when the Gnome file chooser backend creates
1550 threads. */
1551 sigblock (sigmask (__SIGRTMIN));
1552 #endif /* HAVE_GTK_AND_PTHREAD */
1554 #ifdef HAVE_GTK_FILE_BOTH
1556 if (xg_uses_old_file_dialog ())
1557 w = xg_get_file_with_selection (f, prompt, default_filename,
1558 mustmatch_p, only_dir_p, &func);
1559 else
1560 w = xg_get_file_with_chooser (f, prompt, default_filename,
1561 mustmatch_p, only_dir_p, &func);
1563 #else /* not HAVE_GTK_FILE_BOTH */
1565 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1566 w = xg_get_file_with_selection (f, prompt, default_filename,
1567 mustmatch_p, only_dir_p, &func);
1568 #endif
1569 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1570 w = xg_get_file_with_chooser (f, prompt, default_filename,
1571 mustmatch_p, only_dir_p, &func);
1572 #endif
1574 #endif /* HAVE_GTK_FILE_BOTH */
1576 xg_set_screen (w, f);
1577 gtk_widget_set_name (w, "emacs-filedialog");
1578 gtk_window_set_transient_for (GTK_WINDOW (w),
1579 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1580 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1581 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1583 g_signal_connect (G_OBJECT (w),
1584 "response",
1585 G_CALLBACK (xg_dialog_response_cb),
1586 &filesel_done);
1588 /* Don't destroy the widget if closed by the window manager close button. */
1589 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1591 gtk_widget_show (w);
1593 record_unwind_protect (pop_down_dialog, make_save_value (w, 0));
1594 while (! filesel_done)
1596 x_menu_wait_for_event (0);
1597 gtk_main_iteration ();
1600 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1601 sigunblock (sigmask (__SIGRTMIN));
1602 #endif
1604 if (filesel_done == GTK_RESPONSE_OK)
1605 fn = (*func) (w);
1607 unbind_to (count, Qnil);
1609 return fn;
1612 #ifdef HAVE_FREETYPE
1613 /* Pop up a GTK font selector and return the name of the font the user
1614 selects, as a C string. The returned font name follows GTK's own
1615 format:
1617 `FAMILY [VALUE1 VALUE2] SIZE'
1619 This can be parsed using font_parse_fcname in font.c.
1620 DEFAULT_NAME, if non-zero, is the default font name. */
1622 char *
1623 xg_get_font_name (f, default_name)
1624 FRAME_PTR f;
1625 char *default_name;
1627 GtkWidget *w = 0;
1628 int count = SPECPDL_INDEX ();
1629 char *fontname = NULL;
1630 int done = 0;
1632 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1633 sigblock (sigmask (__SIGRTMIN));
1634 #endif /* HAVE_GTK_AND_PTHREAD */
1636 w = gtk_font_selection_dialog_new ("Pick a font");
1637 if (default_name)
1638 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w),
1639 default_name);
1641 xg_set_screen (w, f);
1642 gtk_widget_set_name (w, "emacs-fontdialog");
1643 gtk_window_set_transient_for (GTK_WINDOW (w),
1644 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1645 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1646 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1648 g_signal_connect (G_OBJECT (w), "response",
1649 G_CALLBACK (xg_dialog_response_cb), &done);
1651 /* Don't destroy the widget if closed by the window manager close button. */
1652 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1654 gtk_widget_show (w);
1656 record_unwind_protect (pop_down_dialog, make_save_value (w, 0));
1657 while (!done)
1659 x_menu_wait_for_event (0);
1660 gtk_main_iteration ();
1663 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1664 sigunblock (sigmask (__SIGRTMIN));
1665 #endif
1667 if (done == GTK_RESPONSE_OK)
1668 fontname = gtk_font_selection_dialog_get_font_name
1669 ((GtkFontSelectionDialog *) w);
1671 unbind_to (count, Qnil);
1673 return fontname;
1675 #endif /* HAVE_FREETYPE */
1679 /***********************************************************************
1680 Menu functions.
1681 ***********************************************************************/
1683 /* The name of menu items that can be used for customization. Since GTK
1684 RC files are very crude and primitive, we have to set this on all
1685 menu item names so a user can easily customize menu items. */
1687 #define MENU_ITEM_NAME "emacs-menuitem"
1690 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1691 during GC. The next member points to the items. */
1692 static xg_list_node xg_menu_cb_list;
1694 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1695 during GC. The next member points to the items. */
1696 static xg_list_node xg_menu_item_cb_list;
1698 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1699 F is the frame CL_DATA will be initialized for.
1700 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1702 The menu bar and all sub menus under the menu bar in a frame
1703 share the same structure, hence the reference count.
1705 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1706 allocated xg_menu_cb_data if CL_DATA is NULL. */
1708 static xg_menu_cb_data *
1709 make_cl_data (cl_data, f, highlight_cb)
1710 xg_menu_cb_data *cl_data;
1711 FRAME_PTR f;
1712 GCallback highlight_cb;
1714 if (! cl_data)
1716 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1717 cl_data->f = f;
1718 cl_data->menu_bar_vector = f->menu_bar_vector;
1719 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1720 cl_data->highlight_cb = highlight_cb;
1721 cl_data->ref_count = 0;
1723 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1726 cl_data->ref_count++;
1728 return cl_data;
1731 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1732 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1734 When the menu bar is updated, menu items may have been added and/or
1735 removed, so menu_bar_vector and menu_bar_items_used change. We must
1736 then update CL_DATA since it is used to determine which menu
1737 item that is invoked in the menu.
1738 HIGHLIGHT_CB could change, there is no check that the same
1739 function is given when modifying a menu bar as was given when
1740 creating the menu bar. */
1742 static void
1743 update_cl_data (cl_data, f, highlight_cb)
1744 xg_menu_cb_data *cl_data;
1745 FRAME_PTR f;
1746 GCallback highlight_cb;
1748 if (cl_data)
1750 cl_data->f = f;
1751 cl_data->menu_bar_vector = f->menu_bar_vector;
1752 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1753 cl_data->highlight_cb = highlight_cb;
1757 /* Decrease reference count for CL_DATA.
1758 If reference count is zero, free CL_DATA. */
1760 static void
1761 unref_cl_data (cl_data)
1762 xg_menu_cb_data *cl_data;
1764 if (cl_data && cl_data->ref_count > 0)
1766 cl_data->ref_count--;
1767 if (cl_data->ref_count == 0)
1769 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1770 xfree (cl_data);
1775 /* Function that marks all lisp data during GC. */
1777 void
1778 xg_mark_data ()
1780 xg_list_node *iter;
1782 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1783 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1785 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1787 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1789 if (! NILP (cb_data->help))
1790 mark_object (cb_data->help);
1795 /* Callback called when a menu item is destroyed. Used to free data.
1796 W is the widget that is being destroyed (not used).
1797 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1799 static void
1800 menuitem_destroy_callback (w, client_data)
1801 GtkWidget *w;
1802 gpointer client_data;
1804 if (client_data)
1806 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1807 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1808 xfree (data);
1812 /* Callback called when the pointer enters/leaves a menu item.
1813 W is the parent of the menu item.
1814 EVENT is either an enter event or leave event.
1815 CLIENT_DATA is not used.
1817 Returns FALSE to tell GTK to keep processing this event. */
1819 static gboolean
1820 menuitem_highlight_callback (w, event, client_data)
1821 GtkWidget *w;
1822 GdkEventCrossing *event;
1823 gpointer client_data;
1825 GdkEvent ev;
1826 GtkWidget *subwidget;
1827 xg_menu_item_cb_data *data;
1829 ev.crossing = *event;
1830 subwidget = gtk_get_event_widget (&ev);
1831 data = (xg_menu_item_cb_data *) g_object_get_data (G_OBJECT (subwidget),
1832 XG_ITEM_DATA);
1833 if (data)
1835 if (! NILP (data->help) && data->cl_data->highlight_cb)
1837 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
1838 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1839 (*func) (subwidget, call_data);
1843 return FALSE;
1846 /* Callback called when a menu is destroyed. Used to free data.
1847 W is the widget that is being destroyed (not used).
1848 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1850 static void
1851 menu_destroy_callback (w, client_data)
1852 GtkWidget *w;
1853 gpointer client_data;
1855 unref_cl_data ((xg_menu_cb_data*) client_data);
1858 /* Callback called when a menu does a grab or ungrab. That means the
1859 menu has been activated or deactivated.
1860 Used to start a timer so the small timeout the menus in GTK uses before
1861 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1862 W is the widget that does the grab (not used).
1863 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1864 CLIENT_DATA is NULL (not used). */
1866 /* Keep track of total number of grabs. */
1867 static int menu_grab_callback_cnt;
1869 static void
1870 menu_grab_callback (GtkWidget *widget,
1871 gboolean ungrab_p,
1872 gpointer client_data)
1874 if (ungrab_p) menu_grab_callback_cnt--;
1875 else menu_grab_callback_cnt++;
1877 if (menu_grab_callback_cnt > 0 && ! xg_timer) xg_start_timer ();
1878 else if (menu_grab_callback_cnt == 0 && xg_timer) xg_stop_timer ();
1881 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1882 must be non-NULL) and can be inserted into a menu item.
1884 Returns the GtkHBox. */
1886 static GtkWidget *
1887 make_widget_for_menu_item (utf8_label, utf8_key)
1888 char *utf8_label;
1889 char *utf8_key;
1891 GtkWidget *wlbl;
1892 GtkWidget *wkey;
1893 GtkWidget *wbox;
1895 wbox = gtk_hbox_new (FALSE, 0);
1896 wlbl = gtk_label_new (utf8_label);
1897 wkey = gtk_label_new (utf8_key);
1899 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1900 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1902 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1903 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1905 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1906 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1907 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1909 return wbox;
1912 /* Make and return a menu item widget with the key to the right.
1913 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1914 UTF8_KEY is the text representing the key binding.
1915 ITEM is the widget_value describing the menu item.
1917 GROUP is an in/out parameter. If the menu item to be created is not
1918 part of any radio menu group, *GROUP contains NULL on entry and exit.
1919 If the menu item to be created is part of a radio menu group, on entry
1920 *GROUP contains the group to use, or NULL if this is the first item
1921 in the group. On exit, *GROUP contains the radio item group.
1923 Unfortunately, keys don't line up as nicely as in Motif,
1924 but the MacOS X version doesn't either, so I guess that is OK. */
1926 static GtkWidget *
1927 make_menu_item (utf8_label, utf8_key, item, group)
1928 char *utf8_label;
1929 char *utf8_key;
1930 widget_value *item;
1931 GSList **group;
1933 GtkWidget *w;
1934 GtkWidget *wtoadd = 0;
1936 /* It has been observed that some menu items have a NULL name field.
1937 This will lead to this function being called with a NULL utf8_label.
1938 GTK crashes on that so we set a blank label. Why there is a NULL
1939 name remains to be investigated. */
1940 if (! utf8_label) utf8_label = " ";
1942 if (utf8_key)
1943 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1945 if (item->button_type == BUTTON_TYPE_TOGGLE)
1947 *group = NULL;
1948 if (utf8_key) w = gtk_check_menu_item_new ();
1949 else w = gtk_check_menu_item_new_with_label (utf8_label);
1950 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1952 else if (item->button_type == BUTTON_TYPE_RADIO)
1954 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1955 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1956 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1957 if (item->selected)
1958 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1960 else
1962 *group = NULL;
1963 if (utf8_key) w = gtk_menu_item_new ();
1964 else w = gtk_menu_item_new_with_label (utf8_label);
1967 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1968 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1970 return w;
1973 /* Return non-zero if LABEL specifies a separator (GTK only has one
1974 separator type) */
1976 static char* separator_names[] = {
1977 "space",
1978 "no-line",
1979 "single-line",
1980 "double-line",
1981 "single-dashed-line",
1982 "double-dashed-line",
1983 "shadow-etched-in",
1984 "shadow-etched-out",
1985 "shadow-etched-in-dash",
1986 "shadow-etched-out-dash",
1987 "shadow-double-etched-in",
1988 "shadow-double-etched-out",
1989 "shadow-double-etched-in-dash",
1990 "shadow-double-etched-out-dash",
1994 static int
1995 xg_separator_p (char *label)
1997 if (! label) return 0;
1998 else if (strlen (label) > 3
1999 && strncmp (label, "--", 2) == 0
2000 && label[2] != '-')
2002 int i;
2004 label += 2;
2005 for (i = 0; separator_names[i]; ++i)
2006 if (strcmp (label, separator_names[i]) == 0)
2007 return 1;
2009 else
2011 /* Old-style separator, maybe. It's a separator if it contains
2012 only dashes. */
2013 while (*label == '-')
2014 ++label;
2015 if (*label == 0) return 1;
2018 return 0;
2021 static int xg_detached_menus;
2023 /* Returns non-zero if there are detached menus. */
2026 xg_have_tear_offs ()
2028 return xg_detached_menus > 0;
2031 /* Callback invoked when a detached menu window is removed. Here we
2032 decrease the xg_detached_menus count.
2033 WIDGET is the top level window that is removed (the parent of the menu).
2034 CLIENT_DATA is not used. */
2036 static void
2037 tearoff_remove (widget, client_data)
2038 GtkWidget *widget;
2039 gpointer client_data;
2041 if (xg_detached_menus > 0) --xg_detached_menus;
2044 /* Callback invoked when a menu is detached. It increases the
2045 xg_detached_menus count.
2046 WIDGET is the GtkTearoffMenuItem.
2047 CLIENT_DATA is not used. */
2049 static void
2050 tearoff_activate (widget, client_data)
2051 GtkWidget *widget;
2052 gpointer client_data;
2054 GtkWidget *menu = gtk_widget_get_parent (widget);
2055 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
2057 ++xg_detached_menus;
2058 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
2059 "destroy",
2060 G_CALLBACK (tearoff_remove), 0);
2065 /* Create a menu item widget, and connect the callbacks.
2066 ITEM decribes the menu item.
2067 F is the frame the created menu belongs to.
2068 SELECT_CB is the callback to use when a menu item is selected.
2069 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2070 CL_DATA points to the callback data to be used for this menu.
2071 GROUP is an in/out parameter. If the menu item to be created is not
2072 part of any radio menu group, *GROUP contains NULL on entry and exit.
2073 If the menu item to be created is part of a radio menu group, on entry
2074 *GROUP contains the group to use, or NULL if this is the first item
2075 in the group. On exit, *GROUP contains the radio item group.
2077 Returns the created GtkWidget. */
2079 static GtkWidget *
2080 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
2081 widget_value *item;
2082 FRAME_PTR f;
2083 GCallback select_cb;
2084 GCallback highlight_cb;
2085 xg_menu_cb_data *cl_data;
2086 GSList **group;
2088 char *utf8_label;
2089 char *utf8_key;
2090 GtkWidget *w;
2091 xg_menu_item_cb_data *cb_data;
2093 utf8_label = get_utf8_string (item->name);
2094 utf8_key = get_utf8_string (item->key);
2096 w = make_menu_item (utf8_label, utf8_key, item, group);
2098 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2099 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
2101 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
2103 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2105 cb_data->select_id = 0;
2106 cb_data->help = item->help;
2107 cb_data->cl_data = cl_data;
2108 cb_data->call_data = item->call_data;
2110 g_signal_connect (G_OBJECT (w),
2111 "destroy",
2112 G_CALLBACK (menuitem_destroy_callback),
2113 cb_data);
2115 /* Put cb_data in widget, so we can get at it when modifying menubar */
2116 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2118 /* final item, not a submenu */
2119 if (item->call_data && ! item->contents)
2121 if (select_cb)
2122 cb_data->select_id
2123 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2126 return w;
2129 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
2130 GCallback, GCallback, int, int, int,
2131 GtkWidget *, xg_menu_cb_data *, char *));
2133 /* Create a full menu tree specified by DATA.
2134 F is the frame the created menu belongs to.
2135 SELECT_CB is the callback to use when a menu item is selected.
2136 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2137 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2138 POP_UP_P is non-zero if we shall create a popup menu.
2139 MENU_BAR_P is non-zero if we shall create a menu bar.
2140 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2141 if MENU_BAR_P is non-zero.
2142 TOPMENU is the topmost GtkWidget that others shall be placed under.
2143 It may be NULL, in that case we create the appropriate widget
2144 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2145 CL_DATA is the callback data we shall use for this menu, or NULL
2146 if we haven't set the first callback yet.
2147 NAME is the name to give to the top level menu if this function
2148 creates it. May be NULL to not set any name.
2150 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2151 not NULL.
2153 This function calls itself to create submenus. */
2155 static GtkWidget *
2156 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
2157 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
2158 widget_value *data;
2159 FRAME_PTR f;
2160 GCallback select_cb;
2161 GCallback deactivate_cb;
2162 GCallback highlight_cb;
2163 int pop_up_p;
2164 int menu_bar_p;
2165 int add_tearoff_p;
2166 GtkWidget *topmenu;
2167 xg_menu_cb_data *cl_data;
2168 char *name;
2170 widget_value *item;
2171 GtkWidget *wmenu = topmenu;
2172 GSList *group = NULL;
2174 if (! topmenu)
2176 if (! menu_bar_p)
2178 wmenu = gtk_menu_new ();
2179 xg_set_screen (wmenu, f);
2180 /* Connect this to the menu instead of items so we get enter/leave for
2181 disabled items also. TODO: Still does not get enter/leave for
2182 disabled items in detached menus. */
2183 g_signal_connect (G_OBJECT (wmenu),
2184 "enter-notify-event",
2185 G_CALLBACK (menuitem_highlight_callback),
2186 NULL);
2187 g_signal_connect (G_OBJECT (wmenu),
2188 "leave-notify-event",
2189 G_CALLBACK (menuitem_highlight_callback),
2190 NULL);
2192 else wmenu = gtk_menu_bar_new ();
2194 /* Put cl_data on the top menu for easier access. */
2195 cl_data = make_cl_data (cl_data, f, highlight_cb);
2196 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2197 g_signal_connect (G_OBJECT (wmenu), "destroy",
2198 G_CALLBACK (menu_destroy_callback), cl_data);
2200 if (name)
2201 gtk_widget_set_name (wmenu, name);
2203 if (deactivate_cb)
2204 g_signal_connect (G_OBJECT (wmenu),
2205 "selection-done", deactivate_cb, 0);
2207 g_signal_connect (G_OBJECT (wmenu),
2208 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
2211 if (! menu_bar_p && add_tearoff_p)
2213 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2214 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2216 g_signal_connect (G_OBJECT (tearoff), "activate",
2217 G_CALLBACK (tearoff_activate), 0);
2220 for (item = data; item; item = item->next)
2222 GtkWidget *w;
2224 if (pop_up_p && !item->contents && !item->call_data
2225 && !xg_separator_p (item->name))
2227 char *utf8_label;
2228 /* A title for a popup. We do the same as GTK does when
2229 creating titles, but it does not look good. */
2230 group = NULL;
2231 utf8_label = get_utf8_string (item->name);
2233 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2234 w = gtk_menu_item_new_with_label (utf8_label);
2235 gtk_widget_set_sensitive (w, FALSE);
2236 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2238 else if (xg_separator_p (item->name))
2240 group = NULL;
2241 /* GTK only have one separator type. */
2242 w = gtk_separator_menu_item_new ();
2244 else
2246 w = xg_create_one_menuitem (item,
2248 item->contents ? 0 : select_cb,
2249 highlight_cb,
2250 cl_data,
2251 &group);
2253 /* Create a possibly empty submenu for menu bar items, since some
2254 themes don't highlight items correctly without it. */
2255 if (item->contents || menu_bar_p)
2257 GtkWidget *submenu = create_menus (item->contents,
2259 select_cb,
2260 deactivate_cb,
2261 highlight_cb,
2264 add_tearoff_p,
2266 cl_data,
2268 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2272 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2273 gtk_widget_set_name (w, MENU_ITEM_NAME);
2276 return wmenu;
2279 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2280 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2281 with some text and buttons.
2282 F is the frame the created item belongs to.
2283 NAME is the name to use for the top widget.
2284 VAL is a widget_value structure describing items to be created.
2285 SELECT_CB is the callback to use when a menu item is selected or
2286 a dialog button is pressed.
2287 DEACTIVATE_CB is the callback to use when an item is deactivated.
2288 For a menu, when a sub menu is not shown anymore, for a dialog it is
2289 called when the dialog is popped down.
2290 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2292 Returns the widget created. */
2294 GtkWidget *
2295 xg_create_widget (type, name, f, val,
2296 select_cb, deactivate_cb, highlight_cb)
2297 char *type;
2298 char *name;
2299 FRAME_PTR f;
2300 widget_value *val;
2301 GCallback select_cb;
2302 GCallback deactivate_cb;
2303 GCallback highlight_cb;
2305 GtkWidget *w = 0;
2306 int menu_bar_p = strcmp (type, "menubar") == 0;
2307 int pop_up_p = strcmp (type, "popup") == 0;
2309 if (strcmp (type, "dialog") == 0)
2311 w = create_dialog (val, select_cb, deactivate_cb);
2312 xg_set_screen (w, f);
2313 gtk_window_set_transient_for (GTK_WINDOW (w),
2314 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2315 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2316 gtk_widget_set_name (w, "emacs-dialog");
2317 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2319 else if (menu_bar_p || pop_up_p)
2321 w = create_menus (val->contents,
2323 select_cb,
2324 deactivate_cb,
2325 highlight_cb,
2326 pop_up_p,
2327 menu_bar_p,
2328 menu_bar_p,
2331 name);
2333 /* Set the cursor to an arrow for popup menus when they are mapped.
2334 This is done by default for menu bar menus. */
2335 if (pop_up_p)
2337 /* Must realize so the GdkWindow inside the widget is created. */
2338 gtk_widget_realize (w);
2339 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2342 else
2344 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2345 type);
2348 return w;
2351 /* Return the label for menu item WITEM. */
2353 static const char *
2354 xg_get_menu_item_label (witem)
2355 GtkMenuItem *witem;
2357 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2358 return gtk_label_get_label (wlabel);
2361 /* Return non-zero if the menu item WITEM has the text LABEL. */
2363 static int
2364 xg_item_label_same_p (witem, label)
2365 GtkMenuItem *witem;
2366 char *label;
2368 int is_same = 0;
2369 char *utf8_label = get_utf8_string (label);
2370 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2372 if (! old_label && ! utf8_label)
2373 is_same = 1;
2374 else if (old_label && utf8_label)
2375 is_same = strcmp (utf8_label, old_label) == 0;
2377 if (utf8_label && utf8_label != label) g_free (utf8_label);
2379 return is_same;
2382 /* Destroy widgets in LIST. */
2384 static void
2385 xg_destroy_widgets (list)
2386 GList *list;
2388 GList *iter;
2390 for (iter = list; iter; iter = g_list_next (iter))
2392 GtkWidget *w = GTK_WIDGET (iter->data);
2394 /* Destroying the widget will remove it from the container it is in. */
2395 gtk_widget_destroy (w);
2399 /* Update the top level names in MENUBAR (i.e. not submenus).
2400 F is the frame the menu bar belongs to.
2401 *LIST is a list with the current menu bar names (menu item widgets).
2402 ITER is the item within *LIST that shall be updated.
2403 POS is the numerical position, starting at 0, of ITER in *LIST.
2404 VAL describes what the menu bar shall look like after the update.
2405 SELECT_CB is the callback to use when a menu item is selected.
2406 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2407 CL_DATA points to the callback data to be used for this menu bar.
2409 This function calls itself to walk through the menu bar names. */
2411 static void
2412 xg_update_menubar (menubar, f, list, iter, pos, val,
2413 select_cb, deactivate_cb, highlight_cb, cl_data)
2414 GtkWidget *menubar;
2415 FRAME_PTR f;
2416 GList **list;
2417 GList *iter;
2418 int pos;
2419 widget_value *val;
2420 GCallback select_cb;
2421 GCallback deactivate_cb;
2422 GCallback highlight_cb;
2423 xg_menu_cb_data *cl_data;
2425 if (! iter && ! val)
2426 return;
2427 else if (iter && ! val)
2429 /* Item(s) have been removed. Remove all remaining items. */
2430 xg_destroy_widgets (iter);
2432 /* All updated. */
2433 val = 0;
2434 iter = 0;
2436 else if (! iter && val)
2438 /* Item(s) added. Add all new items in one call. */
2439 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2440 0, 1, 0, menubar, cl_data, 0);
2442 /* All updated. */
2443 val = 0;
2444 iter = 0;
2446 /* Below this neither iter or val is NULL */
2447 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2449 /* This item is still the same, check next item. */
2450 val = val->next;
2451 iter = g_list_next (iter);
2452 ++pos;
2454 else /* This item is changed. */
2456 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2457 GtkMenuItem *witem2 = 0;
2458 int val_in_menubar = 0;
2459 int iter_in_new_menubar = 0;
2460 GList *iter2;
2461 widget_value *cur;
2463 /* See if the changed entry (val) is present later in the menu bar */
2464 for (iter2 = iter;
2465 iter2 && ! val_in_menubar;
2466 iter2 = g_list_next (iter2))
2468 witem2 = GTK_MENU_ITEM (iter2->data);
2469 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2472 /* See if the current entry (iter) is present later in the
2473 specification for the new menu bar. */
2474 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2475 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2477 if (val_in_menubar && ! iter_in_new_menubar)
2479 int nr = pos;
2481 /* This corresponds to:
2482 Current: A B C
2483 New: A C
2484 Remove B. */
2486 gtk_widget_ref (GTK_WIDGET (witem));
2487 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2488 gtk_widget_destroy (GTK_WIDGET (witem));
2490 /* Must get new list since the old changed. */
2491 g_list_free (*list);
2492 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2493 while (nr-- > 0) iter = g_list_next (iter);
2495 else if (! val_in_menubar && ! iter_in_new_menubar)
2497 /* This corresponds to:
2498 Current: A B C
2499 New: A X C
2500 Rename B to X. This might seem to be a strange thing to do,
2501 since if there is a menu under B it will be totally wrong for X.
2502 But consider editing a C file. Then there is a C-mode menu
2503 (corresponds to B above).
2504 If then doing C-x C-f the minibuf menu (X above) replaces the
2505 C-mode menu. When returning from the minibuffer, we get
2506 back the C-mode menu. Thus we do:
2507 Rename B to X (C-mode to minibuf menu)
2508 Rename X to B (minibuf to C-mode menu).
2509 If the X menu hasn't been invoked, the menu under B
2510 is up to date when leaving the minibuffer. */
2511 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2512 char *utf8_label = get_utf8_string (val->name);
2513 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2515 gtk_label_set_text (wlabel, utf8_label);
2517 /* If this item has a submenu that has been detached, change
2518 the title in the WM decorations also. */
2519 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2520 /* Set the title of the detached window. */
2521 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2523 iter = g_list_next (iter);
2524 val = val->next;
2525 ++pos;
2527 else if (! val_in_menubar && iter_in_new_menubar)
2529 /* This corresponds to:
2530 Current: A B C
2531 New: A X B C
2532 Insert X. */
2534 int nr = pos;
2535 GList *group = 0;
2536 GtkWidget *w = xg_create_one_menuitem (val,
2538 select_cb,
2539 highlight_cb,
2540 cl_data,
2541 &group);
2543 /* Create a possibly empty submenu for menu bar items, since some
2544 themes don't highlight items correctly without it. */
2545 GtkWidget *submenu = create_menus (NULL, f,
2546 select_cb, deactivate_cb,
2547 highlight_cb,
2548 0, 0, 0, 0, cl_data, 0);
2549 gtk_widget_set_name (w, MENU_ITEM_NAME);
2550 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2551 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2553 g_list_free (*list);
2554 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2555 while (nr-- > 0) iter = g_list_next (iter);
2556 iter = g_list_next (iter);
2557 val = val->next;
2558 ++pos;
2560 else /* if (val_in_menubar && iter_in_new_menubar) */
2562 int nr = pos;
2563 /* This corresponds to:
2564 Current: A B C
2565 New: A C B
2566 Move C before B */
2568 gtk_widget_ref (GTK_WIDGET (witem2));
2569 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2570 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2571 GTK_WIDGET (witem2), pos);
2572 gtk_widget_unref (GTK_WIDGET (witem2));
2574 g_list_free (*list);
2575 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2576 while (nr-- > 0) iter = g_list_next (iter);
2577 if (iter) iter = g_list_next (iter);
2578 val = val->next;
2579 ++pos;
2583 /* Update the rest of the menu bar. */
2584 xg_update_menubar (menubar, f, list, iter, pos, val,
2585 select_cb, deactivate_cb, highlight_cb, cl_data);
2588 /* Update the menu item W so it corresponds to VAL.
2589 SELECT_CB is the callback to use when a menu item is selected.
2590 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2591 CL_DATA is the data to set in the widget for menu invokation. */
2593 static void
2594 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2595 widget_value *val;
2596 GtkWidget *w;
2597 GCallback select_cb;
2598 GCallback highlight_cb;
2599 xg_menu_cb_data *cl_data;
2601 GtkWidget *wchild;
2602 GtkLabel *wlbl = 0;
2603 GtkLabel *wkey = 0;
2604 char *utf8_label;
2605 char *utf8_key;
2606 const char *old_label = 0;
2607 const char *old_key = 0;
2608 xg_menu_item_cb_data *cb_data;
2610 wchild = gtk_bin_get_child (GTK_BIN (w));
2611 utf8_label = get_utf8_string (val->name);
2612 utf8_key = get_utf8_string (val->key);
2614 /* See if W is a menu item with a key. See make_menu_item above. */
2615 if (GTK_IS_HBOX (wchild))
2617 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2619 wlbl = GTK_LABEL (list->data);
2620 wkey = GTK_LABEL (list->next->data);
2621 g_list_free (list);
2623 if (! utf8_key)
2625 /* Remove the key and keep just the label. */
2626 gtk_widget_ref (GTK_WIDGET (wlbl));
2627 gtk_container_remove (GTK_CONTAINER (w), wchild);
2628 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2629 wkey = 0;
2633 else /* Just a label. */
2635 wlbl = GTK_LABEL (wchild);
2637 /* Check if there is now a key. */
2638 if (utf8_key)
2640 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2641 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2643 wlbl = GTK_LABEL (list->data);
2644 wkey = GTK_LABEL (list->next->data);
2645 g_list_free (list);
2647 gtk_container_remove (GTK_CONTAINER (w), wchild);
2648 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2653 if (wkey) old_key = gtk_label_get_label (wkey);
2654 if (wlbl) old_label = gtk_label_get_label (wlbl);
2656 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2657 gtk_label_set_text (wkey, utf8_key);
2659 if (! old_label || strcmp (utf8_label, old_label) != 0)
2660 gtk_label_set_text (wlbl, utf8_label);
2662 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2663 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2665 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2666 gtk_widget_set_sensitive (w, FALSE);
2667 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2668 gtk_widget_set_sensitive (w, TRUE);
2670 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2671 XG_ITEM_DATA);
2672 if (cb_data)
2674 cb_data->call_data = val->call_data;
2675 cb_data->help = val->help;
2676 cb_data->cl_data = cl_data;
2678 /* We assume the callback functions don't change. */
2679 if (val->call_data && ! val->contents)
2681 /* This item shall have a select callback. */
2682 if (! cb_data->select_id)
2683 cb_data->select_id
2684 = g_signal_connect (G_OBJECT (w), "activate",
2685 select_cb, cb_data);
2687 else if (cb_data->select_id)
2689 g_signal_handler_disconnect (w, cb_data->select_id);
2690 cb_data->select_id = 0;
2695 /* Update the toggle menu item W so it corresponds to VAL. */
2697 static void
2698 xg_update_toggle_item (val, w)
2699 widget_value *val;
2700 GtkWidget *w;
2702 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2705 /* Update the radio menu item W so it corresponds to VAL. */
2707 static void
2708 xg_update_radio_item (val, w)
2709 widget_value *val;
2710 GtkWidget *w;
2712 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2715 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2716 SUBMENU may be NULL, in that case a new menu is created.
2717 F is the frame the menu bar belongs to.
2718 VAL describes the contents of the menu bar.
2719 SELECT_CB is the callback to use when a menu item is selected.
2720 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2721 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2722 CL_DATA is the call back data to use for any newly created items.
2724 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2725 was NULL. */
2727 static GtkWidget *
2728 xg_update_submenu (submenu, f, val,
2729 select_cb, deactivate_cb, highlight_cb, cl_data)
2730 GtkWidget *submenu;
2731 FRAME_PTR f;
2732 widget_value *val;
2733 GCallback select_cb;
2734 GCallback deactivate_cb;
2735 GCallback highlight_cb;
2736 xg_menu_cb_data *cl_data;
2738 GtkWidget *newsub = submenu;
2739 GList *list = 0;
2740 GList *iter;
2741 widget_value *cur;
2742 int has_tearoff_p = 0;
2743 GList *first_radio = 0;
2745 if (submenu)
2746 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2748 for (cur = val, iter = list;
2749 cur && iter;
2750 iter = g_list_next (iter), cur = cur->next)
2752 GtkWidget *w = GTK_WIDGET (iter->data);
2754 /* Skip tearoff items, they have no counterpart in val. */
2755 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2757 has_tearoff_p = 1;
2758 iter = g_list_next (iter);
2759 if (iter) w = GTK_WIDGET (iter->data);
2760 else break;
2763 /* Remember first radio button in a group. If we get a mismatch in
2764 a radio group we must rebuild the whole group so that the connections
2765 in GTK becomes correct. */
2766 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2767 first_radio = iter;
2768 else if (cur->button_type != BUTTON_TYPE_RADIO
2769 && ! GTK_IS_RADIO_MENU_ITEM (w))
2770 first_radio = 0;
2772 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2774 if (! xg_separator_p (cur->name))
2775 break;
2777 else if (GTK_IS_CHECK_MENU_ITEM (w))
2779 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2780 break;
2781 xg_update_toggle_item (cur, w);
2782 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2784 else if (GTK_IS_RADIO_MENU_ITEM (w))
2786 if (cur->button_type != BUTTON_TYPE_RADIO)
2787 break;
2788 xg_update_radio_item (cur, w);
2789 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2791 else if (GTK_IS_MENU_ITEM (w))
2793 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2794 GtkWidget *sub;
2796 if (cur->button_type != BUTTON_TYPE_NONE ||
2797 xg_separator_p (cur->name))
2798 break;
2800 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2802 sub = gtk_menu_item_get_submenu (witem);
2803 if (sub && ! cur->contents)
2805 /* Not a submenu anymore. */
2806 gtk_widget_ref (sub);
2807 gtk_menu_item_remove_submenu (witem);
2808 gtk_widget_destroy (sub);
2810 else if (cur->contents)
2812 GtkWidget *nsub;
2814 nsub = xg_update_submenu (sub, f, cur->contents,
2815 select_cb, deactivate_cb,
2816 highlight_cb, cl_data);
2818 /* If this item just became a submenu, we must set it. */
2819 if (nsub != sub)
2820 gtk_menu_item_set_submenu (witem, nsub);
2823 else
2825 /* Structural difference. Remove everything from here and down
2826 in SUBMENU. */
2827 break;
2831 /* Remove widgets from first structual change. */
2832 if (iter)
2834 /* If we are adding new menu items below, we must remove from
2835 first radio button so that radio groups become correct. */
2836 if (cur && first_radio) xg_destroy_widgets (first_radio);
2837 else xg_destroy_widgets (iter);
2840 if (cur)
2842 /* More items added. Create them. */
2843 newsub = create_menus (cur,
2845 select_cb,
2846 deactivate_cb,
2847 highlight_cb,
2850 ! has_tearoff_p,
2851 submenu,
2852 cl_data,
2856 if (list) g_list_free (list);
2858 return newsub;
2861 /* Update the MENUBAR.
2862 F is the frame the menu bar belongs to.
2863 VAL describes the contents of the menu bar.
2864 If DEEP_P is non-zero, rebuild all but the top level menu names in
2865 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2866 SELECT_CB is the callback to use when a menu item is selected.
2867 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2868 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2870 void
2871 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2872 select_cb, deactivate_cb, highlight_cb)
2873 GtkWidget *menubar;
2874 FRAME_PTR f;
2875 widget_value *val;
2876 int deep_p;
2877 GCallback select_cb;
2878 GCallback deactivate_cb;
2879 GCallback highlight_cb;
2881 xg_menu_cb_data *cl_data;
2882 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2884 if (! list) return;
2886 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2887 XG_FRAME_DATA);
2889 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2890 select_cb, deactivate_cb, highlight_cb, cl_data);
2892 if (deep_p)
2894 widget_value *cur;
2896 /* Update all sub menus.
2897 We must keep the submenus (GTK menu item widgets) since the
2898 X Window in the XEvent that activates the menu are those widgets. */
2900 /* Update cl_data, menu_item things in F may have changed. */
2901 update_cl_data (cl_data, f, highlight_cb);
2903 for (cur = val->contents; cur; cur = cur->next)
2905 GList *iter;
2906 GtkWidget *sub = 0;
2907 GtkWidget *newsub;
2908 GtkMenuItem *witem;
2910 /* Find sub menu that corresponds to val and update it. */
2911 for (iter = list ; iter; iter = g_list_next (iter))
2913 witem = GTK_MENU_ITEM (iter->data);
2914 if (xg_item_label_same_p (witem, cur->name))
2916 sub = gtk_menu_item_get_submenu (witem);
2917 break;
2921 newsub = xg_update_submenu (sub,
2923 cur->contents,
2924 select_cb,
2925 deactivate_cb,
2926 highlight_cb,
2927 cl_data);
2928 /* sub may still be NULL. If we just updated non deep and added
2929 a new menu bar item, it has no sub menu yet. So we set the
2930 newly created sub menu under witem. */
2931 if (newsub != sub)
2933 xg_set_screen (newsub, f);
2934 gtk_menu_item_set_submenu (witem, newsub);
2939 g_list_free (list);
2940 gtk_widget_show_all (menubar);
2943 /* Recompute all the widgets of frame F, when the menu bar has been
2944 changed. Value is non-zero if widgets were updated. */
2947 xg_update_frame_menubar (f)
2948 FRAME_PTR f;
2950 struct x_output *x = f->output_data.x;
2951 GtkRequisition req;
2953 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2954 return 0;
2956 BLOCK_INPUT;
2958 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2959 FALSE, FALSE, 0);
2960 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2962 gtk_widget_show_all (x->menubar_widget);
2963 gtk_widget_size_request (x->menubar_widget, &req);
2965 FRAME_MENUBAR_HEIGHT (f) = req.height;
2967 /* The height has changed, resize outer widget and set columns
2968 rows to what we had before adding the menu bar. */
2969 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
2971 SET_FRAME_GARBAGED (f);
2972 UNBLOCK_INPUT;
2974 return 1;
2977 /* Get rid of the menu bar of frame F, and free its storage.
2978 This is used when deleting a frame, and when turning off the menu bar. */
2980 void
2981 free_frame_menubar (f)
2982 FRAME_PTR f;
2984 struct x_output *x = f->output_data.x;
2986 if (x->menubar_widget)
2988 BLOCK_INPUT;
2990 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2991 /* The menubar and its children shall be deleted when removed from
2992 the container. */
2993 x->menubar_widget = 0;
2994 FRAME_MENUBAR_HEIGHT (f) = 0;
2996 /* The height has changed, resize outer widget and set columns
2997 rows to what we had before removing the menu bar. */
2998 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
3000 SET_FRAME_GARBAGED (f);
3001 UNBLOCK_INPUT;
3007 /***********************************************************************
3008 Scroll bar functions
3009 ***********************************************************************/
3012 /* Setting scroll bar values invokes the callback. Use this variable
3013 to indicate that callback should do nothing. */
3015 int xg_ignore_gtk_scrollbar;
3017 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3018 may be larger than 32 bits. Keep a mapping from integer index to widget
3019 pointers to get around the 32 bit limitation. */
3021 static struct
3023 GtkWidget **widgets;
3024 int max_size;
3025 int used;
3026 } id_to_widget;
3028 /* Grow this much every time we need to allocate more */
3030 #define ID_TO_WIDGET_INCR 32
3032 /* Store the widget pointer W in id_to_widget and return the integer index. */
3034 static int
3035 xg_store_widget_in_map (w)
3036 GtkWidget *w;
3038 int i;
3040 if (id_to_widget.max_size == id_to_widget.used)
3042 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3044 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
3045 sizeof (GtkWidget *)*new_size);
3047 for (i = id_to_widget.max_size; i < new_size; ++i)
3048 id_to_widget.widgets[i] = 0;
3049 id_to_widget.max_size = new_size;
3052 /* Just loop over the array and find a free place. After all,
3053 how many scroll bars are we creating? Should be a small number.
3054 The check above guarantees we will find a free place. */
3055 for (i = 0; i < id_to_widget.max_size; ++i)
3057 if (! id_to_widget.widgets[i])
3059 id_to_widget.widgets[i] = w;
3060 ++id_to_widget.used;
3062 return i;
3066 /* Should never end up here */
3067 abort ();
3070 /* Remove pointer at IDX from id_to_widget.
3071 Called when scroll bar is destroyed. */
3073 static void
3074 xg_remove_widget_from_map (idx)
3075 int idx;
3077 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3079 id_to_widget.widgets[idx] = 0;
3080 --id_to_widget.used;
3084 /* Get the widget pointer at IDX from id_to_widget. */
3086 static GtkWidget *
3087 xg_get_widget_from_map (idx)
3088 int idx;
3090 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3091 return id_to_widget.widgets[idx];
3093 return 0;
3096 /* Return the scrollbar id for X Window WID on display DPY.
3097 Return -1 if WID not in id_to_widget. */
3100 xg_get_scroll_id_for_window (dpy, wid)
3101 Display *dpy;
3102 Window wid;
3104 int idx;
3105 GtkWidget *w;
3107 w = xg_win_to_widget (dpy, wid);
3109 if (w)
3111 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3112 if (id_to_widget.widgets[idx] == w)
3113 return idx;
3116 return -1;
3119 /* Callback invoked when scroll bar WIDGET is destroyed.
3120 DATA is the index into id_to_widget for WIDGET.
3121 We free pointer to last scroll bar values here and remove the index. */
3123 static void
3124 xg_gtk_scroll_destroy (widget, data)
3125 GtkWidget *widget;
3126 gpointer data;
3128 gpointer p;
3129 int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
3131 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
3132 xfree (p);
3133 xg_remove_widget_from_map (id);
3136 /* Callback for button press/release events. Used to start timer so that
3137 the scroll bar repetition timer in GTK gets handeled.
3138 Also, sets bar->dragging to Qnil when dragging (button release) is done.
3139 WIDGET is the scroll bar widget the event is for (not used).
3140 EVENT contains the event.
3141 USER_DATA points to the struct scrollbar structure.
3143 Returns FALSE to tell GTK that it shall continue propagate the event
3144 to widgets. */
3146 static gboolean
3147 scroll_bar_button_cb (widget, event, user_data)
3148 GtkWidget *widget;
3149 GdkEventButton *event;
3150 gpointer user_data;
3152 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
3153 xg_start_timer ();
3154 else if (event->type == GDK_BUTTON_RELEASE)
3156 struct scroll_bar *bar = (struct scroll_bar *) user_data;
3157 if (xg_timer) xg_stop_timer ();
3158 bar->dragging = Qnil;
3161 return FALSE;
3164 /* Create a scroll bar widget for frame F. Store the scroll bar
3165 in BAR.
3166 SCROLL_CALLBACK is the callback to invoke when the value of the
3167 bar changes.
3168 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3169 to set resources for the widget. */
3171 void
3172 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
3173 FRAME_PTR f;
3174 struct scroll_bar *bar;
3175 GCallback scroll_callback;
3176 char *scroll_bar_name;
3178 GtkWidget *wscroll;
3179 GtkWidget *webox;
3180 GtkObject *vadj;
3181 int scroll_id;
3183 /* Page, step increment values are not so important here, they
3184 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3185 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3186 0.1, 0.1, 0.1);
3188 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
3189 webox = gtk_event_box_new ();
3190 gtk_widget_set_name (wscroll, scroll_bar_name);
3191 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3193 scroll_id = xg_store_widget_in_map (wscroll);
3195 g_signal_connect (G_OBJECT (wscroll),
3196 "value-changed",
3197 scroll_callback,
3198 (gpointer) bar);
3199 /* The EMACS_INT cast avoids a warning. */
3200 g_signal_connect (G_OBJECT (wscroll),
3201 "destroy",
3202 G_CALLBACK (xg_gtk_scroll_destroy),
3203 (gpointer) (EMACS_INT) scroll_id);
3205 /* Connect to button press and button release to detect if any scroll bar
3206 has the pointer. */
3207 g_signal_connect (G_OBJECT (wscroll),
3208 "button-press-event",
3209 G_CALLBACK (scroll_bar_button_cb),
3210 (gpointer) bar);
3211 g_signal_connect (G_OBJECT (wscroll),
3212 "button-release-event",
3213 G_CALLBACK (scroll_bar_button_cb),
3214 (gpointer) bar);
3216 /* The scroll bar widget does not draw on a window of its own. Instead
3217 it draws on the parent window, in this case the edit widget. So
3218 whenever the edit widget is cleared, the scroll bar needs to redraw
3219 also, which causes flicker. Put an event box between the edit widget
3220 and the scroll bar, so the scroll bar instead draws itself on the
3221 event box window. */
3222 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3223 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3226 /* Set the cursor to an arrow. */
3227 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3229 bar->x_window = scroll_id;
3232 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
3234 void
3235 xg_show_scroll_bar (scrollbar_id)
3236 int scrollbar_id;
3238 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3239 if (w)
3240 gtk_widget_show_all (gtk_widget_get_parent (w));
3243 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3245 void
3246 xg_remove_scroll_bar (f, scrollbar_id)
3247 FRAME_PTR f;
3248 int scrollbar_id;
3250 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3251 if (w)
3253 GtkWidget *wparent = gtk_widget_get_parent (w);
3254 gtk_widget_destroy (w);
3255 gtk_widget_destroy (wparent);
3256 SET_FRAME_GARBAGED (f);
3260 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3261 in frame F.
3262 TOP/LEFT are the new pixel positions where the bar shall appear.
3263 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3265 void
3266 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
3267 FRAME_PTR f;
3268 int scrollbar_id;
3269 int top;
3270 int left;
3271 int width;
3272 int height;
3275 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3277 if (wscroll)
3279 GtkWidget *wfixed = f->output_data.x->edit_widget;
3280 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3282 /* Move and resize to new values. */
3283 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3284 gtk_widget_set_size_request (wscroll, width, height);
3285 gtk_widget_queue_draw (wparent);
3286 gdk_window_process_all_updates ();
3287 /* GTK does not redraw until the main loop is entered again, but
3288 if there are no X events pending we will not enter it. So we sync
3289 here to get some events. */
3290 x_sync (f);
3291 SET_FRAME_GARBAGED (f);
3292 cancel_mouse_face (f);
3296 /* Set the thumb size and position of scroll bar BAR. We are currently
3297 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3299 void
3300 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
3301 struct scroll_bar *bar;
3302 int portion, position, whole;
3304 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3306 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3308 if (wscroll && NILP (bar->dragging))
3310 GtkAdjustment *adj;
3311 gdouble shown;
3312 gdouble top;
3313 int size, value;
3314 int new_step;
3315 int changed = 0;
3317 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3319 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3320 rather than the real portion value. This makes the thumb less likely
3321 to resize and that looks better. */
3322 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3323 /* When the thumb is at the bottom, position == whole.
3324 So we need to increase `whole' to make space for the thumb. */
3325 whole += portion;
3327 if (whole <= 0)
3328 top = 0, shown = 1;
3329 else
3331 top = (gdouble) position / whole;
3332 shown = (gdouble) portion / whole;
3335 size = shown * XG_SB_RANGE;
3336 size = min (size, XG_SB_RANGE);
3337 size = max (size, 1);
3339 value = top * XG_SB_RANGE;
3340 value = min (value, XG_SB_MAX - size);
3341 value = max (value, XG_SB_MIN);
3343 /* Assume all lines are of equal size. */
3344 new_step = size / max (1, FRAME_LINES (f));
3346 if ((int) adj->page_size != size
3347 || (int) adj->step_increment != new_step)
3349 adj->page_size = size;
3350 adj->step_increment = new_step;
3351 /* Assume a page increment is about 95% of the page size */
3352 adj->page_increment = (int) (0.95*adj->page_size);
3353 changed = 1;
3356 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3358 GtkWidget *wfixed = f->output_data.x->edit_widget;
3360 BLOCK_INPUT;
3362 /* gtk_range_set_value invokes the callback. Set
3363 ignore_gtk_scrollbar to make the callback do nothing */
3364 xg_ignore_gtk_scrollbar = 1;
3366 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3367 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3368 else if (changed)
3369 gtk_adjustment_changed (adj);
3371 xg_ignore_gtk_scrollbar = 0;
3373 UNBLOCK_INPUT;
3379 /***********************************************************************
3380 Tool bar functions
3381 ***********************************************************************/
3382 /* The key for the data we put in the GtkImage widgets. The data is
3383 the image used by Emacs. We use this to see if we need to update
3384 the GtkImage with a new image. */
3385 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3387 /* The key for storing the latest modifiers so the activate callback can
3388 get them. */
3389 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3391 /* The key for storing the button widget in its proxy menu item. */
3392 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3394 /* The key for the data we put in the GtkImage widgets. The data is
3395 the stock name used by Emacs. We use this to see if we need to update
3396 the GtkImage with a new image. */
3397 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3399 /* As above, but this is used for named theme widgets, as opposed to
3400 stock items. */
3401 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3403 /* Callback function invoked when a tool bar item is pressed.
3404 W is the button widget in the tool bar that got pressed,
3405 CLIENT_DATA is an integer that is the index of the button in the
3406 tool bar. 0 is the first button. */
3408 static gboolean
3409 xg_tool_bar_button_cb (widget, event, user_data)
3410 GtkWidget *widget;
3411 GdkEventButton *event;
3412 gpointer user_data;
3414 /* Casts to avoid warnings when gpointer is 64 bits and int is 32 bits */
3415 gpointer ptr = (gpointer) (EMACS_INT) event->state;
3416 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3417 return FALSE;
3421 /* Callback function invoked when a tool bar item is pressed.
3422 W is the button widget in the tool bar that got pressed,
3423 CLIENT_DATA is an integer that is the index of the button in the
3424 tool bar. 0 is the first button. */
3426 static void
3427 xg_tool_bar_callback (w, client_data)
3428 GtkWidget *w;
3429 gpointer client_data;
3431 /* The EMACS_INT cast avoids a warning. */
3432 int idx = (int) (EMACS_INT) client_data;
3433 int mod = (int) (EMACS_INT) g_object_get_data (G_OBJECT (w),
3434 XG_TOOL_BAR_LAST_MODIFIER);
3436 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3437 Lisp_Object key, frame;
3438 struct input_event event;
3439 EVENT_INIT (event);
3441 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3442 return;
3444 idx *= TOOL_BAR_ITEM_NSLOTS;
3446 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3447 XSETFRAME (frame, f);
3449 /* We generate two events here. The first one is to set the prefix
3450 to `(tool_bar)', see keyboard.c. */
3451 event.kind = TOOL_BAR_EVENT;
3452 event.frame_or_window = frame;
3453 event.arg = frame;
3454 kbd_buffer_store_event (&event);
3456 event.kind = TOOL_BAR_EVENT;
3457 event.frame_or_window = frame;
3458 event.arg = key;
3459 /* Convert between the modifier bits GDK uses and the modifier bits
3460 Emacs uses. This assumes GDK an X masks are the same, which they are when
3461 this is written. */
3462 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
3463 kbd_buffer_store_event (&event);
3466 /* Callback function invoked when a tool bar item is pressed in a detached
3467 tool bar or the overflow drop down menu.
3468 We just call xg_tool_bar_callback.
3469 W is the menu item widget that got pressed,
3470 CLIENT_DATA is an integer that is the index of the button in the
3471 tool bar. 0 is the first button. */
3473 static void
3474 xg_tool_bar_proxy_callback (w, client_data)
3475 GtkWidget *w;
3476 gpointer client_data;
3478 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3479 XG_TOOL_BAR_PROXY_BUTTON));
3480 xg_tool_bar_callback (wbutton, client_data);
3481 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (wbutton),
3482 XG_FRAME_DATA);
3483 /* Put focus back to the frame after we have clicked on a detached
3484 tool bar button. */
3485 Lisp_Object frame;
3486 XSETFRAME (frame, f);
3487 Fx_focus_frame (frame);
3490 /* This callback is called when a tool item should create a proxy item,
3491 such as for the overflow menu. Also called when the tool bar is detached.
3492 If we don't create a proxy menu item, the detached tool bar will be
3493 blank. */
3495 static gboolean
3496 xg_tool_bar_menu_proxy (toolitem, user_data)
3497 GtkToolItem *toolitem;
3498 gpointer user_data;
3500 GtkWidget *weventbox = gtk_bin_get_child (GTK_BIN (toolitem));
3501 GtkButton *wbutton = GTK_BUTTON (gtk_bin_get_child (GTK_BIN (weventbox)));
3502 GtkWidget *wmenuitem = gtk_image_menu_item_new ();
3503 GtkWidget *wmenuimage;
3505 if (gtk_button_get_use_stock (wbutton))
3506 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3507 GTK_ICON_SIZE_MENU);
3508 else
3510 GtkImage *wimage = GTK_IMAGE (gtk_bin_get_child (GTK_BIN (wbutton)));
3511 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3512 GtkImageType store_type = gtk_image_get_storage_type (wimage);
3514 if (store_type == GTK_IMAGE_STOCK)
3516 gchar *stock_id;
3517 gtk_image_get_stock (wimage, &stock_id, NULL);
3518 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3520 else if (store_type == GTK_IMAGE_ICON_SET)
3522 GtkIconSet *icon_set;
3523 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3524 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3525 GTK_ICON_SIZE_MENU);
3527 else if (store_type == GTK_IMAGE_PIXBUF)
3529 gint width, height;
3531 if (settings &&
3532 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3533 &width, &height))
3535 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3537 src_pixbuf = gtk_image_get_pixbuf (wimage);
3538 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3539 GDK_INTERP_BILINEAR);
3541 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3543 else
3545 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3546 abort ();
3549 else if (store_type == GTK_IMAGE_ICON_NAME)
3551 const gchar *icon_name;
3552 GtkIconSize icon_size;
3554 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3555 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3556 GTK_ICON_SIZE_MENU);
3558 else
3560 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3561 abort ();
3564 if (wmenuimage)
3565 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3567 g_signal_connect (G_OBJECT (wmenuitem),
3568 "activate",
3569 GTK_SIGNAL_FUNC (xg_tool_bar_proxy_callback),
3570 user_data);
3572 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3573 (gpointer) wbutton);
3574 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
3576 return TRUE;
3579 /* This callback is called when a tool bar is detached. We must set
3580 the height of the tool bar to zero when this happens so frame sizes
3581 are correctly calculated.
3582 WBOX is the handle box widget that enables detach/attach of the tool bar.
3583 W is the tool bar widget.
3584 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3586 static void
3587 xg_tool_bar_detach_callback (wbox, w, client_data)
3588 GtkHandleBox *wbox;
3589 GtkWidget *w;
3590 gpointer client_data;
3592 FRAME_PTR f = (FRAME_PTR) client_data;
3593 extern int x_gtk_whole_detached_tool_bar;
3595 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
3596 NULL);
3598 if (f)
3600 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3602 /* When detaching a tool bar, not everything dissapear. There are
3603 a few pixels left that are used to drop the tool bar back into
3604 place. */
3605 FRAME_TOOLBAR_HEIGHT (f) = 2;
3607 /* The height has changed, resize outer widget and set columns
3608 rows to what we had before detaching the tool bar. */
3609 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
3613 /* This callback is called when a tool bar is reattached. We must set
3614 the height of the tool bar when this happens so frame sizes
3615 are correctly calculated.
3616 WBOX is the handle box widget that enables detach/attach of the tool bar.
3617 W is the tool bar widget.
3618 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3620 static void
3621 xg_tool_bar_attach_callback (wbox, w, client_data)
3622 GtkHandleBox *wbox;
3623 GtkWidget *w;
3624 gpointer client_data;
3626 FRAME_PTR f = (FRAME_PTR) client_data;
3627 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
3629 if (f)
3631 GtkRequisition req;
3633 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3635 gtk_widget_size_request (w, &req);
3636 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3638 /* The height has changed, resize outer widget and set columns
3639 rows to what we had before attaching the tool bar. */
3640 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
3644 /* This callback is called when the mouse enters or leaves a tool bar item.
3645 It is used for displaying and hiding the help text.
3646 W is the tool bar item, a button.
3647 EVENT is either an enter event or leave event.
3648 CLIENT_DATA is an integer that is the index of the button in the
3649 tool bar. 0 is the first button.
3651 Returns FALSE to tell GTK to keep processing this event. */
3653 static gboolean
3654 xg_tool_bar_help_callback (w, event, client_data)
3655 GtkWidget *w;
3656 GdkEventCrossing *event;
3657 gpointer client_data;
3659 /* The EMACS_INT cast avoids a warning. */
3660 int idx = (int) (EMACS_INT) client_data;
3661 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3662 Lisp_Object help, frame;
3664 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3665 return FALSE;
3667 if (event->type == GDK_ENTER_NOTIFY)
3669 idx *= TOOL_BAR_ITEM_NSLOTS;
3670 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3672 if (NILP (help))
3673 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3675 else
3676 help = Qnil;
3678 XSETFRAME (frame, f);
3679 kbd_buffer_store_help_event (frame, help);
3681 return FALSE;
3685 /* This callback is called when a tool bar item shall be redrawn.
3686 It modifies the expose event so that the GtkImage widget redraws the
3687 whole image. This to overcome a bug that makes GtkImage draw the image
3688 in the wrong place when it tries to redraw just a part of the image.
3689 W is the GtkImage to be redrawn.
3690 EVENT is the expose event for W.
3691 CLIENT_DATA is unused.
3693 Returns FALSE to tell GTK to keep processing this event. */
3695 static gboolean
3696 xg_tool_bar_item_expose_callback (w, event, client_data)
3697 GtkWidget *w;
3698 GdkEventExpose *event;
3699 gpointer client_data;
3701 gint width, height;
3703 gdk_drawable_get_size (event->window, &width, &height);
3705 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3706 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3708 event->area.x = max (0, event->area.x);
3709 event->area.y = max (0, event->area.y);
3711 event->area.width = max (width, event->area.width);
3712 event->area.height = max (height, event->area.height);
3714 return FALSE;
3717 /* Attach a tool bar to frame F. */
3719 static void
3720 xg_pack_tool_bar (f)
3721 FRAME_PTR f;
3723 struct x_output *x = f->output_data.x;
3724 int vbox_pos = x->menubar_widget ? 1 : 0;
3726 x->handlebox_widget = gtk_handle_box_new ();
3727 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3728 G_CALLBACK (xg_tool_bar_detach_callback), f);
3729 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3730 G_CALLBACK (xg_tool_bar_attach_callback), f);
3732 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3733 x->toolbar_widget);
3735 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3736 FALSE, FALSE, 0);
3738 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3739 vbox_pos);
3740 gtk_widget_show_all (x->handlebox_widget);
3743 /* Create a tool bar for frame F. */
3745 static void
3746 xg_create_tool_bar (f)
3747 FRAME_PTR f;
3749 struct x_output *x = f->output_data.x;
3750 GtkRequisition req;
3752 x->toolbar_widget = gtk_toolbar_new ();
3753 x->toolbar_detached = 0;
3755 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3757 /* We only have icons, so override any user setting. We could
3758 use the caption property of the toolbar item (see update_frame_tool_bar
3759 below), but some of those strings are long, making the toolbar so
3760 long it does not fit on the screen. The GtkToolbar widget makes every
3761 item equal size, so the longest caption determine the size of every
3762 tool bar item. I think the creators of the GtkToolbar widget
3763 counted on 4 or 5 character long strings. */
3764 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3765 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3766 GTK_ORIENTATION_HORIZONTAL);
3770 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3772 /* Find the right-to-left image named by RTL in the tool bar images for F.
3773 Returns IMAGE if RTL is not found. */
3775 static Lisp_Object
3776 find_rtl_image (f, image, rtl)
3777 FRAME_PTR f;
3778 Lisp_Object image;
3779 Lisp_Object rtl;
3781 int i;
3782 Lisp_Object file, rtl_name;
3783 struct gcpro gcpro1, gcpro2;
3784 GCPRO2 (file, rtl_name);
3786 rtl_name = Ffile_name_nondirectory (rtl);
3788 for (i = 0; i < f->n_tool_bar_items; ++i)
3790 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
3791 if (!NILP (file = file_for_image (rtl_image)))
3793 file = call1 (intern ("file-name-sans-extension"),
3794 Ffile_name_nondirectory (file));
3795 if (EQ (Fequal (file, rtl_name), Qt))
3797 image = rtl_image;
3798 break;
3803 return image;
3806 /* Update the tool bar for frame F. Add new buttons and remove old. */
3808 void
3809 update_frame_tool_bar (f)
3810 FRAME_PTR f;
3812 int i;
3813 GtkRequisition old_req, new_req;
3814 struct x_output *x = f->output_data.x;
3815 int hmargin = 0, vmargin = 0;
3816 GtkToolbar *wtoolbar;
3817 GtkToolItem *ti;
3818 GtkTextDirection dir;
3819 int pack_tool_bar = x->handlebox_widget == NULL;
3821 if (! FRAME_GTK_WIDGET (f))
3822 return;
3824 BLOCK_INPUT;
3826 if (INTEGERP (Vtool_bar_button_margin)
3827 && XINT (Vtool_bar_button_margin) > 0)
3829 hmargin = XFASTINT (Vtool_bar_button_margin);
3830 vmargin = XFASTINT (Vtool_bar_button_margin);
3832 else if (CONSP (Vtool_bar_button_margin))
3834 if (INTEGERP (XCAR (Vtool_bar_button_margin))
3835 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
3836 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
3838 if (INTEGERP (XCDR (Vtool_bar_button_margin))
3839 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
3840 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
3843 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
3844 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
3845 i.e. zero. This means that margins less than
3846 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
3847 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3848 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3850 if (! x->toolbar_widget)
3851 xg_create_tool_bar (f);
3853 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
3854 gtk_widget_size_request (GTK_WIDGET (wtoolbar), &old_req);
3855 dir = gtk_widget_get_direction (x->toolbar_widget);
3857 for (i = 0; i < f->n_tool_bar_items; ++i)
3860 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3861 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3862 int idx;
3863 int img_id;
3864 int icon_size = 0;
3865 struct image *img = NULL;
3866 Lisp_Object image;
3867 Lisp_Object stock = Qnil;
3868 GtkStockItem stock_item;
3869 char *stock_name = NULL;
3870 char *icon_name = NULL;
3871 Lisp_Object rtl;
3872 GtkWidget *wbutton = NULL;
3873 GtkWidget *weventbox;
3874 Lisp_Object func = intern ("x-gtk-map-stock");
3875 Lisp_Object specified_file;
3877 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i);
3879 if (ti)
3881 weventbox = gtk_bin_get_child (GTK_BIN (ti));
3882 wbutton = gtk_bin_get_child (GTK_BIN (weventbox));
3885 image = PROP (TOOL_BAR_ITEM_IMAGES);
3887 /* Ignore invalid image specifications. */
3888 if (!valid_image_p (image))
3890 if (wbutton) gtk_widget_hide (wbutton);
3891 continue;
3894 specified_file = file_for_image (image);
3895 if (!NILP (specified_file) && EQ (Qt, Ffboundp (func)))
3896 stock = call1 (func, specified_file);
3898 if (! NILP (stock) && STRINGP (stock))
3900 stock_name = SSDATA (stock);
3901 if (stock_name[0] == 'n' && stock_name[1] == ':')
3903 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
3904 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
3906 icon_name = stock_name + 2;
3907 stock_name = NULL;
3908 stock = Qnil;
3910 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
3911 icon_name = NULL;
3912 else
3913 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
3915 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
3916 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
3917 else
3919 stock = Qnil;
3920 stock_name = NULL;
3924 if (stock_name == NULL && icon_name == NULL)
3926 /* No stock image, or stock item not known. Try regular image. */
3928 /* If image is a vector, choose the image according to the
3929 button state. */
3930 if (dir == GTK_TEXT_DIR_RTL
3931 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
3932 && STRINGP (rtl))
3934 image = find_rtl_image (f, image, rtl);
3937 if (VECTORP (image))
3939 if (enabled_p)
3940 idx = (selected_p
3941 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3942 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3943 else
3944 idx = (selected_p
3945 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3946 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3948 xassert (ASIZE (image) >= idx);
3949 image = AREF (image, idx);
3951 else
3952 idx = -1;
3954 img_id = lookup_image (f, image);
3955 img = IMAGE_FROM_ID (f, img_id);
3956 prepare_image_for_display (f, img);
3958 if (img->load_failed_p || img->pixmap == None)
3960 if (ti)
3961 gtk_widget_hide_all (GTK_WIDGET (ti));
3962 else
3964 /* Insert an empty (non-image) button */
3965 weventbox = gtk_event_box_new ();
3966 wbutton = gtk_button_new ();
3967 gtk_button_set_focus_on_click (GTK_BUTTON (wbutton), FALSE);
3968 gtk_button_set_relief (GTK_BUTTON (wbutton),
3969 GTK_RELIEF_NONE);
3970 gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
3971 ti = gtk_tool_item_new ();
3972 gtk_container_add (GTK_CONTAINER (ti), weventbox);
3973 gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, -1);
3975 continue;
3979 if (ti == NULL)
3981 GtkWidget *w;
3982 if (stock_name)
3984 w = gtk_image_new_from_stock (stock_name, icon_size);
3985 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
3986 (gpointer) xstrdup (stock_name),
3987 (GDestroyNotify) xfree);
3989 else if (icon_name)
3991 w = gtk_image_new_from_icon_name (icon_name, icon_size);
3992 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
3993 (gpointer) xstrdup (icon_name),
3994 (GDestroyNotify) xfree);
3996 else
3998 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
3999 /* Save the image so we can see if an update is needed when
4000 this function is called again. */
4001 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4002 (gpointer)img->pixmap);
4005 gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4006 wbutton = gtk_button_new ();
4007 gtk_button_set_focus_on_click (GTK_BUTTON (wbutton), FALSE);
4008 gtk_button_set_relief (GTK_BUTTON (wbutton), GTK_RELIEF_NONE);
4009 gtk_container_add (GTK_CONTAINER (wbutton), w);
4010 weventbox = gtk_event_box_new ();
4011 gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
4012 ti = gtk_tool_item_new ();
4013 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4014 gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, -1);
4017 /* The EMACS_INT cast avoids a warning. */
4018 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
4019 GTK_SIGNAL_FUNC (xg_tool_bar_menu_proxy),
4020 (gpointer) (EMACS_INT) i);
4022 g_signal_connect (G_OBJECT (wbutton), "clicked",
4023 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
4024 (gpointer) (EMACS_INT) i);
4026 gtk_widget_show_all (GTK_WIDGET (ti));
4029 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4031 /* Catch expose events to overcome an annoying redraw bug, see
4032 comment for xg_tool_bar_item_expose_callback. */
4033 g_signal_connect (G_OBJECT (ti),
4034 "expose-event",
4035 G_CALLBACK (xg_tool_bar_item_expose_callback),
4038 gtk_widget_set_sensitive (wbutton, enabled_p);
4039 gtk_tool_item_set_homogeneous (ti, FALSE);
4041 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
4042 no distinction based on modifiers in the activate callback,
4043 so we have to do it ourselves. */
4044 g_signal_connect (wbutton, "button-release-event",
4045 GTK_SIGNAL_FUNC (xg_tool_bar_button_cb),
4046 NULL);
4048 g_object_set_data (G_OBJECT (wbutton), XG_FRAME_DATA, (gpointer)f);
4050 /* Use enter/leave notify to show help. We use the events
4051 rather than the GtkButton specific signals "enter" and
4052 "leave", so we can have only one callback. The event
4053 will tell us what kind of event it is. */
4054 /* The EMACS_INT cast avoids a warning. */
4055 g_signal_connect (G_OBJECT (weventbox),
4056 "enter-notify-event",
4057 G_CALLBACK (xg_tool_bar_help_callback),
4058 (gpointer) (EMACS_INT) i);
4059 g_signal_connect (G_OBJECT (weventbox),
4060 "leave-notify-event",
4061 G_CALLBACK (xg_tool_bar_help_callback),
4062 (gpointer) (EMACS_INT) i);
4064 else
4066 GtkWidget *wimage = gtk_bin_get_child (GTK_BIN (wbutton));
4067 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
4068 XG_TOOL_BAR_IMAGE_DATA);
4069 gpointer old_stock_name = g_object_get_data (G_OBJECT (wimage),
4070 XG_TOOL_BAR_STOCK_NAME);
4071 gpointer old_icon_name = g_object_get_data (G_OBJECT (wimage),
4072 XG_TOOL_BAR_ICON_NAME);
4073 if (stock_name &&
4074 (! old_stock_name || strcmp (old_stock_name, stock_name) != 0))
4076 gtk_image_set_from_stock (GTK_IMAGE (wimage),
4077 stock_name, icon_size);
4078 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4079 (gpointer) xstrdup (stock_name),
4080 (GDestroyNotify) xfree);
4081 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4082 NULL);
4083 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME, NULL);
4085 else if (icon_name &&
4086 (! old_icon_name || strcmp (old_icon_name, icon_name) != 0))
4088 gtk_image_set_from_icon_name (GTK_IMAGE (wimage),
4089 icon_name, icon_size);
4090 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
4091 (gpointer) xstrdup (icon_name),
4092 (GDestroyNotify) xfree);
4093 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4094 NULL);
4095 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4096 NULL);
4098 else if (img && old_img != img->pixmap)
4100 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
4101 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4102 (gpointer)img->pixmap);
4104 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4105 NULL);
4106 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME, NULL);
4109 gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
4111 gtk_widget_set_sensitive (wbutton, enabled_p);
4112 gtk_widget_show_all (GTK_WIDGET (ti));
4115 #undef PROP
4118 /* Remove buttons not longer needed. We just hide them so they
4119 can be reused later on. */
4122 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i++);
4123 if (ti) gtk_widget_hide_all (GTK_WIDGET (ti));
4124 } while (ti != NULL);
4126 new_req.height = 0;
4127 gtk_widget_size_request (GTK_WIDGET (wtoolbar), &new_req);
4129 if (pack_tool_bar && f->n_tool_bar_items != 0)
4130 xg_pack_tool_bar (f);
4132 if (new_req.height != 0
4133 && f->n_tool_bar_items != 0
4134 && old_req.height != new_req.height
4135 && ! FRAME_X_OUTPUT (f)->toolbar_detached)
4137 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
4138 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
4141 UNBLOCK_INPUT;
4144 /* Deallocate all resources for the tool bar on frame F.
4145 Remove the tool bar. */
4147 void
4148 free_frame_tool_bar (f)
4149 FRAME_PTR f;
4151 struct x_output *x = f->output_data.x;
4153 if (x->toolbar_widget)
4155 int is_packed = x->handlebox_widget != 0;
4156 BLOCK_INPUT;
4157 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4158 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4159 if (is_packed)
4160 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4161 x->handlebox_widget);
4162 else
4163 gtk_widget_destroy (x->toolbar_widget);
4165 x->toolbar_widget = 0;
4166 x->handlebox_widget = 0;
4167 FRAME_TOOLBAR_HEIGHT (f) = 0;
4169 /* The height has changed, resize outer widget and set columns
4170 rows to what we had before removing the tool bar. */
4171 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
4172 UNBLOCK_INPUT;
4178 /***********************************************************************
4179 Initializing
4180 ***********************************************************************/
4181 void
4182 xg_initialize ()
4184 GtkBindingSet *binding_set;
4186 #if HAVE_XFT
4187 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4188 we keep it permanently linked in. */
4189 XftInit (0);
4190 #endif
4192 gdpy_def = NULL;
4193 xg_ignore_gtk_scrollbar = 0;
4194 xg_detached_menus = 0;
4195 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4196 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4198 id_to_widget.max_size = id_to_widget.used = 0;
4199 id_to_widget.widgets = 0;
4201 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4202 bindings. It doesn't seem to be any way to remove properties,
4203 so we set it to VoidSymbol which in X means "no key". */
4204 gtk_settings_set_string_property (gtk_settings_get_default (),
4205 "gtk-menu-bar-accel",
4206 "VoidSymbol",
4207 EMACS_CLASS);
4209 /* Make GTK text input widgets use Emacs style keybindings. This is
4210 Emacs after all. */
4211 gtk_settings_set_string_property (gtk_settings_get_default (),
4212 "gtk-key-theme-name",
4213 "Emacs",
4214 EMACS_CLASS);
4216 /* Make dialogs close on C-g. Since file dialog inherits from
4217 dialog, this works for them also. */
4218 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_DIALOG));
4219 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4220 "close", 0);
4222 /* Make menus close on C-g. */
4223 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_MENU_SHELL));
4224 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4225 "cancel", 0);
4228 #endif /* USE_GTK */
4230 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
4231 (do not change this comment) */