headerbar.mod 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. MODULE headerbar ;
  2. (*
  3. m2-GTK4 example - a header bar with window controls.
  4. A GtkApplicationWindow whose titlebar is a GtkHeaderBar:
  5. title widget - a label
  6. packed start - a "New" button (connected with GtkClosures.Connect2)
  7. packed end - a GtkMenuButton with an application menu
  8. (Bold toggle, Quit)
  9. plus the Ctrl+N / Ctrl+B / Ctrl+Q accelerators.
  10. Build: make examples
  11. Run: ./build/examples/headerbar
  12. *)
  13. FROM Gio IMPORT g_application_run, g_application_quit;
  14. FROM GtkApplication IMPORT gtk_application_new;
  15. FROM GtkApplicationWindow IMPORT gtk_application_window_new;
  16. FROM GtkWindow IMPORT gtk_window_set_title, gtk_window_set_default_size,
  17. gtk_window_set_titlebar, gtk_window_set_child, gtk_window_present;
  18. FROM GtkHeaderBar IMPORT gtk_header_bar_new, gtk_header_bar_set_title_widget,
  19. gtk_header_bar_pack_start, gtk_header_bar_pack_end;
  20. FROM GtkMenuButton IMPORT gtk_menu_button_new,
  21. gtk_menu_button_set_menu_model, gtk_menu_button_set_icon_name;
  22. FROM GtkLabel IMPORT gtk_label_new, gtk_label_set_text;
  23. FROM GtkButton IMPORT gtk_button_new_with_label;
  24. FROM GSimpleAction IMPORT g_simple_action_new, g_simple_action_new_stateful;
  25. FROM GActionMap IMPORT g_action_map_add_action;
  26. FROM GMenu IMPORT g_menu_new, g_menu_append;
  27. FROM GVariant IMPORT g_variant_new_boolean, g_variant_get_boolean;
  28. FROM GtkUtils IMPORT Connect, SetAccel;
  29. FROM GtkClosures IMPORT Connect2;
  30. FROM SYSTEM IMPORT ADDRESS, ADR;
  31. FROM libc IMPORT printf;
  32. CONST
  33. AppId = "org.example.m2gtk4.HeaderBar";
  34. VAR
  35. app, status: ADDRESS;
  36. PROCEDURE OnQuit (action: ADDRESS; param: ADDRESS; data: ADDRESS);
  37. BEGIN
  38. g_application_quit(app)
  39. END OnQuit;
  40. (* GtkClosures.Handler2: void handler(instance, user_data) *)
  41. PROCEDURE OnNew (instance: ADDRESS; user: ADDRESS);
  42. BEGIN
  43. gtk_label_set_text(status, "New selected")
  44. END OnNew;
  45. PROCEDURE OnBold (action: ADDRESS; param: ADDRESS; data: ADDRESS);
  46. BEGIN
  47. IF g_variant_get_boolean(param) # 0 THEN
  48. gtk_label_set_text(status, "Bold on")
  49. ELSE
  50. gtk_label_set_text(status, "Bold off")
  51. END
  52. END OnBold;
  53. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  54. VAR
  55. window, header, newButton, menuButton, menu, action, title: ADDRESS;
  56. BEGIN
  57. (* app-scoped action: quit *)
  58. action := g_simple_action_new("quit", NIL);
  59. Connect(action, "activate", ADR(OnQuit), NIL);
  60. g_action_map_add_action(application, action);
  61. window := gtk_application_window_new(application);
  62. (* window-scoped stateful action: bold *)
  63. action := g_simple_action_new_stateful("bold", NIL,
  64. g_variant_new_boolean(0));
  65. Connect(action, "change-state", ADR(OnBold), NIL);
  66. g_action_map_add_action(window, action);
  67. (* header bar: title, start button, end-packed menu button *)
  68. title := gtk_label_new("m2-GTK4 Header Bar");
  69. header := gtk_header_bar_new();
  70. gtk_header_bar_set_title_widget(header, title);
  71. newButton := gtk_button_new_with_label("New");
  72. Connect2(newButton, "clicked", OnNew, NIL);
  73. gtk_header_bar_pack_start(header, newButton);
  74. menu := g_menu_new();
  75. g_menu_append(menu, "Bold", "win.bold");
  76. g_menu_append(menu, "Quit", "app.quit");
  77. menuButton := gtk_menu_button_new();
  78. gtk_menu_button_set_icon_name(menuButton, "open-menu-symbolic");
  79. gtk_menu_button_set_menu_model(menuButton, menu);
  80. gtk_header_bar_pack_end(header, menuButton);
  81. status := gtk_label_new("Ready");
  82. SetAccel(application, "app.quit", "<primary>q");
  83. SetAccel(application, "win.bold", "<primary>b");
  84. gtk_window_set_title(window, "m2-GTK4 Header Bar");
  85. gtk_window_set_default_size(window, 420, 220);
  86. gtk_window_set_titlebar(window, header);
  87. gtk_window_set_child(window, status);
  88. gtk_window_present(window)
  89. END OnActivate;
  90. VAR
  91. rc: INTEGER;
  92. BEGIN
  93. app := gtk_application_new(AppId, 0);
  94. IF app = NIL THEN
  95. printf("gtk_application_new failed\n");
  96. HALT(1)
  97. END;
  98. Connect(app, "activate", ADR(OnActivate), NIL);
  99. rc := g_application_run(app, 0, NIL)
  100. END headerbar.