1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2001. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 * @Short_description: Displays an arrow
29 * @See_also: gtk_render_arrow()
31 * GtkArrow should be used to draw simple arrows that need to point in
32 * one of the four cardinal directions (up, down, left, or right). The
33 * style of the arrow can be one of shadow in, shadow out, etched in, or
34 * etched out. Note that these directions and style types may be
35 * ammended in versions of GTK+ to come.
37 * GtkArrow will fill any space alloted to it, but since it is inherited
38 * from #GtkMisc, it can be padded and/or aligned, to fill exactly the
39 * space the programmer desires.
41 * Arrows are created with a call to gtk_arrow_new(). The direction or
42 * style of an arrow can be changed after creation by using gtk_arrow_set().
48 #include "gtksizerequest.h"
49 #include "gtktypebuiltins.h"
50 #include "gtkprivate.h"
53 #include "a11y/gtkarrowaccessible.h"
55 #define MIN_ARROW_SIZE 15
57 struct _GtkArrowPrivate
70 static void gtk_arrow_set_property (GObject *object,
74 static void gtk_arrow_get_property (GObject *object,
78 static gboolean gtk_arrow_draw (GtkWidget *widget,
81 static void gtk_arrow_get_preferred_width (GtkWidget *widget,
84 static void gtk_arrow_get_preferred_height (GtkWidget *widget,
88 G_DEFINE_TYPE (GtkArrow, gtk_arrow, GTK_TYPE_MISC)
92 gtk_arrow_class_init (GtkArrowClass *class)
94 GObjectClass *gobject_class;
95 GtkWidgetClass *widget_class;
97 gobject_class = (GObjectClass*) class;
98 widget_class = (GtkWidgetClass*) class;
100 gobject_class->set_property = gtk_arrow_set_property;
101 gobject_class->get_property = gtk_arrow_get_property;
103 widget_class->draw = gtk_arrow_draw;
104 widget_class->get_preferred_width = gtk_arrow_get_preferred_width;
105 widget_class->get_preferred_height = gtk_arrow_get_preferred_height;
107 g_object_class_install_property (gobject_class,
109 g_param_spec_enum ("arrow-type",
110 P_("Arrow direction"),
111 P_("The direction the arrow should point"),
114 GTK_PARAM_READWRITE));
116 g_object_class_install_property (gobject_class,
118 g_param_spec_enum ("shadow-type",
120 P_("Appearance of the shadow surrounding the arrow"),
121 GTK_TYPE_SHADOW_TYPE,
123 GTK_PARAM_READWRITE));
125 gtk_widget_class_install_style_property (widget_class,
126 g_param_spec_float ("arrow-scaling",
128 P_("Amount of space used up by arrow"),
130 GTK_PARAM_READABLE));
132 g_type_class_add_private (class, sizeof (GtkArrowPrivate));
134 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_ARROW_ACCESSIBLE);
138 gtk_arrow_set_property (GObject *object,
143 GtkArrow *arrow = GTK_ARROW (object);
144 GtkArrowPrivate *priv = arrow->priv;
148 case PROP_ARROW_TYPE:
149 gtk_arrow_set (arrow,
150 g_value_get_enum (value),
153 case PROP_SHADOW_TYPE:
154 gtk_arrow_set (arrow,
156 g_value_get_enum (value));
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 gtk_arrow_get_property (GObject *object,
170 GtkArrow *arrow = GTK_ARROW (object);
171 GtkArrowPrivate *priv = arrow->priv;
175 case PROP_ARROW_TYPE:
176 g_value_set_enum (value, priv->arrow_type);
178 case PROP_SHADOW_TYPE:
179 g_value_set_enum (value, priv->shadow_type);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 gtk_arrow_init (GtkArrow *arrow)
190 GtkArrowPrivate *priv;
192 arrow->priv = G_TYPE_INSTANCE_GET_PRIVATE (arrow,
197 gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
199 priv->arrow_type = GTK_ARROW_RIGHT;
200 priv->shadow_type = GTK_SHADOW_OUT;
204 gtk_arrow_get_preferred_width (GtkWidget *widget,
210 _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
213 *minimum_size = MIN_ARROW_SIZE + border.left + border.right;
216 *natural_size = MIN_ARROW_SIZE + border.left + border.right;
220 gtk_arrow_get_preferred_height (GtkWidget *widget,
226 _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
229 *minimum_size = MIN_ARROW_SIZE + border.top + border.bottom;
232 *natural_size = MIN_ARROW_SIZE + border.top + border.bottom;
237 * @arrow_type: a valid #GtkArrowType.
238 * @shadow_type: a valid #GtkShadowType.
240 * Creates a new #GtkArrow widget.
242 * Returns: the new #GtkArrow widget.
245 gtk_arrow_new (GtkArrowType arrow_type,
246 GtkShadowType shadow_type)
248 GtkArrowPrivate *priv;
251 arrow = g_object_new (GTK_TYPE_ARROW, NULL);
255 priv->arrow_type = arrow_type;
256 priv->shadow_type = shadow_type;
258 return GTK_WIDGET (arrow);
263 * @arrow: a widget of type #GtkArrow.
264 * @arrow_type: a valid #GtkArrowType.
265 * @shadow_type: a valid #GtkShadowType.
267 * Sets the direction and style of the #GtkArrow, @arrow.
270 gtk_arrow_set (GtkArrow *arrow,
271 GtkArrowType arrow_type,
272 GtkShadowType shadow_type)
274 GtkArrowPrivate *priv;
277 g_return_if_fail (GTK_IS_ARROW (arrow));
281 if (priv->arrow_type != arrow_type
282 || priv->shadow_type != shadow_type)
284 g_object_freeze_notify (G_OBJECT (arrow));
286 if ((GtkArrowType) priv->arrow_type != arrow_type)
288 priv->arrow_type = arrow_type;
289 g_object_notify (G_OBJECT (arrow), "arrow-type");
292 if (priv->shadow_type != shadow_type)
294 priv->shadow_type = shadow_type;
295 g_object_notify (G_OBJECT (arrow), "shadow-type");
298 g_object_thaw_notify (G_OBJECT (arrow));
300 widget = GTK_WIDGET (arrow);
301 if (gtk_widget_is_drawable (widget))
302 gtk_widget_queue_draw (widget);
307 gtk_arrow_draw (GtkWidget *widget,
310 GtkArrow *arrow = GTK_ARROW (widget);
311 GtkArrowPrivate *priv = arrow->priv;
312 GtkStyleContext *context;
317 gfloat xalign, yalign;
318 GtkArrowType effective_arrow_type;
319 gfloat arrow_scaling;
322 if (priv->arrow_type == GTK_ARROW_NONE)
325 context = gtk_widget_get_style_context (widget);
326 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
328 _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
329 gtk_misc_get_alignment (GTK_MISC (widget), &xalign, &yalign);
331 width = gtk_widget_get_allocated_width (widget) - border.left - border.right;
332 height = gtk_widget_get_allocated_height (widget) - border.top - border.bottom;
334 extent = MIN (width, height) * arrow_scaling;
335 effective_arrow_type = priv->arrow_type;
337 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
339 xalign = 1.0 - xalign;
340 if (priv->arrow_type == GTK_ARROW_LEFT)
341 effective_arrow_type = GTK_ARROW_RIGHT;
342 else if (priv->arrow_type == GTK_ARROW_RIGHT)
343 effective_arrow_type = GTK_ARROW_LEFT;
346 x = border.left + ((width - extent) * xalign);
347 y = border.top + ((height - extent) * yalign);
349 switch (effective_arrow_type)
354 case GTK_ARROW_RIGHT:
362 angle = (3 * G_PI) / 2;
366 gtk_render_arrow (context, cr, angle, x, y, extent);