~ubuntu-branches/ubuntu/intrepid/htop/intrepid

« back to all changes in this revision

Viewing changes to Panel.c

  • Committer: Bazaar Package Importer
  • Author(s): Bartosz Fenski
  • Date: 2006-08-14 13:03:15 UTC
  • mfrom: (1.1.6 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20060814130315-mm00h2owyqz2zr2j
Tags: 0.6.3-1
* New upstream version.
  - allows to rekill tagged group of processes (Closes: #375219)
* Bumped Standards-Version to 3.7.2 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
htop - Panel.c
 
3
(C) 2004-2006 Hisham H. Muhammad
 
4
Released under the GNU GPL, see the COPYING file
 
5
in the source distribution for its full text.
 
6
*/
 
7
 
 
8
#include "Object.h"
 
9
#include "Panel.h"
 
10
#include "Vector.h"
 
11
#include "CRT.h"
 
12
#include "RichString.h"
 
13
#include "ListItem.h"
 
14
 
 
15
#include <math.h>
 
16
#include <stdbool.h>
 
17
 
 
18
#include "debug.h"
 
19
#include <assert.h>
 
20
 
 
21
#include <curses.h>
 
22
//#link curses
 
23
 
 
24
/*{
 
25
 
 
26
typedef struct Panel_ Panel;
 
27
 
 
28
typedef enum HandlerResult_ {
 
29
   HANDLED,
 
30
   IGNORED,
 
31
   BREAK_LOOP
 
32
} HandlerResult;
 
33
 
 
34
typedef HandlerResult(*Panel_EventHandler)(Panel*, int);
 
35
 
 
36
struct Panel_ {
 
37
   Object super;
 
38
   int x, y, w, h;
 
39
   WINDOW* window;
 
40
   Vector* items;
 
41
   int selected;
 
42
   int scrollV, scrollH;
 
43
   int scrollHAmount;
 
44
   int oldSelected;
 
45
   bool needsRedraw;
 
46
   RichString header;
 
47
   Panel_EventHandler eventHandler;
 
48
};
 
49
 
 
50
}*/
 
51
 
 
52
#ifndef MIN
 
53
#define MIN(a,b) ((a)<(b)?(a):(b))
 
54
#endif
 
55
#ifndef MAX
 
56
#define MAX(a,b) ((a)>(b)?(a):(b))
 
57
#endif
 
58
 
 
59
#ifdef DEBUG
 
60
char* PANEL_CLASS = "Panel";
 
61
#else
 
62
#define PANEL_CLASS NULL
 
63
#endif
 
64
 
 
65
 
 
66
Panel* Panel_new(int x, int y, int w, int h, char* type, bool owner, Object_Compare compare) {
 
67
   Panel* this;
 
68
   this = malloc(sizeof(Panel));
 
69
   Panel_init(this, x, y, w, h, type, owner);
 
70
   this->items->compare = compare;
 
71
   return this;
 
72
}
 
73
 
 
74
void Panel_delete(Object* cast) {
 
75
   Panel* this = (Panel*)cast;
 
76
   Panel_done(this);
 
77
   free(this);
 
78
}
 
79
 
 
80
void Panel_init(Panel* this, int x, int y, int w, int h, char* type, bool owner) {
 
81
   Object* super = (Object*) this;
 
82
   Object_setClass(this, PANEL_CLASS);
 
83
   super->delete = Panel_delete;
 
84
   this->x = x;
 
85
   this->y = y;
 
86
   this->w = w;
 
87
   this->h = h;
 
88
   this->eventHandler = NULL;
 
89
   this->items = Vector_new(type, owner, DEFAULT_SIZE, ListItem_compare);
 
90
   this->scrollV = 0;
 
91
   this->scrollH = 0;
 
92
   this->selected = 0;
 
93
   this->oldSelected = 0;
 
94
   this->needsRedraw = true;
 
95
   this->header.len = 0;
 
96
   if (String_eq(CRT_termType, "linux"))
 
97
      this->scrollHAmount = 40;
 
98
   else
 
99
      this->scrollHAmount = 5;
 
100
}
 
101
 
 
102
void Panel_done(Panel* this) {
 
103
   assert (this != NULL);
 
104
   Vector_delete(this->items);
 
105
}
 
106
 
 
107
inline void Panel_setRichHeader(Panel* this, RichString header) {
 
108
   assert (this != NULL);
 
109
 
 
110
   this->header = header;
 
111
   this->needsRedraw = true;
 
112
}
 
113
 
 
114
inline void Panel_setHeader(Panel* this, char* header) {
 
115
   Panel_setRichHeader(this, RichString_quickString(CRT_colors[PANEL_HEADER_FOCUS], header));
 
116
}
 
117
 
 
118
void Panel_setEventHandler(Panel* this, Panel_EventHandler eh) {
 
119
   this->eventHandler = eh;
 
120
}
 
121
 
 
122
void Panel_move(Panel* this, int x, int y) {
 
123
   assert (this != NULL);
 
124
 
 
125
   this->x = x;
 
126
   this->y = y;
 
127
   this->needsRedraw = true;
 
128
}
 
129
 
 
130
void Panel_resize(Panel* this, int w, int h) {
 
131
   assert (this != NULL);
 
132
 
 
133
   if (this->header.len > 0)
 
134
      h--;
 
135
   this->w = w;
 
136
   this->h = h;
 
137
   this->needsRedraw = true;
 
138
}
 
139
 
 
140
void Panel_prune(Panel* this) {
 
141
   assert (this != NULL);
 
142
 
 
143
   Vector_prune(this->items);
 
144
   this->scrollV = 0;
 
145
   this->selected = 0;
 
146
   this->oldSelected = 0;
 
147
   this->needsRedraw = true;
 
148
}
 
149
 
 
150
void Panel_add(Panel* this, Object* o) {
 
151
   assert (this != NULL);
 
152
 
 
153
   Vector_add(this->items, o);
 
154
   this->needsRedraw = true;
 
155
}
 
156
 
 
157
void Panel_insert(Panel* this, int i, Object* o) {
 
158
   assert (this != NULL);
 
159
 
 
160
   Vector_insert(this->items, i, o);
 
161
   this->needsRedraw = true;
 
162
}
 
163
 
 
164
void Panel_set(Panel* this, int i, Object* o) {
 
165
   assert (this != NULL);
 
166
 
 
167
   Vector_set(this->items, i, o);
 
168
}
 
169
 
 
170
Object* Panel_get(Panel* this, int i) {
 
171
   assert (this != NULL);
 
172
 
 
173
   return Vector_get(this->items, i);
 
174
}
 
175
 
 
176
Object* Panel_remove(Panel* this, int i) {
 
177
   assert (this != NULL);
 
178
 
 
179
   this->needsRedraw = true;
 
180
   Object* removed = Vector_remove(this->items, i);
 
181
   if (this->selected > 0 && this->selected >= Vector_size(this->items))
 
182
      this->selected--;
 
183
   return removed;
 
184
}
 
185
 
 
186
Object* Panel_getSelected(Panel* this) {
 
187
   assert (this != NULL);
 
188
 
 
189
   return Vector_get(this->items, this->selected);
 
190
}
 
191
 
 
192
void Panel_moveSelectedUp(Panel* this) {
 
193
   assert (this != NULL);
 
194
 
 
195
   Vector_moveUp(this->items, this->selected);
 
196
   if (this->selected > 0)
 
197
      this->selected--;
 
198
}
 
199
 
 
200
void Panel_moveSelectedDown(Panel* this) {
 
201
   assert (this != NULL);
 
202
 
 
203
   Vector_moveDown(this->items, this->selected);
 
204
   if (this->selected + 1 < Vector_size(this->items))
 
205
      this->selected++;
 
206
}
 
207
 
 
208
int Panel_getSelectedIndex(Panel* this) {
 
209
   assert (this != NULL);
 
210
 
 
211
   return this->selected;
 
212
}
 
213
 
 
214
int Panel_getSize(Panel* this) {
 
215
   assert (this != NULL);
 
216
 
 
217
   return Vector_size(this->items);
 
218
}
 
219
 
 
220
void Panel_setSelected(Panel* this, int selected) {
 
221
   assert (this != NULL);
 
222
 
 
223
   selected = MAX(0, MIN(Vector_size(this->items) - 1, selected));
 
224
   this->selected = selected;
 
225
}
 
226
 
 
227
void Panel_draw(Panel* this, bool focus) {
 
228
   assert (this != NULL);
 
229
 
 
230
   int first, last;
 
231
   int itemCount = Vector_size(this->items);
 
232
   int scrollH = this->scrollH;
 
233
   int y = this->y; int x = this->x;
 
234
   first = this->scrollV;
 
235
 
 
236
   if (this->h > itemCount) {
 
237
      last = this->scrollV + itemCount;
 
238
      move(y + last, x + 0);
 
239
   } else {
 
240
      last = MIN(itemCount, this->scrollV + this->h);
 
241
   }
 
242
   if (this->selected < first) {
 
243
      first = this->selected;
 
244
      this->scrollV = first;
 
245
      this->needsRedraw = true;
 
246
   }
 
247
   if (this->selected >= last) {
 
248
      last = MIN(itemCount, this->selected + 1);
 
249
      first = MAX(0, last - this->h);
 
250
      this->scrollV = first;
 
251
      this->needsRedraw = true;
 
252
   }
 
253
   assert(first >= 0);
 
254
   assert(last <= itemCount);
 
255
 
 
256
   if (this->header.len > 0) {
 
257
      int attr = focus
 
258
               ? CRT_colors[PANEL_HEADER_FOCUS]
 
259
               : CRT_colors[PANEL_HEADER_UNFOCUS];
 
260
      attrset(attr);
 
261
      mvhline(y, x, ' ', this->w);
 
262
      if (scrollH < this->header.len) {
 
263
         mvaddchnstr(y, x, this->header.chstr + scrollH,
 
264
                     MIN(this->header.len - scrollH, this->w));
 
265
      }
 
266
      attrset(CRT_colors[RESET_COLOR]);
 
267
      y++;
 
268
   }
 
269
   
 
270
   int highlight = focus
 
271
                 ? CRT_colors[PANEL_HIGHLIGHT_FOCUS]
 
272
                 : CRT_colors[PANEL_HIGHLIGHT_UNFOCUS];
 
273
 
 
274
   if (this->needsRedraw) {
 
275
 
 
276
      for(int i = first, j = 0; j < this->h && i < last; i++, j++) {
 
277
         Object* itemObj = Vector_get(this->items, i);
 
278
         RichString itemRef;
 
279
         RichString_initVal(itemRef);
 
280
         itemObj->display(itemObj, &itemRef);
 
281
         int amt = MIN(itemRef.len - scrollH, this->w);
 
282
         if (i == this->selected) {
 
283
            attrset(highlight);
 
284
            RichString_setAttr(&itemRef, highlight);
 
285
            mvhline(y + j, x+0, ' ', this->w);
 
286
            if (amt > 0)
 
287
               mvaddchnstr(y+j, x+0, itemRef.chstr + scrollH, amt);
 
288
            attrset(CRT_colors[RESET_COLOR]);
 
289
         } else {
 
290
            mvhline(y+j, x+0, ' ', this->w);
 
291
            if (amt > 0)
 
292
               mvaddchnstr(y+j, x+0, itemRef.chstr + scrollH, amt);
 
293
         }
 
294
      }
 
295
      for (int i = y + (last - first); i < y + this->h; i++)
 
296
         mvhline(i, x+0, ' ', this->w);
 
297
      this->needsRedraw = false;
 
298
 
 
299
   } else {
 
300
      Object* oldObj = Vector_get(this->items, this->oldSelected);
 
301
      RichString oldRef;
 
302
      RichString_initVal(oldRef);
 
303
      oldObj->display(oldObj, &oldRef);
 
304
      Object* newObj = Vector_get(this->items, this->selected);
 
305
      RichString newRef;
 
306
      RichString_initVal(newRef);
 
307
      newObj->display(newObj, &newRef);
 
308
      mvhline(y+ this->oldSelected - this->scrollV, x+0, ' ', this->w);
 
309
      if (scrollH < oldRef.len)
 
310
         mvaddchnstr(y+ this->oldSelected - this->scrollV, x+0, oldRef.chstr + this->scrollH, MIN(oldRef.len - scrollH, this->w));
 
311
      attrset(highlight);
 
312
      mvhline(y+this->selected - this->scrollV, x+0, ' ', this->w);
 
313
      RichString_setAttr(&newRef, highlight);
 
314
      if (scrollH < newRef.len)
 
315
         mvaddchnstr(y+this->selected - this->scrollV, x+0, newRef.chstr + this->scrollH, MIN(newRef.len - scrollH, this->w));
 
316
      attrset(CRT_colors[RESET_COLOR]);
 
317
   }
 
318
   this->oldSelected = this->selected;
 
319
   move(0, 0);
 
320
}
 
321
 
 
322
void Panel_onKey(Panel* this, int key) {
 
323
   assert (this != NULL);
 
324
   switch (key) {
 
325
   case KEY_DOWN:
 
326
      if (this->selected + 1 < Vector_size(this->items))
 
327
         this->selected++;
 
328
      break;
 
329
   case KEY_UP:
 
330
      if (this->selected > 0)
 
331
         this->selected--;
 
332
      break;
 
333
   case KEY_LEFT:
 
334
      if (this->scrollH > 0) {
 
335
         this->scrollH -= this->scrollHAmount;
 
336
         this->needsRedraw = true;
 
337
      }
 
338
      break;
 
339
   case KEY_RIGHT:
 
340
      this->scrollH += this->scrollHAmount;
 
341
      this->needsRedraw = true;
 
342
      break;
 
343
   case KEY_PPAGE:
 
344
      this->selected -= this->h;
 
345
      if (this->selected < 0)
 
346
         this->selected = 0;
 
347
      break;
 
348
   case KEY_NPAGE:
 
349
      this->selected += this->h;
 
350
      int size = Vector_size(this->items);
 
351
      if (this->selected >= size)
 
352
         this->selected = size - 1;
 
353
      break;
 
354
   case KEY_HOME:
 
355
      this->selected = 0;
 
356
      break;
 
357
   case KEY_END:
 
358
      this->selected = Vector_size(this->items) - 1;
 
359
      break;
 
360
   }
 
361
}