Post subject: Gtk events call functions with different number of arguments

Posted: Tue Feb 19, 2013 2:46 am

Joined: Tue Oct 30, 2012 2:25 amPosts: 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;}

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 RuthMost of the programming I do is for controlling machines or scientific calculations.

Post subject: Re: Gtk events call functions with different number of argum

Posted: Wed Feb 20, 2013 9:05 am

Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 amPosts: 833Location: 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.

Who is online

Users browsing this forum: Google [Bot] and 6 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum