ColorsPanel.c 2.51 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

   Settings* settings;
   ScreenManager* scr;
26
} ColorsPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
27
28
29

}*/

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

40
static void ColorsPanel_delete(Object* object) {
41
42
43
   Panel* super = (Panel*) object;
   ColorsPanel* this = (ColorsPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
44
45
46
   free(this);
}

47
static HandlerResult ColorsPanel_EventHandler(Panel* super, int ch) {
48
   ColorsPanel* this = (ColorsPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
49
50
   
   HandlerResult result = IGNORED;
51
   int mark = Panel_getSelectedIndex(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
52
53
54
55
56

   switch(ch) {
   case 0x0a:
   case 0x0d:
   case KEY_ENTER:
57
   case KEY_MOUSE:
Hisham Muhammad's avatar
Hisham Muhammad committed
58
   case ' ':
59
60
61
      for (int i = 0; ColorSchemes[i] != NULL; i++)
         CheckItem_set((CheckItem*)Panel_get(super, i), false);
      CheckItem_set((CheckItem*)Panel_get(super, mark), true);
Hisham Muhammad's avatar
Hisham Muhammad committed
62
63
64
65
66
      this->settings->colorScheme = mark;
      result = HANDLED;
   }

   if (result == HANDLED) {
67
      this->settings->changed = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
68
69
      Header* header = this->settings->header;
      CRT_setColors(mark);
Hisham Muhammad's avatar
Hisham Muhammad committed
70
      Panel* menu = (Panel*) Vector_get(this->scr->items, 0);
Hisham Muhammad's avatar
Hisham Muhammad committed
71
72
      Header_draw(header);
      RichString_setAttr(&(super->header), CRT_colors[PANEL_HEADER_FOCUS]);
Hisham Muhammad's avatar
Hisham Muhammad committed
73
      RichString_setAttr(&(menu->header), CRT_colors[PANEL_HEADER_UNFOCUS]);
Hisham Muhammad's avatar
Hisham Muhammad committed
74
75
76
77
78
      ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
   }
   return result;
}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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;

   this->settings = settings;
   this->scr = scr;
   super->eventHandler = ColorsPanel_EventHandler;

   Panel_setHeader(super, "Colors");
   for (int i = 0; ColorSchemes[i] != NULL; i++) {
      Panel_add(super, (Object*) CheckItem_new(String_copy(ColorSchemes[i]), NULL, false));
   }
   CheckItem_set((CheckItem*)Panel_get(super, settings->colorScheme), true);
   return this;
}