ColumnsPanel.c 3.6 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"
9
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
10

Hisham Muhammad's avatar
Hisham Muhammad committed
11
#include "String.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12
#include "ListItem.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
18

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
19
20
#include "Panel.h"
#include "Settings.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

   Settings* settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
26
   bool moving;
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
   ColumnsPanel* const this = (ColumnsPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
40
   
41
   int selected = Panel_getSelectedIndex(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
42
   HandlerResult result = IGNORED;
Hisham Muhammad's avatar
Hisham Muhammad committed
43
   int size = Panel_size(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
44
45

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

113
114
115
116
117
118
119
120
PanelClass ColumnsPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = ColumnsPanel_delete
   },
   .eventHandler = ColumnsPanel_eventHandler
};

Hisham Muhammad's avatar
Hisham Muhammad committed
121
ColumnsPanel* ColumnsPanel_new(Settings* settings) {
122
   ColumnsPanel* this = AllocThis(ColumnsPanel);
123
   Panel* super = (Panel*) this;
124
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);
125
126

   this->settings = settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
127
   this->moving = false;
128
129
   Panel_setHeader(super, "Active Columns");

Hisham Muhammad's avatar
Hisham Muhammad committed
130
   ProcessField* fields = this->settings->fields;
131
   for (; *fields; fields++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
132
      Panel_add(super, (Object*) ListItem_new(Process_fields[*fields].name, *fields));
133
134
135
136
137
   }
   return this;
}

int ColumnsPanel_fieldNameToIndex(const char* name) {
138
   for (int j = 1; j <= Platform_numberOfFields; j++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
139
      if (String_eq(name, Process_fields[j].name)) {
140
141
142
         return j;
      }
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
143
   return -1;
144
145
146
147
}

void ColumnsPanel_update(Panel* super) {
   ColumnsPanel* this = (ColumnsPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
148
   int size = Panel_size(super);
149
   this->settings->changed = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
150
151
   this->settings->fields = realloc(this->settings->fields, sizeof(ProcessField) * (size+1));
   this->settings->flags = 0;
152
   for (int i = 0; i < size; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
153
154
155
      int key = ((ListItem*) Panel_get(super, i))->key;
      this->settings->fields[i] = key;
      this->settings->flags |= Process_fields[key].flags;
156
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
157
   this->settings->fields[size] = 0;
158
159
}