MetersPanel.c 2.6 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);
}

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
82
83
84
85
86
87
         }
         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;
}
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

MetersPanel* MetersPanel_new(Settings* settings, char* header, Vector* meters, ScreenManager* scr) {
   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;
   super->eventHandler = MetersPanel_EventHandler;
   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;
}