summaryrefslogtreecommitdiff
path: root/modules/other/gail/gailscale.c
blob: 17372141ca3292f5dfc9eae2f3597e34cff10bfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* GAIL - The GNOME Accessibility Implementation Library
 * Copyright 2004 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include <string.h>
#include <gtk/gtk.h>
#include "gailscale.h"
#include <libgail-util/gailmisc.h>

static void	    gail_scale_class_init        (GailScaleClass *klass);

static void         gail_scale_init              (GailScale      *scale);

static void         gail_scale_real_initialize   (AtkObject      *obj,
                                                  gpointer      data);
static void         gail_scale_notify            (GObject       *obj,
                                                  GParamSpec    *pspec);
static void         gail_scale_finalize          (GObject        *object);

/* atktext.h */ 
static void	    atk_text_interface_init        (AtkTextIface      *iface);

static gchar*	    gail_scale_get_text	           (AtkText	      *text,
                                                    gint	      start_pos,
						    gint	      end_pos);
static gunichar	    gail_scale_get_character_at_offset
                                                   (AtkText	      *text,
						    gint	      offset);
static gchar*       gail_scale_get_text_before_offset
                                                   (AtkText	      *text,
 						    gint	      offset,
						    AtkTextBoundary   boundary_type,
						    gint	      *start_offset,
						    gint	      *end_offset);
static gchar*       gail_scale_get_text_at_offset  (AtkText	      *text,
 						    gint	      offset,
						    AtkTextBoundary   boundary_type,
						    gint	      *start_offset,
						    gint	      *end_offset);
static gchar*      gail_scale_get_text_after_offset
                                                   (AtkText	      *text,
 						    gint	      offset,
						    AtkTextBoundary   boundary_type,
						    gint	      *start_offset,
						    gint	      *end_offset);
static gint	   gail_scale_get_character_count  (AtkText	      *text);
static void        gail_scale_get_character_extents
                                                   (AtkText	      *text,
						    gint 	      offset,
		                                    gint 	      *x,
                    		   	            gint 	      *y,
                                		    gint 	      *width,
                                     		    gint 	      *height,
			        		    AtkCoordType      coords);
static gint        gail_scale_get_offset_at_point  (AtkText           *text,
                                                    gint              x,
                                                    gint              y,
			                            AtkCoordType      coords);
static AtkAttributeSet* gail_scale_get_run_attributes 
                                                   (AtkText           *text,
              					    gint 	      offset,
                                                    gint 	      *start_offset,
					            gint	      *end_offset);
static AtkAttributeSet* gail_scale_get_default_attributes
                                                   (AtkText           *text);

G_DEFINE_TYPE_WITH_CODE (GailScale, gail_scale, GAIL_TYPE_RANGE,
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))

static void	 
gail_scale_class_init (GailScaleClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  AtkObjectClass *class = ATK_OBJECT_CLASS (klass);

  class->initialize = gail_scale_real_initialize;

  gobject_class->finalize = gail_scale_finalize;
  gobject_class->notify = gail_scale_notify;
}

static void
gail_scale_init (GailScale      *scale)
{
}

static void
gail_scale_real_initialize (AtkObject *obj,
                            gpointer  data)
{
  GailScale *gail_scale;
  const gchar *txt;
  PangoLayout *layout;

  ATK_OBJECT_CLASS (gail_scale_parent_class)->initialize (obj, data);

  gail_scale = GAIL_SCALE (obj);
  gail_scale->textutil = gail_text_util_new ();

  layout = gtk_scale_get_layout (GTK_SCALE (data));
  if (layout)
    {
      txt = pango_layout_get_text (layout);
      if (txt)
        {
          gail_text_util_text_setup (gail_scale->textutil, txt);
        }
    }
}

static void
gail_scale_finalize (GObject *object)
{
  GailScale *scale = GAIL_SCALE (object);

  g_object_unref (scale->textutil);
  G_OBJECT_CLASS (gail_scale_parent_class)->finalize (object);

}

static void
gail_scale_notify (GObject    *obj,
                   GParamSpec *pspec)
{
  GailScale *scale = GAIL_SCALE (obj);

  if (strcmp (pspec->name, "accessible-value") == 0)
    {
      GtkWidget *widget;

      widget = GTK_ACCESSIBLE (obj)->widget;
      if (widget)
        {
          GtkScale *gtk_scale;
          PangoLayout *layout;
          const gchar *txt;

          gtk_scale = GTK_SCALE (widget);
          layout = gtk_scale_get_layout (gtk_scale);
          if (layout)
            {
              txt = pango_layout_get_text (layout);
              if (txt)
                {
	          g_signal_emit_by_name (obj, "text_changed::delete", 0,
                                         gtk_text_buffer_get_char_count (scale->textutil->buffer));
                  gail_text_util_text_setup (scale->textutil, txt);
	          g_signal_emit_by_name (obj, "text_changed::insert", 0,
                                         g_utf8_strlen (txt, -1));
                }
            }
        }
    }
  G_OBJECT_CLASS (gail_scale_parent_class)->notify (obj, pspec);
}

