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
# callback needs to return true
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
# callback needs to return true
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
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