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.
22 #undef GTK_DISABLE_DEPRECATED
24 #include "gtkseparatormenuitem.h"
25 #include "gtkseparatortoolitem.h"
27 #include "gtktoolbar.h"
29 /* note: keep in sync with DEFAULT_SPACE_SIZE and DEFAULT_SPACE_STYLE in gtktoolbar.c */
30 #define DEFAULT_SPACE_SIZE 4
31 #define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE
33 #define SPACE_LINE_DIVISION 10
34 #define SPACE_LINE_START 3
35 #define SPACE_LINE_END 7
37 #define MENU_ID "gtk-separator-tool-item-menu-id"
44 static void gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class);
45 static void gtk_separator_tool_item_init (GtkSeparatorToolItem *separator_item,
46 GtkSeparatorToolItemClass *class);
47 static gboolean gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item);
48 static void gtk_separator_tool_item_set_property (GObject *object,
52 static void gtk_separator_tool_item_get_property (GObject *object,
56 static void gtk_separator_tool_item_size_request (GtkWidget *widget,
57 GtkRequisition *requisition);
58 static gboolean gtk_separator_tool_item_expose (GtkWidget *widget,
59 GdkEventExpose *event);
60 static void gtk_separator_tool_item_add (GtkContainer *container,
62 static GtkToolbarSpaceStyle get_space_style (GtkToolItem *tool_item);
63 static gint get_space_size (GtkToolItem *tool_item);
67 static GObjectClass *parent_class = NULL;
69 #define GTK_SEPARATOR_TOOL_ITEM_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SEPARATOR_TOOL_ITEM, GtkSeparatorToolItemPrivate))
71 struct _GtkSeparatorToolItemPrivate
77 gtk_separator_tool_item_get_type (void)
79 static GType type = 0;
83 static const GTypeInfo type_info =
85 sizeof (GtkSeparatorToolItemClass),
87 (GBaseFinalizeFunc) 0,
88 (GClassInitFunc) gtk_separator_tool_item_class_init,
89 (GClassFinalizeFunc) 0,
91 sizeof (GtkSeparatorToolItem),
93 (GInstanceInitFunc) gtk_separator_tool_item_init,
96 type = g_type_register_static (GTK_TYPE_TOOL_ITEM,
97 "GtkSeparatorToolItem", &type_info, 0);
102 static GtkToolbarSpaceStyle
103 get_space_style (GtkToolItem *tool_item)
105 GtkToolbarSpaceStyle space_style = DEFAULT_SPACE_STYLE;
106 GtkWidget *parent = GTK_WIDGET (tool_item)->parent;
108 if (GTK_IS_TOOLBAR (parent))
110 gtk_widget_style_get (parent,
111 "space_style", &space_style,
119 get_space_size (GtkToolItem *tool_item)
121 gint space_size = DEFAULT_SPACE_SIZE;
122 GtkWidget *parent = GTK_WIDGET (tool_item)->parent;
124 if (GTK_IS_TOOLBAR (parent))
126 gtk_widget_style_get (parent,
127 "space_size", &space_size,
135 gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
137 GObjectClass *object_class;
138 GtkContainerClass *container_class;
139 GtkToolItemClass *toolitem_class;
140 GtkWidgetClass *widget_class;
142 parent_class = g_type_class_peek_parent (class);
143 object_class = (GObjectClass *)class;
144 container_class = (GtkContainerClass *)class;
145 toolitem_class = (GtkToolItemClass *)class;
146 widget_class = (GtkWidgetClass *)class;
148 object_class->set_property = gtk_separator_tool_item_set_property;
149 object_class->get_property = gtk_separator_tool_item_get_property;
150 widget_class->size_request = gtk_separator_tool_item_size_request;
151 widget_class->expose_event = gtk_separator_tool_item_expose;
152 toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
154 container_class->add = gtk_separator_tool_item_add;
156 g_object_class_install_property (object_class,
158 g_param_spec_boolean ("draw",
160 _("Whether the separator is drawn, or just blank"),
164 g_type_class_add_private (object_class, sizeof (GtkSeparatorToolItemPrivate));
168 gtk_separator_tool_item_init (GtkSeparatorToolItem *separator_item,
169 GtkSeparatorToolItemClass *class)
171 separator_item->priv = GTK_SEPARATOR_TOOL_ITEM_GET_PRIVATE (separator_item);
172 separator_item->priv->draw = TRUE;
176 gtk_separator_tool_item_add (GtkContainer *container,
179 g_warning("attempt to add a child to an GtkSeparatorToolItem");
183 gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
185 GtkWidget *menu_item = NULL;
187 menu_item = gtk_separator_menu_item_new();
189 gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
195 gtk_separator_tool_item_set_property (GObject *object,
200 GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
205 gtk_separator_tool_item_set_draw (item, g_value_get_boolean (value));
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 gtk_separator_tool_item_get_property (GObject *object,
219 GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
224 g_value_set_boolean (value, gtk_separator_tool_item_get_draw (item));
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233 gtk_separator_tool_item_size_request (GtkWidget *widget,
234 GtkRequisition *requisition)
236 GtkToolItem *item = GTK_TOOL_ITEM (widget);
237 GtkOrientation orientation = gtk_tool_item_get_orientation (item);
239 if (orientation == GTK_ORIENTATION_HORIZONTAL)
241 requisition->width = get_space_size (item);
242 requisition->height = 1;
246 requisition->height = get_space_size (item);
247 requisition->width = 1;
252 gtk_separator_tool_item_expose (GtkWidget *widget,
253 GdkEventExpose *event)
255 GtkToolItem *tool_item = GTK_TOOL_ITEM (widget);
256 GtkSeparatorToolItem *separator_tool_item = GTK_SEPARATOR_TOOL_ITEM (widget);
258 GtkAllocation *allocation;
259 GtkOrientation orientation;
262 if (separator_tool_item->priv->draw &&
263 get_space_style (tool_item) == GTK_TOOLBAR_SPACE_LINE)
265 space_size = get_space_size (tool_item);
266 allocation = &(widget->allocation);
267 orientation = gtk_tool_item_get_orientation (tool_item);
268 area = &(event->area);
270 if (orientation == GTK_ORIENTATION_HORIZONTAL)
272 gtk_paint_vline (widget->style, widget->window,
273 GTK_WIDGET_STATE (widget), area, widget,
274 "separator_tool_item",
275 allocation->y + allocation->height *
276 SPACE_LINE_START / SPACE_LINE_DIVISION,
277 allocation->y + allocation->height *
278 SPACE_LINE_END / SPACE_LINE_DIVISION,
279 allocation->x + (space_size - widget->style->xthickness) / 2);
281 else if (orientation == GTK_ORIENTATION_VERTICAL)
283 gtk_paint_hline (widget->style, widget->window,
284 GTK_WIDGET_STATE (widget), area, widget,
285 "separator_tool_item",
286 allocation->x + allocation->width *
287 SPACE_LINE_START / SPACE_LINE_DIVISION,
288 allocation->x + allocation->width *
289 SPACE_LINE_END / SPACE_LINE_DIVISION,
290 allocation->y + (space_size - widget->style->ythickness) / 2);
298 * gtk_separator_tool_item_new:
300 * Create a new #GtkSeparatorToolItem
302 * Return value: the new #GtkSeparatorToolItem
307 gtk_separator_tool_item_new (void)
311 self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
318 * gtk_separator_tool_item_get_draw:
319 * @separator_tool_item: a #GtkSeparatorToolItem
321 * Returns whether @separator_tool_item is drawn as a
322 * line, or just blank. See gtk_separator_tool_item_set_draw().
324 * Return value: #TRUE if @separator_tool_item is drawn as a line, or just blank.
329 gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *separator_tool_item)
331 g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (separator_tool_item), FALSE);
333 return separator_tool_item->priv->draw;
337 * gtk_separator_tool_item_set_draw:
338 * @separator_tool_item: a #GtkSeparatorToolItem
339 * @draw: whether @separator_tool_item is drawn as a vertical iln
341 * When @separator_tool_items is drawn as a vertical line, or just blank.
342 * Setting this #FALSE along with gtk_tool_item_set_expand() is useful
343 * to create an item that forces following items to the end of the toolbar.
348 gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *separator_tool_item,
351 g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (separator_tool_item));
353 draw = draw != FALSE;
355 if (draw != separator_tool_item->priv->draw)
357 separator_tool_item->priv->draw = draw;
359 gtk_widget_queue_draw (GTK_WIDGET (separator_tool_item));
361 g_object_notify (G_OBJECT (separator_tool_item), "draw");