ColumnsPanel.c 3.04 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
/*
htop - ColumnsPanel.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 "ColumnsPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9

Hisham Muhammad's avatar
Hisham Muhammad committed
10
#include "String.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
11
#include "debug.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
17

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

22
23
typedef struct ColumnsPanel_ {
   Panel super;
Hisham Muhammad's avatar
Hisham Muhammad committed
24
25
26

   Settings* settings;
   ScreenManager* scr;
27
} ColumnsPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
28
29
30

}*/

31
static void ColumnsPanel_delete(Object* object) {
32
33
34
   Panel* super = (Panel*) object;
   ColumnsPanel* this = (ColumnsPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
35
36
37
   free(this);
}

38
static HandlerResult ColumnsPanel_eventHandler(Panel* super, int ch) {
Hisham Muhammad's avatar
Hisham Muhammad committed
39
   
40
   int selected = Panel_getSelectedIndex(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
41
   HandlerResult result = IGNORED;
Hisham Muhammad's avatar
Hisham Muhammad committed
42
   int size = Panel_size(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
43
44
45
46
47
48
49

   switch(ch) {
      case KEY_F(7):
      case '[':
      case '-':
      {
         if (selected < size - 1)
50
            Panel_moveSelectedUp(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
51
52
53
54
55
56
57
         result = HANDLED;
         break;
      }
      case KEY_F(8):
      case ']':
      case '+':
      {
58
         if (selected < size - 2) 
59
            Panel_moveSelectedDown(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
60
61
62
63
64
65
66
         result = HANDLED;
         break;
      }
      case KEY_F(9):
      case KEY_DC:
      {
         if (selected < size - 1) {
67
            Panel_remove(super, selected);
Hisham Muhammad's avatar
Hisham Muhammad committed
68
69
70
71
         }
         result = HANDLED;
         break;
      }
72
73
      default:
      {
Hisham Muhammad's avatar
Hisham Muhammad committed
74
75
         if (isalpha(ch))
            result = Panel_selectByTyping(super, ch);
76
77
78
79
         if (result == BREAK_LOOP)
            result = IGNORED;
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
80
81
   }
   if (result == HANDLED)
82
      ColumnsPanel_update(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
83
84
   return result;
}
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

ColumnsPanel* ColumnsPanel_new(Settings* settings, ScreenManager* scr) {
   ColumnsPanel* this = (ColumnsPanel*) malloc(sizeof(ColumnsPanel));
   Panel* super = (Panel*) this;
   Panel_init(super, 1, 1, 1, 1, LISTITEM_CLASS, true);
   ((Object*)this)->delete = ColumnsPanel_delete;

   this->settings = settings;
   this->scr = scr;
   super->eventHandler = ColumnsPanel_eventHandler;
   Panel_setHeader(super, "Active Columns");

   ProcessField* fields = this->settings->pl->fields;
   for (; *fields; fields++) {
      Panel_add(super, (Object*) ListItem_new(Process_fieldNames[*fields], 0));
   }
   return this;
}

int ColumnsPanel_fieldNameToIndex(const char* name) {
   for (int j = 1; j <= LAST_PROCESSFIELD; j++) {
      if (String_eq(name, Process_fieldNames[j])) {
         return j;
      }
   }
   return 0;
}

void ColumnsPanel_update(Panel* super) {
   ColumnsPanel* this = (ColumnsPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
115
   int size = Panel_size(super);
116
117
118
119
120
121
122
123
124
125
126
127
128
   this->settings->changed = true;
   // FIXME: this is crappily inefficient
   free(this->settings->pl->fields);
   this->settings->pl->fields = (ProcessField*) malloc(sizeof(ProcessField) * (size+1));
   for (int i = 0; i < size; i++) {
      char* text = ((ListItem*) Panel_get(super, i))->value;
          int j = ColumnsPanel_fieldNameToIndex(text);
          if (j > 0)
             this->settings->pl->fields[i] = j;
   }
   this->settings->pl->fields[size] = 0;
}