gtkcellaccessible.c 14 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 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 Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20
#include <glib/gi18n-lib.h>
21
#include <gtk/gtk.h>
22
#include "gtkcontainercellaccessible.h"
23
#include "gtkcellaccessibleprivate.h"
24
#include "gtkcellaccessibleparent.h"
25

26 27 28 29 30
struct _GtkCellAccessiblePrivate
{
  AtkObject *parent;
};

31 32 33 34 35 36 37 38 39 40 41 42 43 44
static const struct {
  AtkState atk_state;
  GtkCellRendererState renderer_state;
  gboolean invert;
} state_map[] = {
  { ATK_STATE_SENSITIVE, GTK_CELL_RENDERER_INSENSITIVE, TRUE },
  { ATK_STATE_ENABLED,   GTK_CELL_RENDERER_INSENSITIVE, TRUE },
  { ATK_STATE_SELECTED,  GTK_CELL_RENDERER_SELECTED,    FALSE },
  /* XXX: why do we map ACTIVE here? */
  { ATK_STATE_ACTIVE,    GTK_CELL_RENDERER_FOCUSED,     FALSE },
  { ATK_STATE_FOCUSED,   GTK_CELL_RENDERER_FOCUSED,     FALSE },
  { ATK_STATE_EXPANDABLE,GTK_CELL_RENDERER_EXPANDABLE,  FALSE },
  { ATK_STATE_EXPANDED,  GTK_CELL_RENDERER_EXPANDED,    FALSE },
};
45

46
static GtkCellRendererState gtk_cell_accessible_get_state (GtkCellAccessible *cell);
47 48 49
static void atk_action_interface_init    (AtkActionIface    *iface);
static void atk_component_interface_init (AtkComponentIface *iface);

50
G_DEFINE_TYPE_WITH_CODE (GtkCellAccessible, gtk_cell_accessible, GTK_TYPE_ACCESSIBLE,
51
                         G_ADD_PRIVATE (GtkCellAccessible)
52 53 54 55 56 57 58
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, atk_component_interface_init))

static gint
gtk_cell_accessible_get_index_in_parent (AtkObject *obj)
{
  GtkCellAccessible *cell;
59
  AtkObject *parent;
60 61

  cell = GTK_CELL_ACCESSIBLE (obj);
62

63 64
  if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (cell->priv->parent))
    return g_list_index (gtk_container_cell_accessible_get_children (GTK_CONTAINER_CELL_ACCESSIBLE (cell->priv->parent)), obj);
65

66
  parent = gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)));
67 68 69
  if (parent == NULL)
    return -1;

70
  return gtk_cell_accessible_parent_get_child_index (GTK_CELL_ACCESSIBLE_PARENT (cell->priv->parent), cell);
71 72
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static AtkRelationSet *
gtk_cell_accessible_ref_relation_set (AtkObject *object)
{
  GtkCellAccessible *cell;
  AtkRelationSet *relationset;
  AtkObject *parent;

  relationset = ATK_OBJECT_CLASS (gtk_cell_accessible_parent_class)->ref_relation_set (object);
  if (relationset == NULL)
    relationset = atk_relation_set_new ();

  cell = GTK_CELL_ACCESSIBLE (object);
  parent = gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)));

  gtk_cell_accessible_parent_update_relationset (GTK_CELL_ACCESSIBLE_PARENT (parent),
                                                 cell,
                                                 relationset);

  return relationset;
}

94
static AtkStateSet *
95
gtk_cell_accessible_ref_state_set (AtkObject *accessible)
96
{
97 98 99
  GtkCellAccessible *cell_accessible;
  AtkStateSet *state_set;
  GtkCellRendererState flags;
100
  guint i;
101 102 103 104 105

  cell_accessible = GTK_CELL_ACCESSIBLE (accessible);

  state_set = atk_state_set_new ();

106
  if (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell_accessible)) == NULL)
