1 /* Functions for creating and updating GTK widgets.
3 Copyright (C) 2003-2011 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/>. */
28 #include "blockinput.h"
29 #include "syssignal.h"
32 #include "termhooks.h"
36 #include <gdk/gdkkeysyms.h>
37 #include "xsettings.h"
40 #include <X11/Xft/Xft.h>
45 #include "emacsgtkfixed.h"
48 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
49 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
51 #define FRAME_TOTAL_PIXEL_WIDTH(f) \
52 (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f))
54 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
55 #define gtk_widget_set_has_window(w, b) \
56 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
58 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
59 #define gtk_dialog_get_action_area(w) ((w)->action_area)
60 #define gtk_dialog_get_content_area(w) ((w)->vbox)
62 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
63 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
65 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
66 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
67 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
68 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
69 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
71 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
72 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
74 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
78 #ifdef USE_GTK_TOOLTIP
79 #define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
81 #define gdk_window_get_geometry(w, a, b, c, d) \
82 gdk_window_get_geometry (w, a, b, c, d, 0)
83 #define gdk_x11_window_lookup_for_display(d, w) \
84 gdk_xid_table_lookup_for_display (d, w)
86 #define GDK_KEY_g GDK_g
90 #define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
92 static void update_theme_scrollbar_width (void);
95 /***********************************************************************
96 Display handling functions
97 ***********************************************************************/
99 /* Keep track of the default display, or NULL if there is none. Emacs
100 may close all its displays. */
102 static GdkDisplay
*gdpy_def
;
104 /* When the GTK widget W is to be created on a display for F that
105 is not the default display, set the display for W.
106 W can be a GtkMenu or a GtkWindow widget. */
109 xg_set_screen (GtkWidget
*w
, FRAME_PTR f
)
111 if (FRAME_X_DISPLAY (f
) != DEFAULT_GDK_DISPLAY ())
113 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
114 GdkScreen
*gscreen
= gdk_display_get_default_screen (gdpy
);
117 gtk_menu_set_screen (GTK_MENU (w
), gscreen
);
119 gtk_window_set_screen (GTK_WINDOW (w
), gscreen
);
124 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
125 *DPY is set to NULL if the display can't be opened.
127 Returns non-zero if display could be opened, zero if display could not
128 be opened, and less than zero if the GTK version doesn't support
132 xg_display_open (char *display_name
, Display
**dpy
)
136 gdpy
= gdk_display_open (display_name
);
137 if (!gdpy_def
&& gdpy
)
140 gdk_display_manager_set_default_display (gdk_display_manager_get (),
144 *dpy
= gdpy
? GDK_DISPLAY_XDISPLAY (gdpy
) : NULL
;
148 /* Close display DPY. */
151 xg_display_close (Display
*dpy
)
153 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (dpy
);
155 /* If this is the default display, try to change it before closing.
156 If there is no other display to use, gdpy_def is set to NULL, and
157 the next call to xg_display_open resets the default display. */
158 if (gdk_display_get_default () == gdpy
)
160 struct x_display_info
*dpyinfo
;
161 GdkDisplay
*gdpy_new
= NULL
;
163 /* Find another display. */
164 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
165 if (dpyinfo
->display
!= dpy
)
167 gdpy_new
= gdk_x11_lookup_xdisplay (dpyinfo
->display
);
168 gdk_display_manager_set_default_display (gdk_display_manager_get (),
175 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
176 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
177 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
178 can continue running, but there will be memory leaks. */
179 g_object_run_dispose (G_OBJECT (gdpy
));
181 /* This seems to be fixed in GTK 2.10. */
182 gdk_display_close (gdpy
);
187 /***********************************************************************
189 ***********************************************************************/
190 /* The next two variables and functions are taken from lwlib. */
191 static widget_value
*widget_value_free_list
;
192 static int malloc_cpt
;
194 /* Allocate a widget_value structure, either by taking one from the
195 widget_value_free_list or by malloc:ing a new one.
197 Return a pointer to the allocated structure. */
200 malloc_widget_value (void)
203 if (widget_value_free_list
)
205 wv
= widget_value_free_list
;
206 widget_value_free_list
= wv
->free_list
;
211 wv
= (widget_value
*) xmalloc (sizeof (widget_value
));
214 memset (wv
, 0, sizeof (widget_value
));
218 /* This is analogous to free. It frees only what was allocated
219 by malloc_widget_value, and no substructures. */
222 free_widget_value (widget_value
*wv
)
229 /* When the number of already allocated cells is too big,
236 wv
->free_list
= widget_value_free_list
;
237 widget_value_free_list
= wv
;
242 /* Create and return the cursor to be used for popup menus and
243 scroll bars on display DPY. */
246 xg_create_default_cursor (Display
*dpy
)
248 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (dpy
);
249 return gdk_cursor_new_for_display (gdpy
, GDK_LEFT_PTR
);
253 xg_get_pixbuf_from_pixmap (FRAME_PTR f
, Pixmap pix
)
258 unsigned int width
, height
, uunused
;
261 XGetGeometry (FRAME_X_DISPLAY (f
), pix
, &wunused
, &iunused
, &iunused
,
262 &width
, &height
, &uunused
, &uunused
);
264 xim
= XGetImage (FRAME_X_DISPLAY (f
), pix
, 0, 0, width
, height
,
268 tmp_buf
= gdk_pixbuf_new_from_data ((guchar
*) xim
->data
,
281 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
284 xg_get_pixbuf_from_pix_and_mask (FRAME_PTR f
,
289 GdkPixbuf
*icon_buf
, *tmp_buf
;
291 tmp_buf
= xg_get_pixbuf_from_pixmap (f
, pix
);
292 icon_buf
= gdk_pixbuf_add_alpha (tmp_buf
, FALSE
, 0, 0, 0);
293 g_object_unref (G_OBJECT (tmp_buf
));
295 width
= gdk_pixbuf_get_width (icon_buf
);
296 height
= gdk_pixbuf_get_height (icon_buf
);
300 GdkPixbuf
*mask_buf
= xg_get_pixbuf_from_pixmap (f
, mask
);
301 guchar
*pixels
= gdk_pixbuf_get_pixels (icon_buf
);
302 guchar
*mask_pixels
= gdk_pixbuf_get_pixels (mask_buf
);
303 int rowstride
= gdk_pixbuf_get_rowstride (icon_buf
);
304 int mask_rowstride
= gdk_pixbuf_get_rowstride (mask_buf
);
307 for (y
= 0; y
< height
; ++y
)
309 guchar
*iconptr
, *maskptr
;
312 iconptr
= pixels
+ y
* rowstride
;
313 maskptr
= mask_pixels
+ y
* mask_rowstride
;
315 for (x
= 0; x
< width
; ++x
)
317 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
318 just R is sufficient. */
320 iconptr
[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
322 iconptr
+= rowstride
/width
;
323 maskptr
+= mask_rowstride
/width
;
327 g_object_unref (G_OBJECT (mask_buf
));
334 file_for_image (Lisp_Object image
)
336 Lisp_Object specified_file
= Qnil
;
339 for (tail
= XCDR (image
);
340 NILP (specified_file
) && CONSP (tail
) && CONSP (XCDR (tail
));
341 tail
= XCDR (XCDR (tail
)))
342 if (EQ (XCAR (tail
), QCfile
))
343 specified_file
= XCAR (XCDR (tail
));
345 return specified_file
;
348 /* For the image defined in IMG, make and return a GtkImage. For displays with
349 8 planes or less we must make a GdkPixbuf and apply the mask manually.
350 Otherwise the highlightning and dimming the tool bar code in GTK does
351 will look bad. For display with more than 8 planes we just use the
352 pixmap and mask directly. For monochrome displays, GTK doesn't seem
353 able to use external pixmaps, it looks bad whatever we do.
354 The image is defined on the display where frame F is.
355 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
356 If OLD_WIDGET is NULL, a new widget is constructed and returned.
357 If OLD_WIDGET is not NULL, that widget is modified. */
360 xg_get_image_for_pixmap (FRAME_PTR f
,
363 GtkImage
*old_widget
)
367 /* If we have a file, let GTK do all the image handling.
368 This seems to be the only way to make insensitive and activated icons
369 look good in all cases. */
370 Lisp_Object specified_file
= file_for_image (img
->spec
);
373 /* We already loaded the image once before calling this
374 function, so this only fails if the image file has been removed.
375 In that case, use the pixmap already loaded. */
377 if (STRINGP (specified_file
)
378 && STRINGP (file
= x_find_image_file (specified_file
)))
381 old_widget
= GTK_IMAGE (gtk_image_new_from_file (SSDATA (file
)));
383 gtk_image_set_from_file (old_widget
, SSDATA (file
));
385 return GTK_WIDGET (old_widget
);
388 /* No file, do the image handling ourselves. This will look very bad
389 on a monochrome display, and sometimes bad on all displays with
392 /* This is a workaround to make icons look good on pseudo color
393 displays. Apparently GTK expects the images to have an alpha
394 channel. If they don't, insensitive and activated icons will
395 look bad. This workaround does not work on monochrome displays,
396 and is strictly not needed on true color/static color displays (i.e.
397 16 bits and higher). But we do it anyway so we get a pixbuf that is
398 not associated with the img->pixmap. The img->pixmap may be removed
399 by clearing the image cache and then the tool bar redraw fails, since
400 Gtk+ assumes the pixmap is always there. */
401 icon_buf
= xg_get_pixbuf_from_pix_and_mask (f
, img
->pixmap
, img
->mask
);
406 old_widget
= GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf
));
408 gtk_image_set_from_pixbuf (old_widget
, icon_buf
);
410 g_object_unref (G_OBJECT (icon_buf
));
413 return GTK_WIDGET (old_widget
);
417 /* Set CURSOR on W and all widgets W contain. We must do like this
418 for scroll bars and menu because they create widgets internally,
419 and it is those widgets that are visible. */
422 xg_set_cursor (GtkWidget
*w
, GdkCursor
*cursor
)
424 GdkWindow
*window
= gtk_widget_get_window(w
);
425 GList
*children
= gdk_window_peek_children (window
);
427 gdk_window_set_cursor (window
, cursor
);
429 /* The scroll bar widget has more than one GDK window (had to look at
430 the source to figure this out), and there is no way to set cursor
431 on widgets in GTK. So we must set the cursor for all GDK windows.
434 for ( ; children
; children
= g_list_next (children
))
435 gdk_window_set_cursor (GDK_WINDOW (children
->data
), cursor
);
438 /* Insert NODE into linked LIST. */
441 xg_list_insert (xg_list_node
*list
, xg_list_node
*node
)
443 xg_list_node
*list_start
= list
->next
;
445 if (list_start
) list_start
->prev
= node
;
446 node
->next
= list_start
;
451 /* Remove NODE from linked LIST. */
454 xg_list_remove (xg_list_node
*list
, xg_list_node
*node
)
456 xg_list_node
*list_start
= list
->next
;
457 if (node
== list_start
)
459 list
->next
= node
->next
;
460 if (list
->next
) list
->next
->prev
= 0;
464 node
->prev
->next
= node
->next
;
465 if (node
->next
) node
->next
->prev
= node
->prev
;
469 /* Allocate and return a utf8 version of STR. If STR is already
470 utf8 or NULL, just return a copy of STR.
471 A new string is allocated and the caller must free the result
475 get_utf8_string (const char *str
)
479 if (!str
) return NULL
;
481 /* If not UTF-8, try current locale. */
482 if (!g_utf8_validate (str
, -1, NULL
))
483 utf8_str
= g_locale_to_utf8 (str
, -1, 0, 0, 0);
485 return g_strdup (str
);
489 /* Probably some control characters in str. Escape them. */
493 unsigned char *p
= (unsigned char *)str
;
497 while (! (cp
= g_locale_to_utf8 ((char *)p
, -1, &bytes_read
,
498 &bytes_written
, &err
))
499 && err
->code
== G_CONVERT_ERROR_ILLEGAL_SEQUENCE
)
502 p
+= bytes_written
+1;
514 up
= utf8_str
= xmalloc (strlen (str
) + nr_bad
* 4 + 1);
515 p
= (unsigned char *)str
;
517 while (! (cp
= g_locale_to_utf8 ((char *)p
, -1, &bytes_read
,
518 &bytes_written
, &err
))
519 && err
->code
== G_CONVERT_ERROR_ILLEGAL_SEQUENCE
)
521 strncpy (up
, (char *)p
, bytes_written
);
522 sprintf (up
+ bytes_written
, "\\%03o", p
[bytes_written
]);
523 up
[bytes_written
+4] = '\0';
524 up
+= bytes_written
+4;
525 p
+= bytes_written
+1;
532 strcat (utf8_str
, cp
);
544 /* Check for special colors used in face spec for region face.
545 The colors are fetched from the Gtk+ theme.
546 Return 1 if color was found, 0 if not. */
549 xg_check_special_colors (struct frame
*f
,
550 const char *color_name
,
554 int get_bg
= strcmp ("gtk_selection_bg_color", color_name
) == 0;
555 int get_fg
= !get_bg
&& strcmp ("gtk_selection_fg_color", color_name
) == 0;
557 if (! FRAME_GTK_WIDGET (f
) || ! (get_bg
|| get_fg
))
563 GtkStyleContext
*gsty
564 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f
));
567 int state
= GTK_STATE_FLAG_SELECTED
|GTK_STATE_FLAG_FOCUSED
;
569 gtk_style_context_get_color (gsty
, state
, &col
);
571 gtk_style_context_get_background_color (gsty
, state
, &col
);
573 sprintf (buf
, "rgbi:%lf/%lf/%lf", col
.red
, col
.green
, col
.blue
);
574 success_p
= XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
577 GtkStyle
*gsty
= gtk_widget_get_style (FRAME_GTK_WIDGET (f
));
578 GdkColor
*grgb
= get_bg
579 ? &gsty
->bg
[GTK_STATE_SELECTED
]
580 : &gsty
->fg
[GTK_STATE_SELECTED
];
582 color
->red
= grgb
->red
;
583 color
->green
= grgb
->green
;
584 color
->blue
= grgb
->blue
;
585 color
->pixel
= grgb
->pixel
;
596 /***********************************************************************
598 ***********************************************************************/
599 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
600 We use that to pop down the tooltip. This happens if Gtk+ for some
601 reason wants to change or hide the tooltip. */
603 #ifdef USE_GTK_TOOLTIP
606 hierarchy_ch_cb (GtkWidget
*widget
,
607 GtkWidget
*previous_toplevel
,
610 FRAME_PTR f
= (FRAME_PTR
) user_data
;
611 struct x_output
*x
= f
->output_data
.x
;
612 GtkWidget
*top
= gtk_widget_get_toplevel (x
->ttip_lbl
);
614 if (! top
|| ! GTK_IS_WINDOW (top
))
615 gtk_widget_hide (previous_toplevel
);
618 /* Callback called when Gtk+ thinks a tooltip should be displayed.
619 We use it to get the tooltip window and the tooltip widget so
620 we can manipulate the ourselves.
622 Return FALSE ensures that the tooltip is not shown. */
625 qttip_cb (GtkWidget
*widget
,
628 gboolean keyboard_mode
,
632 FRAME_PTR f
= (FRAME_PTR
) user_data
;
633 struct x_output
*x
= f
->output_data
.x
;
634 if (x
->ttip_widget
== NULL
)
639 g_object_set (G_OBJECT (widget
), "has-tooltip", FALSE
, NULL
);
640 x
->ttip_widget
= tooltip
;
641 g_object_ref (G_OBJECT (tooltip
));
642 x
->ttip_lbl
= gtk_label_new ("");
643 g_object_ref (G_OBJECT (x
->ttip_lbl
));
644 gtk_tooltip_set_custom (tooltip
, x
->ttip_lbl
);
645 x
->ttip_window
= GTK_WINDOW (gtk_widget_get_toplevel (x
->ttip_lbl
));
647 /* Change stupid Gtk+ default line wrapping. */
648 p
= gtk_widget_get_parent (x
->ttip_lbl
);
649 list
= gtk_container_get_children (GTK_CONTAINER (p
));
650 for (iter
= list
; iter
; iter
= g_list_next (iter
))
652 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
653 if (GTK_IS_LABEL (w
))
654 gtk_label_set_line_wrap (GTK_LABEL (w
), FALSE
);
658 /* ATK needs an empty title for some reason. */
659 gtk_window_set_title (x
->ttip_window
, "");
660 /* Realize so we can safely get screen later on. */
661 gtk_widget_realize (GTK_WIDGET (x
->ttip_window
));
662 gtk_widget_realize (x
->ttip_lbl
);
664 g_signal_connect (x
->ttip_lbl
, "hierarchy-changed",
665 G_CALLBACK (hierarchy_ch_cb
), f
);
670 #endif /* USE_GTK_TOOLTIP */
672 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
673 Return zero if no system tooltip available, non-zero otherwise. */
676 xg_prepare_tooltip (FRAME_PTR f
,
681 #ifndef USE_GTK_TOOLTIP
684 struct x_output
*x
= f
->output_data
.x
;
688 GtkSettings
*settings
;
689 gboolean tt_enabled
= TRUE
;
691 Lisp_Object encoded_string
;
693 if (!x
->ttip_lbl
) return 0;
696 encoded_string
= ENCODE_UTF_8 (string
);
697 widget
= GTK_WIDGET (x
->ttip_lbl
);
698 gwin
= gtk_widget_get_window (GTK_WIDGET (x
->ttip_window
));
699 screen
= gdk_window_get_screen (gwin
);
700 settings
= gtk_settings_get_for_screen (screen
);
701 g_object_get (settings
, "gtk-enable-tooltips", &tt_enabled
, NULL
);
704 g_object_set (settings
, "gtk-enable-tooltips", FALSE
, NULL
);
705 /* Record that we disabled it so it can be enabled again. */
706 g_object_set_data (G_OBJECT (x
->ttip_window
), "restore-tt",
710 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
711 g_object_set_data (G_OBJECT
712 (gtk_widget_get_display (GTK_WIDGET (x
->ttip_window
))),
713 "gdk-display-current-tooltip", NULL
);
715 /* Put our dummy widget in so we can get callbacks for unrealize and
716 hierarchy-changed. */
717 gtk_tooltip_set_custom (x
->ttip_widget
, widget
);
718 gtk_tooltip_set_text (x
->ttip_widget
, SSDATA (encoded_string
));
719 gtk_widget_get_preferred_size (GTK_WIDGET (x
->ttip_window
), NULL
, &req
);
720 if (width
) *width
= req
.width
;
721 if (height
) *height
= req
.height
;
726 #endif /* USE_GTK_TOOLTIP */
729 /* Show the tooltip at ROOT_X and ROOT_Y.
730 xg_prepare_tooltip must have been called before this function. */
733 xg_show_tooltip (FRAME_PTR f
, int root_x
, int root_y
)
735 #ifdef USE_GTK_TOOLTIP
736 struct x_output
*x
= f
->output_data
.x
;
740 gtk_window_move (x
->ttip_window
, root_x
, root_y
);
741 gtk_widget_show_all (GTK_WIDGET (x
->ttip_window
));
747 /* Hide tooltip if shown. Do nothing if not shown.
748 Return non-zero if tip was hidden, non-zero if not (i.e. not using
752 xg_hide_tooltip (FRAME_PTR f
)
755 #ifdef USE_GTK_TOOLTIP
756 if (f
->output_data
.x
->ttip_window
)
758 GtkWindow
*win
= f
->output_data
.x
->ttip_window
;
760 gtk_widget_hide (GTK_WIDGET (win
));
762 if (g_object_get_data (G_OBJECT (win
), "restore-tt"))
764 GdkWindow
*gwin
= gtk_widget_get_window (GTK_WIDGET (win
));
765 GdkScreen
*screen
= gdk_window_get_screen (gwin
);
766 GtkSettings
*settings
= gtk_settings_get_for_screen (screen
);
767 g_object_set (settings
, "gtk-enable-tooltips", TRUE
, NULL
);
778 /***********************************************************************
779 General functions for creating widgets, resizing, events, e.t.c.
780 ***********************************************************************/
782 /* Make a geometry string and pass that to GTK. It seems this is the
783 only way to get geometry position right if the user explicitly
784 asked for a position when starting Emacs.
785 F is the frame we shall set geometry for. */
788 xg_set_geometry (FRAME_PTR f
)
790 if (f
->size_hint_flags
& (USPosition
| PPosition
))
792 int left
= f
->left_pos
;
793 int xneg
= f
->size_hint_flags
& XNegative
;
794 int top
= f
->top_pos
;
795 int yneg
= f
->size_hint_flags
& YNegative
;
803 sprintf (geom_str
, "=%dx%d%c%d%c%d",
804 FRAME_PIXEL_WIDTH (f
),
805 FRAME_PIXEL_HEIGHT (f
),
806 (xneg
? '-' : '+'), left
,
807 (yneg
? '-' : '+'), top
);
809 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
811 fprintf (stderr
, "Failed to parse: '%s'\n", geom_str
);
815 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
816 and use a GtkFixed widget, this doesn't happen automatically. */
819 xg_clear_under_internal_border (FRAME_PTR f
)
821 if (FRAME_INTERNAL_BORDER_WIDTH (f
) > 0)
823 GtkWidget
*wfixed
= f
->output_data
.x
->edit_widget
;
824 gtk_widget_queue_draw (wfixed
);
825 gdk_window_process_all_updates ();
826 x_clear_area (FRAME_X_DISPLAY (f
),
829 FRAME_PIXEL_WIDTH (f
),
830 FRAME_INTERNAL_BORDER_WIDTH (f
), 0);
831 x_clear_area (FRAME_X_DISPLAY (f
),
834 FRAME_INTERNAL_BORDER_WIDTH (f
),
835 FRAME_PIXEL_HEIGHT (f
), 0);
836 x_clear_area (FRAME_X_DISPLAY (f
),
838 0, FRAME_PIXEL_HEIGHT (f
) - FRAME_INTERNAL_BORDER_WIDTH (f
),
839 FRAME_PIXEL_WIDTH (f
),
840 FRAME_INTERNAL_BORDER_WIDTH (f
), 0);
841 x_clear_area (FRAME_X_DISPLAY (f
),
843 FRAME_PIXEL_WIDTH (f
) - FRAME_INTERNAL_BORDER_WIDTH (f
),
845 FRAME_INTERNAL_BORDER_WIDTH (f
),
846 FRAME_PIXEL_HEIGHT (f
), 0);
850 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
851 and a Gtk+ menu bar, we get resize events for the edit part of the
852 frame only. We let Gtk+ deal with the Gtk+ parts.
853 F is the frame to resize.
854 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
857 xg_frame_resized (FRAME_PTR f
, int pixelwidth
, int pixelheight
)
861 if (pixelwidth
== -1 && pixelheight
== -1)
863 if (FRAME_GTK_WIDGET (f
) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f
)))
864 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f
)),
866 &pixelwidth
, &pixelheight
);
871 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f
, pixelheight
);
872 columns
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f
, pixelwidth
);
874 if (columns
!= FRAME_COLS (f
)
875 || rows
!= FRAME_LINES (f
)
876 || pixelwidth
!= FRAME_PIXEL_WIDTH (f
)
877 || pixelheight
!= FRAME_PIXEL_HEIGHT (f
))
879 FRAME_PIXEL_WIDTH (f
) = pixelwidth
;
880 FRAME_PIXEL_HEIGHT (f
) = pixelheight
;
882 xg_clear_under_internal_border (f
);
883 change_frame_size (f
, rows
, columns
, 0, 1, 0);
884 SET_FRAME_GARBAGED (f
);
885 cancel_mouse_face (f
);
889 /* Resize the outer window of frame F after chainging the height.
890 COLUMNS/ROWS is the size the edit area shall have after the resize. */
893 xg_frame_set_char_size (FRAME_PTR f
, int cols
, int rows
)
895 int pixelheight
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f
, rows
)
896 + FRAME_MENUBAR_HEIGHT (f
) + FRAME_TOOLBAR_HEIGHT (f
);
899 if (FRAME_PIXEL_HEIGHT (f
) == 0)
902 /* Take into account the size of the scroll bar. Always use the
903 number of columns occupied by the scroll bar here otherwise we
904 might end up with a frame width that is not a multiple of the
905 frame's character width which is bad for vertically split
907 f
->scroll_bar_actual_width
908 = FRAME_SCROLL_BAR_COLS (f
) * FRAME_COLUMN_WIDTH (f
);
910 compute_fringe_widths (f
, 0);
912 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
913 after calculating that value. */
914 pixelwidth
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f
, cols
)
915 + FRAME_TOOLBAR_WIDTH (f
);
918 /* Do this before resize, as we don't know yet if we will be resized. */
919 xg_clear_under_internal_border (f
);
921 /* Must resize our top level widget. Font size may have changed,
922 but not rows/cols. */
923 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
924 pixelwidth
, pixelheight
);
925 x_wm_set_size_hint (f
, 0, 0);
927 SET_FRAME_GARBAGED (f
);
928 cancel_mouse_face (f
);
930 /* We can not call change_frame_size for a mapped frame,
931 we can not set pixel width/height either. The window manager may
932 override our resize request, XMonad does this all the time.
933 The best we can do is try to sync, so lisp code sees the updated
934 size as fast as possible.
935 For unmapped windows, we can set rows/cols. When
936 the frame is mapped again we will (hopefully) get the correct size. */
937 if (f
->async_visible
)
939 /* Must call this to flush out events */
940 (void)gtk_events_pending ();
942 x_wait_for_event (f
, ConfigureNotify
);
946 change_frame_size (f
, rows
, cols
, 0, 1, 0);
947 FRAME_PIXEL_WIDTH (f
) = pixelwidth
;
948 FRAME_PIXEL_HEIGHT (f
) = pixelheight
;
952 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
953 The policy is to keep the number of editable lines. */
956 xg_height_or_width_changed (FRAME_PTR f
)
958 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
959 FRAME_TOTAL_PIXEL_WIDTH (f
),
960 FRAME_TOTAL_PIXEL_HEIGHT (f
));
961 f
->output_data
.x
->hint_flags
= 0;
962 x_wm_set_size_hint (f
, 0, 0);
965 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
966 Must be done like this, because GtkWidget:s can have "hidden"
967 X Window that aren't accessible.
969 Return 0 if no widget match WDESC. */
972 xg_win_to_widget (Display
*dpy
, Window wdesc
)
975 GtkWidget
*gwdesc
= 0;
979 gdkwin
= gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy
),
984 event
.any
.window
= gdkwin
;
985 gwdesc
= gtk_get_event_widget (&event
);
992 /* Set the background of widget W to PIXEL. */
995 xg_set_widget_bg (FRAME_PTR f
, GtkWidget
*w
, long unsigned int pixel
)
1001 if (XQueryColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
), &xbg
))
1003 bg
.red
= (double)xbg
.red
/65536.0;
1004 bg
.green
= (double)xbg
.green
/65536.0;
1005 bg
.blue
= (double)xbg
.blue
/65536.0;
1007 gtk_widget_override_background_color (w
, GTK_STATE_FLAG_NORMAL
, &bg
);
1011 GdkColormap
*map
= gtk_widget_get_colormap (w
);
1012 gdk_colormap_query_color (map
, pixel
, &bg
);
1013 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f
), GTK_STATE_NORMAL
, &bg
);
1017 /* Callback called when the gtk theme changes.
1018 We notify lisp code so it can fix faces used for region for example. */
1021 style_changed_cb (GObject
*go
,
1025 struct input_event event
;
1026 GdkDisplay
*gdpy
= (GdkDisplay
*) user_data
;
1027 const char *display_name
= gdk_display_get_name (gdpy
);
1028 Display
*dpy
= GDK_DISPLAY_XDISPLAY (gdpy
);
1031 event
.kind
= CONFIG_CHANGED_EVENT
;
1032 event
.frame_or_window
= build_string (display_name
);
1033 /* Theme doesn't change often, so intern is called seldom. */
1034 event
.arg
= intern ("theme-name");
1035 kbd_buffer_store_event (&event
);
1037 update_theme_scrollbar_width ();
1039 /* If scroll bar width changed, we need set the new size on all frames
1043 Lisp_Object rest
, frame
;
1044 FOR_EACH_FRAME (rest
, frame
)
1046 FRAME_PTR f
= XFRAME (frame
);
1047 if (FRAME_X_DISPLAY (f
) == dpy
)
1049 x_set_scroll_bar_default_width (f
);
1050 xg_frame_set_char_size (f
, FRAME_COLS (f
), FRAME_LINES (f
));
1056 /* Called when a delete-event occurs on WIDGET. */
1059 delete_cb (GtkWidget
*widget
,
1064 /* The event doesn't arrive in the normal event loop. Send event
1066 FRAME_PTR f
= (FRAME_PTR
) user_data
;
1067 struct input_event ie
;
1070 ie
.kind
= DELETE_WINDOW_EVENT
;
1071 XSETFRAME (ie
.frame_or_window
, f
);
1072 kbd_buffer_store_event (&ie
);
1078 /* Create and set up the GTK widgets for frame F.
1079 Return 0 if creation failed, non-zero otherwise. */
1082 xg_create_frame_widgets (FRAME_PTR f
)
1085 GtkWidget
*wvbox
, *whbox
;
1092 if (FRAME_X_EMBEDDED_P (f
))
1093 wtop
= gtk_plug_new (f
->output_data
.x
->parent_desc
);
1095 wtop
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
1097 xg_set_screen (wtop
, f
);
1099 wvbox
= gtk_vbox_new (FALSE
, 0);
1100 whbox
= gtk_hbox_new (FALSE
, 0);
1103 wfixed
= emacs_fixed_new (f
);
1105 wfixed
= gtk_fixed_new ();
1108 if (! wtop
|| ! wvbox
|| ! whbox
|| ! wfixed
)
1110 if (wtop
) gtk_widget_destroy (wtop
);
1111 if (wvbox
) gtk_widget_destroy (wvbox
);
1112 if (whbox
) gtk_widget_destroy (whbox
);
1113 if (wfixed
) gtk_widget_destroy (wfixed
);
1119 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1120 gtk_widget_set_name (wtop
, EMACS_CLASS
);
1121 gtk_widget_set_name (wvbox
, "pane");
1122 gtk_widget_set_name (wfixed
, SSDATA (Vx_resource_name
));
1124 /* If this frame has a title or name, set it in the title bar. */
1125 if (! NILP (f
->title
)) title
= SSDATA (ENCODE_UTF_8 (f
->title
));
1126 else if (! NILP (f
->name
)) title
= SSDATA (ENCODE_UTF_8 (f
->name
));
1128 if (title
) gtk_window_set_title (GTK_WINDOW (wtop
), title
);
1130 FRAME_GTK_OUTER_WIDGET (f
) = wtop
;
1131 FRAME_GTK_WIDGET (f
) = wfixed
;
1132 f
->output_data
.x
->vbox_widget
= wvbox
;
1133 f
->output_data
.x
->hbox_widget
= whbox
;
1135 gtk_widget_set_has_window (wfixed
, TRUE
);
1137 gtk_container_add (GTK_CONTAINER (wtop
), wvbox
);
1138 gtk_box_pack_start (GTK_BOX (wvbox
), whbox
, TRUE
, TRUE
, 0);
1139 gtk_box_pack_start (GTK_BOX (whbox
), wfixed
, TRUE
, TRUE
, 0);
1141 if (FRAME_EXTERNAL_TOOL_BAR (f
))
1142 update_frame_tool_bar (f
);
1144 /* We don't want this widget double buffered, because we draw on it
1145 with regular X drawing primitives, so from a GTK/GDK point of
1146 view, the widget is totally blank. When an expose comes, this
1147 will make the widget blank, and then Emacs redraws it. This flickers
1148 a lot, so we turn off double buffering. */
1149 gtk_widget_set_double_buffered (wfixed
, FALSE
);
1151 gtk_window_set_wmclass (GTK_WINDOW (wtop
),
1152 SSDATA (Vx_resource_name
),
1153 SSDATA (Vx_resource_class
));
1155 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1156 GTK is to destroy the widget. We want Emacs to do that instead. */
1157 g_signal_connect (G_OBJECT (wtop
), "delete-event",
1158 G_CALLBACK (delete_cb
), f
);
1160 /* Convert our geometry parameters into a geometry string
1162 GTK will itself handle calculating the real position this way. */
1163 xg_set_geometry (f
);
1165 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
1167 gtk_widget_add_events (wfixed
,
1168 GDK_POINTER_MOTION_MASK
1170 | GDK_BUTTON_PRESS_MASK
1171 | GDK_BUTTON_RELEASE_MASK
1172 | GDK_KEY_PRESS_MASK
1173 | GDK_ENTER_NOTIFY_MASK
1174 | GDK_LEAVE_NOTIFY_MASK
1175 | GDK_FOCUS_CHANGE_MASK
1176 | GDK_STRUCTURE_MASK
1177 | GDK_VISIBILITY_NOTIFY_MASK
);
1179 /* Must realize the windows so the X window gets created. It is used
1180 by callers of this function. */
1181 gtk_widget_realize (wfixed
);
1182 FRAME_X_WINDOW (f
) = GTK_WIDGET_TO_X_WIN (wfixed
);
1184 /* Since GTK clears its window by filling with the background color,
1185 we must keep X and GTK background in sync. */
1186 xg_set_widget_bg (f
, wfixed
, FRAME_BACKGROUND_PIXEL (f
));
1189 /* Also, do not let any background pixmap to be set, this looks very
1190 bad as Emacs overwrites the background pixmap with its own idea
1191 of background color. */
1192 style
= gtk_widget_get_modifier_style (wfixed
);
1194 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1195 style
->bg_pixmap_name
[GTK_STATE_NORMAL
] = g_strdup ("<none>");
1196 gtk_widget_modify_style (wfixed
, style
);
1198 gtk_widget_set_can_focus (wfixed
, TRUE
);
1199 gtk_window_set_resizable (GTK_WINDOW (wtop
), TRUE
);
1202 #ifdef USE_GTK_TOOLTIP
1203 /* Steal a tool tip window we can move ourselves. */
1204 f
->output_data
.x
->ttip_widget
= 0;
1205 f
->output_data
.x
->ttip_lbl
= 0;
1206 f
->output_data
.x
->ttip_window
= 0;
1207 gtk_widget_set_tooltip_text (wtop
, "Dummy text");
1208 g_signal_connect (wtop
, "query-tooltip", G_CALLBACK (qttip_cb
), f
);
1212 GdkScreen
*screen
= gtk_widget_get_screen (wtop
);
1213 GtkSettings
*gs
= gtk_settings_get_for_screen (screen
);
1214 /* Only connect this signal once per screen. */
1215 if (! g_signal_handler_find (G_OBJECT (gs
),
1216 G_SIGNAL_MATCH_FUNC
,
1218 G_CALLBACK (style_changed_cb
),
1221 g_signal_connect (G_OBJECT (gs
), "notify::gtk-theme-name",
1222 G_CALLBACK (style_changed_cb
),
1223 gdk_screen_get_display (screen
));
1233 xg_free_frame_widgets (FRAME_PTR f
)
1235 if (FRAME_GTK_OUTER_WIDGET (f
))
1237 #ifdef USE_GTK_TOOLTIP
1238 struct x_output
*x
= f
->output_data
.x
;
1240 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f
));
1241 FRAME_X_WINDOW (f
) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1242 FRAME_GTK_OUTER_WIDGET (f
) = 0;
1243 #ifdef USE_GTK_TOOLTIP
1245 gtk_widget_destroy (x
->ttip_lbl
);
1247 g_object_unref (G_OBJECT (x
->ttip_widget
));
1252 /* Set the normal size hints for the window manager, for frame F.
1253 FLAGS is the flags word to use--or 0 meaning preserve the flags
1254 that the window now has.
1255 If USER_POSITION is nonzero, we set the User Position
1256 flag (this is useful when FLAGS is 0). */
1259 x_wm_set_size_hint (FRAME_PTR f
, long int flags
, int user_position
)
1261 /* Must use GTK routines here, otherwise GTK resets the size hints
1262 to its own defaults. */
1263 GdkGeometry size_hints
;
1264 gint hint_flags
= 0;
1265 int base_width
, base_height
;
1266 int min_rows
= 0, min_cols
= 0;
1267 int win_gravity
= f
->win_gravity
;
1269 /* Don't set size hints during initialization; that apparently leads
1270 to a race condition. See the thread at
1271 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1272 if (NILP (Vafter_init_time
) || !FRAME_GTK_OUTER_WIDGET (f
))
1277 memset (&size_hints
, 0, sizeof (size_hints
));
1278 f
->output_data
.x
->size_hints
= size_hints
;
1279 f
->output_data
.x
->hint_flags
= hint_flags
;
1282 flags
= f
->size_hint_flags
;
1284 size_hints
= f
->output_data
.x
->size_hints
;
1285 hint_flags
= f
->output_data
.x
->hint_flags
;
1287 hint_flags
|= GDK_HINT_RESIZE_INC
| GDK_HINT_MIN_SIZE
;
1288 size_hints
.width_inc
= FRAME_COLUMN_WIDTH (f
);
1289 size_hints
.height_inc
= FRAME_LINE_HEIGHT (f
);
1291 hint_flags
|= GDK_HINT_BASE_SIZE
;
1292 base_width
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f
, 0) + FRAME_TOOLBAR_WIDTH (f
);
1293 base_height
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f
, 0)
1294 + FRAME_MENUBAR_HEIGHT (f
) + FRAME_TOOLBAR_HEIGHT (f
);
1296 check_frame_size (f
, &min_rows
, &min_cols
);
1298 size_hints
.base_width
= base_width
;
1299 size_hints
.base_height
= base_height
;
1300 size_hints
.min_width
= base_width
+ min_cols
* size_hints
.width_inc
;
1301 size_hints
.min_height
= base_height
+ min_rows
* size_hints
.height_inc
;
1303 /* These currently have a one to one mapping with the X values, but I
1304 don't think we should rely on that. */
1305 hint_flags
|= GDK_HINT_WIN_GRAVITY
;
1306 size_hints
.win_gravity
= 0;
1307 if (win_gravity
== NorthWestGravity
)
1308 size_hints
.win_gravity
= GDK_GRAVITY_NORTH_WEST
;
1309 else if (win_gravity
== NorthGravity
)
1310 size_hints
.win_gravity
= GDK_GRAVITY_NORTH
;
1311 else if (win_gravity
== NorthEastGravity
)
1312 size_hints
.win_gravity
= GDK_GRAVITY_NORTH_EAST
;
1313 else if (win_gravity
== WestGravity
)
1314 size_hints
.win_gravity
= GDK_GRAVITY_WEST
;
1315 else if (win_gravity
== CenterGravity
)
1316 size_hints
.win_gravity
= GDK_GRAVITY_CENTER
;
1317 else if (win_gravity
== EastGravity
)
1318 size_hints
.win_gravity
= GDK_GRAVITY_EAST
;
1319 else if (win_gravity
== SouthWestGravity
)
1320 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH_WEST
;
1321 else if (win_gravity
== SouthGravity
)
1322 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH
;
1323 else if (win_gravity
== SouthEastGravity
)
1324 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH_EAST
;
1325 else if (win_gravity
== StaticGravity
)
1326 size_hints
.win_gravity
= GDK_GRAVITY_STATIC
;
1330 hint_flags
&= ~GDK_HINT_POS
;
1331 hint_flags
|= GDK_HINT_USER_POS
;
1334 if (hint_flags
!= f
->output_data
.x
->hint_flags
1335 || memcmp (&size_hints
,
1336 &f
->output_data
.x
->size_hints
,
1337 sizeof (size_hints
)) != 0)
1340 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
1341 NULL
, &size_hints
, hint_flags
);
1342 f
->output_data
.x
->size_hints
= size_hints
;
1343 f
->output_data
.x
->hint_flags
= hint_flags
;
1348 /* Change background color of a frame.
1349 Since GTK uses the background color to clear the window, we must
1350 keep the GTK and X colors in sync.
1351 F is the frame to change,
1352 BG is the pixel value to change to. */
1355 xg_set_background_color (FRAME_PTR f
, long unsigned int bg
)
1357 if (FRAME_GTK_WIDGET (f
))
1360 xg_set_widget_bg (f
, FRAME_GTK_WIDGET (f
), FRAME_BACKGROUND_PIXEL (f
));
1366 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1367 functions so GTK does not overwrite the icon. */
1370 xg_set_frame_icon (FRAME_PTR f
, Pixmap icon_pixmap
, Pixmap icon_mask
)
1372 GdkPixbuf
*gp
= xg_get_pixbuf_from_pix_and_mask (f
,
1376 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)), gp
);
1381 /***********************************************************************
1383 ***********************************************************************/
1384 /* Return the dialog title to use for a dialog of type KEY.
1385 This is the encoding used by lwlib. We use the same for GTK. */
1388 get_dialog_title (char key
)
1390 const char *title
= "";
1398 title
= "Information";
1417 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1418 the dialog, but return TRUE so the event does not propagate further
1419 in GTK. This prevents GTK from destroying the dialog widget automatically
1420 and we can always destrou the widget manually, regardles of how
1421 it was popped down (button press or WM_DELETE_WINDOW).
1422 W is the dialog widget.
1423 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1424 user_data is NULL (not used).
1426 Returns TRUE to end propagation of event. */
1429 dialog_delete_callback (GtkWidget
*w
, GdkEvent
*event
, gpointer user_data
)
1431 gtk_widget_unmap (w
);
1435 /* Create a popup dialog window. See also xg_create_widget below.
1436 WV is a widget_value describing the dialog.
1437 SELECT_CB is the callback to use when a button has been pressed.
1438 DEACTIVATE_CB is the callback to use when the dialog pops down.
1440 Returns the GTK dialog widget. */
1443 create_dialog (widget_value
*wv
,
1444 GCallback select_cb
,
1445 GCallback deactivate_cb
)
1447 const char *title
= get_dialog_title (wv
->name
[0]);
1448 int total_buttons
= wv
->name
[1] - '0';
1449 int right_buttons
= wv
->name
[4] - '0';
1452 int button_spacing
= 10;
1453 GtkWidget
*wdialog
= gtk_dialog_new ();
1454 GtkDialog
*wd
= GTK_DIALOG (wdialog
);
1455 GtkBox
*cur_box
= GTK_BOX (gtk_dialog_get_action_area (wd
));
1457 GtkWidget
*whbox_down
;
1459 /* If the number of buttons is greater than 4, make two rows of buttons
1460 instead. This looks better. */
1461 int make_two_rows
= total_buttons
> 4;
1463 if (right_buttons
== 0) right_buttons
= total_buttons
/2;
1464 left_buttons
= total_buttons
- right_buttons
;
1466 gtk_window_set_title (GTK_WINDOW (wdialog
), title
);
1467 gtk_widget_set_name (wdialog
, "emacs-dialog");
1472 GtkWidget
*wvbox
= gtk_vbox_new (TRUE
, button_spacing
);
1473 GtkWidget
*whbox_up
= gtk_hbox_new (FALSE
, 0);
1474 whbox_down
= gtk_hbox_new (FALSE
, 0);
1476 gtk_box_pack_start (cur_box
, wvbox
, FALSE
, FALSE
, 0);
1477 gtk_box_pack_start (GTK_BOX (wvbox
), whbox_up
, FALSE
, FALSE
, 0);
1478 gtk_box_pack_start (GTK_BOX (wvbox
), whbox_down
, FALSE
, FALSE
, 0);
1480 cur_box
= GTK_BOX (whbox_up
);
1483 g_signal_connect (G_OBJECT (wdialog
), "delete-event",
1484 G_CALLBACK (dialog_delete_callback
), 0);
1488 g_signal_connect (G_OBJECT (wdialog
), "close", deactivate_cb
, 0);
1489 g_signal_connect (G_OBJECT (wdialog
), "response", deactivate_cb
, 0);
1492 for (item
= wv
->contents
; item
; item
= item
->next
)
1494 char *utf8_label
= get_utf8_string (item
->value
);
1498 if (item
->name
&& strcmp (item
->name
, "message") == 0)
1500 GtkBox
*wvbox
= GTK_BOX (gtk_dialog_get_content_area (wd
));
1501 /* This is the text part of the dialog. */
1502 w
= gtk_label_new (utf8_label
);
1503 gtk_box_pack_start (wvbox
, gtk_label_new (""), FALSE
, FALSE
, 0);
1504 gtk_box_pack_start (wvbox
, w
, TRUE
, TRUE
, 0);
1505 gtk_misc_set_alignment (GTK_MISC (w
), 0.1, 0.5);
1507 /* Try to make dialog look better. Must realize first so
1508 the widget can calculate the size it needs. */
1509 gtk_widget_realize (w
);
1510 gtk_widget_get_preferred_size (w
, NULL
, &req
);
1511 gtk_box_set_spacing (wvbox
, req
.height
);
1512 if (item
->value
&& strlen (item
->value
) > 0)
1513 button_spacing
= 2*req
.width
/strlen (item
->value
);
1517 /* This is one button to add to the dialog. */
1518 w
= gtk_button_new_with_label (utf8_label
);
1519 if (! item
->enabled
)
1520 gtk_widget_set_sensitive (w
, FALSE
);
1522 g_signal_connect (G_OBJECT (w
), "clicked",
1523 select_cb
, item
->call_data
);
1525 gtk_box_pack_start (cur_box
, w
, TRUE
, TRUE
, button_spacing
);
1526 if (++button_nr
== left_buttons
)
1529 cur_box
= GTK_BOX (whbox_down
);
1531 gtk_box_pack_start (cur_box
,
1539 g_free (utf8_label
);
1545 struct xg_dialog_data
1553 /* Function that is called when the file or font dialogs pop down.
1554 W is the dialog widget, RESPONSE is the response code.
1555 USER_DATA is what we passed in to g_signal_connect. */
1558 xg_dialog_response_cb (GtkDialog
*w
,
1562 struct xg_dialog_data
*dd
= (struct xg_dialog_data
*)user_data
;
1563 dd
->response
= response
;
1564 g_main_loop_quit (dd
->loop
);
1568 /* Destroy the dialog. This makes it pop down. */
1571 pop_down_dialog (Lisp_Object arg
)
1573 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
1574 struct xg_dialog_data
*dd
= (struct xg_dialog_data
*) p
->pointer
;
1577 if (dd
->w
) gtk_widget_destroy (dd
->w
);
1578 if (dd
->timerid
!= 0) g_source_remove (dd
->timerid
);
1580 g_main_loop_quit (dd
->loop
);
1581 g_main_loop_unref (dd
->loop
);
1588 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1589 We pass in DATA as gpointer* so we can use this as a callback. */
1592 xg_maybe_add_timer (gpointer data
)
1594 struct xg_dialog_data
*dd
= (struct xg_dialog_data
*) data
;
1595 EMACS_TIME next_time
= timer_check ();
1596 long secs
= EMACS_SECS (next_time
);
1597 long usecs
= EMACS_USECS (next_time
);
1601 if (secs
>= 0 && usecs
>= 0 && secs
< ((guint
)-1)/1000)
1603 dd
->timerid
= g_timeout_add (secs
* 1000 + usecs
/1000,
1611 /* Pops up a modal dialog W and waits for response.
1612 We don't use gtk_dialog_run because we want to process emacs timers.
1613 The dialog W is not destroyed when this function returns. */
1616 xg_dialog_run (FRAME_PTR f
, GtkWidget
*w
)
1618 int count
= SPECPDL_INDEX ();
1619 struct xg_dialog_data dd
;
1621 xg_set_screen (w
, f
);
1622 gtk_window_set_transient_for (GTK_WINDOW (w
),
1623 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
1624 gtk_window_set_destroy_with_parent (GTK_WINDOW (w
), TRUE
);
1625 gtk_window_set_modal (GTK_WINDOW (w
), TRUE
);
1627 dd
.loop
= g_main_loop_new (NULL
, FALSE
);
1628 dd
.response
= GTK_RESPONSE_CANCEL
;
1632 g_signal_connect (G_OBJECT (w
),
1634 G_CALLBACK (xg_dialog_response_cb
),
1636 /* Don't destroy the widget if closed by the window manager close button. */
1637 g_signal_connect (G_OBJECT (w
), "delete-event", G_CALLBACK (gtk_true
), NULL
);
1638 gtk_widget_show (w
);
1640 record_unwind_protect (pop_down_dialog
, make_save_value (&dd
, 0));
1642 (void) xg_maybe_add_timer (&dd
);
1643 g_main_loop_run (dd
.loop
);
1646 unbind_to (count
, Qnil
);
1652 /***********************************************************************
1653 File dialog functions
1654 ***********************************************************************/
1655 /* Return non-zero if the old file selection dialog is being used.
1656 Return zero if not. */
1659 xg_uses_old_file_dialog (void)
1661 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1662 return x_gtk_use_old_file_dialog
;
1669 typedef char * (*xg_get_file_func
) (GtkWidget
*);
1671 /* Return the selected file for file chooser dialog W.
1672 The returned string must be free:d. */
1675 xg_get_file_name_from_chooser (GtkWidget
*w
)
1677 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w
));
1680 /* Callback called when the "Show hidden files" toggle is pressed.
1681 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1684 xg_toggle_visibility_cb (GtkWidget
*widget
, gpointer data
)
1686 GtkFileChooser
*dialog
= GTK_FILE_CHOOSER (data
);
1688 g_object_get (G_OBJECT (dialog
), "show-hidden", &visible
, NULL
);
1689 g_object_set (G_OBJECT (dialog
), "show-hidden", !visible
, NULL
);
1693 /* Callback called when a property changes in a file chooser.
1694 GOBJECT is the file chooser dialog, ARG1 describes the property.
1695 USER_DATA is the toggle widget in the file chooser dialog.
1696 We use this to update the "Show hidden files" toggle when the user
1697 changes that property by right clicking in the file list. */
1700 xg_toggle_notify_cb (GObject
*gobject
, GParamSpec
*arg1
, gpointer user_data
)
1702 if (strcmp (arg1
->name
, "show-hidden") == 0)
1704 GtkWidget
*wtoggle
= GTK_WIDGET (user_data
);
1705 gboolean visible
, toggle_on
;
1707 g_object_get (G_OBJECT (gobject
), "show-hidden", &visible
, NULL
);
1708 toggle_on
= gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle
));
1710 if (!!visible
!= !!toggle_on
)
1712 g_signal_handlers_block_by_func (G_OBJECT (wtoggle
),
1713 G_CALLBACK (xg_toggle_visibility_cb
),
1715 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle
), visible
);
1716 g_signal_handlers_unblock_by_func
1717 (G_OBJECT (wtoggle
),
1718 G_CALLBACK (xg_toggle_visibility_cb
),
1721 x_gtk_show_hidden_files
= visible
;
1725 /* Read a file name from the user using a file chooser dialog.
1726 F is the current frame.
1727 PROMPT is a prompt to show to the user. May not be NULL.
1728 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1729 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1730 file. *FUNC is set to a function that can be used to retrieve the
1731 selected file name from the returned widget.
1733 Returns the created widget. */
1736 xg_get_file_with_chooser (FRAME_PTR f
,
1738 char *default_filename
,
1739 int mustmatch_p
, int only_dir_p
,
1740 xg_get_file_func
*func
)
1744 GtkWidget
*filewin
, *wtoggle
, *wbox
, *wmessage
IF_LINT (= NULL
);
1745 GtkWindow
*gwin
= GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
));
1746 GtkFileChooserAction action
= (mustmatch_p
?
1747 GTK_FILE_CHOOSER_ACTION_OPEN
:
1748 GTK_FILE_CHOOSER_ACTION_SAVE
);
1751 action
= GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
;
1753 filewin
= gtk_file_chooser_dialog_new (prompt
, gwin
, action
,
1754 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1755 (mustmatch_p
|| only_dir_p
?
1756 GTK_STOCK_OPEN
: GTK_STOCK_OK
),
1759 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin
), TRUE
);
1761 wbox
= gtk_vbox_new (FALSE
, 0);
1762 gtk_widget_show (wbox
);
1763 wtoggle
= gtk_check_button_new_with_label ("Show hidden files.");
1765 if (x_gtk_show_hidden_files
)
1767 g_object_set (G_OBJECT (filewin
), "show-hidden", TRUE
, NULL
);
1768 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle
), TRUE
);
1770 gtk_widget_show (wtoggle
);
1771 g_signal_connect (G_OBJECT (wtoggle
), "clicked",
1772 G_CALLBACK (xg_toggle_visibility_cb
), filewin
);
1773 g_signal_connect (G_OBJECT (filewin
), "notify",
1774 G_CALLBACK (xg_toggle_notify_cb
), wtoggle
);
1776 if (x_gtk_file_dialog_help_text
)
1779 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1780 Show the C-l help text only for versions < 2.10. */
1781 if (gtk_check_version (2, 10, 0) && action
!= GTK_FILE_CHOOSER_ACTION_SAVE
)
1782 strcat (msgbuf
, "\nType C-l to display a file name text entry box.\n");
1783 strcat (msgbuf
, "\nIf you don't like this file selector, use the "
1784 "corresponding\nkey binding or customize "
1785 "use-file-dialog to turn it off.");
1787 wmessage
= gtk_label_new (msgbuf
);
1788 gtk_widget_show (wmessage
);
1791 gtk_box_pack_start (GTK_BOX (wbox
), wtoggle
, FALSE
, FALSE
, 0);
1792 if (x_gtk_file_dialog_help_text
)
1793 gtk_box_pack_start (GTK_BOX (wbox
), wmessage
, FALSE
, FALSE
, 0);
1794 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin
), wbox
);
1796 if (default_filename
)
1799 struct gcpro gcpro1
;
1800 char *utf8_filename
;
1803 file
= build_string (default_filename
);
1805 /* File chooser does not understand ~/... in the file name. It must be
1806 an absolute name starting with /. */
1807 if (default_filename
[0] != '/')
1808 file
= Fexpand_file_name (file
, Qnil
);
1810 utf8_filename
= SSDATA (ENCODE_UTF_8 (file
));
1811 if (! NILP (Ffile_directory_p (file
)))
1812 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin
),
1816 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin
),
1818 if (action
== GTK_FILE_CHOOSER_ACTION_SAVE
)
1820 char *cp
= strrchr (utf8_filename
, '/');
1822 else cp
= utf8_filename
;
1823 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin
), cp
);
1830 *func
= xg_get_file_name_from_chooser
;
1834 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1836 /* Return the selected file for file selector dialog W.
1837 The returned string must be free:d. */
1840 xg_get_file_name_from_selector (GtkWidget
*w
)
1842 GtkFileSelection
*filesel
= GTK_FILE_SELECTION (w
);
1843 return xstrdup ((char*) gtk_file_selection_get_filename (filesel
));
1846 /* Create a file selection dialog.
1847 F is the current frame.
1848 PROMPT is a prompt to show to the user. May not be NULL.
1849 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1850 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1851 file. *FUNC is set to a function that can be used to retrieve the
1852 selected file name from the returned widget.
1854 Returns the created widget. */
1857 xg_get_file_with_selection (FRAME_PTR f
,
1859 char *default_filename
,
1860 int mustmatch_p
, int only_dir_p
,
1861 xg_get_file_func
*func
)
1864 GtkFileSelection
*filesel
;
1866 filewin
= gtk_file_selection_new (prompt
);
1867 filesel
= GTK_FILE_SELECTION (filewin
);
1869 if (default_filename
)
1870 gtk_file_selection_set_filename (filesel
, default_filename
);
1874 /* The selection_entry part of filesel is not documented. */
1875 gtk_widget_set_sensitive (filesel
->selection_entry
, FALSE
);
1876 gtk_file_selection_hide_fileop_buttons (filesel
);
1879 *func
= xg_get_file_name_from_selector
;
1883 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1885 /* Read a file name from the user using a file dialog, either the old
1886 file selection dialog, or the new file chooser dialog. Which to use
1887 depends on what the GTK version used has, and what the value of
1888 gtk-use-old-file-dialog.
1889 F is the current frame.
1890 PROMPT is a prompt to show to the user. May not be NULL.
1891 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1892 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1895 Returns a file name or NULL if no file was selected.
1896 The returned string must be freed by the caller. */
1899 xg_get_file_name (FRAME_PTR f
,
1901 char *default_filename
,
1907 int filesel_done
= 0;
1908 xg_get_file_func func
;
1910 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
1911 /* I really don't know why this is needed, but without this the GLIBC add on
1912 library linuxthreads hangs when the Gnome file chooser backend creates
1914 sigblock (sigmask (__SIGRTMIN
));
1915 #endif /* HAVE_PTHREAD */
1917 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1919 if (xg_uses_old_file_dialog ())
1920 w
= xg_get_file_with_selection (f
, prompt
, default_filename
,
1921 mustmatch_p
, only_dir_p
, &func
);
1923 w
= xg_get_file_with_chooser (f
, prompt
, default_filename
,
1924 mustmatch_p
, only_dir_p
, &func
);
1926 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
1927 w
= xg_get_file_with_chooser (f
, prompt
, default_filename
,
1928 mustmatch_p
, only_dir_p
, &func
);
1929 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
1931 gtk_widget_set_name (w
, "emacs-filedialog");
1933 filesel_done
= xg_dialog_run (f
, w
);
1935 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
1936 sigunblock (sigmask (__SIGRTMIN
));
1939 if (filesel_done
== GTK_RESPONSE_OK
)
1942 gtk_widget_destroy (w
);
1946 #ifdef HAVE_FREETYPE
1947 /* Pop up a GTK font selector and return the name of the font the user
1948 selects, as a C string. The returned font name follows GTK's own
1951 `FAMILY [VALUE1 VALUE2] SIZE'
1953 This can be parsed using font_parse_fcname in font.c.
1954 DEFAULT_NAME, if non-zero, is the default font name. */
1957 xg_get_font_name (FRAME_PTR f
, const char *default_name
)
1960 char *fontname
= NULL
;
1963 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
1964 sigblock (sigmask (__SIGRTMIN
));
1965 #endif /* HAVE_PTHREAD */
1967 w
= gtk_font_selection_dialog_new ("Pick a font");
1969 default_name
= "Monospace 10";
1970 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w
),
1973 gtk_widget_set_name (w
, "emacs-fontdialog");
1975 done
= xg_dialog_run (f
, w
);
1977 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
1978 sigunblock (sigmask (__SIGRTMIN
));
1981 if (done
== GTK_RESPONSE_OK
)
1982 fontname
= gtk_font_selection_dialog_get_font_name
1983 (GTK_FONT_SELECTION_DIALOG (w
));
1985 gtk_widget_destroy (w
);
1988 #endif /* HAVE_FREETYPE */
1992 /***********************************************************************
1994 ***********************************************************************/
1996 /* The name of menu items that can be used for customization. Since GTK
1997 RC files are very crude and primitive, we have to set this on all
1998 menu item names so a user can easily customize menu items. */
2000 #define MENU_ITEM_NAME "emacs-menuitem"
2003 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2004 during GC. The next member points to the items. */
2005 static xg_list_node xg_menu_cb_list
;
2007 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2008 during GC. The next member points to the items. */
2009 static xg_list_node xg_menu_item_cb_list
;
2011 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2012 F is the frame CL_DATA will be initialized for.
2013 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2015 The menu bar and all sub menus under the menu bar in a frame
2016 share the same structure, hence the reference count.
2018 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2019 allocated xg_menu_cb_data if CL_DATA is NULL. */
2021 static xg_menu_cb_data
*
2022 make_cl_data (xg_menu_cb_data
*cl_data
, FRAME_PTR f
, GCallback highlight_cb
)
2026 cl_data
= (xg_menu_cb_data
*) xmalloc (sizeof (*cl_data
));
2028 cl_data
->menu_bar_vector
= f
->menu_bar_vector
;
2029 cl_data
->menu_bar_items_used
= f
->menu_bar_items_used
;
2030 cl_data
->highlight_cb
= highlight_cb
;
2031 cl_data
->ref_count
= 0;
2033 xg_list_insert (&xg_menu_cb_list
, &cl_data
->ptrs
);
2036 cl_data
->ref_count
++;
2041 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2042 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2044 When the menu bar is updated, menu items may have been added and/or
2045 removed, so menu_bar_vector and menu_bar_items_used change. We must
2046 then update CL_DATA since it is used to determine which menu
2047 item that is invoked in the menu.
2048 HIGHLIGHT_CB could change, there is no check that the same
2049 function is given when modifying a menu bar as was given when
2050 creating the menu bar. */
2053 update_cl_data (xg_menu_cb_data
*cl_data
,
2055 GCallback highlight_cb
)
2060 cl_data
->menu_bar_vector
= f
->menu_bar_vector
;
2061 cl_data
->menu_bar_items_used
= f
->menu_bar_items_used
;
2062 cl_data
->highlight_cb
= highlight_cb
;
2066 /* Decrease reference count for CL_DATA.
2067 If reference count is zero, free CL_DATA. */
2070 unref_cl_data (xg_menu_cb_data
*cl_data
)
2072 if (cl_data
&& cl_data
->ref_count
> 0)
2074 cl_data
->ref_count
--;
2075 if (cl_data
->ref_count
== 0)
2077 xg_list_remove (&xg_menu_cb_list
, &cl_data
->ptrs
);
2083 /* Function that marks all lisp data during GC. */
2090 for (iter
= xg_menu_cb_list
.next
; iter
; iter
= iter
->next
)
2091 mark_object (((xg_menu_cb_data
*) iter
)->menu_bar_vector
);
2093 for (iter
= xg_menu_item_cb_list
.next
; iter
; iter
= iter
->next
)
2095 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) iter
;
2097 if (! NILP (cb_data
->help
))
2098 mark_object (cb_data
->help
);
2103 /* Callback called when a menu item is destroyed. Used to free data.
2104 W is the widget that is being destroyed (not used).
2105 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2108 menuitem_destroy_callback (GtkWidget
*w
, gpointer client_data
)
2112 xg_menu_item_cb_data
*data
= (xg_menu_item_cb_data
*) client_data
;
2113 xg_list_remove (&xg_menu_item_cb_list
, &data
->ptrs
);
2118 /* Callback called when the pointer enters/leaves a menu item.
2119 W is the parent of the menu item.
2120 EVENT is either an enter event or leave event.
2121 CLIENT_DATA is not used.
2123 Returns FALSE to tell GTK to keep processing this event. */
2126 menuitem_highlight_callback (GtkWidget
*w
,
2127 GdkEventCrossing
*event
,
2128 gpointer client_data
)
2131 GtkWidget
*subwidget
;
2132 xg_menu_item_cb_data
*data
;
2134 ev
.crossing
= *event
;
2135 subwidget
= gtk_get_event_widget (&ev
);
2136 data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (subwidget
),
2140 if (! NILP (data
->help
) && data
->cl_data
->highlight_cb
)
2142 gpointer call_data
= event
->type
== GDK_LEAVE_NOTIFY
? 0 : data
;
2143 GtkCallback func
= (GtkCallback
) data
->cl_data
->highlight_cb
;
2144 (*func
) (subwidget
, call_data
);
2151 /* Callback called when a menu is destroyed. Used to free data.
2152 W is the widget that is being destroyed (not used).
2153 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2156 menu_destroy_callback (GtkWidget
*w
, gpointer client_data
)
2158 unref_cl_data ((xg_menu_cb_data
*) client_data
);
2161 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2162 must be non-NULL) and can be inserted into a menu item.
2164 Returns the GtkHBox. */
2167 make_widget_for_menu_item (const char *utf8_label
, const char *utf8_key
)
2173 wbox
= gtk_hbox_new (FALSE
, 0);
2174 wlbl
= gtk_label_new (utf8_label
);
2175 wkey
= gtk_label_new (utf8_key
);
2177 gtk_misc_set_alignment (GTK_MISC (wlbl
), 0.0, 0.5);
2178 gtk_misc_set_alignment (GTK_MISC (wkey
), 0.0, 0.5);
2180 gtk_box_pack_start (GTK_BOX (wbox
), wlbl
, TRUE
, TRUE
, 0);
2181 gtk_box_pack_start (GTK_BOX (wbox
), wkey
, FALSE
, FALSE
, 0);
2183 gtk_widget_set_name (wlbl
, MENU_ITEM_NAME
);
2184 gtk_widget_set_name (wkey
, MENU_ITEM_NAME
);
2185 gtk_widget_set_name (wbox
, MENU_ITEM_NAME
);
2190 /* Make and return a menu item widget with the key to the right.
2191 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2192 UTF8_KEY is the text representing the key binding.
2193 ITEM is the widget_value describing the menu item.
2195 GROUP is an in/out parameter. If the menu item to be created is not
2196 part of any radio menu group, *GROUP contains NULL on entry and exit.
2197 If the menu item to be created is part of a radio menu group, on entry
2198 *GROUP contains the group to use, or NULL if this is the first item
2199 in the group. On exit, *GROUP contains the radio item group.
2201 Unfortunately, keys don't line up as nicely as in Motif,
2202 but the MacOS X version doesn't either, so I guess that is OK. */
2205 make_menu_item (const char *utf8_label
,
2206 const char *utf8_key
,
2211 GtkWidget
*wtoadd
= 0;
2213 /* It has been observed that some menu items have a NULL name field.
2214 This will lead to this function being called with a NULL utf8_label.
2215 GTK crashes on that so we set a blank label. Why there is a NULL
2216 name remains to be investigated. */
2217 if (! utf8_label
) utf8_label
= " ";
2220 wtoadd
= make_widget_for_menu_item (utf8_label
, utf8_key
);
2222 if (item
->button_type
== BUTTON_TYPE_TOGGLE
)
2225 if (utf8_key
) w
= gtk_check_menu_item_new ();
2226 else w
= gtk_check_menu_item_new_with_label (utf8_label
);
2227 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), item
->selected
);
2229 else if (item
->button_type
== BUTTON_TYPE_RADIO
)
2231 if (utf8_key
) w
= gtk_radio_menu_item_new (*group
);
2232 else w
= gtk_radio_menu_item_new_with_label (*group
, utf8_label
);
2233 *group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w
));
2235 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), TRUE
);
2240 if (utf8_key
) w
= gtk_menu_item_new ();
2241 else w
= gtk_menu_item_new_with_label (utf8_label
);
2244 if (wtoadd
) gtk_container_add (GTK_CONTAINER (w
), wtoadd
);
2245 if (! item
->enabled
) gtk_widget_set_sensitive (w
, FALSE
);
2250 static int xg_detached_menus
;
2252 /* Returns non-zero if there are detached menus. */
2255 xg_have_tear_offs (void)
2257 return xg_detached_menus
> 0;
2260 /* Callback invoked when a detached menu window is removed. Here we
2261 decrease the xg_detached_menus count.
2262 WIDGET is the top level window that is removed (the parent of the menu).
2263 CLIENT_DATA is not used. */
2266 tearoff_remove (GtkWidget
*widget
, gpointer client_data
)
2268 if (xg_detached_menus
> 0) --xg_detached_menus
;
2271 /* Callback invoked when a menu is detached. It increases the
2272 xg_detached_menus count.
2273 WIDGET is the GtkTearoffMenuItem.
2274 CLIENT_DATA is not used. */
2277 tearoff_activate (GtkWidget
*widget
, gpointer client_data
)
2279 GtkWidget
*menu
= gtk_widget_get_parent (widget
);
2280 if (gtk_menu_get_tearoff_state (GTK_MENU (menu
)))
2282 ++xg_detached_menus
;
2283 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget
)),
2285 G_CALLBACK (tearoff_remove
), 0);
2290 /* Create a menu item widget, and connect the callbacks.
2291 ITEM decribes the menu item.
2292 F is the frame the created menu belongs to.
2293 SELECT_CB is the callback to use when a menu item is selected.
2294 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2295 CL_DATA points to the callback data to be used for this menu.
2296 GROUP is an in/out parameter. If the menu item to be created is not
2297 part of any radio menu group, *GROUP contains NULL on entry and exit.
2298 If the menu item to be created is part of a radio menu group, on entry
2299 *GROUP contains the group to use, or NULL if this is the first item
2300 in the group. On exit, *GROUP contains the radio item group.
2302 Returns the created GtkWidget. */
2305 xg_create_one_menuitem (widget_value
*item
,
2307 GCallback select_cb
,
2308 GCallback highlight_cb
,
2309 xg_menu_cb_data
*cl_data
,
2315 xg_menu_item_cb_data
*cb_data
;
2317 utf8_label
= get_utf8_string (item
->name
);
2318 utf8_key
= get_utf8_string (item
->key
);
2320 w
= make_menu_item (utf8_label
, utf8_key
, item
, group
);
2322 if (utf8_label
) g_free (utf8_label
);
2323 if (utf8_key
) g_free (utf8_key
);
2325 cb_data
= xmalloc (sizeof (xg_menu_item_cb_data
));
2327 xg_list_insert (&xg_menu_item_cb_list
, &cb_data
->ptrs
);
2329 cb_data
->select_id
= 0;
2330 cb_data
->help
= item
->help
;
2331 cb_data
->cl_data
= cl_data
;
2332 cb_data
->call_data
= item
->call_data
;
2334 g_signal_connect (G_OBJECT (w
),
2336 G_CALLBACK (menuitem_destroy_callback
),
2339 /* Put cb_data in widget, so we can get at it when modifying menubar */
2340 g_object_set_data (G_OBJECT (w
), XG_ITEM_DATA
, cb_data
);
2342 /* final item, not a submenu */
2343 if (item
->call_data
&& ! item
->contents
)
2347 = g_signal_connect (G_OBJECT (w
), "activate", select_cb
, cb_data
);
2353 /* Create a full menu tree specified by DATA.
2354 F is the frame the created menu belongs to.
2355 SELECT_CB is the callback to use when a menu item is selected.
2356 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2357 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2358 POP_UP_P is non-zero if we shall create a popup menu.
2359 MENU_BAR_P is non-zero if we shall create a menu bar.
2360 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2361 if MENU_BAR_P is non-zero.
2362 TOPMENU is the topmost GtkWidget that others shall be placed under.
2363 It may be NULL, in that case we create the appropriate widget
2364 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2365 CL_DATA is the callback data we shall use for this menu, or NULL
2366 if we haven't set the first callback yet.
2367 NAME is the name to give to the top level menu if this function
2368 creates it. May be NULL to not set any name.
2370 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2373 This function calls itself to create submenus. */
2376 create_menus (widget_value
*data
,
2378 GCallback select_cb
,
2379 GCallback deactivate_cb
,
2380 GCallback highlight_cb
,
2385 xg_menu_cb_data
*cl_data
,
2389 GtkWidget
*wmenu
= topmenu
;
2390 GSList
*group
= NULL
;
2396 wmenu
= gtk_menu_new ();
2397 xg_set_screen (wmenu
, f
);
2398 /* Connect this to the menu instead of items so we get enter/leave for
2399 disabled items also. TODO: Still does not get enter/leave for
2400 disabled items in detached menus. */
2401 g_signal_connect (G_OBJECT (wmenu
),
2402 "enter-notify-event",
2403 G_CALLBACK (menuitem_highlight_callback
),
2405 g_signal_connect (G_OBJECT (wmenu
),
2406 "leave-notify-event",
2407 G_CALLBACK (menuitem_highlight_callback
),
2412 wmenu
= gtk_menu_bar_new ();
2413 /* Set width of menu bar to a small value so it doesn't enlarge
2414 a small initial frame size. The width will be set to the
2415 width of the frame later on when it is added to a container.
2416 height -1: Natural height. */
2417 gtk_widget_set_size_request (wmenu
, 1, -1);
2420 /* Put cl_data on the top menu for easier access. */
2421 cl_data
= make_cl_data (cl_data
, f
, highlight_cb
);
2422 g_object_set_data (G_OBJECT (wmenu
), XG_FRAME_DATA
, (gpointer
)cl_data
);
2423 g_signal_connect (G_OBJECT (wmenu
), "destroy",
2424 G_CALLBACK (menu_destroy_callback
), cl_data
);
2427 gtk_widget_set_name (wmenu
, name
);
2430 g_signal_connect (G_OBJECT (wmenu
),
2431 "selection-done", deactivate_cb
, 0);
2434 if (! menu_bar_p
&& add_tearoff_p
)
2436 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new ();
2437 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu
), tearoff
);
2439 g_signal_connect (G_OBJECT (tearoff
), "activate",
2440 G_CALLBACK (tearoff_activate
), 0);
2443 for (item
= data
; item
; item
= item
->next
)
2447 if (pop_up_p
&& !item
->contents
&& !item
->call_data
2448 && !menu_separator_name_p (item
->name
))
2451 /* A title for a popup. We do the same as GTK does when
2452 creating titles, but it does not look good. */
2454 utf8_label
= get_utf8_string (item
->name
);
2456 gtk_menu_set_title (GTK_MENU (wmenu
), utf8_label
);
2457 w
= gtk_menu_item_new_with_label (utf8_label
);
2458 gtk_widget_set_sensitive (w
, FALSE
);
2459 if (utf8_label
) g_free (utf8_label
);
2461 else if (menu_separator_name_p (item
->name
))
2464 /* GTK only have one separator type. */
2465 w
= gtk_separator_menu_item_new ();
2469 w
= xg_create_one_menuitem (item
,
2471 item
->contents
? 0 : select_cb
,
2476 /* Create a possibly empty submenu for menu bar items, since some
2477 themes don't highlight items correctly without it. */
2478 if (item
->contents
|| menu_bar_p
)
2480 GtkWidget
*submenu
= create_menus (item
->contents
,
2491 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w
), submenu
);
2495 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu
), w
);
2496 gtk_widget_set_name (w
, MENU_ITEM_NAME
);
2502 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2503 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2504 with some text and buttons.
2505 F is the frame the created item belongs to.
2506 NAME is the name to use for the top widget.
2507 VAL is a widget_value structure describing items to be created.
2508 SELECT_CB is the callback to use when a menu item is selected or
2509 a dialog button is pressed.
2510 DEACTIVATE_CB is the callback to use when an item is deactivated.
2511 For a menu, when a sub menu is not shown anymore, for a dialog it is
2512 called when the dialog is popped down.
2513 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2515 Returns the widget created. */
2518 xg_create_widget (const char *type
, const char *name
, FRAME_PTR f
, widget_value
*val
,
2519 GCallback select_cb
, GCallback deactivate_cb
,
2520 GCallback highlight_cb
)
2523 int menu_bar_p
= strcmp (type
, "menubar") == 0;
2524 int pop_up_p
= strcmp (type
, "popup") == 0;
2526 if (strcmp (type
, "dialog") == 0)
2528 w
= create_dialog (val
, select_cb
, deactivate_cb
);
2529 xg_set_screen (w
, f
);
2530 gtk_window_set_transient_for (GTK_WINDOW (w
),
2531 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
2532 gtk_window_set_destroy_with_parent (GTK_WINDOW (w
), TRUE
);
2533 gtk_widget_set_name (w
, "emacs-dialog");
2534 gtk_window_set_modal (GTK_WINDOW (w
), TRUE
);
2536 else if (menu_bar_p
|| pop_up_p
)
2538 w
= create_menus (val
->contents
,
2550 /* Set the cursor to an arrow for popup menus when they are mapped.
2551 This is done by default for menu bar menus. */
2554 /* Must realize so the GdkWindow inside the widget is created. */
2555 gtk_widget_realize (w
);
2556 xg_set_cursor (w
, FRAME_X_DISPLAY_INFO (f
)->xg_cursor
);
2561 fprintf (stderr
, "bad type in xg_create_widget: %s, doing nothing\n",
2568 /* Return the label for menu item WITEM. */
2571 xg_get_menu_item_label (GtkMenuItem
*witem
)
2573 GtkLabel
*wlabel
= GTK_LABEL (XG_BIN_CHILD (witem
));
2574 return gtk_label_get_label (wlabel
);
2577 /* Return non-zero if the menu item WITEM has the text LABEL. */
2580 xg_item_label_same_p (GtkMenuItem
*witem
, const char *label
)
2583 char *utf8_label
= get_utf8_string (label
);
2584 const char *old_label
= witem
? xg_get_menu_item_label (witem
) : 0;
2586 if (! old_label
&& ! utf8_label
)
2588 else if (old_label
&& utf8_label
)
2589 is_same
= strcmp (utf8_label
, old_label
) == 0;
2591 if (utf8_label
) g_free (utf8_label
);
2596 /* Destroy widgets in LIST. */
2599 xg_destroy_widgets (GList
*list
)
2603 for (iter
= list
; iter
; iter
= g_list_next (iter
))
2605 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
2607 /* Destroying the widget will remove it from the container it is in. */
2608 gtk_widget_destroy (w
);
2612 /* Update the top level names in MENUBAR (i.e. not submenus).
2613 F is the frame the menu bar belongs to.
2614 *LIST is a list with the current menu bar names (menu item widgets).
2615 ITER is the item within *LIST that shall be updated.
2616 POS is the numerical position, starting at 0, of ITER in *LIST.
2617 VAL describes what the menu bar shall look like after the update.
2618 SELECT_CB is the callback to use when a menu item is selected.
2619 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2620 CL_DATA points to the callback data to be used for this menu bar.
2622 This function calls itself to walk through the menu bar names. */
2625 xg_update_menubar (GtkWidget
*menubar
,
2631 GCallback select_cb
,
2632 GCallback deactivate_cb
,
2633 GCallback highlight_cb
,
2634 xg_menu_cb_data
*cl_data
)
2636 if (! iter
&& ! val
)
2638 else if (iter
&& ! val
)
2640 /* Item(s) have been removed. Remove all remaining items. */
2641 xg_destroy_widgets (iter
);
2643 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2644 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
),
2645 gtk_menu_item_new_with_label (""),
2651 else if (! iter
&& val
)
2653 /* Item(s) added. Add all new items in one call. */
2654 create_menus (val
, f
, select_cb
, deactivate_cb
, highlight_cb
,
2655 0, 1, 0, menubar
, cl_data
, 0);
2661 /* Below this neither iter or val is NULL */
2662 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter
->data
), val
->name
))
2664 /* This item is still the same, check next item. */
2666 iter
= g_list_next (iter
);
2669 else /* This item is changed. */
2671 GtkMenuItem
*witem
= GTK_MENU_ITEM (iter
->data
);
2672 GtkMenuItem
*witem2
= 0;
2673 int val_in_menubar
= 0;
2674 int iter_in_new_menubar
= 0;
2678 /* See if the changed entry (val) is present later in the menu bar */
2680 iter2
&& ! val_in_menubar
;
2681 iter2
= g_list_next (iter2
))
2683 witem2
= GTK_MENU_ITEM (iter2
->data
);
2684 val_in_menubar
= xg_item_label_same_p (witem2
, val
->name
);
2687 /* See if the current entry (iter) is present later in the
2688 specification for the new menu bar. */
2689 for (cur
= val
; cur
&& ! iter_in_new_menubar
; cur
= cur
->next
)
2690 iter_in_new_menubar
= xg_item_label_same_p (witem
, cur
->name
);
2692 if (val_in_menubar
&& ! iter_in_new_menubar
)
2696 /* This corresponds to:
2701 g_object_ref (G_OBJECT (witem
));
2702 gtk_container_remove (GTK_CONTAINER (menubar
), GTK_WIDGET (witem
));
2703 gtk_widget_destroy (GTK_WIDGET (witem
));
2705 /* Must get new list since the old changed. */
2706 g_list_free (*list
);
2707 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2708 while (nr
-- > 0) iter
= g_list_next (iter
);
2710 else if (! val_in_menubar
&& ! iter_in_new_menubar
)
2712 /* This corresponds to:
2715 Rename B to X. This might seem to be a strange thing to do,
2716 since if there is a menu under B it will be totally wrong for X.
2717 But consider editing a C file. Then there is a C-mode menu
2718 (corresponds to B above).
2719 If then doing C-x C-f the minibuf menu (X above) replaces the
2720 C-mode menu. When returning from the minibuffer, we get
2721 back the C-mode menu. Thus we do:
2722 Rename B to X (C-mode to minibuf menu)
2723 Rename X to B (minibuf to C-mode menu).
2724 If the X menu hasn't been invoked, the menu under B
2725 is up to date when leaving the minibuffer. */
2726 GtkLabel
*wlabel
= GTK_LABEL (XG_BIN_CHILD (witem
));
2727 char *utf8_label
= get_utf8_string (val
->name
);
2728 GtkWidget
*submenu
= gtk_menu_item_get_submenu (witem
);
2730 gtk_label_set_text (wlabel
, utf8_label
);
2732 /* If this item has a submenu that has been detached, change
2733 the title in the WM decorations also. */
2734 if (submenu
&& gtk_menu_get_tearoff_state (GTK_MENU (submenu
)))
2735 /* Set the title of the detached window. */
2736 gtk_menu_set_title (GTK_MENU (submenu
), utf8_label
);
2738 if (utf8_label
) g_free (utf8_label
);
2739 iter
= g_list_next (iter
);
2743 else if (! val_in_menubar
&& iter_in_new_menubar
)
2745 /* This corresponds to:
2752 GtkWidget
*w
= xg_create_one_menuitem (val
,
2759 /* Create a possibly empty submenu for menu bar items, since some
2760 themes don't highlight items correctly without it. */
2761 GtkWidget
*submenu
= create_menus (NULL
, f
,
2762 select_cb
, deactivate_cb
,
2764 0, 0, 0, 0, cl_data
, 0);
2765 gtk_widget_set_name (w
, MENU_ITEM_NAME
);
2766 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
), w
, pos
);
2767 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w
), submenu
);
2769 g_list_free (*list
);
2770 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2771 while (nr
-- > 0) iter
= g_list_next (iter
);
2772 iter
= g_list_next (iter
);
2776 else /* if (val_in_menubar && iter_in_new_menubar) */
2779 /* This corresponds to:
2784 g_object_ref (G_OBJECT (witem2
));
2785 gtk_container_remove (GTK_CONTAINER (menubar
), GTK_WIDGET (witem2
));
2786 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
),
2787 GTK_WIDGET (witem2
), pos
);
2788 g_object_unref (G_OBJECT (witem2
));
2790 g_list_free (*list
);
2791 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2792 while (nr
-- > 0) iter
= g_list_next (iter
);
2793 if (iter
) iter
= g_list_next (iter
);
2799 /* Update the rest of the menu bar. */
2800 xg_update_menubar (menubar
, f
, list
, iter
, pos
, val
,
2801 select_cb
, deactivate_cb
, highlight_cb
, cl_data
);
2804 /* Update the menu item W so it corresponds to VAL.
2805 SELECT_CB is the callback to use when a menu item is selected.
2806 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2807 CL_DATA is the data to set in the widget for menu invocation. */
2810 xg_update_menu_item (widget_value
*val
,
2812 GCallback select_cb
,
2813 GCallback highlight_cb
,
2814 xg_menu_cb_data
*cl_data
)
2821 const char *old_label
= 0;
2822 const char *old_key
= 0;
2823 xg_menu_item_cb_data
*cb_data
;
2825 wchild
= XG_BIN_CHILD (w
);
2826 utf8_label
= get_utf8_string (val
->name
);
2827 utf8_key
= get_utf8_string (val
->key
);
2829 /* See if W is a menu item with a key. See make_menu_item above. */
2830 if (GTK_IS_HBOX (wchild
))
2832 GList
*list
= gtk_container_get_children (GTK_CONTAINER (wchild
));
2834 wlbl
= GTK_LABEL (list
->data
);
2835 wkey
= GTK_LABEL (list
->next
->data
);
2840 /* Remove the key and keep just the label. */
2841 g_object_ref (G_OBJECT (wlbl
));
2842 gtk_container_remove (GTK_CONTAINER (w
), wchild
);
2843 gtk_container_add (GTK_CONTAINER (w
), GTK_WIDGET (wlbl
));
2844 g_object_unref (G_OBJECT (wlbl
));
2849 else /* Just a label. */
2851 wlbl
= GTK_LABEL (wchild
);
2853 /* Check if there is now a key. */
2856 GtkWidget
*wtoadd
= make_widget_for_menu_item (utf8_label
, utf8_key
);
2857 GList
*list
= gtk_container_get_children (GTK_CONTAINER (wtoadd
));
2859 wlbl
= GTK_LABEL (list
->data
);
2860 wkey
= GTK_LABEL (list
->next
->data
);
2863 gtk_container_remove (GTK_CONTAINER (w
), wchild
);
2864 gtk_container_add (GTK_CONTAINER (w
), wtoadd
);
2869 if (wkey
) old_key
= gtk_label_get_label (wkey
);
2870 if (wlbl
) old_label
= gtk_label_get_label (wlbl
);
2872 if (wkey
&& utf8_key
&& (! old_key
|| strcmp (utf8_key
, old_key
) != 0))
2873 gtk_label_set_text (wkey
, utf8_key
);
2875 if (! old_label
|| strcmp (utf8_label
, old_label
) != 0)
2876 gtk_label_set_text (wlbl
, utf8_label
);
2878 if (utf8_key
) g_free (utf8_key
);
2879 if (utf8_label
) g_free (utf8_label
);
2881 if (! val
->enabled
&& gtk_widget_get_sensitive (w
))
2882 gtk_widget_set_sensitive (w
, FALSE
);
2883 else if (val
->enabled
&& ! gtk_widget_get_sensitive (w
))
2884 gtk_widget_set_sensitive (w
, TRUE
);
2886 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (w
),
2890 cb_data
->call_data
= val
->call_data
;
2891 cb_data
->help
= val
->help
;
2892 cb_data
->cl_data
= cl_data
;
2894 /* We assume the callback functions don't change. */
2895 if (val
->call_data
&& ! val
->contents
)
2897 /* This item shall have a select callback. */
2898 if (! cb_data
->select_id
)
2900 = g_signal_connect (G_OBJECT (w
), "activate",
2901 select_cb
, cb_data
);
2903 else if (cb_data
->select_id
)
2905 g_signal_handler_disconnect (w
, cb_data
->select_id
);
2906 cb_data
->select_id
= 0;
2911 /* Update the toggle menu item W so it corresponds to VAL. */
2914 xg_update_toggle_item (widget_value
*val
, GtkWidget
*w
)
2916 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), val
->selected
);
2919 /* Update the radio menu item W so it corresponds to VAL. */
2922 xg_update_radio_item (widget_value
*val
, GtkWidget
*w
)
2924 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), val
->selected
);
2927 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2928 SUBMENU may be NULL, in that case a new menu is created.
2929 F is the frame the menu bar belongs to.
2930 VAL describes the contents of the menu bar.
2931 SELECT_CB is the callback to use when a menu item is selected.
2932 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2933 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2934 CL_DATA is the call back data to use for any newly created items.
2936 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2940 xg_update_submenu (GtkWidget
*submenu
,
2943 GCallback select_cb
,
2944 GCallback deactivate_cb
,
2945 GCallback highlight_cb
,
2946 xg_menu_cb_data
*cl_data
)
2948 GtkWidget
*newsub
= submenu
;
2952 int has_tearoff_p
= 0;
2953 GList
*first_radio
= 0;
2956 list
= gtk_container_get_children (GTK_CONTAINER (submenu
));
2958 for (cur
= val
, iter
= list
;
2960 iter
= g_list_next (iter
), cur
= cur
->next
)
2962 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
2964 /* Skip tearoff items, they have no counterpart in val. */
2965 if (GTK_IS_TEAROFF_MENU_ITEM (w
))
2968 iter
= g_list_next (iter
);
2969 if (iter
) w
= GTK_WIDGET (iter
->data
);
2973 /* Remember first radio button in a group. If we get a mismatch in
2974 a radio group we must rebuild the whole group so that the connections
2975 in GTK becomes correct. */
2976 if (cur
->button_type
== BUTTON_TYPE_RADIO
&& ! first_radio
)
2978 else if (cur
->button_type
!= BUTTON_TYPE_RADIO
2979 && ! GTK_IS_RADIO_MENU_ITEM (w
))
2982 if (GTK_IS_SEPARATOR_MENU_ITEM (w
))
2984 if (! menu_separator_name_p (cur
->name
))
2987 else if (GTK_IS_CHECK_MENU_ITEM (w
))
2989 if (cur
->button_type
!= BUTTON_TYPE_TOGGLE
)
2991 xg_update_toggle_item (cur
, w
);
2992 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
2994 else if (GTK_IS_RADIO_MENU_ITEM (w
))
2996 if (cur
->button_type
!= BUTTON_TYPE_RADIO
)
2998 xg_update_radio_item (cur
, w
);
2999 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
3001 else if (GTK_IS_MENU_ITEM (w
))
3003 GtkMenuItem
*witem
= GTK_MENU_ITEM (w
);
3006 if (cur
->button_type
!= BUTTON_TYPE_NONE
||
3007 menu_separator_name_p (cur
->name
))
3010 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
3012 sub
= gtk_menu_item_get_submenu (witem
);
3013 if (sub
&& ! cur
->contents
)
3015 /* Not a submenu anymore. */
3016 g_object_ref (G_OBJECT (sub
));
3017 remove_submenu (witem
);
3018 gtk_widget_destroy (sub
);
3020 else if (cur
->contents
)
3024 nsub
= xg_update_submenu (sub
, f
, cur
->contents
,
3025 select_cb
, deactivate_cb
,
3026 highlight_cb
, cl_data
);
3028 /* If this item just became a submenu, we must set it. */
3030 gtk_menu_item_set_submenu (witem
, nsub
);
3035 /* Structural difference. Remove everything from here and down
3041 /* Remove widgets from first structual change. */
3044 /* If we are adding new menu items below, we must remove from
3045 first radio button so that radio groups become correct. */
3046 if (cur
&& first_radio
) xg_destroy_widgets (first_radio
);
3047 else xg_destroy_widgets (iter
);
3052 /* More items added. Create them. */
3053 newsub
= create_menus (cur
,
3066 if (list
) g_list_free (list
);
3071 /* Update the MENUBAR.
3072 F is the frame the menu bar belongs to.
3073 VAL describes the contents of the menu bar.
3074 If DEEP_P is non-zero, rebuild all but the top level menu names in
3075 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3076 SELECT_CB is the callback to use when a menu item is selected.
3077 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3078 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3081 xg_modify_menubar_widgets (GtkWidget
*menubar
, FRAME_PTR f
, widget_value
*val
,
3083 GCallback select_cb
, GCallback deactivate_cb
,
3084 GCallback highlight_cb
)
3086 xg_menu_cb_data
*cl_data
;
3087 GList
*list
= gtk_container_get_children (GTK_CONTAINER (menubar
));
3091 cl_data
= (xg_menu_cb_data
*) g_object_get_data (G_OBJECT (menubar
),
3094 xg_update_menubar (menubar
, f
, &list
, list
, 0, val
->contents
,
3095 select_cb
, deactivate_cb
, highlight_cb
, cl_data
);
3101 /* Update all sub menus.
3102 We must keep the submenus (GTK menu item widgets) since the
3103 X Window in the XEvent that activates the menu are those widgets. */
3105 /* Update cl_data, menu_item things in F may have changed. */
3106 update_cl_data (cl_data
, f
, highlight_cb
);
3108 for (cur
= val
->contents
; cur
; cur
= cur
->next
)
3113 GtkMenuItem
*witem
= 0;
3115 /* Find sub menu that corresponds to val and update it. */
3116 for (iter
= list
; iter
; iter
= g_list_next (iter
))
3118 witem
= GTK_MENU_ITEM (iter
->data
);
3119 if (xg_item_label_same_p (witem
, cur
->name
))
3121 sub
= gtk_menu_item_get_submenu (witem
);
3126 newsub
= xg_update_submenu (sub
,
3133 /* sub may still be NULL. If we just updated non deep and added
3134 a new menu bar item, it has no sub menu yet. So we set the
3135 newly created sub menu under witem. */
3136 if (newsub
!= sub
&& witem
!= 0)
3138 xg_set_screen (newsub
, f
);
3139 gtk_menu_item_set_submenu (witem
, newsub
);
3145 gtk_widget_show_all (menubar
);
3148 /* Callback called when the menu bar W is mapped.
3149 Used to find the height of the menu bar if we didn't get it
3150 after showing the widget. */
3153 menubar_map_cb (GtkWidget
*w
, gpointer user_data
)
3156 FRAME_PTR f
= (FRAME_PTR
) user_data
;
3157 gtk_widget_get_preferred_size (w
, NULL
, &req
);
3158 if (FRAME_MENUBAR_HEIGHT (f
) != req
.height
)
3160 FRAME_MENUBAR_HEIGHT (f
) = req
.height
;
3161 xg_height_or_width_changed (f
);
3165 /* Recompute all the widgets of frame F, when the menu bar has been
3166 changed. Value is non-zero if widgets were updated. */
3169 xg_update_frame_menubar (FRAME_PTR f
)
3171 struct x_output
*x
= f
->output_data
.x
;
3174 if (!x
->menubar_widget
|| gtk_widget_get_mapped (x
->menubar_widget
))
3177 if (x
->menubar_widget
&& gtk_widget_get_parent (x
->menubar_widget
))
3178 return 0; /* Already done this, happens for frames created invisible. */
3182 gtk_box_pack_start (GTK_BOX (x
->vbox_widget
), x
->menubar_widget
,
3184 gtk_box_reorder_child (GTK_BOX (x
->vbox_widget
), x
->menubar_widget
, 0);
3186 g_signal_connect (x
->menubar_widget
, "map", G_CALLBACK (menubar_map_cb
), f
);
3187 gtk_widget_show_all (x
->menubar_widget
);
3188 gtk_widget_get_preferred_size (x
->menubar_widget
, NULL
, &req
);
3190 /* If menu bar doesn't know its height yet, cheat a little so the frame
3191 doesn't jump so much when resized later in menubar_map_cb. */
3192 if (req
.height
== 0)
3195 if (FRAME_MENUBAR_HEIGHT (f
) != req
.height
)
3197 FRAME_MENUBAR_HEIGHT (f
) = req
.height
;
3198 xg_height_or_width_changed (f
);
3205 /* Get rid of the menu bar of frame F, and free its storage.
3206 This is used when deleting a frame, and when turning off the menu bar. */
3209 free_frame_menubar (FRAME_PTR f
)
3211 struct x_output
*x
= f
->output_data
.x
;
3213 if (x
->menubar_widget
)
3217 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
), x
->menubar_widget
);
3218 /* The menubar and its children shall be deleted when removed from
3220 x
->menubar_widget
= 0;
3221 FRAME_MENUBAR_HEIGHT (f
) = 0;
3222 xg_height_or_width_changed (f
);
3228 xg_event_is_for_menubar (FRAME_PTR f
, XEvent
*event
)
3230 struct x_output
*x
= f
->output_data
.x
;
3239 if (! x
->menubar_widget
) return 0;
3241 if (! (event
->xbutton
.x
>= 0
3242 && event
->xbutton
.x
< FRAME_PIXEL_WIDTH (f
)
3243 && event
->xbutton
.y
>= 0
3244 && event
->xbutton
.y
< f
->output_data
.x
->menubar_height
3245 && event
->xbutton
.same_screen
))
3248 gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
3249 gw
= gdk_x11_window_lookup_for_display (gdpy
, event
->xbutton
.window
);
3251 gevent
.any
.window
= gw
;
3252 gwdesc
= gtk_get_event_widget (&gevent
);
3253 if (! gwdesc
) return 0;
3254 if (! GTK_IS_MENU_BAR (gwdesc
)
3255 && ! GTK_IS_MENU_ITEM (gwdesc
)
3256 && ! gtk_widget_is_ancestor (x
->menubar_widget
, gwdesc
))
3259 list
= gtk_container_get_children (GTK_CONTAINER (x
->menubar_widget
));
3260 if (! list
) return 0;
3261 rec
.x
= event
->xbutton
.x
;
3262 rec
.y
= event
->xbutton
.y
;
3266 for (iter
= list
; iter
; iter
= g_list_next (iter
))
3268 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
3269 if (gtk_widget_get_mapped (w
) && gtk_widget_intersect (w
, &rec
, NULL
))
3273 return iter
== 0 ? 0 : 1;
3278 /***********************************************************************
3279 Scroll bar functions
3280 ***********************************************************************/
3283 /* Setting scroll bar values invokes the callback. Use this variable
3284 to indicate that callback should do nothing. */
3286 int xg_ignore_gtk_scrollbar
;
3288 /* The width of the scroll bar for the current theme. */
3290 static int scroll_bar_width_for_theme
;
3292 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3293 may be larger than 32 bits. Keep a mapping from integer index to widget
3294 pointers to get around the 32 bit limitation. */
3298 GtkWidget
**widgets
;
3303 /* Grow this much every time we need to allocate more */
3305 #define ID_TO_WIDGET_INCR 32
3307 /* Store the widget pointer W in id_to_widget and return the integer index. */
3310 xg_store_widget_in_map (GtkWidget
*w
)
3314 if (id_to_widget
.max_size
== id_to_widget
.used
)
3316 int new_size
= id_to_widget
.max_size
+ ID_TO_WIDGET_INCR
;
3318 id_to_widget
.widgets
= xrealloc (id_to_widget
.widgets
,
3319 sizeof (GtkWidget
*)*new_size
);
3321 for (i
= id_to_widget
.max_size
; i
< new_size
; ++i
)
3322 id_to_widget
.widgets
[i
] = 0;
3323 id_to_widget
.max_size
= new_size
;
3326 /* Just loop over the array and find a free place. After all,
3327 how many scroll bars are we creating? Should be a small number.
3328 The check above guarantees we will find a free place. */
3329 for (i
= 0; i
< id_to_widget
.max_size
; ++i
)
3331 if (! id_to_widget
.widgets
[i
])
3333 id_to_widget
.widgets
[i
] = w
;
3334 ++id_to_widget
.used
;
3340 /* Should never end up here */
3344 /* Remove pointer at IDX from id_to_widget.
3345 Called when scroll bar is destroyed. */
3348 xg_remove_widget_from_map (int idx
)
3350 if (idx
< id_to_widget
.max_size
&& id_to_widget
.widgets
[idx
] != 0)
3352 id_to_widget
.widgets
[idx
] = 0;
3353 --id_to_widget
.used
;
3357 /* Get the widget pointer at IDX from id_to_widget. */
3360 xg_get_widget_from_map (int idx
)
3362 if (idx
< id_to_widget
.max_size
&& id_to_widget
.widgets
[idx
] != 0)
3363 return id_to_widget
.widgets
[idx
];
3369 update_theme_scrollbar_width (void)
3372 GtkAdjustment
*vadj
;
3379 vadj
= gtk_adjustment_new (XG_SB_MIN
, XG_SB_MIN
, XG_SB_MAX
, 0.1, 0.1, 0.1);
3380 wscroll
= gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj
));
3381 g_object_ref_sink (G_OBJECT (wscroll
));
3382 gtk_widget_style_get (wscroll
, "slider-width", &w
, "trough-border", &b
, NULL
);
3383 gtk_widget_destroy (wscroll
);
3384 g_object_unref (G_OBJECT (wscroll
));
3387 scroll_bar_width_for_theme
= w
;
3391 xg_get_default_scrollbar_width (void)
3393 return scroll_bar_width_for_theme
;
3396 /* Return the scrollbar id for X Window WID on display DPY.
3397 Return -1 if WID not in id_to_widget. */
3400 xg_get_scroll_id_for_window (Display
*dpy
, Window wid
)
3405 w
= xg_win_to_widget (dpy
, wid
);
3409 for (idx
= 0; idx
< id_to_widget
.max_size
; ++idx
)
3410 if (id_to_widget
.widgets
[idx
] == w
)
3417 /* Callback invoked when scroll bar WIDGET is destroyed.
3418 DATA is the index into id_to_widget for WIDGET.
3419 We free pointer to last scroll bar values here and remove the index. */
3422 xg_gtk_scroll_destroy (GtkWidget
*widget
, gpointer data
)
3424 int id
= (intptr_t) data
;
3425 xg_remove_widget_from_map (id
);
3428 /* Create a scroll bar widget for frame F. Store the scroll bar
3430 SCROLL_CALLBACK is the callback to invoke when the value of the
3432 END_CALLBACK is the callback to invoke when scrolling ends.
3433 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3434 to set resources for the widget. */
3437 xg_create_scroll_bar (FRAME_PTR f
,
3438 struct scroll_bar
*bar
,
3439 GCallback scroll_callback
,
3440 GCallback end_callback
,
3441 const char *scroll_bar_name
)
3447 GtkAdjustment
*vadj
;
3452 /* Page, step increment values are not so important here, they
3453 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3454 vadj
= gtk_adjustment_new (XG_SB_MIN
, XG_SB_MIN
, XG_SB_MAX
,
3457 wscroll
= gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj
));
3458 webox
= gtk_event_box_new ();
3459 gtk_widget_set_name (wscroll
, scroll_bar_name
);
3461 gtk_range_set_update_policy (GTK_RANGE (wscroll
), GTK_UPDATE_CONTINUOUS
);
3463 g_object_set_data (G_OBJECT (wscroll
), XG_FRAME_DATA
, (gpointer
)f
);
3465 scroll_id
= xg_store_widget_in_map (wscroll
);
3467 g_signal_connect (G_OBJECT (wscroll
),
3469 G_CALLBACK (xg_gtk_scroll_destroy
),
3470 (gpointer
) scroll_id
);
3471 g_signal_connect (G_OBJECT (wscroll
),
3475 g_signal_connect (G_OBJECT (wscroll
),
3476 "button-release-event",
3480 /* The scroll bar widget does not draw on a window of its own. Instead
3481 it draws on the parent window, in this case the edit widget. So
3482 whenever the edit widget is cleared, the scroll bar needs to redraw
3483 also, which causes flicker. Put an event box between the edit widget
3484 and the scroll bar, so the scroll bar instead draws itself on the
3485 event box window. */
3486 gtk_fixed_put (GTK_FIXED (f
->output_data
.x
->edit_widget
), webox
, -1, -1);
3487 gtk_container_add (GTK_CONTAINER (webox
), wscroll
);
3490 /* Set the cursor to an arrow. */
3491 xg_set_cursor (webox
, FRAME_X_DISPLAY_INFO (f
)->xg_cursor
);
3493 bar
->x_window
= scroll_id
;
3496 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3499 xg_remove_scroll_bar (FRAME_PTR f
, int scrollbar_id
)
3501 GtkWidget
*w
= xg_get_widget_from_map (scrollbar_id
);
3504 GtkWidget
*wparent
= gtk_widget_get_parent (w
);
3505 gtk_widget_destroy (w
);
3506 gtk_widget_destroy (wparent
);
3507 SET_FRAME_GARBAGED (f
);
3511 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3513 TOP/LEFT are the new pixel positions where the bar shall appear.
3514 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3517 xg_update_scrollbar_pos (FRAME_PTR f
,
3525 GtkWidget
*wscroll
= xg_get_widget_from_map (scrollbar_id
);
3529 GtkWidget
*wfixed
= f
->output_data
.x
->edit_widget
;
3530 GtkWidget
*wparent
= gtk_widget_get_parent (wscroll
);
3533 /* Clear out old position. */
3534 int oldx
= -1, oldy
= -1, oldw
, oldh
;
3535 if (gtk_widget_get_parent (wparent
) == wfixed
)
3537 gtk_container_child_get (GTK_CONTAINER (wfixed
), wparent
,
3538 "x", &oldx
, "y", &oldy
, NULL
);
3539 gtk_widget_get_size_request (wscroll
, &oldw
, &oldh
);
3542 /* Move and resize to new values. */
3543 gtk_fixed_move (GTK_FIXED (wfixed
), wparent
, left
, top
);
3544 gtk_widget_style_get (wscroll
, "min-slider-length", &msl
, NULL
);
3547 /* No room. Hide scroll bar as some themes output a warning if
3548 the height is less than the min size. */
3549 gtk_widget_hide (wparent
);
3550 gtk_widget_hide (wscroll
);
3554 gtk_widget_show_all (wparent
);
3555 gtk_widget_set_size_request (wscroll
, width
, height
);
3557 gtk_widget_queue_draw (wfixed
);
3558 gdk_window_process_all_updates ();
3559 if (oldx
!= -1 && oldw
> 0 && oldh
> 0)
3561 /* Clear under old scroll bar position. This must be done after
3562 the gtk_widget_queue_draw and gdk_window_process_all_updates
3564 x_clear_area (FRAME_X_DISPLAY (f
),
3566 oldx
, oldy
, oldw
, oldh
, 0);
3569 /* GTK does not redraw until the main loop is entered again, but
3570 if there are no X events pending we will not enter it. So we sync
3571 here to get some events. */
3574 SET_FRAME_GARBAGED (f
);
3575 cancel_mouse_face (f
);
3579 /* Get the current value of the range, truncated to an integer. */
3582 int_gtk_range_get_value (GtkRange
*range
)
3584 return gtk_range_get_value (range
);
3588 /* Set the thumb size and position of scroll bar BAR. We are currently
3589 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3592 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar
*bar
,
3597 GtkWidget
*wscroll
= xg_get_widget_from_map (bar
->x_window
);
3599 FRAME_PTR f
= XFRAME (WINDOW_FRAME (XWINDOW (bar
->window
)));
3601 if (wscroll
&& NILP (bar
->dragging
))
3611 adj
= gtk_range_get_adjustment (GTK_RANGE (wscroll
));
3613 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3614 rather than the real portion value. This makes the thumb less likely
3615 to resize and that looks better. */
3616 portion
= WINDOW_TOTAL_LINES (XWINDOW (bar
->window
)) * 30;
3617 /* When the thumb is at the bottom, position == whole.
3618 So we need to increase `whole' to make space for the thumb. */
3625 top
= (gdouble
) position
/ whole
;
3626 shown
= (gdouble
) portion
/ whole
;
3629 size
= shown
* XG_SB_RANGE
;
3630 size
= min (size
, XG_SB_RANGE
);
3631 size
= max (size
, 1);
3633 value
= top
* XG_SB_RANGE
;
3634 value
= min (value
, XG_SB_MAX
- size
);
3635 value
= max (value
, XG_SB_MIN
);
3637 /* Assume all lines are of equal size. */
3638 new_step
= size
/ max (1, FRAME_LINES (f
));
3640 old_size
= gtk_adjustment_get_page_size (adj
);
3641 if (old_size
!= size
)
3643 int old_step
= gtk_adjustment_get_step_increment (adj
);
3644 if (old_step
!= new_step
)
3646 gtk_adjustment_set_page_size (adj
, size
);
3647 gtk_adjustment_set_step_increment (adj
, new_step
);
3648 /* Assume a page increment is about 95% of the page size */
3649 gtk_adjustment_set_page_increment (adj
, size
- size
/ 20);
3654 if (changed
|| int_gtk_range_get_value (GTK_RANGE (wscroll
)) != value
)
3658 /* gtk_range_set_value invokes the callback. Set
3659 ignore_gtk_scrollbar to make the callback do nothing */
3660 xg_ignore_gtk_scrollbar
= 1;
3662 if (int_gtk_range_get_value (GTK_RANGE (wscroll
)) != value
)
3663 gtk_range_set_value (GTK_RANGE (wscroll
), (gdouble
)value
);
3665 gtk_adjustment_changed (adj
);
3667 xg_ignore_gtk_scrollbar
= 0;
3674 /* Return non-zero if EVENT is for a scroll bar in frame F.
3675 When the same X window is used for several Gtk+ widgets, we cannot
3676 say for sure based on the X window alone if an event is for the
3677 frame. This function does additional checks.
3679 Return non-zero if the event is for a scroll bar, zero otherwise. */
3682 xg_event_is_for_scrollbar (FRAME_PTR f
, XEvent
*event
)
3686 if (f
&& event
->type
== ButtonPress
&& event
->xbutton
.button
< 4)
3688 /* Check if press occurred outside the edit widget. */
3689 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
3690 retval
= gdk_display_get_window_at_pointer (gdpy
, NULL
, NULL
)
3691 != gtk_widget_get_window (f
->output_data
.x
->edit_widget
);
3694 && ((event
->type
== ButtonRelease
&& event
->xbutton
.button
< 4)
3695 || event
->type
== MotionNotify
))
3697 /* If we are releasing or moving the scroll bar, it has the grab. */
3698 GtkWidget
*w
= gtk_grab_get_current ();
3699 retval
= w
!= 0 && GTK_IS_SCROLLBAR (w
);
3707 /***********************************************************************
3709 ***********************************************************************/
3710 /* The key for the data we put in the GtkImage widgets. The data is
3711 the image used by Emacs. We use this to see if we need to update
3712 the GtkImage with a new image. */
3713 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3715 /* The key for storing the latest modifiers so the activate callback can
3717 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3719 /* The key for storing the button widget in its proxy menu item. */
3720 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3722 /* The key for the data we put in the GtkImage widgets. The data is
3723 the stock name used by Emacs. We use this to see if we need to update
3724 the GtkImage with a new image. */
3725 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3727 /* As above, but this is used for named theme widgets, as opposed to
3729 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3731 /* Callback function invoked when a tool bar item is pressed.
3732 W is the button widget in the tool bar that got pressed,
3733 CLIENT_DATA is an integer that is the index of the button in the
3734 tool bar. 0 is the first button. */
3737 xg_tool_bar_button_cb (GtkWidget
*widget
,
3738 GdkEventButton
*event
,
3741 intptr_t state
= event
->state
;
3742 gpointer ptr
= (gpointer
) state
;
3743 g_object_set_data (G_OBJECT (widget
), XG_TOOL_BAR_LAST_MODIFIER
, ptr
);
3748 /* Callback function invoked when a tool bar item is pressed.
3749 W is the button widget in the tool bar that got pressed,
3750 CLIENT_DATA is an integer that is the index of the button in the
3751 tool bar. 0 is the first button. */
3754 xg_tool_bar_callback (GtkWidget
*w
, gpointer client_data
)
3756 intptr_t idx
= (intptr_t) client_data
;
3757 gpointer gmod
= g_object_get_data (G_OBJECT (w
), XG_TOOL_BAR_LAST_MODIFIER
);
3758 intptr_t mod
= (intptr_t) gmod
;
3760 FRAME_PTR f
= (FRAME_PTR
) g_object_get_data (G_OBJECT (w
), XG_FRAME_DATA
);
3761 Lisp_Object key
, frame
;
3762 struct input_event event
;
3765 if (! f
|| ! f
->n_tool_bar_items
|| NILP (f
->tool_bar_items
))
3768 idx
*= TOOL_BAR_ITEM_NSLOTS
;
3770 key
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_KEY
);
3771 XSETFRAME (frame
, f
);
3773 /* We generate two events here. The first one is to set the prefix
3774 to `(tool_bar)', see keyboard.c. */
3775 event
.kind
= TOOL_BAR_EVENT
;
3776 event
.frame_or_window
= frame
;
3778 kbd_buffer_store_event (&event
);
3780 event
.kind
= TOOL_BAR_EVENT
;
3781 event
.frame_or_window
= frame
;
3783 /* Convert between the modifier bits GDK uses and the modifier bits
3784 Emacs uses. This assumes GDK and X masks are the same, which they are when
3786 event
.modifiers
= x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f
), mod
);
3787 kbd_buffer_store_event (&event
);
3789 /* Return focus to the frame after we have clicked on a detached
3791 Fx_focus_frame (frame
);
3794 /* Callback function invoked when a tool bar item is pressed in a detached
3795 tool bar or the overflow drop down menu.
3796 We just call xg_tool_bar_callback.
3797 W is the menu item widget that got pressed,
3798 CLIENT_DATA is an integer that is the index of the button in the
3799 tool bar. 0 is the first button. */
3802 xg_tool_bar_proxy_callback (GtkWidget
*w
, gpointer client_data
)
3804 GtkWidget
*wbutton
= GTK_WIDGET (g_object_get_data (G_OBJECT (w
),
3805 XG_TOOL_BAR_PROXY_BUTTON
));
3806 xg_tool_bar_callback (wbutton
, client_data
);
3811 xg_tool_bar_help_callback (GtkWidget
*w
,
3812 GdkEventCrossing
*event
,
3813 gpointer client_data
);
3815 /* This callback is called when a help is to be shown for an item in
3816 the detached tool bar when the detached tool bar it is not expanded. */
3819 xg_tool_bar_proxy_help_callback (GtkWidget
*w
,
3820 GdkEventCrossing
*event
,
3821 gpointer client_data
)
3823 GtkWidget
*wbutton
= GTK_WIDGET (g_object_get_data (G_OBJECT (w
),
3824 XG_TOOL_BAR_PROXY_BUTTON
));
3826 return xg_tool_bar_help_callback (wbutton
, event
, client_data
);
3830 xg_get_tool_bar_widgets (GtkWidget
*vb
, GtkWidget
**wimage
)
3832 GList
*clist
= gtk_container_get_children (GTK_CONTAINER (vb
));
3833 GtkWidget
*c1
= (GtkWidget
*) clist
->data
;
3834 GtkWidget
*c2
= clist
->next
? (GtkWidget
*) clist
->next
->data
: NULL
;
3836 *wimage
= GTK_IS_IMAGE (c1
) ? c1
: c2
;
3837 g_list_free (clist
);
3838 return GTK_IS_LABEL (c1
) ? c1
: c2
;
3842 /* This callback is called when a tool item should create a proxy item,
3843 such as for the overflow menu. Also called when the tool bar is detached.
3844 If we don't create a proxy menu item, the detached tool bar will be
3848 xg_tool_bar_menu_proxy (GtkToolItem
*toolitem
, gpointer user_data
)
3850 GtkButton
*wbutton
= GTK_BUTTON (XG_BIN_CHILD (XG_BIN_CHILD (toolitem
)));
3851 GtkWidget
*vb
= XG_BIN_CHILD (wbutton
);
3853 GtkLabel
*wlbl
= GTK_LABEL (xg_get_tool_bar_widgets (vb
, &c1
));
3854 GtkImage
*wimage
= GTK_IMAGE (c1
);
3855 GtkWidget
*wmenuitem
= gtk_image_menu_item_new_with_label
3856 (wlbl
? gtk_label_get_text (wlbl
) : "");
3857 GtkWidget
*wmenuimage
;
3860 if (gtk_button_get_use_stock (wbutton
))
3861 wmenuimage
= gtk_image_new_from_stock (gtk_button_get_label (wbutton
),
3862 GTK_ICON_SIZE_MENU
);
3865 GtkSettings
*settings
= gtk_widget_get_settings (GTK_WIDGET (wbutton
));
3866 GtkImageType store_type
= gtk_image_get_storage_type (wimage
);
3868 g_object_set (G_OBJECT (settings
), "gtk-menu-images", TRUE
, NULL
);
3870 if (store_type
== GTK_IMAGE_STOCK
)
3873 gtk_image_get_stock (wimage
, &stock_id
, NULL
);
3874 wmenuimage
= gtk_image_new_from_stock (stock_id
, GTK_ICON_SIZE_MENU
);
3876 else if (store_type
== GTK_IMAGE_ICON_SET
)
3878 GtkIconSet
*icon_set
;
3879 gtk_image_get_icon_set (wimage
, &icon_set
, NULL
);
3880 wmenuimage
= gtk_image_new_from_icon_set (icon_set
,
3881 GTK_ICON_SIZE_MENU
);
3883 else if (store_type
== GTK_IMAGE_PIXBUF
)
3888 gtk_icon_size_lookup_for_settings (settings
, GTK_ICON_SIZE_MENU
,
3891 GdkPixbuf
*src_pixbuf
, *dest_pixbuf
;
3893 src_pixbuf
= gtk_image_get_pixbuf (wimage
);
3894 dest_pixbuf
= gdk_pixbuf_scale_simple (src_pixbuf
, width
, height
,
3895 GDK_INTERP_BILINEAR
);
3897 wmenuimage
= gtk_image_new_from_pixbuf (dest_pixbuf
);
3901 fprintf (stderr
, "internal error: GTK_IMAGE_PIXBUF failed\n");
3905 else if (store_type
== GTK_IMAGE_ICON_NAME
)
3907 const gchar
*icon_name
;
3908 GtkIconSize icon_size
;
3910 gtk_image_get_icon_name (wimage
, &icon_name
, &icon_size
);
3911 wmenuimage
= gtk_image_new_from_icon_name (icon_name
,
3912 GTK_ICON_SIZE_MENU
);
3916 fprintf (stderr
, "internal error: store_type is %d\n", store_type
);
3921 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem
), wmenuimage
);
3923 g_signal_connect (G_OBJECT (wmenuitem
),
3925 G_CALLBACK (xg_tool_bar_proxy_callback
),
3929 g_object_set_data (G_OBJECT (wmenuitem
), XG_TOOL_BAR_PROXY_BUTTON
,
3930 (gpointer
) wbutton
);
3931 gtk_tool_item_set_proxy_menu_item (toolitem
, "Emacs toolbar item", wmenuitem
);
3932 gtk_widget_set_sensitive (wmenuitem
,
3933 gtk_widget_get_sensitive (GTK_WIDGET (wbutton
)));
3935 /* Use enter/leave notify to show help. We use the events
3936 rather than the GtkButton specific signals "enter" and
3937 "leave", so we can have only one callback. The event
3938 will tell us what kind of event it is. */
3939 g_signal_connect (G_OBJECT (wmenuitem
),
3940 "enter-notify-event",
3941 G_CALLBACK (xg_tool_bar_proxy_help_callback
),
3943 g_signal_connect (G_OBJECT (wmenuitem
),
3944 "leave-notify-event",
3945 G_CALLBACK (xg_tool_bar_proxy_help_callback
),
3951 /* This callback is called when a tool bar is detached. We must set
3952 the height of the tool bar to zero when this happens so frame sizes
3953 are correctly calculated.
3954 WBOX is the handle box widget that enables detach/attach of the tool bar.
3955 W is the tool bar widget.
3956 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3959 xg_tool_bar_detach_callback (GtkHandleBox
*wbox
,
3961 gpointer client_data
)
3963 FRAME_PTR f
= (FRAME_PTR
) client_data
;
3965 g_object_set (G_OBJECT (w
), "show-arrow", !x_gtk_whole_detached_tool_bar
,
3970 GtkRequisition req
, req2
;
3971 FRAME_X_OUTPUT (f
)->toolbar_detached
= 1;
3972 gtk_widget_get_preferred_size (GTK_WIDGET (wbox
), NULL
, &req
);
3973 gtk_widget_get_preferred_size (w
, NULL
, &req2
);
3974 req
.width
-= req2
.width
;
3975 req
.height
-= req2
.height
;
3976 if (FRAME_TOOLBAR_TOP_HEIGHT (f
) != 0)
3977 FRAME_TOOLBAR_TOP_HEIGHT (f
) = req
.height
;
3978 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) != 0)
3979 FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = req
.height
;
3980 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f
) != 0)
3981 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = req
.width
;
3982 else if (FRAME_TOOLBAR_LEFT_WIDTH (f
) != 0)
3983 FRAME_TOOLBAR_LEFT_WIDTH (f
) = req
.width
;
3984 xg_height_or_width_changed (f
);
3988 /* This callback is called when a tool bar is reattached. We must set
3989 the height of the tool bar when this happens so frame sizes
3990 are correctly calculated.
3991 WBOX is the handle box widget that enables detach/attach of the tool bar.
3992 W is the tool bar widget.
3993 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3996 xg_tool_bar_attach_callback (GtkHandleBox
*wbox
,
3998 gpointer client_data
)
4000 FRAME_PTR f
= (FRAME_PTR
) client_data
;
4001 g_object_set (G_OBJECT (w
), "show-arrow", TRUE
, NULL
);
4005 GtkRequisition req
, req2
;
4006 FRAME_X_OUTPUT (f
)->toolbar_detached
= 0;
4007 gtk_widget_get_preferred_size (GTK_WIDGET (wbox
), NULL
, &req
);
4008 gtk_widget_get_preferred_size (w
, NULL
, &req2
);
4009 req
.width
+= req2
.width
;
4010 req
.height
+= req2
.height
;
4011 if (FRAME_TOOLBAR_TOP_HEIGHT (f
) != 0)
4012 FRAME_TOOLBAR_TOP_HEIGHT (f
) = req
.height
;
4013 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) != 0)
4014 FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = req
.height
;
4015 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f
) != 0)
4016 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = req
.width
;
4017 else if (FRAME_TOOLBAR_LEFT_WIDTH (f
) != 0)
4018 FRAME_TOOLBAR_LEFT_WIDTH (f
) = req
.width
;
4019 xg_height_or_width_changed (f
);
4023 /* This callback is called when the mouse enters or leaves a tool bar item.
4024 It is used for displaying and hiding the help text.
4025 W is the tool bar item, a button.
4026 EVENT is either an enter event or leave event.
4027 CLIENT_DATA is an integer that is the index of the button in the
4028 tool bar. 0 is the first button.
4030 Returns FALSE to tell GTK to keep processing this event. */
4033 xg_tool_bar_help_callback (GtkWidget
*w
,
4034 GdkEventCrossing
*event
,
4035 gpointer client_data
)
4037 intptr_t idx
= (intptr_t) client_data
;
4038 FRAME_PTR f
= (FRAME_PTR
) g_object_get_data (G_OBJECT (w
), XG_FRAME_DATA
);
4039 Lisp_Object help
, frame
;
4041 if (! f
|| ! f
->n_tool_bar_items
|| NILP (f
->tool_bar_items
))
4044 if (event
->type
== GDK_ENTER_NOTIFY
)
4046 idx
*= TOOL_BAR_ITEM_NSLOTS
;
4047 help
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_HELP
);
4050 help
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_CAPTION
);
4055 XSETFRAME (frame
, f
);
4056 kbd_buffer_store_help_event (frame
, help
);
4062 /* This callback is called when a tool bar item shall be redrawn.
4063 It modifies the expose event so that the GtkImage widget redraws the
4064 whole image. This to overcome a bug that makes GtkImage draw the image
4065 in the wrong place when it tries to redraw just a part of the image.
4066 W is the GtkImage to be redrawn.
4067 EVENT is the expose event for W.
4068 CLIENT_DATA is unused.
4070 Returns FALSE to tell GTK to keep processing this event. */
4074 xg_tool_bar_item_expose_callback (GtkWidget
*w
,
4075 GdkEventExpose
*event
,
4076 gpointer client_data
)
4080 gdk_drawable_get_size (event
->window
, &width
, &height
);
4081 event
->area
.x
-= width
> event
->area
.width
? width
-event
->area
.width
: 0;
4082 event
->area
.y
-= height
> event
->area
.height
? height
-event
->area
.height
: 0;
4084 event
->area
.x
= max (0, event
->area
.x
);
4085 event
->area
.y
= max (0, event
->area
.y
);
4087 event
->area
.width
= max (width
, event
->area
.width
);
4088 event
->area
.height
= max (height
, event
->area
.height
);
4094 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4095 #define toolbar_set_orientation(w, o) \
4096 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4098 #define toolbar_set_orientation(w, o) \
4099 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4102 /* Attach a tool bar to frame F. */
4105 xg_pack_tool_bar (FRAME_PTR f
, Lisp_Object pos
)
4107 struct x_output
*x
= f
->output_data
.x
;
4108 int into_hbox
= EQ (pos
, Qleft
) || EQ (pos
, Qright
);
4110 toolbar_set_orientation (x
->toolbar_widget
,
4112 ? GTK_ORIENTATION_VERTICAL
4113 : GTK_ORIENTATION_HORIZONTAL
);
4114 if (!x
->handlebox_widget
)
4116 x
->handlebox_widget
= gtk_handle_box_new ();
4117 g_signal_connect (G_OBJECT (x
->handlebox_widget
), "child-detached",
4118 G_CALLBACK (xg_tool_bar_detach_callback
), f
);
4119 g_signal_connect (G_OBJECT (x
->handlebox_widget
), "child-attached",
4120 G_CALLBACK (xg_tool_bar_attach_callback
), f
);
4121 gtk_container_add (GTK_CONTAINER (x
->handlebox_widget
),
4127 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x
->handlebox_widget
),
4129 gtk_box_pack_start (GTK_BOX (x
->hbox_widget
), x
->handlebox_widget
,
4132 if (EQ (pos
, Qleft
))
4133 gtk_box_reorder_child (GTK_BOX (x
->hbox_widget
),
4134 x
->handlebox_widget
,
4136 x
->toolbar_in_hbox
= 1;
4140 int vbox_pos
= x
->menubar_widget
? 1 : 0;
4141 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x
->handlebox_widget
),
4143 gtk_box_pack_start (GTK_BOX (x
->vbox_widget
), x
->handlebox_widget
,
4147 gtk_box_reorder_child (GTK_BOX (x
->vbox_widget
),
4148 x
->handlebox_widget
,
4150 x
->toolbar_in_hbox
= 0;
4154 /* Create a tool bar for frame F. */
4157 xg_create_tool_bar (FRAME_PTR f
)
4159 struct x_output
*x
= f
->output_data
.x
;
4161 x
->toolbar_widget
= gtk_toolbar_new ();
4162 x
->toolbar_detached
= 0;
4164 gtk_widget_set_name (x
->toolbar_widget
, "emacs-toolbar");
4166 gtk_toolbar_set_style (GTK_TOOLBAR (x
->toolbar_widget
), GTK_TOOLBAR_ICONS
);
4167 toolbar_set_orientation (x
->toolbar_widget
, GTK_ORIENTATION_HORIZONTAL
);
4171 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4173 /* Find the right-to-left image named by RTL in the tool bar images for F.
4174 Returns IMAGE if RTL is not found. */
4177 find_rtl_image (FRAME_PTR f
, Lisp_Object image
, Lisp_Object rtl
)
4180 Lisp_Object file
, rtl_name
;
4181 struct gcpro gcpro1
, gcpro2
;
4182 GCPRO2 (file
, rtl_name
);
4184 rtl_name
= Ffile_name_nondirectory (rtl
);
4186 for (i
= 0; i
< f
->n_tool_bar_items
; ++i
)
4188 Lisp_Object rtl_image
= PROP (TOOL_BAR_ITEM_IMAGES
);
4189 if (!NILP (file
= file_for_image (rtl_image
)))
4191 file
= call1 (intern ("file-name-sans-extension"),
4192 Ffile_name_nondirectory (file
));
4193 if (EQ (Fequal (file
, rtl_name
), Qt
))
4204 static GtkToolItem
*
4205 xg_make_tool_item (FRAME_PTR f
,
4207 GtkWidget
**wbutton
,
4209 int i
, int horiz
, int text_image
)
4211 GtkToolItem
*ti
= gtk_tool_item_new ();
4212 GtkWidget
*vb
= horiz
? gtk_hbox_new (FALSE
, 0) : gtk_vbox_new (FALSE
, 0);
4213 GtkWidget
*wb
= gtk_button_new ();
4214 GtkWidget
*weventbox
= gtk_event_box_new ();
4216 if (wimage
&& !text_image
)
4217 gtk_box_pack_start (GTK_BOX (vb
), wimage
, TRUE
, TRUE
, 0);
4219 gtk_box_pack_start (GTK_BOX (vb
), gtk_label_new (label
), TRUE
, TRUE
, 0);
4220 if (wimage
&& text_image
)
4221 gtk_box_pack_start (GTK_BOX (vb
), wimage
, TRUE
, TRUE
, 0);
4223 gtk_button_set_focus_on_click (GTK_BUTTON (wb
), FALSE
);
4224 gtk_button_set_relief (GTK_BUTTON (wb
), GTK_RELIEF_NONE
);
4225 gtk_container_add (GTK_CONTAINER (wb
), vb
);
4226 gtk_container_add (GTK_CONTAINER (weventbox
), wb
);
4227 gtk_container_add (GTK_CONTAINER (ti
), weventbox
);
4232 gpointer gi
= (gpointer
) ii
;
4234 g_signal_connect (G_OBJECT (ti
), "create-menu-proxy",
4235 G_CALLBACK (xg_tool_bar_menu_proxy
),
4238 g_signal_connect (G_OBJECT (wb
), "clicked",
4239 G_CALLBACK (xg_tool_bar_callback
),
4242 g_object_set_data (G_OBJECT (weventbox
), XG_FRAME_DATA
, (gpointer
)f
);
4245 /* Catch expose events to overcome an annoying redraw bug, see
4246 comment for xg_tool_bar_item_expose_callback. */
4247 g_signal_connect (G_OBJECT (ti
),
4249 G_CALLBACK (xg_tool_bar_item_expose_callback
),
4252 gtk_tool_item_set_homogeneous (ti
, FALSE
);
4254 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
4255 no distinction based on modifiers in the activate callback,
4256 so we have to do it ourselves. */
4257 g_signal_connect (wb
, "button-release-event",
4258 G_CALLBACK (xg_tool_bar_button_cb
),
4261 g_object_set_data (G_OBJECT (wb
), XG_FRAME_DATA
, (gpointer
)f
);
4263 /* Use enter/leave notify to show help. We use the events
4264 rather than the GtkButton specific signals "enter" and
4265 "leave", so we can have only one callback. The event
4266 will tell us what kind of event it is. */
4267 /* The EMACS_INT cast avoids a warning. */
4268 g_signal_connect (G_OBJECT (weventbox
),
4269 "enter-notify-event",
4270 G_CALLBACK (xg_tool_bar_help_callback
),
4272 g_signal_connect (G_OBJECT (weventbox
),
4273 "leave-notify-event",
4274 G_CALLBACK (xg_tool_bar_help_callback
),
4278 if (wbutton
) *wbutton
= wb
;
4284 xg_tool_item_stale_p (GtkWidget
*wbutton
, const char *stock_name
,
4285 const char *icon_name
, const struct image
*img
,
4286 const char *label
, int horiz
)
4290 GtkWidget
*vb
= XG_BIN_CHILD (wbutton
);
4291 GtkWidget
*wlbl
= xg_get_tool_bar_widgets (vb
, &wimage
);
4293 /* Check if the tool icon matches. */
4296 old
= g_object_get_data (G_OBJECT (wimage
),
4297 XG_TOOL_BAR_STOCK_NAME
);
4298 if (!old
|| strcmp (old
, stock_name
))
4303 old
= g_object_get_data (G_OBJECT (wimage
),
4304 XG_TOOL_BAR_ICON_NAME
);
4305 if (!old
|| strcmp (old
, icon_name
))
4310 gpointer gold_img
= g_object_get_data (G_OBJECT (wimage
),
4311 XG_TOOL_BAR_IMAGE_DATA
);
4312 Pixmap old_img
= (Pixmap
) gold_img
;
4313 if (old_img
!= img
->pixmap
)
4317 /* Check button configuration and label. */
4318 if ((horiz
? GTK_IS_VBOX (vb
) : GTK_IS_HBOX (vb
))
4319 || (label
? (wlbl
== NULL
) : (wlbl
!= NULL
)))
4322 /* Ensure label is correct. */
4324 gtk_label_set_text (GTK_LABEL (wlbl
), label
);
4329 xg_update_tool_bar_sizes (FRAME_PTR f
)
4331 struct x_output
*x
= f
->output_data
.x
;
4333 int nl
= 0, nr
= 0, nt
= 0, nb
= 0;
4335 gtk_widget_get_preferred_size (GTK_WIDGET (x
->handlebox_widget
), NULL
, &req
);
4336 if (x
->toolbar_in_hbox
)
4339 gtk_container_child_get (GTK_CONTAINER (x
->hbox_widget
),
4340 x
->handlebox_widget
,
4341 "position", &pos
, NULL
);
4342 if (pos
== 0) nl
= req
.width
;
4343 else nr
= req
.width
;
4348 gtk_container_child_get (GTK_CONTAINER (x
->vbox_widget
),
4349 x
->handlebox_widget
,
4350 "position", &pos
, NULL
);
4351 if (pos
== 0 || (pos
== 1 && x
->menubar_widget
)) nt
= req
.height
;
4352 else nb
= req
.height
;
4355 if (nl
!= FRAME_TOOLBAR_LEFT_WIDTH (f
)
4356 || nr
!= FRAME_TOOLBAR_RIGHT_WIDTH (f
)
4357 || nt
!= FRAME_TOOLBAR_TOP_HEIGHT (f
)
4358 || nb
!= FRAME_TOOLBAR_BOTTOM_HEIGHT (f
))
4360 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = FRAME_TOOLBAR_LEFT_WIDTH (f
)
4361 = FRAME_TOOLBAR_TOP_HEIGHT (f
) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = 0;
4362 FRAME_TOOLBAR_LEFT_WIDTH (f
) = nl
;
4363 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = nr
;
4364 FRAME_TOOLBAR_TOP_HEIGHT (f
) = nt
;
4365 FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = nb
;
4373 /* Update the tool bar for frame F. Add new buttons and remove old. */
4376 update_frame_tool_bar (FRAME_PTR f
)
4379 struct x_output
*x
= f
->output_data
.x
;
4380 int hmargin
= 0, vmargin
= 0;
4381 GtkToolbar
*wtoolbar
;
4383 GtkTextDirection dir
;
4384 int pack_tool_bar
= x
->handlebox_widget
== NULL
;
4386 int text_image
, horiz
;
4388 if (! FRAME_GTK_WIDGET (f
))
4393 if (INTEGERP (Vtool_bar_button_margin
)
4394 && XINT (Vtool_bar_button_margin
) > 0)
4396 hmargin
= XFASTINT (Vtool_bar_button_margin
);
4397 vmargin
= XFASTINT (Vtool_bar_button_margin
);
4399 else if (CONSP (Vtool_bar_button_margin
))
4401 if (INTEGERP (XCAR (Vtool_bar_button_margin
))
4402 && XINT (XCAR (Vtool_bar_button_margin
)) > 0)
4403 hmargin
= XFASTINT (XCAR (Vtool_bar_button_margin
));
4405 if (INTEGERP (XCDR (Vtool_bar_button_margin
))
4406 && XINT (XCDR (Vtool_bar_button_margin
)) > 0)
4407 vmargin
= XFASTINT (XCDR (Vtool_bar_button_margin
));
4410 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4411 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4412 i.e. zero. This means that margins less than
4413 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4414 hmargin
= max (0, hmargin
- DEFAULT_TOOL_BAR_BUTTON_MARGIN
);
4415 vmargin
= max (0, vmargin
- DEFAULT_TOOL_BAR_BUTTON_MARGIN
);
4417 if (! x
->toolbar_widget
)
4418 xg_create_tool_bar (f
);
4420 wtoolbar
= GTK_TOOLBAR (x
->toolbar_widget
);
4421 dir
= gtk_widget_get_direction (GTK_WIDGET (wtoolbar
));
4423 style
= Ftool_bar_get_system_style ();
4424 text_image
= EQ (style
, Qtext_image_horiz
);
4425 horiz
= EQ (style
, Qboth_horiz
) || text_image
;
4427 for (i
= j
= 0; i
< f
->n_tool_bar_items
; ++i
)
4429 int enabled_p
= !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P
));
4430 int selected_p
= !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P
));
4434 struct image
*img
= NULL
;
4436 Lisp_Object stock
= Qnil
;
4437 GtkStockItem stock_item
;
4438 char *stock_name
= NULL
;
4439 char *icon_name
= NULL
;
4441 GtkWidget
*wbutton
= NULL
;
4442 Lisp_Object specified_file
;
4443 int vert_only
= ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY
));
4445 = (EQ (style
, Qimage
) || (vert_only
&& horiz
)) ? NULL
4446 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL
))
4447 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL
))
4450 ti
= gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar
), j
);
4452 /* If this is a separator, use a gtk separator item. */
4453 if (EQ (PROP (TOOL_BAR_ITEM_TYPE
), Qt
))
4455 if (ti
== NULL
|| !GTK_IS_SEPARATOR_TOOL_ITEM (ti
))
4458 gtk_container_remove (GTK_CONTAINER (wtoolbar
),
4460 ti
= gtk_separator_tool_item_new ();
4461 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar
), ti
, j
);
4467 /* Otherwise, the tool-bar item is an ordinary button. */
4469 if (ti
&& GTK_IS_SEPARATOR_TOOL_ITEM (ti
))
4471 gtk_container_remove (GTK_CONTAINER (wtoolbar
), GTK_WIDGET (ti
));
4475 if (ti
) wbutton
= XG_BIN_CHILD (XG_BIN_CHILD (ti
));
4477 /* Ignore invalid image specifications. */
4478 image
= PROP (TOOL_BAR_ITEM_IMAGES
);
4479 if (!valid_image_p (image
))
4482 gtk_container_remove (GTK_CONTAINER (wtoolbar
),
4487 specified_file
= file_for_image (image
);
4488 if (!NILP (specified_file
) && !NILP (Ffboundp (Qx_gtk_map_stock
)))
4489 stock
= call1 (Qx_gtk_map_stock
, specified_file
);
4491 if (STRINGP (stock
))
4493 stock_name
= SSDATA (stock
);
4494 if (stock_name
[0] == 'n' && stock_name
[1] == ':')
4496 GdkScreen
*screen
= gtk_widget_get_screen (GTK_WIDGET (wtoolbar
));
4497 GtkIconTheme
*icon_theme
= gtk_icon_theme_get_for_screen (screen
);
4499 icon_name
= stock_name
+ 2;
4503 if (! gtk_icon_theme_has_icon (icon_theme
, icon_name
))
4506 icon_size
= gtk_toolbar_get_icon_size (wtoolbar
);
4508 else if (gtk_stock_lookup (SSDATA (stock
), &stock_item
))
4509 icon_size
= gtk_toolbar_get_icon_size (wtoolbar
);
4517 if (stock_name
== NULL
&& icon_name
== NULL
)
4519 /* No stock image, or stock item not known. Try regular
4520 image. If image is a vector, choose it according to the
4522 if (dir
== GTK_TEXT_DIR_RTL
4523 && !NILP (rtl
= PROP (TOOL_BAR_ITEM_RTL_IMAGE
))
4525 image
= find_rtl_image (f
, image
, rtl
);
4527 if (VECTORP (image
))
4531 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4532 : TOOL_BAR_IMAGE_ENABLED_DESELECTED
);
4535 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4536 : TOOL_BAR_IMAGE_DISABLED_DESELECTED
);
4538 xassert (ASIZE (image
) >= idx
);
4539 image
= AREF (image
, idx
);
4544 img_id
= lookup_image (f
, image
);
4545 img
= IMAGE_FROM_ID (f
, img_id
);
4546 prepare_image_for_display (f
, img
);
4548 if (img
->load_failed_p
|| img
->pixmap
== None
)
4551 gtk_container_remove (GTK_CONTAINER (wtoolbar
),
4557 /* If there is an existing widget, check if it's stale; if so,
4558 remove it and make a new tool item from scratch. */
4559 if (ti
&& xg_tool_item_stale_p (wbutton
, stock_name
, icon_name
,
4562 gtk_container_remove (GTK_CONTAINER (wtoolbar
),
4571 /* Save the image so we can see if an update is needed the
4572 next time we call xg_tool_item_match_p. */
4573 if (EQ (style
, Qtext
))
4575 else if (stock_name
)
4577 w
= gtk_image_new_from_stock (stock_name
, icon_size
);
4578 g_object_set_data_full (G_OBJECT (w
), XG_TOOL_BAR_STOCK_NAME
,
4579 (gpointer
) xstrdup (stock_name
),
4580 (GDestroyNotify
) xfree
);
4584 w
= gtk_image_new_from_icon_name (icon_name
, icon_size
);
4585 g_object_set_data_full (G_OBJECT (w
), XG_TOOL_BAR_ICON_NAME
,
4586 (gpointer
) xstrdup (icon_name
),
4587 (GDestroyNotify
) xfree
);
4591 w
= xg_get_image_for_pixmap (f
, img
, x
->widget
, NULL
);
4592 g_object_set_data (G_OBJECT (w
), XG_TOOL_BAR_IMAGE_DATA
,
4593 (gpointer
)img
->pixmap
);
4596 if (w
) gtk_misc_set_padding (GTK_MISC (w
), hmargin
, vmargin
);
4597 ti
= xg_make_tool_item (f
, w
, &wbutton
, label
, i
, horiz
, text_image
);
4598 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar
), ti
, j
);
4603 gtk_widget_set_sensitive (wbutton
, enabled_p
);
4607 /* Remove buttons not longer needed. */
4610 ti
= gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar
), j
);
4612 gtk_container_remove (GTK_CONTAINER (wtoolbar
), GTK_WIDGET (ti
));
4613 } while (ti
!= NULL
);
4615 if (f
->n_tool_bar_items
!= 0)
4618 xg_pack_tool_bar (f
, f
->tool_bar_position
);
4619 gtk_widget_show_all (GTK_WIDGET (x
->handlebox_widget
));
4620 if (xg_update_tool_bar_sizes (f
))
4621 xg_height_or_width_changed (f
);
4627 /* Deallocate all resources for the tool bar on frame F.
4628 Remove the tool bar. */
4631 free_frame_tool_bar (FRAME_PTR f
)
4633 struct x_output
*x
= f
->output_data
.x
;
4635 if (x
->toolbar_widget
)
4637 int is_packed
= x
->handlebox_widget
!= 0;
4639 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4640 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4643 if (x
->toolbar_in_hbox
)
4644 gtk_container_remove (GTK_CONTAINER (x
->hbox_widget
),
4645 x
->handlebox_widget
);
4647 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
),
4648 x
->handlebox_widget
);
4651 gtk_widget_destroy (x
->toolbar_widget
);
4653 x
->toolbar_widget
= 0;
4654 x
->handlebox_widget
= 0;
4655 FRAME_TOOLBAR_TOP_HEIGHT (f
) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = 0;
4656 FRAME_TOOLBAR_LEFT_WIDTH (f
) = FRAME_TOOLBAR_RIGHT_WIDTH (f
) = 0;
4658 xg_height_or_width_changed (f
);
4665 xg_change_toolbar_position (FRAME_PTR f
, Lisp_Object pos
)
4667 struct x_output
*x
= f
->output_data
.x
;
4669 if (! x
->toolbar_widget
|| ! x
->handlebox_widget
)
4673 g_object_ref (x
->handlebox_widget
);
4674 if (x
->toolbar_in_hbox
)
4675 gtk_container_remove (GTK_CONTAINER (x
->hbox_widget
),
4676 x
->handlebox_widget
);
4678 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
),
4679 x
->handlebox_widget
);
4680 xg_pack_tool_bar (f
, pos
);
4681 g_object_unref (x
->handlebox_widget
);
4682 if (xg_update_tool_bar_sizes (f
))
4683 xg_height_or_width_changed (f
);
4691 /***********************************************************************
4693 ***********************************************************************/
4695 xg_initialize (void)
4697 GtkBindingSet
*binding_set
;
4700 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4701 we keep it permanently linked in. */
4706 xg_ignore_gtk_scrollbar
= 0;
4707 xg_detached_menus
= 0;
4708 xg_menu_cb_list
.prev
= xg_menu_cb_list
.next
=
4709 xg_menu_item_cb_list
.prev
= xg_menu_item_cb_list
.next
= 0;
4711 id_to_widget
.max_size
= id_to_widget
.used
= 0;
4712 id_to_widget
.widgets
= 0;
4714 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4715 bindings. It doesn't seem to be any way to remove properties,
4716 so we set it to VoidSymbol which in X means "no key". */
4717 gtk_settings_set_string_property (gtk_settings_get_default (),
4718 "gtk-menu-bar-accel",
4722 /* Make GTK text input widgets use Emacs style keybindings. This is
4724 gtk_settings_set_string_property (gtk_settings_get_default (),
4725 "gtk-key-theme-name",
4729 /* Make dialogs close on C-g. Since file dialog inherits from
4730 dialog, this works for them also. */
4731 binding_set
= gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG
));
4732 gtk_binding_entry_add_signal (binding_set
, GDK_KEY_g
, GDK_CONTROL_MASK
,
4735 /* Make menus close on C-g. */
4736 binding_set
= gtk_binding_set_by_class (g_type_class_ref
4737 (GTK_TYPE_MENU_SHELL
));
4738 gtk_binding_entry_add_signal (binding_set
, GDK_KEY_g
, GDK_CONTROL_MASK
,
4740 update_theme_scrollbar_width ();
4743 #endif /* USE_GTK */