htop/CheckItem.c

74 lines
1.6 KiB
C
Raw Normal View History

2006-03-04 18:16:49 +00:00
/*
2011-12-26 21:35:57 +00:00
htop - CheckItem.c
2011-05-26 16:35:07 +00:00
(C) 2004-2011 Hisham H. Muhammad
Released under the GNU GPLv2, see the COPYING file
2006-03-04 18:16:49 +00:00
in the source distribution for its full text.
*/
#include "CheckItem.h"
2011-12-26 21:35:57 +00:00
#include <assert.h>
#include <stdlib.h>
#include "CRT.h"
#include "RichString.h"
2006-03-04 18:16:49 +00:00
static void CheckItem_delete(Object* cast) {
CheckItem* this = (CheckItem*)cast;
assert (this != NULL);
free(this->text);
free(this);
}
static void CheckItem_display(const Object* cast, RichString* out) {
const CheckItem* this = (const CheckItem*)cast;
assert (this != NULL);
RichString_write(out, CRT_colors[CHECK_BOX], "[");
2020-11-01 00:09:51 +00:00
if (CheckItem_get(this)) {
RichString_append(out, CRT_colors[CHECK_MARK], "x");
2020-11-01 00:09:51 +00:00
} else {
RichString_append(out, CRT_colors[CHECK_MARK], " ");
2020-11-01 00:09:51 +00:00
}
RichString_append(out, CRT_colors[CHECK_BOX], "] ");
RichString_append(out, CRT_colors[CHECK_TEXT], this->text);
}
2020-10-05 11:19:50 +00:00
const ObjectClass CheckItem_class = {
.display = CheckItem_display,
.delete = CheckItem_delete
};
CheckItem* CheckItem_newByRef(char* text, bool* ref) {
CheckItem* this = AllocThis(CheckItem);
2006-03-04 18:16:49 +00:00
this->text = text;
this->value = false;
this->ref = ref;
2006-03-04 18:16:49 +00:00
return this;
}
CheckItem* CheckItem_newByVal(char* text, bool value) {
CheckItem* this = AllocThis(CheckItem);
this->text = text;
this->value = value;
this->ref = NULL;
return this;
}
void CheckItem_set(CheckItem* this, bool value) {
2020-11-01 00:09:51 +00:00
if (this->ref) {
*(this->ref) = value;
2020-11-01 00:09:51 +00:00
} else {
this->value = value;
2020-11-01 00:09:51 +00:00
}
}
bool CheckItem_get(const CheckItem* this) {
2020-11-01 00:09:51 +00:00
if (this->ref) {
return *(this->ref);
2020-11-01 00:09:51 +00:00
} else {
return this->value;
2020-11-01 00:09:51 +00:00
}
}