]> Pileus Git - ~andy/gtk/blob - gtk/gtkseparator.c
Set vertical/horizontal class on all widgets overriding GtkOrientable::orientation
[~andy/gtk] / gtk / gtkseparator.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #include "config.h"
28
29 #include "gtkorientable.h"
30 #include "gtkseparator.h"
31 #include "gtkprivate.h"
32 #include "gtkintl.h"
33
34 /**
35  * SECTION:gtkseparator
36  * @Short_description: Base class for GtkHSeparator and GtkVSeparator
37  * @Title: GtkSeparator
38  *
39  * The GtkSeparator widget is the base class for #GtkHSeparator and
40  * #GtkVSeparator. It can be used in the same way as these, by setting
41  * the "orientation" property suitably.
42  */
43
44
45 struct _GtkSeparatorPrivate
46 {
47   GtkOrientation orientation;
48 };
49
50
51 enum {
52   PROP_0,
53   PROP_ORIENTATION
54 };
55
56 static void       gtk_separator_set_property (GObject        *object,
57                                               guint           prop_id,
58                                               const GValue   *value,
59                                               GParamSpec     *pspec);
60 static void       gtk_separator_get_property (GObject        *object,
61                                               guint           prop_id,
62                                               GValue         *value,
63                                               GParamSpec     *pspec);
64 static void       gtk_separator_get_preferred_width
65                                              (GtkWidget      *widget,
66                                               gint           *minimum,
67                                               gint           *natural);
68 static void       gtk_separator_get_preferred_height
69                                              (GtkWidget      *widget,
70                                               gint           *minimum,
71                                               gint           *natural);
72 static gboolean   gtk_separator_draw         (GtkWidget      *widget,
73                                               cairo_t        *cr);
74
75
76 G_DEFINE_TYPE_WITH_CODE (GtkSeparator, gtk_separator, GTK_TYPE_WIDGET,
77                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
78                                                 NULL))
79
80
81 static void
82 gtk_separator_class_init (GtkSeparatorClass *class)
83 {
84   GObjectClass *object_class = G_OBJECT_CLASS (class);
85   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
86
87   object_class->set_property = gtk_separator_set_property;
88   object_class->get_property = gtk_separator_get_property;
89
90   widget_class->get_preferred_width = gtk_separator_get_preferred_width;
91   widget_class->get_preferred_height = gtk_separator_get_preferred_height;
92
93   widget_class->draw         = gtk_separator_draw;
94
95   g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
96
97   g_type_class_add_private (object_class, sizeof (GtkSeparatorPrivate));
98 }
99
100 static void
101 gtk_separator_init (GtkSeparator *separator)
102 {
103   GtkSeparatorPrivate *private;
104
105   separator->priv = G_TYPE_INSTANCE_GET_PRIVATE (separator,
106                                                  GTK_TYPE_SEPARATOR,
107                                                  GtkSeparatorPrivate);
108   private = separator->priv;
109
110   gtk_widget_set_has_window (GTK_WIDGET (separator), FALSE);
111
112   private->orientation = GTK_ORIENTATION_HORIZONTAL;
113 }
114
115 static void
116 reset_orientation_style (GtkSeparator *separator)
117 {
118   GtkStyleContext *context;
119
120   context = gtk_widget_get_style_context (GTK_WIDGET (separator));
121
122   if (separator->priv->orientation == GTK_ORIENTATION_VERTICAL)
123     {
124       gtk_style_context_add_class (context, GTK_STYLE_CLASS_VERTICAL);
125       gtk_style_context_remove_class (context, GTK_STYLE_CLASS_HORIZONTAL);
126     }
127   else
128     {
129       gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);
130       gtk_style_context_remove_class (context, GTK_STYLE_CLASS_VERTICAL);
131     }
132 }
133
134 static void
135 gtk_separator_set_property (GObject      *object,
136                             guint         prop_id,
137                             const GValue *value,
138                             GParamSpec   *pspec)
139 {
140   GtkSeparator *separator = GTK_SEPARATOR (object);
141   GtkSeparatorPrivate *private = separator->priv;
142
143   switch (prop_id)
144     {
145     case PROP_ORIENTATION:
146       private->orientation = g_value_get_enum (value);
147       reset_orientation_style (separator);
148       gtk_widget_queue_resize (GTK_WIDGET (object));
149       break;
150     default:
151       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152       break;
153     }
154 }
155
156 static void
157 gtk_separator_get_property (GObject    *object,
158                             guint       prop_id,
159                             GValue     *value,
160                             GParamSpec *pspec)
161 {
162   GtkSeparator *separator = GTK_SEPARATOR (object);
163   GtkSeparatorPrivate *private = separator->priv;
164
165   switch (prop_id)
166     {
167     case PROP_ORIENTATION:
168       g_value_set_enum (value, private->orientation);
169       break;
170     default:
171       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172       break;
173     }
174 }
175
176 static void
177 gtk_separator_get_preferred_size (GtkWidget      *widget,
178                                   GtkOrientation  orientation,
179                                   gint           *minimum,
180                                   gint           *natural)
181 {
182   GtkSeparator *separator = GTK_SEPARATOR (widget);
183   GtkSeparatorPrivate *private = separator->priv;
184   GtkStyleContext *context;
185   GtkStateFlags state;
186   GtkBorder border;
187   gboolean wide_sep;
188   gint     sep_width;
189   gint     sep_height;
190
191   context = gtk_widget_get_style_context (widget);
192   state = gtk_widget_get_state_flags (widget);
193   gtk_style_context_get_border (context, state, &border);
194
195   gtk_widget_style_get (widget,
196                         "wide-separators",  &wide_sep,
197                         "separator-width",  &sep_width,
198                         "separator-height", &sep_height,
199                         NULL);
200
201   if (orientation == private->orientation)
202     {
203       *minimum = *natural = 1;
204     }
205   else if (orientation == GTK_ORIENTATION_VERTICAL)
206     {
207       *minimum = *natural = wide_sep ? sep_height : border.top;
208     }
209   else
210     {
211       *minimum = *natural = wide_sep ? sep_width : border.left;
212     }
213 }
214
215 static void
216 gtk_separator_get_preferred_width (GtkWidget *widget,
217                                    gint      *minimum,
218                                    gint      *natural)
219 {
220   gtk_separator_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
221 }
222
223 static void
224 gtk_separator_get_preferred_height (GtkWidget *widget,
225                                     gint      *minimum,
226                                     gint      *natural)
227 {
228   gtk_separator_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum, natural);
229 }
230
231 static gboolean
232 gtk_separator_draw (GtkWidget    *widget,
233                     cairo_t      *cr)
234 {
235   GtkSeparator *separator = GTK_SEPARATOR (widget);
236   GtkSeparatorPrivate *private = separator->priv;
237   GtkStateFlags state;
238   GtkStyleContext *context;
239   GtkBorder padding;
240   GdkWindow *window;
241   gboolean wide_separators;
242   gint separator_width;
243   gint separator_height;
244   int width, height;
245
246   context = gtk_widget_get_style_context (widget);
247   gtk_widget_style_get (widget,
248                         "wide-separators",  &wide_separators,
249                         "separator-width",  &separator_width,
250                         "separator-height", &separator_height,
251                         NULL);
252
253   window = gtk_widget_get_window (widget);
254   state = gtk_widget_get_state_flags (widget);
255   width = gtk_widget_get_allocated_width (widget);
256   height = gtk_widget_get_allocated_height (widget);
257
258   gtk_style_context_get_padding (context, state, &padding);
259
260   gtk_style_context_save (context);
261   gtk_style_context_set_state (context, state);
262
263   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
264     {
265       if (wide_separators)
266         gtk_render_frame (context, cr,
267                           0, (height - separator_height) / 2,
268                           width, separator_height);
269       else
270         gtk_render_line (context, cr,
271                          0, (height - padding.top) / 2,
272                          width - 1, (height - padding.top) / 2);
273     }
274   else
275     {
276       if (wide_separators)
277         gtk_render_frame (context, cr,
278                           (width - separator_width) / 2, 0,
279                           separator_width, height);
280       else
281         gtk_render_line (context, cr,
282                          (width - padding.left) / 2, 0,
283                          (width - padding.left) / 2, height - 1);
284     }
285
286   return FALSE;
287 }
288
289 /**
290  * gtk_separator_new:
291  * @orientation: the separator's orientation.
292  *
293  * Creates a new #GtkSeparator with the given orientation.
294  *
295  * Return value: a new #GtkSeparator.
296  *
297  * Since: 3.0
298  **/
299 GtkWidget *
300 gtk_separator_new (GtkOrientation orientation)
301 {
302   return g_object_new (GTK_TYPE_SEPARATOR,
303                        "orientation", orientation,
304                        NULL);
305 }