customlayout.mod 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. MODULE customlayout ;
  2. (*
  3. m2-GTK4 example - a custom layout manager from Modula-2.
  4. The shim's M2LayoutManager positions the parent's children in a
  5. single column using the Modula-2 measure/allocate callbacks.
  6. Build: make examples
  7. Run: ./build/examples/customlayout
  8. *)
  9. FROM Gio IMPORT g_application_run;
  10. FROM GtkApplication IMPORT gtk_application_new;
  11. FROM GtkWindow IMPORT gtk_application_window_new, gtk_window_set_title,
  12. gtk_window_set_default_size, gtk_window_set_child, gtk_window_present;
  13. FROM GtkLabel IMPORT gtk_label_new;
  14. FROM GtkWidget IMPORT gtk_widget_set_parent, gtk_widget_get_first_child,
  15. gtk_widget_get_next_sibling, gtk_widget_set_layout_manager;
  16. FROM M2GtkShim IMPORT m2_gtk_widget_new, m2_layout_manager_new,
  17. m2_layout_allocate_child;
  18. FROM GtkClosures IMPORT Connect2;
  19. FROM SYSTEM IMPORT ADDRESS, ADR;
  20. FROM libc IMPORT printf;
  21. CONST
  22. AppId = "org.example.m2gtk4.CustomLayout";
  23. RowHeight = 36;
  24. PROCEDURE Measure (widget: ADDRESS; orientation: INTEGER; for_size: INTEGER;
  25. VAR minimum, natural: INTEGER; user: ADDRESS);
  26. BEGIN
  27. minimum := 240;
  28. natural := 240
  29. END Measure;
  30. PROCEDURE Allocate (widget: ADDRESS; width, height, baseline: INTEGER;
  31. user: ADDRESS);
  32. VAR
  33. child: ADDRESS;
  34. i: INTEGER;
  35. BEGIN
  36. i := 0;
  37. child := gtk_widget_get_first_child(widget);
  38. WHILE child # NIL DO
  39. m2_layout_allocate_child(child, 8, i * RowHeight, width - 16,
  40. RowHeight - 4);
  41. INC(i);
  42. child := gtk_widget_get_next_sibling(child)
  43. END
  44. END Allocate;
  45. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  46. VAR
  47. window, parent, manager: ADDRESS;
  48. BEGIN
  49. parent := m2_gtk_widget_new(NIL, NIL, NIL);
  50. manager := m2_layout_manager_new(ADR(Measure), ADR(Allocate), NIL, NIL);
  51. gtk_widget_set_layout_manager(parent, manager);
  52. gtk_widget_set_parent(gtk_label_new("row one"), parent);
  53. gtk_widget_set_parent(gtk_label_new("row two"), parent);
  54. gtk_widget_set_parent(gtk_label_new("row three"), parent);
  55. gtk_widget_set_parent(gtk_label_new("row four"), parent);
  56. window := gtk_application_window_new(application);
  57. gtk_window_set_title(window, "m2-GTK4 Custom Layout");
  58. gtk_window_set_default_size(window, 300, 220);
  59. gtk_window_set_child(window, parent);
  60. gtk_window_present(window)
  61. END OnActivate;
  62. VAR
  63. app: ADDRESS;
  64. BEGIN
  65. app := gtk_application_new(AppId, 0);
  66. IF app = NIL THEN
  67. printf("gtk_application_new failed\n");
  68. HALT(1)
  69. END;
  70. Connect2(app, "activate", OnActivate, NIL);
  71. g_application_run(app, 0, NIL)
  72. END customlayout.