2001-12-07 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / libspi / accessible.c
index 96b0a3c..ff11bbd 100644 (file)
 /*
- * accessible.c: test for accessibility implementation
+ * AT-SPI - Assistive Technology Service Provider Interface
+ * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
  *
+ * Copyright 2001 Sun Microsystems Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library 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 <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>
+
+/* Our parent Gtk object type  */
+#define PARENT_TYPE SPI_TYPE_BASE
+
+static AtkObject *
+get_accessible_from_servant (PortableServer_Servant servant)
+{
+  SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
+
+  if (!object)
+    {
+      return NULL;
+    }
+
+  return object->atko;
+}
+
+/*
+ * CORBA Accessibility::Accessible::get_name method implementation
+ */
+static CORBA_char *
+impl_accessibility_accessible_get_name (PortableServer_Servant servant,
+                                        CORBA_Environment     *ev)
+{
+  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 ("");
+    }
+
+  return retval;
+}
+
+/*
+ * CORBA Accessibility::Accessible::set_name method implementation
+ */
+static void
+impl_accessibility_accessible_set_name (PortableServer_Servant servant,
+                                        const CORBA_char      *name,
+                                        CORBA_Environment     *ev)
+{
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_if_fail (object != NULL);
+
+  atk_object_set_name (object, name);
+}
 
 /*
- * This pulls the CORBA definitions for the "Accessibility::Accessible" server
+ * CORBA Accessibility::Accessible::get_description method implementation
  */
-#include "Accessible.h"
+static CORBA_char *
+impl_accessibility_accessible_get_description (PortableServer_Servant servant,
+                                               CORBA_Environment     *ev)
+{
+  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;
+}
 
 /*
- * This pulls the definition for the BonoboObject (Gtk Type)
+ * CORBA Accessibility::Accessible::set_description method implementation
  */
-#include "accessible.h"
+static void
+impl_accessibility_accessible_set_description (PortableServer_Servant servant,
+                                               const CORBA_char      *descr,
+                                               CORBA_Environment     *ev)
+{
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_if_fail (object != NULL);
+
+  atk_object_set_description (object, descr);
+}
 
 /*
- * Our parent Gtk object type
+ * CORBA Accessibility::Accessible::get_parent method implementation
  */
-#define PARENT_TYPE BONOBO_X_OBJECT_TYPE
+static Accessibility_Accessible
+impl_accessibility_accessible_get_parent (PortableServer_Servant servant,
+                                          CORBA_Environment     *ev)
+{
+  AtkObject *parent;
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, CORBA_OBJECT_NIL);
+
+  parent = atk_object_get_parent (object);
+
+  return spi_accessible_new_return (parent, FALSE, ev);
+}
 
 /*
- * A pointer to our parent object class
+ * CORBA Accessibility::Accessible::get_IndexInParent method implementation
  */
-static GObjectClass *accessible_parent_class;
+static CORBA_long
+impl_accessibility_accessible_get_index_in_parent (PortableServer_Servant servant,
+                                                   CORBA_Environment     *ev)
+{
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, -1);
+
+  return atk_object_get_index_in_parent (object);
+}
 
 /*
- * Implemented GObject::finalize
+ * CORBA Accessibility::Accessible::get_childCount method implementation
  */
-static void
-accessible_object_finalize (GObject *object)
+static CORBA_long
+impl_accessibility_accessible_get_child_count (PortableServer_Servant servant,
+                                               CORBA_Environment     *ev)
 {
-        Accessible *accessible = ACCESSIBLE (object);
+  AtkObject *object = get_accessible_from_servant (servant);
 
-        printf("accessible_object_finalize called\n");
-        g_free (accessible->atko);
+  g_return_val_if_fail (object != NULL, 0);
 
-        printf("atko freed, calling parent finalize\n");
-        accessible_parent_class->finalize (object);
+  return atk_object_get_n_accessible_children (object);
 }
 
 /*
- * CORBA Accessibility::Accessible::get_name method implementation
+ * CORBA Accessibility::Accessible::getChildAtIndex method implementation
  */
-static CORBA_char *
-impl_accessibility_accessible_get_name (PortableServer_Servant servant,
-                                        CORBA_Environment     *ev)
+static Accessibility_Accessible
+impl_accessibility_accessible_get_child_at_index (PortableServer_Servant servant,
+                                                  const CORBA_long      index,
+                                                  CORBA_Environment     *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);
+}
+
+/*
+ * CORBA Accessibility::Accessible::getState method implementation
+ */
+static Accessibility_StateSet
+impl_accessibility_accessible_get_state (PortableServer_Servant servant,
+                                        CORBA_Environment     *ev)
 {
-  CORBA_char * retval;
-  Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
-  retval = CORBA_string_dup (atk_object_get_name (accessible->atko));
-  fprintf (stderr, "Accessible get_name called: %s\n", retval);
+  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) NULL;
+}
+
+/*
+ * CORBA Accessibility::Accessible::getRelationSet method implementation
+ */
+static Accessibility_RelationSet *
+impl_accessibility_accessible_get_relation_set (PortableServer_Servant servant,
+                                               CORBA_Environment     *ev)
+{
+  Accessibility_RelationSet *retval;
+  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 ();
+  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;
 }
 
 /*
- * CORBA Accessibility::Accessible::set_name method implementation
+ * CORBA Accessibility::Accessible::getRole method implementation
  */
