First commit:
[kugel-rb.git] / apps / gui / yesno.c
blob891e73809b81f12d97449c043a656114a0ebea22
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 enum yesno_res gui_syncyesno_run(struct text_message * main_message,
105 struct text_message * yes_message,
106 struct text_message * no_message)
108 int i;
109 unsigned button;
110 int result=-1;
111 bool result_displayed;
112 struct gui_yesno yn[NB_SCREENS];
113 long talked_tick = 0;
114 FOR_NB_SCREENS(i)
116 gui_yesno_init(&(yn[i]), main_message, yes_message, no_message);
117 gui_yesno_set_display(&(yn[i]), &(screens[i]));
118 gui_yesno_draw(&(yn[i]));
120 while (result==-1)
122 /* Repeat the question every 5secs (more or less) */
123 if (global_settings.talk_menu
124 && (talked_tick==0 || TIME_AFTER(current_tick, talked_tick+HZ*5)))
126 talked_tick = current_tick;
127 talk_text_message(main_message, false);
129 button = get_action(CONTEXT_YESNOSCREEN, HZ*5);
130 switch (button)
132 case ACTION_YESNO_ACCEPT:
133 result=YESNO_YES;
134 break;
135 case ACTION_NONE:
136 case SYS_CHARGER_DISCONNECTED:
137 /* ignore some SYS events that can happen */
138 continue;
139 default:
140 if(default_event_handler(button) == SYS_USB_CONNECTED)
141 return(YESNO_USB);
142 result = YESNO_NO;
146 FOR_NB_SCREENS(i)
147 result_displayed=gui_yesno_draw_result(&(yn[i]), result);
149 if (global_settings.talk_menu)
151 talk_text_message((result == YESNO_YES) ? yes_message
152 : no_message, false);
153 talk_force_enqueue_next();
155 if(result_displayed)
156 sleep(HZ);
157 return(result);