dialogs.mod 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. MODULE dialogs ;
  2. (*
  3. m2-GTK4 example - modern dialogs.
  4. "Alert" shows a GtkAlertDialog with custom buttons; "Choose colour"
  5. opens a GtkColorDialog asynchronously and reports the chosen GdkRGBA
  6. in a label.
  7. Build: make examples
  8. Run: ./build/examples/dialogs
  9. *)
  10. FROM Gio IMPORT g_application_run;
  11. FROM GtkApplication IMPORT gtk_application_new;
  12. FROM GtkWindow IMPORT gtk_application_window_new, gtk_window_set_title,
  13. gtk_window_set_default_size, gtk_window_set_child, gtk_window_present;
  14. FROM GtkBox IMPORT gtk_box_new, gtk_box_append, gtk_box_set_spacing;
  15. FROM GtkEnums IMPORT GtkVertical;
  16. FROM GtkLabel IMPORT gtk_label_new, gtk_label_set_text;
  17. FROM GtkButton IMPORT gtk_button_new_with_label;
  18. FROM GtkWidget IMPORT gtk_widget_set_margin_top,
  19. gtk_widget_set_margin_bottom, gtk_widget_set_margin_start,
  20. gtk_widget_set_margin_end;
  21. FROM GtkAlertDialog IMPORT gtk_alert_dialog_new,
  22. gtk_alert_dialog_set_message, gtk_alert_dialog_set_detail,
  23. gtk_alert_dialog_set_buttons, gtk_alert_dialog_set_default_button,
  24. gtk_alert_dialog_show;
  25. FROM GtkColorDialog IMPORT gtk_color_dialog_new, gtk_color_dialog_set_title,
  26. gtk_color_dialog_set_modal, gtk_color_dialog_choose_rgba,
  27. gtk_color_dialog_choose_rgba_finish;
  28. FROM GError IMPORT GError;
  29. FROM Gdk IMPORT gdk_rgba_free, gdk_rgba_to_string, GdkRGBAPtr;
  30. FROM GtkUtils IMPORT CStrToM2, Free, ClearError;
  31. FROM GtkClosures IMPORT Connect2;
  32. FROM SYSTEM IMPORT ADDRESS, ADR, CAST;
  33. FROM libc IMPORT printf;
  34. CONST
  35. AppId = "org.example.m2gtk4.Dialogs";
  36. VAR
  37. window, status, alert, colorDialog: ADDRESS;
  38. okLabel: ARRAY [0..2] OF CHAR;
  39. cancelLabel: ARRAY [0..7] OF CHAR;
  40. buttons: ARRAY [0..2] OF ADDRESS;
  41. PROCEDURE OnAlert (button: ADDRESS; user: ADDRESS);
  42. BEGIN
  43. gtk_alert_dialog_show(alert, window)
  44. END OnAlert;
  45. (* void chosen(GObject *source, GAsyncResult *result, gpointer user_data) *)
  46. PROCEDURE OnColorChosen (source: ADDRESS; result: ADDRESS; user: ADDRESS);
  47. VAR
  48. rgba: ADDRESS;
  49. p: GdkRGBAPtr;
  50. str: ADDRESS;
  51. buf: ARRAY [0..63] OF CHAR;
  52. err: GError;
  53. BEGIN
  54. err := NIL;
  55. rgba := gtk_color_dialog_choose_rgba_finish(colorDialog, result, err);
  56. IF rgba # NIL THEN
  57. p := CAST(GdkRGBAPtr, rgba);
  58. str := gdk_rgba_to_string(p^);
  59. CStrToM2(str, buf);
  60. gtk_label_set_text(status, buf);
  61. Free(str);
  62. gdk_rgba_free(rgba)
  63. ELSE
  64. gtk_label_set_text(status, "No colour chosen");
  65. ClearError(err)
  66. END
  67. END OnColorChosen;
  68. PROCEDURE OnColor (button: ADDRESS; user: ADDRESS);
  69. BEGIN
  70. gtk_color_dialog_choose_rgba(colorDialog, window, NIL, NIL,
  71. ADR(OnColorChosen), NIL)
  72. END OnColor;
  73. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  74. VAR
  75. box, alertButton, colorButton: ADDRESS;
  76. BEGIN
  77. okLabel := "OK";
  78. cancelLabel := "Cancel";
  79. buttons[0] := ADR(okLabel);
  80. buttons[1] := ADR(cancelLabel);
  81. buttons[2] := NIL;
  82. alert := gtk_alert_dialog_new("This is a test alert");
  83. gtk_alert_dialog_set_message(alert, "Delete the file?");
  84. gtk_alert_dialog_set_detail(alert, "This cannot be undone.");
  85. gtk_alert_dialog_set_buttons(alert, ADR(buttons));
  86. gtk_alert_dialog_set_default_button(alert, 0);
  87. colorDialog := gtk_color_dialog_new();
  88. gtk_color_dialog_set_title(colorDialog, "Pick a colour");
  89. gtk_color_dialog_set_modal(colorDialog, 1);
  90. alertButton := gtk_button_new_with_label("Alert");
  91. Connect2(alertButton, "clicked", OnAlert, NIL);
  92. colorButton := gtk_button_new_with_label("Choose colour");
  93. Connect2(colorButton, "clicked", OnColor, NIL);
  94. status := gtk_label_new("No colour chosen");
  95. box := gtk_box_new(GtkVertical, 12);
  96. gtk_box_set_spacing(box, 12);
  97. gtk_widget_set_margin_top(box, 14);
  98. gtk_widget_set_margin_bottom(box, 14);
  99. gtk_widget_set_margin_start(box, 14);
  100. gtk_widget_set_margin_end(box, 14);
  101. gtk_box_append(box, alertButton);
  102. gtk_box_append(box, colorButton);
  103. gtk_box_append(box, status);
  104. window := gtk_application_window_new(application);
  105. gtk_window_set_title(window, "m2-GTK4 Dialogs");
  106. gtk_window_set_default_size(window, 400, 200);
  107. gtk_window_set_child(window, box);
  108. gtk_window_present(window)
  109. END OnActivate;
  110. VAR
  111. app: ADDRESS;
  112. BEGIN
  113. app := gtk_application_new(AppId, 0);
  114. IF app = NIL THEN
  115. printf("gtk_application_new failed\n");
  116. HALT(1)
  117. END;
  118. Connect2(app, "activate", OnActivate, NIL);
  119. g_application_run(app, 0, NIL)
  120. END dialogs.