Bonjour tout le monde

Je suis débutant dans le monde GTK et j'ai un problème qui freine depuis 2 jours dans mon projet GTK. J'ai, donc, décidé de vous en parler en espérant que vous aurez la solution ^^

J'ai créer un widget GtkMorceau personnalisé et je voulais créer un tableau de ce widget. Tout se passe bien, mais pour l'initialisation ça m'affiche un "segmentation fault".

Voici le code du widget "Morceau.c" :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
 
/* gtkmorceau.c */
 
#include "morceau.h"
 
 
static void gtk_morceau_class_init (GtkMorceauClass * klass);
static void gtk_morceau_init (GtkMorceau * morceau);
static void gtk_morceau_load_image (GtkMorceau * morceau);
static void gtk_morceau_size_request (GtkWidget * widget,
                                  GtkRequisition * requisition);
static void gtk_morceau_size_allocate (GtkWidget * widget,
                                   GtkAllocation * allocation);
static void gtk_morceau_realize (GtkWidget * widget);
static gboolean gtk_morceau_expose (GtkWidget * widget,
                                GdkEventExpose * event);
static void gtk_morceau_paint (GtkWidget * widget);
static void gtk_morceau_destroy (GtkObject * object);
 
 
GtkType
gtk_morceau_get_type (void)
{
   static GtkType gtk_morceau_type = 0;
 
 
   if (! gtk_morceau_type) {
      static const GtkTypeInfo gtk_morceau_info = {
         "GtkMorceau",
         sizeof (GtkMorceau),
         sizeof (GtkMorceauClass),
         (GtkClassInitFunc) gtk_morceau_class_init,
         (GtkObjectInitFunc) gtk_morceau_init,
         NULL,
         NULL,
         (GtkClassInitFunc) NULL
      };
      gtk_morceau_type = gtk_type_unique (GTK_TYPE_WIDGET, &gtk_morceau_info);
   }
 
 
   return gtk_morceau_type;
}
 
 
gboolean
gtk_morceau_get_star (GtkMorceau * morceau)
{
   return morceau->star;
}
 
 
void
gtk_morceau_set_star (GtkMorceau * morceau, gboolean star)
{
   morceau->star = star;
  gtk_morceau_paint (GTK_WIDGET (morceau));
}
 
int
gtk_morceau_get_curs_on (GtkMorceau * morceau)
{
   return morceau->curs_on;
}
 
 
void
gtk_morceau_set_curs_on (GtkMorceau * morceau, int curs_on)
{
   morceau->curs_on = curs_on;
  gtk_morceau_paint (GTK_WIDGET (morceau));
}
 
 
 
