]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-boot-editor.c
ui/ncurses: Split boot editor layout
[petitboot] / ui / ncurses / nc-boot-editor.c
index c7c2ed8e3723594b4a2471b965e5128dd8eb1c26..f9f689af8a042b8bd679258225911e08ce25e663 100644 (file)
 #include "nc-boot-editor.h"
 #include "nc-widgets.h"
 
+struct boot_editor {
+       struct nc_scr           scr;
+       struct cui              *cui;
+       void                    *data;
+       struct pmenu_item       *item;
+       void                    (*on_exit)(struct cui *cui,
+                                       struct pmenu_item *item,
+                                       struct pb_boot_data *bd);
+
+       int                     label_x;
+       int                     field_x;
+
+       struct nc_widgetset     *widgetset;
+       struct {
+               struct nc_widget_label          *image_l;
+               struct nc_widget_textbox        *image_f;
+               struct nc_widget_label          *initrd_l;
+               struct nc_widget_textbox        *initrd_f;
+               struct nc_widget_label          *dtb_l;
+               struct nc_widget_textbox        *dtb_f;
+               struct nc_widget_label          *args_l;
+               struct nc_widget_textbox        *args_f;
+               struct nc_widget_button         *ok_b;
+               struct nc_widget_button         *cancel_b;
+       } widgets;
+};
+
 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
 {
        struct boot_editor *boot_editor;
@@ -65,6 +92,11 @@ static int boot_editor_unpost(struct nc_scr *scr)
        return 0;
 }
 
+struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
+{
+       return &boot_editor->scr;
+}
+
 static void boot_editor_resize(struct nc_scr *scr)
 {
        /* FIXME: forms can't be resized, need to recreate here */
@@ -118,7 +150,7 @@ static void boot_editor_process_key(struct nc_scr *scr, int key)
        switch (key) {
        case 'x':
        case 27: /* ESC */
-               boot_editor->on_exit(boot_editor, boot_editor_cancel, NULL);
+               boot_editor->on_exit(boot_editor->cui, NULL, NULL);
                nc_flush_keys();
        }
 }
@@ -140,38 +172,77 @@ static void ok_click(void *arg)
        struct pb_boot_data *bd;
 
        bd = boot_editor_prepare_data(boot_editor);
-       boot_editor->on_exit(boot_editor, boot_editor_update, bd);
+       boot_editor->on_exit(boot_editor->cui, boot_editor->item, bd);
 }
 
 static void cancel_click(void *arg)
 {
        struct boot_editor *boot_editor = arg;
-       boot_editor->on_exit(boot_editor, boot_editor_cancel, NULL);
+       boot_editor->on_exit(boot_editor->cui, NULL, NULL);
+}
+
+static int layout_pair(struct boot_editor *boot_editor, int y,
+               struct nc_widget_label *label,
+               struct nc_widget_textbox *field)
+{
+       struct nc_widget *label_w = widget_label_base(label);
+       struct nc_widget *field_w = widget_textbox_base(field);
+       widget_move(label_w, y, boot_editor->label_x);
+       widget_move(field_w, y, boot_editor->field_x);
+       return max(widget_height(label_w), widget_height(field_w));
 }
 
