summaryrefslogtreecommitdiff
path: root/modules/other/gail/gailoptionmenu.c
blob: 680ab20de3694774189f2a0527bf36d10a1dcb10 (plain)
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
/* GAIL - The GNOME Accessibility Implementation Library
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>

#undef GTK_DISABLE_DEPRECATED

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

#include "gailoptionmenu.h"

static void                  gail_option_menu_class_init       (GailOptionMenuClass *klass);
static void                  gail_option_menu_init             (GailOptionMenu  *menu);
static void		     gail_option_menu_real_initialize  (AtkObject       *obj,
                                                                gpointer        data);

static gint                  gail_option_menu_get_n_children   (AtkObject       *obj);
static AtkObject*            gail_option_menu_ref_child        (AtkObject       *obj,
                                                                gint            i);
static gint                  gail_option_menu_real_add_gtk     (GtkContainer    *container,
                                                                GtkWidget       *widget,
                                                                gpointer        data);
static gint                  gail_option_menu_real_remove_gtk  (GtkContainer    *container,
                                                                GtkWidget       *widget,
                                                                gpointer        data);


static void                  atk_action_interface_init         (AtkActionIface  *iface);

static gboolean              gail_option_menu_do_action        (AtkAction       *action,
                                                                gint            i);
static gboolean              idle_do_action                    (gpointer        data);
static gint                  gail_option_menu_get_n_actions    (AtkAction       *action);
static G_CONST_RETURN gchar* gail_option_menu_get_description  (AtkAction       *action,
                                                                gint            i);
static G_CONST_RETURN gchar* gail_option_menu_action_get_name  (AtkAction       *action,
                                                                gint            i);
static gboolean              gail_option_menu_set_description  (AtkAction       *action,
                                                                gint            i,
                                                                const gchar     *desc);
static void                  gail_option_menu_changed          (GtkOptionMenu   *option_menu);

G_DEFINE_TYPE_WITH_CODE (GailOptionMenu, gail_option_menu, GAIL_TYPE_BUTTON,
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))

static void
gail_option_menu_class_init (GailOptionMenuClass *klass)
{
  AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
  GailContainerClass *container_class;

  container_class = (GailContainerClass *) klass;

  class->get_n_children = gail_option_menu_get_n_children;
  class->ref_child = gail_option_menu_ref_child;
  class->initialize = gail_option_menu_real_initialize;

  container_class->add_gtk = gail_option_menu_real_add_gtk;
  container_class->remove_gtk = gail_option_menu_real_remove_gtk;
}

static void
gail_option_menu_init (GailOptionMenu  *menu)
{
}

static void
gail_option_menu_real_initialize (AtkObject *obj,
                                  gpointer  data)
{
  GtkOptionMenu *option_menu;

  ATK_OBJECT_CLASS (gail_option_menu_parent_class)->initialize (obj, data);

  option_menu = GTK_OPTION_MENU (data);

  g_signal_connect (option_menu, "changed",
                    G_CALLBACK (gail_option_menu_changed), NULL);

  obj->role = ATK_ROLE_COMBO_BOX;
}

static gint
gail_option_menu_get_n_children (AtkObject *obj)
{
  GtkWidget *widget;
  GtkOptionMenu *option_menu;
  gint n_children = 0;

  g_return_val_if_fail (GAIL_IS_OPTION_MENU (obj), 0);

  widget = GTK_ACCESSIBLE (obj)->widget;
  if (widget == NULL)
    /*
     * State is defunct
     */
    return 0;

  option_menu = GTK_OPTION_MENU (widget);
  if (gtk_option_menu_get_menu (option_menu))
      n_children++;

  return n_children;;
}

static AtkObject*
gail_option_menu_ref_child (AtkObject *obj,
                            gint      i)
{
  GtkWidget *widget;
  AtkObject *accessible;

  g_return_val_if_fail (GAIL_IS_OPTION_MENU (obj), NULL);

  widget = GTK_ACCESSIBLE (obj)->widget;
  if (widget == NULL)
    /*
     * State is defunct
     */
    return NULL;


  if (i == 0)
    accessible = g_object_ref (gtk_widget_get_accessible (gtk_option_menu_get_menu (GTK_OPTION_MENU (widget))));
   else
    accessible = NULL;

  return accessible;
}

static gint
gail_option_menu_real_add_gtk (GtkContainer *container,
                               GtkWidget    *widget,
                               gpointer     data)
{
  AtkObject* atk_parent = ATK_OBJECT (data);
  AtkObject* atk_child = gtk_widget_get_accessible (widget);

  GAIL_CONTAINER_CLASS (gail_option_menu_parent_class)->add_gtk (container, widget, data);

  g_object_notify (G_OBJECT (atk_child), "accessible_parent");

  g_signal_emit_by_name (atk_parent, "children_changed::add",
			 1, atk_child, NULL);

  return 1;
}

