ScreensPanel.c 8.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
htop - ScreensPanel.c
(C) 2004-2011 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "ScreensPanel.h"
#include "Platform.h"

#include "StringUtils.h"
#include "CRT.h"

#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

/*{
#include "Panel.h"
21
22
#include "ScreenManager.h"
#include "ColumnsPanel.h"
23
#include "Settings.h"
24
#include "ListItem.h"
25
26
27
28
29
30
31

#ifndef SCREEN_NAME_LEN
#define SCREEN_NAME_LEN 20
#endif

typedef struct ScreensPanel_ {
   Panel super;
32
33
   
   ScreenManager* scr;
34
   Settings* settings;
35
   ColumnsPanel* columns;
36
37
38
39
40
41
42
   char buffer[SCREEN_NAME_LEN + 1];
   char* saved;
   int cursor;
   bool moving;
   bool renaming;
} ScreensPanel;

43
44
45
46
47
typedef struct ScreenListItem_ {
   ListItem super;
   ScreenSettings* ss;
} ScreenListItem;

48
49
}*/

50
ObjectClass ScreenListItem_class = {
51
   .extends = Class(ListItem),
52
53
54
55
56
   .display = ListItem_display,
   .delete = ListItem_delete,
   .compare = ListItem_compare
};

57
ScreenListItem* ScreenListItem_new(const char* value, ScreenSettings* ss) {
58
   ScreenListItem* this = AllocThis(ScreenListItem);
59
   ListItem_init((ListItem*)this, value, 0);
60
61
62
63
64
   this->ss = ss;
   return this;
}

static const char* const ScreensFunctions[] = {"      ", "Rename", "      ", "      ", "New   ", "      ", "MoveUp", "MoveDn", "Remove", "Done  ", NULL};
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

static void ScreensPanel_delete(Object* object) {
   Panel* super = (Panel*) object;
   ScreensPanel* this = (ScreensPanel*) object;
   Panel_done(super);
   free(this);
}

static HandlerResult ScreensPanel_eventHandlerRenaming(Panel* super, int ch) {
   ScreensPanel* const this = (ScreensPanel*) super;

   //ListItem* item = (ListItem*)Panel_getSelected(super);
   if (ch >= 32 && ch < 127 && ch != 61 && ch != 22) {
      if (this->cursor < SCREEN_NAME_LEN - 1) {
         this->buffer[this->cursor] = ch;
         this->cursor++;
         super->selectedLen = strlen(this->buffer);
         Panel_setCursorToSelection(super);
      }
   } else {
      switch(ch) {
         case 127:
         case KEY_BACKSPACE:
         {
            if (this->cursor > 0) {
               this->cursor--;
               this->buffer[this->cursor] = '\0';
               super->selectedLen = strlen(this->buffer);
               Panel_setCursorToSelection(super);
            }
            break;
         }
         case 0x0a:
         case 0x0d:
         case KEY_ENTER:
         {
            ListItem* item = (ListItem*) Panel_getSelected(super);
            free(this->saved);
            item->value = xStrdup(this->buffer);
            this->renaming = false;
            super->cursorOn = false;
            Panel_setSelectionColor(super, CRT_colors[PANEL_SELECTION_FOCUS]);
107
            ScreensPanel_update(super);
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
            break;
         }
         case 27: // Esc
         {
            ListItem* item = (ListItem*) Panel_getSelected(super);
            item->value = this->saved;
            this->renaming = false;
            super->cursorOn = false;
            Panel_setSelectionColor(super, CRT_colors[PANEL_SELECTION_FOCUS]);
            break;
         }
      }
   }
   return HANDLED;
}

124
static void startRenaming(Panel* super) {
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
   ScreensPanel* const this = (ScreensPanel*) super;

   ListItem* item = (ListItem*) Panel_getSelected(super);
   this->renaming = true;
   super->cursorOn = true;
   char* name = item->value;
   this->saved = name;
   strncpy(this->buffer, name, SCREEN_NAME_LEN);
   this->buffer[SCREEN_NAME_LEN] = '\0';
   this->cursor = strlen(this->buffer);
   item->value = this->buffer;
   Panel_setSelectionColor(super, CRT_colors[PANEL_EDIT]);
   super->selectedLen = strlen(this->buffer);
   Panel_setCursorToSelection(super);
}

