]> Pileus Git - ~andy/gtk/blob - gtk/gtkseparatortoolitem.c
Merge branch 'gdk-backend-wayland'
[~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 #define MENU_ID "gtk-separator-tool-item-menu-id"
48
49 struct _GtkSeparatorToolItemPrivate
50 {
51   GdkWindow *event_window;
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_get_preferred_width (GtkWidget               *widget,
70                                                            gint                      *minimum,
71                                                            gint                      *natural);
72 static void     gtk_separator_tool_item_get_preferred_height (GtkWidget              *widget,
73                                                            gint                      *minimum,
74                                                            gint                      *natural);
75 static void     gtk_separator_tool_item_size_allocate     (GtkWidget                 *widget,
76                                                            GtkAllocation             *allocation);
77 static gboolean gtk_separator_tool_item_draw              (GtkWidget                 *widget,
78                                                            cairo_t                   *cr);
79 static void     gtk_separator_tool_item_add               (GtkContainer              *container,
80                                                            GtkWidget                 *child);
81 static gint     get_space_size                            (GtkToolItem               *tool_item);
82 static void     gtk_separator_tool_item_realize           (GtkWidget                 *widget);
83 static void     gtk_separator_tool_item_unrealize         (GtkWidget                 *widget);
84 static void     gtk_separator_tool_item_map               (GtkWidget                 *widget);
85 static void     gtk_separator_tool_item_unmap             (GtkWidget                 *widget);
86 static gboolean gtk_separator_tool_item_button_event      (GtkWidget                 *widget,
87                                                            GdkEventButton            *event);
88
89
90 G_DEFINE_TYPE (GtkSeparatorToolItem, gtk_separator_tool_item, GTK_TYPE_TOOL_ITEM)
91
92 static gint
93 get_space_size (GtkToolItem *tool_item)
94 {
95   gint space_size = _gtk_toolbar_get_default_space_size();
96   GtkWidget *parent;
97
98   parent = gtk_widget_get_parent (GTK_WIDGET (tool_item));
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   object_class = (GObjectClass *)class;
119   container_class = (GtkContainerClass *)class;
120   toolitem_class = (GtkToolItemClass *)class;
121   widget_class = (GtkWidgetClass *)class;
122
123   object_class->set_property = gtk_separator_tool_item_set_property;
124   object_class->get_property = gtk_separator_tool_item_get_property;
125   widget_class->get_preferred_width = gtk_separator_tool_item_get_preferred_width;
126   widget_class->get_preferred_height = gtk_separator_tool_item_get_preferred_height;
127   widget_class->size_allocate = gtk_separator_tool_item_size_allocate;
128   widget_class->draw = gtk_separator_tool_item_draw;
129   widget_class->realize = gtk_separator_tool_item_realize;
130   widget_class->unrealize = gtk_separator_tool_item_unrealize;
131   widget_class->map = gtk_separator_tool_item_map;
132   widget_class->unmap = gtk_separator_tool_item_unmap;
133   widget_class->button_press_event = gtk_separator_tool_item_button_event;
134   widget_class->button_release_event = gtk_separator_tool_item_button_event;
135
136   toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
137   
138   container_class->add = gtk_separator_tool_item_add;
139   
140   g_object_class_install_property (object_class,
141                                    PROP_DRAW,
142                                    g_param_spec_boolean ("draw",
143                                                          P_("Draw"),
144                                                          P_("Whether the separator is drawn, or just blank"),
145                                                          TRUE,
146                                                          GTK_PARAM_READWRITE));
147   
148
149   g_type_class_add_private (object_class, sizeof (GtkSeparatorToolItemPrivate));
150 }
151
152 static void
153 gtk_separator_tool_item_init (GtkSeparatorToolItem *separator_item)
154 {
155   GtkStyleContext *context;
156
157   separator_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (separator_item,
158                                                       GTK_TYPE_SEPARATOR_TOOL_ITEM,
159                                                       GtkSeparatorToolItemPrivate);
160   separator_item->priv->draw = TRUE;
161
162   gtk_widget_set_has_window (GTK_WIDGET (separator_item), FALSE);
163
164   context = gtk_widget_get_style_context (GTK_WIDGET (separator_item));
165   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SEPARATOR);
166 }
167
168 static void
169 gtk_separator_tool_item_add (GtkContainer *container,
170                              GtkWidget    *child)
171 {
172   g_warning ("attempt to add a child to an GtkSeparatorToolItem");
173 }
174
175 static gboolean
176 gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
177 {
178   GtkWidget *menu_item = NULL;
179   
180   menu_item = gtk_separator_menu_item_new();
181   
182   gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
183   
184   return TRUE;
185 }
186
187 static void
188 gtk_separator_tool_item_set_property (GObject      *object,
189                                       guint         prop_id,
190                                       const GValue *value,
191                                       GParamSpec   *pspec)
192 {
193   GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
194   
195   switch (prop_id)
196     {
197     case PROP_DRAW:
198       gtk_separator_tool_item_set_draw (item, g_value_get_boolean (value));
199       break;
200     default:
201       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202       break;
203     }
204 }
205
206 static void
207 gtk_separator_tool_item_get_property (GObject      *object,
208                                       guint         prop_id,
209                                       GValue       *value,
210                                       GParamSpec   *pspec)
211 {
212   GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
213   
214   switch (prop_id)
215     {
216     case PROP_DRAW:
217       g_value_set_boolean (value, gtk_separator_tool_item_get_draw (item));
218       break;
219     default:
220       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221       break;
222     }
223 }
224
225 static void
226 gtk_separator_tool_item_get_preferred_size (GtkWidget      *widget,
227                                             GtkOrientation  orientation,
228                                             gint           *minimum,
229                                             gint           *natural)
230 {
231   if (gtk_tool_item_get_orientation (GTK_TOOL_ITEM (widget)) == orientation)
232     *minimum = *natural = get_space_size (GTK_TOOL_ITEM (widget));
233   else
234     *minimum = *natural = 1;
235 }
236
237 static void
238 gtk_separator_tool_item_get_preferred_width (GtkWidget *widget,
239                                              gint      *minimum,
240                                              gint      *natural)
241 {
242   gtk_separator_tool_item_get_preferred_size (widget,
243                                               GTK_ORIENTATION_HORIZONTAL,
244                                               minimum,
245                                               natural);
246 }
247
248 static void
249 gtk_separator_tool_item_get_preferred_height (GtkWidget *widget,
250                                               gint      *minimum,
251                                               gint      *natural)
252 {
253   gtk_separator_tool_item_get_preferred_size (widget,
254                                               GTK_ORIENTATION_VERTICAL,
255                                               minimum,
256                                               natural);
257 }
258
259 static void
260 gtk_separator_tool_item_size_allocate (GtkWidget     *widget,
261                                        GtkAllocation *allocation)
262 {
263   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
264   GtkSeparatorToolItemPrivate *priv = separator->priv;
265
266   gtk_widget_set_allocation (widget, allocation);
267
268   if (gtk_widget_get_realized (widget))
269     gdk_window_move_resize (priv->event_window,
270                             allocation->x,
271                             allocation->y,
272                             allocation->width,
273                             allocation->height);
274
275 }
276
277 static void
278 gtk_separator_tool_item_realize (GtkWidget *widget)
279 {
280   GtkAllocation allocation;
281   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
282   GtkSeparatorToolItemPrivate *priv = separator->priv;
283   GdkWindow *window;
284   GdkWindowAttr attributes;
285   gint attributes_mask;
286
287   gtk_widget_set_realized (widget, TRUE);
288
289   gtk_widget_get_allocation (widget, &allocation);
290
291   attributes.window_type = GDK_WINDOW_CHILD;
292   attributes.x = allocation.x;
293   attributes.y = allocation.y;
294   attributes.width = allocation.width;
295   attributes.height = allocation.height;
296   attributes.wclass = GDK_INPUT_ONLY;
297   attributes.visual = gtk_widget_get_visual (widget);
298   attributes.event_mask = gtk_widget_get_events (widget) |
299                           GDK_BUTTON_PRESS_MASK |
300                           GDK_BUTTON_RELEASE_MASK;
301   attributes_mask = GDK_WA_X | GDK_WA_Y;
302
303   window = gtk_widget_get_parent_window (widget);
304   gtk_widget_set_window (widget, window);
305   g_object_ref (window);
306
307   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
308                                        &attributes, attributes_mask);
309   gdk_window_set_user_data (priv->event_window, widget);
310 }
311
312 static void
313 gtk_separator_tool_item_unrealize (GtkWidget *widget)
314 {
315   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
316   GtkSeparatorToolItemPrivate *priv = separator->priv;
317
318   if (priv->event_window)
319     {
320       gdk_window_set_user_data (priv->event_window, NULL);
321       gdk_window_destroy (priv->event_window);
322       priv->event_window = NULL;
323     }
324
325   GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->unrealize (widget);
326 }
327
328 static void
329 gtk_separator_tool_item_map (GtkWidget *widget)
330 {
331   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
332   GtkSeparatorToolItemPrivate *priv = separator->priv;
333
334   GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->map (widget);
335
336   if (priv->event_window)
337     gdk_window_show (priv->event_window);
338 }
339
340 static void
341 gtk_separator_tool_item_unmap (GtkWidget *widget)
342 {
343   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
344   GtkSeparatorToolItemPrivate *priv = separator->priv;
345
346   if (priv->event_window)
347     gdk_window_hide (priv->event_window);
348
349   GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->unmap (widget);
350 }
351
352 static gboolean
353 gtk_separator_tool_item_button_event (GtkWidget      *widget,
354                                       GdkEventButton *event)
355 {
356   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
357   GtkSeparatorToolItemPrivate *priv = separator->priv;
358
359   /* We want window dragging to work on empty toolbar areas,
360    * so we only eat button events on visible separators
361    */
362   return priv->draw;
363 }
364
365 static gboolean
366 gtk_separator_tool_item_draw (GtkWidget *widget,
367                               cairo_t   *cr)
368 {
369   GtkAllocation allocation;
370   GtkToolbar *toolbar = NULL;
371   GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
372   GtkSeparatorToolItemPrivate *priv = separator->priv;
373   GtkWidget *parent;
374
375   if (priv->draw)
376     {
377       parent = gtk_widget_get_parent (widget);
378       if (GTK_IS_TOOLBAR (parent))
379         toolbar = GTK_TOOLBAR (parent);
380
381       gtk_widget_get_allocation (widget, &allocation);
382       _gtk_toolbar_paint_space_line (widget, toolbar, cr);
383     }
384
385   return FALSE;
386 }
387
388 /**
389  * gtk_separator_tool_item_new:
390  * 
391  * Create a new #GtkSeparatorToolItem
392  * 
393  * Return value: the new #GtkSeparatorToolItem
394  * 
395  * Since: 2.4
396  */
397 GtkToolItem *
398 gtk_separator_tool_item_new (void)
399 {
400   GtkToolItem *self;
401   
402   self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
403                        NULL);
404   
405   return self;
406 }
407
408 /**
409  * gtk_separator_tool_item_get_draw:
410  * @item: a #GtkSeparatorToolItem 
411  * 
412  * Returns whether @item is drawn as a line, or just blank. 
413  * See gtk_separator_tool_item_set_draw().
414  * 
415  * Return value: %TRUE if @item is drawn as a line, or just blank.
416  * 
417  * Since: 2.4
418  */
419 gboolean
420 gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
421 {
422   g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
423   
424   return item->priv->draw;
425 }
426
427 /**
428  * gtk_separator_tool_item_set_draw:
429  * @item: a #GtkSeparatorToolItem
430  * @draw: whether @item is drawn as a vertical line
431  * 
432  * Whether @item is drawn as a vertical line, or just blank.
433  * Setting this to %FALSE along with gtk_tool_item_set_expand() is useful
434  * to create an item that forces following items to the end of the toolbar.
435  * 
436  * Since: 2.4
437  */
438 void
439 gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *item,
440                                   gboolean              draw)
441 {
442   g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item));
443
444   draw = draw != FALSE;
445
446   if (draw != item->priv->draw)
447     {
448       item->priv->draw = draw;
449
450       gtk_widget_queue_draw (GTK_WIDGET (item));
451
452       g_object_notify (G_OBJECT (item), "draw");
453     }
454 }