ColorsPanel.c 2.7 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. ColorsPanel* this = (ColorsPanel*) object;
  35. Panel_done(&this->super);
  36. free(this);
  37. }
  38. static HandlerResult ColorsPanel_eventHandler(Panel* super, int ch) {
  39. ColorsPanel* this = (ColorsPanel*) super;
  40. HandlerResult result = IGNORED;
  41. switch (ch) {
  42. case 0x0a:
  43. case 0x0d:
  44. case KEY_ENTER:
  45. case KEY_MOUSE:
  46. case KEY_RECLICK:
  47. case ' ': {
  48. int mark = Panel_getSelectedIndex(super);
  49. assert(mark >= 0);
  50. assert(mark < LAST_COLORSCHEME);
  51. for (int i = 0; ColorSchemeNames[i] != NULL; i++)
  52. CheckItem_set((CheckItem*)Panel_get(super, i), false);
  53. CheckItem_set((CheckItem*)Panel_get(super, mark), true);
  54. this->settings->colorScheme = mark;
  55. this->settings->changed = true;
  56. this->settings->lastUpdate++;
  57. CRT_setColors(mark);
  58. clear();
  59. result = HANDLED | REDRAW;
  60. }
  61. }
  62. return result;
  63. }
  64. const PanelClass ColorsPanel_class = {
  65. .super = {
  66. .extends = Class(Panel),
  67. .delete = ColorsPanel_delete
  68. },
  69. .eventHandler = ColorsPanel_eventHandler
  70. };
  71. ColorsPanel* ColorsPanel_new(Settings* settings) {
  72. ColorsPanel* this = AllocThis(ColorsPanel);
  73. Panel* super = &this->super;
  74. FunctionBar* fuBar = FunctionBar_new(ColorsFunctions, NULL, NULL);
  75. Panel_init(super, 1, 1, 1, 1, Class(CheckItem), true, fuBar);
  76. this->settings = settings;
  77. assert(ARRAYSIZE(ColorSchemeNames) == LAST_COLORSCHEME + 1);
  78. Panel_setHeader(super, "Colors");
  79. for (int i = 0; ColorSchemeNames[i] != NULL; i++) {
  80. Panel_add(super, (Object*) CheckItem_newByVal(ColorSchemeNames[i], false));
  81. }
  82. CheckItem_set((CheckItem*)Panel_get(super, settings->colorScheme), true);
  83. return this;
  84. }