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 Nov 29, 2014 2:22 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Correct callback data parameter
PostPosted: Mon Feb 18, 2008 10:06 pm 
Offline

Joined: Tue Feb 12, 2008 2:50 pm
Posts: 4
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:
/*
* 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);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 3:46 pm 
Offline
Familiar Face

Joined: Wed Dec 19, 2007 5:06 pm
Posts: 13
I suggest you check the callback signature for button_press_event. You're missing a parameter.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 3:59 pm 
Offline

Joined: Tue Feb 12, 2008 2:50 pm
Posts: 4
Code:
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.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group