static gint 
gail_option_menu_real_remove_gtk (GtkContainer *container,
                                  GtkWidget    *widget,
                                  gpointer     data)
{
  AtkPropertyValues values = { NULL };
  AtkObject* atk_parent = ATK_OBJECT (data);
  AtkObject *atk_child = gtk_widget_get_accessible (widget);

  g_value_init (&values.old_value, G_TYPE_POINTER);
  g_value_set_pointer (&values.old_value, atk_parent);

  values.property_name = "accessible-parent";
  g_signal_emit_by_name (atk_child,
                         "property_change::accessible-parent", &values, NULL);
  g_signal_emit_by_name (atk_parent, "children_changed::remove",
			 1, atk_child, NULL);

  return 1;
}

static void
atk_action_interface_init (AtkActionIface *iface)
{
  iface->do_action = gail_option_menu_do_action;
  iface->get_n_actions = gail_option_menu_get_n_actions;
  iface->get_description = gail_option_menu_get_description;
  iface->get_name = gail_option_menu_action_get_name;
  iface->set_description = gail_option_menu_set_description;
}

static gboolean
gail_option_menu_do_action (AtkAction *action,
                            gint      i)
{
  GtkWidget *widget;
  GailButton *button; 
  gboolean return_value = TRUE;

  button = GAIL_BUTTON (action);
  widget = GTK_ACCESSIBLE (action)->widget;
  if (widget == NULL)
    /*
     * State is defunct
     */
    return FALSE;

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

  switch (i)
    {
    case 0:
      if (button->action_idle_handler)
        return_value = FALSE;
      else
        button->action_idle_handler = gdk_threads_add_idle (idle_do_action, button);
      break;
    default:
      return_value = FALSE;
      break;
    }
  return return_value; 
}

static gboolean 
idle_do_action (gpointer data)
{
  GtkButton *button; 
  GtkWidget *widget;
  GdkEvent tmp_event;
  GailButton *gail_button;

  gail_button = GAIL_BUTTON (data);
  gail_button->action_idle_handler = 0;

  widget = GTK_ACCESSIBLE (gail_button)->widget;
  if (widget == NULL /* State is defunct */ ||
      !gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
    return FALSE;

  button = GTK_BUTTON (widget); 

  button->in_button = TRUE;
  g_signal_emit_by_name (button, "enter");
  /*
   * Simulate a button press event. calling gtk_button_pressed() does
   * not get the job done for a GtkOptionMenu.  
   */
  tmp_event.button.type = GDK_BUTTON_PRESS;
  tmp_event.button.window = widget->window;
  tmp_event.button.button = 1;
  tmp_event.button.send_event = TRUE;
  tmp_event.button.time = GDK_CURRENT_TIME;
  tmp_event.button.axes = NULL;

  gtk_widget_event (widget, &tmp_event);

  return FALSE;
}

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

static G_CONST_RETURN gchar*
gail_option_menu_get_description (AtkAction *action,
                                  gint      i)
{
  GailButton *button;
  G_CONST_RETURN gchar *return_value;

  button = GAIL_BUTTON (action);

  switch (i)
    {
    case 0:
      return_value = button->press_description;
      break;
    default:
      return_value = NULL;
      break;
    }
  return return_value; 
}

static G_CONST_RETURN gchar*
gail_option_menu_action_get_name (AtkAction *action,
                                  gint      i)
{
  G_CONST_RETURN gchar *return_value;

  switch (i)
    {
    case 0:
      /*
       * This action simulates a button press by simulating moving the
       * mouse into the button followed by pressing the left mouse button.
       */
      return_value = "press";
      break;
    default:
      return_value = NULL;
      break;
  }
  return return_value; 
}

static gboolean
gail_option_menu_set_description (AtkAction      *action,
                                  gint           i,
                                  const gchar    *desc)
{
  GailButton *button;
  gchar **value;

  button = GAIL_BUTTON (action);

  switch (i)
    {
    case 0:
      value = &button->press_description;
      break;
    default:
      value = NULL;
      break;
    }

  if (value)
    {
      g_free (*value);
      *value = g_strdup (desc);
      return TRUE;
    }
  else
    return FALSE;
}

static void
gail_option_menu_changed (GtkOptionMenu   *option_menu)
{
  GailOptionMenu *gail_option_menu;

  gail_option_menu = GAIL_OPTION_MENU (gtk_widget_get_accessible (GTK_WIDGET (option_menu)));
  g_object_notify (G_OBJECT (gail_option_menu), "accessible-name");
}