Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I make Gobject? #27

Closed
bigos opened this issue Sep 2, 2023 · 12 comments
Closed

How do I make Gobject? #27

bigos opened this issue Sep 2, 2023 · 12 comments

Comments

@bigos
Copy link

bigos commented Sep 2, 2023

I need it for radio and checkbox menus.

https://gitlab.gnome.org/GNOME/gtk/blob/main/examples/application9/exampleappwin.c

https://docs.gtk.org/gobject/ctor.Object.new.html

@bigos
Copy link
Author

bigos commented Sep 3, 2023

@bigos
Copy link
Author

bigos commented Sep 3, 2023

https://docs.gtk.org/gtk4/class.Builder.html#property-bindings

builder may have a clue, tomorrow I will see if I can use that to create the objects I want

@bohonghuang
Copy link
Owner

Sorry, I can't think of a scenario where you would directly create a GObject. If you want to create a subclass object of GObject from another GObject, you can use gobj:coerce to achieve that.

@bigos
Copy link
Author

bigos commented Sep 3, 2023

I suspect the GTK4 documentation is not precise, and I do not need an instance of Gobject, but an instance of a class that inherits from Gobject. The bindings provide such functions so it is a matter of finding how to make something similar work in Lisp.

https://stackoverflow.com/questions/66509574/gio-menu-items-always-disabled-for-boolean-actions

@bigos
Copy link
Author

bigos commented Sep 3, 2023

https://gtk-rs.org/gtk4-rs/stable/latest/book/actions.html

This appears to be closest to what I want.

@bohonghuang
Copy link
Owner

bohonghuang commented Sep 3, 2023

I guess this might be the example you're looking for:

(define-application (:name stateful-menu
                     :id "org.bohonghuang.gtk4-example.stateful-menu")
  (defun stateful-menu-menu ()
    (let ((menu (gio:make-menu)))
      (let ((submenu (gio:make-menu)))
        (gio:menu-append-item submenu (gio:make-menu-item :model menu :label "Fullscreen" :detailed-action "app.fullscreen"))
        (gio:menu-append-item submenu (gio:make-menu-item :model menu :label "Always On Top" :detailed-action "app.always-on-top"))
        (gio:menu-append-submenu menu "Window" submenu))
      (let ((submenu (gio:make-menu)))
        (gio:menu-append-item submenu (let ((item (gio:make-menu-item :model menu :label "Light" :detailed-action "app.color-scheme")))
                                        (setf (gio:menu-item-action-and-target-value item) (list "app.color-scheme" (glib:make-string-variant :string "LIGHT")))
                                        item))
        (gio:menu-append-item submenu (let ((item (gio:make-menu-item :model menu :label "Dark" :detailed-action "app.color-scheme")))
                                        (setf (gio:menu-item-action-and-target-value item) (list "app.color-scheme" (glib:make-string-variant :string "DARK")))
                                        item))
        (gio:menu-append-submenu menu "Color Scheme" submenu))
      (values menu)))
  (define-main-window (window (make-application-window :application *application*))
    (setf (window-title window) "Stateful Menu")
    (let ((header-bar (make-header-bar)))
      (let ((menu-button (make-menu-button)))
        (setf (menu-button-menu-model menu-button) (stateful-menu-menu)
              (button-icon-name menu-button) "open-menu-symbolic")
        (header-bar-pack-end header-bar menu-button))
      (setf (window-titlebar window) header-bar))
    (let ((action (gio:make-stateful-simple-action :name "fullscreen" :parameter-type nil :state (glib:make-boolean-variant :value nil))))
      (gio:action-map-add-action *application* action))
    (let ((action (gio:make-stateful-simple-action :name "always-on-top" :parameter-type nil :state (glib:make-boolean-variant :value t))))
      (gio:action-map-add-action *application* action))
    (let ((action (gio:make-stateful-simple-action :name "color-scheme"
                                                   :parameter-type (glib:make-variant-type :type-string "s")
                                                   :state (glib:make-string-variant :string "LIGHT"))))
      (gio:action-map-add-action *application* action))
    (let ((window-box (make-box :orientation +orientation-vertical+
                                :spacing 0)))
      (let ((menu-bar (make-popover-menu-bar :model (simple-menu-menu))))
        (box-append window-box menu-bar))
      (let ((empty-box (make-box :orientation +orientation-vertical+
                                 :spacing 0)))
        (setf (widget-size-request empty-box) '(400 200))
        (box-append window-box empty-box))
      (setf (window-child window) window-box))
    (unless (widget-visible-p window)
      (window-present window))))

@bigos
Copy link
Author

bigos commented Sep 3, 2023

Yes, it appears to be what I was looking for. Thank you very much. I guess that closes the issue. If I have more specific question I will ask it another time.

@bohonghuang
Copy link
Owner

I suggest you close this issue so that you can reopen it anytime if you have any further questions.

@bigos
Copy link
Author

bigos commented Sep 3, 2023

Thank you for your hekp

@bigos bigos closed this as completed Sep 3, 2023
@bigos bigos reopened this Sep 3, 2023
@bigos
Copy link
Author

bigos commented Sep 3, 2023

Good job the checkbox on the menu shows, but how do I go to the next step and toggle the checkbox on several subsequent clicks? I only managed to uncheck the boolean menu item.

@bigos
Copy link
Author

bigos commented Sep 3, 2023

I figured out the way to toggle the state. Glib:variant-hash returns 1 or 0 for boolean variants. It is not ideal but it works. Can I close the issue now? :-)

                   ;; toggle the state
                   (gio:action-change-state menu-action (glib:make-boolean-variant
                                                         :value (if (zerop (glib:variant-hash (gio:action-state menu-action)))
                                                                    T
                                                                    nil)))

@bohonghuang
Copy link
Owner

Sure ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants