ColorsPanel.c 2.97 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
19
// * Add the entry in the ColorSchemeNames array below in the file
Hisham Muhammad's avatar
Hisham Muhammad committed
20
21
22
23
// * 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

}*/

37
38
static const char* ColorsFunctions[] = {"      ", "      ", "      ", "      ", "      ", "      ", "      ", "      ", "      ", "Done  ", NULL};

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

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

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

   switch(ch) {
   case 0x0a:
   case 0x0d:
   case KEY_ENTER:
66
   case KEY_MOUSE:
Hisham Muhammad's avatar
Hisham Muhammad committed
67
   case ' ':
68
      for (int i = 0; ColorSchemeNames[i] != NULL; i++)
69
70
         CheckItem_set((CheckItem*)Panel_get(super, i), false);
      CheckItem_set((CheckItem*)Panel_get(super, mark), true);
Hisham Muhammad's avatar
Hisham Muhammad committed
71
72
73
74
75
      this->settings->colorScheme = mark;
      result = HANDLED;
   }

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

88
89
90
91
92
93
94
95
PanelClass ColorsPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = ColorsPanel_delete
   },
   .eventHandler = ColorsPanel_eventHandler
};

96
ColorsPanel* ColorsPanel_new(Settings* settings, ScreenManager* scr) {
97
   ColorsPanel* this = AllocThis(ColorsPanel);
98
   Panel* super = (Panel*) this;
99
100
   FunctionBar* fuBar = FunctionBar_new(ColorsFunctions, NULL, NULL);
   Panel_init(super, 1, 1, 1, 1, Class(CheckItem), true, fuBar);
101
102
103
104
105

   this->settings = settings;
   this->scr = scr;

   Panel_setHeader(super, "Colors");
106
107
   for (int i = 0; ColorSchemeNames[i] != NULL; i++) {
      Panel_add(super, (Object*) CheckItem_new(strdup(ColorSchemeNames[i]), NULL, false));
108
109
110
111
   }
   CheckItem_set((CheckItem*)Panel_get(super, settings->colorScheme), true);
   return this;
}