GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Oct 25, 2014 8:41 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Help whith image handling
PostPosted: Sat Dec 01, 2007 3:14 am 
Offline

Joined: Sat Dec 01, 2007 2:45 am
Posts: 2
Hello,

I want to handle an image click. In order to do it i used an event_box. Except the image I would like to pass an other variable ( array a ) but the following code is not working correctly.Could you please help me because i am new to gtk? (Sorry if my code is a litle bit messy :))

Code:
#include <iostream>
#include <gtk/gtk.h>

void print(GtkEventBox *less,gpointer *hap,GtkWidget *one,int a[]){
gtk_image_set_from_file(GTK_IMAGE(one),"x.jpg");
std::cout << a[1];
}

int main( int   argc,
          char *argv[] )
{
GtkWidget *one;
GtkWidget *event_box;
GtkWidget *window;

int a[2];
a[1]=1;
a[2]=2;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
one = gtk_image_new_from_file ("blank.jpg");
event_box = gtk_event_box_new ();


gtk_widget_add_events(event_box, GDK_BUTTON_PRESS_MASK);
gtk_container_add (GTK_CONTAINER (event_box), one);
g_signal_connect (G_OBJECT (event_box),
                      "button_release_event",
                      G_CALLBACK (print),
                      one);

gtk_container_add (GTK_CONTAINER (window), event_box);
gtk_widget_show_all (window);
gtk_main ();
   
    return 0;

}


Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 01, 2007 6:02 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Do you mean that you want to pass 2 variables, the array 'a' and the GtkImage 'one' to the callback funtion? We often use a struct for this (or create an object or widget if applicable).

Also, you might want to call gtk_main_quit on the window's destroy event.

Code:
#include <iostream>
#include <gtk/gtk.h>

typedef struct
{
        int             a[2];
        GtkWidget       *one;
} MyStruct;

void print (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
        MyStruct        *s;
       
        s = (MyStruct *)user_data;
       
        gtk_image_set_from_file(GTK_IMAGE(s->one),"x.jpg");
        std::cout << s->a[1];
}

int main (int   argc, char *argv[])
{
        GtkWidget       *event_box;
        GtkWidget       *window;
        MyStruct        *s;
       
        s = g_slice_new (MyStruct);
       
        s->a[1]=1;
        s->a[2]=2;

        gtk_init (&argc, &argv);

        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        s->one = gtk_image_new_from_file ("blank.jpg");
        event_box = gtk_event_box_new ();


        gtk_widget_add_events(event_box, GDK_BUTTON_PRESS_MASK);
        gtk_container_add (GTK_CONTAINER (event_box), s->one);
       
        /* call gtk_main_quit() when window is destroyed */
        g_signal_connect (G_OBJECT (window),
                              "destroy",
                              G_CALLBACK (gtk_main_quit),
                              NULL);
                             
        g_signal_connect (G_OBJECT (event_box),
                              "button_release_event",
                              G_CALLBACK (print),
                              s);

        gtk_container_add (GTK_CONTAINER (window), event_box);
        gtk_widget_show_all (window);
        gtk_main ();
   
        return 0;

}

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 6:50 am 
Offline

Joined: Sat Dec 01, 2007 2:45 am
Posts: 2
thank you very much for your anser. If only you could help me with something else.... :)

Actualy, I had made a tic-tac-toe game which runs in terminal and i want to make it gtk. In this code I used a function named print in which I ask from the user to give me the position in which he would like to play. Can I do sth in order to stop the program when the user is about to play and continue only if the user clicks on a button...

tnx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 3:11 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
I don't understand the question

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 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