| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | htop - HeaderOptionsPanel.c |
| 3 | (C) 2021 htop dev team |
| Daniel Lange | 94ad111 | 2021-09-22 11:33:00 +0200 | [diff] [blame^] | 4 | Released under the GNU GPLv2+, see the COPYING file |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 5 | in the source distribution for its full text. |
| 6 | */ |
| 7 | |
| 8 | #include "HeaderOptionsPanel.h" |
| 9 | |
| Christian Göttsche | 2bf626c | 2021-08-24 17:27:43 +0200 | [diff] [blame] | 10 | #include <assert.h> |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 11 | #include <stdbool.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | #include "CRT.h" |
| 15 | #include "FunctionBar.h" |
| 16 | #include "Header.h" |
| Christian Göttsche | 2bf626c | 2021-08-24 17:27:43 +0200 | [diff] [blame] | 17 | #include "HeaderLayout.h" |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 18 | #include "Object.h" |
| 19 | #include "OptionItem.h" |
| 20 | #include "ProvideCurses.h" |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 21 | |
| 22 | |
| 23 | static const char* const HeaderOptionsFunctions[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", "Done ", NULL}; |
| 24 | |
| 25 | static void HeaderOptionsPanel_delete(Object* object) { |
| 26 | Panel* super = (Panel*) object; |
| 27 | HeaderOptionsPanel* this = (HeaderOptionsPanel*) object; |
| 28 | Panel_done(super); |
| 29 | free(this); |
| 30 | } |
| 31 | |
| 32 | static HandlerResult HeaderOptionsPanel_eventHandler(Panel* super, int ch) { |
| 33 | HeaderOptionsPanel* this = (HeaderOptionsPanel*) super; |
| 34 | |
| 35 | HandlerResult result = IGNORED; |
| 36 | int mark; |
| 37 | |
| 38 | switch(ch) { |
| 39 | case 0x0a: |
| 40 | case 0x0d: |
| 41 | case KEY_ENTER: |
| 42 | case KEY_MOUSE: |
| 43 | case KEY_RECLICK: |
| 44 | case ' ': |
| 45 | mark = Panel_getSelectedIndex(super); |
| 46 | assert(mark >= 0); |
| 47 | assert(mark < LAST_HEADER_LAYOUT); |
| 48 | |
| 49 | for (int i = 0; i < LAST_HEADER_LAYOUT; i++) |
| 50 | CheckItem_set((CheckItem*)Panel_get(super, i), false); |
| 51 | CheckItem_set((CheckItem*)Panel_get(super, mark), true); |
| 52 | |
| 53 | Header_setLayout(this->scr->header, mark); |
| 54 | this->settings->changed = true; |
| 55 | |
| 56 | ScreenManager_resize(this->scr); |
| 57 | |
| 58 | result = HANDLED; |
| 59 | } |
| 60 | |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | const PanelClass HeaderOptionsPanel_class = { |
| 65 | .super = { |
| 66 | .extends = Class(Panel), |
| 67 | .delete = HeaderOptionsPanel_delete |
| 68 | }, |
| 69 | .eventHandler = HeaderOptionsPanel_eventHandler |
| 70 | }; |
| 71 | |
| 72 | HeaderOptionsPanel* HeaderOptionsPanel_new(Settings* settings, ScreenManager* scr) { |
| 73 | HeaderOptionsPanel* this = AllocThis(HeaderOptionsPanel); |
| 74 | Panel* super = (Panel*) this; |
| 75 | FunctionBar* fuBar = FunctionBar_new(HeaderOptionsFunctions, NULL, NULL); |
| 76 | Panel_init(super, 1, 1, 1, 1, Class(CheckItem), true, fuBar); |
| 77 | |
| 78 | this->scr = scr; |
| 79 | this->settings = settings; |
| 80 | |
| 81 | Panel_setHeader(super, "Header Layout"); |
| 82 | for (int i = 0; i < LAST_HEADER_LAYOUT; i++) { |
| 83 | Panel_add(super, (Object*) CheckItem_newByVal(HeaderLayout_layouts[i].description, false)); |
| 84 | } |
| Christian Göttsche | dd88510 | 2021-09-10 17:00:50 +0200 | [diff] [blame] | 85 | CheckItem_set((CheckItem*)Panel_get(super, scr->header->headerLayout), true); |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 86 | return this; |
| 87 | } |