gtkbuttonaccessible.c 12.2 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1
/* GTK+ - accessibility implementations
2 3 4 5 6 7 8 9 10 11 12 13 14
 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21
 */

#include "config.h"

#include <string.h>
#include <gtk/gtk.h>
22
#include <glib/gi18n-lib.h>
23 24 25 26 27 28
#include "gtkbuttonaccessible.h"


static void atk_action_interface_init (AtkActionIface *iface);
static void atk_image_interface_init  (AtkImageIface  *iface);

29
G_DEFINE_TYPE_WITH_CODE (GtkButtonAccessible, gtk_button_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
30 31 32 33 34 35 36 37 38
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE, atk_image_interface_init))

static void
gtk_button_accessible_initialize (AtkObject *obj,
                                  gpointer   data)
{
  GtkWidget *parent;

39
  ATK_OBJECT_CLASS (gtk_button_accessible_parent_class)->initialize (obj, data);
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

  parent = gtk_widget_get_parent (gtk_accessible_get_widget (GTK_ACCESSIBLE (obj)));
  if (GTK_IS_TREE_VIEW (parent))
    {
      /* Even though the accessible parent of the column header will
       * be reported as the table because the parent widget of the
       * GtkTreeViewColumn's button is the GtkTreeView we set
       * the accessible parent for column header to be the table
       * to ensure that atk_object_get_index_in_parent() returns
       * the correct value; see gail_widget_get_index_in_parent().
       */
      atk_object_set_parent (obj, gtk_widget_get_accessible (parent));
      obj->role = ATK_ROLE_TABLE_COLUMN_HEADER;
    }
  else
    obj->role = ATK_ROLE_PUSH_BUTTON;
}

static GtkWidget *
get_image_from_button (GtkWidget *button)
{
  GtkWidget *image;

  image = gtk_button_get_image (GTK_BUTTON (button));
  if (GTK_IS_IMAGE (image))
    return image;

  return NULL;
}

static GtkWidget *
find_label_child (GtkContainer *container)
{
  GList *children, *tmp_list;
  GtkWidget *child;

  children = gtk_container_get_children (container);

  child = NULL;
  for (tmp_list = children; tmp_list != NULL; tmp_list = tmp_list->next)
    {
      if (GTK_IS_LABEL (tmp_list->data))
        {
          child = GTK_WIDGET (tmp_list->data);
          break;
        }
      else if (GTK_IS_CONTAINER (tmp_list->data))
        {
          child = find_label_child (GTK_CONTAINER (tmp_list->data));
          if (child)
            break;
        }
    }
  g_list_free (children);
  return child;
}

static GtkWidget *
get_label_from_button (GtkWidget *button)
{
  GtkWidget *child;

  child = gtk_bin_get_child (GTK_BIN (button));
Matthias Clasen's avatar
Matthias Clasen committed
103
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
104 105
  if (GTK_IS_ALIGNMENT (child))
    child = gtk_bin_get_child (GTK_BIN (child));
Matthias Clasen's avatar
Matthias Clasen committed
106
G_GNUC_END_IGNORE_DEPRECATIONS
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

  if (GTK_IS_CONTAINER (child))
    child = find_label_child (GTK_CONTAINER (child));
  else if (!GTK_IS_LABEL (child))
    child = NULL;

  return child;
}

static const gchar *
gtk_button_accessible_get_name (AtkObject *obj)
{
  const gchar *name = NULL;
  GtkWidget *widget;
  GtkWidget *child;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
  if (widget == NULL)
    return NULL;

127
  name = ATK_OBJECT_CLASS (gtk_button_accessible_parent_class)->get_name (obj);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  if (name != NULL)
    return name;

  child = get_label_from_button (widget);
  if (GTK_IS_LABEL (child))
    name = gtk_label_get_text (GTK_LABEL (child));
  else
    {
      GtkWidget *image;

      image = get_image_from_button (widget);
      if (GTK_IS_IMAGE (image))
        {
          AtkObject *atk_obj;

          atk_obj = gtk_widget_get_accessible (image);
          name = atk_object_get_name (atk_obj);
        }
    }

  return name;
}

