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 void gail_range_real_notify_gtk (GObject *obj,
41 static void atk_value_interface_init (AtkValueIface *iface);
42 static void gail_range_get_current_value (AtkValue *obj,
44 static void gail_range_get_maximum_value (AtkValue *obj,
46 static void gail_range_get_minimum_value (AtkValue *obj,
48 static void gail_range_get_minimum_increment (AtkValue *obj,
50 static gboolean gail_range_set_current_value (AtkValue *obj,
52 static void gail_range_value_changed (GtkAdjustment *adjustment,
55 static void atk_action_interface_init (AtkActionIface *iface);
56 static gboolean gail_range_do_action (AtkAction *action,
58 static gboolean idle_do_action (gpointer data);
59 static gint gail_range_get_n_actions (AtkAction *action);
60 static G_CONST_RETURN gchar* gail_range_get_description (AtkAction *action,
62 static G_CONST_RETURN gchar* gail_range_get_keybinding (AtkAction *action,
64 static G_CONST_RETURN gchar* gail_range_action_get_name (AtkAction *action,
66 static gboolean gail_range_set_description (AtkAction *action,
70 G_DEFINE_TYPE_WITH_CODE (GailRange, gail_range, GAIL_TYPE_WIDGET,
71 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
72 G_IMPLEMENT_INTERFACE (ATK_TYPE_VALUE, atk_value_interface_init))
75 gail_range_class_init (GailRangeClass *klass)
77 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
78 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
79 GailWidgetClass *widget_class;
81 widget_class = (GailWidgetClass*)klass;
83 widget_class->notify_gtk = gail_range_real_notify_gtk;
85 class->initialize = gail_range_real_initialize;
87 gobject_class->finalize = gail_range_finalize;
91 gail_range_init (GailRange *range)
96 gail_range_real_initialize (AtkObject *obj,
99 GailRange *range = GAIL_RANGE (obj);
103 ATK_OBJECT_CLASS (gail_range_parent_class)->initialize (obj, data);
105 gtk_range = GTK_RANGE (data);
107 * If a GtkAdjustment already exists for the GtkRange,
108 * create the GailAdjustment
110 adj = gtk_range_get_adjustment (gtk_range);
113 range->adjustment = gail_adjustment_new (adj);
114 g_signal_connect (adj,
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 atk_value_interface_init (AtkValueIface *iface)
132 iface->get_current_value = gail_range_get_current_value;
133 iface->get_maximum_value = gail_range_get_maximum_value;
134 iface->get_minimum_value = gail_range_get_minimum_value;
135 iface->get_minimum_increment = gail_range_get_minimum_increment;
136 iface->set_current_value = gail_range_set_current_value;
140 gail_range_get_current_value (AtkValue *obj,
145 g_return_if_fail (GAIL_IS_RANGE (obj));
147 range = GAIL_RANGE (obj);
148 if (range->adjustment == NULL)
150 * Adjustment has not been specified
154 atk_value_get_current_value (ATK_VALUE (range->adjustment), value);
158 gail_range_get_maximum_value (AtkValue *obj,
163 GtkAdjustment *gtk_adjustment;
166 g_return_if_fail (GAIL_IS_RANGE (obj));
168 range = GAIL_RANGE (obj);
169 if (range->adjustment == NULL)
171 * Adjustment has not been specified
175 atk_value_get_maximum_value (ATK_VALUE (range->adjustment), value);
177 gtk_range = GTK_RANGE (gtk_accessible_get_widget (GTK_ACCESSIBLE (range)));
178 g_return_if_fail (gtk_range);
180 gtk_adjustment = gtk_range_get_adjustment (gtk_range);
181 max = g_value_get_double (value);
182 max -= gtk_adjustment_get_page_size (gtk_adjustment);
184 if (gtk_range_get_restrict_to_fill_level (gtk_range))
185 max = MIN (max, gtk_range_get_fill_level (gtk_range));
187 g_value_set_double (value, max);
191 gail_range_get_minimum_value (AtkValue *obj,
196 g_return_if_fail (GAIL_IS_RANGE (obj));
198 range = GAIL_RANGE (obj);
199 if (range->adjustment == NULL)
201 * Adjustment has not been specified
205 atk_value_get_minimum_value (ATK_VALUE (range->adjustment), value);
209 gail_range_get_minimum_increment (AtkValue *obj, GValue *value)
213 g_return_if_fail (GAIL_IS_RANGE (obj));
215 range = GAIL_RANGE (obj);
216 if (range->adjustment == NULL)
218 * Adjustment has not been specified
222 atk_value_get_minimum_increment (ATK_VALUE (range->adjustment), value);
225 static gboolean gail_range_set_current_value (AtkValue *obj,
230 g_return_val_if_fail (GAIL_IS_RANGE (obj), FALSE);
232 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
236 if (G_VALUE_HOLDS_DOUBLE (value))
238 GtkRange *range = GTK_RANGE (widget);
241 new_value = g_value_get_double (value);
242 gtk_range_set_value (range, new_value);
252 gail_range_finalize (GObject *object)
254 GailRange *range = GAIL_RANGE (object);
256 if (range->adjustment)
259 * The GtkAdjustment may live on so we need to dicsonnect the
262 if (GAIL_ADJUSTMENT (range->adjustment)->adjustment)
264 g_signal_handlers_disconnect_by_func (GAIL_ADJUSTMENT (range->adjustment)->adjustment,
265 (void *)gail_range_value_changed,
268 g_object_unref (range->adjustment);
269 range->adjustment = NULL;
271 range->activate_keybinding=NULL;
272 range->activate_description=NULL;
273 if (range->action_idle_handler)
275 g_source_remove (range->action_idle_handler);
276 range->action_idle_handler = 0;
279 G_OBJECT_CLASS (gail_range_parent_class)->finalize (object);
284 gail_range_real_notify_gtk (GObject *obj,
288 GtkWidget *widget = GTK_WIDGET (obj);
289 GailRange *range = GAIL_RANGE (gtk_widget_get_accessible (widget));
291 if (strcmp (pspec->name, "adjustment") == 0)
294 * Get rid of the GailAdjustment for the GtkAdjustment
295 * which was associated with the range.
297 if (range->adjustment)
299 g_object_unref (range->adjustment);
300 range->adjustment = NULL;
303 * Create the GailAdjustment when notify for "adjustment" property
306 adj = gtk_range_get_adjustment (GTK_RANGE (widget));
307 range->adjustment = gail_adjustment_new (adj);
308 g_signal_connect (adj,
310 G_CALLBACK (gail_range_value_changed),
314 GAIL_WIDGET_CLASS (gail_range_parent_class)->notify_gtk (obj, pspec);
318 gail_range_value_changed (GtkAdjustment *adjustment,
323 g_return_if_fail (adjustment != NULL);
324 gail_return_if_fail (data != NULL);
326 range = GAIL_RANGE (data);
328 g_object_notify (G_OBJECT (range), "accessible-value");
332 atk_action_interface_init (AtkActionIface *iface)
334 iface->do_action = gail_range_do_action;
335 iface->get_n_actions = gail_range_get_n_actions;
336 iface->get_description = gail_range_get_description;
337 iface->get_keybinding = gail_range_get_keybinding;
338 iface->get_name = gail_range_action_get_name;
339 iface->set_description = gail_range_set_description;
343 gail_range_do_action (AtkAction *action,
348 gboolean return_value = TRUE;
350 range = GAIL_RANGE (action);
351 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
357 if (!gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
361 if (range->action_idle_handler)
362 return_value = FALSE;
364 range->action_idle_handler = gdk_threads_add_idle (idle_do_action, range);
367 return_value = FALSE;
372 idle_do_action (gpointer data)
377 range = GAIL_RANGE (data);
378 range->action_idle_handler = 0;
379 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (range));
380 if (widget == NULL /* State is defunct */ ||
381 !gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
384 gtk_widget_activate (widget);
390 gail_range_get_n_actions (AtkAction *action)
395 static G_CONST_RETURN gchar*
396 gail_range_get_description (AtkAction *action,
400 G_CONST_RETURN gchar *return_value;
402 range = GAIL_RANGE (action);
404 return_value = range->activate_description;
410 static G_CONST_RETURN gchar*
411 gail_range_get_keybinding (AtkAction *action,
415 gchar *return_value = NULL;
416 range = GAIL_RANGE (action);
422 AtkRelation *relation;
424 gpointer target_object;
427 range = GAIL_RANGE (action);
428 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (range));
431 set = atk_object_ref_relation_set (ATK_OBJECT (action));
436 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
439 target = atk_relation_get_target (relation);
440 target_object = g_ptr_array_index (target, 0);
441 label = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
443 g_object_unref (set);
444 if (GTK_IS_LABEL (label))
446 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
447 if (key_val != GDK_KEY_VoidSymbol)
448 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
450 g_free (range->activate_keybinding);
451 range->activate_keybinding = return_value;
456 static G_CONST_RETURN gchar*
457 gail_range_action_get_name (AtkAction *action,
460 G_CONST_RETURN gchar *return_value;
463 return_value = "activate";
471 gail_range_set_description (AtkAction *action,
478 range = GAIL_RANGE (action);
481 value = &range->activate_description;
488 *value = g_strdup (desc);