GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Aug 31, 2016 11:29 pm

All times are UTC

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: How to create a button using full image - no border?
PostPosted: Thu Apr 17, 2008 8:24 pm 

Joined: Thu Apr 17, 2008 7:08 pm
Posts: 2
I am relatively new to GTK and am trying to create a button with just a single image, but which has no extra border, background, etc. around the image. I've tried changing the style properties, of which inner-border looked promising, but a 0 value still leaves a border (and negative numbers gave an error). Any help would be appreciated!

Let me also provide a larger overview of what I'm trying to do (in case I'm going about it all the wrong way). Essentially I'd like to create a realistic gui based on camera pictures taken of the real thing. When the user clicks on a button in the image I'll change the image to another picture taken of the button in the new state. For this to work however I'll need all the buttons and background image to butt up to each other - i.e. no extra space/border.

I could just use image widgets themselves it looks like (I don't see a border with them), but then I will have to catch the mouse clicks myself and calculate if the part of the image they are clicking on is one of the buttons, etc. I had thought it would be easier to just use this built-in functionality of gtk instead.

Thanks again for any assistance.

 Post subject:
PostPosted: Fri Apr 18, 2008 2:12 pm 
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
To do this, you can't use a GtkButton. Instead, place your image inside of a GtkEventBox widget and connect it to the button-press-event signal. This is a simple example from my book (Foundations of GTK+ Development) that uses an event box and GtkLabel. Just replace the label with an image widget:

#include <gtk/gtk.h>

static void destroy (GtkWidget*, gpointer);
static gboolean button_pressed (GtkWidget*, GdkEventButton*, GtkLabel*);

int main (int argc,
          char *argv[])
  GtkWidget *window, *eventbox, *label;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Event Box");
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  gtk_widget_set_size_request (window, 200, 50);

  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (destroy), NULL);

  eventbox = gtk_event_box_new ();
  label = gtk_label_new ("Click Me!");

  /* Set the order in which widgets will receive notification of events. */
  gtk_event_box_set_above_child (GTK_EVENT_BOX (eventbox), FALSE);

  g_signal_connect (G_OBJECT (eventbox), "button_press_event",
                    G_CALLBACK (button_pressed), (gpointer) label);

  gtk_container_add (GTK_CONTAINER (eventbox), label);
  gtk_container_add (GTK_CONTAINER (window), eventbox);

  /* Allow the event box to catch button presses, realize the widget, and set the
   * cursor that will be displayed when the mouse is over the event box. */
  gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK);
  gtk_widget_realize (eventbox);
  gdk_window_set_cursor (eventbox->window, gdk_cursor_new (GDK_HAND1));
  gtk_widget_show_all (window);
  gtk_main ();
  return 0;

static void
destroy (GtkWidget *window,
         gpointer data)
  gtk_main_quit ();

/* This is called every time a button-press-event occurs on the GtkEventBox. */
static gboolean
button_pressed (GtkWidget *eventbox,
                GdkEventButton *event,
                GtkLabel *label)
  if (event->type == GDK_BUTTON_PRESS)
    const gchar *text = gtk_label_get_text (label);
    if (text[0] == 'D')
      gtk_label_set_text (label, "I Was Clicked!");
      gtk_label_set_text (label, "Click Me Again!");

  return FALSE;

Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group