gtkcomboboxaccessible.c 11.8 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 2004 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
 */

#include "config.h"

20
#include <glib/gi18n-lib.h>
21 22 23
#include <gtk/gtk.h>
#include "gtkcomboboxaccessible.h"

24 25 26 27 28 29
struct _GtkComboBoxAccessiblePrivate
{
  gchar         *name;
  gint           old_selection;
  gboolean       popup_set;
};
30 31

static void atk_action_interface_init    (AtkActionIface    *iface);
32
static void atk_selection_interface_init (AtkSelectionIface *iface);
33

34
G_DEFINE_TYPE_WITH_CODE (GtkComboBoxAccessible, gtk_combo_box_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
35
                         G_ADD_PRIVATE (GtkComboBoxAccessible)
36 37
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
38 39 40 41 42 43 44 45 46 47 48 49 50 51

static void
changed_cb (GtkWidget *widget)
{
  GtkComboBox *combo_box;
  AtkObject *obj;
  GtkComboBoxAccessible *accessible;
  gint index;

  combo_box = GTK_COMBO_BOX (widget);

  index = gtk_combo_box_get_active (combo_box);
  obj = gtk_widget_get_accessible (widget);
  accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
52
  if (accessible->priv->old_selection != index)
53
    {
54
      accessible->priv->old_selection = index;
55
      g_object_notify (G_OBJECT (obj), "accessible-name");
56
      g_signal_emit_by_name (obj, "selection-changed");
57 58 59 60 61 62 63 64 65 66 67
    }
}

static void
gtk_combo_box_accessible_initialize (AtkObject *obj,
                                     gpointer   data)
{
  GtkComboBox *combo_box;
  GtkComboBoxAccessible *accessible;
  AtkObject *popup;

68
  ATK_OBJECT_CLASS (gtk_combo_box_accessible_parent_class)->initialize (obj, data);
69 70 71 72 73

  combo_box = GTK_COMBO_BOX (data);
  accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);

  g_signal_connect (combo_box, "changed", G_CALLBACK (changed_cb), NULL);
74
  accessible->priv->old_selection = gtk_combo_box_get_active (combo_box);
75 76 77 78 79

  popup = gtk_combo_box_get_popup_accessible (combo_box);
  if (popup)
    {
      atk_object_set_parent (popup, obj);
80
      accessible->priv->popup_set = TRUE;
81 82 83 84 85 86 87 88 89 90 91 92
    }
  if (gtk_combo_box_get_has_entry (combo_box))
    atk_object_set_parent (gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (combo_box))), obj);

  obj->role = ATK_ROLE_COMBO_BOX;
}

static void
gtk_combo_box_accessible_finalize (GObject *object)
{
  GtkComboBoxAccessible *combo_box = GTK_COMBO_BOX_ACCESSIBLE (object);

93
  g_free (combo_box->priv->name);
94

95
  G_OBJECT_CLASS (gtk_combo_box_accessible_parent_class)->finalize (object);
96 97
}

98
static const gchar *
99 100 101 102 103 104 105 106 107 108 109
gtk_combo_box_accessible_get_name (AtkObject *obj)
{
  GtkWidget *widget;
  GtkComboBox *combo_box;
  GtkComboBoxAccessible *accessible;
  GtkTreeIter iter;
  const gchar *name;
  GtkTreeModel *model;
  gint n_columns;
  gint i;

110
  name = ATK_OBJECT_CLASS (gtk_combo_box_accessible_parent_class)->get_name (obj);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  if (name)
    return name;

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

  combo_box = GTK_COMBO_BOX (widget);
  accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
  if (gtk_combo_box_get_active_iter (combo_box, &iter))
    {
      model = gtk_combo_box_get_model (combo_box);
      n_columns = gtk_tree_model_get_n_columns (model);
      for (i = 0; i < n_columns; i++)
        {
Javier Jardon's avatar
Javier Jardon committed
126
          GValue value = G_VALUE_INIT;
127 128 129 130

          gtk_tree_model_get_value (model, &iter, i, &value);
          if (G_VALUE_HOLDS_STRING (&value))
            {
131 132
              g_free (accessible->priv->name);
              accessible->priv->name =  g_strdup (g_value_get_string (&value));
133 134 135 136 137 138 139
              g_value_unset (&value);
              break;
            }
          else
            g_value_unset (&value);
        }
    }
140
  return accessible->priv->name;
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 174 175
}

static gint
gtk_combo_box_accessible_get_n_children (AtkObject* obj)
{
  gint n_children = 0;
  GtkWidget *widget;

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

  n_children++;
  if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (widget)))
    n_children++;

  return n_children;
}

static AtkObject *
gtk_combo_box_accessible_ref_child (AtkObject *obj,
                                    gint       i)
{
  GtkWidget *widget;
  AtkObject *child;
  GtkComboBoxAccessible *box;

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

  if (i == 0)
    {
      child = gtk_combo_box_get_popup_accessible (GTK_COMBO_BOX (widget));
      box = GTK_COMBO_BOX_ACCESSIBLE (obj);
176
      if (!box->priv->popup_set)
177 178
        {
          atk_object_set_parent (child, obj);
179
          box->priv->popup_set = TRUE;
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        }
    }
  else if (i == 1 && gtk_combo_box_get_has_entry (GTK_COMBO_BOX (widget)))
    {
      child = gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (widget)));
    }
  else
    {
      return NULL;
    }

  return g_object_ref (child);
}

