]> Pileus Git - ~andy/gtk/blob - gtk/gtkarrow.c
Don't use GTK_WIDGET_*SET_FLAGS (wid, GTK_NO_WINDOW)
[~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 #include "config.h"
28 #include <math.h>
29 #include "gtkarrow.h"
30 #include "gtkprivate.h"
31 #include "gtkintl.h"
32 #include "gtkalias.h"
33
34 #define MIN_ARROW_SIZE  15
35
36 enum {
37   PROP_0,
38   PROP_ARROW_TYPE,
39   PROP_SHADOW_TYPE
40 };
41
42
43 static void     gtk_arrow_set_property (GObject        *object,
44                                         guint           prop_id,
45                                         const GValue   *value,
46                                         GParamSpec     *pspec);
47 static void     gtk_arrow_get_property (GObject        *object,
48                                         guint           prop_id,
49                                         GValue         *value,
50                                         GParamSpec     *pspec);
51 static gboolean gtk_arrow_expose       (GtkWidget      *widget,
52                                         GdkEventExpose *event);
53
54
55 G_DEFINE_TYPE (GtkArrow, gtk_arrow, GTK_TYPE_MISC)
56
57
58 static void
59 gtk_arrow_class_init (GtkArrowClass *class)
60 {
61   GObjectClass *gobject_class;
62   GtkWidgetClass *widget_class;
63
64   gobject_class = (GObjectClass*) class;
65   widget_class = (GtkWidgetClass*) class;
66
67   gobject_class->set_property = gtk_arrow_set_property;
68   gobject_class->get_property = gtk_arrow_get_property;
69
70   widget_class->expose_event = gtk_arrow_expose;
71
72   g_object_class_install_property (gobject_class,
73                                    PROP_ARROW_TYPE,
74                                    g_param_spec_enum ("arrow-type",
75                                                       P_("Arrow direction"),
76                                                       P_("The direction the arrow should point"),
77                                                       GTK_TYPE_ARROW_TYPE,
78                                                       GTK_ARROW_RIGHT,
79                                                       GTK_PARAM_READWRITE));
80
81   g_object_class_install_property (gobject_class,
82                                    PROP_SHADOW_TYPE,
83                                    g_param_spec_enum ("shadow-type",
84                                                       P_("Arrow shadow"),
85                                                       P_("Appearance of the shadow surrounding the arrow"),
86                                                       GTK_TYPE_SHADOW_TYPE,
87                                                       GTK_SHADOW_OUT,
88                                                       GTK_PARAM_READWRITE));
89
90   gtk_widget_class_install_style_property (widget_class,
91                                            g_param_spec_float ("arrow-scaling",
92                                                                P_("Arrow Scaling"),
93                                                                P_("Amount of space used up by arrow"),
94                                                                0.0, 1.0, 0.7,
95                                                                GTK_PARAM_READABLE));
96 }
97
98 static void
99 gtk_arrow_set_property (GObject         *object,
100                         guint            prop_id,
101                         const GValue    *value,
102                         GParamSpec      *pspec)
103 {
104   GtkArrow *arrow = GTK_ARROW (object);
105
106   switch (prop_id)
107     {
108     case PROP_ARROW_TYPE:
109       gtk_arrow_set (arrow,
110                      g_value_get_enum (value),
111                      arrow->shadow_type);
112       break;
113     case PROP_SHADOW_TYPE:
114       gtk_arrow_set (arrow,
115                      arrow->arrow_type,
116                      g_value_get_enum (value));
117       break;
118     default:
119       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120       break;
121     }
122 }
123
124 static void
125 gtk_arrow_get_property (GObject         *object,
126                         guint            prop_id,
127                         GValue          *value,
128                         GParamSpec      *pspec)
129 {
130   GtkArrow *arrow = GTK_ARROW (object);
131
132   switch (prop_id)
133     {
134     case PROP_ARROW_TYPE:
135       g_value_set_enum (value, arrow->arrow_type);
136       break;
137     case PROP_SHADOW_TYPE:
138       g_value_set_enum (value, arrow->shadow_type);
139       break;
140     default:
141       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142       break;
143     }
144 }
145
146 static void
147 gtk_arrow_init (GtkArrow *arrow)
148 {
149   gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
150
151   GTK_WIDGET (arrow)->requisition.width = MIN_ARROW_SIZE + GTK_MISC (arrow)->xpad * 2;
152   GTK_WIDGET (arrow)->requisition.height = MIN_ARROW_SIZE + GTK_MISC (arrow)->ypad * 2;
153
154   arrow->arrow_type = GTK_ARROW_RIGHT;
155   arrow->shadow_type = GTK_SHADOW_OUT;
156 }
157
158 GtkWidget*
159 gtk_arrow_new (GtkArrowType  arrow_type,
160                GtkShadowType shadow_type)
161 {
162   GtkArrow *arrow;
163
164   arrow = g_object_new (GTK_TYPE_ARROW, NULL);
165
166   arrow->arrow_type = arrow_type;
167   arrow->shadow_type = shadow_type;
168
169   return GTK_WIDGET (arrow);
170 }
171
172 void
173 gtk_arrow_set (GtkArrow      *arrow,
174                GtkArrowType   arrow_type,
175                GtkShadowType  shadow_type)
176 {
177   GtkWidget *widget;
178
179   g_return_if_fail (GTK_IS_ARROW (arrow));
180
181   if (   ((GtkArrowType) arrow->arrow_type != arrow_type)
182       || ((GtkShadowType) arrow->shadow_type != shadow_type))
183     {
184       g_object_freeze_notify (G_OBJECT (arrow));
185
186       if ((GtkArrowType) arrow->arrow_type != arrow_type)
187         {
188           arrow->arrow_type = arrow_type;
189           g_object_notify (G_OBJECT (arrow), "arrow-type");
190         }
191
192       if ((GtkShadowType) arrow->shadow_type != shadow_type)
193         {
194           arrow->shadow_type = shadow_type;
195           g_object_notify (G_OBJECT (arrow), "shadow-type");
196         }
197
198       g_object_thaw_notify (G_OBJECT (arrow));
199
200       widget = GTK_WIDGET (arrow);
201       if (gtk_widget_is_drawable (widget))
202         gtk_widget_queue_draw (widget);
203     }
204 }
205
206
207 static gboolean
208 gtk_arrow_expose (GtkWidget      *widget,
209                   GdkEventExpose *event)
210 {
211   if (gtk_widget_is_drawable (widget))
212     {
213       GtkArrow *arrow = GTK_ARROW (widget);
214       GtkMisc *misc = GTK_MISC (widget);
215       GtkShadowType shadow_type;
216       gint width, height;
217       gint x, y;
218       gint extent;
219       gfloat xalign;
220       GtkArrowType effective_arrow_type;
221       gfloat arrow_scaling;
222
223       gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
224
225       width = widget->allocation.width - misc->xpad * 2;
226       height = widget->allocation.height - misc->ypad * 2;
227       extent = MIN (width, height) * arrow_scaling;
228       effective_arrow_type = arrow->arrow_type;
229
230       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
231         xalign = misc->xalign;
232       else
233         {
234           xalign = 1.0 - misc->xalign;
235           if (arrow->arrow_type == GTK_ARROW_LEFT)
236             effective_arrow_type = GTK_ARROW_RIGHT;
237           else if (arrow->arrow_type == GTK_ARROW_RIGHT)
238             effective_arrow_type = GTK_ARROW_LEFT;
239         }
240
241       x = floor (widget->allocation.x + misc->xpad
242                  + ((widget->allocation.width - extent) * xalign));
243       y = floor (widget->allocation.y + misc->ypad
244                  + ((widget->allocation.height - extent) * misc->yalign));
245
246       shadow_type = arrow->shadow_type;
247
248       if (widget->state == GTK_STATE_ACTIVE)
249         {
250           if (shadow_type == GTK_SHADOW_IN)
251             shadow_type = GTK_SHADOW_OUT;
252           else if (shadow_type == GTK_SHADOW_OUT)
253             shadow_type = GTK_SHADOW_IN;
254           else if (shadow_type == GTK_SHADOW_ETCHED_IN)
255             shadow_type = GTK_SHADOW_ETCHED_OUT;
256           else if (shadow_type == GTK_SHADOW_ETCHED_OUT)
257             shadow_type = GTK_SHADOW_ETCHED_IN;
258         }
259
260       gtk_paint_arrow (widget->style, widget->window,
261                        widget->state, shadow_type,
262                        &event->area, widget, "arrow",
263                        effective_arrow_type, TRUE,
264                        x, y, extent, extent);
265     }
266
267   return FALSE;
268 }
269
270 #define __GTK_ARROW_C__
271 #include "gtkaliasdef.c"