AvailableColumnsPanel.c 2.23 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"
Hisham Muhammad's avatar
Hisham Muhammad committed
9

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

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

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

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

}*/

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

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

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

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

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

   Panel_setHeader(super, "Available Columns");

   for (int i = 1; i < LAST_PROCESSFIELD; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
78
79
80
81
82
      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));
      }
83
84
85
86
   }
   this->columns = columns;
   return this;
}