test_constraints.mod 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. MODULE test_constraints ;
  2. (*
  3. m2-GTK4 test - GtkConstraintLayout, GtkConstraint and
  4. GtkConstraintGuide. Requires a display.
  5. *)
  6. FROM Gio IMPORT g_application_run, g_application_quit;
  7. FROM GtkApplication IMPORT gtk_application_new;
  8. FROM GtkWindow IMPORT gtk_application_window_new, gtk_window_set_child,
  9. gtk_window_present;
  10. FROM GtkLabel IMPORT gtk_label_new;
  11. FROM GtkWidget IMPORT gtk_widget_set_parent, gtk_widget_set_layout_manager;
  12. FROM M2GtkShim IMPORT m2_gtk_widget_new;
  13. FROM GtkConstraintLayout IMPORT gtk_constraint_layout_new,
  14. gtk_constraint_layout_add_constraint, gtk_constraint_layout_add_guide,
  15. gtk_constraint_layout_remove_all_constraints;
  16. FROM GtkConstraint IMPORT gtk_constraint_new, gtk_constraint_new_constant,
  17. GtkConstraintAttributeWidth, GtkConstraintAttributeHeight,
  18. GtkConstraintRelationEq, GtkConstraintStrengthRequired;
  19. FROM GtkConstraintGuide IMPORT gtk_constraint_guide_new,
  20. gtk_constraint_guide_set_min_size, gtk_constraint_guide_get_min_size,
  21. gtk_constraint_guide_set_strength, gtk_constraint_guide_set_name,
  22. gtk_constraint_guide_get_name;
  23. FROM GtkUtils IMPORT Connect, CStrToM2;
  24. FROM SYSTEM IMPORT ADDRESS, ADR;
  25. FROM libc IMPORT printf;
  26. CONST
  27. AppId = "org.example.m2gtk4.TestConstraints";
  28. VAR
  29. app: ADDRESS;
  30. passed: BOOLEAN;
  31. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  32. VAR
  33. window, parent, layout, label, c1, c2, guide: ADDRESS;
  34. gw, gh: INTEGER;
  35. buf: ARRAY [0..31] OF CHAR;
  36. ok: BOOLEAN;
  37. BEGIN
  38. ok := TRUE;
  39. layout := gtk_constraint_layout_new();
  40. IF layout = NIL THEN printf("layout NIL\n"); ok := FALSE END;
  41. parent := m2_gtk_widget_new(NIL, NIL, NIL);
  42. gtk_widget_set_layout_manager(parent, layout);
  43. label := gtk_label_new("constrained");
  44. gtk_widget_set_parent(label, parent);
  45. c1 := gtk_constraint_new(label, GtkConstraintAttributeWidth,
  46. GtkConstraintRelationEq, NIL, 0,
  47. 1.0, 100.0, GtkConstraintStrengthRequired);
  48. IF c1 = NIL THEN printf("constraint NIL\n"); ok := FALSE END;
  49. gtk_constraint_layout_add_constraint(layout, c1);
  50. c2 := gtk_constraint_new_constant(label, GtkConstraintAttributeHeight,
  51. GtkConstraintRelationEq, 40.0,
  52. GtkConstraintStrengthRequired);
  53. gtk_constraint_layout_add_constraint(layout, c2);
  54. guide := gtk_constraint_guide_new();
  55. gtk_constraint_guide_set_min_size(guide, 50, 20);
  56. gtk_constraint_guide_get_min_size(guide, gw, gh);
  57. IF (gw # 50) OR (gh # 20) THEN
  58. printf("guide min size wrong: %d x %d\n", gw, gh); ok := FALSE
  59. END;
  60. gtk_constraint_guide_set_strength(guide, 1);
  61. gtk_constraint_guide_set_name(guide, "g1");
  62. CStrToM2(gtk_constraint_guide_get_name(guide), buf);
  63. IF (buf[0] # 'g') OR (buf[1] # '1') OR (buf[2] # 0C) THEN
  64. printf("guide name wrong: [%s]\n", buf); ok := FALSE
  65. END;
  66. gtk_constraint_layout_add_guide(layout, guide);
  67. window := gtk_application_window_new(application);
  68. gtk_window_set_child(window, parent);
  69. gtk_window_present(window);
  70. gtk_constraint_layout_remove_all_constraints(layout);
  71. passed := ok;
  72. g_application_quit(application)
  73. END OnActivate;
  74. VAR
  75. rc: INTEGER;
  76. BEGIN
  77. passed := FALSE;
  78. app := gtk_application_new(AppId, 0);
  79. IF app = NIL THEN
  80. printf("test_constraints: FAIL (no app)\n");
  81. HALT(1)
  82. END;
  83. Connect(app, "activate", ADR(OnActivate), NIL);
  84. rc := g_application_run(app, 0, NIL);
  85. IF passed THEN
  86. printf("test_constraints: PASS\n");
  87. HALT(0)
  88. ELSE
  89. printf("test_constraints: FAIL\n");
  90. HALT(1)
  91. END
  92. END test_constraints.