ui/ncurses/widgets: Add widget focus offset query
authorJeremy Kerr <jk@ozlabs.org>
Tue, 19 Nov 2013 03:18:28 +0000 (14:18 +1100)
committerJeremy Kerr <jk@ozlabs.org>
Fri, 22 Nov 2013 02:45:54 +0000 (10:45 +0800)
When we have widgets that are larger than the screen, we'd like to
scroll to the currently-focussed component of that widget.

This change introduces a widget_focus_y function, which returns the y
offset of the field's focus.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
ui/ncurses/nc-widgets.c
ui/ncurses/nc-widgets.h

index d96480e960ab4d54e876a75655f2909777c1e0cd..583e5ed82d9169c6762ff986cd2b599e928d103e 100644 (file)
@@ -85,10 +85,12 @@ struct nc_widget {
        bool    (*process_key)(struct nc_widget *, FORM *, int);
        void    (*set_visible)(struct nc_widget *, bool);
        void    (*move)(struct nc_widget *, int, int);
+       void    (*field_focus)(struct nc_widget *, FIELD *);
        int     focussed_attr;
        int     unfocussed_attr;
        int     height;
        int     width;
+       int     focus_y;
        int     x;
        int     y;
 };
@@ -414,6 +416,19 @@ static void select_move(struct nc_widget *widget, int y, int x)
                field_move(select->options[i].field, y + i, x);
 }
 
+static void select_field_focus(struct nc_widget *widget, FIELD *field)
+{
+       struct nc_widget_select *select = to_select(widget);
+       int i;
+
+       for (i = 0; i < select->n_options; i++) {
+               if (field != select->options[i].field)
+                       continue;
+               widget->focus_y = i;
+               return;
+       }
+}
+
 static int select_destructor(void *ptr)
 {
        struct nc_widget_select *select = ptr;
@@ -438,6 +453,7 @@ struct nc_widget_select *widget_new_select(struct nc_widgetset *set,
        select->widget.process_key = select_process_key;
        select->widget.set_visible = select_set_visible;
        select->widget.move = select_move;
+       select->widget.field_focus = select_field_focus;
        select->widget.focussed_attr = A_REVERSE;
        select->widget.unfocussed_attr = A_NORMAL;
        select->top = y;
@@ -622,6 +638,8 @@ bool widgetset_process_key(struct nc_widgetset *set, int key)
                field = current_field(set->form);
                widget = field_userptr(field);
                widget_focus_change(widget, field, true);
+               if (widget->field_focus)
+                       widget->field_focus(widget, field);
                if (set->widget_focus)
                        set->widget_focus(widget, set->widget_focus_arg);
                return true;
@@ -755,3 +773,8 @@ int widget_y(struct nc_widget *widget)
        return widget->y;
 }
 
+int widget_focus_y(struct nc_widget *widget)
+{
+       return widget->focus_y;
+}
+
index 53f158334fdac1cde76ae666e5669bb32bae97de..d598d5aa440dc4c87a5cebac8b7fd470c814ddbd 100644 (file)
@@ -59,6 +59,7 @@ int widget_height(struct nc_widget *widget);
 int widget_width(struct nc_widget *widget);
 int widget_y(struct nc_widget *widget);
 int widget_x(struct nc_widget *widget);
+int widget_focus_y(struct nc_widget *widget);
 
 /* widgetset API */
 typedef void (*widget_focus_cb)(struct nc_widget *widget, void *arg);