jhass / crystal-gobject

gobject-introspection for Crystal

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Arguments for connect callbacks

AndiLavera opened this issue · comments

Hi, i just started with GTK so if something is wrong on my part, i apologize.

I am trying to make a custom right click menu. The code below throws a Error: wrong number of block argument's arguments (given 1, expected 0):

connect("button-press-event", &->(e : Gdk::EventButton){
  if e.type == Gdk::EventType::BUTTON_PRESS && e.button == Gdk::BUTTON_SECONDARY
    menu.popup_at_pointer(e)
  end

  false
})

This is the entire code:

require "gobject/gtk"
require "gobject/g_lib"

class MainWindow < Gtk::ApplicationWindow
  def self.new(app : Gtk::Application)
    super
  end

  def initialize(ptr)
    super(ptr)

    self.border_width = 100
    self.title = "Hello"
    add Gtk::Label.new("Hello")

    hide_titlebar_when_maximized = true;
    maximize

    wallpaper_item = Gtk::MenuItem.new_with_label("Change Wallpaper…")
    displays_item = Gtk::MenuItem.new_with_label("Display Settings…")
    separator = Gtk::SeparatorMenuItem.new
    settings_item = Gtk::MenuItem.new_with_label("System Settings…")

    menu = Gtk::Menu.new
    menu.attach_to_widget(self, nil)
    menu.append(wallpaper_item);
    menu.append(displays_item);
    menu.append(separator);
    menu.append(settings_item);
    menu.show_all
    connect("button-press-event", &->(e : Gdk::EventButton){
      if e.type == Gdk::EventType::BUTTON_PRESS && e.button == Gdk::BUTTON_SECONDARY
        menu.popup_at_pointer(e)
      end

      false
    })

    # More to do down here
  end
end

class MyApp < Gtk::Application
  def self.new(id, flags : Gio::ApplicationFlags)
    super
  end

  def initialize(ptr)
    super(ptr)

    on_activate do |application|
      window = MainWindow.new(self)
      window.show_all
    end
  end
end

app = MyApp.new("org.crystal.mysample", :flags_none)
app.run()

So yeah, the connect helper only works if you don't care about the argument. Given the event name is passed as a string it cannot type the argument list for the given event.

That's why the lib also generally tries to generate helper method for each event, so you can do

    on_button_press_event do |_, e|
      # ...
    end

and that works in this case, except that e seems to be representing garbage data. I'm not sure yet what's going on there.

Okay figured it out. So in addition to the above you want to update to branch: master and then it should work now :)

Hey, thanks for such a quick reply! Your fix has worked. What's the best way to identify which methods are available from this lib? I didn't even know on_button_press_event existed. Like i am currently looking for AppInfo.launch_default_for_uri but i don't think that part of GLib is implemented.
What i am currently doing is {% MainWindow.ancestors %} and then AnAncestor.methods to see what's available. Is there a better method you would recommend?

If you have some patience you can try got get the gi-doc target running, basically make sure you could compile the compiler itself, clone this repo, shards build gi-doc, bin/gi-doc Gtk and then enjoy your one gigabyte of docs in docs/.

What I often do is just grep through the generated source code, so again clone this repo and then just crystal src/generator/build_namespace.cr Gtk > Gtk.cr for example.

There seems to be a Gio::AppInfo.launch_default_for_uri.

require "gobject"
require "gobject/g_lib"
require_gobject "Gio"

Gio::AppInfo.launch_default_for_uri("http://google.com", nil)

(should probably figure out a way to make the require "gobject/g_lib" unnecessary, but then loading up Gtk will load up all of these too already).

I would love an entire gigabyte of docs. Would be better than none! I can't build the docs. I have tried on debian & arch based distros. Tried with llvm 6 & 10. Also tried crenv crystal 0.33.0 & 0.34.0 & the snap version 0.34.0. Here is the error message for a crenv/llvm 6.0 environment:

