From 52b5beb4e47b635712d9832ffe524d17c730f7b8 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sat, 27 Jan 2018 23:28:37 -0200 Subject: Move responsibility for cursor placement to Panels --- CRT.c | 2 -- CRT.h | 2 -- FunctionBar.c | 13 ++++++------- FunctionBar.h | 4 ++-- IncSet.c | 43 +++++++++++++++++++++++++------------------ IncSet.h | 7 ++++--- InfoScreen.c | 7 ++----- Panel.c | 22 +++++++++++++++++++++- Panel.h | 7 +++++++ ScreenManager.c | 14 +++++++------- TraceScreen.c | 2 +- 11 files changed, 75 insertions(+), 48 deletions(-) diff --git a/CRT.c b/CRT.c index ca9a10dd..676c5509 100644 --- a/CRT.c +++ b/CRT.c @@ -531,8 +531,6 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = { [COLORSCHEME_BROKENGRAY] = { 0 } // dynamically generated. }; -int CRT_cursorX = 0; - int CRT_scrollHAmount = 5; int CRT_scrollWheelVAmount = 10; diff --git a/CRT.h b/CRT.h index 933fe068..6825fff6 100644 --- a/CRT.h +++ b/CRT.h @@ -144,8 +144,6 @@ int* CRT_colors; extern int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT]; -extern int CRT_cursorX; - extern int CRT_scrollHAmount; extern int CRT_scrollWheelVAmount; diff --git a/FunctionBar.c b/FunctionBar.c index 4e4baaf3..630b1c83 100644 --- a/FunctionBar.c +++ b/FunctionBar.c @@ -96,11 +96,12 @@ void FunctionBar_setLabel(FunctionBar* this, int event, const char* text) { } } -void FunctionBar_draw(const FunctionBar* this, char* buffer) { - FunctionBar_drawAttr(this, buffer, CRT_colors[FUNCTION_BAR]); +int FunctionBar_draw(const FunctionBar* this, char* buffer) { + return FunctionBar_drawAttr(this, buffer, CRT_colors[FUNCTION_BAR]); } -void FunctionBar_drawAttr(const FunctionBar* this, char* buffer, int attr) { +int FunctionBar_drawAttr(const FunctionBar* this, char* buffer, int attr) { + int cursorX = 0; attrset(CRT_colors[FUNCTION_BAR]); mvhline(LINES-1, 0, ' ', COLS); int x = 0; @@ -115,12 +116,10 @@ void FunctionBar_drawAttr(const FunctionBar* this, char* buffer, int attr) { if (buffer) { attrset(attr); mvaddstr(LINES-1, x, buffer); - CRT_cursorX = x + strlen(buffer); - curs_set(1); - } else { - curs_set(0); + cursorX = x + strlen(buffer); } attrset(CRT_colors[RESET_COLOR]); + return cursorX; } int FunctionBar_synthesizeEvent(const FunctionBar* this, int pos) { diff --git a/FunctionBar.h b/FunctionBar.h index b60f6582..b590a57c 100644 --- a/FunctionBar.h +++ b/FunctionBar.h @@ -30,9 +30,9 @@ void FunctionBar_delete(FunctionBar* this); void FunctionBar_setLabel(FunctionBar* this, int event, const char* text); -void FunctionBar_draw(const FunctionBar* this, char* buffer); +int FunctionBar_draw(const FunctionBar* this, char* buffer); -void FunctionBar_drawAttr(const FunctionBar* this, char* buffer, int attr); +int FunctionBar_drawAttr(const FunctionBar* this, char* buffer, int attr); int FunctionBar_synthesizeEvent(const FunctionBar* this, int pos); diff --git a/IncSet.c b/IncSet.c index bb9f9544..d0cd5ec8 100644 --- a/IncSet.c +++ b/IncSet.c @@ -38,6 +38,7 @@ typedef struct IncMode_ { typedef struct IncSet_ { IncMode modes[2]; IncMode* active; + Panel* panel; FunctionBar* defaultBar; bool filtering; bool found; @@ -115,23 +116,35 @@ static void updateWeakPanel(IncSet* this, Panel* panel, Vector* lines) { } } -static bool search(IncMode* mode, Panel* panel, IncMode_GetPanelValue getPanelValue) { +static bool search(IncSet* this, Panel* panel, IncMode_GetPanelValue getPanelValue) { int size = Panel_size(panel); bool found = false; for (int i = 0; i < size; i++) { - if (String_contains_i(getPanelValue(panel, i), mode->buffer)) { + if (String_contains_i(getPanelValue(panel, i), this->active->buffer)) { Panel_setSelected(panel, i); found = true; break; } } - if (found) - FunctionBar_draw(mode->bar, mode->buffer); - else - FunctionBar_drawAttr(mode->bar, mode->buffer, CRT_colors[FAILED_SEARCH]); + IncSet_drawBar(this, found ? CRT_colors[FUNCTION_BAR] : CRT_colors[FAILED_SEARCH]); return found; } +void IncSet_activate(IncSet* this, IncType type, Panel* panel) { + this->active = &(this->modes[type]); + panel->currentBar = this->active->bar; + panel->cursorOn = true; + this->panel = panel; + IncSet_drawBar(this, CRT_colors[FUNCTION_BAR]); +} + +static void IncSet_deactivate(IncSet* this, Panel* panel) { + this->active = NULL; + Panel_setDefaultBar(panel); + panel->cursorOn = false; + FunctionBar_draw(this->defaultBar, NULL); +} + bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue getPanelValue, Vector* lines) { if (ch == ERR) return true; @@ -189,13 +202,11 @@ bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue } else { IncMode_reset(mode); } - this->active = NULL; - Panel_setDefaultBar(panel); - FunctionBar_draw(this->defaultBar, NULL); + IncSet_deactivate(this, panel); doSearch = false; } if (doSearch) { - this->found = search(mode, panel, getPanelValue); + this->found = search(this, panel, getPanelValue); } if (filterChanged && lines) { updateWeakPanel(this, panel, lines); @@ -210,15 +221,11 @@ const char* IncSet_getListItemValue(Panel* panel, int i) { return ""; } -void IncSet_activate(IncSet* this, IncType type, Panel* panel) { - this->active = &(this->modes[type]); - FunctionBar_draw(this->active->bar, this->active->buffer); - panel->currentBar = this->active->bar; -} - -void IncSet_drawBar(IncSet* this) { +void IncSet_drawBar(IncSet* this, int attr) { if (this->active) { - FunctionBar_draw(this->active->bar, this->active->buffer); + int cursorX = FunctionBar_drawAttr(this->active->bar, this->active->buffer, attr); + this->panel->cursorY = LINES - 1; + this->panel->cursorX = cursorX; } else { FunctionBar_draw(this->defaultBar, NULL); } diff --git a/IncSet.h b/IncSet.h index 27538f4b..393e64b6 100644 --- a/IncSet.h +++ b/IncSet.h @@ -33,6 +33,7 @@ typedef struct IncMode_ { typedef struct IncSet_ { IncMode modes[2]; IncMode* active; + Panel* panel; FunctionBar* defaultBar; bool filtering; bool found; @@ -45,13 +46,13 @@ IncSet* IncSet_new(FunctionBar* bar); void IncSet_delete(IncSet* this); +void IncSet_activate(IncSet* this, IncType type, Panel* panel); + bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue getPanelValue, Vector* lines); const char* IncSet_getListItemValue(Panel* panel, int i); -void IncSet_activate(IncSet* this, IncType type, Panel* panel); - -void IncSet_drawBar(IncSet* this); +void IncSet_drawBar(IncSet* this, int attr); int IncSet_synthesizeEvent(IncSet* this, int x); diff --git a/InfoScreen.c b/InfoScreen.c index dd5095c7..88dc9df3 100644 --- a/InfoScreen.c +++ b/InfoScreen.c @@ -85,7 +85,7 @@ void InfoScreen_drawTitled(InfoScreen* this, char* fmt, ...) { attrset(CRT_colors[DEFAULT_COLOR]); this->display->needsRedraw = true; Panel_draw(this->display, true); - IncSet_drawBar(this->inc); + IncSet_drawBar(this->inc, CRT_colors[FUNCTION_BAR]); va_end(ap); } @@ -115,10 +115,7 @@ void InfoScreen_run(InfoScreen* this) { Panel_draw(panel, true); - if (this->inc->active) - move(LINES-1, CRT_cursorX); - set_escdelay(25); - int ch = getch(); + int ch = Panel_getCh(panel); if (ch == ERR) { if (As_InfoScreen(this)->onErr) { diff --git a/Panel.c b/Panel.c index 1e53b4a4..e9d69ecb 100644 --- a/Panel.c +++ b/Panel.c @@ -57,6 +57,7 @@ typedef struct PanelClass_ { struct Panel_ { Object super; int x, y, w, h; + int cursorX, cursorY; WINDOW* window; Vector* items; int selected; @@ -66,6 +67,7 @@ struct Panel_ { int scrollV; short scrollH; bool needsRedraw; + bool cursorOn; FunctionBar* currentBar; FunctionBar* defaultBar; RichString header; @@ -85,6 +87,11 @@ struct Panel_ { #define KEY_CTRL(l) ((l)-'A'+1) +void Panel_setCursorToSelection(Panel* this) { + this->cursorY = this->y + this->selected - this->scrollV + 1; + this->cursorX = this->x + this->selectedLen - this->scrollH; +} + PanelClass Panel_class = { .super = { .extends = Class(Object), @@ -112,6 +119,8 @@ void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool this->y = y; this->w = w; this->h = h; + this->cursorX = 0; + this->cursorY = 0; this->eventHandlerState = NULL; this->items = Vector_new(type, owner, DEFAULT_SIZE); this->scrollV = 0; @@ -367,7 +376,6 @@ void Panel_draw(Panel* this, bool focus) { RichString_end(old); } this->oldSelected = this->selected; - move(0, 0); } bool Panel_onKey(Panel* this, int key) { @@ -499,3 +507,15 @@ HandlerResult Panel_selectByTyping(Panel* this, int ch) { } return IGNORED; } + +int Panel_getCh(Panel* this) { + if (this->cursorOn) { + move(this->cursorY, this->cursorX); + curs_set(1); + } else { + curs_set(0); + } + set_escdelay(25); + return getch(); +} + diff --git a/Panel.h b/Panel.h index 5253fc2e..1ebeedf7 100644 --- a/Panel.h +++ b/Panel.h @@ -46,6 +46,7 @@ typedef struct PanelClass_ { struct Panel_ { Object super; int x, y, w, h; + int cursorX, cursorY; WINDOW* window; Vector* items; int selected; @@ -55,6 +56,7 @@ struct Panel_ { int scrollV; short scrollH; bool needsRedraw; + bool cursorOn; FunctionBar* currentBar; FunctionBar* defaultBar; RichString header; @@ -73,6 +75,8 @@ struct Panel_ { #define KEY_CTRL(l) ((l)-'A'+1) +void Panel_setCursorToSelection(Panel* this); + extern PanelClass Panel_class; Panel* Panel_new(int x, int y, int w, int h, bool owner, ObjectClass* type, FunctionBar* fuBar); @@ -123,4 +127,7 @@ bool Panel_onKey(Panel* this, int key); HandlerResult Panel_selectByTyping(Panel* this, int ch); +int Panel_getCh(Panel* this); + + #endif diff --git a/ScreenManager.c b/ScreenManager.c index 05e1c024..1a93ae00 100644 --- a/ScreenManager.c +++ b/ScreenManager.c @@ -157,7 +157,8 @@ static void ScreenManager_drawPanels(ScreenManager* this, int focus) { } } -static Panel* setCurrentPanel(Panel* panel) { +static Panel* setCurrentPanel(ScreenManager* this, int focus) { + Panel* panel = (Panel*) Vector_get(this->panels, focus); FunctionBar_draw(panel->currentBar, NULL); return panel; } @@ -166,7 +167,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { bool quit = false; int focus = 0; - Panel* panelFocus = setCurrentPanel((Panel*) Vector_get(this->panels, focus)); + Panel* panelFocus = setCurrentPanel(this, focus); double oldTime = 0.0; @@ -189,8 +190,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { } int prevCh = ch; - set_escdelay(25); - ch = getch(); + ch = Panel_getCh(panelFocus); HandlerResult result = IGNORED; if (ch == KEY_MOUSE) { @@ -212,7 +212,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { ch = KEY_MOUSE; if (panel == panelFocus || this->allowFocusChange) { focus = i; - panelFocus = setCurrentPanel(panel); + panelFocus = setCurrentPanel(this, i); Object* oldSelection = Panel_getSelected(panel); Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1); if (Panel_getSelected(panel) == oldSelection) { @@ -288,7 +288,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { tryLeft: if (focus > 0) focus--; - panelFocus = setCurrentPanel((Panel*) Vector_get(this->panels, focus)); + panelFocus = setCurrentPanel(this, focus); if (Panel_size(panelFocus) == 0 && focus > 0) goto tryLeft; break; @@ -303,7 +303,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { tryRight: if (focus < this->panelCount - 1) focus++; - panelFocus = setCurrentPanel((Panel*) Vector_get(this->panels, focus)); + panelFocus = setCurrentPanel(this, focus); if (Panel_size(panelFocus) == 0 && focus < this->panelCount - 1) goto tryRight; break; diff --git a/TraceScreen.c b/TraceScreen.c index abef7120..57efcf88 100644 --- a/TraceScreen.c +++ b/TraceScreen.c @@ -86,7 +86,7 @@ void TraceScreen_draw(InfoScreen* this) { mvhline(0, 0, ' ', COLS); mvprintw(0, 0, "Trace of process %d - %s", this->process->pid, this->process->comm); attrset(CRT_colors[DEFAULT_COLOR]); - IncSet_drawBar(this->inc); + IncSet_drawBar(this->inc, CRT_colors[FUNCTION_BAR]); } bool TraceScreen_forkTracer(TraceScreen* this) { -- cgit v1.2.3