2001-12-10 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_accessible.c
index 9074be5..ca366c1 100644 (file)
@@ -1,37 +1,26 @@
 #include <stdlib.h> /* for malloc */
 #include <cspi/spi-private.h>
 
-#define MAX_ROLES 100
-
-static char *role_names [MAX_ROLES] =
+static const char *role_names [] =
 {
-  " ",
-  "accelerator label",
+  "<invalid>",
   "alert",
-  "animation",
-  "arrow",
-  "calendar",
   "canvas",
   "check box",
-  "menu item",
   "color chooser",
   "column header",
   "combo box",
-  "date editor",
   "desktop icon",
   "desktop frame",
-  "dial",
   "dialog",
   "directory pane",
-  "drawing area",
   "file chooser",
   "filler",
-  "font chooser",
+  "focus traversable",
   "frame",
   "glass pane",
   "HTML container",
   "icon",
-  "image",
   "internal frame",
   "label",
   "layered pane",
@@ -49,7 +38,6 @@ static char *role_names [MAX_ROLES] =
   "progress bar",
   "pushbutton",
   "radiobutton",
-  "radio menu item",
   "root pane",
   "row header",
   "scrollbar",
@@ -57,23 +45,38 @@ static char *role_names [MAX_ROLES] =
   "separator",
   "slider",
   "split pane",
-  "spin button",
-  "status bar",
   "table",
   "table cell",
   "table column header",
   "table row header",
-  "tearoff menu item",
   "text",
   "toggle button",
   "toolbar",
   "tooltip",
   "tree",
-  " ",
+  "<unknown>",
   "viewport",
   "window",
+
+  /* These have no equivalent AccessibleRole enum values */
+  "accelerator label",
+  "animation",
+  "arrow",
+  "calendar",
+  "menu item",
+  "date editor",
+  "dial",
+  "drawing area",
+  "font chooser",
+  "image",
+  "radio menu item",
+  "tearoff menu item",
+  "spin button",
+  "status bar",
 };
 
+#define MAX_ROLES (sizeof (role_names) / sizeof (char *))
+
 /**
  * AccessibleRole_getName:
  * @role: an #AccessibleRole object to query.
@@ -82,15 +85,23 @@ static char *role_names [MAX_ROLES] =
  *
  * Returns: a localizable string name for an #AccessibleRole enumerated type.
  **/
-char*
+const char *
 AccessibleRole_getName (AccessibleRole role)
 {
-  if (role < MAX_ROLES) return role_names [(int) role];
-  else return "";
+  if (role < MAX_ROLES)
+    {
+      return role_names [(int) role];
+    }
+  else
+    {
+      return "";
+    }
+  /*
+   * TODO: replace with implementation linked to ATK, which
+   *  now supports Role/Name mapping
+   */
 }
 
-
-
 /**
  * Accessible_ref:
  * @obj: a pointer to the #Accessible object on which to operate.
@@ -100,28 +111,23 @@ AccessibleRole_getName (AccessibleRole role)
  * Returns: (no return code implemented yet).
  *
  **/
-int
+void
 Accessible_ref (Accessible *obj)
 {
   cspi_object_ref (obj);
-  return 0;
 }
 
-
 /**
  * Accessible_unref:
  * @obj: a pointer to the #Accessible object on which to operate.
  *
  * Decrement the reference count for an #Accessible object.
  *
- * Returns: (no return code implemented yet).
- *
  **/
-int
+void
 Accessible_unref (Accessible *obj)
 {
   cspi_object_unref (obj);
-  return 0;
 }
 
 /**
@@ -131,15 +137,20 @@ Accessible_unref (Accessible *obj)
  * Get the name of an #Accessible object.
  *
  * Returns: a UTF-8 string indicating the name of the #Accessible object.
- *
+ * or NULL on exception
  **/
 char *
 Accessible_getName (Accessible *obj)
 {
-  char *retval = 
-    (char *)
+  char *retval;
+
+  cspi_return_val_if_fail (obj != NULL, NULL);
+
+  retval = (char *)
     Accessibility_Accessible__get_name (CSPI_OBJREF (obj), cspi_ev ());
-  cspi_check_ev (cspi_ev (), "getName"); 
+
+  cspi_return_val_if_ev ("getName", NULL); 
+
   return retval;
 }
 
