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