ColorsPanel.c 2.68 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
#include "CheckItem.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12

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

// TO ADD A NEW SCHEME:
18
// * Increment the size of bool check in ColorsPanel.h
Hisham Muhammad's avatar
Hisham Muhammad committed
19
20
21
22
23
// * 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
24
25
26
#include "Panel.h"
#include "Settings.h"
#include "ScreenManager.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
27

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

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

}*/

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

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

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

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

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

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