test_models.mod 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. MODULE test_models ;
  2. (*
  3. m2-GTK4 test - filter and sort models.
  4. Wraps a GListStore of strings with a GtkCustomFilter (via
  5. GtkFilterListModel) and a GtkCustomSorter (via GtkSortListModel).
  6. The match/compare callbacks are plain Modula-2 procedures. Needs no
  7. display.
  8. *)
  9. FROM GListStore IMPORT g_list_store_new, g_list_store_append;
  10. FROM GtkStringObject IMPORT gtk_string_object_new,
  11. gtk_string_object_get_string, gtk_string_object_get_type;
  12. FROM GListModel IMPORT g_list_model_get_n_items, g_list_model_get_item;
  13. FROM GtkCustomFilter IMPORT gtk_custom_filter_new;
  14. FROM GtkFilterListModel IMPORT gtk_filter_list_model_new,
  15. gtk_filter_list_model_set_incremental, gtk_filter_list_model_get_filter;
  16. FROM GtkCustomSorter IMPORT gtk_custom_sorter_new;
  17. FROM GtkSortListModel IMPORT gtk_sort_list_model_new,
  18. gtk_sort_list_model_get_sorter;
  19. FROM GObject IMPORT g_object_ref, g_object_unref;
  20. FROM GtkUtils IMPORT CStrToM2;
  21. FROM Gtk IMPORT gtk_init;
  22. FROM GLib IMPORT g_strcmp0;
  23. FROM SYSTEM IMPORT ADDRESS, ADR;
  24. FROM libc IMPORT strncpy, printf;
  25. VAR
  26. failed: BOOLEAN;
  27. PROCEDURE Check (condition: BOOLEAN; what: ARRAY OF CHAR);
  28. BEGIN
  29. IF NOT condition THEN
  30. printf("FAIL: %s\n", what);
  31. failed := TRUE
  32. END
  33. END Check;
  34. (* gboolean match(gpointer item, gpointer user_data): keep names with 'l'. *)
  35. PROCEDURE Match (item: ADDRESS; user: ADDRESS) : INTEGER;
  36. VAR
  37. src: ADDRESS;
  38. buf: ARRAY [0..63] OF CHAR;
  39. i: CARDINAL;
  40. found: BOOLEAN;
  41. BEGIN
  42. src := gtk_string_object_get_string(item);
  43. strncpy(ADR(buf), src, HIGH(buf) + 1);
  44. buf[HIGH(buf)] := 0C;
  45. found := FALSE;
  46. i := 0;
  47. WHILE (i < HIGH(buf)) AND (buf[i] # 0C) DO
  48. IF (buf[i] = 'l') OR (buf[i] = 'L') THEN found := TRUE END;
  49. INC(i)
  50. END;
  51. IF found THEN RETURN 1 ELSE RETURN 0 END
  52. END Match;
  53. (* int compare(gconstpointer a, gconstpointer b, gpointer user_data) *)
  54. PROCEDURE Compare (a, b, user: ADDRESS) : INTEGER;
  55. BEGIN
  56. RETURN g_strcmp0(gtk_string_object_get_string(a),
  57. gtk_string_object_get_string(b))
  58. END Compare;
  59. VAR
  60. store, filter, filterModel, sorter, sortModel, item: ADDRESS;
  61. buf: ARRAY [0..63] OF CHAR;
  62. BEGIN
  63. failed := FALSE;
  64. gtk_init(); (* GTK model types need the GTK type system initialised *)
  65. store := g_list_store_new(gtk_string_object_get_type());
  66. g_list_store_append(store, gtk_string_object_new("Alpha"));
  67. g_list_store_append(store, gtk_string_object_new("Beta"));
  68. g_list_store_append(store, gtk_string_object_new("Gamma"));
  69. g_list_store_append(store, gtk_string_object_new("Delta"));
  70. (* filter: only "Alpha" and "Delta" contain 'l'.
  71. gtk_filter_list_model_new takes ownership of model and filter. *)
  72. filter := gtk_custom_filter_new(ADR(Match), NIL, NIL);
  73. filterModel := gtk_filter_list_model_new(g_object_ref(store), filter);
  74. gtk_filter_list_model_set_incremental(filterModel, 0);
  75. Check(gtk_filter_list_model_get_filter(filterModel) = filter,
  76. "filter list model filter");
  77. Check(g_list_model_get_n_items(filterModel) = 2, "filtered count");
  78. (* sort: ascending by string; first item must be "Alpha".
  79. gtk_sort_list_model_new takes ownership of model and sorter. *)
  80. sorter := gtk_custom_sorter_new(ADR(Compare), NIL, NIL);
  81. sortModel := gtk_sort_list_model_new(g_object_ref(store), sorter);
  82. Check(gtk_sort_list_model_get_sorter(sortModel) = sorter,
  83. "sort list model sorter");
  84. Check(g_list_model_get_n_items(sortModel) = 4, "sorted count");
  85. item := g_list_model_get_item(sortModel, 0);
  86. CStrToM2(gtk_string_object_get_string(item), buf);
  87. Check(buf[0] = 'A', "sorted first item is Alpha");
  88. g_object_unref(item);
  89. (* filter and sorter were consumed by the models above *)
  90. g_object_unref(filterModel);
  91. g_object_unref(sortModel);
  92. g_object_unref(store);
  93. IF failed THEN
  94. printf("test_models: FAIL\n");
  95. HALT(1)
  96. ELSE
  97. printf("test_models: PASS\n");
  98. HALT(0)
  99. END
  100. END test_models.