]> Pileus Git - ~andy/gtk/blob - gtk/gtkseparator.c
Box, etc: Improve docs after H* and V* deprecations.
[~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 "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   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   GtkStyleContext *context;
171   GtkStateFlags state;
172   GtkBorder border;
173   gboolean wide_sep;
174   gint     sep_width;
175   gint     sep_height;
176
177   context = gtk_widget_get_style_context (widget);
178   state = gtk_widget_get_state_flags (widget);
179   gtk_style_context_get_border (context, state, &border);
180
181   gtk_widget_style_get (widget,
182                         "wide-separators",  &wide_sep,
183                         "separator-width",  &sep_width,
184                         "separator-height", &sep_height,
185                         NULL);
186
187   if (orientation == private->orientation)
188     {
189       *minimum = *natural = 1;
190     }
191   else if (orientation == GTK_ORIENTATION_VERTICAL)
192     {
193       *minimum = *natural = wide_sep ? sep_height : border.top;
194     }
195   else
196     {
197       *minimum = *natural = wide_sep ? sep_width : border.left;
198     }
199 }
200
201 static void
202 gtk_separator_get_preferred_width (GtkWidget *widget,
203                                    gint      *minimum,
204                                    gint      *natural)
205 {
206   gtk_separator_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
207 }
208
209 static void
210 gtk_separator_get_preferred_height (GtkWidget *widget,
211                                     gint      *minimum,
212                                     gint      *natural)
213 {
214   gtk_separator_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum, natural);
215 }
216
217 static gboolean
218 gtk_separator_draw (GtkWidget *widget,
219                     cairo_t   *cr)
220 {
221   GtkSeparator *separator = GTK_SEPARATOR (widget);
222   GtkSeparatorPrivate *private = separator->priv;
223   GtkStateFlags state;
224   GtkStyleContext *context;
225   GtkBorder padding;
226   gboolean wide_separators;
227   gint separator_width;
228   gint separator_height;
229   int width, height;
230
231   context = gtk_widget_get_style_context (widget);
232   gtk_widget_style_get (widget,
233                         "wide-separators",  &wide_separators,
234                         "separator-width",  &separator_width,
235                         "separator-height", &separator_height,
236                         NULL);
237
238   state = gtk_widget_get_state_flags (widget);
239   width = gtk_widget_get_allocated_width (widget);
240   height = gtk_widget_get_allocated_height (widget);
241
242   gtk_style_context_get_padding (context, state, &padding);
243
244   gtk_style_context_save (context);
245   gtk_style_context_set_state (context, state);
246
247   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
248     {
249       if (wide_separators)
250         gtk_render_frame (context, cr,
251                           0, (height - separator_height) / 2,
252                           width, separator_height);
253       else
254         gtk_render_line (context, cr,
255                          0, (height - padding.top) / 2,
256                          width - 1, (height - padding.top) / 2);
257     }
258   else
259     {
260       if (wide_separators)
261         gtk_render_frame (context, cr,
262                           (width - separator_width) / 2, 0,
263                           separator_width, height);
264       else
265         gtk_render_line (context, cr,
266                          (width - padding.left) / 2, 0,
267                          (width - padding.left) / 2, height - 1);
268     }
269
270   gtk_style_context_restore (context);
271
272   return FALSE;
273 }
274
275 /**
276  * gtk_separator_new:
277  * @orientation: the separator's orientation.
278  *
279  * Creates a new #GtkSeparator with the given orientation.
280  *
281  * Return value: a new #GtkSeparator.
282  *
283  * Since: 3.0
284  */
285 GtkWidget *
286 gtk_separator_new (GtkOrientation orientation)
287 {
288   return g_object_new (GTK_TYPE_SEPARATOR,
289                        "orientation", orientation,
290                        NULL);
291 }