FunctionBar.c 3.57 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop - FunctionBar.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 "FunctionBar.h"

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

Hisham Muhammad's avatar
Hisham Muhammad committed
12
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
13
14
15
16
17
18
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <curses.h>

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
19
#include "Object.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
20
21
22
23
24
25
26
27
28
29
30
31

typedef struct FunctionBar_ {
   Object super;
   int size;
   char** functions;
   char** keys;
   int* events;
   bool staticData;
} FunctionBar;

}*/

32
#ifdef DEBUG
Hisham Muhammad's avatar
Hisham Muhammad committed
33
char* FUNCTIONBAR_CLASS = "FunctionBar";
34
35
36
#else
#define FUNCTIONBAR_CLASS NULL
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
37

Hisham Muhammad's avatar
Hisham Muhammad committed
38
static const char* FunctionBar_FKeys[] = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", NULL};
Hisham Muhammad's avatar
Hisham Muhammad committed
39

Hisham Muhammad's avatar
Hisham Muhammad committed
40
static const char* FunctionBar_FLabels[] = {"      ", "      ", "      ", "      ", "      ", "      ", "      ", "      ", "      ", "      ", NULL};
Hisham Muhammad's avatar
Hisham Muhammad committed
41

Hisham Muhammad's avatar
Hisham Muhammad committed
42
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)};
Hisham Muhammad's avatar
Hisham Muhammad committed
43

Hisham Muhammad's avatar
Hisham Muhammad committed
44
FunctionBar* FunctionBar_new(const char** functions, const char** keys, int* events) {
Hisham Muhammad's avatar
Hisham Muhammad committed
45
   FunctionBar* this = malloc(sizeof(FunctionBar));
46
   Object_setClass(this, FUNCTIONBAR_CLASS);
Hisham Muhammad's avatar
Hisham Muhammad committed
47
   ((Object*) this)->delete = FunctionBar_delete;
Hisham Muhammad's avatar
Hisham Muhammad committed
48
   this->functions = (char**) functions;
Hisham Muhammad's avatar
Hisham Muhammad committed
49
50
   if (keys && events) {
      this->staticData = false; 
Hisham Muhammad's avatar
Hisham Muhammad committed
51
52
53
      this->functions = malloc(sizeof(char*) * 15);
      this->keys = malloc(sizeof(char*) * 15);
      this->events = malloc(sizeof(int) * 15);
Hisham Muhammad's avatar
oops    
Hisham Muhammad committed
54
55
      int i = 0;
      while (i < 15 && functions[i]) {
Hisham Muhammad's avatar
Hisham Muhammad committed
56
57
         this->functions[i] = strdup(functions[i]);
         this->keys[i] = strdup(keys[i]);
Hisham Muhammad's avatar
Hisham Muhammad committed
58
         this->events[i] = events[i];
Hisham Muhammad's avatar
oops    
Hisham Muhammad committed
59
         i++;
Hisham Muhammad's avatar
Hisham Muhammad committed
60
      }
Hisham Muhammad's avatar
oops    
Hisham Muhammad committed
61
      this->size = i;
Hisham Muhammad's avatar
Hisham Muhammad committed
62
63
   } else {
      this->staticData = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
64
65
      this->functions = (char**)( functions ? functions : FunctionBar_FLabels );
      this->keys = (char**) FunctionBar_FKeys;
Hisham Muhammad's avatar
Hisham Muhammad committed
66
      this->events = FunctionBar_FEvents;
Hisham Muhammad's avatar
Hisham Muhammad committed
67
      this->size = 10;
Hisham Muhammad's avatar
Hisham Muhammad committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
   }
   return this;
}

void FunctionBar_delete(Object* cast) {
   FunctionBar* this = (FunctionBar*) cast;
   if (!this->staticData) {
      for (int i = 0; i < this->size; i++) {
         free(this->functions[i]);
         free(this->keys[i]);
      }
      free(this->functions);
      free(this->keys);
      free(this->events);
   }
   free(this);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
86
void FunctionBar_setLabel(FunctionBar* this, int event, const char* text) {
Hisham Muhammad's avatar
Hisham Muhammad committed
87
88
89
90
   assert(!this->staticData);
   for (int i = 0; i < this->size; i++) {
      if (this->events[i] == event) {
         free(this->functions[i]);
Hisham Muhammad's avatar
Hisham Muhammad committed
91
         this->functions[i] = strdup(text);
Hisham Muhammad's avatar
Hisham Muhammad committed
92
93
94
95
96
         break;
      }
   }
}

97
void FunctionBar_draw(const FunctionBar* this, char* buffer) {
Hisham Muhammad's avatar
Hisham Muhammad committed
98
99
100
   FunctionBar_drawAttr(this, buffer, CRT_colors[FUNCTION_BAR]);
}

101
void FunctionBar_drawAttr(const FunctionBar* this, char* buffer, int attr) {
Hisham Muhammad's avatar
Hisham Muhammad committed
102
103
104
105
106
107
108
109
110
111
112
   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]);
   }
113
   if (buffer) {
Hisham Muhammad's avatar
Hisham Muhammad committed
114
115
      attrset(attr);
      mvaddstr(LINES-1, x, buffer);
116
117
118
119
      CRT_cursorX = x + strlen(buffer);
      curs_set(1);
   } else {
      curs_set(0);
Hisham Muhammad's avatar
Hisham Muhammad committed
120
121
122
123
   }
   attrset(CRT_colors[RESET_COLOR]);
}

124
int FunctionBar_synthesizeEvent(const FunctionBar* this, int pos) {
Hisham Muhammad's avatar
Hisham Muhammad committed
125
126
127
128
129
130
131
132
133
134
   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;
}