files.mod 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. MODULE files ;
  2. (*
  3. m2-GTK4 example - the native GTK4 file dialog.
  4. A button opens a GtkFileDialog; the asynchronous callback receives
  5. the result and calls gtk_file_dialog_open_finish() to get a GFile,
  6. whose basename is shown in a label.
  7. Build: make examples
  8. Run: ./build/examples/files
  9. *)
  10. FROM Gio IMPORT g_application_run;
  11. FROM GtkApplication IMPORT gtk_application_new;
  12. FROM GtkApplicationWindow IMPORT gtk_application_window_new;
  13. FROM GtkWindow IMPORT gtk_window_set_title, gtk_window_set_default_size,
  14. gtk_window_set_child, gtk_window_present;
  15. FROM GtkBox IMPORT gtk_box_new, gtk_box_append, gtk_box_set_spacing;
  16. FROM GtkEnums IMPORT GtkVertical;
  17. FROM GtkLabel IMPORT gtk_label_new, gtk_label_set_text;
  18. FROM GtkButton IMPORT gtk_button_new_with_label;
  19. FROM GtkWidget IMPORT gtk_widget_set_margin_top,
  20. gtk_widget_set_margin_bottom, gtk_widget_set_margin_start,
  21. gtk_widget_set_margin_end;
  22. FROM GtkFileDialog IMPORT gtk_file_dialog_new, gtk_file_dialog_open,
  23. gtk_file_dialog_open_finish, gtk_file_dialog_set_title;
  24. FROM GFile IMPORT g_file_get_basename;
  25. FROM GObject IMPORT g_object_unref;
  26. FROM GLib IMPORT g_free;
  27. FROM GtkClosures IMPORT Connect2;
  28. FROM GtkUtils IMPORT CStrToM2, StrAppend;
  29. FROM SYSTEM IMPORT ADDRESS, ADR;
  30. FROM libc IMPORT printf;
  31. CONST
  32. AppId = "org.example.m2gtk4.Files";
  33. VAR
  34. dialog, window, status: ADDRESS;
  35. PROCEDURE OnOpenClicked (button: ADDRESS; user: ADDRESS);
  36. BEGIN
  37. gtk_file_dialog_open(dialog, window, NIL, ADR(OnOpenFinished), NIL)
  38. END OnOpenClicked;
  39. (* void finished(GObject *source, GAsyncResult *result, gpointer user_data) *)
  40. PROCEDURE OnOpenFinished (source: ADDRESS; result: ADDRESS; user: ADDRESS);
  41. VAR
  42. file, name: ADDRESS;
  43. buf: ARRAY [0..255] OF CHAR;
  44. line: ARRAY [0..287] OF CHAR;
  45. BEGIN
  46. file := gtk_file_dialog_open_finish(dialog, result, NIL);
  47. IF file = NIL THEN
  48. gtk_label_set_text(status, "No file selected")
  49. ELSE
  50. name := g_file_get_basename(file);
  51. CStrToM2(name, buf);
  52. g_free(name);
  53. line[0] := 0C;
  54. StrAppend(line, "Opened: ");
  55. StrAppend(line, buf);
  56. gtk_label_set_text(status, line);
  57. g_object_unref(file)
  58. END
  59. END OnOpenFinished;
  60. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  61. VAR
  62. box, button: ADDRESS;
  63. BEGIN
  64. dialog := gtk_file_dialog_new();
  65. gtk_file_dialog_set_title(dialog, "Open a file");
  66. status := gtk_label_new("No file selected");
  67. button := gtk_button_new_with_label("Open file...");
  68. Connect2(button, "clicked", OnOpenClicked, NIL);
  69. box := gtk_box_new(GtkVertical, 10);
  70. gtk_box_set_spacing(box, 10);
  71. gtk_widget_set_margin_top(box, 14);
  72. gtk_widget_set_margin_bottom(box, 14);
  73. gtk_widget_set_margin_start(box, 14);
  74. gtk_widget_set_margin_end(box, 14);
  75. gtk_box_append(box, button);
  76. gtk_box_append(box, status);
  77. window := gtk_application_window_new(application);
  78. gtk_window_set_title(window, "m2-GTK4 Files");
  79. gtk_window_set_default_size(window, 420, 160);
  80. gtk_window_set_child(window, box);
  81. gtk_window_present(window)
  82. END OnActivate;
  83. VAR
  84. app: ADDRESS;
  85. BEGIN
  86. app := gtk_application_new(AppId, 0);
  87. IF app = NIL THEN
  88. printf("gtk_application_new failed\n");
  89. HALT(1)
  90. END;
  91. Connect2(app, "activate", OnActivate, NIL);
  92. g_application_run(app, 0, NIL)
  93. END files.