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 #define MINIMUM_ARROW_SIZE 5
56 #define NATURAL_ARROW_SIZE 15
58 struct _GtkArrowPrivate
71 static void gtk_arrow_set_property (GObject *object,
75 static void gtk_arrow_get_property (GObject *object,
79 static gboolean gtk_arrow_draw (GtkWidget *widget,
82 static void gtk_arrow_get_preferred_width (GtkWidget *widget,
85 static void gtk_arrow_get_preferred_height (GtkWidget *widget,
89 G_DEFINE_TYPE (GtkArrow, gtk_arrow, GTK_TYPE_MISC)
93 gtk_arrow_class_init (GtkArrowClass *class)
95 GObjectClass *gobject_class;
96 GtkWidgetClass *widget_class;
98 gobject_class = (GObjectClass*) class;
99 widget_class = (GtkWidgetClass*) class;
101 gobject_class->set_property = gtk_arrow_set_property;
102 gobject_class->get_property = gtk_arrow_get_property;
104 widget_class->draw = gtk_arrow_draw;
105 widget_class->get_preferred_width = gtk_arrow_get_preferred_width;
106 widget_class->get_preferred_height = gtk_arrow_get_preferred_height;
108 g_object_class_install_property (gobject_class,
110 g_param_spec_enum ("arrow-type",
111 P_("Arrow direction"),
112 P_("The direction the arrow should point"),
115 GTK_PARAM_READWRITE));
117 g_object_class_install_property (gobject_class,
119 g_param_spec_enum ("shadow-type",
121 P_("Appearance of the shadow surrounding the arrow"),
122 GTK_TYPE_SHADOW_TYPE,
124 GTK_PARAM_READWRITE));
126 gtk_widget_class_install_style_property (widget_class,
127 g_param_spec_float ("arrow-scaling",
129 P_("Amount of space used up by arrow"),
131 GTK_PARAM_READABLE));
133 g_type_class_add_private (class, sizeof (GtkArrowPrivate));
137 gtk_arrow_set_property (GObject *object,
142 GtkArrow *arrow = GTK_ARROW (object);
143 GtkArrowPrivate *priv = arrow->priv;
147 case PROP_ARROW_TYPE:
148 gtk_arrow_set (arrow,
149 g_value_get_enum (value),
152 case PROP_SHADOW_TYPE:
153 gtk_arrow_set (arrow,
155 g_value_get_enum (value));
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164 gtk_arrow_get_property (GObject *object,
169 GtkArrow *arrow = GTK_ARROW (object);
170 GtkArrowPrivate *priv = arrow->priv;
174 case PROP_ARROW_TYPE:
175 g_value_set_enum (value, priv->arrow_type);
177 case PROP_SHADOW_TYPE:
178 g_value_set_enum (value, priv->shadow_type);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 gtk_arrow_init (GtkArrow *arrow)
189 GtkArrowPrivate *priv;
191 arrow->priv = G_TYPE_INSTANCE_GET_PRIVATE (arrow,
196 gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
198 priv->arrow_type = GTK_ARROW_RIGHT;
199 priv->shadow_type = GTK_SHADOW_OUT;
203 gtk_arrow_get_preferred_width (GtkWidget *widget,
209 gtk_misc_get_padding (GTK_MISC (widget), &xpad, NULL);
212 *minimum_size = MINIMUM_ARROW_SIZE + xpad * 2;
215 *natural_size = NATURAL_ARROW_SIZE + xpad * 2;
219 gtk_arrow_get_preferred_height (GtkWidget *widget,
225 gtk_misc_get_padding (GTK_MISC (widget), NULL, &ypad);
228 *minimum_size = MINIMUM_ARROW_SIZE + ypad * 2;
231 *natural_size = NATURAL_ARROW_SIZE + ypad * 2;
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);
308 gtk_arrow_draw (GtkWidget *widget,
311 GtkArrow *arrow = GTK_ARROW (widget);
312 GtkArrowPrivate *priv = arrow->priv;
313 GtkMisc *misc = GTK_MISC (widget);
314 GtkStyleContext *context;
320 gfloat xalign, yalign;
321 GtkArrowType effective_arrow_type;
322 gfloat arrow_scaling;
325 if (priv->arrow_type == GTK_ARROW_NONE)
328 context = gtk_widget_get_style_context (widget);
329 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
331 gtk_misc_get_padding (misc, &xpad, &ypad);
332 gtk_misc_get_alignment (misc, &xalign, &yalign);
334 width = gtk_widget_get_allocated_width (widget);
335 height = gtk_widget_get_allocated_height (widget);
337 extent = MIN (width - 2 * xpad, height - 2 * ypad) * arrow_scaling;
338 effective_arrow_type = priv->arrow_type;
340 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
342 xalign = 1.0 - xalign;
343 if (priv->arrow_type == GTK_ARROW_LEFT)
344 effective_arrow_type = GTK_ARROW_RIGHT;
345 else if (priv->arrow_type == GTK_ARROW_RIGHT)
346 effective_arrow_type = GTK_ARROW_LEFT;
349 x = xpad + ((width - extent) * xalign);
350 y = ypad + ((height - extent) * yalign);
352 switch (effective_arrow_type)
357 case GTK_ARROW_RIGHT:
365 angle = (3 * G_PI) / 2;
369 gtk_style_context_save (context);
371 state = gtk_widget_get_state_flags (widget);
372 gtk_style_context_set_state (context, state);
373 gtk_render_arrow (context, cr, angle, x, y, extent);
375 gtk_style_context_restore (context);