htop/ScreenManager.c

226 lines
5.9 KiB
C
Raw Normal View History

2006-03-04 18:16:49 +00:00
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "ScreenManager.h"
2006-05-30 13:47:28 +00:00
#include "Panel.h"
2006-03-04 18:16:49 +00:00
#include "Object.h"
#include "Vector.h"
2006-03-04 18:16:49 +00:00
#include "FunctionBar.h"
#include "debug.h"
#include <assert.h>
#include <stdbool.h>
/*{
typedef enum Orientation_ {
VERTICAL,
HORIZONTAL
} Orientation;
typedef struct ScreenManager_ {
int x1;
int y1;
int x2;
int y2;
Orientation orientation;
Vector* items;
Vector* fuBars;
2006-03-04 18:16:49 +00:00
int itemCount;
FunctionBar* fuBar;
bool owner;
} ScreenManager;
}*/
ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, bool owner) {
ScreenManager* this;
this = malloc(sizeof(ScreenManager));
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
this->y2 = y2;
this->fuBar = NULL;
this->orientation = orientation;
2006-05-30 13:47:28 +00:00
this->items = Vector_new(PANEL_CLASS, owner, DEFAULT_SIZE);
this->fuBars = Vector_new(FUNCTIONBAR_CLASS, true, DEFAULT_SIZE);
2006-03-04 18:16:49 +00:00
this->itemCount = 0;
this->owner = owner;
return this;
}
void ScreenManager_delete(ScreenManager* this) {
Vector_delete(this->items);
Vector_delete(this->fuBars);
2006-03-04 18:16:49 +00:00
free(this);
}
inline int ScreenManager_size(ScreenManager* this) {
return this->itemCount;
}
2006-05-30 13:47:28 +00:00
void ScreenManager_add(ScreenManager* this, Panel* item, FunctionBar* fuBar, int size) {
2006-03-04 18:16:49 +00:00
if (this->orientation == HORIZONTAL) {
int lastX = 0;
if (this->itemCount > 0) {
2006-05-30 13:47:28 +00:00
Panel* last = (Panel*) Vector_get(this->items, this->itemCount - 1);
2006-03-04 18:16:49 +00:00
lastX = last->x + last->w + 1;
}
if (size > 0) {
2006-05-30 13:47:28 +00:00
Panel_resize(item, size, LINES-this->y1+this->y2);
2006-03-04 18:16:49 +00:00
} else {
2006-05-30 13:47:28 +00:00
Panel_resize(item, COLS-this->x1+this->x2-lastX, LINES-this->y1+this->y2);
2006-03-04 18:16:49 +00:00
}
2006-05-30 13:47:28 +00:00
Panel_move(item, lastX, this->y1);
2006-03-04 18:16:49 +00:00
}
// TODO: VERTICAL
Vector_add(this->items, item);
2006-03-04 18:16:49 +00:00
if (fuBar)
Vector_add(this->fuBars, fuBar);
2006-03-04 18:16:49 +00:00
else
Vector_add(this->fuBars, FunctionBar_new(0, NULL, NULL, NULL));
2006-03-04 18:16:49 +00:00
if (!this->fuBar && fuBar) this->fuBar = fuBar;
item->needsRedraw = true;
this->itemCount++;
}
2006-05-30 13:47:28 +00:00
Panel* ScreenManager_remove(ScreenManager* this, int index) {
2006-03-04 18:16:49 +00:00
assert(this->itemCount > index);
2006-05-30 13:47:28 +00:00
Panel* lb = (Panel*) Vector_remove(this->items, index);
Vector_remove(this->fuBars, index);
2006-03-04 18:16:49 +00:00
this->fuBar = NULL;
this->itemCount--;
return lb;
}
void ScreenManager_setFunctionBar(ScreenManager* this, FunctionBar* fuBar) {
if (this->owner && this->fuBar)
FunctionBar_delete((Object*)this->fuBar);
this->fuBar = fuBar;
}
void ScreenManager_resize(ScreenManager* this, int x1, int y1, int x2, int y2) {
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
this->y2 = y2;
int items = this->itemCount;
int lastX = 0;
for (int i = 0; i < items - 1; i++) {
2006-05-30 13:47:28 +00:00
Panel* lb = (Panel*) Vector_get(this->items, i);
Panel_resize(lb, lb->w, LINES-y1+y2);
Panel_move(lb, lastX, y1);
2006-03-04 18:16:49 +00:00
lastX = lb->x + lb->w + 1;
}
2006-05-30 13:47:28 +00:00
Panel* lb = (Panel*) Vector_get(this->items, items-1);
Panel_resize(lb, COLS-x1+x2-lastX, LINES-y1+y2);
Panel_move(lb, lastX, y1);
2006-03-04 18:16:49 +00:00
}
2006-05-30 13:47:28 +00:00
void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
2006-03-04 18:16:49 +00:00
bool quit = false;
int focus = 0;
2006-05-30 13:47:28 +00:00
Panel* lbFocus = (Panel*) Vector_get(this->items, focus);
2006-03-04 18:16:49 +00:00
if (this->fuBar)
FunctionBar_draw(this->fuBar, NULL);
int ch;
while (!quit) {
int items = this->itemCount;
for (int i = 0; i < items; i++) {
2006-05-30 13:47:28 +00:00
Panel* lb = (Panel*) Vector_get(this->items, i);
Panel_draw(lb, i == focus);
2006-03-04 18:16:49 +00:00
if (i < items) {
if (this->orientation == HORIZONTAL) {
mvvline(lb->y, lb->x+lb->w, ' ', lb->h+1);
}
}
}
FunctionBar* bar = (FunctionBar*) Vector_get(this->fuBars, focus);
2006-03-04 18:16:49 +00:00
if (bar)
this->fuBar = bar;
if (this->fuBar)
FunctionBar_draw(this->fuBar, NULL);
ch = getch();
bool loop = false;
if (ch == KEY_MOUSE) {
MEVENT mevent;
int ok = getmouse(&mevent);
if (ok == OK) {
if (mevent.y == LINES - 1) {
ch = FunctionBar_synthesizeEvent(this->fuBar, mevent.x);
} else {
for (int i = 0; i < this->itemCount; i++) {
2006-05-30 13:47:28 +00:00
Panel* lb = (Panel*) Vector_get(this->items, i);
2006-03-04 18:16:49 +00:00
if (mevent.x > lb->x && mevent.x <= lb->x+lb->w &&
mevent.y > lb->y && mevent.y <= lb->y+lb->h) {
focus = i;
lbFocus = lb;
2006-05-30 13:47:28 +00:00
Panel_setSelected(lb, mevent.y - lb->y + lb->scrollV - 1);
2006-03-04 18:16:49 +00:00
loop = true;
break;
}
}
}
}
}
if (loop) continue;
if (lbFocus->eventHandler) {
HandlerResult result = lbFocus->eventHandler(lbFocus, ch);
if (result == HANDLED) {
continue;
} else if (result == BREAK_LOOP) {
quit = true;
continue;
}
}
switch (ch) {
case ERR:
continue;
case KEY_RESIZE:
{
ScreenManager_resize(this, this->x1, this->y1, this->x2, this->y2);
continue;
}
case KEY_LEFT:
tryLeft:
if (focus > 0)
focus--;
2006-05-30 13:47:28 +00:00
lbFocus = (Panel*) Vector_get(this->items, focus);
if (Panel_getSize(lbFocus) == 0 && focus > 0)
2006-03-04 18:16:49 +00:00
goto tryLeft;
break;
case KEY_RIGHT:
case 9:
tryRight:
if (focus < this->itemCount - 1)
focus++;
2006-05-30 13:47:28 +00:00
lbFocus = (Panel*) Vector_get(this->items, focus);
if (Panel_getSize(lbFocus) == 0 && focus < this->itemCount - 1)
2006-03-04 18:16:49 +00:00
goto tryRight;
break;
case KEY_F(10):
case 'q':
case 27:
quit = true;
continue;
default:
2006-05-30 13:47:28 +00:00
Panel_onKey(lbFocus, ch);
2006-03-04 18:16:49 +00:00
break;
}
}
*lastFocus = lbFocus;
*lastKey = ch;
}