/modules/other/gail/tests/testselection.c

https://github.com/garnacho/gtk-mpx · C · 198 lines · 155 code · 21 blank · 22 comment · 27 complexity · a9da26bf13046fb8170397dcb1387b54 MD5 · raw file

  1. #include <string.h>
  2. #include <atk/atk.h>
  3. #include <gtk/gtk.h>
  4. /*
  5. * This module tests the selection interface on menu items.
  6. * To use this module run the test program testgtk and use the menus
  7. * option.
  8. */
  9. static void _do_selection (AtkObject *obj);
  10. static gint _finish_selection (gpointer data);
  11. static AtkObject* _find_object (AtkObject* obj, AtkRole role);
  12. static void _print_type (AtkObject *obj);
  13. static AtkObject*
  14. _find_object (AtkObject *obj,
  15. AtkRole role)
  16. {
  17. /*
  18. * Find the first object which is a descendant of the specified object
  19. * which matches the specified role.
  20. *
  21. * This function returns a reference to the AtkObject which should be
  22. * removed when finished with the object.
  23. */
  24. gint i;
  25. gint n_children;
  26. AtkObject *child;
  27. n_children = atk_object_get_n_accessible_children (obj);
  28. for (i = 0; i < n_children; i++)
  29. {
  30. AtkObject* found_obj;
  31. child = atk_object_ref_accessible_child (obj, i);
  32. if (atk_object_get_role (child) == role)
  33. {
  34. return child;
  35. }
  36. found_obj = _find_object (child, role);
  37. g_object_unref (child);
  38. if (found_obj)
  39. {
  40. return found_obj;
  41. }
  42. }
  43. return NULL;
  44. }
  45. static void _print_type (AtkObject *obj)
  46. {
  47. G_CONST_RETURN gchar * typename = NULL;
  48. G_CONST_RETURN gchar * name = NULL;
  49. AtkRole role;
  50. if (GTK_IS_ACCESSIBLE (obj))
  51. {
  52. GtkWidget* widget = NULL;
  53. widget = GTK_ACCESSIBLE (obj)->widget;
  54. typename = g_type_name (GTK_OBJECT_TYPE (widget));
  55. g_print ("Widget type name: %s\n", typename ? typename : "NULL");
  56. }
  57. typename = g_type_name (G_OBJECT_TYPE (obj));
  58. g_print ("Accessible type name: %s\n", typename ? typename : "NULL");
  59. name = atk_object_get_name (obj);
  60. g_print("Accessible Name: %s\n", (name) ? name : "NULL");
  61. role = atk_object_get_role(obj);
  62. g_print ("Accessible Role: %d\n", role);
  63. }
  64. static void
  65. _do_selection (AtkObject *obj)
  66. {
  67. gint i;
  68. AtkObject *selected;
  69. AtkRole role;
  70. AtkObject *selection_obj;
  71. role = atk_object_get_role (obj);
  72. if ((role == ATK_ROLE_FRAME) &&
  73. (strcmp (atk_object_get_name (obj), "menus") == 0))
  74. {
  75. selection_obj = _find_object (obj, ATK_ROLE_MENU_BAR);
  76. if (selection_obj)
  77. {
  78. g_object_unref (selection_obj);
  79. }
  80. }
  81. else if (role == ATK_ROLE_COMBO_BOX)
  82. {
  83. selection_obj = obj;
  84. }
  85. else
  86. return;
  87. g_print ("*** Start do_selection ***\n");
  88. if (!selection_obj)
  89. {
  90. g_print ("no selection_obj\n");
  91. return;
  92. }
  93. i = atk_selection_get_selection_count (ATK_SELECTION (selection_obj));
  94. if (i != 0)
  95. {
  96. for (i = 0; i < atk_object_get_n_accessible_children (selection_obj); i++)
  97. {
  98. if (atk_selection_is_child_selected (ATK_SELECTION (selection_obj), i))
  99. {
  100. g_print ("%d child selected\n", i);
  101. }
  102. else
  103. {
  104. g_print ("%d child not selected\n", i);
  105. }
  106. }
  107. }
  108. /*
  109. * Should not be able to select all items on a menu bar
  110. */
  111. atk_selection_select_all_selection (ATK_SELECTION (selection_obj));
  112. i = atk_selection_get_selection_count (ATK_SELECTION (selection_obj));
  113. if ( i != 0)
  114. {
  115. g_print ("Unexpected selection count: %d, expected 0\n", i);
  116. return;
  117. }
  118. /*
  119. * There should not be any items selected
  120. */
  121. selected = atk_selection_ref_selection (ATK_SELECTION (selection_obj), 0);
  122. if ( selected != NULL)
  123. {
  124. g_print ("Unexpected selection: %d, expected 0\n", i);
  125. }
  126. atk_selection_add_selection (ATK_SELECTION (selection_obj), 1);
  127. g_timeout_add (2000, _finish_selection, selection_obj);
  128. g_print ("*** End _do_selection ***\n");
  129. }
  130. static gint _finish_selection (gpointer data)
  131. {
  132. AtkObject *obj = ATK_OBJECT (data);
  133. AtkObject *selected;
  134. gint i;
  135. gboolean is_selected;
  136. g_print ("*** Start Finish selection ***\n");
  137. /*
  138. * If being run for for menus, at this point menu item foo should be
  139. * selected which means that its submenu should be visible.
  140. */
  141. i = atk_selection_get_selection_count (ATK_SELECTION (obj));
  142. if (i != 1)
  143. {
  144. g_print ("Unexpected selection count: %d, expected 1\n", i);
  145. return FALSE;
  146. }
  147. selected = atk_selection_ref_selection (ATK_SELECTION (obj), 0);
  148. g_return_val_if_fail (selected != NULL, FALSE);
  149. g_print ("*** Selected Item ***\n");
  150. _print_type (selected);
  151. g_object_unref (selected);
  152. is_selected = atk_selection_is_child_selected (ATK_SELECTION (obj), 1);
  153. g_return_val_if_fail (is_selected, FALSE);
  154. is_selected = atk_selection_is_child_selected (ATK_SELECTION (obj), 0);
  155. g_return_val_if_fail (!is_selected, FALSE);
  156. selected = atk_selection_ref_selection (ATK_SELECTION (obj), 1);
  157. g_return_val_if_fail (selected == NULL, FALSE);
  158. atk_selection_remove_selection (ATK_SELECTION (obj), 0);
  159. i = atk_selection_get_selection_count (ATK_SELECTION (obj));
  160. g_return_val_if_fail (i == 0, FALSE);
  161. selected = atk_selection_ref_selection (ATK_SELECTION (obj), 0);
  162. g_return_val_if_fail (selected == NULL, FALSE);
  163. g_print ("*** End Finish selection ***\n");
  164. return FALSE;
  165. }
  166. static void
  167. _create_event_watcher (void)
  168. {
  169. atk_add_focus_tracker (_do_selection);
  170. }
  171. int
  172. gtk_module_init(gint argc, char* argv[])
  173. {
  174. g_print("testselection Module loaded\n");
  175. _create_event_watcher();
  176. return 0;
  177. }