FunctionBar.c 3.69 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
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
9
#include "CRT.h"
10
#include "RichString.h"
Hisham's avatar
Hisham committed
11
#include "XAlloc.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12

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

/*{
18
19

#include <stdbool.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
20
21
22
23
24
25
26
27
28
29
30

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

}*/

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

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

Hisham Muhammad's avatar
Hisham Muhammad committed
35
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
36

Hisham Muhammad's avatar
Hisham Muhammad committed
37
38
39
40
41
42
43
44
static const char* FunctionBar_EnterEscKeys[] = {"Enter", "Esc", NULL};
static int FunctionBar_EnterEscEvents[] = {13, 27};

FunctionBar* FunctionBar_newEnterEsc(const char* enter, const char* esc) {
   const char* functions[] = {enter, esc, NULL};
   return FunctionBar_new(functions, FunctionBar_EnterEscKeys, FunctionBar_EnterEscEvents);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
45
FunctionBar* FunctionBar_new(const char** functions, const char** keys, int* events) {
Hisham's avatar
Hisham committed
46
47
   FunctionBar* this = xCalloc(1, sizeof(FunctionBar));
   this->functions = xCalloc(16, sizeof(char*));
48
49
50
51
   if (!functions) {
      functions = FunctionBar_FLabels;
   }
   for (int i = 0; i < 15 && functions[i]; i++) {
Hisham's avatar
Hisham committed
52
      this->functions[i] = xStrdup(functions[i]);
53
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
54
55
   if (keys && events) {
      this->staticData = false; 
Hisham's avatar
Hisham committed
56
57
      this->keys = xCalloc(15, sizeof(char*));
      this->events = xCalloc(15, sizeof(int));
Hisham Muhammad's avatar
oops    
Hisham Muhammad committed
58
59
      int i = 0;
      while (i < 15 && functions[i]) {
Hisham's avatar
Hisham committed
60
         this->keys[i] = xStrdup(keys[i]);
Hisham Muhammad's avatar
Hisham Muhammad committed
61
         this->events[i] = events[i];
Hisham Muhammad's avatar
oops    
Hisham Muhammad committed
62
         i++;
Hisham Muhammad's avatar
Hisham Muhammad committed
63
      }
Hisham Muhammad's avatar
oops    
Hisham Muhammad committed
64
      this->size = i;
Hisham Muhammad's avatar
Hisham Muhammad committed
65
66
   } else {
      this->staticData = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
67
      this->keys = (char**) FunctionBar_FKeys;
Hisham Muhammad's avatar
Hisham Muhammad committed
68
      this->events = FunctionBar_FEvents;
Hisham Muhammad's avatar
Hisham Muhammad committed
69
      this->size = 10;
Hisham Muhammad's avatar
Hisham Muhammad committed
70
71
72
73
   }
   return this;
}

74
void FunctionBar_delete(FunctionBar* this) {
75
76
77
78
   for (int i = 0; i < 15 && this->functions[i]; i++) {
      free(this->functions[i]);
   }
   free(this->functions);
Hisham Muhammad's avatar
Hisham Muhammad committed
79
80
81
82
83
84
85
86
87
88
   if (!this->staticData) {
      for (int i = 0; i < this->size; i++) {
         free(this->keys[i]);
      }
      free(this->keys);
      free(this->events);
   }
   free(this);
}

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

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

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

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