adw.mod 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. MODULE adw ;
  2. (*
  3. m2-GTK4 example - a libadwaita preferences application.
  4. AdwApplicationWindow > AdwToastOverlay > AdwToolbarView
  5. top bar - AdwHeaderBar (title + "About" button, packed end)
  6. content - AdwPreferencesPage >
  7. AdwPreferencesGroup >
  8. AdwActionRow (activatable -> toast)
  9. AdwSwitchRow
  10. AdwEntryRow
  11. AdwComboRow
  12. AdwExpanderRow (with a switch row inside)
  13. Build: make examples
  14. Run: ./build/examples/adw
  15. *)
  16. FROM Gio IMPORT g_application_run;
  17. FROM AdwApplication IMPORT adw_application_new;
  18. FROM AdwApplicationWindow IMPORT adw_application_window_new,
  19. adw_application_window_set_content;
  20. FROM AdwHeaderBar IMPORT adw_header_bar_new, adw_header_bar_pack_end,
  21. adw_header_bar_set_title_widget;
  22. FROM AdwToolbarView IMPORT adw_toolbar_view_new, adw_toolbar_view_add_top_bar,
  23. adw_toolbar_view_set_content;
  24. FROM AdwPreferencesPage IMPORT adw_preferences_page_new,
  25. adw_preferences_page_add, adw_preferences_page_set_title;
  26. FROM AdwPreferencesGroup IMPORT adw_preferences_group_new,
  27. adw_preferences_group_add, adw_preferences_group_set_title,
  28. adw_preferences_group_set_description;
  29. FROM AdwPreferencesRow IMPORT adw_preferences_row_set_title;
  30. FROM AdwActionRow IMPORT adw_action_row_new, adw_action_row_set_subtitle,
  31. adw_action_row_set_icon_name;
  32. FROM AdwSwitchRow IMPORT adw_switch_row_new;
  33. FROM AdwEntryRow IMPORT adw_entry_row_new;
  34. FROM AdwComboRow IMPORT adw_combo_row_new, adw_combo_row_set_model,
  35. adw_combo_row_set_selected;
  36. FROM AdwExpanderRow IMPORT adw_expander_row_new, adw_expander_row_add_row;
  37. FROM AdwToast IMPORT adw_toast_new, adw_toast_set_timeout;
  38. FROM AdwToastOverlay IMPORT adw_toast_overlay_new,
  39. adw_toast_overlay_add_toast, adw_toast_overlay_set_child;
  40. FROM AdwAboutWindow IMPORT adw_about_window_new,
  41. adw_about_window_set_application_name,
  42. adw_about_window_set_application_icon, adw_about_window_set_version,
  43. adw_about_window_set_developer_name, adw_about_window_set_comments,
  44. adw_about_window_set_website;
  45. FROM GtkWindow IMPORT gtk_window_present;
  46. FROM GtkButton IMPORT gtk_button_new_with_label;
  47. FROM GtkLabel IMPORT gtk_label_new;
  48. FROM GtkStringList IMPORT gtk_string_list_new, gtk_string_list_append;
  49. FROM GtkClosures IMPORT Connect2;
  50. FROM SYSTEM IMPORT ADDRESS;
  51. FROM libc IMPORT printf;
  52. CONST
  53. AppId = "org.example.m2gtk4.Adw";
  54. VAR
  55. overlay: ADDRESS;
  56. PROCEDURE ShowToast (text: ARRAY OF CHAR);
  57. VAR
  58. toast: ADDRESS;
  59. BEGIN
  60. toast := adw_toast_new(text);
  61. adw_toast_set_timeout(toast, 2);
  62. adw_toast_overlay_add_toast(overlay, toast)
  63. END ShowToast;
  64. (* void activated(AdwActionRow *row, gpointer data) *)
  65. PROCEDURE OnRowActivated (row: ADDRESS; data: ADDRESS);
  66. BEGIN
  67. ShowToast("Row activated")
  68. END OnRowActivated;
  69. PROCEDURE OnAbout (button: ADDRESS; data: ADDRESS);
  70. VAR
  71. about: ADDRESS;
  72. BEGIN
  73. about := adw_about_window_new();
  74. adw_about_window_set_application_name(about, "m2-GTK4");
  75. adw_about_window_set_application_icon(about, "application-x-executable");
  76. adw_about_window_set_version(about, "0.1");
  77. adw_about_window_set_developer_name(about, "m2-GTK4");
  78. adw_about_window_set_comments(about, "libadwaita bindings for GNU Modula-2");
  79. adw_about_window_set_website(about, "https://example.org");
  80. gtk_window_present(about)
  81. END OnAbout;
  82. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  83. VAR
  84. window, toolbar, header, aboutButton, page, group, row, sw, er, combo,
  85. list, expander, inner: ADDRESS;
  86. BEGIN
  87. window := adw_application_window_new(application);
  88. aboutButton := gtk_button_new_with_label("About");
  89. Connect2(aboutButton, "clicked", OnAbout, NIL);
  90. header := adw_header_bar_new();
  91. adw_header_bar_set_title_widget(header, gtk_label_new("Preferences"));
  92. adw_header_bar_pack_end(header, aboutButton);
  93. toolbar := adw_toolbar_view_new();
  94. adw_toolbar_view_add_top_bar(toolbar, header);
  95. page := adw_preferences_page_new();
  96. adw_preferences_page_set_title(page, "Settings");
  97. group := adw_preferences_group_new();
  98. adw_preferences_group_set_title(group, "General");
  99. adw_preferences_group_set_description(group, "Example Adwaita rows");
  100. row := adw_action_row_new();
  101. adw_preferences_row_set_title(row, "Appearance");
  102. adw_action_row_set_subtitle(row, "Tap to show a toast");
  103. adw_action_row_set_icon_name(row, "applications-graphics");
  104. Connect2(row, "activated", OnRowActivated, NIL);
  105. adw_preferences_group_add(group, row);
  106. sw := adw_switch_row_new();
  107. adw_preferences_row_set_title(sw, "Enable notifications");
  108. adw_preferences_group_add(group, sw);
  109. er := adw_entry_row_new();
  110. adw_preferences_row_set_title(er, "Name");
  111. adw_preferences_group_add(group, er);
  112. list := gtk_string_list_new(NIL);
  113. gtk_string_list_append(list, "System");
  114. gtk_string_list_append(list, "Light");
  115. gtk_string_list_append(list, "Dark");
  116. combo := adw_combo_row_new();
  117. adw_preferences_row_set_title(combo, "Theme");
  118. adw_combo_row_set_model(combo, list);
  119. adw_combo_row_set_selected(combo, 0);
  120. adw_preferences_group_add(group, combo);
  121. expander := adw_expander_row_new();
  122. adw_preferences_row_set_title(expander, "Advanced");
  123. inner := adw_switch_row_new();
  124. adw_preferences_row_set_title(inner, "Developer mode");
  125. adw_expander_row_add_row(expander, inner);
  126. adw_preferences_group_add(group, expander);
  127. adw_preferences_page_add(page, group);
  128. adw_toolbar_view_set_content(toolbar, page);
  129. overlay := adw_toast_overlay_new();
  130. adw_toast_overlay_set_child(overlay, toolbar);
  131. adw_application_window_set_content(window, overlay);
  132. gtk_window_present(window)
  133. END OnActivate;
  134. VAR
  135. app: ADDRESS;
  136. BEGIN
  137. app := adw_application_new(AppId, 0);
  138. IF app = NIL THEN
  139. printf("adw_application_new failed\n");
  140. HALT(1)
  141. END;
  142. Connect2(app, "activate", OnActivate, NIL);
  143. g_application_run(app, 0, NIL)
  144. END adw.