AvailableColumnsPanel.c 2.26 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
/*
htop - AvailableColumnsPanel.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 "AvailableColumnsPanel.h"
9
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
10

Hisham Muhammad's avatar
Hisham Muhammad committed
11
#include "Header.h"
12
#include "ColumnsPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13

Hisham Muhammad's avatar
Hisham Muhammad committed
14
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
15
16
#include <stdlib.h>
#include <ctype.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
17
#include <string.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
18
19

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
20
#include "Panel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
21

22
23
24
25
typedef struct AvailableColumnsPanel_ {
   Panel super;
   Panel* columns;
} AvailableColumnsPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
28

}*/

29
static void AvailableColumnsPanel_delete(Object* object) {
30
31
32
   Panel* super = (Panel*) object;
   AvailableColumnsPanel* this = (AvailableColumnsPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
33
34
35
   free(this);
}

36
static HandlerResult AvailableColumnsPanel_eventHandler(Panel* super, int ch) {
37
   AvailableColumnsPanel* this = (AvailableColumnsPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
38
   int key = ((ListItem*) Panel_getSelected(super))->key;
Hisham Muhammad's avatar
Hisham Muhammad committed
39
40
41
42
43
44
45
   HandlerResult result = IGNORED;

   switch(ch) {
      case 13:
      case KEY_ENTER:
      case KEY_F(5):
      {
46
         int at = Panel_getSelectedIndex(this->columns);
Hisham Muhammad's avatar
Hisham Muhammad committed
47
         Panel_insert(this->columns, at, (Object*) ListItem_new(Process_fields[key].name, key));
48
         Panel_setSelected(this->columns, at+1);
49
         ColumnsPanel_update(this->columns);
Hisham Muhammad's avatar
Hisham Muhammad committed
50
51
52
         result = HANDLED;
         break;
      }
53
54
      default:
      {
Hisham Muhammad's avatar
Hisham Muhammad committed
55
56
         if (isalpha(ch))
            result = Panel_selectByTyping(super, ch);
57
58
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
59
60
61
   }
   return result;
}
62

63
64
65
66
67
68
69
70
PanelClass AvailableColumnsPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = AvailableColumnsPanel_delete
   },
   .eventHandler = AvailableColumnsPanel_eventHandler
};

Hisham Muhammad's avatar
Hisham Muhammad committed
71
AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns) {
72
   AvailableColumnsPanel* this = AllocThis(AvailableColumnsPanel);
73
   Panel* super = (Panel*) this;
74
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);
75
76
77

   Panel_setHeader(super, "Available Columns");

78
   for (int i = 1; i < Platform_numberOfFields; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
79
80
81
82
83
      if (i != COMM && Process_fields[i].description) {
         char description[256];
         snprintf(description, sizeof(description), "%s - %s", Process_fields[i].name, Process_fields[i].description);
         Panel_add(super, (Object*) ListItem_new(description, i));
      }
84
85
86
87
   }
   this->columns = columns;
   return this;
}