L-L-V-M-5858P-assR-egistry.o: In function `initialize_inst_combine':
/home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/pass_registry.cr:11: undefined reference to `LLVMInitializeInstCombine'
/home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/ext/llvm_ext.o: In function `LLVMExtNewDIBuilder':
llvm_ext.cc:(.text+0x3f): undefined reference to `llvm::DIBuilder::DIBuilder(llvm::Module&, bool, llvm::DICompileUnit*)'
/home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/ext/llvm_ext.o: In function `LLVMExtDIBuilderCreateFile':
llvm_ext.cc:(.text+0x169): undefined reference to `llvm::DIBuilder::createFile(llvm::StringRef, llvm::StringRef, llvm::Optional<llvm::DIFile::ChecksumInfo<llvm::StringRef> >, llvm::Optional<llvm::StringRef>)'
/home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/ext/llvm_ext.o: In function `LLVMExtDIBuilderCreateCompileUnit':
llvm_ext.cc:(.text+0x2c5): undefined reference to `llvm::DIBuilder::createFile(llvm::StringRef, llvm::StringRef, llvm::Optional<llvm::DIFile::ChecksumInfo<llvm::StringRef> >, llvm::Optional<llvm::StringRef>)'
llvm_ext.cc:(.text+0x3de): undefined reference to `llvm::DIBuilder::createCompileUnit(unsigned int, llvm::DIFile*, llvm::StringRef, bool, llvm::StringRef, unsigned int, llvm::StringRef, llvm::DICompileUnit::DebugEmissionKind, unsigned long, bool, bool, llvm::DICompileUnit::DebugNameTableKind, bool)'
/home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/ext/llvm_ext.o: In function `LLVMExtDIBuilderCreateFunction':
llvm_ext.cc:(.text+0x5b1): undefined reference to `llvm::DIBuilder::createFunction(llvm::DIScope*, llvm::StringRef, llvm::StringRef, llvm::DIFile*, unsigned int, llvm::DISubroutineType*, unsigned int, llvm::DINode::DIFlags, llvm::DISubprogram::DISPFlags, llvm::MDTupleTypedArrayWrapper<llvm::DITemplateParameter>, llvm::DISubprogram*, llvm::MDTupleTypedArrayWrapper<llvm::DIType>)'
/home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/ext/llvm_ext.o: In function `LLVMExtDIBuilderCreateBasicType':
llvm_ext.cc:(.text+0x6e7): undefined reference to `llvm::DIBuilder::createBasicType(llvm::StringRef, unsigned long, unsigned int, llvm::DINode::DIFlags)'
/home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/ext/llvm_ext.o: In function `LLVMExtDIBuilderCreateEnumerationType':
llvm_ext.cc:(.text+0xd5c): undefined reference to `llvm::DIBuilder::createEnumerationType(llvm::DIScope*, llvm::StringRef, llvm::DIFile*, unsigned int, unsigned long, unsigned int, llvm::MDTupleTypedArrayWrapper<llvm::DINode>, llvm::DIType*, llvm::StringRef, bool)'
/home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/ext/llvm_ext.o: In function `LLVMExtDIBuilderCreateEnumerator':
llvm_ext.cc:(.text+0xdfe): undefined reference to `llvm::DIBuilder::createEnumerator(llvm::StringRef, long, bool)'
/home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/ext/llvm_ext.o: In function `LLVMExtSetCurrentDebugLocation':
llvm_ext.cc:(.text+0x14c8): undefined reference to `llvm::DebugLoc::get(unsigned int, unsigned int, llvm::MDNode const*, llvm::MDNode const*, bool)'
/home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/ext/llvm_ext.o: In function `LLVMExtWriteBitcodeWithSummaryToFile':
llvm_ext.cc:(.text+0x1d07): undefined reference to `llvm::WriteBitcodeToFile(llvm::Module const&, llvm::raw_ostream&, bool, llvm::ModuleSummaryIndex const*, bool, std::array<unsigned int, 5ul>*)'
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc "${@}" -o '/home/andrew/workspace/crystal/crystal-gobject/bin/gi-doc'  -rdynamic  -lz `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libssl || printf %s '-lssl -lcrypto'` `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '-lcrypto'` /home/andrew/.crenv/versions/0.34.0/share/crystal/src/llvm/ext/llvm_ext.o `/usr/bin/llvm-config-6.0 --libs --system-libs --ldflags 2> /dev/null` -lstdc++ -lgirepository-1.0 -lxml2 -lpcre -lm /home/andrew/.crenv/versions/0.34.0/bin/../lib/crystal/lib/libgc.a -lpthread /home/andrew/.crenv/versions/0.34.0/share/crystal/src/ext/libcrystal.a -levent -lrt -ldl -L/home/andrew/.crenv/versions/0.34.0/bin/../lib/crystal/lib -L/home/andrew/.crenv/versions/0.34.0/bin/../lib/crystal/lib`

Makes me wonder how it works on my system :D

Try with a custom build of the compiler:

git clone https://github.com/crystal/crystal
cd crystal
git checkout 0.34.0
make
export PATH="$(pwd)/bin:$PATH"
cd /.../crystal-gobject
shards build gi-doc

Or makes you wonder how anything ever works on my system. Error but rather similar. Googling this error shows some bug in llvm 6.0.0. Ill try building both crystal and the docs on arch later tonight.

Error:

CRYSTAL_CONFIG_PATH="/home/andrew/workspace/crystal-0.34.0/src" CRYSTAL_CONFIG_LIBRARY_PATH="/snap/crystal/331/bin/../lib/crystal/lib" CRYSTAL_CONFIG_BUILD_COMMIT="" ./bin/crystal build  -o .build/crystal src/compiler/crystal.cr -D without_openssl -D without_zlib
L-L-V-M-5858P-assR-egistry.o: In function `initialize_inst_combine':
/home/andrew/workspace/crystal-0.34.0/src/llvm/pass_registry.cr:11: undefined reference to `LLVMInitializeInstCombine'
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc "${@}" -o '/home/andrew/workspace/crystal-0.34.0/.build/crystal'  -rdynamic  /home/andrew/workspace/crystal-0.34.0/src/llvm/ext/llvm_ext.o `/usr/bin/llvm-config-6.0 --libs --system-libs --ldflags 2> /dev/null` -lstdc++ -lpcre -lm /snap/crystal/331/bin/../lib/crystal/lib/libgc.a -lpthread /home/andrew/workspace/crystal-0.34.0/src/ext/libcrystal.a -levent -lrt -ldl -L/snap/crystal/331/bin/../lib/crystal/lib -L/snap/crystal/331/bin/../lib/crystal/lib`
Makefile:117: recipe for target '.build/crystal' failed
make: *** [.build/crystal] Error 1

Okay, i built both shards & crystal from source. It took awhile but it built. Wonder why building crystal from source fixed the issue. Thanks!

It fixed it because crystal ships a little wrapper to wrap some of LLVM's C++ API into a C API, and for some reason your installs didn't ship the build of that to link against.

The doc tool doesn't need to invoke LLVM at all though, I gotta try to fix it linking against it.

Well thank you very much for all the help! It is greatly appreciated! And thanks for building this!