2006-03-04 18:16:49 +00:00
|
|
|
/*
|
2006-05-30 13:47:28 +00:00
|
|
|
htop - Panel.c
|
2006-03-23 18:53:08 +00:00
|
|
|
(C) 2004-2006 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Object.h"
|
2006-05-30 13:47:28 +00:00
|
|
|
#include "Panel.h"
|
2006-05-30 13:45:40 +00:00
|
|
|
#include "Vector.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
#include "CRT.h"
|
|
|
|
#include "RichString.h"
|
2006-07-11 06:13:32 +00:00
|
|
|
#include "ListItem.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <curses.h>
|
|
|
|
//#link curses
|
|
|
|
|
|
|
|
/*{
|
|
|
|
|
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;
|
|
|
|
|
2008-03-05 06:54:30 +00:00
|
|
|
#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
|
|
|
|
2006-05-30 13:47:28 +00:00
|
|
|
struct Panel_ {
|
2006-03-04 18:16:49 +00:00
|
|
|
Object super;
|
|
|
|
int x, y, w, h;
|
|
|
|
WINDOW* window;
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* items;
|
2006-03-04 18:16:49 +00:00
|
|
|
int selected;
|
|
|
|
int scrollV, scrollH;
|
2006-07-23 22:45:24 +00:00
|
|
|
int scrollHAmount;
|
2006-03-04 18:16:49 +00:00
|
|
|
int oldSelected;
|
|
|
|
bool needsRedraw;
|
|
|
|
RichString header;
|
2006-05-30 13:47:28 +00:00
|
|
|
Panel_EventHandler eventHandler;
|
2006-03-04 18:16:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a,b) ((a)<(b)?(a):(b))
|
|
|
|
#endif
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a,b) ((a)>(b)?(a):(b))
|
|
|
|
#endif
|
|
|
|
|
2006-07-11 06:13:32 +00:00
|
|
|
#ifdef DEBUG
|
2006-05-30 13:47:28 +00:00
|
|
|
char* PANEL_CLASS = "Panel";
|
2006-07-11 06:13:32 +00:00
|
|
|
#else
|
|
|
|
#define PANEL_CLASS NULL
|
|
|
|
#endif
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2006-07-11 06:13:32 +00:00
|
|
|
Panel* Panel_new(int x, int y, int w, int h, char* type, bool owner, Object_Compare compare) {
|
2006-05-30 13:47:28 +00:00
|
|
|
Panel* this;
|
|
|
|
this = malloc(sizeof(Panel));
|
|
|
|
Panel_init(this, x, y, w, h, type, owner);
|
2006-07-11 06:13:32 +00:00
|
|
|
this->items->compare = compare;
|
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);
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:47:28 +00:00
|
|
|
void Panel_init(Panel* this, int x, int y, int w, int h, char* type, bool owner) {
|
2006-03-04 18:16:49 +00:00
|
|
|
Object* super = (Object*) this;
|
2006-07-11 06:13:32 +00:00
|
|
|
Object_setClass(this, PANEL_CLASS);
|
2006-05-30 13:47:28 +00:00
|
|
|
super->delete = Panel_delete;
|
2006-03-04 18:16:49 +00:00
|
|
|
this->x = x;
|
|
|
|
this->y = y;
|
|
|
|
this->w = w;
|
|
|
|
this->h = h;
|
|
|
|
this->eventHandler = NULL;
|
2006-07-11 06:13:32 +00:00
|
|
|
this->items = Vector_new(type, owner, DEFAULT_SIZE, ListItem_compare);
|
2006-03-04 18:16:49 +00:00
|
|
|
this->scrollV = 0;
|
|
|
|
this->scrollH = 0;
|
|
|
|
this->selected = 0;
|
|
|
|
this->oldSelected = 0;
|
|
|
|
this->needsRedraw = true;
|
|
|
|
this->header.len = 0;
|
2006-07-23 22:45:24 +00:00
|
|
|
if (String_eq(CRT_termType, "linux"))
|
|
|
|
this->scrollHAmount = 40;
|
|
|
|
else
|
|
|
|
this->scrollHAmount = 5;
|
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);
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector_delete(this->items);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2006-05-30 13:47:28 +00:00
|
|
|
inline void Panel_setRichHeader(Panel* this, RichString header) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert (this != NULL);
|
|
|
|
|
|
|
|
this->header = header;
|
|
|
|
this->needsRedraw = true;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:47:28 +00:00
|
|
|
inline void Panel_setHeader(Panel* this, char* header) {
|
|
|
|
Panel_setRichHeader(this, RichString_quickString(CRT_colors[PANEL_HEADER_FOCUS], header));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2006-05-30 13:47:28 +00:00
|
|
|
void Panel_setEventHandler(Panel* this, Panel_EventHandler eh) {
|
2006-03-04 18:16:49 +00:00
|
|
|
this->eventHandler = eh;
|
|
|
|
}
|
|
|
|
|
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 (this->header.len > 0)
|
|
|
|
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);
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
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);
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
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);
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
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);
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
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);
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
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;
|
2006-05-30 13:45:40 +00:00
|
|
|
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);
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
return Vector_get(this->items, this->selected);
|
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);
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
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);
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:47:28 +00:00
|
|
|
int Panel_getSize(Panel* this) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert (this != NULL);
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
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);
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
selected = MAX(0, MIN(Vector_size(this->items) - 1, selected));
|
2006-03-04 18:16:49 +00:00
|
|
|
this->selected = selected;
|
2008-03-05 06:54:30 +00:00
|
|
|
if (this->eventHandler) {
|
|
|
|
this->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 first, last;
|
2006-05-30 13:45:40 +00:00
|
|
|
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;
|
|
|
|
first = this->scrollV;
|
|
|
|
|
|
|
|
if (this->h > itemCount) {
|
|
|
|
last = this->scrollV + itemCount;
|
|
|
|
move(y + last, x + 0);
|
|
|
|
} else {
|
|
|
|
last = MIN(itemCount, this->scrollV + this->h);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (this->header.len > 0) {
|
|
|
|
int attr = focus
|
|
|
|
? CRT_colors[PANEL_HEADER_FOCUS]
|
|
|
|
: CRT_colors[PANEL_HEADER_UNFOCUS];
|
|
|
|
attrset(attr);
|
|
|
|
mvhline(y, x, ' ', this->w);
|
|
|
|
if (scrollH < this->header.len) {
|
|
|
|
mvaddchnstr(y, x, this->header.chstr + scrollH,
|
|
|
|
MIN(this->header.len - scrollH, this->w));
|
|
|
|
}
|
|
|
|
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++) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Object* itemObj = Vector_get(this->items, i);
|
2006-07-12 01:16:03 +00:00
|
|
|
RichString itemRef;
|
|
|
|
RichString_initVal(itemRef);
|
2006-03-04 18:16:49 +00:00
|
|
|
itemObj->display(itemObj, &itemRef);
|
|
|
|
int amt = MIN(itemRef.len - scrollH, this->w);
|
|
|
|
if (i == this->selected) {
|
|
|
|
attrset(highlight);
|
|
|
|
RichString_setAttr(&itemRef, highlight);
|
|
|
|
mvhline(y + j, x+0, ' ', this->w);
|
|
|
|
if (amt > 0)
|
|
|
|
mvaddchnstr(y+j, x+0, itemRef.chstr + scrollH, amt);
|
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
|
|
|
} else {
|
|
|
|
mvhline(y+j, x+0, ' ', this->w);
|
|
|
|
if (amt > 0)
|
|
|
|
mvaddchnstr(y+j, x+0, itemRef.chstr + scrollH, amt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = y + (last - first); i < y + this->h; i++)
|
|
|
|
mvhline(i, x+0, ' ', this->w);
|
|
|
|
this->needsRedraw = false;
|
|
|
|
|
|
|
|
} else {
|
2006-05-30 13:45:40 +00:00
|
|
|
Object* oldObj = Vector_get(this->items, this->oldSelected);
|
2006-07-12 01:16:03 +00:00
|
|
|
RichString oldRef;
|
|
|
|
RichString_initVal(oldRef);
|
2006-03-04 18:16:49 +00:00
|
|
|
oldObj->display(oldObj, &oldRef);
|
2006-05-30 13:45:40 +00:00
|
|
|
Object* newObj = Vector_get(this->items, this->selected);
|
2006-07-12 01:16:03 +00:00
|
|
|
RichString newRef;
|
|
|
|
RichString_initVal(newRef);
|
2006-03-04 18:16:49 +00:00
|
|
|
newObj->display(newObj, &newRef);
|
|
|
|
mvhline(y+ this->oldSelected - this->scrollV, x+0, ' ', this->w);
|
|
|
|
if (scrollH < oldRef.len)
|
|
|
|
mvaddchnstr(y+ this->oldSelected - this->scrollV, x+0, oldRef.chstr + this->scrollH, MIN(oldRef.len - scrollH, this->w));
|
|
|
|
attrset(highlight);
|
|
|
|
mvhline(y+this->selected - this->scrollV, x+0, ' ', this->w);
|
|
|
|
RichString_setAttr(&newRef, highlight);
|
|
|
|
if (scrollH < newRef.len)
|
|
|
|
mvaddchnstr(y+this->selected - this->scrollV, x+0, newRef.chstr + this->scrollH, MIN(newRef.len - scrollH, this->w));
|
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
|
|
|
}
|
|
|
|
this->oldSelected = this->selected;
|
|
|
|
move(0, 0);
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:47:28 +00:00
|
|
|
void Panel_onKey(Panel* this, int key) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert (this != NULL);
|
|
|
|
switch (key) {
|
|
|
|
case KEY_DOWN:
|
2006-05-30 13:45:40 +00:00
|
|
|
if (this->selected + 1 < Vector_size(this->items))
|
2006-03-04 18:16:49 +00:00
|
|
|
this->selected++;
|
|
|
|
break;
|
|
|
|
case KEY_UP:
|
|
|
|
if (this->selected > 0)
|
|
|
|
this->selected--;
|
|
|
|
break;
|
|
|
|
case KEY_LEFT:
|
|
|
|
if (this->scrollH > 0) {
|
2006-07-23 22:45:24 +00:00
|
|
|
this->scrollH -= this->scrollHAmount;
|
2006-03-04 18:16:49 +00:00
|
|
|
this->needsRedraw = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_RIGHT:
|
2006-07-23 22:45:24 +00:00
|
|
|
this->scrollH += this->scrollHAmount;
|
2006-03-04 18:16:49 +00:00
|
|
|
this->needsRedraw = true;
|
|
|
|
break;
|
|
|
|
case KEY_PPAGE:
|
|
|
|
this->selected -= this->h;
|
|
|
|
if (this->selected < 0)
|
|
|
|
this->selected = 0;
|
|
|
|
break;
|
|
|
|
case KEY_NPAGE:
|
|
|
|
this->selected += this->h;
|
2006-05-30 13:45:40 +00:00
|
|
|
int size = Vector_size(this->items);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (this->selected >= size)
|
|
|
|
this->selected = size - 1;
|
|
|
|
break;
|
|
|
|
case KEY_HOME:
|
|
|
|
this->selected = 0;
|
|
|
|
break;
|
|
|
|
case KEY_END:
|
2006-05-30 13:45:40 +00:00
|
|
|
this->selected = Vector_size(this->items) - 1;
|
2006-03-04 18:16:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|