diff options
Diffstat (limited to 'Panel.c')
-rw-r--r-- | Panel.c | 154 |
1 files changed, 91 insertions, 63 deletions
@@ -1,10 +1,12 @@ /* htop - Panel.c (C) 2004-2011 Hisham H. Muhammad -Released under the GNU GPLv2, see the COPYING file +Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ +#include "config.h" // IWYU pragma: keep + #include "Panel.h" #include <assert.h> @@ -49,6 +51,8 @@ void Panel_init(Panel* this, int x, int y, int w, int h, const ObjectClass* type 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; @@ -57,6 +61,7 @@ void Panel_init(Panel* this, int x, int y, int w, int h, const ObjectClass* type this->oldSelected = 0; this->selectedLen = 0; this->needsRedraw = true; + this->cursorOn = false; this->wasFocus = false; RichString_beginAllocated(this->header); this->defaultBar = fuBar; @@ -72,6 +77,11 @@ void Panel_done(Panel* this) { RichString_delete(&this->header); } +void Panel_setCursorToSelection(Panel* this) { + this->cursorY = this->y + this->selected - this->scrollV + 1; + this->cursorX = this->x + this->selectedLen - this->scrollH; +} + void Panel_setSelectionColor(Panel* this, ColorElements colorId) { this->selectionColorId = colorId; } @@ -263,8 +273,8 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect int upTo = MINIMUM(first + h, size); int selectionColor = focus - ? CRT_colors[this->selectionColorId] - : CRT_colors[PANEL_SELECTION_UNFOCUS]; + ? CRT_colors[this->selectionColorId] + : CRT_colors[PANEL_SELECTION_UNFOCUS]; if (this->needsRedraw || force_redraw) { int line = 0; @@ -330,7 +340,6 @@ void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelect this->oldSelected = this->selected; this->wasFocus = focus; this->needsRedraw = false; - move(0, 0); } static int Panel_headerHeight(const Panel* this) { @@ -350,72 +359,74 @@ bool Panel_onKey(Panel* this, int key) { } while (0) switch (key) { - case KEY_DOWN: - case KEY_CTRL('N'): - #ifdef KEY_C_DOWN - case KEY_C_DOWN: - #endif - this->selected++; - break; - - case KEY_UP: - case KEY_CTRL('P'): - #ifdef KEY_C_UP - case KEY_C_UP: - #endif - this->selected--; - break; + case KEY_DOWN: + case KEY_CTRL('N'): + #ifdef KEY_C_DOWN + case KEY_C_DOWN: + #endif + this->selected++; + break; + + case KEY_UP: + case KEY_CTRL('P'): + #ifdef KEY_C_UP + case KEY_C_UP: + #endif + this->selected--; + break; + + case KEY_LEFT: + case KEY_CTRL('B'): + if (this->scrollH > 0) { + this->scrollH -= MAXIMUM(CRT_scrollHAmount, 0); + this->needsRedraw = true; + } + break; - case KEY_LEFT: - case KEY_CTRL('B'): - if (this->scrollH > 0) { - this->scrollH -= MAXIMUM(CRT_scrollHAmount, 0); + case KEY_RIGHT: + case KEY_CTRL('F'): + this->scrollH += CRT_scrollHAmount; this->needsRedraw = true; - } - break; + break; - case KEY_RIGHT: - case KEY_CTRL('F'): - this->scrollH += CRT_scrollHAmount; - this->needsRedraw = true; - break; + case KEY_PPAGE: + PANEL_SCROLL(-(this->h - Panel_headerHeight(this))); + break; - case KEY_PPAGE: - PANEL_SCROLL(-(this->h - Panel_headerHeight(this))); - break; + case KEY_NPAGE: + PANEL_SCROLL(+(this->h - Panel_headerHeight(this))); + break; - case KEY_NPAGE: - PANEL_SCROLL(+(this->h - Panel_headerHeight(this))); - break; + case KEY_WHEELUP: + PANEL_SCROLL(-CRT_scrollWheelVAmount); + break; - case KEY_WHEELUP: - PANEL_SCROLL(-CRT_scrollWheelVAmount); - break; + case KEY_WHEELDOWN: + PANEL_SCROLL(+CRT_scrollWheelVAmount); + break; - case KEY_WHEELDOWN: - PANEL_SCROLL(+CRT_scrollWheelVAmount); - break; + case KEY_HOME: + this->selected = 0; + break; - case KEY_HOME: - this->selected = 0; - break; + case KEY_END: + this->selected = size - 1; + break; - case KEY_END: - this->selected = size - 1; - break; + case KEY_CTRL('A'): + case '^': + this->scrollH = 0; + this->needsRedraw = true; + break; - case KEY_CTRL('A'): - case '^': - this->scrollH = 0; - this->needsRedraw = true; - break; - case KEY_CTRL('E'): - case '$': - this->scrollH = MAXIMUM(this->selectedLen - this->w, 0); - this->needsRedraw = true; - break; - default: - return false; + case KEY_CTRL('E'): + case '$': + this->scrollH = MAXIMUM(this->selectedLen - this->w, 0); + this->needsRedraw = true; + break; + + default: + return false; } #undef PANEL_SCROLL @@ -436,6 +447,9 @@ bool Panel_onKey(Panel* this, int key) { HandlerResult Panel_selectByTyping(Panel* this, int ch) { int size = Panel_size(this); + if (ch == '#') + return IGNORED; + if (!this->eventHandlerState) this->eventHandlerState = xCalloc(100, sizeof(char)); char* buffer = this->eventHandlerState; @@ -453,7 +467,7 @@ HandlerResult Panel_selectByTyping(Panel* this, int ch) { } if (len < 99) { - buffer[len] = ch; + buffer[len] = (char) ch; buffer[len + 1] = '\0'; } @@ -461,7 +475,8 @@ HandlerResult Panel_selectByTyping(Panel* this, int ch) { len = strlen(buffer); for (int i = 0; i < size; i++) { const char* cur = ((ListItem*) Panel_get(this, i))->value; - while (*cur == ' ') cur++; + while (*cur == ' ') + cur++; if (strncasecmp(cur, buffer, len) == 0) { Panel_setSelected(this, i); return HANDLED; @@ -470,7 +485,7 @@ HandlerResult Panel_selectByTyping(Panel* this, int ch) { // if current word did not match, // retry considering the character the start of a new word. - buffer[0] = ch; + buffer[0] = (char) ch; buffer[1] = '\0'; } @@ -485,3 +500,16 @@ 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); + } +#ifdef HAVE_SET_ESCDELAY + set_escdelay(25); +#endif + return getch(); +} |