/* atktext.h */

static void
atk_text_interface_init (AtkTextIface *iface)
{
  iface->get_text = gail_scale_get_text;
  iface->get_character_at_offset = gail_scale_get_character_at_offset;
  iface->get_text_before_offset = gail_scale_get_text_before_offset;
  iface->get_text_at_offset = gail_scale_get_text_at_offset;
  iface->get_text_after_offset = gail_scale_get_text_after_offset;
  iface->get_character_count = gail_scale_get_character_count;
  iface->get_character_extents = gail_scale_get_character_extents;
  iface->get_offset_at_point = gail_scale_get_offset_at_point;
  iface->get_run_attributes = gail_scale_get_run_attributes;
  iface->get_default_attributes = gail_scale_get_default_attributes;
}

static gchar*
gail_scale_get_text (AtkText *text,
                     gint    start_pos,
                     gint    end_pos)
{
  GtkWidget *widget;
  GailScale *scale;

  widget = GTK_ACCESSIBLE (text)->widget;
  if (widget == NULL)
    /* State is defunct */
    return NULL;

  scale = GAIL_SCALE (text);
  return gail_text_util_get_substring (scale->textutil, 
                                       start_pos, end_pos);
}

static gchar*
gail_scale_get_text_before_offset (AtkText         *text,
     				   gint            offset,
				   AtkTextBoundary boundary_type,
				   gint            *start_offset,
				   gint            *end_offset)
{
  GtkWidget *widget;
  GailScale *scale;
  PangoLayout *layout;
  gchar *txt;
  
  widget = GTK_ACCESSIBLE (text)->widget;
  
  if (widget == NULL)
    /* State is defunct */
    return NULL;
  
  scale = GAIL_SCALE (text);
  layout = gtk_scale_get_layout (GTK_SCALE (widget));
  if (layout)
    {
      txt =  gail_text_util_get_text (scale->textutil,
                           layout, GAIL_BEFORE_OFFSET, 
                           boundary_type, offset, start_offset, end_offset); 
    }
  else
    txt = NULL;

  return txt;
}

static gchar*
gail_scale_get_text_at_offset (AtkText         *text,
	   		       gint            offset,
			       AtkTextBoundary boundary_type,
 			       gint            *start_offset,
			       gint            *end_offset)
{
  GtkWidget *widget;
  GailScale *scale;
  PangoLayout *layout;
  gchar *txt;
 
  widget = GTK_ACCESSIBLE (text)->widget;
  
  if (widget == NULL)
    /* State is defunct */
    return NULL;
  
  scale = GAIL_SCALE (text);
  layout = gtk_scale_get_layout (GTK_SCALE (widget));
  if (layout)
    {
      txt =  gail_text_util_get_text (scale->textutil,
                              layout, GAIL_AT_OFFSET, 
                              boundary_type, offset, start_offset, end_offset);
    }
  else
    txt = NULL;

  return txt;
}

static gchar*
gail_scale_get_text_after_offset (AtkText         *text,
				  gint            offset,
				  AtkTextBoundary boundary_type,
				  gint            *start_offset,
				  gint            *end_offset)
{
  GtkWidget *widget;
  GailScale *scale;
  PangoLayout *layout;
  gchar *txt;

  widget = GTK_ACCESSIBLE (text)->widget;
  
  if (widget == NULL)
    /* State is defunct */
    return NULL;
  
  scale = GAIL_SCALE (text);
  layout = gtk_scale_get_layout (GTK_SCALE (widget));
  if (layout)
    {
      txt =  gail_text_util_get_text (scale->textutil,
                              layout, GAIL_AFTER_OFFSET, 
                              boundary_type, offset, start_offset, end_offset);
    }
  else
    txt = NULL;

  return txt;
}

static gint
gail_scale_get_character_count (AtkText *text)
{
  GtkWidget *widget;
  GailScale *scale;

  widget = GTK_ACCESSIBLE (text)->widget;
  if (widget == NULL)
    /* State is defunct */
    return 0;

  scale = GAIL_SCALE (text);
  if (scale->textutil->buffer)
    return gtk_text_buffer_get_char_count (scale->textutil->buffer);
  else
    return 0;

}

