2008-01-14 Li Yuan <li.yuan@sun.com>
[platform/core/uifw/at-spi2-atk.git] / libspi / text.c
index 8aa5764..103e719 100644 (file)
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <atk/atktext.h>
 #include <libspi/text.h>
+#include <libspi/spi-private.h>
 
 /* Our parent Gtk object type */
 #define PARENT_TYPE SPI_TYPE_BASE
 
+typedef struct {
+  gint x;
+  gint y;
+  gint w;
+  gint h;
+} SpiTextRect;
+
 static AtkText *
 get_text_from_servant (PortableServer_Servant servant)
 {
@@ -67,7 +76,7 @@ impl_getText (PortableServer_Servant servant,
 }
 
 
-CORBA_string
+static CORBA_string
 impl_getTextAfterOffset (PortableServer_Servant servant,
                         const CORBA_long offset,
                         const
@@ -249,6 +258,61 @@ impl_getAttributes (PortableServer_Servant servant,
   return rv;  
 }
 
+static CORBA_string
+impl_getAttributeValue (PortableServer_Servant servant,
+                       const CORBA_long offset,
+                       const CORBA_char *attributename,
+                       CORBA_long * startOffset,
+                       CORBA_long * endOffset,
+                       CORBA_boolean * defined,
+                       CORBA_Environment *ev)
+{
+  AtkAttributeSet *set;
+  gint intstart_offset, intend_offset;
+  GSList *cur_attr;
+  CORBA_string rv = NULL;
+  AtkText *text = get_text_from_servant (servant);
+  AtkAttribute *at;
+
+  g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
+
+  set = atk_text_get_run_attributes (text, offset,
+                                    &intstart_offset, &intend_offset);
+  *startOffset = intstart_offset;
+  *endOffset = intend_offset;
+  *defined = FALSE;
+  cur_attr = (GSList *) set;
+  while (cur_attr)
+    {
+      at = (AtkAttribute *) cur_attr->data;
+      if (!strcmp (at->name, attributename))
+      {
+         rv = CORBA_string_dup (at->value);
+         *defined = TRUE;
+         break;
+      }
+      cur_attr = cur_attr->next;
+    }
+  atk_attribute_set_free (set);
+  return (rv ? rv : CORBA_string_dup (""));  
+}
+
+static CORBA_string
+impl_getDefaultAttributes (PortableServer_Servant servant,
+                          CORBA_Environment *ev)
+{
+  AtkAttributeSet *set;
+  CORBA_char *rv;
+  AtkText *text = get_text_from_servant (servant);
+
+  g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
+
+  set = atk_text_get_default_attributes (text);
+
+  rv = _string_from_attribute_set (set);
+  atk_attribute_set_free (set);
+  return rv;  
+}
 
 static void 
 impl_getCharacterExtents (PortableServer_Servant servant,
@@ -391,6 +455,175 @@ impl_setCaretOffset (PortableServer_Servant servant,
 }
 
 static void
+impl_getRangeExtents(PortableServer_Servant servant,
+                    const CORBA_long startOffset,
+                    const CORBA_long endOffset,
+                    CORBA_long * x, CORBA_long * y,
+                    CORBA_long * width,
+                    CORBA_long * height,
+                    const CORBA_short coordType,
+                    CORBA_Environment * ev)
+{
+  AtkText *text = get_text_from_servant (servant);
+  AtkTextRectangle rect;
+
+  g_return_if_fail (text != NULL);
+  
+  atk_text_get_range_extents (text, (gint) startOffset, (gint) endOffset, 
+                             (AtkCoordType) coordType, &rect);
+  *x = rect.x;
+  *y = rect.y;
+  *width = rect.width;
+  *height = rect.height;
+}
+
+#define MAXRANGELEN 512
+
+static Accessibility_Text_RangeList *
+_spi_text_range_seq_from_atkrangelist (AtkTextRange **range_list) 
+{ 
+  Accessibility_Text_RangeList *rangeList = 
+    Accessibility_Text_RangeList__alloc ();
+  int i, len;
+
+  for (len = 0; len < MAXRANGELEN && range_list[len]; ++len);
+
+  rangeList->_length = len;
+  rangeList->_buffer = Accessibility_Text_RangeList_allocbuf (len);
+  for (i = 0; i < len; ++i) 
+    {
+      rangeList->_buffer[i].startOffset = range_list[i]->start_offset; 
+      rangeList->_buffer[i].endOffset = range_list[i]->end_offset;
+      rangeList->_buffer[i].content = CORBA_string_dup (range_list[i]->content);
+    }
+
+  return rangeList;
+}
+
+static Accessibility_Text_RangeList *
+impl_getBoundedRanges(PortableServer_Servant servant,
+                     const CORBA_long x,
+                     const CORBA_long y,
+                     const CORBA_long width,
+                     const CORBA_long height,
+                     const CORBA_short coordType,
+                     const Accessibility_TEXT_CLIP_TYPE xClipType,
+                     const Accessibility_TEXT_CLIP_TYPE yClipType, 
+                     CORBA_Environment * ev)
+{
+  AtkText *text = get_text_from_servant (servant);
+  AtkTextRange **range_list = NULL;
+  AtkTextRectangle rect;
+
+  rect.x = x;
+  rect.y = y;
+  rect.width = width;
+  rect.height = height;
+
+  range_list = atk_text_get_bounded_ranges (text, &rect, 
+                                           (AtkCoordType) coordType,
+                                           (AtkTextClipType) xClipType,
+                                           (AtkTextClipType) yClipType);
+
+  return _spi_text_range_seq_from_atkrangelist (range_list); 
+}
+
+
+
+static Accessibility_AttributeSet *    
+impl_getAttributeRun (PortableServer_Servant servant,             
+                     const CORBA_long offset, 
+                     CORBA_long *startOffset, CORBA_long *endOffset, 
+                     const CORBA_boolean includeDefaults, 
+                     CORBA_Environment *ev){
+                     
+     AtkAttributeSet *attributes, *default_attributes = NULL;
+     AtkAttribute *attr = NULL;
+     gint intstart_offset, intend_offset;
+     Accessibility_AttributeSet *retval = NULL;
+     AtkText *text = get_text_from_servant (servant);
+     gint n_attributes = 0, total_attributes = 0, n_default_attributes = 0;
+     gint i, j;
+     
+     g_return_val_if_fail (text != NULL, NULL);
+
+     attributes = atk_text_get_run_attributes (text, offset,
+                                              &intstart_offset, &intend_offset);
+
+     if (attributes) total_attributes = n_attributes = g_slist_length (attributes);
+
+     if (includeDefaults)
+     {
+        default_attributes = atk_text_get_default_attributes (text);
+        if (default_attributes)
+            n_default_attributes = g_slist_length (default_attributes);
+        total_attributes += n_default_attributes;
+     }
+
+     *startOffset = intstart_offset;
+     *endOffset = intend_offset; 
+
+     retval = CORBA_sequence_CORBA_string__alloc ();
+     retval->_length = retval->_maximum = total_attributes;
+     retval->_buffer = CORBA_sequence_CORBA_string_allocbuf (total_attributes);
+     CORBA_sequence_set_release (retval, CORBA_TRUE);
+
+     if (total_attributes)
+     {  
+        for (i = 0; i < n_attributes; ++i)
+        {
+            attr = g_slist_nth_data (attributes, i);
+            retval->_buffer[i] = CORBA_string_dup (g_strconcat (attr->name, ":", attr->value, NULL));
+        }
+        
+        for (j = 0; j < n_default_attributes; ++i, ++j)
+        {
+            attr = g_slist_nth_data (default_attributes, j);
+            retval->_buffer[i] = CORBA_string_dup (g_strconcat (attr->name, ":", attr->value, NULL));
+        }
+        
+        atk_attribute_set_free (attributes);
+        if (default_attributes)
+            atk_attribute_set_free (default_attributes);
+     }
+     return retval;
+}
+
+static Accessibility_AttributeSet *
+impl_getDefaultAttributeSet (PortableServer_Servant servant,
+                            CORBA_Environment *ev){
+     AtkAttributeSet *attributes;
+     AtkAttribute *attr = NULL;
+     Accessibility_AttributeSet *retval = NULL;
+     AtkText *text = get_text_from_servant (servant);
+     gint n_attributes = 0;
+     gint i;
+     
+     g_return_val_if_fail (text != NULL, NULL);
+     
+     attributes = atk_text_get_default_attributes (text);
+     
+     if (attributes) 
+     {
+        n_attributes = g_slist_length (attributes);
+
+        retval = CORBA_sequence_CORBA_string__alloc ();
+        retval->_length = retval->_maximum = n_attributes;
+        retval->_buffer = CORBA_sequence_CORBA_string_allocbuf (n_attributes);
+        CORBA_sequence_set_release (retval, CORBA_TRUE);
+        
+        for (i = 0; i < n_attributes; ++i)
+        {
+            attr = g_slist_nth_data (attributes, i);
+            retval->_buffer [i] = CORBA_string_dup (g_strconcat (attr->name, ":", attr->value, NULL));
+        }
+        atk_attribute_set_free (attributes);
+     }     
+     return retval;       
+}
+
+
+static void
 spi_text_class_init (SpiTextClass *klass)
 {
   POA_Accessibility_Text__epv *epv = &klass->epv;
@@ -404,6 +637,7 @@ spi_text_class_init (SpiTextClass *klass)
   epv->getTextBeforeOffset = impl_getTextBeforeOffset;
   epv->_get_caretOffset = impl__get_caretOffset;
   epv->getAttributes = impl_getAttributes;
+  epv->getDefaultAttributes = impl_getDefaultAttributes;
   epv->getCharacterExtents = impl_getCharacterExtents;
   epv->_get_characterCount = impl__get_characterCount;
   epv->getOffsetAtPoint = impl_getOffsetAtPoint;
@@ -413,6 +647,11 @@ spi_text_class_init (SpiTextClass *klass)
   epv->removeSelection = impl_removeSelection;
   epv->setSelection = impl_setSelection;
   epv->setCaretOffset = impl_setCaretOffset;
+  epv->getRangeExtents = impl_getRangeExtents;
+  epv->getBoundedRanges = impl_getBoundedRanges;
+  epv->getAttributeValue = impl_getAttributeValue;
+  epv->getAttributeRun = impl_getAttributeRun;
+  epv->getDefaultAttributeSet = impl_getDefaultAttributeSet;
 }
 
 static void
@@ -423,7 +662,7 @@ spi_text_init (SpiText *text)
 BONOBO_TYPE_FUNC_FULL (SpiText,
                       Accessibility_Text,
                       PARENT_TYPE,
-                      spi_text);
+                      spi_text)
 
 void
 spi_text_construct (SpiText *text, AtkObject *obj)