GtkWidget *
gtk_morceau_new (const gchar * without_star_image,
                         const gchar * with_star_image, const gchar * with_curs_up_image,
                         const gchar * with_curs_down_image, const gchar * with_curs_left_image,
                         const gchar * with_curs_rigth_image,             
                         gboolean star, int curs_on)
{
   GtkMorceau * morceau = NULL;
 
 
   if (with_star_image != NULL && without_star_image != NULL && with_curs_up_image != NULL && with_curs_down_image!=NULL && with_curs_left_image!=NULL && with_curs_rigth_image!=NULL) {
      morceau = gtk_type_new (gtk_morceau_get_type ());
 
      if (morceau != NULL) {
 
         morceau->without_star_image = without_star_image;
         morceau->with_star_image = with_star_image;
         morceau->with_curs_up_image = with_curs_up_image;
         morceau->with_curs_down_image = with_curs_down_image;
         morceau->with_curs_left_image = with_curs_left_image;
         morceau->with_curs_rigth_image = with_curs_rigth_image;
 
         morceau->star = star;
         morceau->curs_on= curs_on;
 
         gtk_morceau_load_image (morceau);
      }
   }
 
 
   return GTK_WIDGET (morceau);
}
 
 
static void
gtk_morceau_class_init (GtkMorceauClass * klass)
{
   GtkWidgetClass * widget_class;
   GtkObjectClass * object_class;
 
 
   widget_class = (GtkWidgetClass *) klass;
   object_class = (GtkObjectClass *) klass;
 
   widget_class->realize = gtk_morceau_realize;
   widget_class->size_request = gtk_morceau_size_request;
   widget_class->size_allocate = gtk_morceau_size_allocate;
   widget_class->expose_event = gtk_morceau_expose;
 
   object_class->destroy = gtk_morceau_destroy;
}
 
 
static void
gtk_morceau_init (GtkMorceau * morceau)
{
   morceau->without_star_image = NULL;
   morceau->with_star_image = NULL;
   morceau->with_curs_up_image = NULL;
morceau->with_curs_down_image = NULL;
morceau->with_curs_left_image = NULL;
morceau->with_curs_rigth_image = NULL;
   morceau->with_star_pixbuf = NULL;
   morceau->without_star_pixbuf = NULL;
  morceau->with_curs_up_pixbuf = NULL;
  morceau->with_curs_down_pixbuf = NULL;
  morceau->with_curs_left_pixbuf = NULL;
  morceau->with_curs_rigth_pixbuf = NULL;
   morceau->width = 0;
   morceau->height = 0;
   morceau->star = FALSE;
   morceau->curs_on = 0;
}
 
 
static void
gtk_morceau_load_image (GtkMorceau * morceau)
{
   morceau->without_star_pixbuf = gdk_pixbuf_new_from_file (morceau->without_star_image, NULL);
   morceau->with_star_pixbuf = gdk_pixbuf_new_from_file (morceau->with_star_image, NULL);
   morceau->with_curs_up_pixbuf = gdk_pixbuf_new_from_file (morceau->with_curs_up_image, NULL);
   morceau->with_curs_down_pixbuf = gdk_pixbuf_new_from_file (morceau->with_curs_down_image, NULL);
   morceau->with_curs_left_pixbuf = gdk_pixbuf_new_from_file (morceau->with_curs_left_image, NULL);
   morceau->with_curs_rigth_pixbuf = gdk_pixbuf_new_from_file (morceau->with_curs_rigth_image, NULL);
   morceau->width = gdk_pixbuf_get_width (morceau->without_star_pixbuf);
   morceau->height = gdk_pixbuf_get_height (morceau->without_star_pixbuf);
}
 
 
static void
gtk_morceau_size_request (GtkWidget * widget,
                      GtkRequisition * requisition)
{
   g_return_if_fail (widget != NULL);
   g_return_if_fail (GTK_IS_MORCEAU (widget));
   g_return_if_fail (requisition != NULL);
 
   requisition->width = GTK_MORCEAU (widget)->width;
   requisition->height = GTK_MORCEAU (widget)->height;
}
 
 
static void
gtk_morceau_size_allocate (GtkWidget * widget,
                       GtkAllocation * allocation)
{
   g_return_if_fail (widget != NULL);
   g_return_if_fail (GTK_IS_MORCEAU (widget));
   g_return_if_fail (allocation != NULL);
 
 
   widget->allocation = *allocation;
 
   if (GTK_WIDGET_REALIZED (widget)) {
      gdk_window_move_resize (
         widget->window,
         allocation->x, allocation->y,
         allocation->width, allocation->height
      );
   }
}
 
 
static void
gtk_morceau_realize (GtkWidget * widget)
{
   GdkWindowAttr attributes;
   guint attributes_mask;
 
 
   g_return_if_fail (widget != NULL);
   g_return_if_fail (GTK_IS_MORCEAU (widget));
 
 
   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
 
   attributes.window_type = GDK_WINDOW_CHILD;
   attributes.x = widget->allocation.x;
   attributes.y = widget->allocation.y;
   attributes.width = widget->allocation.width;
   attributes.height = widget->allocation.height;
   attributes.wclass = GDK_INPUT_OUTPUT;
   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
 
   attributes_mask = GDK_WA_X | GDK_WA_Y;
 
   widget->window = gdk_window_new (
      gtk_widget_get_parent_window (widget),
      & attributes, attributes_mask
   );
 
   gdk_window_set_user_data (widget->window, widget);
 
   widget->style = gtk_style_attach (widget->style, widget->window);
   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
}
 
 
static gboolean
gtk_morceau_expose (GtkWidget * widget,
                GdkEventExpose * event)
{
   g_return_val_if_fail (widget != NULL, FALSE);
   g_return_val_if_fail (GTK_IS_MORCEAU (widget), FALSE);
   g_return_val_if_fail (event != NULL, FALSE);
 
 
   gtk_morceau_paint (widget);
 
 
   return FALSE;
}
 
 
static void
gtk_morceau_paint (GtkWidget * widget)
{
   GdkPixbuf * pixbuf = NULL;
   gint center_x = 0;
   gint center_y = 0;
 
 
   gdk_window_clear_area (
      widget->window,
      0, 0,
      widget->allocation.width,
      widget->allocation.height
   );
 
 
   center_x = (widget->allocation.width / 2) - (GTK_MORCEAU (widget)->width / 2);
   center_y = (widget->allocation.height / 2) - (GTK_MORCEAU (widget)->height / 2);
 
 
 
 
   if (GTK_MORCEAU (widget)->curs_on == 0){
      if (GTK_MORCEAU (widget)->star)
      pixbuf = GTK_MORCEAU (widget)->with_star_pixbuf;
   else
      pixbuf = GTK_MORCEAU (widget)->without_star_pixbuf;
}
   else if (GTK_MORCEAU (widget)->curs_on == 1)
      pixbuf = GTK_MORCEAU (widget)->with_curs_up_pixbuf;
   else if (GTK_MORCEAU (widget)->curs_on == 2)
      pixbuf = GTK_MORCEAU (widget)->with_curs_down_pixbuf;
   else if (GTK_MORCEAU (widget)->curs_on == 3)
      pixbuf = GTK_MORCEAU (widget)->with_curs_left_pixbuf;
   else if (GTK_MORCEAU (widget)->curs_on == 4)
      pixbuf = GTK_MORCEAU (widget)->with_curs_rigth_pixbuf;
 
   gdk_pixbuf_render_to_drawable (
      pixbuf,
      GDK_DRAWABLE (widget->window),
      NULL,
      0, 0, center_x, center_y,
      GTK_MORCEAU (widget)->width, GTK_MORCEAU (widget)->height,
      GDK_RGB_DITHER_NORMAL, 1, 1
   );
}
 
 
static void
gtk_morceau_destroy (GtkObject * object)
{
   GtkMorceau * morceau;
   GtkMorceauClass * klass;
 
 
   g_return_if_fail (object != NULL);
   g_return_if_fail (GTK_IS_MORCEAU (object));
 
 
   morceau = GTK_MORCEAU (object);
 
   if (morceau->without_star_pixbuf != NULL && morceau->with_star_pixbuf != NULL && morceau->with_curs_up_pixbuf != NULL && morceau->with_curs_down_pixbuf != NULL && morceau->with_curs_left_pixbuf != NULL && morceau->with_curs_rigth_pixbuf != NULL) {
      gdk_pixbuf_unref (morceau->with_star_pixbuf);
      gdk_pixbuf_unref (morceau->without_star_pixbuf);
      gdk_pixbuf_unref (morceau->with_curs_up_pixbuf);
      gdk_pixbuf_unref (morceau->with_curs_down_pixbuf);
gdk_pixbuf_unref (morceau->with_curs_left_pixbuf);
gdk_pixbuf_unref (morceau->with_curs_rigth_pixbuf);
 
      morceau->with_curs_up_pixbuf != NULL;
      morceau->with_curs_down_pixbuf != NULL;
      morceau->with_curs_left_pixbuf != NULL;
      morceau->with_curs_rigth_pixbuf != NULL;
      morceau->without_star_pixbuf = NULL;
      morceau->with_star_pixbuf = NULL;
   }
 
 
   klass = gtk_type_class (gtk_widget_get_type ());
 
   if (GTK_OBJECT_CLASS (klass)->destroy) {
      (* GTK_OBJECT_CLASS (klass)->destroy) (object);
   }
}
et voici le fichier de ma commande "main" :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "curs.h"
#include "morceau.h"
 
 
static void cb_switch (GtkWidget * widget, gpointer data)
{
   GtkWidget * curs = (GtkWidget *) data;
   gtk_curs_set_direction (GTK_CURS (curs), 2);
}
static void cb_switch2 (GtkWidget * widget, gpointer data)
{
   GtkWidget * curs = (GtkWidget *) data;
   gtk_curs_set_direction (GTK_CURS (curs), 3);
}
 
