| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- MODULE files ;
- (*
- m2-GTK4 example - the native GTK4 file dialog.
- A button opens a GtkFileDialog; the asynchronous callback receives
- the result and calls gtk_file_dialog_open_finish() to get a GFile,
- whose basename is shown in a label.
- Build: make examples
- Run: ./build/examples/files
- *)
- FROM Gio IMPORT g_application_run;
- FROM GtkApplication IMPORT gtk_application_new;
- FROM GtkApplicationWindow IMPORT gtk_application_window_new;
- FROM GtkWindow IMPORT gtk_window_set_title, gtk_window_set_default_size,
- gtk_window_set_child, gtk_window_present;
- FROM GtkBox IMPORT gtk_box_new, gtk_box_append, gtk_box_set_spacing;
- FROM GtkEnums IMPORT GtkVertical;
- FROM GtkLabel IMPORT gtk_label_new, gtk_label_set_text;
- FROM GtkButton IMPORT gtk_button_new_with_label;
- FROM GtkWidget IMPORT gtk_widget_set_margin_top,
- gtk_widget_set_margin_bottom, gtk_widget_set_margin_start,
- gtk_widget_set_margin_end;
- FROM GtkFileDialog IMPORT gtk_file_dialog_new, gtk_file_dialog_open,
- gtk_file_dialog_open_finish, gtk_file_dialog_set_title;
- FROM GFile IMPORT g_file_get_basename;
- FROM GObject IMPORT g_object_unref;
- FROM GLib IMPORT g_free;
- FROM GtkClosures IMPORT Connect2;
- FROM GtkUtils IMPORT CStrToM2, StrAppend;
- FROM SYSTEM IMPORT ADDRESS, ADR;
- FROM libc IMPORT printf;
- CONST
- AppId = "org.example.m2gtk4.Files";
- VAR
- dialog, window, status: ADDRESS;
- PROCEDURE OnOpenClicked (button: ADDRESS; user: ADDRESS);
- BEGIN
- gtk_file_dialog_open(dialog, window, NIL, ADR(OnOpenFinished), NIL)
- END OnOpenClicked;
- (* void finished(GObject *source, GAsyncResult *result, gpointer user_data) *)
- PROCEDURE OnOpenFinished (source: ADDRESS; result: ADDRESS; user: ADDRESS);
- VAR
- file, name: ADDRESS;
- buf: ARRAY [0..255] OF CHAR;
- line: ARRAY [0..287] OF CHAR;
- BEGIN
- file := gtk_file_dialog_open_finish(dialog, result, NIL);
- IF file = NIL THEN
- gtk_label_set_text(status, "No file selected")
- ELSE
- name := g_file_get_basename(file);
- CStrToM2(name, buf);
- g_free(name);
- line[0] := 0C;
- StrAppend(line, "Opened: ");
- StrAppend(line, buf);
- gtk_label_set_text(status, line);
- g_object_unref(file)
- END
- END OnOpenFinished;
- PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
- VAR
- box, button: ADDRESS;
- BEGIN
- dialog := gtk_file_dialog_new();
- gtk_file_dialog_set_title(dialog, "Open a file");
- status := gtk_label_new("No file selected");
- button := gtk_button_new_with_label("Open file...");
- Connect2(button, "clicked", OnOpenClicked, NIL);
- box := gtk_box_new(GtkVertical, 10);
- gtk_box_set_spacing(box, 10);
- gtk_widget_set_margin_top(box, 14);
- gtk_widget_set_margin_bottom(box, 14);
- gtk_widget_set_margin_start(box, 14);
- gtk_widget_set_margin_end(box, 14);
- gtk_box_append(box, button);
- gtk_box_append(box, status);
- window := gtk_application_window_new(application);
- gtk_window_set_title(window, "m2-GTK4 Files");
- gtk_window_set_default_size(window, 420, 160);
- gtk_window_set_child(window, box);
- gtk_window_present(window)
- END OnActivate;
- VAR
- app: ADDRESS;
- BEGIN
- app := gtk_application_new(AppId, 0);
- IF app = NIL THEN
- printf("gtk_application_new failed\n");
- HALT(1)
- END;
- Connect2(app, "activate", OnActivate, NIL);
- g_application_run(app, 0, NIL)
- END files.
|