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 

Correct callback data parameter

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
montgoss



Joined: 12 Feb 2008
Posts: 4

PostPosted: Mon Feb 18, 2008 10:06 pm    Post subject: Correct callback data parameter Reply with quote

I'm trying to pass some data through to a callback and I'm getting weird behavior. I have the type SudokuTile that has a member called "id". I'm trying to pass a pointer to a SudokuTile (one tile out of an array of them) into the callback. Right now, I'm just printing out the id to test. It's almost always 4. If I click the label fast enough, I can get some 5's and 6's in there too, but mostly 4... :?

So, what should go on Line 190-191 below?

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
/*
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <gtk/gtk.h>
#include "stdio.h"

/*--------------------------------------------------------------------------------------------------*/
/* type definitions */
/*--------------------------------------------------------------------------------------------------*/

typedef struct
{
    unsigned int id;
    GtkWidget *mBoardWidget;
    GtkWidget *mLabelBox;
    GtkWidget *mEventBox;
    GtkWidget *mTileLabel1;
    GtkWidget *mTileLabel2;
    GtkWidget *mTileLabel3;
    gboolean editable;
    gboolean penciled;
    unsigned int color;
    short the_value;
    gboolean values[9];
} SudokuTile;

/*--------------------------------------------------------------------------------------------------*/
/* signal callbacks */
/*--------------------------------------------------------------------------------------------------*/

static void new_button_cb (GtkWidget * button, gpointer data);

static void check_button_cb (GtkWidget * button, gpointer data);

static void reset_button_cb (GtkWidget * button, gpointer data);

static void pause_button_cb (GtkWidget * button, gpointer data);

static void tile_button_cb (GtkWidget * button, gpointer tile_data);

static void popup_button_cb (GtkWidget * button, Popup * data);

void init_puzzle(void);

/*--------------------------------------------------------------------------------------------------*/
/* data */
/*--------------------------------------------------------------------------------------------------*/

GtkWidget *window; // main window

/* globals */
static SudokuTile mBoardTiles[81];

unsigned long mPuzzleID;
unsigned int mCurPuzzleRating;


/*--------------------------------------------------------------------------------------------------*/
/* main program */
/*--------------------------------------------------------------------------------------------------*/

int main (int argc, char **argv)
{
    GtkWidget *notebook, *icon;
    GtkWidget *box, *toolbar, *table, *details, *navigation, *w;
    GtkWidget *vsep, *hsep;
    GtkTreeViewColumn *column;
    GtkWidget *widget;
    GtkRcStyle *tile_style;
    GdkColor tile_color_blk = {0, 0, 0, 0};
    GtkToolItem *toolitem;
    GtkListStore *liststore;
    GtkTreeIter it;
    SudokuTile *data;
   
    unsigned int index;

    gtk_init (&argc, &argv);

    /* main window */
   
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    g_signal_connect (G_OBJECT (window), "delete-event",
                        (GCallback) gtk_main_quit, NULL);
    gtk_window_set_title (GTK_WINDOW (window), "Sudoku");

    /* main notebook */
   
notebook = gtk_notebook_new ();
    gtk_container_add (GTK_CONTAINER (window), notebook);
    gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_BOTTOM);

    /* navigation */
   
box = gtk_vbox_new (FALSE, 0);
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook), box,
                                gtk_image_new_from_stock (GTK_STOCK_INDEX,
                                                        GTK_ICON_SIZE_LARGE_TOOLBAR));
    gtk_container_child_set (GTK_CONTAINER (notebook), box, "tab-expand",
                            TRUE, NULL);


    /* puzzle board */
   
