The UHF of the film world.


quietearth [General News 01.23.07]

Share on Google+


First off we need the egg tray source written by Anders Carlsson which is widely used. This just contains one C file and a header file:

http://www.quietearth.us/src/eggtrayicon.tar.gz


Next we setup the headers for our main file, main.c:
#include <stdio.h>
#include <gtk/gtk.h>
#include "eggtrayicon.h"


and then declare the necessary variables for our docklet:
static EggTrayIcon *docklet = NULL;
GtkWidget *box;


And now we can setup some event call backs, depending on which mouse button you push you wsill get a different result:
void docklet_button_press(GtkWidget *button, GdkEventButton *event, void *data)
{
if (event->type != GDK_BUTTON_PRESS) return;

switch (event->button) {
        case 1:
                printf("egg tray button 1\n");
                break;
        case 2:
                printf("egg tray button 2\n");
                break;
        case 3:
                printf("egg tray button 3\n");
                break;
        }
}


Now we add in our little docklet_init routine so we can get the panel up and running. You might need to change the path of the png icon used.
docklet_init()
{
GtkWidget *image;

docklet = egg_tray_icon_new("Bluetooth");
box = gtk_event_box_new();
#if GTK_MINOR_VERSION >= 6
image = gtk_image_new_from_icon_name ("stock_bluetooth", GTK_ICON_SIZE_BUTTON);
#else
image = gtk_image_new_from_file ("/usr/share/icons/gnome/24x24/stock/io/stock_bluetooth.png");
#endif

/* the box container is so we can add callbacks events */
gtk_container_add(GTK_CONTAINER(box), image);
gtk_container_add(GTK_CONTAINER(docklet), box);

if(!gtk_check_version(2,4,0))
        g_object_set(G_OBJECT(box), "visible-window", FALSE, NULL);

/* callbacks */
g_signal_connect(G_OBJECT(box), "button-press-event", G_CALLBACK(docklet_button_
press), NULL);

gtk_widget_show_all(GTK_WIDGET(docklet));
}


And now simply main()
main(int argc, char *argv[])
{
gtk_set_locale();
gtk_init(&argc, &argv);
docklet_init();
gtk_main();
}


Now lets compile:
# gcc `pkg-config --cflags gtk+-2.0` -c main.c
# gcc `pkg-config --cflags gtk+-2.0` -c eggtrayicon.c
# gcc `pkg-config --cflags --libs gtk+-2.0`  main.o eggtrayicon.o -o main


Now just run this from any xterm and hit any mouse button on the applet which showed upt, and it will tell you which button you pushed.

avatar

Andrew Ruthven (4 years ago) Reply

Hey, nice intro. Just a heads for people that there are a few typos in there (in the case block), but gcc will point them out pretty quickly.

Thanks!

avatar

quietearth (4 years ago) Reply

Thanks for pointing that out, I fixed it.

avatar

Andrew Ruthven (4 years ago) Reply

No worries, you still have prihnt in there. :)

avatar

quietearth (4 years ago) Reply

aaaaghh!!! I fixed that too, anything else I'm missing?

avatar

Kim Johansen (4 years ago) Reply

Thanks for a great guide!

avatar

firecracker (3 years ago) Reply

thanks for the quick and easy guide :)

avatar

jypark (2 years ago) Reply

Thanks for the best guide!

avatar

JasonWoof (2 years ago) Reply

Thank you! thank you!

From this tutorial, I learned about eggtrayicon, and how to use it.

I made a little program that displays the contents of a text file in the notification area (checks for updates every second)

http://gitorious.org/jasonwoof/now-playing-d

avatar

glowhand (1 year ago) Reply

Funktioniert leider nicht:

ain.o: In function `main':
main.c:(.text+0x21): undefined reference to `docklet_init'
collect2: ld returned 1 exit status


Leave a comment








Related articles