Change the image used in a GtkButton
- From: "Jorge Monsalvo" <jm_tecno2002 yahoo com ar>
- To: <gtk-app-devel-list gnome org>
- Subject: Change the image used in a GtkButton
- Date: Thu, 13 Jan 2005 19:36:10 -0300
Hi people!,
I'm developing a small applicaction where you can assign different
buttons to actions. One thing I want to put in this application is the
user can select the color and also the shape of the button.
What I imagined was different pixmaps to set into the button. So the
first attempt was get the button from the window and reload the image,
but I can't make it work.
Can you give me a little help??
Thanks
Jorge Monsalvo
Argentina
PS:
1) I'm working with GTK 2.4.10 on Win32
2)Sorry about my english.....
This is the code for testing (modified from glade project)
GtkWidget*
create_pbtn_wnd (void)
{
GtkWidget *pbtn_wnd;
GtkWidget *hbox1;
GtkWidget *pbtn_push;
GtkWidget *image;
GtkWidget *table1;
GtkWidget *pbtn_link;
GtkWidget *pbtn_addr;
GtkWidget *pbtn_out;
GtkWidget *label4;
GtkWidget *label3;
GtkWidget *label2;
GtkWidget *label1;
GtkWidget *pbtn_color;
pbtn_wnd = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (pbtn_wnd), _("pbtn_wnd"));
hbox1 = gtk_hbox_new (TRUE, 4);
gtk_widget_show (hbox1);
gtk_container_add (GTK_CONTAINER (pbtn_wnd), hbox1);
gtk_container_set_border_width (GTK_CONTAINER (hbox1), 3);
pbtn_push = gtk_button_new ();
gtk_widget_show (pbtn_push);
gtk_box_pack_start (GTK_BOX (hbox1), pbtn_push, FALSE, FALSE, 0);
gtk_button_set_relief (GTK_BUTTON (pbtn_push), GTK_RELIEF_NONE);
gtk_button_set_focus_on_click (GTK_BUTTON (pbtn_push), FALSE);
image = create_pixmap (pbtn_wnd, "pbtn_black.png");
gtk_widget_show (image);
gtk_container_add (GTK_CONTAINER (pbtn_push), image);
table1 = gtk_table_new (4, 2, FALSE);
gtk_widget_show (table1);
gtk_box_pack_start (GTK_BOX (hbox1), table1, FALSE, FALSE, 5);
gtk_container_set_border_width (GTK_CONTAINER (table1), 3);
gtk_table_set_row_spacings (GTK_TABLE (table1), 4);
gtk_table_set_col_spacings (GTK_TABLE (table1), 2);
pbtn_link = gtk_entry_new ();
gtk_widget_show (pbtn_link);
gtk_table_attach (GTK_TABLE (table1), pbtn_link, 1, 2, 0, 1,
(GtkAttachOptions) (GTK_EXPAND),
(GtkAttachOptions) (0), 0, 0);
pbtn_addr = gtk_entry_new ();
gtk_widget_show (pbtn_addr);
gtk_table_attach (GTK_TABLE (table1), pbtn_addr, 1, 2, 1, 2,
(GtkAttachOptions) (GTK_EXPAND),
(GtkAttachOptions) (0), 0, 0);
pbtn_out = gtk_entry_new ();
gtk_widget_show (pbtn_out);
gtk_table_attach (GTK_TABLE (table1), pbtn_out, 1, 2, 2, 3,
(GtkAttachOptions) (GTK_EXPAND),
(GtkAttachOptions) (0), 0, 0);
gtk_editable_set_editable (GTK_EDITABLE (pbtn_out), FALSE);
label4 = gtk_label_new (_("Color :"));
gtk_widget_show (label4);
gtk_table_attach (GTK_TABLE (table1), label4, 0, 1, 3, 4,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 0);
gtk_misc_set_alignment (GTK_MISC (label4), 0, 0.5);
label3 = gtk_label_new (_("Output :"));
gtk_widget_show (label3);
gtk_table_attach (GTK_TABLE (table1), label3, 0, 1, 2, 3,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 0);
gtk_misc_set_alignment (GTK_MISC (label3), 0, 0.5);
label2 = gtk_label_new (_("Address :"));
gtk_widget_show (label2);
gtk_table_attach (GTK_TABLE (table1), label2, 0, 1, 1, 2,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 0);
gtk_misc_set_alignment (GTK_MISC (label2), 0, 0.5);
label1 = gtk_label_new (_("Link :"));
gtk_widget_show (label1);
gtk_table_attach (GTK_TABLE (table1), label1, 0, 1, 0, 1,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 0);
gtk_misc_set_alignment (GTK_MISC (label1), 0, 0.5);
pbtn_color = gtk_combo_box_new_text ();
gtk_widget_show (pbtn_color);
gtk_table_attach (GTK_TABLE (table1), pbtn_color, 1, 2, 3, 4,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (GTK_FILL), 0, 0);
gtk_combo_box_append_text (GTK_COMBO_BOX (pbtn_color),
_("PBTN_BLACK"));
gtk_combo_box_append_text (GTK_COMBO_BOX (pbtn_color),
_("PBTN_BLUE"));
gtk_combo_box_append_text (GTK_COMBO_BOX (pbtn_color),
_("PBTN_GREEN"));
gtk_combo_box_append_text (GTK_COMBO_BOX (pbtn_color), _("PBTN_RED"));
gtk_combo_box_append_text (GTK_COMBO_BOX (pbtn_color),
_("PBTN_YELLOW"));
gtk_combo_box_append_text (GTK_COMBO_BOX (pbtn_color),
_("PBTN_E-STOP"));
gtk_combo_box_set_active( GTK_COMBO_BOX (pbtn_color), 0);
g_signal_connect ((gpointer) pbtn_push, "pressed",
G_CALLBACK (on_pbtn_push_pressed),
NULL);
g_signal_connect ((gpointer) pbtn_push, "released",
G_CALLBACK (on_pbtn_push_released),
NULL);
g_signal_connect ((gpointer) pbtn_color, "changed",
G_CALLBACK (on_pbtn_color_changed),
pbtn_wnd);
/* Store pointers to all widgets, for use by lookup_widget(). */
GLADE_HOOKUP_OBJECT_NO_REF (pbtn_wnd, pbtn_wnd, "pbtn_wnd");
GLADE_HOOKUP_OBJECT (pbtn_wnd, hbox1, "hbox1");
GLADE_HOOKUP_OBJECT (pbtn_wnd, pbtn_push, "pbtn_push");
GLADE_HOOKUP_OBJECT (pbtn_wnd, image, "image");
GLADE_HOOKUP_OBJECT (pbtn_wnd, table1, "table1");
GLADE_HOOKUP_OBJECT (pbtn_wnd, pbtn_link, "pbtn_link");
GLADE_HOOKUP_OBJECT (pbtn_wnd, pbtn_addr, "pbtn_addr");
GLADE_HOOKUP_OBJECT (pbtn_wnd, pbtn_out, "pbtn_out");
GLADE_HOOKUP_OBJECT (pbtn_wnd, label4, "label4");
GLADE_HOOKUP_OBJECT (pbtn_wnd, label3, "label3");
GLADE_HOOKUP_OBJECT (pbtn_wnd, label2, "label2");
GLADE_HOOKUP_OBJECT (pbtn_wnd, label1, "label1");
GLADE_HOOKUP_OBJECT (pbtn_wnd, pbtn_color, "pbtn_color");
return pbtn_wnd;
}
void
on_pbtn_color_changed (GtkComboBox *combobox,
gpointer user_data)
{
GtkWidget *image;
GtkWidget *pbtn_push;
GtkWidget *wnd;
gint idx;
wnd = (GtkWidget *)user_data;
pbtn_push = lookup_widget(wnd, "pbtn_push");
idx = gtk_combo_box_get_active(GTK_COMBO_BOX(combobox));
switch(idx){
case 0:
image = create_pixmap (wnd, "pbtn_black.png");
break;
case 1:
image = create_pixmap (wnd, "pbtn_blue.png");
break;
case 2:
image = create_pixmap (wnd, "pbtn_green.png");
break;
case 3:
image = create_pixmap (wnd, "pbtn_red.png");
break;
case 4:
image = create_pixmap (wnd, "pbtn_yellow.png");
break;
case 5:
image = create_pixmap (wnd, "pbtn_e-stop.png");
break;
}
gtk_widget_show (image);
gtk_container_add (GTK_CONTAINER (pbtn_push), image);
gtk_widget_show (pbtn_push);
}
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.11 - Release Date: 12/01/2005
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]