table = gtk_table_new( 11, 11, FALSE );
    gtk_box_pack_start (GTK_BOX (box), table, FALSE, FALSE, 0);
    hsep = gtk_hseparator_new();
    gtk_table_attach( GTK_TABLE(table),
                      hsep,
                      0,
                      11,
                      3,
                      4,
                      GTK_SHRINK | GTK_FILL,
                      GTK_SHRINK | GTK_FILL,
                      0,
                      0 );
    hsep = gtk_hseparator_new();
    gtk_table_attach( GTK_TABLE(table),
                      hsep,
                      0,
                      11,
                      7,
                      8,
                      GTK_SHRINK | GTK_FILL,
                      GTK_SHRINK | GTK_FILL,
                      0,
                      0 );

    vsep = gtk_vseparator_new();
    gtk_table_attach( GTK_TABLE(table),
                        vsep,
                        3,
                        4,
                        0,
                        11,
                        GTK_SHRINK | GTK_FILL,
                        GTK_SHRINK | GTK_FILL,
                        0,
                        0 );
    vsep = gtk_vseparator_new();
    gtk_table_attach( GTK_TABLE(table),
                        vsep,
                        7,
                        8,
                        0,
                        11,
                        GTK_SHRINK | GTK_FILL,
                        GTK_SHRINK | GTK_FILL,
                        0,
                        0 );
    for( index = 0; index < 81; index++ )
    {
        mBoardTiles[index].id = index;

        mBoardTiles[index].mBoardWidget = gtk_frame_new(NULL);
       
        mBoardTiles[index].mTileLabel1 = gtk_label_new( "123" );
        mBoardTiles[index].mTileLabel2 = gtk_label_new( "456" );
        mBoardTiles[index].mTileLabel3 = gtk_label_new( "789" );
        gtk_label_set_markup( GTK_LABEL(mBoardTiles[index].mTileLabel1),
                                "<span size=\"x-small\">123</span>");
        gtk_label_set_markup( GTK_LABEL(mBoardTiles[index].mTileLabel2),
                                "<span size=\"x-small\">456</span>");
        gtk_label_set_markup( GTK_LABEL(mBoardTiles[index].mTileLabel3),
                                "<span size=\"x-small\">789</span>");

        gtk_widget_set_size_request( mBoardTiles[index].mTileLabel1, 24, 10);
        gtk_widget_set_size_request( mBoardTiles[index].mTileLabel2, 24, 10);
        gtk_widget_set_size_request( mBoardTiles[index].mTileLabel3, 24, 10);
        mBoardTiles[index].mLabelBox =  gtk_vbox_new( TRUE, 0);
        gtk_container_add( GTK_CONTAINER(mBoardTiles[index].mLabelBox), mBoardTiles[index].mTileLabel1 );
        gtk_container_add( GTK_CONTAINER(mBoardTiles[index].mLabelBox), mBoardTiles[index].mTileLabel2 );
        gtk_container_add( GTK_CONTAINER(mBoardTiles[index].mLabelBox), mBoardTiles[index].mTileLabel3 );
       
        mBoardTiles[index].mEventBox = gtk_event_box_new();
        gtk_container_add( GTK_CONTAINER(mBoardTiles[index].mEventBox), mBoardTiles[index].mLabelBox );
       
        gtk_container_add( GTK_CONTAINER(mBoardTiles[index].mBoardWidget), mBoardTiles[index].mEventBox );

        g_signal_connect( G_OBJECT(mBoardTiles[index].mEventBox), "button_press_event",
                          (GCallback)tile_button_cb, (gpointer)(&mBoardTiles[index]) );

        gtk_table_attach( GTK_TABLE(table),
                          mBoardTiles[index].mBoardWidget,
                          (index%9)+((index/3)%3),
                          ((index%9)+((index/3)%3)+1),
                          (index/9)+(index/27),
                          ((index/9)+(index/27)+1),
                          GTK_SHRINK | GTK_FILL,
                          GTK_SHRINK | GTK_FILL,
                          0,
                          0 );
    }

   
    /* let's go! */
   
gtk_widget_show_all (window);
    gtk_main ();

    return 0;
}

/*--------------------------------------------------------------------------------------------------*/
/* signal callbacks */
/*--------------------------------------------------------------------------------------------------*/

static void new_button_cb (GtkWidget * button, gpointer data)
{
    printf("new_button_cb\n");
    init_puzzle();
}

/*--------------------------------------------------------------------------------------------------*/

static void check_button_cb (GtkWidget * button, gpointer data)
{
    printf("check_button_cb\n");
}

/*--------------------------------------------------------------------------------------------------*/

static void reset_button_cb (GtkWidget * button, gpointer data)
{
    printf("reset_button_cb\n");
}

/*--------------------------------------------------------------------------------------------------*/

static void pause_button_cb (GtkWidget * button, gpointer data)
{
    printf("pause_button_cb\n");
}

/*--------------------------------------------------------------------------------------------------*/

static void tile_button_cb (GtkWidget * button, gpointer tile_data)
{
    SudokuTile *this_tile = (SudokuTile *)tile_data;

    printf("id = %d\n", this_tile->id);
}
 
Back to top
Lindley
Familiar Face


Joined: 19 Dec 2007
Posts: 13

PostPosted: Wed Feb 20, 2008 3:46 pm    Post subject: Reply with quote

I suggest you check the callback signature for button_press_event. You're missing a parameter.
Back to top
montgoss



Joined: 12 Feb 2008
Posts: 4

PostPosted: Wed Feb 20, 2008 3:59 pm    Post subject: Reply with quote

Code: (C)
1
static void tile_button_cb(GtkWidget* button, GdkEventButton* event, gpointer tile_data)

That fixes it. I guess that would explain why it worked when I used a GtkButton widget but not when I used the GtkEventBox.
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