From b63d0d04db903701a09d5b7674cdca925835c17f Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sun, 28 Jan 2018 17:06:53 -0200 Subject: Screens: Fix "New Screen" option --- ScreensPanel.c | 22 +++++++++++++++------- ScreensPanel.h | 2 +- Settings.c | 9 +++++---- Settings.h | 2 ++ 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/ScreensPanel.c b/ScreensPanel.c index 19ac96bd..1fe6283f 100644 --- a/ScreensPanel.c +++ b/ScreensPanel.c @@ -53,9 +53,9 @@ ObjectClass ScreenListItem_class = { .compare = ListItem_compare }; -ScreenListItem* ScreenListItem_new(const char* value, int key, ScreenSettings* ss) { +ScreenListItem* ScreenListItem_new(const char* value, ScreenSettings* ss) { ScreenListItem* this = AllocThis(ScreenListItem); - ListItem_init((ListItem*)this, value, key); + ListItem_init((ListItem*)this, value, 0); this->ss = ss; return this; } @@ -150,6 +150,17 @@ static void rebuildSettingsArray(Panel* super) { } } +static void addNewScreen(Panel* super) { + ScreensPanel* const this = (ScreensPanel*) super; + + char* name = "New"; + ScreenSettings* ss = Settings_newScreen(this->settings, name, "PID Command"); + ScreenListItem* item = ScreenListItem_new(name, ss); + int idx = Panel_getSelectedIndex(super); + Panel_insert(super, idx + 1, (Object*) item); + Panel_setSelected(super, idx + 1); +} + static HandlerResult ScreensPanel_eventHandlerNormal(Panel* super, int ch) { ScreensPanel* const this = (ScreensPanel*) super; @@ -190,10 +201,7 @@ static HandlerResult ScreensPanel_eventHandlerNormal(Panel* super, int ch) { case KEY_F(5): case KEY_CTRL('N'): { - ListItem* item = ListItem_new("", 0); - int idx = Panel_getSelectedIndex(super); - Panel_insert(super, idx + 1, (Object*) item); - Panel_setSelected(super, idx + 1); + addNewScreen(super); startRenaming(super); shouldRebuildArray = true; result = HANDLED; @@ -300,7 +308,7 @@ ScreensPanel* ScreensPanel_new(Settings* settings) { for (unsigned int i = 0; i < settings->nScreens; i++) { ScreenSettings* ss = settings->screens[i]; char* name = ss->name; - Panel_add(super, (Object*) ScreenListItem_new(name, i, ss)); + Panel_add(super, (Object*) ScreenListItem_new(name, ss)); } return this; } diff --git a/ScreensPanel.h b/ScreensPanel.h index f1c80647..944adb36 100644 --- a/ScreensPanel.h +++ b/ScreensPanel.h @@ -40,7 +40,7 @@ typedef struct ScreenListItem_ { extern ObjectClass ScreenListItem_class; -ScreenListItem* ScreenListItem_new(const char* value, int key, ScreenSettings* ss); +ScreenListItem* ScreenListItem_new(const char* value, ScreenSettings* ss); extern PanelClass ScreensPanel_class; diff --git a/Settings.c b/Settings.c index 31d344ba..afc76a77 100644 --- a/Settings.c +++ b/Settings.c @@ -246,7 +246,7 @@ static void readFields(ProcessField* fields, int* flags, const char* line) { String_freeArray(ids); } -static void Settings_readScreen(Settings* this, const char* name, const char* line) { +ScreenSettings* Settings_newScreen(Settings* this, const char* name, const char* line) { ScreenSettings* ss = xCalloc(sizeof(ScreenSettings), 1); ss->name = xStrdup(name); ss->fields = xCalloc(Platform_numberOfFields+1, sizeof(ProcessField)); @@ -258,11 +258,12 @@ static void Settings_readScreen(Settings* this, const char* name, const char* li this->nScreens++; this->screens = xRealloc(this->screens, sizeof(ScreenSettings*) * (this->nScreens + 1)); this->screens[this->nScreens] = NULL; + return ss; } static void Settings_defaultScreens(Settings* this) { - Settings_readScreen(this, "Default", "PID USER PRIORITY NICE M_SIZE M_RESIDENT M_SHARE STATE PERCENT_CPU PERCENT_MEM TIME Command"); - Settings_readScreen(this, "I/O", "PID IO_PRIORITY USER IO_READ_RATE IO_WRITE_RATE Command"); + Settings_newScreen(this, "Default", "PID USER PRIORITY NICE M_SIZE M_RESIDENT M_SHARE STATE PERCENT_CPU PERCENT_MEM TIME Command"); + Settings_newScreen(this, "I/O", "PID IO_PRIORITY USER IO_READ_RATE IO_WRITE_RATE Command"); } static bool Settings_read(Settings* this, const char* fileName) { @@ -342,7 +343,7 @@ static bool Settings_read(Settings* this, const char* fileName) { Settings_readMeterModes(this, option[1], 1); readMeters = true; } else if (strncmp(option[0], "screen:", 7) == 0) { - Settings_readScreen(this, option[0] + 7, option[1]); + Settings_newScreen(this, option[0] + 7, option[1]); } else if (String_eq(option[0], ".tree_view")) { if (this->nScreens > 0) { this->screens[this->nScreens - 1]->treeView = atoi(option[1]); diff --git a/Settings.h b/Settings.h index eccdae55..9687e12f 100644 --- a/Settings.h +++ b/Settings.h @@ -74,6 +74,8 @@ typedef struct Settings_ { void Settings_delete(Settings* this); +ScreenSettings* Settings_newScreen(Settings* this, const char* name, const char* line); + bool Settings_write(Settings* this); Settings* Settings_new(int cpuCount); -- cgit v1.2.3