1 /* GAIL - The GNOME Accessibility Implementation Library
2 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include <gdk/gdkkeysyms.h>
23 #include "gailrange.h"
24 #include "gailadjustment.h"
25 #include "gail-private-macros.h"
27 static void gail_range_class_init (GailRangeClass *klass);
29 static void gail_range_real_initialize (AtkObject *obj,
32 static void gail_range_finalize (GObject *object);
34 static AtkStateSet* gail_range_ref_state_set (AtkObject *obj);
37 static void gail_range_real_notify_gtk (GObject *obj,
40 static void atk_value_interface_init (AtkValueIface *iface);
41 static void gail_range_get_current_value (AtkValue *obj,
43 static void gail_range_get_maximum_value (AtkValue *obj,
45 static void gail_range_get_minimum_value (AtkValue *obj,
47 static gboolean gail_range_set_current_value (AtkValue *obj,
49 static void gail_range_value_changed (GtkAdjustment *adjustment,
52 static void atk_action_interface_init (AtkActionIface *iface);
53 static gboolean gail_range_do_action (AtkAction *action,
55 static gboolean idle_do_action (gpointer data);
56 static gint gail_range_get_n_actions (AtkAction *action);
57 static G_CONST_RETURN gchar* gail_range_get_description (AtkAction *action,
59 static G_CONST_RETURN gchar* gail_range_get_keybinding (AtkAction *action,
61 static G_CONST_RETURN gchar* gail_range_action_get_name (AtkAction *action,
63 static gboolean gail_range_set_description (AtkAction *action,
66 static GailWidgetClass *parent_class = NULL;
69 gail_range_get_type (void)
71 static GType type = 0;
75 static const GTypeInfo tinfo =
77 sizeof (GailRangeClass),
78 (GBaseInitFunc) NULL, /* base init */
79 (GBaseFinalizeFunc) NULL, /* base finalize */
80 (GClassInitFunc) gail_range_class_init, /* class init */
81 (GClassFinalizeFunc) NULL, /* class finalize */
82 NULL, /* class data */
83 sizeof (GailRange), /* instance size */
85 (GInstanceInitFunc) NULL, /* instance init */
86 NULL /* value table */
89 static const GInterfaceInfo atk_action_info =
91 (GInterfaceInitFunc) atk_action_interface_init,
92 (GInterfaceFinalizeFunc) NULL,
97 static const GInterfaceInfo atk_value_info =
99 (GInterfaceInitFunc) atk_value_interface_init,
100 (GInterfaceFinalizeFunc) NULL,
104 type = g_type_register_static (GAIL_TYPE_WIDGET,
105 "GailRange", &tinfo, 0);
107 g_type_add_interface_static (type, ATK_TYPE_ACTION,
110 g_type_add_interface_static (type, ATK_TYPE_VALUE,
117 gail_range_class_init (GailRangeClass *klass)
119 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
120 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
121 GailWidgetClass *widget_class;
123 widget_class = (GailWidgetClass*)klass;
125 widget_class->notify_gtk = gail_range_real_notify_gtk;
127 class->ref_state_set = gail_range_ref_state_set;
128 class->initialize = gail_range_real_initialize;
130 gobject_class->finalize = gail_range_finalize;
132 parent_class = g_type_class_peek_parent (klass);
136 gail_range_new (GtkWidget *widget)
139 AtkObject *accessible;
141 g_return_val_if_fail (GTK_IS_RANGE (widget), NULL);
143 object = g_object_new (GAIL_TYPE_RANGE, NULL);
145 accessible = ATK_OBJECT (object);
146 atk_object_initialize (accessible, widget);
152 gail_range_real_initialize (AtkObject *obj,
155 GailRange *range = GAIL_RANGE (obj);
158 ATK_OBJECT_CLASS (parent_class)->initialize (obj, data);
160 gtk_range = GTK_RANGE (data);
162 * If a GtkAdjustment already exists for the GtkRange,
163 * create the GailAdjustment
165 if (gtk_range->adjustment)
167 range->adjustment = gail_adjustment_new (gtk_range->adjustment);
168 g_signal_connect (gtk_range->adjustment,
170 G_CALLBACK (gail_range_value_changed),
174 range->adjustment = NULL;
175 range->activate_keybinding=NULL;
176 range->activate_description=NULL;
178 * Assumed to GtkScale (either GtkHScale or GtkVScale)
180 obj->role = ATK_ROLE_SLIDER;
184 gail_range_ref_state_set (AtkObject *obj)
186 AtkStateSet *state_set;
190 state_set = ATK_OBJECT_CLASS (parent_class)->ref_state_set (obj);
191 widget = GTK_ACCESSIBLE (obj)->widget;
196 range = GTK_RANGE (widget);
199 * We do not generate property change for orientation change as there
200 * is no interface to change the orientation which emits a notification
202 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
203 atk_state_set_add_state (state_set, ATK_STATE_HORIZONTAL);
205 atk_state_set_add_state (state_set, ATK_STATE_VERTICAL);
211 atk_value_interface_init (AtkValueIface *iface)
213 g_return_if_fail (iface != NULL);
215 iface->get_current_value = gail_range_get_current_value;
216 iface->get_maximum_value = gail_range_get_maximum_value;
217 iface->get_minimum_value = gail_range_get_minimum_value;
218 iface->set_current_value = gail_range_set_current_value;
223 gail_range_get_current_value (AtkValue *obj,
228 g_return_if_fail (GAIL_IS_RANGE (obj));
230 range = GAIL_RANGE (obj);
231 if (range->adjustment == NULL)
233 * Adjustment has not been specified
237 atk_value_get_current_value (ATK_VALUE (range->adjustment), value);
241 gail_range_get_maximum_value (AtkValue *obj,
246 g_return_if_fail (GAIL_IS_RANGE (obj));
248 range = GAIL_RANGE (obj);
249 if (range->adjustment == NULL)
251 * Adjustment has not been specified
255 atk_value_get_maximum_value (ATK_VALUE (range->adjustment), value);
259 gail_range_get_minimum_value (AtkValue *obj,
264 g_return_if_fail (GAIL_IS_RANGE (obj));
266 range = GAIL_RANGE (obj);
267 if (range->adjustment == NULL)
269 * Adjustment has not been specified
273 atk_value_get_minimum_value (ATK_VALUE (range->adjustment), value);
276 static gboolean gail_range_set_current_value (AtkValue *obj,
281 g_return_val_if_fail (GAIL_IS_RANGE (obj), FALSE);
283 widget = GTK_ACCESSIBLE (obj)->widget;
287 if (G_VALUE_HOLDS_DOUBLE (value))
289 GtkRange *range = GTK_RANGE (widget);
292 new_value = g_value_get_double (value);
293 gtk_range_set_value (range, new_value);
303 gail_range_finalize (GObject *object)
305 GailRange *range = GAIL_RANGE (object);
307 if (range->adjustment)
310 * The GtkAdjustment may live on so we need to dicsonnect the
313 if (GAIL_ADJUSTMENT (range->adjustment)->adjustment)
315 g_signal_handlers_disconnect_by_func (GAIL_ADJUSTMENT (range->adjustment)->adjustment,
316 (void *)gail_range_value_changed,
319 g_object_unref (range->adjustment);
320 range->adjustment = NULL;
322 range->activate_keybinding=NULL;
323 range->activate_description=NULL;
324 if (range->action_idle_handler)
326 g_source_remove (range->action_idle_handler);
327 range->action_idle_handler = 0;
330 G_OBJECT_CLASS (parent_class)->finalize (object);
335 gail_range_real_notify_gtk (GObject *obj,
338 GtkWidget *widget = GTK_WIDGET (obj);
339 GailRange *range = GAIL_RANGE (gtk_widget_get_accessible (widget));
341 if (strcmp (pspec->name, "adjustment") == 0)
344 * Get rid of the GailAdjustment for the GtkAdjustment
345 * which was associated with the range.
347 if (range->adjustment)
349 g_object_unref (range->adjustment);
350 range->adjustment = NULL;
353 * Create the GailAdjustment when notify for "adjustment" property
356 range->adjustment = gail_adjustment_new (GTK_RANGE (widget)->adjustment);
357 g_signal_connect (GTK_RANGE (widget)->adjustment,
359 G_CALLBACK (gail_range_value_changed),
363 parent_class->notify_gtk (obj, pspec);
367 gail_range_value_changed (GtkAdjustment *adjustment,
372 g_return_if_fail (adjustment != NULL);
373 gail_return_if_fail (data != NULL);
375 range = GAIL_RANGE (data);
377 g_object_notify (G_OBJECT (range), "accessible-value");
381 atk_action_interface_init (AtkActionIface *iface)
383 g_return_if_fail (iface != NULL);
385 iface->do_action = gail_range_do_action;
386 iface->get_n_actions = gail_range_get_n_actions;
387 iface->get_description = gail_range_get_description;
388 iface->get_keybinding = gail_range_get_keybinding;
389 iface->get_name = gail_range_action_get_name;
390 iface->set_description = gail_range_set_description;
394 gail_range_do_action (AtkAction *action,
399 gboolean return_value = TRUE;
401 range = GAIL_RANGE (action);
402 widget = GTK_ACCESSIBLE (action)->widget;
408 if (!GTK_WIDGET_SENSITIVE (widget) || !GTK_WIDGET_VISIBLE (widget))
412 if (range->action_idle_handler)
413 return_value = FALSE;
415 range->action_idle_handler = gdk_threads_add_idle (idle_do_action, range);
418 return_value = FALSE;
423 idle_do_action (gpointer data)
428 range = GAIL_RANGE (data);
429 range->action_idle_handler = 0;
430 widget = GTK_ACCESSIBLE (range)->widget;
431 if (widget == NULL /* State is defunct */ ||
432 !GTK_WIDGET_SENSITIVE (widget) || !GTK_WIDGET_VISIBLE (widget))
435 gtk_widget_activate (widget);
441 gail_range_get_n_actions (AtkAction *action)
446 static G_CONST_RETURN gchar*
447 gail_range_get_description (AtkAction *action,
451 G_CONST_RETURN gchar *return_value;
453 range = GAIL_RANGE (action);
455 return_value = range->activate_description;
461 static G_CONST_RETURN gchar*
462 gail_range_get_keybinding (AtkAction *action,
466 gchar *return_value = NULL;
467 range = GAIL_RANGE (action);
473 AtkRelation *relation;
475 gpointer target_object;
478 range = GAIL_RANGE (action);
479 widget = GTK_ACCESSIBLE (range)->widget;
482 set = atk_object_ref_relation_set (ATK_OBJECT (action));
487 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
490 target = atk_relation_get_target (relation);
491 target_object = g_ptr_array_index (target, 0);
492 if (GTK_IS_ACCESSIBLE (target_object))
493 label = GTK_ACCESSIBLE (target_object)->widget;
495 g_object_unref (set);
496 if (GTK_IS_LABEL (label))
498 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
499 if (key_val != GDK_VoidSymbol)
500 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
502 g_free (range->activate_keybinding);
503 range->activate_keybinding = return_value;
508 static G_CONST_RETURN gchar*
509 gail_range_action_get_name (AtkAction *action,
512 G_CONST_RETURN gchar *return_value;
515 return_value = "activate";
523 gail_range_set_description (AtkAction *action,
530 range = GAIL_RANGE (action);
533 value = &range->activate_description;
540 *value = g_strdup (desc);