Panel.c 11 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
/*
2
htop - Panel.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2011 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

8
#include "Panel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9

Hisham Muhammad's avatar
Hisham Muhammad committed
10
11
#include "CRT.h"
#include "RichString.h"
12
#include "ListItem.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13
#include "String.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
14
15
16

#include <math.h>
#include <stdbool.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
17
18
19
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
20
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
21

Hisham Muhammad's avatar
Hisham Muhammad committed
22
23
24
//#link curses

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
25
26
#include "Object.h"
#include "Vector.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
27

28
typedef struct Panel_ Panel;
Hisham Muhammad's avatar
Hisham Muhammad committed
29
30

typedef enum HandlerResult_ {
Hisham Muhammad's avatar
Hisham Muhammad committed
31
32
33
34
35
   HANDLED     = 0x00,
   IGNORED     = 0x01,
   BREAK_LOOP  = 0x02,
   REFRESH     = 0x04,
   RECALCULATE = 0x08,
Hisham Muhammad's avatar
Hisham Muhammad committed
36
37
} HandlerResult;

38
39
#define EVENT_SETSELECTED -1

40
typedef HandlerResult(*Panel_EventHandler)(Panel*, int);
Hisham Muhammad's avatar
Hisham Muhammad committed
41

42
43
44
45
46
47
48
49
50
typedef struct PanelClass_ {
   const ObjectClass super;
   const Panel_EventHandler eventHandler;
} PanelClass;

#define As_Panel(this_)                ((PanelClass*)((this_)->super.klass))
#define Panel_eventHandlerFn(this_)    As_Panel(this_)->eventHandler
#define Panel_eventHandler(this_, ev_) As_Panel(this_)->eventHandler((Panel*)(this_), ev_)

51
struct Panel_ {
Hisham Muhammad's avatar
Hisham Muhammad committed
52
   Object super;
53
   PanelClass* class;
Hisham Muhammad's avatar
Hisham Muhammad committed
54
55
   int x, y, w, h;
   WINDOW* window;
56
   Vector* items;
Hisham Muhammad's avatar
Hisham Muhammad committed
57
58
   int selected;
   int oldSelected;
Hisham Muhammad's avatar
Hisham Muhammad committed
59
   void* eventHandlerState;
Hisham Muhammad's avatar
Hisham Muhammad committed
60
61
   int scrollV;
   short scrollH;
Hisham Muhammad's avatar
Hisham Muhammad committed
62
63
64
65
66
67
68
69
70
71
72
73
74
   bool needsRedraw;
   RichString header;
};

}*/

#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif

75
76
77
78
#define KEY_CTRLN      0016            /* control-n key */
#define KEY_CTRLP      0020            /* control-p key */
#define KEY_CTRLF      0006            /* control-f key */
#define KEY_CTRLB      0002            /* control-b key */
Hisham Muhammad's avatar
Hisham Muhammad committed
79

80
81
82
83
84
85
86
87
88
PanelClass Panel_class = {
   .super = {
      .extends = Class(Object),
      .delete = Panel_delete
   },
   .eventHandler = Panel_selectByTyping
};

Panel* Panel_new(int x, int y, int w, int h, bool owner, ObjectClass* type) {
89
90
   Panel* this;
   this = malloc(sizeof(Panel));
91
   Object_setClass(this, Class(Panel));
92
   Panel_init(this, x, y, w, h, type, owner);
Hisham Muhammad's avatar
Hisham Muhammad committed
93
94
95
   return this;
}

96
97
98
void Panel_delete(Object* cast) {
   Panel* this = (Panel*)cast;
   Panel_done(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
99
100
101
   free(this);
}

102
void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool owner) {
Hisham Muhammad's avatar
Hisham Muhammad committed
103
104
105
106
   this->x = x;
   this->y = y;
   this->w = w;
   this->h = h;
Hisham Muhammad's avatar
Hisham Muhammad committed
107
   this->eventHandlerState = NULL;
108
   this->items = Vector_new(type, owner, DEFAULT_SIZE);
Hisham Muhammad's avatar
Hisham Muhammad committed
109
110
111
112
113
   this->scrollV = 0;
   this->scrollH = 0;
   this->selected = 0;
   this->oldSelected = 0;
   this->needsRedraw = true;
114
   RichString_beginAllocated(this->header);
Hisham Muhammad's avatar
Hisham Muhammad committed
115
116
}

