2001-12-10 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / libspi / accessible.c
index 8e2b8e2..1148cb0 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
-/*
- * accessible.c: test for accessibility implementation
- *
- */
-#include <config.h>
-#include <bonobo/Bonobo.h>
+/* accessible.c: the core of the accessibility implementation */
 
+#include <config.h>
 #include <stdio.h>
+#include <bonobo/bonobo-exception.h>
+#include <atk/atk.h>
+#include <libspi/libspi.h>
 
-/*
- * This pulls the CORBA definitions for the "Accessibility::Accessible" server
- */
-#include <libspi/Accessibility.h>
-
-/*
- * This pulls the definition for the BonoboObject (Gtk Type)
- */
-#include "accessible.h"
-#include "component.h"
-#include "editabletext.h"
-#include "hyperlink.h"
-#include "hypertext.h"
-#include "image.h"
-#include "selection.h"
-#include "table.h"
-#include "text.h"
-#include "value.h"
-#include "action.h"
-
-/*
- * Our parent Gtk object type
- */
-#define PARENT_TYPE BONOBO_OBJECT_TYPE
+/* Our parent Gtk object type  */
+#define PARENT_TYPE SPI_TYPE_BASE
 
-/*
- * A pointer to our parent object class
- */
-static GObjectClass *spi_accessible_parent_class;
-
-/*
- * Implemented GObject::finalize
- */
-static void
-spi_accessible_object_finalize (GObject *object)
+static AtkObject *
+get_accessible_from_servant (PortableServer_Servant servant)
 {
-        SpiAccessible *accessible = SPI_ACCESSIBLE (object);
+  SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
 
-        printf("spi_accessible_object_finalize called\n");
-       ATK_OBJECT (accessible->atko); /* assertion */
-        g_object_unref (G_OBJECT(accessible->atko));
-        accessible->atko = NULL;
+  if (!object)
+    {
+      return NULL;
+    }
 
-        printf("atko freed, calling parent finalize\n");
-        spi_accessible_parent_class->finalize (object);
+  return object->atko;
 }
 
 /*
@@ -83,13 +51,22 @@ static CORBA_char *
 impl_accessibility_accessible_get_name (PortableServer_Servant servant,
                                         CORBA_Environment     *ev)
 {
-  CORBA_char * retval;
-  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-  retval = (CORBA_char *) atk_object_get_name (accessible->atko);
-  if (retval )
-    retval = CORBA_string_dup (retval);
+  const gchar *name;
+  CORBA_char  *retval;
+  AtkObject   *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, CORBA_string_dup (""));
+
+  name = atk_object_get_name (object);
+
+  if (name)
+    {
+      retval = CORBA_string_dup (name);
+    }
   else
-    retval = CORBA_string_dup ("");
+    {
+      retval = CORBA_string_dup ("");
+    }
 
   return retval;
 }
@@ -102,9 +79,11 @@ impl_accessibility_accessible_set_name (PortableServer_Servant servant,
                                         const CORBA_char      *name,
                                         CORBA_Environment     *ev)
 {
-  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-  atk_object_set_name (accessible->atko, name);
-  printf ("SpiAccessible set_name called: %s\n", name);
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_if_fail (object != NULL);
+
+  atk_object_set_name (object, name);
 }
 
 /*
@@ -114,9 +93,22 @@ static CORBA_char *
 impl_accessibility_accessible_get_description (PortableServer_Servant servant,
                                                CORBA_Environment     *ev)
 {
-  CORBA_char * retval;
-  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-  retval = CORBA_string_dup (atk_object_get_description (accessible->atko));
+  const gchar *descr;
+  CORBA_char  *retval;
+  AtkObject   *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, CORBA_string_dup (""));
+
+  descr = atk_object_get_description (object);
+
+  if (descr)
+    {
+      retval = CORBA_string_dup (descr);
+    }
+  else
+    {
+      retval = CORBA_string_dup ("");
+    }
 
   return retval;
 }
@@ -126,12 +118,14 @@ impl_accessibility_accessible_get_description (PortableServer_Servant servant,
  */
 static void
 impl_accessibility_accessible_set_description (PortableServer_Servant servant,
-                                               const CORBA_char      *name,
+                                               const CORBA_char      *descr,
                                                CORBA_Environment     *ev)
 {
-  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-  atk_object_set_description (accessible->atko, name);
-  printf ("SpiAccessible set_description called: %s\n", name);
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_if_fail (object != NULL);
+
+  atk_object_set_description (object, descr);
 }
 
 /*
@@ -141,16 +135,14 @@ static Accessibility_Accessible
 impl_accessibility_accessible_get_parent (PortableServer_Servant servant,
                                           CORBA_Environment     *ev)
 {
-  Accessibility_Accessible retval;
-  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-  AtkObject     *parent;
+  AtkObject *parent;
+  AtkObject *object = get_accessible_from_servant (servant);
 
-  parent = atk_object_get_parent (accessible->atko);
-  retval = BONOBO_OBJREF (spi_accessible_new (parent));
+  g_return_val_if_fail (object != NULL, CORBA_OBJECT_NIL);
 
-  printf ("SpiAccessible get_parent called\n");
+  parent = atk_object_get_parent (object);
 
-  return CORBA_Object_duplicate (retval, ev);
+  return spi_accessible_new_return (parent, FALSE, ev);
 }
 
 /*
@@ -160,11 +152,11 @@ static CORBA_long
 impl_accessibility_accessible_get_index_in_parent (PortableServer_Servant servant,
                                                    CORBA_Environment     *ev)
 {
-  CORBA_long retval;
-  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-  retval = (CORBA_long) atk_object_get_index_in_parent (accessible->atko);
-  printf ("SpiAccessible get_index_in_parent called\n");
-  return retval;
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, -1);
+
+  return atk_object_get_index_in_parent (object);
 }
 
 /*
@@ -174,11 +166,11 @@ static CORBA_long
 impl_accessibility_accessible_get_child_count (PortableServer_Servant servant,
                                                CORBA_Environment     *ev)
 {
-  CORBA_long retval;
-  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-  retval = (CORBA_long) atk_object_get_n_accessible_children (accessible->atko);
-  printf ("SpiAccessible get_childCount called: %d\n", (int) retval);
-  return retval;
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, 0);
+
+  return atk_object_get_n_accessible_children (object);
 }
 
 /*
@@ -189,12 +181,14 @@ impl_accessibility_accessible_get_child_at_index (PortableServer_Servant servant
                                                   const CORBA_long      index,
                                                   CORBA_Environment     *ev)
 {
-  Accessibility_Accessible retval;
-  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-  AtkObject *child = atk_object_ref_accessible_child (accessible->atko, (gint) index);
-  retval = BONOBO_OBJREF (spi_accessible_new (child));
-  printf ("SpiAccessible get_child_at_index called.\n");
-  return CORBA_Object_duplicate (retval, ev);
+  AtkObject *child;
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, 0);
+
+  child = atk_object_ref_accessible_child (object, index);
+
+  return spi_accessible_new_return (child, TRUE, ev);
 }
 
 /*
@@ -204,13 +198,14 @@ static Accessibility_StateSet
 impl_accessibility_accessible_get_state (PortableServer_Servant servant,
                                         CORBA_Environment     *ev)
 {
-  Accessibility_StateSet retval;
-/*  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-    AtkStateSet *state = atk_object_ref_state_set (accessible->atko); */
-  retval = CORBA_OBJECT_NIL;
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  bonobo_return_val_if_fail (object != NULL, NULL, ev);
+
   printf ("SpiAccessible get_state.\n");
