HeaderOptionsPanel.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. htop - HeaderOptionsPanel.c
  3. (C) 2021 htop dev team
  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 "HeaderOptionsPanel.h"
  9. #include <assert.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include "CRT.h"
  13. #include "FunctionBar.h"
  14. #include "Header.h"
  15. #include "HeaderLayout.h"
  16. #include "Object.h"
  17. #include "OptionItem.h"
  18. #include "ProvideCurses.h"
  19. static const char* const HeaderOptionsFunctions[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", "Done ", NULL};
  20. static void HeaderOptionsPanel_delete(Object* object) {
  21. Panel* super = (Panel*) object;
  22. HeaderOptionsPanel* this = (HeaderOptionsPanel*) object;
  23. Panel_done(super);
  24. free(this);
  25. }
  26. static HandlerResult HeaderOptionsPanel_eventHandler(Panel* super, int ch) {
  27. HeaderOptionsPanel* this = (HeaderOptionsPanel*) super;
  28. HandlerResult result = IGNORED;
  29. switch (ch) {
  30. case 0x0a:
  31. case 0x0d:
  32. case KEY_ENTER:
  33. case KEY_MOUSE:
  34. case KEY_RECLICK:
  35. case ' ': {
  36. int mark = Panel_getSelectedIndex(super);
  37. assert(mark >= 0);
  38. assert(mark < LAST_HEADER_LAYOUT);
  39. for (int i = 0; i < LAST_HEADER_LAYOUT; i++)
  40. CheckItem_set((CheckItem*)Panel_get(super, i), false);
  41. CheckItem_set((CheckItem*)Panel_get(super, mark), true);
  42. Header_setLayout(this->scr->header, mark);
  43. this->settings->changed = true;
  44. this->settings->lastUpdate++;
  45. ScreenManager_resize(this->scr);
  46. result = HANDLED;
  47. }
  48. }
  49. return result;
  50. }
  51. const PanelClass HeaderOptionsPanel_class = {
  52. .super = {
  53. .extends = Class(Panel),
  54. .delete = HeaderOptionsPanel_delete
  55. },
  56. .eventHandler = HeaderOptionsPanel_eventHandler
  57. };
  58. HeaderOptionsPanel* HeaderOptionsPanel_new(Settings* settings, ScreenManager* scr) {
  59. HeaderOptionsPanel* this = AllocThis(HeaderOptionsPanel);
  60. Panel* super = (Panel*) this;
  61. FunctionBar* fuBar = FunctionBar_new(HeaderOptionsFunctions, NULL, NULL);
  62. Panel_init(super, 1, 1, 1, 1, Class(CheckItem), true, fuBar);
  63. this->scr = scr;
  64. this->settings = settings;
  65. Panel_setHeader(super, "Header Layout");
  66. for (int i = 0; i < LAST_HEADER_LAYOUT; i++) {
  67. Panel_add(super, (Object*) CheckItem_newByVal(HeaderLayout_layouts[i].description, false));
  68. }
  69. CheckItem_set((CheckItem*)Panel_get(super, scr->header->headerLayout), true);
  70. return this;
  71. }