htop/Panel.c

452 lines
11 KiB
C
Raw Normal View History

2006-03-04 18:16:49 +00:00
/*
2006-05-30 13:47:28 +00:00
htop - Panel.c
2011-05-26 16:35:07 +00:00
(C) 2004-2011 Hisham H. Muhammad
2006-03-04 18:16:49 +00:00
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
2006-05-30 13:47:28 +00:00
#include "Panel.h"
2011-12-26 21:35:57 +00:00
2006-03-04 18:16:49 +00:00
#include "CRT.h"
#include "RichString.h"
#include "ListItem.h"
2011-12-26 21:35:57 +00:00
#include "String.h"
2006-03-04 18:16:49 +00:00
#include <math.h>
#include <stdbool.h>
2011-12-26 21:35:57 +00:00
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
2006-03-04 18:16:49 +00:00
#include <assert.h>
2011-12-26 21:35:57 +00:00
2006-03-04 18:16:49 +00:00
//#link curses
/*{
2011-12-26 21:35:57 +00:00
#include "Object.h"
#include "Vector.h"
2006-03-04 18:16:49 +00:00
2006-05-30 13:47:28 +00:00
typedef struct Panel_ Panel;
2006-03-04 18:16:49 +00:00
typedef enum HandlerResult_ {
HANDLED,
IGNORED,
BREAK_LOOP
} HandlerResult;
#define EVENT_SETSELECTED -1
2006-05-30 13:47:28 +00:00
typedef HandlerResult(*Panel_EventHandler)(Panel*, int);
2006-03-04 18:16:49 +00:00
typedef struct PanelClass_ {
const ObjectClass super;
const Panel_EventHandler eventHandler;
} PanelClass;
#define As_Panel(this_) ((PanelClass*)((this_)->super.klass))
#define Panel_eventHandlerFn(this_) As_Panel(this_)->eventHandler
#define Panel_eventHandler(this_, ev_) As_Panel(this_)->eventHandler((Panel*)(this_), ev_)
2006-05-30 13:47:28 +00:00
struct Panel_ {
2006-03-04 18:16:49 +00:00
Object super;
PanelClass* class;
2006-03-04 18:16:49 +00:00
int x, y, w, h;
WINDOW* window;
Vector* items;
2006-03-04 18:16:49 +00:00
int selected;
int oldSelected;
2014-02-27 19:35:22 +00:00
char* eventHandlerBuffer;
int scrollV;
short scrollH;
2006-03-04 18:16:49 +00:00
bool needsRedraw;
RichString header;
};
}*/
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif
#define KEY_CTRLN 0016 /* control-n key */
#define KEY_CTRLP 0020 /* control-p key */
#define KEY_CTRLF 0006 /* control-f key */
#define KEY_CTRLB 0002 /* control-b key */
2006-03-04 18:16:49 +00:00
PanelClass Panel_class = {
.super = {
.extends = Class(Object),
.delete = Panel_delete
},
.eventHandler = Panel_selectByTyping
};
Panel* Panel_new(int x, int y, int w, int h, bool owner, ObjectClass* type) {
2006-05-30 13:47:28 +00:00
Panel* this;
this = malloc(sizeof(Panel));
Object_setClass(this, Class(Panel));
2006-05-30 13:47:28 +00:00
Panel_init(this, x, y, w, h, type, owner);
2006-03-04 18:16:49 +00:00
return this;
}
2006-05-30 13:47:28 +00:00
void Panel_delete(Object* cast) {
Panel* this = (Panel*)cast;
Panel_done(this);
2006-03-04 18:16:49 +00:00
free(this);
}
void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool owner) {
2006-03-04 18:16:49 +00:00
this->x = x;
this->y = y;
this->w = w;
this->h = h;
this->eventHandlerBuffer = NULL;
this->items = Vector_new(type, owner, DEFAULT_SIZE);
2006-03-04 18:16:49 +00:00
this->scrollV = 0;
this->scrollH = 0;
this->selected = 0;
this->oldSelected = 0;
this->needsRedraw = true;
RichString_beginAllocated(this->header);
2006-03-04 18:16:49 +00:00
}
2006-05-30 13:47:28 +00:00
void Panel_done(Panel* this) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
free(this->eventHandlerBuffer);
Vector_delete(this->items);
RichString_end(this->header);
2006-03-04 18:16:49 +00:00
}
RichString* Panel_getHeader(Panel* this) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
this->needsRedraw = true;
return &(this->header);
2006-03-04 18:16:49 +00:00
}
2010-02-25 01:43:18 +00:00
inline void Panel_setHeader(Panel* this, const char* header) {
RichString_write(&(this->header), CRT_colors[PANEL_HEADER_FOCUS], header);
this->needsRedraw = true;
2006-03-04 18:16:49 +00:00
}
2006-05-30 13:47:28 +00:00
void Panel_move(Panel* this, int x, int y) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
this->x = x;
this->y = y;
this->needsRedraw = true;
}
2006-05-30 13:47:28 +00:00
void Panel_resize(Panel* this, int w, int h) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
if (RichString_sizeVal(this->header) > 0)
2006-03-04 18:16:49 +00:00
h--;
this->w = w;
this->h = h;
this->needsRedraw = true;
}
2006-05-30 13:47:28 +00:00
void Panel_prune(Panel* this) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
Vector_prune(this->items);
2006-03-04 18:16:49 +00:00
this->scrollV = 0;
this->selected = 0;
this->oldSelected = 0;
this->needsRedraw = true;
}
2006-05-30 13:47:28 +00:00
void Panel_add(Panel* this, Object* o) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
Vector_add(this->items, o);
2006-03-04 18:16:49 +00:00
this->needsRedraw = true;
}
2006-05-30 13:47:28 +00:00
void Panel_insert(Panel* this, int i, Object* o) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
Vector_insert(this->items, i, o);
2006-03-04 18:16:49 +00:00
this->needsRedraw = true;
}
2006-05-30 13:47:28 +00:00
void Panel_set(Panel* this, int i, Object* o) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
Vector_set(this->items, i, o);
2006-03-04 18:16:49 +00:00
}
2006-05-30 13:47:28 +00:00
Object* Panel_get(Panel* this, int i) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
return Vector_get(this->items, i);
2006-03-04 18:16:49 +00:00
}
2006-05-30 13:47:28 +00:00
Object* Panel_remove(Panel* this, int i) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
this->needsRedraw = true;
Object* removed = Vector_remove(this->items, i);
if (this->selected > 0 && this->selected >= Vector_size(this->items))
2006-03-04 18:16:49 +00:00
this->selected--;
return removed;
}
2006-05-30 13:47:28 +00:00
Object* Panel_getSelected(Panel* this) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
2012-03-05 11:18:27 +00:00
if (Vector_size(this->items) > 0)
return Vector_get(this->items, this->selected);
else
return NULL;
2006-03-04 18:16:49 +00:00
}
2006-05-30 13:47:28 +00:00
void Panel_moveSelectedUp(Panel* this) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
Vector_moveUp(this->items, this->selected);
2006-03-04 18:16:49 +00:00
if (this->selected > 0)
this->selected--;
}
2006-05-30 13:47:28 +00:00
void Panel_moveSelectedDown(Panel* this) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
Vector_moveDown(this->items, this->selected);
if (this->selected + 1 < Vector_size(this->items))
2006-03-04 18:16:49 +00:00
this->selected++;
}
2006-05-30 13:47:28 +00:00
int Panel_getSelectedIndex(Panel* this) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
return this->selected;
}
2009-06-02 04:51:23 +00:00
int Panel_size(Panel* this) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
return Vector_size(this->items);
2006-03-04 18:16:49 +00:00
}
2006-05-30 13:47:28 +00:00
void Panel_setSelected(Panel* this, int selected) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
selected = MAX(0, MIN(Vector_size(this->items) - 1, selected));
2006-03-04 18:16:49 +00:00
this->selected = selected;
if (Panel_eventHandlerFn(this)) {
Panel_eventHandler(this, EVENT_SETSELECTED);
}
2006-03-04 18:16:49 +00:00
}
2006-05-30 13:47:28 +00:00
void Panel_draw(Panel* this, bool focus) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
int itemCount = Vector_size(this->items);
2006-03-04 18:16:49 +00:00
int scrollH = this->scrollH;
int y = this->y; int x = this->x;
int first = this->scrollV;
2012-08-10 21:54:41 +00:00
if (itemCount > this->h && first > itemCount - this->h) {
first = itemCount - this->h;
this->scrollV = first;
}
int last = MIN(itemCount, first + MIN(itemCount, this->h));
2006-03-04 18:16:49 +00:00
if (this->selected < first) {
first = this->selected;
this->scrollV = first;
this->needsRedraw = true;
}
if (this->selected >= last) {
last = MIN(itemCount, this->selected + 1);
first = MAX(0, last - this->h);
this->scrollV = first;
this->needsRedraw = true;
}
assert(first >= 0);
assert(last <= itemCount);
int headerLen = RichString_sizeVal(this->header);
if (headerLen > 0) {
2006-03-04 18:16:49 +00:00
int attr = focus
? CRT_colors[PANEL_HEADER_FOCUS]
: CRT_colors[PANEL_HEADER_UNFOCUS];
attrset(attr);
mvhline(y, x, ' ', this->w);
if (scrollH < headerLen) {
RichString_printoffnVal(this->header, y, x, scrollH,
MIN(headerLen - scrollH, this->w));
2006-03-04 18:16:49 +00:00
}
attrset(CRT_colors[RESET_COLOR]);
y++;
}
int highlight = focus
? CRT_colors[PANEL_HIGHLIGHT_FOCUS]
: CRT_colors[PANEL_HIGHLIGHT_UNFOCUS];
if (this->needsRedraw) {
for(int i = first, j = 0; j < this->h && i < last; i++, j++) {
Object* itemObj = Vector_get(this->items, i);
assert(itemObj); if(!itemObj) continue;
RichString_begin(item);
Object_display(itemObj, &item);
int itemLen = RichString_sizeVal(item);
int amt = MIN(itemLen - scrollH, this->w);
2014-02-27 19:35:22 +00:00
bool selected = (i == this->selected);
if (selected) {
2006-03-04 18:16:49 +00:00
attrset(highlight);
RichString_setAttr(&item, highlight);
2006-03-04 18:16:49 +00:00
}
2014-02-27 19:35:22 +00:00
mvhline(y + j, x, ' ', this->w);
if (amt > 0)
RichString_printoffnVal(item, y+j, x, scrollH, amt);
if (selected)
attrset(CRT_colors[RESET_COLOR]);
RichString_end(item);
2006-03-04 18:16:49 +00:00
}
for (int i = y + (last - first); i < y + this->h; i++)
mvhline(i, x+0, ' ', this->w);
this->needsRedraw = false;
} else {
Object* oldObj = Vector_get(this->items, this->oldSelected);
assert(oldObj);
RichString_begin(old);
Object_display(oldObj, &old);
int oldLen = RichString_sizeVal(old);
Object* newObj = Vector_get(this->items, this->selected);
RichString_begin(new);
Object_display(newObj, &new);
int newLen = RichString_sizeVal(new);
2006-03-04 18:16:49 +00:00
mvhline(y+ this->oldSelected - this->scrollV, x+0, ' ', this->w);
if (scrollH < oldLen)
RichString_printoffnVal(old, y+this->oldSelected - this->scrollV, x,
2014-02-27 19:35:22 +00:00
scrollH, MIN(oldLen - scrollH, this->w));
2006-03-04 18:16:49 +00:00
attrset(highlight);
mvhline(y+this->selected - this->scrollV, x+0, ' ', this->w);
RichString_setAttr(&new, highlight);
if (scrollH < newLen)
RichString_printoffnVal(new, y+this->selected - this->scrollV, x,
2014-02-27 19:35:22 +00:00
scrollH, MIN(newLen - scrollH, this->w));
2006-03-04 18:16:49 +00:00
attrset(CRT_colors[RESET_COLOR]);
RichString_end(new);
RichString_end(old);
2006-03-04 18:16:49 +00:00
}
this->oldSelected = this->selected;
move(0, 0);
}
2009-06-02 04:51:23 +00:00
bool Panel_onKey(Panel* this, int key) {
2006-03-04 18:16:49 +00:00
assert (this != NULL);
switch (key) {
case KEY_DOWN:
case KEY_CTRLN:
if (this->selected + 1 < Vector_size(this->items))
2006-03-04 18:16:49 +00:00
this->selected++;
2009-06-02 04:51:23 +00:00
return true;
2006-03-04 18:16:49 +00:00
case KEY_UP:
case KEY_CTRLP:
2006-03-04 18:16:49 +00:00
if (this->selected > 0)
this->selected--;
2009-06-02 04:51:23 +00:00
return true;
#ifdef KEY_C_DOWN
case KEY_C_DOWN:
if (this->selected + 1 < Vector_size(this->items)) {
this->selected++;
if (this->scrollV < Vector_size(this->items) - this->h) {
this->scrollV++;
this->needsRedraw = true;
}
}
return true;
#endif
#ifdef KEY_C_UP
case KEY_C_UP:
if (this->selected > 0) {
this->selected--;
if (this->scrollV > 0) {
this->scrollV--;
this->needsRedraw = true;
}
}
return true;
#endif
2006-03-04 18:16:49 +00:00
case KEY_LEFT:
case KEY_CTRLB:
2006-03-04 18:16:49 +00:00
if (this->scrollH > 0) {
2014-02-27 19:35:22 +00:00
this->scrollH -= CRT_scrollHAmount;
2006-03-04 18:16:49 +00:00
this->needsRedraw = true;
}
2009-06-02 04:51:23 +00:00
return true;
2006-03-04 18:16:49 +00:00
case KEY_RIGHT:
case KEY_CTRLF:
2014-02-27 19:35:22 +00:00
this->scrollH += CRT_scrollHAmount;
2006-03-04 18:16:49 +00:00
this->needsRedraw = true;
2009-06-02 04:51:23 +00:00
return true;
2006-03-04 18:16:49 +00:00
case KEY_PPAGE:
2009-06-02 04:51:23 +00:00
this->selected -= (this->h - 1);
this->scrollV -= (this->h - 1);
2006-03-04 18:16:49 +00:00
if (this->selected < 0)
this->selected = 0;
2009-06-02 04:51:23 +00:00
if (this->scrollV < 0)
this->scrollV = 0;
this->needsRedraw = true;
return true;
2006-03-04 18:16:49 +00:00
case KEY_NPAGE:
2009-06-02 04:51:23 +00:00
this->selected += (this->h - 1);
int size = Vector_size(this->items);
2006-03-04 18:16:49 +00:00
if (this->selected >= size)
this->selected = size - 1;
2009-06-02 04:51:23 +00:00
this->scrollV += (this->h - 1);
if (this->scrollV >= MAX(0, size - this->h))
this->scrollV = MAX(0, size - this->h - 1);
this->needsRedraw = true;
return true;
2006-03-04 18:16:49 +00:00
case KEY_HOME:
this->selected = 0;
2009-06-02 04:51:23 +00:00
return true;
2006-03-04 18:16:49 +00:00
case KEY_END:
this->selected = Vector_size(this->items) - 1;
2009-06-02 04:51:23 +00:00
return true;
2006-03-04 18:16:49 +00:00
}
2009-06-02 04:51:23 +00:00
return false;
2006-03-04 18:16:49 +00:00
}
HandlerResult Panel_selectByTyping(Panel* this, int ch) {
int size = Panel_size(this);
if (!this->eventHandlerBuffer)
this->eventHandlerBuffer = calloc(100, 1);
if (isalnum(ch)) {
int len = strlen(this->eventHandlerBuffer);
if (len < 99) {
this->eventHandlerBuffer[len] = ch;
this->eventHandlerBuffer[len+1] = '\0';
}
for (int try = 0; try < 2; try++) {
len = strlen(this->eventHandlerBuffer);
for (int i = 0; i < size; i++) {
char* cur = ((ListItem*) Panel_get(this, i))->value;
while (*cur == ' ') cur++;
if (strncasecmp(cur, this->eventHandlerBuffer, len) == 0) {
Panel_setSelected(this, i);
return HANDLED;
}
}
this->eventHandlerBuffer[0] = ch;
this->eventHandlerBuffer[1] = '\0';
}
return HANDLED;
} else if (ch != ERR) {
this->eventHandlerBuffer[0] = '\0';
}
if (ch == 13) {
return BREAK_LOOP;
}
return IGNORED;
}