]> Pileus Git - ~andy/gtk/blob - gtk/gtkseparatortoolitem.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21 #include "gtkseparatormenuitem.h"
22 #include "gtkseparatortoolitem.h"
23 #include "gtkintl.h"
24 #include "gtktoolbar.h"
25 #include "gtkprivate.h"
26
27 /**
28  * SECTION:gtkseparatortoolitem
29  * @Short_description: A toolbar item that separates groups of other
30  *   toolbar items
31  * @Title: GtkSeparatorToolItem
32  * @See_also: #GtkToolbar, #GtkRadioToolButton
33  *
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.
37  *
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.
41  *
42  * Use gtk_separator_tool_item_new() to create a new #GtkSeparatorToolItem.
43  */
44
45 #define MENU_ID "gtk-separator-tool-item-menu-id"
46
47 struct _GtkSeparatorToolItemPrivate
48 {
49   GdkWindow *event_window;
50   guint draw : 1;
51 };
52
53 enum {
54   PROP_0,
55   PROP_DRAW
56 };
57
58 static gboolean gtk_separator_tool_item_create_menu_proxy (GtkToolItem               *item);
59 static void     gtk_separator_tool_item_set_property      (GObject                   *object,
60                                                            guint                      prop_id,
61                                                            const GValue              *value,
62                                                            GParamSpec                *pspec);
63 static void     gtk_separator_tool_item_get_property      (GObject                   *object,
64                                                            guint                      prop_id,
65                                                            GValue                    *value,
66                                                            GParamSpec                *pspec);
67 static void     gtk_separator_tool_item_get_preferred_width (GtkWidget               *widget,
68                                                            gint                      *minimum,
69                                                            gint                      *natural);
70 static void     gtk_separator_tool_item_get_preferred_height (GtkWidget              *widget,
71                                                            gint                      *minimum,
72                                                            gint                      *natural);
73 static void     gtk_separator_tool_item_size_allocate     (GtkWidget                 *widget,
74                                                            GtkAllocation             *allocation);
75 static gboolean gtk_separator_tool_item_draw              (GtkWidget                 *widget,
76                                                            cairo_t                   *cr);
77 static void     gtk_separator_tool_item_add               (GtkContainer              *container,
78                                                            GtkWidget                 *child);
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);
86
87
88 G_DEFINE_TYPE (GtkSeparatorToolItem, gtk_separator_tool_item, GTK_TYPE_TOOL_ITEM)
89
90 static gint
91 get_space_size (GtkToolItem *tool_item)
92 {
93   gint space_size = _gtk_toolbar_get_default_space_size();
94   GtkWidget *parent;
95
96   parent = gtk_widget_get_parent (GTK_WIDGET (tool_item));
97
98   if (GTK_IS_TOOLBAR (parent))
99     {
100       gtk_widget_style_get (parent,
101                             "space-size", &space_size,
102                             NULL);
103     }
104   
105   return space_size;
106 }
107
108 static void
109 gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
110 {
111   GObjectClass *object_class;
112   GtkContainerClass *container_class;
113   GtkToolItemClass *toolitem_class;
114   GtkWidgetClass *widget_class;
115   
116   object_class = (GObjectClass *)class;
117   container_class = (GtkContainerClass *)class;
118   toolitem_class = (GtkToolItemClass *)class;
119   widget_class = (GtkWidgetClass *)class;
120
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;
133
134   toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
135   
136   container_class->add = gtk_separator_tool_item_add;
137   
138   g_object_class_install_property (object_class,
139                                    PROP_DRAW,
140                                    g_param_spec_boolean ("draw",
141                                                          P_("Draw"),
142                                                          P_("Whether the separator is drawn, or just blank"),
143                                                          TRUE,
144                                                          GTK_PARAM_READWRITE));
145   
146
147   g_type_class_add_private (object_class, sizeof (GtkSeparatorToolItemPrivate));
148 }
149
150 static void
151 gtk_separator_tool_item_init (GtkSeparatorToolItem *separator_item)
152 {
153   GtkStyleContext *context;
154
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;
159
160   gtk_widget_set_has_window (GTK_WIDGET (separator_item), FALSE);
161
162   context = gtk_widget_get_style_context (GTK_WIDGET (separator_item));
163   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SEPARATOR);
164 }
165
166 static void
167 gtk_separator_tool_item_add (GtkContainer *container,
168                              GtkWidget    *child)
169 {
170   g_warning ("attempt to add a child to an GtkSeparatorToolItem");
171 }
172
173 static gboolean
174 gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
175 {
176   GtkWidget *menu_item = NULL;
177   
178   menu_item = gtk_separator_menu_item_new();
179   
180   gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
181   
182   return TRUE;
183 }
184
185 static void
186 gtk_separator_tool_item_set_property (GObject      *object,
187                                       guint         prop_id,
188                                       const GValue *value,
189                                       GParamSpec   *pspec)
190 {
191   GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
192   
193   switch (prop_id)
194     {
195     case PROP_DRAW:
196       gtk_separator_tool_item_set_draw (item, g_value_get_boolean (value));
197       break;
198     default:
199       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200       break;
201     }
202 }
203
204 static void
205 gtk_separator_tool_item_get_property (GObject      *object,
206                                       guint         prop_id,
207                                       GValue       *value,
208                                       GParamSpec   *pspec)
209 {
210   GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
211   
212   switch (prop_id)
213     {
214     case PROP_DRAW:
215       g_value_set_boolean (value, gtk_separator_tool_item_get_draw (item));
216       break;
217     default:
218       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219       break;
220     }
221 }
222
223 static void
224 gtk_separator_tool_item_get_preferred_size (GtkWidget      *widget,
225                                             GtkOrientation  orientation,
226                                             gint           *minimum,
227                                             gint           *natural)
228 {
229   if (gtk_tool_item_get_orientation (GTK_TOOL_ITEM (widget)) == orientation)
230     *minimum = *natural = get_space_size (GTK_TOOL_ITEM (widget));
231   else
232     *minimum = *natural = 1;
233 }
234
235 static void
236 gtk_separator_tool_item_get_preferred_width (GtkWidget *widget,
237                                              gint      *minimum,
238                                              gint      *natural)
239 {
240   gtk_separator_tool_item_get_preferred_size (widget,
241                                               GTK_ORIENTATION_HORIZONTAL,
242                                               minimum,
243                                               natural);
244 }
245
246 static void
247 gtk_separator_tool_item_get_preferred_height (GtkWidget *widget,
248                                               gint      *minimum,
249                                               gint      *natural)
250 {
251   gtk_separator_tool_item_get_preferred_size (widget,
252                                               GTK_ORIENTATION_VERTICAL,
253                                               minimum,
254                                               natural);
255 }
256
257 static void
258 gtk_separator_tool_item_size_allocate (GtkWidget     *widget,
259                                        GtkAllocation *allocation)
260 {
261   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
262   GtkSeparatorToolItemPrivate *priv = separator->priv;
263
264   gtk_widget_set_allocation (widget, allocation);
265
266   if (gtk_widget_get_realized (widget))
267     gdk_window_move_resize (priv->event_window,
268                             allocation->x,
269                             allocation->y,
270                             allocation->width,
271                             allocation->height);
272
273 }
274
275 static void
276 gtk_separator_tool_item_realize (GtkWidget *widget)
277 {
278   GtkAllocation allocation;
279   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
280   GtkSeparatorToolItemPrivate *priv = separator->priv;
281   GdkWindow *window;
282   GdkWindowAttr attributes;
283   gint attributes_mask;
284
285   gtk_widget_set_realized (widget, TRUE);
286
287   gtk_widget_get_allocation (widget, &allocation);
288
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;
300
301   window = gtk_widget_get_parent_window (widget);
302   gtk_widget_set_window (widget, window);
303   g_object_ref (window);
304
305   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
306                                        &attributes, attributes_mask);
307   gtk_widget_register_window (widget, priv->event_window);
308 }
309
310 static void
311 gtk_separator_tool_item_unrealize (GtkWidget *widget)
312 {
313   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
314   GtkSeparatorToolItemPrivate *priv = separator->priv;
315
316   if (priv->event_window)
317     {
318       gtk_widget_unregister_window (widget, priv->event_window);
319       gdk_window_destroy (priv->event_window);
320       priv->event_window = NULL;
321     }
322
323   GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->unrealize (widget);
324 }
325
326 static void
327 gtk_separator_tool_item_map (GtkWidget *widget)
328 {
329   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
330   GtkSeparatorToolItemPrivate *priv = separator->priv;
331
332   GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->map (widget);
333
334   if (priv->event_window)
335     gdk_window_show (priv->event_window);
336 }
337
338 static void
339 gtk_separator_tool_item_unmap (GtkWidget *widget)
340 {
341   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
342   GtkSeparatorToolItemPrivate *priv = separator->priv;
343
344   if (priv->event_window)
345     gdk_window_hide (priv->event_window);
346
347   GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->unmap (widget);
348 }
349
350 static gboolean
351 gtk_separator_tool_item_button_event (GtkWidget      *widget,
352                                       GdkEventButton *event)
353 {
354   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
355   GtkSeparatorToolItemPrivate *priv = separator->priv;
356
357   /* We want window dragging to work on empty toolbar areas,
358    * so we only eat button events on visible separators
359    */
360   return priv->draw;
361 }
362
363 static gboolean
364 gtk_separator_tool_item_draw (GtkWidget *widget,
365                               cairo_t   *cr)
366 {
367   GtkAllocation allocation;
368   GtkToolbar *toolbar = NULL;
369   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
370   GtkSeparatorToolItemPrivate *priv = separator->priv;
371   GtkWidget *parent;
372
373   if (priv->draw)
374     {
375       parent = gtk_widget_get_parent (widget);
376       if (GTK_IS_TOOLBAR (parent))
377         toolbar = GTK_TOOLBAR (parent);
378
379       gtk_widget_get_allocation (widget, &allocation);
380       _gtk_toolbar_paint_space_line (widget, toolbar, cr);
381     }
382
383   return FALSE;
384 }
385
386 /**
387  * gtk_separator_tool_item_new:
388  * 
389  * Create a new #GtkSeparatorToolItem
390  * 
391  * Return value: the new #GtkSeparatorToolItem
392  * 
393  * Since: 2.4
394  */
395 GtkToolItem *
396 gtk_separator_tool_item_new (void)
397 {
398   GtkToolItem *self;
399   
400   self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
401                        NULL);
402   
403   return self;
404 }
405
406 /**
407  * gtk_separator_tool_item_get_draw:
408  * @item: a #GtkSeparatorToolItem 
409  * 
410  * Returns whether @item is drawn as a line, or just blank. 
411  * See gtk_separator_tool_item_set_draw().
412  * 
413  * Return value: %TRUE if @item is drawn as a line, or just blank.
414  * 
415  * Since: 2.4
416  */
417 gboolean
418 gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
419 {
420   g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
421   
422   return item->priv->draw;
423 }
424
425 /**
426  * gtk_separator_tool_item_set_draw:
427  * @item: a #GtkSeparatorToolItem
428  * @draw: whether @item is drawn as a vertical line
429  * 
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.
433  * 
434  * Since: 2.4
435  */
436 void
437 gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *item,
438                                   gboolean              draw)
439 {
440   g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item));
441
442   draw = draw != FALSE;
443
444   if (draw != item->priv->draw)
445     {
446       item->priv->draw = draw;
447
448       gtk_widget_queue_draw (GTK_WIDGET (item));
449
450       g_object_notify (G_OBJECT (item), "draw");
451     }
452 }