# Ruby-Gtk question

**URL:** <https://www.ruby-forum.com/t/ruby-gtk-question/156680>\
**Category:** Ruby-Gnome 2\
**Created:** [December 31, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/ruby-gtk-question/156680 "2008-12-31T21:42:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubinx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a5b964/32.png) [@rubinx](https://www.ruby-forum.com/u/rubinx)\
**Post date:** [December 31, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/ruby-gtk-question/156680/1 "2008-12-31T21:42:15Z")

</div>

Hi,

Is there an equivalent to the g\_signal\_connect\_swapped() gtk+ call  
in Ruby. Following is the c code that allows a button to destroy  
an including window:

±-----------------------------------------------------------+  
| g\_signal\_connect\_swapped (G\_OBJECT (button), “clicked”, |

| G\_CALLBACK (gtk\_widget\_destroy), |

| (gpointer) window); |  
±-----------------------------------------------------------+

The following seems to be the Ruby way to do the above:

±----------------------------------------------------+  
| button = Gtk::Button.new("\_Hello World") |  
| button.signal\_connect(“clicked”) { Gtk.main\_quit } |  
±----------------------------------------------------+

However, it is sometimes necessary to destroy a certain widget,  
and I believe it should be made available to the callback in  
some other way than via a global variable! Which brings me back  
to my original question.

---

<div class="post-metadata">

**Author:** ![rubinx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a5b964/32.png) [@rubinx](https://www.ruby-forum.com/u/rubinx)\
**Post date:** [January 1, 2009, 12:36am UTC](https://www.ruby-forum.com/t/ruby-gtk-question/156680/2 "2009-01-01T00:36:21Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] Ruby-Gtk question” on Wed, 31 Dec 2008  
21:42:15 +0100,  
Torli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ±-----------------------------------------------------------+  
> some other way than via a global variable! Which brings me back  
> to my original question.

You don’t need to use a global variable. Signal handlers are  
closure.

window = get\_window  
button = Gtk::Button.new(“\_Hello World”)  
button.signal\_connect(“clicked”) {window.destroy}

## Happy new year,

kou

---

<div class="post-metadata">

**Author:** ![rubinx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a5b964/32.png) [@rubinx](https://www.ruby-forum.com/u/rubinx)\
**Post date:** [January 1, 2009, 12:59am UTC](https://www.ruby-forum.com/t/ruby-gtk-question/156680/3 "2009-01-01T00:59:14Z")

</div>

Kouhei S. wrote:

> You don’t need to use a global variable. Signal handlers are  
> closure.

Of course, Kou. Now that you’ve mentioned it I do feel a bit silly.  
My mind has not yet switched back to Ruby, and perhaps I’m trying  
too hard to see relationships to the good old GTK+ foundation.

Thank you, and happy New Year to you too!  
Torli

---

<div class="post-metadata">

**Author:** ![rubinx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a5b964/32.png) [@rubinx](https://www.ruby-forum.com/u/rubinx)\
**Post date:** [January 6, 2009, 6:19pm UTC](https://www.ruby-forum.com/t/ruby-gtk-question/156680/4 "2009-01-06T18:19:20Z")

</div>

Cursor is a {{ Gdk::Window }} feature. Gtk provides {{ EventBox }} to  
access (extend) {{ Gdk }} functionality. In the following program we can  
see that the Ruby works with {{ Gdk::Events }}, however, I can not get  
the cursor working. Is this feature available in Ruby, or am I not doing  
something right?

±-------------------------------------------------------------+  
| require ‘gtk2’ |  
| |  
| def change\_label(event, label) |  
| if event.event\_type == Gdk::Event::BUTTON2\_PRESS |  
| if label.text[0].chr == ‘D’ |  
| label.text = “I Was Double-Clicked!” |  
| else |  
| label.text = “Double-Click Me Again!” |  
| end |  
| end |  
| end |  
| window = Gtk::Window.new(Gtk::Window::TOPLEVEL) |  
| |  
| window.set\_title “Two buttons” |  
| window.border\_width = 10 |  
| window.signal\_connect(‘delete\_event’) { false } |  
| window.signal\_connect(‘destroy’) { Gtk.main\_quit } |  
| |  
| eventbox = Gtk::EventBox.new |  
| label = Gtk::Label.new(‘Double-click me!’) |  
| |  
| eventbox.set\_events(Gdk::Event::BUTTON\_PRESS\_MASK) |  
| |  
| #??? |  
| # eventbox.set\_cursor(Gdk::Cursor.new(Gdk::Cursor::HAND1)) ? |  
| #??? |  
| |  
| eventbox.add(label) |  
| window.add(eventbox) |  
| |  
| eventbox.signal\_connect(‘button\_press\_event’) do |w, e| |  
| change\_label(e, label) |  
| end |  
| |  
| window.show\_all |  
| Gtk.main |  
±-------------------------------------------------------------+

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![rubinx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a5b964/32.png) [@rubinx](https://www.ruby-forum.com/u/rubinx)\
**Post date:** [January 6, 2009, 7:08pm UTC](https://www.ruby-forum.com/t/ruby-gtk-question/156680/5 "2009-01-06T19:08:20Z")

</div>

Hi Torli,

That’s not quite how this works. I spent a good deal of time trying to  
get this right before I finally found the useful solution.

On Tue, 2009-01-06 at 18:19 +0100, Torli B. wrote:

> | if event.event\_type == Gdk::Event::BUTTON2\_PRESS |  
> | window.border\_width = 10 |  
> | #??? |  
> You can only change the cursor for the GDK window, not the GTK window.  
> In your case, I’d try eventbox.visible\_window.cursor = ..whatever…

One other thing you might want to consider is not creating the same  
cursor all the time. I generally create a set of cursors that I can  
reference from everywhere in the code. You’ll also probably want to  
investigate the symbolic names as well as the constants. For example,  
to get the “appstart” cursor, you need to do:

appstart = Gdk::Cursor.new(Gdk::Display.default, “left\_ptr\_watch”)

_That_ little gem took me ages to find, so it might save you some  
trouble too.

Hope this helps,

## ast

Andrew S. Townley [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> **[The Personal Site of Andrew S. Townley](http://atownley.org)**

---

<div class="post-metadata">

**Author:** ![rubinx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a5b964/32.png) [@rubinx](https://www.ruby-forum.com/u/rubinx)\
**Post date:** [January 6, 2009, 8:11pm UTC](https://www.ruby-forum.com/t/ruby-gtk-question/156680/6 "2009-01-06T20:11:18Z")

</div>

Hi Andrew,

Andrew, are you using Ruby 1.9.x? I am running 1.8.6. This may be my  
problem. Namely, after adding :  
±----------------------------------------------------------------+  
| eventbox.realize |  
| eventbox.window.cursor = Gdk::Cursor.new(Gdk::Cursor::HAND1) |  
|# or |  
|#eventbox.window.set\_cursor(Gdk::Cursor.new(Gdk::Cursor::HAND1)) |  
±----------------------------------------------------------------+  
I get the following error on line {{ 3 require ‘gtk2’ }} :  
±-------------------------------------------------------------------+  
| eventbox.rb: line 3 |  
| Gtk-CRITICAL \*\*:gtk\_widget\_realize: assertion `GTK_WIDGET_ANCHORED | | (widget) || GTK_IS_INVISIBLE (widget)' failed | | eventbox.rb:26: undefined method`cursor=’ for nil:NilClass |  
| (NoMethodError) |  
±-------------------------------------------------------------------+

As you see line {{ eventbox.window.cursor =  
Gdk::Cursor.new(Gdk::Cursor::HAND1) }} also causes problems. Interpreter  
complains about undefined method {{ cursor }}, but I have trouble  
finding out where did the {{ window }} came from. Though I can find it  
in the book “Foundations of Gtk+ Development” where in the “C” code  
example it looks like {{ gtk\_window\_set\_cursor(eventbox-\>window,  
gtk\_cursor\_new(GTK\_HAND1)); }}, I on the other hand can not find it  
anywhere in Ruby/GNOME2/Gtk/Gdk documentation.

Regardless, I hope my problem is Ruby1.9.x/1.8.6 related.  
Thx, Torli.

---

<div class="post-metadata">

**Author:** ![rubinx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a5b964/32.png) [@rubinx](https://www.ruby-forum.com/u/rubinx)\
**Post date:** [January 6, 2009, 9:10pm UTC](https://www.ruby-forum.com/t/ruby-gtk-question/156680/7 "2009-01-06T21:10:47Z")

</div>

Andrew, now it’s time for me to self-followup :). Your suggestion  
works!!! I made mistake to call realize before I added it to the top  
window.

Thank you for your help,  
Torli

---

<div class="post-metadata">

**Author:** ![rubinx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a5b964/32.png) [@rubinx](https://www.ruby-forum.com/u/rubinx)\
**Post date:** [January 6, 2009, 7:14pm UTC](https://www.ruby-forum.com/t/ruby-gtk-question/156680/8 "2009-01-06T19:14:16Z")

</div>

Sorry for the self follow-up, but I didn’t have the ability to actually  
try this before. I’ve amended the example below and it seems to do what  
you intend.

Apologies for missing a couple of ordering issues.

On Tue, 2009-01-06 at 17:30 +0000, Andrew S. Townley wrote:

> > something right?  
> > | end |  
> > | label = Gtk::Label.new(‘Double-click me!’) |  
> > | |  
> > | eventbox.set\_events(Gdk::Event::BUTTON\_PRESS\_MASK)

eventbox.add(label)  
window.add(eventbox)  
window.show\_all

eventbox.realize  
eventbox.window.cursor = Gdk::Cursor.new(Gdk::Cursor::HAND1)

```
 eventbox.signal_connect('button_press_event') do |w, e|
     change_label(e, label)
 end

 Gtk.main

```

EOF

> reference from everywhere in the code. You’ll also probably want to  
> ast  
> –  
> Andrew S. Townley [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://atownley.org](http://atownley.org)
