1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| #include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <string.h>
#define MAX_LIGNE 8
#define MAX_COLONNE 4
int coord[MAX_LIGNE][MAX_COLONNE];
void charge(GtkWidget *pButton, gpointer data);
struct _Structure
{
GtkWidget *pVBox;
GtkWidget *pImage;
};
typedef struct _Structure Structure;
GtkWidget *pWindow;
void position_souris( GtkWidget *widget, GdkEventMotion *event )
{
gint x1, y1;
gint x2, y2;
/* Position dans la fenêtre */
if (event->is_hint)
gdk_window_get_pointer (event->window, &x1, &y1, NULL);
else
{
x1 = event->x;
y1 = event->y;
}
/* Position dans l'écran */
if (event->is_hint)
gdk_window_get_pointer (gdk_screen_get_root_window(gdk_screen_get_default()),&x2, &y2, NULL);
else
{
x2 = event->x_root;
y2 = event->y_root;
}
/*RECUPERATION DES COORDONNEES ICI*/
coord[0][0]=x1;
coord[0][1]=y1;
coord[0][2]=x2;
coord[0][3]=y2;
// printf("%d \t%d\n %d\t %d\n",coord[0][0],coord[0][1],coord[0][2],coord[0][3]);
/* Affichage */
g_printf("Fenetre x:%d y:%d\t\t Ecran x:%d y:%d\n", x1, y1, x2, y2);
}
int main(int argc,char **argv)
{
Structure *structure;
GtkWidget *pButton;
GtkWidget *pImage;
gtk_init(&argc,&argv);
structure = g_malloc(sizeof(Structure));
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(pWindow), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(pWindow), 600, 400);
gtk_window_set_title(GTK_WINDOW(pWindow), "Charger Image");
/* agrandissement automatique de la fenêtre */
gtk_window_maximize (GTK_WINDOW (pWindow));
structure->pVBox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(pWindow), structure->pVBox);
pButton = gtk_button_new_with_label("Charger");
gtk_box_pack_start(GTK_BOX(structure->pVBox), pButton, FALSE, FALSE, 0);
structure->pImage = gtk_image_new();
gtk_box_pack_end(GTK_BOX(structure->pVBox), structure->pImage, TRUE, FALSE, 0);
g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_signal_connect (GTK_OBJECT (pWindow), "button_press_event", G_CALLBACK(charge),(gpointer*) structure);
gtk_signal_connect (GTK_OBJECT (pWindow), "button_press_event", G_CALLBACK(position_souris) , NULL);
/* Désigne les évènements à gérer */
gtk_widget_set_events (pWindow, GDK_BUTTON_PRESS_MASK);
gtk_widget_show_all(pWindow);
gtk_main();
g_free(structure);
return EXIT_SUCCESS;
}
void charge(GtkWidget *pWindow, gpointer data)
{
Structure *structure;
int i;
char nom_image[10];
srand(time (NULL));
i = rand() % 8;
sprintf(nom_image,"%d",i);
strcat(nom_image,".png");
/*Récupération de la pVBox */
structure = (Structure*) data;
/*Création de l'image*/
gtk_image_set_from_file(GTK_IMAGE (structure->pImage), nom_image);
} |