Using an event box with an image in it is a nice and simple way to make a button that will do exactly what you want – display an image and trigger an event when pressed and released. Its advantage over GtbButton is that is just does that without adding a highlight effect and border. It’s perfectly simple.
Create The Button
GtkWidget *image_button_down;
GtkWidget *event_box_button_down;
//----- EVENT BOX BASED IMAGE BUTTON -----
image_button_down = gtk_image_new_from_file("/home/pi/projects/ui/button_down_a.png");
event_box_button_down = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(event_box_button_down), image_button_down);
//gtk_widget_set_size_request(event_box_button_down, 68, 68); //Will be image size by default, but you can force the widget to be either larger or smaller than it normally would be using this
gtk_fixed_put(GTK_FIXED(fixed), event_box_button_down, 460, 270); //x, y from top left
//----- SET EVENTS TO BE CALLED -----
g_signal_connect(G_OBJECT(event_box_button_down), "button_press_event", G_CALLBACK(event_button_press_event), NULL); //("button_press_event" must not be changed)
g_signal_connect(G_OBJECT(event_box_button_down), "button_release_event", G_CALLBACK(event_button_release_event), NULL); //Include if you want the release event ("button_release_event" must not be changed)
Button Events
//***********************************************
//***********************************************
//********** A BUTTON HAS BEEN PRESSED **********
//***********************************************
//***********************************************
gboolean event_button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer data)
{
if (widget == event_box_button_down)
{
gtk_image_set_from_file(GTK_IMAGE(image_button_down), "/home/pi/projects/ui/button_down_b.png");
}
//If you want the coordinates of the touch event:
int ClickCoordX;
int ClickCoordY;
ClickCoordX = event->x;
ClickCoordY = event->y;
return FALSE; //Return false so event will be called again
}
//************************************************
//************************************************
//********** A BUTTON HAS BEEN RELEASED **********
//************************************************
//************************************************
gboolean event_button_release_event (GtkWidget *widget, GdkEventButton *event, gpointer data)
{
if (widget == event_box_button_down)
{
gtk_image_set_from_file(GTK_IMAGE(image_button_down), "/home/pi/projects/ui/button_down_a.png");
}
return FALSE; //Return false so event will be called again
}
Delete a button
gtk_widget_destroy(imgButtonTimerUp);
imgButtonTimerUp = NULL;
gtk_widget_destroy(ebButtonTimerUp);
ebButtonTimerUp = NULL;
When a widget is destroyed, it will break any references it holds to other objects. You can safely used destroy on a widget that hasn’t actaully been displayed yet. You can re-create and place the widget again later after it has previously been destroyed, you’re just removing its current usage.
Detecting A Touch Anywhere On The Screen
Place your background image in an eventbox just as above. Any event box buttons you create over the top of it will generate their own touch events when pressed, but if you touch anywhere else the background event box will generate the touch event.