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

#include "CheckItem.h"

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

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

Hisham Muhammad's avatar
Hisham Muhammad committed
16
/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
17
#include "Object.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
18
19
20
21

typedef struct CheckItem_ {
   Object super;
   char* text;
22
23
   bool value;
   bool* ref;
Hisham Muhammad's avatar
Hisham Muhammad committed
24
25
26
27
} CheckItem;

}*/

28
#ifdef DEBUG
Hisham Muhammad's avatar
Hisham Muhammad committed
29
char* CHECKITEM_CLASS = "CheckItem";
30
31
32
#else
#define CHECKITEM_CLASS NULL
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
static void CheckItem_delete(Object* cast) {
   CheckItem* this = (CheckItem*)cast;
   assert (this != NULL);

   free(this->text);
   free(this);
}

static void CheckItem_display(Object* cast, RichString* out) {
   CheckItem* this = (CheckItem*)cast;
   assert (this != NULL);
   RichString_write(out, CRT_colors[CHECK_BOX], "[");
   if (CheckItem_get(this))
      RichString_append(out, CRT_colors[CHECK_MARK], "x");
   else
      RichString_append(out, CRT_colors[CHECK_MARK], " ");
   RichString_append(out, CRT_colors[CHECK_BOX], "] ");
   RichString_append(out, CRT_colors[CHECK_TEXT], this->text);
}

54
CheckItem* CheckItem_new(char* text, bool* ref, bool value) {
Hisham Muhammad's avatar
Hisham Muhammad committed
55
   CheckItem* this = malloc(sizeof(CheckItem));
56
   Object_setClass(this, CHECKITEM_CLASS);
Hisham Muhammad's avatar
Hisham Muhammad committed
57
58
59
60
   ((Object*)this)->display = CheckItem_display;
   ((Object*)this)->delete = CheckItem_delete;
   this->text = text;
   this->value = value;
61
   this->ref = ref;
Hisham Muhammad's avatar
Hisham Muhammad committed
62
63
64
   return this;
}

65
66
67
68
69
70
71
72
73
74
75
76
77
void CheckItem_set(CheckItem* this, bool value) {
   if (this->ref) 
      *(this->ref) = value;
   else
      this->value = value;
}

bool CheckItem_get(CheckItem* this) {
   if (this->ref) 
      return *(this->ref);
   else
      return this->value;
}