Forum: wxRuby evt_button + images_click

Posted by Misha Ognev (rarstd)
on 2010-11-12 23:24
Hello.

I have two questions.

Here is my code:
---------
require "wx"

class FFF < Wx::Frame
  def initialize
    @b = Array.new
    super(nil, 2, "Bljllll", [5,5], [640,480])
    @mypanel = Wx::Panel.new(self)
    for i in 0..6
      @b[i] = Wx::Button.new(@mypanel, i, "#{i}", [10+25*i, 10],
[20,20])
      evt_button(i){puts i}
    end
  end

end

Wx::App.run do
  FFF.new.show
end
--------

1) I'm create pre-condition number of buttons(7 in example above). What
I must do to set every button event which puts button id(btw, it's equal
label of buttons too)? For example: if I print button "0", it must puts
0, not 6 as a my code(in my code click to all buttons puts last number
in array, here it is 6).

2) Are there events to images such as click and mouse_on?
Posted by Mario Steele (Guest)
on 2010-11-13 08:11
(Received via mailing list)
Hello Michael,

On Fri, Nov 12, 2010 at 5:24 PM, Misha Ognev <lists@ruby-forum.com> 
wrote:

>    @b = Array.new
>    super(nil, 2, "Bljllll", [5,5], [640,480])
>    @mypanel = Wx::Panel.new(self)
>    for i in 0..6
>      @b[i] = Wx::Button.new(@mypanel, i, "#{i}", [10+25*i, 10],
> [20,20])
>      evt_button(i){puts i}
>
        evt_button(@b[i]) { puts i }

> 1) I'm create pre-condition number of buttons(7 in example above). What
> I must do to set every button event which puts button id(btw, it's equal
> label of buttons too)? For example: if I print button "0", it must puts
> 0, not 6 as a my code(in my code click to all buttons puts last number
> in array, here it is 6).
>

This is answered in-line in the code.


> 2) Are there events to images such as click and mouse_on?
>

I believe the standard event handlers for StaticBitmap will work, as
it's hierarchy is Object > EvtHandler > Window > Control > StaticBitmap.

Standard Images, through Wx::Image, Wx::Bitmap, and such, do not have
Events, as they are considered Memory Objects, not Actual Controls.

Basically, if you look at the docs for any class we have, and it has
somewhere in it's hierarchy EvtHandler, then there are events that the 
Class
will receive.

hth,

Mario
Posted by Misha Ognev (rarstd)
on 2010-11-13 13:39
1) evt_button(@b[i]) { puts i } doesn't work I need. It works like a 
evt_button(i){puts i}. I don't know why.
Posted by Łukasz Korecki (plugawy)
on 2010-11-13 21:56
Hi!

That's the way to do it - it's not very clean since it probably will be 
triggered by any button event
but you can process only events starting with a specific value etc and 
ignore the rest (and use more strict handlers):

    class FFF < Wx::Frame
      def initialize
        @b = Array.new
        super(nil, 2, "Bljllll", [5,5], [640,480])
        @mypanel = Wx::Panel.new(self)
        for i in 0..6
          @b[i] = Wx::Button.new(@mypanel, i, "#{i}", [10+25*i, 10],
    [20,20])
        end
       # here you specify global event handler which
      # will process any button press
      # this uses event delegation and works dynamically
      # so it will works regardless of number of buttons
        evt_button(Wx::ID_ANY){ |ev| puts ev.get_id }
      end

    end

If you want clickable images you should use Wx::BitmapButton class: 
http://wxruby.rubyforge.org/doc/bitmapbutton.html

Hope that helps :-)


Łukasz
Posted by Misha Ognev (rarstd)
on 2010-11-15 14:00
Hello else one.

I decided a problem 1):

evt_button(i){|xx| puts xx.get_id}

Now working with problem 2
Posted by Łukasz Korecki (plugawy)
on 2010-11-15 14:28
Misha Ognev wrote in post #961550:
> Hello else one.
>
> I decided a problem 1):
>
> evt_button(i){|xx| puts xx.get_id}
>
> Now working with problem 2

Did you try wx::BitmapButton?


Łukasz
Posted by Misha Ognev (rarstd)
on 2010-11-15 19:00
Can I match the event "click right button of the mouse? (non left)"?

Hello. I will try BitmapButton some later.
Posted by Alex Fenton (Guest)
on 2010-11-16 14:03
(Received via mailing list)
Hi

Yes, there is evt_right_down, evt_right_up and evt_right_dclick (and
also similar events for the middle button).

Note that if you're creating context menus (a common use for right
clicks) it is better to use evt_context_menu which abstracts away
platform differences in the means by which such a menu is summoned and
dismissed.

alex
Posted by Misha Ognev (rarstd)
on 2010-11-18 20:15
Can you help me?

For example, I create the button:

b = Wx::Button.new(@mypanel, 10, "This is button")
evt_button(10){puts "This is button-10"} # Here is event for left-click 
mouse.

Can you write event for b, but with the right click?
Posted by Misha Ognev (rarstd)
on 2010-11-19 01:33
I tried to add evt_right_dclick, for example, but this is an error:

C:/Ruby/lib/ruby/gems/1.9.1/gems/wxruby-ruby19-2.0.1-x86-mingw32/lib/wx/classes/evthandler.rb:136:in 
`acquire_handler': Specify event handler with a method, name, proc OR 
block (ArgumentError)
Posted by Misha Ognev (rarstd)
on 2010-11-20 19:01
Else one question: how can I close the frame and stop the program?

And how can I run program on ruby?
Posted by Mario Steele (Guest)
on 2010-11-21 02:01
(Received via mailing list)
It is easy to do:
evt_button(@button) do
  self.close
end

If that is the only window you have, the app should end on it's own, 
otherwise you will need to call Wx::App.exit_main_loop

hth,

Mario


Sent from my iPod
Posted by Misha Ognev (rarstd)
on 2010-11-23 23:32
Thanks.

Another question: how can I make in Wx::Frame a scrollbar(vertical)? I 
have big number of labels, that's not fit into screen.
Posted by Alex Fenton (Guest)
on 2010-11-25 16:33
(Received via mailing list)
On 23/11/10 22:32, Misha Ognev wrote:
> Another question: how can I make in Wx::Frame a scrollbar(vertical)? I
> have big number of labels, that's not fit into screen.
>

Maybe Wx::ScrolledWindow? Have a look at the samples as there are
several ways to use this class. The easiest way is with sizers where the
scrolling will be calculated automatically for you.

alex
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.