RE: How do you make a button out of xpm(pxmap)?



I'm writing an app which requires an xpm (pixmap) as
image for button ( I hope you got this.. )
 and when that image is clicked.. 

I need to replace that image with another xpm of same
size with my function executed meanwhile... 
( hope I'm clear on this too.. )

(The pixmap has not been inserted in a button widget)


Now I've drawn the pixmap, but all my attempts to
connect it to a signal handler went in vain... I tried
button_press_event, button_release_event etc.
Nothing worked..


Is there anything else I can do to achieve this?


All you should have to do is connect the signal "clicked" to the button in
question and change the image and execute the code on the callback function.

Assuming you use the same method that glade does to create a button with an
image this is how you would probably do it:

        mybutton = gtk_button_new ();
        gtk_widget_show (mybutton);
        gtk_container_add (GTK_CONTAINER (mywindow), mybutton);
        gtk_button_set_relief (GTK_BUTTON (mybutton), GTK_RELIEF_NONE);

        myalignment = gtk_alignment_new (0.5, 0.5, 0, 0);
        gtk_widget_show (myalignment);
        gtk_container_add (GTK_CONTAINER (mybutton), myalignment);

        myhbox = gtk_hbox_new (FALSE, 2);
        gtk_widget_show (myhbox);
        gtk_container_add (GTK_CONTAINER (myalignment), myhbox);

        /* myimage = gtk_image_new_from_stock ("gtk-goto-bottom",
GTK_ICON_SIZE_BUTTON); */ 
        myimage = gtk_image_new (...);  
        gtk_widget_show (myimage);
        gtk_box_pack_start (GTK_BOX (myhbox), myimage, FALSE, FALSE, 0);

        mylabel = gtk_label_new_with_mnemonic (_("some text"));
        gtk_widget_show (mylabel);
        gtk_box_pack_start (GTK_BOX (myhbox), mylabel, FALSE, FALSE, 0);
        gtk_label_set_justify (GTK_LABEL (mylabel), GTK_JUSTIFY_LEFT);

        /* connect signals */
        gtk_signal_connect (GTK_OBJECT (mybutton), "clicked",
                      GTK_SIGNAL_FUNC (on_mybutton_clicked),
                          NULL);        
        .
        .
        .

        void on_mybutton_clicked(GtkButton *button, gpointer user_data)
        {
                /* change the image */
                gtk_image_set_from_file (myimage, "somefile.xpm");


                /* do some code */
        }

Regards,
Martyn



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