MetersPanel.c 6.28 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
void MetersPanel_setMoving(MetersPanel* this, bool moving) {
   Panel* super = (Panel*) this;
   this->moving = moving;
53
54
55
56
   ListItem* selected = (ListItem*)Panel_getSelected(super);
   if (selected) {
      selected->moving = moving;
   }
57
   if (!moving) {
58
      Panel_setSelectionColor(super, CRT_colors[PANEL_SELECTION_FOCUS]);
59
60
      Panel_setDefaultBar(super);
   } else {
61
      Panel_setSelectionColor(super, CRT_colors[PANEL_SELECTION_FOLLOW]);
62
63
64
65
      super->currentBar = Meters_movingBar;
   }
}

66
67
68
69
70
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)) {
71
            MetersPanel_setMoving(this, false);
72

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

79
            MetersPanel_setMoving(neighbor, true);
80
81
82
83
84
85
86
            return true;
         }
      }
   }
   return false;
}

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

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

195
196
197
198
199
200
201
202
PanelClass MetersPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = MetersPanel_delete
   },
   .eventHandler = MetersPanel_eventHandler
};

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

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