]> Pileus Git - ~andy/gtk/blob - gtk/gtkarrow.c
Intern some more strings.
[~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
39   PROP_ARROW_TYPE,
40   PROP_SHADOW_TYPE,
41   
42   PROP_LAST
43 };
44
45
46 static void gtk_arrow_class_init (GtkArrowClass  *klass);
47 static void gtk_arrow_init       (GtkArrow       *arrow);
48 static gint gtk_arrow_expose     (GtkWidget      *widget,
49                                   GdkEventExpose *event);
50 static void gtk_arrow_set_property (GObject         *object,
51                                     guint            prop_id,
52                                     const GValue    *value,
53                                     GParamSpec      *pspec);
54 static void gtk_arrow_get_property (GObject         *object,
55                                     guint            prop_id,
56                                     GValue          *value,
57                                     GParamSpec      *pspec);
58
59 GType
60 gtk_arrow_get_type (void)
61 {
62   static GType arrow_type = 0;
63
64   if (!arrow_type)
65     {
66       static const GTypeInfo arrow_info =
67       {
68         sizeof (GtkArrowClass),
69         NULL,           /* base_init */
70         NULL,           /* base_finalize */
71         (GClassInitFunc) gtk_arrow_class_init,
72         NULL,           /* class_finalize */
73         NULL,           /* class_data */
74         sizeof (GtkArrow),
75         0,              /* n_preallocs */
76         (GInstanceInitFunc) gtk_arrow_init,
77       };
78
79       arrow_type = g_type_register_static (GTK_TYPE_MISC, I_("GtkArrow"),
80                                            &arrow_info, 0);
81     }
82
83   return arrow_type;
84 }
85
86 static void
87 gtk_arrow_class_init (GtkArrowClass *class)
88 {
89   GObjectClass *gobject_class;
90   GtkWidgetClass *widget_class;
91
92   gobject_class = (GObjectClass*) class;
93   widget_class = (GtkWidgetClass*) class;
94
95   gobject_class->set_property = gtk_arrow_set_property;
96   gobject_class->get_property = gtk_arrow_get_property;
97
98   g_object_class_install_property (gobject_class,
99                                    PROP_ARROW_TYPE,
100                                    g_param_spec_enum ("arrow-type",
101                                                       P_("Arrow direction"),
102                                                       P_("The direction the arrow should point"),
103                                                       GTK_TYPE_ARROW_TYPE,
104                                                       GTK_ARROW_RIGHT,
105                                                       GTK_PARAM_READWRITE));
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   widget_class->expose_event = gtk_arrow_expose;
116 }
117
118 static void
119 gtk_arrow_set_property (GObject         *object,
120                         guint            prop_id,
121                         const GValue    *value,
122                         GParamSpec      *pspec)
123 {
124   GtkArrow *arrow;
125   
126   arrow = GTK_ARROW (object);
127
128   switch (prop_id)
129     {
130     case PROP_ARROW_TYPE:
131       gtk_arrow_set (arrow,
132                      g_value_get_enum (value),
133                      arrow->shadow_type);
134       break;
135     case PROP_SHADOW_TYPE:
136       gtk_arrow_set (arrow,
137                      arrow->arrow_type,
138                      g_value_get_enum (value));
139       break;
140     default:
141       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142       break;
143     }
144 }
145
146
147 static void
148 gtk_arrow_get_property (GObject         *object,
149                         guint            prop_id,
150                         GValue          *value,
151                         GParamSpec      *pspec)
152 {
153   GtkArrow *arrow;
154   
155   arrow = GTK_ARROW (object);
156   switch (prop_id)
157     {
158     case PROP_ARROW_TYPE:
159       g_value_set_enum (value, arrow->arrow_type);
160       break;
161     case PROP_SHADOW_TYPE:
162       g_value_set_enum (value, arrow->shadow_type);
163       break;
164     default:
165       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166       break;
167     }
168 }
169
170 static void
171 gtk_arrow_init (GtkArrow *arrow)
172 {
173   GTK_WIDGET_SET_FLAGS (arrow, GTK_NO_WINDOW);
174
175   GTK_WIDGET (arrow)->requisition.width = MIN_ARROW_SIZE + GTK_MISC (arrow)->xpad * 2;
176   GTK_WIDGET (arrow)->requisition.height = MIN_ARROW_SIZE + GTK_MISC (arrow)->ypad * 2;
177
178   arrow->arrow_type = GTK_ARROW_RIGHT;
179   arrow->shadow_type = GTK_SHADOW_OUT;
180 }
181
182 GtkWidget*
183 gtk_arrow_new (GtkArrowType  arrow_type,
184                GtkShadowType shadow_type)
185 {
186   GtkArrow *arrow;
187
188   arrow = g_object_new (GTK_TYPE_ARROW, NULL);
189
190   arrow->arrow_type = arrow_type;
191   arrow->shadow_type = shadow_type;
192
193   return GTK_WIDGET (arrow);
194 }
195
196 void
197 gtk_arrow_set (GtkArrow      *arrow,
198                GtkArrowType   arrow_type,
199                GtkShadowType  shadow_type)
200 {
201   g_return_if_fail (GTK_IS_ARROW (arrow));
202
203   if (   ((GtkArrowType) arrow->arrow_type != arrow_type)
204       || ((GtkShadowType) arrow->shadow_type != shadow_type))
205     {
206       g_object_freeze_notify (G_OBJECT (arrow));
207
208       if ((GtkArrowType) arrow->arrow_type != arrow_type)
209         {
210           arrow->arrow_type = arrow_type;
211           g_object_notify (G_OBJECT (arrow), "arrow-type");
212         }
213
214       if ((GtkShadowType) arrow->shadow_type != shadow_type)
215         {
216           arrow->shadow_type = shadow_type;
217           g_object_notify (G_OBJECT (arrow), "shadow-type");
218         }
219
220       g_object_thaw_notify (G_OBJECT (arrow));
221
222       if (GTK_WIDGET_DRAWABLE (arrow))
223         gtk_widget_queue_draw (GTK_WIDGET (arrow));
224     }
225 }
226
227
228 static gboolean 
229 gtk_arrow_expose (GtkWidget      *widget,
230                   GdkEventExpose *event)
231 {
232   GtkArrow *arrow;
233   GtkMisc *misc;
234   GtkShadowType shadow_type;
235   gint width, height;
236   gint x, y;
237   gint extent;
238   gfloat xalign;
239   GtkArrowType effective_arrow_type;
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) * 0.7;
249       effective_arrow_type = arrow->arrow_type;
250
251       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
252         xalign = misc->xalign;
253       else
254         {
255           xalign = 1.0 - misc->xalign;
256           if (arrow->arrow_type == GTK_ARROW_LEFT)
257             effective_arrow_type = GTK_ARROW_RIGHT;
258           else if (arrow->arrow_type == GTK_ARROW_RIGHT)
259             effective_arrow_type = GTK_ARROW_LEFT;
260         }
261
262       x = floor (widget->allocation.x + misc->xpad
263                  + ((widget->allocation.width - extent) * xalign));
264       y = floor (widget->allocation.y + misc->ypad 
265                  + ((widget->allocation.height - extent) * misc->yalign));
266       
267       shadow_type = arrow->shadow_type;
268
269       if (widget->state == GTK_STATE_ACTIVE)
270         {
271           if (shadow_type == GTK_SHADOW_IN)
272             shadow_type = GTK_SHADOW_OUT;
273           else if (shadow_type == GTK_SHADOW_OUT)
274             shadow_type = GTK_SHADOW_IN;
275           else if (shadow_type == GTK_SHADOW_ETCHED_IN)
276             shadow_type = GTK_SHADOW_ETCHED_OUT;
277           else if (shadow_type == GTK_SHADOW_ETCHED_OUT)
278             shadow_type = GTK_SHADOW_ETCHED_IN;
279         }
280
281       gtk_paint_arrow (widget->style, widget->window,
282                        widget->state, shadow_type,
283                        &event->area, widget, "arrow",
284                        effective_arrow_type, TRUE,
285                        x, y, extent, extent);
286     }
287
288   return FALSE;
289 }
290
291 #define __GTK_ARROW_C__
292 #include "gtkaliasdef.c"