@@ -150,14 +161,21 @@ Accessible_getName (Accessible *obj)
  * Get the description of an #Accessible object.
  *
  * Returns: a UTF-8 string describing the #Accessible object.
- *
+ * or NULL on exception
  **/
 char *
 Accessible_getDescription (Accessible *obj)
 {
-  char *retval = (char *)
-    Accessibility_Accessible__get_description (CSPI_OBJREF (obj), cspi_ev ());
-  cspi_check_ev (cspi_ev (), "getDescription");
+  char *retval;
+
+  cspi_return_val_if_fail (obj != NULL, NULL);
+
+  retval = (char *)
+    Accessibility_Accessible__get_description (CSPI_OBJREF (obj),
+                                              cspi_ev ());
+
+  cspi_return_val_if_ev ("getDescription", NULL); 
+
   return retval;
 }
 
@@ -174,9 +192,16 @@ Accessible_getDescription (Accessible *obj)
 Accessible *
 Accessible_getParent (Accessible *obj)
 {
-  Accessible *retval = 
-      cspi_object_add (Accessibility_Accessible__get_parent (CSPI_OBJREF (obj), cspi_ev ()));
-  cspi_check_ev (cspi_ev (), "getParent");
+  Accessible *retval;
+
+  cspi_return_val_if_fail (obj != NULL, NULL);
+
+  retval = cspi_object_add (
+    Accessibility_Accessible__get_parent (CSPI_OBJREF (obj),
+                                         cspi_ev ()));
+
+  cspi_return_val_if_ev ("getParent", NULL); 
+
   return retval;
 }
 
@@ -187,14 +212,22 @@ Accessible_getParent (Accessible *obj)
  * Get the number of children contained by an #Accessible object.
  *
  * Returns: a #long indicating the number of #Accessible children
- *          contained by an #Accessible object.
+ *          contained by an #Accessible object. or -1 on exception
  *
  **/
 long
 Accessible_getChildCount (Accessible *obj)
 {
-  long retval = (long) Accessibility_Accessible__get_childCount (CSPI_OBJREF (obj), cspi_ev ());
-  cspi_check_ev (cspi_ev (), "getChildCount");
+  long retval;
+
+  cspi_return_val_if_fail (obj != NULL, -1);
+
+  retval = (long) 
+    Accessibility_Accessible__get_childCount (CSPI_OBJREF (obj),
+                                             cspi_ev ());
+
+  cspi_return_val_if_ev ("getChildCount", -1); 
+
   return retval;
 }
 
@@ -206,15 +239,20 @@ Accessible_getChildCount (Accessible *obj)
  * Get the #Accessible child of an #Accessible object at a given index.
  *
  * Returns: a pointer to the #Accessible child object at index
- *          @childIndex.
- *
+ *          @childIndex. or NULL on exception
  **/
 Accessible *
 Accessible_getChildAtIndex (Accessible *obj,
-                            long int childIndex)
+                            long int    childIndex)
 {
-  Accessible *retval = cspi_object_add (Accessibility_Accessible_getChildAtIndex (CSPI_OBJREF (obj), childIndex, cspi_ev ()));
-  cspi_check_ev (cspi_ev (), "getChildAtIndex");
+  Accessible *retval;
+
+  cspi_return_val_if_fail (obj != NULL, NULL);
+
+  retval = cspi_object_add (
+    Accessibility_Accessible_getChildAtIndex (CSPI_OBJREF (obj),
+                                             childIndex, cspi_ev ()));
+
   return retval;
 }
 
