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.