htop/MetersPanel.c

190 lines
4.9 KiB
C
Raw Normal View History

2011-12-26 21:35:57 +00:00
/*
htop - MetersPanel.c
(C) 2004-2011 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
2006-03-04 18:16:49 +00:00
2006-05-30 13:47:28 +00:00
#include "MetersPanel.h"
2006-03-04 18:16:49 +00:00
2011-12-26 21:35:57 +00:00
#include <stdlib.h>
2006-03-04 18:16:49 +00:00
#include <assert.h>
/*{
2011-12-26 21:35:57 +00:00
#include "Panel.h"
#include "Settings.h"
#include "ScreenManager.h"
2006-03-04 18:16:49 +00:00
typedef struct MetersPanel_ MetersPanel;
struct MetersPanel_ {
2006-05-30 13:47:28 +00:00
Panel super;
2006-03-04 18:16:49 +00:00
Settings* settings;
Vector* meters;
2006-03-04 18:16:49 +00:00
ScreenManager* scr;
MetersPanel* leftNeighbor;
MetersPanel* rightNeighbor;
bool moving;
};
2006-03-04 18:16:49 +00:00
}*/
static void MetersPanel_delete(Object* object) {
2006-05-30 13:47:28 +00:00
Panel* super = (Panel*) object;
MetersPanel* this = (MetersPanel*) object;
Panel_done(super);
2006-03-04 18:16:49 +00:00
free(this);
}
static inline bool moveToNeighbor(MetersPanel* this, MetersPanel* neighbor, int selected) {
Panel* super = (Panel*) this;
if (this->moving) {
if (neighbor) {
if (selected < Vector_size(this->meters)) {
((ListItem*)Panel_getSelected(super))->moving = false;
Meter* meter = (Meter*) Vector_take(this->meters, selected);
Panel_remove(super, selected);
Vector_insert(neighbor->meters, selected, meter);
Panel_insert(&(neighbor->super), selected, (Object*) Meter_toListItem(meter, false));
Panel_setSelected(&(neighbor->super), selected);
this->moving = false;
neighbor->moving = true;
((ListItem*)Panel_getSelected((Panel*)neighbor))->moving = true;
return true;
}
}
}
return false;
}
2011-11-05 03:50:44 +00:00
static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
2006-05-30 13:47:28 +00:00
MetersPanel* this = (MetersPanel*) super;
2006-03-04 18:16:49 +00:00
2006-05-30 13:47:28 +00:00
int selected = Panel_getSelectedIndex(super);
2006-03-04 18:16:49 +00:00
HandlerResult result = IGNORED;
bool sideMove = false;
2006-03-04 18:16:49 +00:00
switch(ch) {
case 0x0a:
case 0x0d:
case KEY_ENTER:
{
this->moving = !(this->moving);
((ListItem*)Panel_getSelected(super))->moving = this->moving;
result = HANDLED;
break;
}
case ' ':
2006-03-04 18:16:49 +00:00
case KEY_F(4):
case 't':
{
Meter* meter = (Meter*) Vector_get(this->meters, selected);
int mode = meter->mode + 1;
if (mode == LAST_METERMODE) mode = 1;
2006-03-04 18:16:49 +00:00
Meter_setMode(meter, mode);
Panel_set(super, selected, (Object*) Meter_toListItem(meter, this->moving));
2006-03-04 18:16:49 +00:00
result = HANDLED;
break;
}
case KEY_UP:
{
if (!this->moving) {
break;
}
/* else fallthrough */
}
2006-03-04 18:16:49 +00:00
case KEY_F(7):
case '[':
case '-':
{
Vector_moveUp(this->meters, selected);
2006-05-30 13:47:28 +00:00
Panel_moveSelectedUp(super);
2006-03-04 18:16:49 +00:00
result = HANDLED;
break;
}
case KEY_DOWN:
{
if (!this->moving) {
break;
}
/* else fallthrough */
}
2006-03-04 18:16:49 +00:00
case KEY_F(8):
case ']':
case '+':
{
Vector_moveDown(this->meters, selected);
2006-05-30 13:47:28 +00:00
Panel_moveSelectedDown(super);
2006-03-04 18:16:49 +00:00
result = HANDLED;
break;
}
case KEY_RIGHT:
{
sideMove = moveToNeighbor(this, this->rightNeighbor, selected);
if (this->moving && !sideMove) {
// lock user here until it exits positioning-mode
result = HANDLED;
}
// if user is free, don't set HANDLED;
// let ScreenManager handle focus.
break;
}
case KEY_LEFT:
{
sideMove = moveToNeighbor(this, this->leftNeighbor, selected);
if (this->moving && !sideMove) {
result = HANDLED;
}
break;
}
2006-03-04 18:16:49 +00:00
case KEY_F(9):
case KEY_DC:
{
if (selected < Vector_size(this->meters)) {
Vector_remove(this->meters, selected);
2006-05-30 13:47:28 +00:00
Panel_remove(super, selected);
2006-03-04 18:16:49 +00:00
}
result = HANDLED;
break;
}
}
if (result == HANDLED || sideMove) {
Header* header = (Header*) this->scr->header;
2009-06-02 04:51:23 +00:00
this->settings->changed = true;
2006-03-04 18:16:49 +00:00
Header_calculateHeight(header);
Header_draw(header);
ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
}
return result;
}
PanelClass MetersPanel_class = {
.super = {
.extends = Class(Panel),
.delete = MetersPanel_delete
},
.eventHandler = MetersPanel_eventHandler
};
2010-02-25 01:43:18 +00:00
MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* meters, ScreenManager* scr) {
MetersPanel* this = AllocThis(MetersPanel);
Panel* super = (Panel*) this;
Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);
this->settings = settings;
this->meters = meters;
this->scr = scr;
this->moving = false;
2015-02-04 13:40:44 +00:00
this->rightNeighbor = NULL;
this->leftNeighbor = NULL;
Panel_setHeader(super, header);
for (int i = 0; i < Vector_size(meters); i++) {
Meter* meter = (Meter*) Vector_get(meters, i);
Panel_add(super, (Object*) Meter_toListItem(meter, false));
}
return this;
}