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