+
   /* TODO: implement the bonobo stateset class */
-  return (Accessibility_StateSet) retval;
+  return (Accessibility_StateSet) NULL;
 }
 
 /*
@@ -221,13 +216,28 @@ impl_accessibility_accessible_get_relation_set (PortableServer_Servant servant,
                                                CORBA_Environment     *ev)
 {
   Accessibility_RelationSet *retval;
-/*  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-    AtkRelationSet *relation_set = atk_object_ref_relation_set (accessible->atko); */
+  gint n_relations;
+  gint i;
+  AtkRelationSet *relation_set;
+  AtkObject      *object = get_accessible_from_servant (servant);
+
+  bonobo_return_val_if_fail (object != NULL, NULL, ev);
+
+  relation_set = atk_object_ref_relation_set (object);
+
+  n_relations = atk_relation_set_get_n_relations (relation_set);
   retval = CORBA_sequence_Accessibility_Relation__alloc ();
-  /*
-   *  TODO: fill the sequence with relation set objects, themselves
-   *  initialized from the AtkRelation object in the AtkRelationSet.
-   */
+  CORBA_sequence_Accessibility_Relation_allocbuf (n_relations);
+         
+  for (i = 0; i < n_relations; ++i)
+    {
+      retval->_buffer[i] =
+        bonobo_object_dup_ref (
+          BONOBO_OBJREF (
+            spi_relation_new (atk_relation_set_get_relation (relation_set, i))),
+         ev);
+    }
+  
   printf ("SpiAccessible get_relation_set.\n");
   return retval;
 }
