GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Nov 21, 2014 5:51 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Gtk events call functions with different number of arguments
PostPosted: Tue Feb 19, 2013 2:46 am 
Offline

Joined: Tue Oct 30, 2012 2:25 am
Posts: 4
I keep running into this problem when using the drawing area. I have an "expose-event" where I need to draw on the drawing area to refresh the picture, but also certain actions require drawing on the drawing area such as clicking a toggle button or spinbutton. For instance changing the toggle button changes the picture I'm drawing so it should be redrawn. An example is illustrated below along with the way I have been handling it. For brevity I've left out some initialization calls, but it should be noted that all of the variables in the example some how affect the drawing, so they need to be accessible in the draw function.

Code:
gboolean draw(GtkWidget *widget,GtkExposeEvent *event, void *arguments)  /*Note that widget and event are not used in this function. If the widget is needed it is extracted from the pointer array instead of here.*/
{
              long *pointerarray = (long *)arguments;  /*There is more information needed than can be housed in a single variable, so I created an array that points to all the necessary data*/
              GtkWidget *DrawingArea = (GtkWidget *)*(pointerarray);
              int mode = gtk_toggle_button_get_mode(GTK_TOGGLE_BUTTON(*(pointerarray+1)));//Get state of toggle button
              double *drawingparameter1 = (double *)*(pointerarray+2);
              double *drawingparameter2 = (double *)*(pointerarray+3);
         
             /*Draw stuff on the drawing area based on mode, parameter 1, and parameter 2*/
            return FALSE;
}

gboolean draw_callback(GtkWidget *widget,void *arguments)
{
            draw(NULL,NULL,arguments);
            return FALSE;
}

void main()
{
                  //Initialize everything
                  g_signal_connect(DrawingArea,"expose-event",G_Callback(draw),arguments);
                  g_signal_connect(togglebutton,"pressed",G_Callback(draw_callback),arguments);
}


I have quite a few things that work like this, but its cumbersome and requires extra code and bookkeeping. Something doesn't feel right about having dummy/unused variables and passing NULLs to fill them. The expose event sends 3 arguments and the pressed event sends 2 arguments, so I had to handle them separately. I'm wondering if there is a way to make it so both events use the same number of arguments. Another possibility is that pressing the togglebutton would trigger the drawing area to spit out an "expose-event". If you know a way to do either of these or have another idea on how to handle this please share.

-Anthony Ruth
Most of the programming I do is for controlling machines or scientific calculations.


Top
 Profile  
 
 Post subject: Re: Gtk events call functions with different number of argum
PostPosted: Wed Feb 20, 2013 9:05 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 777
Location: UK
Hi,

Just a couple of things before I move onto your question.

1 - It would be better to pass a pointer to a structure instead of an array if it contains different data types. This would avoid the need to do too much casting which can be error prone. It would also make your code easier to read as you would not need to remember what *(argument+32) means. Or be in the situation where a "long" is not the same size as a pointer.

2 - The "pressed" signal was deprecated in GTK+ 2.8 (August 2005 looking at the date stamps). The signal to use now is "button-press-event", to use this you would need to set the event masks to enable receiving this. This would make the call-back function have the same proto-type as the "expose-event" call-back. NOTE that this signal is not the same as the "toggled" signal. It is possible to receive "button-press-event" signals with out any changes to the state of the GtkToggleButton and the same the other way round.

Going back to your original question, if you really want to cut down on the amount of code you can use g_signal_connect_swapped() http://developer.gnome.org/gobject/stable/gobject-Signals.html#g-signal-connect-swapped. This would swap the user data with the signal data giving you.

Code:
gboolean draw(void *arguments, GtkWidget *widget, GtkExposeEvent *event)
{
   ...
}

gboolean draw_callback(void *arguments, GtkWidget *widget)
{
   ...
}

You can the reduce these by ignoring the arguments after "void *arguments" if you defiantly will not need them
Code:
gboolean draw(void *arguments)
{
   ...
}

gboolean draw_callback(void *arguments)
{
   ...
}

_________________
E.


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

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 3 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group