G-Golf usage questions, g-object-unref and others

Zelphir Kaltstahl <[email protected]> Tue, 27 May 2025 16:50:34 +0000
Newsgroups gmane.lisp.guile.user
Message-ID <[email protected]>
Hello Guile/G-Golf users,

I am slowly figuring things out with G-Golf and trying to build my own examples 
for how to do things with it. (repo: 
https://codeberg.org/ZelphirKaltstahl/guilt-gtk-g-golf-examples, or most recent 
branch: 
https://codeberg.org/ZelphirKaltstahl/guilt-gtk-g-golf-examples/src/branch/feature/add-menu-bar)

I have a few questions:

(Q1) Using G-Golf, does one ever have to call `g-object-unref`, or does G-Golf 
take care of that? For example it is explicitly shown in the GTK example at: 
https://docs.gtk.org/gtk3/class.ApplicationWindow.html#a-gtkapplicationwindow-with-a-menubar, 
where the reference for the "builder" is reduced, if I understand it correctly. 
I am wondering, whether G-Golf abstracts from this and maybe saves me from 
having to do that kind of stuff. It might prevent me from encapsulating things 
like in the following function: 
https://codeberg.org/ZelphirKaltstahl/guilt-gtk-g-golf-examples/src/commit/465d0a3263c36b46ff828544a353397c15103ba5/examples/menu-bar/main.scm#L88:

~~~~
;; A UI XML string can be processed by a GtkBuilder. Then one can get
;; a specific UI element from that builder. The builder has some
;; internal state, it seems, after processing the XML. To get a
;; specific UI element from the builder, one uses
;; `gtk-builder-get-object` and the id of the element, which must be
;; mentioned in the XML. One then gets a `GObject`, an instance of the
;; base widget class.
(define ui-xml-string->g-object
   (λ (ui-xml-string ui-elem-id)
     "Takes a UI XML string and an id of the element selected inside that
XML and returns a GtkBuilder."
     ;; for now ignore length and pass -1 as shown in example at
     ;;https://docs.gtk.org/gtk3/class.ApplicationWindow.html#a-gtkapplicationwindow-with-a-menubar
     (let ([ui-string-length -1])
       (gtk-builder-get-object (gtk-builder-new-from-string ui-xml-string ui-string-length)
                               ui-elem-id))))
~~~~

(Q2) What does `next-method` do in an `initialize` method? For example: 
https://codeberg.org/ZelphirKaltstahl/guilt-gtk-g-golf-examples/src/commit/465d0a3263c36b46ff828544a353397c15103ba5/examples/menu-bar/main.scm#L121:

~~~~
;; Create a window inheriting from <gtk-application-window>. Currently
;; serves no purpose other than being a placeholder for future
;; specialization of the window.
(define-class <menu-bar-demo-window> (<gtk-application-window>))


;; Define the `initialize` method for the Gtk.ApplicationWindow
;; class. This method will be called by GTK itself, when the window is
;; created apparently.
(define-method (initialize (self <menu-bar-demo-window>) initargs)
   ;; No idea what `next-method` does.
   (next-method))
~~~~

It took me a long time to get the menu bar added to the application. The 
examples show a way how to do things, but this is not always easily adapted or 
understood. Specifically with the menu bar I experienced the following issues in 
getting it to work:

(1) The adwaita example renders the _whole_ window from a template, not only the 
menu bar.
(2) The adwaita example does not show you how to add a menu bar that is 
dynamically built from a string to an application.
(3) The example does not show how to only load parts of the UI from a file. One 
can have a Gtk ApplicationWindow and use the #:template keyword arg. But what if 
you want to populate or create another widget, like in my case the menu? Then 
you will need to search a lot and try a lot, before you can make it work.
(4) The examples have almost zero explanatory comments in them.
(5) I did not understand at all, why there is an SXML representation of the UI 
templates in the code, but also those ".ui" files, with basically the same info 
in them, just that the formatting of the ".ui" files makes them utterly 
unreadable. At first I assumed, that some kind of GUI tool must have been used 
to output the XML in those ".ui" files. I tried to understand how that SXML is 
used in the code to actually make the menu, but I failed. That is because the 
actual example doesn't even reference the SXML anywhere. It uses the ".ui" 
files. But then why is the SXML there ... And only then it dawned on me ... The 
SXML is used in some kind of preparation step, to generate the ".ui" files! And 
that is also, why the function `|sxml->ui|` is not actually a function, that 
returns a value, but merely `display`s the XML string. I think the name of that 
procedure is misleading. Fortunately Guile / Scheme provides means to still get 
the string: `|with-output-to-string|` as seen in 
https://codeberg.org/ZelphirKaltstahl/guilt-gtk-g-golf-examples/src/commit/465d0a3263c36b46ff828544a353397c15103ba5/examples/menu-bar/main.scm#L39.

I think the examples could greatly benefit from actual explanation, also 
regarding the UI files and how the workflow of the developer working on the 
examples, instead of just the code.

While I want to make an application, I think I should continue putting examples 
into my repo, for myself, and for future readers, so that they don't have to 
figure out the same things I had to figure out. I will try to provide more 
examples in my repo, as long as I have motivation and energy to continue with 
this project.

(Q3) Maybe in the future I could contribute the examples to the G-Golf repo?

I think to get this little bit of code, one window with a dynamically created 
menu bar, I have spent around 4-6h. It is not a problem, since it is a free time 
project, but compared to any more mainstream way of making a GUI application, 
this is a long time to figure out the basics.

Keep in mind though, that I have never used GOOPS before. So that might also be 
part of the reason, why I don't get some ideas of the example code.

Also I probably still need to read more of the docs of Gio and Gtk (still not so 
clear to me what is what) and also G-Golf.

(Q4) In the past using JavaFX I have had the experience, that rendering UI from 
template files was adding noticeable delay when opening for example a dialog for 
the first time. That is why in that project I switched to writing classes and 
methods to build up the UI, which did not introduce such delay. I am not as far 
yet with G-Golf. I don't even have the main window figured out yet. But in your 
experience, how fast is rendering UI from those UI XML files? Is there a similar 
slowdown/delay? Or is GTK so good at parsing those UI files, that it is not 
noticeable?

Anyway, great to have gained the power of menu bar now! Menu bar items are still 
disabled, but I am guessing, that that is due to me not having any `GAction`s 
registered. `GAction`s I have already in another example, so if I marry those 2 
examples ... I should get working menu items!

Best regards,
Zelphir

-- 
repositories:https://codeberg.org/ZelphirKaltstahl