]> Pileus Git - ~andy/gtk/blob - gtk/gtkarrow.c
gtk/: fully remove gtkalias hacks
[~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
53 #define MIN_ARROW_SIZE  15
54
55 enum {
56   PROP_0,
57   PROP_ARROW_TYPE,
58   PROP_SHADOW_TYPE
59 };
60
61
62 static void     gtk_arrow_set_property (GObject        *object,
63                                         guint           prop_id,
64                                         const GValue   *value,
65                                         GParamSpec     *pspec);
66 static void     gtk_arrow_get_property (GObject        *object,
67                                         guint           prop_id,
68                                         GValue         *value,
69                                         GParamSpec     *pspec);
70 static gboolean gtk_arrow_expose       (GtkWidget      *widget,
71                                         GdkEventExpose *event);
72
73
74 G_DEFINE_TYPE (GtkArrow, gtk_arrow, GTK_TYPE_MISC)
75
76
77 static void
78 gtk_arrow_class_init (GtkArrowClass *class)
79 {
80   GObjectClass *gobject_class;
81   GtkWidgetClass *widget_class;
82
83   gobject_class = (GObjectClass*) class;
84   widget_class = (GtkWidgetClass*) class;
85
86   gobject_class->set_property = gtk_arrow_set_property;
87   gobject_class->get_property = gtk_arrow_get_property;
88
89   widget_class->expose_event = gtk_arrow_expose;
90
91   g_object_class_install_property (gobject_class,
92                                    PROP_ARROW_TYPE,
93                                    g_param_spec_enum ("arrow-type",
94                                                       P_("Arrow direction"),
95                                                       P_("The direction the arrow should point"),
96                                                       GTK_TYPE_ARROW_TYPE,
97                                                       GTK_ARROW_RIGHT,
98                                                       GTK_PARAM_READWRITE));
99
100   g_object_class_install_property (gobject_class,
101                                    PROP_SHADOW_TYPE,
102                                    g_param_spec_enum ("shadow-type",
103                                                       P_("Arrow shadow"),
104                                                       P_("Appearance of the shadow surrounding the arrow"),
105                                                       GTK_TYPE_SHADOW_TYPE,
106                                                       GTK_SHADOW_OUT,
107                                                       GTK_PARAM_READWRITE));
108
109   gtk_widget_class_install_style_property (widget_class,
110                                            g_param_spec_float ("arrow-scaling",
111                                                                P_("Arrow Scaling"),
112                                                                P_("Amount of space used up by arrow"),
113                                                                0.0, 1.0, 0.7,
114                                                                GTK_PARAM_READABLE));
115 }
116
117 static void
118 gtk_arrow_set_property (GObject         *object,
119                         guint            prop_id,
120                         const GValue    *value,
121                         GParamSpec      *pspec)
122 {
123   GtkArrow *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 static void
144 gtk_arrow_get_property (GObject         *object,
145                         guint            prop_id,
146                         GValue          *value,
147                         GParamSpec      *pspec)
148 {
149   GtkArrow *arrow = GTK_ARROW (object);
150
151   switch (prop_id)
152     {
153     case PROP_ARROW_TYPE:
154       g_value_set_enum (value, arrow->arrow_type);
155       break;
156     case PROP_SHADOW_TYPE:
157       g_value_set_enum (value, arrow->shadow_type);
158       break;
159     default:
160       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161       break;
162     }
163 }
164
165 static void
166 gtk_arrow_init (GtkArrow *arrow)
167 {
168   gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
169
170   GTK_WIDGET (arrow)->requisition.width = MIN_ARROW_SIZE + GTK_MISC (arrow)->xpad * 2;
171   GTK_WIDGET (arrow)->requisition.height = MIN_ARROW_SIZE + GTK_MISC (arrow)->ypad * 2;
172
173   arrow->arrow_type = GTK_ARROW_RIGHT;
174   arrow->shadow_type = GTK_SHADOW_OUT;
175 }
176
177 /**
178  * gtk_arrow_new:
179  * @arrow_type: a valid #GtkArrowType.
180  * @shadow_type: a valid #GtkShadowType.
181  *
182  * Creates a new #GtkArrow widget.
183  *
184  * Returns: the new #GtkArrow widget.
185  */
186 GtkWidget*
187 gtk_arrow_new (GtkArrowType  arrow_type,
188                GtkShadowType shadow_type)
189 {
190   GtkArrow *arrow;
191
192   arrow = g_object_new (GTK_TYPE_ARROW, NULL);
193
194   arrow->arrow_type = arrow_type;
195   arrow->shadow_type = shadow_type;
196
197   return GTK_WIDGET (arrow);
198 }
199
200 /**
201  * gtk_arrow_set:
202  * @arrow: a widget of type #GtkArrow.
203  * @arrow_type: a valid #GtkArrowType.
204  * @shadow_type: a valid #GtkShadowType.
205  *
206  * Sets the direction and style of the #GtkArrow, @arrow.
207  */
208 void
209 gtk_arrow_set (GtkArrow      *arrow,
210                GtkArrowType   arrow_type,
211                GtkShadowType  shadow_type)
212 {
213   GtkWidget *widget;
214
215   g_return_if_fail (GTK_IS_ARROW (arrow));
216
217   if (   ((GtkArrowType) arrow->arrow_type != arrow_type)
218       || ((GtkShadowType) arrow->shadow_type != shadow_type))
219     {
220       g_object_freeze_notify (G_OBJECT (arrow));
221
222       if ((GtkArrowType) arrow->arrow_type != arrow_type)
223         {
224           arrow->arrow_type = arrow_type;
225           g_object_notify (G_OBJECT (arrow), "arrow-type");
226         }
227
228       if ((GtkShadowType) arrow->shadow_type != shadow_type)
229         {
230           arrow->shadow_type = shadow_type;
231           g_object_notify (G_OBJECT (arrow), "shadow-type");
232         }
233
234       g_object_thaw_notify (G_OBJECT (arrow));
235
236       widget = GTK_WIDGET (arrow);
237       if (gtk_widget_is_drawable (widget))
238         gtk_widget_queue_draw (widget);
239     }
240 }
241
242
243 static gboolean
244 gtk_arrow_expose (GtkWidget      *widget,
245                   GdkEventExpose *event)
246 {
247   if (gtk_widget_is_drawable (widget))
248     {
249       GtkArrow *arrow = GTK_ARROW (widget);
250       GtkMisc *misc = GTK_MISC (widget);
251       GtkShadowType shadow_type;
252       gint width, height;
253       gint x, y;
254       gint extent;
255       gfloat xalign;
256       GtkArrowType effective_arrow_type;
257       gfloat arrow_scaling;
258
259       gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
260
261       width = widget->allocation.width - misc->xpad * 2;
262       height = widget->allocation.height - misc->ypad * 2;
263       extent = MIN (width, height) * arrow_scaling;
264       effective_arrow_type = arrow->arrow_type;
265
266       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
267         xalign = misc->xalign;
268       else
269         {
270           xalign = 1.0 - misc->xalign;
271           if (arrow->arrow_type == GTK_ARROW_LEFT)
272             effective_arrow_type = GTK_ARROW_RIGHT;
273           else if (arrow->arrow_type == GTK_ARROW_RIGHT)
274             effective_arrow_type = GTK_ARROW_LEFT;
275         }
276
277       x = floor (widget->allocation.x + misc->xpad
278                  + ((widget->allocation.width - extent) * xalign));
279       y = floor (widget->allocation.y + misc->ypad
280                  + ((widget->allocation.height - extent) * misc->yalign));
281
282       shadow_type = arrow->shadow_type;
283
284       if (widget->state == GTK_STATE_ACTIVE)
285         {
286           if (shadow_type == GTK_SHADOW_IN)
287             shadow_type = GTK_SHADOW_OUT;
288           else if (shadow_type == GTK_SHADOW_OUT)
289             shadow_type = GTK_SHADOW_IN;
290           else if (shadow_type == GTK_SHADOW_ETCHED_IN)
291             shadow_type = GTK_SHADOW_ETCHED_OUT;
292           else if (shadow_type == GTK_SHADOW_ETCHED_OUT)
293             shadow_type = GTK_SHADOW_ETCHED_IN;
294         }
295
296       gtk_paint_arrow (widget->style, widget->window,
297                        widget->state, shadow_type,
298                        &event->area, widget, "arrow",
299                        effective_arrow_type, TRUE,
300                        x, y, extent, extent);
301     }
302
303   return FALSE;
304 }