~azzar1/unity/add-missing-header

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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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
/*
 * 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-switcher-accessible
 * @Title: UnitySwitcherAccessible
 * @short_description: Implementation of the ATK interfaces for #SwitcherView
 * @see_also: SwitcherView
 *
 * #UnitySwitcherAccessible implements the required ATK interfaces for
 * #SwitcherView, ie: exposing the different AbstractLauncherIcon on the
 * #model as child of the object.
 *
 */

#include <glib/gi18n.h>

#include "unity-switcher-accessible.h"
#include "unity-launcher-icon-accessible.h"

#include "unitya11y.h"
#include "SwitcherView.h"
#include "SwitcherModel.h"

using namespace unity::switcher;
using namespace unity::launcher;

/* GObject */
static void unity_switcher_accessible_class_init(UnitySwitcherAccessibleClass* klass);
static void unity_switcher_accessible_init(UnitySwitcherAccessible* self);
static void unity_switcher_accessible_finalize(GObject* object);

/* AtkObject.h */
static void       unity_switcher_accessible_initialize(AtkObject* accessible,
                                                       gpointer   data);
static gint       unity_switcher_accessible_get_n_children(AtkObject* obj);
static AtkObject* unity_switcher_accessible_ref_child(AtkObject* obj,
                                                      gint i);
static AtkStateSet* unity_switcher_accessible_ref_state_set(AtkObject* obj);

/* AtkSelection */
static void       atk_selection_interface_init(AtkSelectionIface* iface);
static AtkObject* unity_switcher_accessible_ref_selection(AtkSelection* selection,
                                                          gint i);
static gint       unity_switcher_accessible_get_selection_count(AtkSelection* selection);
static gboolean   unity_switcher_accessible_is_child_selected(AtkSelection* selection,
                                                              gint i);
/* NuxAreaAccessible */
static gboolean   unity_switcher_accessible_check_pending_notification(NuxAreaAccessible* self);

/* private */
static void       on_selection_changed_cb(AbstractLauncherIcon::Ptr const& icon,
                                          UnitySwitcherAccessible* switcher_accessible);
static void       create_children(UnitySwitcherAccessible* self);


G_DEFINE_TYPE_WITH_CODE(UnitySwitcherAccessible, unity_switcher_accessible,  NUX_TYPE_VIEW_ACCESSIBLE,
                        G_IMPLEMENT_INTERFACE(ATK_TYPE_SELECTION, atk_selection_interface_init))

#define UNITY_SWITCHER_ACCESSIBLE_GET_PRIVATE(obj)                      \
  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UNITY_TYPE_SWITCHER_ACCESSIBLE,  \
                                UnitySwitcherAccessiblePrivate))

struct _UnitySwitcherAccessiblePrivate
{
  /* We maintain the children. Although the LauncherIcon are shared
   * between the Switcher and Launcher, in order to keep a hierarchy
   * coherence, we create a different accessible object  */
  GSList* children;

  sigc::connection on_selection_changed_connection;
};


static void
unity_switcher_accessible_class_init(UnitySwitcherAccessibleClass* klass)
{
  GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
  AtkObjectClass* atk_class = ATK_OBJECT_CLASS(klass);
  NuxAreaAccessibleClass* area_class = NUX_AREA_ACCESSIBLE_CLASS(klass);

  gobject_class->finalize = unity_switcher_accessible_finalize;

  /* AtkObject */
  atk_class->get_n_children = unity_switcher_accessible_get_n_children;
  atk_class->ref_child = unity_switcher_accessible_ref_child;
  atk_class->initialize = unity_switcher_accessible_initialize;
  atk_class->ref_state_set = unity_switcher_accessible_ref_state_set;

  /* NuxAreaAccessible */
  area_class->check_pending_notification = unity_switcher_accessible_check_pending_notification;

  g_type_class_add_private(gobject_class, sizeof(UnitySwitcherAccessiblePrivate));
}

static void
unity_switcher_accessible_init(UnitySwitcherAccessible* self)
{
  UnitySwitcherAccessiblePrivate* priv =
    UNITY_SWITCHER_ACCESSIBLE_GET_PRIVATE(self);

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

static void
unity_switcher_accessible_finalize(GObject* object)
{
  UnitySwitcherAccessible* self = UNITY_SWITCHER_ACCESSIBLE(object);

  self->priv->on_selection_changed_connection.disconnect();

  if (self->priv->children)
  {
    g_slist_free_full(self->priv->children, g_object_unref);
    self->priv->children = NULL;
  }

  G_OBJECT_CLASS(unity_switcher_accessible_parent_class)->finalize(object);
}

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

  g_return_val_if_fail(dynamic_cast<SwitcherView*>(object), NULL);

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

  atk_object_initialize(accessible, object);
  atk_object_set_name(accessible, _("Switcher"));

  return accessible;
}

