From c55320e9e2a8916e911bcd39ab37b79e3a7d03b2 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 11 Jan 2021 20:43:27 +0100 Subject: New upstream version 3.0.5 --- Panel.c | 90 ++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 40 deletions(-) (limited to 'Panel.c') diff --git a/Panel.c b/Panel.c index 21dfbe2..5d10375 100644 --- a/Panel.c +++ b/Panel.c @@ -30,7 +30,7 @@ const PanelClass Panel_class = { .eventHandler = Panel_selectByTyping, }; -Panel* Panel_new(int x, int y, int w, int h, bool owner, const ObjectClass* type, FunctionBar* fuBar) { +Panel* Panel_new(int x, int y, int w, int h, const ObjectClass* type, bool owner, FunctionBar* fuBar) { Panel* this; this = xMalloc(sizeof(Panel)); Object_setClass(this, Class(Panel)); @@ -55,11 +55,13 @@ void Panel_init(Panel* this, int x, int y, int w, int h, const ObjectClass* type this->scrollH = 0; this->selected = 0; this->oldSelected = 0; + this->selectedLen = 0; this->needsRedraw = true; + this->wasFocus = false; RichString_beginAllocated(this->header); this->defaultBar = fuBar; this->currentBar = fuBar; - this->selectionColor = CRT_colors[PANEL_SELECTION_FOCUS]; + this->selectionColorId = PANEL_SELECTION_FOCUS; } void Panel_done(Panel* this) { @@ -70,19 +72,12 @@ void Panel_done(Panel* this) { RichString_end(this->header); } -void Panel_setSelectionColor(Panel* this, int color) { - this->selectionColor = color; -} - -RichString* Panel_getHeader(Panel* this) { - assert (this != NULL); - - this->needsRedraw = true; - return &(this->header); +void Panel_setSelectionColor(Panel* this, ColorElements colorId) { + this->selectionColorId = colorId; } inline void Panel_setHeader(Panel* this, const char* header) { - RichString_write(&(this->header), CRT_colors[PANEL_HEADER_FOCUS], header); + RichString_writeWide(&(this->header), CRT_colors[PANEL_HEADER_FOCUS], header); this->needsRedraw = true; } @@ -97,10 +92,6 @@ void Panel_move(Panel* this, int x, int y) { void Panel_resize(Panel* this, int w, int h) { assert (this != NULL); - if (RichString_sizeVal(this->header) > 0) { - h--; - } - this->w = w; this->h = h; this->needsRedraw = true; @@ -217,7 +208,7 @@ void Panel_splice(Panel* this, Vector* from) { this->needsRedraw = true; } -void Panel_draw(Panel* this, bool focus, bool highlightSelected) { +void Panel_draw(Panel* this, bool force_redraw, bool focus, bool highlightSelected, bool hideFunctionBar) { assert (this != NULL); int size = Vector_size(this->items); @@ -226,12 +217,21 @@ void Panel_draw(Panel* this, bool focus, bool highlightSelected) { int x = this->x; int h = this->h; + if (hideFunctionBar) + h++; + + const int header_attr = focus + ? CRT_colors[PANEL_HEADER_FOCUS] + : CRT_colors[PANEL_HEADER_UNFOCUS]; + if (force_redraw) { + if (Panel_printHeaderFn(this)) + Panel_printHeader(this); + else + RichString_setAttr(&this->header, header_attr); + } int headerLen = RichString_sizeVal(this->header); if (headerLen > 0) { - int attr = focus - ? CRT_colors[PANEL_HEADER_FOCUS] - : CRT_colors[PANEL_HEADER_UNFOCUS]; - attrset(attr); + attrset(header_attr); mvhline(y, x, ' ', this->w); if (scrollH < headerLen) { RichString_printoffnVal(this->header, y, x, scrollH, @@ -239,6 +239,7 @@ void Panel_draw(Panel* this, bool focus, bool highlightSelected) { } attrset(CRT_colors[RESET_COLOR]); y++; + h--; } // ensure scroll area is on screen @@ -262,10 +263,10 @@ void Panel_draw(Panel* this, bool focus, bool highlightSelected) { int upTo = MINIMUM(first + h, size); int selectionColor = focus - ? this->selectionColor + ? CRT_colors[this->selectionColorId] : CRT_colors[PANEL_SELECTION_UNFOCUS]; - if (this->needsRedraw) { + if (this->needsRedraw || force_redraw) { int line = 0; for (int i = first; line < h && i < upTo; i++) { Object* itemObj = Vector_get(this->items, i); @@ -293,7 +294,6 @@ void Panel_draw(Panel* this, bool focus, bool highlightSelected) { mvhline(y + line, x, ' ', this->w); line++; } - this->needsRedraw = false; } else { Object* oldObj = Vector_get(this->items, this->oldSelected); @@ -319,17 +319,35 @@ void Panel_draw(Panel* this, bool focus, bool highlightSelected) { RichString_end(new); RichString_end(old); } + + if (focus && (this->needsRedraw || force_redraw || !this->wasFocus)) { + if (Panel_drawFunctionBarFn(this)) + Panel_drawFunctionBar(this, hideFunctionBar); + else if (!hideFunctionBar) + FunctionBar_draw(this->currentBar); + } + this->oldSelected = this->selected; + this->wasFocus = focus; + this->needsRedraw = false; move(0, 0); } +static int Panel_headerHeight(const Panel* this) { + return RichString_sizeVal(this->header) > 0 ? 1 : 0; +} + bool Panel_onKey(Panel* this, int key) { assert (this != NULL); - int size = Vector_size(this->items); + const int size = Vector_size(this->items); - #define CLAMP_INDEX(var, delta, min, max) \ - CLAMP((var) + (delta), (min), MAXIMUM(0, (max))) + #define PANEL_SCROLL(amount) \ + do { \ + this->selected += (amount); \ + this->scrollV = CLAMP(this->scrollV + (amount), 0, MAXIMUM(0, (size - this->h - Panel_headerHeight(this)))); \ + this->needsRedraw = true; \ + } while (0) switch (key) { case KEY_DOWN: @@ -363,27 +381,19 @@ bool Panel_onKey(Panel* this, int key) { break; case KEY_PPAGE: - this->selected -= (this->h - 1); - this->scrollV = CLAMP_INDEX(this->scrollV, -(this->h - 1), 0, size - this->h); - this->needsRedraw = true; + PANEL_SCROLL(-(this->h - Panel_headerHeight(this))); break; case KEY_NPAGE: - this->selected += (this->h - 1); - this->scrollV = CLAMP_INDEX(this->scrollV, +(this->h - 1), 0, size - this->h); - this->needsRedraw = true; + PANEL_SCROLL(+(this->h - Panel_headerHeight(this))); break; case KEY_WHEELUP: - this->selected -= CRT_scrollWheelVAmount; - this->scrollV = CLAMP_INDEX(this->scrollV, -CRT_scrollWheelVAmount, 0, size - this->h); - this->needsRedraw = true; + PANEL_SCROLL(-CRT_scrollWheelVAmount); break; case KEY_WHEELDOWN: - this->selected += CRT_scrollWheelVAmount; - this->scrollV = CLAMP_INDEX(this->scrollV, +CRT_scrollWheelVAmount, 0, size - this->h); - this->needsRedraw = true; + PANEL_SCROLL(+CRT_scrollWheelVAmount); break; case KEY_HOME: @@ -408,7 +418,7 @@ bool Panel_onKey(Panel* this, int key) { return false; } - #undef CLAMP_INDEX + #undef PANEL_SCROLL // ensure selection within bounds if (this->selected < 0 || size == 0) { -- cgit v1.2.3