ui/ncurses: Adjust position of add-url field to fit translated label
[petitboot] / ui / ncurses / nc-lang.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <talloc/talloc.h>
26 #include <types/types.h>
27 #include <log/log.h>
28 #include <i18n/i18n.h>
29 #include <pb-config/pb-config.h>
30
31 #include "nc-cui.h"
32 #include "nc-lang.h"
33 #include "nc-widgets.h"
34
35 #define N_FIELDS        5
36
37 static struct lang {
38         const char      *name;
39         const wchar_t   *label;
40 } languages[] = {
41         { "de_DE.utf8", L"Deutsch"},
42         { "en_US.utf8", L"English"},
43         { "es_ES.utf8", L"Espa\u00f1ol"},
44         { "fr_FR.utf8", L"Fran\u00e7ais"},
45         { "it_IT.utf8", L"Italiano"},
46         { "ja_JP.utf8", L"\u65e5\u672c\u8a9e"},
47         { "ko_KR.utf8", L"\ud55c\uad6d\uc5b4"},
48         { "pt_BR.utf8", L"Portugu\u00eas/Brasil"},
49         { "ru_RU.utf8", L"\u0420\u0443\u0441\u0441\u043a\u0438\u0439"},
50         { "zh_CN.utf8", L"\u7b80\u4f53\u4e2d\u6587"},
51         { "zh_TW.utf8", L"\u7e41\u9ad4\u4e2d\u6587"},
52 };
53
54 struct lang_screen {
55         struct nc_scr           scr;
56         struct cui              *cui;
57         struct nc_widgetset     *widgetset;
58         WINDOW                  *pad;
59
60         bool                    exit;
61         void                    (*on_exit)(struct cui *);
62
63         int                     scroll_y;
64
65         int                     label_x;
66         int                     field_x;
67
68         struct {
69                 struct nc_widget_select         *lang_f;
70                 struct nc_widget_label          *lang_l;
71
72                 struct nc_widget_label          *safe_mode;
73                 struct nc_widget_button         *ok_b;
74                 struct nc_widget_button         *cancel_b;
75         } widgets;
76 };
77
78 static struct lang_screen *lang_screen_from_scr(struct nc_scr *scr)
79 {
80         struct lang_screen *lang_screen;
81
82         assert(scr->sig == pb_lang_screen_sig);
83         lang_screen = (struct lang_screen *)
84                 ((char *)scr - (size_t)&((struct lang_screen *)0)->scr);
85         assert(lang_screen->scr.sig == pb_lang_screen_sig);
86         return lang_screen;
87 }
88
89 static void pad_refresh(struct lang_screen *screen)
90 {
91         int y, x, rows, cols;
92
93         getmaxyx(screen->scr.sub_ncw, rows, cols);
94         getbegyx(screen->scr.sub_ncw, y, x);
95
96         prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
97 }
98
99 static void lang_screen_process_key(struct nc_scr *scr, int key)
100 {
101         struct lang_screen *screen = lang_screen_from_scr(scr);
102         bool handled;
103
104         handled = widgetset_process_key(screen->widgetset, key);
105
106         if (!handled) {
107                 switch (key) {
108                 case 'x':
109                 case 27: /* esc */
110                         screen->exit = true;
111                         break;
112                 }
113         }
114
115         if (screen->exit) {
116                 screen->on_exit(screen->cui);
117
118         } else if (handled) {
119                 pad_refresh(screen);
120         }
121 }
122
123 static void lang_screen_resize(struct nc_scr *scr)
124 {
125         struct lang_screen *screen = lang_screen_from_scr(scr);
126         (void)screen;
127 }
128
129 static int lang_screen_post(struct nc_scr *scr)
130 {
131         struct lang_screen *screen = lang_screen_from_scr(scr);
132         widgetset_post(screen->widgetset);
133         nc_scr_frame_draw(scr);
134         redrawwin(scr->main_ncw);
135         wrefresh(screen->scr.main_ncw);
136         pad_refresh(screen);
137         return 0;
138 }
139
140 static int lang_screen_unpost(struct nc_scr *scr)
141 {
142         struct lang_screen *screen = lang_screen_from_scr(scr);
143         widgetset_unpost(screen->widgetset);
144         return 0;
145 }
146
147 struct nc_scr *lang_screen_scr(struct lang_screen *screen)
148 {
149         return &screen->scr;
150 }
151
152 static int lang_process_form(struct lang_screen *screen)
153 {
154         struct config *config;
155         struct lang *lang;
156         int idx, rc;
157
158         config = config_copy(screen, screen->cui->config);
159
160         idx = widget_select_get_value(screen->widgets.lang_f);
161
162         /* Option -1 ("Unknown") can only be populated from the current
163          * language, so there's no change here */
164         if (idx == -1)
165                 return 0;
166
167         lang = &languages[idx];
168
169         if (config->lang && !strcmp(lang->name, config->lang))
170                 return 0;
171
172         config->lang = talloc_strdup(screen, lang->name);
173
174         config->safe_mode = false;
175         rc = cui_send_config(screen->cui, config);
176         talloc_free(config);
177
178         if (rc)
179                 pb_log("cui_send_config failed!\n");
180         else
181                 pb_debug("config sent!\n");
182
183         return 0;
184 }
185
186 static void ok_click(void *arg)
187 {
188         struct lang_screen *screen = arg;
189         if (lang_process_form(screen))
190                 /* errors are written to the status line, so we'll need
191                  * to refresh */
192                 wrefresh(screen->scr.main_ncw);
193         else
194                 screen->exit = true;
195 }
196
197 static void cancel_click(void *arg)
198 {
199         struct lang_screen *screen = arg;
200         screen->exit = true;
201 }
202
203 static int layout_pair(struct lang_screen *screen, int y,
204                 struct nc_widget_label *label,
205                 struct nc_widget *field)
206 {
207         struct nc_widget *label_w = widget_label_base(label);
208         widget_move(label_w, y, screen->label_x);
209         widget_move(field, y, screen->field_x);
210         return max(widget_height(label_w), widget_height(field));
211 }
212
213 static void lang_screen_layout_widgets(struct lang_screen *screen)
214 {
215         int y;
216
217         y = 1;
218
219         y += layout_pair(screen, y, screen->widgets.lang_l,
220                         widget_select_base(screen->widgets.lang_f));
221
222         y += 1;
223
224         if (screen->cui->config->safe_mode) {
225                 widget_move(widget_label_base(screen->widgets.safe_mode),
226                         y, screen->field_x);
227                 y += 1;
228         }
229
230         widget_move(widget_button_base(screen->widgets.ok_b),
231                         y, screen->field_x);
232         widget_move(widget_button_base(screen->widgets.cancel_b),
233                         y, screen->field_x + 10);
234 }
235
236 static void lang_screen_setup_empty(struct lang_screen *screen)
237 {
238         widget_new_label(screen->widgetset, 2, screen->field_x,
239                         _("Waiting for configuration data..."));
240         screen->widgets.cancel_b = widget_new_button(screen->widgetset,
241                         4, screen->field_x, 9, _("Cancel"),
242                         cancel_click, screen);
243 }
244
245
246 static void lang_screen_setup_widgets(struct lang_screen *screen,
247                 const struct config *config)
248 {
249         struct nc_widgetset *set = screen->widgetset;
250         unsigned int i;
251         bool found;
252
253         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
254                         == N_FIELDS);
255
256         screen->widgets.lang_l = widget_new_label(set, 0, 0, _("Language"));
257         screen->widgets.lang_f = widget_new_select(set, 0, 0, 50);
258
259         found = false;
260
261         for (i = 0; i < ARRAY_SIZE(languages); i++) {
262                 struct lang *lang = &languages[i];
263                 bool selected;
264                 char *label;
265                 int len;
266
267                 len = wcstombs(NULL, lang->label, 0);
268                 assert(len >= 0);
269                 label = talloc_array(screen, char, len + 1);
270                 wcstombs(label, lang->label, len + 1);
271
272                 selected = config->lang && !strcmp(lang->name, config->lang);
273                 found |= selected;
274
275                 widget_select_add_option(screen->widgets.lang_f, i,
276                                         label, selected);
277         }
278
279         if (!found && config->lang) {
280                 char *label = talloc_asprintf(screen,
281                                 _("Unknown language '%s'"), config->lang);
282                 widget_select_add_option(screen->widgets.lang_f, -1,
283                                         label, true);
284         }
285
286         if (config->safe_mode)
287                 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
288                          _("Selecting 'OK' will exit safe mode"));
289
290         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, _("OK"),
291                         ok_click, screen);
292         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 9, _("Cancel"),
293                         cancel_click, screen);
294 }
295
296 static void lang_screen_widget_focus(struct nc_widget *widget, void *arg)
297 {
298         struct lang_screen *screen = arg;
299         int w_y, s_max;
300
301         w_y = widget_y(widget) + widget_focus_y(widget);
302         s_max = getmaxy(screen->scr.sub_ncw) - 1;
303
304         if (w_y < screen->scroll_y)
305                 screen->scroll_y = w_y;
306
307         else if (w_y + screen->scroll_y + 1 > s_max)
308                 screen->scroll_y = 1 + w_y - s_max;
309
310         else
311                 return;
312
313         pad_refresh(screen);
314 }
315
316 static void lang_screen_draw(struct lang_screen *screen,
317                 const struct config *config)
318 {
319         bool repost = false;
320         int height;
321
322         height = ARRAY_SIZE(languages) + 4;
323         if (!screen->pad || getmaxy(screen->pad) < height) {
324                 if (screen->pad)
325                         delwin(screen->pad);
326                 screen->pad = newpad(height, COLS);
327         }
328
329         if (screen->widgetset) {
330                 widgetset_unpost(screen->widgetset);
331                 talloc_free(screen->widgetset);
332                 repost = true;
333         }
334
335         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
336                         screen->pad);
337         widgetset_set_widget_focus(screen->widgetset,
338                         lang_screen_widget_focus, screen);
339
340         if (!config) {
341                 lang_screen_setup_empty(screen);
342         } else {
343                 lang_screen_setup_widgets(screen, config);
344                 lang_screen_layout_widgets(screen);
345         }
346
347         if (repost)
348                 widgetset_post(screen->widgetset);
349 }
350
351 void lang_screen_update(struct lang_screen *screen,
352                 const struct config *config)
353 {
354         lang_screen_draw(screen, config);
355         pad_refresh(screen);
356 }
357
358 static int lang_screen_destroy(void *arg)
359 {
360         struct lang_screen *screen = arg;
361         if (screen->pad)
362                 delwin(screen->pad);
363         return 0;
364 }
365
366 struct lang_screen *lang_screen_init(struct cui *cui,
367                 const struct config *config,
368                 void (*on_exit)(struct cui *))
369 {
370         struct lang_screen *screen;
371
372         screen = talloc_zero(cui, struct lang_screen);
373         talloc_set_destructor(screen, lang_screen_destroy);
374         nc_scr_init(&screen->scr, pb_lang_screen_sig, 0,
375                         cui, lang_screen_process_key,
376                         lang_screen_post, lang_screen_unpost,
377                         lang_screen_resize);
378
379         screen->cui = cui;
380         screen->on_exit = on_exit;
381         screen->label_x = 2;
382         screen->field_x = 17;
383
384         screen->scr.frame.ltitle = talloc_strdup(screen,
385                         _("Petitboot Language Selection"));
386         screen->scr.frame.rtitle = NULL;
387         screen->scr.frame.help = talloc_strdup(screen,
388                         _("tab=next, shift+tab=previous, x=exit, h=help"));
389         nc_scr_frame_draw(&screen->scr);
390
391         scrollok(screen->scr.sub_ncw, true);
392
393         lang_screen_draw(screen, config);
394
395         return screen;
396 }