@@ -226,14 +264,19 @@ Accessible_getChildAtIndex (Accessible *obj,
  *
  * Returns: a #long indicating the index of the #Accessible object
  *          in its parent (i.e. containing) #Accessible instance,
- *          or -1 if @obj has no containing parent.
- *
+ *          or -1 if @obj has no containing parent or on exception.
  **/
 long
 Accessible_getIndexInParent (Accessible *obj)
 {
-  long retval = (long) Accessibility_Accessible_getIndexInParent (CSPI_OBJREF (obj), cspi_ev ());
-  cspi_check_ev (cspi_ev (), "getIndexInParent");
+  long retval;
+
+  cspi_return_val_if_fail (obj != NULL, -1);
+
+  retval = (long)
+    Accessibility_Accessible_getIndexInParent (CSPI_OBJREF (obj), cspi_ev ());
+
+  cspi_return_val_if_ev ("getIndexInparent", -1); 
   return retval;
 }
 
@@ -244,28 +287,36 @@ Accessible_getIndexInParent (Accessible *obj)
  * Get the set of #AccessibleRelation objects which describe this #Accessible object's
  *       relationships with other #Accessible objects.
  *
- * Returns: an array of #AccessibleRelation pointers.
- *
+ * Returns: an array of #AccessibleRelation pointers. or NULL on exception
  **/
 AccessibleRelation **
 Accessible_getRelationSet (Accessible *obj)
 {
-  AccessibleRelation **relations;
-  int n_relations;
   int i;
-  Accessibility_RelationSet *relation_set =    
-         Accessibility_Accessible_getRelationSet (CSPI_OBJREF (obj), cspi_ev ());
+  int n_relations;
+  AccessibleRelation **relations;
+  Accessibility_RelationSet *relation_set;
+
+  cspi_return_val_if_fail (obj != NULL, NULL);
+
+  relation_set =
+    Accessibility_Accessible_getRelationSet (CSPI_OBJREF (obj), cspi_ev ());
+
+  cspi_return_val_if_ev ("getRelationSet", NULL); 
   
   /* this looks hack-ish, but it's based on the CORBA C bindings spec */
   n_relations = relation_set->_length;
   relations = malloc (sizeof (AccessibleRelation *) * n_relations);
   
-  for (i=0; i<n_relations; ++i)
+  for (i = 0; i < n_relations; ++i)
     {
-      relations[i] = cspi_object_add (relation_set->_buffer[i]);
+      relations[i] = cspi_object_add (CORBA_Object_duplicate (
+             relation_set->_buffer[i], cspi_ev ()));
     }
   relations[i] = CORBA_OBJECT_NIL;
 
+  CORBA_free (relation_set);
+
   return relations;
 }
 
@@ -278,12 +329,18 @@ Accessible_getRelationSet (Accessible *obj)
  * Returns: a UTF-8 string indicating the UI role of the #Accessible object.
  *
  **/
-char *
+const char *
 Accessible_getRole (Accessible *obj)
 {
-  char *retval = AccessibleRole_getName (
-                 Accessibility_Accessible_getRole (CSPI_OBJREF (obj), cspi_ev ()));
-  cspi_check_ev (cspi_ev (), "getRole");
+  const char *retval;
+
+  cspi_return_val_if_fail (obj != NULL, NULL);
+
+  retval = AccessibleRole_getName (
+    Accessibility_Accessible_getRole (CSPI_OBJREF (obj), cspi_ev ()));
+
+  cspi_return_val_if_ev ("getRole", NULL); 
+
   return retval;
 }
 
@@ -303,36 +360,6 @@ Accessible_getStateSet (Accessible *obj)
 
 /* Interface query methods */
 
-static boolean
-cspi_accessible_is_a (Accessible *obj,
-                    const char *interface_name)
-{
-  boolean        retval;
-  Bonobo_Unknown unknown;
-
-  unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (obj),
-                                          interface_name, cspi_ev ());
-
-  if (BONOBO_EX (cspi_ev ()))
-    {
-      g_error ("Exception '%s' checking if is '%s'",
-              bonobo_exception_get_text (cspi_ev ()),
-              interface_name);
-    }
-
-  if (unknown != CORBA_OBJECT_NIL)
-    {
-      retval = TRUE;
-      bonobo_object_release_unref (unknown, NULL);
-    }
-  else
-    {
-      retval= FALSE;
-    }
-
-  return retval;
-}
-
 /**
  * Accessible_isAction:
  * @obj: a pointer to the #Accessible instance to query.
@@ -342,7 +369,7 @@ cspi_accessible_is_a (Accessible *obj,
  * Returns: #TRUE if @obj implements the #AccessibleAction interface,
  *          #FALSE otherwise.
  **/
