From 0807b405139259e719115b99755cc4a6153542f6 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Tue, 4 May 2010 22:41:58 +0000 Subject: [PATCH] rationalise callback routines to use a unified interface svn path=/trunk/netsurf/; revision=10550 --- framebuffer/fbtk.c | 261 +++++++++++++++++++------------ framebuffer/fbtk.h | 50 +++--- framebuffer/fbtk_widget.h | 20 +-- framebuffer/fbtk_widget_scroll.c | 162 ++++++------------- framebuffer/gui.c | 150 +++++++----------- 5 files changed, 293 insertions(+), 350 deletions(-) diff --git a/framebuffer/fbtk.c b/framebuffer/fbtk.c index cd5816f92..56215bdd5 100644 --- a/framebuffer/fbtk.c +++ b/framebuffer/fbtk.c @@ -230,10 +230,11 @@ remove_widget_from_window(fbtk_widget_t *window, fbtk_widget_t *widget) } static void -fbtk_redraw_widget(fbtk_widget_t *root, fbtk_widget_t *widget) +fbtk_redraw_widget(fbtk_widget_t *widget) { nsfb_bbox_t saved_plot_ctx; nsfb_bbox_t plot_ctx; + fbtk_widget_t *root = get_root_widget(widget); //LOG(("widget %p type %d", widget, widget->type)); if (widget->redraw_required == false) @@ -242,7 +243,7 @@ fbtk_redraw_widget(fbtk_widget_t *root, fbtk_widget_t *widget) widget->redraw_required = false; /* ensure there is a redraw handler */ - if (widget->redraw == NULL) + if (fbtk_get_handler(widget, FBTK_CBT_REDRAW) == NULL) return; /* get the current clipping rectangle */ @@ -258,8 +259,7 @@ fbtk_redraw_widget(fbtk_widget_t *root, fbtk_widget_t *widget) nsfb_plot_set_clip(root->u.root.fb, &plot_ctx); - /* do our drawing according to type */ - widget->redraw(root, widget, widget->redrawpw); + fbtk_post_callback(widget, FBTK_CBT_REDRAW); /* restore clipping rectangle */ nsfb_plot_set_clip(root->u.root.fb, &saved_plot_ctx); @@ -272,9 +272,11 @@ fbtk_redraw_widget(fbtk_widget_t *root, fbtk_widget_t *widget) /*************** redraw widgets **************/ static int -fb_redraw_fill(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) +fb_redraw_fill(fbtk_widget_t *widget, fbtk_callback_info *cbi) { nsfb_bbox_t bbox; + fbtk_widget_t *root = get_root_widget(widget); + fbtk_get_bbox(widget, &bbox); nsfb_claim(root->u.root.fb, &bbox); @@ -292,10 +294,11 @@ fb_redraw_fill(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) static int -fb_redraw_bitmap(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) +fb_redraw_bitmap(fbtk_widget_t *widget, fbtk_callback_info *cbi) { nsfb_bbox_t bbox; nsfb_bbox_t rect; + fbtk_widget_t *root = get_root_widget(widget); fbtk_get_bbox(widget, &bbox); @@ -310,7 +313,13 @@ fb_redraw_bitmap(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) } /* plot the image */ - nsfb_plot_bitmap(root->u.root.fb, &rect, (nsfb_colour_t *)widget->u.bitmap.bitmap->pixdata, widget->u.bitmap.bitmap->width, widget->u.bitmap.bitmap->height, widget->u.bitmap.bitmap->width, !widget->u.bitmap.bitmap->opaque); + nsfb_plot_bitmap(root->u.root.fb, + &rect, + (nsfb_colour_t *)widget->u.bitmap.bitmap->pixdata, + widget->u.bitmap.bitmap->width, + widget->u.bitmap.bitmap->height, + widget->u.bitmap.bitmap->width, + !widget->u.bitmap.bitmap->opaque); nsfb_update(root->u.root.fb, &bbox); @@ -318,26 +327,23 @@ fb_redraw_bitmap(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) } static int -fbtk_window_default_redraw(fbtk_widget_t *root, fbtk_widget_t *window, void *pw) +fbtk_window_default_redraw(fbtk_widget_t *window, fbtk_callback_info *cbi) { fbtk_widget_list_t *lent; int res = 0; - if (!window->redraw) - return res; - /* get the list of widgets */ lent = window->u.window.widgets; while (lent != NULL) { - fbtk_redraw_widget(root, lent->widget); + fbtk_redraw_widget(lent->widget); lent = lent->next; } return res; } static int -fbtk_window_default_move(fbtk_widget_t *window, int x, int y, void *pw) +fbtk_window_default_move(fbtk_widget_t *window, fbtk_callback_info *cbi) { fbtk_widget_list_t *lent; fbtk_widget_t *widget; @@ -349,16 +355,14 @@ fbtk_window_default_move(fbtk_widget_t *window, int x, int y, void *pw) while (lent != NULL) { widget = lent->widget; - if ((x > widget->x) && - (y > widget->y) && - (x < widget->x + widget->width) && - (y < widget->y + widget->height)) { - if (widget->move != NULL) { - res = widget->move(widget, - x - widget->x, - y - widget->y, - widget->movepw); - } + if ((cbi->x > widget->x) && + (cbi->y > widget->y) && + (cbi->x < widget->x + widget->width) && + (cbi->y < widget->y + widget->height)) { + res = fbtk_post_callback(widget, + FBTK_CBT_POINTERMOVE, + cbi->x - widget->x, + cbi->y - widget->y); break; } lent = lent->prev; @@ -367,7 +371,7 @@ fbtk_window_default_move(fbtk_widget_t *window, int x, int y, void *pw) } static int -fbtk_window_default_click(fbtk_widget_t *window, nsfb_event_t *event, int x, int y, void *pw) +fbtk_window_default_click(fbtk_widget_t *window, fbtk_callback_info *cbi) { fbtk_widget_list_t *lent; fbtk_widget_t *widget; @@ -379,25 +383,22 @@ fbtk_window_default_click(fbtk_widget_t *window, nsfb_event_t *event, int x, int while (lent != NULL) { widget = lent->widget; - if ((x > widget->x) && - (y > widget->y) && - (x < widget->x + widget->width) && - (y < widget->y + widget->height)) { - if (widget->input != NULL) { + if ((cbi->x > widget->x) && + (cbi->y > widget->y) && + (cbi->x < widget->x + widget->width) && + (cbi->y < widget->y + widget->height)) { + if (fbtk_get_handler(widget, FBTK_CBT_INPUT) != NULL) { fbtk_widget_t *root = get_root_widget(widget); root->u.root.input = widget; } - if (widget->click != NULL) { - res = widget->click(widget, - event, - x - widget->x, - y - widget->y, - widget->clickpw); - break; - } - - + res = fbtk_post_callback(widget, + FBTK_CBT_CLICK, + cbi->event, + cbi->x - widget->x, + cbi->y - widget->y); + if (res != 0) + break; } lent = lent->next; @@ -406,10 +407,11 @@ fbtk_window_default_click(fbtk_widget_t *window, nsfb_event_t *event, int x, int } static int -fb_redraw_text(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) +fb_redraw_text(fbtk_widget_t *widget, fbtk_callback_info *cbi ) { nsfb_bbox_t bbox; nsfb_bbox_t rect; + fbtk_widget_t *root = get_root_widget(widget); fbtk_get_bbox(widget, &bbox); @@ -450,10 +452,10 @@ fb_redraw_text(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) static int -text_input(fbtk_widget_t *widget, nsfb_event_t *event, void *pw) +text_input(fbtk_widget_t *widget, fbtk_callback_info *cbi) { int value; - if (event == NULL) { + if (cbi->event == NULL) { /* gain focus */ if (widget->u.text.text == NULL) widget->u.text.text = calloc(1,1); @@ -462,13 +464,12 @@ text_input(fbtk_widget_t *widget, nsfb_event_t *event, void *pw) fbtk_request_redraw(widget); return 0; - } - if (event->type != NSFB_EVENT_KEY_DOWN) + if (cbi->event->type != NSFB_EVENT_KEY_DOWN) return 0; - value = event->value.keycode; + value = cbi->event->value.keycode; switch (value) { case NSFB_KEY_BACKSPACE: if (widget->u.text.idx <= 0) @@ -501,8 +502,7 @@ text_input(fbtk_widget_t *widget, nsfb_event_t *event, void *pw) if (temp != NULL) { widget->u.text.text = temp; widget->u.text.text[widget->u.text.idx] = value; - widget->u.text.text[widget->u.text.idx + 1] = - '\0'; + widget->u.text.text[widget->u.text.idx + 1] = '\0'; widget->u.text.idx++; } } @@ -521,8 +521,7 @@ fbtk_writable_text(fbtk_widget_t *widget, fbtk_enter_t enter, void *pw) widget->u.text.enter = enter; widget->u.text.pw = pw; - widget->input = text_input; - widget->inputpw = widget; + fbtk_set_handler(widget, FBTK_CBT_INPUT, text_input, widget); } @@ -585,33 +584,38 @@ fbtk_get_bbox(fbtk_widget_t *widget, nsfb_bbox_t *bbox) return true; } -void -fbtk_set_handler_click(fbtk_widget_t *widget, fbtk_mouseclick_t click, void *pw) +fbtk_callback +fbtk_get_handler(fbtk_widget_t *widget, fbtk_callback_type cbt) { - widget->click = click; - widget->clickpw = pw; + if ((cbt <= FBTK_CBT_START) || (cbt >= FBTK_CBT_END)) { + /* type out of range, no way to report error so return NULL */ + return NULL; + } + + return widget->callback[cbt]; } -void -fbtk_set_handler_input(fbtk_widget_t *widget, fbtk_input_t input, void *pw) +fbtk_callback +fbtk_set_handler(fbtk_widget_t *widget, + fbtk_callback_type cbt, + fbtk_callback cb, + void *context) { - widget->input = input; - widget->inputpw = pw; + fbtk_callback prevcb; + + if ((cbt <= FBTK_CBT_START) || (cbt >= FBTK_CBT_END)) { + /* type out of range, no way to report error so return NULL */ + return NULL; + } + + prevcb = widget->callback[cbt]; + + widget->callback[cbt] = cb; + widget->callback_context[cbt] = context; + + return prevcb; } -void -fbtk_set_handler_redraw(fbtk_widget_t *widget, fbtk_redraw_t redraw, void *pw) -{ - widget->redraw = redraw; - widget->redrawpw = pw; -} - -void -fbtk_set_handler_move(fbtk_widget_t *widget, fbtk_move_t move, void *pw) -{ - widget->move = move; - widget->movepw = pw; -} void * fbtk_get_userpw(fbtk_widget_t *widget) @@ -701,18 +705,13 @@ fbtk_input(fbtk_widget_t *root, nsfb_event_t *event) if (input == NULL) return; /* no widget with input */ - if (input->input == NULL) - return; - - /* call the widgets input method */ - input->input(input, event, input->inputpw); + fbtk_post_callback(input, FBTK_CBT_INPUT, event); } void fbtk_click(fbtk_widget_t *widget, nsfb_event_t *event) { fbtk_widget_t *root; - fbtk_widget_t *window; nsfb_bbox_t cloc; /* ensure we have the root widget */ @@ -720,11 +719,9 @@ fbtk_click(fbtk_widget_t *widget, nsfb_event_t *event) nsfb_cursor_loc_get(root->u.root.fb, &cloc); - /* get the root window */ - window = root->u.root.rootw; - LOG(("click %d, %d",cloc.x0,cloc.y0)); - if (window->click != NULL) - window->click(window, event, cloc.x0, cloc.y0, window->clickpw); + + /* post the click */ + fbtk_post_callback(root->u.root.rootw, FBTK_CBT_CLICK, event, cloc.x0, cloc.y0); } @@ -733,7 +730,6 @@ void fbtk_move_pointer(fbtk_widget_t *widget, int x, int y, bool relative) { fbtk_widget_t *root; - fbtk_widget_t *window; nsfb_bbox_t cloc; /* ensure we have the root widget */ @@ -750,13 +746,11 @@ fbtk_move_pointer(fbtk_widget_t *widget, int x, int y, bool relative) root->redraw_required = true; + /* update the pointer cursor */ nsfb_cursor_loc_set(root->u.root.fb, &cloc); - /* get the root window */ - window = root->u.root.rootw; - - if (window->move != NULL) - window->move(window, cloc.x0, cloc.y0, window->movepw); + /* post the movement */ + fbtk_post_callback(root->u.root.rootw, FBTK_CBT_POINTERMOVE, cloc.x0, cloc.y0); } @@ -782,7 +776,7 @@ fbtk_redraw(fbtk_widget_t *widget) if (!root->redraw_required) return 0; - fbtk_redraw_widget(root, root->u.root.rootw); + fbtk_post_callback(root->u.root.rootw, FBTK_CBT_REDRAW); widget->redraw_required = false; @@ -822,7 +816,7 @@ fbtk_create_text(fbtk_widget_t *window, newt->fg = fg; newt->bg = bg; - newt->redraw = fb_redraw_text; + fbtk_set_handler(newt, FBTK_CBT_REDRAW, fb_redraw_text, NULL); return add_widget_to_window(window, newt); } @@ -840,7 +834,7 @@ fbtk_create_bitmap(fbtk_widget_t *window, int x, int y, colour c, struct bitmap newb->u.bitmap.bitmap = image; - newb->redraw = fb_redraw_bitmap; + fbtk_set_handler(newb, FBTK_CBT_REDRAW, fb_redraw_bitmap, NULL); return add_widget_to_window(window, newb); } @@ -882,25 +876,39 @@ fbtk_create_fill(fbtk_widget_t *window, int x, int y, int width, int height, col neww->bg = c; - neww->redraw = fb_redraw_fill; + fbtk_set_handler(neww, FBTK_CBT_REDRAW, fb_redraw_fill, NULL); return add_widget_to_window(window, neww); } +/* set pointer to bitmap in context on cursor move */ +static int +fbtk_set_ptr_move(fbtk_widget_t *widget, fbtk_callback_info *cbi) +{ + fbtk_widget_t *root = get_root_widget(widget); + struct bitmap *bm = cbi->context; + nsfb_cursor_set(root->u.root.fb, + (nsfb_colour_t *)bm->pixdata, + bm->width, + bm->height, + bm->width); + + return 0; +} fbtk_widget_t * fbtk_create_button(fbtk_widget_t *window, int x, int y, colour c, struct bitmap *image, - fbtk_mouseclick_t click, + fbtk_callback click, void *pw) { fbtk_widget_t *newb = fbtk_create_bitmap(window, x, y, c, image); - newb->click = click; - newb->clickpw = pw; + fbtk_set_handler(newb, FBTK_CBT_CLICK, click, pw); + fbtk_set_handler(newb, FBTK_CBT_POINTERMOVE, fbtk_set_ptr_move, &hand_image); return newb; } @@ -917,8 +925,8 @@ fbtk_create_writable_text(fbtk_widget_t *window, newt->u.text.enter = enter; newt->u.text.pw = pw; - newt->input = text_input; - newt->inputpw = newt; + fbtk_set_handler(newt, FBTK_CBT_INPUT, text_input, newt); + return newt; } @@ -1011,15 +1019,68 @@ fbtk_create_window(fbtk_widget_t *parent, newwin->width = width; newwin->height = height; - newwin->redraw = fbtk_window_default_redraw; - newwin->move = fbtk_window_default_move; - newwin->click = fbtk_window_default_click; - + fbtk_set_handler(newwin, FBTK_CBT_REDRAW, fbtk_window_default_redraw, NULL); + fbtk_set_handler(newwin, FBTK_CBT_POINTERMOVE, fbtk_window_default_move, NULL); + fbtk_set_handler(newwin, FBTK_CBT_CLICK, fbtk_window_default_click, NULL); LOG(("Created window %p %d,%d %d,%d",newwin,x,y,width,height)); return add_widget_to_window(parent, newwin); } +int +fbtk_post_callback(fbtk_widget_t *widget, fbtk_callback_type cbt, ...) +{ + fbtk_callback_info cbi; + int ret = 0; + va_list ap; + + if (widget->callback[cbt] != NULL) { + cbi.type = cbt; + cbi.context = widget->callback_context[cbt]; + + va_start(ap, cbt); + + switch (cbt) { + case FBTK_CBT_SCROLLX: + cbi.x = va_arg(ap,int); + break; + + case FBTK_CBT_SCROLLY: + cbi.y = va_arg(ap,int); + break; + + case FBTK_CBT_CLICK: + cbi.event = va_arg(ap, void *); + cbi.x = va_arg(ap, int); + cbi.y = va_arg(ap, int); + break; + + case FBTK_CBT_INPUT: + cbi.event = va_arg(ap, void *); + break; + + case FBTK_CBT_POINTERMOVE: + cbi.x = va_arg(ap, int); + cbi.y = va_arg(ap, int); + break; + + case FBTK_CBT_REDRAW: + break; + + case FBTK_CBT_USER: + break; + + default: + break; + } + va_end(ap); + + ret = (widget->callback[cbt])(widget, &cbi); + } + + return ret; +} + bool fbtk_event(fbtk_widget_t *root, nsfb_event_t *event, int timeout) { bool unused = false; /* is the event available */ diff --git a/framebuffer/fbtk.h b/framebuffer/fbtk.h index e1f117e0b..fd104452b 100644 --- a/framebuffer/fbtk.h +++ b/framebuffer/fbtk.h @@ -27,23 +27,26 @@ typedef struct fbtk_widget_s fbtk_widget_t; -enum fbtk_callback_info_type { - FBTK_CBIT_SCROLLX = 0, - FBTK_CBIT_SCROLLY, - FBTK_CBIT_CLICK, - FBTK_CBIT_INPUT, - FBTK_CBIT_MOVE, - FBTK_CBIT_REDRAW, - FBTK_CBIT_END, -}; +typedef enum fbtk_callback_type { + FBTK_CBT_START = 0, + FBTK_CBT_SCROLLX, + FBTK_CBT_SCROLLY, + FBTK_CBT_CLICK, + FBTK_CBT_INPUT, + FBTK_CBT_POINTERMOVE, + FBTK_CBT_REDRAW, + FBTK_CBT_USER, + FBTK_CBT_END, +} fbtk_callback_type; typedef struct fbtk_callback_info { - enum fbtk_callback_info_type type; + enum fbtk_callback_type type; void *context; nsfb_event_t *event; int x; int y; char *text; + fbtk_widget_t *widget; } fbtk_callback_info; typedef int (*fbtk_callback)(fbtk_widget_t *widget, fbtk_callback_info *cbi); @@ -52,22 +55,14 @@ typedef int (*fbtk_callback)(fbtk_widget_t *widget, fbtk_callback_info *cbi); /* user widget callback */ typedef int (*fbtk_user_t)(fbtk_widget_t *widget, void *pw); -/* input callback */ -typedef int (*fbtk_input_t)(fbtk_widget_t *widget, nsfb_event_t *event, void *pw); - -/* mouse click callback */ -typedef int (*fbtk_mouseclick_t)(fbtk_widget_t *widget, nsfb_event_t *event, int x, int y, void *pw); - -/* mouse move callback */ -typedef int (*fbtk_move_t)(fbtk_widget_t *widget, int x, int y, void *pw); - -/* redraw function */ -typedef int (*fbtk_redraw_t)(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw); /* enter pressed on writable icon */ typedef int (*fbtk_enter_t)(void *pw, char *text); +/* helper function to allow simple method to call callbacks */ +int fbtk_post_callback(fbtk_widget_t *widget, fbtk_callback_type cbt, ...); + /* Widget creation */ /** Initialise widget toolkit. @@ -160,7 +155,7 @@ fbtk_widget_t *fbtk_create_user(fbtk_widget_t *window, int x, int y, int width, * @param window The window to add the button widget to. * @return new widget handle or NULL on error. */ -fbtk_widget_t *fbtk_create_button(fbtk_widget_t *window, int x, int y, colour c, struct bitmap *image, fbtk_mouseclick_t click, void *pw); +fbtk_widget_t *fbtk_create_button(fbtk_widget_t *window, int x, int y, colour c, struct bitmap *image, fbtk_callback click, void *pw); /** Create a writable text widget. * @@ -199,10 +194,13 @@ void fbtk_set_bitmap(fbtk_widget_t *widget, struct bitmap *image); void fbtk_set_scroll(fbtk_widget_t *widget, int pct); void fbtk_set_scroll_pos(fbtk_widget_t *widget, int pos); void fbtk_set_pos_and_size(fbtk_widget_t *widget, int x, int y, int width, int height); -void fbtk_set_handler_redraw(fbtk_widget_t *widget, fbtk_redraw_t input, void *pw); -void fbtk_set_handler_input(fbtk_widget_t *widget, fbtk_input_t input, void *pw); -void fbtk_set_handler_click(fbtk_widget_t *widget, fbtk_mouseclick_t click, void *pw); -void fbtk_set_handler_move(fbtk_widget_t *widget, fbtk_move_t move, void *pw); +; + +/** Set a callback handler */ +fbtk_callback fbtk_set_handler(fbtk_widget_t *widget, fbtk_callback_type cbt, fbtk_callback cb, void *pw); + +/** Get a callback handler */ +fbtk_callback fbtk_get_handler(fbtk_widget_t *widget, fbtk_callback_type cbt); /** Alter a text widget to be writable. */ diff --git a/framebuffer/fbtk_widget.h b/framebuffer/fbtk_widget.h index c6d365712..c542c2012 100644 --- a/framebuffer/fbtk_widget.h +++ b/framebuffer/fbtk_widget.h @@ -42,26 +42,14 @@ struct fbtk_widget_s { colour bg; colour fg; - /* handlers */ - fbtk_mouseclick_t click; - void *clickpw; /* private data for callback */ + /* event callback handlers */ + fbtk_callback callback[FBTK_CBT_END]; + void *callback_context[FBTK_CBT_END]; - fbtk_input_t input; - void *inputpw; /* private data for callback */ - - fbtk_move_t move; - void *movepw; /* private data for callback */ - - fbtk_redraw_t redraw; - void *redrawpw; /* private data for callback */ - - bool redraw_required; + bool redraw_required; /* the widget requires redrawing */ fbtk_widget_t *parent; /* parent widget */ - fbtk_callback callback; /* event callback */ - void *callback_context; - /* Widget specific */ enum fbtk_widgettype_e type; diff --git a/framebuffer/fbtk_widget_scroll.c b/framebuffer/fbtk_widget_scroll.c index fab2d0445..c28887bda 100644 --- a/framebuffer/fbtk_widget_scroll.c +++ b/framebuffer/fbtk_widget_scroll.c @@ -37,13 +37,14 @@ /** Vertical scroll widget */ static int -vscroll_redraw(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) +vscroll_redraw(fbtk_widget_t *widget, fbtk_callback_info *cbi) { int vscroll; int vpos; nsfb_bbox_t bbox; nsfb_bbox_t rect; + fbtk_widget_t *root = get_root_widget(widget); fbtk_get_bbox(widget, &bbox); @@ -82,85 +83,46 @@ vscroll_redraw(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) } static int -vscrollu_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, void *pw) +vscrollu_click(fbtk_widget_t *widget, fbtk_callback_info *cbi) { - fbtk_widget_t *scrollw = pw; - fbtk_callback_info cbi; - - if (event->type != NSFB_EVENT_KEY_DOWN) - return 0; - - cbi.type = FBTK_CBIT_SCROLLY; - cbi.context = scrollw->callback_context; - cbi.y = -1; - - return (scrollw->callback)(scrollw, &cbi); + int ret = 0; + if (cbi->event->type == NSFB_EVENT_KEY_DOWN) + ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLY, -1); + return ret; } static int -vscrolld_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, void *pw) +vscrolld_click(fbtk_widget_t *widget, fbtk_callback_info *cbi) { - fbtk_widget_t *scrollw = pw; - fbtk_callback_info cbi; - - if (event->type != NSFB_EVENT_KEY_DOWN) - return 0; - - cbi.type = FBTK_CBIT_SCROLLY; - cbi.context = scrollw->callback_context; - cbi.y = 1; - - return (scrollw->callback)(scrollw, &cbi); + int ret = 0; + if (cbi->event->type == NSFB_EVENT_KEY_DOWN) + ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLY, 1); + return ret; } static int -vscrollarea_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, void *pw) +vscrollarea_click(fbtk_widget_t *widget, fbtk_callback_info *cbi) { - fbtk_widget_t *scrollw = pw; - fbtk_callback_info cbi; int vscroll; int vpos; + int ret = 0; - if (event->type != NSFB_EVENT_KEY_DOWN) + if (cbi->event->type != NSFB_EVENT_KEY_DOWN) return 0; vscroll = ((widget->height - 4) * widget->u.scroll.pct) / 100 ; vpos = ((widget->height - 4) * widget->u.scroll.pos) / 100 ; - cbi.type = FBTK_CBIT_SCROLLY; - cbi.context = scrollw->callback_context; - if (y < vpos) { + if (cbi->y < vpos) { /* above bar */ - cbi.y = -1; - return (scrollw->callback)(scrollw, &cbi); - } else if (y > (vpos+vscroll)) { + ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLY, -1); + } else if (cbi->y > (vpos + vscroll)) { /* below bar */ - cbi.y = 1; - return (scrollw->callback)(scrollw, &cbi); + ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLY, 1); } - return 0; + return ret; } -static int -set_ptr_hand_move(fbtk_widget_t *widget, - int x, int y, - void *pw) -{ - fbtk_widget_t *root = get_root_widget(widget); - nsfb_cursor_set(root->u.root.fb, - (nsfb_colour_t *)hand_image.pixdata, - hand_image.width, - hand_image.height, - hand_image.width); - - return 0; -} fbtk_widget_t * fbtk_create_vscroll(fbtk_widget_t *window, @@ -180,19 +142,15 @@ fbtk_create_vscroll(fbtk_widget_t *window, neww->fg = fg; neww->bg = bg; - neww->redraw = vscroll_redraw; + fbtk_set_handler(neww, FBTK_CBT_REDRAW, vscroll_redraw, NULL); - neww->click = vscrollarea_click; - neww->clickpw = neww; + fbtk_set_handler(neww, FBTK_CBT_CLICK, vscrollarea_click, neww); - neww->callback = callback; - neww->callback_context = context; + fbtk_set_handler(neww, FBTK_CBT_SCROLLY, callback, context); neww->u.scroll.btnul = fbtk_create_button(window, x + (width - scrollu.width) / 2, y, fg, &scrollu, vscrollu_click, neww); - fbtk_set_handler_move(neww->u.scroll.btnul, set_ptr_hand_move, NULL); neww->u.scroll.btndr = fbtk_create_button(window, x + (width - scrolld.width) / 2, y + height - scrolld.height, fg, &scrolld, vscrolld_click, neww); - fbtk_set_handler_move(neww->u.scroll.btndr, set_ptr_hand_move, NULL); return add_widget_to_window(window, neww); @@ -201,12 +159,13 @@ fbtk_create_vscroll(fbtk_widget_t *window, /* Horizontal scroll widget */ static int -hscroll_redraw(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) +hscroll_redraw(fbtk_widget_t *widget, fbtk_callback_info *cbi) { int hscroll; int hpos; nsfb_bbox_t bbox; nsfb_bbox_t rect; + fbtk_widget_t *root = get_root_widget(widget); fbtk_get_bbox(widget, &bbox); @@ -245,69 +204,44 @@ hscroll_redraw(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) } static int -hscrolll_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, void *pw) +hscrolll_click(fbtk_widget_t *widget, fbtk_callback_info *cbi) { - fbtk_widget_t *scrollw = pw; - fbtk_callback_info cbi; - - if (event->type != NSFB_EVENT_KEY_DOWN) - return 0; - - cbi.type = FBTK_CBIT_SCROLLX; - cbi.context = scrollw->callback_context; - cbi.x = -1; - - return (scrollw->callback)(scrollw, &cbi); + int ret = 0; + if (cbi->event->type == NSFB_EVENT_KEY_DOWN) + ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLX, -1); + return ret; } static int -hscrollr_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, void *pw) +hscrollr_click(fbtk_widget_t *widget, fbtk_callback_info *cbi) { - fbtk_widget_t *scrollw = pw; - fbtk_callback_info cbi; - - if (event->type != NSFB_EVENT_KEY_DOWN) - return 0; - - cbi.type = FBTK_CBIT_SCROLLX; - cbi.context = scrollw->callback_context; - cbi.x = 1; - - return (scrollw->callback)(scrollw, &cbi); + int ret = 0; + if (cbi->event->type == NSFB_EVENT_KEY_DOWN) + ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLX, 1); + return ret; } static int -hscrollarea_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, void *pw) +hscrollarea_click(fbtk_widget_t *widget, fbtk_callback_info *cbi) { - fbtk_widget_t *scrollw = pw; - fbtk_callback_info cbi; int hscroll; int hpos; + int ret; - if (event->type != NSFB_EVENT_KEY_DOWN) + if (cbi->event->type != NSFB_EVENT_KEY_DOWN) return 0; hscroll = ((widget->width - 4) * widget->u.scroll.pct) / 100 ; hpos = ((widget->width - 4) * widget->u.scroll.pos) / 100 ; - cbi.type = FBTK_CBIT_SCROLLX; - cbi.context = scrollw->callback_context; - if (x < hpos) { + if (cbi->x < hpos) { /* above bar */ - cbi.x = -1; - return (scrollw->callback)(scrollw, &cbi); - } else if (x > (hpos + hscroll)) { + ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLX, -1); + } else if (cbi->x > (hpos + hscroll)) { /* below bar */ - cbi.x = 1; - return (scrollw->callback)(scrollw, &cbi); + ret = fbtk_post_callback(cbi->context, FBTK_CBT_SCROLLX, 1); } - return 0; + return ret; } fbtk_widget_t * @@ -328,13 +262,9 @@ fbtk_create_hscroll(fbtk_widget_t *window, neww->fg = fg; neww->bg = bg; - neww->redraw = hscroll_redraw; - - neww->click = hscrollarea_click; - neww->clickpw = neww; - - neww->callback = callback; - neww->callback_context = context; + fbtk_set_handler(neww, FBTK_CBT_REDRAW, hscroll_redraw, NULL); + fbtk_set_handler(neww, FBTK_CBT_CLICK, hscrollarea_click, neww); + fbtk_set_handler(neww, FBTK_CBT_SCROLLX, callback, context); neww->u.scroll.btnul = fbtk_create_button(window, x, y + ((height - scrolll.height) / 2), fg, &scrolll, hscrolll_click, neww); diff --git a/framebuffer/gui.c b/framebuffer/gui.c index 14771ad27..d970fcf78 100644 --- a/framebuffer/gui.c +++ b/framebuffer/gui.c @@ -294,9 +294,9 @@ static void fb_redraw(fbtk_widget_t *widget, } static int -fb_browser_window_redraw(fbtk_widget_t *root, fbtk_widget_t *widget, void *pw) +fb_browser_window_redraw(fbtk_widget_t *widget, fbtk_callback_info *cbi) { - struct gui_window *gw = pw; + struct gui_window *gw = cbi->context; struct browser_widget_s *bwidget; bwidget = fbtk_get_userpw(widget); @@ -498,35 +498,32 @@ void gui_quit(void) /* called back when click in browser window */ static int -fb_browser_window_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, - void *pw) +fb_browser_window_click(fbtk_widget_t *widget, fbtk_callback_info *cbi) { - struct browser_window *bw = pw; + struct browser_window *bw = cbi->context; struct browser_widget_s *bwidget = fbtk_get_userpw(widget); - if (event->type != NSFB_EVENT_KEY_DOWN && - event->type != NSFB_EVENT_KEY_UP) + if (cbi->event->type != NSFB_EVENT_KEY_DOWN && + cbi->event->type != NSFB_EVENT_KEY_UP) return 0; - LOG(("browser window clicked at %d,%d",x,y)); + LOG(("browser window clicked at %d,%d",cbi->x,cbi->y)); - switch (event->type) { + switch (cbi->event->type) { case NSFB_EVENT_KEY_DOWN: - switch (event->value.keycode) { + switch (cbi->event->value.keycode) { case NSFB_KEY_MOUSE_1: browser_window_mouse_click(bw, BROWSER_MOUSE_PRESS_1, - x + bwidget->scrollx, - y + bwidget->scrolly); + cbi->x + bwidget->scrollx, + cbi->y + bwidget->scrolly); break; case NSFB_KEY_MOUSE_3: browser_window_mouse_click(bw, BROWSER_MOUSE_PRESS_2, - x + bwidget->scrollx, - y + bwidget->scrolly); + cbi->x + bwidget->scrollx, + cbi->y + bwidget->scrolly); break; case NSFB_KEY_MOUSE_4: @@ -546,19 +543,19 @@ fb_browser_window_click(fbtk_widget_t *widget, break; case NSFB_EVENT_KEY_UP: - switch (event->value.keycode) { + switch (cbi->event->value.keycode) { case NSFB_KEY_MOUSE_1: browser_window_mouse_click(bw, BROWSER_MOUSE_CLICK_1, - x + bwidget->scrollx, - y + bwidget->scrolly); + cbi->x + bwidget->scrollx, + cbi->y + bwidget->scrolly); break; case NSFB_KEY_MOUSE_3: browser_window_mouse_click(bw, BROWSER_MOUSE_CLICK_2, - x + bwidget->scrollx, - y + bwidget->scrolly); + cbi->x + bwidget->scrollx, + cbi->y + bwidget->scrolly); break; default: @@ -571,42 +568,38 @@ fb_browser_window_click(fbtk_widget_t *widget, break; } - return 0; + return 1; } /* called back when movement in browser window */ static int -fb_browser_window_move(fbtk_widget_t *widget, - int x, int y, - void *pw) +fb_browser_window_move(fbtk_widget_t *widget, fbtk_callback_info *cbi) { - struct browser_window *bw = pw; + struct browser_window *bw = cbi->context; struct browser_widget_s *bwidget = fbtk_get_userpw(widget); browser_window_mouse_track(bw, 0, - x + bwidget->scrollx, - y + bwidget->scrolly); + cbi->x + bwidget->scrollx, + cbi->y + bwidget->scrolly); return 0; } static int -fb_browser_window_input(fbtk_widget_t *widget, - nsfb_event_t *event, - void *pw) +fb_browser_window_input(fbtk_widget_t *widget, fbtk_callback_info *cbi) { - struct gui_window *gw = pw; + struct gui_window *gw = cbi->context; int res = 0; static uint8_t modifier = 0; int ucs4 = -1; - LOG(("got value %d", event->value.keycode)); + LOG(("got value %d", cbi->event->value.keycode)); - switch (event->type) { + switch (cbi->event->type) { case NSFB_EVENT_KEY_DOWN: - switch (event->value.keycode) { + switch (cbi->event->value.keycode) { case NSFB_KEY_PAGEUP: if (browser_window_key_press(gw->bw, KEY_PAGE_UP) == false) @@ -647,7 +640,8 @@ fb_browser_window_input(fbtk_widget_t *widget, break; default: - ucs4 = fbtk_keycode_to_ucs4(event->value.keycode, modifier); + ucs4 = fbtk_keycode_to_ucs4(cbi->event->value.keycode, + modifier); if (ucs4 != -1) res = browser_window_key_press(gw->bw, ucs4); break; @@ -655,7 +649,7 @@ fb_browser_window_input(fbtk_widget_t *widget, break; case NSFB_EVENT_KEY_UP: - switch (event->value.keycode) { + switch (cbi->event->value.keycode) { case NSFB_KEY_RSHIFT: modifier &= ~1; break; @@ -691,71 +685,60 @@ fb_update_back_forward(struct gui_window *gw) /* left icon click routine */ static int -fb_leftarrow_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, void *pw) +fb_leftarrow_click(fbtk_widget_t *widget,fbtk_callback_info *cbi) { - struct gui_window *gw = pw; + struct gui_window *gw = cbi->context; struct browser_window *bw = gw->bw; - if (event->type != NSFB_EVENT_KEY_DOWN) + if (cbi->event->type != NSFB_EVENT_KEY_DOWN) return 0; if (history_back_available(bw->history)) history_back(bw, bw->history); fb_update_back_forward(gw); - return 0; + return 1; } /* right arrow icon click routine */ static int -fb_rightarrow_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, - void *pw) +fb_rightarrow_click(fbtk_widget_t *widget,fbtk_callback_info *cbi) { - struct gui_window *gw =pw; + struct gui_window *gw = cbi->context; struct browser_window *bw = gw->bw; - if (event->type != NSFB_EVENT_KEY_DOWN) + if (cbi->event->type != NSFB_EVENT_KEY_DOWN) return 0; if (history_forward_available(bw->history)) history_forward(bw, bw->history); fb_update_back_forward(gw); - return 0; + return 1; } /* reload icon click routine */ static int -fb_reload_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, - void *pw) +fb_reload_click(fbtk_widget_t *widget,fbtk_callback_info *cbi) { - struct browser_window *bw = pw; + struct browser_window *bw = cbi->context; - if (event->type != NSFB_EVENT_KEY_DOWN) + if (cbi->event->type != NSFB_EVENT_KEY_DOWN) return 0; browser_window_reload(bw, true); - return 0; + return 1; } /* stop icon click routine */ static int -fb_stop_click(fbtk_widget_t *widget, - nsfb_event_t *event, - int x, int y, - void *pw) +fb_stop_click(fbtk_widget_t *widget,fbtk_callback_info *cbi) { - struct browser_window *bw = pw; + struct browser_window *bw = cbi->context; - if (event->type != NSFB_EVENT_KEY_DOWN) + if (cbi->event->type != NSFB_EVENT_KEY_DOWN) return 0; browser_window_stop(bw); @@ -769,11 +752,11 @@ fb_scroll_callback(fbtk_widget_t *widget, fbtk_callback_info *cbi) struct gui_window *gw = cbi->context; switch (cbi->type) { - case FBTK_CBIT_SCROLLY: + case FBTK_CBT_SCROLLY: fb_window_scroll(gw->browser, 0, 100 * cbi->y); break; - case FBTK_CBIT_SCROLLX: + case FBTK_CBT_SCROLLX: fb_window_scroll(gw->browser, 100 * cbi->x, 0); break; @@ -792,32 +775,19 @@ fb_url_enter(void *pw, char *text) } static int -fb_url_move(fbtk_widget_t *widget, - int x, int y, - void *pw) +fb_url_move(fbtk_widget_t *widget, fbtk_callback_info *cbi) { framebuffer_set_cursor(&caret_image); return 0; } static int -set_ptr_default_move(fbtk_widget_t *widget, - int x, int y, - void *pw) +set_ptr_default_move(fbtk_widget_t *widget, fbtk_callback_info *cbi) { framebuffer_set_cursor(&pointer_image); return 0; } -static int -set_ptr_hand_move(fbtk_widget_t *widget, - int x, int y, - void *pw) -{ - framebuffer_set_cursor(&hand_image); - return 0; -} - struct gui_window * gui_create_browser_window(struct browser_window *bw, struct browser_window *clone, @@ -865,14 +835,13 @@ gui_create_browser_window(struct browser_window *bw, widget = fbtk_create_fill(gw->window, 0, 0, 0, toolbar_height, FB_FRAME_COLOUR); - fbtk_set_handler_move(widget, set_ptr_default_move, bw); + fbtk_set_handler(widget, FBTK_CBT_POINTERMOVE, set_ptr_default_move, bw); /* back button */ gw->back = fbtk_create_button(gw->window, xpos, (toolbar_height - left_arrow.height) / 2, FB_FRAME_COLOUR, &left_arrow, fb_leftarrow_click, gw); - fbtk_set_handler_move(gw->back, set_ptr_hand_move, bw); xpos += left_arrow.width + spacing_width; /* forward button */ @@ -880,7 +849,6 @@ gui_create_browser_window(struct browser_window *bw, xpos, (toolbar_height - right_arrow.height) / 2, FB_FRAME_COLOUR, &right_arrow, fb_rightarrow_click, gw); - fbtk_set_handler_move(gw->forward, set_ptr_hand_move, bw); xpos += right_arrow.width + spacing_width; /* reload button */ @@ -888,7 +856,6 @@ gui_create_browser_window(struct browser_window *bw, xpos, (toolbar_height - stop_image.height) / 2, FB_FRAME_COLOUR, &stop_image, fb_stop_click, bw); - fbtk_set_handler_move(widget, set_ptr_hand_move, bw); xpos += stop_image.width + spacing_width; /* reload button */ @@ -896,7 +863,6 @@ gui_create_browser_window(struct browser_window *bw, xpos, (toolbar_height - reload.height) / 2, FB_FRAME_COLOUR, &reload, fb_reload_click, bw); - fbtk_set_handler_move(widget, set_ptr_hand_move, bw); xpos += reload.width + spacing_width; /* url widget */ @@ -909,7 +875,7 @@ gui_create_browser_window(struct browser_window *bw, url_bar_height, FB_COLOUR_WHITE, FB_COLOUR_BLACK, true, fb_url_enter, bw); - fbtk_set_handler_move(gw->url, fb_url_move, bw); + fbtk_set_handler(gw->url, FBTK_CBT_POINTERMOVE, fb_url_move, bw); xpos += fbtk_get_width(gw->window) - xpos - spacing_width - throbber0.width; @@ -928,7 +894,7 @@ gui_create_browser_window(struct browser_window *bw, statusbar_width, furniture_width, FB_FRAME_COLOUR, FB_COLOUR_BLACK, false); - fbtk_set_handler_move(gw->status, set_ptr_default_move, bw); + fbtk_set_handler(gw->status, FBTK_CBT_POINTERMOVE, set_ptr_default_move, bw); /* create horizontal scrollbar */ gw->hscroll = fbtk_create_hscroll(gw->window, @@ -948,7 +914,7 @@ gui_create_browser_window(struct browser_window *bw, furniture_width, furniture_width, FB_FRAME_COLOUR); - fbtk_set_handler_move(widget, set_ptr_default_move, bw); + fbtk_set_handler(widget, FBTK_CBT_POINTERMOVE, set_ptr_default_move, bw); /* create vertical scrollbar */ gw->vscroll = fbtk_create_vscroll(gw->window, @@ -979,10 +945,10 @@ gui_create_browser_window(struct browser_window *bw, gw->browser = fbtk_create_user(gw->window, 0, toolbar_height, -furniture_width, - (furniture_width + toolbar_height), browser_widget); - fbtk_set_handler_click(gw->browser, fb_browser_window_click, bw); - fbtk_set_handler_input(gw->browser, fb_browser_window_input, gw); - fbtk_set_handler_redraw(gw->browser, fb_browser_window_redraw, gw); - fbtk_set_handler_move(gw->browser, fb_browser_window_move, bw); + fbtk_set_handler(gw->browser, FBTK_CBT_REDRAW, fb_browser_window_redraw, gw); + fbtk_set_handler(gw->browser, FBTK_CBT_INPUT, fb_browser_window_input, gw); + fbtk_set_handler(gw->browser, FBTK_CBT_CLICK, fb_browser_window_click, bw); + fbtk_set_handler(gw->browser, FBTK_CBT_POINTERMOVE, fb_browser_window_move, bw); return gw; }