Panel.c 11.5 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"
27
#include "FunctionBar.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
28

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

typedef enum HandlerResult_ {
32
33
34
   HANDLED     = 0x01,
   IGNORED     = 0x02,
   BREAK_LOOP  = 0x04,
35
36
   REDRAW      = 0x08,
   RESCAN      = 0x10,
37
   SYNTH_KEY   = 0x20,
Hisham Muhammad's avatar
Hisham Muhammad committed
38
39
} HandlerResult;

40
41
42
43
44
#define EVENT_SET_SELECTED -1

#define EVENT_HEADER_CLICK(x_) (-10000 + x_)
#define EVENT_IS_HEADER_CLICK(ev_) (ev_ >= -10000 && ev_ <= -9000)
#define EVENT_HEADER_CLICK_GET_X(ev_) (ev_ + 10000)
45

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

48
49
50
51
52
53
54
55
56
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_)

57
struct Panel_ {
Hisham Muhammad's avatar
Hisham Muhammad committed
58
59
60
   Object super;
   int x, y, w, h;
   WINDOW* window;
61
   Vector* items;
Hisham Muhammad's avatar
Hisham Muhammad committed
62
63
   int selected;
   int oldSelected;
Hisham Muhammad's avatar
Hisham Muhammad committed
64
   void* eventHandlerState;
Hisham Muhammad's avatar
Hisham Muhammad committed
65
66
   int scrollV;
   short scrollH;
Hisham Muhammad's avatar
Hisham Muhammad committed
67
   bool needsRedraw;
68
69
   FunctionBar* currentBar;
   FunctionBar* defaultBar;
Hisham Muhammad's avatar
Hisham Muhammad committed
70
71
72
   RichString header;
};

73
74
#define Panel_setDefaultBar(this_) do{ (this_)->currentBar = (this_)->defaultBar; }while(0)

Hisham Muhammad's avatar
Hisham Muhammad committed
75
76
77
78
79
80
81
82
83
}*/

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

84
85
86
87
#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
88

89
90
91
92
93
PanelClass Panel_class = {
   .super = {
      .extends = Class(Object),
      .delete = Panel_delete
   },
94
   .eventHandler = Panel_selectByTyping,
95
96
};

97
Panel* Panel_new(int x, int y, int w, int h, bool owner, ObjectClass* type, FunctionBar* fuBar) {
98
99
   Panel* this;
   this = malloc(sizeof(Panel));
100
   Object_setClass(this, Class(Panel));
101
   Panel_init(this, x, y, w, h, type, owner, fuBar);
Hisham Muhammad's avatar
Hisham Muhammad committed
102
103
104
   return this;
}

105
106
107
void Panel_delete(Object* cast) {
   Panel* this = (Panel*)cast;
   Panel_done(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
108
109
110
   free(this);
}

111
void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool owner, FunctionBar* fuBar) {
Hisham Muhammad's avatar
Hisham Muhammad committed
112
113
114
115
   this->x = x;
   this->y = y;
   this->w = w;
   this->h = h;
Hisham Muhammad's avatar
Hisham Muhammad committed
116
   this->eventHandlerState = NULL;
117
   this->items = Vector_new(type, owner, DEFAULT_SIZE);
Hisham Muhammad's avatar
Hisham Muhammad committed
118
119
120
121
122
   this->scrollV = 0;
   this->scrollH = 0;
   this->selected = 0;
   this->oldSelected = 0;
   this->needsRedraw = true;
123
   RichString_beginAllocated(this->header);
124
125
   this->defaultBar = fuBar;
   this->currentBar = fuBar;
Hisham Muhammad's avatar
Hisham Muhammad committed
126
127
}

128
void Panel_done(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
129
   assert (this != NULL);
Hisham Muhammad's avatar
Hisham Muhammad committed
130
   free(this->eventHandlerState);
131
   Vector_delete(this->items);
132
   FunctionBar_delete(this->defaultBar);
133
   RichString_end(this->header);
Hisham Muhammad's avatar
Hisham Muhammad committed
134
135
}

136
RichString* Panel_getHeader(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
137
138
139
   assert (this != NULL);

   this->needsRedraw = true;
140
   return &(this->header);
Hisham Muhammad's avatar
Hisham Muhammad committed
141
142
}

