test_shim3.mod 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. MODULE test_shim3 ;
  2. (*
  3. m2-GTK4 test - the shim's custom GtkLayoutManager.
  4. Installs a Modula-2 layout manager on a parent widget, adds two
  5. children, and checks (from a timeout) that the allocate callback ran
  6. and positioned them. Requires a display.
  7. *)
  8. FROM Gio IMPORT g_application_run, g_application_quit;
  9. FROM GtkApplication IMPORT gtk_application_new;
  10. FROM GtkWindow IMPORT gtk_application_window_new, gtk_window_set_child,
  11. gtk_window_present;
  12. FROM GtkLabel IMPORT gtk_label_new;
  13. FROM GtkWidget IMPORT gtk_widget_set_parent, gtk_widget_get_first_child,
  14. gtk_widget_get_next_sibling, gtk_widget_set_layout_manager,
  15. gtk_widget_get_width;
  16. FROM M2GtkShim IMPORT m2_gtk_widget_new, m2_layout_manager_new,
  17. m2_layout_allocate_child;
  18. FROM GLib IMPORT g_timeout_add, GSourceRemove;
  19. FROM GtkUtils IMPORT Connect;
  20. FROM SYSTEM IMPORT ADDRESS, ADR;
  21. FROM libc IMPORT printf;
  22. CONST
  23. AppId = "org.example.m2gtk4.TestShim3";
  24. VAR
  25. app, child1: ADDRESS;
  26. passed, allocated: BOOLEAN;
  27. (* PROCEDURE (widget, orientation, for_size; VAR minimum, natural; user) *)
  28. PROCEDURE Measure (widget: ADDRESS; orientation: INTEGER; for_size: INTEGER;
  29. VAR minimum, natural: INTEGER; user: ADDRESS);
  30. BEGIN
  31. minimum := 200;
  32. natural := 200
  33. END Measure;
  34. (* PROCEDURE (widget, width, height, baseline, user) *)
  35. PROCEDURE Allocate (widget: ADDRESS; width, height, baseline: INTEGER;
  36. user: ADDRESS);
  37. VAR
  38. child: ADDRESS;
  39. i: INTEGER;
  40. BEGIN
  41. allocated := TRUE;
  42. i := 0;
  43. child := gtk_widget_get_first_child(widget);
  44. WHILE child # NIL DO
  45. m2_layout_allocate_child(child, 0, i * 30, width, 30);
  46. INC(i);
  47. child := gtk_widget_get_next_sibling(child)
  48. END
  49. END Allocate;
  50. PROCEDURE AfterMap (data: ADDRESS) : INTEGER;
  51. BEGIN
  52. IF NOT allocated THEN
  53. printf("allocate callback did not run\n");
  54. passed := FALSE
  55. END;
  56. IF (child1 # NIL) AND (gtk_widget_get_width(child1) <= 0) THEN
  57. printf("child was not allocated\n");
  58. passed := FALSE
  59. END;
  60. g_application_quit(app);
  61. RETURN GSourceRemove
  62. END AfterMap;
  63. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  64. VAR
  65. window, parent, manager: ADDRESS;
  66. ok: BOOLEAN;
  67. BEGIN
  68. ok := TRUE;
  69. allocated := FALSE;
  70. parent := m2_gtk_widget_new(NIL, NIL, NIL);
  71. manager := m2_layout_manager_new(ADR(Measure), ADR(Allocate), NIL, NIL);
  72. gtk_widget_set_layout_manager(parent, manager);
  73. child1 := gtk_label_new("one");
  74. gtk_widget_set_parent(child1, parent);
  75. gtk_widget_set_parent(gtk_label_new("two"), parent);
  76. window := gtk_application_window_new(application);
  77. gtk_window_set_child(window, parent);
  78. gtk_window_present(window);
  79. passed := ok;
  80. g_timeout_add(300, AfterMap, NIL)
  81. END OnActivate;
  82. VAR
  83. rc: INTEGER;
  84. BEGIN
  85. passed := FALSE;
  86. app := gtk_application_new(AppId, 0);
  87. IF app = NIL THEN
  88. printf("test_shim3: FAIL (no app)\n");
  89. HALT(1)
  90. END;
  91. Connect(app, "activate", ADR(OnActivate), NIL);
  92. rc := g_application_run(app, 0, NIL);
  93. IF passed THEN
  94. printf("test_shim3: PASS\n");
  95. HALT(0)
  96. ELSE
  97. printf("test_shim3: FAIL\n");
  98. HALT(1)
  99. END
  100. END test_shim3.