117
void Panel_done(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
118
   assert (this != NULL);
Hisham Muhammad's avatar
Hisham Muhammad committed
119
   free(this->eventHandlerState);
120
   Vector_delete(this->items);
121
   RichString_end(this->header);
Hisham Muhammad's avatar
Hisham Muhammad committed
122
123
}

124
RichString* Panel_getHeader(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
125
126
127
   assert (this != NULL);

   this->needsRedraw = true;
128
   return &(this->header);
Hisham Muhammad's avatar
Hisham Muhammad committed
129
130
}

Hisham Muhammad's avatar
Hisham Muhammad committed
131
inline void Panel_setHeader(Panel* this, const char* header) {
132
133
   RichString_write(&(this->header), CRT_colors[PANEL_HEADER_FOCUS], header);
   this->needsRedraw = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
134
135
}

136
void Panel_move(Panel* this, int x, int y) {
Hisham Muhammad's avatar
Hisham Muhammad committed
137
138
139
140
141
142
143
   assert (this != NULL);

   this->x = x;
   this->y = y;
   this->needsRedraw = true;
}

144
void Panel_resize(Panel* this, int w, int h) {
Hisham Muhammad's avatar
Hisham Muhammad committed
145
146
   assert (this != NULL);

147
   if (RichString_sizeVal(this->header) > 0)
Hisham Muhammad's avatar
Hisham Muhammad committed
148
149
150
151
152
153
      h--;
   this->w = w;
   this->h = h;
   this->needsRedraw = true;
}

