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