107 108 109 110 111
    {
      atk_state_set_add_state (state_set, ATK_STATE_DEFUNCT);
      return state_set;
    }

112
  flags = gtk_cell_accessible_get_state (cell_accessible);
113

114
  atk_state_set_add_state (state_set, ATK_STATE_FOCUSABLE);
115
  atk_state_set_add_state (state_set, ATK_STATE_SELECTABLE);
116
  atk_state_set_add_state (state_set, ATK_STATE_TRANSIENT);
117
  atk_state_set_add_state (state_set, ATK_STATE_VISIBLE);
118

119
  for (i = 0; i < G_N_ELEMENTS (state_map); i++)
120
    {
121 122 123 124 125 126 127 128 129 130
      if (flags & state_map[i].renderer_state)
        {
          if (!state_map[i].invert)
            atk_state_set_add_state (state_set, state_map[i].atk_state);
        }
      else
        {
          if (state_map[i].invert)
            atk_state_set_add_state (state_set, state_map[i].atk_state);
        }
131 132
    }

133
  if (gtk_widget_get_mapped (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell_accessible))))
134
    atk_state_set_add_state (state_set, ATK_STATE_SHOWING);
135

136
  return state_set;
137 138
}

139 140 141 142 143 144 145
static AtkObject *
gtk_cell_accessible_get_parent (AtkObject *object)
{
  GtkCellAccessible *cell = GTK_CELL_ACCESSIBLE (object);

  return cell->priv->parent;
}
146

147
static void
148
gtk_cell_accessible_class_init (GtkCellAccessibleClass *klass)
149 150 151 152 153
{
  AtkObjectClass *class = ATK_OBJECT_CLASS (klass);

  class->get_index_in_parent = gtk_cell_accessible_get_index_in_parent;
  class->ref_state_set = gtk_cell_accessible_ref_state_set;
154
  class->ref_relation_set = gtk_cell_accessible_ref_relation_set;
155
  class->get_parent = gtk_cell_accessible_get_parent;
156 157 158
}

static void
159
gtk_cell_accessible_init (GtkCellAccessible *cell)
160
{
161
  cell->priv = gtk_cell_accessible_get_instance_private (cell);
162 163 164
}

void
165
_gtk_cell_accessible_initialize (GtkCellAccessible *cell,
166
                                 GtkWidget         *widget,
167
                                 AtkObject         *parent)
168
{
169
  gtk_accessible_set_widget (GTK_ACCESSIBLE (cell), widget);
170
  cell->priv->parent = parent;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
}

gboolean
_gtk_cell_accessible_add_state (GtkCellAccessible *cell,
                                AtkStateType       state_type,
                                gboolean           emit_signal)
{
  /* The signal should only be generated if the value changed,
   * not when the cell is set up. So states that are set
   * initially should pass FALSE as the emit_signal argument.
   */
  if (emit_signal)
    {
      atk_object_notify_state_change (ATK_OBJECT (cell), state_type, TRUE);
      /* If state_type is ATK_STATE_VISIBLE, additional notification */
      if (state_type == ATK_STATE_VISIBLE)
Matthias Clasen's avatar
Matthias Clasen committed
187
        g_signal_emit_by_name (cell, "visible-data-changed");
188 189 190 191 192
    }

  /* If the parent is a flyweight container cell, propagate the state
   * change to it also
   */
193 194
  if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (cell->priv->parent))
    _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell->priv->parent), state_type, emit_signal);
195

196
  return TRUE;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
}

