]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtkimageaccessible.c
acaa5b9bea2dcde1446822b5a9a11514f423ddf8
[~andy/gtk] / gtk / a11y / gtkimageaccessible.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2001 Sun Microsystems Inc.
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 #include "config.h"
21
22 #include <string.h>
23 #include <gtk/gtk.h>
24 #include "gtkimageaccessible.h"
25
26
27 static void atk_image_interface_init (AtkImageIface  *iface);
28
29 G_DEFINE_TYPE_WITH_CODE (GtkImageAccessible, _gtk_image_accessible, GTK_TYPE_WIDGET_ACCESSIBLE,
30                          G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE, atk_image_interface_init))
31
32 static void
33 gtk_image_accessible_initialize (AtkObject *accessible,
34                                  gpointer   data)
35 {
36   ATK_OBJECT_CLASS (_gtk_image_accessible_parent_class)->initialize (accessible, data);
37
38   accessible->role = ATK_ROLE_ICON;
39 }
40
41 static void
42 gtk_image_accessible_finalize (GObject *object)
43 {
44   GtkImageAccessible *aimage = GTK_IMAGE_ACCESSIBLE (object);
45
46   g_free (aimage->image_description);
47   g_free (aimage->stock_name);
48
49   G_OBJECT_CLASS (_gtk_image_accessible_parent_class)->finalize (object);
50 }
51
52 static const gchar *
53 gtk_image_accessible_get_name (AtkObject *accessible)
54 {
55   GtkWidget* widget;
56   GtkImage *image;
57   GtkImageAccessible *image_accessible;
58   GtkStockItem stock_item;
59   gchar *stock_id;
60   const gchar *name;
61
62   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
63   if (widget == NULL)
64     return NULL;
65
66   name = ATK_OBJECT_CLASS (_gtk_image_accessible_parent_class)->get_name (accessible);
67   if (name)
68     return name;
69
70   image = GTK_IMAGE (widget);
71   image_accessible = GTK_IMAGE_ACCESSIBLE (accessible);
72
73   g_free (image_accessible->stock_name);
74   image_accessible->stock_name = NULL;
75
76   if (gtk_image_get_storage_type (image) != GTK_IMAGE_STOCK)
77     return NULL;
78
79   gtk_image_get_stock (image, &stock_id, NULL);
80   if (stock_id == NULL)
81     return NULL;
82
83   if (!gtk_stock_lookup (stock_id, &stock_item))
84     return NULL;
85
86   image_accessible->stock_name = _gtk_toolbar_elide_underscores (stock_item.label);
87   return image_accessible->stock_name;
88 }
89
90 static void
91 _gtk_image_accessible_class_init (GtkImageAccessibleClass *klass)
92 {
93   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94   AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);
95
96   gobject_class->finalize = gtk_image_accessible_finalize;
97   class->initialize = gtk_image_accessible_initialize;
98   class->get_name = gtk_image_accessible_get_name;
99 }
100
101 static void
102 _gtk_image_accessible_init (GtkImageAccessible *image)
103 {
104 }
105
106 static const gchar *
107 gtk_image_accessible_get_image_description (AtkImage *image)
108 {
109   GtkImageAccessible *accessible = GTK_IMAGE_ACCESSIBLE (image);
110
111   return accessible->image_description;
112 }
113
114 static void
115 gtk_image_accessible_get_image_position (AtkImage     *image,
116                                          gint         *x,
117                                          gint         *y,
118                                          AtkCoordType  coord_type)
119 {
120   atk_component_get_position (ATK_COMPONENT (image), x, y, coord_type);
121 }
122
123 static void
124 gtk_image_accessible_get_image_size (AtkImage *image,
125                                      gint     *width,
126                                      gint     *height)
127 {
128   GtkWidget* widget;
129   GtkImage *gtk_image;
130   GtkImageType image_type;
131
132   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
133   if (widget == NULL)
134     {
135       *height = -1;
136       *width = -1;
137       return;
138     }
139
140   gtk_image = GTK_IMAGE (widget);
141
142   image_type = gtk_image_get_storage_type (gtk_image);
143   switch (image_type)
144     {
145     case GTK_IMAGE_PIXBUF:
146       {
147         GdkPixbuf *pixbuf;
148
149         pixbuf = gtk_image_get_pixbuf (gtk_image);
150         *height = gdk_pixbuf_get_height (pixbuf);
151         *width = gdk_pixbuf_get_width (pixbuf);
152         break;
153       }
154     case GTK_IMAGE_STOCK:
155     case GTK_IMAGE_ICON_SET:
156     case GTK_IMAGE_ICON_NAME:
157     case GTK_IMAGE_GICON:
158       {
159         GtkIconSize size;
160         GtkSettings *settings;
161
162         settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
163
164         g_object_get (gtk_image, "icon-size", &size, NULL);
165         gtk_icon_size_lookup_for_settings (settings, size, width, height);
166         break;
167       }
168     case GTK_IMAGE_ANIMATION:
169       {
170         GdkPixbufAnimation *animation;
171
172         animation = gtk_image_get_animation (gtk_image);
173         *height = gdk_pixbuf_animation_get_height (animation);
174         *width = gdk_pixbuf_animation_get_width (animation);
175         break;
176       }
177     default:
178       {
179         *height = -1;
180         *width = -1;
181         break;
182       }
183     }
184 }
185
186 static gboolean
187 gtk_image_accessible_set_image_description (AtkImage    *image,
188                                             const gchar *description)
189 {
190   GtkImageAccessible* accessible = GTK_IMAGE_ACCESSIBLE (image);
191
192   g_free (accessible->image_description);
193   accessible->image_description = g_strdup (description);
194
195   return TRUE;
196 }
197
198 static void
199 atk_image_interface_init (AtkImageIface *iface)
200 {
201   iface->get_image_description = gtk_image_accessible_get_image_description;
202   iface->get_image_position = gtk_image_accessible_get_image_position;
203   iface->get_image_size = gtk_image_accessible_get_image_size;
204   iface->set_image_description = gtk_image_accessible_set_image_description;
205 }