154
void Panel_prune(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
155
156
   assert (this != NULL);

157
   Vector_prune(this->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
158
159
160
161
162
163
   this->scrollV = 0;
   this->selected = 0;
   this->oldSelected = 0;
   this->needsRedraw = true;
}

164
void Panel_add(Panel* this, Object* o) {
Hisham Muhammad's avatar
Hisham Muhammad committed
165
166
   assert (this != NULL);

167
   Vector_add(this->items, o);
Hisham Muhammad's avatar
Hisham Muhammad committed
168
169
170
   this->needsRedraw = true;
}

171
void Panel_insert(Panel* this, int i, Object* o) {
Hisham Muhammad's avatar
Hisham Muhammad committed
172
173
   assert (this != NULL);

174
   Vector_insert(this->items, i, o);
Hisham Muhammad's avatar
Hisham Muhammad committed
175
176
177
   this->needsRedraw = true;
}

178
void Panel_set(Panel* this, int i, Object* o) {
Hisham Muhammad's avatar
Hisham Muhammad committed
179
180
   assert (this != NULL);

181
   Vector_set(this->items, i, o);
Hisham Muhammad's avatar
Hisham Muhammad committed
182
183
}

184
Object* Panel_get(Panel* this, int i) {
Hisham Muhammad's avatar
Hisham Muhammad committed
185
186
   assert (this != NULL);

187
   return Vector_get(this->items, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
188
189
}

190
Object* Panel_remove(Panel* this, int i) {
Hisham Muhammad's avatar
Hisham Muhammad committed
191
192
193
   assert (this != NULL);

   this->needsRedraw = true;
194
195
   Object* removed = Vector_remove(this->items, i);
   if (this->selected > 0 && this->selected >= Vector_size(this->items))
Hisham Muhammad's avatar
Hisham Muhammad committed
196
197
198
199
      this->selected--;
   return removed;
}

200
Object* Panel_getSelected(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
201
   assert (this != NULL);
202
203
204
205
   if (Vector_size(this->items) > 0)
      return Vector_get(this->items, this->selected);
   else
      return NULL;
Hisham Muhammad's avatar
Hisham Muhammad committed
206
207
}

208
void Panel_moveSelectedUp(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
209
210
   assert (this != NULL);

211
   Vector_moveUp(this->items, this->selected);
Hisham Muhammad's avatar
Hisham Muhammad committed
212
213
214
215
   if (this->selected > 0)
      this->selected--;
}

216
void Panel_moveSelectedDown(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
217
218
   assert (this != NULL);

219
220
   Vector_moveDown(this->items, this->selected);
   if (this->selected + 1 < Vector_size(this->items))
Hisham Muhammad's avatar
Hisham Muhammad committed
221
222
223
      this->selected++;
}

224
int Panel_getSelectedIndex(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
225
226
227
228
229
   assert (this != NULL);

   return this->selected;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
230
int Panel_size(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
231
232
   assert (this != NULL);

233
   return Vector_size(this->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
234
235
}

236
void Panel_setSelected(Panel* this, int selected) {
Hisham Muhammad's avatar
Hisham Muhammad committed
237
238
   assert (this != NULL);

239
240
241
   selected = MIN(Vector_size(this->items) - 1, selected);
   if (selected < 0)
      selected = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
242
   this->selected = selected;
243
244
   if (Panel_eventHandlerFn(this)) {
      Panel_eventHandler(this, EVENT_SETSELECTED);
245
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
246
247
}

248
void Panel_draw(Panel* this, bool focus) {
Hisham Muhammad's avatar
Hisham Muhammad committed
249
250
   assert (this != NULL);

Hisham Muhammad's avatar
Hisham Muhammad committed
251
   int size = Vector_size(this->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
252
   int scrollH = this->scrollH;
Hisham Muhammad's avatar
Hisham Muhammad committed
253
254
255
   int y = this->y;
   int x = this->x;
   int h = this->h;
Hisham Muhammad's avatar
Hisham Muhammad committed
256

257
258
   int headerLen = RichString_sizeVal(this->header);
   if (headerLen > 0) {
Hisham Muhammad's avatar
Hisham Muhammad committed
259
260
261
262
263
      int attr = focus
               ? CRT_colors[PANEL_HEADER_FOCUS]
               : CRT_colors[PANEL_HEADER_UNFOCUS];
      attrset(attr);
      mvhline(y, x, ' ', this->w);
264
      if (scrollH < headerLen) {
265
         RichString_printoffnVal(this->header, y, x, scrollH,
266
            MIN(headerLen - scrollH, this->w));
Hisham Muhammad's avatar
Hisham Muhammad committed
267
268
269
270
      }
      attrset(CRT_colors[RESET_COLOR]);
      y++;
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

   // ensure scroll area is on screen
   if (this->scrollV < 0) {
      this->scrollV = 0;
      this->needsRedraw = true;
   } else if (this->scrollV >= size) {
      this->scrollV = MAX(size - 1, 0);
      this->needsRedraw = true;
   }
   // ensure selection is on screen
   if (this->selected < this->scrollV) {
      this->scrollV = this->selected;
      this->needsRedraw = true;
   } else if (this->selected >= this->scrollV + h) {
      this->scrollV = this->selected - h + 1;
      this->needsRedraw = true;
   }

   int first = this->scrollV;
   int upTo = MIN(first + h, size);

Hisham Muhammad's avatar
Hisham Muhammad committed
292
293
294
295
296
   int highlight = focus
                 ? CRT_colors[PANEL_HIGHLIGHT_FOCUS]
                 : CRT_colors[PANEL_HIGHLIGHT_UNFOCUS];

   if (this->needsRedraw) {
Hisham Muhammad's avatar
Hisham Muhammad committed
297
298
      int line = 0;
      for(int i = first; line < h && i < upTo; i++) {
299
         Object* itemObj = Vector_get(this->items, i);
300
         assert(itemObj); if(!itemObj) continue;
301
         RichString_begin(item);
302
         Object_display(itemObj, &item);
303
304
         int itemLen = RichString_sizeVal(item);
         int amt = MIN(itemLen - scrollH, this->w);
Hisham Muhammad's avatar
Hisham Muhammad committed
305
306
         bool selected = (i == this->selected);
         if (selected) {
Hisham Muhammad's avatar
Hisham Muhammad committed
307
            attrset(highlight);
308
            RichString_setAttr(&item, highlight);
Hisham Muhammad's avatar
Hisham Muhammad committed
309
         }
Hisham Muhammad's avatar
Hisham Muhammad committed
310
         mvhline(y + line, x, ' ', this->w);
Hisham Muhammad's avatar
Hisham Muhammad committed
311
         if (amt > 0)
Hisham Muhammad's avatar
Hisham Muhammad committed
312
            RichString_printoffnVal(item, y + line, x, scrollH, amt);
Hisham Muhammad's avatar
Hisham Muhammad committed
313
314
         if (selected)
            attrset(CRT_colors[RESET_COLOR]);
315
         RichString_end(item);
Hisham Muhammad's avatar
Hisham Muhammad committed
316
317
318
319
320
         line++;
      }
      while (line < h) {
         mvhline(y + line, x, ' ', this->w);
         line++;
Hisham Muhammad's avatar
Hisham Muhammad committed
321
322
323
324
      }
      this->needsRedraw = false;

   } else {
325
      Object* oldObj = Vector_get(this->items, this->oldSelected);
326
      assert(oldObj);
327
      RichString_begin(old);
328
      Object_display(oldObj, &old);
329
      int oldLen = RichString_sizeVal(old);
330
      Object* newObj = Vector_get(this->items, this->selected);
331
      RichString_begin(new);
332
      Object_display(newObj, &new);
333
      int newLen = RichString_sizeVal(new);
Hisham Muhammad's avatar
Hisham Muhammad committed
334
      mvhline(y+ this->oldSelected - first, x+0, ' ', this->w);
335
      if (scrollH < oldLen)
Hisham Muhammad's avatar
Hisham Muhammad committed
336
         RichString_printoffnVal(old, y+this->oldSelected - first, x,
Hisham Muhammad's avatar
Hisham Muhammad committed
337
            scrollH, MIN(oldLen - scrollH, this->w));
Hisham Muhammad's avatar
Hisham Muhammad committed
338
      attrset(highlight);
Hisham Muhammad's avatar
Hisham Muhammad committed
339
      mvhline(y+this->selected - first, x+0, ' ', this->w);
340
341
      RichString_setAttr(&new, highlight);
      if (scrollH < newLen)
Hisham Muhammad's avatar
Hisham Muhammad committed
342
         RichString_printoffnVal(new, y+this->selected - first, x,
Hisham Muhammad's avatar
Hisham Muhammad committed
343
            scrollH, MIN(newLen - scrollH, this->w));
Hisham Muhammad's avatar
Hisham Muhammad committed
344
      attrset(CRT_colors[RESET_COLOR]);
345
346
      RichString_end(new);
      RichString_end(old);
Hisham Muhammad's avatar
Hisham Muhammad committed
347
348
349
350
351
   }
   this->oldSelected = this->selected;
   move(0, 0);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
352
bool Panel_onKey(Panel* this, int key) {
Hisham Muhammad's avatar
Hisham Muhammad committed
353
   assert (this != NULL);
Hisham Muhammad's avatar
Hisham Muhammad committed
354
355
   
   int size = Vector_size(this->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
356
357
   switch (key) {
   case KEY_DOWN:
358
   case KEY_CTRLN:
Hisham Muhammad's avatar
Hisham Muhammad committed
359
360
      this->selected++;
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
361
   case KEY_UP:
362
   case KEY_CTRLP:
Hisham Muhammad's avatar
Hisham Muhammad committed
363
364
      this->selected--;
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
365
366
   #ifdef KEY_C_DOWN
   case KEY_C_DOWN:
Hisham Muhammad's avatar
Hisham Muhammad committed
367
368
      this->selected++;
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
369
370
371
   #endif
   #ifdef KEY_C_UP
   case KEY_C_UP:
Hisham Muhammad's avatar
Hisham Muhammad committed
372
373
      this->selected--;
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
374
   #endif
Hisham Muhammad's avatar
Hisham Muhammad committed
375
   case KEY_LEFT:
376
   case KEY_CTRLB:
Hisham Muhammad's avatar
Hisham Muhammad committed
377
      if (this->scrollH > 0) {
Hisham Muhammad's avatar
Hisham Muhammad committed
378
         this->scrollH -= CRT_scrollHAmount;
Hisham Muhammad's avatar
Hisham Muhammad committed
379
380
         this->needsRedraw = true;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
381
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
382
   case KEY_RIGHT:
383
   case KEY_CTRLF:
Hisham Muhammad's avatar
Hisham Muhammad committed
384
      this->scrollH += CRT_scrollHAmount;
Hisham Muhammad's avatar
Hisham Muhammad committed
385
      this->needsRedraw = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
386
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
387
   case KEY_PPAGE:
Hisham Muhammad's avatar
Hisham Muhammad committed
388
389
390
      this->selected -= (this->h - 1);
      this->scrollV -= (this->h - 1);
      this->needsRedraw = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
391
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
392
   case KEY_NPAGE:
Hisham Muhammad's avatar
Hisham Muhammad committed
393
394
395
      this->selected += (this->h - 1);
      this->scrollV += (this->h - 1);
      this->needsRedraw = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
396
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
397
398
   case KEY_HOME:
      this->selected = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
399
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
400
   case KEY_END:
Hisham Muhammad's avatar
Hisham Muhammad committed
401
402
403
404
405
406
407
408
409
410
411
412
413
      this->selected = size - 1;
      break;
   default:
      return false;
   }

   // ensure selection within bounds
   if (this->selected < 0) {
      this->selected = 0;
      this->needsRedraw = true;
   } else if (this->selected >= size) {   
      this->selected = size - 1;
      this->needsRedraw = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
414
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
415
   return true;
Hisham Muhammad's avatar
Hisham Muhammad committed
416
}
417
418
419
420


HandlerResult Panel_selectByTyping(Panel* this, int ch) {
   int size = Panel_size(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
421
422
423
   if (!this->eventHandlerState)
      this->eventHandlerState = calloc(100, 1);
   char* buffer = this->eventHandlerState;
424
425

   if (isalnum(ch)) {
Hisham Muhammad's avatar
Hisham Muhammad committed
426
      int len = strlen(buffer);
427
      if (len < 99) {
Hisham Muhammad's avatar
Hisham Muhammad committed
428
429
         buffer[len] = ch;
         buffer[len+1] = '\0';
430
431
      }
      for (int try = 0; try < 2; try++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
432
         len = strlen(buffer);
433
434
435
         for (int i = 0; i < size; i++) {
            char* cur = ((ListItem*) Panel_get(this, i))->value;
            while (*cur == ' ') cur++;
Hisham Muhammad's avatar
Hisham Muhammad committed
436
            if (strncasecmp(cur, buffer, len) == 0) {
437
438
439
440
               Panel_setSelected(this, i);
               return HANDLED;
            }
         }
Hisham Muhammad's avatar
Hisham Muhammad committed
441
442
443
444
         // if current word did not match,
         // retry considering the character the start of a new word.
         buffer[0] = ch;
         buffer[1] = '\0';
445
446
447
      }
      return HANDLED;
   } else if (ch != ERR) {
Hisham Muhammad's avatar
Hisham Muhammad committed
448
      buffer[0] = '\0';
449
450
451
452
453
454
   }
   if (ch == 13) {
      return BREAK_LOOP;
   }
   return IGNORED;
}