1 /* GAIL - The GNOME Accessibility Implementation Library
2 * Copyright 2004 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 "gailscale.h"
25 #include <libgail-util/gailmisc.h>
27 static void gail_scale_class_init (GailScaleClass *klass);
29 static void gail_scale_init (GailScale *scale);
31 static void gail_scale_real_initialize (AtkObject *obj,
33 static void gail_scale_notify (GObject *obj,
35 static void gail_scale_finalize (GObject *object);
38 static void atk_text_interface_init (AtkTextIface *iface);
40 static gchar* gail_scale_get_text (AtkText *text,
43 static gunichar gail_scale_get_character_at_offset
46 static gchar* gail_scale_get_text_before_offset
49 AtkTextBoundary boundary_type,
52 static gchar* gail_scale_get_text_at_offset (AtkText *text,
54 AtkTextBoundary boundary_type,
57 static gchar* gail_scale_get_text_after_offset
60 AtkTextBoundary boundary_type,
63 static gint gail_scale_get_character_count (AtkText *text);
64 static void gail_scale_get_character_extents
72 static gint gail_scale_get_offset_at_point (AtkText *text,
76 static AtkAttributeSet* gail_scale_get_run_attributes
81 static AtkAttributeSet* gail_scale_get_default_attributes
84 G_DEFINE_TYPE_WITH_CODE (GailScale, gail_scale, GAIL_TYPE_RANGE,
85 G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
88 gail_scale_class_init (GailScaleClass *klass)
90 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
91 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
93 class->initialize = gail_scale_real_initialize;
95 gobject_class->finalize = gail_scale_finalize;
96 gobject_class->notify = gail_scale_notify;
100 gail_scale_init (GailScale *scale)
105 gail_scale_real_initialize (AtkObject *obj,
108 GailScale *gail_scale;
112 ATK_OBJECT_CLASS (gail_scale_parent_class)->initialize (obj, data);
114 gail_scale = GAIL_SCALE (obj);
115 gail_scale->textutil = gail_text_util_new ();
117 layout = gtk_scale_get_layout (GTK_SCALE (data));
120 txt = pango_layout_get_text (layout);
123 gail_text_util_text_setup (gail_scale->textutil, txt);
129 gail_scale_finalize (GObject *object)
131 GailScale *scale = GAIL_SCALE (object);
133 g_object_unref (scale->textutil);
134 G_OBJECT_CLASS (gail_scale_parent_class)->finalize (object);
139 gail_scale_notify (GObject *obj,
142 GailScale *scale = GAIL_SCALE (obj);
144 if (strcmp (pspec->name, "accessible-value") == 0)
148 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
155 gtk_scale = GTK_SCALE (widget);
156 layout = gtk_scale_get_layout (gtk_scale);
159 txt = pango_layout_get_text (layout);
162 g_signal_emit_by_name (obj, "text_changed::delete", 0,
163 gtk_text_buffer_get_char_count (scale->textutil->buffer));
164 gail_text_util_text_setup (scale->textutil, txt);
165 g_signal_emit_by_name (obj, "text_changed::insert", 0,
166 g_utf8_strlen (txt, -1));
171 G_OBJECT_CLASS (gail_scale_parent_class)->notify (obj, pspec);
177 atk_text_interface_init (AtkTextIface *iface)
179 iface->get_text = gail_scale_get_text;
180 iface->get_character_at_offset = gail_scale_get_character_at_offset;
181 iface->get_text_before_offset = gail_scale_get_text_before_offset;
182 iface->get_text_at_offset = gail_scale_get_text_at_offset;
183 iface->get_text_after_offset = gail_scale_get_text_after_offset;
184 iface->get_character_count = gail_scale_get_character_count;
185 iface->get_character_extents = gail_scale_get_character_extents;
186 iface->get_offset_at_point = gail_scale_get_offset_at_point;
187 iface->get_run_attributes = gail_scale_get_run_attributes;
188 iface->get_default_attributes = gail_scale_get_default_attributes;
192 gail_scale_get_text (AtkText *text,
199 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
201 /* State is defunct */
204 scale = GAIL_SCALE (text);
205 return gail_text_util_get_substring (scale->textutil,
210 gail_scale_get_text_before_offset (AtkText *text,
212 AtkTextBoundary boundary_type,
221 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
224 /* State is defunct */
227 scale = GAIL_SCALE (text);
228 layout = gtk_scale_get_layout (GTK_SCALE (widget));
231 txt = gail_text_util_get_text (scale->textutil,
232 layout, GAIL_BEFORE_OFFSET,
233 boundary_type, offset, start_offset, end_offset);
242 gail_scale_get_text_at_offset (AtkText *text,
244 AtkTextBoundary boundary_type,
253 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
256 /* State is defunct */
259 scale = GAIL_SCALE (text);
260 layout = gtk_scale_get_layout (GTK_SCALE (widget));
263 txt = gail_text_util_get_text (scale->textutil,
264 layout, GAIL_AT_OFFSET,
265 boundary_type, offset, start_offset, end_offset);
274 gail_scale_get_text_after_offset (AtkText *text,
276 AtkTextBoundary boundary_type,
285 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
288 /* State is defunct */
291 scale = GAIL_SCALE (text);
292 layout = gtk_scale_get_layout (GTK_SCALE (widget));
295 txt = gail_text_util_get_text (scale->textutil,
296 layout, GAIL_AFTER_OFFSET,
297 boundary_type, offset, start_offset, end_offset);
306 gail_scale_get_character_count (AtkText *text)
311 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
313 /* State is defunct */
316 scale = GAIL_SCALE (text);
317 if (scale->textutil->buffer)
318 return gtk_text_buffer_get_char_count (scale->textutil->buffer);
325 gail_scale_get_character_extents (AtkText *text,
335 PangoRectangle char_rect;
337 gint index, x_layout, y_layout;
338 const gchar *scale_text;
340 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
343 /* State is defunct */
346 scale = GTK_SCALE (widget);
347 layout = gtk_scale_get_layout (scale);
350 scale_text = pango_layout_get_text (layout);
353 index = g_utf8_offset_to_pointer (scale_text, offset) - scale_text;
354 gtk_scale_get_layout_offsets (scale, &x_layout, &y_layout);
355 pango_layout_index_to_pos (layout, index, &char_rect);
356 gail_misc_get_extents_from_pango_rectangle (widget, &char_rect,
357 x_layout, y_layout, x, y, width, height, coords);
361 gail_scale_get_offset_at_point (AtkText *text,
369 gint index, x_layout, y_layout;
370 const gchar *scale_text;
372 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
374 /* State is defunct */
377 scale = GTK_SCALE (widget);
378 layout = gtk_scale_get_layout (scale);
381 scale_text = pango_layout_get_text (layout);
385 gtk_scale_get_layout_offsets (scale, &x_layout, &y_layout);
386 index = gail_misc_get_index_at_point_in_layout (widget,
388 x_layout, y_layout, x, y, coords);
391 if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
392 index = g_utf8_strlen (scale_text, -1);
395 index = g_utf8_pointer_to_offset (scale_text, scale_text + index);
400 static AtkAttributeSet*
401 gail_scale_get_run_attributes (AtkText *text,
408 AtkAttributeSet *at_set = NULL;
409 GtkTextDirection dir;
411 const gchar *scale_text;
413 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
415 /* State is defunct */
418 scale = GTK_SCALE (widget);
420 layout = gtk_scale_get_layout (scale);
423 scale_text = pango_layout_get_text (layout);
427 dir = gtk_widget_get_direction (widget);
428 if (dir == GTK_TEXT_DIR_RTL)
430 at_set = gail_misc_add_attribute (at_set,
431 ATK_TEXT_ATTR_DIRECTION,
432 g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION, dir)));
435 at_set = gail_misc_layout_get_run_attributes (at_set,
444 static AtkAttributeSet*
445 gail_scale_get_default_attributes (AtkText *text)
448 AtkAttributeSet *at_set = NULL;
451 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
453 /* State is defunct */
456 layout = gtk_scale_get_layout (GTK_SCALE (widget));
459 at_set = gail_misc_get_default_attributes (at_set,
467 gail_scale_get_character_at_offset (AtkText *text,
477 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
479 /* State is defunct */
482 scale = GTK_SCALE (widget);
484 layout = gtk_scale_get_layout (scale);
487 string = pango_layout_get_text (layout);
488 if (offset >= g_utf8_strlen (string, -1))
492 index = g_utf8_offset_to_pointer (string, offset);
494 c = g_utf8_get_char (index);