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