ColorsPanel.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. htop - ColorsPanel.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 "ColorsPanel.h"
  9. #include <assert.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include "CRT.h"
  13. #include "FunctionBar.h"
  14. #include "Object.h"
  15. #include "OptionItem.h"
  16. #include "ProvideCurses.h"
  17. // TO ADD A NEW SCHEME:
  18. // * Increment the size of bool check in ColorsPanel.h
  19. // * Add the entry in the ColorSchemeNames array below in the file
  20. // * Add a define in CRT.h that matches the order of the array
  21. // * Add the colors in CRT_setColors
  22. static const char* const ColorsFunctions[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", "Done ", NULL};
  23. static const char* const ColorSchemeNames[] = {
  24. "Default",
  25. "Monochromatic",
  26. "Black on White",
  27. "Light Terminal",
  28. "MC",
  29. "Black Night",
  30. "Broken Gray",
  31. NULL
  32. };
  33. static void ColorsPanel_delete(Object* object) {
  34. Panel* super = (Panel*) object;
  35. ColorsPanel* this = (ColorsPanel*) object;
  36. Panel_done(super);
  37. free(this);
  38. }
  39. static HandlerResult ColorsPanel_eventHandler(Panel* super, int ch) {
  40. ColorsPanel* this = (ColorsPanel*) super;
  41. HandlerResult result = IGNORED;
  42. switch (ch) {
  43. case 0x0a:
  44. case 0x0d:
  45. case KEY_ENTER:
  46. case KEY_MOUSE:
  47. case KEY_RECLICK:
  48. case ' ': {
  49. int mark = Panel_getSelectedIndex(super);
  50. assert(mark >= 0);
  51. assert(mark < LAST_COLORSCHEME);
  52. for (int i = 0; ColorSchemeNames[i] != NULL; i++)
  53. CheckItem_set((CheckItem*)Panel_get(super, i), false);
  54. CheckItem_set((CheckItem*)Panel_get(super, mark), true);
  55. this->settings->colorScheme = mark;
  56. this->settings->changed = true;
  57. this->settings->lastUpdate++;
  58. CRT_setColors(mark);
  59. clear();
  60. result = HANDLED | REDRAW;
  61. }
  62. }
  63. return result;
  64. }
  65. const PanelClass ColorsPanel_class = {
  66. .super = {
  67. .extends = Class(Panel),
  68. .delete = ColorsPanel_delete
  69. },
  70. .eventHandler = ColorsPanel_eventHandler
  71. };
  72. ColorsPanel* ColorsPanel_new(Settings* settings) {
  73. ColorsPanel* this = AllocThis(ColorsPanel);
  74. Panel* super = (Panel*) this;
  75. FunctionBar* fuBar = FunctionBar_new(ColorsFunctions, NULL, NULL);
  76. Panel_init(super, 1, 1, 1, 1, Class(CheckItem), true, fuBar);
  77. this->settings = settings;
  78. assert(ARRAYSIZE(ColorSchemeNames) == LAST_COLORSCHEME + 1);
  79. Panel_setHeader(super, "Colors");
  80. for (int i = 0; ColorSchemeNames[i] != NULL; i++) {
  81. Panel_add(super, (Object*) CheckItem_newByVal(ColorSchemeNames[i], false));
  82. }
  83. CheckItem_set((CheckItem*)Panel_get(super, settings->colorScheme), true);
  84. return this;
  85. }