Fix wrong file type.
[Rockbox.git] / apps / gui / yesno.c
blob7c8afb3245c0655c2016fa5346327c1edb6a0d8c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id $
10 * Copyright (C) 2005 by Kevin Ferrare
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "yesno.h"
21 #include "system.h"
22 #include "kernel.h"
23 #include "misc.h"
24 #include "lang.h"
25 #include "action.h"
26 #include "talk.h"
29 * Initializes the yesno asker
30 * - yn : the yesno structure
31 * - main_message : the question the user has to answer
32 * - yes_message : message displayed if answer is 'yes'
33 * - no_message : message displayed if answer is 'no'
35 static void gui_yesno_init(struct gui_yesno * yn,
36 struct text_message * main_message,
37 struct text_message * yes_message,
38 struct text_message * no_message)
40 yn->main_message=main_message;
41 yn->result_message[YESNO_YES]=yes_message;
42 yn->result_message[YESNO_NO]=no_message;
43 yn->display=0;
47 * Attach the yesno to a screen
48 * - yn : the yesno structure
49 * - display : the screen to attach
51 static void gui_yesno_set_display(struct gui_yesno * yn,
52 struct screen * display)
54 yn->display=display;
58 * Draws the yesno
59 * - yn : the yesno structure
61 static void gui_yesno_draw(struct gui_yesno * yn)
63 struct screen * display=yn->display;
64 int nb_lines, line_shift=0;
66 gui_textarea_clear(display);
67 nb_lines=yn->main_message->nb_lines;
69 if(nb_lines+3<display->nb_lines)
70 line_shift=1;
71 nb_lines=gui_textarea_put_message(display, yn->main_message, line_shift);
73 /* Space remaining for yes / no text ? */
74 if(nb_lines+line_shift+2<=display->nb_lines)
76 if(nb_lines+line_shift+3<=display->nb_lines)
77 nb_lines++;
78 display->puts(0, nb_lines+line_shift, str(LANG_CONFIRM_WITH_BUTTON));
79 #ifdef HAVE_LCD_BITMAP
80 display->puts(0, nb_lines+line_shift+1, str(LANG_CANCEL_WITH_ANY));
81 #endif
83 gui_textarea_update(display);
87 * Draws the yesno result
88 * - yn : the yesno structure
89 * - result : the result tha must be displayed :
90 * YESNO_NO if no
91 * YESNO_YES if yes
93 static bool gui_yesno_draw_result(struct gui_yesno * yn, enum yesno_res result)
95 struct text_message * message=yn->result_message[result];
96 if(message==NULL)
97 return false;
98 gui_textarea_put_message(yn->display, message, 0);
99 return(true);
102 #include "debug.h"
104 /* Processes a text_message whose lines may be virtual pointers
105 representing language / voicefont IDs (see settings.h). Copies out
106 the IDs to the ids array, which is of length maxlen, and replaces
107 the pointers in the text_message with the actual language strings.
108 The ids array is terminated with the TALK_FINAL_ID sentinel
109 element. */
110 static void extract_talk_ids(struct text_message *m, long *ids, int maxlen)
112 int line, i=0;
113 if(m)
114 for(line=0; line<m->nb_lines; line++) {
115 long id = P2ID((unsigned char *)m->message_lines[line]);
116 if(id>=0 && i<maxlen-1)
117 ids[i++] = id;
118 m->message_lines[line] = (char *)P2STR((unsigned char *)m->message_lines[line]);
120 ids[i] = TALK_FINAL_ID;
123 enum yesno_res gui_syncyesno_run(struct text_message * main_message,
124 struct text_message * yes_message,
125 struct text_message * no_message)
127 int i;
128 unsigned button;
129 int result=-1;
130 bool result_displayed;
131 struct gui_yesno yn[NB_SCREENS];
132 long voice_ids[5];
133 long talked_tick = 0;
134 /* The text messages may contain virtual pointers to IDs (see
135 settings.h) instead of plain strings. Copy the IDs out so we
136 can speak them, and unwrap the actual language strings. */
137 extract_talk_ids(main_message, voice_ids,
138 sizeof(voice_ids)/sizeof(voice_ids[0]));
139 FOR_NB_SCREENS(i)
141 gui_yesno_init(&(yn[i]), main_message, yes_message, no_message);
142 gui_yesno_set_display(&(yn[i]), &(screens[i]));
143 gui_yesno_draw(&(yn[i]));
145 while (result==-1)
147 /* Repeat the question every 5secs (more or less) */
148 if (talk_menus_enabled()
149 && (talked_tick==0 || TIME_AFTER(current_tick, talked_tick+HZ*5)))
151 talked_tick = current_tick;
152 talk_idarray(voice_ids, false);
154 button = get_action(CONTEXT_YESNOSCREEN, HZ*5);
155 switch (button)
157 case ACTION_YESNO_ACCEPT:
158 result=YESNO_YES;
159 break;
160 case ACTION_NONE:
161 case SYS_CHARGER_DISCONNECTED:
162 /* ignore some SYS events that can happen */
163 continue;
164 default:
165 if(default_event_handler(button) == SYS_USB_CONNECTED)
166 return(YESNO_USB);
167 result = YESNO_NO;
171 /* extract_talk_ids also converts ID to STR */
172 extract_talk_ids((result == YESNO_YES) ? yes_message : no_message,
173 voice_ids, sizeof(voice_ids)/sizeof(voice_ids[0]));
175 FOR_NB_SCREENS(i)
176 result_displayed=gui_yesno_draw_result(&(yn[i]), result);
178 if (talk_menus_enabled())
180 talk_idarray(voice_ids, false);
181 talk_force_enqueue_next();
183 if(result_displayed)
184 sleep(HZ);
185 return(result);