Re-factor code for the plugins main menu item, enabling better icons in that menu.
[maemo-rb.git] / apps / gui / yesno.c
blobdfce5411475443cb6c6d88e3dfb806bdb7805a87
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Kevin Ferrare
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
22 #include "yesno.h"
23 #include "system.h"
24 #include "kernel.h"
25 #include "misc.h"
26 #include "lang.h"
27 #include "action.h"
28 #include "talk.h"
29 #include "settings.h"
30 #include "viewport.h"
33 struct gui_yesno
35 const struct text_message * main_message;
36 const struct text_message * result_message[2];
38 struct viewport *vp;
39 struct screen * display;
42 static void talk_text_message(const struct text_message * message, bool enqueue)
44 int line;
45 if(message)
47 for(line=0; line<message->nb_lines; line++)
49 long id = P2ID((unsigned char *)message->message_lines[line]);
50 if(id>=0)
52 talk_id(id, enqueue);
53 enqueue = true;
59 static int put_message(struct screen *display,
60 const struct text_message * message,
61 int start, int max_y)
63 int i;
64 for(i=0; i<message->nb_lines && i+start<max_y; i++)
66 display->puts_scroll(0, i+start,
67 P2STR((unsigned char *)message->message_lines[i]));
69 return i;
73 * Draws the yesno
74 * - yn : the yesno structure
76 static void gui_yesno_draw(struct gui_yesno * yn)
78 struct screen * display=yn->display;
79 struct viewport *vp = yn->vp;
80 int nb_lines, vp_lines, line_shift=0;
82 display->set_viewport(vp);
83 display->clear_viewport();
84 nb_lines = yn->main_message->nb_lines;
85 vp_lines = viewport_get_nb_lines(vp);
87 if(nb_lines+3< vp_lines)
88 line_shift=1;
90 line_shift += put_message(display, yn->main_message,
91 line_shift, vp_lines);
92 #ifdef HAVE_TOUCHSCREEN
93 if (display->screen_type == SCREEN_MAIN)
95 int w,h;
96 int rect_w = vp->width/2, rect_h = vp->height/2;
97 int old_pattern = vp->fg_pattern;
98 vp->fg_pattern = LCD_RGBPACK(0,255,0);
99 display->drawrect(0, rect_h, rect_w, rect_h);
100 display->getstringsize(str(LANG_SET_BOOL_YES), &w, &h);
101 display->putsxy((rect_w-w)/2, rect_h+(rect_h-h)/2, str(LANG_SET_BOOL_YES));
102 vp->fg_pattern = LCD_RGBPACK(255,0,0);
103 display->drawrect(rect_w, rect_h, rect_w, rect_h);
104 display->getstringsize(str(LANG_SET_BOOL_NO), &w, &h);
105 display->putsxy(rect_w + (rect_w-w)/2, rect_h+(rect_h-h)/2, str(LANG_SET_BOOL_NO));
106 vp->fg_pattern = old_pattern;
108 #else
109 /* Space remaining for yes / no text ? */
110 if(line_shift+2 <= vp_lines)
112 if(line_shift+3 <= vp_lines)
113 line_shift++;
114 display->puts(0, line_shift, str(LANG_CONFIRM_WITH_BUTTON));
115 #ifdef HAVE_LCD_BITMAP
116 display->puts(0, line_shift+1, str(LANG_CANCEL_WITH_ANY));
117 #endif
119 #endif
120 display->update_viewport();
121 display->set_viewport(NULL);
125 * Draws the yesno result
126 * - yn : the yesno structure
127 * - result : the result tha must be displayed :
128 * YESNO_NO if no
129 * YESNO_YES if yes
131 static bool gui_yesno_draw_result(struct gui_yesno * yn, enum yesno_res result)
133 const struct text_message * message=yn->result_message[result];
134 struct viewport *vp = yn->vp;
135 struct screen * display=yn->display;
136 if(message==NULL)
137 return false;
138 display->set_viewport(vp);
139 display->clear_viewport();
140 put_message(yn->display, message, 0, viewport_get_nb_lines(vp));
141 display->update_viewport();
142 display->set_viewport(NULL);
143 return(true);
146 enum yesno_res gui_syncyesno_run(const struct text_message * main_message,
147 const struct text_message * yes_message,
148 const struct text_message * no_message)
150 int i;
151 int button;
152 int result=-1;
153 bool result_displayed;
154 struct gui_yesno yn[NB_SCREENS];
155 struct viewport vp[NB_SCREENS];
156 long talked_tick = 0;
157 FOR_NB_SCREENS(i)
159 yn[i].main_message=main_message;
160 yn[i].result_message[YESNO_YES]=yes_message;
161 yn[i].result_message[YESNO_NO]=no_message;
162 yn[i].display=&screens[i];
163 yn[i].vp = &vp[i];
164 #ifdef HAVE_LCD_CHARCELLS
165 /* Quick fix. Viewports should really be enabled proper for charcell */
166 viewport_set_defaults(yn[i].vp, i);
167 #else
168 viewportmanager_theme_enable(i, true, yn[i].vp);
169 #endif
170 screens[i].stop_scroll();
171 gui_yesno_draw(&(yn[i]));
173 while (result==-1)
175 /* Repeat the question every 5secs (more or less) */
176 if (global_settings.talk_menu
177 && (talked_tick==0 || TIME_AFTER(current_tick, talked_tick+HZ*5)))
179 talked_tick = current_tick;
180 talk_text_message(main_message, false);
182 button = get_action(CONTEXT_YESNOSCREEN, HZ*5);
183 switch (button)
185 #ifdef HAVE_TOUCHSCREEN
186 case ACTION_TOUCHSCREEN:
188 short int x, y;
189 if (action_get_touchscreen_press_in_vp(&x, &y, yn[0].vp) == BUTTON_TOUCHSCREEN)
191 if (y > yn[0].vp->height/2)
193 if (x <= yn[0].vp->width/2)
194 result = YESNO_YES;
195 else
196 result = YESNO_NO;
200 break;
201 #endif
202 case ACTION_YESNO_ACCEPT:
203 result=YESNO_YES;
204 break;
205 case ACTION_NONE:
206 case SYS_CHARGER_DISCONNECTED:
207 /* ignore some SYS events that can happen */
208 continue;
209 default:
210 if(default_event_handler(button) == SYS_USB_CONNECTED)
211 return(YESNO_USB);
212 result = YESNO_NO;
216 FOR_NB_SCREENS(i)
217 result_displayed=gui_yesno_draw_result(&(yn[i]), result);
219 if (global_settings.talk_menu)
221 talk_text_message((result == YESNO_YES) ? yes_message
222 : no_message, false);
223 talk_force_enqueue_next();
225 if(result_displayed)
226 sleep(HZ);
228 FOR_NB_SCREENS(i)
230 screens[i].scroll_stop(yn[i].vp);
231 viewportmanager_theme_undo(i, true);
233 return(result);