-boolean
+SPIBoolean
 Accessible_isAction (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
@@ -350,6 +377,22 @@ Accessible_isAction (Accessible *obj)
 }
 
 /**
+ * Accessible_isApplication:
+ * @obj: a pointer to the #Accessible instance to query.
+ *
+ * Query whether the specified #Accessible implements #AccessibleApplication.
+ *
+ * Returns: #TRUE if @obj implements the #AccessibleApplication interface,
+ *          #FALSE otherwise.
+ **/
+SPIBoolean
+Accessible_isApplication (Accessible *obj)
+{
+  return cspi_accessible_is_a (obj,
+                             "IDL:Accessibility/Application:1.0");
+}
+
+/**
  * Accessible_isComponent:
  * @obj: a pointer to the #Accessible instance to query.
  *
@@ -358,7 +401,7 @@ Accessible_isAction (Accessible *obj)
  * Returns: #TRUE if @obj implements the #AccessibleComponent interface,
  *          #FALSE otherwise.
  **/
-boolean
+SPIBoolean
 Accessible_isComponent (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
@@ -374,7 +417,7 @@ Accessible_isComponent (Accessible *obj)
  * Returns: #TRUE if @obj implements the #AccessibleEditableText interface,
  *          #FALSE otherwise.
  **/
-boolean
+SPIBoolean
 Accessible_isEditableText (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
@@ -390,7 +433,7 @@ Accessible_isEditableText (Accessible *obj)
  * Returns: #TRUE if @obj implements the #AccessibleHypertext interface,
  *          #FALSE otherwise.
  **/
-boolean
+SPIBoolean
 Accessible_isHypertext (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
@@ -406,7 +449,7 @@ Accessible_isHypertext (Accessible *obj)
  * Returns: #TRUE if @obj implements the #AccessibleImage interface,
  *          #FALSE otherwise.
 **/
-boolean
+SPIBoolean
 Accessible_isImage (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
@@ -422,7 +465,7 @@ Accessible_isImage (Accessible *obj)
  * Returns: #TRUE if @obj implements the #AccessibleSelection interface,
  *          #FALSE otherwise.
 **/
-boolean
+SPIBoolean
 Accessible_isSelection (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
@@ -438,7 +481,7 @@ Accessible_isSelection (Accessible *obj)
  * Returns: #TRUE if @obj implements the #AccessibleTable interface,
  *          #FALSE otherwise.
 **/
-boolean
+SPIBoolean
 Accessible_isTable (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
@@ -454,7 +497,7 @@ Accessible_isTable (Accessible *obj)
  * Returns: #TRUE if @obj implements the #AccessibleText interface,
  *          #FALSE otherwise.
 **/
-boolean
+SPIBoolean
 Accessible_isText (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
@@ -470,7 +513,7 @@ Accessible_isText (Accessible *obj)
  * Returns: #TRUE if @obj implements the #AccessibleValue interface,
  *          #FALSE otherwise.
 **/
-boolean
+SPIBoolean
 Accessible_isValue (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
@@ -478,6 +521,22 @@ Accessible_isValue (Accessible *obj)
 }
 
 /**
+ * Accessible_getApplication:
+ * @obj: a pointer to the #Accessible instance to query.
+ *
+ * Get the #AccessibleApplication interface for an #Accessible.
+ *
+ * Returns: a pointer to an #AccessibleApplication interface instance, or
+ *          NULL if @obj does not implement #AccessibleApplication.
+ **/
+AccessibleApplication *
+Accessible_getApplication (Accessible *obj)
+{
+  return (AccessibleApplication *) Accessible_queryInterface (
+         obj, "IDL:Accessibility/Application:1.0");
+}
+
+/**
  * Accessible_getAction:
  * @obj: a pointer to the #Accessible instance to query.
  *
@@ -645,14 +704,23 @@ Accessible_getValue (Accessible *obj)
  *
  **/
 GenericInterface *
-Accessible_queryInterface (Accessible *obj, char *interface_name)
+Accessible_queryInterface (Accessible *obj,
+                          const char *interface_name)
 {
   Bonobo_Unknown iface;
+  
+  if (!obj)
+    {
+      return NULL;
+    }
 
   iface = Accessibility_Accessible_queryInterface (CSPI_OBJREF (obj),
                                                   interface_name,
                                                   cspi_ev ());
 
+
+  cspi_return_val_if_ev ("queryInterface", NULL); 
+
   /*
    * FIXME: we need to be fairly sure that references are going
    * to mach up if we are going to expose QueryInterface, ie. we
@@ -673,13 +741,11 @@ Accessible_queryInterface (Accessible *obj, char *interface_name)
  * Increment the reference count for an #AccessibleRelation object.
  *
  * Returns: (no return code implemented yet).
- *
  **/
-int
+void
 AccessibleRelation_ref (AccessibleRelation *obj)
 {
   cspi_object_ref (obj);
-  return 0;
 }
 
 /**
@@ -689,13 +755,11 @@ AccessibleRelation_ref (AccessibleRelation *obj)
  * Decrement the reference count for an #AccessibleRelation object.
  *
  * Returns: (no return code implemented yet).
- *
  **/
-int
+void
 AccessibleRelation_unref (AccessibleRelation *obj)
 {
   cspi_object_unref (obj);
-  return 0;
 }
 
 /**
@@ -711,6 +775,7 @@ AccessibleRelation_unref (AccessibleRelation *obj)
 AccessibleRelationType
 AccessibleRelation_getRelationType (AccessibleRelation *obj)
 {
+  cspi_return_val_if_fail (obj != NULL, -1);
   return 0;
 }
 
@@ -729,6 +794,7 @@ AccessibleRelation_getRelationType (AccessibleRelation *obj)
 int
 AccessibleRelation_getNTargets (AccessibleRelation *obj)
 {
+  cspi_return_val_if_fail (obj != NULL, -1);
   return 0;
 }
 
@@ -747,6 +813,7 @@ AccessibleRelation_getNTargets (AccessibleRelation *obj)
 Accessible *
 AccessibleRelation_getTarget (AccessibleRelation *obj, int i)
 {
+  cspi_return_val_if_fail (obj != NULL, NULL);
   return NULL;
 }
 
@@ -759,11 +826,10 @@ AccessibleRelation_getTarget (AccessibleRelation *obj, int i)
  * Returns: (no return code implemented yet).
  *
  **/
-int
+void
 AccessibleStateSet_ref (AccessibleStateSet *obj)
 {
   cspi_object_ref (obj);
-  return 0;
 }
 
 /**
@@ -775,14 +841,12 @@ AccessibleStateSet_ref (AccessibleStateSet *obj)
  * Returns: (no return code implemented yet).
  *
  **/
-int
+void
 AccessibleStateSet_unref (AccessibleStateSet *obj)
 {
   cspi_object_unref (obj);
-  return 0;
 }
 
-
 /**
  * AccessibleStateSet_contains:
  * @obj: a pointer to the #AccessibleStateSet object on which to operate.
@@ -796,13 +860,20 @@ AccessibleStateSet_unref (AccessibleStateSet *obj)
  *          otherwise #FALSE.
  *
  **/
-boolean
+SPIBoolean
 AccessibleStateSet_contains (AccessibleStateSet *obj,
                             AccessibleState state)
 {
-  CORBA_boolean retval = Accessibility_StateSet_contains (CSPI_OBJREF (obj), state, cspi_ev ());
-  cspi_check_ev (cspi_ev (), "contains");
-  return (boolean) retval;
+  CORBA_boolean retval;
+
+  cspi_return_val_if_fail (obj != NULL, FALSE);
+
+  retval = Accessibility_StateSet_contains (CSPI_OBJREF (obj),
+                                           state, cspi_ev ());
+
+  cspi_return_val_if_ev ("contains", FALSE);
+
+  return (SPIBoolean) retval;
 }
 
 /**
@@ -818,11 +889,12 @@ void
 AccessibleStateSet_add (AccessibleStateSet *obj,
                        AccessibleState state)
 {
+  cspi_return_if_fail (obj != NULL);
+
   Accessibility_StateSet_add (CSPI_OBJREF (obj), state, cspi_ev ());
-  cspi_check_ev (cspi_ev (), "contains");
+  cspi_check_ev ("add");
 }
 
-
 /**
  * AccessibleStateSet_remove:
  * @obj: a pointer to the #AccessibleStateSet object on which to operate.
@@ -836,8 +908,10 @@ void
 AccessibleStateSet_remove (AccessibleStateSet *obj,
                           AccessibleState state)
 {
+  cspi_return_if_fail (obj != NULL);
+
   Accessibility_StateSet_remove (CSPI_OBJREF (obj), state, cspi_ev ());
-  cspi_check_ev (cspi_ev (), "contains");
+  cspi_check_ev ("remove");
 }
 
 /**
@@ -855,22 +929,29 @@ AccessibleStateSet_remove (AccessibleStateSet *obj,
  *          otherwise #FALSE.
  *
  **/
-boolean
+SPIBoolean
 AccessibleStateSet_equals (AccessibleStateSet *obj,
                            AccessibleStateSet *obj2)
 {
-  return Accessibility_StateSet_equals (CSPI_OBJREF (obj), CSPI_OBJREF (obj2), cspi_ev ());
+  if (obj == obj2)
+    {
+      return TRUE;
+    }
+
+  cspi_return_val_if_fail (obj != NULL, FALSE);
+  cspi_return_val_if_fail (obj2 != NULL, FALSE);
+
+  return Accessibility_StateSet_equals (CSPI_OBJREF (obj),
+                                       CSPI_OBJREF (obj2), cspi_ev ());
 }
 
 /**
  * AccessibleStateSet_compare:
  * @obj: a pointer to the first #AccessibleStateSet object on which to operate.
  * @obj2: a pointer to the second #AccessibleStateSet object on which to operate.
- * @differenceSet: a pointer to an array of #AccessibleStates, which is set when the
- *        fuction returns to point to an array of states representing the states which
- *        the two state sets do not have in common.
  *
  * Determine the differences between two instances of #AccessibleStateSet.
+ * Not Yet Implemented.
  *.
  * @see AccessibleStateSet_equals().
  *
@@ -878,11 +959,14 @@ AccessibleStateSet_equals (AccessibleStateSet *obj,
  *          the two sets but not the other.
  *
  **/
-void
+AccessibleStateSet *
 AccessibleStateSet_compare (AccessibleStateSet *obj,
-                            AccessibleStateSet *obj2,
-                            AccessibleStateSet **differenceSet);
-
+                            AccessibleStateSet *obj2)
+{
+  cspi_return_val_if_fail (obj != NULL, NULL);
+  cspi_return_val_if_fail (obj2 != NULL, NULL);
+  return NULL; 
+}
 
 /**
  * AccessibleStateSet_isEmpty:
@@ -894,13 +978,12 @@ AccessibleStateSet_compare (AccessibleStateSet *obj,
  *          otherwise #FALSE.
  *
  **/
-boolean
+SPIBoolean
 AccessibleStateSet_isEmpty (AccessibleStateSet *obj)
 {
+  cspi_return_val_if_fail (obj != NULL, FALSE);
   return TRUE; 
   /*  return Accessibility_StateSet_isEmpty (CSPI_OBJREF (obj), cspi_ev ());*/
 }
 
 
-
-