]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtkimageaccessible.c
GtkRadioMenuItemAccessible: add a private struct
[~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, 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
104 static void
105 _gtk_image_accessible_init (GtkImageAccessible *image)
106 {
107   image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
108                                              GTK_TYPE_IMAGE_ACCESSIBLE,
109                                              GtkImageAccessiblePrivate);
110 }
111
112 static const gchar *
113 gtk_image_accessible_get_image_description (AtkImage *image)
114 {
115   GtkImageAccessible *accessible = GTK_IMAGE_ACCESSIBLE (image);
116
117   return accessible->priv->image_description;
118 }
119
120 static void
121 gtk_image_accessible_get_image_position (AtkImage     *image,
122                                          gint         *x,
123                                          gint         *y,
124                                          AtkCoordType  coord_type)
125 {
126   atk_component_get_position (ATK_COMPONENT (image), x, y, coord_type);
127 }
128
129 static void
130 gtk_image_accessible_get_image_size (AtkImage *image,
131                                      gint     *width,
132                                      gint     *height)
133 {
134   GtkWidget* widget;
135   GtkImage *gtk_image;
136   GtkImageType image_type;
137
138   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
139   if (widget == NULL)
140     {
141       *height = -1;
142       *width = -1;
143       return;
144     }
145
146   gtk_image = GTK_IMAGE (widget);
147
148   image_type = gtk_image_get_storage_type (gtk_image);
149   switch (image_type)
150     {
151     case GTK_IMAGE_PIXBUF:
152       {
153         GdkPixbuf *pixbuf;
154
155         pixbuf = gtk_image_get_pixbuf (gtk_image);
156         *height = gdk_pixbuf_get_height (pixbuf);
157         *width = gdk_pixbuf_get_width (pixbuf);
158         break;
159       }
160     case GTK_IMAGE_STOCK:
161     case GTK_IMAGE_ICON_SET:
162     case GTK_IMAGE_ICON_NAME:
163     case GTK_IMAGE_GICON:
164       {
165         GtkIconSize size;
166         GtkSettings *settings;
167
168         settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
169
170         g_object_get (gtk_image, "icon-size", &size, NULL);
171         gtk_icon_size_lookup_for_settings (settings, size, width, height);
172         break;
173       }
174     case GTK_IMAGE_ANIMATION:
175       {
176         GdkPixbufAnimation *animation;
177
178         animation = gtk_image_get_animation (gtk_image);
179         *height = gdk_pixbuf_animation_get_height (animation);
180         *width = gdk_pixbuf_animation_get_width (animation);
181         break;
182       }
183     default:
184       {
185         *height = -1;
186         *width = -1;
187         break;
188       }
189     }
190 }
191
192 static gboolean
193 gtk_image_accessible_set_image_description (AtkImage    *image,
194                                             const gchar *description)
195 {
196   GtkImageAccessible* accessible = GTK_IMAGE_ACCESSIBLE (image);
197
198   g_free (accessible->priv->image_description);
199   accessible->priv->image_description = g_strdup (description);
200
201   return TRUE;
202 }
203
204 static void
205 atk_image_interface_init (AtkImageIface *iface)
206 {
207   iface->get_image_description = gtk_image_accessible_get_image_description;
208   iface->get_image_position = gtk_image_accessible_get_image_position;
209   iface->get_image_size = gtk_image_accessible_get_image_size;
210   iface->set_image_description = gtk_image_accessible_set_image_description;
211 }