-struct boot_editor *boot_editor_init(struct pmenu *menu,
-               const struct pb_boot_data *bd,
-               void (*on_exit)(struct boot_editor *,
-                               enum boot_editor_result,
-                               struct pb_boot_data *))
+static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
+{
+       int y = 1;
+
+       y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
+                                        boot_editor->widgets.image_f);
+
+       y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
+                                        boot_editor->widgets.initrd_f);
+
+       y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
+                                        boot_editor->widgets.dtb_f);
+
+       y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
+                                        boot_editor->widgets.args_f);
+
+
+       y++;
+       widget_move(widget_button_base(boot_editor->widgets.ok_b), y, 9);
+       widget_move(widget_button_base(boot_editor->widgets.cancel_b), y, 19);
+}
+
+struct boot_editor *boot_editor_init(struct cui *cui,
+               struct pmenu_item *item,
+               const struct system_info *sysinfo,
+               void (*on_exit)(struct cui *cui,
+                               struct pmenu_item *item,
+                               struct pb_boot_data *bd))
 {
-       int y, field_size, label_x = 1, field_x = 9;
        char *image, *initrd, *dtb, *args;
        struct boot_editor *boot_editor;
        struct nc_widgetset *set;
+       int field_size;
 
-       assert(on_exit);
+       (void)sysinfo;
 
-       boot_editor = talloc_zero(menu, struct boot_editor);
+       boot_editor = talloc_zero(cui, struct boot_editor);
 
        if (!boot_editor)
                return NULL;
 
        talloc_set_destructor(boot_editor, boot_editor_destructor);
-       boot_editor->original_pmenu = menu;
+       boot_editor->cui = cui;
+       boot_editor->item = item;
+       boot_editor->on_exit = on_exit;
+
+       boot_editor->label_x = 1;
+       boot_editor->field_x = 9;
 
        nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
-                       menu, boot_editor_process_key,
+                       cui, boot_editor_process_key,
                boot_editor_post, boot_editor_unpost, boot_editor_resize);
 
        boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
@@ -180,9 +251,8 @@ struct boot_editor *boot_editor_init(struct pmenu *menu,
        boot_editor->scr.frame.help = talloc_strdup(boot_editor,
                        "Enter=accept");
 
-       boot_editor->on_exit = on_exit;
-
-       if (bd) {
+       if (item) {
+               struct pb_boot_data *bd = cod_from_item(item)->bd;
                image = bd->image;
                initrd = bd->initrd;
                dtb = bd->dtb;
@@ -191,43 +261,34 @@ struct boot_editor *boot_editor_init(struct pmenu *menu,
                image = initrd = dtb = args = "";
        }
 
-       y = 0;
-       field_size = COLS - 1 - field_x;
+       field_size = COLS - 1 - boot_editor->field_x;
 
        boot_editor->widgetset = set = widgetset_create(boot_editor,
                        boot_editor->scr.main_ncw,
                        boot_editor->scr.sub_ncw);
 
-       boot_editor->widgets.image_l = widget_new_label(set, y,
-                                       label_x, "image:");
-       boot_editor->widgets.image_f = widget_new_textbox(set, y,
-                                       field_x, field_size, image);
+       boot_editor->widgets.image_l = widget_new_label(set, 0, 0, "image:");
+       boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
+                                               field_size, image);
 
-       y++;
-       boot_editor->widgets.initrd_l = widget_new_label(set, y,
-                                       label_x, "initrd:");
-       boot_editor->widgets.initrd_f = widget_new_textbox(set, y,
-                                       field_x, field_size, initrd);
+       boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0, "initrd:");
+       boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
+                                               field_size, initrd);
 
-       y++;
-       boot_editor->widgets.dtb_l = widget_new_label(set, y,
-                                       label_x, "dtb:");
-       boot_editor->widgets.dtb_f = widget_new_textbox(set, y,
-                                       field_x, field_size, dtb);
+       boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0, "dtb:");
+       boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
+                                               field_size, dtb);
 
-       y++;
-       boot_editor->widgets.args_l = widget_new_label(set, y,
-                                       label_x, "args:");
-       boot_editor->widgets.args_f = widget_new_textbox(set, y,
-                                       field_x, field_size, args);
+       boot_editor->widgets.args_l = widget_new_label(set, 0, 0, "args:");
+       boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
+                                       field_size, args);
 
-       y++;
-       y++;
-       boot_editor->widgets.ok_b = widget_new_button(set, y,
-                                       9, 6, "OK", ok_click, boot_editor);
-       boot_editor->widgets.cancel_b = widget_new_button(set, y,
-                                       19, 6, "Cancel", cancel_click,
-                                       boot_editor);
+       boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 6,
+                                       "OK", ok_click, boot_editor);
+       boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 6,
+                                       "Cancel", cancel_click, boot_editor);
+
+       boot_editor_layout_widgets(boot_editor);
 
        return boot_editor;
 }