| 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 | |
| Benny Baumann | 6490590 | 2023-12-11 10:04:39 +0100 | [diff] [blame] | 8 | #include "config.h" // IWYU pragma: keep |
| 9 | |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 10 | #include "HeaderOptionsPanel.h" |
| 11 | |
| Christian Göttsche | 2bf626c | 2021-08-24 17:27:43 +0200 | [diff] [blame] | 12 | #include <assert.h> |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 13 | #include <stdbool.h> |
| 14 | #include <stdlib.h> |
| 15 | |
| 16 | #include "CRT.h" |
| 17 | #include "FunctionBar.h" |
| 18 | #include "Header.h" |
| Christian Göttsche | 2bf626c | 2021-08-24 17:27:43 +0200 | [diff] [blame] | 19 | #include "HeaderLayout.h" |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 20 | #include "Object.h" |
| 21 | #include "OptionItem.h" |
| 22 | #include "ProvideCurses.h" |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 23 | |
| 24 | |
| 25 | static const char* const HeaderOptionsFunctions[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", "Done ", NULL}; |
| 26 | |
| 27 | static void HeaderOptionsPanel_delete(Object* object) { |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 28 | HeaderOptionsPanel* this = (HeaderOptionsPanel*) object; |
| Benny Baumann | 24b881c | 2025-02-20 09:18:47 +0100 | [diff] [blame] | 29 | Panel_done(&this->super); |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 30 | free(this); |
| 31 | } |
| 32 | |
| 33 | static HandlerResult HeaderOptionsPanel_eventHandler(Panel* super, int ch) { |
| 34 | HeaderOptionsPanel* this = (HeaderOptionsPanel*) super; |
| 35 | |
| 36 | HandlerResult result = IGNORED; |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 37 | |
| Sohaib Mohamed | 21cb1c4 | 2021-11-04 21:40:39 +0200 | [diff] [blame] | 38 | switch (ch) { |
| Benny Baumann | 6aa9ef2 | 2023-11-23 12:22:02 +0100 | [diff] [blame] | 39 | case 0x0a: |
| 40 | case 0x0d: |
| 41 | case KEY_ENTER: |
| 42 | case KEY_MOUSE: |
| 43 | case KEY_RECLICK: |
| 44 | case ' ': { |
| 45 | int mark = Panel_getSelectedIndex(super); |
| 46 | assert(mark >= 0); |
| 47 | assert(mark < LAST_HEADER_LAYOUT); |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 48 | |
| Benny Baumann | 6aa9ef2 | 2023-11-23 12:22:02 +0100 | [diff] [blame] | 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); |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 52 | |
| Benny Baumann | 6aa9ef2 | 2023-11-23 12:22:02 +0100 | [diff] [blame] | 53 | Header_setLayout(this->scr->header, mark); |
| 54 | this->settings->changed = true; |
| 55 | this->settings->lastUpdate++; |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 56 | |
| Benny Baumann | 6aa9ef2 | 2023-11-23 12:22:02 +0100 | [diff] [blame] | 57 | ScreenManager_resize(this->scr); |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 58 | |
| Benny Baumann | 6aa9ef2 | 2023-11-23 12:22:02 +0100 | [diff] [blame] | 59 | result = HANDLED; |
| 60 | } |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return result; |
| 64 | } |
| 65 | |
| 66 | const PanelClass HeaderOptionsPanel_class = { |
| 67 | .super = { |
| 68 | .extends = Class(Panel), |
| 69 | .delete = HeaderOptionsPanel_delete |
| 70 | }, |
| 71 | .eventHandler = HeaderOptionsPanel_eventHandler |
| 72 | }; |
| 73 | |
| 74 | HeaderOptionsPanel* HeaderOptionsPanel_new(Settings* settings, ScreenManager* scr) { |
| 75 | HeaderOptionsPanel* this = AllocThis(HeaderOptionsPanel); |
| Benny Baumann | 24b881c | 2025-02-20 09:18:47 +0100 | [diff] [blame] | 76 | Panel* super = &this->super; |
| 77 | |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 78 | FunctionBar* fuBar = FunctionBar_new(HeaderOptionsFunctions, NULL, NULL); |
| 79 | Panel_init(super, 1, 1, 1, 1, Class(CheckItem), true, fuBar); |
| 80 | |
| 81 | this->scr = scr; |
| 82 | this->settings = settings; |
| 83 | |
| 84 | Panel_setHeader(super, "Header Layout"); |
| 85 | for (int i = 0; i < LAST_HEADER_LAYOUT; i++) { |
| 86 | Panel_add(super, (Object*) CheckItem_newByVal(HeaderLayout_layouts[i].description, false)); |
| 87 | } |
| Christian Göttsche | dd88510 | 2021-09-10 17:00:50 +0200 | [diff] [blame] | 88 | CheckItem_set((CheckItem*)Panel_get(super, scr->header->headerLayout), true); |
| Christian Göttsche | 9060a41 | 2020-12-25 16:42:35 +0100 | [diff] [blame] | 89 | return this; |
| 90 | } |