]> Pileus Git - ~andy/gtk/blob - gtk/gtkarrow.c
7c7e38d3beba7886e84544fea1d2b8b31482e429
[~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_render_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 "gtksizerequest.h"
51 #include "gtktypebuiltins.h"
52 #include "gtkprivate.h"
53 #include "gtkintl.h"
54
55 #include "a11y/gtkarrowaccessible.h"
56
57 #define MIN_ARROW_SIZE  15
58
59 struct _GtkArrowPrivate
60 {
61   gint16 arrow_type;
62   gint16 shadow_type;
63 };
64
65 enum {
66   PROP_0,
67   PROP_ARROW_TYPE,
68   PROP_SHADOW_TYPE
69 };
70
71
72 static void     gtk_arrow_set_property (GObject        *object,
73                                         guint           prop_id,
74                                         const GValue   *value,
75                                         GParamSpec     *pspec);
76 static void     gtk_arrow_get_property (GObject        *object,
77                                         guint           prop_id,
78                                         GValue         *value,
79                                         GParamSpec     *pspec);
80 static gboolean gtk_arrow_draw         (GtkWidget      *widget,
81                                         cairo_t        *cr);
82
83 static void     gtk_arrow_get_preferred_width         (GtkWidget           *widget,
84                                                        gint                *minimum_size,
85                                                        gint                *natural_size);
86 static void     gtk_arrow_get_preferred_height        (GtkWidget           *widget,
87                                                        gint                *minimum_size,
88                                                        gint                *natural_size);
89
90 G_DEFINE_TYPE (GtkArrow, gtk_arrow, GTK_TYPE_MISC)
91
92
93 static void
94 gtk_arrow_class_init (GtkArrowClass *class)
95 {
96   GObjectClass *gobject_class;
97   GtkWidgetClass *widget_class;
98
99   gobject_class = (GObjectClass*) class;
100   widget_class = (GtkWidgetClass*) class;
101
102   gobject_class->set_property = gtk_arrow_set_property;
103   gobject_class->get_property = gtk_arrow_get_property;
104
105   widget_class->draw = gtk_arrow_draw;
106   widget_class->get_preferred_width  = gtk_arrow_get_preferred_width;
107   widget_class->get_preferred_height = gtk_arrow_get_preferred_height;
108
109   g_object_class_install_property (gobject_class,
110                                    PROP_ARROW_TYPE,
111                                    g_param_spec_enum ("arrow-type",
112                                                       P_("Arrow direction"),
113                                                       P_("The direction the arrow should point"),
114                                                       GTK_TYPE_ARROW_TYPE,
115                                                       GTK_ARROW_RIGHT,
116                                                       GTK_PARAM_READWRITE));
117
118   g_object_class_install_property (gobject_class,
119                                    PROP_SHADOW_TYPE,
120                                    g_param_spec_enum ("shadow-type",
121                                                       P_("Arrow shadow"),
122                                                       P_("Appearance of the shadow surrounding the arrow"),
123                                                       GTK_TYPE_SHADOW_TYPE,
124                                                       GTK_SHADOW_OUT,
125                                                       GTK_PARAM_READWRITE));
126
127   gtk_widget_class_install_style_property (widget_class,
128                                            g_param_spec_float ("arrow-scaling",
129                                                                P_("Arrow Scaling"),
130                                                                P_("Amount of space used up by arrow"),
131                                                                0.0, 1.0, 0.7,
132                                                                GTK_PARAM_READABLE));
133
134   g_type_class_add_private (class, sizeof (GtkArrowPrivate));
135
136   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_ARROW_ACCESSIBLE);
137 }
138
139 static void
140 gtk_arrow_set_property (GObject         *object,
141                         guint            prop_id,
142                         const GValue    *value,
143                         GParamSpec      *pspec)
144 {
145   GtkArrow *arrow = GTK_ARROW (object);
146   GtkArrowPrivate *priv = arrow->priv;
147
148   switch (prop_id)
149     {
150     case PROP_ARROW_TYPE:
151       gtk_arrow_set (arrow,
152                      g_value_get_enum (value),
153                      priv->shadow_type);
154       break;
155     case PROP_SHADOW_TYPE:
156       gtk_arrow_set (arrow,
157                      priv->arrow_type,
158                      g_value_get_enum (value));
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_get_property (GObject         *object,
168                         guint            prop_id,
169                         GValue          *value,
170                         GParamSpec      *pspec)
171 {
172   GtkArrow *arrow = GTK_ARROW (object);
173   GtkArrowPrivate *priv = arrow->priv;
174
175   switch (prop_id)
176     {
177     case PROP_ARROW_TYPE:
178       g_value_set_enum (value, priv->arrow_type);
179       break;
180     case PROP_SHADOW_TYPE:
181       g_value_set_enum (value, priv->shadow_type);
182       break;
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186     }
187 }
188
189 static void
190 gtk_arrow_init (GtkArrow *arrow)
191 {
192   GtkArrowPrivate *priv;
193
194   arrow->priv = G_TYPE_INSTANCE_GET_PRIVATE (arrow,
195                                              GTK_TYPE_ARROW,
196                                              GtkArrowPrivate);
197   priv = arrow->priv;
198
199   gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
200
201   priv->arrow_type = GTK_ARROW_RIGHT;
202   priv->shadow_type = GTK_SHADOW_OUT;
203 }
204
205 static void
206 gtk_arrow_get_preferred_width (GtkWidget *widget,
207                                gint      *minimum_size,
208                                gint      *natural_size)
209 {
210   GtkBorder border;
211
212   _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
213
214   if (minimum_size)
215     *minimum_size = MIN_ARROW_SIZE + border.left + border.right;
216
217   if (natural_size)
218     *natural_size = MIN_ARROW_SIZE + border.left + border.right;
219 }
220
221 static void
222 gtk_arrow_get_preferred_height (GtkWidget *widget,
223                                 gint      *minimum_size,
224                                 gint      *natural_size)
225 {
226   GtkBorder border;
227
228   _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
229
230   if (minimum_size)
231     *minimum_size = MIN_ARROW_SIZE + border.top + border.bottom;
232
233   if (natural_size)
234     *natural_size = MIN_ARROW_SIZE + border.top + border.bottom;
235 }
236
237 /**
238  * gtk_arrow_new:
239  * @arrow_type: a valid #GtkArrowType.
240  * @shadow_type: a valid #GtkShadowType.
241  *
242  * Creates a new #GtkArrow widget.
243  *
244  * Returns: the new #GtkArrow widget.
245  */
246 GtkWidget*
247 gtk_arrow_new (GtkArrowType  arrow_type,
248                GtkShadowType shadow_type)
249 {
250   GtkArrowPrivate *priv;
251   GtkArrow *arrow;
252
253   arrow = g_object_new (GTK_TYPE_ARROW, NULL);
254
255   priv = arrow->priv;
256
257   priv->arrow_type = arrow_type;
258   priv->shadow_type = shadow_type;
259
260   return GTK_WIDGET (arrow);
261 }
262
263 /**
264  * gtk_arrow_set:
265  * @arrow: a widget of type #GtkArrow.
266  * @arrow_type: a valid #GtkArrowType.
267  * @shadow_type: a valid #GtkShadowType.
268  *
269  * Sets the direction and style of the #GtkArrow, @arrow.
270  */
271 void
272 gtk_arrow_set (GtkArrow      *arrow,
273                GtkArrowType   arrow_type,
274                GtkShadowType  shadow_type)
275 {
276   GtkArrowPrivate *priv;
277   GtkWidget *widget;
278
279   g_return_if_fail (GTK_IS_ARROW (arrow));
280
281   priv = arrow->priv;
282
283   if (priv->arrow_type != arrow_type
284       || priv->shadow_type != shadow_type)
285     {
286       g_object_freeze_notify (G_OBJECT (arrow));
287
288       if ((GtkArrowType) priv->arrow_type != arrow_type)
289         {
290           priv->arrow_type = arrow_type;
291           g_object_notify (G_OBJECT (arrow), "arrow-type");
292         }
293
294       if (priv->shadow_type != shadow_type)
295         {
296           priv->shadow_type = shadow_type;
297           g_object_notify (G_OBJECT (arrow), "shadow-type");
298         }
299
300       g_object_thaw_notify (G_OBJECT (arrow));
301
302       widget = GTK_WIDGET (arrow);
303       if (gtk_widget_is_drawable (widget))
304         gtk_widget_queue_draw (widget);
305     }
306 }
307
308 static gboolean
309 gtk_arrow_draw (GtkWidget *widget,
310                 cairo_t   *cr)
311 {
312   GtkArrow *arrow = GTK_ARROW (widget);
313   GtkArrowPrivate *priv = arrow->priv;
314   GtkStyleContext *context;
315   gdouble x, y;
316   gint width, height;
317   gint extent;
318   GtkBorder border;
319   gfloat xalign, yalign;
320   GtkArrowType effective_arrow_type;
321   gfloat arrow_scaling;
322   gdouble angle;
323
324   if (priv->arrow_type == GTK_ARROW_NONE)
325     return FALSE;
326
327   context = gtk_widget_get_style_context (widget);
328   gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
329
330   _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
331   gtk_misc_get_alignment (GTK_MISC (widget), &xalign, &yalign);
332
333   width = gtk_widget_get_allocated_width (widget) - border.left - border.right;
334   height = gtk_widget_get_allocated_height (widget) - border.top - border.bottom;
335
336   extent = MIN (width, height) * arrow_scaling;
337   effective_arrow_type = priv->arrow_type;
338
339   if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
340     {
341       xalign = 1.0 - xalign;
342       if (priv->arrow_type == GTK_ARROW_LEFT)
343         effective_arrow_type = GTK_ARROW_RIGHT;
344       else if (priv->arrow_type == GTK_ARROW_RIGHT)
345         effective_arrow_type = GTK_ARROW_LEFT;
346     }
347
348   x = border.left + ((width - extent) * xalign);
349   y = border.top + ((height - extent) * yalign);
350
351   switch (effective_arrow_type)
352     {
353     case GTK_ARROW_UP:
354       angle = 0;
355       break;
356     case GTK_ARROW_RIGHT:
357       angle = G_PI / 2;
358       break;
359     case GTK_ARROW_DOWN:
360       angle = G_PI;
361       break;
362     case GTK_ARROW_LEFT:
363     default:
364       angle = (3 * G_PI) / 2;
365       break;
366     }
367
368   gtk_render_arrow (context, cr, angle, x, y, extent);
369
370   return FALSE;
371 }