]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailtextcell.c
Use accessor functions to access GtkAccesible variables
[~andy/gtk] / modules / other / gail / gailtextcell.c
1 /* GAIL - The GNOME Accessibility Enabling Library
2  * Copyright 2001 Sun Microsystems Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21 #include <string.h>
22 #include <gtk/gtk.h>
23 #include "gailtextcell.h"
24 #include "gailcontainercell.h"
25 #include "gailcellparent.h"
26 #include <libgail-util/gailmisc.h>
27 #include "gail-private-macros.h"
28
29 static void      gail_text_cell_class_init              (GailTextCellClass *klass);
30 static void      gail_text_cell_init                    (GailTextCell   *text_cell);
31 static void      gail_text_cell_finalize                (GObject        *object);
32
33 static G_CONST_RETURN gchar* gail_text_cell_get_name    (AtkObject      *atk_obj);
34
35 static void      atk_text_interface_init                (AtkTextIface   *iface);
36
37 /* atktext.h */
38
39 static gchar*    gail_text_cell_get_text                (AtkText        *text,
40                                                         gint            start_pos,
41                                                         gint            end_pos);
42 static gunichar gail_text_cell_get_character_at_offset  (AtkText        *text,
43                                                          gint           offset);
44 static gchar*   gail_text_cell_get_text_before_offset   (AtkText        *text,
45                                                          gint           offset,
46                                                          AtkTextBoundary boundary_type,
47                                                          gint           *start_offset,
48                                                          gint           *end_offset);
49 static gchar*   gail_text_cell_get_text_at_offset       (AtkText        *text,
50                                                          gint           offset,
51                                                          AtkTextBoundary boundary_type,
52                                                          gint           *start_offset,
53                                                          gint           *end_offset);
54 static gchar*   gail_text_cell_get_text_after_offset    (AtkText        *text,
55                                                          gint           offset,
56                                                          AtkTextBoundary boundary_type,
57                                                          gint           *start_offset,
58                                                          gint           *end_offset);
59 static gint      gail_text_cell_get_character_count     (AtkText        *text);
60 static gint      gail_text_cell_get_caret_offset        (AtkText        *text);
61 static gboolean  gail_text_cell_set_caret_offset        (AtkText        *text,
62                                                          gint           offset);
63 static void      gail_text_cell_get_character_extents   (AtkText        *text,
64                                                          gint           offset,
65                                                          gint           *x,
66                                                          gint           *y,
67                                                          gint           *width,
68                                                          gint           *height,
69                                                          AtkCoordType   coords);
70 static gint      gail_text_cell_get_offset_at_point     (AtkText        *text,
71                                                          gint           x,
72                                                          gint           y,
73                                                          AtkCoordType   coords);
74 static AtkAttributeSet* gail_text_cell_get_run_attributes 
75                                                         (AtkText        *text,
76                                                          gint           offset,
77                                                          gint           *start_offset,      
78                                                          gint           *end_offset); 
79 static AtkAttributeSet* gail_text_cell_get_default_attributes 
80                                                         (AtkText        *text);
81
82 static PangoLayout*     create_pango_layout             (GtkCellRendererText *gtk_renderer,
83                                                          GtkWidget           *widget);
84 static void             add_attr                        (PangoAttrList  *attr_list,
85                                                          PangoAttribute *attr);
86
87 /* Misc */
88
89 static gboolean gail_text_cell_update_cache             (GailRendererCell *cell,
90                                                          gboolean       emit_change_signal);
91
92 gchar *gail_text_cell_property_list[] = {
93   /* Set font_desc first since it resets other values if it is NULL */
94   "font_desc",
95
96   "attributes",
97   "background_gdk",
98   "editable",
99   "family",
100   "foreground_gdk",
101   "rise",
102   "scale",
103   "size",
104   "size_points",
105   "stretch",
106   "strikethrough",
107   "style",
108   "text",
109   "underline",
110   "variant",
111   "weight",
112
113   /* Also need the sets */
114   "background_set",
115   "editable_set",
116   "family_set",
117   "foreground_set",
118   "rise_set",
119   "scale_set",
120   "size_set",
121   "stretch_set",
122   "strikethrough_set",
123   "style_set",
124   "underline_set",
125   "variant_set",
126   "weight_set",
127   NULL
128 };
129
130 G_DEFINE_TYPE_WITH_CODE (GailTextCell, gail_text_cell, GAIL_TYPE_RENDERER_CELL,
131                          G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init))
132
133 static void 
134 gail_text_cell_class_init (GailTextCellClass *klass)
135 {
136   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
137   AtkObjectClass *atk_object_class = ATK_OBJECT_CLASS (klass);
138   GailRendererCellClass *renderer_cell_class = GAIL_RENDERER_CELL_CLASS (klass);
139
140   renderer_cell_class->update_cache = gail_text_cell_update_cache;
141   renderer_cell_class->property_list = gail_text_cell_property_list;
142
143   atk_object_class->get_name = gail_text_cell_get_name;
144
145   gobject_class->finalize = gail_text_cell_finalize;
146 }
147
148 /* atktext.h */
149
150 static void
151 gail_text_cell_init (GailTextCell *text_cell)
152 {
153   text_cell->cell_text = NULL;
154   text_cell->caret_pos = 0;
155   text_cell->cell_length = 0;
156   text_cell->textutil = gail_text_util_new ();
157   atk_state_set_add_state (GAIL_CELL (text_cell)->state_set,
158                            ATK_STATE_SINGLE_LINE);
159 }
160
161 AtkObject* 
162 gail_text_cell_new (void)
163 {
164   GObject *object;
165   AtkObject *atk_object;
166   GailRendererCell *cell;
167
168   object = g_object_new (GAIL_TYPE_TEXT_CELL, NULL);
169
170   g_return_val_if_fail (object != NULL, NULL);
171
172   atk_object = ATK_OBJECT (object);
173   atk_object->role = ATK_ROLE_TABLE_CELL;
174
175   cell = GAIL_RENDERER_CELL(object);
176
177   cell->renderer = gtk_cell_renderer_text_new ();
178   g_object_ref_sink (cell->renderer);
179   return atk_object;
180 }
181
182 static void
183 gail_text_cell_finalize (GObject            *object)
184 {
185   GailTextCell *text_cell = GAIL_TEXT_CELL (object);
186
187   g_object_unref (text_cell->textutil);
188   g_free (text_cell->cell_text);
189
190   G_OBJECT_CLASS (gail_text_cell_parent_class)->finalize (object);
191 }
192
193 static G_CONST_RETURN gchar*
194 gail_text_cell_get_name (AtkObject *atk_obj)
195 {
196   if (atk_obj->name)
197     return atk_obj->name;
198   else
199     {
200       GailTextCell *text_cell = GAIL_TEXT_CELL (atk_obj);
201
202       return text_cell->cell_text;
203     }
204 }
205
206 static gboolean
207 gail_text_cell_update_cache (GailRendererCell *cell,
208                              gboolean         emit_change_signal)
209 {
210   GailTextCell *text_cell = GAIL_TEXT_CELL (cell);
211   AtkObject *obj = ATK_OBJECT (cell);
212   gboolean rv = FALSE;
213   gint temp_length;
214   gchar *new_cache;
215
216   g_object_get (G_OBJECT (cell->renderer), "text", &new_cache, NULL);
217
218   if (text_cell->cell_text)
219     {
220      /*
221       * If the new value is NULL and the old value isn't NULL, then the
222       * value has changed.
223       */
224       if (new_cache == NULL ||
225           strcmp (text_cell->cell_text, new_cache))
226         {
227           g_free (text_cell->cell_text);
228           temp_length = text_cell->cell_length;
229           text_cell->cell_text = NULL;
230           text_cell->cell_length = 0;
231           if (emit_change_signal)
232             {
233               g_signal_emit_by_name (cell, "text_changed::delete", 0, temp_length);
234               if (obj->name == NULL)
235                 g_object_notify (G_OBJECT (obj), "accessible-name");
236             }
237           if (new_cache)
238             rv = TRUE;
239         }
240     }
241   else
242     rv = TRUE;
243
244   if (rv)
245     {
246       if (new_cache == NULL)
247         {
248           text_cell->cell_text = g_strdup ("");
249           text_cell->cell_length = 0;
250         }
251       else
252         {
253           text_cell->cell_text = g_strdup (new_cache);
254           text_cell->cell_length = g_utf8_strlen (new_cache, -1);
255         }
256     }
257
258   g_free (new_cache);
259   gail_text_util_text_setup (text_cell->textutil, text_cell->cell_text);
260   
261   if (rv)
262     {
263       if (emit_change_signal)
264         {
265           g_signal_emit_by_name (cell, "text_changed::insert",
266                                  0, text_cell->cell_length);
267
268           if (obj->name == NULL)
269             g_object_notify (G_OBJECT (obj), "accessible-name");
270         }
271     }
272   return rv;
273 }
274
275 static void
276 atk_text_interface_init (AtkTextIface *iface)
277 {
278   iface->get_text = gail_text_cell_get_text;
279   iface->get_character_at_offset = gail_text_cell_get_character_at_offset;
280   iface->get_text_before_offset = gail_text_cell_get_text_before_offset;
281   iface->get_text_at_offset = gail_text_cell_get_text_at_offset;
282   iface->get_text_after_offset = gail_text_cell_get_text_after_offset;
283   iface->get_character_count = gail_text_cell_get_character_count;
284   iface->get_caret_offset = gail_text_cell_get_caret_offset;
285   iface->set_caret_offset = gail_text_cell_set_caret_offset;
286   iface->get_run_attributes = gail_text_cell_get_run_attributes;
287   iface->get_default_attributes = gail_text_cell_get_default_attributes;
288   iface->get_character_extents = gail_text_cell_get_character_extents;
289   iface->get_offset_at_point = gail_text_cell_get_offset_at_point;
290 }
291
292 static gchar* 
293 gail_text_cell_get_text (AtkText *text, 
294                          gint    start_pos,
295                          gint    end_pos)
296 {
297   if (GAIL_TEXT_CELL (text)->cell_text)
298     return gail_text_util_get_substring (GAIL_TEXT_CELL (text)->textutil,
299               start_pos, end_pos);
300   else
301     return g_strdup ("");
302 }
303
304 static gchar* 
305 gail_text_cell_get_text_before_offset (AtkText         *text,
306                                        gint            offset,
307                                        AtkTextBoundary boundary_type,
308                                        gint            *start_offset,
309                                        gint            *end_offset)
310 {
311   return gail_text_util_get_text (GAIL_TEXT_CELL (text)->textutil,
312         NULL, GAIL_BEFORE_OFFSET, boundary_type, offset, start_offset,
313         end_offset);
314 }
315
316 static gchar* 
317 gail_text_cell_get_text_at_offset (AtkText         *text,
318                                    gint            offset,
319                                    AtkTextBoundary boundary_type,
320                                    gint            *start_offset,
321                                    gint            *end_offset)
322 {
323   return gail_text_util_get_text (GAIL_TEXT_CELL (text)->textutil,
324         NULL, GAIL_AT_OFFSET, boundary_type, offset, start_offset, end_offset);
325 }
326
327 static gchar* 
328 gail_text_cell_get_text_after_offset (AtkText         *text,
329                                       gint            offset,
330                                       AtkTextBoundary boundary_type,
331                                       gint            *start_offset,
332                                       gint            *end_offset)
333 {
334   return gail_text_util_get_text (GAIL_TEXT_CELL (text)->textutil,
335         NULL, GAIL_AFTER_OFFSET, boundary_type, offset, start_offset,
336         end_offset);
337 }
338
339 static gint 
340 gail_text_cell_get_character_count (AtkText *text)
341 {
342   if (GAIL_TEXT_CELL (text)->cell_text != NULL)
343     return GAIL_TEXT_CELL (text)->cell_length;
344   else
345     return 0;
346 }
347
348 static gint 
349 gail_text_cell_get_caret_offset (AtkText *text)
350 {
351   return GAIL_TEXT_CELL (text)->caret_pos;
352 }
353
354 static gboolean 
355 gail_text_cell_set_caret_offset (AtkText *text,
356                                  gint    offset)
357 {
358   GailTextCell *text_cell = GAIL_TEXT_CELL (text);
359
360   if (text_cell->cell_text == NULL)
361     return FALSE;
362   else
363     {
364
365       /* Only set the caret within the bounds and if it is to a new position. */
366       if (offset >= 0 && 
367           offset <= text_cell->cell_length &&
368           offset != text_cell->caret_pos)
369         {
370           text_cell->caret_pos = offset;
371
372           /* emit the signal */
373           g_signal_emit_by_name (text, "text_caret_moved", offset);
374           return TRUE;
375         }
376       else
377         return FALSE;
378     }
379 }
380
381 static AtkAttributeSet*
382 gail_text_cell_get_run_attributes (AtkText *text,
383                                   gint     offset,
384                                   gint     *start_offset,
385                                   gint     *end_offset) 
386 {
387   GailRendererCell *gail_renderer; 
388   GtkCellRendererText *gtk_renderer;
389   AtkAttributeSet *attrib_set = NULL;
390   PangoLayout *layout;
391   AtkObject *parent;
392   GtkWidget *widget;
393
394   gail_renderer = GAIL_RENDERER_CELL (text);
395   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
396
397   parent = atk_object_get_parent (ATK_OBJECT (text));
398   if (GAIL_IS_CONTAINER_CELL (parent))
399     parent = atk_object_get_parent (parent);
400   g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), NULL);
401   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
402   layout = create_pango_layout (gtk_renderer, widget),
403   attrib_set = gail_misc_layout_get_run_attributes (attrib_set, 
404                                                     layout,
405                                                     gtk_renderer->text,
406                                                     offset,
407                                                     start_offset,
408                                                     end_offset);
409   g_object_unref (G_OBJECT (layout));
410   
411   return attrib_set;
412 }
413
414 static AtkAttributeSet*
415 gail_text_cell_get_default_attributes (AtkText  *text)
416 {
417   GailRendererCell *gail_renderer; 
418   GtkCellRendererText *gtk_renderer;
419   AtkAttributeSet *attrib_set = NULL;
420   PangoLayout *layout;
421   AtkObject *parent;
422   GtkWidget *widget;
423
424   gail_renderer = GAIL_RENDERER_CELL (text);
425   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
426
427   parent = atk_object_get_parent (ATK_OBJECT (text));
428   if (GAIL_IS_CONTAINER_CELL (parent))
429     parent = atk_object_get_parent (parent);
430   g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), NULL);
431   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
432   layout = create_pango_layout (gtk_renderer, widget),
433
434   attrib_set = gail_misc_get_default_attributes (attrib_set, 
435                                                  layout,
436                                                  widget);
437   g_object_unref (G_OBJECT (layout));
438   return attrib_set;
439 }
440
441 /* 
442  * This function is used by gail_text_cell_get_offset_at_point()
443  * and gail_text_cell_get_character_extents(). There is no 
444  * cached PangoLayout for gailtextcell so we must create a temporary
445  * one using this function.
446  */ 
447 static PangoLayout*
448 create_pango_layout(GtkCellRendererText *gtk_renderer,
449                     GtkWidget           *widget)
450 {
451   PangoAttrList *attr_list;
452   PangoLayout *layout;
453   PangoUnderline uline;
454   PangoFontMask mask;
455
456   layout = gtk_widget_create_pango_layout (widget, gtk_renderer->text);
457
458   if (gtk_renderer->extra_attrs)
459     attr_list = pango_attr_list_copy (gtk_renderer->extra_attrs);
460   else
461     attr_list = pango_attr_list_new ();
462
463   if (gtk_renderer->foreground_set)
464     {
465       PangoColor color;
466       color = gtk_renderer->foreground;
467       add_attr (attr_list, pango_attr_foreground_new (color.red,
468                                                       color.green, color.blue));
469     }
470
471   if (gtk_renderer->strikethrough_set)
472     add_attr (attr_list,
473               pango_attr_strikethrough_new (gtk_renderer->strikethrough));
474
475   mask = pango_font_description_get_set_fields (gtk_renderer->font);
476
477   if (mask & PANGO_FONT_MASK_FAMILY)
478     add_attr (attr_list,
479       pango_attr_family_new (pango_font_description_get_family (gtk_renderer->font)));
480
481   if (mask & PANGO_FONT_MASK_STYLE)
482     add_attr (attr_list, pango_attr_style_new (pango_font_description_get_style (gtk_renderer->font)));
483
484   if (mask & PANGO_FONT_MASK_VARIANT)
485     add_attr (attr_list, pango_attr_variant_new (pango_font_description_get_variant (gtk_renderer->font)));
486
487   if (mask & PANGO_FONT_MASK_WEIGHT)
488     add_attr (attr_list, pango_attr_weight_new (pango_font_description_get_weight (gtk_renderer->font)));
489
490   if (mask & PANGO_FONT_MASK_STRETCH)
491     add_attr (attr_list, pango_attr_stretch_new (pango_font_description_get_stretch (gtk_renderer->font)));
492
493   if (mask & PANGO_FONT_MASK_SIZE)
494     add_attr (attr_list, pango_attr_size_new (pango_font_description_get_size (gtk_renderer->font)));
495
496   if (gtk_renderer->scale_set &&
497       gtk_renderer->font_scale != 1.0)
498     add_attr (attr_list, pango_attr_scale_new (gtk_renderer->font_scale));
499
500   if (gtk_renderer->underline_set)
501     uline = gtk_renderer->underline_style;
502   else
503     uline = PANGO_UNDERLINE_NONE;
504
505   if (uline != PANGO_UNDERLINE_NONE)
506     add_attr (attr_list,
507       pango_attr_underline_new (gtk_renderer->underline_style));
508
509   if (gtk_renderer->rise_set)
510     add_attr (attr_list, pango_attr_rise_new (gtk_renderer->rise));
511
512   pango_layout_set_attributes (layout, attr_list);
513   pango_layout_set_width (layout, -1);
514   pango_attr_list_unref (attr_list);
515
516   return layout;
517 }
518
519 static void 
520 add_attr (PangoAttrList  *attr_list,
521          PangoAttribute *attr)
522 {
523   attr->start_index = 0;
524   attr->end_index = G_MAXINT;
525   pango_attr_list_insert (attr_list, attr);
526 }
527
528 static void      
529 gail_text_cell_get_character_extents (AtkText          *text,
530                                       gint             offset,
531                                       gint             *x,
532                                       gint             *y,
533                                       gint             *width,
534                                       gint             *height,
535                                       AtkCoordType     coords)
536 {
537   GailRendererCell *gail_renderer; 
538   GtkCellRendererText *gtk_renderer;
539   GdkRectangle rendered_rect;
540   GtkWidget *widget;
541   AtkObject *parent;
542   PangoRectangle char_rect;
543   PangoLayout *layout;
544   gint x_offset, y_offset, index, cell_height, cell_width;
545
546   if (!GAIL_TEXT_CELL (text)->cell_text)
547     {
548       *x = *y = *height = *width = 0;
549       return;
550     }
551   if (offset < 0 || offset >= GAIL_TEXT_CELL (text)->cell_length)
552     {
553       *x = *y = *height = *width = 0;
554       return;
555     }
556   gail_renderer = GAIL_RENDERER_CELL (text);
557   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
558   /*
559    * Thus would be inconsistent with the cache
560    */
561   gail_return_if_fail (gtk_renderer->text);
562
563   parent = atk_object_get_parent (ATK_OBJECT (text));
564   if (GAIL_IS_CONTAINER_CELL (parent))
565     parent = atk_object_get_parent (parent);
566   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
567   g_return_if_fail (GAIL_IS_CELL_PARENT (parent));
568   gail_cell_parent_get_cell_area (GAIL_CELL_PARENT (parent), GAIL_CELL (text),
569                                   &rendered_rect);
570
571   gtk_cell_renderer_get_size (GTK_CELL_RENDERER (gtk_renderer), widget,
572     &rendered_rect, &x_offset, &y_offset, &cell_width, &cell_height);
573   layout = create_pango_layout (gtk_renderer, widget);
574
575   index = g_utf8_offset_to_pointer (gtk_renderer->text,
576     offset) - gtk_renderer->text;
577   pango_layout_index_to_pos (layout, index, &char_rect); 
578
579   gail_misc_get_extents_from_pango_rectangle (widget,
580       &char_rect,
581       x_offset + rendered_rect.x + gail_renderer->renderer->xpad,
582       y_offset + rendered_rect.y + gail_renderer->renderer->ypad,
583       x, y, width, height, coords);
584   g_object_unref (layout);
585   return;
586
587
588 static gint      
589 gail_text_cell_get_offset_at_point (AtkText          *text,
590                                     gint             x,
591                                     gint             y,
592                                     AtkCoordType     coords)
593 {
594   AtkObject *parent;
595   GailRendererCell *gail_renderer; 
596   GtkCellRendererText *gtk_renderer;
597   GtkWidget *widget;
598   GdkRectangle rendered_rect;
599   PangoLayout *layout;
600   gint x_offset, y_offset, index;
601  
602   if (!GAIL_TEXT_CELL (text)->cell_text)
603     return -1;
604
605   gail_renderer = GAIL_RENDERER_CELL (text);
606   gtk_renderer = GTK_CELL_RENDERER_TEXT (gail_renderer->renderer);
607   parent = atk_object_get_parent (ATK_OBJECT (text));
608
609   g_return_val_if_fail (gtk_renderer->text, -1);
610   if (GAIL_IS_CONTAINER_CELL (parent))
611     parent = atk_object_get_parent (parent);
612
613   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
614
615   g_return_val_if_fail (GAIL_IS_CELL_PARENT (parent), -1);
616   gail_cell_parent_get_cell_area (GAIL_CELL_PARENT (parent), GAIL_CELL (text),
617                                   &rendered_rect);
618   gtk_cell_renderer_get_size (GTK_CELL_RENDERER (gtk_renderer), widget,
619      &rendered_rect, &x_offset, &y_offset, NULL, NULL);
620
621   layout = create_pango_layout (gtk_renderer, widget);
622    
623   index = gail_misc_get_index_at_point_in_layout (widget, layout,
624         x_offset + rendered_rect.x + gail_renderer->renderer->xpad,
625         y_offset + rendered_rect.y + gail_renderer->renderer->ypad,
626         x, y, coords);
627   g_object_unref (layout);
628   if (index == -1)
629     {
630       if (coords == ATK_XY_WINDOW || coords == ATK_XY_SCREEN)
631         return g_utf8_strlen (gtk_renderer->text, -1);
632     
633       return index;  
634     }
635   else
636     return g_utf8_pointer_to_offset (gtk_renderer->text,
637        gtk_renderer->text + index);  
638 }
639
640 static gunichar 
641 gail_text_cell_get_character_at_offset (AtkText       *text,
642                                         gint          offset)
643 {
644   gchar *index;
645   gchar *string;
646
647   string = GAIL_TEXT_CELL(text)->cell_text;
648
649   if (!string)
650     return '\0';
651
652   if (offset >= g_utf8_strlen (string, -1))
653     return '\0';
654
655   index = g_utf8_offset_to_pointer (string, offset);
656
657   return g_utf8_get_char (index);
658 }
659