MetersPanel.c 4.9 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
20
typedef struct MetersPanel_ MetersPanel;

struct MetersPanel_ {
21
   Panel super;
Hisham Muhammad's avatar
Hisham Muhammad committed
22
23

   Settings* settings;
24
   Vector* meters;
Hisham Muhammad's avatar
Hisham Muhammad committed
25
   ScreenManager* scr;
26
27
   MetersPanel* leftNeighbor;
   MetersPanel* rightNeighbor;
Hisham Muhammad's avatar
Hisham Muhammad committed
28
   bool moving;
29
};
Hisham Muhammad's avatar
Hisham Muhammad committed
30
31
32

}*/

33
static void MetersPanel_delete(Object* object) {
34
35
36
   Panel* super = (Panel*) object;
   MetersPanel* this = (MetersPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
37
38
39
   free(this);
}

40
41
42
43
44
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)) {
45
46
            ((ListItem*)Panel_getSelected(super))->moving = false;

47
48
49
            Meter* meter = (Meter*) Vector_take(this->meters, selected);
            Panel_remove(super, selected);
            Vector_insert(neighbor->meters, selected, meter);
50
            Panel_insert(&(neighbor->super), selected, (Object*) Meter_toListItem(meter, false));
51
52
            Panel_setSelected(&(neighbor->super), selected);

53
            this->moving = false;
54
55
56
57
58
59
60
61
62
            neighbor->moving = true;
            ((ListItem*)Panel_getSelected((Panel*)neighbor))->moving = true;
            return true;
         }
      }
   }
   return false;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
63
static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
64
   MetersPanel* this = (MetersPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
65
   
66
   int selected = Panel_getSelectedIndex(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
67
   HandlerResult result = IGNORED;
68
   bool sideMove = false;
Hisham Muhammad's avatar
Hisham Muhammad committed
69
70
71
72
73

   switch(ch) {
      case 0x0a:
      case 0x0d:
      case KEY_ENTER:
Hisham Muhammad's avatar
Hisham Muhammad committed
74
75
76
77
78
79
80
      {
         this->moving = !(this->moving);
         ((ListItem*)Panel_getSelected(super))->moving = this->moving;
         result = HANDLED;
         break;
      }
      case ' ':
Hisham Muhammad's avatar
Hisham Muhammad committed
81
82
83
      case KEY_F(4):
      case 't':
      {
84
         Meter* meter = (Meter*) Vector_get(this->meters, selected);
85
86
         int mode = meter->mode + 1;
         if (mode == LAST_METERMODE) mode = 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
87
         Meter_setMode(meter, mode);
88
         Panel_set(super, selected, (Object*) Meter_toListItem(meter, this->moving));
Hisham Muhammad's avatar
Hisham Muhammad committed
89
90
91
         result = HANDLED;
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
92
93
94
95
96
97
98
      case KEY_UP:
      {
         if (!this->moving) {
            break;
         }
         /* else fallthrough */
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
99
100
101
102
      case KEY_F(7):
      case '[':
      case '-':
      {
103
         Vector_moveUp(this->meters, selected);
104
         Panel_moveSelectedUp(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
105
106
107
         result = HANDLED;
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
108
109
110
111
112
113
114
      case KEY_DOWN:
      {
         if (!this->moving) {
            break;
         }
         /* else fallthrough */
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
115
116
117
118
      case KEY_F(8):
      case ']':
      case '+':
      {
119
         Vector_moveDown(this->meters, selected);
120
         Panel_moveSelectedDown(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
121
122
123
         result = HANDLED;
         break;
      }
124
125
126
      case KEY_RIGHT:
      {
         sideMove = moveToNeighbor(this, this->rightNeighbor, selected);
127
128
129
130
131
132
         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.
133
134
135
136
137
         break;
      }
      case KEY_LEFT:
      {
         sideMove = moveToNeighbor(this, this->leftNeighbor, selected);
138
139
140
         if (this->moving && !sideMove) {
            result = HANDLED;
         }
141
142
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
143
144
145
      case KEY_F(9):
      case KEY_DC:
      {
146
147
         if (selected < Vector_size(this->meters)) {
            Vector_remove(this->meters, selected);
148
            Panel_remove(super, selected);
Hisham Muhammad's avatar
Hisham Muhammad committed
149
150
151
152
153
         }
         result = HANDLED;
         break;
      }
   }
154
   if (result == HANDLED || sideMove) {
Hisham Muhammad's avatar
Hisham Muhammad committed
155
      Header* header = (Header*) this->scr->header;
Hisham Muhammad's avatar
Hisham Muhammad committed
156
      this->settings->changed = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
157
158
159
160
161
162
      Header_calculateHeight(header);
      Header_draw(header);
      ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
   }
   return result;
}
163

164
165
166
167
168
169
170
171
PanelClass MetersPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = MetersPanel_delete
   },
   .eventHandler = MetersPanel_eventHandler
};

Hisham Muhammad's avatar
Hisham Muhammad committed
172
MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* meters, ScreenManager* scr) {
173
   MetersPanel* this = AllocThis(MetersPanel);
174
   Panel* super = (Panel*) this;
175
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);
176
177
178
179

   this->settings = settings;
   this->meters = meters;
   this->scr = scr;
Hisham Muhammad's avatar
Hisham Muhammad committed
180
   this->moving = false;
181
182
   this->rightNeighbor = NULL;
   this->leftNeighbor = NULL;
183
184
185
   Panel_setHeader(super, header);
   for (int i = 0; i < Vector_size(meters); i++) {
      Meter* meter = (Meter*) Vector_get(meters, i);
186
      Panel_add(super, (Object*) Meter_toListItem(meter, false));
187
188
189
   }
   return this;
}