htop/FunctionBar.c

134 lines
3.4 KiB
C
Raw Permalink Normal View History

2006-03-04 18:16:49 +00:00
/*
htop - FunctionBar.c
2011-05-26 16:35:07 +00:00
(C) 2004-2011 Hisham H. Muhammad
2006-03-04 18:16:49 +00:00
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "FunctionBar.h"
2011-12-26 21:35:57 +00:00
#include "CRT.h"
2006-03-04 18:16:49 +00:00
2011-12-26 21:35:57 +00:00
#include <assert.h>
2006-03-04 18:16:49 +00:00
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
/*{
2011-12-26 21:35:57 +00:00
#include "Object.h"
2006-03-04 18:16:49 +00:00
typedef struct FunctionBar_ {
Object super;
int size;
char** functions;
char** keys;
int* events;
bool staticData;
} FunctionBar;
}*/
2010-02-25 01:43:18 +00:00
static const char* FunctionBar_FKeys[] = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", NULL};
2006-03-04 18:16:49 +00:00
2010-02-25 01:43:18 +00:00
static const char* FunctionBar_FLabels[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", NULL};
2006-03-04 18:16:49 +00:00
2009-06-02 04:51:23 +00:00
static int FunctionBar_FEvents[] = {KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6), KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10)};
2006-03-04 18:16:49 +00:00
ObjectClass FunctionBar_class = {
.delete = FunctionBar_delete
};
2010-02-25 01:43:18 +00:00
FunctionBar* FunctionBar_new(const char** functions, const char** keys, int* events) {
FunctionBar* this = AllocThis(FunctionBar);
this->functions = calloc(16, sizeof(char*));
if (!functions) {
functions = FunctionBar_FLabels;
}
for (int i = 0; i < 15 && functions[i]; i++) {
this->functions[i] = strdup(functions[i]);
}
2006-03-04 18:16:49 +00:00
if (keys && events) {
this->staticData = false;
2009-06-02 04:51:23 +00:00
this->keys = malloc(sizeof(char*) * 15);
this->events = malloc(sizeof(int) * 15);
int i = 0;
while (i < 15 && functions[i]) {
2011-12-25 20:23:53 +00:00
this->keys[i] = strdup(keys[i]);
2006-03-04 18:16:49 +00:00
this->events[i] = events[i];
i++;
2006-03-04 18:16:49 +00:00
}
this->size = i;
2006-03-04 18:16:49 +00:00
} else {
this->staticData = true;
2010-02-25 01:43:18 +00:00
this->keys = (char**) FunctionBar_FKeys;
2006-03-04 18:16:49 +00:00
this->events = FunctionBar_FEvents;
2009-06-02 04:51:23 +00:00
this->size = 10;
2006-03-04 18:16:49 +00:00
}
return this;
}
void FunctionBar_delete(Object* cast) {
FunctionBar* this = (FunctionBar*) cast;
for (int i = 0; i < 15 && this->functions[i]; i++) {
free(this->functions[i]);
}
free(this->functions);
2006-03-04 18:16:49 +00:00
if (!this->staticData) {
for (int i = 0; i < this->size; i++) {
free(this->keys[i]);
}
free(this->keys);
free(this->events);
}
free(this);
}
2010-02-25 01:43:18 +00:00
void FunctionBar_setLabel(FunctionBar* this, int event, const char* text) {
2006-03-04 18:16:49 +00:00
for (int i = 0; i < this->size; i++) {
if (this->events[i] == event) {
free(this->functions[i]);
2011-12-25 20:23:53 +00:00
this->functions[i] = strdup(text);
2006-03-04 18:16:49 +00:00
break;
}
}
}
void FunctionBar_draw(const FunctionBar* this, char* buffer) {
2006-03-04 18:16:49 +00:00
FunctionBar_drawAttr(this, buffer, CRT_colors[FUNCTION_BAR]);
}
void FunctionBar_drawAttr(const FunctionBar* this, char* buffer, int attr) {
2006-03-04 18:16:49 +00:00
attrset(CRT_colors[FUNCTION_BAR]);
mvhline(LINES-1, 0, ' ', COLS);
int x = 0;
for (int i = 0; i < this->size; i++) {
attrset(CRT_colors[FUNCTION_KEY]);
mvaddstr(LINES-1, x, this->keys[i]);
x += strlen(this->keys[i]);
attrset(CRT_colors[FUNCTION_BAR]);
mvaddstr(LINES-1, x, this->functions[i]);
x += strlen(this->functions[i]);
}
if (buffer) {
2006-03-04 18:16:49 +00:00
attrset(attr);
mvaddstr(LINES-1, x, buffer);
CRT_cursorX = x + strlen(buffer);
curs_set(1);
} else {
curs_set(0);
2006-03-04 18:16:49 +00:00
}
attrset(CRT_colors[RESET_COLOR]);
}
int FunctionBar_synthesizeEvent(const FunctionBar* this, int pos) {
2006-03-04 18:16:49 +00:00
int x = 0;
for (int i = 0; i < this->size; i++) {
x += strlen(this->keys[i]);
x += strlen(this->functions[i]);
if (pos < x) {
return this->events[i];
}
}
return ERR;
}