Merge branch 'master' into gsoc-ifdef-cleanup
[kugel-rb.git] / apps / plugins / lib / display_text.c
blob5d13fc678d8d6112455f89333fce03e772974f85
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Johannes Schwarz
11 * based on Will Robertson code in superdom
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "plugin.h"
23 #include "display_text.h"
25 #ifdef HAVE_LCD_CHARCELLS
26 #define MARGIN 0
27 #else
28 #define MARGIN 5
29 #endif
31 static bool wait_key_press(void)
33 int button;
34 do {
35 button = rb->button_get(true);
36 if ( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
37 return true;
38 } while( ( button == BUTTON_NONE )
39 || ( button & (BUTTON_REL|BUTTON_REPEAT) ) );
40 return false;
43 bool display_text(unsigned short words, char** text, struct style_text* style,
44 struct viewport* vp_text, bool wait_key)
46 #ifdef HAVE_LCD_BITMAP
47 int prev_drawmode;
48 #endif
49 #ifdef HAVE_LCD_COLOR
50 int standard_fgcolor;
51 #endif
52 int space_w, width, height;
53 unsigned short x , y;
54 unsigned short vp_width = LCD_WIDTH;
55 unsigned short vp_height = LCD_HEIGHT;
56 unsigned short i = 0, style_index = 0;
57 if (vp_text != NULL) {
58 vp_width = vp_text->width;
59 vp_height = vp_text->height;
61 rb->screens[SCREEN_MAIN]->set_viewport(vp_text);
62 #ifdef HAVE_LCD_BITMAP
63 prev_drawmode = rb->lcd_get_drawmode();
64 rb->lcd_set_drawmode(DRMODE_SOLID);
65 #endif
66 #ifdef HAVE_LCD_COLOR
67 standard_fgcolor = rb->lcd_get_foreground();
68 #endif
69 rb->screens[SCREEN_MAIN]->clear_viewport();
70 x = MARGIN;
71 y = MARGIN;
72 rb->button_clear_queue();
73 rb->lcd_getstringsize(" ", &space_w, &height);
74 for (i = 0; i < words; i++) {
75 rb->lcd_getstringsize(text[i], &width, NULL);
76 /* skip to next line if the word is an empty string */
77 if (rb->strcmp(text[i], "")==0) {
78 x = MARGIN;
79 y = y + height;
80 continue;
81 /* .. or if the current one can't fit the word */
82 } else if (x + width > vp_width - MARGIN) {
83 x = MARGIN;
84 y = y + height;
86 /* display the remaining text by button click or exit */
87 if (y + height > vp_height - MARGIN) {
88 y = MARGIN;
89 rb->screens[SCREEN_MAIN]->update_viewport();
90 if (wait_key_press())
91 return true;
92 rb->screens[SCREEN_MAIN]->clear_viewport();
94 /* no text formatting available */
95 if (style==NULL || style[style_index].index != i) {
96 rb->lcd_putsxy(x, y, text[i]);
97 } else {
98 /* set align */
99 if (style[style_index].flags&TEXT_CENTER) {
100 x = (vp_width-width)/2;
102 /* set font color */
103 #ifdef HAVE_LCD_COLOR
104 switch (style[style_index].flags&TEXT_COLOR_MASK) {
105 case C_RED:
106 rb->lcd_set_foreground(LCD_RGBPACK(255,0,0));
107 break;
108 case C_YELLOW:
109 rb->lcd_set_foreground(LCD_RGBPACK(255,255,0));
110 break;
111 case C_GREEN:
112 rb->lcd_set_foreground(LCD_RGBPACK(0,192,0));
113 break;
114 case C_BLUE:
115 rb->lcd_set_foreground(LCD_RGBPACK(0,0,255));
116 break;
117 case C_ORANGE:
118 rb->lcd_set_foreground(LCD_RGBPACK(255,192,0));
119 break;
120 case STANDARD:
121 default:
122 rb->lcd_set_foreground(standard_fgcolor);
123 break;
125 #endif
126 rb->lcd_putsxy(x, y, text[i]);
127 /* underline the word */
128 #ifdef HAVE_LCD_BITMAP
129 if (style[style_index].flags&TEXT_UNDERLINE) {
130 rb->lcd_hline(x, x+width, y+height-1);
132 #endif
133 #ifdef HAVE_LCD_COLOR
134 rb->lcd_set_foreground(standard_fgcolor);
135 #endif
136 style_index++;
138 x += width + space_w;
140 rb->screens[SCREEN_MAIN]->update_viewport();
141 #ifdef HAVE_LCD_BITMAP
142 rb->lcd_set_drawmode(prev_drawmode);
143 #endif
144 if (wait_key)
146 if (wait_key_press())
147 return true;
149 return false;