gboolean
_gtk_cell_accessible_remove_state (GtkCellAccessible *cell,
                                   AtkStateType       state_type,
                                   gboolean           emit_signal)
{
  /* The signal should only be generated if the value changed,
   * not when the cell is set up.  So states that are set
   * initially should pass FALSE as the emit_signal argument.
   */
  if (emit_signal)
    {
      atk_object_notify_state_change (ATK_OBJECT (cell), state_type, FALSE);
      /* If state_type is ATK_STATE_VISIBLE, additional notification */
      if (state_type == ATK_STATE_VISIBLE)
Matthias Clasen's avatar
Matthias Clasen committed
213
        g_signal_emit_by_name (cell, "visible-data-changed");
214 215 216 217 218
    }

  /* If the parent is a flyweight container cell, propagate the state
   * change to it also
   */
219 220
  if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (cell->priv->parent))
    _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (cell->priv->parent), state_type, emit_signal);
221

222
  return TRUE;
223 224 225 226 227
}

static gint
gtk_cell_accessible_action_get_n_actions (AtkAction *action)
{
228
  return 3;
229 230 231 232 233 234
}

static const gchar *
gtk_cell_accessible_action_get_name (AtkAction *action,
                                     gint       index)
{
235 236 237 238 239 240 241 242 243 244 245
  switch (index)
    {
    case 0:
      return "expand or contract";
    case 1:
      return "edit";
    case 2:
      return "activate";
    default:
      return NULL;
    }
246 247
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
static const gchar *
gtk_cell_accessible_action_get_localized_name (AtkAction *action,
                                               gint       index)
{
  switch (index)
    {
    case 0:
      return C_("Action name", "Expand or contract");
    case 1:
      return C_("Action name", "Edit");
    case 2:
      return C_("Action name", "Activate");
    default:
      return NULL;
    }
}

265 266 267 268
static const gchar *
gtk_cell_accessible_action_get_description (AtkAction *action,
                                            gint       index)
{
269 270 271
  switch (index)
    {
    case 0:
272
      return C_("Action description", "Expands or contracts the row in the tree view containing this cell");
273
    case 1:
274
      return C_("Action description", "Creates a widget in which the contents of the cell can be edited");
275
    case 2:
276
      return C_("Action description", "Activates the cell");
277 278 279
    default:
      return NULL;
    }
280 281 282 283 284 285
}

static const gchar *
gtk_cell_accessible_action_get_keybinding (AtkAction *action,
                                           gint       index)
{
286
  return NULL;
287 288 289 290 291 292 293
}

static gboolean
gtk_cell_accessible_action_do_action (AtkAction *action,
                                      gint       index)
{
  GtkCellAccessible *cell = GTK_CELL_ACCESSIBLE (action);
294
  GtkCellAccessibleParent *parent;
295

296
  cell = GTK_CELL_ACCESSIBLE (action);
297
  if (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)) == NULL)
298 299
    return FALSE;

300
  parent = GTK_CELL_ACCESSIBLE_PARENT (gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell))));
301

302 303 304
  switch (index)
    {
    case 0:
305
      gtk_cell_accessible_parent_expand_collapse (parent, cell);
306
      break;
307
    case 1:
308
      gtk_cell_accessible_parent_edit (parent, cell);
309
      break;
310
    case 2:
311
      gtk_cell_accessible_parent_activate (parent, cell);
312
      break;
313 314 315
    default:
      return FALSE;
    }
316 317 318 319 320 321 322 323 324 325

  return TRUE;
}

static void
atk_action_interface_init (AtkActionIface *iface)
{
  iface->get_n_actions = gtk_cell_accessible_action_get_n_actions;
  iface->do_action = gtk_cell_accessible_action_do_action;
  iface->get_name = gtk_cell_accessible_action_get_name;
326
  iface->get_localized_name = gtk_cell_accessible_action_get_localized_name;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
  iface->get_description = gtk_cell_accessible_action_get_description;
  iface->get_keybinding = gtk_cell_accessible_action_get_keybinding;
}

static void
gtk_cell_accessible_get_extents (AtkComponent *component,
                                 gint         *x,
                                 gint         *y,
                                 gint         *width,
                                 gint         *height,
                                 AtkCoordType  coord_type)
{
  GtkCellAccessible *cell;
  AtkObject *parent;

  cell = GTK_CELL_ACCESSIBLE (component);
343
  parent = gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)));