static void
195
gtk_combo_box_accessible_class_init (GtkComboBoxAccessibleClass *klass)
196 197 198 199 200 201 202 203 204 205 206 207 208
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  AtkObjectClass *class = ATK_OBJECT_CLASS (klass);

  gobject_class->finalize = gtk_combo_box_accessible_finalize;

  class->get_name = gtk_combo_box_accessible_get_name;
  class->get_n_children = gtk_combo_box_accessible_get_n_children;
  class->ref_child = gtk_combo_box_accessible_ref_child;
  class->initialize = gtk_combo_box_accessible_initialize;
}

static void
209
gtk_combo_box_accessible_init (GtkComboBoxAccessible *combo_box)
210
{
211
  combo_box->priv = gtk_combo_box_accessible_get_instance_private (combo_box);
212 213 214
  combo_box->priv->old_selection = -1;
  combo_box->priv->name = NULL;
  combo_box->priv->popup_set = FALSE;
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
}

static gboolean
gtk_combo_box_accessible_do_action (AtkAction *action,
                                    gint       i)
{
  GtkComboBox *combo_box;
  GtkWidget *widget;
  gboolean popup_shown;

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

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

232
  if (i != 0)
233
    return FALSE;
234 235 236 237 238 239 240 241 242

  combo_box = GTK_COMBO_BOX (widget);
  g_object_get (combo_box, "popup-shown", &popup_shown, NULL);
  if (popup_shown)
    gtk_combo_box_popdown (combo_box);
  else
    gtk_combo_box_popup (combo_box);

  return TRUE;
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
}

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

static const gchar *
gtk_combo_box_accessible_get_keybinding (AtkAction *action,
                                         gint       i)
{
  GtkComboBoxAccessible *combo_box;
  GtkWidget *widget;
  GtkWidget *label;
  AtkRelationSet *set;
  AtkRelation *relation;
  GPtrArray *target;
  gpointer target_object;
  guint key_val;
  gchar *return_value = NULL;

  if (i != 0)
    return NULL;

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

  set = atk_object_ref_relation_set (ATK_OBJECT (action));
  if (set == NULL)
    return NULL;

  label = NULL;
  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);
283
      label = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    }
  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_combo_box_accessible_action_get_name (AtkAction *action,
                                          gint       i)
{
300 301 302 303 304 305 306 307 308 309 310 311 312
  if (i == 0)
    return "press";
  return NULL;
}

static const gchar *
gtk_combo_box_accessible_action_get_localized_name (AtkAction *action,
                                                    gint       i)
{
  if (i == 0)
    return C_("Action name", "Press");
  return NULL;
}
313

314 315 316 317 318 319 320
static const gchar *
gtk_combo_box_accessible_action_get_description (AtkAction *action,
                                                 gint       i)
{
  if (i == 0)
    return C_("Action description", "Presses the combobox");
  return NULL;
321 322 323 324 325 326 327 328 329
}

static void
atk_action_interface_init (AtkActionIface *iface)
{
  iface->do_action = gtk_combo_box_accessible_do_action;
  iface->get_n_actions = gtk_combo_box_accessible_get_n_actions;
  iface->get_keybinding = gtk_combo_box_accessible_get_keybinding;
  iface->get_name = gtk_combo_box_accessible_action_get_name;
330 331
  iface->get_localized_name = gtk_combo_box_accessible_action_get_localized_name;
  iface->get_description = gtk_combo_box_accessible_action_get_description;
332
}
333 334 335 336 337 338 339 340 341 342 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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

static gboolean
gtk_combo_box_accessible_add_selection (AtkSelection *selection,
                                        gint          i)
{
  GtkWidget *widget;

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

  gtk_combo_box_set_active (GTK_COMBO_BOX (widget), i);

  return TRUE;
}

static gboolean
gtk_combo_box_accessible_clear_selection (AtkSelection *selection)
{
  GtkWidget *widget;

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

  gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);

  return TRUE;
}

static AtkObject *
gtk_combo_box_accessible_ref_selection (AtkSelection *selection,
                                        gint          i)
{
  GtkComboBox *combo_box;
  GtkWidget *widget;
  AtkObject *obj;
  gint index;

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

  if (i != 0)
    return NULL;

  combo_box = GTK_COMBO_BOX (widget);

  obj = gtk_combo_box_get_popup_accessible (combo_box);
  index = gtk_combo_box_get_active (combo_box);

  return atk_object_ref_accessible_child (obj, index);
}

static gint
gtk_combo_box_accessible_get_selection_count (AtkSelection *selection)
{
  GtkWidget *widget;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
  if (widget == NULL)
    return 0;

  return (gtk_combo_box_get_active (GTK_COMBO_BOX (widget)) == -1) ? 0 : 1;
}

static gboolean
gtk_combo_box_accessible_is_child_selected (AtkSelection *selection,
                                            gint          i)
{
  GtkWidget *widget;
  gint j;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));

  if (widget == NULL)
    return FALSE;

  j = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));

  return (j == i);
}

static gboolean
gtk_combo_box_accessible_remove_selection (AtkSelection *selection,
                                           gint          i)
{
  if (atk_selection_is_child_selected (selection, i))
    atk_selection_clear_selection (selection);

  return TRUE;
}

static void
atk_selection_interface_init (AtkSelectionIface *iface)
{
  iface->add_selection = gtk_combo_box_accessible_add_selection;
  iface->clear_selection = gtk_combo_box_accessible_clear_selection;
  iface->ref_selection = gtk_combo_box_accessible_ref_selection;
  iface->get_selection_count = gtk_combo_box_accessible_get_selection_count;
  iface->is_child_selected = gtk_combo_box_accessible_is_child_selected;
  iface->remove_selection = gtk_combo_box_accessible_remove_selection;
}