1 /*(GPL)
2 ------------------------------------------------------------
3 Kobo Deluxe - An enhanced SDL port of XKobo
4 ------------------------------------------------------------
5 * Copyright (C) 2001, 2002, 2007 David Olofson
6 * Copyright (C) 2005 Erik Auerswald
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #define DBG(x)
24
25 #include "kobolog.h"
26 #include "config.h"
27 #include "form.h"
28 #include "kobo.h"
29 #include "audio.h"
30
kobo_form_t()31 kobo_form_t::kobo_form_t()
32 {
33 ypos = 0;
34 current_list = NULL;
35 _big = 0;
36 xoffs = 0.5;
37 halign = ALIGN_DEFAULT;
38 }
39
40
~kobo_form_t()41 kobo_form_t::~kobo_form_t()
42 {
43 }
44
45
next()46 void kobo_form_t::next()
47 {
48 sound.ui_move();
49 ct_form_t::next();
50 }
51
52
prev()53 void kobo_form_t::prev()
54 {
55 sound.ui_move();
56 ct_form_t::prev();
57 }
58
59
change(int delta)60 /* virtual */ void kobo_form_t::change(int delta)
61 {
62 ct_form_t::change(delta);
63 if(selected()->user)
64 {
65 int *val = (int *)selected()->user;
66 *val = selected()->value();
67 DBG(log_printf(D2LOG, "Changed to %d\n", selected()->value());)
68 }
69 }
70
71
build()72 /* virtual */void kobo_form_t::build()
73 {
74 }
75
76
init_widget(ct_widget_t * w)77 void kobo_form_t::init_widget(ct_widget_t *w)
78 {
79 current_widget = w;
80 w->init(engine);
81 switch(_big)
82 {
83 case 0:
84 w->place(x(), y()+ypos, width(), LINE_H);
85 w->font(B_NORMAL_FONT);
86 ypos += LINE_H;
87 break;
88 case 1:
89 w->place(x(), y()+ypos, width(), BIG_LINE_H);
90 w->font(B_BIG_FONT);
91 ypos += BIG_LINE_H;
92 break;
93 case 2:
94 w->place(x(), y()+ypos, width(), BIG_LINE_H);
95 w->font(B_MEDIUM_FONT);
96 ypos += BIG_LINE_H;
97 break;
98 }
99 w->halign(halign);
100 add(w);
101 }
102
103
big()104 void kobo_form_t::big()
105 {
106 _big = 1;
107 }
108
109
medium()110 void kobo_form_t::medium()
111 {
112 _big = 2;
113 }
114
115
small()116 void kobo_form_t::small()
117 {
118 _big = 0;
119 }
120
121
begin()122 void kobo_form_t::begin()
123 {
124 clean();
125 ypos = 15;
126 current_list = NULL;
127 small();
128 }
129
130
label(const char * cap)131 void kobo_form_t::label(const char *cap)
132 {
133 ct_label_t *w = new ct_label_t(cap);
134 init_widget(w);
135 }
136
137
yesno(const char * cap,int * var,int tag)138 void kobo_form_t::yesno(const char *cap, int *var, int tag)
139 {
140 current_list = new ct_list_t(cap);
141 current_list->offset(xoffs, 0);
142 current_list->user = var;
143 current_list->tag = tag;
144 current_list->add("Yes", 1);
145 current_list->add("No", 0);
146 init_widget(current_list);
147 }
148
149
onoff(const char * cap,int * var,int tag)150 void kobo_form_t::onoff(const char *cap, int *var, int tag)
151 {
152 current_list = new ct_list_t(cap);
153 current_list->offset(xoffs, 0);
154 current_list->user = var;
155 current_list->tag = tag;
156 current_list->add("On", 1);
157 current_list->add("Off", 0);
158 init_widget(current_list);
159 }
160
161
spin(const char * cap,int * var,int min,int max,const char * unit,int tag)162 void kobo_form_t::spin(const char *cap, int *var, int min, int max,
163 const char *unit, int tag)
164 {
165 ct_spin_t *w = new ct_spin_t(cap, min, max, unit);
166 w->offset(xoffs, 0);
167 w->user = var;
168 w->tag = tag;
169 init_widget(w);
170 }
171
172
list(const char * cap,int * var,int tag)173 void kobo_form_t::list(const char *cap, int *var, int tag)
174 {
175 ct_list_t *w = new ct_list_t(cap);
176 w->offset(xoffs, 0);
177 w->user = var;
178 w->tag = tag;
179 current_list = w;
180 init_widget(w);
181 }
182
183
item(const char * cap,int value,int ind)184 void kobo_form_t::item(const char *cap, int value, int ind)
185 {
186 if(current_list)
187 {
188 ct_item_t *i = new ct_item_t(cap, value);
189 i->index(ind);
190 current_list->add(i);
191 }
192 else
193 log_printf(ELOG, "kobo_form_t::form_item(): No list!\n");
194 }
195
196
perc_list(int first,int last,int step)197 void kobo_form_t::perc_list(int first, int last, int step)
198 {
199 char buf[50];
200 for(int i = first; i <= last; i += step)
201 {
202 snprintf(buf, sizeof(buf), "%d%%", i);
203 item(buf, i);
204 }
205 }
206
207
enum_list(int first,int last)208 void kobo_form_t::enum_list(int first, int last)
209 {
210 char buf[50];
211 for(int i = first; i <= last; i++)
212 {
213 snprintf(buf, sizeof(buf), "%d", i);
214 item(buf, i);
215 }
216 }
217
218
button(const char * cap,int tag)219 void kobo_form_t::button(const char *cap, int tag)
220 {
221 ct_widget_t *w = new ct_button_t(cap);
222 w->offset(xoffs, 0);
223 w->tag = tag;
224 init_widget(w);
225 }
226
227
data(config_parser_t * _d)228 void kobo_form_t::data(config_parser_t *_d)
229 {
230 _data = _d;
231 }
232
233
editor(int handle,int tag)234 void kobo_form_t::editor(int handle, int tag)
235 {
236 if(!_data)
237 return;
238
239 switch(_data->type(handle))
240 {
241 default:
242 log_printf(ELOG, "kobo_form_t: Unknown data type!\n");
243 break;
244 case CFG_BOOL:
245 onoff(_data->description(handle), (int *)NULL, tag);
246 current_widget->user2 = handle;
247 break;
248 case CFG_INT:
249 case CFG_FLOAT:
250 spin(_data->description(handle), (int *)NULL,
251 -100000, 100000, "", tag);
252 current_widget->user2 = handle;
253 break;
254 case CFG_STRING:
255 log_printf(ELOG, "kobo_form_t: String editor not yet implemented!\n");
256 break;
257 }
258 }
259
260
editor(const char * name,int tag)261 void kobo_form_t::editor(const char *name, int tag)
262 {
263 if(!_data)
264 return;
265
266 int h = _data->find(name);
267 if(h < 0)
268 return;
269 editor(h, tag);
270 }
271
272
space(int lines)273 void kobo_form_t::space(int lines)
274 {
275 if(_big)
276 ypos += BIG_SPACE_SIZE * lines;
277 else
278 ypos += SPACE_SIZE * lines;
279 }
280
281
end()282 void kobo_form_t::end()
283 {
284 }
285
286
build_all()287 void kobo_form_t::build_all()
288 {
289 xoffs = 0.5;
290 begin();
291 build();
292 end();
293
294 /*
295 * Initialize all widgets from prefs and/or config_parser.
296 */
297 ct_widget_t *w = widgets;
298 while(w)
299 {
300 if(w->user)
301 {
302 int *val = (int *)w->user;
303 if(val)
304 {
305 w->value(*val);
306 log_printf(D3LOG, "Initialized to %d from prefs\n",
307 w->value());
308 }
309 else if(_data)
310 {
311 w->value(_data->get_i(w->user2));
312 log_printf(D3LOG, "Initialized to %d from config_parser\n",
313 w->value());
314 }
315 }
316 w = w->next;
317 if(w == widgets)
318 break; //Done!
319 }
320 }
321
322