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_real_initialize (AtkObject *obj,
101 GailRange *range = GAIL_RANGE (obj);
104 ATK_OBJECT_CLASS (gail_range_parent_class)->initialize (obj, data);
106 gtk_range = GTK_RANGE (data);
108 * If a GtkAdjustment already exists for the GtkRange,
109 * create the GailAdjustment
111 if (gtk_range->adjustment)
113 range->adjustment = gail_adjustment_new (gtk_range->adjustment);
114 g_signal_connect (gtk_range->adjustment,
116 G_CALLBACK (gail_range_value_changed),
120 range->adjustment = NULL;
121 range->activate_keybinding=NULL;
122 range->activate_description=NULL;
124 * Assumed to GtkScale (either GtkHScale or GtkVScale)
126 obj->role = ATK_ROLE_SLIDER;
130 gail_range_ref_state_set (AtkObject *obj)
132 AtkStateSet *state_set;
136 state_set = ATK_OBJECT_CLASS (gail_range_parent_class)->ref_state_set (obj);
137 widget = GTK_ACCESSIBLE (obj)->widget;
142 range = GTK_RANGE (widget);
145 * We do not generate property change for orientation change as there
146 * is no interface to change the orientation which emits a notification
148 if (range->orientation == GTK_ORIENTATION_HORIZONTAL)
149 atk_state_set_add_state (state_set, ATK_STATE_HORIZONTAL);
151 atk_state_set_add_state (state_set, ATK_STATE_VERTICAL);
157 atk_value_interface_init (AtkValueIface *iface)
159 iface->get_current_value = gail_range_get_current_value;
160 iface->get_maximum_value = gail_range_get_maximum_value;
161 iface->get_minimum_value = gail_range_get_minimum_value;
162 iface->set_current_value = gail_range_set_current_value;
166 gail_range_get_current_value (AtkValue *obj,
171 g_return_if_fail (GAIL_IS_RANGE (obj));
173 range = GAIL_RANGE (obj);
174 if (range->adjustment == NULL)
176 * Adjustment has not been specified
180 atk_value_get_current_value (ATK_VALUE (range->adjustment), value);
184 gail_range_get_maximum_value (AtkValue *obj,
189 g_return_if_fail (GAIL_IS_RANGE (obj));
191 range = GAIL_RANGE (obj);
192 if (range->adjustment == NULL)
194 * Adjustment has not been specified
198 atk_value_get_maximum_value (ATK_VALUE (range->adjustment), value);
202 gail_range_get_minimum_value (AtkValue *obj,
207 g_return_if_fail (GAIL_IS_RANGE (obj));
209 range = GAIL_RANGE (obj);
210 if (range->adjustment == NULL)
212 * Adjustment has not been specified
216 atk_value_get_minimum_value (ATK_VALUE (range->adjustment), value);
219 static gboolean gail_range_set_current_value (AtkValue *obj,
224 g_return_val_if_fail (GAIL_IS_RANGE (obj), FALSE);
226 widget = GTK_ACCESSIBLE (obj)->widget;
230 if (G_VALUE_HOLDS_DOUBLE (value))
232 GtkRange *range = GTK_RANGE (widget);
235 new_value = g_value_get_double (value);
236 gtk_range_set_value (range, new_value);
246 gail_range_finalize (GObject *object)
248 GailRange *range = GAIL_RANGE (object);
250 if (range->adjustment)
253 * The GtkAdjustment may live on so we need to dicsonnect the
256 if (GAIL_ADJUSTMENT (range->adjustment)->adjustment)
258 g_signal_handlers_disconnect_by_func (GAIL_ADJUSTMENT (range->adjustment)->adjustment,
259 (void *)gail_range_value_changed,
262 g_object_unref (range->adjustment);
263 range->adjustment = NULL;
265 range->activate_keybinding=NULL;
266 range->activate_description=NULL;
267 if (range->action_idle_handler)
269 g_source_remove (range->action_idle_handler);
270 range->action_idle_handler = 0;
273 G_OBJECT_CLASS (gail_range_parent_class)->finalize (object);
278 gail_range_real_notify_gtk (GObject *obj,
281 GtkWidget *widget = GTK_WIDGET (obj);
282 GailRange *range = GAIL_RANGE (gtk_widget_get_accessible (widget));
284 if (strcmp (pspec->name, "adjustment") == 0)
287 * Get rid of the GailAdjustment for the GtkAdjustment
288 * which was associated with the range.
290 if (range->adjustment)
292 g_object_unref (range->adjustment);
293 range->adjustment = NULL;
296 * Create the GailAdjustment when notify for "adjustment" property
299 range->adjustment = gail_adjustment_new (GTK_RANGE (widget)->adjustment);
300 g_signal_connect (GTK_RANGE (widget)->adjustment,
302 G_CALLBACK (gail_range_value_changed),
306 GAIL_WIDGET_CLASS (gail_range_parent_class)->notify_gtk (obj, pspec);
310 gail_range_value_changed (GtkAdjustment *adjustment,
315 g_return_if_fail (adjustment != NULL);
316 gail_return_if_fail (data != NULL);
318 range = GAIL_RANGE (data);
320 g_object_notify (G_OBJECT (range), "accessible-value");
324 atk_action_interface_init (AtkActionIface *iface)
326 iface->do_action = gail_range_do_action;
327 iface->get_n_actions = gail_range_get_n_actions;
328 iface->get_description = gail_range_get_description;
329 iface->get_keybinding = gail_range_get_keybinding;
330 iface->get_name = gail_range_action_get_name;
331 iface->set_description = gail_range_set_description;
335 gail_range_do_action (AtkAction *action,
340 gboolean return_value = TRUE;
342 range = GAIL_RANGE (action);
343 widget = GTK_ACCESSIBLE (action)->widget;
349 if (!gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
353 if (range->action_idle_handler)
354 return_value = FALSE;
356 range->action_idle_handler = gdk_threads_add_idle (idle_do_action, range);
359 return_value = FALSE;
364 idle_do_action (gpointer data)
369 range = GAIL_RANGE (data);
370 range->action_idle_handler = 0;
371 widget = GTK_ACCESSIBLE (range)->widget;
372 if (widget == NULL /* State is defunct */ ||
373 !gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
376 gtk_widget_activate (widget);
382 gail_range_get_n_actions (AtkAction *action)
387 static G_CONST_RETURN gchar*
388 gail_range_get_description (AtkAction *action,
392 G_CONST_RETURN gchar *return_value;
394 range = GAIL_RANGE (action);
396 return_value = range->activate_description;
402 static G_CONST_RETURN gchar*
403 gail_range_get_keybinding (AtkAction *action,
407 gchar *return_value = NULL;
408 range = GAIL_RANGE (action);
414 AtkRelation *relation;
416 gpointer target_object;
419 range = GAIL_RANGE (action);
420 widget = GTK_ACCESSIBLE (range)->widget;
423 set = atk_object_ref_relation_set (ATK_OBJECT (action));
428 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
431 target = atk_relation_get_target (relation);
432 target_object = g_ptr_array_index (target, 0);
433 if (GTK_IS_ACCESSIBLE (target_object))
434 label = GTK_ACCESSIBLE (target_object)->widget;
436 g_object_unref (set);
437 if (GTK_IS_LABEL (label))
439 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
440 if (key_val != GDK_VoidSymbol)
441 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
443 g_free (range->activate_keybinding);
444 range->activate_keybinding = return_value;
449 static G_CONST_RETURN gchar*
450 gail_range_action_get_name (AtkAction *action,
453 G_CONST_RETURN gchar *return_value;
456 return_value = "activate";
464 gail_range_set_description (AtkAction *action,
471 range = GAIL_RANGE (action);
474 value = &range->activate_description;
481 *value = g_strdup (desc);