bohonghuang / cl-gtk4

GTK4/Libadwaita/WebKit2 bindings for Common Lisp.
GNU Lesser General Public License v3.0
215 stars 9 forks source link

How to get the string value from a `drop-down` whose model is a `string-list`? (bad FFI method "get_string") #69

Closed rgri closed 1 month ago

rgri commented 1 month ago

Hi, I cannot figure out how to get the value of a string corresponding to the currently selected item of a drop-down:

(let* ((stringlist (make-string-list :strings '("first string" "second string")))
       (dropdown (make-drop-down :model stringlist :expression nil)))
  (drop-down-selected dropdown) ; => 0
  (drop-down-selected-item dropdown) ; => #<GIR::OBJECT-INSTANCE {1009F61E33}>
  (string-list-get-string stringlist 0) ; => "first string"
  (string-object-string (drop-down-selected-item dropdown)) ; => bad FFI method "get_string"
  (string-list-get-string (drop-down-model dropdown) (drop-down-selected dropdown)) ; => The function COMMON-LISP:NIL is undefined.
  )

Reading the gnome forums it seems I should expect string-object-string to work, but I get

bad FFI method "get_string"

Similarly, the last line in my example code seems to break in some inscrutable way.

Edit: As a workaround, it seems that (gobj:coerce (drop-down-model dropdown) 'string-list) will return the original :model argument, i.e. stringlist. Similarly you can coerce the drop-down-selected-item to 'string-object.

bohonghuang commented 1 month ago

Edit: As a workaround, it seems that (gobj:coerce (drop-down-model dropdown) 'string-list) will return the original :model argument, i.e. stringlist. Similarly you can coerce the drop-down-selected-item to 'string-object.

Yes, this is the correct approach, but also a limitation, where the subtype information of the object returned from the GIR function will be lost. The same approach applies: if you use (setf gtk:window-child) to set the child of gtk:window, but when you call (gtk:window-child) on it, the object type you receive is only gtk:widget. In this case, you also need to use gobj:coerce to convert it back to its original type, which is the purpose of my adding that function to cl-glib.

rgri commented 1 month ago

Ok, well I guess this resolves the issue. Thanks. It seems like they even do something similar for the Nim bindings example I linked in my issue.