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

37
static void MetersPanel_delete(Object* object) {
38
39
40
   Panel* super = (Panel*) object;
   MetersPanel* this = (MetersPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
41
42
43
   free(this);
}

44
45
46
47
48
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)) {
49
50
            ((ListItem*)Panel_getSelected(super))->moving = false;

51
52
53
            Meter* meter = (Meter*) Vector_take(this->meters, selected);
            Panel_remove(super, selected);
            Vector_insert(neighbor->meters, selected, meter);
54
            Panel_insert(&(neighbor->super), selected, (Object*) Meter_toListItem(meter, false));
55
56
            Panel_setSelected(&(neighbor->super), selected);

57
            this->moving = false;
58
59
60
61
62
63
64
65
66
            neighbor->moving = true;
            ((ListItem*)Panel_getSelected((Panel*)neighbor))->moving = true;
            return true;
         }
      }
   }
   return false;
}

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

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

174
175
176
177
178
179
180
181
PanelClass MetersPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = MetersPanel_delete
   },
   .eventHandler = MetersPanel_eventHandler
};

Hisham Muhammad's avatar
Hisham Muhammad committed
182
MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* meters, ScreenManager* scr) {
183
   MetersPanel* this = AllocThis(MetersPanel);
184
   Panel* super = (Panel*) this;
185
186
   FunctionBar* fuBar = FunctionBar_new(MetersFunctions, MetersKeys, MetersEvents);
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);
187
188
189
190

   this->settings = settings;
   this->meters = meters;
   this->scr = scr;
Hisham Muhammad's avatar
Hisham Muhammad committed
191
   this->moving = false;
192
193
   this->rightNeighbor = NULL;
   this->leftNeighbor = NULL;
194
195
196
   Panel_setHeader(super, header);
   for (int i = 0; i < Vector_size(meters); i++) {
      Meter* meter = (Meter*) Vector_get(meters, i);
197
      Panel_add(super, (Object*) Meter_toListItem(meter, false));
198
199
200
   }
   return this;
}