From 76d88df10ff9beb170fdfb8acccf569dd7096fcf Mon Sep 17 00:00:00 2001 From: Ahmed Abouzied Date: Mon, 16 Aug 2021 07:12:37 +0200 Subject: [PATCH] Add increment and decrement controls for numeric display options Signed-off-by: Ahmed Abouzied --- DisplayOptionsPanel.c | 48 ++++++++++++++++++++++++++++++++++++++++--- DisplayOptionsPanel.h | 2 ++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/DisplayOptionsPanel.c b/DisplayOptionsPanel.c index 7e05ae0798..75ff470360 100644 --- a/DisplayOptionsPanel.c +++ b/DisplayOptionsPanel.c @@ -20,15 +20,32 @@ in the source distribution for its full text. #include "ProvideCurses.h" -static const char* const DisplayOptionsFunctions[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", "Done ", NULL}; +static const char* const CheckboxDisplayOptionsFunctions[] = {"Select ", "Done ", NULL}; +static const char* const CheckboxDisplayOptionsKeys[] = {"Enter", "F10", "Esc"}; +static const int CheckboxDisplayOptionsEvents[] = {KEY_ENTER, KEY_F(10), 27}; +static const char* const NumericDisplayOptionsFunctions[] = {"Decrement ", "Increment ", "Done ", NULL}; +static const char* const NumericDisplayOptionsKeys[] = {"-", "+", "F10", "Esc"}; +static const int NumericDisplayOptionsEvents[] = {'-', '+', KEY_F(10), 27}; static void DisplayOptionsPanel_delete(Object* object) { Panel* super = (Panel*) object; DisplayOptionsPanel* this = (DisplayOptionsPanel*) object; + FunctionBar_delete(this->numericFuBar); + FunctionBar_delete(this->checkboxFuBar); Panel_done(super); free(this); } +static void DisplayOptionsPanel_setFunctionBar(DisplayOptionsPanel* this, OptionItem* item) { + Panel* super = (Panel*) this; + if (OptionItem_kind(item) == OPTION_ITEM_NUMBER) { + super->currentBar = this->numericFuBar; + } else { + super->currentBar = this->checkboxFuBar; + } + FunctionBar_draw(super->currentBar); +} + static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) { DisplayOptionsPanel* this = (DisplayOptionsPanel*) super; @@ -36,6 +53,23 @@ static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) { OptionItem* selected = (OptionItem*) Panel_getSelected(super); switch (ch) { + + case KEY_UP: { + int selected_index = Panel_getSelectedIndex(super); + if (selected_index > 1) { + OptionItem* next_to_select = (OptionItem*) Panel_get(super, selected_index - 1); + DisplayOptionsPanel_setFunctionBar(this, next_to_select); + } + break; + } + case KEY_DOWN: { + int selected_index = Panel_getSelectedIndex(super); + if (selected_index < Panel_size(super) - 1) { + OptionItem* next_to_select = (OptionItem*) Panel_get(super, selected_index + 1); + DisplayOptionsPanel_setFunctionBar(this, next_to_select); + } + break; + } case '\n': case '\r': case KEY_ENTER: @@ -90,11 +124,15 @@ const PanelClass DisplayOptionsPanel_class = { DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager* scr) { DisplayOptionsPanel* this = AllocThis(DisplayOptionsPanel); Panel* super = (Panel*) this; - FunctionBar* fuBar = FunctionBar_new(DisplayOptionsFunctions, NULL, NULL); - Panel_init(super, 1, 1, 1, 1, Class(OptionItem), true, fuBar); + FunctionBar* numericFuBar = FunctionBar_new(CheckboxDisplayOptionsFunctions, CheckboxDisplayOptionsKeys, CheckboxDisplayOptionsEvents); + FunctionBar* selectFuBar = FunctionBar_new(NumericDisplayOptionsFunctions, NumericDisplayOptionsKeys, NumericDisplayOptionsEvents); + FunctionBar* emptyFuBar = FunctionBar_new(NULL, NULL, NULL); + Panel_init(super, 1, 1, 1, 1, Class(OptionItem), true, emptyFuBar); this->settings = settings; this->scr = scr; + this->numericFuBar = numericFuBar; + this->checkboxFuBar = selectFuBar; Panel_setHeader(super, "Display options"); Panel_add(super, (Object*) CheckItem_newByRef("Tree view", &(settings->treeView))); @@ -141,5 +179,9 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager* #ifdef HAVE_LIBHWLOC Panel_add(super, (Object*) CheckItem_newByRef("Show topology when selecting affinity by default", &(settings->topologyAffinity))); #endif + + OptionItem* defaultSelected = (OptionItem*) Panel_getSelected(super); + DisplayOptionsPanel_setFunctionBar(this, defaultSelected); + return this; } diff --git a/DisplayOptionsPanel.h b/DisplayOptionsPanel.h index 745f125f3e..517401108e 100644 --- a/DisplayOptionsPanel.h +++ b/DisplayOptionsPanel.h @@ -17,6 +17,8 @@ typedef struct DisplayOptionsPanel_ { Settings* settings; ScreenManager* scr; + FunctionBar *numericFuBar; + FunctionBar *checkboxFuBar; } DisplayOptionsPanel; extern const PanelClass DisplayOptionsPanel_class;