Fix for 95827, adds API for registering "AccessibleDeviceListeners"
[platform/core/uifw/at-spi2-atk.git] / libspi / text.c
index bc74e26..8aa5764 100644 (file)
@@ -2,7 +2,8 @@
  * AT-SPI - Assistive Technology Service Provider Interface
  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
  *
- * Copyright 2001 Sun Microsystems Inc.
+ * Copyright 2001, 2002 Sun Microsystems Inc.,
+ * Copyright 2001, 2002 Ximian, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -36,12 +37,9 @@ get_text_from_servant (PortableServer_Servant servant)
 {
   SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
 
-  if (!object)
-    {
-      return NULL;
-    }
-
-  return ATK_TEXT (object->atko);
+  g_return_val_if_fail (object, NULL);
+  g_return_val_if_fail (ATK_IS_OBJECT(object->gobj), NULL);
+  return ATK_TEXT (object->gobj);
 }
 
 static CORBA_string
@@ -56,7 +54,7 @@ impl_getText (PortableServer_Servant servant,
 
   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
   
-  txt = atk_text_get_text (text, (gint) startOffset, (gint) endOffset);
+  txt = atk_text_get_text (text, startOffset, endOffset);
   if (txt)
     {
       rv = CORBA_string_dup (txt);
@@ -86,10 +84,10 @@ impl_getTextAfterOffset (PortableServer_Servant servant,
   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
 
   txt = atk_text_get_text_after_offset (text,
-                                       (gint) offset, (AtkTextBoundary) type,
+                                       offset, (AtkTextBoundary) type,
                                        &intStartOffset, &intEndOffset);
-  *startOffset = (CORBA_long) intStartOffset;
-  *endOffset = (CORBA_long) intEndOffset;
+  *startOffset = intStartOffset;
+  *endOffset = intEndOffset;
 
   if (txt)
     {
@@ -111,20 +109,20 @@ impl_getTextAtOffset (PortableServer_Servant servant,
                      CORBA_long * endOffset,
                      CORBA_Environment *ev)
 {
-  CORBA_char *txt;
+  gchar *txt;
   CORBA_char *rv;
   gint intStartOffset, intEndOffset;
   AtkText *text = get_text_from_servant (servant);
 
   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
 
-  txt = (CORBA_char *) atk_text_get_text_at_offset (
+  txt = atk_text_get_text_at_offset (
          text,
-         (gint) offset, (AtkTextBoundary) type,
+         offset, (AtkTextBoundary) type,
          &intStartOffset, &intEndOffset);
 
-  *startOffset = (CORBA_long) intStartOffset;
-  *endOffset = (CORBA_long) intEndOffset;
+  *startOffset = intStartOffset;
+  *endOffset = intEndOffset;
 
   if (txt)
     {
@@ -147,8 +145,7 @@ impl_getCharacterAtOffset (PortableServer_Servant servant,
 
   g_return_val_if_fail (text != NULL, 0);
 
-  return (CORBA_unsigned_long)
-    atk_text_get_character_at_offset (text, (gint) offset);
+  return atk_text_get_character_at_offset (text, offset);
 }
 
 
@@ -169,11 +166,11 @@ impl_getTextBeforeOffset (PortableServer_Servant servant,
   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
 
   txt = atk_text_get_text_before_offset (text,
-                                        (gint) offset, (AtkTextBoundary) type,
+                                        offset, (AtkTextBoundary) type,
                                         &intStartOffset, &intEndOffset);
 
-  *startOffset = (CORBA_long) intStartOffset;
-  *endOffset = (CORBA_long) intEndOffset;
+  *startOffset = intStartOffset;
+  *endOffset = intEndOffset;
 
   if (txt)
     {
@@ -195,10 +192,40 @@ impl__get_caretOffset (PortableServer_Servant servant,
 
   g_return_val_if_fail (text != NULL, -1);
 
-  return (CORBA_long) atk_text_get_caret_offset (text);
+  return atk_text_get_caret_offset (text);
+}
+
+
+static CORBA_char *
+_string_from_attribute_set (AtkAttributeSet *set)
+{
+  gchar *attributes, *tmp, *tmp2;
+  CORBA_char *rv;
+  GSList *cur_attr;
+  AtkAttribute *at;
+  
+  attributes = g_strdup ("");
+  cur_attr = (GSList *) set;
+  while (cur_attr)
+    {
+      at = (AtkAttribute *) cur_attr->data;
+      tmp = g_strdup_printf ("%s%s:%s%s",
+                            ((GSList *)(set) == cur_attr) ? "" : " ",
+                            at->name, at->value,
+                            (cur_attr->next) ? ";" : "");
+      tmp2 = g_strconcat (attributes, tmp, NULL);
+      g_free (tmp);
+      g_free (attributes);
+      attributes = tmp2;
+      cur_attr = cur_attr->next;
+    }
+  rv = CORBA_string_dup (attributes);
+  g_free (attributes);
+  return rv;
 }
 
 
+
 static CORBA_string
 impl_getAttributes (PortableServer_Servant servant,
                    const CORBA_long offset,
@@ -206,13 +233,20 @@ impl_getAttributes (PortableServer_Servant servant,
                    CORBA_long * endOffset,
                    CORBA_Environment *ev)
 {
+  AtkAttributeSet *set;
+  gint intstart_offset, intend_offset;
+  CORBA_char *rv;
   AtkText *text = get_text_from_servant (servant);
 
   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
 
-  g_print ("getAttributes not yet implemented.\n");
-
-  return CORBA_string_dup ("");
+  set = atk_text_get_run_attributes (text, offset,
+                                    &intstart_offset, &intend_offset);
+  *startOffset = intstart_offset;
+  *endOffset = intend_offset;
+  rv = _string_from_attribute_set (set);
+  atk_attribute_set_free (set);
+  return rv;  
 }
 
 
@@ -225,14 +259,18 @@ impl_getCharacterExtents (PortableServer_Servant servant,
                          CORBA_Environment *ev)
 {
   AtkText *text = get_text_from_servant (servant);
+  gint ix, iy, iw, ih;
 
   g_return_if_fail (text != NULL);
 
-  /* FIXME: Casting a CORBA_long to a gint * is inherantly risky */
   atk_text_get_character_extents (
-         text, (gint) offset,
-         (gint *) x, (gint *) y, (gint *) width, (gint *) height,
+         text, offset,
+         &ix, &iy, &iw, &ih,
          (AtkCoordType) coordType);
+  *x = ix;
+  *y = iy;
+  *width = iw;
+  *height = ih;
 }
 
 
@@ -240,14 +278,11 @@ static CORBA_long
 impl__get_characterCount (PortableServer_Servant servant,
                          CORBA_Environment    *ev)
 {
-  CORBA_long retval;
   AtkText *text = get_text_from_servant (servant);
 
   g_return_val_if_fail (text != NULL, 0);
 
-  retval = (CORBA_long) atk_text_get_character_count (text);
-
-  return retval;
+  return atk_text_get_character_count (text);
 }
 
 
@@ -261,9 +296,8 @@ impl_getOffsetAtPoint (PortableServer_Servant servant,
 
   g_return_val_if_fail (text != NULL, -1);
 
-  return (CORBA_long)
-    atk_text_get_offset_at_point (text,
-                                 (gint) x, (gint) y,
+  return atk_text_get_offset_at_point (text,
+                                 x, y,
                                  (AtkCoordType) coordType);
 }
 
@@ -276,7 +310,7 @@ impl_getNSelections (PortableServer_Servant servant,
 
   g_return_val_if_fail (text != NULL, 0);
 
-  return (CORBA_long) atk_text_get_n_selections (text);
+  return atk_text_get_n_selections (text);
 }
 
 
@@ -287,11 +321,16 @@ impl_getSelection (PortableServer_Servant servant,
                   CORBA_Environment *ev)
 {
   AtkText *text = get_text_from_servant (servant);
-
+  gint intStartOffset, intEndOffset;
+  
   g_return_if_fail (text != NULL);
 
-  atk_text_get_selection (text, (gint) selectionNum,
-                         (gint *) startOffset, (gint *) endOffset);
+  /* atk_text_get_selection returns gchar* which we discard */
+  g_free (atk_text_get_selection (text, selectionNum,
+                                 &intStartOffset, &intEndOffset));
+  
+  *startOffset = intStartOffset;
+  *endOffset = intEndOffset;
 }
 
 
@@ -305,9 +344,8 @@ impl_addSelection (PortableServer_Servant servant,
 
   g_return_val_if_fail (text != NULL, FALSE);
 
-  return (CORBA_boolean)
-    atk_text_add_selection (text,
-                           (gint) startOffset, (gint) endOffset);
+  return atk_text_add_selection (text,
+                           startOffset, endOffset);
 }
 
 
@@ -320,8 +358,7 @@ impl_removeSelection (PortableServer_Servant servant,
 
   g_return_val_if_fail (text != NULL, FALSE);
 
-  return (CORBA_boolean)
-    atk_text_remove_selection (text, (gint) selectionNum);
+  return atk_text_remove_selection (text, selectionNum);
 }
 
 
@@ -336,9 +373,8 @@ impl_setSelection (PortableServer_Servant servant,
 
   g_return_val_if_fail (text != NULL, FALSE);
 
-  return (CORBA_boolean)
-    atk_text_set_selection (text,
-                           (gint) selectionNum, (gint) startOffset, (gint) endOffset);
+  return atk_text_set_selection (text,
+                           selectionNum, startOffset, endOffset);
 }
 
 
@@ -351,21 +387,7 @@ impl_setCaretOffset (PortableServer_Servant servant,
 
   g_return_val_if_fail (text != NULL, FALSE);
 
-  return (CORBA_boolean)
-    atk_text_set_caret_offset (text, (gint) value);
-}
-
-
-static void 
-impl_getRowColAtOffset (PortableServer_Servant servant,
-                       const CORBA_long offset, CORBA_long * row,
-                       CORBA_long * column, CORBA_Environment *ev)
-{
-  AtkText *text = get_text_from_servant (servant);
-
-  g_return_if_fail (text != NULL);
-
-  g_print ("getRowColAtOffset not yet implemented\n");
+  return atk_text_set_caret_offset (text, value);
 }
 
 static void
@@ -406,9 +428,10 @@ BONOBO_TYPE_FUNC_FULL (SpiText,
 void
 spi_text_construct (SpiText *text, AtkObject *obj)
 {
-  spi_base_construct (SPI_BASE (text), obj);
+  spi_base_construct (SPI_BASE (text), G_OBJECT(obj));
 }
 
+
 SpiText *
 spi_text_interface_new (AtkObject *obj)
 {