]> Pileus Git - ~andy/gtk/blob - gtk/gtkarrow.c
Merge branch 'native-layout-incubator'
[~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 /**
28  * SECTION:gtkarrow
29  * @Short_description: Displays an arrow
30  * @Title: GtkArrow
31  * @See_also: gtk_paint_arrow()
32  *
33  * GtkArrow should be used to draw simple arrows that need to point in
34  * one of the four cardinal directions (up, down, left, or right).  The
35  * style of the arrow can be one of shadow in, shadow out, etched in, or
36  * etched out.  Note that these directions and style types may be
37  * ammended in versions of GTK+ to come.
38  *
39  * GtkArrow will fill any space alloted to it, but since it is inherited
40  * from #GtkMisc, it can be padded and/or aligned, to fill exactly the
41  * space the programmer desires.
42  *
43  * Arrows are created with a call to gtk_arrow_new().  The direction or
44  * style of an arrow can be changed after creation by using gtk_arrow_set().
45  */
46
47 #include "config.h"
48 #include <math.h>
49 #include "gtkarrow.h"
50 #include "gtkprivate.h"
51 #include "gtkintl.h"
52 #include "gtkalias.h"
53
54 #define MIN_ARROW_SIZE  15
55
56 enum {
57   PROP_0,
58   PROP_ARROW_TYPE,
59   PROP_SHADOW_TYPE
60 };
61
62
63 static void     gtk_arrow_set_property (GObject        *object,
64                                         guint           prop_id,
65                                         const GValue   *value,
66                                         GParamSpec     *pspec);
67 static void     gtk_arrow_get_property (GObject        *object,
68                                         guint           prop_id,
69                                         GValue         *value,
70                                         GParamSpec     *pspec);
71 static gboolean gtk_arrow_expose       (GtkWidget      *widget,
72                                         GdkEventExpose *event);
73
74
75 G_DEFINE_TYPE (GtkArrow, gtk_arrow, GTK_TYPE_MISC)
76
77
78 static void
79 gtk_arrow_class_init (GtkArrowClass *class)
80 {
81   GObjectClass *gobject_class;
82   GtkWidgetClass *widget_class;
83
84   gobject_class = (GObjectClass*) class;
85   widget_class = (GtkWidgetClass*) class;
86
87   gobject_class->set_property = gtk_arrow_set_property;
88   gobject_class->get_property = gtk_arrow_get_property;
89
90   widget_class->expose_event = gtk_arrow_expose;
91
92   g_object_class_install_property (gobject_class,
93                                    PROP_ARROW_TYPE,
94                                    g_param_spec_enum ("arrow-type",
95                                                       P_("Arrow direction"),
96                                                       P_("The direction the arrow should point"),
97                                                       GTK_TYPE_ARROW_TYPE,
98                                                       GTK_ARROW_RIGHT,
99                                                       GTK_PARAM_READWRITE));
100
101   g_object_class_install_property (gobject_class,
102                                    PROP_SHADOW_TYPE,
103                                    g_param_spec_enum ("shadow-type",
104                                                       P_("Arrow shadow"),
105                                                       P_("Appearance of the shadow surrounding the arrow"),
106                                                       GTK_TYPE_SHADOW_TYPE,
107                                                       GTK_SHADOW_OUT,
108                                                       GTK_PARAM_READWRITE));
109
110   gtk_widget_class_install_style_property (widget_class,
111                                            g_param_spec_float ("arrow-scaling",
112                                                                P_("Arrow Scaling"),
113                                                                P_("Amount of space used up by arrow"),
114                                                                0.0, 1.0, 0.7,
115                                                                GTK_PARAM_READABLE));
116 }
117
118 static void
119 gtk_arrow_set_property (GObject         *object,
120                         guint            prop_id,
121                         const GValue    *value,
122                         GParamSpec      *pspec)
123 {
124   GtkArrow *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 static void
145 gtk_arrow_get_property (GObject         *object,
146                         guint            prop_id,
147                         GValue          *value,
148                         GParamSpec      *pspec)
149 {
150   GtkArrow *arrow = GTK_ARROW (object);
151
152   switch (prop_id)
153     {
154     case PROP_ARROW_TYPE:
155       g_value_set_enum (value, arrow->arrow_type);
156       break;
157     case PROP_SHADOW_TYPE:
158       g_value_set_enum (value, arrow->shadow_type);
159       break;
160     default:
161       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162       break;
163     }
164 }
165
166 static void
167 gtk_arrow_init (GtkArrow *arrow)
168 {
169   gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
170
171   GTK_WIDGET (arrow)->requisition.width = MIN_ARROW_SIZE + GTK_MISC (arrow)->xpad * 2;
172   GTK_WIDGET (arrow)->requisition.height = MIN_ARROW_SIZE + GTK_MISC (arrow)->ypad * 2;
173
174   arrow->arrow_type = GTK_ARROW_RIGHT;
175   arrow->shadow_type = GTK_SHADOW_OUT;
176 }
177
178 /**
179  * gtk_arrow_new:
180  * @arrow_type: a valid #GtkArrowType.
181  * @shadow_type: a valid #GtkShadowType.
182  *
183  * Creates a new #GtkArrow widget.
184  *
185  * Returns: the new #GtkArrow widget.
186  */
187 GtkWidget*
188 gtk_arrow_new (GtkArrowType  arrow_type,
189                GtkShadowType shadow_type)
190 {
191   GtkArrow *arrow;
192
193   arrow = g_object_new (GTK_TYPE_ARROW, NULL);
194
195   arrow->arrow_type = arrow_type;
196   arrow->shadow_type = shadow_type;
197
198   return GTK_WIDGET (arrow);
199 }
200
201 /**
202  * gtk_arrow_set:
203  * @arrow: a widget of type #GtkArrow.
204  * @arrow_type: a valid #GtkArrowType.
205  * @shadow_type: a valid #GtkShadowType.
206  *
207  * Sets the direction and style of the #GtkArrow, @arrow.
208  */
209 void
210 gtk_arrow_set (GtkArrow      *arrow,
211                GtkArrowType   arrow_type,
212                GtkShadowType  shadow_type)
213 {
214   GtkWidget *widget;
215
216   g_return_if_fail (GTK_IS_ARROW (arrow));
217
218   if (   ((GtkArrowType) arrow->arrow_type != arrow_type)
219       || ((GtkShadowType) arrow->shadow_type != shadow_type))
220     {
221       g_object_freeze_notify (G_OBJECT (arrow));
222
223       if ((GtkArrowType) arrow->arrow_type != arrow_type)
224         {
225           arrow->arrow_type = arrow_type;
226           g_object_notify (G_OBJECT (arrow), "arrow-type");
227         }
228
229       if ((GtkShadowType) arrow->shadow_type != shadow_type)
230         {
231           arrow->shadow_type = shadow_type;
232           g_object_notify (G_OBJECT (arrow), "shadow-type");
233         }
234
235       g_object_thaw_notify (G_OBJECT (arrow));
236
237       widget = GTK_WIDGET (arrow);
238       if (gtk_widget_is_drawable (widget))
239         gtk_widget_queue_draw (widget);
240     }
241 }
242
243
244 static gboolean
245 gtk_arrow_expose (GtkWidget      *widget,
246                   GdkEventExpose *event)
247 {
248   if (gtk_widget_is_drawable (widget))
249     {
250       GtkArrow *arrow = GTK_ARROW (widget);
251       GtkMisc *misc = GTK_MISC (widget);
252       GtkShadowType shadow_type;
253       gint width, height;
254       gint x, y;
255       gint extent;
256       gfloat xalign;
257       GtkArrowType effective_arrow_type;
258       gfloat arrow_scaling;
259
260       gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
261
262       width = widget->allocation.width - misc->xpad * 2;
263       height = widget->allocation.height - misc->ypad * 2;
264       extent = MIN (width, height) * arrow_scaling;
265       effective_arrow_type = arrow->arrow_type;
266
267       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
268         xalign = misc->xalign;
269       else
270         {
271           xalign = 1.0 - misc->xalign;
272           if (arrow->arrow_type == GTK_ARROW_LEFT)
273             effective_arrow_type = GTK_ARROW_RIGHT;
274           else if (arrow->arrow_type == GTK_ARROW_RIGHT)
275             effective_arrow_type = GTK_ARROW_LEFT;
276         }
277
278       x = floor (widget->allocation.x + misc->xpad
279                  + ((widget->allocation.width - extent) * xalign));
280       y = floor (widget->allocation.y + misc->ypad
281                  + ((widget->allocation.height - extent) * misc->yalign));
282
283       shadow_type = arrow->shadow_type;
284
285       if (widget->state == GTK_STATE_ACTIVE)
286         {
287           if (shadow_type == GTK_SHADOW_IN)
288             shadow_type = GTK_SHADOW_OUT;
289           else if (shadow_type == GTK_SHADOW_OUT)
290             shadow_type = GTK_SHADOW_IN;
291           else if (shadow_type == GTK_SHADOW_ETCHED_IN)
292             shadow_type = GTK_SHADOW_ETCHED_OUT;
293           else if (shadow_type == GTK_SHADOW_ETCHED_OUT)
294             shadow_type = GTK_SHADOW_ETCHED_IN;
295         }
296
297       gtk_paint_arrow (widget->style, widget->window,
298                        widget->state, shadow_type,
299                        &event->area, widget, "arrow",
300                        effective_arrow_type, TRUE,
301                        x, y, extent, extent);
302     }
303
304   return FALSE;
305 }
306
307 #define __GTK_ARROW_C__
308 #include "gtkaliasdef.c"