ColumnsPanel.c 3.93 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

David Hunt's avatar
David Hunt committed
11
#include "StringUtils.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12
#include "ListItem.h"
13
#include "CRT.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
14

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

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

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

   Settings* settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
27
   bool moving;
28
} ColumnsPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
29
30
31

}*/

32
33
static const char* ColumnsFunctions[] = {"      ", "      ", "      ", "      ", "      ", "      ", "MoveUp", "MoveDn", "Remove", "Done  ", NULL};

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

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

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

117
118
119
120
121
122
123
124
PanelClass ColumnsPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = ColumnsPanel_delete
   },
   .eventHandler = ColumnsPanel_eventHandler
};

Hisham Muhammad's avatar
Hisham Muhammad committed
125
ColumnsPanel* ColumnsPanel_new(Settings* settings) {
126
   ColumnsPanel* this = AllocThis(ColumnsPanel);
127
   Panel* super = (Panel*) this;
128
129
   FunctionBar* fuBar = FunctionBar_new(ColumnsFunctions, NULL, NULL);
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);
130
131

   this->settings = settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
132
   this->moving = false;
133
134
   Panel_setHeader(super, "Active Columns");

Hisham Muhammad's avatar
Hisham Muhammad committed
135
   ProcessField* fields = this->settings->fields;
136
   for (; *fields; fields++) {
137
138
139
      if (Process_fields[*fields].name) {
         Panel_add(super, (Object*) ListItem_new(Process_fields[*fields].name, *fields));
      }
140
141
142
143
144
   }
   return this;
}

int ColumnsPanel_fieldNameToIndex(const char* name) {
145
   for (int j = 1; j <= Platform_numberOfFields; j++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
146
      if (String_eq(name, Process_fields[j].name)) {
147
148
149
         return j;
      }
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
150
   return -1;
151
152
153
154
}

void ColumnsPanel_update(Panel* super) {
   ColumnsPanel* this = (ColumnsPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
155
   int size = Panel_size(super);
156
   this->settings->changed = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
157
158
   this->settings->fields = realloc(this->settings->fields, sizeof(ProcessField) * (size+1));
   this->settings->flags = 0;
159
   for (int i = 0; i < size; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
160
161
162
      int key = ((ListItem*) Panel_get(super, i))->key;
      this->settings->fields[i] = key;
      this->settings->flags |= Process_fields[key].flags;
163
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
164
   this->settings->fields[size] = 0;
165
166
}