GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A problem in resetting the event box child widget......

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
luhuadeng
Familiar Face


Joined: 05 May 2008
Posts: 10

PostPosted: Fri May 16, 2008 7:02 am    Post subject: A problem in resetting the event box child widget...... Reply with quote

I am writing a simple game.... I use event box to container image. people can move image
from one event box to another.... etc: When click a event box A the image in A: aimage is selected Then click event box B .....After the two clicks aimage would move to the event box B;
The problem is : When I move the image somethings it does not work . It appears the event box disappear when I remove the image...

the mainly code is show bellow:




My English is pool ,,,,, And I do not know whether I have describe clearly.

Quote:
GtkWidget *image1 = gtk_bin_get_child(GTK_BIN(eventbox[current_state.select_i][current_state.select_j]));
138 GtkWidget *image2 = gtk_bin_get_child(GTK_BIN(eventbox[i][j]));
139
140 g_object_ref(image1);
141 g_object_ref(image2);
142
143 gtk_container_remove(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image1);
144 gtk_container_remove(GTK_CONTAINER(eventbox[i][j]),image2);
145
146 if(position[i][j] >= R_CHE && position[i][j] <= R_BIN)
147 {
148 gtk_widget_destroy(image2);
149 image2 = gtk_image_new_from_pixbuf(pixbuf[14]);
150 }
151 else if(position[i][j] >= B_CHE && position[i][j] <= B_BIN)
152 {
153 gtk_container_add(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image1);
154 gtk_container_add(GTK_CONTAINER(eventbox[i][j]),image2);
155 return TRUE;
156 }
157
158 gtk_container_add(GTK_CONTAINER(eventbox[i][j]),image1);
159 gtk_container_add(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image2);


the code at branche number :15148,149 went wrong. I can not add the image
by line 159...Bur when in branch 153,154 it OK.
Back to top
luhuadeng
Familiar Face


Joined: 05 May 2008
Posts: 10

PostPosted: Fri May 16, 2008 7:19 am    Post subject: The complete cold is here Reply with quote

Code: (C)
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

#include <gtk/gtk.h>


enum _chess
{
    R_CHE,/*&#32418;*/
   
R_MA,
    R_PAO,
    R_JIANG,
    R_SHI,
    R_XIANG,
    R_BIN,
    B_CHE,/*&#40657;*/
   
B_MA,
    B_PAO,
    B_JIANG,
    B_SHI,
    B_XIANG,
    B_BIN,
    EMPTY,
};

static position[10][9]
={
    {R_CHE,R_MA,R_XIANG,R_SHI,R_JIANG,R_SHI,R_XIANG,R_MA,R_CHE},
    {EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY},
    {EMPTY,R_PAO,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,R_PAO,EMPTY},
    {R_BIN,EMPTY,R_BIN,EMPTY,R_BIN,EMPTY,R_BIN,EMPTY,R_BIN,},
    {EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY},
    {EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY},
    {B_BIN,EMPTY,B_BIN,EMPTY,B_BIN,EMPTY,B_BIN,EMPTY,B_BIN,},
    {EMPTY,B_PAO,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,B_PAO,EMPTY},
    {EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY},
    {B_CHE,B_MA,B_XIANG,B_SHI,B_JIANG,B_SHI,B_XIANG,B_MA,B_CHE},
};
typedef struct _ij{
    int i;
    int j;
} IJ;


GtkWidget * eventbox[10][9];
GdkPixbuf *pixbuf[16];
IJ ij[10][9];

GtkWidget *fixed;