-static void
-impl_accessibility_accessible_set_name (PortableServer_Servant servant,
-                                        const CORBA_char      *name,
-                                        CORBA_Environment     *ev)
+static Accessibility_Role
+impl_accessibility_accessible_get_role (PortableServer_Servant servant,
+                                       CORBA_Environment     *ev)
 {
-  Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
-  atk_object_set_name (accessible->atko, name);
-  printf ("Accessible set_name called: %s\n", name);
+  AtkRole            role;
+  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
-accessible_class_init (AccessibleClass *klass)
+spi_accessible_class_init (SpiAccessibleClass *klass)
 {
-        GObjectClass * object_class = (GObjectClass *) klass;
         POA_Accessibility_Accessible__epv *epv = &klass->epv;
-        accessible_parent_class = g_type_class_ref (BONOBO_X_OBJECT_TYPE);
-        /*accessible_parent_class = g_type_class_peek_parent (klass);*/
-
-        object_class->finalize = accessible_object_finalize;
 
         epv->_get_name = impl_accessibility_accessible_get_name;
         epv->_set_name = impl_accessibility_accessible_set_name;
+        epv->_get_description = impl_accessibility_accessible_get_description;
+        epv->_set_description = impl_accessibility_accessible_set_description;
+
+        epv->_get_parent = impl_accessibility_accessible_get_parent;
+        epv->_get_childCount = impl_accessibility_accessible_get_child_count;
+        epv->getChildAtIndex = impl_accessibility_accessible_get_child_at_index;
+        epv->getIndexInParent = impl_accessibility_accessible_get_index_in_parent;
+
+        epv->getRelationSet = impl_accessibility_accessible_get_relation_set;
+        epv->getState = impl_accessibility_accessible_get_state;
+        epv->getRole = impl_accessibility_accessible_get_role;
+}
+
+static void
+spi_accessible_init (SpiAccessible *accessible)
+{
+}
+
+BONOBO_TYPE_FUNC_FULL (SpiAccessible,
+                      Accessibility_Accessible,
+                      PARENT_TYPE,
+                      spi_accessible);
+
+static GHashTable *public_corba_refs = NULL;
+
+static GHashTable *
+get_public_refs (void)
+{
+  if (!public_corba_refs)
+    {
+      public_corba_refs = g_hash_table_new (NULL, NULL);
+    }
+  return public_corba_refs;
 }
 
 static void
-accessible_init (Accessible *accessible)
-{
-}
-
-GType
-accessible_get_type (void)
-{
-        static GType type = 0;
-
-        if (!type) {
-                static const GTypeInfo tinfo = {
-                        sizeof (AccessibleClass),
-                        (GBaseInitFunc) NULL,
-                        (GBaseFinalizeFunc) NULL,
-                        (GClassInitFunc) accessible_class_init,
-                        (GClassFinalizeFunc) NULL,
-                        NULL, /* class data */
-                        sizeof (Accessible),
-                        0, /* n preallocs */
-                        (GInstanceInitFunc) accessible_init,
-                        NULL /* value table */
-                };
-                /*
-                 *   Here we use bonobo_x_type_unique instead of
-                 * gtk_type_unique, this auto-generates a load of
-                 * CORBA structures for us. All derived types must
-                 * use bonobo_x_type_unique.
-                 */
-                type = bonobo_x_type_unique (
-                        PARENT_TYPE,
-                        POA_Accessibility_Accessible__init,
-                        NULL,
-                        G_STRUCT_OFFSET (AccessibleClass, epv),
-                        &tinfo,
-                        "Accessible");
-        }
-
-        return type;
-}
-
-Accessible *
-accessible_new (AtkObject *o)
-{
-    Accessible *retval =
-               ACCESSIBLE (g_object_new (accessible_get_type (), NULL));
-    retval->atko = ATK_OBJECT (o);
+de_register_public_ref (SpiBase *object)
+{
+  g_hash_table_remove (get_public_refs (), object->atko);
+}
+
+SpiAccessible *
+spi_accessible_new (AtkObject *o)
+{
+    SpiAccessible *retval;
+    CORBA_Environment ev;
+
+    CORBA_exception_init (&ev);
+
+    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);
+
+    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))
+      {
+        bonobo_object_add_interface (bonobo_object (retval),
+                                     BONOBO_OBJECT (spi_action_interface_new (o)));
+      }
+
+    if (ATK_IS_COMPONENT (o))
+      {
+        bonobo_object_add_interface (bonobo_object (retval),
+                                     BONOBO_OBJECT (spi_component_interface_new (o)));
+      }
+
+    if (ATK_IS_EDITABLE_TEXT (o))
+      {
+        bonobo_object_add_interface (bonobo_object (retval),
+                                     BONOBO_OBJECT(spi_editable_text_interface_new (o)));
+      }
+
+    else if (ATK_IS_HYPERTEXT (o))
+      {
+        bonobo_object_add_interface (bonobo_object (retval),
+                                     BONOBO_OBJECT (spi_hypertext_interface_new (o)));
+      }
+
+    else if (ATK_IS_TEXT (o))
+      {
+        bonobo_object_add_interface (bonobo_object (retval),
+                                     BONOBO_OBJECT (spi_text_interface_new (o)));
+      }
+
+    if (ATK_IS_IMAGE (o))
+      {
+        bonobo_object_add_interface (bonobo_object (retval),
+                                     BONOBO_OBJECT (spi_image_interface_new (o)));
+      }
+
+    if (ATK_IS_SELECTION (o))
+      {
+        bonobo_object_add_interface (bonobo_object (retval),
+                                     BONOBO_OBJECT (spi_selection_interface_new (o)));
+      }
+
+    if (ATK_IS_TABLE (o))
+      {
+        bonobo_object_add_interface (bonobo_object (retval),
+                                     BONOBO_OBJECT (spi_table_interface_new (o)));
+      }
+
+    if (ATK_IS_VALUE (o))
+      {
+        bonobo_object_add_interface (bonobo_object (retval),
+                                     BONOBO_OBJECT (spi_value_interface_new (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);
+}