]> Pileus Git - ~andy/gtk/blob - gtk/gtkseparator.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24
25 #include "config.h"
26
27 #include "gtkorientableprivate.h"
28 #include "gtkseparator.h"
29 #include "gtkprivate.h"
30 #include "gtkintl.h"
31
32 /**
33  * SECTION:gtkseparator
34  * @Short_description: A separator widget
35  * @Title: GtkSeparator
36  *
37  * GtkSeparator is a horizontal or vertical separator widget, depending on the 
38  * value of the #GtkOrientable:orientation property, used to group the widgets within a
39  * window. It displays a line with a shadow to make it appear sunken into the 
40  * interface.
41  */
42
43
44 struct _GtkSeparatorPrivate
45 {
46   GtkOrientation orientation;
47 };
48
49
50 enum {
51   PROP_0,
52   PROP_ORIENTATION
53 };
54
55 static void       gtk_separator_set_property (GObject        *object,
56                                               guint           prop_id,
57                                               const GValue   *value,
58                                               GParamSpec     *pspec);
59 static void       gtk_separator_get_property (GObject        *object,
60                                               guint           prop_id,
61                                               GValue         *value,
62                                               GParamSpec     *pspec);
63 static void       gtk_separator_get_preferred_width
64                                              (GtkWidget      *widget,
65                                               gint           *minimum,
66                                               gint           *natural);
67 static void       gtk_separator_get_preferred_height
68                                              (GtkWidget      *widget,
69                                               gint           *minimum,
70                                               gint           *natural);
71 static gboolean   gtk_separator_draw         (GtkWidget      *widget,
72                                               cairo_t        *cr);
73
74
75 G_DEFINE_TYPE_WITH_CODE (GtkSeparator, gtk_separator, GTK_TYPE_WIDGET,
76                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
77                                                 NULL))
78
79
80 static void
81 gtk_separator_class_init (GtkSeparatorClass *class)
82 {
83   GObjectClass *object_class = G_OBJECT_CLASS (class);
84   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
85
86   object_class->set_property = gtk_separator_set_property;
87   object_class->get_property = gtk_separator_get_property;
88
89   widget_class->get_preferred_width = gtk_separator_get_preferred_width;
90   widget_class->get_preferred_height = gtk_separator_get_preferred_height;
91
92   widget_class->draw = gtk_separator_draw;
93
94   gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_SEPARATOR);
95
96   g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
97
98   g_type_class_add_private (object_class, sizeof (GtkSeparatorPrivate));
99 }
100
101 static void
102 gtk_separator_init (GtkSeparator *separator)
103 {
104   GtkSeparatorPrivate *private;
105   GtkStyleContext *context;
106
107   separator->priv = G_TYPE_INSTANCE_GET_PRIVATE (separator,
108                                                  GTK_TYPE_SEPARATOR,
109                                                  GtkSeparatorPrivate);
110   private = separator->priv;
111
112   gtk_widget_set_has_window (GTK_WIDGET (separator), FALSE);
113
114   private->orientation = GTK_ORIENTATION_HORIZONTAL;
115
116   context = gtk_widget_get_style_context (GTK_WIDGET (separator));
117   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SEPARATOR);
118 }
119
120 static void
121 gtk_separator_set_property (GObject      *object,
122                             guint         prop_id,
123                             const GValue *value,
124                             GParamSpec   *pspec)
125 {
126   GtkSeparator *separator = GTK_SEPARATOR (object);
127   GtkSeparatorPrivate *private = separator->priv;
128
129   switch (prop_id)
130     {
131     case PROP_ORIENTATION:
132       private->orientation = g_value_get_enum (value);
133       _gtk_orientable_set_style_classes (GTK_ORIENTABLE (object));
134       gtk_widget_queue_resize (GTK_WIDGET (object));
135       break;
136     default:
137       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138       break;
139     }
140 }
141
142 static void
143 gtk_separator_get_property (GObject    *object,
144                             guint       prop_id,
145                             GValue     *value,
146                             GParamSpec *pspec)
147 {
148   GtkSeparator *separator = GTK_SEPARATOR (object);
149   GtkSeparatorPrivate *private = separator->priv;
150
151   switch (prop_id)
152     {
153     case PROP_ORIENTATION:
154       g_value_set_enum (value, private->orientation);
155       break;
156     default:
157       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158       break;
159     }
160 }
161
162 static void
163 gtk_separator_get_preferred_size (GtkWidget      *widget,
164                                   GtkOrientation  orientation,
165                                   gint           *minimum,
166                                   gint           *natural)
167 {
168   GtkSeparator *separator = GTK_SEPARATOR (widget);
169   GtkSeparatorPrivate *private = separator->priv;
170   gboolean wide_sep;
171   gint     sep_width;
172   gint     sep_height;
173
174   gtk_widget_style_get (widget,
175                         "wide-separators",  &wide_sep,
176                         "separator-width",  &sep_width,
177                         "separator-height", &sep_height,
178                         NULL);
179
180   if (orientation == private->orientation)
181     {
182       *minimum = *natural = 1;
183     }
184   else if (orientation == GTK_ORIENTATION_VERTICAL)
185     {
186       *minimum = *natural = wide_sep ? sep_height : 1;
187     }
188   else
189     {
190       *minimum = *natural = wide_sep ? sep_width : 1;
191     }
192 }
193
194 static void
195 gtk_separator_get_preferred_width (GtkWidget *widget,
196                                    gint      *minimum,
197                                    gint      *natural)
198 {
199   gtk_separator_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
200 }
201
202 static void
203 gtk_separator_get_preferred_height (GtkWidget *widget,
204                                     gint      *minimum,
205                                     gint      *natural)
206 {
207   gtk_separator_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum, natural);
208 }
209
210 static gboolean
211 gtk_separator_draw (GtkWidget *widget,
212                     cairo_t   *cr)
213 {
214   GtkSeparator *separator = GTK_SEPARATOR (widget);
215   GtkSeparatorPrivate *private = separator->priv;
216   GtkStyleContext *context;
217   gboolean wide_separators;
218   gint separator_width;
219   gint separator_height;
220   int width, height;
221
222   gtk_widget_style_get (widget,
223                         "wide-separators",  &wide_separators,
224                         "separator-width",  &separator_width,
225                         "separator-height", &separator_height,
226                         NULL);
227
228   context = gtk_widget_get_style_context (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_render_frame (context, cr,
236                           0, (height - separator_height) / 2,
237                           width, separator_height);
238       else
239         gtk_render_line (context, cr,
240                          0, height / 2,
241                          width - 1, height / 2);
242     }
243   else
244     {
245       if (wide_separators)
246         gtk_render_frame (context, cr,
247                           (width - separator_width) / 2, 0,
248                           separator_width, height);
249       else
250         gtk_render_line (context, cr,
251                          width / 2, 0,
252                          width / 2, height - 1);
253     }
254
255   return FALSE;
256 }
257
258 /**
259  * gtk_separator_new:
260  * @orientation: the separator's orientation.
261  *
262  * Creates a new #GtkSeparator with the given orientation.
263  *
264  * Return value: a new #GtkSeparator.
265  *
266  * Since: 3.0
267  */
268 GtkWidget *
269 gtk_separator_new (GtkOrientation orientation)
270 {
271   return g_object_new (GTK_TYPE_SEPARATOR,
272                        "orientation", orientation,
273                        NULL);
274 }