jhass / crystal-gobject

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

Getting mouse (x,y) position from Gtk::EventBox #72

Closed gummybears closed 4 years ago

gummybears commented 4 years ago

Working on a small program which loads an image into a Gtk::Image. I am trying to get the mouse (x,y) position when I click on the image and moves the mouse. Using a Gtk::EventBox around the Gtk::Image.

Things seem to work, loading and displaying the image, capturing the mouse down and mouse move events except I am not getting the correct x,y values. Either these values are not set or most likely I am doing something wrong.

Here is the code

require "gobject/gtk/autorun"

builder = Gtk::Builder.new_from_file("#{__DIR__}/main.glade")
builder.connect_signals

window = Gtk::Window.cast builder["window1"]
# close application when window's 'x' button is clicked
window.connect("destroy", &->Gtk.main_quit)
window.set_default_size(1920,1080)

menu_filequit = Gtk::MenuItem.cast(builder["file_quit"])
menu_fileopen = Gtk::MenuItem.cast(builder["file_open"])
viewport = Gtk::Viewport.cast(builder["viewport1"])
imagebuffer = Gtk::Image.cast(builder["image1"])
eventbox = Gtk::EventBox.cast(builder["eventbox1"])

eventbox.events = Gdk::EventMask::EXPOSURE_MASK | Gdk::EventMask::LEAVE_NOTIFY_MASK | Gdk::EventMask::BUTTON_PRESS_MASK | Gdk::EventMask::POINTER_MOTION_MASK | Gdk::EventMask::POINTER_MOTION_HINT_MASK

statusbar1  = Gtk::Statusbar.cast(builder["statusbar1"])
filechooser_open   = Gtk::Button.cast(builder["filechooser_open"])
filechooser_cancel = Gtk::Button.cast(builder["filechooser_cancel"])
filechooser_dialog  = Gtk::FileChooserDialog.cast(builder["filechooserdialog1"])

# event is of type Gdk::EventButton
eventbox.on_button_press_event do |widget,eventbutton|
  context_id = statusbar1.context_id("statusbar")
  statusbar1.push(context_id, "mouse moved (#{eventbutton.x},#{eventbutton.y})") 
  true
end

# event is of type Gdk::EventMotion
eventbox.on_motion_notify_event do |widget,eventmotion|
  context_id = statusbar1.context_id("statusbar")
  statusbar1.push(context_id, "mouse moved (#{eventmotion.x},#{eventmotion.y})")
  true
end

# exit application
menu_filequit.on_activate do |button|
  exit
end

# file chooser dialog
menu_fileopen.on_activate do |button|
  filechooser_dialog.show
end

filechooser_open.on_clicked do |button|
  x = filechooser_dialog.filename()
  if File.directory?(x) == false && File.readable?(x)

    filechooser_dialog.hide()
    window.title = window.title + " | " + "loaded image #{x}"
    imagebuffer.file = x
  end
end

filechooser_cancel.on_clicked do |button|
  filechooser_dialog.hide()
end

# maximize shows title and window buttons
window.maximize
window.show_all
gummybears commented 4 years ago

Tried the following

name: test
version: 1.0.0

dependencies:
  gobject:
    github: jhass/crystal-gobject
    version: ~> 0.8.0

Changed line 18 in lib/gobject/src/gtk/gtk.cr

require "signal"

require "../gobject"
require_gobject "Gtk", "3.0"

require "../g_object"
require "../gdk"

module Gtk
  class Window
    def self.new : self
      new type: Gtk::WindowType::TOPLEVEL
    end
  end

  class Builder
    def connect_signals
      connect_signals # nil <------- commented 
    end

    def [](id)
      object(id).not_nil!
    end

    def []?(id)
      object(id)
    end
  end

  class CssProvider
    def load_from_data(string : String)
      load_from_data(string.to_slice)
    end
  end

  class Application
    def run
      run Array.new(ARGC_UNSAFE) { |i| String.new(ARGV_UNSAFE[i]) }
    end
  end
end

and commented line 7 in my source program

builder = Gtk::Builder.new_from_file("#{__DIR__}/main.glade")
# builder.connect_signals <----- commented 

and removed the line

eventbox.events = Gdk::EventMask::EXPOSURE_MASK | Gdk::EventMask::LEAVE_NOTIFY_MASK | Gdk::EventMask::BUTTON_PRESS_MASK | Gdk::EventMask::POINTER_MOTION_MASK | Gdk::EventMask::POINTER_MOTION_HINT_MASK

I was able to build and run my test program, the file chooser dialog works, loading of a png image also works, and also got the correct (x,y) coordinates of the mouse.