DemosLayout.mod 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. IMPLEMENTATION MODULE DemosLayout ;
  2. (*
  3. m2-GTK4 - the layout-related gtk-demo demos: Size Groups, Fixed Layout
  4. Transformations and Layout Manager/Transition. Ported from the GTK C
  5. demo sources in gtk/demos/gtk-demo (sizegroup.c, fixed2.c,
  6. layoutmanager.c).
  7. Bindings that are not (yet) exposed under src/ forced a few faithful
  8. simplifications; each one is marked with a "SIMPLIFIED" note below.
  9. *)
  10. FROM SYSTEM IMPORT ADDRESS;
  11. FROM GtkWindow IMPORT gtk_window_new, gtk_window_set_title,
  12. gtk_window_set_default_size, gtk_window_set_resizable,
  13. gtk_window_set_child, gtk_window_destroy;
  14. FROM GtkWidget IMPORT gtk_widget_set_visible, gtk_widget_get_visible,
  15. gtk_widget_set_margin_start, gtk_widget_set_margin_end,
  16. gtk_widget_set_margin_top, gtk_widget_set_margin_bottom,
  17. gtk_widget_set_halign, gtk_widget_set_valign, gtk_widget_set_hexpand;
  18. FROM GtkBox IMPORT gtk_box_new, gtk_box_append;
  19. FROM GtkGrid IMPORT gtk_grid_new, gtk_grid_attach,
  20. gtk_grid_set_row_spacing, gtk_grid_set_column_spacing;
  21. FROM GtkFrame IMPORT gtk_frame_new, gtk_frame_set_child;
  22. FROM GtkLabel IMPORT gtk_label_new;
  23. FROM GtkDropDown IMPORT gtk_drop_down_new;
  24. FROM GtkStringList IMPORT gtk_string_list_new, gtk_string_list_append;
  25. FROM GtkCheckButton IMPORT gtk_check_button_new_with_label,
  26. gtk_check_button_set_active;
  27. FROM GtkScrolledWindow IMPORT gtk_scrolled_window_new,
  28. gtk_scrolled_window_set_child;
  29. FROM GtkFixed IMPORT gtk_fixed_new, gtk_fixed_put;
  30. FROM GtkEnums IMPORT GtkVertical, GtkAlignStart, GtkAlignEnd,
  31. GtkAlignBaseline;
  32. (* ------------------------------------------------------------------ *)
  33. (* Size Groups *)
  34. (* ------------------------------------------------------------------ *)
  35. VAR
  36. sizeGroupWindow: ADDRESS;
  37. (* Build a drop-down from three literal labels. The C demo passes a
  38. NULL-terminated const char * array to gtk_drop_down_new_from_strings();
  39. this binding favours a GtkStringList model, which is the equivalent
  40. route exposed under src/. *)
  41. PROCEDURE MakeDropDown (a, b, c: ARRAY OF CHAR) : ADDRESS;
  42. VAR
  43. list: ADDRESS;
  44. BEGIN
  45. list := gtk_string_list_new(NIL);
  46. gtk_string_list_append(list, a);
  47. gtk_string_list_append(list, b);
  48. gtk_string_list_append(list, c);
  49. RETURN gtk_drop_down_new(list, NIL)
  50. END MakeDropDown;
  51. (* add_row(): one mnemonic label plus a drop-down in a grid row.
  52. SIMPLIFIED: gtk_label_new_with_mnemonic() and
  53. gtk_label_set_mnemonic_widget() are not bound, so a plain label is
  54. used (the underscores are dropped rather than shown literally). *)
  55. PROCEDURE AddRow (table: ADDRESS; row: INTEGER;
  56. labelText, a, b, c: ARRAY OF CHAR);
  57. VAR
  58. label, dropdown: ADDRESS;
  59. BEGIN
  60. label := gtk_label_new(labelText);
  61. gtk_widget_set_halign(label, GtkAlignStart);
  62. gtk_widget_set_valign(label, GtkAlignBaseline);
  63. gtk_widget_set_hexpand(label, 1);
  64. gtk_grid_attach(table, label, 0, row, 1, 1);
  65. dropdown := MakeDropDown(a, b, c);
  66. gtk_widget_set_halign(dropdown, GtkAlignEnd);
  67. gtk_widget_set_valign(dropdown, GtkAlignBaseline);
  68. gtk_grid_attach(table, dropdown, 1, row, 1, 1)
  69. END AddRow;
  70. PROCEDURE DoSizeGroup (doWidget: ADDRESS) : ADDRESS;
  71. VAR
  72. vbox, frame, table, checkButton: ADDRESS;
  73. BEGIN
  74. IF sizeGroupWindow = NIL THEN
  75. sizeGroupWindow := gtk_window_new();
  76. gtk_window_set_title(sizeGroupWindow, "Size Groups");
  77. gtk_window_set_resizable(sizeGroupWindow, 0);
  78. vbox := gtk_box_new(GtkVertical, 5);
  79. gtk_widget_set_margin_start(vbox, 5);
  80. gtk_widget_set_margin_end(vbox, 5);
  81. gtk_widget_set_margin_top(vbox, 5);
  82. gtk_widget_set_margin_bottom(vbox, 5);
  83. gtk_window_set_child(sizeGroupWindow, vbox);
  84. (* SIMPLIFIED: GtkSizeGroup is not bound under src/, so the
  85. gtk_size_group_new()/add_widget()/set_mode() machinery and the
  86. "toggled" callback that switches it on and off are omitted.
  87. The two grids below are otherwise identical to the C demo. *)
  88. (* Color options *)
  89. frame := gtk_frame_new("Color Options");
  90. gtk_box_append(vbox, frame);
  91. table := gtk_grid_new();
  92. gtk_widget_set_margin_start(table, 5);
  93. gtk_widget_set_margin_end(table, 5);
  94. gtk_widget_set_margin_top(table, 5);
  95. gtk_widget_set_margin_bottom(table, 5);
  96. gtk_grid_set_row_spacing(table, 5);
  97. gtk_grid_set_column_spacing(table, 10);
  98. gtk_frame_set_child(frame, table);
  99. AddRow(table, 0, "Foreground", "Red", "Green", "Blue");
  100. AddRow(table, 1, "Background", "Red", "Green", "Blue");
  101. (* Line options *)
  102. frame := gtk_frame_new("Line Options");
  103. gtk_box_append(vbox, frame);
  104. table := gtk_grid_new();
  105. gtk_widget_set_margin_start(table, 5);
  106. gtk_widget_set_margin_end(table, 5);
  107. gtk_widget_set_margin_top(table, 5);
  108. gtk_widget_set_margin_bottom(table, 5);
  109. gtk_grid_set_row_spacing(table, 5);
  110. gtk_grid_set_column_spacing(table, 10);
  111. gtk_frame_set_child(frame, table);
  112. AddRow(table, 0, "Dashing", "Solid", "Dashed", "Dotted");
  113. AddRow(table, 1, "Line ends", "Square", "Round", "Double Arrow");
  114. checkButton := gtk_check_button_new_with_label("Enable grouping");
  115. gtk_box_append(vbox, checkButton);
  116. gtk_check_button_set_active(checkButton, 1)
  117. END;
  118. IF gtk_widget_get_visible(sizeGroupWindow) = 0 THEN
  119. gtk_widget_set_visible(sizeGroupWindow, 1)
  120. ELSE
  121. gtk_window_destroy(sizeGroupWindow);
  122. sizeGroupWindow := NIL
  123. END;
  124. RETURN sizeGroupWindow
  125. END DoSizeGroup;
  126. (* ------------------------------------------------------------------ *)
  127. (* Fixed Layout / Transformations *)
  128. (* ------------------------------------------------------------------ *)
  129. VAR
  130. fixed2Window: ADDRESS;
  131. PROCEDURE DoFixed2 (doWidget: ADDRESS) : ADDRESS;
  132. VAR
  133. sw, fixed, child: ADDRESS;
  134. BEGIN
  135. IF fixed2Window = NIL THEN
  136. fixed2Window := gtk_window_new();
  137. gtk_window_set_title(fixed2Window, "Fixed Layout - Transformations");
  138. gtk_window_set_default_size(fixed2Window, 400, 300);
  139. sw := gtk_scrolled_window_new();
  140. gtk_window_set_child(fixed2Window, sw);
  141. fixed := gtk_fixed_new();
  142. gtk_scrolled_window_set_child(sw, fixed);
  143. child := gtk_label_new("All fixed?");
  144. gtk_fixed_put(fixed, child, 0.0, 0.0)
  145. (* SIMPLIFIED: the C demo animates the child with a frame-clock
  146. tick callback (gtk_widget_add_tick_callback), a transform on the
  147. fixed child (gtk_fixed_set_child_transform) and
  148. gtk_widget_set_overflow(); none of those are bound under src/.
  149. The widget hierarchy is kept, the rotation/scale animation is
  150. omitted. *)
  151. END;
  152. IF gtk_widget_get_visible(fixed2Window) = 0 THEN
  153. gtk_widget_set_visible(fixed2Window, 1)
  154. ELSE
  155. gtk_window_destroy(fixed2Window);
  156. fixed2Window := NIL
  157. END;
  158. RETURN fixed2Window
  159. END DoFixed2;
  160. (* ------------------------------------------------------------------ *)
  161. (* Layout Manager / Transition *)
  162. (* ------------------------------------------------------------------ *)
  163. VAR
  164. layoutManagerWindow: ADDRESS;
  165. layoutColors: ARRAY [0..15] OF ARRAY [0..9] OF CHAR;
  166. (* SIMPLIFIED: the C demo builds a bespoke DemoWidget whose custom
  167. GtkLayoutManager animates its 16 DemoChild() squares between a grid
  168. and a circle. Neither that C widget pair nor a GtkLayoutManager /
  169. GtkCustomLayout binding exists under src/. The bound equivalent used
  170. here is a GtkGrid holding the same 16 coloured children; the names
  171. are shown as label text (the animation and the drawn colour fill are
  172. omitted). *)
  173. PROCEDURE InitColors;
  174. BEGIN
  175. layoutColors[0] := "red";
  176. layoutColors[1] := "orange";
  177. layoutColors[2] := "yellow";
  178. layoutColors[3] := "green";
  179. layoutColors[4] := "blue";
  180. layoutColors[5] := "grey";
  181. layoutColors[6] := "magenta";
  182. layoutColors[7] := "lime";
  183. layoutColors[8] := "yellow";
  184. layoutColors[9] := "firebrick";
  185. layoutColors[10] := "aqua";
  186. layoutColors[11] := "purple";
  187. layoutColors[12] := "tomato";
  188. layoutColors[13] := "pink";
  189. layoutColors[14] := "thistle";
  190. layoutColors[15] := "maroon"
  191. END InitColors;
  192. PROCEDURE DoLayoutManager (doWidget: ADDRESS) : ADDRESS;
  193. VAR
  194. grid, child: ADDRESS;
  195. i: INTEGER;
  196. BEGIN
  197. IF layoutManagerWindow = NIL THEN
  198. layoutManagerWindow := gtk_window_new();
  199. gtk_window_set_title(layoutManagerWindow,
  200. "Layout Manager - Transition");
  201. gtk_window_set_default_size(layoutManagerWindow, 600, 600);
  202. InitColors;
  203. grid := gtk_grid_new();
  204. FOR i := 0 TO 15 DO
  205. child := gtk_label_new(layoutColors[i]);
  206. gtk_widget_set_margin_start(child, 4);
  207. gtk_widget_set_margin_end(child, 4);
  208. gtk_widget_set_margin_top(child, 4);
  209. gtk_widget_set_margin_bottom(child, 4);
  210. gtk_grid_attach(grid, child, i MOD 4, i DIV 4, 1, 1)
  211. END;
  212. gtk_window_set_child(layoutManagerWindow, grid)
  213. END;
  214. IF gtk_widget_get_visible(layoutManagerWindow) = 0 THEN
  215. gtk_widget_set_visible(layoutManagerWindow, 1)
  216. ELSE
  217. gtk_window_destroy(layoutManagerWindow);
  218. layoutManagerWindow := NIL
  219. END;
  220. RETURN layoutManagerWindow
  221. END DoLayoutManager;
  222. END DemosLayout.