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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
29 * @Short_description: Displays an arrow
31 * @See_also: gtk_render_arrow()
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.
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.
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().
50 #include "gtksizerequest.h"
51 #include "gtktypebuiltins.h"
52 #include "gtkprivate.h"
55 #include "a11y/gtkarrowaccessible.h"
57 #define MIN_ARROW_SIZE 15
59 struct _GtkArrowPrivate
72 static void gtk_arrow_set_property (GObject *object,
76 static void gtk_arrow_get_property (GObject *object,
80 static gboolean gtk_arrow_draw (GtkWidget *widget,
83 static void gtk_arrow_get_preferred_width (GtkWidget *widget,
86 static void gtk_arrow_get_preferred_height (GtkWidget *widget,
90 G_DEFINE_TYPE (GtkArrow, gtk_arrow, GTK_TYPE_MISC)
94 gtk_arrow_class_init (GtkArrowClass *class)
96 GObjectClass *gobject_class;
97 GtkWidgetClass *widget_class;
99 gobject_class = (GObjectClass*) class;
100 widget_class = (GtkWidgetClass*) class;
102 gobject_class->set_property = gtk_arrow_set_property;
103 gobject_class->get_property = gtk_arrow_get_property;
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;
109 g_object_class_install_property (gobject_class,
111 g_param_spec_enum ("arrow-type",
112 P_("Arrow direction"),
113 P_("The direction the arrow should point"),
116 GTK_PARAM_READWRITE));
118 g_object_class_install_property (gobject_class,
120 g_param_spec_enum ("shadow-type",
122 P_("Appearance of the shadow surrounding the arrow"),
123 GTK_TYPE_SHADOW_TYPE,
125 GTK_PARAM_READWRITE));
127 gtk_widget_class_install_style_property (widget_class,
128 g_param_spec_float ("arrow-scaling",
130 P_("Amount of space used up by arrow"),
132 GTK_PARAM_READABLE));
134 g_type_class_add_private (class, sizeof (GtkArrowPrivate));
136 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_ARROW_ACCESSIBLE);
140 gtk_arrow_set_property (GObject *object,
145 GtkArrow *arrow = GTK_ARROW (object);
146 GtkArrowPrivate *priv = arrow->priv;
150 case PROP_ARROW_TYPE:
151 gtk_arrow_set (arrow,
152 g_value_get_enum (value),
155 case PROP_SHADOW_TYPE:
156 gtk_arrow_set (arrow,
158 g_value_get_enum (value));
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 gtk_arrow_get_property (GObject *object,
172 GtkArrow *arrow = GTK_ARROW (object);
173 GtkArrowPrivate *priv = arrow->priv;
177 case PROP_ARROW_TYPE:
178 g_value_set_enum (value, priv->arrow_type);
180 case PROP_SHADOW_TYPE:
181 g_value_set_enum (value, priv->shadow_type);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190 gtk_arrow_init (GtkArrow *arrow)
192 GtkArrowPrivate *priv;
194 arrow->priv = G_TYPE_INSTANCE_GET_PRIVATE (arrow,
199 gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
201 priv->arrow_type = GTK_ARROW_RIGHT;
202 priv->shadow_type = GTK_SHADOW_OUT;
206 gtk_arrow_get_preferred_width (GtkWidget *widget,
212 gtk_misc_get_padding (GTK_MISC (widget), &xpad, NULL);
215 *minimum_size = MIN_ARROW_SIZE + xpad * 2;
218 *natural_size = MIN_ARROW_SIZE + xpad * 2;
222 gtk_arrow_get_preferred_height (GtkWidget *widget,
228 gtk_misc_get_padding (GTK_MISC (widget), NULL, &ypad);
231 *minimum_size = MIN_ARROW_SIZE + ypad * 2;
234 *natural_size = MIN_ARROW_SIZE + ypad * 2;
240 * @arrow_type: a valid #GtkArrowType.
241 * @shadow_type: a valid #GtkShadowType.
243 * Creates a new #GtkArrow widget.
245 * Returns: the new #GtkArrow widget.
248 gtk_arrow_new (GtkArrowType arrow_type,
249 GtkShadowType shadow_type)
251 GtkArrowPrivate *priv;
254 arrow = g_object_new (GTK_TYPE_ARROW, NULL);
258 priv->arrow_type = arrow_type;
259 priv->shadow_type = shadow_type;
261 return GTK_WIDGET (arrow);
266 * @arrow: a widget of type #GtkArrow.
267 * @arrow_type: a valid #GtkArrowType.
268 * @shadow_type: a valid #GtkShadowType.
270 * Sets the direction and style of the #GtkArrow, @arrow.
273 gtk_arrow_set (GtkArrow *arrow,
274 GtkArrowType arrow_type,
275 GtkShadowType shadow_type)
277 GtkArrowPrivate *priv;
280 g_return_if_fail (GTK_IS_ARROW (arrow));
284 if (priv->arrow_type != arrow_type
285 || priv->shadow_type != shadow_type)
287 g_object_freeze_notify (G_OBJECT (arrow));
289 if ((GtkArrowType) priv->arrow_type != arrow_type)
291 priv->arrow_type = arrow_type;
292 g_object_notify (G_OBJECT (arrow), "arrow-type");
295 if (priv->shadow_type != shadow_type)
297 priv->shadow_type = shadow_type;
298 g_object_notify (G_OBJECT (arrow), "shadow-type");
301 g_object_thaw_notify (G_OBJECT (arrow));
303 widget = GTK_WIDGET (arrow);
304 if (gtk_widget_is_drawable (widget))
305 gtk_widget_queue_draw (widget);
311 gtk_arrow_draw (GtkWidget *widget,
314 GtkArrow *arrow = GTK_ARROW (widget);
315 GtkArrowPrivate *priv = arrow->priv;
316 GtkMisc *misc = GTK_MISC (widget);
317 GtkStyleContext *context;
323 gfloat xalign, yalign;
324 GtkArrowType effective_arrow_type;
325 gfloat arrow_scaling;
328 if (priv->arrow_type == GTK_ARROW_NONE)
331 context = gtk_widget_get_style_context (widget);
332 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
334 gtk_misc_get_padding (misc, &xpad, &ypad);
335 gtk_misc_get_alignment (misc, &xalign, &yalign);
337 width = gtk_widget_get_allocated_width (widget) - 2 * xpad;
338 height = gtk_widget_get_allocated_height (widget) - 2 * ypad;
340 extent = MIN (width, height) * arrow_scaling;
341 effective_arrow_type = priv->arrow_type;
343 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
345 xalign = 1.0 - xalign;
346 if (priv->arrow_type == GTK_ARROW_LEFT)
347 effective_arrow_type = GTK_ARROW_RIGHT;
348 else if (priv->arrow_type == GTK_ARROW_RIGHT)
349 effective_arrow_type = GTK_ARROW_LEFT;
352 x = xpad + ((width - extent) * xalign);
353 y = ypad + ((height - extent) * yalign);
355 switch (effective_arrow_type)
360 case GTK_ARROW_RIGHT:
368 angle = (3 * G_PI) / 2;
372 gtk_style_context_save (context);
374 state = gtk_widget_get_state_flags (widget);
375 gtk_style_context_set_state (context, state);
376 gtk_render_arrow (context, cr, angle, x, y, extent);
378 gtk_style_context_restore (context);