]> Pileus Git - ~andy/gtk/blob - gtk/gtkseparatortoolitem.c
54f72dfbe4d374c5a753412361b566b6e02942ed
[~andy/gtk] / gtk / gtkseparatortoolitem.c
1 /* gtkseparatortoolitem.c
2  *
3  * Copyright (C) 2002 Anders Carlsson <andersca@gnome.org>
4  * Copyright (C) 2002 James Henstridge <james@daa.com.au>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23 #include "gtkseparatormenuitem.h"
24 #include "gtkseparatortoolitem.h"
25 #include "gtkintl.h"
26 #include "gtktoolbar.h"
27 #include "gtkprivate.h"
28
29 /**
30  * SECTION:gtkseparatortoolitem
31  * @Short_description: A toolbar item that separates groups of other
32  *   toolbar items
33  * @Title: GtkSeparatorToolItem
34  * @See_also: #GtkToolbar, #GtkRadioToolButton
35  *
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.
39  *
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.
43  *
44  * Use gtk_separator_tool_item_new() to create a new #GtkSeparatorToolItem.
45  */
46
47
48 #define MENU_ID "gtk-separator-tool-item-menu-id"
49
50 struct _GtkSeparatorToolItemPrivate
51 {
52   guint draw : 1;
53 };
54
55 enum {
56   PROP_0,
57   PROP_DRAW
58 };
59
60 static gboolean gtk_separator_tool_item_create_menu_proxy (GtkToolItem               *item);
61 static void     gtk_separator_tool_item_set_property      (GObject                   *object,
62                                                            guint                      prop_id,
63                                                            const GValue              *value,
64                                                            GParamSpec                *pspec);
65 static void     gtk_separator_tool_item_get_property       (GObject                   *object,
66                                                            guint                      prop_id,
67                                                            GValue                    *value,
68                                                            GParamSpec                *pspec);
69 static void     gtk_separator_tool_item_size_request      (GtkWidget                 *widget,
70                                                            GtkRequisition            *requisition);
71 static gboolean gtk_separator_tool_item_expose            (GtkWidget                 *widget,
72                                                            GdkEventExpose            *event);
73 static void     gtk_separator_tool_item_add               (GtkContainer              *container,
74                                                            GtkWidget                 *child);
75 static gint     get_space_size                            (GtkToolItem               *tool_item);
76
77
78 G_DEFINE_TYPE (GtkSeparatorToolItem, gtk_separator_tool_item, GTK_TYPE_TOOL_ITEM)
79
80 static gint
81 get_space_size (GtkToolItem *tool_item)
82 {
83   gint space_size = _gtk_toolbar_get_default_space_size();
84   GtkWidget *parent = GTK_WIDGET (tool_item)->parent;
85   
86   if (GTK_IS_TOOLBAR (parent))
87     {
88       gtk_widget_style_get (parent,
89                             "space-size", &space_size,
90                             NULL);
91     }
92   
93   return space_size;
94 }
95
96 static void
97 gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
98 {
99   GObjectClass *object_class;
100   GtkContainerClass *container_class;
101   GtkToolItemClass *toolitem_class;
102   GtkWidgetClass *widget_class;
103   
104   object_class = (GObjectClass *)class;
105   container_class = (GtkContainerClass *)class;
106   toolitem_class = (GtkToolItemClass *)class;
107   widget_class = (GtkWidgetClass *)class;
108
109   object_class->set_property = gtk_separator_tool_item_set_property;
110   object_class->get_property = gtk_separator_tool_item_get_property;
111   widget_class->size_request = gtk_separator_tool_item_size_request;
112   widget_class->expose_event = gtk_separator_tool_item_expose;
113   toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
114   
115   container_class->add = gtk_separator_tool_item_add;
116   
117   g_object_class_install_property (object_class,
118                                    PROP_DRAW,
119                                    g_param_spec_boolean ("draw",
120                                                          P_("Draw"),
121                                                          P_("Whether the separator is drawn, or just blank"),
122                                                          TRUE,
123                                                          GTK_PARAM_READWRITE));
124   
125   g_type_class_add_private (object_class, sizeof (GtkSeparatorToolItemPrivate));
126 }
127
128 static void
129 gtk_separator_tool_item_init (GtkSeparatorToolItem      *separator_item)
130 {
131   separator_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (separator_item,
132                                                       GTK_TYPE_SEPARATOR_TOOL_ITEM,
133                                                       GtkSeparatorToolItemPrivate);
134   separator_item->priv->draw = TRUE;
135 }
136
137 static void
138 gtk_separator_tool_item_add (GtkContainer *container,
139                              GtkWidget    *child)
140 {
141   g_warning ("attempt to add a child to an GtkSeparatorToolItem");
142 }
143
144 static gboolean
145 gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
146 {
147   GtkWidget *menu_item = NULL;
148   
149   menu_item = gtk_separator_menu_item_new();
150   
151   gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
152   
153   return TRUE;
154 }
155
156 static void
157 gtk_separator_tool_item_set_property (GObject      *object,
158                                       guint         prop_id,
159                                       const GValue *value,
160                                       GParamSpec   *pspec)
161 {
162   GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
163   
164   switch (prop_id)
165     {
166     case PROP_DRAW:
167       gtk_separator_tool_item_set_draw (item, g_value_get_boolean (value));
168       break;
169     default:
170       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171       break;
172     }
173 }
174
175 static void
176 gtk_separator_tool_item_get_property (GObject      *object,
177                                       guint         prop_id,
178                                       GValue       *value,
179                                       GParamSpec   *pspec)
180 {
181   GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
182   
183   switch (prop_id)
184     {
185     case PROP_DRAW:
186       g_value_set_boolean (value, gtk_separator_tool_item_get_draw (item));
187       break;
188     default:
189       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190       break;
191     }
192 }
193
194 static void
195 gtk_separator_tool_item_size_request (GtkWidget      *widget,
196                                       GtkRequisition *requisition)
197 {
198   GtkToolItem *item = GTK_TOOL_ITEM (widget);
199   GtkOrientation orientation = gtk_tool_item_get_orientation (item);
200   
201   if (orientation == GTK_ORIENTATION_HORIZONTAL)
202     {
203       requisition->width = get_space_size (item);
204       requisition->height = 1;
205     }
206   else
207     {
208       requisition->height = get_space_size (item);
209       requisition->width = 1;
210     }
211 }
212
213 static gboolean
214 gtk_separator_tool_item_expose (GtkWidget      *widget,
215                                 GdkEventExpose *event)
216 {
217   GtkToolbar *toolbar = NULL;
218   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
219   GtkSeparatorToolItemPrivate *priv = separator->priv;
220
221   if (priv->draw)
222     {
223       if (GTK_IS_TOOLBAR (widget->parent))
224         toolbar = GTK_TOOLBAR (widget->parent);
225
226       _gtk_toolbar_paint_space_line (widget, toolbar,
227                                      &(event->area), &widget->allocation);
228     }
229   
230   return FALSE;
231 }
232
233 /**
234  * gtk_separator_tool_item_new:
235  * 
236  * Create a new #GtkSeparatorToolItem
237  * 
238  * Return value: the new #GtkSeparatorToolItem
239  * 
240  * Since: 2.4
241  */
242 GtkToolItem *
243 gtk_separator_tool_item_new (void)
244 {
245   GtkToolItem *self;
246   
247   self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
248                        NULL);
249   
250   return self;
251 }
252
253 /**
254  * gtk_separator_tool_item_get_draw:
255  * @item: a #GtkSeparatorToolItem 
256  * 
257  * Returns whether @item is drawn as a line, or just blank. 
258  * See gtk_separator_tool_item_set_draw().
259  * 
260  * Return value: %TRUE if @item is drawn as a line, or just blank.
261  * 
262  * Since: 2.4
263  */
264 gboolean
265 gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
266 {
267   g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
268   
269   return item->priv->draw;
270 }
271
272 /**
273  * gtk_separator_tool_item_set_draw:
274  * @item: a #GtkSeparatorToolItem
275  * @draw: whether @item is drawn as a vertical line
276  * 
277  * Whether @item is drawn as a vertical line, or just blank.
278  * Setting this to %FALSE along with gtk_tool_item_set_expand() is useful
279  * to create an item that forces following items to the end of the toolbar.
280  * 
281  * Since: 2.4
282  */
283 void
284 gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *item,
285                                   gboolean              draw)
286 {
287   g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item));
288
289   draw = draw != FALSE;
290
291   if (draw != item->priv->draw)
292     {
293       item->priv->draw = draw;
294
295       gtk_widget_queue_draw (GTK_WIDGET (item));
296
297       g_object_notify (G_OBJECT (item), "draw");
298     }
299 }