MetersPanel.c 2.84 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
/*
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.
*/
Hisham Muhammad's avatar
Hisham Muhammad committed
7

8
#include "MetersPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9

Hisham Muhammad's avatar
Hisham Muhammad committed
10
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
11
12
13
#include <assert.h>

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
14
15
16
#include "Panel.h"
#include "Settings.h"
#include "ScreenManager.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
17

18
19
typedef struct MetersPanel_ {
   Panel super;
Hisham Muhammad's avatar
Hisham Muhammad committed
20
21

   Settings* settings;
22
   Vector* meters;
Hisham Muhammad's avatar
Hisham Muhammad committed
23
   ScreenManager* scr;
24
} MetersPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
25
26
27

}*/

28
static void MetersPanel_delete(Object* object) {
29
30
31
   Panel* super = (Panel*) object;
   MetersPanel* this = (MetersPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
32
33
34
   free(this);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
35
static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
36
   MetersPanel* this = (MetersPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
37
   
38
   int selected = Panel_getSelectedIndex(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
39
40
41
42
43
44
45
46
47
   HandlerResult result = IGNORED;

   switch(ch) {
      case 0x0a:
      case 0x0d:
      case KEY_ENTER:
      case KEY_F(4):
      case 't':
      {
48
         Meter* meter = (Meter*) Vector_get(this->meters, selected);
49
50
         int mode = meter->mode + 1;
         if (mode == LAST_METERMODE) mode = 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
51
         Meter_setMode(meter, mode);
52
         Panel_set(super, selected, (Object*) Meter_toListItem(meter));
Hisham Muhammad's avatar
Hisham Muhammad committed
53
54
55
56
57
58
59
         result = HANDLED;
         break;
      }
      case KEY_F(7):
      case '[':
      case '-':
      {
60
         Vector_moveUp(this->meters, selected);
61
         Panel_moveSelectedUp(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
62
63
64
65
66
67
68
         result = HANDLED;
         break;
      }
      case KEY_F(8):
      case ']':
      case '+':
      {
69
         Vector_moveDown(this->meters, selected);
70
         Panel_moveSelectedDown(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
71
72
73
74
75
76
         result = HANDLED;
         break;
      }
      case KEY_F(9):
      case KEY_DC:
      {
77
78
         if (selected < Vector_size(this->meters)) {
            Vector_remove(this->meters, selected);
79
            Panel_remove(super, selected);
Hisham Muhammad's avatar
Hisham Muhammad committed
80
81
82
83
84
85
86
         }
         result = HANDLED;
         break;
      }
   }
   if (result == HANDLED) {
      Header* header = this->settings->header;
Hisham Muhammad's avatar
Hisham Muhammad committed
87
      this->settings->changed = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
88
89
90
91
92
93
      Header_calculateHeight(header);
      Header_draw(header);
      ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
   }
   return result;
}
94

95
96
97
98
99
100
101
102
PanelClass MetersPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = MetersPanel_delete
   },
   .eventHandler = MetersPanel_eventHandler
};

Hisham Muhammad's avatar
Hisham Muhammad committed
103
MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* meters, ScreenManager* scr) {
104
   MetersPanel* this = AllocThis(MetersPanel);
105
   Panel* super = (Panel*) this;
106
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);
107
108
109
110
111
112
113
114
115
116
117

   this->settings = settings;
   this->meters = meters;
   this->scr = scr;
   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));
   }
   return this;
}