]> Pileus Git - ~andy/gtk/blob - gtk/gtkarrow.c
Fix a typo in the docs. (#390423, David Lodge)
[~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   gtk_widget_class_install_style_property (widget_class,
90                                            g_param_spec_float ("arrow-scaling",
91                                                                P_("Arrow Scaling"),
92                                                                P_("Amount of space used up by arrow"),
93                                                                0.0, 1.0, 0.7,
94                                                                GTK_PARAM_READABLE));
95
96   widget_class->expose_event = gtk_arrow_expose;
97 }
98
99 static void
100 gtk_arrow_set_property (GObject         *object,
101                         guint            prop_id,
102                         const GValue    *value,
103                         GParamSpec      *pspec)
104 {
105   GtkArrow *arrow;
106   
107   arrow = GTK_ARROW (object);
108
109   switch (prop_id)
110     {
111     case PROP_ARROW_TYPE:
112       gtk_arrow_set (arrow,
113                      g_value_get_enum (value),
114                      arrow->shadow_type);
115       break;
116     case PROP_SHADOW_TYPE:
117       gtk_arrow_set (arrow,
118                      arrow->arrow_type,
119                      g_value_get_enum (value));
120       break;
121     default:
122       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123       break;
124     }
125 }
126
127
128 static void
129 gtk_arrow_get_property (GObject         *object,
130                         guint            prop_id,
131                         GValue          *value,
132                         GParamSpec      *pspec)
133 {
134   GtkArrow *arrow;
135   
136   arrow = GTK_ARROW (object);
137   switch (prop_id)
138     {
139     case PROP_ARROW_TYPE:
140       g_value_set_enum (value, arrow->arrow_type);
141       break;
142     case PROP_SHADOW_TYPE:
143       g_value_set_enum (value, arrow->shadow_type);
144       break;
145     default:
146       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147       break;
148     }
149 }
150
151 static void
152 gtk_arrow_init (GtkArrow *arrow)
153 {
154   GTK_WIDGET_SET_FLAGS (arrow, GTK_NO_WINDOW);
155
156   GTK_WIDGET (arrow)->requisition.width = MIN_ARROW_SIZE + GTK_MISC (arrow)->xpad * 2;
157   GTK_WIDGET (arrow)->requisition.height = MIN_ARROW_SIZE + GTK_MISC (arrow)->ypad * 2;
158
159   arrow->arrow_type = GTK_ARROW_RIGHT;
160   arrow->shadow_type = GTK_SHADOW_OUT;
161 }
162
163 GtkWidget*
164 gtk_arrow_new (GtkArrowType  arrow_type,
165                GtkShadowType shadow_type)
166 {
167   GtkArrow *arrow;
168
169   arrow = g_object_new (GTK_TYPE_ARROW, NULL);
170
171   arrow->arrow_type = arrow_type;
172   arrow->shadow_type = shadow_type;
173
174   return GTK_WIDGET (arrow);
175 }
176
177 void
178 gtk_arrow_set (GtkArrow      *arrow,
179                GtkArrowType   arrow_type,
180                GtkShadowType  shadow_type)
181 {
182   g_return_if_fail (GTK_IS_ARROW (arrow));
183
184   if (   ((GtkArrowType) arrow->arrow_type != arrow_type)
185       || ((GtkShadowType) arrow->shadow_type != shadow_type))
186     {
187       g_object_freeze_notify (G_OBJECT (arrow));
188
189       if ((GtkArrowType) arrow->arrow_type != arrow_type)
190         {
191           arrow->arrow_type = arrow_type;
192           g_object_notify (G_OBJECT (arrow), "arrow-type");
193         }
194
195       if ((GtkShadowType) arrow->shadow_type != shadow_type)
196         {
197           arrow->shadow_type = shadow_type;
198           g_object_notify (G_OBJECT (arrow), "shadow-type");
199         }
200
201       g_object_thaw_notify (G_OBJECT (arrow));
202
203       if (GTK_WIDGET_DRAWABLE (arrow))
204         gtk_widget_queue_draw (GTK_WIDGET (arrow));
205     }
206 }
207
208
209 static gboolean 
210 gtk_arrow_expose (GtkWidget      *widget,
211                   GdkEventExpose *event)
212 {
213   GtkShadowType shadow_type;
214   gint width, height;
215   gint x, y;
216   gint extent;
217   gfloat xalign;
218   GtkArrowType effective_arrow_type;
219
220   if (GTK_WIDGET_DRAWABLE (widget))
221     {
222       GtkArrow *arrow = GTK_ARROW (widget);
223       GtkMisc *misc = GTK_MISC (widget);
224       gfloat arrow_scaling;
225       gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
226
227       width = widget->allocation.width - misc->xpad * 2;
228       height = widget->allocation.height - misc->ypad * 2;
229       extent = MIN (width, height) * arrow_scaling;
230       effective_arrow_type = arrow->arrow_type;
231
232       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
233         xalign = misc->xalign;
234       else
235         {
236           xalign = 1.0 - misc->xalign;
237           if (arrow->arrow_type == GTK_ARROW_LEFT)
238             effective_arrow_type = GTK_ARROW_RIGHT;
239           else if (arrow->arrow_type == GTK_ARROW_RIGHT)
240             effective_arrow_type = GTK_ARROW_LEFT;
241         }
242
243       x = floor (widget->allocation.x + misc->xpad
244                  + ((widget->allocation.width - extent) * xalign));
245       y = floor (widget->allocation.y + misc->ypad 
246                  + ((widget->allocation.height - extent) * misc->yalign));
247       
248       shadow_type = arrow->shadow_type;
249
250       if (widget->state == GTK_STATE_ACTIVE)
251         {
252           if (shadow_type == GTK_SHADOW_IN)
253             shadow_type = GTK_SHADOW_OUT;
254           else if (shadow_type == GTK_SHADOW_OUT)
255             shadow_type = GTK_SHADOW_IN;
256           else if (shadow_type == GTK_SHADOW_ETCHED_IN)
257             shadow_type = GTK_SHADOW_ETCHED_OUT;
258           else if (shadow_type == GTK_SHADOW_ETCHED_OUT)
259             shadow_type = GTK_SHADOW_ETCHED_IN;
260         }
261
262       gtk_paint_arrow (widget->style, widget->window,
263                        widget->state, shadow_type,
264                        &event->area, widget, "arrow",
265                        effective_arrow_type, TRUE,
266                        x, y, extent, extent);
267     }
268
269   return FALSE;
270 }
271
272 #define __GTK_ARROW_C__
273 #include "gtkaliasdef.c"