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