Bump version numbers for 3.13
[maemo-rb.git] / apps / gui / yesno.c
blob74e3ad05a73b61a74e000fcfdbccb2304bf2b068
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]));
173 /* make sure to eat any extranous keypresses */
174 action_wait_for_release();
176 while (result==-1)
178 /* Repeat the question every 5secs (more or less) */
179 if (global_settings.talk_menu
180 && (talked_tick==0 || TIME_AFTER(current_tick, talked_tick+HZ*5)))
182 talked_tick = current_tick;
183 talk_text_message(main_message, false);
185 button = get_action(CONTEXT_YESNOSCREEN, HZ*5);
186 switch (button)
188 #ifdef HAVE_TOUCHSCREEN
189 case ACTION_TOUCHSCREEN:
191 short int x, y;
192 if (action_get_touchscreen_press_in_vp(&x, &y, yn[0].vp) == BUTTON_REL)
194 if (y > yn[0].vp->height/2)
196 if (x <= yn[0].vp->width/2)
197 result = YESNO_YES;
198 else
199 result = YESNO_NO;
203 break;
204 #endif
205 case ACTION_YESNO_ACCEPT:
206 result=YESNO_YES;
207 break;
208 case ACTION_NONE:
209 case SYS_CHARGER_DISCONNECTED:
210 case SYS_BATTERY_UPDATE:
211 /* ignore some SYS events that can happen */
212 continue;
213 default:
214 if(default_event_handler(button) == SYS_USB_CONNECTED)
215 return(YESNO_USB);
216 result = YESNO_NO;
220 FOR_NB_SCREENS(i)
221 result_displayed=gui_yesno_draw_result(&(yn[i]), result);
223 if (global_settings.talk_menu)
225 talk_text_message((result == YESNO_YES) ? yes_message
226 : no_message, false);
227 talk_force_enqueue_next();
229 if(result_displayed)
230 sleep(HZ);
232 FOR_NB_SCREENS(i)
234 screens[i].scroll_stop(yn[i].vp);
235 viewportmanager_theme_undo(i, true);
238 return(result);
242 /* Function to manipulate all yesno dialogues.
243 This function needs the output text as an argument. */
244 bool yesno_pop(const char* text)
246 const char *lines[]={text};
247 const struct text_message message={lines, 1};
248 bool ret = (gui_syncyesno_run(&message,NULL,NULL)== YESNO_YES);
249 FOR_NB_SCREENS(i)
250 screens[i].clear_viewport();
251 return ret;