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