Hisham Muhammad's avatar
Hisham Muhammad committed
143
inline void Panel_setHeader(Panel* this, const char* header) {
144
145
   RichString_write(&(this->header), CRT_colors[PANEL_HEADER_FOCUS], header);
   this->needsRedraw = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
146
147
}

148
void Panel_move(Panel* this, int x, int y) {
Hisham Muhammad's avatar
Hisham Muhammad committed
149
150
151
152
153
154
155
   assert (this != NULL);

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

156
void Panel_resize(Panel* this, int w, int h) {
Hisham Muhammad's avatar
Hisham Muhammad committed
157
158
   assert (this != NULL);

159
   if (RichString_sizeVal(this->header) > 0)
Hisham Muhammad's avatar
Hisham Muhammad committed
160
161
162
163
164
165
      h--;
   this->w = w;
   this->h = h;
   this->needsRedraw = true;
}

166
void Panel_prune(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
167
168
   assert (this != NULL);

169
   Vector_prune(this->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
170
171
172
173
174
175
   this->scrollV = 0;
   this->selected = 0;
   this->oldSelected = 0;
   this->needsRedraw = true;
}

176
void Panel_add(Panel* this, Object* o) {
Hisham Muhammad's avatar
Hisham Muhammad committed
177
178
   assert (this != NULL);

179
   Vector_add(this->items, o);
Hisham Muhammad's avatar
Hisham Muhammad committed
180
181
182
   this->needsRedraw = true;
}

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

186
   Vector_insert(this->items, i, o);
Hisham Muhammad's avatar
Hisham Muhammad committed
187
188
189
   this->needsRedraw = true;
}

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

193
   Vector_set(this->items, i, o);
Hisham Muhammad's avatar
Hisham Muhammad committed
194
195
}

