Re: Image on button



On Tue, 2005-09-20 at 09:12 +0530, Vinayak J wrote:
> Try This out... 
Or, for anyone who doesn't want to wade through 7 files of generated
code (Using glade for code generation is generally frowned upon; please
please please use libglade instead), I've got two examples here that
weigh in at under 40 lines apiece.

GTK 2.6 added gtk_button_set_image() which certainly covers the most
common simple cases of this. If you've got to be able to build against
an older version of gtk, or need more flexibility, a GtkButton is
actually a GtkBin, so you can pack whatever you want in there.

image.c shows that; I just make myself a GtkHBox, load it up with a
GtkImage and a GtkLabel, and pack it into a GtkButton.

image-since-26.c shows the simpler case if you've got access to
gtk_button_set_image() and are happy with its behavior.
/* gcc -Wall `pkg-config --cflags --libs gtk+-2.0` image-since-26.c -o image-since-26 */
#include <gtk/gtk.h>

int
main (int argc, char **argv)
{
   gchar *file;
   GtkWidget *image, *window, *button;

   gtk_init(&argc, &argv);
   if(argc > 1)
   {
      file = argv[1];
   } else {
      file = "foo.png";
   }

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);

   button = gtk_button_new_with_label(file);
   g_signal_connect(button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
   gtk_container_add(GTK_CONTAINER(window), button);

   image = gtk_image_new_from_file(file);
   gtk_button_set_image(GTK_BUTTON(button), image);


   gtk_widget_show_all(window);

   gtk_main();
   return 0;
}
/* gcc -Wall `pkg-config --cflags --libs gtk+-2.0` image.c -o image */
#include <gtk/gtk.h>

int
main (int argc, char **argv)
{
   gchar *file;
   GtkWidget *image, *window, *button, *hbox, *label;

   gtk_init(&argc, &argv);
   if(argc > 1)
   {
      file = argv[1];
   } else {
      file = "foo.png";
   }

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);

   button = gtk_button_new();
   g_signal_connect(button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
   gtk_container_add(GTK_CONTAINER(window), button);

   hbox = gtk_hbox_new(FALSE, 6);
   image = gtk_image_new_from_file(file);
   label = gtk_label_new(file);

   gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, TRUE, 0);
   gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);

   gtk_container_add(GTK_CONTAINER(button), hbox);

   gtk_widget_show_all(window);

   gtk_main();
   return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]