ColumnsPanel.c 3.18 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
11
#include "String.h"

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

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

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

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

}*/

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

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

   switch(ch) {
      case KEY_F(7):
      case '[':
      case '-':
      {
         if (selected < size - 1)
49
            Panel_moveSelectedUp(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
50
51
52
53
54
55
56
         result = HANDLED;
         break;
      }
      case KEY_F(8):
      case ']':
      case '+':
      {
57
         if (selected < size - 2) 
58
            Panel_moveSelectedDown(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
59
60
61
62
63
64
65
         result = HANDLED;
         break;
      }
      case KEY_F(9):
      case KEY_DC:
      {
         if (selected < size - 1) {
66
            Panel_remove(super, selected);
Hisham Muhammad's avatar
Hisham Muhammad committed
67
68
69
70
         }
         result = HANDLED;
         break;
      }
71
72
      default:
      {
Hisham Muhammad's avatar
Hisham Muhammad committed
73
74
         if (isalpha(ch))
            result = Panel_selectByTyping(super, ch);
75
76
77
78
         if (result == BREAK_LOOP)
            result = IGNORED;
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
79
80
   }
   if (result == HANDLED)
81
      ColumnsPanel_update(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
82
83
   return result;
}
84

85
86
87
88
89
90
91
92
PanelClass ColumnsPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = ColumnsPanel_delete
   },
   .eventHandler = ColumnsPanel_eventHandler
};

93
ColumnsPanel* ColumnsPanel_new(Settings* settings, ScreenManager* scr) {
94
   ColumnsPanel* this = AllocThis(ColumnsPanel);
95
   Panel* super = (Panel*) this;
96
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

   this->settings = settings;
   this->scr = scr;
   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
120
   int size = Panel_size(super);
121
122
123
124
   this->settings->changed = true;
   // FIXME: this is crappily inefficient
   free(this->settings->pl->fields);
   this->settings->pl->fields = (ProcessField*) malloc(sizeof(ProcessField) * (size+1));
125
   this->settings->pl->flags = 0;
126
127
128
   for (int i = 0; i < size; i++) {
      char* text = ((ListItem*) Panel_get(super, i))->value;
          int j = ColumnsPanel_fieldNameToIndex(text);
129
          if (j > 0) {
130
             this->settings->pl->fields[i] = j;
131
132
             this->settings->pl->flags |= Process_fieldFlags[j];
          }
133
134
135
136
   }
   this->settings->pl->fields[size] = 0;
}