example-05-table.scm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ;;; Example adapted from:
  2. ;;; https://github.com/spk121/guile-gi/files/7915746/gtk-tree-store.scm.txt
  3. ;;; Some formatting by me. Most comments by me.
  4. (import (gi)
  5. (gi repository)
  6. (gi types)
  7. (gi util)
  8. ;; receive multiple values
  9. (ice-9 receive))
  10. ;; guile-gi internally creates GOOPS classes for GTK and other GObject
  11. ;; Introspection things. Methods of such classes can have the same name. The
  12. ;; question is how those methods are named, when importing methods of same name
  13. ;; into the same module. The following like tells Guile how to handle that
  14. ;; case. For more detail see:
  15. ;; https://www.gnu.org/software/guile/manual/html_node/Merging-Generics.html
  16. (push-duplicate-handler! 'merge-generics)
  17. (use-typelibs ("GLib" "2.0")
  18. ;; Which thing is renamed to what?
  19. (("Gio" "2.0") #:renamer (protect* '(application:new receive)))
  20. ;; Which thing is renamed to what?
  21. (("Gtk" "3.0") #:renamer (protect* '(tree-store:new) 'gtk::))
  22. ("Gdk" "3.0"))
  23. (define (activate app)
  24. (let* (#|main window|#
  25. [window (application-window:new app)]
  26. ;; layout manager: grid layout
  27. [grid (grid:new)]
  28. [store
  29. ;; Create a store, which accepts the types, which
  30. ;; the columns will contain. The store will back
  31. ;; the tree view and contain the actual data. The
  32. ;; data will be rendered by renderers, which one
  33. ;; needs to specify. The rendered data will be
  34. ;; displayed in the tree view.
  35. (gtk::tree-store:new (vector G_TYPE_INT G_TYPE_STRING))]
  36. [treeview (tree-view:new-with-model store)]
  37. ;; Columns and renderers. Renderers are default
  38. ;; text renderers.
  39. [column1 (tree-view-column:new)]
  40. [renderer1 (cell-renderer-text:new)]
  41. [column2 (tree-view-column:new)]
  42. [renderer2 (cell-renderer-text:new)])
  43. ;; Connect the window delete event with a closure, which
  44. ;; destroys the window and quits the main loop.
  45. (connect window
  46. delete-event
  47. ;; The callback gets 2 arguments. The widget
  48. ;; from which the event originated and the
  49. ;; event itself.
  50. (λ (window event)
  51. (gtk-widget-destroy window)
  52. (gtk-main-quit)
  53. #f)) ;; do not stop the event propagation
  54. ;; The tree view has columns and those columns have
  55. ;; titles. For those we set a title.
  56. (set-title column1 "Column 1")
  57. ;; (pack the column) Tell the tree view, that to make
  58. ;; the columns change their width to snugly fit their
  59. ;; content. For that, the tree view needs to know, how
  60. ;; to render its contents. This is why a renderer needs
  61. ;; to be specified.
  62. (pack-start column1 renderer1 #f)
  63. ;; ???
  64. (add-attribute column1 renderer1 "text" 0)
  65. ;; Add the column to the tree view.
  66. (append-column treeview column1)
  67. ;; Set the title for the other column.
  68. (set-title column2 "Column 2")
  69. ;; (pack the column)
  70. (pack-start column2 renderer2 #f)
  71. ;; ???
  72. (add-attribute column2 renderer2 "text" 1)
  73. ;; Add the column to the tree view.
  74. (append-column treeview column2)
  75. (let ([iter (make <GtkTreeIter>)]
  76. [val1 (make <GValue>)]
  77. [val2 (make <GValue>)])
  78. ;; Set an integer value to val1.
  79. (set! (val1 G_TYPE_INT) 0)
  80. ;; Set an integer value to val2.
  81. (set! (val2 G_TYPE_STRING) "hello world")
  82. ;; Insert the values into the store, which is backing
  83. ;; the tree view.
  84. (tree-store:insert-with-values! store
  85. iter
  86. #f
  87. 0
  88. (list->int-vector '(0 1))
  89. (vector val1 val2)))
  90. ;; Compose widgets.
  91. (add window grid)
  92. (add grid treeview)
  93. ;; Display the whole thing.
  94. (show-all window)))
  95. (define (main)
  96. (let ((app (application:new "org.gtk.example" (number->application-flags 0))))
  97. ;; Connect the application:activate function/method with the activate
  98. ;; function defined above.
  99. (connect app application:activate activate)
  100. (exit
  101. ;; Call application:run. If there are no arguments in the command line
  102. ;; args, this will send the `activate' signal.
  103. (run app (command-line)))))
  104. (main)