Just to frame things up, I'm doing a clean-up pass on some big interface code in GTK and I've run into a curious issue. Let me explain:
I use glade-2 to bodge the basic code chunks together, and then mogrify them into what I need. In this case it's a column of 8 buttons. Of course glade-2 does a lovely job and makes 8 big blobs of button creation code that take up 4 or 5 screenfuls. That's a good thing. I'm looking at this and know I can make it cleaner and more sophisticated. What I do is this:
Code:
typedef GtkWidget *(*PCFunc)(gpointer);
typedef gboolean (*BTN_cb)(GtkWidget*, GdkEventButton*, gpointer );
No problem. Then this:
Code:
/*--------------------------------------------------------------------------*/
typedef struct _btn_cfg btn_cfg;
struct _btn_cfg
{
char *name;
PCFunc create_func;
BTN_cb cb_func;
};
btn_cfg t1btns[] = {
{"<b>LOC_K\nSCREEN</b>", create_lock_btn, (BTN_cb)lock_btn_cb},
{"<b>L_OG\nOUT</b>", NULL},
{"Start\nShift", NULL},
{"End\nShift", NULL},
{"Cash\nIn", NULL},
{"Cash\nOut", NULL},
{"Msg", NULL},
{"_Admin", NULL},
{NULL}
};
/*--------------------------------------------------------------------------*/
There's the 8 buttons defined all nice and everything. I've left them all out but the first, for clarity. Here's the create_func and cb_func for the first one:
Code:
/*--------------------------------------------------------------------------*/
BTN_cb lock_btn_cb (GtkWidget*w, GdkEventButton*e, gpointer d){
printf ("%s\n", __FUNCTION__);
static int state = 0;
state = !state;
if (!K.logonwin) {
K.logonwin = create_logonwin ();
}
if(state) {
gtk_widget_hide (K.mainwin);
gtk_widget_show (K.logonwin);
K.logonOKbtn = w;
system (SCREEN_BLANK_COMMAND);
} else {
gtk_widget_hide (K.logonwin);
gtk_widget_show (K.mainwin);
}
return (void*)TRUE;
}
/*--------------------------------------------------------------------------*/
GtkWidget *create_lock_btn (void *data)
{
GtkWidget *w = NULL;
char *name = (char*)data;
w = gtk_button_new_with_mnemonic (name);
button_center_label (w);
return w;
}
/*--------------------------------------------------------------------------*/
It works great. No problem.
2 questions:
#1: Why do I need to cast (BTN_cb)lock_btn_cb in the struct definition like this or I get:
Code:
t1.c:97: warning: initialization from incompatible pointer type
#2. Why do I need to cast TRUE to (void*) in the BTN_cb or I get:
Code:
t1.c: In function `lock_btn_cb':
t1.c:77: warning: return makes pointer from integer without a cast
Somehow I think this has to do with macro jiggery pokery. That's a term I got from a guy on another forum when we were talking about the open() func.
Really, this is not a big issue. Just a puzzlement that I'd like to figure out for my own knowledge. I like getting clean compiles with -Wall, it often leads me into odd things, like this.