Changes in object model: separate class objects to store vtable. Also, nicer UTF-8 display of big numbers.

This commit is contained in:
Hisham Muhammad
2012-12-05 15:12:20 +00:00
parent 2a73405cd0
commit 00b324bfc1
64 changed files with 662 additions and 566 deletions

51
Panel.c
View File

@ -38,8 +38,18 @@ typedef enum HandlerResult_ {
typedef HandlerResult(*Panel_EventHandler)(Panel*, int);
typedef struct PanelClass_ {
const ObjectClass super;
const Panel_EventHandler eventHandler;
} PanelClass;
#define As_Panel(this_) ((PanelClass*)((this_)->super.klass))
#define Panel_eventHandlerFn(this_) As_Panel(this_)->eventHandler
#define Panel_eventHandler(this_, ev_) As_Panel(this_)->eventHandler((Panel*)(this_), ev_)
struct Panel_ {
Object super;
PanelClass* class;
int x, y, w, h;
WINDOW* window;
Vector* items;
@ -49,7 +59,6 @@ struct Panel_ {
int oldSelected;
bool needsRedraw;
RichString header;
Panel_EventHandler eventHandler;
char* eventHandlerBuffer;
};
@ -62,22 +71,24 @@ struct Panel_ {
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif
#ifdef DEBUG
char* PANEL_CLASS = "Panel";
#else
#define PANEL_CLASS NULL
#endif
#define KEY_CTRLN 0016 /* control-n key */
#define KEY_CTRLP 0020 /* control-p key */
#define KEY_CTRLF 0006 /* control-f key */
#define KEY_CTRLB 0002 /* control-b key */
Panel* Panel_new(int x, int y, int w, int h, char* type, bool owner, Object_Compare compare) {
PanelClass Panel_class = {
.super = {
.extends = Class(Object),
.delete = Panel_delete
},
.eventHandler = Panel_selectByTyping
};
Panel* Panel_new(int x, int y, int w, int h, bool owner, ObjectClass* type) {
Panel* this;
this = malloc(sizeof(Panel));
Object_setClass(this, Class(Panel));
Panel_init(this, x, y, w, h, type, owner);
this->items->compare = compare;
return this;
}
@ -87,17 +98,13 @@ void Panel_delete(Object* cast) {
free(this);
}
void Panel_init(Panel* this, int x, int y, int w, int h, char* type, bool owner) {
Object* super = (Object*) this;
Object_setClass(this, PANEL_CLASS);
super->delete = Panel_delete;
void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool owner) {
this->x = x;
this->y = y;
this->w = w;
this->h = h;
this->eventHandler = NULL;
this->eventHandlerBuffer = NULL;
this->items = Vector_new(type, owner, DEFAULT_SIZE, ListItem_compare);
this->items = Vector_new(type, owner, DEFAULT_SIZE);
this->scrollV = 0;
this->scrollH = 0;
this->selected = 0;
@ -129,10 +136,6 @@ inline void Panel_setHeader(Panel* this, const char* header) {
this->needsRedraw = true;
}
void Panel_setEventHandler(Panel* this, Panel_EventHandler eh) {
this->eventHandler = eh;
}
void Panel_move(Panel* this, int x, int y) {
assert (this != NULL);
@ -238,8 +241,8 @@ void Panel_setSelected(Panel* this, int selected) {
selected = MAX(0, MIN(Vector_size(this->items) - 1, selected));
this->selected = selected;
if (this->eventHandler) {
this->eventHandler(this, EVENT_SETSELECTED);
if (Panel_eventHandlerFn(this)) {
Panel_eventHandler(this, EVENT_SETSELECTED);
}
}
@ -293,7 +296,7 @@ void Panel_draw(Panel* this, bool focus) {
for(int i = first, j = 0; j < this->h && i < last; i++, j++) {
Object* itemObj = Vector_get(this->items, i);
RichString_begin(item);
itemObj->display(itemObj, &item);
Object_display(itemObj, &item);
int itemLen = RichString_sizeVal(item);
int amt = MIN(itemLen - scrollH, this->w);
if (i == this->selected) {
@ -317,11 +320,11 @@ void Panel_draw(Panel* this, bool focus) {
} else {
Object* oldObj = Vector_get(this->items, this->oldSelected);
RichString_begin(old);
oldObj->display(oldObj, &old);
Object_display(oldObj, &old);
int oldLen = RichString_sizeVal(old);
Object* newObj = Vector_get(this->items, this->selected);
RichString_begin(new);
newObj->display(newObj, &new);
Object_display(newObj, &new);
int newLen = RichString_sizeVal(new);
mvhline(y+ this->oldSelected - this->scrollV, x+0, ' ', this->w);
if (scrollH < oldLen)