int main(int argc, char** argv){
 
    int i,j,k;
    GtkWidget *pWindow;
    GtkWidget *pVBox2;
    GtkWidget *pVBox;
    GtkWidget *pHBox;
    GtkWidget  *pTable;
    GtkWidget *pEnterBtn;
    GtkWidget *pMoveBtn[4];
    GtkWidget *pMorceau[12];
    GtkWidget *curs;
 
	int n,m;
	int puzzle[3][4] = {{1,1,1,1},{2,1,3,2},{1,1,1,1}};
 
 
 
	// Initialisation GTK.
    gtk_init(&argc, &argv);
 
    // Création de la fenêtre.
    pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(pWindow), 640, 400);
    gtk_window_set_title(GTK_WINDOW(pWindow), "ZeldaBot");
    g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
 
    pVBox = gtk_vbox_new(FALSE, 0);
 
 
        gtk_container_add(GTK_CONTAINER(pWindow), pVBox);
 
 
 
	// Chargement des cases. (Ca coince ici)
   int d =  0;
   while(d<12){
  pMorceau[i]= gtk_morceau_new ("/home/zaadoud/Téléchargements/Bot/Bot/icone1.png",
                         "/home/zaadoud/Téléchargements/Bot/Bot/icone2.png", "/home/zaadoud/Téléchargements/Bot/Bot/up.png",
                         "/home/zaadoud/Téléchargements/Bot/Bot/1.png", "/home/zaadoud/Téléchargements/Bot/Bot/2.png",
                         "/home/zaadoud/Téléchargements/Bot/Bot/Doll.png",
                         FALSE, 0);
d++;
} 
 
	n=3;
	m=4;
   //Création de la table.
   pTable = gtk_table_new(n,m,FALSE);
 
   //Remplissage de la Table.
	k=0;
	/* for (i = 0; i<3; i++) {
		for (j=0; j<4; j++) {
			if (puzzle[i][j] == 1) {
				// Si la valeur est 1, alors case normale.
				gtk_table_attach_defaults(GTK_TABLE(pTable), pMorceau[k],j,j+1,i,i+1);
                                k++;
			} else if (puzzle[i][j]==2) {
				// Si la valeur est 2, alors case avec étoile.
				gtk_morceau_set_star(pMorceau[k], TRUE);
				gtk_table_attach_defaults(GTK_TABLE(pTable), pMorceau[k],j,j+1,i,i+1);
				k++;
			} else if (puzzle[i][j]==3) {
				// Si la valeur est 3, alors case avec curseur.
			        gtk_morceau_set_curs_on (pMorceau[k], 3);
				gtk_table_attach_defaults(GTK_TABLE(pTable), pMorceau[k],j,j+1,i,i+1);
				k++;
			}
 
 
		}
	} */
 
 
 
    gtk_box_pack_start( GTK_BOX(pVBox), pTable, FALSE, FALSE, 0);
 
 
    // Création des 4 boutons principaux.
    pMoveBtn[0] = gtk_button_new_with_label("Forward");
    pMoveBtn[1] = gtk_button_new_with_label("Left");
    pMoveBtn[2] = gtk_button_new_with_label("Right");
    pMoveBtn[3] = gtk_button_new_with_label("Go !");
 
 
 
 
 
    gtk_box_pack_start(GTK_BOX(pHBox), pMoveBtn[0], FALSE,FALSE, 0);
    gtk_box_pack_start(GTK_BOX(pHBox), pMoveBtn[1], FALSE,FALSE, 0);
    gtk_box_pack_start(GTK_BOX(pHBox), pMoveBtn[2], FALSE,FALSE, 0);
    gtk_box_pack_start(GTK_BOX(pHBox), pMoveBtn[3], FALSE,FALSE, 0);
 
 
 
 
    curs = gtk_curs_new("/home/zaadoud/Téléchargements/Bot/Bot/Doll.png","/home/zaadoud/Téléchargements/Bot/Bot/1.png", "/home/zaadoud/Téléchargements/Bot/Bot/2.png", "/home/zaadoud/Téléchargements/Bot/Bot/up.png", 1);
    if (curs == NULL) {
      printf ("Impossible de créer le widget GtkCurs !\n");
   } else {
      gtk_box_pack_start (GTK_BOX (pVBox), curs, TRUE, TRUE, 0);
   /*  g_signal_connect (
         G_OBJECT (pMoveBtn[1]),
         "clicked",
         G_CALLBACK (cb_switch),
         (gpointer) curs
      ); */
 
   }
 
 
 
 
    pHBox = gtk_hbox_new(TRUE, 0);
    gtk_box_pack_start(GTK_BOX(pVBox), pHBox, FALSE,FALSE, 0);
 
 
 
 
 
	gtk_widget_show_all(pWindow);
 
 
 
    gtk_main();
 
    return EXIT_SUCCESS;
 
}

Voilà.
Je vous souhaite un bon aprés midi.