Index: libspi/collection.c
===================================================================
--- libspi/collection.c	(revision 982)
+++ libspi/collection.c	(working copy)
@@ -157,8 +157,7 @@
      mrp =  spimatchrule->_mrp;
      
      CORBA_free (mrp->attributes);
-     CORBA_free (mrp->roles);
-     CORBA_free (mrp->interfaces);
+     CORBA_free (mrp->roles);     CORBA_free (mrp->interfaces);
 
      g_free (mrp);
 
@@ -560,12 +559,11 @@
 		      Accessibility_Accessible obj, glong index, gboolean flag, 
 		      Accessibility_Accessible pobj, CORBA_boolean recurse, 
 		      CORBA_boolean traverse, CORBA_Environment *ev){
-
      gint i = index;
      glong acount  = Accessibility_Accessible__get_childCount (obj, ev);
      gboolean prev = pobj? TRUE : FALSE;
-   
-     for (; i < acount && (max == 0 || kount < max); i++){
+     
+     for (; i < acount &&  (max == 0 || kount < max); i++){
 	  Accessibility_Accessible child = Accessibility_Accessible_getChildAtIndex (obj, i, ev);
 
 	          
@@ -575,7 +573,8 @@
 
 	  }
 	 
-	  if (flag  && match_interfaces_lookup (child, mrp, ev) && match_states_lookup (child, mrp, ev)     
+	  if (flag  && match_interfaces_lookup (child, mrp, ev) 
+                    && match_states_lookup (child, mrp, ev)     
                     && match_roles_lookup (child, mrp, ev)  
 		    && match_attributes_lookup (child, mrp, ev)
 	      	    ){
@@ -595,6 +594,69 @@
      return kount;
 } 
 
+static int 
+sort_order_rev_canonical (MatchRulePrivate *mrp, GList *ls, 		      
+		      gint kount, gint max,
+		      Accessibility_Accessible obj, gboolean flag, 
+		      Accessibility_Accessible pobj, CORBA_Environment *ev){
+    Accessibility_Accessible nextobj;
+    Accessibility_Accessible parent;
+    glong indexinparent;
+
+    // This breaks us out of the recursion.  
+    // todo: might need to restrict the bounds of the "Collection"
+    if (obj == CORBA_OBJECT_NIL 
+            || CORBA_Object_is_equivalent (obj, pobj, ev))
+    {
+        return kount;           
+    } 
+	 
+    // Add to the list if it matches
+    if (flag && match_interfaces_lookup (obj, mrp, ev) 
+               && match_states_lookup (obj, mrp, ev)     
+               && match_roles_lookup (obj, mrp, ev)  
+               && match_attributes_lookup (obj, mrp, ev))
+    {
+         ls = g_list_append (ls, obj);
+         kount++;
+    }
+
+    if(!flag) flag = TRUE;
+
+    // Get the current nodes index in it's parent and the parent object.
+    indexinparent = Accessibility_Accessible_getIndexInParent (obj, ev);
+    parent = Accessibility_Accessible__get_parent (obj, ev);
+
+    if(indexinparent > 0)
+    {
+         // there are still some siblings to visit so get the previous sibling
+         // and get it's last descendant.
+         // First, get the previous sibling 
+         nextobj = Accessibility_Accessible_getChildAtIndex (parent, 
+                                                             indexinparent-1, 
+                                                             ev);
+
+         // Now, drill down the right side to the last descendant
+         while(Accessibility_Accessible__get_childCount (nextobj, ev) > 0)
+         {
+              nextobj = Accessibility_Accessible_getChildAtIndex (nextobj, 
+                   Accessibility_Accessible__get_childCount (nextobj, ev)-1, ev);
+
+         } 
+         // recurse with the last descendant
+         kount = sort_order_rev_canonical (mrp, ls,  kount, max, 
+                                       nextobj, TRUE, pobj, ev);
+    } 
+    else
+    {
+         // no more siblings so next node must be the parent
+         kount = sort_order_rev_canonical (mrp, ls,  kount, max, 
+                                       parent, TRUE, pobj, ev);
+
+    }
+    return kount;
+} 
+
 static int
 query_exec (MatchRulePrivate *mrp,  Accessibility_Collection_SortOrder sortby, 
 	    GList *ls, gint kount, gint max, 
@@ -638,15 +700,21 @@
      return retval;
 }
 
+/*
+  GetMatchesTo es normal segun API. Podria usarse tambien para anterior cuando ya recorremos el arbol normalmente.
+  Este solamente sirve para encontrar los siguientes nodos a partir de current_object teniendo este como tope,
+  o siguientes matches tomando como marco el parent del current_object
+*/
+
 static Accessibility_AccessibleSet *
-impl_getMatchesFrom (PortableServer_Servant servant,
-		      const Accessibility_Accessible current_object,
-		      const Accessibility_MatchRule rule,
-		      const Accessibility_Collection_SortOrder sortby,
-		      const CORBA_boolean isrestrict,
-		      CORBA_long  count,
-		      const CORBA_boolean traverse,
-		      CORBA_Environment *ev){
+getMatchesFrom (PortableServer_Servant servant,
+		     const Accessibility_Accessible current_object,
+		     const Accessibility_MatchRule rule,
+		     const Accessibility_Collection_SortOrder sortby,
+		     const CORBA_boolean isrestrict,
+		     CORBA_long  count,
+		     const CORBA_boolean traverse,
+		     CORBA_Environment *ev){
     
      GList *ls = NULL;
      Accessibility_Accessible parent; 
@@ -654,6 +722,7 @@
      glong index = Accessibility_Accessible_getIndexInParent (current_object, ev);
      gint kount = 0;
 
+
      ls = g_list_append (ls, current_object);
      mrp =  get_collection_from_servant (servant)->_mrp;;
           
@@ -675,33 +744,148 @@
 }
 
 
+/*
+  inorder traversal from a given object in the hierarchy
+*/
+
+static int
+inorder (Accessibility_Accessible collection, MatchRulePrivate *mrp, 
+	GList *ls, gint kount, gint max,
+	Accessibility_Accessible obj, 
+	gboolean flag,
+	Accessibility_Accessible pobj,
+	CORBA_boolean traverse,
+	CORBA_Environment *ev){
+
+  int i = 0;
+  
+  // First, look through the children recursively.
+  kount = sort_order_canonical (mrp, ls, kount, max, obj, 0, TRUE,
+				CORBA_OBJECT_NIL, TRUE, TRUE, ev); 
+  
+  // Next, we look through the right subtree
+  while ((max == 0 || kount < max) 
+          && ! CORBA_Object_is_equivalent (obj, collection, ev))
+  {
+
+    i = Accessibility_Accessible_getIndexInParent (obj, ev);
+    Accessibility_Accessible parent =  
+                                Accessibility_Accessible__get_parent (obj, ev);
+    kount  = sort_order_canonical (mrp, ls, kount, max, parent, 
+                                   i+1, TRUE, FALSE, TRUE, TRUE, ev);
+    obj = parent;
+  }
+
+  // unknown function ???
+  if (kount < max)
+  {
+     kount = sort_order_canonical (mrp, ls, kount, max, 
+                                   obj, i + 1, TRUE, FALSE, 
+                                   TRUE, TRUE, ev);
+  }
+
+  return kount;
+}
+
+/*
+  GetMatchesInOrder: get matches from a given object in an inorder traversal.
+*/
+
 static Accessibility_AccessibleSet *
-impl_getMatchesTo (PortableServer_Servant servant,
-		      const Accessibility_Accessible current_object,
-		      const Accessibility_MatchRule rule,
-		      const Accessibility_Collection_SortOrder sortby,
-		      const CORBA_boolean recurse, 
-		      CORBA_long  count,
-		      const CORBA_boolean traverse,
-		      CORBA_Environment *ev){
+getMatchesInOrder (PortableServer_Servant servant,
+		   const Accessibility_Accessible current_object,
+		   const Accessibility_MatchRule rule,
+		   const Accessibility_Collection_SortOrder sortby,
+		   const CORBA_boolean recurse,
+		   CORBA_long count,
+		   const CORBA_boolean traverse, 
+		   CORBA_Environment *ev){
+  GList *ls = NULL;
+  AtkObject *aobj;
+  Accessibility_Accessible obj, collection;
+  MatchRulePrivate *mrp;
+  gint kount = 0;
 
+  ls = g_list_append (ls, current_object);
+  mrp = get_collection_from_servant (servant)->_mrp;
 
+  aobj = get_atkobject_from_servant (servant);
+  obj = spi_accessible_new_return (aobj, FALSE, ev);
+  collection = obj;
+  
+  kount = inorder (collection, mrp, ls, 0, count, 
+                   current_object, TRUE, CORBA_OBJECT_NIL, traverse, ev);
+
+  ls = g_list_next (ls);
+
+  if (sortby == Accessibility_Collection_SORT_ORDER_REVERSE_CANONICAL)
+    ls = g_list_reverse (ls);
+
+  return _accessible_list_to_set (ls, kount); 
+}
+
+/*
+  GetMatchesInOrder: get matches from a given object in an inorder traversal.
+*/
+
+static Accessibility_AccessibleSet *
+getMatchesInBackOrder (PortableServer_Servant servant,
+		   const Accessibility_Accessible current_object,
+		   const Accessibility_MatchRule rule,
+		   const Accessibility_Collection_SortOrder sortby,
+		   CORBA_long count,
+		   CORBA_Environment *ev){
   GList *ls = NULL;
   AtkObject *aobj;
+  Accessibility_Accessible collection;
+  MatchRulePrivate *mrp;
+  gint kount = 0;
+
+  ls = g_list_append (ls, current_object);
+  mrp = get_collection_from_servant (servant)->_mrp;
+
+  aobj = get_atkobject_from_servant (servant);
+  collection = spi_accessible_new_return (aobj, FALSE, ev);
+
+  kount = sort_order_rev_canonical (mrp, ls, 0, count, current_object, 
+                                   FALSE, collection, ev);
+
+  ls = g_list_next (ls);
+
+  if (sortby == Accessibility_Collection_SORT_ORDER_REVERSE_CANONICAL)
+    ls = g_list_reverse (ls);
+
+  return _accessible_list_to_set (ls, kount); 
+}
+
+
+static Accessibility_AccessibleSet *
+getMatchesTo (PortableServer_Servant servant,
+	      const Accessibility_Accessible current_object,
+	      const Accessibility_MatchRule rule,
+	      const Accessibility_Collection_SortOrder sortby,
+	      const CORBA_boolean recurse, 
+	      const CORBA_boolean isrestrict,
+	      CORBA_long  count,
+	      const CORBA_boolean traverse,
+	      CORBA_Environment *ev){
+
+
+  GList *ls = NULL;
+  AtkObject *aobj;
   Accessibility_Accessible obj;
   MatchRulePrivate *mrp;
-   gint kount = 0;
+  gint kount = 0;
 
 
   ls = g_list_append (ls, current_object); 
   mrp =  get_collection_from_servant (servant)->_mrp;
-  
-  
+    
   if (recurse){
     obj = Accessibility_Accessible__get_parent (current_object, ev);
     kount =  query_exec (mrp,  sortby, ls, 0, count,  obj, 0, TRUE, current_object, TRUE, traverse, ev);
   }
-  else{
+  else{ 
     aobj = get_atkobject_from_servant (servant);
     obj = spi_accessible_new_return (aobj, FALSE, ev);
     kount = query_exec (mrp,  sortby, ls, 0, count, obj, 0, TRUE, current_object, TRUE, traverse, ev); 
@@ -710,16 +894,60 @@
 
   ls = g_list_next (ls); 
    
-  if (sortby == Accessibility_Collection_SORT_ORDER_REVERSE_CANONICAL)
+  if (sortby != Accessibility_Collection_SORT_ORDER_REVERSE_CANONICAL)
     ls = g_list_reverse (ls);
  
   return  _accessible_list_to_set (ls, kount);
   
 }
 
+static Accessibility_AccessibleSet *
+impl_getMatchesFrom (PortableServer_Servant servant,
+		    const Accessibility_Accessible current_object,
+		    const Accessibility_MatchRule rule,
+		    const Accessibility_Collection_SortOrder sortby,
+		    const Accessibility_Collection_TreeTraversalType tree,
+		    CORBA_long  count,
+		    const CORBA_boolean traverse,
+		    CORBA_Environment *ev){
 
+  switch (tree){
+  case Accessibility_Collection_TREE_RESTRICT_CHILDREN : 
+    return getMatchesFrom (servant, current_object, rule, sortby, TRUE, count, traverse, ev); break;
+  case Accessibility_Collection_TREE_RESTRICT_SIBLING :
+    return getMatchesFrom (servant, current_object, rule, sortby, FALSE, count, traverse, ev); break;
+  case Accessibility_Collection_TREE_INORDER :
+    return getMatchesInOrder (servant, current_object, rule, sortby, TRUE, count, traverse, ev); break;
+  default : return CORBA_OBJECT_NIL;
+  }
+}
 
 static Accessibility_AccessibleSet *
+impl_getMatchesTo (PortableServer_Servant servant,
+		   const Accessibility_Accessible current_object,
+		   const Accessibility_MatchRule rule,
+		   const Accessibility_Collection_SortOrder sortby,
+		   const Accessibility_Collection_TreeTraversalType tree,
+		   const CORBA_boolean recurse,
+		   CORBA_long  count,
+		   const CORBA_boolean traverse,
+		   CORBA_Environment *ev){
+
+  switch (tree){
+  case Accessibility_Collection_TREE_RESTRICT_CHILDREN : 
+    return getMatchesTo (servant, current_object, rule, sortby, recurse, TRUE, count, traverse, ev); break;
+  case Accessibility_Collection_TREE_RESTRICT_SIBLING :
+    return getMatchesTo (servant, current_object, rule, sortby, recurse, FALSE, count, traverse, ev); break;
+  case Accessibility_Collection_TREE_INORDER :
+    return getMatchesInBackOrder (servant, current_object, rule, sortby, count, ev); break;
+  default : return CORBA_OBJECT_NIL;
+  }
+}
+
+
+
+
+static Accessibility_AccessibleSet *
 impl_getMatches (PortableServer_Servant servant,
 		 const Accessibility_MatchRule rule,
 		 const Accessibility_Collection_SortOrder sortby,
Index: idl/Accessibility_Collection.idl
===================================================================
--- idl/Accessibility_Collection.idl	(revision 982)
+++ idl/Accessibility_Collection.idl	(working copy)
@@ -55,6 +55,13 @@
            MATCH_LAST_DEFINED
 	   };
 
+       enum TreeTraversalType {
+	 
+	 TREE_RESTRICT_CHILDREN,
+	 TREE_RESTRICT_SIBLING,
+	 TREE_INORDER,
+	 TREE_LAST_DEFINED
+       };
 
 	boolean isAncestorOf (in Accessible object); 
   
@@ -74,21 +81,22 @@
                                   in SortOrder sortby,
 				  in long count,
                                   in boolean traverse); 
-
+       
 	AccessibleSet getMatchesTo (in Accessible current_object,
-                                           in MatchRule rule, 
-                                           in SortOrder sortby, 
-                                           in boolean recurse,
-                                           in long count,
-                                           in boolean traverse);
+				    in MatchRule rule, 
+				    in SortOrder sortby,
+				    in TreeTraversalType tree,
+				    in boolean recurse,
+				    in long count,
+				    in boolean traverse);
 
 
         AccessibleSet getMatchesFrom (in Accessible current_object,
-				       in MatchRule rule, 
-                                       in SortOrder sortby, 
-                                       in boolean isrestrict,
-                                       in long count,
-                                       in boolean traverse);
+				      in MatchRule rule, 
+				      in SortOrder sortby, 
+				      in TreeTraversalType tree,
+				      in long count,
+				      in boolean traverse);
 
         Accessible getActiveDescendant ();
         