static gint
gtk_button_accessible_get_n_children (AtkObject* obj)
{
  return 0;
}

static AtkObject *
gtk_button_accessible_ref_child (AtkObject *obj,
                                 gint       i)
{
  return NULL;
}

static AtkStateSet *
gtk_button_accessible_ref_state_set (AtkObject *obj)
{
  AtkStateSet *state_set;
  GtkWidget *widget;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
  if (widget == NULL)
    return NULL;

174
  state_set = ATK_OBJECT_CLASS (gtk_button_accessible_parent_class)->ref_state_set (obj);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

  if ((gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_ACTIVE) != 0)
    atk_state_set_add_state (state_set, ATK_STATE_ARMED);

  if (!gtk_widget_get_can_focus (widget))
    atk_state_set_remove_state (state_set, ATK_STATE_SELECTABLE);

  return state_set;
}

static void
gtk_button_accessible_notify_gtk (GObject    *obj,
                                  GParamSpec *pspec)
{
  GtkWidget *widget = GTK_WIDGET (obj);
  AtkObject *atk_obj = gtk_widget_get_accessible (widget);

  if (strcmp (pspec->name, "label") == 0)
    {
      if (atk_obj->name == NULL)
        g_object_notify (G_OBJECT (atk_obj), "accessible-name");

Matthias Clasen's avatar
Matthias Clasen committed
197
      g_signal_emit_by_name (atk_obj, "visible-data-changed");
198 199
    }
  else
200
    GTK_WIDGET_ACCESSIBLE_CLASS (gtk_button_accessible_parent_class)->notify_gtk (obj, pspec);
201 202 203
}

static void
204
gtk_button_accessible_class_init (GtkButtonAccessibleClass *klass)
205 206
{
  AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
207
  GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
208
  GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
209 210 211 212 213 214 215 216 217 218 219 220 221 222

  class->get_name = gtk_button_accessible_get_name;
  class->get_n_children = gtk_button_accessible_get_n_children;
  class->ref_child = gtk_button_accessible_ref_child;
  class->ref_state_set = gtk_button_accessible_ref_state_set;
  class->initialize = gtk_button_accessible_initialize;

  widget_class->notify_gtk = gtk_button_accessible_notify_gtk;

  container_class->add_gtk = NULL;
  container_class->remove_gtk = NULL;
}

static void
223
gtk_button_accessible_init (GtkButtonAccessible *button)
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
{
}

static gboolean
gtk_button_accessible_do_action (AtkAction *action,
                                 gint       i)
{
  GtkWidget *widget;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
  if (widget == NULL)
    return FALSE;

  if (i != 0)
    return FALSE;

  if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
    return FALSE;

  gtk_button_clicked (GTK_BUTTON (widget));
  return TRUE;
}

static gint
gtk_button_accessible_get_n_actions (AtkAction *action)
{
  return 1;
}

static const gchar *
gtk_button_accessible_get_keybinding (AtkAction *action,
                                      gint       i)
{
  gchar *return_value = NULL;
  GtkWidget *widget;
  GtkWidget *label;
  guint key_val;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
  if (widget == NULL)
    return NULL;

  if (i != 0)
    return NULL;

  label = get_label_from_button (widget);
  if (GTK_IS_LABEL (label))
    {
      key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
      if (key_val != GDK_KEY_VoidSymbol)
        return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
    }
  if (return_value == NULL)
    {
      /* Find labelled-by relation */
      AtkRelationSet *set;
      AtkRelation *relation;
      GPtrArray *target;
      gpointer target_object;

      set = atk_object_ref_relation_set (ATK_OBJECT (action));
      if (set)
        {
          relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
          if (relation)
            {
              target = atk_relation_get_target (relation);
              target_object = g_ptr_array_index (target, 0);
              label = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
            }
          g_object_unref (set);
        }

      if (GTK_IS_LABEL (label))
        {
          key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
          if (key_val != GDK_KEY_VoidSymbol)
            return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
        }
    }
  return return_value;
}

