Forum: Ruby-Gnome 2 A working example for Gtk::StatusIcon

Posted by Vincent Carmona (vinc-mai)
on 2009-12-15 09:45
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.
Posted by Simon Arnaud (Guest)
on 2009-12-15 11:34
(Received via mailing list)
2009/12/15 Vincent Carmona <ruby-forum-incoming@andreas-s.net>:
> 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

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.

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

3)
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

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

5)
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
Posted by Vincent Carmona (vinc-mai)
on 2009-12-15 11:55
Simon Arnaud 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.

> 5)
> 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
Posted by Mathieu Blondel (Guest)
on 2009-12-15 12:25
(Received via mailing list)
On Tue, Dec 15, 2009 at 7:55 PM, Vincent Carmona
<ruby-forum-incoming@andreas-s.net> 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

Mathieu
Posted by Vincent Carmona (vinc-mai)
on 2009-12-15 12:30
Mathieu Blondel wrote:
> On Tue, Dec 15, 2009 at 7:55 PM, Vincent Carmona
> <ruby-forum-incoming@andreas-s.net> 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
> 
> 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.
Posted by Vincent Carmona (vinc-mai)
on 2009-12-16 19:21
Vincent Carmona 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 .
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.