2 * Copyright (C) 2013 IBM Corporation
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.
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.
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
23 #include <talloc/talloc.h>
24 #include <types/types.h>
29 #include "nc-config.h"
30 #include "nc-widgets.h"
34 struct config_screen {
37 struct nc_widgetset *widgetset;
39 void (*on_exit)(struct cui *);
45 struct nc_widget_checkbox *autoboot_f;
46 struct nc_widget_label *autoboot_l;
47 struct nc_widget_textbox *timeout_f;
48 struct nc_widget_label *timeout_l;
50 struct nc_widget_label *network_l;
51 struct nc_widget_label *iface_l;
52 struct nc_widget_select *iface_f;
54 struct nc_widget_button *ok_b;
55 struct nc_widget_button *cancel_b;
59 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
61 struct config_screen *config_screen;
63 assert(scr->sig == pb_config_screen_sig);
64 config_screen = (struct config_screen *)
65 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
66 assert(config_screen->scr.sig == pb_config_screen_sig);
70 static void config_screen_process_key(struct nc_scr *scr, int key)
72 struct config_screen *screen = config_screen_from_scr(scr);
75 handled = widgetset_process_key(screen->widgetset, key);
77 screen->on_exit(screen->cui);
79 wrefresh(screen->scr.main_ncw);
82 static void config_screen_resize(struct nc_scr *scr)
84 struct config_screen *screen = config_screen_from_scr(scr);
88 static int config_screen_post(struct nc_scr *scr)
90 struct config_screen *screen = config_screen_from_scr(scr);
91 widgetset_post(screen->widgetset);
92 nc_scr_frame_draw(scr);
93 wrefresh(scr->main_ncw);
97 static int config_screen_unpost(struct nc_scr *scr)
99 struct config_screen *screen = config_screen_from_scr(scr);
100 widgetset_unpost(screen->widgetset);
104 struct nc_scr *config_screen_scr(struct config_screen *screen)
109 static void ok_click(void *arg)
111 struct config_screen *screen = arg;
112 /* todo: save config */
113 screen->on_exit(screen->cui);
116 static void cancel_click(void *arg)
118 struct config_screen *screen = arg;
122 static int layout_pair(struct config_screen *screen, int y,
123 struct nc_widget_label *label,
124 struct nc_widget *field)
126 struct nc_widget *label_w = widget_label_base(label);
127 widget_move(label_w, y, screen->label_x);
128 widget_move(field, y, screen->field_x);
129 return max(widget_height(label_w), widget_height(field));
132 static void config_screen_layout_widgets(struct config_screen *screen)
138 y += layout_pair(screen, y, screen->widgets.autoboot_l,
139 widget_checkbox_base(screen->widgets.autoboot_f));
141 y += layout_pair(screen, y, screen->widgets.timeout_l,
142 widget_textbox_base(screen->widgets.timeout_f));
146 widget_move(widget_button_base(screen->widgets.ok_b),
148 widget_move(widget_button_base(screen->widgets.cancel_b),
149 y, screen->field_x + 10);
152 static void config_screen_setup_widgets(struct config_screen *screen,
153 const struct config *config,
154 const struct system_info *sysinfo)
156 struct nc_widgetset *set = screen->widgetset;
161 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
164 screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
165 screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
166 config->autoboot_enabled);
168 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
169 screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
170 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
172 screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
174 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
175 cancel_click, screen);
179 struct config_screen *config_screen_init(struct cui *cui,
180 const struct config *config,
181 const struct system_info *sysinfo,
182 void (*on_exit)(struct cui *))
184 struct config_screen *screen;
186 screen = talloc_zero(cui, struct config_screen);
187 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
188 cui, config_screen_process_key,
189 config_screen_post, config_screen_unpost,
190 config_screen_resize);
193 screen->on_exit = on_exit;
195 screen->field_x = 16;
197 screen->scr.frame.ltitle = talloc_strdup(screen,
198 "Petitboot System Configuration");
199 screen->scr.frame.rtitle = NULL;
200 screen->scr.frame.help = talloc_strdup(screen,
201 "tab=next, shift+tab=previous");
202 nc_scr_frame_draw(&screen->scr);
204 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
205 screen->scr.sub_ncw);
206 config_screen_setup_widgets(screen, config, sysinfo);
207 config_screen_layout_widgets(screen);
209 wrefresh(screen->scr.main_ncw);
210 scrollok(screen->scr.sub_ncw, true);