builder.mod 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. MODULE builder ;
  2. (*
  3. m2-GTK4 example - building a window from a .ui file with GtkBuilder.
  4. Loads examples/builder.ui (object ids: window, button, status), wires
  5. the button's "clicked" signal to update the status label, and shows
  6. the window.
  7. Build: make examples
  8. Run: ./build/examples/builder (from the repository root)
  9. *)
  10. FROM Gio IMPORT g_application_run;
  11. FROM GtkApplication IMPORT gtk_application_new;
  12. FROM GtkBuilder IMPORT GtkBuilder, gtk_builder_new, gtk_builder_add_from_file,
  13. gtk_builder_get_object;
  14. FROM GtkWindow IMPORT gtk_window_set_application, gtk_window_present;
  15. FROM GtkLabel IMPORT gtk_label_set_text;
  16. FROM GError IMPORT GError;
  17. FROM GtkClosures IMPORT Connect2;
  18. FROM GtkUtils IMPORT ErrorMessage, ClearError;
  19. FROM SYSTEM IMPORT ADDRESS, ADR;
  20. FROM libc IMPORT printf;
  21. CONST
  22. AppId = "org.example.m2gtk4.Builder";
  23. VAR
  24. builder: ADDRESS;
  25. PROCEDURE OnClicked (button: ADDRESS; user: ADDRESS);
  26. BEGIN
  27. gtk_label_set_text(user, "Button clicked!")
  28. END OnClicked;
  29. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  30. VAR
  31. window, button, status: ADDRESS;
  32. err: GError;
  33. buf: ARRAY [0..255] OF CHAR;
  34. BEGIN
  35. builder := gtk_builder_new();
  36. err := NIL;
  37. IF gtk_builder_add_from_file(builder, "examples/builder.ui", err) = 0 THEN
  38. ErrorMessage(err, buf);
  39. printf("failed to load examples/builder.ui: %s\n", buf);
  40. printf("(run this example from the repository root)\n");
  41. ClearError(err);
  42. HALT(1)
  43. END;
  44. window := gtk_builder_get_object(builder, "window");
  45. button := gtk_builder_get_object(builder, "button");
  46. status := gtk_builder_get_object(builder, "status");
  47. Connect2(button, "clicked", OnClicked, status);
  48. gtk_window_set_application(window, application);
  49. gtk_window_present(window)
  50. END OnActivate;
  51. VAR
  52. app: ADDRESS;
  53. BEGIN
  54. app := gtk_application_new(AppId, 0);
  55. IF app = NIL THEN
  56. printf("gtk_application_new failed\n");
  57. HALT(1)
  58. END;
  59. Connect2(app, "activate", OnActivate, NIL);
  60. g_application_run(app, 0, NIL)
  61. END builder.