Index: apps/plugins/lib/help.c =================================================================== --- apps/plugins/lib/help.c (revision 0) +++ apps/plugins/lib/help.c (revision 0) @@ -0,0 +1,145 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2004-2007 Antoine Cellerier + * Copyright (C) 2008 William Poetra Yoga Hadisoeseno + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include "plugin.h" +#include "help.h" + +#include + +static const struct plugin_api *rb; + +/* Initialize the context and format the help text to fit on the screen */ +void help_init(struct help_context *ctx, const struct plugin_api *api, int scrollbar_width, char *help_text, int button_up, int button_down, int button_quit) +{ + int w_space, h_space; + + /* Initialize the context */ + + rb = api; + ctx->scrollbar = (scrollbar_width > 0); + ctx->scrollbar_width = ctx->scrollbar ? scrollbar_width : 0; + ctx->help_text = help_text; + ctx->button_up = button_up; + ctx->button_down = button_down; + ctx->button_quit = button_quit; + + rb->lcd_getstringsize(" ", &w_space, &h_space); + ctx->line_height = h_space; + ctx->display_lines = LCD_HEIGHT / h_space; + + /* Format the help text */ + + int lines; + char *c, *p, s; + int w_line, w_word; + + c = ctx->help_text; + w_line = 0; + while (*c) { + if (*c == '\n') { + c++; + w_line = 0; + continue; + } + + p = c + 1; + while (*p && !isspace(*p)) + p++; + + s = *p; + *p = '\0'; + api->lcd_getstringsize(c, &w_word, NULL); + *p = s; + if (w_line == 0) { + w_line = w_word; + } else if (w_line + w_word > LCD_WIDTH) { + *c = '\n'; + w_line = w_word; + } else { + w_line += w_word; + } + c = p; + } + + /* count the number of lines */ + + c = ctx->help_text; + lines = 1; + while (*c) { + if (*c == '\n') + lines++; + c++; + } + ctx->total_lines = lines; + + if (ctx->display_lines > ctx->total_lines) + ctx->display_lines = ctx->total_lines; +} + +/* Show the help contents */ +enum help_ret help_show(const struct help_context *ctx) +{ + int firstline, i; + char *c; + int button; + int scrollbar_shift = (ctx->scrollbar) ? ctx->scrollbar_width + 1 : 0; + + firstline = 0; + while (true) { + c = ctx->help_text; + for (i = 0; i < firstline; ++i) + c = rb->strchr(c, '\n') + 1; + + rb->lcd_clear_display(); + for (i = 0; i < ctx->display_lines; ++i) { + char s, *p; + p = rb->strchr(c, '\n'); + if (p) { + s = *p; + *p = '\0'; + rb->lcd_putsxy(scrollbar_shift, i * ctx->line_height, c); + *p = s; + } else { + rb->lcd_putsxy(scrollbar_shift, i * ctx->line_height, c); + } + c = p + 1; + } + if (ctx->scrollbar) + rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN], 0, 0, ctx->scrollbar_width, LCD_HEIGHT, ctx->total_lines, firstline, firstline + ctx->display_lines, VERTICAL); + rb->lcd_update(); + + button = rb->button_get(true); + if ((button&~BUTTON_REPEAT) == ctx->button_up) { + if (firstline > 0) + firstline--; + } else if ((button&~BUTTON_REPEAT) == ctx->button_down) { + if (firstline + ctx->display_lines < ctx->total_lines) + firstline++; + } else if (button == ctx->button_quit) { + return HELP_QUIT; + } else { + if(rb->default_event_handler(button) == SYS_USB_CONNECTED) + return HELP_USB; + } + } +} + Index: apps/plugins/lib/help.h =================================================================== --- apps/plugins/lib/help.h (revision 0) +++ apps/plugins/lib/help.h (revision 0) @@ -0,0 +1,49 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2004-2007 Antoine Cellerier + * Copyright (C) 2008 William Poetra Yoga Hadisoeseno + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef _LIB_HELP_H +#define _LIB_HELP_H + +#include "plugin.h" + +struct help_context { + // whether to show the scrollbar and how wide + bool scrollbar; + int scrollbar_width; + // this is just a pointer to a user-supplied static buffer + char *help_text; + // buttons for scrolling up and down, and quitting + int button_up; + int button_down; + int button_quit; + // height of one line + int line_height; + // number of lines displayed and the total number of lines + int display_lines; + int total_lines; +}; + +enum help_ret { HELP_QUIT, HELP_USB }; + +void help_init(struct help_context *, const struct plugin_api *api, int scrollbar_width, char *help_text, int button_up, int button_down, int button_quit); +enum help_ret help_show(const struct help_context *); + +#endif /* _LIB_HELP_H */