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