141
142
143
144
145
146
147
148
149
150
151
static void rebuildSettingsArray(Panel* super) {
   ScreensPanel* const this = (ScreensPanel*) super;

   int n = Panel_size(super);
   free(this->settings->screens);
   this->settings->screens = xMalloc(sizeof(ScreenSettings*) * (n + 1));
   this->settings->screens[n] = NULL;
   for (int i = 0; i < n; i++) {
      ScreenListItem* item = (ScreenListItem*) Panel_get(super, i);
      this->settings->screens[i] = item->ss;
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
152
   this->settings->nScreens = n;
153
154
}

155
156
157
158
159
160
161
162
163
164
165
static void addNewScreen(Panel* super) {
   ScreensPanel* const this = (ScreensPanel*) super;

   char* name = "New";
   ScreenSettings* ss = Settings_newScreen(this->settings, name, "PID Command");
   ScreenListItem* item = ScreenListItem_new(name, ss);
   int idx = Panel_getSelectedIndex(super);
   Panel_insert(super, idx + 1, (Object*) item);
   Panel_setSelected(super, idx + 1);
}

166
167
168
169
static HandlerResult ScreensPanel_eventHandlerNormal(Panel* super, int ch) {
   ScreensPanel* const this = (ScreensPanel*) super;
   
   int selected = Panel_getSelectedIndex(super);
170
171
   ScreenListItem* oldFocus = (ScreenListItem*) Panel_getSelected(super);
   bool shouldRebuildArray = false;
172
173
174
175
176
177
178
179
180
181
182
183
184
185
   HandlerResult result = IGNORED;
   switch(ch) {
      case 0x0a:
      case 0x0d:
      case KEY_ENTER:
      case KEY_MOUSE:
      case KEY_RECLICK:
      {
         this->moving = !(this->moving);
         Panel_setSelectionColor(super, this->moving ? CRT_colors[PANEL_SELECTION_FOLLOW] : CRT_colors[PANEL_SELECTION_FOCUS]);
         ((ListItem*)Panel_getSelected(super))->moving = this->moving;
         result = HANDLED;
         break;
      }
186
187
188
189
190
191
192
193
194
195
      case EVENT_SET_SELECTED:
         result = HANDLED;
         break;
      case KEY_NPAGE:
      case KEY_PPAGE:
      case KEY_HOME:
      case KEY_END: {
         Panel_onKey(super, ch);
         break;
      }
196
      case KEY_F(2):
197
      case KEY_CTRL('R'):
198
199
200
201
202
203
      {
         startRenaming(super);
         result = HANDLED;
         break;
      }
      case KEY_F(5):
204
      case KEY_CTRL('N'):
205
      {
206
         addNewScreen(super);
207
         startRenaming(super);
208
         shouldRebuildArray = true;
209
210
211
212
213
214
         result = HANDLED;
         break;
      }
      case KEY_UP:
      {
         if (!this->moving) {
215
            Panel_onKey(super, ch);
216
217
218
219
220
221
222
223
224
            break;
         }
         /* else fallthrough */
      }
      case KEY_F(7):
      case '[':
      case '-':
      {
         Panel_moveSelectedUp(super);
225
         shouldRebuildArray = true;
226
227
228
229
230
231
         result = HANDLED;
         break;
      }
      case KEY_DOWN:
      {
         if (!this->moving) {
232
            Panel_onKey(super, ch);
233
234
235
236
237
238
239
240
241
            break;
         }
         /* else fallthrough */
      }
      case KEY_F(8):
      case ']':
      case '+':
      {
         Panel_moveSelectedDown(super);
242
         shouldRebuildArray = true;
243
244
245
246
247
248
         result = HANDLED;
         break;
      }
      case KEY_F(9):
      //case KEY_DC:
      {
249
250
251
252
         if (Panel_size(super) > 1) {
            Panel_remove(super, selected);
         }
         shouldRebuildArray = true;
253
254
255
256
257
258
259
260
261
262
263
264
         result = HANDLED;
         break;
      }
      default:
      {
         if (ch < 255 && isalpha(ch))
            result = Panel_selectByTyping(super, ch);
         if (result == BREAK_LOOP)
            result = IGNORED;
         break;
      }
   }
265
266
267
268
269
270
271
272
   ScreenListItem* newFocus = (ScreenListItem*) Panel_getSelected(super);
   if (oldFocus != newFocus) {
      ColumnsPanel_fill(this->columns, newFocus->ss);
      result = HANDLED;
   }
   if (shouldRebuildArray) {
      rebuildSettingsArray(super);
   }
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
   if (result == HANDLED)
      ScreensPanel_update(super);
   return result;
}
   
static HandlerResult ScreensPanel_eventHandler(Panel* super, int ch) {
   ScreensPanel* const this = (ScreensPanel*) super;

   if (this->renaming) {
      return ScreensPanel_eventHandlerRenaming(super, ch);
   } else {
      return ScreensPanel_eventHandlerNormal(super, ch);
   }
}

PanelClass ScreensPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = ScreensPanel_delete
   },
   .eventHandler = ScreensPanel_eventHandler
};

ScreensPanel* ScreensPanel_new(Settings* settings) {
   ScreensPanel* this = AllocThis(ScreensPanel);
   Panel* super = (Panel*) this;
   FunctionBar* fuBar = FunctionBar_new(ScreensFunctions, NULL, NULL);
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);

   this->settings = settings;
303
   this->columns = ColumnsPanel_new(settings->screens[0], &(settings->changed));
304
305
306
307
308
309
   this->moving = false;
   this->renaming = false;
   super->cursorOn = false;
   this->cursor = 0;
   Panel_setHeader(super, "Screens");

310
311
312
   for (unsigned int i = 0; i < settings->nScreens; i++) {
      ScreenSettings* ss = settings->screens[i];
      char* name = ss->name;
313
      Panel_add(super, (Object*) ScreenListItem_new(name, ss));
314
315
316
317
318
319
320
321
322
323
   }
   return this;
}

void ScreensPanel_update(Panel* super) {
   ScreensPanel* this = (ScreensPanel*) super;
   int size = Panel_size(super);
   this->settings->changed = true;
   this->settings->screens = xRealloc(this->settings->screens, sizeof(char*) * (size+1));
   for (int i = 0; i < size; i++) {
324
325
326
327
328
      ScreenListItem* item = (ScreenListItem*) Panel_get(super, i);
      ScreenSettings* ss = item->ss;
      free(ss->name);
      this->settings->screens[i] = ss;
      ss->name = xStrdup(((ListItem*) item)->value);
329
330
331
   }
   this->settings->screens[size] = NULL;
}