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