MetersPanel.c 6.22 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
#include <assert.h>
12
#include "CRT.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13
14

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

19
20
21
typedef struct MetersPanel_ MetersPanel;

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

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

}*/

34
35
static const char* MetersFunctions[] = {"Type  ", "Move  ", "Delete", "Done  ", NULL};
static const char* MetersKeys[] = {"Space", "Enter", "Del", "Esc"};
36
static int MetersEvents[] = {' ', 13, KEY_DC, 27};
37

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

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

50
51
52
53
54
void MetersPanel_setMoving(MetersPanel* this, bool moving) {
   Panel* super = (Panel*) this;
   this->moving = moving;
   ((ListItem*)Panel_getSelected(super))->moving = moving;
   if (!moving) {
55
      Panel_setSelectionColor(super, CRT_colors[PANEL_SELECTION_FOCUS]);
56
57
      Panel_setDefaultBar(super);
   } else {
58
      Panel_setSelectionColor(super, CRT_colors[PANEL_SELECTION_FOLLOW]);
59
60
61
62
      super->currentBar = Meters_movingBar;
   }
}

63
64
65
66
67
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)) {
68
            MetersPanel_setMoving(this, false);
69

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

76
            MetersPanel_setMoving(neighbor, true);
77
78
79
80
81
82
83
            return true;
         }
      }
   }
   return false;
}

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

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

192
193
194
195
196
197
198
199
PanelClass MetersPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = MetersPanel_delete
   },
   .eventHandler = MetersPanel_eventHandler
};

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

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