A working example for Gtk::StatusIcon

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
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.

2009/12/15 Vincent C. [email protected]:

Hi everyone.

I wrote an article in french on how to use Gtk::StatusIcon.
Blog du projet ZiK

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

  • comments

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.

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::window::CONTROL_MASK|Gdk::window::SHIFT_MASK)
etat_actuel = (event.state & etat_a_gerer)
case etat_actuel
when 0
p ‘Aucun modificateur’
when Gdk::window::CONTROL_MASK
p ‘Control’
when Gdk::window::SHIFT_MASK
p ‘Shift’
when (Gdk::window::CONTROL_MASK|Gdk::window::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.

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

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

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

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.

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.

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

On Tue, Dec 15, 2009 at 7:55 PM, Vincent C.
[email protected] 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

Mathieu

Mathieu B. wrote:

On Tue, Dec 15, 2009 at 7:55 PM, Vincent C.
[email protected] 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 download | SourceForge.net

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.

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
.
=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::window::CONTROL_MASK|Gdk::window::SHIFT_MASK)
mod=modifier&ctrl_shift
case mod
when 0
print “(None)”
when Gdk::window::CONTROL_MASK
print “Control+”
when Gdk::window::SHIFT_MASK
print “Shift+”
when (Gdk::window::CONTROL_MASK|Gdk::window::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 .