/* AtkObject.h */
static void
unity_switcher_accessible_initialize(AtkObject* accessible,
                                     gpointer data)
{
  SwitcherView* switcher = NULL;
  nux::Object* nux_object = NULL;
  UnitySwitcherAccessible* self = NULL;
  SwitcherModel::Ptr model;

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

  atk_object_set_role(accessible, ATK_ROLE_TOOL_BAR);

  self = UNITY_SWITCHER_ACCESSIBLE(accessible);
  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(accessible));
  switcher = dynamic_cast<SwitcherView*>(nux_object);
  if (switcher == NULL)
    return;

  model = switcher->GetModel();

  if (model)
  {
    self->priv->on_selection_changed_connection  =
      model->selection_changed.connect(sigc::bind(sigc::ptr_fun(on_selection_changed_cb),
                                                  self));

    create_children(self);
  }

  /* To force being connected to the window::activate signal */
  nux_area_accessible_parent_window_active(NUX_AREA_ACCESSIBLE(self));
}

static gint
unity_switcher_accessible_get_n_children(AtkObject* obj)
{
  nux::Object* object = NULL;
  UnitySwitcherAccessible* self = NULL;

  g_return_val_if_fail(UNITY_IS_SWITCHER_ACCESSIBLE(obj), 0);
  self = UNITY_SWITCHER_ACCESSIBLE(obj);

  object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(obj));
  if (!object) /* state is defunct */
    return 0;

  return g_slist_length(self->priv->children);
}

static AtkObject*
unity_switcher_accessible_ref_child(AtkObject* obj,
                                    gint i)
{
  gint num = 0;
  nux::Object* nux_object = NULL;
  AtkObject* child_accessible = NULL;
  UnitySwitcherAccessible* self = NULL;

  g_return_val_if_fail(UNITY_IS_SWITCHER_ACCESSIBLE(obj), NULL);
  num = atk_object_get_n_accessible_children(obj);
  g_return_val_if_fail((i < num) && (i >= 0), NULL);
  self = UNITY_SWITCHER_ACCESSIBLE(obj);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(obj));
  if (!nux_object) /* state is defunct */
    return 0;

  child_accessible = ATK_OBJECT(g_slist_nth_data(self->priv->children, i));

  g_object_ref(child_accessible);

  return child_accessible;
}

static AtkStateSet*
unity_switcher_accessible_ref_state_set(AtkObject* obj)
{
  AtkStateSet* state_set = NULL;
  nux::Object* nux_object = NULL;

  g_return_val_if_fail(UNITY_IS_SWITCHER_ACCESSIBLE(obj), NULL);

  state_set =
    ATK_OBJECT_CLASS(unity_switcher_accessible_parent_class)->ref_state_set(obj);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(obj));

  if (nux_object == NULL) /* defunct */
    return state_set;

  /* The Switcher is always focusable */
  atk_state_set_add_state(state_set, ATK_STATE_FOCUSABLE);

  /* The Switcher is always focused. Looking SwitcherController code,
   * SwitcherView is only created to be presented to the user */
  atk_state_set_add_state(state_set, ATK_STATE_FOCUSED);

  return state_set;
}

/* AtkSelection */
static void
atk_selection_interface_init(AtkSelectionIface* iface)
{
  iface->ref_selection = unity_switcher_accessible_ref_selection;
  iface->get_selection_count = unity_switcher_accessible_get_selection_count;
  iface->is_child_selected = unity_switcher_accessible_is_child_selected;

  /* NOTE: for the moment we don't provide the implementation for the
     "interactable" methods, it is, the methods that allow to change
     the selected icon. The Switcher doesn't provide that API, and
     right now  we are focusing on a normal user input.*/
  /* iface->add_selection = unity_switcher_accessible_add_selection; */
  /* iface->clear_selection = unity_switcher_accessible_clear_selection; */
  /* iface->remove_selection = unity_switcher_accessible_remove_selection; */

  /* This method will never be implemented, as select all the switcher
     icons makes no sense */
  /* iface->select_all = unity_switcher_accessible_select_all_selection; */
}