344

345
  gtk_cell_accessible_parent_get_cell_extents (GTK_CELL_ACCESSIBLE_PARENT (parent),
346 347
                                                cell,
                                                x, y, width, height, coord_type);
348 349 350 351 352 353 354 355 356
}

static gboolean
gtk_cell_accessible_grab_focus (AtkComponent *component)
{
  GtkCellAccessible *cell;
  AtkObject *parent;

  cell = GTK_CELL_ACCESSIBLE (component);
357
  parent = gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)));
358

359
  return gtk_cell_accessible_parent_grab_focus (GTK_CELL_ACCESSIBLE_PARENT (parent), cell);
360 361 362 363 364 365 366 367
}

static void
atk_component_interface_init (AtkComponentIface *iface)
{
  iface->get_extents = gtk_cell_accessible_get_extents;
  iface->grab_focus = gtk_cell_accessible_grab_focus;
}
368

369 370
static GtkCellRendererState
gtk_cell_accessible_get_state (GtkCellAccessible *cell)
371 372 373 374 375
{
  AtkObject *parent;

  g_return_val_if_fail (GTK_IS_CELL_ACCESSIBLE (cell), 0);

376
  parent = gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)));
377 378 379
  if (parent == NULL)
    return 0;

380
  return gtk_cell_accessible_parent_get_renderer_state (GTK_CELL_ACCESSIBLE_PARENT (parent), cell);
381
}
382

383 384
/*
 * gtk_cell_accessible_state_changed:
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
 * @cell: a #GtkCellAccessible
 * @added: the flags that were added from @cell
 * @removed: the flags that were removed from @cell
 *
 * Notifies @cell of state changes. Multiple states may be added
 * or removed at the same time. A state that is @added may not be
 * @removed at the same time.
 **/
void
_gtk_cell_accessible_state_changed (GtkCellAccessible    *cell,
                                    GtkCellRendererState  added,
                                    GtkCellRendererState  removed)
{
  AtkObject *object;
  guint i;

  g_return_if_fail (GTK_IS_CELL_ACCESSIBLE (cell));
  g_return_if_fail ((added & removed) == 0);

  object = ATK_OBJECT (cell);

  for (i = 0; i < G_N_ELEMENTS (state_map); i++)
    {
      if (added & state_map[i].renderer_state)
409
        atk_object_notify_state_change (object,
410 411
                                        state_map[i].atk_state,
                                        !state_map[i].invert);
412
      if (removed & state_map[i].renderer_state)
413
        atk_object_notify_state_change (object,
414 415 416 417
                                        state_map[i].atk_state,
                                        state_map[i].invert);
    }
}
418

419 420
/*
 * gtk_cell_accessible_update_cache:
421
 * @cell: the cell that is changed
422
 * @emit_signal: whether or not to notify the ATK bridge
423 424 425 426 427
 *
 * Notifies the cell that the values in the data in the row that
 * is used to feed the cell renderer with has changed. The
 * cell_changed function of @cell is called to send update
 * notifications for the properties it takes from its cell
428 429 430
 * renderer. If @emit_signal is TRUE, also notify the ATK bridge
 * of the change. The bridge should be notified when an existing
 * cell changes; not when a newly-created cell is being set up.
431
 *
432 433 434 435 436
 * Note that there is no higher granularity available about which
 * properties changed, so you will need to make do with this
 * function.
 **/
void
437 438
_gtk_cell_accessible_update_cache (GtkCellAccessible *cell,
                                   gboolean           emit_signal)
439 440
{
  GtkCellAccessibleClass *klass;
441

442 443 444 445 446
  g_return_if_fail (GTK_CELL_ACCESSIBLE (cell));

  klass = GTK_CELL_ACCESSIBLE_GET_CLASS (cell);

  if (klass->update_cache)
447
    klass->update_cache (cell, emit_signal);
448
}