]> Pileus Git - ~andy/gtk/blob - gtk/gtkseparator.c
45e93f3fb8e508f93863fb1d230a999a4dce32da
[~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 enum {
46   PROP_0,
47   PROP_ORIENTATION
48 };
49
50
51 typedef struct _GtkSeparatorPrivate GtkSeparatorPrivate;
52
53 struct _GtkSeparatorPrivate
54 {
55   GtkOrientation orientation;
56 };
57
58 #define GTK_SEPARATOR_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SEPARATOR, GtkSeparatorPrivate))
59
60
61 static void       gtk_separator_set_property (GObject        *object,
62                                               guint           prop_id,
63                                               const GValue   *value,
64                                               GParamSpec     *pspec);
65 static void       gtk_separator_get_property (GObject        *object,
66                                               guint           prop_id,
67                                               GValue         *value,
68                                               GParamSpec     *pspec);
69
70 static void       gtk_separator_size_request (GtkWidget      *widget,
71                                               GtkRequisition *requisition);
72 static gboolean   gtk_separator_expose       (GtkWidget      *widget,
73                                               GdkEventExpose *event);
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->size_request = gtk_separator_size_request;
91   widget_class->expose_event = gtk_separator_expose;
92
93   g_object_class_override_property (object_class,
94                                     PROP_ORIENTATION,
95                                     "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   GtkWidget *widget = GTK_WIDGET (separator);
104   GtkSeparatorPrivate *private = GTK_SEPARATOR_GET_PRIVATE (separator);
105
106   gtk_widget_set_has_window (GTK_WIDGET (separator), FALSE);
107
108   private->orientation = GTK_ORIENTATION_HORIZONTAL;
109
110   widget->requisition.width  = 1;
111   widget->requisition.height = widget->style->ythickness;
112 }
113
114 static void
115 gtk_separator_set_property (GObject      *object,
116                             guint         prop_id,
117                             const GValue *value,
118                             GParamSpec   *pspec)
119 {
120   GtkSeparatorPrivate *private = GTK_SEPARATOR_GET_PRIVATE (object);
121
122   switch (prop_id)
123     {
124     case PROP_ORIENTATION:
125       private->orientation = g_value_get_enum (value);
126       gtk_widget_queue_resize (GTK_WIDGET (object));
127       break;
128     default:
129       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130       break;
131     }
132 }
133
134 static void
135 gtk_separator_get_property (GObject    *object,
136                             guint       prop_id,
137                             GValue     *value,
138                             GParamSpec *pspec)
139 {
140   GtkSeparatorPrivate *private = GTK_SEPARATOR_GET_PRIVATE (object);
141
142   switch (prop_id)
143     {
144     case PROP_ORIENTATION:
145       g_value_set_enum (value, private->orientation);
146       break;
147     default:
148       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149       break;
150     }
151 }
152
153 static void
154 gtk_separator_size_request (GtkWidget      *widget,
155                             GtkRequisition *requisition)
156 {
157   GtkSeparatorPrivate *private = GTK_SEPARATOR_GET_PRIVATE (widget);
158   gboolean wide_separators;
159   gint     separator_width;
160   gint     separator_height;
161
162   gtk_widget_style_get (widget,
163                         "wide-separators",  &wide_separators,
164                         "separator-width",  &separator_width,
165                         "separator-height", &separator_height,
166                         NULL);
167
168   requisition->width  = 1;
169   requisition->height = 1;
170
171   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
172     {
173       if (wide_separators)
174         requisition->height = separator_height;
175       else
176         requisition->height = widget->style->ythickness;
177     }
178   else
179     {
180       if (wide_separators)
181         requisition->width = separator_width;
182       else
183         requisition->width = widget->style->xthickness;
184     }
185 }
186
187 static gboolean
188 gtk_separator_expose (GtkWidget      *widget,
189                       GdkEventExpose *event)
190 {
191   GtkSeparatorPrivate *private = GTK_SEPARATOR_GET_PRIVATE (widget);
192   gboolean wide_separators;
193   gint     separator_width;
194   gint     separator_height;
195
196   if (!gtk_widget_is_drawable (widget))
197     return FALSE;
198
199   gtk_widget_style_get (widget,
200                         "wide-separators",  &wide_separators,
201                         "separator-width",  &separator_width,
202                         "separator-height", &separator_height,
203                         NULL);
204
205   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
206     {
207       if (wide_separators)
208         gtk_paint_box (widget->style, widget->window,
209                        gtk_widget_get_state (widget), GTK_SHADOW_ETCHED_OUT,
210                        &event->area, widget, "hseparator",
211                        widget->allocation.x,
212                        widget->allocation.y + (widget->allocation.height -
213                                                separator_height) / 2,
214                        widget->allocation.width,
215                        separator_height);
216       else
217         gtk_paint_hline (widget->style, widget->window,
218                          gtk_widget_get_state (widget),
219                          &event->area, widget, "hseparator",
220                          widget->allocation.x,
221                          widget->allocation.x + widget->allocation.width - 1,
222                          widget->allocation.y + (widget->allocation.height -
223                                                  widget->style->ythickness) / 2);
224     }
225   else
226     {
227       if (wide_separators)
228         gtk_paint_box (widget->style, widget->window,
229                        gtk_widget_get_state (widget), GTK_SHADOW_ETCHED_OUT,
230                        &event->area, widget, "vseparator",
231                        widget->allocation.x + (widget->allocation.width -
232                                                separator_width) / 2,
233                        widget->allocation.y,
234                        separator_width,
235                        widget->allocation.height);
236       else
237         gtk_paint_vline (widget->style, widget->window,
238                          gtk_widget_get_state (widget),
239                          &event->area, widget, "vseparator",
240                          widget->allocation.y,
241                          widget->allocation.y + widget->allocation.height - 1,
242                          widget->allocation.x + (widget->allocation.width -
243                                                  widget->style->xthickness) / 2);
244     }
245
246   return FALSE;
247 }
248
249 /**
250  * gtk_separator_new:
251  * @orientation: the separator's orientation.
252  *
253  * Creates a new #GtkSeparator with the given orientation.
254  *
255  * Return value: a new #GtkSeparator.
256  *
257  * Since: 3.0
258  **/
259 GtkWidget *
260 gtk_separator_new (GtkOrientation orientation)
261 {
262   return g_object_new (GTK_TYPE_SEPARATOR,
263                        "orientation", orientation,
264                        NULL);
265 }