AvailableColumnsPanel.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. htop - AvailableColumnsPanel.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 "AvailableColumnsPanel.h"
  9. #include <assert.h>
  10. #include <ctype.h>
  11. #include <stdbool.h>
  12. #include <stdlib.h>
  13. #include "ColumnsPanel.h"
  14. #include "DynamicColumn.h"
  15. #include "FunctionBar.h"
  16. #include "Hashtable.h"
  17. #include "ListItem.h"
  18. #include "Object.h"
  19. #include "Platform.h"
  20. #include "Process.h"
  21. #include "ProvideCurses.h"
  22. #include "RowField.h"
  23. #include "XUtils.h"
  24. static const char* const AvailableColumnsFunctions[] = {" ", " ", " ", " ", "Add ", " ", " ", " ", " ", "Done ", NULL};
  25. static void AvailableColumnsPanel_delete(Object* object) {
  26. Panel* super = (Panel*) object;
  27. AvailableColumnsPanel* this = (AvailableColumnsPanel*) object;
  28. Panel_done(super);
  29. free(this);
  30. }
  31. static void AvailableColumnsPanel_insert(AvailableColumnsPanel* this, int at, int key) {
  32. const char* name;
  33. if (key >= ROW_DYNAMIC_FIELDS)
  34. name = DynamicColumn_name(key);
  35. else
  36. name = Process_fields[key].name;
  37. Panel_insert(this->columns, at, (Object*) ListItem_new(name, key));
  38. }
  39. static HandlerResult AvailableColumnsPanel_eventHandler(Panel* super, int ch) {
  40. AvailableColumnsPanel* this = (AvailableColumnsPanel*) super;
  41. HandlerResult result = IGNORED;
  42. switch (ch) {
  43. case 13:
  44. case KEY_ENTER:
  45. case KEY_F(5): {
  46. const ListItem* selected = (ListItem*) Panel_getSelected(super);
  47. if (!selected)
  48. break;
  49. int at = Panel_getSelectedIndex(this->columns);
  50. AvailableColumnsPanel_insert(this, at, selected->key);
  51. Panel_setSelected(this->columns, at + 1);
  52. ColumnsPanel_update(this->columns);
  53. result = HANDLED;
  54. break;
  55. }
  56. default:
  57. if (0 < ch && ch < 255 && isgraph((unsigned char)ch))
  58. result = Panel_selectByTyping(super, ch);
  59. break;
  60. }
  61. return result;
  62. }
  63. const PanelClass AvailableColumnsPanel_class = {
  64. .super = {
  65. .extends = Class(Panel),
  66. .delete = AvailableColumnsPanel_delete
  67. },
  68. .eventHandler = AvailableColumnsPanel_eventHandler
  69. };
  70. static void AvailableColumnsPanel_addDynamicColumn(ht_key_t key, void* value, void* data) {
  71. const DynamicColumn* column = (const DynamicColumn*) value;
  72. if (column->table) /* DynamicScreen, handled differently */
  73. return;
  74. AvailableColumnsPanel* this = (AvailableColumnsPanel*) data;
  75. const char* title = column->heading ? column->heading : column->name;
  76. const char* text = column->description ? column->description : column->caption;
  77. char description[256];
  78. if (text)
  79. xSnprintf(description, sizeof(description), "%s - %s", title, text);
  80. else
  81. xSnprintf(description, sizeof(description), "%s", title);
  82. Panel_add(&this->super, (Object*) ListItem_new(description, key));
  83. }
  84. // Handle DynamicColumns entries in the AvailableColumnsPanel
  85. static void AvailableColumnsPanel_addDynamicColumns(AvailableColumnsPanel* this, Hashtable* dynamicColumns) {
  86. assert(dynamicColumns);
  87. Hashtable_foreach(dynamicColumns, AvailableColumnsPanel_addDynamicColumn, this);
  88. }
  89. // Handle remaining Platform Meter entries in the AvailableColumnsPanel
  90. static void AvailableColumnsPanel_addPlatformColumns(AvailableColumnsPanel* this) {
  91. for (int i = 1; i < LAST_PROCESSFIELD; i++) {
  92. if (i != COMM && Process_fields[i].description) {
  93. char description[256];
  94. xSnprintf(description, sizeof(description), "%s - %s", Process_fields[i].name, Process_fields[i].description);
  95. Panel_add(&this->super, (Object*) ListItem_new(description, i));
  96. }
  97. }
  98. }
  99. // Handle DynamicColumns entries associated with DynamicScreens
  100. static void AvailableColumnsPanel_addDynamicScreens(AvailableColumnsPanel* this, const char* screen) {
  101. Platform_addDynamicScreenAvailableColumns(&this->super, screen);
  102. }
  103. void AvailableColumnsPanel_fill(AvailableColumnsPanel* this, const char* dynamicScreen, Hashtable* dynamicColumns) {
  104. Panel* super = (Panel*) this;
  105. Panel_prune(super);
  106. if (dynamicScreen) {
  107. AvailableColumnsPanel_addDynamicScreens(this, dynamicScreen);
  108. } else {
  109. AvailableColumnsPanel_addPlatformColumns(this);
  110. AvailableColumnsPanel_addDynamicColumns(this, dynamicColumns);
  111. }
  112. }
  113. AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns, Hashtable* dynamicColumns) {
  114. AvailableColumnsPanel* this = AllocThis(AvailableColumnsPanel);
  115. Panel* super = (Panel*) this;
  116. FunctionBar* fuBar = FunctionBar_new(AvailableColumnsFunctions, NULL, NULL);
  117. Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);
  118. Panel_setHeader(super, "Available Columns");
  119. this->columns = columns;
  120. AvailableColumnsPanel_fill(this, NULL, dynamicColumns);
  121. return this;
  122. }