[patch] avoiding casts of function pointers
- From: Roland Illig <roland illig gmx de>
- To: MC Devel <mc-devel gnome org>
- Subject: [patch] avoiding casts of function pointers
- Date: Tue, 07 Jun 2005 17:12:59 +0200
This patch fixes the gcc warning about incompatible pointer types in
widget.c. I is much more complicated than Jindrich's patch, but in
return it is fully ISO C compliant. :)
Roland
Index: widget.h
===================================================================
RCS file: /cvsroot/mc/mc/src/widget.h,v
retrieving revision 1.40
diff -u -p -r1.40 widget.h
--- widget.h 24 May 2005 11:50:12 -0000 1.40
+++ widget.h 7 Jun 2005 15:11:16 -0000
@@ -123,6 +123,7 @@ typedef struct WGroupbox {
char *title;
} WGroupbox;
+typedef void (*voidfn)(void);
typedef void (*buttonbarfn)(void *);
typedef struct {
@@ -130,7 +131,11 @@ typedef struct {
int visible; /* Is it visible? */
struct {
char *text;
- buttonbarfn function;
+ enum { BBFUNC_NONE, BBFUNC_VOID, BBFUNC_PTR } which;
+ union {
+ voidfn fn_void;
+ buttonbarfn fn_ptr;
+ } u;
void *data;
} labels [10];
} WButtonBar;
@@ -197,8 +202,6 @@ char *listbox_add_item (WListbox *l, enu
/* Hintbar routines */
/* Buttonbar routines */
-typedef void (*voidfn)(void);
-
WButtonBar *buttonbar_new (int visible);
WButtonBar *find_buttonbar (Dlg_head *h);
void buttonbar_set_label (Dlg_head *, int index, const char *text, voidfn);
Index: widget.c
===================================================================
RCS file: /cvsroot/mc/mc/src/widget.c,v
retrieving revision 1.128
diff -u -p -r1.128 widget.c
--- widget.c 27 May 2005 03:35:15 -0000 1.128
+++ widget.c 7 Jun 2005 15:11:16 -0000
@@ -2244,6 +2244,23 @@ listbox_get_current (WListbox *l, char *
*extra = l->current->data;
}
+/* returns TRUE if a function has been called, FALSE otherwise. */
+static gboolean
+buttonbar_call (WButtonBar *bb, int i)
+{
+ switch (bb->labels[i].which) {
+ case BBFUNC_NONE:
+ break;
+ case BBFUNC_VOID:
+ bb->labels[i].u.fn_void ();
+ return TRUE;
+ case BBFUNC_PTR:
+ bb->labels[i].u.fn_ptr (bb->labels[i].data);
+ return TRUE;
+ }
+ return FALSE;
+}
+
static cb_ret_t
buttonbar_callback (Widget *w, widget_msg_t msg, int parm)
@@ -2257,10 +2274,8 @@ buttonbar_callback (Widget *w, widget_ms
case WIDGET_HOTKEY:
for (i = 0; i < 10; i++) {
- if (parm == KEY_F (i + 1) && bb->labels[i].function) {
- (*bb->labels[i].function) (bb->labels[i].data);
+ if (parm == KEY_F (i + 1) && buttonbar_call (bb, i))
return MSG_HANDLED;
- }
}
return MSG_NOT_HANDLED;
@@ -2303,8 +2318,8 @@ buttonbar_event (Gpm_Event *event, void
if (event->y == 2)
return MOU_NORMAL;
button = event->x / 8;
- if (button < 10 && bb->labels [button].function)
- (*bb->labels [button].function)(bb->labels [button].data);
+ if (button < 10)
+ buttonbar_call (bb, button);
return MOU_NORMAL;
}
@@ -2319,8 +2334,8 @@ buttonbar_new (int visible)
bb->visible = visible;
for (i = 0; i < 10; i++){
- bb->labels [i].text = 0;
- bb->labels [i].function = 0;
+ bb->labels[i].text = NULL;
+ bb->labels[i].which = BBFUNC_NONE;
}
widget_want_hotkey (bb->widget, 1);
widget_want_cursor (bb->widget, 0);
@@ -2356,14 +2371,22 @@ buttonbar_set_label_data (Dlg_head *h, i
return;
set_label_text (bb, idx, text);
- bb->labels[idx - 1].function = cback;
+ bb->labels[idx - 1].which = BBFUNC_PTR;
+ bb->labels[idx - 1].u.fn_ptr = cback;
bb->labels[idx - 1].data = data;
}
void
buttonbar_set_label (Dlg_head *h, int idx, const char *text, void (*cback) (void))
{
- buttonbar_set_label_data (h, idx, text, cback, 0);
+ WButtonBar *bb = find_buttonbar (h);
+
+ if (!bb)
+ return;
+
+ set_label_text (bb, idx, text);
+ bb->labels[idx - 1].which = BBFUNC_VOID;
+ bb->labels[idx - 1].u.fn_void = cback;
}
void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]