example-04-window-with-button.scm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #| GTK Application Templage (Guile Scheme version). |#
  2. (import (prefix (gi) gi:)
  3. ;; function of gi repository can be found at:
  4. ;; https://spk121.github.io/guile-gi/Typelib-Introspection.html
  5. (prefix (gi repository) gir:))
  6. ;; (use-typelibs ("GLib" "2.0")
  7. ;; ("Gio" "2.0")
  8. ;; ("Gtk" "3.0"))
  9. ;; Load Gio in version 2.0, but only, if it is not already
  10. ;; loaded.
  11. (gir:require "Gio" "2.0")
  12. ;; Load GTK in version 3.0, but only, if it is not already
  13. ;; loaded.
  14. (gir:require "Gtk" "3.0")
  15. ;; After having loaded the Gio and Gtk libraries, generate
  16. ;; bindings for the specified types.
  17. ;; (gir:load-by-name "Gio" "ActionMap") ; add-action
  18. (gir:load-by-name "Gio" "Application") ; activate, run
  19. ;; (gir:load-by-name "Gio" "SimpleAction")
  20. (gir:load-by-name "Gtk" "Application")
  21. (gir:load-by-name "Gtk" "ApplicationWindow")
  22. (gir:load-by-name "Gtk" "Button")
  23. (gir:load-by-name "Gtk" "ButtonBox")
  24. ;; (gir:load-by-name "Gtk" "ApplicationFlags") ; not valid
  25. ;; (gir:load-by-name "Gtk" "Builder")
  26. (gir:load-by-name "Gtk" "init")
  27. ;; (gir:load-by-name "Gtk" "MenuButton") ; set-popover
  28. ;; (gir:load-by-name "Gtk" "Popover")
  29. (gir:load-by-name "Gtk" "Widget") ; show-all
  30. ;; initialize GTK
  31. (init!)
  32. (define app:activate
  33. (λ (app)
  34. ;; ApplicationWindow has a constructur.
  35. (define window
  36. (gi:make <GtkApplicationWindow>
  37. ;; Set some values for the window.
  38. #:application app
  39. #:default-height 200
  40. #:default-width 200
  41. #:title "Example"))
  42. ;; (set-title window "Example")
  43. ;; (set-default-size window 200 200)
  44. ;; ButtonBox has a constructor
  45. ;; button-box:new. button-box:new expects an orientation
  46. ;; for the buttons to be put into the box.
  47. (define button-box
  48. (gi:make <GtkButtonBox>
  49. #:parent window))
  50. ;; Button has more than one constructor. Here the one is
  51. ;; used, which takes a string as a label.
  52. (define button
  53. (gi:make <GtkButton>
  54. #:parent button-box
  55. #:label "Exit"))
  56. ;; Define an action performed, when the button is
  57. ;; clicked.
  58. (gi:connect button clicked (λ args (destroy window)))
  59. ;; Next widgets are being added to other widgets and
  60. ;; ultimately to the window. This is done using the (add
  61. ;; ...) function.
  62. ;; Add the ButtonBox to the ApplicationWindow.
  63. ;; (add window button-box)
  64. ;; Add the Button to the ButtonBox.
  65. ;; (add button-box button)
  66. ;; The window needs to be added to the application.
  67. ;; (add-window app window)
  68. ;; Show the windows of the application.
  69. (show-all window)))
  70. (define main
  71. (λ ()
  72. ;; Call Gtk's application:new to create a new
  73. ;; <GtkApplication>
  74. (let ([app (gi:make <GtkApplication> #:application-id "org.gtk.example")
  75. #;(application:new
  76. "org.gtk.example"
  77. ;; Application flags govern how the application
  78. ;; behaves. See more at:
  79. ;; https://docs.gtk.org/gio/flags.ApplicationFlags.html.
  80. (list->application-flags '(flags-none)))])
  81. (gi:connect app activate app:activate)
  82. (exit
  83. ;; Call application:run. If there are no arguments in
  84. ;; the command line args, this will send the `activate'
  85. ;; signal.
  86. (run app (command-line))))))
  87. (main)