CheckItem.c 1.57 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "CheckItem.h"
#include "Object.h"
#include "CRT.h"

#include "debug.h"

/*{

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

}*/

25
#ifdef DEBUG
Hisham Muhammad's avatar
Hisham Muhammad committed
26
char* CHECKITEM_CLASS = "CheckItem";
27
28
29
#else
#define CHECKITEM_CLASS NULL
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
30

31
CheckItem* CheckItem_new(char* text, bool* ref, bool value) {
Hisham Muhammad's avatar
Hisham Muhammad committed
32
   CheckItem* this = malloc(sizeof(CheckItem));
33
   Object_setClass(this, CHECKITEM_CLASS);
Hisham Muhammad's avatar
Hisham Muhammad committed
34
35
36
37
   ((Object*)this)->display = CheckItem_display;
   ((Object*)this)->delete = CheckItem_delete;
   this->text = text;
   this->value = value;
38
   this->ref = ref;
Hisham Muhammad's avatar
Hisham Muhammad committed
39
40
41
42
43
44
45
46
47
48
49
   return this;
}

void CheckItem_delete(Object* cast) {
   CheckItem* this = (CheckItem*)cast;
   assert (this != NULL);

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

50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
64
65
66
67
void CheckItem_display(Object* cast, RichString* out) {
   CheckItem* this = (CheckItem*)cast;
   assert (this != NULL);
   RichString_write(out, CRT_colors[CHECK_BOX], "[");
68
   if (CheckItem_get(this))
Hisham Muhammad's avatar
Hisham Muhammad committed
69
70
71
72
73
74
      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);
}