static const gchar *
gtk_button_accessible_action_get_name (AtkAction *action,
                                       gint       i)
{
311 312 313 314 315 316 317 318 319 320 321 322 323
  if (i == 0)
    return "click";
  return NULL;
}

static const gchar *
gtk_button_accessible_action_get_localized_name (AtkAction *action,
                                                 gint       i)
{
  if (i == 0)
    return C_("Action name", "Click");
  return NULL;
}
324

325 326 327 328 329 330 331
static const gchar *
gtk_button_accessible_action_get_description (AtkAction *action,
                                              gint       i)
{
  if (i == 0)
    return C_("Action description", "Clicks the button");
  return NULL;
332 333 334 335 336 337 338 339 340
}

static void
atk_action_interface_init (AtkActionIface *iface)
{
  iface->do_action = gtk_button_accessible_do_action;
  iface->get_n_actions = gtk_button_accessible_get_n_actions;
  iface->get_keybinding = gtk_button_accessible_get_keybinding;
  iface->get_name = gtk_button_accessible_action_get_name;
341 342
  iface->get_localized_name = gtk_button_accessible_action_get_localized_name;
  iface->get_description = gtk_button_accessible_action_get_description;
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
}

static const gchar *
gtk_button_accessible_get_image_description (AtkImage *image)
{
  GtkWidget *widget;
  GtkWidget  *button_image;
  AtkObject *obj;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
  if (widget == NULL)
    return NULL;

  button_image = get_image_from_button (widget);
  if (GTK_IS_IMAGE (button_image))
    {
      obj = gtk_widget_get_accessible (button_image);
      return atk_image_get_image_description (ATK_IMAGE (obj));
    }

  return NULL;
}

static void
gtk_button_accessible_get_image_position (AtkImage     *image,
                                          gint         *x,
                                          gint         *y,
                                          AtkCoordType  coord_type)
{
  GtkWidget *widget;
373
  GtkWidget *button_image;
374 375 376 377 378 379 380 381 382 383 384 385 386
  AtkObject *obj;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
  if (widget == NULL)
    {
      *x = G_MININT;
      *y = G_MININT;
      return;
    }

  button_image = get_image_from_button (widget);
  if (button_image != NULL)
    {
387
      obj = gtk_widget_get_accessible (button_image);
388 389
      atk_component_get_extents (ATK_COMPONENT (obj), x, y, NULL, NULL,
                                 coord_type);
390 391 392 393 394 395 396 397 398 399 400 401 402 403
    }
  else
    {
      *x = G_MININT;
      *y = G_MININT;
    }
}

static void
gtk_button_accessible_get_image_size (AtkImage *image,
                                      gint     *width,
                                      gint     *height)
{
  GtkWidget *widget;
404
  GtkWidget *button_image;
405 406 407 408 409 410 411 412 413 414 415
  AtkObject *obj;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
  if (widget == NULL)
    {
      *width = -1;
      *height = -1;
      return;
    }

  button_image = get_image_from_button (widget);
416
  if (GTK_IS_IMAGE (button_image))
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    {
      obj = gtk_widget_get_accessible (GTK_WIDGET (button_image));
      atk_image_get_image_size (ATK_IMAGE (obj), width, height);
    }
  else
    {
      *width = -1;
      *height = -1;
    }
}

static gboolean
gtk_button_accessible_set_image_description (AtkImage    *image,
                                             const gchar *description)
{
  GtkWidget *widget;
433
  GtkWidget *button_image;
434 435 436 437 438 439 440 441
  AtkObject *obj;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));

  if (widget == NULL)
    return FALSE;

  button_image = get_image_from_button (widget);
442
  if (GTK_IMAGE (button_image))
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    {
      obj = gtk_widget_get_accessible (GTK_WIDGET (button_image));
      return atk_image_set_image_description (ATK_IMAGE (obj), description);
    }

  return FALSE;
}

static void
atk_image_interface_init (AtkImageIface *iface)
{
  iface->get_image_description = gtk_button_accessible_get_image_description;
  iface->get_image_position = gtk_button_accessible_get_image_position;
  iface->get_image_size = gtk_button_accessible_get_image_size;
  iface->set_image_description = gtk_button_accessible_set_image_description;
}