andy128k / cl-gobject-introspection

BSD 2-Clause "Simplified" License
49 stars 15 forks source link

Web view not displaying in box #71

Closed Ambrevar closed 4 years ago

Ambrevar commented 4 years ago

Still new to gobject-introspection, so I may be doing something stupid :)

Using the hello.lisp example, I'm trying to display a web view next to a widget:

(cl:defpackage #:gir-test-web
  (:use #:cl))
(in-package #:gir-test-web)

(defvar *gtk* (gir:ffi "Gtk"))
(defvar *webkit* (gir:require-namespace "WebKit2"))

(cffi:defcallback hello :void ((btn-ptr :pointer))
  (let ((button (gir::build-object-ptr (gir:nget *gtk* "Button") btn-ptr)))
    (setf (gir:property button 'label) "OK"))
  (format t "Hello, pressed~%"))

(defun main ()
  (gir:invoke (*gtk* 'init) nil)
  (let ((window (gir:invoke (*gtk* "Window" 'new)
                            (gir:nget *gtk* "WindowType" :toplevel)))
        (view (gir:invoke (*webkit* "WebView" 'new)))
        (button (gir:invoke (*gtk* "Button" 'new-with-label) "Hello, world!"))
        (button2 (gir:invoke (*gtk* "Button" 'new-with-label) "Dummy!"))
        (box (gir:invoke (*gtk* "Box" 'new)
                         (gir:nget *gtk* "Orientation" :vertical)
                         0)))
    (gir::g-signal-connect-data (gir::this-of window)
                                "destroy"
                                (cffi:foreign-symbol-pointer "gtk_main_quit")
                                (cffi:null-pointer)
                                (cffi:null-pointer)
                                0)
    (gir:connect button :clicked
                 (lambda (button)
                   (setf (gir:property button 'label) "OK")))
    (gir:invoke (view 'load_uri) "https://gnu.org")
    (gir:invoke (box 'add) button)
    (gir:invoke (box 'add) view)
    (gir:invoke (box 'add) button2)
    (gir:invoke (window 'add) box)
    (gir:invoke (window 'show-all))
    (gir:invoke (*gtk* 'main))))

The above only displays the 2 buttons, but nothing in between. If I call

(gir:invoke (window 'add) view)

then the view displays (instead of the buttons).

Any idea what's wrong?

andy128k commented 4 years ago

This is because you use add method to which is gtk_container_add. You should use box-specific methods like pack-start and pack-end (gtk_box_pack_start and gtk_box_pack_end). To fix your example just replace those lines with something like

    (gir:invoke (box 'pack-start) button t t 0)
    (gir:invoke (box 'pack-start) view t t 0)
    (gir:invoke (box 'pack-start) button2 t t 0)

Add something like this also to make window larger (setf (gir:property window 'height-request) 500)

Ambrevar commented 4 years ago

This works, thanks a lot!