MODULE dialogs ; (* m2-GTK4 example - modern dialogs. "Alert" shows a GtkAlertDialog with custom buttons; "Choose colour" opens a GtkColorDialog asynchronously and reports the chosen GdkRGBA in a label. Build: make examples Run: ./build/examples/dialogs *) FROM Gio IMPORT g_application_run; FROM GtkApplication IMPORT gtk_application_new; FROM GtkWindow IMPORT gtk_application_window_new, 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 GtkAlertDialog IMPORT gtk_alert_dialog_new, gtk_alert_dialog_set_message, gtk_alert_dialog_set_detail, gtk_alert_dialog_set_buttons, gtk_alert_dialog_set_default_button, gtk_alert_dialog_show; FROM GtkColorDialog IMPORT gtk_color_dialog_new, gtk_color_dialog_set_title, gtk_color_dialog_set_modal, gtk_color_dialog_choose_rgba, gtk_color_dialog_choose_rgba_finish; FROM GError IMPORT GError; FROM Gdk IMPORT gdk_rgba_free, gdk_rgba_to_string, GdkRGBAPtr; FROM GtkUtils IMPORT CStrToM2, Free, ClearError; FROM GtkClosures IMPORT Connect2; FROM SYSTEM IMPORT ADDRESS, ADR, CAST; FROM libc IMPORT printf; CONST AppId = "org.example.m2gtk4.Dialogs"; VAR window, status, alert, colorDialog: ADDRESS; okLabel: ARRAY [0..2] OF CHAR; cancelLabel: ARRAY [0..7] OF CHAR; buttons: ARRAY [0..2] OF ADDRESS; PROCEDURE OnAlert (button: ADDRESS; user: ADDRESS); BEGIN gtk_alert_dialog_show(alert, window) END OnAlert; (* void chosen(GObject *source, GAsyncResult *result, gpointer user_data) *) PROCEDURE OnColorChosen (source: ADDRESS; result: ADDRESS; user: ADDRESS); VAR rgba: ADDRESS; p: GdkRGBAPtr; str: ADDRESS; buf: ARRAY [0..63] OF CHAR; err: GError; BEGIN err := NIL; rgba := gtk_color_dialog_choose_rgba_finish(colorDialog, result, err); IF rgba # NIL THEN p := CAST(GdkRGBAPtr, rgba); str := gdk_rgba_to_string(p^); CStrToM2(str, buf); gtk_label_set_text(status, buf); Free(str); gdk_rgba_free(rgba) ELSE gtk_label_set_text(status, "No colour chosen"); ClearError(err) END END OnColorChosen; PROCEDURE OnColor (button: ADDRESS; user: ADDRESS); BEGIN gtk_color_dialog_choose_rgba(colorDialog, window, NIL, NIL, ADR(OnColorChosen), NIL) END OnColor; PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS); VAR box, alertButton, colorButton: ADDRESS; BEGIN okLabel := "OK"; cancelLabel := "Cancel"; buttons[0] := ADR(okLabel); buttons[1] := ADR(cancelLabel); buttons[2] := NIL; alert := gtk_alert_dialog_new("This is a test alert"); gtk_alert_dialog_set_message(alert, "Delete the file?"); gtk_alert_dialog_set_detail(alert, "This cannot be undone."); gtk_alert_dialog_set_buttons(alert, ADR(buttons)); gtk_alert_dialog_set_default_button(alert, 0); colorDialog := gtk_color_dialog_new(); gtk_color_dialog_set_title(colorDialog, "Pick a colour"); gtk_color_dialog_set_modal(colorDialog, 1); alertButton := gtk_button_new_with_label("Alert"); Connect2(alertButton, "clicked", OnAlert, NIL); colorButton := gtk_button_new_with_label("Choose colour"); Connect2(colorButton, "clicked", OnColor, NIL); status := gtk_label_new("No colour chosen"); box := gtk_box_new(GtkVertical, 12); gtk_box_set_spacing(box, 12); 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, alertButton); gtk_box_append(box, colorButton); gtk_box_append(box, status); window := gtk_application_window_new(application); gtk_window_set_title(window, "m2-GTK4 Dialogs"); gtk_window_set_default_size(window, 400, 200); 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 dialogs.