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

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

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

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

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

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

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