Ricerca tra la vecchia roba

System tray icon con Gtk

Posted: Aprile 22nd, 2007 | Author: | Filed under: Programmazione | 1 Comment »

 

/*
 *      *-system_tray_icon.c-*
 *
 *      Applicazione che visualizza un icona nel system tray scritto
 *      tramite libreria Gtk; è disponibile anche un menù attivabile
 *      tramite il tasto destro del mouse tramite cui si esce dal
 *      programma.

  *      Istruzioni per compilarlo
 *
 *              gcc -Wall -g `pkg-config –cflags gtk+-2.0` system_tray_icon.c
 *              -o system_tray_icon `pkg-config –libs gtk+-2.0`
 *
 *      Di default viene compilato con un menù allineato all'icona, per
 *      averne uno che si posiziona nel punto in cui si clicca con il
 *      mouse, passare come opzione al compilatore
 *
 *              -DDINAMIC_MENU
 *
 *      (se si usa il Makefile 'make DEFINE=-DDINAMIC_MENU')
 *
 *
 *      ::SPIEGAZIONE TECNICA::
 *
 *      La classe GtkStatusIcon è una classe pensata apposta per
 *      visualizzare le icone nel 'system tray'.
 *
 *      Particolarità di questa classe:
 *
 *      1 – Viene proprio utilizzata la classe GtkStatusIcon e non GtkWidget
 *              (infatti dalla documentazione risulta:
 *              "Note that a GtkStatusIcon is not a widget,
 *              but just a GObject. Making it a widget would
 *              be impractical, since the system tray on Win32
 *              doesn't allow to embed arbitrary widgets.")
 *
 *      2 – Non c'è bisogno di chiamare gtk_widget_show_*
 *
 *      È capable dei seguenti segnali
 *
 *      – activate                              click tasto sinistro mouse
 *      – popup-menu                    click tasto destro mouse
 *      – size-changed
 *
 *
 *      Copyright (C) 2007 packz
 *
 *      This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place – Suite 330, Boston, MA 02111-1307, USA.
 */
#include<stdlib.h>
#include<gtk/gtk.h>

void display_menu(
        GtkStatusIcon* status_icon,
        guint button,
        guint activate_time,
        gpointer user
){
        /*creiamo il nostro bel menu*/
        GtkWidget* menu;
        /*le sue voci*/
        GtkWidget* menu_item;

        menu_item = gtk_menu_item_new_with_label("Exit");
        menu = gtk_menu_new();

        //gtk_menu_attach((GtkMenu*)menu,menu_item,0,0,0,0);
        gtk_menu_shell_append((GtkMenuShell*)menu,menu_item);
        gtk_widget_show_all(menu);

        /*ovviamente deve fare qualcosa */
        g_signal_connect(menu_item,"activate",G_CALLBACK(gtk_main_quit),NULL);

#ifdef DINAMIC_MENU
        /*menu che compare nel punto in cui clicco*/
        gtk_menu_popup(
                (GtkMenu*)menu,
                NULL,
                NULL,
                NULL,
                NULL,
                button,
                activate_time
        );
#else
        /*menu che compare sotto l'icona*/
        gtk_menu_popup(
                (GtkMenu*)menu,
                NULL,
                NULL,
                gtk_status_icon_position_menu,/*posiziona il menù allineato*/
                status_icon,/*questi sono i dati da passare alla fz precedente*/
                button,
                activate_time
        );
#endif
}

int main(int argc,char* argv[]){
        /*inizializziamo*/
        gtk_init(&argc,&argv);
        /*widget necessari*/
        GtkStatusIcon* status;

        /*creiamola*/
        status = gtk_status_icon_new_from_file("cat.png");
        /*impostiamo il tooltip*/
        gtk_status_icon_set_tooltip(status,"packz 'system tray icon' testing application");

        /*colleghiamo i segnali*/
        g_signal_connect(status,"popup-menu",G_CALLBACK(display_menu),NULL);
        g_signal_connect(status,"activate",G_CALLBACK(gtk_main_quit),NULL);
        //g_signal_connect(status,"size-changed",G_CALLBACK(gtk_main_quit),NULL);

        /*passiamo la palla alle GTK*/
        gtk_main();

        return EXIT_SUCCESS;
}

Servirebbe anche un'immagine da far visualizzare nel system tray come icona: io ho usatologo del gatto, voi mettete quella che volete nella directory in cui si eseguite il programma cambiando nel sorgente la stringa "cat.png" con l'immagine voluta (e ricompilando of course)…


One Comment on “System tray icon con Gtk”

  1. 1 packz said at 9:53 pm on Aprile 12th, 2011:

    E dopo 4 anni questo codice funziona ancora… FTW!!!