jhass / crystal-gobject

gobject-introspection for Crystal
BSD 3-Clause "New" or "Revised" License
127 stars 13 forks source link

struct has no field 'to_unsafe' #5

Closed aeosynth closed 8 years ago

aeosynth commented 8 years ago
require "gobject/gdk"

attr = LibGdk::WindowAttr.new
win = Gdk::Window.new nil, attr, 0
$ crystal civ.cr
Error in ./civ.cr:4: instantiating 'Gdk::Window:Class#new(Nil, LibGdk::WindowAttr, Int32)'

win = Gdk::Window.new nil, attr, 0
                  ^~~

in ./libs/gobject/generated/gdk/window.cr:13: struct LibGdk::WindowAttr has no field 'to_unsafe'

      __return_value = LibGdk.window_new(parent && parent.to_unsafe.as(LibGdk::Window*), attributes.to_unsafe.as(LibGdk::WindowAttr*), attributes_mask)

                   ^~~~~~~~~

i'm not clear on when to use LibGdk vs Gdk

edit: it looks like we're supposed to do this?:

attrV = LibGdk::WindowAttr.new
attr = Gdk::WindowAttr.new pointerof(attrV)
jhass commented 8 years ago

You should never need to use Lib directly, I consider each time you have to a missing feature in the generator.

With d9fa233809fa5fe1fa14946b8362cc32f889e4d8 it now generates wrappers for struct attributes and a constructor for for them. So now it should be:

window = Gdk::Window.new(nil,
  Gdk::WindowAttr.new(title: "Hello World"),
  Gdk::WindowAttributesType.flags(TITLE)
)

However it doesn't work for me

(process:28243): Gdk-CRITICAL **: gdk_screen_get_root_window: assertion 'GDK_IS_SCREEN (screen)' failed

(process:28243): Gdk-CRITICAL **: gdk_window_new: assertion 'GDK_IS_WINDOW (parent)' failed

So https://github.com/GNOME/gtk/blob/master/gdk/gdkwindow.c#L1298 fails for me for some reason. Is it because I'm on wayland?

aeosynth commented 8 years ago

c code will fail similarly; you have to call gdk's init to fix those errors, and set the window_type attribute to actually display the window

i get this error now when setting an attribute:

require "gobject/gdk"

attr = Gdk::WindowAttr.new
attr.window_type = Gdk::WindowType::TOPLEVEL
$ crystal gdk.cr
Error in ./gdk.cr:4: instantiating 'Gdk::WindowAttr#window_type=(LibGdk::WindowType)'

attr.window_type = Gdk::WindowType::TOPLEVEL
     ^~~~~~~~~~~

in ./libs/gobject/generated/gdk/window_attr.cr:102: undefined method 'window_type=' for Nil

      to_unsafe.value.window_type = value
                      ^~~~~~~~~~~

================================================================================

Nil trace:

  /usr/lib/crystal/primitives.cr:169

      def value : T
jhass commented 8 years ago

The following compiles now and has a zero exit code for me:

Gdk.init

window = Gdk::Window.new(nil,
  Gdk::WindowAttr.new(title: "Hello World", window_type: Gdk::WindowType::TOPLEVEL),
  Gdk::WindowAttributesType.flags(TITLE)
)

A sample that actually shows something on the screen would be welcome :)

aeosynth commented 8 years ago
require "gobject/gdk"

Gdk.init

attr = Gdk::WindowAttr.new(
  width: 800,
  height: 600,
  window_type: Gdk::WindowType::TOPLEVEL
  )
mask = Gdk::WindowAttributesType::ZERO_NONE

win = Gdk::Window.new nil, attr, mask
win.show

LibGLib.main_loop_run LibGLib.main_loop_new nil, true

GLib::MainLoop.new has a type mismatch - https://github.com/jhass/crystal-gobject/blob/master/src/generated/g_lib/main_loop.cr#L6:

  ptr = Pointer(UInt8).malloc(0, 0)
jhass commented 8 years ago

Thanks, I added https://github.com/jhass/crystal-gobject/blob/master/samples/gdk_window.cr For me it just generates an invisible window, if there's something easy to draw something on to it that would be nice, but that's better than nothing I guess :)

aeosynth commented 8 years ago

i use a tiling wm, so i didn't notice that. i'm using cairo to draw an image, but i need to create a binding for cairo_paint, so not very easy.

aeosynth commented 8 years ago

btw, is there a reason you use flags(TITLE) in Gdk::WindowAttributesType.flags(TITLE), instead of ::TITLE?

jhass commented 8 years ago

No strong one, just to hint the macro, since in a real usecase you likely want to set more of the optional attributes, and Gdk::WindowAttributesType.flags(TITLE, X, Y) is a lot less verbose than Gdk::WindowAttributesType::TITLE|Gdk::WindowAttributesType::X|Gdk::WindowAttributesType::Y.

aeosynth commented 8 years ago

makes sense. thanks for fixing all these issues!

jhass commented 8 years ago

Thanks for reporting!