mouse coordinates in a gnome2 canvas

ruby

mouse coordinates in a gnome2 canvas

Postby Tool69 » Wed, 04 Jan 2006 23:54:43 GMT

Hi,
I want to show the mouse coordinates inside a label in a gnome2
application if my mouse is on a canvas. How to procede ? I' ve found no
documentation on the canvas on ruby gnome2 API. rbbr is not working on
my ubuntu (no description at all), so you're my last chance...
In fact, it's not really a canvas problem but more a gtk one. I've
tried something like :

@ecran = Gdk::Display.default
canvas.signal_connect("event") do
    pos = @ecran.window_at_pointer
    @label1.set_text("x=#{pos[1]}")
    @label2.set_text("y=#{pos[2]}")
end

...but then all my drawings disappear.

Thanks in advance :
6TooL9


Re: mouse coordinates in a gnome2 canvas

Postby Joe Van Dyk » Thu, 05 Jan 2006 02:56:39 GMT



require 'gnomecanvas2'

Gtk.init

window = Gtk::Window.new "Mouse Coordinate display"
window.set_default_size 300, 300
grid = Gtk::VBox.new
window << grid

canvas = Gnome::Canvas.new
grid.pack_start canvas

coord_label = Gtk::Label.new "sup"
grid.pack_start coord_label, false

window.show_all

window.signal_connect("destroy") { Gtk::main_quit }

canvas.signal_connect("motion-notify-event") do |widget, event|
  x = event.x
  y = event.y
  coord_label.text = "X Pos: #{ x }, Y Pos: #{ y }"
end

Gtk::main



Re: mouse coordinates in a gnome2 canvas

Postby Tool69 » Thu, 05 Jan 2006 03:55:55 GMT

Thanks Joe,
I thought that it was impossible to connect the canvas to something
other than "event".
Your code helped me a lot to understand.


Similar Threads:

1.ruby-gnome2 canvas pixbuf scaling

2.annoying problem with gnome2 canvas

Hello,
I'm trying different things with the gnome2canvas, but don't understand
the following behaviour :

#!/usr/bin/env ruby

require 'gtk2'
require 'gnomecanvas2'

def item_event(item, event)
    case event.event_type
    when Gdk::Event::BUTTON_PRESS
        item_x, item_y = item.parent.w2i(event.x, event.y)
      case event.button
      when 1
        if event.state & Gdk::Window::SHIFT_MASK ==
Gdk::Window::SHIFT_MASK
          item.destroy()
        else
          @x = item_x;
          @y = item_y;
          fleur = Gdk::Cursor.new(Gdk::Cursor::FLEUR)
          item.grab(Gdk::Event::POINTER_MOTION_MASK |
Gdk::Event::BUTTON_RELEASE_MASK,
                    fleur,
                    event.time)
          @dragging = true
        end
      when 2
        if event.state & Gdk::Window::SHIFT_MASK ==
Gdk::Window::SHIFT_MASK
          item.lower_to_bottom()
        else
          item.lower(1)
        end
      when 3
        if event.state & Gdk::Window::SHIFT_MASK ==
Gdk::Window::SHIFT_MASK
          item.raise_to_top()
        else
          item.raise(1)
        end
      end
    when Gdk::Event::MOTION_NOTIFY
      item_x, item_y = item.parent.w2i(event.x, event.y)
      if @dragging && (event.state & Gdk::Window::BUTTON1_MASK ==
Gdk::Window::BUTTON1_MASK)
        item.move(item_x - @x, item_y - @y)
        @x = item_x;
        @y = item_y;
      end
    when Gdk::Event::BUTTON_RELEASE
      item.ungrab(event.time)
      @dragging = false;
    end
end

def setup_item(item)
    item.signal_connect("event") do |item, event|
        item_event(item, event)
    end
end

Gtk.init

@ECX = 600
@ECY = 600

@combo1 = Gtk::ComboBox.new(true)
@combo1.append_text("Build")
@combo1.append_text("Move")
@combo1.set_active(0)

@labelcoord = Gtk::Label.new("")

@box1 = Gtk::HBox.new(false,5)
@boxv1 = Gtk::VBox.new(false,5)
@boxv2 = Gtk::VBox.new(false,5)

@canvas = Gnome::Canvas.new(true)

@zoomf = 1
@canvas.set_pixels_per_unit(@zoomf)
@canvas.set_size_request(@ECX, @ECY)
@canvas.set_scroll_region(0, 0, @ECX, @ECY)
@root = @canvas.root

# ScrolledWindow
@scroller = Gtk::ScrolledWindow.new
@scroller.set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC )
@scroller.add( @canvas )

# Main window
window = Gtk::Window.new
window.signal_connect("destroy") {
  puts "Bye."
  Gtk.main_quit
}

@boxv1.pack_start(@combo1,false, false, 0)
@box1.pack_start(@boxv1)
@box1.pack_start(@boxv2)
@boxv1.pack_start(@labelcoord,false, false, 0)
@boxv2.pack_start(@scroller,false, false, 0)
window.border_width = 10
window.add(@box1)
window.show_all
x,y = 200,200
pt = Gnome::CanvasEllipse.new(@root,{:x1 => x-3,
                                    :y1 => y-3,
                                    :x2 => x+3,
                                    :y2 => y+3,
                                    :outline_color => "goldenrod",
                                    :fill_color_rgba => 0x0000ff80,
                                    :width_units => 0.5})
setup_item(pt)

#@canvas.signal_connect("motion-notify-event") do |widget, event|
  #x,y = event.x,event.y
  #@labelcoord.set_text("X= #{x}, Y= #{y}")
#end

Gtk.main

You can move the point with the mouse easily, right?
Now, uncomment the last 4 lines before "Gtk.main" : I can't move the
point anymore, and I don't know why.
Thanks for your replies.

3.How to simulate Mouse Click with Ruby, or mouse library

4.[ruby-gnome2] general memory problems with ruby-gnome2?

 Hi all.

 This is not a message about a concrete error. I need your advice in
 order to explore memory problems with an application using ruby1.8
 and ruby-gnome2 in Linux Debian Sid.

 My application is relatively complex. It generates dinamic windows
 form represnting data in postgres database. Each window has a
 notebook with various pages, and each page has a treeview
 representing detail of data.

 So, in diary work, program creates and destroys windows continually.

 When I worked with ruby-gnome1, there was no memory problems and
 program was speedy. But now, with same estructure of data
 presentation and using ruby-gnome2, memory use grows without limit
 until machine begins to use virtual memory in great amounts, slowing
 work.

 I destroy each window properly when it is closed, but don't know if
 that is enought. Might I do anything more in order to reduce memory
 usage?

 Thanks for your advice. Greets.

 							David

5.Ruby Tk and X/Y coordinates

6. Ruby/Tk (X,Y) Coordinate Question

7. ruby graph: coordinate plane?

8. Mark GPS coordinates on image file



Return to ruby

 

Who is online

Users browsing this forum: No registered users and 45 guest