test_shim.mod 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. MODULE test_shim ;
  2. (*
  3. m2-GTK4 test - the C subclassing shim (M2GtkShim).
  4. Creates a custom GtkWidget and a custom GdkPaintable, each drawing
  5. through a Modula-2 Cairo callback, presents them and checks (from a
  6. timeout) that both draw callbacks ran. 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 GtkBox IMPORT gtk_box_new, gtk_box_append;
  13. FROM GtkEnums IMPORT GtkVertical;
  14. FROM GtkWidget IMPORT gtk_widget_set_size_request;
  15. FROM GtkPicture IMPORT gtk_picture_new, gtk_picture_set_paintable,
  16. gtk_picture_get_paintable;
  17. FROM M2GtkShim IMPORT m2_gtk_widget_new, m2_paintable_new;
  18. FROM Cairo IMPORT cairo_set_source_rgb, cairo_rectangle, cairo_fill;
  19. FROM GLib IMPORT g_timeout_add, GSourceRemove;
  20. FROM GtkUtils IMPORT Connect;
  21. FROM SYSTEM IMPORT ADDRESS, ADR;
  22. FROM libc IMPORT printf;
  23. CONST
  24. AppId = "org.example.m2gtk4.TestShim";
  25. VAR
  26. app: ADDRESS;
  27. passed, widgetDrew, paintableDrew: BOOLEAN;
  28. PROCEDURE DrawWidget (widget: ADDRESS; cr: ADDRESS; width, height: INTEGER;
  29. user: ADDRESS);
  30. BEGIN
  31. widgetDrew := TRUE;
  32. cairo_set_source_rgb(cr, 0.20, 0.50, 0.90);
  33. cairo_rectangle(cr, 0.0, 0.0, VAL(REAL, width), VAL(REAL, height));
  34. cairo_fill(cr)
  35. END DrawWidget;
  36. PROCEDURE DrawPaintable (cr: ADDRESS; width, height: INTEGER; user: ADDRESS);
  37. BEGIN
  38. paintableDrew := TRUE;
  39. cairo_set_source_rgb(cr, 0.90, 0.30, 0.20);
  40. cairo_rectangle(cr, 0.0, 0.0, VAL(REAL, width), VAL(REAL, height));
  41. cairo_fill(cr)
  42. END DrawPaintable;
  43. PROCEDURE AfterMap (data: ADDRESS) : INTEGER;
  44. BEGIN
  45. IF NOT widgetDrew THEN
  46. printf("custom widget draw not called\n");
  47. passed := FALSE
  48. END;
  49. IF NOT paintableDrew THEN
  50. printf("paintable draw not called\n");
  51. passed := FALSE
  52. END;
  53. g_application_quit(app);
  54. RETURN GSourceRemove
  55. END AfterMap;
  56. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  57. VAR
  58. window, box, widget, paintable, picture: ADDRESS;
  59. ok: BOOLEAN;
  60. BEGIN
  61. ok := TRUE;
  62. widgetDrew := FALSE;
  63. paintableDrew := FALSE;
  64. widget := m2_gtk_widget_new(ADR(DrawWidget), NIL, NIL);
  65. IF widget = NIL THEN
  66. printf("custom widget NIL\n"); ok := FALSE
  67. END;
  68. gtk_widget_set_size_request(widget, 120, 80);
  69. paintable := m2_paintable_new(ADR(DrawPaintable), 60, 40, NIL, NIL);
  70. IF paintable = NIL THEN
  71. printf("paintable NIL\n"); ok := FALSE
  72. END;
  73. picture := gtk_picture_new();
  74. gtk_picture_set_paintable(picture, paintable);
  75. IF gtk_picture_get_paintable(picture) # paintable THEN
  76. printf("picture paintable wrong\n"); ok := FALSE
  77. END;
  78. gtk_widget_set_size_request(picture, 80, 60);
  79. box := gtk_box_new(GtkVertical, 6);
  80. gtk_box_append(box, widget);
  81. gtk_box_append(box, picture);
  82. window := gtk_application_window_new(application);
  83. gtk_window_set_child(window, box);
  84. gtk_window_present(window);
  85. passed := ok;
  86. g_timeout_add(300, AfterMap, NIL)
  87. END OnActivate;
  88. VAR
  89. rc: INTEGER;
  90. BEGIN
  91. passed := FALSE;
  92. app := gtk_application_new(AppId, 0);
  93. IF app = NIL THEN
  94. printf("test_shim: FAIL (no app)\n");
  95. HALT(1)
  96. END;
  97. Connect(app, "activate", ADR(OnActivate), NIL);
  98. rc := g_application_run(app, 0, NIL);
  99. IF passed THEN
  100. printf("test_shim: PASS\n");
  101. HALT(0)
  102. ELSE
  103. printf("test_shim: FAIL\n");
  104. HALT(1)
  105. END
  106. END test_shim.