]> Pileus Git - ~andy/gtk/blob - gtk/gtkseparator.c
GtkSpinButton: Use GtkStyleContext
[~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 gtk_separator_set_property (GObject      *object,
117                             guint         prop_id,
118                             const GValue *value,
119                             GParamSpec   *pspec)
120 {
121   GtkSeparator *separator = GTK_SEPARATOR (object);
122   GtkSeparatorPrivate *private = separator->priv;
123
124   switch (prop_id)
125     {
126     case PROP_ORIENTATION:
127       private->orientation = g_value_get_enum (value);
128       gtk_widget_queue_resize (GTK_WIDGET (object));
129       break;
130     default:
131       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132       break;
133     }
134 }
135
136 static void
137 gtk_separator_get_property (GObject    *object,
138                             guint       prop_id,
139                             GValue     *value,
140                             GParamSpec *pspec)
141 {
142   GtkSeparator *separator = GTK_SEPARATOR (object);
143   GtkSeparatorPrivate *private = separator->priv;
144
145   switch (prop_id)
146     {
147     case PROP_ORIENTATION:
148       g_value_set_enum (value, private->orientation);
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_preferred_size (GtkWidget      *widget,
158                                   GtkOrientation  orientation,
159                                   gint           *minimum,
160                                   gint           *natural)
161 {
162   GtkSeparator *separator = GTK_SEPARATOR (widget);
163   GtkSeparatorPrivate *private = separator->priv;
164   GtkStyle *style;
165   gboolean wide_sep;
166   gint     sep_width;
167   gint     sep_height;
168
169   style = gtk_widget_get_style (widget);
170   gtk_widget_style_get (widget,
171                         "wide-separators",  &wide_sep,
172                         "separator-width",  &sep_width,
173                         "separator-height", &sep_height,
174                         NULL);
175
176   if (orientation == private->orientation)
177     {
178       *minimum = *natural = 1;
179     }
180   else if (orientation == GTK_ORIENTATION_VERTICAL)
181     {
182       *minimum = *natural = wide_sep ? sep_height : style->ythickness;
183     }
184   else
185     {
186       *minimum = *natural = wide_sep ? sep_width : style->xthickness;
187     }
188 }
189
190 static void
191 gtk_separator_get_preferred_width (GtkWidget *widget,
192                                    gint      *minimum,
193                                    gint      *natural)
194 {
195   gtk_separator_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
196 }
197
198 static void
199 gtk_separator_get_preferred_height (GtkWidget *widget,
200                                     gint      *minimum,
201                                     gint      *natural)
202 {
203   gtk_separator_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum, natural);
204 }
205
206 static gboolean
207 gtk_separator_draw (GtkWidget    *widget,
208                     cairo_t      *cr)
209 {
210   GtkSeparator *separator = GTK_SEPARATOR (widget);
211   GtkSeparatorPrivate *private = separator->priv;
212   GtkStateType state;
213   GtkStyle *style;
214   GdkWindow *window;
215   gboolean wide_separators;
216   gint separator_width;
217   gint separator_height;
218   int width, height;
219
220   style = gtk_widget_get_style (widget);
221   gtk_widget_style_get (widget,
222                         "wide-separators",  &wide_separators,
223                         "separator-width",  &separator_width,
224                         "separator-height", &separator_height,
225                         NULL);
226
227   window = gtk_widget_get_window (widget);
228   state = gtk_widget_get_state (widget);
229   width = gtk_widget_get_allocated_width (widget);
230   height = gtk_widget_get_allocated_height (widget);
231
232   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
233     {
234       if (wide_separators)
235         gtk_paint_box (style, cr,
236                        gtk_widget_get_state (widget), GTK_SHADOW_ETCHED_OUT,
237                        widget, "hseparator",
238                        0, (height - separator_height) / 2,
239                        width, separator_height);
240       else
241         gtk_paint_hline (style, cr,
242                          gtk_widget_get_state (widget),
243                          widget, "hseparator",
244                          0, width - 1,
245                          (height - style->ythickness) / 2);
246     }
247   else
248     {
249       if (wide_separators)
250         gtk_paint_box (style, cr,
251                        gtk_widget_get_state (widget), GTK_SHADOW_ETCHED_OUT,
252                        widget, "vseparator",
253                        (width - separator_width) / 2, 0,
254                        separator_width, height);
255       else
256         gtk_paint_vline (style, cr,
257                          gtk_widget_get_state (widget),
258                          widget, "vseparator",
259                          0, height - 1,
260                          (width - style->xthickness) / 2);
261     }
262
263   return FALSE;
264 }
265
266 /**
267  * gtk_separator_new:
268  * @orientation: the separator's orientation.
269  *
270  * Creates a new #GtkSeparator with the given orientation.
271  *
272  * Return value: a new #GtkSeparator.
273  *
274  * Since: 3.0
275  **/
276 GtkWidget *
277 gtk_separator_new (GtkOrientation orientation)
278 {
279   return g_object_new (GTK_TYPE_SEPARATOR,
280                        "orientation", orientation,
281                        NULL);
282 }