ColorsPanel.c 2.53 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1

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

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

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

// TO ADD A NEW SCHEME:
15
// * Increment the size of bool check in ColorsPanel.h
Hisham Muhammad's avatar
Hisham Muhammad committed
16
17
18
19
20
21
// * 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

/*{

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

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

}*/

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

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

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

   switch(ch) {
   case 0x0a:
   case 0x0d:
   case KEY_ENTER:
58
   case KEY_MOUSE:
Hisham Muhammad's avatar
Hisham Muhammad committed
59
   case ' ':
60
61
62
      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
63
64
65
66
67
      this->settings->colorScheme = mark;
      result = HANDLED;
   }

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

80
81
82
83
84
85
86
87
88
89
90
91
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++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
92
      Panel_add(super, (Object*) CheckItem_new(strdup(ColorSchemes[i]), NULL, false));
93
94
95
96
   }
   CheckItem_set((CheckItem*)Panel_get(super, settings->colorScheme), true);
   return this;
}