1 /* gtkseparatortoolitem.c
3 * Copyright (C) 2002 Anders Carlsson <andersca@gnome.org>
4 * Copyright (C) 2002 James Henstridge <james@daa.com.au>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "gtkseparatormenuitem.h"
24 #include "gtkseparatortoolitem.h"
26 #include "gtktoolbar.h"
27 #include "gtkprivate.h"
30 * SECTION:gtkseparatortoolitem
31 * @Short_description: A toolbar item that separates groups of other
33 * @Title: GtkSeparatorToolItem
34 * @See_also: #GtkToolbar, #GtkRadioToolButton
36 * A #GtkSeparatorItem is a #GtkToolItem that separates groups of other
37 * #GtkToolItems. Depending on the theme, a #GtkSeparatorToolItem will
38 * often look like a vertical line on horizontally docked toolbars.
40 * If the #GtkToolbar child property "expand" is %TRUE and the property
41 * #GtkSeparatorToolItem:draw is %FALSE, a #GtkSeparatorToolItem will act as
42 * a "spring" that forces other items to the ends of the toolbar.
44 * Use gtk_separator_tool_item_new() to create a new #GtkSeparatorToolItem.
47 #define MENU_ID "gtk-separator-tool-item-menu-id"
49 struct _GtkSeparatorToolItemPrivate
51 GdkWindow *event_window;
60 static gboolean gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item);
61 static void gtk_separator_tool_item_set_property (GObject *object,
65 static void gtk_separator_tool_item_get_property (GObject *object,
69 static void gtk_separator_tool_item_get_preferred_width (GtkWidget *widget,
72 static void gtk_separator_tool_item_get_preferred_height (GtkWidget *widget,
75 static void gtk_separator_tool_item_size_allocate (GtkWidget *widget,
76 GtkAllocation *allocation);
77 static gboolean gtk_separator_tool_item_draw (GtkWidget *widget,
79 static void gtk_separator_tool_item_add (GtkContainer *container,
81 static gint get_space_size (GtkToolItem *tool_item);
82 static void gtk_separator_tool_item_realize (GtkWidget *widget);
83 static void gtk_separator_tool_item_unrealize (GtkWidget *widget);
84 static void gtk_separator_tool_item_map (GtkWidget *widget);
85 static void gtk_separator_tool_item_unmap (GtkWidget *widget);
86 static gboolean gtk_separator_tool_item_button_event (GtkWidget *widget,
87 GdkEventButton *event);
90 G_DEFINE_TYPE (GtkSeparatorToolItem, gtk_separator_tool_item, GTK_TYPE_TOOL_ITEM)
93 get_space_size (GtkToolItem *tool_item)
95 gint space_size = _gtk_toolbar_get_default_space_size();
98 parent = gtk_widget_get_parent (GTK_WIDGET (tool_item));
100 if (GTK_IS_TOOLBAR (parent))
102 gtk_widget_style_get (parent,
103 "space-size", &space_size,
111 gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
113 GObjectClass *object_class;
114 GtkContainerClass *container_class;
115 GtkToolItemClass *toolitem_class;
116 GtkWidgetClass *widget_class;
118 object_class = (GObjectClass *)class;
119 container_class = (GtkContainerClass *)class;
120 toolitem_class = (GtkToolItemClass *)class;
121 widget_class = (GtkWidgetClass *)class;
123 object_class->set_property = gtk_separator_tool_item_set_property;
124 object_class->get_property = gtk_separator_tool_item_get_property;
125 widget_class->get_preferred_width = gtk_separator_tool_item_get_preferred_width;
126 widget_class->get_preferred_height = gtk_separator_tool_item_get_preferred_height;
127 widget_class->size_allocate = gtk_separator_tool_item_size_allocate;
128 widget_class->draw = gtk_separator_tool_item_draw;
129 widget_class->realize = gtk_separator_tool_item_realize;
130 widget_class->unrealize = gtk_separator_tool_item_unrealize;
131 widget_class->map = gtk_separator_tool_item_map;
132 widget_class->unmap = gtk_separator_tool_item_unmap;
133 widget_class->button_press_event = gtk_separator_tool_item_button_event;
134 widget_class->button_release_event = gtk_separator_tool_item_button_event;
136 toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
138 container_class->add = gtk_separator_tool_item_add;
140 g_object_class_install_property (object_class,
142 g_param_spec_boolean ("draw",
144 P_("Whether the separator is drawn, or just blank"),
146 GTK_PARAM_READWRITE));
149 g_type_class_add_private (object_class, sizeof (GtkSeparatorToolItemPrivate));
153 gtk_separator_tool_item_init (GtkSeparatorToolItem *separator_item)
155 separator_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (separator_item,
156 GTK_TYPE_SEPARATOR_TOOL_ITEM,
157 GtkSeparatorToolItemPrivate);
158 separator_item->priv->draw = TRUE;
160 gtk_widget_set_has_window (GTK_WIDGET (separator_item), FALSE);
164 gtk_separator_tool_item_add (GtkContainer *container,
167 g_warning ("attempt to add a child to an GtkSeparatorToolItem");
171 gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
173 GtkWidget *menu_item = NULL;
175 menu_item = gtk_separator_menu_item_new();
177 gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
183 gtk_separator_tool_item_set_property (GObject *object,
188 GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
193 gtk_separator_tool_item_set_draw (item, g_value_get_boolean (value));
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 gtk_separator_tool_item_get_property (GObject *object,
207 GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
212 g_value_set_boolean (value, gtk_separator_tool_item_get_draw (item));
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 gtk_separator_tool_item_get_preferred_size (GtkWidget *widget,
222 GtkOrientation orientation,
226 if (gtk_tool_item_get_orientation (GTK_TOOL_ITEM (widget)) == orientation)
227 *minimum = *natural = get_space_size (GTK_TOOL_ITEM (widget));
229 *minimum = *natural = 1;
233 gtk_separator_tool_item_get_preferred_width (GtkWidget *widget,
237 gtk_separator_tool_item_get_preferred_size (widget,
238 GTK_ORIENTATION_HORIZONTAL,
244 gtk_separator_tool_item_get_preferred_height (GtkWidget *widget,
248 gtk_separator_tool_item_get_preferred_size (widget,
249 GTK_ORIENTATION_VERTICAL,
255 gtk_separator_tool_item_size_allocate (GtkWidget *widget,
256 GtkAllocation *allocation)
258 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
259 GtkSeparatorToolItemPrivate *priv = separator->priv;
261 gtk_widget_set_allocation (widget, allocation);
263 if (gtk_widget_get_realized (widget))
264 gdk_window_move_resize (priv->event_window,
273 gtk_separator_tool_item_realize (GtkWidget *widget)
275 GtkAllocation allocation;
276 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
277 GtkSeparatorToolItemPrivate *priv = separator->priv;
279 GdkWindowAttr attributes;
280 gint attributes_mask;
282 gtk_widget_set_realized (widget, TRUE);
284 gtk_widget_get_allocation (widget, &allocation);
286 attributes.window_type = GDK_WINDOW_CHILD;
287 attributes.x = allocation.x;
288 attributes.y = allocation.y;
289 attributes.width = allocation.width;
290 attributes.height = allocation.height;
291 attributes.wclass = GDK_INPUT_ONLY;
292 attributes.visual = gtk_widget_get_visual (widget);
293 attributes.event_mask = gtk_widget_get_events (widget) |
294 GDK_BUTTON_PRESS_MASK |
295 GDK_BUTTON_RELEASE_MASK;
296 attributes_mask = GDK_WA_X | GDK_WA_Y;
298 window = gtk_widget_get_parent_window (widget);
299 gtk_widget_set_window (widget, window);
300 g_object_ref (window);
302 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
303 &attributes, attributes_mask);
304 gdk_window_set_user_data (priv->event_window, widget);
306 gtk_widget_style_attach (widget);
310 gtk_separator_tool_item_unrealize (GtkWidget *widget)
312 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
313 GtkSeparatorToolItemPrivate *priv = separator->priv;
315 if (priv->event_window)
317 gdk_window_set_user_data (priv->event_window, NULL);
318 gdk_window_destroy (priv->event_window);
319 priv->event_window = NULL;
322 GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->unrealize (widget);
326 gtk_separator_tool_item_map (GtkWidget *widget)
328 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
329 GtkSeparatorToolItemPrivate *priv = separator->priv;
331 GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->map (widget);
333 if (priv->event_window)
334 gdk_window_show (priv->event_window);
338 gtk_separator_tool_item_unmap (GtkWidget *widget)
340 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
341 GtkSeparatorToolItemPrivate *priv = separator->priv;
343 if (priv->event_window)
344 gdk_window_hide (priv->event_window);
346 GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->unmap (widget);
350 gtk_separator_tool_item_button_event (GtkWidget *widget,
351 GdkEventButton *event)
353 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
354 GtkSeparatorToolItemPrivate *priv = separator->priv;
356 /* We want window dragging to work on empty toolbar areas,
357 * so we only eat button events on visible separators
363 gtk_separator_tool_item_draw (GtkWidget *widget,
366 GtkAllocation allocation;
367 GtkToolbar *toolbar = NULL;
368 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
369 GtkSeparatorToolItemPrivate *priv = separator->priv;
374 parent = gtk_widget_get_parent (widget);
375 if (GTK_IS_TOOLBAR (parent))
376 toolbar = GTK_TOOLBAR (parent);
378 gtk_widget_get_allocation (widget, &allocation);
379 _gtk_toolbar_paint_space_line (widget, toolbar, cr);
386 * gtk_separator_tool_item_new:
388 * Create a new #GtkSeparatorToolItem
390 * Return value: the new #GtkSeparatorToolItem
395 gtk_separator_tool_item_new (void)
399 self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
406 * gtk_separator_tool_item_get_draw:
407 * @item: a #GtkSeparatorToolItem
409 * Returns whether @item is drawn as a line, or just blank.
410 * See gtk_separator_tool_item_set_draw().
412 * Return value: %TRUE if @item is drawn as a line, or just blank.
417 gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
419 g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
421 return item->priv->draw;
425 * gtk_separator_tool_item_set_draw:
426 * @item: a #GtkSeparatorToolItem
427 * @draw: whether @item is drawn as a vertical line
429 * Whether @item is drawn as a vertical line, or just blank.
430 * Setting this to %FALSE along with gtk_tool_item_set_expand() is useful
431 * to create an item that forces following items to the end of the toolbar.
436 gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *item,
439 g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item));
441 draw = draw != FALSE;
443 if (draw != item->priv->draw)
445 item->priv->draw = draw;
447 gtk_widget_queue_draw (GTK_WIDGET (item));
449 g_object_notify (G_OBJECT (item), "draw");