MetersPanel.c 6.03 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
34
static const char* MetersFunctions[] = {"Type  ", "Move  ", "Delete", "Done  ", NULL};
static const char* MetersKeys[] = {"Space", "Enter", "Del", "Esc"};
35
static int MetersEvents[] = {' ', 13, KEY_DC, 27};
36

37
38
39
40
41
static const char* MetersMovingFunctions[] = {"Up    ", "Down  ", "Left  ", "Right ",  "Confirm", "Delete", "Done  ", NULL};
static const char* MetersMovingKeys[] = {"Up", "Dn", "Lt", "Rt", "Arrows", "Enter", "Del", "Esc"};
static int MetersMovingEvents[] = {KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, 13, KEY_DC, 27};
static FunctionBar* Meters_movingBar = NULL;

42
static void MetersPanel_delete(Object* object) {
43
44
45
   Panel* super = (Panel*) object;
   MetersPanel* this = (MetersPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
46
47
48
   free(this);
}

49
50
51
52
53
54
55
56
57
58
59
void MetersPanel_setMoving(MetersPanel* this, bool moving) {
   Panel* super = (Panel*) this;
   this->moving = moving;
   ((ListItem*)Panel_getSelected(super))->moving = moving;
   if (!moving) {
      Panel_setDefaultBar(super);
   } else {
      super->currentBar = Meters_movingBar;
   }
}

60
61
62
63
64
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)) {
65
            MetersPanel_setMoving(this, false);
66

67
68
69
            Meter* meter = (Meter*) Vector_take(this->meters, selected);
            Panel_remove(super, selected);
            Vector_insert(neighbor->meters, selected, meter);
70
            Panel_insert(&(neighbor->super), selected, (Object*) Meter_toListItem(meter, false));
71
72
            Panel_setSelected(&(neighbor->super), selected);

73
            MetersPanel_setMoving(neighbor, true);
74
75
76
77
78
79
80
            return true;
         }
      }
   }
   return false;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
81
static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
82
   MetersPanel* this = (MetersPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
83
   
84
   int selected = Panel_getSelectedIndex(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
85
   HandlerResult result = IGNORED;
86
   bool sideMove = false;
Hisham Muhammad's avatar
Hisham Muhammad committed
87
88
89
90
91

   switch(ch) {
      case 0x0a:
      case 0x0d:
      case KEY_ENTER:
Hisham Muhammad's avatar
Hisham Muhammad committed
92
      {
93
94
         if (!Vector_size(this->meters))
            break;
95
96
         MetersPanel_setMoving(this, !(this->moving));
         FunctionBar_draw(this->super.currentBar, NULL);
Hisham Muhammad's avatar
Hisham Muhammad committed
97
98
99
100
         result = HANDLED;
         break;
      }
      case ' ':
Hisham Muhammad's avatar
Hisham Muhammad committed
101
102
103
      case KEY_F(4):
      case 't':
      {
104
105
         if (!Vector_size(this->meters))
            break;
106
         Meter* meter = (Meter*) Vector_get(this->meters, selected);
107
108
         int mode = meter->mode + 1;
         if (mode == LAST_METERMODE) mode = 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
109
         Meter_setMode(meter, mode);
110
         Panel_set(super, selected, (Object*) Meter_toListItem(meter, this->moving));
Hisham Muhammad's avatar
Hisham Muhammad committed
111
112
113
         result = HANDLED;
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
114
115
116
117
118
119
120
      case KEY_UP:
      {
         if (!this->moving) {
            break;
         }
         /* else fallthrough */
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
121
122
123
124
      case KEY_F(7):
      case '[':
      case '-':
      {
125
         Vector_moveUp(this->meters, selected);
126
         Panel_moveSelectedUp(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
127
128
129
         result = HANDLED;
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
130
131
132
133
134
135
136
      case KEY_DOWN:
      {
         if (!this->moving) {
            break;
         }
         /* else fallthrough */
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
137
138
139
140
      case KEY_F(8):
      case ']':
      case '+':
      {
141
         Vector_moveDown(this->meters, selected);
142
         Panel_moveSelectedDown(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
143
144
145
         result = HANDLED;
         break;
      }
146
147
148
      case KEY_RIGHT:
      {
         sideMove = moveToNeighbor(this, this->rightNeighbor, selected);
149
150
151
152
153
154
         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.
155
156
157
158
159
         break;
      }
      case KEY_LEFT:
      {
         sideMove = moveToNeighbor(this, this->leftNeighbor, selected);
160
161
162
         if (this->moving && !sideMove) {
            result = HANDLED;
         }
163
164
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
165
166
167
      case KEY_F(9):
      case KEY_DC:
      {
168
169
         if (!Vector_size(this->meters))
            break;
170
171
         if (selected < Vector_size(this->meters)) {
            Vector_remove(this->meters, selected);
172
            Panel_remove(super, selected);
Hisham Muhammad's avatar
Hisham Muhammad committed
173
174
175
176
177
         }
         result = HANDLED;
         break;
      }
   }
178
   if (result == HANDLED || sideMove) {
Hisham Muhammad's avatar
Hisham Muhammad committed
179
      Header* header = (Header*) this->scr->header;
Hisham Muhammad's avatar
Hisham Muhammad committed
180
      this->settings->changed = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
181
182
183
184
185
186
      Header_calculateHeight(header);
      Header_draw(header);
      ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
   }
   return result;
}
187

188
189
190
191
192
193
194
195
PanelClass MetersPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = MetersPanel_delete
   },
   .eventHandler = MetersPanel_eventHandler
};

Hisham Muhammad's avatar
Hisham Muhammad committed
196
MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* meters, ScreenManager* scr) {
197
   MetersPanel* this = AllocThis(MetersPanel);
198
   Panel* super = (Panel*) this;
199
   FunctionBar* fuBar = FunctionBar_new(MetersFunctions, MetersKeys, MetersEvents);
200
201
202
   if (!Meters_movingBar) {
      Meters_movingBar = FunctionBar_new(MetersMovingFunctions, MetersMovingKeys, MetersMovingEvents);
   }
203
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);
204
205
206
207

   this->settings = settings;
   this->meters = meters;
   this->scr = scr;
Hisham Muhammad's avatar
Hisham Muhammad committed
208
   this->moving = false;
209
210
   this->rightNeighbor = NULL;
   this->leftNeighbor = NULL;
211
212
213
   Panel_setHeader(super, header);
   for (int i = 0; i < Vector_size(meters); i++) {
      Meter* meter = (Meter*) Vector_get(meters, i);
214
      Panel_add(super, (Object*) Meter_toListItem(meter, false));
215
216
217
   }
   return this;
}