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