MetersPanel.c 2.64 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1

2
#include "MetersPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
3

4
#include "Panel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
5
6
7
8
9
10
11
12
#include "Settings.h"
#include "ScreenManager.h"

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

/*{

13
14
typedef struct MetersPanel_ {
   Panel super;
Hisham Muhammad's avatar
Hisham Muhammad committed
15
16

   Settings* settings;
17
   Vector* meters;
Hisham Muhammad's avatar
Hisham Muhammad committed
18
   ScreenManager* scr;
19
} MetersPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
20
21
22

}*/

23
static void MetersPanel_delete(Object* object) {
24
25
26
   Panel* super = (Panel*) object;
   MetersPanel* this = (MetersPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
27
28
29
   free(this);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
30
static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
31
   MetersPanel* this = (MetersPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
32
   
33
   int selected = Panel_getSelectedIndex(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
34
35
36
37
38
39
40
41
42
   HandlerResult result = IGNORED;

   switch(ch) {
      case 0x0a:
      case 0x0d:
      case KEY_ENTER:
      case KEY_F(4):
      case 't':
      {
43
         Meter* meter = (Meter*) Vector_get(this->meters, selected);
44
45
         int mode = meter->mode + 1;
         if (mode == LAST_METERMODE) mode = 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
46
         Meter_setMode(meter, mode);
47
         Panel_set(super, selected, (Object*) Meter_toListItem(meter));
Hisham Muhammad's avatar
Hisham Muhammad committed
48
49
50
51
52
53
54
         result = HANDLED;
         break;
      }
      case KEY_F(7):
      case '[':
      case '-':
      {
55
         Vector_moveUp(this->meters, selected);
56
         Panel_moveSelectedUp(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
57
58
59
60
61
62
63
         result = HANDLED;
         break;
      }
      case KEY_F(8):
      case ']':
      case '+':
      {
64
         Vector_moveDown(this->meters, selected);
65
         Panel_moveSelectedDown(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
66
67
68
69
70
71
         result = HANDLED;
         break;
      }
      case KEY_F(9):
      case KEY_DC:
      {
72
73
         if (selected < Vector_size(this->meters)) {
            Vector_remove(this->meters, selected);
74
            Panel_remove(super, selected);
Hisham Muhammad's avatar
Hisham Muhammad committed
75
76
77
78
79
80
81
         }
         result = HANDLED;
         break;
      }
   }
   if (result == HANDLED) {
      Header* header = this->settings->header;
Hisham Muhammad's avatar
Hisham Muhammad committed
82
      this->settings->changed = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
83
84
85
86
87
88
      Header_calculateHeight(header);
      Header_draw(header);
      ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
   }
   return result;
}
89

Hisham Muhammad's avatar
Hisham Muhammad committed
90
MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* meters, ScreenManager* scr) {
91
92
93
94
95
96
97
98
   MetersPanel* this = (MetersPanel*) malloc(sizeof(MetersPanel));
   Panel* super = (Panel*) this;
   Panel_init(super, 1, 1, 1, 1, LISTITEM_CLASS, true);
   ((Object*)this)->delete = MetersPanel_delete;

   this->settings = settings;
   this->meters = meters;
   this->scr = scr;
Hisham Muhammad's avatar
Hisham Muhammad committed
99
   super->eventHandler = MetersPanel_eventHandler;
100
101
102
103
104
105
106
   Panel_setHeader(super, header);
   for (int i = 0; i < Vector_size(meters); i++) {
      Meter* meter = (Meter*) Vector_get(meters, i);
      Panel_add(super, (Object*) Meter_toListItem(meter));
   }
   return this;
}