# A working example for Gtk::StatusIcon

**URL:** <https://www.ruby-forum.com/t/a-working-example-for-gtk-statusicon/180232>\
**Category:** Ruby-Gnome 2\
**Created:** [December 15, 2009, 9:45am UTC](https://www.ruby-forum.com/t/a-working-example-for-gtk-statusicon/180232 "2009-12-15T09:45:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 15, 2009, 9:45am UTC](https://www.ruby-forum.com/t/a-working-example-for-gtk-statusicon/180232/1 "2009-12-15T09:45:44Z")

</div>

Hi everyone.

I wrote an article in french on how to use Gtk::StatusIcon.  
[http://blog.developpez.com/zik/p8460/ruby/icone-dans-la-zone-de-notification-en-ru/#more8460](http://blog.developpez.com/zik/p8460/ruby/icone-dans-la-zone-de-notification-en-ru/#more8460)  
The full code is at the very end of the article. If you think it can  
integrate the hiki samples I will add some english comments and publish  
it.

Regards.

Vincent.

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 15, 2009, 11:34am UTC](https://www.ruby-forum.com/t/a-working-example-for-gtk-statusicon/180232/2 "2009-12-15T11:34:05Z")

</div>

2009/12/15 Vincent C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi everyone.
> 
> I wrote an article in french on how to use Gtk::StatusIcon.  
> [http://blog.developpez.com/zik/p8460/ruby/icone-dans-la-zone-de-notification-en-ru/#more8460](http://blog.developpez.com/zik/p8460/ruby/icone-dans-la-zone-de-notification-en-ru/#more8460)

I don’t have a user on this site, so I will comment here.

- comments

1. 

> Si votre bibliothÃ¨que gtk est rÃ©cente,  
> You might want to look which version of gtk2, and ruby gtk2 you need,  
> and putit in the article.

1. 

The mask example is not very clear, you might want to make it more  
verbose :

si.signal\_connect(‘scroll-event’){|icon, event|  
etats\_a\_gerer = (Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK)  
etat\_actuel = (event.state & etat\_a\_gerer)  
case etat\_actuel  
when 0  
p ‘Aucun modificateur’  
when Gdk:🪟:CONTROL\_MASK  
p ‘Control’  
when Gdk:🪟:SHIFT\_MASK  
p ‘Shift’  
when (Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK)  
p ‘Control+Shift’  
end  
}

event.state est _un_ masque reprÃ©sentant l’Ã©tat de l’ensemble des  
modificateurs, par exemple du clavier. Dans le cas prÃ©sent, nous nous  
intÃ©ressons seulement aux modificateurs control et shift. Ã€ cet usage,  
nous filtrons les modificateurs complets avec un masque des  
modificateurs que nous voulons gÃ©rer. Puis nous comparons le rÃ©sultat  
aux diffÃ©rents cas possibles.

1. 

You are interchanging ‘on’ and ‘nous’, it would be better to only have  
one.

> Nous allons maintenant voir comment  
> On construit un menu auquel on ajoute

1. 

I don’t like much your code style, but it’s very subjective :).

1. 

Why is ‘print\_direction’ inside the case ? Don’t you want to print it,  
whatever command key is pressed ?

- bad practices :

si.signal\_connect(‘activate’){si.blinking=!(si.blinking?)}

You are using ‘si’ inside the block, but it might not be available in  
the context. Signals in gtk hands you back whatever you should work  
on.

si.signal\_connect(‘activate’){|icon| icon.blinking=!(icon.blinking?)}

- typos :

> il est nÃ©cessaire d’utiliser Ctrl+C rÃ©cupÃ©rer la main.  
> Ctrl+C _pour_ rÃ©cupÃ©rer

> IntÃ©rargir avec l’icÃ´ne  
> IntÃ©ragir

> La mÃ©thode embedded? permet de vÃ©rifier que la prÃ©sence d’une zone de notification  
> vÃ©rifier la prÃ©sence

> L’objet event retournÃ© lors appartient  
> alors (or nothing)

It’s a nice tutorial of StatusIcon otherwise, simple and to the point.

Simon

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 15, 2009, 11:55am UTC](https://www.ruby-forum.com/t/a-working-example-for-gtk-statusicon/180232/3 "2009-12-15T11:55:13Z")

</div>

Simon A. wrote:

> I don’t have a user on this site, so I will comment here.

Thanks for your comments. I will take account of your comments when  
rewritting this example.

> 1. 
> 
> > Si votre bibliothÃ¨que gtk est rÃ©cente,  
> > You might want to look which version of gtk2, and ruby gtk2 you need,  
> > and putit in the article.

I think gtk 2.16 is needed but I am not sure. It does not work with gtk  
2.14. Gtk documentation does not indicate in which version this feature  
was added.

> 1. 
> 
> Why is ‘print\_direction’ inside the case ? Don’t you want to print it,  
> whatever command key is pressed ?

You are rigth. In the present case, I should call ‘print\_direction’  
after the case.

> - bad practices :
> 
> si.signal\_connect(‘activate’){si.blinking=!(si.blinking?)}
> 
> You are using ‘si’ inside the block, but it might not be available in  
> the context. Signals in gtk hands you back whatever you should work  
> on.
> 
> si.signal\_connect(‘activate’){|icon| icon.blinking=!(icon.blinking?)}

I do that mistake very often. Thanks for pointing it. I have a lot of  
blocks in my code to correct!!

> It’s a nice tutorial of StatusIcon otherwise, simple and to the point.

Thanks.

> Simon

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 15, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/a-working-example-for-gtk-statusicon/180232/4 "2009-12-15T12:25:00Z")

</div>

On Tue, Dec 15, 2009 at 7:55 PM, Vincent C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think gtk 2.16 is needed but I am not sure. It does not work with gtk  
> 2.14. Gtk documentation does not indicate in which version this feature  
> was added.

Strange that it doesn’t work in 2.14 because I’ve been using  
StatusIcon for quite some time and the source actually says it’s  
supported since 2.10

> **[Ruby-GNOME 2](https://sourceforge.net/projects/ruby-gnome2/)**
>
> Download Ruby-GNOME 2 for free. This project is a set of Ruby language bindings for the various application development libraries included with the GNOME/GTK+ environment. This project is for GTK+2.0 or later.

Mathieu

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 15, 2009, 12:30pm UTC](https://www.ruby-forum.com/t/a-working-example-for-gtk-statusicon/180232/5 "2009-12-15T12:30:49Z")

</div>

Mathieu B. wrote:

> On Tue, Dec 15, 2009 at 7:55 PM, Vincent C.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I think gtk 2.16 is needed but I am not sure. It does not work with gtk  
> > 2.14. Gtk documentation does not indicate in which version this feature  
> > was added.
> 
> Strange that it doesn’t work in 2.14 because I’ve been using  
> StatusIcon for quite some time and the source actually says it’s  
> supported since 2.10
> 
> [http://ruby-gnome2.svn.sourceforge.net/viewvc/ruby-gnome2/ruby-gnome2/trunk/gtk/src/rbgtkstatusicon.c?revision=2292&view=markup](http://ruby-gnome2.svn.sourceforge.net/viewvc/ruby-gnome2/ruby-gnome2/trunk/gtk/src/rbgtkstatusicon.c?revision=2292&view=markup)
> 
> Mathieu

We were talking about the scroll-event signal which did not work under  
2.14. But you could not guess from the e-mails.

---

<div class="post-metadata">

**Author:** ![coubi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@coubi](https://www.ruby-forum.com/u/coubi)\
**Post date:** [December 16, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/a-working-example-for-gtk-statusicon/180232/6 "2009-12-16T19:21:30Z")

</div>

Vincent C. wrote:

Hi.

This is the code with english comments.

#!/usr/bin/env ruby

# encoding: UTF-8

=begin  
This example displays a icon in tray and show how to manage interactions  
with the mouse.  
Documentations on StatusIcon class are provided at  
[http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=view&p=Gtk%3A%3AStatusIcon](http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=view&p=Gtk%3A%3AStatusIcon)  
.  
=end

require ‘gtk2’

###\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*###

## Displayed Icon

###\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*###  
si=Gtk::StatusIcon.new  
##use a stock image  
si.stock=Gtk::Stock::DIALOG\_INFO  
##or a personnal one  
#si.pixbuf=Gdk::Pixbuf.new(‘/path/to/image’)  
si.tooltip=‘StatusIcon’

###\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*###

## Handle left click on icon

###\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*###  
si.signal\_connect(‘activate’){|icon| icon.blinking=!(icon.blinking?)}

###\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*###

## Pop up menu on rigth click

###\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*###  
##Build a menu  
info=Gtk::ImageMenuItem.new(Gtk::Stock::INFO)  
info.signal\_connect(‘activate’){p “Embedded: #{si.embedded?}”; p  
“Visible: #{si.visible?}”; p “Blinking: #{si.blinking?}”}  
quit=Gtk::ImageMenuItem.new(Gtk::Stock::QUIT)  
quit.signal\_connect(‘activate’){Gtk.main\_quit}  
menu=Gtk::Menu.new  
menu.append(info)  
menu.append(Gtk::SeparatorMenuItem.new)  
menu.append(quit)  
menu.show\_all  
##Show menu on rigth click  
si.signal\_connect(‘popup-menu’){|tray, button, time| menu.popup(nil,  
nil, button, time)}

###\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*###

## Handle scroll events

#\* Need a recent gtk version (\>=2.16 ?)  
###\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*###  
si.signal\_connect(‘scroll-event’){|icon, event|  
modifier=event.state#A GdkModifierType indicating the state of  
modifier keys and mouse buttons  
##Handle only control and shift key  
ctrl\_shift=(Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK)  
mod=modifier&ctrl\_shift  
case mod  
when 0  
print “(None)”  
when Gdk:🪟:CONTROL\_MASK  
print “Control+”  
when Gdk:🪟:SHIFT\_MASK  
print “Shift+”  
when (Gdk:🪟:CONTROL\_MASK|Gdk:🪟:SHIFT\_MASK)  
print “Control+Shift+”  
end  
##Check for direction  
case event.direction  
when Gdk::EventScroll::UP  
print “up\n”  
when Gdk::EventScroll::DOWN  
print “down\n”  
end  
}

###\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*###

## Main loop

###\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*###  
Gtk.main

I will try to take account of your remarks so feel free to answer this  
mail. Then I would like to add this code to  
[http://ruby-gnome2.sourceforge.jp/hiki.cgi?Samples](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Samples) .
