gtk-tree-store.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. ;; ???
  11. (push-duplicate-handler! 'merge-generics)
  12. (use-typelibs ("GLib" "2.0")
  13. ;; Which thing is renamed to what?
  14. (("Gio" "2.0") #:renamer (protect* '(application:new receive)))
  15. ;; Which thing is renamed to what?
  16. (("Gtk" "3.0") #:renamer (protect* '(tree-store:new) 'gtk::))
  17. ("Gdk" "3.0"))
  18. (define (activate app)
  19. (let* (#|main window|#
  20. [window (application-window:new app)]
  21. ;; layout manager: grid layout
  22. [grid (grid:new)]
  23. [store
  24. ;; Create a store, which accepts the types, which
  25. ;; the columns will contain. The store will back
  26. ;; the tree view and contain the actual data. The
  27. ;; data will be rendered by renderers, which one
  28. ;; needs to specify. The rendered data will be
  29. ;; displayed in the tree view.
  30. (gtk::tree-store:new (vector G_TYPE_INT G_TYPE_STRING))]
  31. [treeview (tree-view:new-with-model store)]
  32. ;; Columns and renderers. Renderers are default
  33. ;; text renderers.
  34. [column1 (tree-view-column:new)]
  35. [renderer1 (cell-renderer-text:new)]
  36. [column2 (tree-view-column:new)]
  37. [renderer2 (cell-renderer-text:new)])
  38. ;; Connect the window delete event with a closure, which
  39. ;; destroys the window and quits the main loop.
  40. (connect window
  41. delete-event
  42. ;; The callback gets 2 arguments. The widget
  43. ;; from which the event originated and the
  44. ;; event itself.
  45. (λ (window event)
  46. (gtk-widget-destroy window)
  47. (gtk-main-quit)
  48. #f)) ;; do not stop the event propagation
  49. ;; The tree view has columns and those columns have
  50. ;; titles. For those we set a title.
  51. (set-title column1 "Column 1")
  52. ;; (pack the column) Tell the tree view, that to make
  53. ;; the columns change their width to snugly fit their
  54. ;; content. For that, the tree view needs to know, how
  55. ;; to render its contents. This is why a renderer needs
  56. ;; to be specified.
  57. (pack-start column1 renderer1 #f)
  58. ;; ???
  59. (add-attribute column1 renderer1 "text" 0)
  60. ;; Add the column to the tree view.
  61. (append-column treeview column1)
  62. ;; Set the title for the other column.
  63. (set-title column2 "Column 2")
  64. ;; (pack the column)
  65. (pack-start column2 renderer2 #f)
  66. ;; ???
  67. (add-attribute column2 renderer2 "text" 1)
  68. ;; Add the column to the tree view.
  69. (append-column treeview column2)
  70. (let ([iter (make <GtkTreeIter>)]
  71. [val1 (make <GValue>)]
  72. [val2 (make <GValue>)])
  73. ;; Set an integer value to val1.
  74. (set! (val1 G_TYPE_INT) 0)
  75. ;; Set an integer value to val2.
  76. (set! (val2 G_TYPE_STRING) "hello world")
  77. ;; Insert the values into the store, which is backing
  78. ;; the tree view.
  79. (tree-store:insert-with-values! store
  80. iter
  81. #f
  82. 0
  83. (list->int-vector '(0 1))
  84. (vector val1 val2)))
  85. ;; Compose widgets.
  86. (add window grid)
  87. (add grid treeview)
  88. ;; Display the whole thing.
  89. (show-all window)))
  90. (define (main)
  91. (let ((app (application:new "org.gtk.example" (number->application-flags 0))))
  92. ;; Connect the application:activate function/method with the activate
  93. ;; function defined above.
  94. (connect app application:activate activate)
  95. (exit
  96. ;; Call application:run. If there are no arguments in the command line
  97. ;; args, this will send the `activate' signal.
  98. (run app (command-line)))))
  99. (main)