Fix oops. Too late now.
[maemo-rb.git] / apps / gui / yesno.c
bloba2abae33aed8efe71a521eb47b1c269aebdc3892
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 button;
151 int result=-1;
152 bool result_displayed;
153 struct gui_yesno yn[NB_SCREENS];
154 struct viewport vp[NB_SCREENS];
155 long talked_tick = 0;
156 FOR_NB_SCREENS(i)
158 yn[i].main_message=main_message;
159 yn[i].result_message[YESNO_YES]=yes_message;
160 yn[i].result_message[YESNO_NO]=no_message;
161 yn[i].display=&screens[i];
162 yn[i].vp = &vp[i];
163 #ifdef HAVE_LCD_CHARCELLS
164 /* Quick fix. Viewports should really be enabled proper for charcell */
165 viewport_set_defaults(yn[i].vp, i);
166 #else
167 viewportmanager_theme_enable(i, true, yn[i].vp);
168 #endif
169 screens[i].stop_scroll();
170 gui_yesno_draw(&(yn[i]));
172 /* make sure to eat any extranous keypresses */
173 while (get_action(CONTEXT_STD+99, TIMEOUT_NOBLOCK))
174 action_wait_for_release();
175 while (result==-1)
177 /* Repeat the question every 5secs (more or less) */
178 if (global_settings.talk_menu
179 && (talked_tick==0 || TIME_AFTER(current_tick, talked_tick+HZ*5)))
181 talked_tick = current_tick;
182 talk_text_message(main_message, false);
184 button = get_action(CONTEXT_YESNOSCREEN, HZ*5);
185 switch (button)
187 #ifdef HAVE_TOUCHSCREEN
188 case ACTION_TOUCHSCREEN:
190 short int x, y;
191 if (action_get_touchscreen_press_in_vp(&x, &y, yn[0].vp) == BUTTON_TOUCHSCREEN)
193 if (y > yn[0].vp->height/2)
195 if (x <= yn[0].vp->width/2)
196 result = YESNO_YES;
197 else
198 result = YESNO_NO;
202 break;
203 #endif
204 case ACTION_YESNO_ACCEPT:
205 result=YESNO_YES;
206 break;
207 case ACTION_NONE:
208 case SYS_CHARGER_DISCONNECTED:
209 case SYS_BATTERY_UPDATE:
210 /* ignore some SYS events that can happen */
211 continue;
212 default:
213 if(default_event_handler(button) == SYS_USB_CONNECTED)
214 return(YESNO_USB);
215 result = YESNO_NO;
219 FOR_NB_SCREENS(i)
220 result_displayed=gui_yesno_draw_result(&(yn[i]), result);
222 if (global_settings.talk_menu)
224 talk_text_message((result == YESNO_YES) ? yes_message
225 : no_message, false);
226 talk_force_enqueue_next();
228 if(result_displayed)
229 sleep(HZ);
231 FOR_NB_SCREENS(i)
233 screens[i].scroll_stop(yn[i].vp);
234 viewportmanager_theme_undo(i, true);
236 return(result);
240 /* Function to manipulate all yesno dialogues.
241 This function needs the output text as an argument. */
242 bool yesno_pop(const char* text)
244 const char *lines[]={text};
245 const struct text_message message={lines, 1};
246 bool ret = (gui_syncyesno_run(&message,NULL,NULL)== YESNO_YES);
247 FOR_NB_SCREENS(i)
248 screens[i].clear_viewport();
249 return ret;