~kaihengfeng/unity/lp1292830

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
/*
 * Copyright (C) 2011 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Alejandro PiƱeiro Iglesias <apinheiro@igalia.com>
 */

/**
 * SECTION:unity-places_group-accessible
 * @Title: UnityPlacesGroupAccessible
 * @short_description: Implementation of the ATK interfaces for #PlacesGroup
 * @see_also: PlacesGroup
 *
 * #UnityPlacesGroupAccessible implements the required ATK interfaces for
 * #PlacesGroup, mainly exposing the text as his name, as this
 * #object is mainly used as a label
 *
 */

#include <glib/gi18n.h>

#include "unity-places-group-accessible.h"

#include "unitya11y.h"
#include "PlacesGroup.h"

/* GObject */
static void unity_places_group_accessible_class_init(UnityPlacesGroupAccessibleClass* klass);
static void unity_places_group_accessible_init(UnityPlacesGroupAccessible* self);

/* AtkObject.h */
static void         unity_places_group_accessible_initialize(AtkObject* accessible,
                                                             gpointer   data);

G_DEFINE_TYPE(UnityPlacesGroupAccessible, unity_places_group_accessible,  NUX_TYPE_VIEW_ACCESSIBLE);


#define UNITY_PLACES_GROUP_ACCESSIBLE_GET_PRIVATE(obj)            \
  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UNITY_TYPE_PLACES_GROUP_ACCESSIBLE, \
                                UnityPlacesGroupAccessiblePrivate))

struct _UnityPlacesGroupAccessiblePrivate
{
  gchar* stripped_name;
};


static void
unity_places_group_accessible_class_init(UnityPlacesGroupAccessibleClass* klass)
{
  GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
  AtkObjectClass* atk_class = ATK_OBJECT_CLASS(klass);

  /* AtkObject */
  atk_class->initialize = unity_places_group_accessible_initialize;

  g_type_class_add_private(gobject_class, sizeof(UnityPlacesGroupAccessiblePrivate));
}

static void
unity_places_group_accessible_init(UnityPlacesGroupAccessible* self)
{
  UnityPlacesGroupAccessiblePrivate* priv =
    UNITY_PLACES_GROUP_ACCESSIBLE_GET_PRIVATE(self);

  self->priv = priv;
  priv->stripped_name = NULL;
}

AtkObject*
unity_places_group_accessible_new(nux::Object* object)
{
  AtkObject* accessible = NULL;

  g_return_val_if_fail(dynamic_cast<unity::dash::PlacesGroup*>(object), NULL);

  accessible = ATK_OBJECT(g_object_new(UNITY_TYPE_PLACES_GROUP_ACCESSIBLE, NULL));

  atk_object_initialize(accessible, object);

  return accessible;
}

/* AtkObject.h */
/* expand label are usually focused during the key nav, but it don't
 * get a proper name always. In those cases we use the label.
 *
 * In the same way, it is possible that the PlacesGroup get focused
 * so we also set the own name with this label
 */
static void
ensure_proper_name(UnityPlacesGroupAccessible* self)
{
  unity::dash::PlacesGroup* group = NULL;
  nux::Object* nux_object = NULL;
  unity::StaticCairoText* label = NULL;
  unity::StaticCairoText* expand_label = NULL;
  AtkObject* label_accessible = NULL;
  AtkObject* expand_label_accessible = NULL;

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(self));
  group = dynamic_cast<unity::dash::PlacesGroup*>(nux_object);

  if (group == NULL)
    return;

  label = group->GetLabel();
  expand_label = group->GetExpandLabel();


  label_accessible = unity_a11y_get_accessible(label);
  expand_label_accessible = unity_a11y_get_accessible(expand_label);

  if ((label_accessible == NULL) || (expand_label_accessible == NULL))
    return;

  atk_object_set_name(ATK_OBJECT(self), atk_object_get_name(label_accessible));

  if (expand_label->GetText() == "")
    atk_object_set_name(expand_label_accessible, atk_object_get_name(label_accessible));
}


static void
on_label_text_change_cb(unity::StaticCairoText* label, UnityPlacesGroupAccessible* self)
{
  ensure_proper_name(self);
}

static void
unity_places_group_accessible_initialize(AtkObject* accessible,
                                         gpointer data)
{
  unity::dash::PlacesGroup* group = NULL;
  nux::Object* nux_object = NULL;
  unity::StaticCairoText* label = NULL;

  ATK_OBJECT_CLASS(unity_places_group_accessible_parent_class)->initialize(accessible, data);

  atk_object_set_role(accessible, ATK_ROLE_PANEL);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(accessible));
  group = dynamic_cast<unity::dash::PlacesGroup*>(nux_object);

  if (group == NULL)
    return;

  label = group->GetLabel();

  if (label == NULL)
    return;

  ensure_proper_name(UNITY_PLACES_GROUP_ACCESSIBLE(accessible));
  label->sigTextChanged.connect(sigc::bind(sigc::ptr_fun(on_label_text_change_cb),
                                           UNITY_PLACES_GROUP_ACCESSIBLE(accessible)));
}