GdkPixmap *load_pixmap (GtkWidget *widget,gchar *filename)
{
    GdkPixbuf *pixbuf=NULL;
    GdkPixmap *pixmap=NULL;
    GdkBitmap *window_shape_bitmap;
    pixbuf = gdk_pixbuf_new_from_file (filename,NULL);
    pixmap = gdk_pixmap_new (widget->window,gdk_pixbuf_get_width(pixbuf),gdk_pixbuf_get_height(pixbuf),-1);

    gdk_pixbuf_render_to_drawable (pixbuf,pixmap,
            widget->style->fg_gc[GTK_STATE_NORMAL],
            0,0,0,0,
            gdk_pixbuf_get_width(pixbuf),
            gdk_pixbuf_get_height(pixbuf),
            GDK_RGB_DITHER_NORMAL,0,0);
    g_object_unref (pixbuf);
    return pixmap;
}
struct {
    int player;/* 0 black 1 red*/
   
int selected;/* 0 select 1 unselect*/
   
int select_i;
    int select_j;
    int dest_i;
    int dest_j;
} current_state;
static gboolean button_press_callback (GtkWidget      *event_box,
        GdkEventButton *event,
        gpointer        data)
{
    IJ *position_ij = (IJ *)data;

    g_print ("Event box pressed at coordinates %d,%d,%d\n",
            position_ij->i, position_ij->j,position[position_ij->i][position_ij->j]);
    int i = position_ij->i;
    int j = position_ij->j;
    if(current_state.selected == 0)//unselect any chess
   
{
        if(current_state.player == 0)//black play
       
{
            if(position[i][j] >= R_CHE && position[i][j] <= R_BIN)
                return TRUE;
            else if(position[i][j] >= B_CHE && position[i][j] <= B_BIN)
            {
                g_print("selected\n");
                current_state.selected = 1;
                current_state.select_i = i;
                current_state.select_j = j;
                return TRUE;
            }
            else
           
{
                g_print("error\n");
                return TRUE;
            }
        }
        else if(current_state.player == 1) //red play
       
{
            if(position[i][j] >= B_CHE && position[i][j] <= B_BIN)
                return TRUE;
            else if(position[i][j] >= R_CHE && position[i][j] <= R_BIN)
            {
                g_print("selected\n");
                current_state.selected = 1;
                current_state.select_i = i;
                current_state.select_j = j;
                return TRUE;
            }
            else
           
{
                g_print("error\n");
                return TRUE;
            }

        }
       
    }
    else if(current_state.selected == 1)
    {
        if(current_state.player == 0) //black player
       
{
            if(current_state.select_i == i && current_state.select_j == j) //select the same chess
           
{
                g_print("unselect\n");
                current_state.selected = 0;
            }
            else
           
{// mv the chess to the dest i,j
               
g_print("mv to the deat i,j\n");

                GtkWidget *image1 = gtk_bin_get_child(GTK_BIN(eventbox[current_state.select_i][current_state.select_j]));
                GtkWidget *image2 = gtk_bin_get_child(GTK_BIN(eventbox[i][j]));
               
                g_object_ref(image1);
                g_object_ref(image2);

                gtk_container_remove(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image1);
                gtk_container_remove(GTK_CONTAINER(eventbox[i][j]),image2);

                if(position[i][j] >= R_CHE && position[i][j] <= R_BIN)
                {
                    gtk_widget_destroy(image2);
                    image2 = gtk_image_new_from_pixbuf(pixbuf[14]);
                }
                else if(position[i][j] >= B_CHE && position[i][j] <= B_BIN)
                {
                    gtk_container_add(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image1);
                    gtk_container_add(GTK_CONTAINER(eventbox[i][j]),image2);
                    return TRUE;
                }

                gtk_container_add(GTK_CONTAINER(eventbox[i][j]),image1);
                gtk_container_add(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image2);
                position[i][j] = position[current_state.select_i][current_state.select_j];
                position[current_state.select_i][current_state.select_j] = EMPTY;
                current_state.player = 1;
                current_state.selected = 0;

            }
        }
        else if(current_state.player == 1) //red player
       
{
            if(current_state.select_i == i && current_state.select_j == j) //select the same chess
           
{
                g_print("unselected\n");
                current_state.selected = 0;
            }
            else
           
{// mv the chess to the dest i,j
               
g_print("mv to the deat i,j\n");

                GtkWidget *image1 = gtk_bin_get_child(GTK_BIN(eventbox[current_state.select_i][current_state.select_j]));
                GtkWidget *image2 = gtk_bin_get_child(GTK_BIN(eventbox[i][j]));
               
                g_object_ref(image1);
                g_object_ref(image2);

                gtk_container_remove(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image1);
                gtk_container_remove(GTK_CONTAINER(eventbox[i][j]),image2);

                if(position[i][j] >= B_CHE && position[i][j] <= B_BIN)
                {
                    gtk_widget_destroy(image2);
                    image2 = gtk_image_new_from_pixbuf(pixbuf[14]);

                }else if(position[i][j] >= R_CHE && position[i][j] <= R_BIN)
                {
                    gtk_container_add(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image1);
                    gtk_container_add(GTK_CONTAINER(eventbox[i][j]),image2);
                    return TRUE;
                }
                position[i][j] = position[current_state.select_i][current_state.select_j];
                position[current_state.select_i][current_state.select_j] = EMPTY;

                gtk_container_add(GTK_CONTAINER(eventbox[i][j]),image1);
                gtk_container_add(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image2);


                current_state.player = 0;
                current_state.selected = 0;

            }

        }
        else
       
{
            g_print("error\n");
        }

    }
    else
   
{
        g_print("error\n");
    }
//    gtk_widget_show_all (window1);
   
return TRUE;
}
void put_event_box(GtkWidget *fixed)
{
    int i;
    int j;
    for(i =0;i<10;i++)
        for(j =0;j<9;j++)
        {
            GtkWidget *image;
            eventbox[i][j] = gtk_event_box_new();
            ij[i][j].i = i;
            ij[i][j].j = j;
            g_signal_connect(G_OBJECT(eventbox[i][j]),"button_press_event",G_CALLBACK(button_press_callback),&ij[i][j]);
            gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox[i][j]),FALSE);
            image = gtk_image_new_from_pixbuf(g_object_ref(pixbuf[position[i][j]]));
            gtk_container_add(GTK_CONTAINER(eventbox[i][j]),image);
            gtk_fixed_put(GTK_FIXED(fixed),eventbox[i][j],190+43 *j ,50+44 *i);
        }
   
}
int main(int argc, char **argv)
{
    GtkWidget *window;
    GtkWidget *image;
    GtkWidget *vbox;

    GdkBitmap *bitmap;
    GdkPixmap *chessborder_bg;
    GdkPixbuf *pixbuf_total;
    GdkColormap *colormap;
    GdkColor black;
    GdkColor white;
    GdkGC *gc;
    gtk_init(&argc, &argv);
//    window = gtk_window_new(GTK_WINDOW_POPUP);
   
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_app_paintable(window,TRUE);
    gtk_window_set_default_size(GTK_WINDOW(window),800,600);
    g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
    gtk_widget_realize(window);
//    vbox = gtk_vbox_new(FALSE,0);
//    gtk_container_add(GTK_CONTAINER(window),vbox);
   
   
   
pixbuf_total = gdk_pixbuf_new_from_file("./800/chess.tga",NULL);
    image = gtk_image_new_from_file("./800/bg.bmp");

    fixed = gtk_fixed_new();
    gtk_container_add(GTK_CONTAINER(window),fixed);
    gtk_widget_show(fixed);   
    chessborder_bg = load_pixmap(fixed,"./800/bg.bmp");
    gdk_window_set_back_pixmap(fixed->window,chessborder_bg,FALSE);
    bitmap = gdk_pixmap_new(NULL,800,600,1);
    gc = gdk_gc_new(bitmap);
    colormap = gdk_colormap_get_system();
    gdk_color_black(colormap,&black);
    gdk_color_white(colormap,&white);
    gdk_gc_set_foreground(gc,&white);
    gdk_gc_set_background(gc,&white);
    gdk_draw_rectangle(bitmap,gc,TRUE,0,0,800,600);
    gtk_widget_shape_combine_mask(window,bitmap,0,0);
   
    gint srcx=0,srcy=0,width=42,height=41,destx=0,desty=0;
    gint i=1;
    for( i =0;i< 14;i++)
    {
        pixbuf[i] = gdk_pixbuf_new_from_file("./800/BlackIcon.tga",NULL);
        gdk_pixbuf_copy_area(pixbuf_total,srcx + width *i,srcy,width-1,height,pixbuf[i],destx,desty);
    //    image = gtk_image_new_from_pixbuf(pixbuf[i]);
        //eventbox[i] = gtk_event_box_new();
        //gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox[i]),FALSE);
        //gtk_container_add(GTK_CONTAINER(eventbox[i]),image);
       
//        gtk_box_pack_start(GTK_BOX(vbox),image,FALSE,FALSE,0);
        //gtk_fixed_put(GTK_FIXED(fixed),eventbox[i],40*i,40*i);
   
}
    pixbuf[15] = gdk_pixbuf_new_from_file("./800/empty.tga",NULL);
    pixbuf[14] = gdk_pixbuf_new_subpixbuf(pixbuf[15],0,0,41,41);

    put_event_box(fixed);


    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

[/url]
Back to top
luhuadeng
Familiar Face


Joined: 05 May 2008
Posts: 10

PostPosted: Sun May 18, 2008 1:48 am    Post subject: I get it.. Reply with quote

Code: (Plaintext)
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

                GtkWidget *image1 = gtk_bin_get_child(GTK_BIN(eventbox[current_state.select_i][current_state.select_j]));
                GtkWidget *image2 = gtk_bin_get_child(GTK_BIN(eventbox[i][j]));
               
                g_object_ref(image1);
                g_object_ref(image2);

                gtk_container_remove(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image1);
                gtk_container_remove(GTK_CONTAINER(eventbox[i][j]),image2);

                if(position[i][j] >= R_CHE && position[i][j] <= R_BIN)
                {
                    gtk_widget_destroy(image2);
                    image2 = gtk_image_new_from_pixbuf(pixbuf[14]);
                }
                else if(position[i][j] >= B_CHE && position[i][j] <= B_BIN)
                {
                    gtk_container_add(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image1);
                    gtk_container_add(GTK_CONTAINER(eventbox[i][j]),image2);
                    return TRUE;
                }

                gtk_container_add(GTK_CONTAINER(eventbox[i][j]),image1);
                gtk_container_add(GTK_CONTAINER(eventbox[current_state.select_i][current_state.select_j]),image2);


It is cause by the newly create widget do not have realized.
add
Code: (Plaintext)
1
2
3

gtk_widget_show(image2)

after the
Code: (Plaintext)
1
2
3

                  image2 = gtk_image_new_from_pixbuf(pixbuf[14]);

can resolve the problem..
the final code look like
Code: (Plaintext)
1
2
3
4
5
6

190                                         gtk_widget_destroy(image2);
191                                         GtkWidget *image2 = gtk_image_new_from_pixbuf(pixbuf[14]);
192                                         gtk_widget_show(GTK_WIDGET(image2));

Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP