ColorsPanel.c 2.7 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
/*
htop - ColorsPanel.c
(C) 2004-2011 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
Hisham Muhammad's avatar
Hisham Muhammad committed
7

8
#include "ColorsPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9

Hisham Muhammad's avatar
Hisham Muhammad committed
10
#include "CRT.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
11
12
#include "CheckItem.h"
#include "debug.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13

Hisham Muhammad's avatar
Hisham Muhammad committed
14
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
15
16
#include <stdlib.h>
#include <string.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
17
18

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

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
25
26
27
#include "Panel.h"
#include "Settings.h"
#include "ScreenManager.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
28

29
30
typedef struct ColorsPanel_ {
   Panel super;
Hisham Muhammad's avatar
Hisham Muhammad committed
31
32
33

   Settings* settings;
   ScreenManager* scr;
34
} ColorsPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
35
36
37

}*/

Hisham Muhammad's avatar
Hisham Muhammad committed
38
static const char* ColorSchemes[] = {
Hisham Muhammad's avatar
Hisham Muhammad committed
39
40
41
42
43
44
45
46
47
   "Default",
   "Monochromatic",
   "Black on White",
   "Light Terminal",
   "MC",
   "Black Night",
   NULL
};

48
static void ColorsPanel_delete(Object* object) {
49
50
51
   Panel* super = (Panel*) object;
   ColorsPanel* this = (ColorsPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
52
53
54
   free(this);
}

55
static HandlerResult ColorsPanel_EventHandler(Panel* super, int ch) {
56
   ColorsPanel* this = (ColorsPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
57
58
   
   HandlerResult result = IGNORED;
59
   int mark = Panel_getSelectedIndex(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
60
61
62
63
64

   switch(ch) {
   case 0x0a:
   case 0x0d:
   case KEY_ENTER:
65
   case KEY_MOUSE:
Hisham Muhammad's avatar
Hisham Muhammad committed
66
   case ' ':
67
68
69
      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
70
71
72
73
74
      this->settings->colorScheme = mark;
      result = HANDLED;
   }

   if (result == HANDLED) {
75
      this->settings->changed = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
76
77
      Header* header = this->settings->header;
      CRT_setColors(mark);
78
      Panel* menu = (Panel*) Vector_get(this->scr->panels, 0);
Hisham Muhammad's avatar
Hisham Muhammad committed
79
80
      Header_draw(header);
      RichString_setAttr(&(super->header), CRT_colors[PANEL_HEADER_FOCUS]);
Hisham Muhammad's avatar
Hisham Muhammad committed
81
      RichString_setAttr(&(menu->header), CRT_colors[PANEL_HEADER_UNFOCUS]);
Hisham Muhammad's avatar
Hisham Muhammad committed
82
83
84
85
86
      ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
   }
   return result;
}

87
88
89
90
91
92
93
94
95
96
97
98
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
99
      Panel_add(super, (Object*) CheckItem_new(strdup(ColorSchemes[i]), NULL, false));
100
101
102
103
   }
   CheckItem_set((CheckItem*)Panel_get(super, settings->colorScheme), true);
   return this;
}