static void
gail_scale_get_character_extents (AtkText      *text,
				  gint         offset,
		                  gint         *x,
                    		  gint 	       *y,
                                  gint 	       *width,
                                  gint 	       *height,
			          AtkCoordType coords)
{
  GtkWidget *widget;
  GtkScale *scale;
  PangoRectangle char_rect;
  PangoLayout *layout;
  gint index, x_layout, y_layout;
  const gchar *scale_text;
 
  widget = GTK_ACCESSIBLE (text)->widget;

  if (widget == NULL)
    /* State is defunct */
    return;

  scale = GTK_SCALE (widget);
  layout = gtk_scale_get_layout (scale);
  if (!layout)
    return;
  scale_text = pango_layout_get_text (layout);
  if (!scale_text)
    return;
  index = g_utf8_offset_to_pointer (scale_text, offset) - scale_text;
  gtk_scale_get_layout_offsets (scale, &x_layout, &y_layout);
  pango_layout_index_to_pos (layout, index, &char_rect);
  gail_misc_get_extents_from_pango_rectangle (widget, &char_rect, 
                    x_layout, y_layout, x, y, width, height, coords);
} 

static gint 
gail_scale_get_offset_at_point (AtkText      *text,
                                gint         x,
                                gint         y,
	          		AtkCoordType coords)
{ 
  GtkWidget *widget;
  GtkScale *scale;
  PangoLayout *layout;
  gint index, x_layout, y_layout;
  const gchar *scale_text;

  widget = GTK_ACCESSIBLE (text)->widget;
  if (widget == NULL)
    /* State is defunct */
    return -1;

  scale = GTK_SCALE (widget);
  layout = gtk_scale_get_layout (scale);
  if (!layout)
    return -1;
  scale_text = pango_layout_get_text (layout);
  if (!scale_text)
    return -1;
  
  gtk_scale_get_layout_offsets (scale, &x_layout, &y_layout);
  index = gail_misc_get_index_at_point_in_layout (widget, 
                                              layout, 
                                              x_layout, y_layout, x, y, coords);
  if (index == -1)
    {
      if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
        index = g_utf8_strlen (scale_text, -1);
    }
  else
    index = g_utf8_pointer_to_offset (scale_text, scale_text + index);  

  return index;
}

static AtkAttributeSet*
gail_scale_get_run_attributes (AtkText *text,
                               gint    offset,
                               gint    *start_offset,
	                       gint    *end_offset)
{
  GtkWidget *widget;
  GtkScale *scale;
  AtkAttributeSet *at_set = NULL;
  GtkTextDirection dir;
  PangoLayout *layout;
  const gchar *scale_text;

  widget = GTK_ACCESSIBLE (text)->widget;
  if (widget == NULL)
    /* State is defunct */
    return NULL;

  scale = GTK_SCALE (widget);

  layout = gtk_scale_get_layout (scale);
  if (!layout)
    return at_set;
  scale_text = pango_layout_get_text (layout);
  if (!scale_text)
    return at_set;

  dir = gtk_widget_get_direction (widget);
  if (dir == GTK_TEXT_DIR_RTL)
    {
      at_set = gail_misc_add_attribute (at_set, 
                                        ATK_TEXT_ATTR_DIRECTION,
     g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION, dir)));
    }

  at_set = gail_misc_layout_get_run_attributes (at_set,
                                                layout,
                                                (gchar *)scale_text,
                                                offset,
                                                start_offset,
                                                end_offset);
  return at_set;
}

static AtkAttributeSet*
gail_scale_get_default_attributes (AtkText *text)
{
  GtkWidget *widget;
  AtkAttributeSet *at_set = NULL;
  PangoLayout *layout;

  widget = GTK_ACCESSIBLE (text)->widget;
  if (widget == NULL)
    /* State is defunct */
    return NULL;

  layout = gtk_scale_get_layout (GTK_SCALE (widget));
  if (layout)
    {
      at_set = gail_misc_get_default_attributes (at_set,
                                                 layout,
                                                 widget);
    }
  return at_set;
}

static gunichar 
gail_scale_get_character_at_offset (AtkText *text,
                                    gint    offset)
{
  GtkWidget *widget;
  GtkScale *scale;
  PangoLayout *layout;
  const gchar *string;
  gchar *index;
  gunichar c;

  widget = GTK_ACCESSIBLE (text)->widget;
  if (widget == NULL)
    /* State is defunct */
    return '\0';

  scale = GTK_SCALE (widget);

  layout = gtk_scale_get_layout (scale);
  if (!layout)
    return '\0';
  string = pango_layout_get_text (layout);
  if (offset >= g_utf8_strlen (string, -1))
    c = '\0';
  else
    {
      index = g_utf8_offset_to_pointer (string, offset);

      c = g_utf8_get_char (index);
    }

  return c;
}