196
Object* Panel_get(Panel* this, int i) {
Hisham Muhammad's avatar
Hisham Muhammad committed
197
198
   assert (this != NULL);

199
   return Vector_get(this->items, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
200
201
}

202
Object* Panel_remove(Panel* this, int i) {
Hisham Muhammad's avatar
Hisham Muhammad committed
203
204
205
   assert (this != NULL);

   this->needsRedraw = true;
206
207
   Object* removed = Vector_remove(this->items, i);
   if (this->selected > 0 && this->selected >= Vector_size(this->items))
Hisham Muhammad's avatar
Hisham Muhammad committed
208
209
210
211
      this->selected--;
   return removed;
}

212
Object* Panel_getSelected(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
213
   assert (this != NULL);
214
215
216
217
   if (Vector_size(this->items) > 0)
      return Vector_get(this->items, this->selected);
   else
      return NULL;
Hisham Muhammad's avatar
Hisham Muhammad committed
218
219
}

220
void Panel_moveSelectedUp(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
221
222
   assert (this != NULL);

223
   Vector_moveUp(this->items, this->selected);
Hisham Muhammad's avatar
Hisham Muhammad committed
224
225
226
227
   if (this->selected > 0)
      this->selected--;
}

228
void Panel_moveSelectedDown(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
229
230
   assert (this != NULL);

231
232
   Vector_moveDown(this->items, this->selected);
   if (this->selected + 1 < Vector_size(this->items))
Hisham Muhammad's avatar
Hisham Muhammad committed
233
234
235
      this->selected++;
}

236
int Panel_getSelectedIndex(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
237
238
239
240
241
   assert (this != NULL);

   return this->selected;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
242
int Panel_size(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
243
244
   assert (this != NULL);

245
   return Vector_size(this->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
246
247
}

248
void Panel_setSelected(Panel* this, int selected) {
Hisham Muhammad's avatar
Hisham Muhammad committed
249
250
   assert (this != NULL);

251
252
253
   selected = MIN(Vector_size(this->items) - 1, selected);
   if (selected < 0)
      selected = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
254
   this->selected = selected;
255
   if (Panel_eventHandlerFn(this)) {
256
      Panel_eventHandler(this, EVENT_SET_SELECTED);
257
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
258
259
}

260
void Panel_draw(Panel* this, bool focus) {
Hisham Muhammad's avatar
Hisham Muhammad committed
261
262
   assert (this != NULL);

Hisham Muhammad's avatar
Hisham Muhammad committed
263
   int size = Vector_size(this->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
264
   int scrollH = this->scrollH;
Hisham Muhammad's avatar
Hisham Muhammad committed
265
266
267
   int y = this->y;
   int x = this->x;
   int h = this->h;
Hisham Muhammad's avatar
Hisham Muhammad committed
268

269
270
   int headerLen = RichString_sizeVal(this->header);
   if (headerLen > 0) {
Hisham Muhammad's avatar
Hisham Muhammad committed
271
272
273
274
275
      int attr = focus
               ? CRT_colors[PANEL_HEADER_FOCUS]
               : CRT_colors[PANEL_HEADER_UNFOCUS];
      attrset(attr);
      mvhline(y, x, ' ', this->w);
276
      if (scrollH < headerLen) {
277
         RichString_printoffnVal(this->header, y, x, scrollH,
278
            MIN(headerLen - scrollH, this->w));
Hisham Muhammad's avatar
Hisham Muhammad committed
279
280
281
282
      }
      attrset(CRT_colors[RESET_COLOR]);
      y++;
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

   // 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
304
305
306
307
308
   int highlight = focus
                 ? CRT_colors[PANEL_HIGHLIGHT_FOCUS]
                 : CRT_colors[PANEL_HIGHLIGHT_UNFOCUS];

   if (this->needsRedraw) {
Hisham Muhammad's avatar
Hisham Muhammad committed
309
310
      int line = 0;
      for(int i = first; line < h && i < upTo; i++) {
311
         Object* itemObj = Vector_get(this->items, i);
312
         assert(itemObj); if(!itemObj) continue;
313
         RichString_begin(item);
314
         Object_display(itemObj, &item);
315
316
         int itemLen = RichString_sizeVal(item);
         int amt = MIN(itemLen - scrollH, this->w);
Hisham Muhammad's avatar
Hisham Muhammad committed
317
318
         bool selected = (i == this->selected);
         if (selected) {
Hisham Muhammad's avatar
Hisham Muhammad committed
319
            attrset(highlight);
320
            RichString_setAttr(&item, highlight);
Hisham Muhammad's avatar
Hisham Muhammad committed
321
         }
Hisham Muhammad's avatar
Hisham Muhammad committed
322
         mvhline(y + line, x, ' ', this->w);
Hisham Muhammad's avatar
Hisham Muhammad committed
323
         if (amt > 0)
Hisham Muhammad's avatar
Hisham Muhammad committed
324
            RichString_printoffnVal(item, y + line, x, scrollH, amt);
Hisham Muhammad's avatar
Hisham Muhammad committed
325
326
         if (selected)
            attrset(CRT_colors[RESET_COLOR]);
327
         RichString_end(item);
Hisham Muhammad's avatar
Hisham Muhammad committed
328
329
330
331
332
         line++;
      }
      while (line < h) {
         mvhline(y + line, x, ' ', this->w);
         line++;
Hisham Muhammad's avatar
Hisham Muhammad committed
333
334
335
336
      }
      this->needsRedraw = false;

   } else {
337
      Object* oldObj = Vector_get(this->items, this->oldSelected);
338
      assert(oldObj);
339
      RichString_begin(old);
340
      Object_display(oldObj, &old);
341
      int oldLen = RichString_sizeVal(old);
342
      Object* newObj = Vector_get(this->items, this->selected);
343
      RichString_begin(new);
344
      Object_display(newObj, &new);
345
      int newLen = RichString_sizeVal(new);
Hisham Muhammad's avatar
Hisham Muhammad committed
346
      mvhline(y+ this->oldSelected - first, x+0, ' ', this->w);
347
      if (scrollH < oldLen)
Hisham Muhammad's avatar
Hisham Muhammad committed
348
         RichString_printoffnVal(old, y+this->oldSelected - first, x,
Hisham Muhammad's avatar
Hisham Muhammad committed
349
            scrollH, MIN(oldLen - scrollH, this->w));
Hisham Muhammad's avatar
Hisham Muhammad committed
350
      attrset(highlight);
Hisham Muhammad's avatar
Hisham Muhammad committed
351
      mvhline(y+this->selected - first, x+0, ' ', this->w);
352
353
      RichString_setAttr(&new, highlight);
      if (scrollH < newLen)
Hisham Muhammad's avatar
Hisham Muhammad committed
354
         RichString_printoffnVal(new, y+this->selected - first, x,
Hisham Muhammad's avatar
Hisham Muhammad committed
355
            scrollH, MIN(newLen - scrollH, this->w));
Hisham Muhammad's avatar
Hisham Muhammad committed
356
      attrset(CRT_colors[RESET_COLOR]);
357
358
      RichString_end(new);
      RichString_end(old);
Hisham Muhammad's avatar
Hisham Muhammad committed
359
360
361
362
363
   }
   this->oldSelected = this->selected;
   move(0, 0);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
364
bool Panel_onKey(Panel* this, int key) {
Hisham Muhammad's avatar
Hisham Muhammad committed
365
   assert (this != NULL);
Hisham Muhammad's avatar
Hisham Muhammad committed
366
367
   
   int size = Vector_size(this->items);
Hisham Muhammad's avatar
Hisham Muhammad committed
368
369
   switch (key) {
   case KEY_DOWN:
370
   case KEY_CTRLN:
Hisham Muhammad's avatar
Hisham Muhammad committed
371
372
      this->selected++;
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
373
   case KEY_UP:
374
   case KEY_CTRLP:
Hisham Muhammad's avatar
Hisham Muhammad committed
375
376
      this->selected--;
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
377
378
   #ifdef KEY_C_DOWN
   case KEY_C_DOWN:
Hisham Muhammad's avatar
Hisham Muhammad committed
379
380
      this->selected++;
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
381
382
383
   #endif
   #ifdef KEY_C_UP
   case KEY_C_UP:
Hisham Muhammad's avatar
Hisham Muhammad committed
384
385
      this->selected--;
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
386
   #endif
Hisham Muhammad's avatar
Hisham Muhammad committed
387
   case KEY_LEFT:
388
   case KEY_CTRLB:
Hisham Muhammad's avatar
Hisham Muhammad committed
389
      if (this->scrollH > 0) {
Hisham Muhammad's avatar
Hisham Muhammad committed
390
         this->scrollH -= CRT_scrollHAmount;
Hisham Muhammad's avatar
Hisham Muhammad committed
391
392
         this->needsRedraw = true;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
393
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
394
   case KEY_RIGHT:
395
   case KEY_CTRLF:
Hisham Muhammad's avatar
Hisham Muhammad committed
396
      this->scrollH += CRT_scrollHAmount;
Hisham Muhammad's avatar
Hisham Muhammad committed
397
      this->needsRedraw = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
398
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
399
   case KEY_PPAGE:
Hisham Muhammad's avatar
Hisham Muhammad committed
400
401
402
      this->selected -= (this->h - 1);
      this->scrollV -= (this->h - 1);
      this->needsRedraw = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
403
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
404
   case KEY_NPAGE:
Hisham Muhammad's avatar
Hisham Muhammad committed
405
406
407
      this->selected += (this->h - 1);
      this->scrollV += (this->h - 1);
      this->needsRedraw = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
408
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
409
410
   case KEY_HOME:
      this->selected = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
411
      break;
Hisham Muhammad's avatar
Hisham Muhammad committed
412
   case KEY_END:
Hisham Muhammad's avatar
Hisham Muhammad committed
413
414
415
416
417
418
419
420
421
422
423
424
425
      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
426
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
427
   return true;
Hisham Muhammad's avatar
Hisham Muhammad committed
428
}
429
430
431
432


HandlerResult Panel_selectByTyping(Panel* this, int ch) {
   int size = Panel_size(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
433
434
435
   if (!this->eventHandlerState)
      this->eventHandlerState = calloc(100, 1);
   char* buffer = this->eventHandlerState;
436

437
   if (ch < 255 && isalnum(ch)) {
Hisham Muhammad's avatar
Hisham Muhammad committed
438
      int len = strlen(buffer);
439
      if (len < 99) {
Hisham Muhammad's avatar
Hisham Muhammad committed
440
441
         buffer[len] = ch;
         buffer[len+1] = '\0';
442
443
      }
      for (int try = 0; try < 2; try++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
444
         len = strlen(buffer);
445
446
447
         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
448
            if (strncasecmp(cur, buffer, len) == 0) {
449
450
451
452
               Panel_setSelected(this, i);
               return HANDLED;
            }
         }
Hisham Muhammad's avatar
Hisham Muhammad committed
453
454
455
456
         // if current word did not match,
         // retry considering the character the start of a new word.
         buffer[0] = ch;
         buffer[1] = '\0';
457
458
459
      }
      return HANDLED;
   } else if (ch != ERR) {
Hisham Muhammad's avatar
Hisham Muhammad committed
460
      buffer[0] = '\0';
461
462
463
464
465
466
   }
   if (ch == 13) {
      return BREAK_LOOP;
   }
   return IGNORED;
}