1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
29 #include "blockinput.h"
30 #include "syssignal.h"
33 #include "termhooks.h"
37 #include <gdk/gdkkeysyms.h>
38 #include "xsettings.h"
41 #include <X11/Xft/Xft.h>
44 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
45 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
47 #define FRAME_TOTAL_PIXEL_WIDTH(f) \
48 (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f))
50 /* Avoid "differ in sign" warnings */
51 #define SSDATA(x) ((char *) SDATA (x))
53 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
54 #define gtk_widget_set_has_window(w, b) \
55 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
57 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
58 #define gtk_dialog_get_action_area(w) ((w)->action_area)
59 #define gtk_dialog_get_content_area(w) ((w)->vbox)
61 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
62 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
64 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
65 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
66 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
67 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
68 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
70 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
71 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
73 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
77 /***********************************************************************
78 Display handling functions
79 ***********************************************************************/
81 /* Keep track of the default display, or NULL if there is none. Emacs
82 may close all its displays. */
84 static GdkDisplay
*gdpy_def
;
86 /* When the GTK widget W is to be created on a display for F that
87 is not the default display, set the display for W.
88 W can be a GtkMenu or a GtkWindow widget. */
91 xg_set_screen (GtkWidget
*w
, FRAME_PTR f
)
93 if (FRAME_X_DISPLAY (f
) != GDK_DISPLAY ())
95 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
96 GdkScreen
*gscreen
= gdk_display_get_default_screen (gdpy
);
99 gtk_menu_set_screen (GTK_MENU (w
), gscreen
);
101 gtk_window_set_screen (GTK_WINDOW (w
), gscreen
);
106 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
107 *DPY is set to NULL if the display can't be opened.
109 Returns non-zero if display could be opened, zero if display could not
110 be opened, and less than zero if the GTK version doesn't support
114 xg_display_open (char *display_name
, Display
**dpy
)
118 gdpy
= gdk_display_open (display_name
);
119 if (!gdpy_def
&& gdpy
)
122 gdk_display_manager_set_default_display (gdk_display_manager_get (),
126 *dpy
= gdpy
? GDK_DISPLAY_XDISPLAY (gdpy
) : NULL
;
130 /* Close display DPY. */
133 xg_display_close (Display
*dpy
)
135 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (dpy
);
137 /* If this is the default display, try to change it before closing.
138 If there is no other display to use, gdpy_def is set to NULL, and
139 the next call to xg_display_open resets the default display. */
140 if (gdk_display_get_default () == gdpy
)
142 struct x_display_info
*dpyinfo
;
143 GdkDisplay
*gdpy_new
= NULL
;
145 /* Find another display. */
146 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
147 if (dpyinfo
->display
!= dpy
)
149 gdpy_new
= gdk_x11_lookup_xdisplay (dpyinfo
->display
);
150 gdk_display_manager_set_default_display (gdk_display_manager_get (),
157 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
158 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
159 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
160 can continue running, but there will be memory leaks. */
161 g_object_run_dispose (G_OBJECT (gdpy
));
163 /* This seems to be fixed in GTK 2.10. */
164 gdk_display_close (gdpy
);
169 /***********************************************************************
171 ***********************************************************************/
172 /* The next two variables and functions are taken from lwlib. */
173 static widget_value
*widget_value_free_list
;
174 static int malloc_cpt
;
176 /* Allocate a widget_value structure, either by taking one from the
177 widget_value_free_list or by malloc:ing a new one.
179 Return a pointer to the allocated structure. */
182 malloc_widget_value (void)
185 if (widget_value_free_list
)
187 wv
= widget_value_free_list
;
188 widget_value_free_list
= wv
->free_list
;
193 wv
= (widget_value
*) xmalloc (sizeof (widget_value
));
196 memset (wv
, 0, sizeof (widget_value
));
200 /* This is analogous to free. It frees only what was allocated
201 by malloc_widget_value, and no substructures. */
204 free_widget_value (widget_value
*wv
)
211 /* When the number of already allocated cells is too big,
218 wv
->free_list
= widget_value_free_list
;
219 widget_value_free_list
= wv
;
224 /* Create and return the cursor to be used for popup menus and
225 scroll bars on display DPY. */
228 xg_create_default_cursor (Display
*dpy
)
230 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (dpy
);
231 return gdk_cursor_new_for_display (gdpy
, GDK_LEFT_PTR
);
234 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
237 xg_get_pixbuf_from_pix_and_mask (GdkPixmap
*gpix
,
242 GdkPixbuf
*icon_buf
, *tmp_buf
;
244 gdk_drawable_get_size (gpix
, &width
, &height
);
245 tmp_buf
= gdk_pixbuf_get_from_drawable (NULL
, gpix
, cmap
,
246 0, 0, 0, 0, width
, height
);
247 icon_buf
= gdk_pixbuf_add_alpha (tmp_buf
, FALSE
, 0, 0, 0);
248 g_object_unref (G_OBJECT (tmp_buf
));
252 GdkPixbuf
*mask_buf
= gdk_pixbuf_get_from_drawable (NULL
,
257 guchar
*pixels
= gdk_pixbuf_get_pixels (icon_buf
);
258 guchar
*mask_pixels
= gdk_pixbuf_get_pixels (mask_buf
);
259 int rowstride
= gdk_pixbuf_get_rowstride (icon_buf
);
260 int mask_rowstride
= gdk_pixbuf_get_rowstride (mask_buf
);
263 for (y
= 0; y
< height
; ++y
)
265 guchar
*iconptr
, *maskptr
;
268 iconptr
= pixels
+ y
* rowstride
;
269 maskptr
= mask_pixels
+ y
* mask_rowstride
;
271 for (x
= 0; x
< width
; ++x
)
273 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
274 just R is sufficient. */
276 iconptr
[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
278 iconptr
+= rowstride
/width
;
279 maskptr
+= mask_rowstride
/width
;
283 g_object_unref (G_OBJECT (mask_buf
));
290 file_for_image (Lisp_Object image
)
292 Lisp_Object specified_file
= Qnil
;
294 extern Lisp_Object QCfile
;
296 for (tail
= XCDR (image
);
297 NILP (specified_file
) && CONSP (tail
) && CONSP (XCDR (tail
));
298 tail
= XCDR (XCDR (tail
)))
299 if (EQ (XCAR (tail
), QCfile
))
300 specified_file
= XCAR (XCDR (tail
));
302 return specified_file
;
305 /* For the image defined in IMG, make and return a GtkImage. For displays with
306 8 planes or less we must make a GdkPixbuf and apply the mask manually.
307 Otherwise the highlightning and dimming the tool bar code in GTK does
308 will look bad. For display with more than 8 planes we just use the
309 pixmap and mask directly. For monochrome displays, GTK doesn't seem
310 able to use external pixmaps, it looks bad whatever we do.
311 The image is defined on the display where frame F is.
312 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
313 If OLD_WIDGET is NULL, a new widget is constructed and returned.
314 If OLD_WIDGET is not NULL, that widget is modified. */
317 xg_get_image_for_pixmap (FRAME_PTR f
,
320 GtkImage
*old_widget
)
328 /* If we have a file, let GTK do all the image handling.
329 This seems to be the only way to make insensitive and activated icons
330 look good in all cases. */
331 Lisp_Object specified_file
= file_for_image (img
->spec
);
334 /* We already loaded the image once before calling this
335 function, so this only fails if the image file has been removed.
336 In that case, use the pixmap already loaded. */
338 if (STRINGP (specified_file
)
339 && STRINGP (file
= x_find_image_file (specified_file
)))
342 old_widget
= GTK_IMAGE (gtk_image_new_from_file (SSDATA (file
)));
344 gtk_image_set_from_file (old_widget
, SSDATA (file
));
346 return GTK_WIDGET (old_widget
);
349 /* No file, do the image handling ourselves. This will look very bad
350 on a monochrome display, and sometimes bad on all displays with
353 gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
354 gpix
= gdk_pixmap_foreign_new_for_display (gdpy
, img
->pixmap
);
355 gmask
= img
->mask
? gdk_pixmap_foreign_new_for_display (gdpy
, img
->mask
) : 0;
357 /* This is a workaround to make icons look good on pseudo color
358 displays. Apparently GTK expects the images to have an alpha
359 channel. If they don't, insensitive and activated icons will
360 look bad. This workaround does not work on monochrome displays,
361 and is strictly not needed on true color/static color displays (i.e.
362 16 bits and higher). But we do it anyway so we get a pixbuf that is
363 not associated with the img->pixmap. The img->pixmap may be removed
364 by clearing the image cache and then the tool bar redraw fails, since
365 Gtk+ assumes the pixmap is always there. */
366 cmap
= gtk_widget_get_colormap (widget
);
367 icon_buf
= xg_get_pixbuf_from_pix_and_mask (gpix
, gmask
, cmap
);
370 old_widget
= GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf
));
372 gtk_image_set_from_pixbuf (old_widget
, icon_buf
);
374 g_object_unref (G_OBJECT (icon_buf
));
376 g_object_unref (G_OBJECT (gpix
));
377 if (gmask
) g_object_unref (G_OBJECT (gmask
));
379 return GTK_WIDGET (old_widget
);
383 /* Set CURSOR on W and all widgets W contain. We must do like this
384 for scroll bars and menu because they create widgets internally,
385 and it is those widgets that are visible. */
388 xg_set_cursor (GtkWidget
*w
, GdkCursor
*cursor
)
390 GdkWindow
*window
= gtk_widget_get_window(w
);
391 GList
*children
= gdk_window_peek_children (window
);
393 gdk_window_set_cursor (window
, cursor
);
395 /* The scroll bar widget has more than one GDK window (had to look at
396 the source to figure this out), and there is no way to set cursor
397 on widgets in GTK. So we must set the cursor for all GDK windows.
400 for ( ; children
; children
= g_list_next (children
))
401 gdk_window_set_cursor (GDK_WINDOW (children
->data
), cursor
);
404 /* Insert NODE into linked LIST. */
407 xg_list_insert (xg_list_node
*list
, xg_list_node
*node
)
409 xg_list_node
*list_start
= list
->next
;
411 if (list_start
) list_start
->prev
= node
;
412 node
->next
= list_start
;
417 /* Remove NODE from linked LIST. */
420 xg_list_remove (xg_list_node
*list
, xg_list_node
*node
)
422 xg_list_node
*list_start
= list
->next
;
423 if (node
== list_start
)
425 list
->next
= node
->next
;
426 if (list
->next
) list
->next
->prev
= 0;
430 node
->prev
->next
= node
->next
;
431 if (node
->next
) node
->next
->prev
= node
->prev
;
435 /* Allocate and return a utf8 version of STR. If STR is already
436 utf8 or NULL, just return STR.
437 If not, a new string is allocated and the caller must free the result
441 get_utf8_string (char *str
)
443 char *utf8_str
= str
;
445 if (!str
) return NULL
;
447 /* If not UTF-8, try current locale. */
448 if (!g_utf8_validate (str
, -1, NULL
))
449 utf8_str
= g_locale_to_utf8 (str
, -1, 0, 0, 0);
453 /* Probably some control characters in str. Escape them. */
457 unsigned char *p
= (unsigned char *)str
;
459 GError
*error
= NULL
;
461 while (! (cp
= g_locale_to_utf8 ((char *)p
, -1, &bytes_read
,
462 &bytes_written
, &error
))
463 && error
->code
== G_CONVERT_ERROR_ILLEGAL_SEQUENCE
)
466 p
+= bytes_written
+1;
467 g_error_free (error
);
473 g_error_free (error
);
478 up
= utf8_str
= xmalloc (strlen (str
) + nr_bad
* 4 + 1);
479 p
= (unsigned char *)str
;
481 while (! (cp
= g_locale_to_utf8 ((char *)p
, -1, &bytes_read
,
482 &bytes_written
, &error
))
483 && error
->code
== G_CONVERT_ERROR_ILLEGAL_SEQUENCE
)
485 strncpy (up
, (char *)p
, bytes_written
);
486 sprintf (up
+ bytes_written
, "\\%03o", p
[bytes_written
]);
487 up
[bytes_written
+4] = '\0';
488 up
+= bytes_written
+4;
489 p
+= bytes_written
+1;
490 g_error_free (error
);
496 strcat (utf8_str
, cp
);
501 g_error_free (error
);
510 /***********************************************************************
511 General functions for creating widgets, resizing, events, e.t.c.
512 ***********************************************************************/
514 /* Make a geometry string and pass that to GTK. It seems this is the
515 only way to get geometry position right if the user explicitly
516 asked for a position when starting Emacs.
517 F is the frame we shall set geometry for. */
520 xg_set_geometry (FRAME_PTR f
)
522 if (f
->size_hint_flags
& (USPosition
| PPosition
))
524 int left
= f
->left_pos
;
525 int xneg
= f
->size_hint_flags
& XNegative
;
526 int top
= f
->top_pos
;
527 int yneg
= f
->size_hint_flags
& YNegative
;
535 sprintf (geom_str
, "=%dx%d%c%d%c%d",
536 FRAME_PIXEL_WIDTH (f
),
537 FRAME_PIXEL_HEIGHT (f
),
538 (xneg
? '-' : '+'), left
,
539 (yneg
? '-' : '+'), top
);
541 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
543 fprintf (stderr
, "Failed to parse: '%s'\n", geom_str
);
547 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
548 and use a GtkFixed widget, this doesn't happen automatically. */
551 xg_clear_under_internal_border (FRAME_PTR f
)
553 if (FRAME_INTERNAL_BORDER_WIDTH (f
) > 0)
555 GtkWidget
*wfixed
= f
->output_data
.x
->edit_widget
;
556 gtk_widget_queue_draw (wfixed
);
557 gdk_window_process_all_updates ();
558 x_clear_area (FRAME_X_DISPLAY (f
),
561 FRAME_PIXEL_WIDTH (f
),
562 FRAME_INTERNAL_BORDER_WIDTH (f
), 0);
563 x_clear_area (FRAME_X_DISPLAY (f
),
566 FRAME_INTERNAL_BORDER_WIDTH (f
),
567 FRAME_PIXEL_HEIGHT (f
), 0);
568 x_clear_area (FRAME_X_DISPLAY (f
),
570 0, FRAME_PIXEL_HEIGHT (f
) - FRAME_INTERNAL_BORDER_WIDTH (f
),
571 FRAME_PIXEL_WIDTH (f
),
572 FRAME_INTERNAL_BORDER_WIDTH (f
), 0);
573 x_clear_area (FRAME_X_DISPLAY (f
),
575 FRAME_PIXEL_WIDTH (f
) - FRAME_INTERNAL_BORDER_WIDTH (f
),
577 FRAME_INTERNAL_BORDER_WIDTH (f
),
578 FRAME_PIXEL_HEIGHT (f
), 0);
582 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
583 and a Gtk+ menu bar, we get resize events for the edit part of the
584 frame only. We let Gtk+ deal with the Gtk+ parts.
585 F is the frame to resize.
586 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
589 xg_frame_resized (FRAME_PTR f
, int pixelwidth
, int pixelheight
)
593 if (pixelwidth
== -1 && pixelheight
== -1)
595 if (FRAME_GTK_WIDGET (f
) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f
)))
596 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f
)),
598 &pixelwidth
, &pixelheight
, 0);
603 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f
, pixelheight
);
604 columns
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f
, pixelwidth
);
606 if (columns
!= FRAME_COLS (f
)
607 || rows
!= FRAME_LINES (f
)
608 || pixelwidth
!= FRAME_PIXEL_WIDTH (f
)
609 || pixelheight
!= FRAME_PIXEL_HEIGHT (f
))
611 FRAME_PIXEL_WIDTH (f
) = pixelwidth
;
612 FRAME_PIXEL_HEIGHT (f
) = pixelheight
;
614 xg_clear_under_internal_border (f
);
615 change_frame_size (f
, rows
, columns
, 0, 1, 0);
616 SET_FRAME_GARBAGED (f
);
617 cancel_mouse_face (f
);
621 /* Resize the outer window of frame F after chainging the height.
622 COLUMNS/ROWS is the size the edit area shall have after the resize. */
625 xg_frame_set_char_size (FRAME_PTR f
, int cols
, int rows
)
627 int pixelheight
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f
, rows
)
628 + FRAME_MENUBAR_HEIGHT (f
) + FRAME_TOOLBAR_HEIGHT (f
);
631 if (FRAME_PIXEL_HEIGHT (f
) == 0)
634 /* Take into account the size of the scroll bar. Always use the
635 number of columns occupied by the scroll bar here otherwise we
636 might end up with a frame width that is not a multiple of the
637 frame's character width which is bad for vertically split
639 f
->scroll_bar_actual_width
640 = FRAME_SCROLL_BAR_COLS (f
) * FRAME_COLUMN_WIDTH (f
);
642 compute_fringe_widths (f
, 0);
644 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
645 after calculating that value. */
646 pixelwidth
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f
, cols
)
647 + FRAME_TOOLBAR_WIDTH (f
);
650 /* Do this before resize, as we don't know yet if we will be resized. */
651 xg_clear_under_internal_border (f
);
653 /* Must resize our top level widget. Font size may have changed,
654 but not rows/cols. */
655 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
656 pixelwidth
, pixelheight
);
657 x_wm_set_size_hint (f
, 0, 0);
659 SET_FRAME_GARBAGED (f
);
660 cancel_mouse_face (f
);
662 /* We can not call change_frame_size for a mapped frame,
663 we can not set pixel width/height either. The window manager may
664 override our resize request, XMonad does this all the time.
665 The best we can do is try to sync, so lisp code sees the updated
666 size as fast as possible.
667 For unmapped windows, we can set rows/cols. When
668 the frame is mapped again we will (hopefully) get the correct size. */
669 if (f
->async_visible
)
671 /* Must call this to flush out events */
672 (void)gtk_events_pending ();
674 x_wait_for_event (f
, ConfigureNotify
);
678 change_frame_size (f
, rows
, cols
, 0, 1, 0);
679 FRAME_PIXEL_WIDTH (f
) = pixelwidth
;
680 FRAME_PIXEL_HEIGHT (f
) = pixelheight
;
684 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
685 The policy is to keep the number of editable lines. */
688 xg_height_or_width_changed (FRAME_PTR f
)
690 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
691 FRAME_TOTAL_PIXEL_WIDTH (f
),
692 FRAME_TOTAL_PIXEL_HEIGHT (f
));
693 f
->output_data
.x
->hint_flags
= 0;
694 x_wm_set_size_hint (f
, 0, 0);
697 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
698 Must be done like this, because GtkWidget:s can have "hidden"
699 X Window that aren't accessible.
701 Return 0 if no widget match WDESC. */
704 xg_win_to_widget (Display
*dpy
, Window wdesc
)
707 GtkWidget
*gwdesc
= 0;
711 gdkwin
= gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy
),
716 event
.any
.window
= gdkwin
;
717 gwdesc
= gtk_get_event_widget (&event
);
724 /* Fill in the GdkColor C so that it represents PIXEL.
725 W is the widget that color will be used for. Used to find colormap. */
728 xg_pix_to_gcolor (GtkWidget
*w
, long unsigned int pixel
, GdkColor
*c
)
730 GdkColormap
*map
= gtk_widget_get_colormap (w
);
731 gdk_colormap_query_color (map
, pixel
, c
);
734 /* Create and set up the GTK widgets for frame F.
735 Return 0 if creation failed, non-zero otherwise. */
738 xg_create_frame_widgets (FRAME_PTR f
)
741 GtkWidget
*wvbox
, *whbox
;
749 if (FRAME_X_EMBEDDED_P (f
))
750 wtop
= gtk_plug_new (f
->output_data
.x
->parent_desc
);
752 wtop
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
754 xg_set_screen (wtop
, f
);
756 wvbox
= gtk_vbox_new (FALSE
, 0);
757 whbox
= gtk_hbox_new (FALSE
, 0);
758 wfixed
= gtk_fixed_new (); /* Must have this to place scroll bars */
760 if (! wtop
|| ! wvbox
|| ! whbox
|| ! wfixed
)
762 if (wtop
) gtk_widget_destroy (wtop
);
763 if (wvbox
) gtk_widget_destroy (wvbox
);
764 if (whbox
) gtk_widget_destroy (whbox
);
765 if (wfixed
) gtk_widget_destroy (wfixed
);
771 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
772 gtk_widget_set_name (wtop
, EMACS_CLASS
);
773 gtk_widget_set_name (wvbox
, "pane");
774 gtk_widget_set_name (wfixed
, SSDATA (Vx_resource_name
));
776 /* If this frame has a title or name, set it in the title bar. */
777 if (! NILP (f
->title
)) title
= SSDATA (ENCODE_UTF_8 (f
->title
));
778 else if (! NILP (f
->name
)) title
= SSDATA (ENCODE_UTF_8 (f
->name
));
780 if (title
) gtk_window_set_title (GTK_WINDOW (wtop
), title
);
782 FRAME_GTK_OUTER_WIDGET (f
) = wtop
;
783 FRAME_GTK_WIDGET (f
) = wfixed
;
784 f
->output_data
.x
->vbox_widget
= wvbox
;
785 f
->output_data
.x
->hbox_widget
= whbox
;
787 gtk_widget_set_has_window (wfixed
, TRUE
);
789 gtk_container_add (GTK_CONTAINER (wtop
), wvbox
);
790 gtk_box_pack_start (GTK_BOX (wvbox
), whbox
, TRUE
, TRUE
, 0);
791 gtk_box_pack_start (GTK_BOX (whbox
), wfixed
, TRUE
, TRUE
, 0);
793 if (FRAME_EXTERNAL_TOOL_BAR (f
))
794 update_frame_tool_bar (f
);
796 /* We don't want this widget double buffered, because we draw on it
797 with regular X drawing primitives, so from a GTK/GDK point of
798 view, the widget is totally blank. When an expose comes, this
799 will make the widget blank, and then Emacs redraws it. This flickers
800 a lot, so we turn off double buffering. */
801 gtk_widget_set_double_buffered (wfixed
, FALSE
);
803 gtk_window_set_wmclass (GTK_WINDOW (wtop
),
804 SSDATA (Vx_resource_name
),
805 SSDATA (Vx_resource_class
));
807 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
808 GTK is to destroy the widget. We want Emacs to do that instead. */
809 g_signal_connect (G_OBJECT (wtop
), "delete-event",
810 G_CALLBACK (gtk_true
), 0);
812 /* Convert our geometry parameters into a geometry string
814 GTK will itself handle calculating the real position this way. */
817 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
819 gtk_widget_add_events (wfixed
,
820 GDK_POINTER_MOTION_MASK
822 | GDK_BUTTON_PRESS_MASK
823 | GDK_BUTTON_RELEASE_MASK
825 | GDK_ENTER_NOTIFY_MASK
826 | GDK_LEAVE_NOTIFY_MASK
827 | GDK_FOCUS_CHANGE_MASK
829 | GDK_VISIBILITY_NOTIFY_MASK
);
831 /* Must realize the windows so the X window gets created. It is used
832 by callers of this function. */
833 gtk_widget_realize (wfixed
);
834 FRAME_X_WINDOW (f
) = GTK_WIDGET_TO_X_WIN (wfixed
);
836 /* Since GTK clears its window by filling with the background color,
837 we must keep X and GTK background in sync. */
838 xg_pix_to_gcolor (wfixed
, FRAME_BACKGROUND_PIXEL (f
), &bg
);
839 gtk_widget_modify_bg (wfixed
, GTK_STATE_NORMAL
, &bg
);
841 /* Also, do not let any background pixmap to be set, this looks very
842 bad as Emacs overwrites the background pixmap with its own idea
843 of background color. */
844 style
= gtk_widget_get_modifier_style (wfixed
);
846 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
847 style
->bg_pixmap_name
[GTK_STATE_NORMAL
] = g_strdup ("<none>");
848 gtk_widget_modify_style (wfixed
, style
);
850 /* GTK does not set any border, and they look bad with GTK. */
851 /* That they look bad is no excuse for imposing this here. --Stef
852 It should be done by providing the proper default in Fx_create_Frame.
854 f->internal_border_width = 0; */
861 /* Set the normal size hints for the window manager, for frame F.
862 FLAGS is the flags word to use--or 0 meaning preserve the flags
863 that the window now has.
864 If USER_POSITION is nonzero, we set the User Position
865 flag (this is useful when FLAGS is 0). */
868 x_wm_set_size_hint (FRAME_PTR f
, long int flags
, int user_position
)
870 /* Must use GTK routines here, otherwise GTK resets the size hints
871 to its own defaults. */
872 GdkGeometry size_hints
;
874 int base_width
, base_height
;
875 int min_rows
= 0, min_cols
= 0;
876 int win_gravity
= f
->win_gravity
;
878 /* Don't set size hints during initialization; that apparently leads
879 to a race condition. See the thread at
880 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
881 if (NILP (Vafter_init_time
) || !FRAME_GTK_OUTER_WIDGET (f
))
886 memset (&size_hints
, 0, sizeof (size_hints
));
887 f
->output_data
.x
->size_hints
= size_hints
;
888 f
->output_data
.x
->hint_flags
= hint_flags
;
891 flags
= f
->size_hint_flags
;
893 size_hints
= f
->output_data
.x
->size_hints
;
894 hint_flags
= f
->output_data
.x
->hint_flags
;
896 hint_flags
|= GDK_HINT_RESIZE_INC
| GDK_HINT_MIN_SIZE
;
897 size_hints
.width_inc
= FRAME_COLUMN_WIDTH (f
);
898 size_hints
.height_inc
= FRAME_LINE_HEIGHT (f
);
900 hint_flags
|= GDK_HINT_BASE_SIZE
;
901 base_width
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f
, 0) + FRAME_TOOLBAR_WIDTH (f
);
902 base_height
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f
, 0)
903 + FRAME_MENUBAR_HEIGHT (f
) + FRAME_TOOLBAR_HEIGHT (f
);
905 check_frame_size (f
, &min_rows
, &min_cols
);
907 size_hints
.base_width
= base_width
;
908 size_hints
.base_height
= base_height
;
909 size_hints
.min_width
= base_width
+ min_cols
* size_hints
.width_inc
;
910 size_hints
.min_height
= base_height
+ min_rows
* size_hints
.height_inc
;
912 /* These currently have a one to one mapping with the X values, but I
913 don't think we should rely on that. */
914 hint_flags
|= GDK_HINT_WIN_GRAVITY
;
915 size_hints
.win_gravity
= 0;
916 if (win_gravity
== NorthWestGravity
)
917 size_hints
.win_gravity
= GDK_GRAVITY_NORTH_WEST
;
918 else if (win_gravity
== NorthGravity
)
919 size_hints
.win_gravity
= GDK_GRAVITY_NORTH
;
920 else if (win_gravity
== NorthEastGravity
)
921 size_hints
.win_gravity
= GDK_GRAVITY_NORTH_EAST
;
922 else if (win_gravity
== WestGravity
)
923 size_hints
.win_gravity
= GDK_GRAVITY_WEST
;
924 else if (win_gravity
== CenterGravity
)
925 size_hints
.win_gravity
= GDK_GRAVITY_CENTER
;
926 else if (win_gravity
== EastGravity
)
927 size_hints
.win_gravity
= GDK_GRAVITY_EAST
;
928 else if (win_gravity
== SouthWestGravity
)
929 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH_WEST
;
930 else if (win_gravity
== SouthGravity
)
931 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH
;
932 else if (win_gravity
== SouthEastGravity
)
933 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH_EAST
;
934 else if (win_gravity
== StaticGravity
)
935 size_hints
.win_gravity
= GDK_GRAVITY_STATIC
;
939 hint_flags
&= ~GDK_HINT_POS
;
940 hint_flags
|= GDK_HINT_USER_POS
;
943 if (hint_flags
!= f
->output_data
.x
->hint_flags
944 || memcmp (&size_hints
,
945 &f
->output_data
.x
->size_hints
,
946 sizeof (size_hints
)) != 0)
949 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
950 NULL
, &size_hints
, hint_flags
);
951 f
->output_data
.x
->size_hints
= size_hints
;
952 f
->output_data
.x
->hint_flags
= hint_flags
;
957 /* Change background color of a frame.
958 Since GTK uses the background color to clear the window, we must
959 keep the GTK and X colors in sync.
960 F is the frame to change,
961 BG is the pixel value to change to. */
964 xg_set_background_color (FRAME_PTR f
, long unsigned int bg
)
966 if (FRAME_GTK_WIDGET (f
))
971 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f
), bg
, &gdk_bg
);
972 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f
), GTK_STATE_NORMAL
, &gdk_bg
);
978 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
979 functions so GTK does not overwrite the icon. */
982 xg_set_frame_icon (FRAME_PTR f
, Pixmap icon_pixmap
, Pixmap icon_mask
)
984 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
985 GdkPixmap
*gpix
= gdk_pixmap_foreign_new_for_display (gdpy
, icon_pixmap
);
986 GdkPixmap
*gmask
= gdk_pixmap_foreign_new_for_display (gdpy
, icon_mask
);
987 GdkPixbuf
*gp
= xg_get_pixbuf_from_pix_and_mask (gpix
, gmask
, NULL
);
989 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)), gp
);
994 /***********************************************************************
996 ***********************************************************************/
997 /* Return the dialog title to use for a dialog of type KEY.
998 This is the encoding used by lwlib. We use the same for GTK. */
1001 get_dialog_title (char key
)
1011 title
= "Information";
1030 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1031 the dialog, but return TRUE so the event does not propagate further
1032 in GTK. This prevents GTK from destroying the dialog widget automatically
1033 and we can always destrou the widget manually, regardles of how
1034 it was popped down (button press or WM_DELETE_WINDOW).
1035 W is the dialog widget.
1036 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1037 user_data is NULL (not used).
1039 Returns TRUE to end propagation of event. */
1042 dialog_delete_callback (GtkWidget
*w
, GdkEvent
*event
, gpointer user_data
)
1044 gtk_widget_unmap (w
);
1048 /* Create a popup dialog window. See also xg_create_widget below.
1049 WV is a widget_value describing the dialog.
1050 SELECT_CB is the callback to use when a button has been pressed.
1051 DEACTIVATE_CB is the callback to use when the dialog pops down.
1053 Returns the GTK dialog widget. */
1056 create_dialog (widget_value
*wv
,
1057 GCallback select_cb
,
1058 GCallback deactivate_cb
)
1060 char *title
= get_dialog_title (wv
->name
[0]);
1061 int total_buttons
= wv
->name
[1] - '0';
1062 int right_buttons
= wv
->name
[4] - '0';
1065 int button_spacing
= 10;
1066 GtkWidget
*wdialog
= gtk_dialog_new ();
1067 GtkDialog
*wd
= GTK_DIALOG (wdialog
);
1068 GtkBox
*cur_box
= GTK_BOX (gtk_dialog_get_action_area (wd
));
1071 GtkWidget
*whbox_up
;
1072 GtkWidget
*whbox_down
;
1074 /* If the number of buttons is greater than 4, make two rows of buttons
1075 instead. This looks better. */
1076 int make_two_rows
= total_buttons
> 4;
1078 if (right_buttons
== 0) right_buttons
= total_buttons
/2;
1079 left_buttons
= total_buttons
- right_buttons
;
1081 gtk_window_set_title (GTK_WINDOW (wdialog
), title
);
1082 gtk_widget_set_name (wdialog
, "emacs-dialog");
1087 wvbox
= gtk_vbox_new (TRUE
, button_spacing
);
1088 whbox_up
= gtk_hbox_new (FALSE
, 0);
1089 whbox_down
= gtk_hbox_new (FALSE
, 0);
1091 gtk_box_pack_start (cur_box
, wvbox
, FALSE
, FALSE
, 0);
1092 gtk_box_pack_start (GTK_BOX (wvbox
), whbox_up
, FALSE
, FALSE
, 0);
1093 gtk_box_pack_start (GTK_BOX (wvbox
), whbox_down
, FALSE
, FALSE
, 0);
1095 cur_box
= GTK_BOX (whbox_up
);
1098 g_signal_connect (G_OBJECT (wdialog
), "delete-event",
1099 G_CALLBACK (dialog_delete_callback
), 0);
1103 g_signal_connect (G_OBJECT (wdialog
), "close", deactivate_cb
, 0);
1104 g_signal_connect (G_OBJECT (wdialog
), "response", deactivate_cb
, 0);
1107 for (item
= wv
->contents
; item
; item
= item
->next
)
1109 char *utf8_label
= get_utf8_string (item
->value
);
1113 if (item
->name
&& strcmp (item
->name
, "message") == 0)
1115 GtkBox
*wvbox
= GTK_BOX (gtk_dialog_get_content_area (wd
));
1116 /* This is the text part of the dialog. */
1117 w
= gtk_label_new (utf8_label
);
1118 gtk_box_pack_start (wvbox
, gtk_label_new (""), FALSE
, FALSE
, 0);
1119 gtk_box_pack_start (wvbox
, w
, TRUE
, TRUE
, 0);
1120 gtk_misc_set_alignment (GTK_MISC (w
), 0.1, 0.5);
1122 /* Try to make dialog look better. Must realize first so
1123 the widget can calculate the size it needs. */
1124 gtk_widget_realize (w
);
1125 gtk_widget_size_request (w
, &req
);
1126 gtk_box_set_spacing (wvbox
, req
.height
);
1127 if (item
->value
&& strlen (item
->value
) > 0)
1128 button_spacing
= 2*req
.width
/strlen (item
->value
);
1132 /* This is one button to add to the dialog. */
1133 w
= gtk_button_new_with_label (utf8_label
);
1134 if (! item
->enabled
)
1135 gtk_widget_set_sensitive (w
, FALSE
);
1137 g_signal_connect (G_OBJECT (w
), "clicked",
1138 select_cb
, item
->call_data
);
1140 gtk_box_pack_start (cur_box
, w
, TRUE
, TRUE
, button_spacing
);
1141 if (++button_nr
== left_buttons
)
1144 cur_box
= GTK_BOX (whbox_down
);
1146 gtk_box_pack_start (cur_box
,
1153 if (utf8_label
&& utf8_label
!= item
->value
)
1154 g_free (utf8_label
);
1160 struct xg_dialog_data
1168 /* Function that is called when the file or font dialogs pop down.
1169 W is the dialog widget, RESPONSE is the response code.
1170 USER_DATA is what we passed in to g_signal_connect. */
1173 xg_dialog_response_cb (GtkDialog
*w
,
1177 struct xg_dialog_data
*dd
= (struct xg_dialog_data
*)user_data
;
1178 dd
->response
= response
;
1179 g_main_loop_quit (dd
->loop
);
1183 /* Destroy the dialog. This makes it pop down. */
1186 pop_down_dialog (Lisp_Object arg
)
1188 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
1189 struct xg_dialog_data
*dd
= (struct xg_dialog_data
*) p
->pointer
;
1192 if (dd
->w
) gtk_widget_destroy (dd
->w
);
1193 if (dd
->timerid
!= 0) g_source_remove (dd
->timerid
);
1195 g_main_loop_quit (dd
->loop
);
1196 g_main_loop_unref (dd
->loop
);
1203 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1204 We pass in DATA as gpointer* so we can use this as a callback. */
1207 xg_maybe_add_timer (gpointer data
)
1209 struct xg_dialog_data
*dd
= (struct xg_dialog_data
*) data
;
1210 EMACS_TIME next_time
= timer_check (1);
1211 long secs
= EMACS_SECS (next_time
);
1212 long usecs
= EMACS_USECS (next_time
);
1216 if (secs
>= 0 && usecs
>= 0 && secs
< ((guint
)-1)/1000)
1218 dd
->timerid
= g_timeout_add (secs
* 1000 + usecs
/1000,
1226 /* Pops up a modal dialog W and waits for response.
1227 We don't use gtk_dialog_run because we want to process emacs timers.
1228 The dialog W is not destroyed when this function returns. */
1231 xg_dialog_run (FRAME_PTR f
, GtkWidget
*w
)
1233 int count
= SPECPDL_INDEX ();
1234 struct xg_dialog_data dd
;
1236 xg_set_screen (w
, f
);
1237 gtk_window_set_transient_for (GTK_WINDOW (w
),
1238 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
1239 gtk_window_set_destroy_with_parent (GTK_WINDOW (w
), TRUE
);
1240 gtk_window_set_modal (GTK_WINDOW (w
), TRUE
);
1242 dd
.loop
= g_main_loop_new (NULL
, FALSE
);
1243 dd
.response
= GTK_RESPONSE_CANCEL
;
1247 g_signal_connect (G_OBJECT (w
),
1249 G_CALLBACK (xg_dialog_response_cb
),
1251 /* Don't destroy the widget if closed by the window manager close button. */
1252 g_signal_connect (G_OBJECT (w
), "delete-event", G_CALLBACK (gtk_true
), NULL
);
1253 gtk_widget_show (w
);
1255 record_unwind_protect (pop_down_dialog
, make_save_value (&dd
, 0));
1257 (void) xg_maybe_add_timer (&dd
);
1258 g_main_loop_run (dd
.loop
);
1261 unbind_to (count
, Qnil
);
1267 /***********************************************************************
1268 File dialog functions
1269 ***********************************************************************/
1270 /* Return non-zero if the old file selection dialog is being used.
1271 Return zero if not. */
1274 xg_uses_old_file_dialog (void)
1276 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1277 extern int x_gtk_use_old_file_dialog
;
1278 return x_gtk_use_old_file_dialog
;
1285 typedef char * (*xg_get_file_func
) (GtkWidget
*);
1287 /* Return the selected file for file chooser dialog W.
1288 The returned string must be free:d. */
1291 xg_get_file_name_from_chooser (GtkWidget
*w
)
1293 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w
));
1296 /* Callback called when the "Show hidden files" toggle is pressed.
1297 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1300 xg_toggle_visibility_cb (GtkWidget
*widget
, gpointer data
)
1302 GtkFileChooser
*dialog
= GTK_FILE_CHOOSER (data
);
1304 g_object_get (G_OBJECT (dialog
), "show-hidden", &visible
, NULL
);
1305 g_object_set (G_OBJECT (dialog
), "show-hidden", !visible
, NULL
);
1309 /* Callback called when a property changes in a file chooser.
1310 GOBJECT is the file chooser dialog, ARG1 describes the property.
1311 USER_DATA is the toggle widget in the file chooser dialog.
1312 We use this to update the "Show hidden files" toggle when the user
1313 changes that property by right clicking in the file list. */
1316 xg_toggle_notify_cb (GObject
*gobject
, GParamSpec
*arg1
, gpointer user_data
)
1318 extern int x_gtk_show_hidden_files
;
1320 if (strcmp (arg1
->name
, "show-hidden") == 0)
1322 GtkWidget
*wtoggle
= GTK_WIDGET (user_data
);
1323 gboolean visible
, toggle_on
;
1325 g_object_get (G_OBJECT (gobject
), "show-hidden", &visible
, NULL
);
1326 toggle_on
= gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle
));
1328 if (!!visible
!= !!toggle_on
)
1330 g_signal_handlers_block_by_func (G_OBJECT (wtoggle
),
1331 G_CALLBACK (xg_toggle_visibility_cb
),
1333 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle
), visible
);
1334 g_signal_handlers_unblock_by_func
1335 (G_OBJECT (wtoggle
),
1336 G_CALLBACK (xg_toggle_visibility_cb
),
1339 x_gtk_show_hidden_files
= visible
;
1343 /* Read a file name from the user using a file chooser dialog.
1344 F is the current frame.
1345 PROMPT is a prompt to show to the user. May not be NULL.
1346 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1347 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1348 file. *FUNC is set to a function that can be used to retrieve the
1349 selected file name from the returned widget.
1351 Returns the created widget. */
1354 xg_get_file_with_chooser (FRAME_PTR f
,
1356 char *default_filename
,
1357 int mustmatch_p
, int only_dir_p
,
1358 xg_get_file_func
*func
)
1362 GtkWidget
*filewin
, *wtoggle
, *wbox
, *wmessage
;
1363 GtkWindow
*gwin
= GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
));
1364 GtkFileChooserAction action
= (mustmatch_p
?
1365 GTK_FILE_CHOOSER_ACTION_OPEN
:
1366 GTK_FILE_CHOOSER_ACTION_SAVE
);
1367 extern int x_gtk_show_hidden_files
;
1368 extern int x_gtk_file_dialog_help_text
;
1372 action
= GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
;
1374 filewin
= gtk_file_chooser_dialog_new (prompt
, gwin
, action
,
1375 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1376 (mustmatch_p
|| only_dir_p
?
1377 GTK_STOCK_OPEN
: GTK_STOCK_OK
),
1380 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin
), TRUE
);
1382 wbox
= gtk_vbox_new (FALSE
, 0);
1383 gtk_widget_show (wbox
);
1384 wtoggle
= gtk_check_button_new_with_label ("Show hidden files.");
1386 if (x_gtk_show_hidden_files
)
1388 g_object_set (G_OBJECT (filewin
), "show-hidden", TRUE
, NULL
);
1389 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle
), TRUE
);
1391 gtk_widget_show (wtoggle
);
1392 g_signal_connect (G_OBJECT (wtoggle
), "clicked",
1393 G_CALLBACK (xg_toggle_visibility_cb
), filewin
);
1394 g_signal_connect (G_OBJECT (filewin
), "notify",
1395 G_CALLBACK (xg_toggle_notify_cb
), wtoggle
);
1397 if (x_gtk_file_dialog_help_text
)
1400 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1401 Show the C-l help text only for versions < 2.10. */
1402 if (gtk_check_version (2, 10, 0) && action
!= GTK_FILE_CHOOSER_ACTION_SAVE
)
1403 strcat (message
, "\nType C-l to display a file name text entry box.\n");
1404 strcat (message
, "\nIf you don't like this file selector, use the "
1405 "corresponding\nkey binding or customize "
1406 "use-file-dialog to turn it off.");
1408 wmessage
= gtk_label_new (message
);
1409 gtk_widget_show (wmessage
);
1412 gtk_box_pack_start (GTK_BOX (wbox
), wtoggle
, FALSE
, FALSE
, 0);
1413 if (x_gtk_file_dialog_help_text
)
1414 gtk_box_pack_start (GTK_BOX (wbox
), wmessage
, FALSE
, FALSE
, 0);
1415 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin
), wbox
);
1417 if (default_filename
)
1420 struct gcpro gcpro1
;
1421 char *utf8_filename
;
1424 file
= build_string (default_filename
);
1426 /* File chooser does not understand ~/... in the file name. It must be
1427 an absolute name starting with /. */
1428 if (default_filename
[0] != '/')
1429 file
= Fexpand_file_name (file
, Qnil
);
1431 utf8_filename
= SSDATA (ENCODE_UTF_8 (file
));
1432 if (! NILP (Ffile_directory_p (file
)))
1433 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin
),
1437 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin
),
1439 if (action
== GTK_FILE_CHOOSER_ACTION_SAVE
)
1441 char *cp
= strrchr (utf8_filename
, '/');
1443 else cp
= utf8_filename
;
1444 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin
), cp
);
1451 *func
= xg_get_file_name_from_chooser
;
1455 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1457 /* Return the selected file for file selector dialog W.
1458 The returned string must be free:d. */
1461 xg_get_file_name_from_selector (GtkWidget
*w
)
1463 GtkFileSelection
*filesel
= GTK_FILE_SELECTION (w
);
1464 return xstrdup ((char*) gtk_file_selection_get_filename (filesel
));
1467 /* Create a file selection dialog.
1468 F is the current frame.
1469 PROMPT is a prompt to show to the user. May not be NULL.
1470 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1471 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1472 file. *FUNC is set to a function that can be used to retrieve the
1473 selected file name from the returned widget.
1475 Returns the created widget. */
1478 xg_get_file_with_selection (FRAME_PTR f
,
1480 char *default_filename
,
1481 int mustmatch_p
, int only_dir_p
,
1482 xg_get_file_func
*func
)
1485 GtkFileSelection
*filesel
;
1487 filewin
= gtk_file_selection_new (prompt
);
1488 filesel
= GTK_FILE_SELECTION (filewin
);
1490 if (default_filename
)
1491 gtk_file_selection_set_filename (filesel
, default_filename
);
1495 /* The selection_entry part of filesel is not documented. */
1496 gtk_widget_set_sensitive (filesel
->selection_entry
, FALSE
);
1497 gtk_file_selection_hide_fileop_buttons (filesel
);
1500 *func
= xg_get_file_name_from_selector
;
1504 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1506 /* Read a file name from the user using a file dialog, either the old
1507 file selection dialog, or the new file chooser dialog. Which to use
1508 depends on what the GTK version used has, and what the value of
1509 gtk-use-old-file-dialog.
1510 F is the current frame.
1511 PROMPT is a prompt to show to the user. May not be NULL.
1512 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1513 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1516 Returns a file name or NULL if no file was selected.
1517 The returned string must be freed by the caller. */
1520 xg_get_file_name (FRAME_PTR f
,
1522 char *default_filename
,
1528 int filesel_done
= 0;
1529 xg_get_file_func func
;
1531 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1532 /* I really don't know why this is needed, but without this the GLIBC add on
1533 library linuxthreads hangs when the Gnome file chooser backend creates
1535 sigblock (sigmask (__SIGRTMIN
));
1536 #endif /* HAVE_GTK_AND_PTHREAD */
1538 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1540 if (xg_uses_old_file_dialog ())
1541 w
= xg_get_file_with_selection (f
, prompt
, default_filename
,
1542 mustmatch_p
, only_dir_p
, &func
);
1544 w
= xg_get_file_with_chooser (f
, prompt
, default_filename
,
1545 mustmatch_p
, only_dir_p
, &func
);
1547 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
1548 w
= xg_get_file_with_chooser (f
, prompt
, default_filename
,
1549 mustmatch_p
, only_dir_p
, &func
);
1550 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
1552 gtk_widget_set_name (w
, "emacs-filedialog");
1554 filesel_done
= xg_dialog_run (f
, w
);
1556 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1557 sigunblock (sigmask (__SIGRTMIN
));
1560 if (filesel_done
== GTK_RESPONSE_OK
)
1563 gtk_widget_destroy (w
);
1567 #ifdef HAVE_FREETYPE
1568 /* Pop up a GTK font selector and return the name of the font the user
1569 selects, as a C string. The returned font name follows GTK's own
1572 `FAMILY [VALUE1 VALUE2] SIZE'
1574 This can be parsed using font_parse_fcname in font.c.
1575 DEFAULT_NAME, if non-zero, is the default font name. */
1578 xg_get_font_name (FRAME_PTR f
, char *default_name
)
1581 char *fontname
= NULL
;
1584 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1585 sigblock (sigmask (__SIGRTMIN
));
1586 #endif /* HAVE_GTK_AND_PTHREAD */
1588 w
= gtk_font_selection_dialog_new ("Pick a font");
1590 default_name
= "Monospace 10";
1591 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w
),
1594 gtk_widget_set_name (w
, "emacs-fontdialog");
1596 done
= xg_dialog_run (f
, w
);
1598 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1599 sigunblock (sigmask (__SIGRTMIN
));
1602 if (done
== GTK_RESPONSE_OK
)
1603 fontname
= gtk_font_selection_dialog_get_font_name
1604 (GTK_FONT_SELECTION_DIALOG (w
));
1606 gtk_widget_destroy (w
);
1609 #endif /* HAVE_FREETYPE */
1613 /***********************************************************************
1615 ***********************************************************************/
1617 /* The name of menu items that can be used for customization. Since GTK
1618 RC files are very crude and primitive, we have to set this on all
1619 menu item names so a user can easily customize menu items. */
1621 #define MENU_ITEM_NAME "emacs-menuitem"
1624 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1625 during GC. The next member points to the items. */
1626 static xg_list_node xg_menu_cb_list
;
1628 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1629 during GC. The next member points to the items. */
1630 static xg_list_node xg_menu_item_cb_list
;
1632 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1633 F is the frame CL_DATA will be initialized for.
1634 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1636 The menu bar and all sub menus under the menu bar in a frame
1637 share the same structure, hence the reference count.
1639 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1640 allocated xg_menu_cb_data if CL_DATA is NULL. */
1642 static xg_menu_cb_data
*
1643 make_cl_data (xg_menu_cb_data
*cl_data
, FRAME_PTR f
, GCallback highlight_cb
)
1647 cl_data
= (xg_menu_cb_data
*) xmalloc (sizeof (*cl_data
));
1649 cl_data
->menu_bar_vector
= f
->menu_bar_vector
;
1650 cl_data
->menu_bar_items_used
= f
->menu_bar_items_used
;
1651 cl_data
->highlight_cb
= highlight_cb
;
1652 cl_data
->ref_count
= 0;
1654 xg_list_insert (&xg_menu_cb_list
, &cl_data
->ptrs
);
1657 cl_data
->ref_count
++;
1662 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1663 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1665 When the menu bar is updated, menu items may have been added and/or
1666 removed, so menu_bar_vector and menu_bar_items_used change. We must
1667 then update CL_DATA since it is used to determine which menu
1668 item that is invoked in the menu.
1669 HIGHLIGHT_CB could change, there is no check that the same
1670 function is given when modifying a menu bar as was given when
1671 creating the menu bar. */
1674 update_cl_data (xg_menu_cb_data
*cl_data
,
1676 GCallback highlight_cb
)
1681 cl_data
->menu_bar_vector
= f
->menu_bar_vector
;
1682 cl_data
->menu_bar_items_used
= f
->menu_bar_items_used
;
1683 cl_data
->highlight_cb
= highlight_cb
;
1687 /* Decrease reference count for CL_DATA.
1688 If reference count is zero, free CL_DATA. */
1691 unref_cl_data (xg_menu_cb_data
*cl_data
)
1693 if (cl_data
&& cl_data
->ref_count
> 0)
1695 cl_data
->ref_count
--;
1696 if (cl_data
->ref_count
== 0)
1698 xg_list_remove (&xg_menu_cb_list
, &cl_data
->ptrs
);
1704 /* Function that marks all lisp data during GC. */
1711 for (iter
= xg_menu_cb_list
.next
; iter
; iter
= iter
->next
)
1712 mark_object (((xg_menu_cb_data
*) iter
)->menu_bar_vector
);
1714 for (iter
= xg_menu_item_cb_list
.next
; iter
; iter
= iter
->next
)
1716 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) iter
;
1718 if (! NILP (cb_data
->help
))
1719 mark_object (cb_data
->help
);
1724 /* Callback called when a menu item is destroyed. Used to free data.
1725 W is the widget that is being destroyed (not used).
1726 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1729 menuitem_destroy_callback (GtkWidget
*w
, gpointer client_data
)
1733 xg_menu_item_cb_data
*data
= (xg_menu_item_cb_data
*) client_data
;
1734 xg_list_remove (&xg_menu_item_cb_list
, &data
->ptrs
);
1739 /* Callback called when the pointer enters/leaves a menu item.
1740 W is the parent of the menu item.
1741 EVENT is either an enter event or leave event.
1742 CLIENT_DATA is not used.
1744 Returns FALSE to tell GTK to keep processing this event. */
1747 menuitem_highlight_callback (GtkWidget
*w
,
1748 GdkEventCrossing
*event
,
1749 gpointer client_data
)
1752 GtkWidget
*subwidget
;
1753 xg_menu_item_cb_data
*data
;
1755 ev
.crossing
= *event
;
1756 subwidget
= gtk_get_event_widget (&ev
);
1757 data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (subwidget
),
1761 if (! NILP (data
->help
) && data
->cl_data
->highlight_cb
)
1763 gpointer call_data
= event
->type
== GDK_LEAVE_NOTIFY
? 0 : data
;
1764 GtkCallback func
= (GtkCallback
) data
->cl_data
->highlight_cb
;
1765 (*func
) (subwidget
, call_data
);
1772 /* Callback called when a menu is destroyed. Used to free data.
1773 W is the widget that is being destroyed (not used).
1774 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1777 menu_destroy_callback (GtkWidget
*w
, gpointer client_data
)
1779 unref_cl_data ((xg_menu_cb_data
*) client_data
);
1782 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1783 must be non-NULL) and can be inserted into a menu item.
1785 Returns the GtkHBox. */
1788 make_widget_for_menu_item (char *utf8_label
, char *utf8_key
)
1794 wbox
= gtk_hbox_new (FALSE
, 0);
1795 wlbl
= gtk_label_new (utf8_label
);
1796 wkey
= gtk_label_new (utf8_key
);
1798 gtk_misc_set_alignment (GTK_MISC (wlbl
), 0.0, 0.5);
1799 gtk_misc_set_alignment (GTK_MISC (wkey
), 0.0, 0.5);
1801 gtk_box_pack_start (GTK_BOX (wbox
), wlbl
, TRUE
, TRUE
, 0);
1802 gtk_box_pack_start (GTK_BOX (wbox
), wkey
, FALSE
, FALSE
, 0);
1804 gtk_widget_set_name (wlbl
, MENU_ITEM_NAME
);
1805 gtk_widget_set_name (wkey
, MENU_ITEM_NAME
);
1806 gtk_widget_set_name (wbox
, MENU_ITEM_NAME
);
1811 /* Make and return a menu item widget with the key to the right.
1812 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1813 UTF8_KEY is the text representing the key binding.
1814 ITEM is the widget_value describing the menu item.
1816 GROUP is an in/out parameter. If the menu item to be created is not
1817 part of any radio menu group, *GROUP contains NULL on entry and exit.
1818 If the menu item to be created is part of a radio menu group, on entry
1819 *GROUP contains the group to use, or NULL if this is the first item
1820 in the group. On exit, *GROUP contains the radio item group.
1822 Unfortunately, keys don't line up as nicely as in Motif,
1823 but the MacOS X version doesn't either, so I guess that is OK. */
1826 make_menu_item (char *utf8_label
,
1832 GtkWidget
*wtoadd
= 0;
1834 /* It has been observed that some menu items have a NULL name field.
1835 This will lead to this function being called with a NULL utf8_label.
1836 GTK crashes on that so we set a blank label. Why there is a NULL
1837 name remains to be investigated. */
1838 if (! utf8_label
) utf8_label
= " ";
1841 wtoadd
= make_widget_for_menu_item (utf8_label
, utf8_key
);
1843 if (item
->button_type
== BUTTON_TYPE_TOGGLE
)
1846 if (utf8_key
) w
= gtk_check_menu_item_new ();
1847 else w
= gtk_check_menu_item_new_with_label (utf8_label
);
1848 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), item
->selected
);
1850 else if (item
->button_type
== BUTTON_TYPE_RADIO
)
1852 if (utf8_key
) w
= gtk_radio_menu_item_new (*group
);
1853 else w
= gtk_radio_menu_item_new_with_label (*group
, utf8_label
);
1854 *group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w
));
1856 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), TRUE
);
1861 if (utf8_key
) w
= gtk_menu_item_new ();
1862 else w
= gtk_menu_item_new_with_label (utf8_label
);
1865 if (wtoadd
) gtk_container_add (GTK_CONTAINER (w
), wtoadd
);
1866 if (! item
->enabled
) gtk_widget_set_sensitive (w
, FALSE
);
1871 /* Return non-zero if LABEL specifies a separator (GTK only has one
1874 static const char* separator_names
[] = {
1879 "single-dashed-line",
1880 "double-dashed-line",
1882 "shadow-etched-out",
1883 "shadow-etched-in-dash",
1884 "shadow-etched-out-dash",
1885 "shadow-double-etched-in",
1886 "shadow-double-etched-out",
1887 "shadow-double-etched-in-dash",
1888 "shadow-double-etched-out-dash",
1893 xg_separator_p (char *label
)
1895 if (! label
) return 0;
1896 else if (strlen (label
) > 3
1897 && strncmp (label
, "--", 2) == 0
1903 for (i
= 0; separator_names
[i
]; ++i
)
1904 if (strcmp (label
, separator_names
[i
]) == 0)
1909 /* Old-style separator, maybe. It's a separator if it contains
1911 while (*label
== '-')
1913 if (*label
== 0) return 1;
1919 static int xg_detached_menus
;
1921 /* Returns non-zero if there are detached menus. */
1924 xg_have_tear_offs (void)
1926 return xg_detached_menus
> 0;
1929 /* Callback invoked when a detached menu window is removed. Here we
1930 decrease the xg_detached_menus count.
1931 WIDGET is the top level window that is removed (the parent of the menu).
1932 CLIENT_DATA is not used. */
1935 tearoff_remove (GtkWidget
*widget
, gpointer client_data
)
1937 if (xg_detached_menus
> 0) --xg_detached_menus
;
1940 /* Callback invoked when a menu is detached. It increases the
1941 xg_detached_menus count.
1942 WIDGET is the GtkTearoffMenuItem.
1943 CLIENT_DATA is not used. */
1946 tearoff_activate (GtkWidget
*widget
, gpointer client_data
)
1948 GtkWidget
*menu
= gtk_widget_get_parent (widget
);
1949 if (gtk_menu_get_tearoff_state (GTK_MENU (menu
)))
1951 ++xg_detached_menus
;
1952 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget
)),
1954 G_CALLBACK (tearoff_remove
), 0);
1959 /* Create a menu item widget, and connect the callbacks.
1960 ITEM decribes the menu item.
1961 F is the frame the created menu belongs to.
1962 SELECT_CB is the callback to use when a menu item is selected.
1963 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1964 CL_DATA points to the callback data to be used for this menu.
1965 GROUP is an in/out parameter. If the menu item to be created is not
1966 part of any radio menu group, *GROUP contains NULL on entry and exit.
1967 If the menu item to be created is part of a radio menu group, on entry
1968 *GROUP contains the group to use, or NULL if this is the first item
1969 in the group. On exit, *GROUP contains the radio item group.
1971 Returns the created GtkWidget. */
1974 xg_create_one_menuitem (widget_value
*item
,
1976 GCallback select_cb
,
1977 GCallback highlight_cb
,
1978 xg_menu_cb_data
*cl_data
,
1984 xg_menu_item_cb_data
*cb_data
;
1986 utf8_label
= get_utf8_string (item
->name
);
1987 utf8_key
= get_utf8_string (item
->key
);
1989 w
= make_menu_item (utf8_label
, utf8_key
, item
, group
);
1991 if (utf8_label
&& utf8_label
!= item
->name
) g_free (utf8_label
);
1992 if (utf8_key
&& utf8_key
!= item
->key
) g_free (utf8_key
);
1994 cb_data
= xmalloc (sizeof (xg_menu_item_cb_data
));
1996 xg_list_insert (&xg_menu_item_cb_list
, &cb_data
->ptrs
);
1998 cb_data
->select_id
= 0;
1999 cb_data
->help
= item
->help
;
2000 cb_data
->cl_data
= cl_data
;
2001 cb_data
->call_data
= item
->call_data
;
2003 g_signal_connect (G_OBJECT (w
),
2005 G_CALLBACK (menuitem_destroy_callback
),
2008 /* Put cb_data in widget, so we can get at it when modifying menubar */
2009 g_object_set_data (G_OBJECT (w
), XG_ITEM_DATA
, cb_data
);
2011 /* final item, not a submenu */
2012 if (item
->call_data
&& ! item
->contents
)
2016 = g_signal_connect (G_OBJECT (w
), "activate", select_cb
, cb_data
);
2022 /* Create a full menu tree specified by DATA.
2023 F is the frame the created menu belongs to.
2024 SELECT_CB is the callback to use when a menu item is selected.
2025 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2026 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2027 POP_UP_P is non-zero if we shall create a popup menu.
2028 MENU_BAR_P is non-zero if we shall create a menu bar.
2029 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2030 if MENU_BAR_P is non-zero.
2031 TOPMENU is the topmost GtkWidget that others shall be placed under.
2032 It may be NULL, in that case we create the appropriate widget
2033 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2034 CL_DATA is the callback data we shall use for this menu, or NULL
2035 if we haven't set the first callback yet.
2036 NAME is the name to give to the top level menu if this function
2037 creates it. May be NULL to not set any name.
2039 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2042 This function calls itself to create submenus. */
2045 create_menus (widget_value
*data
,
2047 GCallback select_cb
,
2048 GCallback deactivate_cb
,
2049 GCallback highlight_cb
,
2054 xg_menu_cb_data
*cl_data
,
2058 GtkWidget
*wmenu
= topmenu
;
2059 GSList
*group
= NULL
;
2065 wmenu
= gtk_menu_new ();
2066 xg_set_screen (wmenu
, f
);
2067 /* Connect this to the menu instead of items so we get enter/leave for
2068 disabled items also. TODO: Still does not get enter/leave for
2069 disabled items in detached menus. */
2070 g_signal_connect (G_OBJECT (wmenu
),
2071 "enter-notify-event",
2072 G_CALLBACK (menuitem_highlight_callback
),
2074 g_signal_connect (G_OBJECT (wmenu
),
2075 "leave-notify-event",
2076 G_CALLBACK (menuitem_highlight_callback
),
2081 wmenu
= gtk_menu_bar_new ();
2082 /* Set width of menu bar to a small value so it doesn't enlarge
2083 a small initial frame size. The width will be set to the
2084 width of the frame later on when it is added to a container.
2085 height -1: Natural height. */
2086 gtk_widget_set_size_request (wmenu
, 1, -1);
2089 /* Put cl_data on the top menu for easier access. */
2090 cl_data
= make_cl_data (cl_data
, f
, highlight_cb
);
2091 g_object_set_data (G_OBJECT (wmenu
), XG_FRAME_DATA
, (gpointer
)cl_data
);
2092 g_signal_connect (G_OBJECT (wmenu
), "destroy",
2093 G_CALLBACK (menu_destroy_callback
), cl_data
);
2096 gtk_widget_set_name (wmenu
, name
);
2099 g_signal_connect (G_OBJECT (wmenu
),
2100 "selection-done", deactivate_cb
, 0);
2103 if (! menu_bar_p
&& add_tearoff_p
)
2105 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new ();
2106 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu
), tearoff
);
2108 g_signal_connect (G_OBJECT (tearoff
), "activate",
2109 G_CALLBACK (tearoff_activate
), 0);
2112 for (item
= data
; item
; item
= item
->next
)
2116 if (pop_up_p
&& !item
->contents
&& !item
->call_data
2117 && !xg_separator_p (item
->name
))
2120 /* A title for a popup. We do the same as GTK does when
2121 creating titles, but it does not look good. */
2123 utf8_label
= get_utf8_string (item
->name
);
2125 gtk_menu_set_title (GTK_MENU (wmenu
), utf8_label
);
2126 w
= gtk_menu_item_new_with_label (utf8_label
);
2127 gtk_widget_set_sensitive (w
, FALSE
);
2128 if (utf8_label
&& utf8_label
!= item
->name
) g_free (utf8_label
);
2130 else if (xg_separator_p (item
->name
))
2133 /* GTK only have one separator type. */
2134 w
= gtk_separator_menu_item_new ();
2138 w
= xg_create_one_menuitem (item
,
2140 item
->contents
? 0 : select_cb
,
2145 /* Create a possibly empty submenu for menu bar items, since some
2146 themes don't highlight items correctly without it. */
2147 if (item
->contents
|| menu_bar_p
)
2149 GtkWidget
*submenu
= create_menus (item
->contents
,
2160 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w
), submenu
);
2164 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu
), w
);
2165 gtk_widget_set_name (w
, MENU_ITEM_NAME
);
2171 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2172 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2173 with some text and buttons.
2174 F is the frame the created item belongs to.
2175 NAME is the name to use for the top widget.
2176 VAL is a widget_value structure describing items to be created.
2177 SELECT_CB is the callback to use when a menu item is selected or
2178 a dialog button is pressed.
2179 DEACTIVATE_CB is the callback to use when an item is deactivated.
2180 For a menu, when a sub menu is not shown anymore, for a dialog it is
2181 called when the dialog is popped down.
2182 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2184 Returns the widget created. */
2187 xg_create_widget (char *type
, char *name
, FRAME_PTR f
, widget_value
*val
,
2188 GCallback select_cb
, GCallback deactivate_cb
,
2189 GCallback highlight_cb
)
2192 int menu_bar_p
= strcmp (type
, "menubar") == 0;
2193 int pop_up_p
= strcmp (type
, "popup") == 0;
2195 if (strcmp (type
, "dialog") == 0)
2197 w
= create_dialog (val
, select_cb
, deactivate_cb
);
2198 xg_set_screen (w
, f
);
2199 gtk_window_set_transient_for (GTK_WINDOW (w
),
2200 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
2201 gtk_window_set_destroy_with_parent (GTK_WINDOW (w
), TRUE
);
2202 gtk_widget_set_name (w
, "emacs-dialog");
2203 gtk_window_set_modal (GTK_WINDOW (w
), TRUE
);
2205 else if (menu_bar_p
|| pop_up_p
)
2207 w
= create_menus (val
->contents
,
2219 /* Set the cursor to an arrow for popup menus when they are mapped.
2220 This is done by default for menu bar menus. */
2223 /* Must realize so the GdkWindow inside the widget is created. */
2224 gtk_widget_realize (w
);
2225 xg_set_cursor (w
, FRAME_X_DISPLAY_INFO (f
)->xg_cursor
);
2230 fprintf (stderr
, "bad type in xg_create_widget: %s, doing nothing\n",
2237 /* Return the label for menu item WITEM. */
2240 xg_get_menu_item_label (GtkMenuItem
*witem
)
2242 GtkLabel
*wlabel
= GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem
)));
2243 return gtk_label_get_label (wlabel
);
2246 /* Return non-zero if the menu item WITEM has the text LABEL. */
2249 xg_item_label_same_p (GtkMenuItem
*witem
, char *label
)
2252 char *utf8_label
= get_utf8_string (label
);
2253 const char *old_label
= witem
? xg_get_menu_item_label (witem
) : 0;
2255 if (! old_label
&& ! utf8_label
)
2257 else if (old_label
&& utf8_label
)
2258 is_same
= strcmp (utf8_label
, old_label
) == 0;
2260 if (utf8_label
&& utf8_label
!= label
) g_free (utf8_label
);
2265 /* Destroy widgets in LIST. */
2268 xg_destroy_widgets (GList
*list
)
2272 for (iter
= list
; iter
; iter
= g_list_next (iter
))
2274 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
2276 /* Destroying the widget will remove it from the container it is in. */
2277 gtk_widget_destroy (w
);
2281 /* Update the top level names in MENUBAR (i.e. not submenus).
2282 F is the frame the menu bar belongs to.
2283 *LIST is a list with the current menu bar names (menu item widgets).
2284 ITER is the item within *LIST that shall be updated.
2285 POS is the numerical position, starting at 0, of ITER in *LIST.
2286 VAL describes what the menu bar shall look like after the update.
2287 SELECT_CB is the callback to use when a menu item is selected.
2288 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2289 CL_DATA points to the callback data to be used for this menu bar.
2291 This function calls itself to walk through the menu bar names. */
2294 xg_update_menubar (GtkWidget
*menubar
,
2300 GCallback select_cb
,
2301 GCallback deactivate_cb
,
2302 GCallback highlight_cb
,
2303 xg_menu_cb_data
*cl_data
)
2305 if (! iter
&& ! val
)
2307 else if (iter
&& ! val
)
2309 /* Item(s) have been removed. Remove all remaining items. */
2310 xg_destroy_widgets (iter
);
2312 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2313 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
),
2314 gtk_menu_item_new_with_label (""),
2320 else if (! iter
&& val
)
2322 /* Item(s) added. Add all new items in one call. */
2323 create_menus (val
, f
, select_cb
, deactivate_cb
, highlight_cb
,
2324 0, 1, 0, menubar
, cl_data
, 0);
2330 /* Below this neither iter or val is NULL */
2331 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter
->data
), val
->name
))
2333 /* This item is still the same, check next item. */
2335 iter
= g_list_next (iter
);
2338 else /* This item is changed. */
2340 GtkMenuItem
*witem
= GTK_MENU_ITEM (iter
->data
);
2341 GtkMenuItem
*witem2
= 0;
2342 int val_in_menubar
= 0;
2343 int iter_in_new_menubar
= 0;
2347 /* See if the changed entry (val) is present later in the menu bar */
2349 iter2
&& ! val_in_menubar
;
2350 iter2
= g_list_next (iter2
))
2352 witem2
= GTK_MENU_ITEM (iter2
->data
);
2353 val_in_menubar
= xg_item_label_same_p (witem2
, val
->name
);
2356 /* See if the current entry (iter) is present later in the
2357 specification for the new menu bar. */
2358 for (cur
= val
; cur
&& ! iter_in_new_menubar
; cur
= cur
->next
)
2359 iter_in_new_menubar
= xg_item_label_same_p (witem
, cur
->name
);
2361 if (val_in_menubar
&& ! iter_in_new_menubar
)
2365 /* This corresponds to:
2370 g_object_ref (G_OBJECT (witem
));
2371 gtk_container_remove (GTK_CONTAINER (menubar
), GTK_WIDGET (witem
));
2372 gtk_widget_destroy (GTK_WIDGET (witem
));
2374 /* Must get new list since the old changed. */
2375 g_list_free (*list
);
2376 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2377 while (nr
-- > 0) iter
= g_list_next (iter
);
2379 else if (! val_in_menubar
&& ! iter_in_new_menubar
)
2381 /* This corresponds to:
2384 Rename B to X. This might seem to be a strange thing to do,
2385 since if there is a menu under B it will be totally wrong for X.
2386 But consider editing a C file. Then there is a C-mode menu
2387 (corresponds to B above).
2388 If then doing C-x C-f the minibuf menu (X above) replaces the
2389 C-mode menu. When returning from the minibuffer, we get
2390 back the C-mode menu. Thus we do:
2391 Rename B to X (C-mode to minibuf menu)
2392 Rename X to B (minibuf to C-mode menu).
2393 If the X menu hasn't been invoked, the menu under B
2394 is up to date when leaving the minibuffer. */
2395 GtkLabel
*wlabel
= GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem
)));
2396 char *utf8_label
= get_utf8_string (val
->name
);
2397 GtkWidget
*submenu
= gtk_menu_item_get_submenu (witem
);
2399 gtk_label_set_text (wlabel
, utf8_label
);
2401 /* If this item has a submenu that has been detached, change
2402 the title in the WM decorations also. */
2403 if (submenu
&& gtk_menu_get_tearoff_state (GTK_MENU (submenu
)))
2404 /* Set the title of the detached window. */
2405 gtk_menu_set_title (GTK_MENU (submenu
), utf8_label
);
2407 iter
= g_list_next (iter
);
2411 else if (! val_in_menubar
&& iter_in_new_menubar
)
2413 /* This corresponds to:
2420 GtkWidget
*w
= xg_create_one_menuitem (val
,
2427 /* Create a possibly empty submenu for menu bar items, since some
2428 themes don't highlight items correctly without it. */
2429 GtkWidget
*submenu
= create_menus (NULL
, f
,
2430 select_cb
, deactivate_cb
,
2432 0, 0, 0, 0, cl_data
, 0);
2433 gtk_widget_set_name (w
, MENU_ITEM_NAME
);
2434 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
), w
, pos
);
2435 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w
), submenu
);
2437 g_list_free (*list
);
2438 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2439 while (nr
-- > 0) iter
= g_list_next (iter
);
2440 iter
= g_list_next (iter
);
2444 else /* if (val_in_menubar && iter_in_new_menubar) */
2447 /* This corresponds to:
2452 g_object_ref (G_OBJECT (witem2
));
2453 gtk_container_remove (GTK_CONTAINER (menubar
), GTK_WIDGET (witem2
));
2454 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
),
2455 GTK_WIDGET (witem2
), pos
);
2456 g_object_unref (G_OBJECT (witem2
));
2458 g_list_free (*list
);
2459 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2460 while (nr
-- > 0) iter
= g_list_next (iter
);
2461 if (iter
) iter
= g_list_next (iter
);
2467 /* Update the rest of the menu bar. */
2468 xg_update_menubar (menubar
, f
, list
, iter
, pos
, val
,
2469 select_cb
, deactivate_cb
, highlight_cb
, cl_data
);
2472 /* Update the menu item W so it corresponds to VAL.
2473 SELECT_CB is the callback to use when a menu item is selected.
2474 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2475 CL_DATA is the data to set in the widget for menu invocation. */
2478 xg_update_menu_item (widget_value
*val
,
2480 GCallback select_cb
,
2481 GCallback highlight_cb
,
2482 xg_menu_cb_data
*cl_data
)
2489 const char *old_label
= 0;
2490 const char *old_key
= 0;
2491 xg_menu_item_cb_data
*cb_data
;
2493 wchild
= gtk_bin_get_child (GTK_BIN (w
));
2494 utf8_label
= get_utf8_string (val
->name
);
2495 utf8_key
= get_utf8_string (val
->key
);
2497 /* See if W is a menu item with a key. See make_menu_item above. */
2498 if (GTK_IS_HBOX (wchild
))
2500 GList
*list
= gtk_container_get_children (GTK_CONTAINER (wchild
));
2502 wlbl
= GTK_LABEL (list
->data
);
2503 wkey
= GTK_LABEL (list
->next
->data
);
2508 /* Remove the key and keep just the label. */
2509 g_object_ref (G_OBJECT (wlbl
));
2510 gtk_container_remove (GTK_CONTAINER (w
), wchild
);
2511 gtk_container_add (GTK_CONTAINER (w
), GTK_WIDGET (wlbl
));
2512 g_object_unref (G_OBJECT (wlbl
));
2517 else /* Just a label. */
2519 wlbl
= GTK_LABEL (wchild
);
2521 /* Check if there is now a key. */
2524 GtkWidget
*wtoadd
= make_widget_for_menu_item (utf8_label
, utf8_key
);
2525 GList
*list
= gtk_container_get_children (GTK_CONTAINER (wtoadd
));
2527 wlbl
= GTK_LABEL (list
->data
);
2528 wkey
= GTK_LABEL (list
->next
->data
);
2531 gtk_container_remove (GTK_CONTAINER (w
), wchild
);
2532 gtk_container_add (GTK_CONTAINER (w
), wtoadd
);
2537 if (wkey
) old_key
= gtk_label_get_label (wkey
);
2538 if (wlbl
) old_label
= gtk_label_get_label (wlbl
);
2540 if (wkey
&& utf8_key
&& (! old_key
|| strcmp (utf8_key
, old_key
) != 0))
2541 gtk_label_set_text (wkey
, utf8_key
);
2543 if (! old_label
|| strcmp (utf8_label
, old_label
) != 0)
2544 gtk_label_set_text (wlbl
, utf8_label
);
2546 if (utf8_key
&& utf8_key
!= val
->key
) g_free (utf8_key
);
2547 if (utf8_label
&& utf8_label
!= val
->name
) g_free (utf8_label
);
2549 if (! val
->enabled
&& gtk_widget_get_sensitive (w
))
2550 gtk_widget_set_sensitive (w
, FALSE
);
2551 else if (val
->enabled
&& ! gtk_widget_get_sensitive (w
))
2552 gtk_widget_set_sensitive (w
, TRUE
);
2554 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (w
),
2558 cb_data
->call_data
= val
->call_data
;
2559 cb_data
->help
= val
->help
;
2560 cb_data
->cl_data
= cl_data
;
2562 /* We assume the callback functions don't change. */
2563 if (val
->call_data
&& ! val
->contents
)
2565 /* This item shall have a select callback. */
2566 if (! cb_data
->select_id
)
2568 = g_signal_connect (G_OBJECT (w
), "activate",
2569 select_cb
, cb_data
);
2571 else if (cb_data
->select_id
)
2573 g_signal_handler_disconnect (w
, cb_data
->select_id
);
2574 cb_data
->select_id
= 0;
2579 /* Update the toggle menu item W so it corresponds to VAL. */
2582 xg_update_toggle_item (widget_value
*val
, GtkWidget
*w
)
2584 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), val
->selected
);
2587 /* Update the radio menu item W so it corresponds to VAL. */
2590 xg_update_radio_item (widget_value
*val
, GtkWidget
*w
)
2592 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), val
->selected
);
2595 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2596 SUBMENU may be NULL, in that case a new menu is created.
2597 F is the frame the menu bar belongs to.
2598 VAL describes the contents of the menu bar.
2599 SELECT_CB is the callback to use when a menu item is selected.
2600 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2601 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2602 CL_DATA is the call back data to use for any newly created items.
2604 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2608 xg_update_submenu (GtkWidget
*submenu
,
2611 GCallback select_cb
,
2612 GCallback deactivate_cb
,
2613 GCallback highlight_cb
,
2614 xg_menu_cb_data
*cl_data
)
2616 GtkWidget
*newsub
= submenu
;
2620 int has_tearoff_p
= 0;
2621 GList
*first_radio
= 0;
2624 list
= gtk_container_get_children (GTK_CONTAINER (submenu
));
2626 for (cur
= val
, iter
= list
;
2628 iter
= g_list_next (iter
), cur
= cur
->next
)
2630 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
2632 /* Skip tearoff items, they have no counterpart in val. */
2633 if (GTK_IS_TEAROFF_MENU_ITEM (w
))
2636 iter
= g_list_next (iter
);
2637 if (iter
) w
= GTK_WIDGET (iter
->data
);
2641 /* Remember first radio button in a group. If we get a mismatch in
2642 a radio group we must rebuild the whole group so that the connections
2643 in GTK becomes correct. */
2644 if (cur
->button_type
== BUTTON_TYPE_RADIO
&& ! first_radio
)
2646 else if (cur
->button_type
!= BUTTON_TYPE_RADIO
2647 && ! GTK_IS_RADIO_MENU_ITEM (w
))
2650 if (GTK_IS_SEPARATOR_MENU_ITEM (w
))
2652 if (! xg_separator_p (cur
->name
))
2655 else if (GTK_IS_CHECK_MENU_ITEM (w
))
2657 if (cur
->button_type
!= BUTTON_TYPE_TOGGLE
)
2659 xg_update_toggle_item (cur
, w
);
2660 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
2662 else if (GTK_IS_RADIO_MENU_ITEM (w
))
2664 if (cur
->button_type
!= BUTTON_TYPE_RADIO
)
2666 xg_update_radio_item (cur
, w
);
2667 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
2669 else if (GTK_IS_MENU_ITEM (w
))
2671 GtkMenuItem
*witem
= GTK_MENU_ITEM (w
);
2674 if (cur
->button_type
!= BUTTON_TYPE_NONE
||
2675 xg_separator_p (cur
->name
))
2678 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
2680 sub
= gtk_menu_item_get_submenu (witem
);
2681 if (sub
&& ! cur
->contents
)
2683 /* Not a submenu anymore. */
2684 g_object_ref (G_OBJECT (sub
));
2685 remove_submenu (witem
);
2686 gtk_widget_destroy (sub
);
2688 else if (cur
->contents
)
2692 nsub
= xg_update_submenu (sub
, f
, cur
->contents
,
2693 select_cb
, deactivate_cb
,
2694 highlight_cb
, cl_data
);
2696 /* If this item just became a submenu, we must set it. */
2698 gtk_menu_item_set_submenu (witem
, nsub
);
2703 /* Structural difference. Remove everything from here and down
2709 /* Remove widgets from first structual change. */
2712 /* If we are adding new menu items below, we must remove from
2713 first radio button so that radio groups become correct. */
2714 if (cur
&& first_radio
) xg_destroy_widgets (first_radio
);
2715 else xg_destroy_widgets (iter
);
2720 /* More items added. Create them. */
2721 newsub
= create_menus (cur
,
2734 if (list
) g_list_free (list
);
2739 /* Update the MENUBAR.
2740 F is the frame the menu bar belongs to.
2741 VAL describes the contents of the menu bar.
2742 If DEEP_P is non-zero, rebuild all but the top level menu names in
2743 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2744 SELECT_CB is the callback to use when a menu item is selected.
2745 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2746 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2749 xg_modify_menubar_widgets (GtkWidget
*menubar
, FRAME_PTR f
, widget_value
*val
,
2751 GCallback select_cb
, GCallback deactivate_cb
,
2752 GCallback highlight_cb
)
2754 xg_menu_cb_data
*cl_data
;
2755 GList
*list
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2759 cl_data
= (xg_menu_cb_data
*) g_object_get_data (G_OBJECT (menubar
),
2762 xg_update_menubar (menubar
, f
, &list
, list
, 0, val
->contents
,
2763 select_cb
, deactivate_cb
, highlight_cb
, cl_data
);
2769 /* Update all sub menus.
2770 We must keep the submenus (GTK menu item widgets) since the
2771 X Window in the XEvent that activates the menu are those widgets. */
2773 /* Update cl_data, menu_item things in F may have changed. */
2774 update_cl_data (cl_data
, f
, highlight_cb
);
2776 for (cur
= val
->contents
; cur
; cur
= cur
->next
)
2781 GtkMenuItem
*witem
= 0;
2783 /* Find sub menu that corresponds to val and update it. */
2784 for (iter
= list
; iter
; iter
= g_list_next (iter
))
2786 witem
= GTK_MENU_ITEM (iter
->data
);
2787 if (xg_item_label_same_p (witem
, cur
->name
))
2789 sub
= gtk_menu_item_get_submenu (witem
);
2794 newsub
= xg_update_submenu (sub
,
2801 /* sub may still be NULL. If we just updated non deep and added
2802 a new menu bar item, it has no sub menu yet. So we set the
2803 newly created sub menu under witem. */
2804 if (newsub
!= sub
&& witem
!= 0)
2806 xg_set_screen (newsub
, f
);
2807 gtk_menu_item_set_submenu (witem
, newsub
);
2813 gtk_widget_show_all (menubar
);
2816 /* Recompute all the widgets of frame F, when the menu bar has been
2817 changed. Value is non-zero if widgets were updated. */
2820 xg_update_frame_menubar (FRAME_PTR f
)
2822 struct x_output
*x
= f
->output_data
.x
;
2825 if (!x
->menubar_widget
|| gtk_widget_get_mapped (x
->menubar_widget
))
2828 if (x
->menubar_widget
&& gtk_widget_get_parent (x
->menubar_widget
))
2829 return 0; /* Already done this, happens for frames created invisible. */
2833 gtk_box_pack_start (GTK_BOX (x
->vbox_widget
), x
->menubar_widget
,
2835 gtk_box_reorder_child (GTK_BOX (x
->vbox_widget
), x
->menubar_widget
, 0);
2837 gtk_widget_show_all (x
->menubar_widget
);
2838 gtk_widget_size_request (x
->menubar_widget
, &req
);
2839 FRAME_MENUBAR_HEIGHT (f
) = req
.height
;
2840 xg_height_or_width_changed (f
);
2846 /* Get rid of the menu bar of frame F, and free its storage.
2847 This is used when deleting a frame, and when turning off the menu bar. */
2850 free_frame_menubar (FRAME_PTR f
)
2852 struct x_output
*x
= f
->output_data
.x
;
2854 if (x
->menubar_widget
)
2858 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
), x
->menubar_widget
);
2859 /* The menubar and its children shall be deleted when removed from
2861 x
->menubar_widget
= 0;
2862 FRAME_MENUBAR_HEIGHT (f
) = 0;
2863 xg_height_or_width_changed (f
);
2869 xg_event_is_for_menubar (FRAME_PTR f
, XEvent
*event
)
2871 struct x_output
*x
= f
->output_data
.x
;
2880 if (! x
->menubar_widget
) return 0;
2882 if (! (event
->xbutton
.x
>= 0
2883 && event
->xbutton
.x
< FRAME_PIXEL_WIDTH (f
)
2884 && event
->xbutton
.y
>= 0
2885 && event
->xbutton
.y
< f
->output_data
.x
->menubar_height
2886 && event
->xbutton
.same_screen
))
2889 gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
2890 gw
= gdk_xid_table_lookup_for_display (gdpy
, event
->xbutton
.window
);
2892 gevent
.any
.window
= gw
;
2893 gwdesc
= gtk_get_event_widget (&gevent
);
2894 if (! gwdesc
) return 0;
2895 if (! GTK_IS_MENU_BAR (gwdesc
)
2896 && ! GTK_IS_MENU_ITEM (gwdesc
)
2897 && ! gtk_widget_is_ancestor (x
->menubar_widget
, gwdesc
))
2900 list
= gtk_container_get_children (GTK_CONTAINER (x
->menubar_widget
));
2901 if (! list
) return 0;
2902 rec
.x
= event
->xbutton
.x
;
2903 rec
.y
= event
->xbutton
.y
;
2907 for (iter
= list
; iter
; iter
= g_list_next (iter
))
2909 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
2910 if (gtk_widget_get_mapped (w
) && gtk_widget_intersect (w
, &rec
, NULL
))
2914 return iter
== 0 ? 0 : 1;
2919 /***********************************************************************
2920 Scroll bar functions
2921 ***********************************************************************/
2924 /* Setting scroll bar values invokes the callback. Use this variable
2925 to indicate that callback should do nothing. */
2927 int xg_ignore_gtk_scrollbar
;
2929 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
2930 may be larger than 32 bits. Keep a mapping from integer index to widget
2931 pointers to get around the 32 bit limitation. */
2935 GtkWidget
**widgets
;
2940 /* Grow this much every time we need to allocate more */
2942 #define ID_TO_WIDGET_INCR 32
2944 /* Store the widget pointer W in id_to_widget and return the integer index. */
2947 xg_store_widget_in_map (GtkWidget
*w
)
2951 if (id_to_widget
.max_size
== id_to_widget
.used
)
2953 int new_size
= id_to_widget
.max_size
+ ID_TO_WIDGET_INCR
;
2955 id_to_widget
.widgets
= xrealloc (id_to_widget
.widgets
,
2956 sizeof (GtkWidget
*)*new_size
);
2958 for (i
= id_to_widget
.max_size
; i
< new_size
; ++i
)
2959 id_to_widget
.widgets
[i
] = 0;
2960 id_to_widget
.max_size
= new_size
;
2963 /* Just loop over the array and find a free place. After all,
2964 how many scroll bars are we creating? Should be a small number.
2965 The check above guarantees we will find a free place. */
2966 for (i
= 0; i
< id_to_widget
.max_size
; ++i
)
2968 if (! id_to_widget
.widgets
[i
])
2970 id_to_widget
.widgets
[i
] = w
;
2971 ++id_to_widget
.used
;
2977 /* Should never end up here */
2981 /* Remove pointer at IDX from id_to_widget.
2982 Called when scroll bar is destroyed. */
2985 xg_remove_widget_from_map (int idx
)
2987 if (idx
< id_to_widget
.max_size
&& id_to_widget
.widgets
[idx
] != 0)
2989 id_to_widget
.widgets
[idx
] = 0;
2990 --id_to_widget
.used
;
2994 /* Get the widget pointer at IDX from id_to_widget. */
2997 xg_get_widget_from_map (int idx
)
2999 if (idx
< id_to_widget
.max_size
&& id_to_widget
.widgets
[idx
] != 0)
3000 return id_to_widget
.widgets
[idx
];
3005 /* Return the scrollbar id for X Window WID on display DPY.
3006 Return -1 if WID not in id_to_widget. */
3009 xg_get_scroll_id_for_window (Display
*dpy
, Window wid
)
3014 w
= xg_win_to_widget (dpy
, wid
);
3018 for (idx
= 0; idx
< id_to_widget
.max_size
; ++idx
)
3019 if (id_to_widget
.widgets
[idx
] == w
)
3026 /* Callback invoked when scroll bar WIDGET is destroyed.
3027 DATA is the index into id_to_widget for WIDGET.
3028 We free pointer to last scroll bar values here and remove the index. */
3031 xg_gtk_scroll_destroy (GtkWidget
*widget
, gpointer data
)
3033 int id
= (int) (EMACS_INT
) data
; /* The EMACS_INT cast avoids a warning. */
3034 xg_remove_widget_from_map (id
);
3037 /* Create a scroll bar widget for frame F. Store the scroll bar
3039 SCROLL_CALLBACK is the callback to invoke when the value of the
3041 END_CALLBACK is the callback to invoke when scrolling ends.
3042 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3043 to set resources for the widget. */
3046 xg_create_scroll_bar (FRAME_PTR f
,
3047 struct scroll_bar
*bar
,
3048 GCallback scroll_callback
,
3049 GCallback end_callback
,
3050 char *scroll_bar_name
)
3057 /* Page, step increment values are not so important here, they
3058 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3059 vadj
= gtk_adjustment_new (XG_SB_MIN
, XG_SB_MIN
, XG_SB_MAX
,
3062 wscroll
= gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj
));
3063 webox
= gtk_event_box_new ();
3064 gtk_widget_set_name (wscroll
, scroll_bar_name
);
3065 gtk_range_set_update_policy (GTK_RANGE (wscroll
), GTK_UPDATE_CONTINUOUS
);
3066 g_object_set_data (G_OBJECT (wscroll
), XG_FRAME_DATA
, (gpointer
)f
);
3068 scroll_id
= xg_store_widget_in_map (wscroll
);
3070 /* The EMACS_INT cast avoids a warning. */
3071 g_signal_connect (G_OBJECT (wscroll
),
3073 G_CALLBACK (xg_gtk_scroll_destroy
),
3074 (gpointer
) (EMACS_INT
) scroll_id
);
3075 g_signal_connect (G_OBJECT (wscroll
),
3079 g_signal_connect (G_OBJECT (wscroll
),
3080 "button-release-event",
3084 /* The scroll bar widget does not draw on a window of its own. Instead
3085 it draws on the parent window, in this case the edit widget. So
3086 whenever the edit widget is cleared, the scroll bar needs to redraw
3087 also, which causes flicker. Put an event box between the edit widget
3088 and the scroll bar, so the scroll bar instead draws itself on the
3089 event box window. */
3090 gtk_fixed_put (GTK_FIXED (f
->output_data
.x
->edit_widget
), webox
, -1, -1);
3091 gtk_container_add (GTK_CONTAINER (webox
), wscroll
);
3094 /* Set the cursor to an arrow. */
3095 xg_set_cursor (webox
, FRAME_X_DISPLAY_INFO (f
)->xg_cursor
);
3097 bar
->x_window
= scroll_id
;
3100 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3103 xg_remove_scroll_bar (FRAME_PTR f
, int scrollbar_id
)
3105 GtkWidget
*w
= xg_get_widget_from_map (scrollbar_id
);
3108 GtkWidget
*wparent
= gtk_widget_get_parent (w
);
3109 gtk_widget_destroy (w
);
3110 gtk_widget_destroy (wparent
);
3111 SET_FRAME_GARBAGED (f
);
3115 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3117 TOP/LEFT are the new pixel positions where the bar shall appear.
3118 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3121 xg_update_scrollbar_pos (FRAME_PTR f
,
3129 GtkWidget
*wscroll
= xg_get_widget_from_map (scrollbar_id
);
3133 GtkWidget
*wfixed
= f
->output_data
.x
->edit_widget
;
3134 GtkWidget
*wparent
= gtk_widget_get_parent (wscroll
);
3137 /* Clear out old position. */
3138 int oldx
= -1, oldy
= -1, oldw
, oldh
;
3139 if (gtk_widget_get_parent (wparent
) == wfixed
)
3141 gtk_container_child_get (GTK_CONTAINER (wfixed
), wparent
,
3142 "x", &oldx
, "y", &oldy
, NULL
);
3143 gtk_widget_get_size_request (wscroll
, &oldw
, &oldh
);
3146 /* Move and resize to new values. */
3147 gtk_fixed_move (GTK_FIXED (wfixed
), wparent
, left
, top
);
3148 gtk_widget_style_get (wscroll
, "min-slider-length", &msl
, NULL
);
3151 /* No room. Hide scroll bar as some themes output a warning if
3152 the height is less than the min size. */
3153 gtk_widget_hide (wparent
);
3154 gtk_widget_hide (wscroll
);
3158 gtk_widget_show_all (wparent
);
3159 gtk_widget_set_size_request (wscroll
, width
, height
);
3161 gtk_widget_queue_draw (wfixed
);
3162 gdk_window_process_all_updates ();
3163 if (oldx
!= -1 && oldw
> 0 && oldh
> 0)
3165 /* Clear under old scroll bar position. This must be done after
3166 the gtk_widget_queue_draw and gdk_window_process_all_updates
3168 x_clear_area (FRAME_X_DISPLAY (f
),
3170 oldx
, oldy
, oldw
, oldh
, 0);
3173 /* GTK does not redraw until the main loop is entered again, but
3174 if there are no X events pending we will not enter it. So we sync
3175 here to get some events. */
3178 SET_FRAME_GARBAGED (f
);
3179 cancel_mouse_face (f
);
3183 /* Set the thumb size and position of scroll bar BAR. We are currently
3184 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3187 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar
*bar
,
3192 GtkWidget
*wscroll
= xg_get_widget_from_map (bar
->x_window
);
3194 FRAME_PTR f
= XFRAME (WINDOW_FRAME (XWINDOW (bar
->window
)));
3196 if (wscroll
&& NILP (bar
->dragging
))
3205 adj
= gtk_range_get_adjustment (GTK_RANGE (wscroll
));
3207 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3208 rather than the real portion value. This makes the thumb less likely
3209 to resize and that looks better. */
3210 portion
= WINDOW_TOTAL_LINES (XWINDOW (bar
->window
)) * 30;
3211 /* When the thumb is at the bottom, position == whole.
3212 So we need to increase `whole' to make space for the thumb. */
3219 top
= (gdouble
) position
/ whole
;
3220 shown
= (gdouble
) portion
/ whole
;
3223 size
= shown
* XG_SB_RANGE
;
3224 size
= min (size
, XG_SB_RANGE
);
3225 size
= max (size
, 1);
3227 value
= top
* XG_SB_RANGE
;
3228 value
= min (value
, XG_SB_MAX
- size
);
3229 value
= max (value
, XG_SB_MIN
);
3231 /* Assume all lines are of equal size. */
3232 new_step
= size
/ max (1, FRAME_LINES (f
));
3234 if ((int) gtk_adjustment_get_page_size (adj
) != size
3235 || (int) gtk_adjustment_get_step_increment (adj
) != new_step
)
3237 gtk_adjustment_set_page_size (adj
, size
);
3238 gtk_adjustment_set_step_increment (adj
, new_step
);
3239 /* Assume a page increment is about 95% of the page size */
3240 gtk_adjustment_set_page_increment (adj
,(int) (0.95*size
));
3244 if (changed
|| (int) gtk_range_get_value (GTK_RANGE (wscroll
)) != value
)
3248 /* gtk_range_set_value invokes the callback. Set
3249 ignore_gtk_scrollbar to make the callback do nothing */
3250 xg_ignore_gtk_scrollbar
= 1;
3252 if ((int) gtk_range_get_value (GTK_RANGE (wscroll
)) != value
)
3253 gtk_range_set_value (GTK_RANGE (wscroll
), (gdouble
)value
);
3255 gtk_adjustment_changed (adj
);
3257 xg_ignore_gtk_scrollbar
= 0;
3264 /* Return non-zero if EVENT is for a scroll bar in frame F.
3265 When the same X window is used for several Gtk+ widgets, we cannot
3266 say for sure based on the X window alone if an event is for the
3267 frame. This function does additional checks.
3269 Return non-zero if the event is for a scroll bar, zero otherwise. */
3272 xg_event_is_for_scrollbar (FRAME_PTR f
, XEvent
*event
)
3276 if (f
&& event
->type
== ButtonPress
&& event
->xbutton
.button
< 4)
3278 /* Check if press occurred outside the edit widget. */
3279 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
3280 retval
= gdk_display_get_window_at_pointer (gdpy
, NULL
, NULL
)
3281 != gtk_widget_get_window (f
->output_data
.x
->edit_widget
);
3284 && ((event
->type
== ButtonRelease
&& event
->xbutton
.button
< 4)
3285 || event
->type
== MotionNotify
))
3287 /* If we are releasing or moving the scroll bar, it has the grab. */
3288 GtkWidget
*w
= gtk_grab_get_current ();
3289 retval
= w
!= 0 && GTK_IS_SCROLLBAR (w
);
3297 /***********************************************************************
3299 ***********************************************************************/
3300 /* The key for the data we put in the GtkImage widgets. The data is
3301 the image used by Emacs. We use this to see if we need to update
3302 the GtkImage with a new image. */
3303 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3305 /* The key for storing the latest modifiers so the activate callback can
3307 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3309 /* The key for storing the button widget in its proxy menu item. */
3310 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3312 /* The key for the data we put in the GtkImage widgets. The data is
3313 the stock name used by Emacs. We use this to see if we need to update
3314 the GtkImage with a new image. */
3315 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3317 /* As above, but this is used for named theme widgets, as opposed to
3319 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3321 /* Callback function invoked when a tool bar item is pressed.
3322 W is the button widget in the tool bar that got pressed,
3323 CLIENT_DATA is an integer that is the index of the button in the
3324 tool bar. 0 is the first button. */
3327 xg_tool_bar_button_cb (GtkWidget
*widget
,
3328 GdkEventButton
*event
,
3331 /* Casts to avoid warnings when gpointer is 64 bits and int is 32 bits */
3332 gpointer ptr
= (gpointer
) (EMACS_INT
) event
->state
;
3333 g_object_set_data (G_OBJECT (widget
), XG_TOOL_BAR_LAST_MODIFIER
, ptr
);
3338 /* Callback function invoked when a tool bar item is pressed.
3339 W is the button widget in the tool bar that got pressed,
3340 CLIENT_DATA is an integer that is the index of the button in the
3341 tool bar. 0 is the first button. */
3344 xg_tool_bar_callback (GtkWidget
*w
, gpointer client_data
)
3346 /* The EMACS_INT cast avoids a warning. */
3347 int idx
= (int) (EMACS_INT
) client_data
;
3348 int mod
= (int) (EMACS_INT
) g_object_get_data (G_OBJECT (w
),
3349 XG_TOOL_BAR_LAST_MODIFIER
);
3351 FRAME_PTR f
= (FRAME_PTR
) g_object_get_data (G_OBJECT (w
), XG_FRAME_DATA
);
3352 Lisp_Object key
, frame
;
3353 struct input_event event
;
3356 if (! f
|| ! f
->n_tool_bar_items
|| NILP (f
->tool_bar_items
))
3359 idx
*= TOOL_BAR_ITEM_NSLOTS
;
3361 key
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_KEY
);
3362 XSETFRAME (frame
, f
);
3364 /* We generate two events here. The first one is to set the prefix
3365 to `(tool_bar)', see keyboard.c. */
3366 event
.kind
= TOOL_BAR_EVENT
;
3367 event
.frame_or_window
= frame
;
3369 kbd_buffer_store_event (&event
);
3371 event
.kind
= TOOL_BAR_EVENT
;
3372 event
.frame_or_window
= frame
;
3374 /* Convert between the modifier bits GDK uses and the modifier bits
3375 Emacs uses. This assumes GDK and X masks are the same, which they are when
3377 event
.modifiers
= x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f
), mod
);
3378 kbd_buffer_store_event (&event
);
3380 /* Return focus to the frame after we have clicked on a detached
3382 Fx_focus_frame (frame
);
3385 /* Callback function invoked when a tool bar item is pressed in a detached
3386 tool bar or the overflow drop down menu.
3387 We just call xg_tool_bar_callback.
3388 W is the menu item widget that got pressed,
3389 CLIENT_DATA is an integer that is the index of the button in the
3390 tool bar. 0 is the first button. */
3393 xg_tool_bar_proxy_callback (GtkWidget
*w
, gpointer client_data
)
3395 GtkWidget
*wbutton
= GTK_WIDGET (g_object_get_data (G_OBJECT (w
),
3396 XG_TOOL_BAR_PROXY_BUTTON
));
3397 xg_tool_bar_callback (wbutton
, client_data
);
3402 xg_tool_bar_help_callback (GtkWidget
*w
,
3403 GdkEventCrossing
*event
,
3404 gpointer client_data
);
3406 /* This callback is called when a help is to be shown for an item in
3407 the detached tool bar when the detached tool bar it is not expanded. */
3410 xg_tool_bar_proxy_help_callback (GtkWidget
*w
,
3411 GdkEventCrossing
*event
,
3412 gpointer client_data
)
3414 GtkWidget
*wbutton
= GTK_WIDGET (g_object_get_data (G_OBJECT (w
),
3415 XG_TOOL_BAR_PROXY_BUTTON
));
3417 return xg_tool_bar_help_callback (wbutton
, event
, client_data
);
3421 xg_get_tool_bar_widgets (GtkWidget
*vb
, GtkWidget
**wimage
)
3423 GList
*clist
= gtk_container_get_children (GTK_CONTAINER (vb
));
3424 GtkWidget
*c1
= (GtkWidget
*) clist
->data
;
3425 GtkWidget
*c2
= (GtkWidget
*) clist
->next
->data
;
3426 *wimage
= GTK_IS_IMAGE (c1
) ? c1
: c2
;
3427 g_list_free (clist
);
3428 return GTK_IS_LABEL (c1
) ? c1
: c2
;
3432 /* This callback is called when a tool item should create a proxy item,
3433 such as for the overflow menu. Also called when the tool bar is detached.
3434 If we don't create a proxy menu item, the detached tool bar will be
3438 xg_tool_bar_menu_proxy (GtkToolItem
*toolitem
, gpointer user_data
)
3440 GtkWidget
*weventbox
= gtk_bin_get_child (GTK_BIN (toolitem
));
3441 GtkButton
*wbutton
= GTK_BUTTON (gtk_bin_get_child (GTK_BIN (weventbox
)));
3442 GtkWidget
*vb
= gtk_bin_get_child (GTK_BIN (wbutton
));
3444 GtkLabel
*wlbl
= GTK_LABEL (xg_get_tool_bar_widgets (vb
, &c1
));
3445 GtkImage
*wimage
= GTK_IMAGE (c1
);
3446 GtkWidget
*wmenuitem
= gtk_image_menu_item_new_with_label
3447 (gtk_label_get_text (wlbl
));
3448 GtkWidget
*wmenuimage
;
3451 if (gtk_button_get_use_stock (wbutton
))
3452 wmenuimage
= gtk_image_new_from_stock (gtk_button_get_label (wbutton
),
3453 GTK_ICON_SIZE_MENU
);
3456 GtkSettings
*settings
= gtk_widget_get_settings (GTK_WIDGET (wbutton
));
3457 GtkImageType store_type
= gtk_image_get_storage_type (wimage
);
3459 if (store_type
== GTK_IMAGE_STOCK
)
3462 gtk_image_get_stock (wimage
, &stock_id
, NULL
);
3463 wmenuimage
= gtk_image_new_from_stock (stock_id
, GTK_ICON_SIZE_MENU
);
3465 else if (store_type
== GTK_IMAGE_ICON_SET
)
3467 GtkIconSet
*icon_set
;
3468 gtk_image_get_icon_set (wimage
, &icon_set
, NULL
);
3469 wmenuimage
= gtk_image_new_from_icon_set (icon_set
,
3470 GTK_ICON_SIZE_MENU
);
3472 else if (store_type
== GTK_IMAGE_PIXBUF
)
3477 gtk_icon_size_lookup_for_settings (settings
, GTK_ICON_SIZE_MENU
,
3480 GdkPixbuf
*src_pixbuf
, *dest_pixbuf
;
3482 src_pixbuf
= gtk_image_get_pixbuf (wimage
);
3483 dest_pixbuf
= gdk_pixbuf_scale_simple (src_pixbuf
, width
, height
,
3484 GDK_INTERP_BILINEAR
);
3486 wmenuimage
= gtk_image_new_from_pixbuf (dest_pixbuf
);
3490 fprintf (stderr
, "internal error: GTK_IMAGE_PIXBUF failed\n");
3494 else if (store_type
== GTK_IMAGE_ICON_NAME
)
3496 const gchar
*icon_name
;
3497 GtkIconSize icon_size
;
3499 gtk_image_get_icon_name (wimage
, &icon_name
, &icon_size
);
3500 wmenuimage
= gtk_image_new_from_icon_name (icon_name
,
3501 GTK_ICON_SIZE_MENU
);
3505 fprintf (stderr
, "internal error: store_type is %d\n", store_type
);
3510 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem
), wmenuimage
);
3512 g_signal_connect (G_OBJECT (wmenuitem
),
3514 G_CALLBACK (xg_tool_bar_proxy_callback
),
3518 g_object_set_data (G_OBJECT (wmenuitem
), XG_TOOL_BAR_PROXY_BUTTON
,
3519 (gpointer
) wbutton
);
3520 gtk_tool_item_set_proxy_menu_item (toolitem
, "Emacs toolbar item", wmenuitem
);
3521 gtk_widget_set_sensitive (wmenuitem
,
3522 gtk_widget_get_sensitive (GTK_WIDGET (wbutton
)));
3524 /* Use enter/leave notify to show help. We use the events
3525 rather than the GtkButton specific signals "enter" and
3526 "leave", so we can have only one callback. The event
3527 will tell us what kind of event it is. */
3528 g_signal_connect (G_OBJECT (wmenuitem
),
3529 "enter-notify-event",
3530 G_CALLBACK (xg_tool_bar_proxy_help_callback
),
3532 g_signal_connect (G_OBJECT (wmenuitem
),
3533 "leave-notify-event",
3534 G_CALLBACK (xg_tool_bar_proxy_help_callback
),
3540 /* This callback is called when a tool bar is detached. We must set
3541 the height of the tool bar to zero when this happens so frame sizes
3542 are correctly calculated.
3543 WBOX is the handle box widget that enables detach/attach of the tool bar.
3544 W is the tool bar widget.
3545 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3548 xg_tool_bar_detach_callback (GtkHandleBox
*wbox
,
3550 gpointer client_data
)
3552 FRAME_PTR f
= (FRAME_PTR
) client_data
;
3553 extern int x_gtk_whole_detached_tool_bar
;
3555 g_object_set (G_OBJECT (w
), "show-arrow", !x_gtk_whole_detached_tool_bar
,
3560 GtkRequisition req
, req2
;
3561 FRAME_X_OUTPUT (f
)->toolbar_detached
= 1;
3562 gtk_widget_size_request (GTK_WIDGET (wbox
), &req
);
3563 gtk_widget_size_request (w
, &req2
);
3564 req
.width
-= req2
.width
;
3565 req
.height
-= req2
.height
;
3566 if (FRAME_TOOLBAR_TOP_HEIGHT (f
) != 0)
3567 FRAME_TOOLBAR_TOP_HEIGHT (f
) = req
.height
;
3568 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) != 0)
3569 FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = req
.height
;
3570 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f
) != 0)
3571 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = req
.width
;
3572 else if (FRAME_TOOLBAR_LEFT_WIDTH (f
) != 0)
3573 FRAME_TOOLBAR_LEFT_WIDTH (f
) = req
.width
;
3574 xg_height_or_width_changed (f
);
3578 /* This callback is called when a tool bar is reattached. We must set
3579 the height of the tool bar when this happens so frame sizes
3580 are correctly calculated.
3581 WBOX is the handle box widget that enables detach/attach of the tool bar.
3582 W is the tool bar widget.
3583 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3586 xg_tool_bar_attach_callback (GtkHandleBox
*wbox
,
3588 gpointer client_data
)
3590 FRAME_PTR f
= (FRAME_PTR
) client_data
;
3591 g_object_set (G_OBJECT (w
), "show-arrow", TRUE
, NULL
);
3595 GtkRequisition req
, req2
;
3596 FRAME_X_OUTPUT (f
)->toolbar_detached
= 0;
3597 gtk_widget_size_request (GTK_WIDGET (wbox
), &req
);
3598 gtk_widget_size_request (w
, &req2
);
3599 req
.width
+= req2
.width
;
3600 req
.height
+= req2
.height
;
3601 if (FRAME_TOOLBAR_TOP_HEIGHT (f
) != 0)
3602 FRAME_TOOLBAR_TOP_HEIGHT (f
) = req
.height
;
3603 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) != 0)
3604 FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = req
.height
;
3605 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f
) != 0)
3606 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = req
.width
;
3607 else if (FRAME_TOOLBAR_LEFT_WIDTH (f
) != 0)
3608 FRAME_TOOLBAR_LEFT_WIDTH (f
) = req
.width
;
3609 xg_height_or_width_changed (f
);
3613 /* This callback is called when the mouse enters or leaves a tool bar item.
3614 It is used for displaying and hiding the help text.
3615 W is the tool bar item, a button.
3616 EVENT is either an enter event or leave event.
3617 CLIENT_DATA is an integer that is the index of the button in the
3618 tool bar. 0 is the first button.
3620 Returns FALSE to tell GTK to keep processing this event. */
3623 xg_tool_bar_help_callback (GtkWidget
*w
,
3624 GdkEventCrossing
*event
,
3625 gpointer client_data
)
3627 /* The EMACS_INT cast avoids a warning. */
3628 int idx
= (int) (EMACS_INT
) client_data
;
3629 FRAME_PTR f
= (FRAME_PTR
) g_object_get_data (G_OBJECT (w
), XG_FRAME_DATA
);
3630 Lisp_Object help
, frame
;
3632 if (! f
|| ! f
->n_tool_bar_items
|| NILP (f
->tool_bar_items
))
3635 if (event
->type
== GDK_ENTER_NOTIFY
)
3637 idx
*= TOOL_BAR_ITEM_NSLOTS
;
3638 help
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_HELP
);
3641 help
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_CAPTION
);
3646 XSETFRAME (frame
, f
);
3647 kbd_buffer_store_help_event (frame
, help
);
3653 /* This callback is called when a tool bar item shall be redrawn.
3654 It modifies the expose event so that the GtkImage widget redraws the
3655 whole image. This to overcome a bug that makes GtkImage draw the image
3656 in the wrong place when it tries to redraw just a part of the image.
3657 W is the GtkImage to be redrawn.
3658 EVENT is the expose event for W.
3659 CLIENT_DATA is unused.
3661 Returns FALSE to tell GTK to keep processing this event. */
3664 xg_tool_bar_item_expose_callback (GtkWidget
*w
,
3665 GdkEventExpose
*event
,
3666 gpointer client_data
)
3670 gdk_drawable_get_size (event
->window
, &width
, &height
);
3672 event
->area
.x
-= width
> event
->area
.width
? width
-event
->area
.width
: 0;
3673 event
->area
.y
-= height
> event
->area
.height
? height
-event
->area
.height
: 0;
3675 event
->area
.x
= max (0, event
->area
.x
);
3676 event
->area
.y
= max (0, event
->area
.y
);
3678 event
->area
.width
= max (width
, event
->area
.width
);
3679 event
->area
.height
= max (height
, event
->area
.height
);
3684 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
3685 #define toolbar_set_orientation(w, o) \
3686 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
3688 #define toolbar_set_orientation(w, o) \
3689 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
3692 /* Attach a tool bar to frame F. */
3695 xg_pack_tool_bar (FRAME_PTR f
, Lisp_Object pos
)
3697 struct x_output
*x
= f
->output_data
.x
;
3698 int into_hbox
= EQ (pos
, Qleft
) || EQ (pos
, Qright
);
3700 toolbar_set_orientation (x
->toolbar_widget
,
3702 ? GTK_ORIENTATION_VERTICAL
3703 : GTK_ORIENTATION_HORIZONTAL
);
3704 if (!x
->handlebox_widget
)
3706 x
->handlebox_widget
= gtk_handle_box_new ();
3707 g_signal_connect (G_OBJECT (x
->handlebox_widget
), "child-detached",
3708 G_CALLBACK (xg_tool_bar_detach_callback
), f
);
3709 g_signal_connect (G_OBJECT (x
->handlebox_widget
), "child-attached",
3710 G_CALLBACK (xg_tool_bar_attach_callback
), f
);
3711 gtk_container_add (GTK_CONTAINER (x
->handlebox_widget
),
3717 gtk_box_pack_start (GTK_BOX (x
->hbox_widget
), x
->handlebox_widget
,
3720 if (EQ (pos
, Qleft
))
3721 gtk_box_reorder_child (GTK_BOX (x
->hbox_widget
),
3722 x
->handlebox_widget
,
3724 x
->toolbar_in_hbox
= 1;
3728 int vbox_pos
= x
->menubar_widget
? 1 : 0;
3729 gtk_box_pack_start (GTK_BOX (x
->vbox_widget
), x
->handlebox_widget
,
3733 gtk_box_reorder_child (GTK_BOX (x
->vbox_widget
),
3734 x
->handlebox_widget
,
3736 x
->toolbar_in_hbox
= 0;
3740 /* Create a tool bar for frame F. */
3743 xg_create_tool_bar (FRAME_PTR f
)
3745 struct x_output
*x
= f
->output_data
.x
;
3747 x
->toolbar_widget
= gtk_toolbar_new ();
3748 x
->toolbar_detached
= 0;
3750 gtk_widget_set_name (x
->toolbar_widget
, "emacs-toolbar");
3752 gtk_toolbar_set_style (GTK_TOOLBAR (x
->toolbar_widget
), GTK_TOOLBAR_ICONS
);
3753 toolbar_set_orientation (x
->toolbar_widget
, GTK_ORIENTATION_HORIZONTAL
);
3757 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3759 /* Find the right-to-left image named by RTL in the tool bar images for F.
3760 Returns IMAGE if RTL is not found. */
3763 find_rtl_image (FRAME_PTR f
, Lisp_Object image
, Lisp_Object rtl
)
3766 Lisp_Object file
, rtl_name
;
3767 struct gcpro gcpro1
, gcpro2
;
3768 GCPRO2 (file
, rtl_name
);
3770 rtl_name
= Ffile_name_nondirectory (rtl
);
3772 for (i
= 0; i
< f
->n_tool_bar_items
; ++i
)
3774 Lisp_Object rtl_image
= PROP (TOOL_BAR_ITEM_IMAGES
);
3775 if (!NILP (file
= file_for_image (rtl_image
)))
3777 file
= call1 (intern ("file-name-sans-extension"),
3778 Ffile_name_nondirectory (file
));
3779 if (EQ (Fequal (file
, rtl_name
), Qt
))
3790 static GtkToolItem
*
3791 xg_make_tool_item (FRAME_PTR f
,
3793 GtkWidget
**wbutton
,
3797 GtkToolItem
*ti
= gtk_tool_item_new ();
3798 Lisp_Object style
= Ftool_bar_get_system_style ();
3799 int both_horiz
= EQ (style
, Qboth_horiz
);
3800 int text_image
= EQ (style
, Qtext_image_horiz
);
3802 GtkWidget
*vb
= both_horiz
|| text_image
3803 ? gtk_hbox_new (FALSE
, 0) : gtk_vbox_new (FALSE
, 0);
3804 GtkWidget
*wb
= gtk_button_new ();
3805 GtkWidget
*weventbox
= gtk_event_box_new ();
3807 if (wimage
&& ! text_image
)
3808 gtk_box_pack_start (GTK_BOX (vb
), wimage
, TRUE
, TRUE
, 0);
3810 gtk_box_pack_start (GTK_BOX (vb
), gtk_label_new (label
), TRUE
, TRUE
, 0);
3812 if (wimage
&& text_image
)
3813 gtk_box_pack_start (GTK_BOX (vb
), wimage
, TRUE
, TRUE
, 0);
3815 gtk_button_set_focus_on_click (GTK_BUTTON (wb
), FALSE
);
3816 gtk_button_set_relief (GTK_BUTTON (wb
), GTK_RELIEF_NONE
);
3817 gtk_container_add (GTK_CONTAINER (wb
), vb
);
3818 gtk_container_add (GTK_CONTAINER (weventbox
), wb
);
3819 gtk_container_add (GTK_CONTAINER (ti
), weventbox
);
3823 /* The EMACS_INT cast avoids a warning. */
3824 g_signal_connect (G_OBJECT (ti
), "create-menu-proxy",
3825 G_CALLBACK (xg_tool_bar_menu_proxy
),
3826 (gpointer
) (EMACS_INT
) i
);
3828 g_signal_connect (G_OBJECT (wb
), "clicked",
3829 G_CALLBACK (xg_tool_bar_callback
),
3830 (gpointer
) (EMACS_INT
) i
);
3832 g_object_set_data (G_OBJECT (weventbox
), XG_FRAME_DATA
, (gpointer
)f
);
3834 /* Catch expose events to overcome an annoying redraw bug, see
3835 comment for xg_tool_bar_item_expose_callback. */
3836 g_signal_connect (G_OBJECT (ti
),
3838 G_CALLBACK (xg_tool_bar_item_expose_callback
),
3841 gtk_tool_item_set_homogeneous (ti
, FALSE
);
3843 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
3844 no distinction based on modifiers in the activate callback,
3845 so we have to do it ourselves. */
3846 g_signal_connect (wb
, "button-release-event",
3847 G_CALLBACK (xg_tool_bar_button_cb
),
3850 g_object_set_data (G_OBJECT (wb
), XG_FRAME_DATA
, (gpointer
)f
);
3852 /* Use enter/leave notify to show help. We use the events
3853 rather than the GtkButton specific signals "enter" and
3854 "leave", so we can have only one callback. The event
3855 will tell us what kind of event it is. */
3856 /* The EMACS_INT cast avoids a warning. */
3857 g_signal_connect (G_OBJECT (weventbox
),
3858 "enter-notify-event",
3859 G_CALLBACK (xg_tool_bar_help_callback
),
3860 (gpointer
) (EMACS_INT
) i
);
3861 g_signal_connect (G_OBJECT (weventbox
),
3862 "leave-notify-event",
3863 G_CALLBACK (xg_tool_bar_help_callback
),
3864 (gpointer
) (EMACS_INT
) i
);
3867 if (wbutton
) *wbutton
= wb
;
3873 xg_show_toolbar_item (GtkToolItem
*ti
)
3875 Lisp_Object style
= Ftool_bar_get_system_style ();
3876 int both_horiz
= EQ (style
, Qboth_horiz
);
3877 int text_image
= EQ (style
, Qtext_image_horiz
);
3879 int horiz
= both_horiz
|| text_image
;
3880 int show_label
= ! EQ (style
, Qimage
);
3881 int show_image
= ! EQ (style
, Qtext
);
3883 GtkWidget
*weventbox
= gtk_bin_get_child (GTK_BIN (ti
));
3884 GtkWidget
*wbutton
= gtk_bin_get_child (GTK_BIN (weventbox
));
3885 GtkWidget
*vb
= gtk_bin_get_child (GTK_BIN (wbutton
));
3887 GtkWidget
*wlbl
= xg_get_tool_bar_widgets (vb
, &wimage
);
3888 GtkWidget
*new_box
= NULL
;
3890 if (GTK_IS_VBOX (vb
) && horiz
)
3891 new_box
= gtk_hbox_new (FALSE
, 0);
3892 else if (GTK_IS_HBOX (vb
) && !horiz
&& show_label
&& show_image
)
3893 new_box
= gtk_vbox_new (FALSE
, 0);
3895 if (!new_box
&& horiz
)
3896 gtk_box_reorder_child (GTK_BOX (vb
), wlbl
, text_image
? 0 : 1);
3899 g_object_ref (G_OBJECT (wimage
));
3900 g_object_ref (G_OBJECT (wlbl
));
3901 gtk_container_remove (GTK_CONTAINER (vb
), wimage
);
3902 gtk_container_remove (GTK_CONTAINER (vb
), wlbl
);
3903 gtk_widget_destroy (GTK_WIDGET (vb
));
3905 gtk_box_pack_start (GTK_BOX (new_box
), wimage
, TRUE
, TRUE
, 0);
3906 gtk_box_pack_start (GTK_BOX (new_box
), wlbl
, TRUE
, TRUE
, 0);
3908 gtk_box_pack_start (GTK_BOX (new_box
), wimage
, TRUE
, TRUE
, 0);
3909 gtk_container_add (GTK_CONTAINER (wbutton
), new_box
);
3910 g_object_unref (G_OBJECT (wimage
));
3911 g_object_unref (G_OBJECT (wlbl
));
3915 if (show_label
) gtk_widget_show (wlbl
);
3916 else gtk_widget_hide (wlbl
);
3917 if (show_image
) gtk_widget_show (wimage
);
3918 else gtk_widget_hide (wimage
);
3919 gtk_widget_show (GTK_WIDGET (weventbox
));
3920 gtk_widget_show (GTK_WIDGET (vb
));
3921 gtk_widget_show (GTK_WIDGET (wbutton
));
3922 gtk_widget_show (GTK_WIDGET (ti
));
3926 xg_update_tool_bar_sizes (FRAME_PTR f
)
3928 struct x_output
*x
= f
->output_data
.x
;
3930 int nl
= 0, nr
= 0, nt
= 0, nb
= 0;
3932 gtk_widget_size_request (GTK_WIDGET (x
->handlebox_widget
), &req
);
3933 if (x
->toolbar_in_hbox
)
3936 gtk_container_child_get (GTK_CONTAINER (x
->hbox_widget
),
3937 x
->handlebox_widget
,
3938 "position", &pos
, NULL
);
3939 if (pos
== 0) nl
= req
.width
;
3940 else nr
= req
.width
;
3945 gtk_container_child_get (GTK_CONTAINER (x
->vbox_widget
),
3946 x
->handlebox_widget
,
3947 "position", &pos
, NULL
);
3948 if (pos
== 0 || (pos
== 1 && x
->menubar_widget
)) nt
= req
.height
;
3949 else nb
= req
.height
;
3952 if (nl
!= FRAME_TOOLBAR_LEFT_WIDTH (f
)
3953 || nr
!= FRAME_TOOLBAR_RIGHT_WIDTH (f
)
3954 || nt
!= FRAME_TOOLBAR_TOP_HEIGHT (f
)
3955 || nb
!= FRAME_TOOLBAR_BOTTOM_HEIGHT (f
))
3957 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = FRAME_TOOLBAR_LEFT_WIDTH (f
)
3958 = FRAME_TOOLBAR_TOP_HEIGHT (f
) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = 0;
3959 FRAME_TOOLBAR_LEFT_WIDTH (f
) = nl
;
3960 FRAME_TOOLBAR_RIGHT_WIDTH (f
) = nr
;
3961 FRAME_TOOLBAR_TOP_HEIGHT (f
) = nt
;
3962 FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = nb
;
3970 /* Update the tool bar for frame F. Add new buttons and remove old. */
3972 extern Lisp_Object Qx_gtk_map_stock
;
3975 update_frame_tool_bar (FRAME_PTR f
)
3978 struct x_output
*x
= f
->output_data
.x
;
3979 int hmargin
= 0, vmargin
= 0;
3980 GtkToolbar
*wtoolbar
;
3982 GtkTextDirection dir
;
3983 int pack_tool_bar
= x
->handlebox_widget
== NULL
;
3985 if (! FRAME_GTK_WIDGET (f
))
3990 if (INTEGERP (Vtool_bar_button_margin
)
3991 && XINT (Vtool_bar_button_margin
) > 0)
3993 hmargin
= XFASTINT (Vtool_bar_button_margin
);
3994 vmargin
= XFASTINT (Vtool_bar_button_margin
);
3996 else if (CONSP (Vtool_bar_button_margin
))
3998 if (INTEGERP (XCAR (Vtool_bar_button_margin
))
3999 && XINT (XCAR (Vtool_bar_button_margin
)) > 0)
4000 hmargin
= XFASTINT (XCAR (Vtool_bar_button_margin
));
4002 if (INTEGERP (XCDR (Vtool_bar_button_margin
))
4003 && XINT (XCDR (Vtool_bar_button_margin
)) > 0)
4004 vmargin
= XFASTINT (XCDR (Vtool_bar_button_margin
));
4007 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4008 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4009 i.e. zero. This means that margins less than
4010 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4011 hmargin
= max (0, hmargin
- DEFAULT_TOOL_BAR_BUTTON_MARGIN
);
4012 vmargin
= max (0, vmargin
- DEFAULT_TOOL_BAR_BUTTON_MARGIN
);
4014 if (! x
->toolbar_widget
)
4015 xg_create_tool_bar (f
);
4017 wtoolbar
= GTK_TOOLBAR (x
->toolbar_widget
);
4018 dir
= gtk_widget_get_direction (GTK_WIDGET (wtoolbar
));
4020 for (i
= 0; i
< f
->n_tool_bar_items
; ++i
)
4022 int enabled_p
= !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P
));
4023 int selected_p
= !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P
));
4027 struct image
*img
= NULL
;
4029 Lisp_Object stock
= Qnil
;
4030 GtkStockItem stock_item
;
4031 char *stock_name
= NULL
;
4032 char *icon_name
= NULL
;
4034 GtkWidget
*wbutton
= NULL
;
4035 GtkWidget
*weventbox
;
4036 Lisp_Object specified_file
;
4037 char *label
= SSDATA (PROP (TOOL_BAR_ITEM_LABEL
));
4039 ti
= gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar
), i
);
4043 weventbox
= gtk_bin_get_child (GTK_BIN (ti
));
4044 wbutton
= gtk_bin_get_child (GTK_BIN (weventbox
));
4048 image
= PROP (TOOL_BAR_ITEM_IMAGES
);
4050 /* Ignore invalid image specifications. */
4051 if (!valid_image_p (image
))
4053 if (wbutton
) gtk_widget_hide (wbutton
);
4057 specified_file
= file_for_image (image
);
4058 if (!NILP (specified_file
) && !NILP (Ffboundp (Qx_gtk_map_stock
)))
4059 stock
= call1 (Qx_gtk_map_stock
, specified_file
);
4061 if (STRINGP (stock
))
4063 stock_name
= SSDATA (stock
);
4064 if (stock_name
[0] == 'n' && stock_name
[1] == ':')
4066 GdkScreen
*screen
= gtk_widget_get_screen (GTK_WIDGET (wtoolbar
));
4067 GtkIconTheme
*icon_theme
= gtk_icon_theme_get_for_screen (screen
);
4069 icon_name
= stock_name
+ 2;
4073 if (! gtk_icon_theme_has_icon (icon_theme
, icon_name
))
4076 icon_size
= gtk_toolbar_get_icon_size (wtoolbar
);
4078 else if (gtk_stock_lookup (SSDATA (stock
), &stock_item
))
4079 icon_size
= gtk_toolbar_get_icon_size (wtoolbar
);
4087 if (stock_name
== NULL
&& icon_name
== NULL
)
4089 /* No stock image, or stock item not known. Try regular image. */
4091 /* If image is a vector, choose the image according to the
4093 if (dir
== GTK_TEXT_DIR_RTL
4094 && !NILP (rtl
= PROP (TOOL_BAR_ITEM_RTL_IMAGE
))
4097 image
= find_rtl_image (f
, image
, rtl
);
4100 if (VECTORP (image
))
4104 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4105 : TOOL_BAR_IMAGE_ENABLED_DESELECTED
);
4108 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4109 : TOOL_BAR_IMAGE_DISABLED_DESELECTED
);
4111 xassert (ASIZE (image
) >= idx
);
4112 image
= AREF (image
, idx
);
4117 img_id
= lookup_image (f
, image
);
4118 img
= IMAGE_FROM_ID (f
, img_id
);
4119 prepare_image_for_display (f
, img
);
4121 if (img
->load_failed_p
|| img
->pixmap
== None
)
4124 gtk_widget_hide_all (GTK_WIDGET (ti
));
4127 /* Insert an empty (non-image) button */
4128 ti
= xg_make_tool_item (f
, NULL
, NULL
, "", i
);
4129 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar
), ti
, -1);
4140 w
= gtk_image_new_from_stock (stock_name
, icon_size
);
4141 g_object_set_data_full (G_OBJECT (w
), XG_TOOL_BAR_STOCK_NAME
,
4142 (gpointer
) xstrdup (stock_name
),
4143 (GDestroyNotify
) xfree
);
4147 w
= gtk_image_new_from_icon_name (icon_name
, icon_size
);
4148 g_object_set_data_full (G_OBJECT (w
), XG_TOOL_BAR_ICON_NAME
,
4149 (gpointer
) xstrdup (icon_name
),
4150 (GDestroyNotify
) xfree
);
4154 w
= xg_get_image_for_pixmap (f
, img
, x
->widget
, NULL
);
4155 /* Save the image so we can see if an update is needed when
4156 this function is called again. */
4157 g_object_set_data (G_OBJECT (w
), XG_TOOL_BAR_IMAGE_DATA
,
4158 (gpointer
)img
->pixmap
);
4161 gtk_misc_set_padding (GTK_MISC (w
), hmargin
, vmargin
);
4162 ti
= xg_make_tool_item (f
, w
, &wbutton
, label
, i
);
4163 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar
), ti
, -1);
4164 gtk_widget_set_sensitive (wbutton
, enabled_p
);
4168 GtkWidget
*vb
= gtk_bin_get_child (GTK_BIN (wbutton
));
4170 GtkWidget
*wlbl
= xg_get_tool_bar_widgets (vb
, &wimage
);
4172 Pixmap old_img
= (Pixmap
)g_object_get_data (G_OBJECT (wimage
),
4173 XG_TOOL_BAR_IMAGE_DATA
);
4174 gpointer old_stock_name
= g_object_get_data (G_OBJECT (wimage
),
4175 XG_TOOL_BAR_STOCK_NAME
);
4176 gpointer old_icon_name
= g_object_get_data (G_OBJECT (wimage
),
4177 XG_TOOL_BAR_ICON_NAME
);
4178 gtk_label_set_text (GTK_LABEL (wlbl
), label
);
4180 (! old_stock_name
|| strcmp (old_stock_name
, stock_name
) != 0))
4182 gtk_image_set_from_stock (GTK_IMAGE (wimage
),
4183 stock_name
, icon_size
);
4184 g_object_set_data_full (G_OBJECT (wimage
), XG_TOOL_BAR_STOCK_NAME
,
4185 (gpointer
) xstrdup (stock_name
),
4186 (GDestroyNotify
) xfree
);
4187 g_object_set_data (G_OBJECT (wimage
), XG_TOOL_BAR_IMAGE_DATA
,
4189 g_object_set_data (G_OBJECT (wimage
), XG_TOOL_BAR_ICON_NAME
,
4192 else if (icon_name
&&
4193 (! old_icon_name
|| strcmp (old_icon_name
, icon_name
) != 0))
4195 gtk_image_set_from_icon_name (GTK_IMAGE (wimage
),
4196 icon_name
, icon_size
);
4197 g_object_set_data_full (G_OBJECT (wimage
), XG_TOOL_BAR_ICON_NAME
,
4198 (gpointer
) xstrdup (icon_name
),
4199 (GDestroyNotify
) xfree
);
4200 g_object_set_data (G_OBJECT (wimage
), XG_TOOL_BAR_IMAGE_DATA
,
4202 g_object_set_data (G_OBJECT (wimage
), XG_TOOL_BAR_STOCK_NAME
,
4205 else if (img
&& old_img
!= img
->pixmap
)
4207 (void) xg_get_image_for_pixmap (f
, img
, x
->widget
,
4208 GTK_IMAGE (wimage
));
4209 g_object_set_data (G_OBJECT (wimage
), XG_TOOL_BAR_IMAGE_DATA
,
4210 (gpointer
)img
->pixmap
);
4212 g_object_set_data (G_OBJECT (wimage
), XG_TOOL_BAR_STOCK_NAME
,
4214 g_object_set_data (G_OBJECT (wimage
), XG_TOOL_BAR_ICON_NAME
,
4218 gtk_misc_set_padding (GTK_MISC (wimage
), hmargin
, vmargin
);
4220 gtk_widget_set_sensitive (wbutton
, enabled_p
);
4222 xg_show_toolbar_item (ti
);
4227 /* Remove buttons not longer needed. We just hide them so they
4228 can be reused later on. */
4231 ti
= gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar
), i
++);
4232 if (ti
) gtk_widget_hide_all (GTK_WIDGET (ti
));
4233 } while (ti
!= NULL
);
4235 if (f
->n_tool_bar_items
!= 0)
4238 xg_pack_tool_bar (f
, f
->tool_bar_position
);
4239 gtk_widget_show (x
->toolbar_widget
);
4240 gtk_widget_show (x
->handlebox_widget
);
4241 if (xg_update_tool_bar_sizes (f
))
4242 xg_height_or_width_changed (f
);
4248 /* Deallocate all resources for the tool bar on frame F.
4249 Remove the tool bar. */
4252 free_frame_tool_bar (FRAME_PTR f
)
4254 struct x_output
*x
= f
->output_data
.x
;
4256 if (x
->toolbar_widget
)
4258 int is_packed
= x
->handlebox_widget
!= 0;
4260 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4261 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4264 if (x
->toolbar_in_hbox
)
4265 gtk_container_remove (GTK_CONTAINER (x
->hbox_widget
),
4266 x
->handlebox_widget
);
4268 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
),
4269 x
->handlebox_widget
);
4272 gtk_widget_destroy (x
->toolbar_widget
);
4274 x
->toolbar_widget
= 0;
4275 x
->handlebox_widget
= 0;
4276 FRAME_TOOLBAR_TOP_HEIGHT (f
) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f
) = 0;
4277 FRAME_TOOLBAR_LEFT_WIDTH (f
) = FRAME_TOOLBAR_RIGHT_WIDTH (f
) = 0;
4279 xg_height_or_width_changed (f
);
4286 xg_change_toolbar_position (FRAME_PTR f
, Lisp_Object pos
)
4288 struct x_output
*x
= f
->output_data
.x
;
4290 if (! x
->toolbar_widget
|| ! x
->handlebox_widget
)
4294 g_object_ref (x
->handlebox_widget
);
4295 if (x
->toolbar_in_hbox
)
4296 gtk_container_remove (GTK_CONTAINER (x
->hbox_widget
),
4297 x
->handlebox_widget
);
4299 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
),
4300 x
->handlebox_widget
);
4301 xg_pack_tool_bar (f
, pos
);
4302 g_object_unref (x
->handlebox_widget
);
4303 if (xg_update_tool_bar_sizes (f
))
4304 xg_height_or_width_changed (f
);
4312 /***********************************************************************
4314 ***********************************************************************/
4316 xg_initialize (void)
4318 GtkBindingSet
*binding_set
;
4321 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4322 we keep it permanently linked in. */
4327 xg_ignore_gtk_scrollbar
= 0;
4328 xg_detached_menus
= 0;
4329 xg_menu_cb_list
.prev
= xg_menu_cb_list
.next
=
4330 xg_menu_item_cb_list
.prev
= xg_menu_item_cb_list
.next
= 0;
4332 id_to_widget
.max_size
= id_to_widget
.used
= 0;
4333 id_to_widget
.widgets
= 0;
4335 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4336 bindings. It doesn't seem to be any way to remove properties,
4337 so we set it to VoidSymbol which in X means "no key". */
4338 gtk_settings_set_string_property (gtk_settings_get_default (),
4339 "gtk-menu-bar-accel",
4343 /* Make GTK text input widgets use Emacs style keybindings. This is
4345 gtk_settings_set_string_property (gtk_settings_get_default (),
4346 "gtk-key-theme-name",
4350 /* Make dialogs close on C-g. Since file dialog inherits from
4351 dialog, this works for them also. */
4352 binding_set
= gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG
));
4353 gtk_binding_entry_add_signal (binding_set
, GDK_g
, GDK_CONTROL_MASK
,
4356 /* Make menus close on C-g. */
4357 binding_set
= gtk_binding_set_by_class (g_type_class_ref
4358 (GTK_TYPE_MENU_SHELL
));
4359 gtk_binding_entry_add_signal (binding_set
, GDK_g
, GDK_CONTROL_MASK
,
4363 #endif /* USE_GTK */
4365 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
4366 (do not change this comment) */