1 /* Functions for creating and updating GTK widgets.
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 2, or (at your option)
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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
29 #include "blockinput.h"
33 #include "termhooks.h"
37 #include <gdk/gdkkeysyms.h>
40 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
41 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
44 /***********************************************************************
45 Display handling functions
46 ***********************************************************************/
48 #ifdef HAVE_GTK_MULTIDISPLAY
50 /* Return the GdkDisplay that corresponds to the X display DPY. */
52 xg_get_gdk_display (dpy
)
55 return gdk_x11_lookup_xdisplay (dpy
);
58 /* When the GTK widget W is to be created on a display for F that
59 is not the default display, set the display for W.
60 W can be a GtkMenu or a GtkWindow widget. */
66 if (FRAME_X_DISPLAY (f
) != GDK_DISPLAY ())
68 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
69 GdkScreen
*gscreen
= gdk_display_get_default_screen (gdpy
);
72 gtk_menu_set_screen (GTK_MENU (w
), gscreen
);
74 gtk_window_set_screen (GTK_WINDOW (w
), gscreen
);
79 #else /* not HAVE_GTK_MULTIDISPLAY */
81 /* Make some defines so we can use the GTK 2.2 functions when
82 compiling with GTK 2.0. */
83 #define xg_set_screen(w, f)
84 #define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
85 #define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
86 #define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
87 #define gdk_x11_lookup_xdisplay(dpy) 0
88 #define GdkDisplay void
90 #endif /* not HAVE_GTK_MULTIDISPLAY */
92 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
93 *DPY is set to NULL if the display can't be opened.
95 Returns non-zero if display could be opened, zero if display could not
96 be opened, and less than zero if the GTK version doesn't support
99 xg_display_open (display_name
, dpy
)
103 #ifdef HAVE_GTK_MULTIDISPLAY
106 gdpy
= gdk_display_open (display_name
);
107 *dpy
= gdpy
? GDK_DISPLAY_XDISPLAY (gdpy
) : NULL
;
111 #else /* not HAVE_GTK_MULTIDISPLAY */
114 #endif /* not HAVE_GTK_MULTIDISPLAY */
119 xg_display_close (Display
*dpy
)
121 #ifdef HAVE_GTK_MULTIDISPLAY
122 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (dpy
);
124 /* GTK 2.2 has a bug that makes gdk_display_close crash (bug
125 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way
126 we can continue running, but there will be memory leaks. */
128 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4
130 /* If this is the default display, we must change it before calling
131 dispose, otherwise it will crash. */
132 if (gdk_display_get_default () == gdpy
)
134 struct x_display_info
*dpyinfo
;
135 Display
*new_dpy
= 0;
136 GdkDisplay
*gdpy_new
;
138 /* Find another display. */
139 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
140 if (dpyinfo
->display
!= dpy
)
142 new_dpy
= dpyinfo
->display
;
146 if (! new_dpy
) return; /* Emacs will exit anyway. */
148 gdpy_new
= gdk_x11_lookup_xdisplay (new_dpy
);
149 gdk_display_manager_set_default_display (gdk_display_manager_get (),
153 g_object_run_dispose (G_OBJECT (gdpy
));
156 /* I hope this will be fixed in GTK 2.4. It is what bug 85715 says. */
157 gdk_display_close (gdpy
);
159 #endif /* HAVE_GTK_MULTIDISPLAY */
163 /***********************************************************************
165 ***********************************************************************/
166 /* The timer for scroll bar repetition and menu bar timeouts.
167 NULL if no timer is started. */
168 static struct atimer
*xg_timer
;
171 /* The next two variables and functions are taken from lwlib. */
172 static widget_value
*widget_value_free_list
;
173 static int malloc_cpt
;
175 /* Allocate a widget_value structure, either by taking one from the
176 widget_value_free_list or by malloc:ing a new one.
178 Return a pointer to the allocated structure. */
180 malloc_widget_value ()
183 if (widget_value_free_list
)
185 wv
= widget_value_free_list
;
186 widget_value_free_list
= wv
->free_list
;
191 wv
= (widget_value
*) malloc (sizeof (widget_value
));
194 memset (wv
, 0, sizeof (widget_value
));
198 /* This is analogous to free. It frees only what was allocated
199 by malloc_widget_value, and no substructures. */
201 free_widget_value (wv
)
209 /* When the number of already allocated cells is too big,
216 wv
->free_list
= widget_value_free_list
;
217 widget_value_free_list
= wv
;
222 /* Create and return the cursor to be used for popup menus and
223 scroll bars on display DPY. */
225 xg_create_default_cursor (dpy
)
228 GdkDisplay
*gdpy
= gdk_x11_lookup_xdisplay (dpy
);
229 return gdk_cursor_new_for_display (gdpy
, GDK_LEFT_PTR
);
232 /* For the image defined in IMG, make and return a GtkImage. For displays with
233 8 planes or less we must make a GdkPixbuf and apply the mask manually.
234 Otherwise the highlightning and dimming the tool bar code in GTK does
235 will look bad. For display with more than 8 planes we just use the
236 pixmap and mask directly. For monochrome displays, GTK doesn't seem
237 able to use external pixmaps, it looks bad whatever we do.
238 The image is defined on the display where frame F is.
239 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
240 If OLD_WIDGET is NULL, a new widget is constructed and returned.
241 If OLD_WIDGET is not NULL, that widget is modified. */
243 xg_get_image_for_pixmap (f
, img
, widget
, old_widget
)
247 GtkImage
*old_widget
;
253 /* If we are on a one bit display, let GTK do all the image handling.
254 This seems to be the only way to make insensitive and activated icons
256 if (x_screen_planes (f
) == 1)
258 Lisp_Object specified_file
= Qnil
;
260 extern Lisp_Object QCfile
;
262 for (tail
= XCDR (img
->spec
);
263 NILP (specified_file
) && CONSP (tail
) && CONSP (XCDR (tail
));
264 tail
= XCDR (XCDR (tail
)))
265 if (EQ (XCAR (tail
), QCfile
))
266 specified_file
= XCAR (XCDR (tail
));
268 if (STRINGP (specified_file
))
271 Lisp_Object file
= Qnil
;
275 file
= x_find_image_file (specified_file
);
276 /* We already loaded the image once before calling this
277 function, so this should not fail. */
278 xassert (STRINGP (file
) != 0);
281 old_widget
= GTK_IMAGE (gtk_image_new_from_file (SDATA (file
)));
283 gtk_image_set_from_file (old_widget
, SDATA (file
));
286 return GTK_WIDGET (old_widget
);
290 gdpy
= gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f
));
291 gpix
= gdk_pixmap_foreign_new_for_display (gdpy
, img
->pixmap
);
292 gmask
= img
->mask
? gdk_pixmap_foreign_new_for_display (gdpy
, img
->mask
) : 0;
294 if (x_screen_planes (f
) > 8 || x_screen_planes (f
) == 1)
297 old_widget
= GTK_IMAGE (gtk_image_new_from_pixmap (gpix
, gmask
));
299 gtk_image_set_from_pixmap (old_widget
, gpix
, gmask
);
303 /* This is a workaround to make icons look good on pseudo color
304 displays. Apparently GTK expects the images to have an alpha
305 channel. If they don't, insensitive and activated icons will
306 look bad. This workaround does not work on monochrome displays,
307 and is not needed on true color/static color displays (i.e.
308 16 bits and higher). */
309 int x
, y
, width
, height
, rowstride
, mask_rowstride
;
310 GdkPixbuf
*icon_buf
, *tmp_buf
;
314 gdk_drawable_get_size (gpix
, &width
, &height
);
315 tmp_buf
= gdk_pixbuf_get_from_drawable (NULL
,
317 gtk_widget_get_colormap (widget
),
318 0, 0, 0, 0, width
, height
);
319 icon_buf
= gdk_pixbuf_add_alpha (tmp_buf
, FALSE
, 0, 0, 0);
320 g_object_unref (G_OBJECT (tmp_buf
));
324 GdkPixbuf
*mask_buf
= gdk_pixbuf_get_from_drawable (NULL
,
329 guchar
*pixels
= gdk_pixbuf_get_pixels (icon_buf
);
330 guchar
*mask_pixels
= gdk_pixbuf_get_pixels (mask_buf
);
331 int rowstride
= gdk_pixbuf_get_rowstride (icon_buf
);
332 int mask_rowstride
= gdk_pixbuf_get_rowstride (mask_buf
);
335 for (y
= 0; y
< height
; ++y
)
337 guchar
*iconptr
, *maskptr
;
340 iconptr
= pixels
+ y
* rowstride
;
341 maskptr
= mask_pixels
+ y
* mask_rowstride
;
343 for (x
= 0; x
< width
; ++x
)
345 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
346 just R is sufficient. */
348 iconptr
[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
350 iconptr
+= rowstride
/width
;
351 maskptr
+= mask_rowstride
/width
;
355 g_object_unref (G_OBJECT (mask_buf
));
359 old_widget
= GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf
));
361 gtk_image_set_from_pixbuf (old_widget
, icon_buf
);
363 g_object_unref (G_OBJECT (icon_buf
));
366 g_object_unref (G_OBJECT (gpix
));
367 if (gmask
) g_object_unref (G_OBJECT (gmask
));
369 return GTK_WIDGET (old_widget
);
373 /* Set CURSOR on W and all widgets W contain. We must do like this
374 for scroll bars and menu because they create widgets internally,
375 and it is those widgets that are visible. */
377 xg_set_cursor (w
, cursor
)
381 GList
*children
= gdk_window_peek_children (w
->window
);
383 gdk_window_set_cursor (w
->window
, cursor
);
385 /* The scroll bar widget has more than one GDK window (had to look at
386 the source to figure this out), and there is no way to set cursor
387 on widgets in GTK. So we must set the cursor for all GDK windows.
390 for ( ; children
; children
= g_list_next (children
))
391 gdk_window_set_cursor (GDK_WINDOW (children
->data
), cursor
);
394 /* Timer function called when a timeout occurs for xg_timer.
395 This function processes all GTK events in a recursive event loop.
396 This is done because GTK timer events are not seen by Emacs event
397 detection, Emacs only looks for X events. When a scroll bar has the
398 pointer (detected by button press/release events below) an Emacs
399 timer is started, and this function can then check if the GTK timer
400 has expired by calling the GTK event loop.
401 Also, when a menu is active, it has a small timeout before it
402 pops down the sub menu under it. */
404 xg_process_timeouts (timer
)
405 struct atimer
*timer
;
408 /* Ideally we would like to just handle timer events, like the Xt version
409 of this does in xterm.c, but there is no such feature in GTK. */
410 while (gtk_events_pending ())
411 gtk_main_iteration ();
415 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
416 xg_process_timeouts is called when the timer expires. The timer
417 started is continuous, i.e. runs until xg_stop_timer is called. */
424 EMACS_SET_SECS_USECS (interval
, 0, 100000);
425 xg_timer
= start_atimer (ATIMER_CONTINUOUS
,
432 /* Stop the xg_timer if started. */
438 cancel_atimer (xg_timer
);
443 /* Insert NODE into linked LIST. */
445 xg_list_insert (xg_list_node
*list
, xg_list_node
*node
)
447 xg_list_node
*list_start
= list
->next
;
449 if (list_start
) list_start
->prev
= node
;
450 node
->next
= list_start
;
455 /* Remove NODE from linked LIST. */
457 xg_list_remove (xg_list_node
*list
, xg_list_node
*node
)
459 xg_list_node
*list_start
= list
->next
;
460 if (node
== list_start
)
462 list
->next
= node
->next
;
463 if (list
->next
) list
->next
->prev
= 0;
467 node
->prev
->next
= node
->next
;
468 if (node
->next
) node
->next
->prev
= node
->prev
;
472 /* Allocate and return a utf8 version of STR. If STR is already
473 utf8 or NULL, just return STR.
474 If not, a new string is allocated and the caller must free the result
477 get_utf8_string (str
)
480 char *utf8_str
= str
;
482 /* If not UTF-8, try current locale. */
483 if (str
&& !g_utf8_validate (str
, -1, NULL
))
484 utf8_str
= g_locale_to_utf8 (str
, -1, 0, 0, 0);
491 /***********************************************************************
492 General functions for creating widgets, resizing, events, e.t.c.
493 ***********************************************************************/
495 /* Make a geometry string and pass that to GTK. It seems this is the
496 only way to get geometry position right if the user explicitly
497 asked for a position when starting Emacs.
498 F is the frame we shall set geometry for. */
503 if (f
->size_hint_flags
& USPosition
)
505 int left
= f
->left_pos
;
506 int xneg
= f
->size_hint_flags
& XNegative
;
507 int top
= f
->top_pos
;
508 int yneg
= f
->size_hint_flags
& YNegative
;
516 sprintf (geom_str
, "=%dx%d%c%d%c%d",
517 FRAME_PIXEL_WIDTH (f
),
518 FRAME_TOTAL_PIXEL_HEIGHT (f
),
519 (xneg
? '-' : '+'), left
,
520 (yneg
? '-' : '+'), top
);
522 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
524 fprintf (stderr
, "Failed to parse: '%s'\n", geom_str
);
529 /* Resize the outer window of frame F after chainging the height.
530 This happend when the menu bar or the tool bar is added or removed.
531 COLUMNS/ROWS is the size the edit area shall have after the resize. */
533 xg_resize_outer_widget (f
, columns
, rows
)
538 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
539 FRAME_PIXEL_WIDTH (f
), FRAME_TOTAL_PIXEL_HEIGHT (f
));
541 /* base_height is now changed. */
542 x_wm_set_size_hint (f
, 0, 0);
544 /* If we are not mapped yet, set geometry once again, as window
545 height now have changed. */
546 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f
)))
549 xg_frame_set_char_size (f
, columns
, rows
);
550 gdk_window_process_all_updates ();
553 /* Function to handle resize of our widgets. Since Emacs has some layouts
554 that does not fit well with GTK standard containers, we do most layout
556 F is the frame to resize.
557 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
559 xg_resize_widgets (f
, pixelwidth
, pixelheight
)
561 int pixelwidth
, pixelheight
;
563 int mbheight
= FRAME_MENUBAR_HEIGHT (f
);
564 int tbheight
= FRAME_TOOLBAR_HEIGHT (f
);
565 int rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f
, (pixelheight
566 - mbheight
- tbheight
));
567 int columns
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f
, pixelwidth
);
569 if (FRAME_GTK_WIDGET (f
)
570 && (columns
!= FRAME_COLS (f
)
571 || rows
!= FRAME_LINES (f
)
572 || pixelwidth
!= FRAME_PIXEL_WIDTH (f
)
573 || pixelheight
!= FRAME_PIXEL_HEIGHT (f
)))
575 struct x_output
*x
= f
->output_data
.x
;
578 all
.y
= mbheight
+ tbheight
;
581 all
.width
= pixelwidth
;
582 all
.height
= pixelheight
- mbheight
- tbheight
;
584 gtk_widget_size_allocate (x
->edit_widget
, &all
);
586 change_frame_size (f
, rows
, columns
, 0, 1, 0);
587 SET_FRAME_GARBAGED (f
);
588 cancel_mouse_face (f
);
593 /* Update our widget size to be COLS/ROWS characters for frame F. */
595 xg_frame_set_char_size (f
, cols
, rows
)
600 int pixelheight
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f
, rows
)
601 + FRAME_MENUBAR_HEIGHT (f
) + FRAME_TOOLBAR_HEIGHT (f
);
604 /* Take into account the size of the scroll bar. Always use the
605 number of columns occupied by the scroll bar here otherwise we
606 might end up with a frame width that is not a multiple of the
607 frame's character width which is bad for vertically split
609 f
->scroll_bar_actual_width
610 = FRAME_SCROLL_BAR_COLS (f
) * FRAME_COLUMN_WIDTH (f
);
612 compute_fringe_widths (f
, 0);
614 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
615 after calculating that value. */
616 pixelwidth
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f
, cols
);
618 /* Must resize our top level widget. Font size may have changed,
619 but not rows/cols. */
620 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
621 pixelwidth
, pixelheight
);
622 xg_resize_widgets (f
, pixelwidth
, pixelheight
);
623 x_wm_set_size_hint (f
, 0, 0);
624 SET_FRAME_GARBAGED (f
);
625 cancel_mouse_face (f
);
628 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
629 Must be done like this, because GtkWidget:s can have "hidden"
630 X Window that aren't accessible.
632 Return 0 if no widget match WDESC. */
634 xg_win_to_widget (dpy
, wdesc
)
639 GtkWidget
*gwdesc
= 0;
643 gdkwin
= gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy
),
648 event
.any
.window
= gdkwin
;
649 gwdesc
= gtk_get_event_widget (&event
);
656 /* Fill in the GdkColor C so that it represents PIXEL.
657 W is the widget that color will be used for. Used to find colormap. */
659 xg_pix_to_gcolor (w
, pixel
, c
)
664 GdkColormap
*map
= gtk_widget_get_colormap (w
);
665 gdk_colormap_query_color (map
, pixel
, c
);
668 /* Create and set up the GTK widgets for frame F.
669 Return 0 if creation failed, non-zero otherwise. */
671 xg_create_frame_widgets (f
)
684 wtop
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
685 xg_set_screen (wtop
, f
);
687 wvbox
= gtk_vbox_new (FALSE
, 0);
688 wfixed
= gtk_fixed_new (); /* Must have this to place scroll bars */
690 if (! wtop
|| ! wvbox
|| ! wfixed
)
692 if (wtop
) gtk_widget_destroy (wtop
);
693 if (wvbox
) gtk_widget_destroy (wvbox
);
694 if (wfixed
) gtk_widget_destroy (wfixed
);
699 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
700 gtk_widget_set_name (wtop
, EMACS_CLASS
);
701 gtk_widget_set_name (wvbox
, "pane");
702 gtk_widget_set_name (wfixed
, SDATA (Vx_resource_name
));
704 /* If this frame has a title or name, set it in the title bar. */
705 if (! NILP (f
->title
)) title
= SDATA (ENCODE_UTF_8 (f
->title
));
706 else if (! NILP (f
->name
)) title
= SDATA (ENCODE_UTF_8 (f
->name
));
708 if (title
) gtk_window_set_title (GTK_WINDOW (wtop
), title
);
710 FRAME_GTK_OUTER_WIDGET (f
) = wtop
;
711 FRAME_GTK_WIDGET (f
) = wfixed
;
712 f
->output_data
.x
->vbox_widget
= wvbox
;
714 gtk_fixed_set_has_window (GTK_FIXED (wfixed
), TRUE
);
716 gtk_widget_set_size_request (wfixed
, FRAME_PIXEL_WIDTH (f
),
717 FRAME_PIXEL_HEIGHT (f
));
719 gtk_container_add (GTK_CONTAINER (wtop
), wvbox
);
720 gtk_box_pack_end (GTK_BOX (wvbox
), wfixed
, TRUE
, TRUE
, 0);
722 if (FRAME_EXTERNAL_TOOL_BAR (f
))
723 update_frame_tool_bar (f
);
725 /* The tool bar is created but first there are no items in it.
726 This causes it to be zero height. Later items are added, but then
727 the frame is already mapped, so there is a "jumping" resize.
728 This makes geometry handling difficult, for example -0-0 will end
729 up in the wrong place as tool bar height has not been taken into account.
730 So we cheat a bit by setting a height that is what it will have
731 later on when tool bar items are added. */
732 if (FRAME_EXTERNAL_TOOL_BAR (f
) && f
->n_tool_bar_items
== 0)
733 FRAME_TOOLBAR_HEIGHT (f
) = 34;
736 /* We don't want this widget double buffered, because we draw on it
737 with regular X drawing primitives, so from a GTK/GDK point of
738 view, the widget is totally blank. When an expose comes, this
739 will make the widget blank, and then Emacs redraws it. This flickers
740 a lot, so we turn off double buffering. */
741 gtk_widget_set_double_buffered (wfixed
, FALSE
);
743 /* GTK documents says use gtk_window_set_resizable. But then a user
744 can't shrink the window from its starting size. */
745 gtk_window_set_policy (GTK_WINDOW (wtop
), TRUE
, TRUE
, TRUE
);
746 gtk_window_set_wmclass (GTK_WINDOW (wtop
),
747 SDATA (Vx_resource_name
),
748 SDATA (Vx_resource_class
));
750 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
751 GTK is to destroy the widget. We want Emacs to do that instead. */
752 g_signal_connect (G_OBJECT (wtop
), "delete-event",
753 G_CALLBACK (gtk_true
), 0);
755 /* Convert our geometry parameters into a geometry string
757 GTK will itself handle calculating the real position this way. */
760 gtk_widget_add_events (wfixed
,
761 GDK_POINTER_MOTION_MASK
763 | GDK_BUTTON_PRESS_MASK
764 | GDK_BUTTON_RELEASE_MASK
766 | GDK_ENTER_NOTIFY_MASK
767 | GDK_LEAVE_NOTIFY_MASK
768 | GDK_FOCUS_CHANGE_MASK
770 | GDK_VISIBILITY_NOTIFY_MASK
);
772 /* Must realize the windows so the X window gets created. It is used
773 by callers of this function. */
774 gtk_widget_realize (wfixed
);
775 FRAME_X_WINDOW (f
) = GTK_WIDGET_TO_X_WIN (wfixed
);
777 /* Since GTK clears its window by filling with the background color,
778 we must keep X and GTK background in sync. */
779 xg_pix_to_gcolor (wfixed
, f
->output_data
.x
->background_pixel
, &bg
);
780 gtk_widget_modify_bg (wfixed
, GTK_STATE_NORMAL
, &bg
);
782 /* Also, do not let any background pixmap to be set, this looks very
783 bad as Emacs overwrites the background pixmap with its own idea
784 of background color. */
785 style
= gtk_widget_get_modifier_style (wfixed
);
787 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
788 style
->bg_pixmap_name
[GTK_STATE_NORMAL
] = g_strdup ("<none>");
789 gtk_widget_modify_style (wfixed
, style
);
791 /* GTK does not set any border, and they look bad with GTK. */
793 f
->internal_border_width
= 0;
800 /* Set the normal size hints for the window manager, for frame F.
801 FLAGS is the flags word to use--or 0 meaning preserve the flags
802 that the window now has.
803 If USER_POSITION is nonzero, we set the User Position
804 flag (this is useful when FLAGS is 0). */
806 x_wm_set_size_hint (f
, flags
, user_position
)
811 if (FRAME_GTK_OUTER_WIDGET (f
))
813 /* Must use GTK routines here, otherwise GTK resets the size hints
814 to its own defaults. */
815 GdkGeometry size_hints
;
817 int base_width
, base_height
;
818 int min_rows
= 0, min_cols
= 0;
819 int win_gravity
= f
->win_gravity
;
823 memset (&size_hints
, 0, sizeof (size_hints
));
824 f
->output_data
.x
->size_hints
= size_hints
;
825 f
->output_data
.x
->hint_flags
= hint_flags
;
828 flags
= f
->size_hint_flags
;
830 size_hints
= f
->output_data
.x
->size_hints
;
831 hint_flags
= f
->output_data
.x
->hint_flags
;
833 hint_flags
|= GDK_HINT_RESIZE_INC
| GDK_HINT_MIN_SIZE
;
834 size_hints
.width_inc
= FRAME_COLUMN_WIDTH (f
);
835 size_hints
.height_inc
= FRAME_LINE_HEIGHT (f
);
837 hint_flags
|= GDK_HINT_BASE_SIZE
;
838 base_width
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f
, 0);
839 base_height
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f
, 0)
840 + FRAME_MENUBAR_HEIGHT (f
) + FRAME_TOOLBAR_HEIGHT (f
);
842 check_frame_size (f
, &min_rows
, &min_cols
);
844 size_hints
.base_width
= base_width
;
845 size_hints
.base_height
= base_height
;
846 size_hints
.min_width
= base_width
+ min_cols
* size_hints
.width_inc
;
847 size_hints
.min_height
= base_height
+ min_rows
* size_hints
.height_inc
;
850 /* These currently have a one to one mapping with the X values, but I
851 don't think we should rely on that. */
852 hint_flags
|= GDK_HINT_WIN_GRAVITY
;
853 size_hints
.win_gravity
= 0;
854 if (win_gravity
== NorthWestGravity
)
855 size_hints
.win_gravity
= GDK_GRAVITY_NORTH_WEST
;
856 else if (win_gravity
== NorthGravity
)
857 size_hints
.win_gravity
= GDK_GRAVITY_NORTH
;
858 else if (win_gravity
== NorthEastGravity
)
859 size_hints
.win_gravity
= GDK_GRAVITY_NORTH_EAST
;
860 else if (win_gravity
== WestGravity
)
861 size_hints
.win_gravity
= GDK_GRAVITY_WEST
;
862 else if (win_gravity
== CenterGravity
)
863 size_hints
.win_gravity
= GDK_GRAVITY_CENTER
;
864 else if (win_gravity
== EastGravity
)
865 size_hints
.win_gravity
= GDK_GRAVITY_EAST
;
866 else if (win_gravity
== SouthWestGravity
)
867 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH_WEST
;
868 else if (win_gravity
== SouthGravity
)
869 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH
;
870 else if (win_gravity
== SouthEastGravity
)
871 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH_EAST
;
872 else if (win_gravity
== StaticGravity
)
873 size_hints
.win_gravity
= GDK_GRAVITY_STATIC
;
875 if (flags
& PPosition
) hint_flags
|= GDK_HINT_POS
;
876 if (flags
& USPosition
) hint_flags
|= GDK_HINT_USER_POS
;
877 if (flags
& USSize
) hint_flags
|= GDK_HINT_USER_SIZE
;
881 hint_flags
&= ~GDK_HINT_POS
;
882 hint_flags
|= GDK_HINT_USER_POS
;
887 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
888 FRAME_GTK_OUTER_WIDGET (f
),
892 f
->output_data
.x
->size_hints
= size_hints
;
893 f
->output_data
.x
->hint_flags
= hint_flags
;
898 /* Change background color of a frame.
899 Since GTK uses the background colour to clear the window, we must
900 keep the GTK and X colors in sync.
901 F is the frame to change,
902 BG is the pixel value to change to. */
904 xg_set_background_color (f
, bg
)
908 if (FRAME_GTK_WIDGET (f
))
913 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f
), bg
, &gdk_bg
);
914 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f
), GTK_STATE_NORMAL
, &gdk_bg
);
921 /***********************************************************************
923 ***********************************************************************/
924 /* Return the dialog title to use for a dialog of type KEY.
925 This is the encoding used by lwlib. We use the same for GTK. */
927 get_dialog_title (char key
)
937 title
= "Information";
956 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
957 the dialog, but return TRUE so the event does not propagate further
958 in GTK. This prevents GTK from destroying the dialog widget automatically
959 and we can always destrou the widget manually, regardles of how
960 it was popped down (button press or WM_DELETE_WINDOW).
961 W is the dialog widget.
962 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
963 user_data is NULL (not used).
965 Returns TRUE to end propagation of event. */
967 dialog_delete_callback (w
, event
, user_data
)
972 gtk_widget_unmap (w
);
976 /* Create a popup dialog window. See also xg_create_widget below.
977 WV is a widget_value describing the dialog.
978 SELECT_CB is the callback to use when a button has been pressed.
979 DEACTIVATE_CB is the callback to use when the dialog pops down.
981 Returns the GTK dialog widget. */
983 create_dialog (wv
, select_cb
, deactivate_cb
)
986 GCallback deactivate_cb
;
988 char *title
= get_dialog_title (wv
->name
[0]);
989 int total_buttons
= wv
->name
[1] - '0';
990 int right_buttons
= wv
->name
[4] - '0';
993 int button_spacing
= 10;
994 GtkWidget
*wdialog
= gtk_dialog_new ();
999 GtkWidget
*whbox_down
;
1001 /* If the number of buttons is greater than 4, make two rows of buttons
1002 instead. This looks better. */
1003 int make_two_rows
= total_buttons
> 4;
1005 if (right_buttons
== 0) right_buttons
= total_buttons
/2;
1006 left_buttons
= total_buttons
- right_buttons
;
1008 gtk_window_set_title (GTK_WINDOW (wdialog
), title
);
1009 gtk_widget_set_name (wdialog
, "emacs-dialog");
1011 cur_box
= GTK_BOX (GTK_DIALOG (wdialog
)->action_area
);
1015 wvbox
= gtk_vbox_new (TRUE
, button_spacing
);
1016 whbox_up
= gtk_hbox_new (FALSE
, 0);
1017 whbox_down
= gtk_hbox_new (FALSE
, 0);
1019 gtk_box_pack_start (cur_box
, wvbox
, FALSE
, FALSE
, 0);
1020 gtk_box_pack_start (GTK_BOX (wvbox
), whbox_up
, FALSE
, FALSE
, 0);
1021 gtk_box_pack_start (GTK_BOX (wvbox
), whbox_down
, FALSE
, FALSE
, 0);
1023 cur_box
= GTK_BOX (whbox_up
);
1026 g_signal_connect (G_OBJECT (wdialog
), "delete-event",
1027 G_CALLBACK (dialog_delete_callback
), 0);
1031 g_signal_connect (G_OBJECT (wdialog
), "close", deactivate_cb
, 0);
1032 g_signal_connect (G_OBJECT (wdialog
), "response", deactivate_cb
, 0);
1035 for (item
= wv
->contents
; item
; item
= item
->next
)
1037 char *utf8_label
= get_utf8_string (item
->value
);
1041 if (item
->name
&& strcmp (item
->name
, "message") == 0)
1043 /* This is the text part of the dialog. */
1044 w
= gtk_label_new (utf8_label
);
1045 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog
)->vbox
),
1048 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog
)->vbox
), w
,
1050 gtk_misc_set_alignment (GTK_MISC (w
), 0.1, 0.5);
1052 /* Try to make dialog look better. Must realize first so
1053 the widget can calculate the size it needs. */
1054 gtk_widget_realize (w
);
1055 gtk_widget_size_request (w
, &req
);
1056 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog
)->vbox
),
1058 if (item
->value
&& strlen (item
->value
) > 0)
1059 button_spacing
= 2*req
.width
/strlen (item
->value
);
1063 /* This is one button to add to the dialog. */
1064 w
= gtk_button_new_with_label (utf8_label
);
1065 if (! item
->enabled
)
1066 gtk_widget_set_sensitive (w
, FALSE
);
1068 g_signal_connect (G_OBJECT (w
), "clicked",
1069 select_cb
, item
->call_data
);
1071 gtk_box_pack_start (cur_box
, w
, TRUE
, TRUE
, button_spacing
);
1072 if (++button_nr
== left_buttons
)
1075 cur_box
= GTK_BOX (whbox_down
);
1077 gtk_box_pack_start (cur_box
,
1084 if (utf8_label
&& utf8_label
!= item
->value
)
1085 g_free (utf8_label
);
1100 /* Callback function invoked when the Ok button is pressed in
1102 W is the file dialog widget,
1103 ARG points to an integer where we record what has happend. */
1105 xg_file_sel_ok (w
, arg
)
1109 *(int*)arg
= XG_FILE_OK
;
1112 /* Callback function invoked when the Cancel button is pressed in
1114 W is the file dialog widget,
1115 ARG points to an integer where we record what has happend. */
1117 xg_file_sel_cancel (w
, arg
)
1121 *(int*)arg
= XG_FILE_CANCEL
;
1124 /* Callback function invoked when the file dialog is destroyed (i.e.
1125 popped down). We must keep track of this, because if this
1126 happens, GTK destroys the widget. But if for example, Ok is pressed,
1127 the dialog is popped down, but the dialog widget is not destroyed.
1128 W is the file dialog widget,
1129 ARG points to an integer where we record what has happend. */
1131 xg_file_sel_destroy (w
, arg
)
1135 *(int*)arg
= XG_FILE_DESTROYED
;
1138 /* Read a file name from the user using a file dialog.
1139 F is the current frame.
1140 PROMPT is a prompt to show to the user. May not be NULL.
1141 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1142 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1145 Returns a file name or NULL if no file was selected.
1146 The returned string must be freed by the caller. */
1148 xg_get_file_name (f
, prompt
, default_filename
, mustmatch_p
)
1151 char *default_filename
;
1155 GtkFileSelection
*filesel
;
1156 int filesel_done
= XG_FILE_NOT_DONE
;
1159 filewin
= gtk_file_selection_new (prompt
);
1160 filesel
= GTK_FILE_SELECTION (filewin
);
1162 xg_set_screen (filewin
, f
);
1164 gtk_widget_set_name (filewin
, "emacs-filedialog");
1166 gtk_window_set_transient_for (GTK_WINDOW (filewin
),
1167 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
1168 gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin
), TRUE
);
1170 g_signal_connect (G_OBJECT (filesel
->ok_button
),
1172 G_CALLBACK (xg_file_sel_ok
),
1174 g_signal_connect (G_OBJECT (filesel
->cancel_button
),
1176 G_CALLBACK (xg_file_sel_cancel
),
1178 g_signal_connect (G_OBJECT (filesel
),
1180 G_CALLBACK (xg_file_sel_destroy
),
1183 if (default_filename
)
1184 gtk_file_selection_set_filename (filesel
, default_filename
);
1188 /* The selection_entry part of filesel is not documented. */
1189 gtk_widget_set_sensitive (filesel
->selection_entry
, FALSE
);
1190 gtk_file_selection_hide_fileop_buttons (filesel
);
1194 gtk_widget_show_all (filewin
);
1196 while (filesel_done
== XG_FILE_NOT_DONE
)
1197 gtk_main_iteration ();
1199 if (filesel_done
== XG_FILE_OK
)
1200 fn
= xstrdup ((char*) gtk_file_selection_get_filename (filesel
));
1202 if (filesel_done
!= XG_FILE_DESTROYED
)
1203 gtk_widget_destroy (filewin
);
1209 /***********************************************************************
1211 ***********************************************************************/
1213 /* The name of menu items that can be used for citomization. Since GTK
1214 RC files are very crude and primitive, we have to set this on all
1215 menu item names so a user can easily cutomize menu items. */
1217 #define MENU_ITEM_NAME "emacs-menuitem"
1220 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1221 during GC. The next member points to the items. */
1222 static xg_list_node xg_menu_cb_list
;
1224 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1225 during GC. The next member points to the items. */
1226 static xg_list_node xg_menu_item_cb_list
;
1228 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1229 F is the frame CL_DATA will be initialized for.
1230 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1232 The menu bar and all sub menus under the menu bar in a frame
1233 share the same structure, hence the reference count.
1235 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1236 allocated xg_menu_cb_data if CL_DATA is NULL. */
1237 static xg_menu_cb_data
*
1238 make_cl_data (cl_data
, f
, highlight_cb
)
1239 xg_menu_cb_data
*cl_data
;
1241 GCallback highlight_cb
;
1245 cl_data
= (xg_menu_cb_data
*) xmalloc (sizeof (*cl_data
));
1247 cl_data
->menu_bar_vector
= f
->menu_bar_vector
;
1248 cl_data
->menu_bar_items_used
= f
->menu_bar_items_used
;
1249 cl_data
->highlight_cb
= highlight_cb
;
1250 cl_data
->ref_count
= 0;
1252 xg_list_insert (&xg_menu_cb_list
, &cl_data
->ptrs
);
1255 cl_data
->ref_count
++;
1260 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1261 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1263 When the menu bar is updated, menu items may have been added and/or
1264 removed, so menu_bar_vector and menu_bar_items_used change. We must
1265 then update CL_DATA since it is used to determine which menu
1266 item that is invoked in the menu.
1267 HIGHLIGHT_CB could change, there is no check that the same
1268 function is given when modifying a menu bar as was given when
1269 creating the menu bar. */
1271 update_cl_data (cl_data
, f
, highlight_cb
)
1272 xg_menu_cb_data
*cl_data
;
1274 GCallback highlight_cb
;
1279 cl_data
->menu_bar_vector
= f
->menu_bar_vector
;
1280 cl_data
->menu_bar_items_used
= f
->menu_bar_items_used
;
1281 cl_data
->highlight_cb
= highlight_cb
;
1285 /* Decrease reference count for CL_DATA.
1286 If reference count is zero, free CL_DATA. */
1288 unref_cl_data (cl_data
)
1289 xg_menu_cb_data
*cl_data
;
1291 if (cl_data
&& cl_data
->ref_count
> 0)
1293 cl_data
->ref_count
--;
1294 if (cl_data
->ref_count
== 0)
1296 xg_list_remove (&xg_menu_cb_list
, &cl_data
->ptrs
);
1302 /* Function that marks all lisp data during GC. */
1308 for (iter
= xg_menu_cb_list
.next
; iter
; iter
= iter
->next
)
1309 mark_object (((xg_menu_cb_data
*) iter
)->menu_bar_vector
);
1311 for (iter
= xg_menu_item_cb_list
.next
; iter
; iter
= iter
->next
)
1313 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) iter
;
1315 if (! NILP (cb_data
->help
))
1316 mark_object (cb_data
->help
);
1321 /* Callback called when a menu item is destroyed. Used to free data.
1322 W is the widget that is being destroyed (not used).
1323 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1325 menuitem_destroy_callback (w
, client_data
)
1327 gpointer client_data
;
1331 xg_menu_item_cb_data
*data
= (xg_menu_item_cb_data
*) client_data
;
1332 xg_list_remove (&xg_menu_item_cb_list
, &data
->ptrs
);
1337 /* Callback called when the pointer enters/leaves a menu item.
1339 EVENT is either an enter event or leave event.
1340 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1342 Returns FALSE to tell GTK to keep processing this event. */
1344 menuitem_highlight_callback (w
, event
, client_data
)
1346 GdkEventCrossing
*event
;
1347 gpointer client_data
;
1351 xg_menu_item_cb_data
*data
= (xg_menu_item_cb_data
*) client_data
;
1352 gpointer call_data
= event
->type
== GDK_LEAVE_NOTIFY
? 0 : client_data
;
1354 if (! NILP (data
->help
) && data
->cl_data
->highlight_cb
)
1356 GtkCallback func
= (GtkCallback
) data
->cl_data
->highlight_cb
;
1357 (*func
) (w
, call_data
);
1364 /* Callback called when a menu is destroyed. Used to free data.
1365 W is the widget that is being destroyed (not used).
1366 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1368 menu_destroy_callback (w
, client_data
)
1370 gpointer client_data
;
1372 unref_cl_data ((xg_menu_cb_data
*) client_data
);
1375 /* Callback called when a menu does a grab or ungrab. That means the
1376 menu has been activated or deactivated.
1377 Used to start a timer so the small timeout the menus in GTK uses before
1378 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1379 W is the widget that does the grab (not used).
1380 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1381 CLIENT_DATA is NULL (not used). */
1383 menu_grab_callback (GtkWidget
*widget
,
1385 gpointer client_data
)
1387 /* Keep track of total number of grabs. */
1390 if (ungrab_p
) cnt
--;
1393 if (cnt
> 0 && ! xg_timer
) xg_start_timer ();
1394 else if (cnt
== 0 && xg_timer
) xg_stop_timer ();
1397 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1398 must be non-NULL) and can be inserted into a menu item.
1400 Returns the GtkHBox. */
1402 make_widget_for_menu_item (utf8_label
, utf8_key
)
1410 wbox
= gtk_hbox_new (FALSE
, 0);
1411 wlbl
= gtk_label_new (utf8_label
);
1412 wkey
= gtk_label_new (utf8_key
);
1414 gtk_misc_set_alignment (GTK_MISC (wlbl
), 0.0, 0.5);
1415 gtk_misc_set_alignment (GTK_MISC (wkey
), 0.0, 0.5);
1417 gtk_box_pack_start (GTK_BOX (wbox
), wlbl
, TRUE
, TRUE
, 0);
1418 gtk_box_pack_start (GTK_BOX (wbox
), wkey
, FALSE
, FALSE
, 0);
1420 gtk_widget_set_name (wlbl
, MENU_ITEM_NAME
);
1421 gtk_widget_set_name (wkey
, MENU_ITEM_NAME
);
1422 gtk_widget_set_name (wbox
, MENU_ITEM_NAME
);
1427 /* Make and return a menu item widget with the key to the right.
1428 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1429 UTF8_KEY is the text representing the key binding.
1430 ITEM is the widget_value describing the menu item.
1432 GROUP is an in/out parameter. If the menu item to be created is not
1433 part of any radio menu group, *GROUP contains NULL on entry and exit.
1434 If the menu item to be created is part of a radio menu group, on entry
1435 *GROUP contains the group to use, or NULL if this is the first item
1436 in the group. On exit, *GROUP contains the radio item group.
1438 Unfortunately, keys don't line up as nicely as in Motif,
1439 but the MacOS X version doesn't either, so I guess that is OK. */
1441 make_menu_item (utf8_label
, utf8_key
, item
, group
)
1448 GtkWidget
*wtoadd
= 0;
1450 /* It has been observed that some menu items have a NULL name field.
1451 This will lead to this function being called with a NULL utf8_label.
1452 GTK crashes on that so we set a blank label. Why there is a NULL
1453 name remains to be investigated. */
1454 if (! utf8_label
) utf8_label
= " ";
1457 wtoadd
= make_widget_for_menu_item (utf8_label
, utf8_key
);
1459 if (item
->button_type
== BUTTON_TYPE_TOGGLE
)
1462 if (utf8_key
) w
= gtk_check_menu_item_new ();
1463 else w
= gtk_check_menu_item_new_with_label (utf8_label
);
1464 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), item
->selected
);
1466 else if (item
->button_type
== BUTTON_TYPE_RADIO
)
1468 if (utf8_key
) w
= gtk_radio_menu_item_new (*group
);
1469 else w
= gtk_radio_menu_item_new_with_label (*group
, utf8_label
);
1470 *group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w
));
1472 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), TRUE
);
1477 if (utf8_key
) w
= gtk_menu_item_new ();
1478 else w
= gtk_menu_item_new_with_label (utf8_label
);
1481 if (wtoadd
) gtk_container_add (GTK_CONTAINER (w
), wtoadd
);
1482 if (! item
->enabled
) gtk_widget_set_sensitive (w
, FALSE
);
1487 /* Return non-zero if LABEL specifies a separator (GTK only has one
1490 xg_separator_p (char *label
)
1492 if (! label
) return 0;
1493 else if (strlen (label
) > 3
1494 && strncmp (label
, "--", 2) == 0
1497 static char* separator_names
[] = {
1502 "single-dashed-line",
1503 "double-dashed-line",
1505 "shadow-etched-out",
1506 "shadow-etched-in-dash",
1507 "shadow-etched-out-dash",
1508 "shadow-double-etched-in",
1509 "shadow-double-etched-out",
1510 "shadow-double-etched-in-dash",
1511 "shadow-double-etched-out-dash",
1518 for (i
= 0; separator_names
[i
]; ++i
)
1519 if (strcmp (label
, separator_names
[i
]) == 0)
1524 /* Old-style separator, maybe. It's a separator if it contains
1526 while (*label
== '-')
1528 if (*label
== 0) return 1;
1534 static int xg_detached_menus
;
1536 /* Returns non-zero if there are detached menus. */
1538 xg_have_tear_offs ()
1540 return xg_detached_menus
> 0;
1543 /* Callback invoked when a detached menu window is removed. Here we
1544 decrease the xg_detached_menus count.
1545 WIDGET is the top level window that is removed (the parent of the menu).
1546 CLIENT_DATA is not used. */
1548 tearoff_remove (widget
, client_data
)
1550 gpointer client_data
;
1552 if (xg_detached_menus
> 0) --xg_detached_menus
;
1555 /* Callback invoked when a menu is detached. It increases the
1556 xg_detached_menus count.
1557 WIDGET is the GtkTearoffMenuItem.
1558 CLIENT_DATA is not used. */
1560 tearoff_activate (widget
, client_data
)
1562 gpointer client_data
;
1564 GtkWidget
*menu
= gtk_widget_get_parent (widget
);
1565 if (gtk_menu_get_tearoff_state (GTK_MENU (menu
)))
1567 ++xg_detached_menus
;
1568 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget
)),
1570 G_CALLBACK (tearoff_remove
), 0);
1575 /* Create a menu item widget, and connect the callbacks.
1576 ITEM decribes the menu item.
1577 F is the frame the created menu belongs to.
1578 SELECT_CB is the callback to use when a menu item is selected.
1579 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1580 CL_DATA points to the callback data to be used for this menu.
1581 GROUP is an in/out parameter. If the menu item to be created is not
1582 part of any radio menu group, *GROUP contains NULL on entry and exit.
1583 If the menu item to be created is part of a radio menu group, on entry
1584 *GROUP contains the group to use, or NULL if this is the first item
1585 in the group. On exit, *GROUP contains the radio item group.
1587 Returns the created GtkWidget. */
1589 xg_create_one_menuitem (item
, f
, select_cb
, highlight_cb
, cl_data
, group
)
1592 GCallback select_cb
;
1593 GCallback highlight_cb
;
1594 xg_menu_cb_data
*cl_data
;
1600 xg_menu_item_cb_data
*cb_data
;
1602 utf8_label
= get_utf8_string (item
->name
);
1603 utf8_key
= get_utf8_string (item
->key
);
1605 w
= make_menu_item (utf8_label
, utf8_key
, item
, group
);
1607 if (utf8_label
&& utf8_label
!= item
->name
) g_free (utf8_label
);
1608 if (utf8_key
&& utf8_key
!= item
->key
) g_free (utf8_key
);
1610 cb_data
= xmalloc (sizeof (xg_menu_item_cb_data
));
1612 xg_list_insert (&xg_menu_item_cb_list
, &cb_data
->ptrs
);
1614 cb_data
->unhighlight_id
= cb_data
->highlight_id
= cb_data
->select_id
= 0;
1615 cb_data
->help
= item
->help
;
1616 cb_data
->cl_data
= cl_data
;
1617 cb_data
->call_data
= item
->call_data
;
1619 g_signal_connect (G_OBJECT (w
),
1621 G_CALLBACK (menuitem_destroy_callback
),
1624 /* Put cb_data in widget, so we can get at it when modifying menubar */
1625 g_object_set_data (G_OBJECT (w
), XG_ITEM_DATA
, cb_data
);
1627 /* final item, not a submenu */
1628 if (item
->call_data
&& ! item
->contents
)
1632 = g_signal_connect (G_OBJECT (w
), "activate", select_cb
, cb_data
);
1635 if (! NILP (item
->help
) && highlight_cb
)
1637 /* We use enter/leave notify instead of select/deselect because
1638 select/deselect doesn't go well with detached menus. */
1639 cb_data
->highlight_id
1640 = g_signal_connect (G_OBJECT (w
),
1641 "enter-notify-event",
1642 G_CALLBACK (menuitem_highlight_callback
),
1644 cb_data
->unhighlight_id
1645 = g_signal_connect (G_OBJECT (w
),
1646 "leave-notify-event",
1647 G_CALLBACK (menuitem_highlight_callback
),
1654 static GtkWidget
*create_menus
P_ ((widget_value
*, FRAME_PTR
, GCallback
,
1655 GCallback
, GCallback
, int, int, int,
1656 GtkWidget
*, xg_menu_cb_data
*, char *));
1658 /* Create a full menu tree specified by DATA.
1659 F is the frame the created menu belongs to.
1660 SELECT_CB is the callback to use when a menu item is selected.
1661 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1662 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1663 POP_UP_P is non-zero if we shall create a popup menu.
1664 MENU_BAR_P is non-zero if we shall create a menu bar.
1665 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1666 if MENU_BAR_P is non-zero.
1667 TOPMENU is the topmost GtkWidget that others shall be placed under.
1668 It may be NULL, in that case we create the appropriate widget
1669 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1670 CL_DATA is the callback data we shall use for this menu, or NULL
1671 if we haven't set the first callback yet.
1672 NAME is the name to give to the top level menu if this function
1673 creates it. May be NULL to not set any name.
1675 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1678 This function calls itself to create submenus. */
1681 create_menus (data
, f
, select_cb
, deactivate_cb
, highlight_cb
,
1682 pop_up_p
, menu_bar_p
, add_tearoff_p
, topmenu
, cl_data
, name
)
1685 GCallback select_cb
;
1686 GCallback deactivate_cb
;
1687 GCallback highlight_cb
;
1692 xg_menu_cb_data
*cl_data
;
1696 GtkWidget
*wmenu
= topmenu
;
1697 GSList
*group
= NULL
;
1703 wmenu
= gtk_menu_new ();
1704 xg_set_screen (wmenu
, f
);
1706 else wmenu
= gtk_menu_bar_new ();
1708 /* Put cl_data on the top menu for easier access. */
1709 cl_data
= make_cl_data (cl_data
, f
, highlight_cb
);
1710 g_object_set_data (G_OBJECT (wmenu
), XG_FRAME_DATA
, (gpointer
)cl_data
);
1711 g_signal_connect (G_OBJECT (wmenu
), "destroy",
1712 G_CALLBACK (menu_destroy_callback
), cl_data
);
1715 gtk_widget_set_name (wmenu
, name
);
1718 g_signal_connect (G_OBJECT (wmenu
),
1719 "deactivate", deactivate_cb
, 0);
1721 g_signal_connect (G_OBJECT (wmenu
),
1722 "grab-notify", G_CALLBACK (menu_grab_callback
), 0);
1725 if (! menu_bar_p
&& add_tearoff_p
)
1727 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new ();
1728 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu
), tearoff
);
1730 g_signal_connect (G_OBJECT (tearoff
), "activate",
1731 G_CALLBACK (tearoff_activate
), 0);
1734 for (item
= data
; item
; item
= item
->next
)
1738 if (pop_up_p
&& !item
->contents
&& !item
->call_data
1739 && !xg_separator_p (item
->name
))
1742 /* A title for a popup. We do the same as GTK does when
1743 creating titles, but it does not look good. */
1745 utf8_label
= get_utf8_string (item
->name
);
1747 gtk_menu_set_title (GTK_MENU (wmenu
), utf8_label
);
1748 w
= gtk_menu_item_new_with_label (utf8_label
);
1749 gtk_widget_set_sensitive (w
, FALSE
);
1750 if (utf8_label
&& utf8_label
!= item
->name
) g_free (utf8_label
);
1752 else if (xg_separator_p (item
->name
))
1755 /* GTK only have one separator type. */
1756 w
= gtk_separator_menu_item_new ();
1760 w
= xg_create_one_menuitem (item
,
1762 item
->contents
? 0 : select_cb
,
1769 GtkWidget
*submenu
= create_menus (item
->contents
,
1780 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w
), submenu
);
1784 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu
), w
);
1785 gtk_widget_set_name (w
, MENU_ITEM_NAME
);
1791 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1792 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1793 with some text and buttons.
1794 F is the frame the created item belongs to.
1795 NAME is the name to use for the top widget.
1796 VAL is a widget_value structure describing items to be created.
1797 SELECT_CB is the callback to use when a menu item is selected or
1798 a dialog button is pressed.
1799 DEACTIVATE_CB is the callback to use when an item is deactivated.
1800 For a menu, when a sub menu is not shown anymore, for a dialog it is
1801 called when the dialog is popped down.
1802 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1804 Returns the widget created. */
1806 xg_create_widget (type
, name
, f
, val
,
1807 select_cb
, deactivate_cb
, highlight_cb
)
1812 GCallback select_cb
;
1813 GCallback deactivate_cb
;
1814 GCallback highlight_cb
;
1817 int menu_bar_p
= strcmp (type
, "menubar") == 0;
1818 int pop_up_p
= strcmp (type
, "popup") == 0;
1820 if (strcmp (type
, "dialog") == 0)
1822 w
= create_dialog (val
, select_cb
, deactivate_cb
);
1823 xg_set_screen (w
, f
);
1824 gtk_window_set_transient_for (GTK_WINDOW (w
),
1825 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
1826 gtk_window_set_destroy_with_parent (GTK_WINDOW (w
), TRUE
);
1827 gtk_widget_set_name (w
, "emacs-dialog");
1829 else if (menu_bar_p
|| pop_up_p
)
1831 w
= create_menus (val
->contents
,
1843 /* Set the cursor to an arrow for popup menus when they are mapped.
1844 This is done by default for menu bar menus. */
1847 /* Must realize so the GdkWindow inside the widget is created. */
1848 gtk_widget_realize (w
);
1849 xg_set_cursor (w
, FRAME_X_DISPLAY_INFO (f
)->xg_cursor
);
1854 fprintf (stderr
, "bad type in xg_create_widget: %s, doing nothing\n",
1861 /* Return the label for menu item WITEM. */
1863 xg_get_menu_item_label (witem
)
1866 GtkLabel
*wlabel
= GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem
)));
1867 return gtk_label_get_label (wlabel
);
1870 /* Return non-zero if the menu item WITEM has the text LABEL. */
1872 xg_item_label_same_p (witem
, label
)
1877 char *utf8_label
= get_utf8_string (label
);
1878 const char *old_label
= witem
? xg_get_menu_item_label (witem
) : 0;
1880 if (! old_label
&& ! utf8_label
)
1882 else if (old_label
&& utf8_label
)
1883 is_same
= strcmp (utf8_label
, old_label
) == 0;
1885 if (utf8_label
&& utf8_label
!= label
) g_free (utf8_label
);
1890 /* Remove widgets in LIST from container WCONT. */
1892 remove_from_container (wcont
, list
)
1898 for (iter
= list
; iter
; iter
= g_list_next (iter
))
1900 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
1902 /* Add a ref to w so we can explicitly destroy it later. */
1904 gtk_container_remove (GTK_CONTAINER (wcont
), w
);
1906 /* If there is a menu under this widget that has been detached,
1907 there is a reference to it, and just removing w from the
1908 container does not destroy the submenu. By explicitly
1909 destroying w we make sure the submenu is destroyed, thus
1910 removing the detached window also if there was one. */
1911 gtk_widget_destroy (w
);
1915 /* Update the top level names in MENUBAR (i.e. not submenus).
1916 F is the frame the menu bar belongs to.
1917 *LIST is a list with the current menu bar names (menu item widgets).
1918 ITER is the item within *LIST that shall be updated.
1919 POS is the numerical position, starting at 0, of ITER in *LIST.
1920 VAL describes what the menu bar shall look like after the update.
1921 SELECT_CB is the callback to use when a menu item is selected.
1922 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1923 CL_DATA points to the callback data to be used for this menu bar.
1925 This function calls itself to walk through the menu bar names. */
1927 xg_update_menubar (menubar
, f
, list
, iter
, pos
, val
,
1928 select_cb
, highlight_cb
, cl_data
)
1935 GCallback select_cb
;
1936 GCallback highlight_cb
;
1937 xg_menu_cb_data
*cl_data
;
1939 if (! iter
&& ! val
)
1941 else if (iter
&& ! val
)
1943 /* Item(s) have been removed. Remove all remaining items. */
1944 remove_from_container (menubar
, iter
);
1950 else if (! iter
&& val
)
1952 /* Item(s) added. Add all new items in one call. */
1953 create_menus (val
, f
, select_cb
, 0, highlight_cb
,
1954 0, 1, 0, menubar
, cl_data
, 0);
1960 /* Below this neither iter or val is NULL */
1961 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter
->data
), val
->name
))
1963 /* This item is still the same, check next item. */
1965 iter
= g_list_next (iter
);
1968 else /* This item is changed. */
1970 GtkMenuItem
*witem
= GTK_MENU_ITEM (iter
->data
);
1971 GtkMenuItem
*witem2
= 0;
1972 int val_in_menubar
= 0;
1973 int iter_in_new_menubar
= 0;
1977 /* See if the changed entry (val) is present later in the menu bar */
1979 iter2
&& ! val_in_menubar
;
1980 iter2
= g_list_next (iter2
))
1982 witem2
= GTK_MENU_ITEM (iter2
->data
);
1983 val_in_menubar
= xg_item_label_same_p (witem2
, val
->name
);
1986 /* See if the current entry (iter) is present later in the
1987 specification for the new menu bar. */
1988 for (cur
= val
; cur
&& ! iter_in_new_menubar
; cur
= cur
->next
)
1989 iter_in_new_menubar
= xg_item_label_same_p (witem
, cur
->name
);
1991 if (val_in_menubar
&& ! iter_in_new_menubar
)
1995 /* This corresponds to:
2000 gtk_widget_ref (GTK_WIDGET (witem
));
2001 gtk_container_remove (GTK_CONTAINER (menubar
), GTK_WIDGET (witem
));
2002 gtk_widget_destroy (GTK_WIDGET (witem
));
2004 /* Must get new list since the old changed. */
2005 g_list_free (*list
);
2006 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2007 while (nr
-- > 0) iter
= g_list_next (iter
);
2009 else if (! val_in_menubar
&& ! iter_in_new_menubar
)
2011 /* This corresponds to:
2014 Rename B to X. This might seem to be a strange thing to do,
2015 since if there is a menu under B it will be totally wrong for X.
2016 But consider editing a C file. Then there is a C-mode menu
2017 (corresponds to B above).
2018 If then doing C-x C-f the minibuf menu (X above) replaces the
2019 C-mode menu. When returning from the minibuffer, we get
2020 back the C-mode menu. Thus we do:
2021 Rename B to X (C-mode to minibuf menu)
2022 Rename X to B (minibuf to C-mode menu).
2023 If the X menu hasn't been invoked, the menu under B
2024 is up to date when leaving the minibuffer. */
2025 GtkLabel
*wlabel
= GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem
)));
2026 char *utf8_label
= get_utf8_string (val
->name
);
2027 GtkWidget
*submenu
= gtk_menu_item_get_submenu (witem
);
2029 gtk_label_set_text (wlabel
, utf8_label
);
2031 /* If this item has a submenu that has been detached, change
2032 the title in the WM decorations also. */
2033 if (submenu
&& gtk_menu_get_tearoff_state (GTK_MENU (submenu
)))
2034 /* Set the title of the detached window. */
2035 gtk_menu_set_title (GTK_MENU (submenu
), utf8_label
);
2037 iter
= g_list_next (iter
);
2041 else if (! val_in_menubar
&& iter_in_new_menubar
)
2043 /* This corresponds to:
2050 GtkWidget
*w
= xg_create_one_menuitem (val
,
2057 gtk_widget_set_name (w
, MENU_ITEM_NAME
);
2058 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
), w
, pos
);
2060 g_list_free (*list
);
2061 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2062 while (nr
-- > 0) iter
= g_list_next (iter
);
2063 iter
= g_list_next (iter
);
2067 else /* if (val_in_menubar && iter_in_new_menubar) */
2070 /* This corresponds to:
2075 gtk_widget_ref (GTK_WIDGET (witem2
));
2076 gtk_container_remove (GTK_CONTAINER (menubar
), GTK_WIDGET (witem2
));
2077 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
),
2078 GTK_WIDGET (witem2
), pos
);
2079 gtk_widget_unref (GTK_WIDGET (witem2
));
2081 g_list_free (*list
);
2082 *list
= iter
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2083 while (nr
-- > 0) iter
= g_list_next (iter
);
2089 /* Update the rest of the menu bar. */
2090 xg_update_menubar (menubar
, f
, list
, iter
, pos
, val
,
2091 select_cb
, highlight_cb
, cl_data
);
2094 /* Update the menu item W so it corresponds to VAL.
2095 SELECT_CB is the callback to use when a menu item is selected.
2096 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2097 CL_DATA is the data to set in the widget for menu invokation. */
2099 xg_update_menu_item (val
, w
, select_cb
, highlight_cb
, cl_data
)
2102 GCallback select_cb
;
2103 GCallback highlight_cb
;
2104 xg_menu_cb_data
*cl_data
;
2111 const char *old_label
= 0;
2112 const char *old_key
= 0;
2113 xg_menu_item_cb_data
*cb_data
;
2115 wchild
= gtk_bin_get_child (GTK_BIN (w
));
2116 utf8_label
= get_utf8_string (val
->name
);
2117 utf8_key
= get_utf8_string (val
->key
);
2119 /* See if W is a menu item with a key. See make_menu_item above. */
2120 if (GTK_IS_HBOX (wchild
))
2122 GList
*list
= gtk_container_get_children (GTK_CONTAINER (wchild
));
2124 wlbl
= GTK_LABEL (list
->data
);
2125 wkey
= GTK_LABEL (list
->next
->data
);
2130 /* Remove the key and keep just the label. */
2131 gtk_widget_ref (GTK_WIDGET (wlbl
));
2132 gtk_container_remove (GTK_CONTAINER (w
), wchild
);
2133 gtk_container_add (GTK_CONTAINER (w
), GTK_WIDGET (wlbl
));
2138 else /* Just a label. */
2140 wlbl
= GTK_LABEL (wchild
);
2142 /* Check if there is now a key. */
2145 GtkWidget
*wtoadd
= make_widget_for_menu_item (utf8_label
, utf8_key
);
2146 GList
*list
= gtk_container_get_children (GTK_CONTAINER (wtoadd
));
2148 wlbl
= GTK_LABEL (list
->data
);
2149 wkey
= GTK_LABEL (list
->next
->data
);
2152 gtk_container_remove (GTK_CONTAINER (w
), wchild
);
2153 gtk_container_add (GTK_CONTAINER (w
), wtoadd
);
2158 if (wkey
) old_key
= gtk_label_get_label (wkey
);
2159 if (wlbl
) old_label
= gtk_label_get_label (wlbl
);
2161 if (wkey
&& utf8_key
&& (! old_key
|| strcmp (utf8_key
, old_key
) != 0))
2162 gtk_label_set_text (wkey
, utf8_key
);
2164 if (! old_label
|| strcmp (utf8_label
, old_label
) != 0)
2165 gtk_label_set_text (wlbl
, utf8_label
);
2167 if (utf8_key
&& utf8_key
!= val
->key
) g_free (utf8_key
);
2168 if (utf8_label
&& utf8_label
!= val
->name
) g_free (utf8_label
);
2170 if (! val
->enabled
&& GTK_WIDGET_SENSITIVE (w
))
2171 gtk_widget_set_sensitive (w
, FALSE
);
2172 else if (val
->enabled
&& ! GTK_WIDGET_SENSITIVE (w
))
2173 gtk_widget_set_sensitive (w
, TRUE
);
2175 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (w
),
2179 cb_data
->call_data
= val
->call_data
;
2180 cb_data
->help
= val
->help
;
2181 cb_data
->cl_data
= cl_data
;
2183 /* We assume the callback functions don't change. */
2184 if (val
->call_data
&& ! val
->contents
)
2186 /* This item shall have a select callback. */
2187 if (! cb_data
->select_id
)
2189 = g_signal_connect (G_OBJECT (w
), "activate",
2190 select_cb
, cb_data
);
2192 else if (cb_data
->select_id
)
2194 g_signal_handler_disconnect (w
, cb_data
->select_id
);
2195 cb_data
->select_id
= 0;
2198 if (NILP (cb_data
->help
))
2200 /* Shall not have help. Remove if any existed previously. */
2201 if (cb_data
->highlight_id
)
2203 g_signal_handler_disconnect (G_OBJECT (w
),
2204 cb_data
->highlight_id
);
2205 cb_data
->highlight_id
= 0;
2207 if (cb_data
->unhighlight_id
)
2209 g_signal_handler_disconnect (G_OBJECT (w
),
2210 cb_data
->unhighlight_id
);
2211 cb_data
->unhighlight_id
= 0;
2214 else if (! cb_data
->highlight_id
&& highlight_cb
)
2216 /* Have help now, but didn't previously. Add callback. */
2217 cb_data
->highlight_id
2218 = g_signal_connect (G_OBJECT (w
),
2219 "enter-notify-event",
2220 G_CALLBACK (menuitem_highlight_callback
),
2222 cb_data
->unhighlight_id
2223 = g_signal_connect (G_OBJECT (w
),
2224 "leave-notify-event",
2225 G_CALLBACK (menuitem_highlight_callback
),
2231 /* Update the toggle menu item W so it corresponds to VAL. */
2233 xg_update_toggle_item (val
, w
)
2237 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), val
->selected
);
2240 /* Update the radio menu item W so it corresponds to VAL. */
2242 xg_update_radio_item (val
, w
)
2246 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), val
->selected
);
2249 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2250 SUBMENU may be NULL, in that case a new menu is created.
2251 F is the frame the menu bar belongs to.
2252 VAL describes the contents of the menu bar.
2253 SELECT_CB is the callback to use when a menu item is selected.
2254 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2255 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2256 CL_DATA is the call back data to use for any newly created items.
2258 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2262 xg_update_submenu (submenu
, f
, val
,
2263 select_cb
, deactivate_cb
, highlight_cb
, cl_data
)
2267 GCallback select_cb
;
2268 GCallback deactivate_cb
;
2269 GCallback highlight_cb
;
2270 xg_menu_cb_data
*cl_data
;
2272 GtkWidget
*newsub
= submenu
;
2276 int has_tearoff_p
= 0;
2277 GList
*first_radio
= 0;
2280 list
= gtk_container_get_children (GTK_CONTAINER (submenu
));
2282 for (cur
= val
, iter
= list
;
2284 iter
= g_list_next (iter
), cur
= cur
->next
)
2286 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
2288 /* Skip tearoff items, they have no counterpart in val. */
2289 if (GTK_IS_TEAROFF_MENU_ITEM (w
))
2292 iter
= g_list_next (iter
);
2293 if (iter
) w
= GTK_WIDGET (iter
->data
);
2297 /* Remember first radio button in a group. If we get a mismatch in
2298 a radio group we must rebuild the whole group so that the connections
2299 in GTK becomes correct. */
2300 if (cur
->button_type
== BUTTON_TYPE_RADIO
&& ! first_radio
)
2302 else if (cur
->button_type
!= BUTTON_TYPE_RADIO
2303 && ! GTK_IS_RADIO_MENU_ITEM (w
))
2306 if (GTK_IS_SEPARATOR_MENU_ITEM (w
))
2308 if (! xg_separator_p (cur
->name
))
2311 else if (GTK_IS_CHECK_MENU_ITEM (w
))
2313 if (cur
->button_type
!= BUTTON_TYPE_TOGGLE
)
2315 xg_update_toggle_item (cur
, w
);
2316 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
2318 else if (GTK_IS_RADIO_MENU_ITEM (w
))
2320 if (cur
->button_type
!= BUTTON_TYPE_RADIO
)
2322 xg_update_radio_item (cur
, w
);
2323 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
2325 else if (GTK_IS_MENU_ITEM (w
))
2327 GtkMenuItem
*witem
= GTK_MENU_ITEM (w
);
2330 if (cur
->button_type
!= BUTTON_TYPE_NONE
||
2331 xg_separator_p (cur
->name
))
2334 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
2336 sub
= gtk_menu_item_get_submenu (witem
);
2337 if (sub
&& ! cur
->contents
)
2339 /* Not a submenu anymore. */
2340 gtk_widget_ref (sub
);
2341 gtk_menu_item_remove_submenu (witem
);
2342 gtk_widget_destroy (sub
);
2344 else if (cur
->contents
)
2348 nsub
= xg_update_submenu (sub
, f
, cur
->contents
,
2349 select_cb
, deactivate_cb
,
2350 highlight_cb
, cl_data
);
2352 /* If this item just became a submenu, we must set it. */
2354 gtk_menu_item_set_submenu (witem
, nsub
);
2359 /* Structural difference. Remove everything from here and down
2365 /* Remove widgets from first structual change. */
2368 /* If we are adding new menu items below, we must remove from
2369 first radio button so that radio groups become correct. */
2370 if (cur
&& first_radio
) remove_from_container (submenu
, first_radio
);
2371 else remove_from_container (submenu
, iter
);
2376 /* More items added. Create them. */
2377 newsub
= create_menus (cur
,
2390 if (list
) g_list_free (list
);
2395 /* Update the MENUBAR.
2396 F is the frame the menu bar belongs to.
2397 VAL describes the contents of the menu bar.
2398 If DEEP_P is non-zero, rebuild all but the top level menu names in
2399 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2400 SELECT_CB is the callback to use when a menu item is selected.
2401 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2402 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2404 xg_modify_menubar_widgets (menubar
, f
, val
, deep_p
,
2405 select_cb
, deactivate_cb
, highlight_cb
)
2410 GCallback select_cb
;
2411 GCallback deactivate_cb
;
2412 GCallback highlight_cb
;
2414 xg_menu_cb_data
*cl_data
;
2415 GList
*list
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2419 cl_data
= (xg_menu_cb_data
*) g_object_get_data (G_OBJECT (menubar
),
2422 xg_update_menubar (menubar
, f
, &list
, list
, 0, val
->contents
,
2423 select_cb
, highlight_cb
, cl_data
);
2429 /* Update all sub menus.
2430 We must keep the submenus (GTK menu item widgets) since the
2431 X Window in the XEvent that activates the menu are those widgets. */
2433 /* Update cl_data, menu_item things in F may have changed. */
2434 update_cl_data (cl_data
, f
, highlight_cb
);
2436 for (cur
= val
->contents
; cur
; cur
= cur
->next
)
2443 /* Find sub menu that corresponds to val and update it. */
2444 for (iter
= list
; iter
; iter
= g_list_next (iter
))
2446 witem
= GTK_MENU_ITEM (iter
->data
);
2447 if (xg_item_label_same_p (witem
, cur
->name
))
2449 sub
= gtk_menu_item_get_submenu (witem
);
2454 newsub
= xg_update_submenu (sub
,
2461 /* sub may still be NULL. If we just updated non deep and added
2462 a new menu bar item, it has no sub menu yet. So we set the
2463 newly created sub menu under witem. */
2466 xg_set_screen (newsub
, f
);
2467 gtk_menu_item_set_submenu (witem
, newsub
);
2473 gtk_widget_show_all (menubar
);
2476 /* Recompute all the widgets of frame F, when the menu bar has been
2477 changed. Value is non-zero if widgets were updated. */
2480 xg_update_frame_menubar (f
)
2483 struct x_output
*x
= f
->output_data
.x
;
2486 if (!x
->menubar_widget
|| GTK_WIDGET_MAPPED (x
->menubar_widget
))
2491 gtk_box_pack_start (GTK_BOX (x
->vbox_widget
), x
->menubar_widget
,
2493 gtk_box_reorder_child (GTK_BOX (x
->vbox_widget
), x
->menubar_widget
, 0);
2495 gtk_widget_show_all (x
->menubar_widget
);
2496 gtk_widget_size_request (x
->menubar_widget
, &req
);
2498 FRAME_MENUBAR_HEIGHT (f
) = req
.height
;
2500 /* The height has changed, resize outer widget and set columns
2501 rows to what we had before adding the menu bar. */
2502 xg_resize_outer_widget (f
, FRAME_COLS (f
), FRAME_LINES (f
));
2504 SET_FRAME_GARBAGED (f
);
2510 /* Get rid of the menu bar of frame F, and free its storage.
2511 This is used when deleting a frame, and when turning off the menu bar. */
2514 free_frame_menubar (f
)
2517 struct x_output
*x
= f
->output_data
.x
;
2519 if (x
->menubar_widget
)
2523 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
), x
->menubar_widget
);
2524 /* The menubar and its children shall be deleted when removed from
2526 x
->menubar_widget
= 0;
2527 FRAME_MENUBAR_HEIGHT (f
) = 0;
2529 /* The height has changed, resize outer widget and set columns
2530 rows to what we had before removing the menu bar. */
2531 xg_resize_outer_widget (f
, FRAME_COLS (f
), FRAME_LINES (f
));
2533 SET_FRAME_GARBAGED (f
);
2540 /***********************************************************************
2541 Scroll bar functions
2542 ***********************************************************************/
2545 /* Setting scroll bar values invokes the callback. Use this variable
2546 to indicate that callback should do nothing. */
2547 int xg_ignore_gtk_scrollbar
;
2549 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2550 32 bits. But we want to store pointers, and they may be larger
2551 than 32 bits. Keep a mapping from integer index to widget pointers
2552 to get around the 32 bit limitation. */
2555 GtkWidget
**widgets
;
2560 /* Grow this much every time we need to allocate more */
2561 #define ID_TO_WIDGET_INCR 32
2563 /* Store the widget pointer W in id_to_widget and return the integer index. */
2565 xg_store_widget_in_map (w
)
2570 if (id_to_widget
.max_size
== id_to_widget
.used
)
2572 int new_size
= id_to_widget
.max_size
+ ID_TO_WIDGET_INCR
;
2574 id_to_widget
.widgets
= xrealloc (id_to_widget
.widgets
,
2575 sizeof (GtkWidget
*)*new_size
);
2577 for (i
= id_to_widget
.max_size
; i
< new_size
; ++i
)
2578 id_to_widget
.widgets
[i
] = 0;
2579 id_to_widget
.max_size
= new_size
;
2582 /* Just loop over the array and find a free place. After all,
2583 how many scroll bars are we creating? Should be a small number.
2584 The check above guarantees we will find a free place. */
2585 for (i
= 0; i
< id_to_widget
.max_size
; ++i
)
2587 if (! id_to_widget
.widgets
[i
])
2589 id_to_widget
.widgets
[i
] = w
;
2590 ++id_to_widget
.used
;
2596 /* Should never end up here */
2600 /* Remove pointer at IDX from id_to_widget.
2601 Called when scroll bar is destroyed. */
2603 xg_remove_widget_from_map (idx
)
2606 if (idx
< id_to_widget
.max_size
&& id_to_widget
.widgets
[idx
] != 0)
2608 id_to_widget
.widgets
[idx
] = 0;
2609 --id_to_widget
.used
;
2613 /* Get the widget pointer at IDX from id_to_widget. */
2615 xg_get_widget_from_map (idx
)
2618 if (idx
< id_to_widget
.max_size
&& id_to_widget
.widgets
[idx
] != 0)
2619 return id_to_widget
.widgets
[idx
];
2624 /* Return the scrollbar id for X Window WID on display DPY.
2625 Return -1 if WID not in id_to_widget. */
2627 xg_get_scroll_id_for_window (dpy
, wid
)
2634 w
= xg_win_to_widget (dpy
, wid
);
2638 for (idx
= 0; idx
< id_to_widget
.max_size
; ++idx
)
2639 if (id_to_widget
.widgets
[idx
] == w
)
2646 /* Callback invoked when scroll bar WIDGET is destroyed.
2647 DATA is the index into id_to_widget for WIDGET.
2648 We free pointer to last scroll bar values here and remove the index. */
2650 xg_gtk_scroll_destroy (widget
, data
)
2657 p
= g_object_get_data (G_OBJECT (widget
), XG_LAST_SB_DATA
);
2659 xg_remove_widget_from_map (id
);
2662 /* Callback for button press/release events. Used to start timer so that
2663 the scroll bar repetition timer in GTK gets handeled.
2664 Also, sets bar->dragging to Qnil when dragging (button release) is done.
2665 WIDGET is the scroll bar widget the event is for (not used).
2666 EVENT contains the event.
2667 USER_DATA points to the struct scrollbar structure.
2669 Returns FALSE to tell GTK that it shall continue propagate the event
2672 scroll_bar_button_cb (widget
, event
, user_data
)
2674 GdkEventButton
*event
;
2677 if (event
->type
== GDK_BUTTON_PRESS
&& ! xg_timer
)
2679 else if (event
->type
== GDK_BUTTON_RELEASE
)
2681 struct scroll_bar
*bar
= (struct scroll_bar
*) user_data
;
2682 if (xg_timer
) xg_stop_timer ();
2683 bar
->dragging
= Qnil
;
2689 /* Create a scroll bar widget for frame F. Store the scroll bar
2691 SCROLL_CALLBACK is the callback to invoke when the value of the
2693 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2694 to set resources for the widget. */
2696 xg_create_scroll_bar (f
, bar
, scroll_callback
, scroll_bar_name
)
2698 struct scroll_bar
*bar
;
2699 GCallback scroll_callback
;
2700 char *scroll_bar_name
;
2707 /* Page, step increment values are not so important here, they
2708 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2709 vadj
= gtk_adjustment_new (XG_SB_MIN
, XG_SB_MIN
, XG_SB_MAX
,
2712 wscroll
= gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj
));
2713 webox
= gtk_event_box_new ();
2714 gtk_widget_set_name (wscroll
, scroll_bar_name
);
2715 gtk_range_set_update_policy (GTK_RANGE (wscroll
), GTK_UPDATE_CONTINUOUS
);
2717 scroll_id
= xg_store_widget_in_map (wscroll
);
2719 g_signal_connect (G_OBJECT (wscroll
),
2723 g_signal_connect (G_OBJECT (wscroll
),
2725 G_CALLBACK (xg_gtk_scroll_destroy
),
2726 (gpointer
) scroll_id
);
2728 /* Connect to button press and button release to detect if any scroll bar
2730 g_signal_connect (G_OBJECT (wscroll
),
2731 "button-press-event",
2732 G_CALLBACK (scroll_bar_button_cb
),
2734 g_signal_connect (G_OBJECT (wscroll
),
2735 "button-release-event",
2736 G_CALLBACK (scroll_bar_button_cb
),
2739 /* The scroll bar widget does not draw on a window of its own. Instead
2740 it draws on the parent window, in this case the edit widget. So
2741 whenever the edit widget is cleared, the scroll bar needs to redraw
2742 also, which causes flicker. Put an event box between the edit widget
2743 and the scroll bar, so the scroll bar instead draws itself on the
2744 event box window. */
2745 gtk_fixed_put (GTK_FIXED (f
->output_data
.x
->edit_widget
), webox
, -1, -1);
2746 gtk_container_add (GTK_CONTAINER (webox
), wscroll
);
2749 /* Set the cursor to an arrow. */
2750 xg_set_cursor (webox
, FRAME_X_DISPLAY_INFO (f
)->xg_cursor
);
2752 SET_SCROLL_BAR_X_WINDOW (bar
, scroll_id
);
2755 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
2757 xg_show_scroll_bar (scrollbar_id
)
2760 GtkWidget
*w
= xg_get_widget_from_map (scrollbar_id
);
2762 gtk_widget_show_all (gtk_widget_get_parent (w
));
2765 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
2767 xg_remove_scroll_bar (f
, scrollbar_id
)
2771 GtkWidget
*w
= xg_get_widget_from_map (scrollbar_id
);
2774 GtkWidget
*wparent
= gtk_widget_get_parent (w
);
2775 gtk_widget_destroy (w
);
2776 gtk_widget_destroy (wparent
);
2777 SET_FRAME_GARBAGED (f
);
2781 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2783 TOP/LEFT are the new pixel positions where the bar shall appear.
2784 WIDTH, HEIGHT is the size in pixels the bar shall have. */
2786 xg_update_scrollbar_pos (f
, scrollbar_id
, top
, left
, width
, height
)
2795 GtkWidget
*wscroll
= xg_get_widget_from_map (scrollbar_id
);
2799 GtkWidget
*wfixed
= f
->output_data
.x
->edit_widget
;
2800 GtkWidget
*wparent
= gtk_widget_get_parent (wscroll
);
2802 /* Move and resize to new values. */
2803 gtk_widget_set_size_request (wscroll
, width
, height
);
2804 gtk_fixed_move (GTK_FIXED (wfixed
), wparent
, left
, top
);
2806 SET_FRAME_GARBAGED (f
);
2807 cancel_mouse_face (f
);
2811 /* Set the thumb size and position of scroll bar BAR. We are currently
2812 displaying PORTION out of a whole WHOLE, and our position POSITION. */
2814 xg_set_toolkit_scroll_bar_thumb (bar
, portion
, position
, whole
)
2815 struct scroll_bar
*bar
;
2816 int portion
, position
, whole
;
2818 GtkWidget
*wscroll
= xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar
));
2820 FRAME_PTR f
= XFRAME (WINDOW_FRAME (XWINDOW (bar
->window
)));
2822 if (wscroll
&& NILP (bar
->dragging
))
2831 adj
= gtk_range_get_adjustment (GTK_RANGE (wscroll
));
2833 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
2834 rather than the real portion value. This makes the thumb less likely
2835 to resize and that looks better. */
2836 portion
= WINDOW_TOTAL_LINES (XWINDOW (bar
->window
)) * 30;
2837 /* When the thumb is at the bottom, position == whole.
2838 So we need to increase `whole' to make space for the thumb. */
2845 top
= (gdouble
) position
/ whole
;
2846 shown
= (gdouble
) portion
/ whole
;
2849 size
= shown
* XG_SB_RANGE
;
2850 size
= min (size
, XG_SB_RANGE
);
2851 size
= max (size
, 1);
2853 value
= top
* XG_SB_RANGE
;
2854 value
= min (value
, XG_SB_MAX
- size
);
2855 value
= max (value
, XG_SB_MIN
);
2857 /* Assume all lines are of equal size. */
2858 new_step
= size
/ max (1, FRAME_LINES (f
));
2860 if ((int) adj
->page_size
!= size
2861 || (int) adj
->step_increment
!= new_step
)
2863 adj
->page_size
= size
;
2864 adj
->step_increment
= new_step
;
2865 /* Assume a page increment is about 95% of the page size */
2866 adj
->page_increment
= (int) (0.95*adj
->page_size
);
2870 if (changed
|| (int) gtk_range_get_value (GTK_RANGE (wscroll
)) != value
)
2872 GtkWidget
*wfixed
= f
->output_data
.x
->edit_widget
;
2876 /* gtk_range_set_value invokes the callback. Set
2877 ignore_gtk_scrollbar to make the callback do nothing */
2878 xg_ignore_gtk_scrollbar
= 1;
2880 if ((int) gtk_range_get_value (GTK_RANGE (wscroll
)) != value
)
2881 gtk_range_set_value (GTK_RANGE (wscroll
), (gdouble
)value
);
2883 gtk_adjustment_changed (adj
);
2885 xg_ignore_gtk_scrollbar
= 0;
2893 /***********************************************************************
2895 ***********************************************************************/
2896 /* The key for the data we put in the GtkImage widgets. The data is
2897 the image used by Emacs. We use this to see if we need to update
2898 the GtkImage with a new image. */
2899 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
2901 /* Callback function invoked when a tool bar item is pressed.
2902 W is the button widget in the tool bar that got pressed,
2903 CLIENT_DATA is an integer that is the index of the button in the
2904 tool bar. 0 is the first button. */
2906 xg_tool_bar_callback (w
, client_data
)
2908 gpointer client_data
;
2910 int idx
= (int)client_data
;
2911 FRAME_PTR f
= (FRAME_PTR
) g_object_get_data (G_OBJECT (w
), XG_FRAME_DATA
);
2912 Lisp_Object key
, frame
;
2913 struct input_event event
;
2916 if (! f
|| ! f
->n_tool_bar_items
|| NILP (f
->tool_bar_items
))
2919 idx
*= TOOL_BAR_ITEM_NSLOTS
;
2921 key
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_KEY
);
2922 XSETFRAME (frame
, f
);
2923 event
.kind
= TOOL_BAR_EVENT
;
2924 event
.frame_or_window
= frame
;
2926 kbd_buffer_store_event (&event
);
2928 event
.kind
= TOOL_BAR_EVENT
;
2929 event
.frame_or_window
= frame
;
2931 event
.modifiers
= 0; /* These are not available. */
2932 kbd_buffer_store_event (&event
);
2935 /* This callback is called when a tool bar is detached. We must set
2936 the height of the tool bar to zero when this happens so frame sizes
2937 are correctly calculated.
2938 WBOX is the handle box widget that enables detach/attach of the tool bar.
2939 W is the tool bar widget.
2940 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
2942 xg_tool_bar_detach_callback (wbox
, w
, client_data
)
2945 gpointer client_data
;
2947 FRAME_PTR f
= (FRAME_PTR
) client_data
;
2951 /* When detaching a tool bar, not everything dissapear. There are
2952 a few pixels left that are used to drop the tool bar back into
2954 int bw
= gtk_container_get_border_width (GTK_CONTAINER (wbox
));
2955 FRAME_TOOLBAR_HEIGHT (f
) = 2;
2957 /* The height has changed, resize outer widget and set columns
2958 rows to what we had before detaching the tool bar. */
2959 xg_resize_outer_widget (f
, FRAME_COLS (f
), FRAME_LINES (f
));
2963 /* This callback is called when a tool bar is reattached. We must set
2964 the height of the tool bar when this happens so frame sizes
2965 are correctly calculated.
2966 WBOX is the handle box widget that enables detach/attach of the tool bar.
2967 W is the tool bar widget.
2968 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
2970 xg_tool_bar_attach_callback (wbox
, w
, client_data
)
2973 gpointer client_data
;
2975 FRAME_PTR f
= (FRAME_PTR
) client_data
;
2981 gtk_widget_size_request (w
, &req
);
2982 FRAME_TOOLBAR_HEIGHT (f
) = req
.height
;
2984 /* The height has changed, resize outer widget and set columns
2985 rows to what we had before detaching the tool bar. */
2986 xg_resize_outer_widget (f
, FRAME_COLS (f
), FRAME_LINES (f
));
2990 /* This callback is called when the mouse enters or leaves a tool bar item.
2991 It is used for displaying and hiding the help text.
2992 W is the tool bar item, a button.
2993 EVENT is either an enter event or leave event.
2994 CLIENT_DATA is an integer that is the index of the button in the
2995 tool bar. 0 is the first button.
2997 Returns FALSE to tell GTK to keep processing this event. */
2999 xg_tool_bar_help_callback (w
, event
, client_data
)
3001 GdkEventCrossing
*event
;
3002 gpointer client_data
;
3004 int idx
= (int)client_data
;
3005 FRAME_PTR f
= (FRAME_PTR
) g_object_get_data (G_OBJECT (w
), XG_FRAME_DATA
);
3006 Lisp_Object help
, frame
;
3008 if (! GTK_IS_BUTTON (w
))
3013 if (! f
|| ! f
->n_tool_bar_items
|| NILP (f
->tool_bar_items
))
3016 if (event
->type
== GDK_ENTER_NOTIFY
)
3018 idx
*= TOOL_BAR_ITEM_NSLOTS
;
3019 help
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_HELP
);
3022 help
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_CAPTION
);
3027 XSETFRAME (frame
, f
);
3028 kbd_buffer_store_help_event (frame
, help
);
3034 /* This callback is called when a tool bar item shall be redrawn.
3035 It modifies the expose event so that the GtkImage widget redraws the
3036 whole image. This to overcome a bug that makes GtkImage draw the image
3037 in the wrong place when it tries to redraw just a part of the image.
3038 W is the GtkImage to be redrawn.
3039 EVENT is the expose event for W.
3040 CLIENT_DATA is unused.
3042 Returns FALSE to tell GTK to keep processing this event. */
3044 xg_tool_bar_item_expose_callback (w
, event
, client_data
)
3046 GdkEventExpose
*event
;
3047 gpointer client_data
;
3051 gdk_drawable_get_size (event
->window
, &width
, &height
);
3053 event
->area
.x
-= width
> event
->area
.width
? width
-event
->area
.width
: 0;
3054 event
->area
.y
-= height
> event
->area
.height
? height
-event
->area
.height
: 0;
3056 event
->area
.x
= max (0, event
->area
.x
);
3057 event
->area
.y
= max (0, event
->area
.y
);
3059 event
->area
.width
= max (width
, event
->area
.width
);
3060 event
->area
.height
= max (height
, event
->area
.height
);
3065 /* This callback is called when a tool bar shall be redrawn.
3066 We need to update the tool bar from here in case the image cache
3067 has deleted the pixmaps used in the tool bar.
3068 W is the GtkToolbar to be redrawn.
3069 EVENT is the expose event for W.
3070 CLIENT_DATA is pointing to the frame for this tool bar.
3072 Returns FALSE to tell GTK to keep processing this event. */
3074 xg_tool_bar_expose_callback (w
, event
, client_data
)
3076 GdkEventExpose
*event
;
3077 gpointer client_data
;
3079 update_frame_tool_bar ((FRAME_PTR
) client_data
);
3084 xg_create_tool_bar (f
)
3087 struct x_output
*x
= f
->output_data
.x
;
3089 int vbox_pos
= x
->menubar_widget
? 1 : 0;
3091 x
->toolbar_widget
= gtk_toolbar_new ();
3092 x
->handlebox_widget
= gtk_handle_box_new ();
3093 gtk_container_add (GTK_CONTAINER (x
->handlebox_widget
),
3096 gtk_box_pack_start (GTK_BOX (x
->vbox_widget
), x
->handlebox_widget
,
3099 gtk_box_reorder_child (GTK_BOX (x
->vbox_widget
), x
->handlebox_widget
,
3102 gtk_widget_set_name (x
->toolbar_widget
, "emacs-toolbar");
3104 /* We only have icons, so override any user setting. We could
3105 use the caption property of the toolbar item (see update_frame_tool_bar
3106 below), but some of those strings are long, making the toolbar so
3107 long it does not fit on the screen. The GtkToolbar widget makes every
3108 item equal size, so the longest caption determine the size of every
3109 tool bar item. I think the creators of the GtkToolbar widget
3110 counted on 4 or 5 character long strings. */
3111 gtk_toolbar_set_style (GTK_TOOLBAR (x
->toolbar_widget
), GTK_TOOLBAR_ICONS
);
3112 gtk_toolbar_set_orientation (GTK_TOOLBAR (x
->toolbar_widget
),
3113 GTK_ORIENTATION_HORIZONTAL
);
3115 g_signal_connect (G_OBJECT (x
->handlebox_widget
), "child-detached",
3116 G_CALLBACK (xg_tool_bar_detach_callback
), f
);
3117 g_signal_connect (G_OBJECT (x
->handlebox_widget
), "child-attached",
3118 G_CALLBACK (xg_tool_bar_attach_callback
), f
);
3119 g_signal_connect (G_OBJECT (x
->toolbar_widget
),
3121 G_CALLBACK (xg_tool_bar_expose_callback
),
3124 gtk_widget_show_all (x
->handlebox_widget
);
3126 gtk_widget_size_request (x
->toolbar_widget
, &req
);
3127 FRAME_TOOLBAR_HEIGHT (f
) = req
.height
;
3129 /* The height has changed, resize outer widget and set columns
3130 rows to what we had before adding the tool bar. */
3131 xg_resize_outer_widget (f
, FRAME_COLS (f
), FRAME_LINES (f
));
3133 SET_FRAME_GARBAGED (f
);
3137 update_frame_tool_bar (f
)
3141 GtkRequisition old_req
, new_req
;
3144 struct x_output
*x
= f
->output_data
.x
;
3146 if (! FRAME_GTK_WIDGET (f
))
3151 if (! x
->toolbar_widget
)
3152 xg_create_tool_bar (f
);
3154 gtk_widget_size_request (x
->toolbar_widget
, &old_req
);
3156 icon_list
= gtk_container_get_children (GTK_CONTAINER (x
->toolbar_widget
));
3159 for (i
= 0; i
< f
->n_tool_bar_items
; ++i
)
3161 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3163 int enabled_p
= !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P
));
3164 int selected_p
= !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P
));
3169 GtkWidget
*wicon
= iter
? GTK_WIDGET (iter
->data
) : 0;
3171 if (iter
) iter
= g_list_next (iter
);
3173 /* If image is a vector, choose the image according to the
3175 image
= PROP (TOOL_BAR_ITEM_IMAGES
);
3176 if (VECTORP (image
))
3180 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3181 : TOOL_BAR_IMAGE_ENABLED_DESELECTED
);
3184 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3185 : TOOL_BAR_IMAGE_DISABLED_DESELECTED
);
3187 xassert (ASIZE (image
) >= idx
);
3188 image
= AREF (image
, idx
);
3193 /* Ignore invalid image specifications. */
3194 if (!valid_image_p (image
))
3196 if (wicon
) gtk_widget_hide (wicon
);
3200 img_id
= lookup_image (f
, image
);
3201 img
= IMAGE_FROM_ID (f
, img_id
);
3202 prepare_image_for_display (f
, img
);
3204 if (img
->load_failed_p
|| img
->pixmap
== None
)
3206 if (wicon
) gtk_widget_hide (wicon
);
3212 GtkWidget
*w
= xg_get_image_for_pixmap (f
, img
, x
->widget
, NULL
);
3214 gtk_toolbar_append_item (GTK_TOOLBAR (x
->toolbar_widget
),
3217 GTK_SIGNAL_FUNC (xg_tool_bar_callback
),
3220 /* Save the image so we can see if an update is needed when
3221 this function is called again. */
3222 g_object_set_data (G_OBJECT (w
), XG_TOOL_BAR_IMAGE_DATA
,
3223 (gpointer
)img
->pixmap
);
3225 /* Catch expose events to overcome an annoying redraw bug, see
3226 comment for xg_tool_bar_item_expose_callback. */
3227 g_signal_connect (G_OBJECT (w
),
3229 G_CALLBACK (xg_tool_bar_item_expose_callback
),
3232 /* We must set sensitive on the button that is the parent
3233 of the GtkImage parent. Go upwards until we find the button. */
3234 while (! GTK_IS_BUTTON (w
))
3235 w
= gtk_widget_get_parent (w
);
3239 /* Save the frame in the button so the xg_tool_bar_callback
3241 g_object_set_data (G_OBJECT (w
), XG_FRAME_DATA
, (gpointer
)f
);
3242 gtk_widget_set_sensitive (w
, enabled_p
);
3244 /* Use enter/leave notify to show help. We use the events
3245 rather than the GtkButton specific signals "enter" and
3246 "leave", so we can have only one callback. The event
3247 will tell us what kind of event it is. */
3248 g_signal_connect (G_OBJECT (w
),
3249 "enter-notify-event",
3250 G_CALLBACK (xg_tool_bar_help_callback
),
3252 g_signal_connect (G_OBJECT (w
),
3253 "leave-notify-event",
3254 G_CALLBACK (xg_tool_bar_help_callback
),
3260 /* The child of the tool bar is a button. Inside that button
3261 is a vbox. Inside that vbox is the GtkImage. */
3262 GtkWidget
*wvbox
= gtk_bin_get_child (GTK_BIN (wicon
));
3263 GList
*chlist
= gtk_container_get_children (GTK_CONTAINER (wvbox
));
3264 GtkImage
*wimage
= GTK_IMAGE (chlist
->data
);
3265 Pixmap old_img
= (Pixmap
)g_object_get_data (G_OBJECT (wimage
),
3266 XG_TOOL_BAR_IMAGE_DATA
);
3267 g_list_free (chlist
);
3269 if (old_img
!= img
->pixmap
)
3270 (void) xg_get_image_for_pixmap (f
, img
, x
->widget
, wimage
);
3272 g_object_set_data (G_OBJECT (wimage
), XG_TOOL_BAR_IMAGE_DATA
,
3273 (gpointer
)img
->pixmap
);
3275 gtk_widget_set_sensitive (wicon
, enabled_p
);
3276 gtk_widget_show (wicon
);
3282 /* Remove buttons not longer needed. We just hide them so they
3283 can be reused later on. */
3286 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
3287 gtk_widget_hide (w
);
3288 iter
= g_list_next (iter
);
3291 gtk_widget_size_request (x
->toolbar_widget
, &new_req
);
3292 if (old_req
.height
!= new_req
.height
)
3294 FRAME_TOOLBAR_HEIGHT (f
) = new_req
.height
;
3295 xg_resize_outer_widget (f
, FRAME_COLS (f
), FRAME_LINES (f
));
3298 if (icon_list
) g_list_free (icon_list
);
3304 free_frame_tool_bar (f
)
3307 struct x_output
*x
= f
->output_data
.x
;
3309 if (x
->toolbar_widget
)
3312 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
),
3313 x
->handlebox_widget
);
3314 x
->toolbar_widget
= 0;
3315 x
->handlebox_widget
= 0;
3316 FRAME_TOOLBAR_HEIGHT (f
) = 0;
3318 /* The height has changed, resize outer widget and set columns
3319 rows to what we had before removing the tool bar. */
3320 xg_resize_outer_widget (f
, FRAME_COLS (f
), FRAME_LINES (f
));
3322 SET_FRAME_GARBAGED (f
);
3329 /***********************************************************************
3331 ***********************************************************************/
3335 xg_ignore_gtk_scrollbar
= 0;
3336 xg_detached_menus
= 0;
3337 xg_menu_cb_list
.prev
= xg_menu_cb_list
.next
=
3338 xg_menu_item_cb_list
.prev
= xg_menu_item_cb_list
.next
= 0;
3340 id_to_widget
.max_size
= id_to_widget
.used
= 0;
3341 id_to_widget
.widgets
= 0;
3343 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
3344 bindings. It doesn't seem to be any way to remove properties,
3345 so we set it to VoidSymbol which in X means "no key". */
3346 gtk_settings_set_string_property (gtk_settings_get_default (),
3347 "gtk-menu-bar-accel",
3351 /* Make GTK text input widgets use Emacs style keybindings. This is
3353 gtk_settings_set_string_property (gtk_settings_get_default (),
3354 "gtk-key-theme-name",
3359 #endif /* USE_GTK */
3361 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
3362 (do not change this comment) */