FunctionBar.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. htop - FunctionBar.c
  3. (C) 2004-2011 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "FunctionBar.h"
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "CRT.h"
  12. #include "Macros.h"
  13. #include "ProvideCurses.h"
  14. #include "XUtils.h"
  15. static const char* const FunctionBar_FKeys[] = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", NULL};
  16. static const char* const FunctionBar_FLabels[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", NULL};
  17. 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)};
  18. static const char* const FunctionBar_EnterEscKeys[] = {"Enter", "Esc", NULL};
  19. static const int FunctionBar_EnterEscEvents[] = {13, 27};
  20. static int currentLen = 0;
  21. FunctionBar* FunctionBar_newEnterEsc(const char* enter, const char* esc) {
  22. const char* functions[FUNCTIONBAR_MAXEVENTS + 1] = {enter, esc, NULL};
  23. return FunctionBar_new(functions, FunctionBar_EnterEscKeys, FunctionBar_EnterEscEvents);
  24. }
  25. FunctionBar* FunctionBar_new(const char* const* functions, const char* const* keys, const int* events) {
  26. FunctionBar* this = xCalloc(1, sizeof(FunctionBar));
  27. this->functions = xCalloc(FUNCTIONBAR_MAXEVENTS + 1, sizeof(char*));
  28. if (!functions) {
  29. functions = FunctionBar_FLabels;
  30. }
  31. for (int i = 0; i < FUNCTIONBAR_MAXEVENTS && functions[i]; i++) {
  32. this->functions[i] = xStrdup(functions[i]);
  33. }
  34. if (keys && events) {
  35. this->staticData = false;
  36. this->keys.keys = xCalloc(FUNCTIONBAR_MAXEVENTS, sizeof(char*));
  37. this->events = xCalloc(FUNCTIONBAR_MAXEVENTS, sizeof(int));
  38. int i = 0;
  39. while (i < FUNCTIONBAR_MAXEVENTS && functions[i]) {
  40. this->keys.keys[i] = xStrdup(keys[i]);
  41. this->events[i] = events[i];
  42. i++;
  43. }
  44. this->size = i;
  45. } else {
  46. this->staticData = true;
  47. this->keys.constKeys = FunctionBar_FKeys;
  48. this->events = FunctionBar_FEvents;
  49. this->size = ARRAYSIZE(FunctionBar_FEvents);
  50. }
  51. return this;
  52. }
  53. void FunctionBar_delete(FunctionBar* this) {
  54. for (int i = 0; i < FUNCTIONBAR_MAXEVENTS && this->functions[i]; i++) {
  55. free(this->functions[i]);
  56. }
  57. free(this->functions);
  58. if (!this->staticData) {
  59. for (int i = 0; i < this->size; i++) {
  60. free(this->keys.keys[i]);
  61. }
  62. free(this->keys.keys);
  63. free(this->events);
  64. }
  65. free(this);
  66. }
  67. void FunctionBar_setLabel(FunctionBar* this, int event, const char* text) {
  68. for (int i = 0; i < this->size; i++) {
  69. if (this->events[i] == event) {
  70. free(this->functions[i]);
  71. this->functions[i] = xStrdup(text);
  72. break;
  73. }
  74. }
  75. }
  76. int FunctionBar_draw(const FunctionBar* this) {
  77. return FunctionBar_drawExtra(this, NULL, -1, false);
  78. }
  79. int FunctionBar_drawExtra(const FunctionBar* this, const char* buffer, int attr, bool setCursor) {
  80. int cursorX = 0;
  81. attrset(CRT_colors[FUNCTION_BAR]);
  82. mvhline(LINES - 1, 0, ' ', COLS);
  83. int x = 0;
  84. for (int i = 0; i < this->size; i++) {
  85. attrset(CRT_colors[FUNCTION_KEY]);
  86. mvaddstr(LINES - 1, x, this->keys.constKeys[i]);
  87. x += strlen(this->keys.constKeys[i]);
  88. attrset(CRT_colors[FUNCTION_BAR]);
  89. mvaddstr(LINES - 1, x, this->functions[i]);
  90. x += strlen(this->functions[i]);
  91. }
  92. if (buffer) {
  93. if (attr == -1) {
  94. attrset(CRT_colors[FUNCTION_BAR]);
  95. } else {
  96. attrset(attr);
  97. }
  98. mvaddstr(LINES - 1, x, buffer);
  99. x += strlen(buffer);
  100. cursorX = x;
  101. }
  102. attrset(CRT_colors[RESET_COLOR]);
  103. if (setCursor) {
  104. curs_set(1);
  105. } else {
  106. curs_set(0);
  107. }
  108. currentLen = x;
  109. return cursorX;
  110. }
  111. void FunctionBar_append(const char* buffer, int attr) {
  112. if (attr == -1) {
  113. attrset(CRT_colors[FUNCTION_BAR]);
  114. } else {
  115. attrset(attr);
  116. }
  117. mvaddstr(LINES - 1, currentLen + 1, buffer);
  118. attrset(CRT_colors[RESET_COLOR]);
  119. currentLen += strlen(buffer) + 1;
  120. }
  121. int FunctionBar_synthesizeEvent(const FunctionBar* this, int pos) {
  122. int x = 0;
  123. for (int i = 0; i < this->size; i++) {
  124. x += strlen(this->keys.constKeys[i]);
  125. x += strlen(this->functions[i]);
  126. if (pos < x) {
  127. return this->events[i];
  128. }
  129. }
  130. return ERR;
  131. }