static AtkObject*
unity_switcher_accessible_ref_selection(AtkSelection* selection,
                                        gint i)
{
  SwitcherView* switcher = NULL;
  SwitcherModel::Ptr switcher_model;
  nux::Object* nux_object = NULL;
  gint selected_index = 0;
  AtkObject* accessible_selected = NULL;
  UnitySwitcherAccessible* self = NULL;

  g_return_val_if_fail(UNITY_IS_SWITCHER_ACCESSIBLE(selection), 0);
  /* there can be only just item selected */
  g_return_val_if_fail(i == 0, NULL);
  self = UNITY_SWITCHER_ACCESSIBLE(selection);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(selection));
  if (!nux_object) /* state is defunct */
    return 0;

  switcher = static_cast<SwitcherView*>(nux_object);
  switcher_model = switcher->GetModel();
  selected_index = switcher_model->SelectionIndex();

  accessible_selected = ATK_OBJECT(g_slist_nth_data(self->priv->children,
                                                    selected_index));

  if (accessible_selected != NULL)
    g_object_ref(accessible_selected);

  return accessible_selected;
}

static gint
unity_switcher_accessible_get_selection_count(AtkSelection* selection)
{
  SwitcherView* switcher = NULL;
  SwitcherModel::Ptr switcher_model;
  nux::Object* nux_object = NULL;

  g_return_val_if_fail(UNITY_IS_SWITCHER_ACCESSIBLE(selection), 0);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(selection));
  if (!nux_object) /* state is defunct */
    return 0;

  switcher = static_cast<SwitcherView*>(nux_object);
  switcher_model = switcher->GetModel();

  if (!switcher_model->Selection())
    return 0;
  else
    return 1;
}

static gboolean
unity_switcher_accessible_is_child_selected(AtkSelection* selection,
                                            gint i)
{
  SwitcherView* switcher = NULL;
  SwitcherModel::Ptr switcher_model;
  SwitcherModel::iterator it;
  nux::Object* nux_object = NULL;
  gint selected_index = 0;

  g_return_val_if_fail(UNITY_IS_SWITCHER_ACCESSIBLE(selection), FALSE);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(selection));
  if (!nux_object) /* state is defunct */
    return 0;

  switcher = static_cast<SwitcherView*>(nux_object);
  switcher_model = switcher->GetModel();
  selected_index = switcher_model->SelectionIndex();

  if (selected_index == i)
    return TRUE;
  else
    return FALSE;
}

/* NuxAreaAccessible */
static gboolean
unity_switcher_accessible_check_pending_notification(NuxAreaAccessible* self)
{
  g_return_val_if_fail(UNITY_IS_SWITCHER_ACCESSIBLE(self), FALSE);

  /* Overriding the method: the switcher doesn't get the key focus of
   * focus (Focusable) */
  /* From SwitcherController: it shows that the switcher only exists
   * to be shown to the user, so if the parent window gets actived, we
   * assume that the switcher will be automatically focused
   */
  atk_object_notify_state_change(ATK_OBJECT(self), ATK_STATE_FOCUSED, TRUE);
  g_signal_emit_by_name(self, "focus-event", TRUE, NULL);

  return TRUE;
}

/* private */
static void
on_selection_changed_cb(AbstractLauncherIcon::Ptr const& icon,
                        UnitySwitcherAccessible* switcher_accessible)
{
  g_signal_emit_by_name(ATK_OBJECT(switcher_accessible), "selection-changed");
}

static void
create_children(UnitySwitcherAccessible* self)
{
  gint index = 0;
  nux::Object* nux_object = NULL;
  SwitcherView* switcher = NULL;
  SwitcherModel::iterator it;
  AtkObject* child_accessible = NULL;

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(self));
  if (!nux_object) /* state is defunct */
    return;

  switcher = static_cast<SwitcherView*>(nux_object);
  SwitcherModel::Ptr const& switcher_model = switcher->GetModel();

  if (!switcher_model)
    return;

  for (AbstractLauncherIcon::Ptr const& child : *switcher_model)
  {
    child_accessible = unity_launcher_icon_accessible_new(child.GetPointer());
    atk_object_set_parent(child_accessible, ATK_OBJECT(self));
    self->priv->children = g_slist_append(self->priv->children,
                                          child_accessible);
    unity_launcher_icon_accessible_set_index(UNITY_LAUNCHER_ICON_ACCESSIBLE(child_accessible),
                                             index++);
  }
}