MetersListBox.c 2.73 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include "MetersListBox.h"

#include "ListBox.h"
#include "Settings.h"
#include "ScreenManager.h"

#include "debug.h"
#include <assert.h>

/*{

typedef struct MetersListBox_ {
   ListBox super;

   Settings* settings;
   TypedVector* meters;
   ScreenManager* scr;
} MetersListBox;

}*/

MetersListBox* MetersListBox_new(Settings* settings, char* header, TypedVector* meters, ScreenManager* scr) {
   MetersListBox* this = (MetersListBox*) malloc(sizeof(MetersListBox));
   ListBox* super = (ListBox*) this;
   ListBox_init(super, 1, 1, 1, 1, LISTITEM_CLASS, true);
   ((Object*)this)->delete = MetersListBox_delete;

   this->settings = settings;
   this->meters = meters;
   this->scr = scr;
   super->eventHandler = MetersListBox_EventHandler;
   ListBox_setHeader(super, header);
   for (int i = 0; i < TypedVector_size(meters); i++) {
      Meter* meter = (Meter*) TypedVector_get(meters, i);
      ListBox_add(super, (Object*) Meter_toListItem(meter));
   }
   return this;
}

void MetersListBox_delete(Object* object) {
   ListBox* super = (ListBox*) object;
   MetersListBox* this = (MetersListBox*) object;
   ListBox_done(super);
   free(this);
}

HandlerResult MetersListBox_EventHandler(ListBox* super, int ch) {
   MetersListBox* this = (MetersListBox*) super;
   
   int selected = ListBox_getSelectedIndex(super);
   HandlerResult result = IGNORED;

   switch(ch) {
      case 0x0a:
      case 0x0d:
      case KEY_ENTER:
      case KEY_F(4):
      case 't':
      {
         Meter* meter = (Meter*) TypedVector_get(this->meters, selected);
         MeterMode mode = meter->mode + 1;
         if (mode == LAST_METERMODE)
            mode = 1; // skip mode 0, "unset"
         Meter_setMode(meter, mode);
         ListBox_set(super, selected, (Object*) Meter_toListItem(meter));
         result = HANDLED;
         break;
      }
      case KEY_F(7):
      case '[':
      case '-':
      {
         TypedVector_moveUp(this->meters, selected);
         ListBox_moveSelectedUp(super);
         result = HANDLED;
         break;
      }
      case KEY_F(8):
      case ']':
      case '+':
      {
         TypedVector_moveDown(this->meters, selected);
         ListBox_moveSelectedDown(super);
         result = HANDLED;
         break;
      }
      case KEY_F(9):
      case KEY_DC:
      {
         if (selected < TypedVector_size(this->meters)) {
            TypedVector_remove(this->meters, selected);
            ListBox_remove(super, selected);
         }
         result = HANDLED;
         break;
      }
   }
   if (result == HANDLED) {
      Header* header = this->settings->header;
      Header_calculateHeight(header);
      Header_draw(header);
      ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
   }
   return result;
}