1 /* Functions for creating and updating GTK widgets.
3 Copyright (C) 2003-2012 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/>. */
29 #include "blockinput.h"
30 #include "syssignal.h"
33 #include "termhooks.h"
37 #include <gdk/gdkkeysyms.h>
38 #include "xsettings.h"
41 #include <X11/Xft/Xft.h>
46 #include "emacsgtkfixed.h"
49 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
50 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
52 #define FRAME_TOTAL_PIXEL_WIDTH(f) \
53 (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f))
55 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
56 #define gtk_widget_set_has_window(w, b) \
57 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
59 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
60 #define gtk_dialog_get_action_area(w) ((w)->action_area)
61 #define gtk_dialog_get_content_area(w) ((w)->vbox)
63 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
64 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
66 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
67 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
68 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
69 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
70 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
72 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
73 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
75 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
79 #ifdef USE_GTK_TOOLTIP
80 #define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
82 #define gdk_window_get_geometry(w, a, b, c, d) \
83 gdk_window_get_geometry (w, a, b, c, d, 0)
84 #define gdk_x11_window_lookup_for_display(d, w) \
85 gdk_xid_table_lookup_for_display (d, w)
86 #define gtk_box_new(ori, spacing) \
87 ((ori) == GTK_ORIENTATION_HORIZONTAL \
88 ? gtk_hbox_new (FALSE, (spacing)) : gtk_vbox_new (FALSE, (spacing)))
89 #define gtk_scrollbar_new(ori, spacing) \
90 ((ori) == GTK_ORIENTATION_HORIZONTAL \
91 ? gtk_hscrollbar_new ((spacing)) : gtk_vscrollbar_new ((spacing)))
93 #define GDK_KEY_g GDK_g
95 #endif /* HAVE_GTK3 */
97 #define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
99 static void update_theme_scrollbar_width (void);
102 /***********************************************************************
103 Display handling functions
104 ***********************************************************************/
106 /* Keep track of the default display, or NULL if there is none. Emacs
107 may close all its displays. */
109 static GdkDisplay
*gdpy_def
;
111 /* When the GTK widget W is to be created on a display for F that
112 is not the default display, set the display for W.
113 W can be a GtkMenu or a GtkWindow widget. */
116 xg_set_screen (GtkWidget
*w
, FRAME_PTR f
)
118 if (FRAME_X_DISPLAY (f
) != DEFAULT_GDK_DISPLAY ())
120 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
121 GdkScreen
*gscreen
= gdk_display_get_default_screen (gdpy
);
124 gtk_menu_set_screen (GTK_MENU (w
), gscreen
);
126 gtk_window_set_screen (GTK_WINDOW (w
), gscreen
);
131 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
132 *DPY is set to NULL if the display can't be opened.
134 Returns non-zero if display could be opened, zero if display could not
135 be opened, and less than zero if the GTK version doesn't support
136 multiple displays. */
139 xg_display_open (char *display_name
, Display
**dpy
)
143 gdpy
= gdk_display_open (display_name
);
144 if (!gdpy_def
&& gdpy
)
147 gdk_display_manager_set_default_display (gdk_display_manager_get (),
151 *dpy
= gdpy
? GDK_DISPLAY_XDISPLAY (gdpy
) : NULL
;
155 /* Close display DPY. */
158 xg_display_close (Display
*dpy
)
160 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (dpy
);
162 /* If this is the default display, try to change it before closing.
163 If there is no other display to use, gdpy_def is set to NULL, and
164 the next call to xg_display_open resets the default display. */
165 if (gdk_display_get_default () == gdpy
)
167 struct x_display_info
*dpyinfo
;
168 GdkDisplay
*gdpy_new
= NULL
;
170 /* Find another display. */
171 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
172 if (dpyinfo
->display
!= dpy
)
174 gdpy_new
= gdk_x11_lookup_xdisplay (dpyinfo
->display
);
175 gdk_display_manager_set_default_display (gdk_display_manager_get (),
182 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
183 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
184 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
185 can continue running, but there will be memory leaks. */
186 g_object_run_dispose (G_OBJECT (gdpy
));
188 /* This seems to be fixed in GTK 2.10. */
189 gdk_display_close (gdpy
);
194 /***********************************************************************
196 ***********************************************************************/
197 /* The next two variables and functions are taken from lwlib. */
198 static widget_value
*widget_value_free_list
;
199 static int malloc_cpt
;
201 /* Allocate a widget_value structure, either by taking one from the
202 widget_value_free_list or by malloc:ing a new one.
204 Return a pointer to the allocated structure. */
207 malloc_widget_value (void)
210 if (widget_value_free_list
)
212 wv
= widget_value_free_list
;
213 widget_value_free_list
= wv
->free_list
;
218 wv
= xmalloc (sizeof *wv
);
221 memset (wv
, 0, sizeof (widget_value
));
225 /* This is analogous to free. It frees only what was allocated
226 by malloc_widget_value, and no substructures. */
229 free_widget_value (widget_value
*wv
)
236 /* When the number of already allocated cells is too big,
243 wv
->free_list
= widget_value_free_list
;
244 widget_value_free_list
= wv
;
249 /* Create and return the cursor to be used for popup menus and
250 scroll bars on display DPY. */
253 xg_create_default_cursor (Display
*dpy
)
255 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (dpy
);
256 return gdk_cursor_new_for_display (gdpy
, GDK_LEFT_PTR
);
260 xg_get_pixbuf_from_pixmap (FRAME_PTR f
, Pixmap pix
)
265 unsigned int width
, height
, uunused
;
268 XGetGeometry (FRAME_X_DISPLAY (f
), pix
, &wunused
, &iunused
, &iunused
,
269 &width
, &height
, &uunused
, &uunused
);
271 xim
= XGetImage (FRAME_X_DISPLAY (f
), pix
, 0, 0, width
, height
,
275 tmp_buf
= gdk_pixbuf_new_from_data ((guchar
*) xim
->data
,
288 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
291 xg_get_pixbuf_from_pix_and_mask (FRAME_PTR f
,
296 GdkPixbuf
*icon_buf
, *tmp_buf
;
298 tmp_buf
= xg_get_pixbuf_from_pixmap (f
, pix
);
299 icon_buf
= gdk_pixbuf_add_alpha (tmp_buf
, FALSE
, 0, 0, 0);
300 g_object_unref (G_OBJECT (tmp_buf
));
302 width
= gdk_pixbuf_get_width (icon_buf
);
303 height
= gdk_pixbuf_get_height (icon_buf
);
307 GdkPixbuf
*mask_buf
= xg_get_pixbuf_from_pixmap (f
, mask
);
308 guchar
*pixels
= gdk_pixbuf_get_pixels (icon_buf
);
309 guchar
*mask_pixels
= gdk_pixbuf_get_pixels (mask_buf
);
310 int rowstride
= gdk_pixbuf_get_rowstride (icon_buf
);
311 int mask_rowstride
= gdk_pixbuf_get_rowstride (mask_buf
);
314 for (y
= 0; y
< height
; ++y
)
316 guchar
*iconptr
, *maskptr
;
319 iconptr
= pixels
+ y
* rowstride
;
320 maskptr
= mask_pixels
+ y
* mask_rowstride
;
322 for (x
= 0; x
< width
; ++x
)
324 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
325 just R is sufficient. */
327 iconptr
[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
329 iconptr
+= rowstride
/width
;
330 maskptr
+= mask_rowstride
/width
;
334 g_object_unref (G_OBJECT (mask_buf
));
341 file_for_image (Lisp_Object image
)
343 Lisp_Object specified_file
= Qnil
;
346 for (tail
= XCDR (image
);
347 NILP (specified_file
) && CONSP (tail
) && CONSP (XCDR (tail
));
348 tail
= XCDR (XCDR (tail
)))
349 if (EQ (XCAR (tail
), QCfile
))
350 specified_file
= XCAR (XCDR (tail
));
352 return specified_file
;
355 /* For the image defined in IMG, make and return a GtkImage. For displays with
356 8 planes or less we must make a GdkPixbuf and apply the mask manually.
357 Otherwise the highlighting and dimming the tool bar code in GTK does
358 will look bad. For display with more than 8 planes we just use the
359 pixmap and mask directly. For monochrome displays, GTK doesn't seem
360 able to use external pixmaps, it looks bad whatever we do.
361 The image is defined on the display where frame F is.
362 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
363 If OLD_WIDGET is NULL, a new widget is constructed and returned.
364 If OLD_WIDGET is not NULL, that widget is modified. */
367 xg_get_image_for_pixmap (FRAME_PTR f
,
370 GtkImage
*old_widget
)
374 /* If we have a file, let GTK do all the image handling.
375 This seems to be the only way to make insensitive and activated icons
376 look good in all cases. */
377 Lisp_Object specified_file
= file_for_image (img
->spec
);
380 /* We already loaded the image once before calling this
381 function, so this only fails if the image file has been removed.
382 In that case, use the pixmap already loaded. */
384 if (STRINGP (specified_file
)
385 && STRINGP (file
= x_find_image_file (specified_file
)))
388 old_widget
= GTK_IMAGE (gtk_image_new_from_file (SSDATA (file
)));
390 gtk_image_set_from_file (old_widget
, SSDATA (file
));
392 return GTK_WIDGET (old_widget
);
395 /* No file, do the image handling ourselves. This will look very bad
396 on a monochrome display, and sometimes bad on all displays with
399 /* This is a workaround to make icons look good on pseudo color
400 displays. Apparently GTK expects the images to have an alpha
401 channel. If they don't, insensitive and activated icons will
402 look bad. This workaround does not work on monochrome displays,
403 and is strictly not needed on true color/static color displays (i.e.
404 16 bits and higher). But we do it anyway so we get a pixbuf that is
405 not associated with the img->pixmap. The img->pixmap may be removed
406 by clearing the image cache and then the tool bar redraw fails, since
407 Gtk+ assumes the pixmap is always there. */
408 icon_buf
= xg_get_pixbuf_from_pix_and_mask (f
, img
->pixmap
, img
->mask
);
413 old_widget
= GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf
));
415 gtk_image_set_from_pixbuf (old_widget
, icon_buf
);
417 g_object_unref (G_OBJECT (icon_buf
));
420 return GTK_WIDGET (old_widget
);
424 /* Set CURSOR on W and all widgets W contain. We must do like this
425 for scroll bars and menu because they create widgets internally,
426 and it is those widgets that are visible. */
429 xg_set_cursor (GtkWidget
*w
, GdkCursor
*cursor
)
431 GdkWindow
*window
= gtk_widget_get_window (w
);
432 GList
*children
= gdk_window_peek_children (window
);
434 gdk_window_set_cursor (window
, cursor
);
436 /* The scroll bar widget has more than one GDK window (had to look at
437 the source to figure this out), and there is no way to set cursor
438 on widgets in GTK. So we must set the cursor for all GDK windows.
441 for ( ; children
; children
= g_list_next (children
))
442 gdk_window_set_cursor (GDK_WINDOW (children
->data
), cursor
);
445 /* Insert NODE into linked LIST. */
448 xg_list_insert (xg_list_node
*list
, xg_list_node
*node
)
450 xg_list_node
*list_start
= list
->next
;
452 if (list_start
) list_start
->prev
= node
;
453 node
->next
= list_start
;
458 /* Remove NODE from linked LIST. */
461 xg_list_remove (xg_list_node
*list
, xg_list_node
*node
)
463 xg_list_node
*list_start
= list
->next
;
464 if (node
== list_start
)
466 list
->next
= node
->next
;
467 if (list
->next
) list
->next
->prev
= 0;
471 node
->prev
->next
= node
->next
;
472 if (node
->next
) node
->next
->prev
= node
->prev
;
476 /* Allocate and return a utf8 version of STR. If STR is already
477 utf8 or NULL, just return a copy of STR.
478 A new string is allocated and the caller must free the result
482 get_utf8_string (const char *str
)
486 if (!str
) return NULL
;
488 /* If not UTF-8, try current locale. */
489 if (!g_utf8_validate (str
, -1, NULL
))
490 utf8_str
= g_locale_to_utf8 (str
, -1, 0, 0, 0);
492 return g_strdup (str
);
496 /* Probably some control characters in str. Escape them. */
498 ptrdiff_t nr_bad
= 0;
501 unsigned char *p
= (unsigned char *)str
;
505 while (! (cp
= g_locale_to_utf8 ((char *)p
, -1, &bytes_read
,
506 &bytes_written
, &err
))
507 && err
->code
== G_CONVERT_ERROR_ILLEGAL_SEQUENCE
)
510 p
+= bytes_written
+1;
523 if ((min (PTRDIFF_MAX
, SIZE_MAX
) - len
- 1) / 4 < nr_bad
)
524 memory_full (SIZE_MAX
);
525 up
= utf8_str
= xmalloc (len
+ nr_bad
* 4 + 1);
526 p
= (unsigned char *)str
;
528 while (! (cp
= g_locale_to_utf8 ((char *)p
, -1, &bytes_read
,
529 &bytes_written
, &err
))
530 && err
->code
== G_CONVERT_ERROR_ILLEGAL_SEQUENCE
)
532 strncpy (up
, (char *)p
, bytes_written
);
533 sprintf (up
+ bytes_written
, "\\%03o", p
[bytes_written
]);
534 up
+= bytes_written
+4;
535 p
+= bytes_written
+1;
542 strcat (utf8_str
, cp
);
554 /* Check for special colors used in face spec for region face.
555 The colors are fetched from the Gtk+ theme.
556 Return 1 if color was found, 0 if not. */
559 xg_check_special_colors (struct frame
*f
,
560 const char *color_name
,
564 int get_bg
= strcmp ("gtk_selection_bg_color", color_name
) == 0;
565 int get_fg
= !get_bg
&& strcmp ("gtk_selection_fg_color", color_name
) == 0;
567 if (! FRAME_GTK_WIDGET (f
) || ! (get_bg
|| get_fg
))
573 GtkStyleContext
*gsty
574 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f
));
576 char buf
[sizeof "rgbi://" + 3 * (DBL_MAX_10_EXP
+ sizeof "-1.000000" - 1)];
577 int state
= GTK_STATE_FLAG_SELECTED
|GTK_STATE_FLAG_FOCUSED
;
579 gtk_style_context_get_color (gsty
, state
, &col
);
581 gtk_style_context_get_background_color (gsty
, state
, &col
);
583 sprintf (buf
, "rgbi:%lf/%lf/%lf", col
.red
, col
.green
, col
.blue
);
584 success_p
= XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
587 GtkStyle
*gsty
= gtk_widget_get_style (FRAME_GTK_WIDGET (f
));
588 GdkColor
*grgb
= get_bg
589 ? &gsty
->bg
[GTK_STATE_SELECTED
]
590 : &gsty
->fg
[GTK_STATE_SELECTED
];
592 color
->red
= grgb
->red
;
593 color
->green
= grgb
->green
;
594 color
->blue
= grgb
->blue
;
595 color
->pixel
= grgb
->pixel
;
606 /***********************************************************************
608 ***********************************************************************/
609 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
610 We use that to pop down the tooltip. This happens if Gtk+ for some
611 reason wants to change or hide the tooltip. */
613 #ifdef USE_GTK_TOOLTIP
616 hierarchy_ch_cb (GtkWidget
*widget
,
617 GtkWidget
*previous_toplevel
,
620 FRAME_PTR f
= (FRAME_PTR
) user_data
;
621 struct x_output
*x
= f
->output_data
.x
;
622 GtkWidget
*top
= gtk_widget_get_toplevel (x
->ttip_lbl
);
624 if (! top
|| ! GTK_IS_WINDOW (top
))
625 gtk_widget_hide (previous_toplevel
);
628 /* Callback called when Gtk+ thinks a tooltip should be displayed.
629 We use it to get the tooltip window and the tooltip widget so
630 we can manipulate the ourselves.
632 Return FALSE ensures that the tooltip is not shown. */
635 qttip_cb (GtkWidget
*widget
,
638 gboolean keyboard_mode
,
642 FRAME_PTR f
= (FRAME_PTR
) user_data
;
643 struct x_output
*x
= f
->output_data
.x
;
644 if (x
->ttip_widget
== NULL
)
649 g_object_set (G_OBJECT (widget
), "has-tooltip", FALSE
, NULL
);
650 x
->ttip_widget
= tooltip
;
651 g_object_ref (G_OBJECT (tooltip
));
652 x
->ttip_lbl
= gtk_label_new ("");
653 g_object_ref (G_OBJECT (x
->ttip_lbl
));
654 gtk_tooltip_set_custom (tooltip
, x
->ttip_lbl
);
655 x
->ttip_window
= GTK_WINDOW (gtk_widget_get_toplevel (x
->ttip_lbl
));
657 /* Change stupid Gtk+ default line wrapping. */
658 p
= gtk_widget_get_parent (x
->ttip_lbl
);
659 list
= gtk_container_get_children (GTK_CONTAINER (p
));
660 for (iter
= list
; iter
; iter
= g_list_next (iter
))
662 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
663 if (GTK_IS_LABEL (w
))
664 gtk_label_set_line_wrap (GTK_LABEL (w
), FALSE
);
668 /* ATK needs an empty title for some reason. */
669 gtk_window_set_title (x
->ttip_window
, "");
670 /* Realize so we can safely get screen later on. */
671 gtk_widget_realize (GTK_WIDGET (x
->ttip_window
));
672 gtk_widget_realize (x
->ttip_lbl
);
674 g_signal_connect (x
->ttip_lbl
, "hierarchy-changed",
675 G_CALLBACK (hierarchy_ch_cb
), f
);
680 #endif /* USE_GTK_TOOLTIP */
682 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
683 Return zero if no system tooltip available, non-zero otherwise. */
686 xg_prepare_tooltip (FRAME_PTR f
,
691 #ifndef USE_GTK_TOOLTIP
694 struct x_output
*x
= f
->output_data
.x
;
698 GtkSettings
*settings
;
699 gboolean tt_enabled
= TRUE
;
701 Lisp_Object encoded_string
;
703 if (!x
->ttip_lbl
) return 0;
706 encoded_string
= ENCODE_UTF_8 (string
);
707 widget
= GTK_WIDGET (x
->ttip_lbl
);
708 gwin
= gtk_widget_get_window (GTK_WIDGET (x
->ttip_window
));
709 screen
= gdk_window_get_screen (gwin
);
710 settings
= gtk_settings_get_for_screen (screen
);
711 g_object_get (settings
, "gtk-enable-tooltips", &tt_enabled
, NULL
);
714 g_object_set (settings
, "gtk-enable-tooltips", FALSE
, NULL
);
715 /* Record that we disabled it so it can be enabled again. */
716 g_object_set_data (G_OBJECT (x
->ttip_window
), "restore-tt",
720 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
721 g_object_set_data (G_OBJECT
722 (gtk_widget_get_display (GTK_WIDGET (x
->ttip_window
))),
723 "gdk-display-current-tooltip", NULL
);
725 /* Put our dummy widget in so we can get callbacks for unrealize and
726 hierarchy-changed. */
727 gtk_tooltip_set_custom (x
->ttip_widget
, widget
);
728 gtk_tooltip_set_text (x
->ttip_widget
, SSDATA (encoded_string
));
729 gtk_widget_get_preferred_size (GTK_WIDGET (x
->ttip_window
), NULL
, &req
);
730 if (width
) *width
= req
.width
;
731 if (height
) *height
= req
.height
;
736 #endif /* USE_GTK_TOOLTIP */
739 /* Show the tooltip at ROOT_X and ROOT_Y.
740 xg_prepare_tooltip must have been called before this function. */
743 xg_show_tooltip (FRAME_PTR f
, int root_x
, int root_y
)
745 #ifdef USE_GTK_TOOLTIP
746 struct x_output
*x
= f
->output_data
.x
;
750 gtk_window_move (x
->ttip_window
, root_x
, root_y
);
751 gtk_widget_show_all (GTK_WIDGET (x
->ttip_window
));
757 /* Hide tooltip if shown. Do nothing if not shown.
758 Return non-zero if tip was hidden, non-zero if not (i.e. not using
762 xg_hide_tooltip (FRAME_PTR f
)
765 #ifdef USE_GTK_TOOLTIP
766 if (f
->output_data
.x
->ttip_window
)
768 GtkWindow
*win
= f
->output_data
.x
->ttip_window
;
770 gtk_widget_hide (GTK_WIDGET (win
));
772 if (g_object_get_data (G_OBJECT (win
), "restore-tt"))
774 GdkWindow
*gwin
= gtk_widget_get_window (GTK_WIDGET (win
));
775 GdkScreen
*screen
= gdk_window_get_screen (gwin
);
776 GtkSettings
*settings
= gtk_settings_get_for_screen (screen
);
777 g_object_set (settings
, "gtk-enable-tooltips", TRUE
, NULL
);
788 /***********************************************************************
789 General functions for creating widgets, resizing, events, e.t.c.
790 ***********************************************************************/
792 /* Make a geometry string and pass that to GTK. It seems this is the
793 only way to get geometry position right if the user explicitly
794 asked for a position when starting Emacs.
795 F is the frame we shall set geometry for. */
798 xg_set_geometry (FRAME_PTR f
)
800 if (f
->size_hint_flags
& (USPosition
| PPosition
))
802 int left
= f
->left_pos
;
803 int xneg
= f
->size_hint_flags
& XNegative
;
804 int top
= f
->top_pos
;
805 int yneg
= f
->size_hint_flags
& YNegative
;
806 char geom_str
[sizeof "=x--" + 4 * INT_STRLEN_BOUND (int)];
813 sprintf (geom_str
, "=%dx%d%c%d%c%d",
814 FRAME_PIXEL_WIDTH (f
),
815 FRAME_PIXEL_HEIGHT (f
),
816 (xneg
? '-' : '+'), left
,
817 (yneg
? '-' : '+'), top
);
819 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
821 fprintf (stderr
, "Failed to parse: '%s'\n", geom_str
);
825 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
826 and use a GtkFixed widget, this doesn't happen automatically. */
829 xg_clear_under_internal_border (FRAME_PTR f
)
831 if (FRAME_INTERNAL_BORDER_WIDTH (f
) > 0)
833 GtkWidget
*wfixed
= f
->output_data
.x
->edit_widget
;
834 gtk_widget_queue_draw (wfixed
);
835 gdk_window_process_all_updates ();
836 x_clear_area (FRAME_X_DISPLAY (f
),
839 FRAME_PIXEL_WIDTH (f
),
840 FRAME_INTERNAL_BORDER_WIDTH (f
), 0);
841 x_clear_area (FRAME_X_DISPLAY (f
),
844 FRAME_INTERNAL_BORDER_WIDTH (f
),
845 FRAME_PIXEL_HEIGHT (f
), 0);
846 x_clear_area (FRAME_X_DISPLAY (f
),
848 0, FRAME_PIXEL_HEIGHT (f
) - FRAME_INTERNAL_BORDER_WIDTH (f
),
849 FRAME_PIXEL_WIDTH (f
),
850 FRAME_INTERNAL_BORDER_WIDTH (f
), 0);
851 x_clear_area (FRAME_X_DISPLAY (f
),
853 FRAME_PIXEL_WIDTH (f
) - FRAME_INTERNAL_BORDER_WIDTH (f
),
855 FRAME_INTERNAL_BORDER_WIDTH (f
),
856 FRAME_PIXEL_HEIGHT (f
), 0);
860 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
861 and a Gtk+ menu bar, we get resize events for the edit part of the
862 frame only. We let Gtk+ deal with the Gtk+ parts.
863 F is the frame to resize.
864 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
867 xg_frame_resized (FRAME_PTR f
, int pixelwidth
, int pixelheight
)
871 if (pixelwidth
== -1 && pixelheight
== -1)
873 if (FRAME_GTK_WIDGET (f
) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f
)))
874 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f
)),
876 &pixelwidth
, &pixelheight
);
881 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f
, pixelheight
);
882 columns
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f
, pixelwidth
);
884 if (columns
!= FRAME_COLS (f
)
885 || rows
!= FRAME_LINES (f
)
886 || pixelwidth
!= FRAME_PIXEL_WIDTH (f
)
887 || pixelheight
!= FRAME_PIXEL_HEIGHT (f
))
889 FRAME_PIXEL_WIDTH (f
) = pixelwidth
;
890 FRAME_PIXEL_HEIGHT (f
) = pixelheight
;
892 xg_clear_under_internal_border (f
);
893 change_frame_size (f
, rows
, columns
, 0, 1, 0);
894 SET_FRAME_GARBAGED (f
);
895 cancel_mouse_face (f
);
899 /* Resize the outer window of frame F after changing the height.
900 COLUMNS/ROWS is the size the edit area shall have after the resize. */
903 xg_frame_set_char_size (FRAME_PTR f
, int cols
, int rows
)
905 int pixelheight
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f
, rows
)
906 + FRAME_MENUBAR_HEIGHT (f
) + FRAME_TOOLBAR_HEIGHT (f
);
909 if (FRAME_PIXEL_HEIGHT (f
) == 0)
912 /* Take into account the size of the scroll bar. Always use the
913 number of columns occupied by the scroll bar here otherwise we
914 might end up with a frame width that is not a multiple of the
915 frame's character width which is bad for vertically split
917 f
->scroll_bar_actual_width
918 = FRAME_SCROLL_BAR_COLS (f
) * FRAME_COLUMN_WIDTH (f
);
920 compute_fringe_widths (f
, 0);
922 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
923 after calculating that value. */
924 pixelwidth
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f
, cols
)
925 + FRAME_TOOLBAR_WIDTH (f
);
928 /* Do this before resize, as we don't know yet if we will be resized. */
929 xg_clear_under_internal_border (f
);
931 /* Must resize our top level widget. Font size may have changed,
932 but not rows/cols. */
933 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
934 pixelwidth
, pixelheight
);
935 x_wm_set_size_hint (f
, 0, 0);
937 SET_FRAME_GARBAGED (f
);
938 cancel_mouse_face (f
);
940 /* We can not call change_frame_size for a mapped frame,
941 we can not set pixel width/height either. The window manager may
942 override our resize request, XMonad does this all the time.
943 The best we can do is try to sync, so lisp code sees the updated
944 size as fast as possible.
945 For unmapped windows, we can set rows/cols. When
946 the frame is mapped again we will (hopefully) get the correct size. */
947 if (f
->async_visible
)
949 /* Must call this to flush out events */
950 (void)gtk_events_pending ();
952 x_wait_for_event (f
, ConfigureNotify
);
956 change_frame_size (f
, rows
, cols
, 0, 1, 0);
957 FRAME_PIXEL_WIDTH (f
) = pixelwidth
;
958 FRAME_PIXEL_HEIGHT (f
) = pixelheight
;
962 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
963 The policy is to keep the number of editable lines. */
966 xg_height_or_width_changed (FRAME_PTR f
)
968 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
969 FRAME_TOTAL_PIXEL_WIDTH (f
),
970 FRAME_TOTAL_PIXEL_HEIGHT (f
));
971 f
->output_data
.x
->hint_flags
= 0;
972 x_wm_set_size_hint (f
, 0, 0);
975 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
976 Must be done like this, because GtkWidget:s can have "hidden"
977 X Window that aren't accessible.
979 Return 0 if no widget match WDESC. */
982 xg_win_to_widget (Display
*dpy
, Window wdesc
)
985 GtkWidget
*gwdesc
= 0;
989 gdkwin
= gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy
),
994 event
.any
.window
= gdkwin
;
995 event
.any
.type
= GDK_NOTHING
;
996 gwdesc
= gtk_get_event_widget (&event
);
1003 /* Set the background of widget W to PIXEL. */
1006 xg_set_widget_bg (FRAME_PTR f
, GtkWidget
*w
, long unsigned int pixel
)
1012 if (XQueryColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
), &xbg
))
1014 bg
.red
= (double)xbg
.red
/65536.0;
1015 bg
.green
= (double)xbg
.green
/65536.0;
1016 bg
.blue
= (double)xbg
.blue
/65536.0;
1018 gtk_widget_override_background_color (w
, GTK_STATE_FLAG_NORMAL
, &bg
);
1022 GdkColormap
*map
= gtk_widget_get_colormap (w
);
1023 gdk_colormap_query_color (map
, pixel
, &bg
);
1024 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f
), GTK_STATE_NORMAL
, &bg
);
1028 /* Callback called when the gtk theme changes.
1029 We notify lisp code so it can fix faces used for region for example. */
1032 style_changed_cb (GObject
*go
,
1036 struct input_event event
;
1037 GdkDisplay
*gdpy
= (GdkDisplay
*) user_data
;
1038 const char *display_name
= gdk_display_get_name (gdpy
);
1039 Display
*dpy
= GDK_DISPLAY_XDISPLAY (gdpy
);
1042 event
.kind
= CONFIG_CHANGED_EVENT
;
1043 event
.frame_or_window
= build_string (display_name
);
1044 /* Theme doesn't change often, so intern is called seldom. */
1045 event
.arg
= intern ("theme-name");
1046 kbd_buffer_store_event (&event
);
1048 update_theme_scrollbar_width ();
1050 /* If scroll bar width changed, we need set the new size on all frames
1054 Lisp_Object rest
, frame
;
1055 FOR_EACH_FRAME (rest
, frame
)
1057 FRAME_PTR f
= XFRAME (frame
);
1058 if (FRAME_X_DISPLAY (f
) == dpy
)
1060 x_set_scroll_bar_default_width (f
);
1061 xg_frame_set_char_size (f
, FRAME_COLS (f
), FRAME_LINES (f
));
1067 /* Called when a delete-event occurs on WIDGET. */
1070 delete_cb (GtkWidget
*widget
,
1075 /* The event doesn't arrive in the normal event loop. Send event
1077 FRAME_PTR f
= (FRAME_PTR
) user_data
;
1078 struct input_event ie
;
1081 ie
.kind
= DELETE_WINDOW_EVENT
;
1082 XSETFRAME (ie
.frame_or_window
, f
);
1083 kbd_buffer_store_event (&ie
);
1089 /* Create and set up the GTK widgets for frame F.
1090 Return 0 if creation failed, non-zero otherwise. */
1093 xg_create_frame_widgets (FRAME_PTR f
)
1096 GtkWidget
*wvbox
, *whbox
;
1105 if (FRAME_X_EMBEDDED_P (f
))
1107 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
1108 wtop
= gtk_plug_new_for_display (gdpy
, f
->output_data
.x
->parent_desc
);
1111 wtop
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
1113 /* gtk_window_set_has_resize_grip is a Gtk+ 3.0 function but Ubuntu
1114 has backported it to Gtk+ 2.0 and they add the resize grip for
1115 Gtk+ 2.0 applications also. But it has a bug that makes Emacs loop
1116 forever, so disable the grip. */
1117 #if GTK_MAJOR_VERSION < 3 && defined (HAVE_GTK_WINDOW_SET_HAS_RESIZE_GRIP)
1118 gtk_window_set_has_resize_grip (GTK_WINDOW (wtop
), FALSE
);
1121 xg_set_screen (wtop
, f
);
1123 wvbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 0);
1124 whbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 0);
1125 gtk_box_set_homogeneous (GTK_BOX (wvbox
), FALSE
);
1126 gtk_box_set_homogeneous (GTK_BOX (whbox
), FALSE
);
1129 wfixed
= emacs_fixed_new (f
);
1131 wfixed
= gtk_fixed_new ();
1134 if (! wtop
|| ! wvbox
|| ! whbox
|| ! wfixed
)
1136 if (wtop
) gtk_widget_destroy (wtop
);
1137 if (wvbox
) gtk_widget_destroy (wvbox
);
1138 if (whbox
) gtk_widget_destroy (whbox
);
1139 if (wfixed
) gtk_widget_destroy (wfixed
);
1145 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1146 gtk_widget_set_name (wtop
, EMACS_CLASS
);
1147 gtk_widget_set_name (wvbox
, "pane");
1148 gtk_widget_set_name (wfixed
, SSDATA (Vx_resource_name
));
1150 /* If this frame has a title or name, set it in the title bar. */
1151 if (! NILP (f
->title
)) title
= SSDATA (ENCODE_UTF_8 (f
->title
));
1152 else if (! NILP (f
->name
)) title
= SSDATA (ENCODE_UTF_8 (f
->name
));
1154 if (title
) gtk_window_set_title (GTK_WINDOW (wtop
), title
);
1156 FRAME_GTK_OUTER_WIDGET (f
) = wtop
;
1157 FRAME_GTK_WIDGET (f
) = wfixed
;
1158 f
->output_data
.x
->vbox_widget
= wvbox
;
1159 f
->output_data
.x
->hbox_widget
= whbox
;
1161 gtk_widget_set_has_window (wfixed
, TRUE
);
1163 gtk_container_add (GTK_CONTAINER (wtop
), wvbox
);
1164 gtk_box_pack_start (GTK_BOX (wvbox
), whbox
, TRUE
, TRUE
, 0);
1165 gtk_box_pack_start (GTK_BOX (whbox
), wfixed
, TRUE
, TRUE
, 0);
1167 if (FRAME_EXTERNAL_TOOL_BAR (f
))
1168 update_frame_tool_bar (f
);
1170 /* We don't want this widget double buffered, because we draw on it
1171 with regular X drawing primitives, so from a GTK/GDK point of
1172 view, the widget is totally blank. When an expose comes, this
1173 will make the widget blank, and then Emacs redraws it. This flickers
1174 a lot, so we turn off double buffering. */
1175 gtk_widget_set_double_buffered (wfixed
, FALSE
);
1177 gtk_window_set_wmclass (GTK_WINDOW (wtop
),
1178 SSDATA (Vx_resource_name
),
1179 SSDATA (Vx_resource_class
));
1181 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1182 GTK is to destroy the widget. We want Emacs to do that instead. */
1183 g_signal_connect (G_OBJECT (wtop
), "delete-event",
1184 G_CALLBACK (delete_cb
), f
);
1186 /* Convert our geometry parameters into a geometry string
1188 GTK will itself handle calculating the real position this way. */
1189 xg_set_geometry (f
);
1191 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
1193 gtk_widget_add_events (wfixed
,
1194 GDK_POINTER_MOTION_MASK
1196 | GDK_BUTTON_PRESS_MASK
1197 | GDK_BUTTON_RELEASE_MASK
1198 | GDK_KEY_PRESS_MASK
1199 | GDK_ENTER_NOTIFY_MASK
1200 | GDK_LEAVE_NOTIFY_MASK
1201 | GDK_FOCUS_CHANGE_MASK
1202 | GDK_STRUCTURE_MASK
1203 | GDK_VISIBILITY_NOTIFY_MASK
);
1205 /* Must realize the windows so the X window gets created. It is used
1206 by callers of this function. */
1207 gtk_widget_realize (wfixed
);
1208 FRAME_X_WINDOW (f
) = GTK_WIDGET_TO_X_WIN (wfixed
);
1210 /* Since GTK clears its window by filling with the background color,
1211 we must keep X and GTK background in sync. */
1212 xg_set_widget_bg (f
, wfixed
, FRAME_BACKGROUND_PIXEL (f
));
1215 /* Also, do not let any background pixmap to be set, this looks very
1216 bad as Emacs overwrites the background pixmap with its own idea
1217 of background color. */
1218 style
= gtk_widget_get_modifier_style (wfixed
);
1220 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1221 style
->bg_pixmap_name
[GTK_STATE_NORMAL
] = g_strdup ("<none>");
1222 gtk_widget_modify_style (wfixed
, style
);
1224 gtk_widget_set_can_focus (wfixed
, TRUE
);
1225 gtk_window_set_resizable (GTK_WINDOW (wtop
), TRUE
);
1228 #ifdef USE_GTK_TOOLTIP
1229 /* Steal a tool tip window we can move ourselves. */
1230 f
->output_data
.x
->ttip_widget
= 0;
1231 f
->output_data
.x
->ttip_lbl
= 0;
1232 f
->output_data
.x
->ttip_window
= 0;
1233 gtk_widget_set_tooltip_text (wtop
, "Dummy text");
1234 g_signal_connect (wtop
, "query-tooltip", G_CALLBACK (qttip_cb
), f
);
1238 GdkScreen
*screen
= gtk_widget_get_screen (wtop
);
1239 GtkSettings
*gs
= gtk_settings_get_for_screen (screen
);
1240 /* Only connect this signal once per screen. */
1241 if (! g_signal_handler_find (G_OBJECT (gs
),
1242 G_SIGNAL_MATCH_FUNC
,
1244 G_CALLBACK (style_changed_cb
),
1247 g_signal_connect (G_OBJECT (gs
), "notify::gtk-theme-name",
1248 G_CALLBACK (style_changed_cb
),
1249 gdk_screen_get_display (screen
));
1259 xg_free_frame_widgets (FRAME_PTR f
)
1261 if (FRAME_GTK_OUTER_WIDGET (f
))
1263 #ifdef USE_GTK_TOOLTIP
1264 struct x_output
*x
= f
->output_data
.x
;
1266 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f
));
1267 FRAME_X_WINDOW (f
) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1268 FRAME_GTK_OUTER_WIDGET (f
) = 0;
1269 #ifdef USE_GTK_TOOLTIP
1271 gtk_widget_destroy (x
->ttip_lbl
);
1273 g_object_unref (G_OBJECT (x
->ttip_widget
));
1278 /* Set the normal size hints for the window manager, for frame F.
1279 FLAGS is the flags word to use--or 0 meaning preserve the flags
1280 that the window now has.
1281 If USER_POSITION is nonzero, we set the User Position
1282 flag (this is useful when FLAGS is 0). */
1285 x_wm_set_size_hint (FRAME_PTR f
, long int flags
, int user_position
)
1287 /* Must use GTK routines here, otherwise GTK resets the size hints
1288 to its own defaults. */
1289 GdkGeometry size_hints
;
1290 gint hint_flags
= 0;
1291 int base_width
, base_height
;
1292 int min_rows
= 0, min_cols
= 0;
1293 int win_gravity
= f
->win_gravity
;
1295 /* Don't set size hints during initialization; that apparently leads
1296 to a race condition. See the thread at
1297 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1298 if (NILP (Vafter_init_time
) || !FRAME_GTK_OUTER_WIDGET (f
))
1303 memset (&size_hints
, 0, sizeof (size_hints
));
1304 f
->output_data
.x
->size_hints
= size_hints
;
1305 f
->output_data
.x
->hint_flags
= hint_flags
;
1308 flags
= f
->size_hint_flags
;
1310 size_hints
= f
->output_data
.x
->size_hints
;
1311 hint_flags
= f
->output_data
.x
->hint_flags
;
1313 hint_flags
|= GDK_HINT_RESIZE_INC
| GDK_HINT_MIN_SIZE
;
1314 size_hints
.width_inc
= FRAME_COLUMN_WIDTH (f
);
1315 size_hints
.height_inc
= FRAME_LINE_HEIGHT (f
);
1317 hint_flags
|= GDK_HINT_BASE_SIZE
;
1318 base_width
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f
, 0) + FRAME_TOOLBAR_WIDTH (f
);
1319 /* Use one row here so base_height does not become zero.
1320 Gtk+ and/or Unity on Ubuntu 12.04 can't handle it. */
1321 base_height
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f
, 1)
1322 + FRAME_MENUBAR_HEIGHT (f
) + FRAME_TOOLBAR_HEIGHT (f
);
1324 check_frame_size (f
, &min_rows
, &min_cols
);
1325 if (min_rows
> 0) --min_rows
; /* We used one row in base_height = ... 1); */
1327 size_hints
.base_width
= base_width
;
1328 size_hints
.base_height
= base_height
;
1329 size_hints
.min_width
= base_width
+ min_cols
* size_hints
.width_inc
;
1330 size_hints
.min_height
= base_height
+ min_rows
* size_hints
.height_inc
;
1332 /* These currently have a one to one mapping with the X values, but I
1333 don't think we should rely on that. */
1334 hint_flags
|= GDK_HINT_WIN_GRAVITY
;
1335 size_hints
.win_gravity
= 0;
1336 if (win_gravity
== NorthWestGravity
)
1337 size_hints
.win_gravity
= GDK_GRAVITY_NORTH_WEST
;
1338 else if (win_gravity
== NorthGravity
)
1339 size_hints
.win_gravity
= GDK_GRAVITY_NORTH
;
1340 else if (win_gravity
== NorthEastGravity
)
1341 size_hints
.win_gravity
= GDK_GRAVITY_NORTH_EAST
;
1342 else if (win_gravity
== WestGravity
)
1343 size_hints
.win_gravity
= GDK_GRAVITY_WEST
;
1344 else if (win_gravity
== CenterGravity
)
1345 size_hints
.win_gravity
= GDK_GRAVITY_CENTER
;
1346 else if (win_gravity
== EastGravity
)
1347 size_hints
.win_gravity
= GDK_GRAVITY_EAST
;
1348 else if (win_gravity
== SouthWestGravity
)
1349 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH_WEST
;
1350 else if (win_gravity
== SouthGravity
)
1351 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH
;
1352 else if (win_gravity
== SouthEastGravity
)
1353 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH_EAST
;
1354 else if (win_gravity
== StaticGravity
)
1355 size_hints
.win_gravity
= GDK_GRAVITY_STATIC
;
1359 hint_flags
&= ~GDK_HINT_POS
;
1360 hint_flags
|= GDK_HINT_USER_POS
;
1363 if (hint_flags
!= f
->output_data
.x
->hint_flags
1364 || memcmp (&size_hints
,
1365 &f
->output_data
.x
->size_hints
,
1366 sizeof (size_hints
)) != 0)
1369 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
1370 NULL
, &size_hints
, hint_flags
);
1371 f
->output_data
.x
->size_hints
= size_hints
;
1372 f
->output_data
.x
->hint_flags
= hint_flags
;
1377 /* Change background color of a frame.
1378 Since GTK uses the background color to clear the window, we must
1379 keep the GTK and X colors in sync.
1380 F is the frame to change,
1381 BG is the pixel value to change to. */
1384 xg_set_background_color (FRAME_PTR f
, long unsigned int bg
)
1386 if (FRAME_GTK_WIDGET (f
))
1389 xg_set_widget_bg (f
, FRAME_GTK_WIDGET (f
), FRAME_BACKGROUND_PIXEL (f
));
1395 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1396 functions so GTK does not overwrite the icon. */
1399 xg_set_frame_icon (FRAME_PTR f
, Pixmap icon_pixmap
, Pixmap icon_mask
)
1401 GdkPixbuf
*gp
= xg_get_pixbuf_from_pix_and_mask (f
,
1405 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)), gp
);
1410 /***********************************************************************
1412 ***********************************************************************/
1413 /* Return the dialog title to use for a dialog of type KEY.
1414 This is the encoding used by lwlib. We use the same for GTK. */
1417 get_dialog_title (char key
)
1419 const char *title
= "";
1427 title
= "Information";
1446 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1447 the dialog, but return TRUE so the event does not propagate further
1448 in GTK. This prevents GTK from destroying the dialog widget automatically
1449 and we can always destroy the widget manually, regardless of how
1450 it was popped down (button press or WM_DELETE_WINDOW).
1451 W is the dialog widget.
1452 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1453 user_data is NULL (not used).
1455 Returns TRUE to end propagation of event. */
1458 dialog_delete_callback (GtkWidget
*w
, GdkEvent
*event
, gpointer user_data
)
1460 gtk_widget_unmap (w
);
1464 /* Create a popup dialog window. See also xg_create_widget below.
1465 WV is a widget_value describing the dialog.
1466 SELECT_CB is the callback to use when a button has been pressed.
1467 DEACTIVATE_CB is the callback to use when the dialog pops down.
1469 Returns the GTK dialog widget. */
1472 create_dialog (widget_value
*wv
,
1473 GCallback select_cb
,
1474 GCallback deactivate_cb
)
1476 const char *title
= get_dialog_title (wv
->name
[0]);
1477 int total_buttons
= wv
->name
[1] - '0';
1478 int right_buttons
= wv
->name
[4] - '0';
1481 int button_spacing
= 10;
1482 GtkWidget
*wdialog
= gtk_dialog_new ();
1483 GtkDialog
*wd
= GTK_DIALOG (wdialog
);
1484 GtkBox
*cur_box
= GTK_BOX (gtk_dialog_get_action_area (wd
));
1486 GtkWidget
*whbox_down
;
1488 /* If the number of buttons is greater than 4, make two rows of buttons
1489 instead. This looks better. */
1490 int make_two_rows
= total_buttons
> 4;
1492 if (right_buttons
== 0) right_buttons
= total_buttons
/2;
1493 left_buttons
= total_buttons
- right_buttons
;
1495 gtk_window_set_title (GTK_WINDOW (wdialog
), title
);
1496 gtk_widget_set_name (wdialog
, "emacs-dialog");
1501 GtkWidget
*wvbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, button_spacing
);
1502 GtkWidget
*whbox_up
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 0);
1503 gtk_box_set_homogeneous (GTK_BOX (wvbox
), TRUE
);
1504 gtk_box_set_homogeneous (GTK_BOX (whbox_up
), FALSE
);
1505 whbox_down
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 0);
1506 gtk_box_set_homogeneous (GTK_BOX (whbox_down
), FALSE
);
1508 gtk_box_pack_start (cur_box
, wvbox
, FALSE
, FALSE
, 0);
1509 gtk_box_pack_start (GTK_BOX (wvbox
), whbox_up
, FALSE
, FALSE
, 0);
1510 gtk_box_pack_start (GTK_BOX (wvbox
), whbox_down
, FALSE
, FALSE
, 0);
1512 cur_box
= GTK_BOX (whbox_up
);
1515 g_signal_connect (G_OBJECT (wdialog
), "delete-event",
1516 G_CALLBACK (dialog_delete_callback
), 0);
1520 g_signal_connect (G_OBJECT (wdialog
), "close", deactivate_cb
, 0);
1521 g_signal_connect (G_OBJECT (wdialog
), "response", deactivate_cb
, 0);
1524 for (item
= wv
->contents
; item
; item
= item
->next
)
1526 char *utf8_label
= get_utf8_string (item
->value
);
1530 if (item
->name
&& strcmp (item
->name
, "message") == 0)
1532 GtkBox
*wvbox
= GTK_BOX (gtk_dialog_get_content_area (wd
));
1533 /* This is the text part of the dialog. */
1534 w
= gtk_label_new (utf8_label
);
1535 gtk_box_pack_start (wvbox
, gtk_label_new (""), FALSE
, FALSE
, 0);
1536 gtk_box_pack_start (wvbox
, w
, TRUE
, TRUE
, 0);
1537 gtk_misc_set_alignment (GTK_MISC (w
), 0.1, 0.5);
1539 /* Try to make dialog look better. Must realize first so
1540 the widget can calculate the size it needs. */
1541 gtk_widget_realize (w
);
1542 gtk_widget_get_preferred_size (w
, NULL
, &req
);
1543 gtk_box_set_spacing (wvbox
, req
.height
);
1544 if (item
->value
&& strlen (item
->value
) > 0)
1545 button_spacing
= 2*req
.width
/strlen (item
->value
);
1549 /* This is one button to add to the dialog. */
1550 w
= gtk_button_new_with_label (utf8_label
);
1551 if (! item
->enabled
)
1552 gtk_widget_set_sensitive (w
, FALSE
);
1554 g_signal_connect (G_OBJECT (w
), "clicked",
1555 select_cb
, item
->call_data
);
1557 gtk_box_pack_start (cur_box
, w
, TRUE
, TRUE
, button_spacing
);
1558 if (++button_nr
== left_buttons
)
1561 cur_box
= GTK_BOX (whbox_down
);
1563 gtk_box_pack_start (cur_box
,
1571 g_free (utf8_label
);
1577 struct xg_dialog_data
1585 /* Function that is called when the file or font dialogs pop down.
1586 W is the dialog widget, RESPONSE is the response code.
1587 USER_DATA is what we passed in to g_signal_connect. */
1590 xg_dialog_response_cb (GtkDialog
*w
,
1594 struct xg_dialog_data
*dd
= (struct xg_dialog_data
*)user_data
;
1595 dd
->response
= response
;
1596 g_main_loop_quit (dd
->loop
);
1600 /* Destroy the dialog. This makes it pop down. */
1603 pop_down_dialog (Lisp_Object arg
)
1605 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
1606 struct xg_dialog_data
*dd
= (struct xg_dialog_data
*) p
->pointer
;
1609 if (dd
->w
) gtk_widget_destroy (dd
->w
);
1610 if (dd
->timerid
!= 0) g_source_remove (dd
->timerid
);
1612 g_main_loop_quit (dd
->loop
);
1613 g_main_loop_unref (dd
->loop
);
1620 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1621 We pass in DATA as gpointer* so we can use this as a callback. */
1624 xg_maybe_add_timer (gpointer data
)
1626 struct xg_dialog_data
*dd
= (struct xg_dialog_data
*) data
;
1627 EMACS_TIME next_time
= timer_check ();
1631 if (EMACS_TIME_VALID_P (next_time
))
1633 time_t s
= EMACS_SECS (next_time
);
1634 int per_ms
= EMACS_TIME_RESOLUTION
/ 1000;
1635 int ms
= (EMACS_NSECS (next_time
) + per_ms
- 1) / per_ms
;
1636 if (s
<= ((guint
) -1 - ms
) / 1000)
1637 dd
->timerid
= g_timeout_add (s
* 1000 + ms
, xg_maybe_add_timer
, dd
);
1643 /* Pops up a modal dialog W and waits for response.
1644 We don't use gtk_dialog_run because we want to process emacs timers.
1645 The dialog W is not destroyed when this function returns. */
1648 xg_dialog_run (FRAME_PTR f
, GtkWidget
*w
)
1650 ptrdiff_t count
= SPECPDL_INDEX ();
1651 struct xg_dialog_data dd
;
1653 xg_set_screen (w
, f
);
1654 gtk_window_set_transient_for (GTK_WINDOW (w
),
1655 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
1656 gtk_window_set_destroy_with_parent (GTK_WINDOW (w
), TRUE
);
1657 gtk_window_set_modal (GTK_WINDOW (w
), TRUE
);
1659 dd
.loop
= g_main_loop_new (NULL
, FALSE
);
1660 dd
.response
= GTK_RESPONSE_CANCEL
;
1664 g_signal_connect (G_OBJECT (w
),
1666 G_CALLBACK (xg_dialog_response_cb
),
1668 /* Don't destroy the widget if closed by the window manager close button. */
1669 g_signal_connect (G_OBJECT (w
), "delete-event", G_CALLBACK (gtk_true
), NULL
);
1670 gtk_widget_show (w
);
1672 record_unwind_protect (pop_down_dialog
, make_save_value (&dd
, 0));
1674 (void) xg_maybe_add_timer (&dd
);
1675 g_main_loop_run (dd
.loop
);
1678 unbind_to (count
, Qnil
);
1684 /***********************************************************************
1685 File dialog functions
1686 ***********************************************************************/
1687 /* Return non-zero if the old file selection dialog is being used.
1688 Return zero if not. */
1691 xg_uses_old_file_dialog (void)
1693 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1694 return x_gtk_use_old_file_dialog
;
1701 typedef char * (*xg_get_file_func
) (GtkWidget
*);
1703 /* Return the selected file for file chooser dialog W.
1704 The returned string must be free:d. */
1707 xg_get_file_name_from_chooser (GtkWidget
*w
)
1709 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w
));
1712 /* Callback called when the "Show hidden files" toggle is pressed.
1713 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1716 xg_toggle_visibility_cb (GtkWidget
*widget
, gpointer data
)
1718 GtkFileChooser
*dialog
= GTK_FILE_CHOOSER (data
);
1720 g_object_get (G_OBJECT (dialog
), "show-hidden", &visible
, NULL
);
1721 g_object_set (G_OBJECT (dialog
), "show-hidden", !visible
, NULL
);
1725 /* Callback called when a property changes in a file chooser.
1726 GOBJECT is the file chooser dialog, ARG1 describes the property.
1727 USER_DATA is the toggle widget in the file chooser dialog.
1728 We use this to update the "Show hidden files" toggle when the user
1729 changes that property by right clicking in the file list. */
1732 xg_toggle_notify_cb (GObject
*gobject
, GParamSpec
*arg1
, gpointer user_data
)
1734 if (strcmp (arg1
->name
, "show-hidden") == 0)
1736 GtkWidget
*wtoggle
= GTK_WIDGET (user_data
);
1737 gboolean visible
, toggle_on
;
1739 g_object_get (G_OBJECT (gobject
), "show-hidden", &visible
, NULL
);
1740 toggle_on
= gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle
));
1742 if (!!visible
!= !!toggle_on
)
1744 g_signal_handlers_block_by_func (G_OBJECT (wtoggle
),
1745 G_CALLBACK (xg_toggle_visibility_cb
),
1747 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle
), visible
);
1748 g_signal_handlers_unblock_by_func
1749 (G_OBJECT (wtoggle
),
1750 G_CALLBACK (xg_toggle_visibility_cb
),
1753 x_gtk_show_hidden_files
= visible
;
1757 /* Read a file name from the user using a file chooser dialog.
1758 F is the current frame.
1759 PROMPT is a prompt to show to the user. May not be NULL.
1760 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1761 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1762 file. (Actually, this only has cosmetic effects, the user can
1763 still enter a non-existing file.) *FUNC is set to a function that
1764 can be used to retrieve the selected file name from the returned widget.
1766 Returns the created widget. */
1769 xg_get_file_with_chooser (FRAME_PTR f
,
1771 char *default_filename
,
1772 int mustmatch_p
, int only_dir_p
,
1773 xg_get_file_func
*func
)
1777 GtkWidget
*filewin
, *wtoggle
, *wbox
, *wmessage
IF_LINT (= NULL
);
1778 GtkWindow
*gwin
= GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
));
1779 GtkFileChooserAction action
= (mustmatch_p
?
1780 GTK_FILE_CHOOSER_ACTION_OPEN
:
1781 GTK_FILE_CHOOSER_ACTION_SAVE
);
1784 action
= GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
;
1786 filewin
= gtk_file_chooser_dialog_new (prompt
, gwin
, action
,
1787 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1788 (mustmatch_p
|| only_dir_p
?
1789 GTK_STOCK_OPEN
: GTK_STOCK_OK
),
1792 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin
), TRUE
);
1794 wbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 0);
1795 gtk_box_set_homogeneous (GTK_BOX (wbox
), FALSE
);
1796 gtk_widget_show (wbox
);
1797 wtoggle
= gtk_check_button_new_with_label ("Show hidden files.");
1799 if (x_gtk_show_hidden_files
)
1801 g_object_set (G_OBJECT (filewin
), "show-hidden", TRUE
, NULL
);
1802 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle
), TRUE
);
1804 gtk_widget_show (wtoggle
);
1805 g_signal_connect (G_OBJECT (wtoggle
), "clicked",
1806 G_CALLBACK (xg_toggle_visibility_cb
), filewin
);
1807 g_signal_connect (G_OBJECT (filewin
), "notify",
1808 G_CALLBACK (xg_toggle_notify_cb
), wtoggle
);
1810 if (x_gtk_file_dialog_help_text
)
1813 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1814 Show the C-l help text only for versions < 2.10. */
1815 if (gtk_check_version (2, 10, 0) && action
!= GTK_FILE_CHOOSER_ACTION_SAVE
)
1816 strcat (msgbuf
, "\nType C-l to display a file name text entry box.\n");
1817 strcat (msgbuf
, "\nIf you don't like this file selector, use the "
1818 "corresponding\nkey binding or customize "
1819 "use-file-dialog to turn it off.");
1821 wmessage
= gtk_label_new (msgbuf
);
1822 gtk_widget_show (wmessage
);
1825 gtk_box_pack_start (GTK_BOX (wbox
), wtoggle
, FALSE
, FALSE
, 0);
1826 if (x_gtk_file_dialog_help_text
)
1827 gtk_box_pack_start (GTK_BOX (wbox
), wmessage
, FALSE
, FALSE
, 0);
1828 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin
), wbox
);
1830 if (default_filename
)
1833 struct gcpro gcpro1
;
1834 char *utf8_filename
;
1837 file
= build_string (default_filename
);
1839 /* File chooser does not understand ~/... in the file name. It must be
1840 an absolute name starting with /. */
1841 if (default_filename
[0] != '/')
1842 file
= Fexpand_file_name (file
, Qnil
);
1844 utf8_filename
= SSDATA (ENCODE_UTF_8 (file
));
1845 if (! NILP (Ffile_directory_p (file
)))
1846 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin
),
1850 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin
),
1852 if (action
== GTK_FILE_CHOOSER_ACTION_SAVE
)
1854 char *cp
= strrchr (utf8_filename
, '/');
1856 else cp
= utf8_filename
;
1857 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin
), cp
);
1864 *func
= xg_get_file_name_from_chooser
;
1868 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1870 /* Return the selected file for file selector dialog W.
1871 The returned string must be free:d. */
1874 xg_get_file_name_from_selector (GtkWidget
*w
)
1876 GtkFileSelection
*filesel
= GTK_FILE_SELECTION (w
);
1877 return xstrdup ((char*) gtk_file_selection_get_filename (filesel
));
1880 /* Create a file selection dialog.
1881 F is the current frame.
1882 PROMPT is a prompt to show to the user. May not be NULL.
1883 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1884 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1885 file. *FUNC is set to a function that can be used to retrieve the
1886 selected file name from the returned widget.
1888 Returns the created widget. */
1891 xg_get_file_with_selection (FRAME_PTR f
,
1893 char *default_filename
,
1894 int mustmatch_p
, int only_dir_p
,
1895 xg_get_file_func
*func
)
1898 GtkFileSelection
*filesel
;
1900 filewin
= gtk_file_selection_new (prompt
);
1901 filesel
= GTK_FILE_SELECTION (filewin
);
1903 if (default_filename
)
1904 gtk_file_selection_set_filename (filesel
, default_filename
);
1908 /* The selection_entry part of filesel is not documented. */
1909 gtk_widget_set_sensitive (filesel
->selection_entry
, FALSE
);
1910 gtk_file_selection_hide_fileop_buttons (filesel
);
1913 *func
= xg_get_file_name_from_selector
;
1917 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1919 /* Read a file name from the user using a file dialog, either the old
1920 file selection dialog, or the new file chooser dialog. Which to use
1921 depends on what the GTK version used has, and what the value of
1922 gtk-use-old-file-dialog.
1923 F is the current frame.
1924 PROMPT is a prompt to show to the user. May not be NULL.
1925 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1926 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1929 Returns a file name or NULL if no file was selected.
1930 The returned string must be freed by the caller. */
1933 xg_get_file_name (FRAME_PTR f
,
1935 char *default_filename
,
1941 int filesel_done
= 0;
1942 xg_get_file_func func
;
1944 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
1945 /* I really don't know why this is needed, but without this the GLIBC add on
1946 library linuxthreads hangs when the Gnome file chooser backend creates
1948 sigblock (sigmask (__SIGRTMIN
));
1949 #endif /* HAVE_PTHREAD */
1951 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1953 if (xg_uses_old_file_dialog ())
1954 w
= xg_get_file_with_selection (f
, prompt
, default_filename
,
1955 mustmatch_p
, only_dir_p
, &func
);
1957 w
= xg_get_file_with_chooser (f
, prompt
, default_filename
,
1958 mustmatch_p
, only_dir_p
, &func
);
1960 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
1961 w
= xg_get_file_with_chooser (f
, prompt
, default_filename
,
1962 mustmatch_p
, only_dir_p
, &func
);
1963 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
1965 gtk_widget_set_name (w
, "emacs-filedialog");
1967 filesel_done
= xg_dialog_run (f
, w
);
1969 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
1970 sigunblock (sigmask (__SIGRTMIN
));
1973 if (filesel_done
== GTK_RESPONSE_OK
)
1976 gtk_widget_destroy (w
);
1980 #ifdef HAVE_FREETYPE
1981 /* Pop up a GTK font selector and return the name of the font the user
1982 selects, as a C string. The returned font name follows GTK's own
1985 `FAMILY [VALUE1 VALUE2] SIZE'
1987 This can be parsed using font_parse_fcname in font.c.
1988 DEFAULT_NAME, if non-zero, is the default font name. */
1991 xg_get_font_name (FRAME_PTR f
, const char *default_name
)
1994 char *fontname
= NULL
;
1997 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
1998 sigblock (sigmask (__SIGRTMIN
));
1999 #endif /* HAVE_PTHREAD */
2001 w
= gtk_font_selection_dialog_new ("Pick a font");
2003 default_name
= "Monospace 10";
2004 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w
),
2007 gtk_widget_set_name (w
, "emacs-fontdialog");
2009 done
= xg_dialog_run (f
, w
);
2011 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
2012 sigunblock (sigmask (__SIGRTMIN
));
2015 if (done
== GTK_RESPONSE_OK
)
2016 fontname
= gtk_font_selection_dialog_get_font_name
2017 (GTK_FONT_SELECTION_DIALOG (w
));
2019 gtk_widget_destroy (w
);
2022 #endif /* HAVE_FREETYPE */
2026 /***********************************************************************
2028 ***********************************************************************/
2030 /* The name of menu items that can be used for customization. Since GTK
2031 RC files are very crude and primitive, we have to set this on all
2032 menu item names so a user can easily customize menu items. */
2034 #define MENU_ITEM_NAME "emacs-menuitem"
2037 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2038 during GC. The next member points to the items. */
2039 static xg_list_node xg_menu_cb_list
;
2041 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2042 during GC. The next member points to the items. */
2043 static xg_list_node xg_menu_item_cb_list
;
2045 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2046 F is the frame CL_DATA will be initialized for.
2047 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2049 The menu bar and all sub menus under the menu bar in a frame
2050 share the same structure, hence the reference count.
2052 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2053 allocated xg_menu_cb_data if CL_DATA is NULL. */
2055 static xg_menu_cb_data
*
2056 make_cl_data (xg_menu_cb_data
*cl_data
, FRAME_PTR f
, GCallback highlight_cb
)
2060 cl_data
= xmalloc (sizeof *cl_data
);
2062 cl_data
->menu_bar_vector
= f
->menu_bar_vector
;
2063 cl_data
->menu_bar_items_used
= f
->menu_bar_items_used
;
2064 cl_data
->highlight_cb
= highlight_cb
;
2065 cl_data
->ref_count
= 0;
2067 xg_list_insert (&xg_menu_cb_list
, &cl_data
->ptrs
);
2070 cl_data
->ref_count
++;
2075 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2076 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2078 When the menu bar is updated, menu items may have been added and/or
2079 removed, so menu_bar_vector and menu_bar_items_used change. We must
2080 then update CL_DATA since it is used to determine which menu
2081 item that is invoked in the menu.
2082 HIGHLIGHT_CB could change, there is no check that the same
2083 function is given when modifying a menu bar as was given when
2084 creating the menu bar. */
2087 update_cl_data (xg_menu_cb_data
*cl_data
,
2089 GCallback highlight_cb
)
2094 cl_data
->menu_bar_vector
= f
->menu_bar_vector
;
2095 cl_data
->menu_bar_items_used
= f
->menu_bar_items_used
;
2096 cl_data
->highlight_cb
= highlight_cb
;
2100 /* Decrease reference count for CL_DATA.
2101 If reference count is zero, free CL_DATA. */
2104 unref_cl_data (xg_menu_cb_data
*cl_data
)
2106 if (cl_data
&& cl_data
->ref_count
> 0)
2108 cl_data
->ref_count
--;
2109 if (cl_data
->ref_count
== 0)
2111 xg_list_remove (&xg_menu_cb_list
, &cl_data
->ptrs
);
2117 /* Function that marks all lisp data during GC. */
2124 for (iter
= xg_menu_cb_list
.next
; iter
; iter
= iter
->next
)
2125 mark_object (((xg_menu_cb_data
*) iter
)->menu_bar_vector
);
2127 for (iter
= xg_menu_item_cb_list
.next
; iter
; iter
= iter
->next
)
2129 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) iter
;
2131 if (! NILP (cb_data
->help
))
2132 mark_object (cb_data
->help
);
2137 /* Callback called when a menu item is destroyed. Used to free data.
2138 W is the widget that is being destroyed (not used).
2139 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2142 menuitem_destroy_callback (GtkWidget
*w
, gpointer client_data
)
2146 xg_menu_item_cb_data
*data
= (xg_menu_item_cb_data
*) client_data
;
2147 xg_list_remove (&xg_menu_item_cb_list
, &data
->ptrs
);
2152 /* Callback called when the pointer enters/leaves a menu item.
2153 W is the parent of the menu item.
2154 EVENT is either an enter event or leave event.
2155 CLIENT_DATA is not used.
2157 Returns FALSE to tell GTK to keep processing this event. */
2160 menuitem_highlight_callback (GtkWidget
*w
,
2161 GdkEventCrossing
*event
,
2162 gpointer client_data
)
2165 GtkWidget
*subwidget
;
2166 xg_menu_item_cb_data
*data
;
2168 ev
.crossing
= *event
;
2169 subwidget
= gtk_get_event_widget (&ev
);
2170 data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (subwidget
),
2174 if (! NILP (data
->help
) && data
->cl_data
->highlight_cb
)
2176 gpointer call_data
= event
->type
== GDK_LEAVE_NOTIFY
? 0 : data
;
2177 GtkCallback func
= (GtkCallback
) data
->cl_data
->highlight_cb
;
2178 (*func
) (subwidget
, call_data
);
2185 /* Callback called when a menu is destroyed. Used to free data.
2186 W is the widget that is being destroyed (not used).
2187 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2190 menu_destroy_callback (GtkWidget
*w
, gpointer client_data
)
2192 unref_cl_data ((xg_menu_cb_data
*) client_data
);
2195 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2196 must be non-NULL) and can be inserted into a menu item.
2198 Returns the GtkHBox. */
2201 make_widget_for_menu_item (const char *utf8_label
, const char *utf8_key
)
2207 wbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 0);
2208 gtk_box_set_homogeneous (GTK_BOX (wbox
), FALSE
);
2209 wlbl
= gtk_label_new (utf8_label
);
2210 wkey
= gtk_label_new (utf8_key
);
2212 gtk_misc_set_alignment (GTK_MISC (wlbl
), 0.0, 0.5);
2213 gtk_misc_set_alignment (GTK_MISC (wkey
), 0.0, 0.5);
2215 gtk_box_pack_start (GTK_BOX (wbox
), wlbl
, TRUE
, TRUE
, 0);
2216 gtk_box_pack_start (GTK_BOX (wbox
), wkey
, FALSE
, FALSE
, 0);
2218 gtk_widget_set_name (wlbl
, MENU_ITEM_NAME
);
2219 gtk_widget_set_name (wkey
, MENU_ITEM_NAME
);
2220 gtk_widget_set_name (wbox
, MENU_ITEM_NAME
);
2225 /* Make and return a menu item widget with the key to the right.
2226 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2227 UTF8_KEY is the text representing the key binding.
2228 ITEM is the widget_value describing the menu item.
2230 GROUP is an in/out parameter. If the menu item to be created is not
2231 part of any radio menu group, *GROUP contains NULL on entry and exit.
2232 If the menu item to be created is part of a radio menu group, on entry
2233 *GROUP contains the group to use, or NULL if this is the first item
2234 in the group. On exit, *GROUP contains the radio item group.
2236 Unfortunately, keys don't line up as nicely as in Motif,
2237 but the MacOS X version doesn't either, so I guess that is OK. */
2240 make_menu_item (const char *utf8_label
,
2241 const char *utf8_key
,
2246 GtkWidget
*wtoadd
= 0;
2248 /* It has been observed that some menu items have a NULL name field.
2249 This will lead to this function being called with a NULL utf8_label.
2250 GTK crashes on that so we set a blank label. Why there is a NULL
2251 name remains to be investigated. */
2252 if (! utf8_label
) utf8_label
= " ";
2255 wtoadd
= make_widget_for_menu_item (utf8_label
, utf8_key
);
2257 if (item
->button_type
== BUTTON_TYPE_TOGGLE
)
2260 if (utf8_key
) w
= gtk_check_menu_item_new ();
2261 else w
= gtk_check_menu_item_new_with_label (utf8_label
);
2262 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), item
->selected
);
2264 else if (item
->button_type
== BUTTON_TYPE_RADIO
)
2266 if (utf8_key
) w
= gtk_radio_menu_item_new (*group
);
2267 else w
= gtk_radio_menu_item_new_with_label (*group
, utf8_label
);
2268 *group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w
));
2270 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), TRUE
);
2275 if (utf8_key
) w
= gtk_menu_item_new ();
2276 else w
= gtk_menu_item_new_with_label (utf8_label
);
2279 if (wtoadd
) gtk_container_add (GTK_CONTAINER (w
), wtoadd
);
2280 if (! item
->enabled
) gtk_widget_set_sensitive (w
, FALSE
);
2285 static int xg_detached_menus
;
2287 /* Returns non-zero if there are detached menus. */
2290 xg_have_tear_offs (void)
2292 return xg_detached_menus
> 0;
2295 /* Callback invoked when a detached menu window is removed. Here we
2296 decrease the xg_detached_menus count.
2297 WIDGET is the top level window that is removed (the parent of the menu).
2298 CLIENT_DATA is not used. */
2301 tearoff_remove (GtkWidget
*widget
, gpointer client_data
)
2303 if (xg_detached_menus
> 0) --xg_detached_menus
;
2306 /* Callback invoked when a menu is detached. It increases the
2307 xg_detached_menus count.
2308 WIDGET is the GtkTearoffMenuItem.
2309 CLIENT_DATA is not used. */
2312 tearoff_activate (GtkWidget
*widget
, gpointer client_data
)
2314 GtkWidget
*menu
= gtk_widget_get_parent (widget
);
2315 if (gtk_menu_get_tearoff_state (GTK_MENU (menu
)))
2317 ++xg_detached_menus
;
2318 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget
)),
2320 G_CALLBACK (tearoff_remove
), 0);
2325 /* Create a menu item widget, and connect the callbacks.
2326 ITEM describes the menu item.
2327 F is the frame the created menu belongs to.
2328 SELECT_CB is the callback to use when a menu item is selected.
2329 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2330 CL_DATA points to the callback data to be used for this menu.
2331 GROUP is an in/out parameter. If the menu item to be created is not
2332 part of any radio menu group, *GROUP contains NULL on entry and exit.
2333 If the menu item to be created is part of a radio menu group, on entry
2334 *GROUP contains the group to use, or NULL if this is the first item
2335 in the group. On exit, *GROUP contains the radio item group.
2337 Returns the created GtkWidget. */
2340 xg_create_one_menuitem (widget_value
*item
,
2342 GCallback select_cb
,
2343 GCallback highlight_cb
,
2344 xg_menu_cb_data
*cl_data
,
2350 xg_menu_item_cb_data
*cb_data
;
2352 utf8_label
= get_utf8_string (item
->name
);
2353 utf8_key
= get_utf8_string (item
->key
);
2355 w
= make_menu_item (utf8_label
, utf8_key
, item
, group
);
2357 if (utf8_label
) g_free (utf8_label
);
2358 if (utf8_key
) g_free (utf8_key
);
2360 cb_data
= xmalloc (sizeof *cb_data
);
2362 xg_list_insert (&xg_menu_item_cb_list
, &cb_data
->ptrs
);
2364 cb_data
->select_id
= 0;
2365 cb_data
->help
= item
->help
;
2366 cb_data
->cl_data
= cl_data
;
2367 cb_data
->call_data
= item
->call_data
;
2369 g_signal_connect (G_OBJECT (w
),
2371 G_CALLBACK (menuitem_destroy_callback
),
2374 /* Put cb_data in widget, so we can get at it when modifying menubar */
2375 g_object_set_data (G_OBJECT (w
), XG_ITEM_DATA
, cb_data
);
2377 /* final item, not a submenu */
2378 if (item
->call_data
&& ! item
->contents
)
2382 = g_signal_connect (G_OBJECT (w
), "activate", select_cb
, cb_data
);
2388 /* Create a full menu tree specified by DATA.
2389 F is the frame the created menu belongs to.
2390 SELECT_CB is the callback to use when a menu item is selected.
2391 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2392 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2393 POP_UP_P is non-zero if we shall create a popup menu.
2394 MENU_BAR_P is non-zero if we shall create a menu bar.
2395 ADD_TEAROFF_P is non-zero if we shall add a tearoff menu item. Ignored
2396 if MENU_BAR_P is non-zero.
2397 TOPMENU is the topmost GtkWidget that others shall be placed under.
2398 It may be NULL, in that case we create the appropriate widget
2399 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2400 CL_DATA is the callback data we shall use for this menu, or NULL
2401 if we haven't set the first callback yet.
2402 NAME is the name to give to the top level menu if this function
2403 creates it. May be NULL to not set any name.
2405 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2408 This function calls itself to create submenus. */
2411 create_menus (widget_value
*data
,
2413 GCallback select_cb
,
2414 GCallback deactivate_cb
,
2415 GCallback highlight_cb
,
2420 xg_menu_cb_data
*cl_data
,
2424 GtkWidget
*wmenu
= topmenu
;
2425 GSList
*group
= NULL
;
2431 wmenu
= gtk_menu_new ();
2432 xg_set_screen (wmenu
, f
);
2433 /* Connect this to the menu instead of items so we get enter/leave for
2434 disabled items also. TODO: Still does not get enter/leave for
2435 disabled items in detached menus. */
2436 g_signal_connect (G_OBJECT (wmenu
),
2437 "enter-notify-event",
2438 G_CALLBACK (menuitem_highlight_callback
),
2440 g_signal_connect (G_OBJECT (wmenu
),
2441 "leave-notify-event",
2442 G_CALLBACK (menuitem_highlight_callback
),
2447 wmenu
= gtk_menu_bar_new ();
2448 /* Set width of menu bar to a small value so it doesn't enlarge
2449 a small initial frame size. The width will be set to the
2450 width of the frame later on when it is added to a container.
2451 height -1: Natural height. */
2452 gtk_widget_set_size_request (wmenu
, 1, -1);
2455 /* Put cl_data on the top menu for easier access. */
2456 cl_data
= make_cl_data (cl_data
, f
, highlight_cb
);
2457 g_object_set_data (G_OBJECT (wmenu
), XG_FRAME_DATA
, (gpointer
)cl_data
);
2458 g_signal_connect (G_OBJECT (wmenu
), "destroy",
2459 G_CALLBACK (menu_destroy_callback
), cl_data
);
2462 gtk_widget_set_name (wmenu
, name
);
2465 g_signal_connect (G_OBJECT (wmenu
),
2466 "selection-done", deactivate_cb
, 0);
2469 if (! menu_bar_p
&& add_tearoff_p
)
2471 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new ();
2472 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu
), tearoff
);
2474 g_signal_connect (G_OBJECT (tearoff
), "activate",
2475 G_CALLBACK (tearoff_activate
), 0);
2478 for (item
= data
; item
; item
= item
->next
)
2482 if (pop_up_p
&& !item
->contents
&& !item
->call_data
2483 && !menu_separator_name_p (item
->name
))
2486 /* A title for a popup. We do the same as GTK does when
2487 creating titles, but it does not look good. */
2489 utf8_label
= get_utf8_string (item
->name
);
2491 gtk_menu_set_title (GTK_MENU (wmenu
), utf8_label
);
2492 w
= gtk_menu_item_new_with_label (utf8_label
);
2493 gtk_widget_set_sensitive (w
, FALSE
);
2494 if (utf8_label
) g_free (utf8_label
);
2496 else if (menu_separator_name_p (item
->name
))
2499 /* GTK only have one separator type. */
2500 w
= gtk_separator_menu_item_new ();
2504 w
= xg_create_one_menuitem (item
,
2506 item
->contents
? 0 : select_cb
,
2511 /* Create a possibly empty submenu for menu bar items, since some
2512 themes don't highlight items correctly without it. */
2513 if (item
->contents
|| menu_bar_p
)
2515 GtkWidget
*submenu
= create_menus (item
->contents
,
2526 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w
), submenu
);
2530 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu
), w
);
2531 gtk_widget_set_name (w
, MENU_ITEM_NAME
);
2537 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2538 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2539 with some text and buttons.
2540 F is the frame the created item belongs to.
2541 NAME is the name to use for the top widget.
2542 VAL is a widget_value structure describing items to be created.
2543 SELECT_CB is the callback to use when a menu item is selected or
2544 a dialog button is pressed.
2545 DEACTIVATE_CB is the callback to use when an item is deactivated.
2546 For a menu, when a sub menu is not shown anymore, for a dialog it is
2547 called when the dialog is popped down.
2548 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2550 Returns the widget created. */
2553 xg_create_widget (const char *type
, const char *name
, FRAME_PTR f
, widget_value
*val
,
2554 GCallback select_cb
, GCallback deactivate_cb
,
2555 GCallback highlight_cb
)
2558 int menu_bar_p
= strcmp (type
, "menubar") == 0;
2559 int pop_up_p
= strcmp (type
, "popup") == 0;
2561 if (strcmp (type
, "dialog") == 0)
2563 w
= create_dialog (val
, select_cb
, deactivate_cb
);
2564 xg_set_screen (w
, f
);
2565 gtk_window_set_transient_for (GTK_WINDOW (w
),
2566 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
2567 gtk_window_set_destroy_with_parent (GTK_WINDOW (w
), TRUE
);
2568 gtk_widget_set_name (w
, "emacs-dialog");
2569 gtk_window_set_modal (GTK_WINDOW (w
), TRUE
);
2571 else if (menu_bar_p
|| pop_up_p
)
2573 w
= create_menus (val
->contents
,
2585 /* Set the cursor to an arrow for popup menus when they are mapped.
2586 This is done by default for menu bar menus. */
2589 /* Must realize so the GdkWindow inside the widget is created. */
2590 gtk_widget_realize (w
);
2591 xg_set_cursor (w
, FRAME_X_DISPLAY_INFO (f
)->xg_cursor
);
2596 fprintf (stderr
, "bad type in xg_create_widget: %s, doing nothing\n",
2603 /* Return the label for menu item WITEM. */
2606 xg_get_menu_item_label (GtkMenuItem
*witem
)
2608 GtkLabel
*wlabel
= GTK_LABEL (XG_BIN_CHILD (witem
));
2609 return gtk_label_get_label (wlabel
);
2612 /* Return non-zero if the menu item WITEM has the text LABEL. */
2615 xg_item_label_same_p (GtkMenuItem
*witem
, const char *label
)
2618 char *utf8_label
= get_utf8_string (label
);
2619 const char *old_label
= witem
? xg_get_menu_item_label (witem
) : 0;
2621 if (! old_label
&& ! utf8_label
)
2623 else if (old_label
&& utf8_label
)
2624 is_same
= strcmp (utf8_label
, old_label
) == 0;
2626 if (utf8_label
) g_free (utf8_label
);
2631 /* Destroy widgets in LIST. */
2634 xg_destroy_widgets (GList
*list
)
2638 for (iter
= list
; iter
; iter
= g_list_next (iter
))
2640 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
2642 /* Destroying the widget will remove it from the container it is in. */
2643 gtk_widget_destroy (w
);
2647 /* Update the top level names in MENUBAR (i.e. not submenus).
2648 F is the frame the menu bar belongs to.
2649 *LIST is a list with the current menu bar names (menu item widgets).
2650 ITER is the item within *LIST that shall be updated.
2651 POS is the numerical position, starting at 0, of ITER in *LIST.
2652 VAL describes what the menu bar shall look like after the update.
2653 SELECT_CB is the callback to use when a menu item is selected.
2654 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2655 CL_DATA points to the callback data to be used for this menu bar.
2657 This function calls itself to walk through the menu bar names. */
2660 xg_update_menubar (GtkWidget
*menubar
,
2666 GCallback select_cb
,
2667 GCallback deactivate_cb
,
2668 GCallback highlight_cb
,
2669 xg_menu_cb_data
*cl_data
)
2671 if (! iter
&& ! val
)
2673 else if (iter
&& ! val
)
2675 /* Item(s) have been removed. Remove all remaining items. */
2676 xg_destroy_widgets (iter
);
2678 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2679 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
),
2680 gtk_menu_item_new_with_label (""),
2686 else if (! iter
&& val
)
2688 /* Item(s) added. Add all new items in one call. */
2689 create_menus (val
, f
, select_cb
, deactivate_cb
, highlight_cb
,
2690 0, 1, 0, menubar
, cl_data
, 0);
2696 /* Below this neither iter or val is NULL */
2697 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter
->data
), val
->name
))
2699 /* This item is still the same, check next item. */
2701 iter
= g_list_next (iter
);
2704 else /* This item is changed. */
2706 GtkMenuItem
*witem
= GTK_MENU_ITEM (iter
->data
);
2707 GtkMenuItem
*witem2
= 0;
2708 int val_in_menubar
= 0;
2709 int iter_in_new_menubar
= 0;
2713 /* See if the changed entry (val) is present later in the menu bar */
2715 iter2
&& ! val_in_menubar
;
2716 iter2
= g_list_next (iter2
))
2718 witem2
= GTK_MENU_ITEM (iter2
->data
);
2719 val_in_menubar
= xg_item_label_same_p (witem2
, val
->name
);
2722 /* See if the current entry (iter) is present later in the
2723 specification for the new menu bar. */
2724 for (cur
= val
; cur
&& ! iter_in_new_menubar
; cur
= cur
->next
)
2725 iter_in_new_menubar
= xg_item_label_same_p (witem
, cur
->name
);
2727 if (val_in_menubar
&& ! iter_in_new_menubar
)
2731 /* This corresponds to:
2736 g_object_ref (G_OBJECT (witem
));
2737 gtk_container_remove (GTK_CONTAINER (menubar
), GTK_WIDGET (witem
));
2738 gtk_widget_destroy (GTK_WIDGET (witem
));
2740 /* Must get new list since the old changed. */
2741 g_list_free (*list
);
2742 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2743 while (nr
-- > 0) iter
= g_list_next (iter
);
2745 else if (! val_in_menubar
&& ! iter_in_new_menubar
)
2747 /* This corresponds to:
2750 Rename B to X. This might seem to be a strange thing to do,
2751 since if there is a menu under B it will be totally wrong for X.
2752 But consider editing a C file. Then there is a C-mode menu
2753 (corresponds to B above).
2754 If then doing C-x C-f the minibuf menu (X above) replaces the
2755 C-mode menu. When returning from the minibuffer, we get
2756 back the C-mode menu. Thus we do:
2757 Rename B to X (C-mode to minibuf menu)
2758 Rename X to B (minibuf to C-mode menu).
2759 If the X menu hasn't been invoked, the menu under B
2760 is up to date when leaving the minibuffer. */
2761 GtkLabel
*wlabel
= GTK_LABEL (XG_BIN_CHILD (witem
));
2762 char *utf8_label
= get_utf8_string (val
->name
);
2763 GtkWidget
*submenu
= gtk_menu_item_get_submenu (witem
);
2765 gtk_label_set_text (wlabel
, utf8_label
);
2767 /* If this item has a submenu that has been detached, change
2768 the title in the WM decorations also. */
2769 if (submenu
&& gtk_menu_get_tearoff_state (GTK_MENU (submenu
)))
2770 /* Set the title of the detached window. */
2771 gtk_menu_set_title (GTK_MENU (submenu
), utf8_label
);
2773 if (utf8_label
) g_free (utf8_label
);
2774 iter
= g_list_next (iter
);
2778 else if (! val_in_menubar
&& iter_in_new_menubar
)
2780 /* This corresponds to:
2787 GtkWidget
*w
= xg_create_one_menuitem (val
,
2794 /* Create a possibly empty submenu for menu bar items, since some
2795 themes don't highlight items correctly without it. */
2796 GtkWidget
*submenu
= create_menus (NULL
, f
,
2797 select_cb
, deactivate_cb
,
2799 0, 0, 0, 0, cl_data
, 0);
2800 gtk_widget_set_name (w
, MENU_ITEM_NAME
);
2801 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
), w
, pos
);
2802 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w
), submenu
);
2804 g_list_free (*list
);
2805 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2806 while (nr
-- > 0) iter
= g_list_next (iter
);
2807 iter
= g_list_next (iter
);
2811 else /* if (val_in_menubar && iter_in_new_menubar) */
2814 /* This corresponds to:
2819 g_object_ref (G_OBJECT (witem2
));
2820 gtk_container_remove (GTK_CONTAINER (menubar
), GTK_WIDGET (witem2
));
2821 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
),
2822 GTK_WIDGET (witem2
), pos
);
2823 g_object_unref (G_OBJECT (witem2
));
2825 g_list_free (*list
);
2826 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2827 while (nr
-- > 0) iter
= g_list_next (iter
);
2828 if (iter
) iter
= g_list_next (iter
);
2834 /* Update the rest of the menu bar. */
2835 xg_update_menubar (menubar
, f
, list
, iter
, pos
, val
,
2836 select_cb
, deactivate_cb
, highlight_cb
, cl_data
);
2839 /* Update the menu item W so it corresponds to VAL.
2840 SELECT_CB is the callback to use when a menu item is selected.
2841 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2842 CL_DATA is the data to set in the widget for menu invocation. */
2845 xg_update_menu_item (widget_value
*val
,
2847 GCallback select_cb
,
2848 GCallback highlight_cb
,
2849 xg_menu_cb_data
*cl_data
)
2856 const char *old_label
= 0;
2857 const char *old_key
= 0;
2858 xg_menu_item_cb_data
*cb_data
;
2860 wchild
= XG_BIN_CHILD (w
);
2861 utf8_label
= get_utf8_string (val
->name
);
2862 utf8_key
= get_utf8_string (val
->key
);
2864 /* See if W is a menu item with a key. See make_menu_item above. */
2865 if (GTK_IS_BOX (wchild
))
2867 GList
*list
= gtk_container_get_children (GTK_CONTAINER (wchild
));
2869 wlbl
= GTK_LABEL (list
->data
);
2870 wkey
= GTK_LABEL (list
->next
->data
);
2875 /* Remove the key and keep just the label. */
2876 g_object_ref (G_OBJECT (wlbl
));
2877 gtk_container_remove (GTK_CONTAINER (w
), wchild
);
2878 gtk_container_add (GTK_CONTAINER (w
), GTK_WIDGET (wlbl
));
2879 g_object_unref (G_OBJECT (wlbl
));
2884 else /* Just a label. */
2886 wlbl
= GTK_LABEL (wchild
);
2888 /* Check if there is now a key. */
2891 GtkWidget
*wtoadd
= make_widget_for_menu_item (utf8_label
, utf8_key
);
2892 GList
*list
= gtk_container_get_children (GTK_CONTAINER (wtoadd
));
2894 wlbl
= GTK_LABEL (list
->data
);
2895 wkey
= GTK_LABEL (list
->next
->data
);
2898 gtk_container_remove (GTK_CONTAINER (w
), wchild
);
2899 gtk_container_add (GTK_CONTAINER (w
), wtoadd
);
2904 if (wkey
) old_key
= gtk_label_get_label (wkey
);
2905 if (wlbl
) old_label
= gtk_label_get_label (wlbl
);
2907 if (wkey
&& utf8_key
&& (! old_key
|| strcmp (utf8_key
, old_key
) != 0))
2908 gtk_label_set_text (wkey
, utf8_key
);
2910 if (! old_label
|| strcmp (utf8_label
, old_label
) != 0)
2911 gtk_label_set_text (wlbl
, utf8_label
);
2913 if (utf8_key
) g_free (utf8_key
);
2914 if (utf8_label
) g_free (utf8_label
);
2916 if (! val
->enabled
&& gtk_widget_get_sensitive (w
))
2917 gtk_widget_set_sensitive (w
, FALSE
);
2918 else if (val
->enabled
&& ! gtk_widget_get_sensitive (w
))
2919 gtk_widget_set_sensitive (w
, TRUE
);
2921 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (w
),
2925 cb_data
->call_data
= val
->call_data
;
2926 cb_data
->help
= val
->help
;
2927 cb_data
->cl_data
= cl_data
;
2929 /* We assume the callback functions don't change. */
2930 if (val
->call_data
&& ! val
->contents
)
2932 /* This item shall have a select callback. */
2933 if (! cb_data
->select_id
)
2935 = g_signal_connect (G_OBJECT (w
), "activate",
2936 select_cb
, cb_data
);
2938 else if (cb_data
->select_id
)
2940 g_signal_handler_disconnect (w
, cb_data
->select_id
);
2941 cb_data
->select_id
= 0;
2946 /* Update the toggle menu item W so it corresponds to VAL. */
2949 xg_update_toggle_item (widget_value
*val
, GtkWidget
*w
)
2951 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), val
->selected
);
2954 /* Update the radio menu item W so it corresponds to VAL. */
2957 xg_update_radio_item (widget_value
*val
, GtkWidget
*w
)
2959 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), val
->selected
);
2962 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2963 SUBMENU may be NULL, in that case a new menu is created.
2964 F is the frame the menu bar belongs to.
2965 VAL describes the contents of the menu bar.
2966 SELECT_CB is the callback to use when a menu item is selected.
2967 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2968 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2969 CL_DATA is the call back data to use for any newly created items.
2971 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2975 xg_update_submenu (GtkWidget
*submenu
,
2978 GCallback select_cb
,
2979 GCallback deactivate_cb
,
2980 GCallback highlight_cb
,
2981 xg_menu_cb_data
*cl_data
)
2983 GtkWidget
*newsub
= submenu
;
2987 int has_tearoff_p
= 0;
2988 GList
*first_radio
= 0;
2991 list
= gtk_container_get_children (GTK_CONTAINER (submenu
));
2993 for (cur
= val
, iter
= list
;
2995 iter
= g_list_next (iter
), cur
= cur
->next
)
2997 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
2999 /* Skip tearoff items, they have no counterpart in val. */
3000 if (GTK_IS_TEAROFF_MENU_ITEM (w
))
3003 iter
= g_list_next (iter
);
3004 if (iter
) w
= GTK_WIDGET (iter
->data
);
3008 /* Remember first radio button in a group. If we get a mismatch in
3009 a radio group we must rebuild the whole group so that the connections
3010 in GTK becomes correct. */
3011 if (cur
->button_type
== BUTTON_TYPE_RADIO
&& ! first_radio
)
3013 else if (cur
->button_type
!= BUTTON_TYPE_RADIO
3014 && ! GTK_IS_RADIO_MENU_ITEM (w
))
3017 if (GTK_IS_SEPARATOR_MENU_ITEM (w
))
3019 if (! menu_separator_name_p (cur
->name
))
3022 else if (GTK_IS_CHECK_MENU_ITEM (w
))
3024 if (cur
->button_type
!= BUTTON_TYPE_TOGGLE
)
3026 xg_update_toggle_item (cur
, w
);
3027 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
3029 else if (GTK_IS_RADIO_MENU_ITEM (w
))
3031 if (cur
->button_type
!= BUTTON_TYPE_RADIO
)
3033 xg_update_radio_item (cur
, w
);
3034 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
3036 else if (GTK_IS_MENU_ITEM (w
))
3038 GtkMenuItem
*witem
= GTK_MENU_ITEM (w
);
3041 if (cur
->button_type
!= BUTTON_TYPE_NONE
||
3042 menu_separator_name_p (cur
->name
))
3045 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
3047 sub
= gtk_menu_item_get_submenu (witem
);
3048 if (sub
&& ! cur
->contents
)
3050 /* Not a submenu anymore. */
3051 g_object_ref (G_OBJECT (sub
));
3052 remove_submenu (witem
);
3053 gtk_widget_destroy (sub
);
3055 else if (cur
->contents
)
3059 nsub
= xg_update_submenu (sub
, f
, cur
->contents
,
3060 select_cb
, deactivate_cb
,
3061 highlight_cb
, cl_data
);
3063 /* If this item just became a submenu, we must set it. */
3065 gtk_menu_item_set_submenu (witem
, nsub
);
3070 /* Structural difference. Remove everything from here and down
3076 /* Remove widgets from first structural change. */
3079 /* If we are adding new menu items below, we must remove from
3080 first radio button so that radio groups become correct. */
3081 if (cur
&& first_radio
) xg_destroy_widgets (first_radio
);
3082 else xg_destroy_widgets (iter
);
3087 /* More items added. Create them. */
3088 newsub
= create_menus (cur
,
3101 if (list
) g_list_free (list
);
3106 /* Update the MENUBAR.
3107 F is the frame the menu bar belongs to.
3108 VAL describes the contents of the menu bar.
3109 If DEEP_P is non-zero, rebuild all but the top level menu names in
3110 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3111 SELECT_CB is the callback to use when a menu item is selected.
3112 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3113 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3116 xg_modify_menubar_widgets (GtkWidget
*menubar
, FRAME_PTR f
, widget_value
*val
,
3118 GCallback select_cb
, GCallback deactivate_cb
,
3119 GCallback highlight_cb
)
3121 xg_menu_cb_data
*cl_data
;
3122 GList
*list
= gtk_container_get_children (GTK_CONTAINER (menubar
));
3126 cl_data
= (xg_menu_cb_data
*) g_object_get_data (G_OBJECT (menubar
),
3129 xg_update_menubar (menubar
, f
, &list
, list
, 0, val
->contents
,
3130 select_cb
, deactivate_cb
, highlight_cb
, cl_data
);
3136 /* Update all sub menus.
3137 We must keep the submenus (GTK menu item widgets) since the
3138 X Window in the XEvent that activates the menu are those widgets. */
3140 /* Update cl_data, menu_item things in F may have changed. */
3141 update_cl_data (cl_data
, f
, highlight_cb
);
3143 for (cur
= val
->contents
; cur
; cur
= cur
->next
)
3148 GtkMenuItem
*witem
= 0;
3150 /* Find sub menu that corresponds to val and update it. */
3151 for (iter
= list
; iter
; iter
= g_list_next (iter
))
3153 witem
= GTK_MENU_ITEM (iter
->data
);
3154 if (xg_item_label_same_p (witem
, cur
->name
))
3156 sub
= gtk_menu_item_get_submenu (witem
);
3161 newsub
= xg_update_submenu (sub
,
3168 /* sub may still be NULL. If we just updated non deep and added
3169 a new menu bar item, it has no sub menu yet. So we set the
3170 newly created sub menu under witem. */
3171 if (newsub
!= sub
&& witem
!= 0)
3173 xg_set_screen (newsub
, f
);
3174 gtk_menu_item_set_submenu (witem
, newsub
);
3180 gtk_widget_show_all (menubar
);
3183 /* Callback called when the menu bar W is mapped.
3184 Used to find the height of the menu bar if we didn't get it
3185 after showing the widget. */
3188 menubar_map_cb (GtkWidget
*w
, gpointer user_data
)
3191 FRAME_PTR f
= (FRAME_PTR
) user_data
;
3192 gtk_widget_get_preferred_size (w
, NULL
, &req
);
3193 if (FRAME_MENUBAR_HEIGHT (f
) != req
.height
)
3195 FRAME_MENUBAR_HEIGHT (f
) = req
.height
;
3196 xg_height_or_width_changed (f
);
3200 /* Recompute all the widgets of frame F, when the menu bar has been
3201 changed. Value is non-zero if widgets were updated. */
3204 xg_update_frame_menubar (FRAME_PTR f
)
3206 struct x_output
*x
= f
->output_data
.x
;
3209 if (!x
->menubar_widget
|| gtk_widget_get_mapped (x
->menubar_widget
))
3212 if (x
->menubar_widget
&& gtk_widget_get_parent (x
->menubar_widget
))
3213 return 0; /* Already done this, happens for frames created invisible. */
3217 gtk_box_pack_start (GTK_BOX (x
->vbox_widget
), x
->menubar_widget
,
3219 gtk_box_reorder_child (GTK_BOX (x
->vbox_widget
), x
->menubar_widget
, 0);
3221 g_signal_connect (x
->menubar_widget
, "map", G_CALLBACK (menubar_map_cb
), f
);
3222 gtk_widget_show_all (x
->menubar_widget
);
3223 gtk_widget_get_preferred_size (x
->menubar_widget
, NULL
, &req
);
3225 /* If menu bar doesn't know its height yet, cheat a little so the frame
3226 doesn't jump so much when resized later in menubar_map_cb. */
3227 if (req
.height
== 0)
3230 if (FRAME_MENUBAR_HEIGHT (f
) != req
.height
)
3232 FRAME_MENUBAR_HEIGHT (f
) = req
.height
;
3233 xg_height_or_width_changed (f
);
3240 /* Get rid of the menu bar of frame F, and free its storage.
3241 This is used when deleting a frame, and when turning off the menu bar. */
3244 free_frame_menubar (FRAME_PTR f
)
3246 struct x_output
*x
= f
->output_data
.x
;
3248 if (x
->menubar_widget
)
3252 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
), x
->menubar_widget
);
3253 /* The menubar and its children shall be deleted when removed from
3255 x
->menubar_widget
= 0;
3256 FRAME_MENUBAR_HEIGHT (f
) = 0;
3257 xg_height_or_width_changed (f
);
3263 xg_event_is_for_menubar (FRAME_PTR f
, XEvent
*event
)
3265 struct x_output
*x
= f
->output_data
.x
;
3274 if (! x
->menubar_widget
) return 0;
3276 if (! (event
->xbutton
.x
>= 0
3277 && event
->xbutton
.x
< FRAME_PIXEL_WIDTH (f
)
3278 && event
->xbutton
.y
>= 0
3279 && event
->xbutton
.y
< f
->output_data
.x
->menubar_height
3280 && event
->xbutton
.same_screen
))
3283 gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
3284 gw
= gdk_x11_window_lookup_for_display (gdpy
, event
->xbutton
.window
);
3286 gevent
.any
.window
= gw
;
3287 gevent
.any
.type
= GDK_NOTHING
;
3288 gwdesc
= gtk_get_event_widget (&gevent
);
3289 if (! gwdesc
) return 0;
3290 if (! GTK_IS_MENU_BAR (gwdesc
)
3291 && ! GTK_IS_MENU_ITEM (gwdesc
)
3292 && ! gtk_widget_is_ancestor (x
->menubar_widget
, gwdesc
))
3295 list
= gtk_container_get_children (GTK_CONTAINER (x
->menubar_widget
));
3296 if (! list
) return 0;
3297 rec
.x
= event
->xbutton
.x
;
3298 rec
.y
= event
->xbutton
.y
;
3302 for (iter
= list
; iter
; iter
= g_list_next (iter
))
3304 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
3305 if (gtk_widget_get_mapped (w
) && gtk_widget_intersect (w
, &rec
, NULL
))
3309 return iter
== 0 ? 0 : 1;
3314 /***********************************************************************
3315 Scroll bar functions
3316 ***********************************************************************/
3319 /* Setting scroll bar values invokes the callback. Use this variable
3320 to indicate that callback should do nothing. */
3322 int xg_ignore_gtk_scrollbar
;
3324 /* The width of the scroll bar for the current theme. */
3326 static int scroll_bar_width_for_theme
;
3328 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3329 may be larger than 32 bits. Keep a mapping from integer index to widget
3330 pointers to get around the 32 bit limitation. */
3334 GtkWidget
**widgets
;
3339 /* Grow this much every time we need to allocate more */
3341 #define ID_TO_WIDGET_INCR 32
3343 /* Store the widget pointer W in id_to_widget and return the integer index. */
3346 xg_store_widget_in_map (GtkWidget
*w
)
3350 if (id_to_widget
.max_size
== id_to_widget
.used
)
3353 if (TYPE_MAXIMUM (Window
) - ID_TO_WIDGET_INCR
< id_to_widget
.max_size
)
3354 memory_full (SIZE_MAX
);
3356 new_size
= id_to_widget
.max_size
+ ID_TO_WIDGET_INCR
;
3357 id_to_widget
.widgets
= xnrealloc (id_to_widget
.widgets
,
3358 new_size
, sizeof (GtkWidget
*));
3360 for (i
= id_to_widget
.max_size
; i
< new_size
; ++i
)
3361 id_to_widget
.widgets
[i
] = 0;
3362 id_to_widget
.max_size
= new_size
;
3365 /* Just loop over the array and find a free place. After all,
3366 how many scroll bars are we creating? Should be a small number.
3367 The check above guarantees we will find a free place. */
3368 for (i
= 0; i
< id_to_widget
.max_size
; ++i
)
3370 if (! id_to_widget
.widgets
[i
])
3372 id_to_widget
.widgets
[i
] = w
;
3373 ++id_to_widget
.used
;
3379 /* Should never end up here */
3383 /* Remove pointer at IDX from id_to_widget.
3384 Called when scroll bar is destroyed. */
3387 xg_remove_widget_from_map (ptrdiff_t idx
)
3389 if (idx
< id_to_widget
.max_size
&& id_to_widget
.widgets
[idx
] != 0)
3391 id_to_widget
.widgets
[idx
] = 0;
3392 --id_to_widget
.used
;
3396 /* Get the widget pointer at IDX from id_to_widget. */
3399 xg_get_widget_from_map (ptrdiff_t idx
)
3401 if (idx
< id_to_widget
.max_size
&& id_to_widget
.widgets
[idx
] != 0)
3402 return id_to_widget
.widgets
[idx
];
3408 update_theme_scrollbar_width (void)
3411 GtkAdjustment
*vadj
;
3418 vadj
= gtk_adjustment_new (XG_SB_MIN
, XG_SB_MIN
, XG_SB_MAX
, 0.1, 0.1, 0.1);
3419 wscroll
= gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL
, GTK_ADJUSTMENT (vadj
));
3420 g_object_ref_sink (G_OBJECT (wscroll
));
3421 gtk_widget_style_get (wscroll
, "slider-width", &w
, "trough-border", &b
, NULL
);
3422 gtk_widget_destroy (wscroll
);
3423 g_object_unref (G_OBJECT (wscroll
));
3426 scroll_bar_width_for_theme
= w
;
3430 xg_get_default_scrollbar_width (void)
3432 return scroll_bar_width_for_theme
;
3435 /* Return the scrollbar id for X Window WID on display DPY.
3436 Return -1 if WID not in id_to_widget. */
3439 xg_get_scroll_id_for_window (Display
*dpy
, Window wid
)
3444 w
= xg_win_to_widget (dpy
, wid
);
3448 for (idx
= 0; idx
< id_to_widget
.max_size
; ++idx
)
3449 if (id_to_widget
.widgets
[idx
] == w
)
3456 /* Callback invoked when scroll bar WIDGET is destroyed.
3457 DATA is the index into id_to_widget for WIDGET.
3458 We free pointer to last scroll bar values here and remove the index. */
3461 xg_gtk_scroll_destroy (GtkWidget
*widget
, gpointer data
)
3463 intptr_t id
= (intptr_t) data
;
3464 xg_remove_widget_from_map (id
);
3467 /* Create a scroll bar widget for frame F. Store the scroll bar
3469 SCROLL_CALLBACK is the callback to invoke when the value of the
3471 END_CALLBACK is the callback to invoke when scrolling ends.
3472 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3473 to set resources for the widget. */
3476 xg_create_scroll_bar (FRAME_PTR f
,
3477 struct scroll_bar
*bar
,
3478 GCallback scroll_callback
,
3479 GCallback end_callback
,
3480 const char *scroll_bar_name
)
3486 GtkAdjustment
*vadj
;
3491 /* Page, step increment values are not so important here, they
3492 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3493 vadj
= gtk_adjustment_new (XG_SB_MIN
, XG_SB_MIN
, XG_SB_MAX
,
3496 wscroll
= gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL
, GTK_ADJUSTMENT (vadj
));
3497 webox
= gtk_event_box_new ();
3498 gtk_widget_set_name (wscroll
, scroll_bar_name
);
3500 gtk_range_set_update_policy (GTK_RANGE (wscroll
), GTK_UPDATE_CONTINUOUS
);
3502 g_object_set_data (G_OBJECT (wscroll
), XG_FRAME_DATA
, (gpointer
)f
);
3504 scroll_id
= xg_store_widget_in_map (wscroll
);
3506 g_signal_connect (G_OBJECT (wscroll
),
3508 G_CALLBACK (xg_gtk_scroll_destroy
),
3509 (gpointer
) scroll_id
);
3510 g_signal_connect (G_OBJECT (wscroll
),
3514 g_signal_connect (G_OBJECT (wscroll
),
3515 "button-release-event",
3519 /* The scroll bar widget does not draw on a window of its own. Instead
3520 it draws on the parent window, in this case the edit widget. So
3521 whenever the edit widget is cleared, the scroll bar needs to redraw
3522 also, which causes flicker. Put an event box between the edit widget
3523 and the scroll bar, so the scroll bar instead draws itself on the
3524 event box window. */
3525 gtk_fixed_put (GTK_FIXED (f
->output_data
.x
->edit_widget
), webox
, -1, -1);
3526 gtk_container_add (GTK_CONTAINER (webox
), wscroll
);
3529 /* Set the cursor to an arrow. */
3530 xg_set_cursor (webox
, FRAME_X_DISPLAY_INFO (f
)->xg_cursor
);
3532 bar
->x_window
= scroll_id
;
3535 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3538 xg_remove_scroll_bar (FRAME_PTR f
, ptrdiff_t scrollbar_id
)
3540 GtkWidget
*w
= xg_get_widget_from_map (scrollbar_id
);
3543 GtkWidget
*wparent
= gtk_widget_get_parent (w
);
3544 gtk_widget_destroy (w
);
3545 gtk_widget_destroy (wparent
);
3546 SET_FRAME_GARBAGED (f
);
3550 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3552 TOP/LEFT are the new pixel positions where the bar shall appear.
3553 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3556 xg_update_scrollbar_pos (FRAME_PTR f
,
3557 ptrdiff_t scrollbar_id
,
3564 GtkWidget
*wscroll
= xg_get_widget_from_map (scrollbar_id
);
3568 GtkWidget
*wfixed
= f
->output_data
.x
->edit_widget
;
3569 GtkWidget
*wparent
= gtk_widget_get_parent (wscroll
);
3572 /* Clear out old position. */
3573 int oldx
= -1, oldy
= -1, oldw
, oldh
;
3574 if (gtk_widget_get_parent (wparent
) == wfixed
)
3576 gtk_container_child_get (GTK_CONTAINER (wfixed
), wparent
,
3577 "x", &oldx
, "y", &oldy
, NULL
);
3578 gtk_widget_get_size_request (wscroll
, &oldw
, &oldh
);
3581 /* Move and resize to new values. */
3582 gtk_fixed_move (GTK_FIXED (wfixed
), wparent
, left
, top
);
3583 gtk_widget_style_get (wscroll
, "min-slider-length", &msl
, NULL
);
3586 /* No room. Hide scroll bar as some themes output a warning if
3587 the height is less than the min size. */
3588 gtk_widget_hide (wparent
);
3589 gtk_widget_hide (wscroll
);
3593 gtk_widget_show_all (wparent
);
3594 gtk_widget_set_size_request (wscroll
, width
, height
);
3596 gtk_widget_queue_draw (wfixed
);
3597 gdk_window_process_all_updates ();
3598 if (oldx
!= -1 && oldw
> 0 && oldh
> 0)
3600 /* Clear under old scroll bar position. This must be done after
3601 the gtk_widget_queue_draw and gdk_window_process_all_updates
3603 x_clear_area (FRAME_X_DISPLAY (f
),
3605 oldx
, oldy
, oldw
, oldh
, 0);
3608 /* GTK does not redraw until the main loop is entered again, but
3609 if there are no X events pending we will not enter it. So we sync
3610 here to get some events. */
3613 SET_FRAME_GARBAGED (f
);
3614 cancel_mouse_face (f
);
3618 /* Get the current value of the range, truncated to an integer. */
3621 int_gtk_range_get_value (GtkRange
*range
)
3623 return gtk_range_get_value (range
);
3627 /* Set the thumb size and position of scroll bar BAR. We are currently
3628 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3631 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar
*bar
,
3636 GtkWidget
*wscroll
= xg_get_widget_from_map (bar
->x_window
);
3638 FRAME_PTR f
= XFRAME (WINDOW_FRAME (XWINDOW (bar
->window
)));
3640 if (wscroll
&& NILP (bar
->dragging
))
3650 adj
= gtk_range_get_adjustment (GTK_RANGE (wscroll
));
3652 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3653 rather than the real portion value. This makes the thumb less likely
3654 to resize and that looks better. */
3655 portion
= WINDOW_TOTAL_LINES (XWINDOW (bar
->window
)) * 30;
3656 /* When the thumb is at the bottom, position == whole.
3657 So we need to increase `whole' to make space for the thumb. */
3664 top
= (gdouble
) position
/ whole
;
3665 shown
= (gdouble
) portion
/ whole
;
3668 size
= shown
* XG_SB_RANGE
;
3669 size
= min (size
, XG_SB_RANGE
);
3670 size
= max (size
, 1);
3672 value
= top
* XG_SB_RANGE
;
3673 value
= min (value
, XG_SB_MAX
- size
);
3674 value
= max (value
, XG_SB_MIN
);
3676 /* Assume all lines are of equal size. */
3677 new_step
= size
/ max (1, FRAME_LINES (f
));
3679 old_size
= gtk_adjustment_get_page_size (adj
);
3680 if (old_size
!= size
)
3682 int old_step
= gtk_adjustment_get_step_increment (adj
);
3683 if (old_step
!= new_step
)
3685 gtk_adjustment_set_page_size (adj
, size
);
3686 gtk_adjustment_set_step_increment (adj
, new_step
);
3687 /* Assume a page increment is about 95% of the page size */
3688 gtk_adjustment_set_page_increment (adj
, size
- size
/ 20);
3693 if (changed
|| int_gtk_range_get_value (GTK_RANGE (wscroll
)) != value
)
3697 /* gtk_range_set_value invokes the callback. Set
3698 ignore_gtk_scrollbar to make the callback do nothing */
3699 xg_ignore_gtk_scrollbar
= 1;
3701 if (int_gtk_range_get_value (GTK_RANGE (wscroll
)) != value
)
3702 gtk_range_set_value (GTK_RANGE (wscroll
), (gdouble
)value
);
3704 gtk_adjustment_changed (adj
);
3706 xg_ignore_gtk_scrollbar
= 0;
3713 /* Return non-zero if EVENT is for a scroll bar in frame F.
3714 When the same X window is used for several Gtk+ widgets, we cannot
3715 say for sure based on the X window alone if an event is for the
3716 frame. This function does additional checks.
3718 Return non-zero if the event is for a scroll bar, zero otherwise. */
3721 xg_event_is_for_scrollbar (FRAME_PTR f
, XEvent
*event
)
3725 if (f
&& event
->type
== ButtonPress
&& event
->xbutton
.button
< 4)
3727 /* Check if press occurred outside the edit widget. */
3728 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
3731 GdkDevice
*gdev
= gdk_device_manager_get_client_pointer
3732 (gdk_display_get_device_manager (gdpy
));
3733 gdk_device_get_window_at_position (gdev
, NULL
, NULL
);
3735 gwin
= gdk_display_get_window_at_pointer (gdpy
, NULL
, NULL
);
3737 retval
= gwin
!= gtk_widget_get_window (f
->output_data
.x
->edit_widget
);
3740 && ((event
->type
== ButtonRelease
&& event
->xbutton
.button
< 4)
3741 || event
->type
== MotionNotify
))
3743 /* If we are releasing or moving the scroll bar, it has the grab. */
3744 GtkWidget
*w
= gtk_grab_get_current ();
3745 retval
= w
!= 0 && GTK_IS_SCROLLBAR (w
);
3753 /***********************************************************************
3755 ***********************************************************************/
3756 /* The key for the data we put in the GtkImage widgets. The data is
3757 the image used by Emacs. We use this to see if we need to update
3758 the GtkImage with a new image. */
3759 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3761 /* The key for storing the latest modifiers so the activate callback can
3763 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3765 /* The key for storing the button widget in its proxy menu item. */
3766 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3768 /* The key for the data we put in the GtkImage widgets. The data is
3769 the stock name used by Emacs. We use this to see if we need to update
3770 the GtkImage with a new image. */
3771 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3773 /* As above, but this is used for named theme widgets, as opposed to
3775 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3777 /* Callback function invoked when a tool bar item is pressed.
3778 W is the button widget in the tool bar that got pressed,
3779 CLIENT_DATA is an integer that is the index of the button in the
3780 tool bar. 0 is the first button. */
3783 xg_tool_bar_button_cb (GtkWidget
*widget
,
3784 GdkEventButton
*event
,
3787 intptr_t state
= event
->state
;
3788 gpointer ptr
= (gpointer
) state
;
3789 g_object_set_data (G_OBJECT (widget
), XG_TOOL_BAR_LAST_MODIFIER
, ptr
);
3794 /* Callback function invoked when a tool bar item is pressed.
3795 W is the button widget in the tool bar that got pressed,
3796 CLIENT_DATA is an integer that is the index of the button in the
3797 tool bar. 0 is the first button. */
3800 xg_tool_bar_callback (GtkWidget
*w
, gpointer client_data
)
3802 intptr_t idx
= (intptr_t) client_data
;
3803 gpointer gmod
= g_object_get_data (G_OBJECT (w
), XG_TOOL_BAR_LAST_MODIFIER
);
3804 intptr_t mod
= (intptr_t) gmod
;
3806 FRAME_PTR f
= (FRAME_PTR
) g_object_get_data (G_OBJECT (w
), XG_FRAME_DATA
);
3807 Lisp_Object key
, frame
;
3808 struct input_event event
;
3811 if (! f
|| ! f
->n_tool_bar_items
|| NILP (f
->tool_bar_items
))
3814 idx
*= TOOL_BAR_ITEM_NSLOTS
;
3816 key
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_KEY
);
3817 XSETFRAME (frame
, f
);
3819 /* We generate two events here. The first one is to set the prefix
3820 to `(tool_bar)', see keyboard.c. */
3821 event
.kind
= TOOL_BAR_EVENT
;
3822 event
.frame_or_window
= frame
;
3824 kbd_buffer_store_event (&event
);
3826 event
.kind
= TOOL_BAR_EVENT
;
3827 event
.frame_or_window
= frame
;
3829 /* Convert between the modifier bits GDK uses and the modifier bits
3830 Emacs uses. This assumes GDK and X masks are the same, which they are when
3832 event
.modifiers
= x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f
), mod
);
3833 kbd_buffer_store_event (&event
);
3835 /* Return focus to the frame after we have clicked on a detached
3837 Fx_focus_frame (frame
);
3840 /* Callback function invoked when a tool bar item is pressed in a detached
3841 tool bar or the overflow drop down menu.
3842 We just call xg_tool_bar_callback.
3843 W is the menu item widget that got pressed,
3844 CLIENT_DATA is an integer that is the index of the button in the
3845 tool bar. 0 is the first button. */
3848 xg_tool_bar_proxy_callback (GtkWidget
*w
, gpointer client_data
)
3850 GtkWidget
*wbutton
= GTK_WIDGET (g_object_get_data (G_OBJECT (w
),
3851 XG_TOOL_BAR_PROXY_BUTTON
));
3852 xg_tool_bar_callback (wbutton
, client_data
);
3857 xg_tool_bar_help_callback (GtkWidget
*w
,
3858 GdkEventCrossing
*event
,
3859 gpointer client_data
);
3861 /* This callback is called when a help is to be shown for an item in
3862 the detached tool bar when the detached tool bar it is not expanded. */
3865 xg_tool_bar_proxy_help_callback (GtkWidget
*w
,
3866 GdkEventCrossing
*event
,
3867 gpointer client_data
)
3869 GtkWidget
*wbutton
= GTK_WIDGET (g_object_get_data (G_OBJECT (w
),
3870 XG_TOOL_BAR_PROXY_BUTTON
));
3872 return xg_tool_bar_help_callback (wbutton
, event
, client_data
);
3876 xg_get_tool_bar_widgets (GtkWidget
*vb
, GtkWidget
**wimage
)
3878 GList
*clist
= gtk_container_get_children (GTK_CONTAINER (vb
));
3879 GtkWidget
*c1
= (GtkWidget
*) clist
->data
;
3880 GtkWidget
*c2
= clist
->next
? (GtkWidget
*) clist
->next
->data
: NULL
;
3882 *wimage
= GTK_IS_IMAGE (c1
) ? c1
: c2
;
3883 g_list_free (clist
);
3884 return GTK_IS_LABEL (c1
) ? c1
: c2
;
3888 /* This callback is called when a tool item should create a proxy item,
3889 such as for the overflow menu. Also called when the tool bar is detached.
3890 If we don't create a proxy menu item, the detached tool bar will be
3894 xg_tool_bar_menu_proxy (GtkToolItem
*toolitem
, gpointer user_data
)
3896 GtkButton
*wbutton
= GTK_BUTTON (XG_BIN_CHILD (XG_BIN_CHILD (toolitem
)));
3897 GtkWidget
*vb
= XG_BIN_CHILD (wbutton
);
3899 GtkLabel
*wlbl
= GTK_LABEL (xg_get_tool_bar_widgets (vb
, &c1
));
3900 GtkImage
*wimage
= GTK_IMAGE (c1
);
3901 GtkWidget
*wmenuitem
= gtk_image_menu_item_new_with_label
3902 (wlbl
? gtk_label_get_text (wlbl
) : "");
3903 GtkWidget
*wmenuimage
;
3906 if (gtk_button_get_use_stock (wbutton
))
3907 wmenuimage
= gtk_image_new_from_stock (gtk_button_get_label (wbutton
),
3908 GTK_ICON_SIZE_MENU
);
3911 GtkSettings
*settings
= gtk_widget_get_settings (GTK_WIDGET (wbutton
));
3912 GtkImageType store_type
= gtk_image_get_storage_type (wimage
);
3914 g_object_set (G_OBJECT (settings
), "gtk-menu-images", TRUE
, NULL
);
3916 if (store_type
== GTK_IMAGE_STOCK
)
3919 gtk_image_get_stock (wimage
, &stock_id
, NULL
);
3920 wmenuimage
= gtk_image_new_from_stock (stock_id
, GTK_ICON_SIZE_MENU
);
3922 else if (store_type
== GTK_IMAGE_ICON_SET
)
3924 GtkIconSet
*icon_set
;
3925 gtk_image_get_icon_set (wimage
, &icon_set
, NULL
);
3926 wmenuimage
= gtk_image_new_from_icon_set (icon_set
,
3927 GTK_ICON_SIZE_MENU
);
3929 else if (store_type
== GTK_IMAGE_PIXBUF
)
3934 gtk_icon_size_lookup_for_settings (settings
, GTK_ICON_SIZE_MENU
,
3937 GdkPixbuf
*src_pixbuf
, *dest_pixbuf
;
3939 src_pixbuf
= gtk_image_get_pixbuf (wimage
);
3940 dest_pixbuf
= gdk_pixbuf_scale_simple (src_pixbuf
, width
, height
,
3941 GDK_INTERP_BILINEAR
);
3943 wmenuimage
= gtk_image_new_from_pixbuf (dest_pixbuf
);
3947 fprintf (stderr
, "internal error: GTK_IMAGE_PIXBUF failed\n");
3951 else if (store_type
== GTK_IMAGE_ICON_NAME
)
3953 const gchar
*icon_name
;
3954 GtkIconSize icon_size
;
3956 gtk_image_get_icon_name (wimage
, &icon_name
, &icon_size
);
3957 wmenuimage
= gtk_image_new_from_icon_name (icon_name
,
3958 GTK_ICON_SIZE_MENU
);
3962 fprintf (stderr
, "internal error: store_type is %d\n", store_type
);
3967 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem
), wmenuimage
);
3969 g_signal_connect (G_OBJECT (wmenuitem
),
3971 G_CALLBACK (xg_tool_bar_proxy_callback
),
3975 g_object_set_data (G_OBJECT (wmenuitem
), XG_TOOL_BAR_PROXY_BUTTON
,
3976 (gpointer
) wbutton
);
3977 gtk_tool_item_set_proxy_menu_item (toolitem
, "Emacs toolbar item", wmenuitem
);
3978 gtk_widget_set_sensitive (wmenuitem
,
3979 gtk_widget_get_sensitive (GTK_WIDGET (wbutton
)));
3981 /* Use enter/leave notify to show help. We use the events
3982 rather than the GtkButton specific signals "enter" and
3983 "leave", so we can have only one callback. The event
3984 will tell us what kind of event it is. */
3985 g_signal_connect (G_OBJECT (wmenuitem
),
3986 "enter-notify-event",
3987 G_CALLBACK (xg_tool_bar_proxy_help_callback
),
3989 g_signal_connect (G_OBJECT (wmenuitem
),
3990 "leave-notify-event",
3991 G_CALLBACK (xg_tool_bar_proxy_help_callback
),
3997 /* This callback is called when a tool bar is detached. We must set
3998 the height of the tool bar to zero when this happens so frame sizes
3999 are correctly calculated.
4000 WBOX is the handle box widget that enables detach/attach of the tool bar.
4001 W is the tool bar widget.
4002 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
4005 xg_tool_bar_detach_callback (GtkHandleBox
*wbox
,
4007 gpointer client_data
)
4009 FRAME_PTR f
= (FRAME_PTR
) client_data
;
4011 g_object_set (G_OBJECT (w
), "show-arrow", !x_gtk_whole_detached_tool_bar
,
4016 GtkRequisition req
, req2
;
4017 FRAME_X_OUTPUT (f
)->toolbar_detached
= 1;
4018 gtk_widget_get_preferred_size (GTK_WIDGET (wbox
), NULL
, &req
);
4019 gtk_widget_get_preferred_size (w
, NULL
, &req2
);
4020 req
.width
-= req2
.width
;
4021 req
.height
-= req2
.height
;
4022 if (FRAME_TOOLBAR_TOP_HEIGHT (f
) != 0)
4023 FRAME_TOOLBAR_TOP_HEIGHT (f
) = req
.height
;
4024 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) != 0)
4025 FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = req
.height
;
4026 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f
) != 0)
4027 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = req
.width
;
4028 else if (FRAME_TOOLBAR_LEFT_WIDTH (f
) != 0)
4029 FRAME_TOOLBAR_LEFT_WIDTH (f
) = req
.width
;
4030 xg_height_or_width_changed (f
);
4034 /* This callback is called when a tool bar is reattached. We must set
4035 the height of the tool bar when this happens so frame sizes
4036 are correctly calculated.
4037 WBOX is the handle box widget that enables detach/attach of the tool bar.
4038 W is the tool bar widget.
4039 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
4042 xg_tool_bar_attach_callback (GtkHandleBox
*wbox
,
4044 gpointer client_data
)
4046 FRAME_PTR f
= (FRAME_PTR
) client_data
;
4047 g_object_set (G_OBJECT (w
), "show-arrow", TRUE
, NULL
);
4051 GtkRequisition req
, req2
;
4052 FRAME_X_OUTPUT (f
)->toolbar_detached
= 0;
4053 gtk_widget_get_preferred_size (GTK_WIDGET (wbox
), NULL
, &req
);
4054 gtk_widget_get_preferred_size (w
, NULL
, &req2
);
4055 req
.width
+= req2
.width
;
4056 req
.height
+= req2
.height
;
4057 if (FRAME_TOOLBAR_TOP_HEIGHT (f
) != 0)
4058 FRAME_TOOLBAR_TOP_HEIGHT (f
) = req
.height
;
4059 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) != 0)
4060 FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = req
.height
;
4061 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f
) != 0)
4062 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = req
.width
;
4063 else if (FRAME_TOOLBAR_LEFT_WIDTH (f
) != 0)
4064 FRAME_TOOLBAR_LEFT_WIDTH (f
) = req
.width
;
4065 xg_height_or_width_changed (f
);
4069 /* This callback is called when the mouse enters or leaves a tool bar item.
4070 It is used for displaying and hiding the help text.
4071 W is the tool bar item, a button.
4072 EVENT is either an enter event or leave event.
4073 CLIENT_DATA is an integer that is the index of the button in the
4074 tool bar. 0 is the first button.
4076 Returns FALSE to tell GTK to keep processing this event. */
4079 xg_tool_bar_help_callback (GtkWidget
*w
,
4080 GdkEventCrossing
*event
,
4081 gpointer client_data
)
4083 intptr_t idx
= (intptr_t) client_data
;
4084 FRAME_PTR f
= (FRAME_PTR
) g_object_get_data (G_OBJECT (w
), XG_FRAME_DATA
);
4085 Lisp_Object help
, frame
;
4087 if (! f
|| ! f
->n_tool_bar_items
|| NILP (f
->tool_bar_items
))
4090 if (event
->type
== GDK_ENTER_NOTIFY
)
4092 idx
*= TOOL_BAR_ITEM_NSLOTS
;
4093 help
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_HELP
);
4096 help
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_CAPTION
);
4101 XSETFRAME (frame
, f
);
4102 kbd_buffer_store_help_event (frame
, help
);
4108 /* This callback is called when a tool bar item shall be redrawn.
4109 It modifies the expose event so that the GtkImage widget redraws the
4110 whole image. This to overcome a bug that makes GtkImage draw the image
4111 in the wrong place when it tries to redraw just a part of the image.
4112 W is the GtkImage to be redrawn.
4113 EVENT is the expose event for W.
4114 CLIENT_DATA is unused.
4116 Returns FALSE to tell GTK to keep processing this event. */
4120 xg_tool_bar_item_expose_callback (GtkWidget
*w
,
4121 GdkEventExpose
*event
,
4122 gpointer client_data
)
4126 gdk_drawable_get_size (event
->window
, &width
, &height
);
4127 event
->area
.x
-= width
> event
->area
.width
? width
-event
->area
.width
: 0;
4128 event
->area
.y
-= height
> event
->area
.height
? height
-event
->area
.height
: 0;
4130 event
->area
.x
= max (0, event
->area
.x
);
4131 event
->area
.y
= max (0, event
->area
.y
);
4133 event
->area
.width
= max (width
, event
->area
.width
);
4134 event
->area
.height
= max (height
, event
->area
.height
);
4140 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4141 #define toolbar_set_orientation(w, o) \
4142 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4144 #define toolbar_set_orientation(w, o) \
4145 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4148 /* Attach a tool bar to frame F. */
4151 xg_pack_tool_bar (FRAME_PTR f
, Lisp_Object pos
)
4153 struct x_output
*x
= f
->output_data
.x
;
4154 int into_hbox
= EQ (pos
, Qleft
) || EQ (pos
, Qright
);
4156 toolbar_set_orientation (x
->toolbar_widget
,
4158 ? GTK_ORIENTATION_VERTICAL
4159 : GTK_ORIENTATION_HORIZONTAL
);
4160 if (!x
->handlebox_widget
)
4162 x
->handlebox_widget
= gtk_handle_box_new ();
4163 g_signal_connect (G_OBJECT (x
->handlebox_widget
), "child-detached",
4164 G_CALLBACK (xg_tool_bar_detach_callback
), f
);
4165 g_signal_connect (G_OBJECT (x
->handlebox_widget
), "child-attached",
4166 G_CALLBACK (xg_tool_bar_attach_callback
), f
);
4167 gtk_container_add (GTK_CONTAINER (x
->handlebox_widget
),
4173 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x
->handlebox_widget
),
4175 gtk_box_pack_start (GTK_BOX (x
->hbox_widget
), x
->handlebox_widget
,
4178 if (EQ (pos
, Qleft
))
4179 gtk_box_reorder_child (GTK_BOX (x
->hbox_widget
),
4180 x
->handlebox_widget
,
4182 x
->toolbar_in_hbox
= 1;
4186 int vbox_pos
= x
->menubar_widget
? 1 : 0;
4187 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x
->handlebox_widget
),
4189 gtk_box_pack_start (GTK_BOX (x
->vbox_widget
), x
->handlebox_widget
,
4193 gtk_box_reorder_child (GTK_BOX (x
->vbox_widget
),
4194 x
->handlebox_widget
,
4196 x
->toolbar_in_hbox
= 0;
4200 /* Create a tool bar for frame F. */
4203 xg_create_tool_bar (FRAME_PTR f
)
4205 struct x_output
*x
= f
->output_data
.x
;
4207 x
->toolbar_widget
= gtk_toolbar_new ();
4208 x
->toolbar_detached
= 0;
4210 gtk_widget_set_name (x
->toolbar_widget
, "emacs-toolbar");
4212 gtk_toolbar_set_style (GTK_TOOLBAR (x
->toolbar_widget
), GTK_TOOLBAR_ICONS
);
4213 toolbar_set_orientation (x
->toolbar_widget
, GTK_ORIENTATION_HORIZONTAL
);
4217 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4219 /* Find the right-to-left image named by RTL in the tool bar images for F.
4220 Returns IMAGE if RTL is not found. */
4223 find_rtl_image (FRAME_PTR f
, Lisp_Object image
, Lisp_Object rtl
)
4226 Lisp_Object file
, rtl_name
;
4227 struct gcpro gcpro1
, gcpro2
;
4228 GCPRO2 (file
, rtl_name
);
4230 rtl_name
= Ffile_name_nondirectory (rtl
);
4232 for (i
= 0; i
< f
->n_tool_bar_items
; ++i
)
4234 Lisp_Object rtl_image
= PROP (TOOL_BAR_ITEM_IMAGES
);
4235 if (!NILP (file
= file_for_image (rtl_image
)))
4237 file
= call1 (intern ("file-name-sans-extension"),
4238 Ffile_name_nondirectory (file
));
4239 if (EQ (Fequal (file
, rtl_name
), Qt
))
4250 static GtkToolItem
*
4251 xg_make_tool_item (FRAME_PTR f
,
4253 GtkWidget
**wbutton
,
4255 int i
, int horiz
, int text_image
)
4257 GtkToolItem
*ti
= gtk_tool_item_new ();
4258 GtkWidget
*vb
= gtk_box_new (horiz
4259 ? GTK_ORIENTATION_HORIZONTAL
4260 : GTK_ORIENTATION_VERTICAL
,
4262 GtkWidget
*wb
= gtk_button_new ();
4263 /* The eventbox is here so we can have tooltips on disabled items. */
4264 GtkWidget
*weventbox
= gtk_event_box_new ();
4266 gtk_box_set_homogeneous (GTK_BOX (vb
), FALSE
);
4268 if (wimage
&& !text_image
)
4269 gtk_box_pack_start (GTK_BOX (vb
), wimage
, TRUE
, TRUE
, 0);
4271 gtk_box_pack_start (GTK_BOX (vb
), gtk_label_new (label
), TRUE
, TRUE
, 0);
4272 if (wimage
&& text_image
)
4273 gtk_box_pack_start (GTK_BOX (vb
), wimage
, TRUE
, TRUE
, 0);
4275 gtk_button_set_focus_on_click (GTK_BUTTON (wb
), FALSE
);
4276 gtk_button_set_relief (GTK_BUTTON (wb
), GTK_RELIEF_NONE
);
4277 gtk_container_add (GTK_CONTAINER (wb
), vb
);
4278 gtk_container_add (GTK_CONTAINER (weventbox
), wb
);
4279 gtk_container_add (GTK_CONTAINER (ti
), weventbox
);
4281 if (wimage
|| label
)
4284 gpointer gi
= (gpointer
) ii
;
4286 g_signal_connect (G_OBJECT (ti
), "create-menu-proxy",
4287 G_CALLBACK (xg_tool_bar_menu_proxy
),
4290 g_signal_connect (G_OBJECT (wb
), "clicked",
4291 G_CALLBACK (xg_tool_bar_callback
),
4294 g_object_set_data (G_OBJECT (weventbox
), XG_FRAME_DATA
, (gpointer
)f
);
4297 /* Catch expose events to overcome an annoying redraw bug, see
4298 comment for xg_tool_bar_item_expose_callback. */
4299 g_signal_connect (G_OBJECT (ti
),
4301 G_CALLBACK (xg_tool_bar_item_expose_callback
),
4304 gtk_tool_item_set_homogeneous (ti
, FALSE
);
4306 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
4307 no distinction based on modifiers in the activate callback,
4308 so we have to do it ourselves. */
4309 g_signal_connect (wb
, "button-release-event",
4310 G_CALLBACK (xg_tool_bar_button_cb
),
4313 g_object_set_data (G_OBJECT (wb
), XG_FRAME_DATA
, (gpointer
)f
);
4315 /* Use enter/leave notify to show help. We use the events
4316 rather than the GtkButton specific signals "enter" and
4317 "leave", so we can have only one callback. The event
4318 will tell us what kind of event it is. */
4319 g_signal_connect (G_OBJECT (weventbox
),
4320 "enter-notify-event",
4321 G_CALLBACK (xg_tool_bar_help_callback
),
4323 g_signal_connect (G_OBJECT (weventbox
),
4324 "leave-notify-event",
4325 G_CALLBACK (xg_tool_bar_help_callback
),
4329 if (wbutton
) *wbutton
= wb
;
4335 is_box_type (GtkWidget
*vb
, int is_horizontal
)
4339 if (GTK_IS_BOX (vb
))
4341 GtkOrientation ori
= gtk_orientable_get_orientation (GTK_ORIENTABLE (vb
));
4342 ret
= (ori
== GTK_ORIENTATION_HORIZONTAL
&& is_horizontal
)
4343 || (ori
== GTK_ORIENTATION_VERTICAL
&& ! is_horizontal
);
4347 return is_horizontal
? GTK_IS_VBOX (vb
) : GTK_IS_HBOX (vb
);
4353 xg_tool_item_stale_p (GtkWidget
*wbutton
, const char *stock_name
,
4354 const char *icon_name
, const struct image
*img
,
4355 const char *label
, int horiz
)
4359 GtkWidget
*vb
= XG_BIN_CHILD (wbutton
);
4360 GtkWidget
*wlbl
= xg_get_tool_bar_widgets (vb
, &wimage
);
4362 /* Check if the tool icon matches. */
4363 if (stock_name
&& wimage
)
4365 old
= g_object_get_data (G_OBJECT (wimage
),
4366 XG_TOOL_BAR_STOCK_NAME
);
4367 if (!old
|| strcmp (old
, stock_name
))
4370 else if (icon_name
&& wimage
)
4372 old
= g_object_get_data (G_OBJECT (wimage
),
4373 XG_TOOL_BAR_ICON_NAME
);
4374 if (!old
|| strcmp (old
, icon_name
))
4379 gpointer gold_img
= g_object_get_data (G_OBJECT (wimage
),
4380 XG_TOOL_BAR_IMAGE_DATA
);
4381 Pixmap old_img
= (Pixmap
) gold_img
;
4382 if (old_img
!= img
->pixmap
)
4386 /* Check button configuration and label. */
4387 if (is_box_type (vb
, horiz
)
4388 || (label
? (wlbl
== NULL
) : (wlbl
!= NULL
)))
4391 /* Ensure label is correct. */
4393 gtk_label_set_text (GTK_LABEL (wlbl
), label
);
4398 xg_update_tool_bar_sizes (FRAME_PTR f
)
4400 struct x_output
*x
= f
->output_data
.x
;
4402 int nl
= 0, nr
= 0, nt
= 0, nb
= 0;
4404 gtk_widget_get_preferred_size (GTK_WIDGET (x
->handlebox_widget
), NULL
, &req
);
4405 if (x
->toolbar_in_hbox
)
4408 gtk_container_child_get (GTK_CONTAINER (x
->hbox_widget
),
4409 x
->handlebox_widget
,
4410 "position", &pos
, NULL
);
4411 if (pos
== 0) nl
= req
.width
;
4412 else nr
= req
.width
;
4417 gtk_container_child_get (GTK_CONTAINER (x
->vbox_widget
),
4418 x
->handlebox_widget
,
4419 "position", &pos
, NULL
);
4420 if (pos
== 0 || (pos
== 1 && x
->menubar_widget
)) nt
= req
.height
;
4421 else nb
= req
.height
;
4424 if (nl
!= FRAME_TOOLBAR_LEFT_WIDTH (f
)
4425 || nr
!= FRAME_TOOLBAR_RIGHT_WIDTH (f
)
4426 || nt
!= FRAME_TOOLBAR_TOP_HEIGHT (f
)
4427 || nb
!= FRAME_TOOLBAR_BOTTOM_HEIGHT (f
))
4429 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = FRAME_TOOLBAR_LEFT_WIDTH (f
)
4430 = FRAME_TOOLBAR_TOP_HEIGHT (f
) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = 0;
4431 FRAME_TOOLBAR_LEFT_WIDTH (f
) = nl
;
4432 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = nr
;
4433 FRAME_TOOLBAR_TOP_HEIGHT (f
) = nt
;
4434 FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = nb
;
4442 /* Update the tool bar for frame F. Add new buttons and remove old. */
4445 update_frame_tool_bar (FRAME_PTR f
)
4448 struct x_output
*x
= f
->output_data
.x
;
4449 int hmargin
= 0, vmargin
= 0;
4450 GtkToolbar
*wtoolbar
;
4452 GtkTextDirection dir
;
4453 int pack_tool_bar
= x
->handlebox_widget
== NULL
;
4455 int text_image
, horiz
;
4457 if (! FRAME_GTK_WIDGET (f
))
4462 if (RANGED_INTEGERP (1, Vtool_bar_button_margin
, INT_MAX
))
4464 hmargin
= XFASTINT (Vtool_bar_button_margin
);
4465 vmargin
= XFASTINT (Vtool_bar_button_margin
);
4467 else if (CONSP (Vtool_bar_button_margin
))
4469 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin
), INT_MAX
))
4470 hmargin
= XFASTINT (XCAR (Vtool_bar_button_margin
));
4472 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin
), INT_MAX
))
4473 vmargin
= XFASTINT (XCDR (Vtool_bar_button_margin
));
4476 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4477 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4478 i.e. zero. This means that margins less than
4479 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4480 hmargin
= max (0, hmargin
- DEFAULT_TOOL_BAR_BUTTON_MARGIN
);
4481 vmargin
= max (0, vmargin
- DEFAULT_TOOL_BAR_BUTTON_MARGIN
);
4483 if (! x
->toolbar_widget
)
4484 xg_create_tool_bar (f
);
4486 wtoolbar
= GTK_TOOLBAR (x
->toolbar_widget
);
4487 dir
= gtk_widget_get_direction (GTK_WIDGET (wtoolbar
));
4489 style
= Ftool_bar_get_system_style ();
4490 text_image
= EQ (style
, Qtext_image_horiz
);
4491 horiz
= EQ (style
, Qboth_horiz
) || text_image
;
4493 for (i
= j
= 0; i
< f
->n_tool_bar_items
; ++i
)
4495 int enabled_p
= !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P
));
4496 int selected_p
= !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P
));
4500 struct image
*img
= NULL
;
4502 Lisp_Object stock
= Qnil
;
4503 GtkStockItem stock_item
;
4504 char *stock_name
= NULL
;
4505 char *icon_name
= NULL
;
4507 GtkWidget
*wbutton
= NULL
;
4508 Lisp_Object specified_file
;
4509 int vert_only
= ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY
));
4511 = (EQ (style
, Qimage
) || (vert_only
&& horiz
)) ? NULL
4512 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL
))
4513 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL
))
4516 ti
= gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar
), j
);
4518 /* If this is a separator, use a gtk separator item. */
4519 if (EQ (PROP (TOOL_BAR_ITEM_TYPE
), Qt
))
4521 if (ti
== NULL
|| !GTK_IS_SEPARATOR_TOOL_ITEM (ti
))
4524 gtk_container_remove (GTK_CONTAINER (wtoolbar
),
4526 ti
= gtk_separator_tool_item_new ();
4527 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar
), ti
, j
);
4533 /* Otherwise, the tool-bar item is an ordinary button. */
4535 if (ti
&& GTK_IS_SEPARATOR_TOOL_ITEM (ti
))
4537 gtk_container_remove (GTK_CONTAINER (wtoolbar
), GTK_WIDGET (ti
));
4541 if (ti
) wbutton
= XG_BIN_CHILD (XG_BIN_CHILD (ti
));
4543 /* Ignore invalid image specifications. */
4544 image
= PROP (TOOL_BAR_ITEM_IMAGES
);
4545 if (!valid_image_p (image
))
4548 gtk_container_remove (GTK_CONTAINER (wtoolbar
),
4553 specified_file
= file_for_image (image
);
4554 if (!NILP (specified_file
) && !NILP (Ffboundp (Qx_gtk_map_stock
)))
4555 stock
= call1 (Qx_gtk_map_stock
, specified_file
);
4557 if (STRINGP (stock
))
4559 stock_name
= SSDATA (stock
);
4560 if (stock_name
[0] == 'n' && stock_name
[1] == ':')
4562 GdkScreen
*screen
= gtk_widget_get_screen (GTK_WIDGET (wtoolbar
));
4563 GtkIconTheme
*icon_theme
= gtk_icon_theme_get_for_screen (screen
);
4565 icon_name
= stock_name
+ 2;
4569 if (! gtk_icon_theme_has_icon (icon_theme
, icon_name
))
4572 icon_size
= gtk_toolbar_get_icon_size (wtoolbar
);
4574 else if (gtk_stock_lookup (SSDATA (stock
), &stock_item
))
4575 icon_size
= gtk_toolbar_get_icon_size (wtoolbar
);
4583 if (stock_name
== NULL
&& icon_name
== NULL
)
4585 /* No stock image, or stock item not known. Try regular
4586 image. If image is a vector, choose it according to the
4588 if (dir
== GTK_TEXT_DIR_RTL
4589 && !NILP (rtl
= PROP (TOOL_BAR_ITEM_RTL_IMAGE
))
4591 image
= find_rtl_image (f
, image
, rtl
);
4593 if (VECTORP (image
))
4597 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4598 : TOOL_BAR_IMAGE_ENABLED_DESELECTED
);
4601 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4602 : TOOL_BAR_IMAGE_DISABLED_DESELECTED
);
4604 eassert (ASIZE (image
) >= idx
);
4605 image
= AREF (image
, idx
);
4610 img_id
= lookup_image (f
, image
);
4611 img
= IMAGE_FROM_ID (f
, img_id
);
4612 prepare_image_for_display (f
, img
);
4614 if (img
->load_failed_p
|| img
->pixmap
== None
)
4617 gtk_container_remove (GTK_CONTAINER (wtoolbar
),
4623 /* If there is an existing widget, check if it's stale; if so,
4624 remove it and make a new tool item from scratch. */
4625 if (ti
&& xg_tool_item_stale_p (wbutton
, stock_name
, icon_name
,
4628 gtk_container_remove (GTK_CONTAINER (wtoolbar
),
4637 /* Save the image so we can see if an update is needed the
4638 next time we call xg_tool_item_match_p. */
4639 if (EQ (style
, Qtext
))
4641 else if (stock_name
)
4643 w
= gtk_image_new_from_stock (stock_name
, icon_size
);
4644 g_object_set_data_full (G_OBJECT (w
), XG_TOOL_BAR_STOCK_NAME
,
4645 (gpointer
) xstrdup (stock_name
),
4646 (GDestroyNotify
) xfree
);
4650 w
= gtk_image_new_from_icon_name (icon_name
, icon_size
);
4651 g_object_set_data_full (G_OBJECT (w
), XG_TOOL_BAR_ICON_NAME
,
4652 (gpointer
) xstrdup (icon_name
),
4653 (GDestroyNotify
) xfree
);
4657 w
= xg_get_image_for_pixmap (f
, img
, x
->widget
, NULL
);
4658 g_object_set_data (G_OBJECT (w
), XG_TOOL_BAR_IMAGE_DATA
,
4659 (gpointer
)img
->pixmap
);
4662 if (w
) gtk_misc_set_padding (GTK_MISC (w
), hmargin
, vmargin
);
4663 ti
= xg_make_tool_item (f
, w
, &wbutton
, label
, i
, horiz
, text_image
);
4664 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar
), ti
, j
);
4669 gtk_widget_set_sensitive (wbutton
, enabled_p
);
4673 /* Remove buttons not longer needed. */
4676 ti
= gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar
), j
);
4678 gtk_container_remove (GTK_CONTAINER (wtoolbar
), GTK_WIDGET (ti
));
4679 } while (ti
!= NULL
);
4681 if (f
->n_tool_bar_items
!= 0)
4684 xg_pack_tool_bar (f
, f
->tool_bar_position
);
4685 gtk_widget_show_all (GTK_WIDGET (x
->handlebox_widget
));
4686 if (xg_update_tool_bar_sizes (f
))
4687 xg_height_or_width_changed (f
);
4693 /* Deallocate all resources for the tool bar on frame F.
4694 Remove the tool bar. */
4697 free_frame_tool_bar (FRAME_PTR f
)
4699 struct x_output
*x
= f
->output_data
.x
;
4701 if (x
->toolbar_widget
)
4703 int is_packed
= x
->handlebox_widget
!= 0;
4705 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4706 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4709 if (x
->toolbar_in_hbox
)
4710 gtk_container_remove (GTK_CONTAINER (x
->hbox_widget
),
4711 x
->handlebox_widget
);
4713 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
),
4714 x
->handlebox_widget
);
4717 gtk_widget_destroy (x
->toolbar_widget
);
4719 x
->toolbar_widget
= 0;
4720 x
->handlebox_widget
= 0;
4721 FRAME_TOOLBAR_TOP_HEIGHT (f
) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = 0;
4722 FRAME_TOOLBAR_LEFT_WIDTH (f
) = FRAME_TOOLBAR_RIGHT_WIDTH (f
) = 0;
4724 xg_height_or_width_changed (f
);
4731 xg_change_toolbar_position (FRAME_PTR f
, Lisp_Object pos
)
4733 struct x_output
*x
= f
->output_data
.x
;
4735 if (! x
->toolbar_widget
|| ! x
->handlebox_widget
)
4739 g_object_ref (x
->handlebox_widget
);
4740 if (x
->toolbar_in_hbox
)
4741 gtk_container_remove (GTK_CONTAINER (x
->hbox_widget
),
4742 x
->handlebox_widget
);
4744 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
),
4745 x
->handlebox_widget
);
4746 xg_pack_tool_bar (f
, pos
);
4747 g_object_unref (x
->handlebox_widget
);
4748 if (xg_update_tool_bar_sizes (f
))
4749 xg_height_or_width_changed (f
);
4757 /***********************************************************************
4759 ***********************************************************************/
4761 xg_initialize (void)
4763 GtkBindingSet
*binding_set
;
4764 GtkSettings
*settings
;
4767 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4768 we keep it permanently linked in. */
4773 xg_ignore_gtk_scrollbar
= 0;
4774 xg_detached_menus
= 0;
4775 xg_menu_cb_list
.prev
= xg_menu_cb_list
.next
=
4776 xg_menu_item_cb_list
.prev
= xg_menu_item_cb_list
.next
= 0;
4778 id_to_widget
.max_size
= id_to_widget
.used
= 0;
4779 id_to_widget
.widgets
= 0;
4781 settings
= gtk_settings_get_for_screen (gdk_display_get_default_screen
4782 (gdk_display_get_default ()));
4783 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4784 bindings. It doesn't seem to be any way to remove properties,
4785 so we set it to VoidSymbol which in X means "no key". */
4786 gtk_settings_set_string_property (settings
,
4787 "gtk-menu-bar-accel",
4791 /* Make GTK text input widgets use Emacs style keybindings. This is
4793 gtk_settings_set_string_property (settings
,
4794 "gtk-key-theme-name",
4798 /* Make dialogs close on C-g. Since file dialog inherits from
4799 dialog, this works for them also. */
4800 binding_set
= gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG
));
4801 gtk_binding_entry_add_signal (binding_set
, GDK_KEY_g
, GDK_CONTROL_MASK
,
4804 /* Make menus close on C-g. */
4805 binding_set
= gtk_binding_set_by_class (g_type_class_ref
4806 (GTK_TYPE_MENU_SHELL
));
4807 gtk_binding_entry_add_signal (binding_set
, GDK_KEY_g
, GDK_CONTROL_MASK
,
4809 update_theme_scrollbar_width ();
4812 #endif /* USE_GTK */