@@ -239,22 +249,22 @@ static Accessibility_Role
 impl_accessibility_accessible_get_role (PortableServer_Servant servant,
                                        CORBA_Environment     *ev)
 {
+  AtkRole            role;
   Accessibility_Role retval;
-  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
-  AtkRole role = atk_object_get_role (accessible->atko);
-  retval = role;
-  printf ("SpiAccessible get_role.\n");
-  return (Accessibility_Role) retval;
+  AtkObject         *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, 0);
+
+  role = atk_object_get_role (object);
+  retval = role; /* FIXME: relies on ability to cast these back and forth */
+
+  return retval;
 }
 
 static void
 spi_accessible_class_init (SpiAccessibleClass *klass)
 {
-        GObjectClass * object_class = (GObjectClass *) klass;
         POA_Accessibility_Accessible__epv *epv = &klass->epv;
-        spi_accessible_parent_class = g_type_class_peek_parent (klass);
-
-        object_class->finalize = spi_accessible_object_finalize;
 
         epv->_get_name = impl_accessibility_accessible_get_name;
         epv->_set_name = impl_accessibility_accessible_set_name;
@@ -276,57 +286,55 @@ spi_accessible_init (SpiAccessible *accessible)
 {
 }
 
-GType
-spi_accessible_get_type (void)
+BONOBO_TYPE_FUNC_FULL (SpiAccessible,
+                      Accessibility_Accessible,
+                      PARENT_TYPE,
+                      spi_accessible);
+
+static GHashTable *public_corba_refs = NULL;
+
+static GHashTable *
+get_public_refs (void)
 {
-        static GType type = 0;
-
-        if (!type) {
-                static const GTypeInfo tinfo = {
-                        sizeof (SpiAccessibleClass),
-                        (GBaseInitFunc) NULL,
-                        (GBaseFinalizeFunc) NULL,
-                        (GClassInitFunc) spi_accessible_class_init,
-                        (GClassFinalizeFunc) NULL,
-                        NULL, /* class data */
-                        sizeof (SpiAccessible),
-                        0, /* n preallocs */
-                        (GInstanceInitFunc) spi_accessible_init,
-                        NULL /* value table */
-                };
-                /*
-                 * Bonobo_type_unique auto-generates a load of
-                 * CORBA structures for us. All derived types must
-                 * use bonobo_type_unique.
-                 */
-                type = bonobo_type_unique (
-                        PARENT_TYPE,
-                        POA_Accessibility_Accessible__init,
-                        NULL,
-                        G_STRUCT_OFFSET (SpiAccessibleClass, epv),
-                        &tinfo,
-                        "SpiAccessible");
-        }
-
-        return type;
+  if (!public_corba_refs)
+    {
+      public_corba_refs = g_hash_table_new (NULL, NULL);
+    }
+  return public_corba_refs;
+}
+
+static void
+de_register_public_ref (SpiBase *object)
+{
+  g_hash_table_remove (get_public_refs (), object->atko);
 }
 
 SpiAccessible *
 spi_accessible_new (AtkObject *o)
 {
-    SpiAccessible *retval =
-               SPI_ACCESSIBLE (g_object_new (spi_accessible_get_type (), NULL));
+    SpiAccessible *retval;
     CORBA_Environment ev;
+
     CORBA_exception_init (&ev);
-    g_object_ref (o);
-    retval->atko = ATK_OBJECT (o);
 
-    /*
-     * TODO: add interface containers/constructors for SPI_EDITABLE_TEXT, SPI_HYPERTEXT,
-     *  SPI_IMAGE, SPI_SELECTION, SPI_TABLE, SPI_TEXT, SPI_VALUE.
-     */
+    g_assert (o);
+
+    if ((retval = g_hash_table_lookup (get_public_refs (), o)))
+      {
+        bonobo_object_ref (BONOBO_OBJECT (retval));
+       return retval;
+      }
+
+    retval = g_object_new (SPI_ACCESSIBLE_TYPE, NULL);
+
+    spi_base_construct (SPI_BASE (retval), o);
 
-    /* add appropriate ATK interfaces */
+    g_hash_table_insert (get_public_refs (), o, retval);
+    g_signal_connect (G_OBJECT (retval), "destroy",
+                     G_CALLBACK (de_register_public_ref),
+                     NULL);
+
+    /* aggregate appropriate SPI interfaces based on ATK interfaces */
 
     if (ATK_IS_ACTION (o))
       {
@@ -384,3 +392,36 @@ spi_accessible_new (AtkObject *o)
 
     return retval;
 }
+
+/**
+ * spi_accessible_new_return:
+ * @o: an AtkObject or NULL
+ * @release_ref: whether to unref this AtkObject before return
+ * @ev: a CORBA environment
+ * 
+ * A helper function to instantiate a CORBA accessiblility
+ * proxy from an AtkObject.
+ * 
+ * Return value: the proxy or CORBA_OBJECT_NIL
+ **/
+Accessibility_Accessible
+spi_accessible_new_return (AtkObject         *o,
+                          gboolean           release_ref,
+                          CORBA_Environment *ev)
+{
+  SpiAccessible *accessible;
+
+  if (!o)
+    {
+      return CORBA_OBJECT_NIL;
+    }
+
+  accessible = spi_accessible_new (o);
+
+  if (release_ref)
+    {
+      g_object_unref (G_OBJECT (o));
+    }
+
+  return CORBA_Object_duplicate (BONOBO_OBJREF (accessible), ev);
+}