MetersPanel.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. htop - MetersPanel.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 "MetersPanel.h"
  9. #include <stdlib.h>
  10. #include "CRT.h"
  11. #include "FunctionBar.h"
  12. #include "Header.h"
  13. #include "ListItem.h"
  14. #include "Meter.h"
  15. #include "Object.h"
  16. #include "ProvideCurses.h"
  17. // Note: In code the meters are known to have bar/text/graph "Modes", but in UI
  18. // we call them "Styles".
  19. static const char* const MetersFunctions[] = {"Style ", "Move ", " ", "Delete", "Done ", NULL};
  20. static const char* const MetersKeys[] = {"Space", "Enter", "", "Del", "F10"};
  21. static const int MetersEvents[] = {' ', 13, ERR, KEY_DC, KEY_F(10)};
  22. // We avoid UTF-8 arrows ← → here as they might display full-width on Chinese
  23. // terminals, breaking our aligning.
  24. // In <http://unicode.org/reports/tr11/>, arrows (U+2019..U+2199) are
  25. // considered "Ambiguous characters".
  26. static const char* const MetersMovingFunctions[] = {"Style ", "Lock ", "Up ", "Down ", "Left ", "Right ", " ", "Delete", "Done ", NULL};
  27. static const char* const MetersMovingKeys[] = {"Space", "Enter", "Up", "Dn", "<-", "->", " ", "Del", "F10"};
  28. static const int MetersMovingEvents[] = {' ', 13, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, ERR, KEY_DC, KEY_F(10)};
  29. static FunctionBar* Meters_movingBar = NULL;
  30. void MetersPanel_cleanup(void) {
  31. if (Meters_movingBar) {
  32. FunctionBar_delete(Meters_movingBar);
  33. Meters_movingBar = NULL;
  34. }
  35. }
  36. static void MetersPanel_delete(Object* object) {
  37. Panel* super = (Panel*) object;
  38. MetersPanel* this = (MetersPanel*) object;
  39. Panel_done(super);
  40. free(this);
  41. }
  42. void MetersPanel_setMoving(MetersPanel* this, bool moving) {
  43. Panel* super = (Panel*) this;
  44. this->moving = moving;
  45. ListItem* selected = (ListItem*)Panel_getSelected(super);
  46. if (selected) {
  47. selected->moving = moving;
  48. }
  49. if (!moving) {
  50. Panel_setSelectionColor(super, PANEL_SELECTION_FOCUS);
  51. Panel_setDefaultBar(super);
  52. } else {
  53. Panel_setSelectionColor(super, PANEL_SELECTION_FOLLOW);
  54. super->currentBar = Meters_movingBar;
  55. }
  56. }
  57. static inline bool moveToNeighbor(MetersPanel* this, MetersPanel* neighbor, int selected) {
  58. Panel* super = (Panel*) this;
  59. if (this->moving) {
  60. if (neighbor) {
  61. if (selected < Vector_size(this->meters)) {
  62. MetersPanel_setMoving(this, false);
  63. Meter* meter = (Meter*) Vector_take(this->meters, selected);
  64. Panel_remove(super, selected);
  65. Vector_insert(neighbor->meters, selected, meter);
  66. Panel_insert(&(neighbor->super), selected, (Object*) Meter_toListItem(meter, false));
  67. Panel_setSelected(&(neighbor->super), selected);
  68. MetersPanel_setMoving(neighbor, true);
  69. return true;
  70. }
  71. }
  72. }
  73. return false;
  74. }
  75. static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
  76. MetersPanel* this = (MetersPanel*) super;
  77. int selected = Panel_getSelectedIndex(super);
  78. HandlerResult result = IGNORED;
  79. bool sideMove = false;
  80. switch (ch) {
  81. case 0x0a:
  82. case 0x0d:
  83. case KEY_ENTER:
  84. if (!Vector_size(this->meters))
  85. break;
  86. MetersPanel_setMoving(this, !(this->moving));
  87. result = HANDLED;
  88. break;
  89. case ' ':
  90. case KEY_F(4):
  91. case 't': {
  92. if (!Vector_size(this->meters))
  93. break;
  94. Meter* meter = (Meter*) Vector_get(this->meters, selected);
  95. MeterModeId mode = Meter_nextSupportedMode(meter);
  96. Meter_setMode(meter, mode);
  97. Panel_set(super, selected, (Object*) Meter_toListItem(meter, this->moving));
  98. result = HANDLED;
  99. break;
  100. }
  101. case KEY_UP:
  102. if (!this->moving)
  103. break;
  104. /* else fallthrough */
  105. case KEY_F(7):
  106. case '[':
  107. case '-':
  108. Vector_moveUp(this->meters, selected);
  109. Panel_moveSelectedUp(super);
  110. result = HANDLED;
  111. break;
  112. case KEY_DOWN:
  113. if (!this->moving)
  114. break;
  115. /* else fallthrough */
  116. case KEY_F(8):
  117. case ']':
  118. case '+':
  119. Vector_moveDown(this->meters, selected);
  120. Panel_moveSelectedDown(super);
  121. result = HANDLED;
  122. break;
  123. case KEY_RIGHT:
  124. sideMove = moveToNeighbor(this, this->rightNeighbor, selected);
  125. if (this->moving && !sideMove) {
  126. // lock user here until it exits positioning-mode
  127. result = HANDLED;
  128. }
  129. // if user is free, don't set HANDLED;
  130. // let ScreenManager handle focus.
  131. break;
  132. case KEY_LEFT:
  133. sideMove = moveToNeighbor(this, this->leftNeighbor, selected);
  134. if (this->moving && !sideMove) {
  135. result = HANDLED;
  136. }
  137. break;
  138. case KEY_F(9):
  139. case KEY_DC:
  140. if (!Vector_size(this->meters))
  141. break;
  142. if (selected < Vector_size(this->meters)) {
  143. Vector_remove(this->meters, selected);
  144. Panel_remove(super, selected);
  145. }
  146. MetersPanel_setMoving(this, false);
  147. result = HANDLED;
  148. break;
  149. }
  150. if (result == HANDLED || sideMove) {
  151. Header* header = this->scr->header;
  152. this->settings->changed = true;
  153. this->settings->lastUpdate++;
  154. Header_calculateHeight(header);
  155. ScreenManager_resize(this->scr);
  156. }
  157. return result;
  158. }
  159. const PanelClass MetersPanel_class = {
  160. .super = {
  161. .extends = Class(Panel),
  162. .delete = MetersPanel_delete
  163. },
  164. .eventHandler = MetersPanel_eventHandler
  165. };
  166. MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* meters, ScreenManager* scr) {
  167. MetersPanel* this = AllocThis(MetersPanel);
  168. Panel* super = (Panel*) this;
  169. FunctionBar* fuBar = FunctionBar_new(MetersFunctions, MetersKeys, MetersEvents);
  170. if (!Meters_movingBar) {
  171. Meters_movingBar = FunctionBar_new(MetersMovingFunctions, MetersMovingKeys, MetersMovingEvents);
  172. }
  173. Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);
  174. this->settings = settings;
  175. this->meters = meters;
  176. this->scr = scr;
  177. this->moving = false;
  178. this->rightNeighbor = NULL;
  179. this->leftNeighbor = NULL;
  180. Panel_setHeader(super, header);
  181. for (int i = 0; i < Vector_size(meters); i++) {
  182. const Meter* meter = (const Meter*) Vector_get(meters, i);
  183. Panel_add(super, (Object*) Meter_toListItem(meter, false));
  184. }
  185. return this;
  186. }