2011-12-26 21:35:57 +00:00
|
|
|
/*
|
|
|
|
htop - MetersPanel.c
|
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2011-12-26 21:35:57 +00:00
|
|
|
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>
|
2020-09-19 11:55:23 +00:00
|
|
|
|
2015-11-02 15:33:37 +00:00
|
|
|
#include "CRT.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "FunctionBar.h"
|
|
|
|
#include "Header.h"
|
|
|
|
#include "ListItem.h"
|
|
|
|
#include "Meter.h"
|
|
|
|
#include "Object.h"
|
|
|
|
#include "ProvideCurses.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
|
2016-03-21 06:30:37 +00:00
|
|
|
// Note: In code the meters are known to have bar/text/graph "Modes", but in UI
|
|
|
|
// we call them "Styles".
|
2020-12-20 10:23:08 +00:00
|
|
|
static const char* const MetersFunctions[] = {"Style ", "Move ", " ", "Delete", "Done ", NULL};
|
|
|
|
static const char* const MetersKeys[] = {"Space", "Enter", "", "Del", "F10"};
|
2020-12-20 15:58:37 +00:00
|
|
|
static const int MetersEvents[] = {' ', 13, ERR, KEY_DC, KEY_F(10)};
|
2016-03-21 07:18:44 +00:00
|
|
|
|
|
|
|
// We avoid UTF-8 arrows ← → here as they might display full-width on Chinese
|
|
|
|
// terminals, breaking our aligning.
|
|
|
|
// In <http://unicode.org/reports/tr11/>, arrows (U+2019..U+2199) are
|
|
|
|
// considered "Ambiguous characters".
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const MetersMovingFunctions[] = {"Style ", "Lock ", "Up ", "Down ", "Left ", "Right ", " ", "Delete", "Done ", NULL};
|
|
|
|
static const char* const MetersMovingKeys[] = {"Space", "Enter", "Up", "Dn", "<-", "->", " ", "Del", "F10"};
|
2020-12-20 15:58:37 +00:00
|
|
|
static const int MetersMovingEvents[] = {' ', 13, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, ERR, KEY_DC, KEY_F(10)};
|
2015-08-27 22:14:13 +00:00
|
|
|
static FunctionBar* Meters_movingBar = NULL;
|
|
|
|
|
2020-09-11 18:14:56 +00:00
|
|
|
void MetersPanel_cleanup() {
|
|
|
|
if (Meters_movingBar) {
|
|
|
|
FunctionBar_delete(Meters_movingBar);
|
|
|
|
Meters_movingBar = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +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);
|
|
|
|
}
|
|
|
|
|
2015-08-27 22:14:13 +00:00
|
|
|
void MetersPanel_setMoving(MetersPanel* this, bool moving) {
|
|
|
|
Panel* super = (Panel*) this;
|
|
|
|
this->moving = moving;
|
2016-02-13 01:33:53 +00:00
|
|
|
ListItem* selected = (ListItem*)Panel_getSelected(super);
|
|
|
|
if (selected) {
|
|
|
|
selected->moving = moving;
|
|
|
|
}
|
2015-08-27 22:14:13 +00:00
|
|
|
if (!moving) {
|
2020-12-16 20:46:11 +00:00
|
|
|
Panel_setSelectionColor(super, PANEL_SELECTION_FOCUS);
|
2015-08-27 22:14:13 +00:00
|
|
|
Panel_setDefaultBar(super);
|
|
|
|
} else {
|
2020-12-16 20:46:11 +00:00
|
|
|
Panel_setSelectionColor(super, PANEL_SELECTION_FOLLOW);
|
2015-08-27 22:14:13 +00:00
|
|
|
super->currentBar = Meters_movingBar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 21:32:07 +00:00
|
|
|
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)) {
|
2015-08-27 22:14:13 +00:00
|
|
|
MetersPanel_setMoving(this, false);
|
2015-03-17 02:01:21 +00:00
|
|
|
|
2015-02-03 21:32:07 +00:00
|
|
|
Meter* meter = (Meter*) Vector_take(this->meters, selected);
|
|
|
|
Panel_remove(super, selected);
|
|
|
|
Vector_insert(neighbor->meters, selected, meter);
|
2015-03-17 02:01:21 +00:00
|
|
|
Panel_insert(&(neighbor->super), selected, (Object*) Meter_toListItem(meter, false));
|
2015-02-03 21:32:07 +00:00
|
|
|
Panel_setSelected(&(neighbor->super), selected);
|
|
|
|
|
2015-08-27 22:14:13 +00:00
|
|
|
MetersPanel_setMoving(neighbor, true);
|
2015-02-03 21:32:07 +00:00
|
|
|
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;
|
2019-10-31 16:39:12 +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;
|
2015-02-03 21:32:07 +00:00
|
|
|
bool sideMove = false;
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
switch(ch) {
|
|
|
|
case 0x0a:
|
|
|
|
case 0x0d:
|
|
|
|
case KEY_ENTER:
|
2015-01-22 01:27:31 +00:00
|
|
|
{
|
2015-06-15 09:09:32 +00:00
|
|
|
if (!Vector_size(this->meters))
|
|
|
|
break;
|
2015-08-27 22:14:13 +00:00
|
|
|
MetersPanel_setMoving(this, !(this->moving));
|
2015-01-22 01:27:31 +00:00
|
|
|
result = HANDLED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ' ':
|
2006-03-04 18:16:49 +00:00
|
|
|
case KEY_F(4):
|
|
|
|
case 't':
|
|
|
|
{
|
2015-06-15 09:09:32 +00:00
|
|
|
if (!Vector_size(this->meters))
|
|
|
|
break;
|
2006-05-30 13:45:40 +00:00
|
|
|
Meter* meter = (Meter*) Vector_get(this->meters, selected);
|
2006-04-10 20:40:38 +00:00
|
|
|
int mode = meter->mode + 1;
|
|
|
|
if (mode == LAST_METERMODE) mode = 1;
|
2006-03-04 18:16:49 +00:00
|
|
|
Meter_setMode(meter, mode);
|
2015-03-17 02:01:21 +00:00
|
|
|
Panel_set(super, selected, (Object*) Meter_toListItem(meter, this->moving));
|
2006-03-04 18:16:49 +00:00
|
|
|
result = HANDLED;
|
|
|
|
break;
|
|
|
|
}
|
2015-01-22 01:27:31 +00:00
|
|
|
case KEY_UP:
|
|
|
|
{
|
|
|
|
if (!this->moving) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 07:05:00 +00:00
|
|
|
/* else fallthrough */
|
2006-03-04 18:16:49 +00:00
|
|
|
case KEY_F(7):
|
|
|
|
case '[':
|
|
|
|
case '-':
|
|
|
|
{
|
2006-05-30 13:45:40 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-01-22 01:27:31 +00:00
|
|
|
case KEY_DOWN:
|
|
|
|
{
|
|
|
|
if (!this->moving) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 07:05:00 +00:00
|
|
|
/* else fallthrough */
|
2006-03-04 18:16:49 +00:00
|
|
|
case KEY_F(8):
|
|
|
|
case ']':
|
|
|
|
case '+':
|
|
|
|
{
|
2006-05-30 13:45:40 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-02-03 21:32:07 +00:00
|
|
|
case KEY_RIGHT:
|
|
|
|
{
|
|
|
|
sideMove = moveToNeighbor(this, this->rightNeighbor, selected);
|
2015-03-17 02:01:21 +00:00
|
|
|
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.
|
2015-02-03 21:32:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KEY_LEFT:
|
|
|
|
{
|
|
|
|
sideMove = moveToNeighbor(this, this->leftNeighbor, selected);
|
2015-03-17 02:01:21 +00:00
|
|
|
if (this->moving && !sideMove) {
|
|
|
|
result = HANDLED;
|
|
|
|
}
|
2015-02-03 21:32:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
case KEY_F(9):
|
|
|
|
case KEY_DC:
|
|
|
|
{
|
2015-06-15 09:09:32 +00:00
|
|
|
if (!Vector_size(this->meters))
|
|
|
|
break;
|
2006-05-30 13:45:40 +00:00
|
|
|
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
|
|
|
}
|
2015-11-02 15:33:22 +00:00
|
|
|
MetersPanel_setMoving(this, false);
|
2006-03-04 18:16:49 +00:00
|
|
|
result = HANDLED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 21:32:07 +00:00
|
|
|
if (result == HANDLED || sideMove) {
|
2020-09-23 12:15:51 +00:00
|
|
|
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);
|
2021-08-21 18:42:04 +00:00
|
|
|
ScreenManager_resize(this->scr);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2008-03-09 08:58:38 +00:00
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
const PanelClass MetersPanel_class = {
|
2012-12-05 15:12:20 +00:00
|
|
|
.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) {
|
2012-12-05 15:12:20 +00:00
|
|
|
MetersPanel* this = AllocThis(MetersPanel);
|
2008-03-09 08:58:38 +00:00
|
|
|
Panel* super = (Panel*) this;
|
2015-03-23 18:26:56 +00:00
|
|
|
FunctionBar* fuBar = FunctionBar_new(MetersFunctions, MetersKeys, MetersEvents);
|
2015-08-27 22:14:13 +00:00
|
|
|
if (!Meters_movingBar) {
|
|
|
|
Meters_movingBar = FunctionBar_new(MetersMovingFunctions, MetersMovingKeys, MetersMovingEvents);
|
|
|
|
}
|
2015-03-23 18:26:56 +00:00
|
|
|
Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);
|
2008-03-09 08:58:38 +00:00
|
|
|
|
|
|
|
this->settings = settings;
|
|
|
|
this->meters = meters;
|
|
|
|
this->scr = scr;
|
2015-01-22 01:27:31 +00:00
|
|
|
this->moving = false;
|
2015-02-04 13:40:44 +00:00
|
|
|
this->rightNeighbor = NULL;
|
|
|
|
this->leftNeighbor = NULL;
|
2008-03-09 08:58:38 +00:00
|
|
|
Panel_setHeader(super, header);
|
|
|
|
for (int i = 0; i < Vector_size(meters); i++) {
|
2021-01-11 22:46:06 +00:00
|
|
|
const Meter* meter = (const Meter*) Vector_get(meters, i);
|
2015-03-17 02:01:21 +00:00
|
|
|
Panel_add(super, (Object*) Meter_toListItem(meter, false));
|
2008-03-09 08:58:38 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|