]> Pileus Git - ~andy/gtk/blob - gtk/gtkarrow.c
Remove excess calls to g_return_if_fail from static and virtual functions.
[~andy/gtk] / gtk / gtkarrow.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-2001.  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 <math.h>
28 #include "gtkarrow.h"
29 #include "gtkintl.h"
30
31 #define MIN_ARROW_SIZE  11
32
33 enum {
34   PROP_0,
35
36   PROP_ARROW_TYPE,
37   PROP_SHADOW_TYPE,
38   
39   PROP_LAST
40 };
41
42
43 static void gtk_arrow_class_init (GtkArrowClass  *klass);
44 static void gtk_arrow_init       (GtkArrow       *arrow);
45 static gint gtk_arrow_expose     (GtkWidget      *widget,
46                                   GdkEventExpose *event);
47 static void gtk_arrow_set_property (GObject         *object,
48                                     guint            prop_id,
49                                     const GValue    *value,
50                                     GParamSpec      *pspec);
51 static void gtk_arrow_get_property (GObject         *object,
52                                     guint            prop_id,
53                                     GValue          *value,
54                                     GParamSpec      *pspec);
55
56 GtkType
57 gtk_arrow_get_type (void)
58 {
59   static GtkType arrow_type = 0;
60
61   if (!arrow_type)
62     {
63       static const GtkTypeInfo arrow_info =
64       {
65         "GtkArrow",
66         sizeof (GtkArrow),
67         sizeof (GtkArrowClass),
68         (GtkClassInitFunc) gtk_arrow_class_init,
69         (GtkObjectInitFunc) gtk_arrow_init,
70         /* reserved_1 */ NULL,
71         /* reserved_2 */ NULL,
72         (GtkClassInitFunc) NULL,
73       };
74
75       arrow_type = gtk_type_unique (GTK_TYPE_MISC, &arrow_info);
76     }
77
78   return arrow_type;
79 }
80
81 static void
82 gtk_arrow_class_init (GtkArrowClass *class)
83 {
84   GObjectClass *gobject_class;
85   GtkObjectClass *object_class;
86   GtkWidgetClass *widget_class;
87
88   gobject_class = (GObjectClass*) class;
89   object_class = (GtkObjectClass*) class;
90   widget_class = (GtkWidgetClass*) class;
91
92   gobject_class->set_property = gtk_arrow_set_property;
93   gobject_class->get_property = gtk_arrow_get_property;
94   
95   g_object_class_install_property (gobject_class,
96                                    PROP_ARROW_TYPE,
97                                    g_param_spec_enum ("arrow_type",
98                                                       _("Arrow direction"),
99                                                       _("The direction the arrow should point"),
100                                                       GTK_TYPE_ARROW_TYPE,
101                                                       GTK_ARROW_RIGHT,
102                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
103   g_object_class_install_property (gobject_class,
104                                    PROP_SHADOW_TYPE,
105                                    g_param_spec_enum ("shadow_type",
106                                                       _("Arrow shadow"),
107                                                       _("Appearance of the shadow surrounding the arrow"),
108                                                       GTK_TYPE_SHADOW_TYPE,
109                                                       GTK_SHADOW_OUT,
110                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
111   
112   widget_class->expose_event = gtk_arrow_expose;
113 }
114
115 static void
116 gtk_arrow_set_property (GObject         *object,
117                         guint            prop_id,
118                         const GValue    *value,
119                         GParamSpec      *pspec)
120 {
121   GtkArrow *arrow;
122   
123   arrow = GTK_ARROW (object);
124
125   switch (prop_id)
126     {
127     case PROP_ARROW_TYPE:
128       gtk_arrow_set (arrow,
129                      g_value_get_enum (value),
130                      arrow->shadow_type);
131       break;
132     case PROP_SHADOW_TYPE:
133       gtk_arrow_set (arrow,
134                      arrow->arrow_type,
135                      g_value_get_enum (value));
136       break;
137     default:
138       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139       break;
140     }
141 }
142
143
144 static void
145 gtk_arrow_get_property (GObject         *object,
146                         guint            prop_id,
147                         GValue          *value,
148                         GParamSpec      *pspec)
149 {
150   GtkArrow *arrow;
151   
152   arrow = GTK_ARROW (object);
153   switch (prop_id)
154     {
155     case PROP_ARROW_TYPE:
156       g_value_set_enum (value, arrow->arrow_type);
157       break;
158     case PROP_SHADOW_TYPE:
159       g_value_set_enum (value, arrow->shadow_type);
160       break;
161     default:
162       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163       break;
164     }
165 }
166
167 static void
168 gtk_arrow_init (GtkArrow *arrow)
169 {
170   GTK_WIDGET_SET_FLAGS (arrow, GTK_NO_WINDOW);
171
172   GTK_WIDGET (arrow)->requisition.width = MIN_ARROW_SIZE + GTK_MISC (arrow)->xpad * 2;
173   GTK_WIDGET (arrow)->requisition.height = MIN_ARROW_SIZE + GTK_MISC (arrow)->ypad * 2;
174
175   arrow->arrow_type = GTK_ARROW_RIGHT;
176   arrow->shadow_type = GTK_SHADOW_OUT;
177 }
178
179 GtkWidget*
180 gtk_arrow_new (GtkArrowType  arrow_type,
181                GtkShadowType shadow_type)
182 {
183   GtkArrow *arrow;
184
185   arrow = gtk_type_new (GTK_TYPE_ARROW);
186
187   arrow->arrow_type = arrow_type;
188   arrow->shadow_type = shadow_type;
189
190   return GTK_WIDGET (arrow);
191 }
192
193 void
194 gtk_arrow_set (GtkArrow      *arrow,
195                GtkArrowType   arrow_type,
196                GtkShadowType  shadow_type)
197 {
198   g_return_if_fail (GTK_IS_ARROW (arrow));
199
200   if (   ((GtkArrowType) arrow->arrow_type != arrow_type)
201       || ((GtkShadowType) arrow->shadow_type != shadow_type))
202     {
203       g_object_freeze_notify (G_OBJECT (arrow));
204
205       if ((GtkArrowType) arrow->arrow_type != arrow_type)
206         {
207           arrow->arrow_type = arrow_type;
208           g_object_notify (G_OBJECT (arrow), "arrow_type");
209         }
210
211       if ((GtkShadowType) arrow->shadow_type != shadow_type)
212         {
213           arrow->shadow_type = shadow_type;
214           g_object_notify (G_OBJECT (arrow), "shadow_type");
215         }
216
217       g_object_thaw_notify (G_OBJECT (arrow));
218
219       if (GTK_WIDGET_DRAWABLE (arrow))
220         gtk_widget_queue_clear (GTK_WIDGET (arrow));
221     }
222 }
223
224
225 static gboolean 
226 gtk_arrow_expose (GtkWidget      *widget,
227                   GdkEventExpose *event)
228 {
229   GtkArrow *arrow;
230   GtkMisc *misc;
231   GtkShadowType shadow_type;
232   gint width, height;
233   gint x, y;
234   gint extent;
235   gfloat xalign;
236
237   if (GTK_WIDGET_DRAWABLE (widget))
238     {
239       arrow = GTK_ARROW (widget);
240       misc = GTK_MISC (widget);
241
242       width = widget->allocation.width - misc->xpad * 2;
243       height = widget->allocation.height - misc->ypad * 2;
244       extent = MIN (width, height);
245
246       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
247         xalign = misc->xalign;
248       else
249         xalign = 1.0 - misc->xalign;
250   
251       x = floor (widget->allocation.x + misc->xpad
252                  + ((widget->allocation.width - extent) * xalign)
253                  + 0.5);
254       y = floor (widget->allocation.y + misc->ypad 
255                  + ((widget->allocation.height - extent) * misc->yalign)
256                  + 0.5);
257       
258       shadow_type = arrow->shadow_type;
259
260       if (widget->state == GTK_STATE_ACTIVE)
261         {
262           if (shadow_type == GTK_SHADOW_IN)
263             shadow_type = GTK_SHADOW_OUT;
264           else if (shadow_type == GTK_SHADOW_OUT)
265             shadow_type = GTK_SHADOW_IN;
266           else if (shadow_type == GTK_SHADOW_ETCHED_IN)
267             shadow_type = GTK_SHADOW_ETCHED_OUT;
268           else if (shadow_type == GTK_SHADOW_ETCHED_OUT)
269             shadow_type = GTK_SHADOW_ETCHED_IN;
270         }
271
272       gtk_paint_arrow (widget->style, widget->window,
273                        widget->state, shadow_type,
274                        &event->area, widget, "arrow",
275                        arrow->arrow_type, TRUE,
276                        x, y, extent, extent);
277     }
278
279   return FALSE;
280 }