amsinfo: do not assume library blocks always have the same size
[kugel-rb.git] / apps / gui / yesno.c
bloba153a43fec91fe879a76eb64f006a67c7a51b063
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);
93 /* Space remaining for yes / no text ? */
94 if(line_shift+2 <= vp_lines)
96 if(line_shift+3 <= vp_lines)
97 line_shift++;
98 display->puts(0, line_shift, str(LANG_CONFIRM_WITH_BUTTON));
99 #ifdef HAVE_LCD_BITMAP
100 display->puts(0, line_shift+1, str(LANG_CANCEL_WITH_ANY));
101 #endif
103 display->update_viewport();
104 display->set_viewport(NULL);
108 * Draws the yesno result
109 * - yn : the yesno structure
110 * - result : the result tha must be displayed :
111 * YESNO_NO if no
112 * YESNO_YES if yes
114 static bool gui_yesno_draw_result(struct gui_yesno * yn, enum yesno_res result)
116 const struct text_message * message=yn->result_message[result];
117 struct viewport *vp = yn->vp;
118 struct screen * display=yn->display;
119 if(message==NULL)
120 return false;
121 display->set_viewport(vp);
122 display->clear_viewport();
123 put_message(yn->display, message, 0, viewport_get_nb_lines(vp));
124 display->update_viewport();
125 display->set_viewport(NULL);
126 return(true);
129 enum yesno_res gui_syncyesno_run(const struct text_message * main_message,
130 const struct text_message * yes_message,
131 const struct text_message * no_message)
133 int i;
134 int button;
135 int result=-1;
136 bool result_displayed;
137 struct gui_yesno yn[NB_SCREENS];
138 struct viewport vp[NB_SCREENS];
139 long talked_tick = 0;
140 FOR_NB_SCREENS(i)
142 yn[i].main_message=main_message;
143 yn[i].result_message[YESNO_YES]=yes_message;
144 yn[i].result_message[YESNO_NO]=no_message;
145 yn[i].display=&screens[i];
146 yn[i].vp = &vp[i];
147 viewportmanager_theme_enable(i, true, yn[i].vp);
148 screens[i].stop_scroll();
149 gui_yesno_draw(&(yn[i]));
151 while (result==-1)
153 /* Repeat the question every 5secs (more or less) */
154 if (global_settings.talk_menu
155 && (talked_tick==0 || TIME_AFTER(current_tick, talked_tick+HZ*5)))
157 talked_tick = current_tick;
158 talk_text_message(main_message, false);
160 button = get_action(CONTEXT_YESNOSCREEN, HZ*5);
161 switch (button)
163 case ACTION_YESNO_ACCEPT:
164 result=YESNO_YES;
165 break;
166 case ACTION_NONE:
167 case SYS_CHARGER_DISCONNECTED:
168 /* ignore some SYS events that can happen */
169 continue;
170 default:
171 if(default_event_handler(button) == SYS_USB_CONNECTED)
172 return(YESNO_USB);
173 result = YESNO_NO;
177 FOR_NB_SCREENS(i)
178 result_displayed=gui_yesno_draw_result(&(yn[i]), result);
180 if (global_settings.talk_menu)
182 talk_text_message((result == YESNO_YES) ? yes_message
183 : no_message, false);
184 talk_force_enqueue_next();
186 if(result_displayed)
187 sleep(HZ);
189 FOR_NB_SCREENS(i)
191 screens[i].scroll_stop(yn[i].vp);
192 viewportmanager_theme_undo(i, true);
194 return(result);