]> Pileus Git - ~andy/gtk/blob - gtk/gtkarrow.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24
25 /**
26  * SECTION:gtkarrow
27  * @Short_description: Displays an arrow
28  * @Title: GtkArrow
29  * @See_also: gtk_render_arrow()
30  *
31  * GtkArrow should be used to draw simple arrows that need to point in
32  * one of the four cardinal directions (up, down, left, or right).  The
33  * style of the arrow can be one of shadow in, shadow out, etched in, or
34  * etched out.  Note that these directions and style types may be
35  * amended in versions of GTK+ to come.
36  *
37  * GtkArrow will fill any space alloted to it, but since it is inherited
38  * from #GtkMisc, it can be padded and/or aligned, to fill exactly the
39  * space the programmer desires.
40  *
41  * Arrows are created with a call to gtk_arrow_new().  The direction or
42  * style of an arrow can be changed after creation by using gtk_arrow_set().
43  */
44
45 #include "config.h"
46 #include <math.h>
47 #include "gtkarrow.h"
48 #include "gtksizerequest.h"
49 #include "gtktypebuiltins.h"
50 #include "gtkprivate.h"
51 #include "gtkintl.h"
52
53 #include "a11y/gtkarrowaccessible.h"
54
55 #define MIN_ARROW_SIZE  15
56
57 struct _GtkArrowPrivate
58 {
59   gint16 arrow_type;
60   gint16 shadow_type;
61 };
62
63 enum {
64   PROP_0,
65   PROP_ARROW_TYPE,
66   PROP_SHADOW_TYPE
67 };
68
69
70 static void     gtk_arrow_set_property (GObject        *object,
71                                         guint           prop_id,
72                                         const GValue   *value,
73                                         GParamSpec     *pspec);
74 static void     gtk_arrow_get_property (GObject        *object,
75                                         guint           prop_id,
76                                         GValue         *value,
77                                         GParamSpec     *pspec);
78 static gboolean gtk_arrow_draw         (GtkWidget      *widget,
79                                         cairo_t        *cr);
80
81 static void     gtk_arrow_get_preferred_width         (GtkWidget           *widget,
82                                                        gint                *minimum_size,
83                                                        gint                *natural_size);
84 static void     gtk_arrow_get_preferred_height        (GtkWidget           *widget,
85                                                        gint                *minimum_size,
86                                                        gint                *natural_size);
87
88 G_DEFINE_TYPE (GtkArrow, gtk_arrow, GTK_TYPE_MISC)
89
90
91 static void
92 gtk_arrow_class_init (GtkArrowClass *class)
93 {
94   GObjectClass *gobject_class;
95   GtkWidgetClass *widget_class;
96
97   gobject_class = (GObjectClass*) class;
98   widget_class = (GtkWidgetClass*) class;
99
100   gobject_class->set_property = gtk_arrow_set_property;
101   gobject_class->get_property = gtk_arrow_get_property;
102
103   widget_class->draw = gtk_arrow_draw;
104   widget_class->get_preferred_width  = gtk_arrow_get_preferred_width;
105   widget_class->get_preferred_height = gtk_arrow_get_preferred_height;
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   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_ARROW_ACCESSIBLE);
135 }
136
137 static void
138 gtk_arrow_set_property (GObject         *object,
139                         guint            prop_id,
140                         const GValue    *value,
141                         GParamSpec      *pspec)
142 {
143   GtkArrow *arrow = GTK_ARROW (object);
144   GtkArrowPrivate *priv = arrow->priv;
145
146   switch (prop_id)
147     {
148     case PROP_ARROW_TYPE:
149       gtk_arrow_set (arrow,
150                      g_value_get_enum (value),
151                      priv->shadow_type);
152       break;
153     case PROP_SHADOW_TYPE:
154       gtk_arrow_set (arrow,
155                      priv->arrow_type,
156                      g_value_get_enum (value));
157       break;
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161     }
162 }
163
164 static void
165 gtk_arrow_get_property (GObject         *object,
166                         guint            prop_id,
167                         GValue          *value,
168                         GParamSpec      *pspec)
169 {
170   GtkArrow *arrow = GTK_ARROW (object);
171   GtkArrowPrivate *priv = arrow->priv;
172
173   switch (prop_id)
174     {
175     case PROP_ARROW_TYPE:
176       g_value_set_enum (value, priv->arrow_type);
177       break;
178     case PROP_SHADOW_TYPE:
179       g_value_set_enum (value, priv->shadow_type);
180       break;
181     default:
182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183       break;
184     }
185 }
186
187 static void
188 gtk_arrow_init (GtkArrow *arrow)
189 {
190   GtkArrowPrivate *priv;
191
192   arrow->priv = G_TYPE_INSTANCE_GET_PRIVATE (arrow,
193                                              GTK_TYPE_ARROW,
194                                              GtkArrowPrivate);
195   priv = arrow->priv;
196
197   gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
198
199   priv->arrow_type = GTK_ARROW_RIGHT;
200   priv->shadow_type = GTK_SHADOW_OUT;
201 }
202
203 static void
204 gtk_arrow_get_preferred_width (GtkWidget *widget,
205                                gint      *minimum_size,
206                                gint      *natural_size)
207 {
208   GtkBorder border;
209
210   _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
211
212   if (minimum_size)
213     *minimum_size = MIN_ARROW_SIZE + border.left + border.right;
214
215   if (natural_size)
216     *natural_size = MIN_ARROW_SIZE + border.left + border.right;
217 }
218
219 static void
220 gtk_arrow_get_preferred_height (GtkWidget *widget,
221                                 gint      *minimum_size,
222                                 gint      *natural_size)
223 {
224   GtkBorder border;
225
226   _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
227
228   if (minimum_size)
229     *minimum_size = MIN_ARROW_SIZE + border.top + border.bottom;
230
231   if (natural_size)
232     *natural_size = MIN_ARROW_SIZE + border.top + border.bottom;
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 static gboolean
307 gtk_arrow_draw (GtkWidget *widget,
308                 cairo_t   *cr)
309 {
310   GtkArrow *arrow = GTK_ARROW (widget);
311   GtkArrowPrivate *priv = arrow->priv;
312   GtkStyleContext *context;
313   gdouble x, y;
314   gint width, height;
315   gint extent;
316   GtkBorder border;
317   gfloat xalign, yalign;
318   GtkArrowType effective_arrow_type;
319   gfloat arrow_scaling;
320   gdouble angle;
321
322   if (priv->arrow_type == GTK_ARROW_NONE)
323     return FALSE;
324
325   context = gtk_widget_get_style_context (widget);
326   gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
327
328   _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
329   gtk_misc_get_alignment (GTK_MISC (widget), &xalign, &yalign);
330
331   width = gtk_widget_get_allocated_width (widget) - border.left - border.right;
332   height = gtk_widget_get_allocated_height (widget) - border.top - border.bottom;
333
334   extent = MIN (width, height) * arrow_scaling;
335   effective_arrow_type = priv->arrow_type;
336
337   if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
338     {
339       xalign = 1.0 - xalign;
340       if (priv->arrow_type == GTK_ARROW_LEFT)
341         effective_arrow_type = GTK_ARROW_RIGHT;
342       else if (priv->arrow_type == GTK_ARROW_RIGHT)
343         effective_arrow_type = GTK_ARROW_LEFT;
344     }
345
346   x = border.left + ((width - extent) * xalign);
347   y = border.top + ((height - extent) * yalign);
348
349   switch (effective_arrow_type)
350     {
351     case GTK_ARROW_UP:
352       angle = 0;
353       break;
354     case GTK_ARROW_RIGHT:
355       angle = G_PI / 2;
356       break;
357     case GTK_ARROW_DOWN:
358       angle = G_PI;
359       break;
360     case GTK_ARROW_LEFT:
361     default:
362       angle = (3 * G_PI) / 2;
363       break;
364     }
365
366   gtk_render_arrow (context, cr, angle, x, y, extent);
367
368   return FALSE;
369 }