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, see <http://www.gnu.org/licenses/>.
21 #include "gtkseparatormenuitem.h"
22 #include "gtkseparatortoolitem.h"
24 #include "gtktoolbar.h"
25 #include "gtkprivate.h"
28 * SECTION:gtkseparatortoolitem
29 * @Short_description: A toolbar item that separates groups of other
31 * @Title: GtkSeparatorToolItem
32 * @See_also: #GtkToolbar, #GtkRadioToolButton
34 * A #GtkSeparatorToolItem is a #GtkToolItem that separates groups of other
35 * #GtkToolItems. Depending on the theme, a #GtkSeparatorToolItem will
36 * often look like a vertical line on horizontally docked toolbars.
38 * If the #GtkToolbar child property "expand" is %TRUE and the property
39 * #GtkSeparatorToolItem:draw is %FALSE, a #GtkSeparatorToolItem will act as
40 * a "spring" that forces other items to the ends of the toolbar.
42 * Use gtk_separator_tool_item_new() to create a new #GtkSeparatorToolItem.
45 #define MENU_ID "gtk-separator-tool-item-menu-id"
47 struct _GtkSeparatorToolItemPrivate
49 GdkWindow *event_window;
58 static gboolean gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item);
59 static void gtk_separator_tool_item_set_property (GObject *object,
63 static void gtk_separator_tool_item_get_property (GObject *object,
67 static void gtk_separator_tool_item_get_preferred_width (GtkWidget *widget,
70 static void gtk_separator_tool_item_get_preferred_height (GtkWidget *widget,
73 static void gtk_separator_tool_item_size_allocate (GtkWidget *widget,
74 GtkAllocation *allocation);
75 static gboolean gtk_separator_tool_item_draw (GtkWidget *widget,
77 static void gtk_separator_tool_item_add (GtkContainer *container,
79 static gint get_space_size (GtkToolItem *tool_item);
80 static void gtk_separator_tool_item_realize (GtkWidget *widget);
81 static void gtk_separator_tool_item_unrealize (GtkWidget *widget);
82 static void gtk_separator_tool_item_map (GtkWidget *widget);
83 static void gtk_separator_tool_item_unmap (GtkWidget *widget);
84 static gboolean gtk_separator_tool_item_button_event (GtkWidget *widget,
85 GdkEventButton *event);
88 G_DEFINE_TYPE (GtkSeparatorToolItem, gtk_separator_tool_item, GTK_TYPE_TOOL_ITEM)
91 get_space_size (GtkToolItem *tool_item)
93 gint space_size = _gtk_toolbar_get_default_space_size();
96 parent = gtk_widget_get_parent (GTK_WIDGET (tool_item));
98 if (GTK_IS_TOOLBAR (parent))
100 gtk_widget_style_get (parent,
101 "space-size", &space_size,
109 gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
111 GObjectClass *object_class;
112 GtkContainerClass *container_class;
113 GtkToolItemClass *toolitem_class;
114 GtkWidgetClass *widget_class;
116 object_class = (GObjectClass *)class;
117 container_class = (GtkContainerClass *)class;
118 toolitem_class = (GtkToolItemClass *)class;
119 widget_class = (GtkWidgetClass *)class;
121 object_class->set_property = gtk_separator_tool_item_set_property;
122 object_class->get_property = gtk_separator_tool_item_get_property;
123 widget_class->get_preferred_width = gtk_separator_tool_item_get_preferred_width;
124 widget_class->get_preferred_height = gtk_separator_tool_item_get_preferred_height;
125 widget_class->size_allocate = gtk_separator_tool_item_size_allocate;
126 widget_class->draw = gtk_separator_tool_item_draw;
127 widget_class->realize = gtk_separator_tool_item_realize;
128 widget_class->unrealize = gtk_separator_tool_item_unrealize;
129 widget_class->map = gtk_separator_tool_item_map;
130 widget_class->unmap = gtk_separator_tool_item_unmap;
131 widget_class->button_press_event = gtk_separator_tool_item_button_event;
132 widget_class->button_release_event = gtk_separator_tool_item_button_event;
134 toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
136 container_class->add = gtk_separator_tool_item_add;
138 g_object_class_install_property (object_class,
140 g_param_spec_boolean ("draw",
142 P_("Whether the separator is drawn, or just blank"),
144 GTK_PARAM_READWRITE));
147 g_type_class_add_private (object_class, sizeof (GtkSeparatorToolItemPrivate));
151 gtk_separator_tool_item_init (GtkSeparatorToolItem *separator_item)
153 GtkStyleContext *context;
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);
162 context = gtk_widget_get_style_context (GTK_WIDGET (separator_item));
163 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SEPARATOR);
167 gtk_separator_tool_item_add (GtkContainer *container,
170 g_warning ("attempt to add a child to an GtkSeparatorToolItem");
174 gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
176 GtkWidget *menu_item = NULL;
178 menu_item = gtk_separator_menu_item_new();
180 gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
186 gtk_separator_tool_item_set_property (GObject *object,
191 GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
196 gtk_separator_tool_item_set_draw (item, g_value_get_boolean (value));
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 gtk_separator_tool_item_get_property (GObject *object,
210 GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
215 g_value_set_boolean (value, gtk_separator_tool_item_get_draw (item));
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 gtk_separator_tool_item_get_preferred_size (GtkWidget *widget,
225 GtkOrientation orientation,
229 if (gtk_tool_item_get_orientation (GTK_TOOL_ITEM (widget)) == orientation)
230 *minimum = *natural = get_space_size (GTK_TOOL_ITEM (widget));
232 *minimum = *natural = 1;
236 gtk_separator_tool_item_get_preferred_width (GtkWidget *widget,
240 gtk_separator_tool_item_get_preferred_size (widget,
241 GTK_ORIENTATION_HORIZONTAL,
247 gtk_separator_tool_item_get_preferred_height (GtkWidget *widget,
251 gtk_separator_tool_item_get_preferred_size (widget,
252 GTK_ORIENTATION_VERTICAL,
258 gtk_separator_tool_item_size_allocate (GtkWidget *widget,
259 GtkAllocation *allocation)
261 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
262 GtkSeparatorToolItemPrivate *priv = separator->priv;
264 gtk_widget_set_allocation (widget, allocation);
266 if (gtk_widget_get_realized (widget))
267 gdk_window_move_resize (priv->event_window,
276 gtk_separator_tool_item_realize (GtkWidget *widget)
278 GtkAllocation allocation;
279 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
280 GtkSeparatorToolItemPrivate *priv = separator->priv;
282 GdkWindowAttr attributes;
283 gint attributes_mask;
285 gtk_widget_set_realized (widget, TRUE);
287 gtk_widget_get_allocation (widget, &allocation);
289 attributes.window_type = GDK_WINDOW_CHILD;
290 attributes.x = allocation.x;
291 attributes.y = allocation.y;
292 attributes.width = allocation.width;
293 attributes.height = allocation.height;
294 attributes.wclass = GDK_INPUT_ONLY;
295 attributes.visual = gtk_widget_get_visual (widget);
296 attributes.event_mask = gtk_widget_get_events (widget) |
297 GDK_BUTTON_PRESS_MASK |
298 GDK_BUTTON_RELEASE_MASK;
299 attributes_mask = GDK_WA_X | GDK_WA_Y;
301 window = gtk_widget_get_parent_window (widget);
302 gtk_widget_set_window (widget, window);
303 g_object_ref (window);
305 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
306 &attributes, attributes_mask);
307 gdk_window_set_user_data (priv->event_window, widget);
311 gtk_separator_tool_item_unrealize (GtkWidget *widget)
313 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
314 GtkSeparatorToolItemPrivate *priv = separator->priv;
316 if (priv->event_window)
318 gdk_window_set_user_data (priv->event_window, NULL);
319 gdk_window_destroy (priv->event_window);
320 priv->event_window = NULL;
323 GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->unrealize (widget);
327 gtk_separator_tool_item_map (GtkWidget *widget)
329 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
330 GtkSeparatorToolItemPrivate *priv = separator->priv;
332 GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->map (widget);
334 if (priv->event_window)
335 gdk_window_show (priv->event_window);
339 gtk_separator_tool_item_unmap (GtkWidget *widget)
341 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
342 GtkSeparatorToolItemPrivate *priv = separator->priv;
344 if (priv->event_window)
345 gdk_window_hide (priv->event_window);
347 GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->unmap (widget);
351 gtk_separator_tool_item_button_event (GtkWidget *widget,
352 GdkEventButton *event)
354 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
355 GtkSeparatorToolItemPrivate *priv = separator->priv;
357 /* We want window dragging to work on empty toolbar areas,
358 * so we only eat button events on visible separators
364 gtk_separator_tool_item_draw (GtkWidget *widget,
367 GtkAllocation allocation;
368 GtkToolbar *toolbar = NULL;
369 GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
370 GtkSeparatorToolItemPrivate *priv = separator->priv;
375 parent = gtk_widget_get_parent (widget);
376 if (GTK_IS_TOOLBAR (parent))
377 toolbar = GTK_TOOLBAR (parent);
379 gtk_widget_get_allocation (widget, &allocation);
380 _gtk_toolbar_paint_space_line (widget, toolbar, cr);
387 * gtk_separator_tool_item_new:
389 * Create a new #GtkSeparatorToolItem
391 * Return value: the new #GtkSeparatorToolItem
396 gtk_separator_tool_item_new (void)
400 self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
407 * gtk_separator_tool_item_get_draw:
408 * @item: a #GtkSeparatorToolItem
410 * Returns whether @item is drawn as a line, or just blank.
411 * See gtk_separator_tool_item_set_draw().
413 * Return value: %TRUE if @item is drawn as a line, or just blank.
418 gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
420 g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
422 return item->priv->draw;
426 * gtk_separator_tool_item_set_draw:
427 * @item: a #GtkSeparatorToolItem
428 * @draw: whether @item is drawn as a vertical line
430 * Whether @item is drawn as a vertical line, or just blank.
431 * Setting this to %FALSE along with gtk_tool_item_set_expand() is useful
432 * to create an item that forces following items to the end of the toolbar.
437 gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *item,
440 g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item));
442 draw = draw != FALSE;
444 if (draw != item->priv->draw)
446 item->priv->draw = draw;
448 gtk_widget_queue_draw (GTK_WIDGET (item));
450 g_object_notify (G_OBJECT (item), "draw");