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