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.
24 #include <gdk/gdkkeysyms.h>
25 #include "gailrange.h"
26 #include "gailadjustment.h"
27 #include "gail-private-macros.h"
29 static void gail_range_class_init (GailRangeClass *klass);
31 static void gail_range_init (GailRange *range);
33 static void gail_range_real_initialize (AtkObject *obj,
36 static void gail_range_finalize (GObject *object);
38 static AtkStateSet* gail_range_ref_state_set (AtkObject *obj);
41 static void gail_range_real_notify_gtk (GObject *obj,
44 static void atk_value_interface_init (AtkValueIface *iface);
45 static void gail_range_get_current_value (AtkValue *obj,
47 static void gail_range_get_maximum_value (AtkValue *obj,
49 static void gail_range_get_minimum_value (AtkValue *obj,
51 static gboolean gail_range_set_current_value (AtkValue *obj,
53 static void gail_range_value_changed (GtkAdjustment *adjustment,
56 static void atk_action_interface_init (AtkActionIface *iface);
57 static gboolean gail_range_do_action (AtkAction *action,
59 static gboolean idle_do_action (gpointer data);
60 static gint gail_range_get_n_actions (AtkAction *action);
61 static G_CONST_RETURN gchar* gail_range_get_description (AtkAction *action,
63 static G_CONST_RETURN gchar* gail_range_get_keybinding (AtkAction *action,
65 static G_CONST_RETURN gchar* gail_range_action_get_name (AtkAction *action,
67 static gboolean gail_range_set_description (AtkAction *action,
71 G_DEFINE_TYPE_WITH_CODE (GailRange, gail_range, GAIL_TYPE_WIDGET,
72 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
73 G_IMPLEMENT_INTERFACE (ATK_TYPE_VALUE, atk_value_interface_init))
76 gail_range_class_init (GailRangeClass *klass)
78 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
79 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
80 GailWidgetClass *widget_class;
82 widget_class = (GailWidgetClass*)klass;
84 widget_class->notify_gtk = gail_range_real_notify_gtk;
86 class->ref_state_set = gail_range_ref_state_set;
87 class->initialize = gail_range_real_initialize;
89 gobject_class->finalize = gail_range_finalize;
93 gail_range_init (GailRange *range)
98 gail_range_new (GtkWidget *widget)
101 AtkObject *accessible;
103 g_return_val_if_fail (GTK_IS_RANGE (widget), NULL);
105 object = g_object_new (GAIL_TYPE_RANGE, NULL);
107 accessible = ATK_OBJECT (object);
108 atk_object_initialize (accessible, widget);
114 gail_range_real_initialize (AtkObject *obj,
117 GailRange *range = GAIL_RANGE (obj);
120 ATK_OBJECT_CLASS (gail_range_parent_class)->initialize (obj, data);
122 gtk_range = GTK_RANGE (data);
124 * If a GtkAdjustment already exists for the GtkRange,
125 * create the GailAdjustment
127 if (gtk_range->adjustment)
129 range->adjustment = gail_adjustment_new (gtk_range->adjustment);
130 g_signal_connect (gtk_range->adjustment,
132 G_CALLBACK (gail_range_value_changed),
136 range->adjustment = NULL;
137 range->activate_keybinding=NULL;
138 range->activate_description=NULL;
140 * Assumed to GtkScale (either GtkHScale or GtkVScale)
142 obj->role = ATK_ROLE_SLIDER;
146 gail_range_ref_state_set (AtkObject *obj)
148 AtkStateSet *state_set;
152 state_set = ATK_OBJECT_CLASS (gail_range_parent_class)->ref_state_set (obj);
153 widget = GTK_ACCESSIBLE (obj)->widget;
158 range = GTK_RANGE (widget);
161 * We do not generate property change for orientation change as there
162 * is no interface to change the orientation which emits a notification
164 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
165 atk_state_set_add_state (state_set, ATK_STATE_HORIZONTAL);
167 atk_state_set_add_state (state_set, ATK_STATE_VERTICAL);
173 atk_value_interface_init (AtkValueIface *iface)
175 iface->get_current_value = gail_range_get_current_value;
176 iface->get_maximum_value = gail_range_get_maximum_value;
177 iface->get_minimum_value = gail_range_get_minimum_value;
178 iface->set_current_value = gail_range_set_current_value;
182 gail_range_get_current_value (AtkValue *obj,
187 g_return_if_fail (GAIL_IS_RANGE (obj));
189 range = GAIL_RANGE (obj);
190 if (range->adjustment == NULL)
192 * Adjustment has not been specified
196 atk_value_get_current_value (ATK_VALUE (range->adjustment), value);
200 gail_range_get_maximum_value (AtkValue *obj,
205 g_return_if_fail (GAIL_IS_RANGE (obj));
207 range = GAIL_RANGE (obj);
208 if (range->adjustment == NULL)
210 * Adjustment has not been specified
214 atk_value_get_maximum_value (ATK_VALUE (range->adjustment), value);
218 gail_range_get_minimum_value (AtkValue *obj,
223 g_return_if_fail (GAIL_IS_RANGE (obj));
225 range = GAIL_RANGE (obj);
226 if (range->adjustment == NULL)
228 * Adjustment has not been specified
232 atk_value_get_minimum_value (ATK_VALUE (range->adjustment), value);
235 static gboolean gail_range_set_current_value (AtkValue *obj,
240 g_return_val_if_fail (GAIL_IS_RANGE (obj), FALSE);
242 widget = GTK_ACCESSIBLE (obj)->widget;
246 if (G_VALUE_HOLDS_DOUBLE (value))
248 GtkRange *range = GTK_RANGE (widget);
251 new_value = g_value_get_double (value);
252 gtk_range_set_value (range, new_value);
262 gail_range_finalize (GObject *object)
264 GailRange *range = GAIL_RANGE (object);
266 if (range->adjustment)
269 * The GtkAdjustment may live on so we need to dicsonnect the
272 if (GAIL_ADJUSTMENT (range->adjustment)->adjustment)
274 g_signal_handlers_disconnect_by_func (GAIL_ADJUSTMENT (range->adjustment)->adjustment,
275 (void *)gail_range_value_changed,
278 g_object_unref (range->adjustment);
279 range->adjustment = NULL;
281 range->activate_keybinding=NULL;
282 range->activate_description=NULL;
283 if (range->action_idle_handler)
285 g_source_remove (range->action_idle_handler);
286 range->action_idle_handler = 0;
289 G_OBJECT_CLASS (gail_range_parent_class)->finalize (object);
294 gail_range_real_notify_gtk (GObject *obj,
297 GtkWidget *widget = GTK_WIDGET (obj);
298 GailRange *range = GAIL_RANGE (gtk_widget_get_accessible (widget));
300 if (strcmp (pspec->name, "adjustment") == 0)
303 * Get rid of the GailAdjustment for the GtkAdjustment
304 * which was associated with the range.
306 if (range->adjustment)
308 g_object_unref (range->adjustment);
309 range->adjustment = NULL;
312 * Create the GailAdjustment when notify for "adjustment" property
315 range->adjustment = gail_adjustment_new (GTK_RANGE (widget)->adjustment);
316 g_signal_connect (GTK_RANGE (widget)->adjustment,
318 G_CALLBACK (gail_range_value_changed),
322 GAIL_WIDGET_CLASS (gail_range_parent_class)->notify_gtk (obj, pspec);
326 gail_range_value_changed (GtkAdjustment *adjustment,
331 g_return_if_fail (adjustment != NULL);
332 gail_return_if_fail (data != NULL);
334 range = GAIL_RANGE (data);
336 g_object_notify (G_OBJECT (range), "accessible-value");
340 atk_action_interface_init (AtkActionIface *iface)
342 iface->do_action = gail_range_do_action;
343 iface->get_n_actions = gail_range_get_n_actions;
344 iface->get_description = gail_range_get_description;
345 iface->get_keybinding = gail_range_get_keybinding;
346 iface->get_name = gail_range_action_get_name;
347 iface->set_description = gail_range_set_description;
351 gail_range_do_action (AtkAction *action,
356 gboolean return_value = TRUE;
358 range = GAIL_RANGE (action);
359 widget = GTK_ACCESSIBLE (action)->widget;
365 if (!GTK_WIDGET_SENSITIVE (widget) || !GTK_WIDGET_VISIBLE (widget))
369 if (range->action_idle_handler)
370 return_value = FALSE;
372 range->action_idle_handler = gdk_threads_add_idle (idle_do_action, range);
375 return_value = FALSE;
380 idle_do_action (gpointer data)
385 range = GAIL_RANGE (data);
386 range->action_idle_handler = 0;
387 widget = GTK_ACCESSIBLE (range)->widget;
388 if (widget == NULL /* State is defunct */ ||
389 !GTK_WIDGET_SENSITIVE (widget) || !GTK_WIDGET_VISIBLE (widget))
392 gtk_widget_activate (widget);
398 gail_range_get_n_actions (AtkAction *action)
403 static G_CONST_RETURN gchar*
404 gail_range_get_description (AtkAction *action,
408 G_CONST_RETURN gchar *return_value;
410 range = GAIL_RANGE (action);
412 return_value = range->activate_description;
418 static G_CONST_RETURN gchar*
419 gail_range_get_keybinding (AtkAction *action,
423 gchar *return_value = NULL;
424 range = GAIL_RANGE (action);
430 AtkRelation *relation;
432 gpointer target_object;
435 range = GAIL_RANGE (action);
436 widget = GTK_ACCESSIBLE (range)->widget;
439 set = atk_object_ref_relation_set (ATK_OBJECT (action));
444 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
447 target = atk_relation_get_target (relation);
448 target_object = g_ptr_array_index (target, 0);
449 if (GTK_IS_ACCESSIBLE (target_object))
450 label = GTK_ACCESSIBLE (target_object)->widget;
452 g_object_unref (set);
453 if (GTK_IS_LABEL (label))
455 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
456 if (key_val != GDK_VoidSymbol)
457 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
459 g_free (range->activate_keybinding);
460 range->activate_keybinding = return_value;
465 static G_CONST_RETURN gchar*
466 gail_range_action_get_name (AtkAction *action,
469 G_CONST_RETURN gchar *return_value;
472 return_value = "activate";
480 gail_range_set_description (AtkAction *action,
487 range = GAIL_RANGE (action);
490 value = &range->activate_description;
497 *value = g_strdup (desc);