ColorsListBox.c 2.47 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2

#include "CRT.h"
3
#include "ColorsPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
4

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

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

// TO ADD A NEW SCHEME:
14
// * Increment the size of bool check in ColorsPanel.h
Hisham Muhammad's avatar
Hisham Muhammad committed
15
16
17
18
19
20
// * Add the entry in the ColorSchemes array below in the file
// * Add a define in CRT.h that matches the order of the array
// * Add the colors in CRT_setColors

/*{

21
22
typedef struct ColorsPanel_ {
   Panel super;
Hisham Muhammad's avatar
Hisham Muhammad committed
23
24
25
26

   Settings* settings;
   ScreenManager* scr;
   bool check[5];
27
} ColorsPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
28
29
30

}*/

Hisham Muhammad's avatar
Hisham Muhammad committed
31
/* private */
Hisham Muhammad's avatar
Hisham Muhammad committed
32
33
34
35
36
37
38
39
40
41
static char* ColorSchemes[] = {
   "Default",
   "Monochromatic",
   "Black on White",
   "Light Terminal",
   "MC",
   "Black Night",
   NULL
};

42
43
44
45
46
ColorsPanel* ColorsPanel_new(Settings* settings, ScreenManager* scr) {
   ColorsPanel* this = (ColorsPanel*) malloc(sizeof(ColorsPanel));
   Panel* super = (Panel*) this;
   Panel_init(super, 1, 1, 1, 1, CHECKITEM_CLASS, true);
   ((Object*)this)->delete = ColorsPanel_delete;
Hisham Muhammad's avatar
Hisham Muhammad committed
47
48
49

   this->settings = settings;
   this->scr = scr;
50
   super->eventHandler = ColorsPanel_EventHandler;
Hisham Muhammad's avatar
Hisham Muhammad committed
51

52
   Panel_setHeader(super, "Colors");
Hisham Muhammad's avatar
Hisham Muhammad committed
53
   for (int i = 0; ColorSchemes[i] != NULL; i++) {
54
      Panel_add(super, (Object*) CheckItem_new(String_copy(ColorSchemes[i]), &(this->check[i])));
Hisham Muhammad's avatar
Hisham Muhammad committed
55
56
57
58
59
60
      this->check[i] = false;
   }
   this->check[settings->colorScheme] = true;
   return this;
}

61
62
63
64
void ColorsPanel_delete(Object* object) {
   Panel* super = (Panel*) object;
   ColorsPanel* this = (ColorsPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
65
66
67
   free(this);
}

68
69
HandlerResult ColorsPanel_EventHandler(Panel* super, int ch) {
   ColorsPanel* this = (ColorsPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
70
71
   
   HandlerResult result = IGNORED;
72
   int mark = Panel_getSelectedIndex(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

   switch(ch) {
   case 0x0a:
   case 0x0d:
   case KEY_ENTER:
   case ' ':
      for (int i = 0; ColorSchemes[i] != NULL; i++) {
         this->check[i] = false;
      }
      this->check[mark] = true;
      this->settings->colorScheme = mark;
      result = HANDLED;
   }

   if (result == HANDLED) {
88
      this->settings->changed = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
89
90
      Header* header = this->settings->header;
      CRT_setColors(mark);
91
      Panel* lbMenu = (Panel*) Vector_get(this->scr->items, 0);
Hisham Muhammad's avatar
Hisham Muhammad committed
92
93
94
95
96
97
98
99
      Header_draw(header);
      RichString_setAttr(&(super->header), CRT_colors[PANEL_HEADER_FOCUS]);
      RichString_setAttr(&(lbMenu->header), CRT_colors[PANEL_HEADER_UNFOCUS]);
      ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
   }
   return result;
}