blob: 0d024f8c8afcf32c73a1050b900abc42b7de1af1 [file] [log] [blame]
Christian Göttsche9060a412020-12-25 16:42:35 +01001/*
2htop - HeaderOptionsPanel.c
3(C) 2021 htop dev team
Daniel Lange94ad1112021-09-22 11:33:00 +02004Released under the GNU GPLv2+, see the COPYING file
Christian Göttsche9060a412020-12-25 16:42:35 +01005in the source distribution for its full text.
6*/
7
Benny Baumann64905902023-12-11 10:04:39 +01008#include "config.h" // IWYU pragma: keep
9
Christian Göttsche9060a412020-12-25 16:42:35 +010010#include "HeaderOptionsPanel.h"
11
Christian Göttsche2bf626c2021-08-24 17:27:43 +020012#include <assert.h>
Christian Göttsche9060a412020-12-25 16:42:35 +010013#include <stdbool.h>
14#include <stdlib.h>
15
16#include "CRT.h"
17#include "FunctionBar.h"
18#include "Header.h"
Christian Göttsche2bf626c2021-08-24 17:27:43 +020019#include "HeaderLayout.h"
Christian Göttsche9060a412020-12-25 16:42:35 +010020#include "Object.h"
21#include "OptionItem.h"
22#include "ProvideCurses.h"
Christian Göttsche9060a412020-12-25 16:42:35 +010023
24
25static const char* const HeaderOptionsFunctions[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", "Done ", NULL};
26
27static void HeaderOptionsPanel_delete(Object* object) {
Christian Göttsche9060a412020-12-25 16:42:35 +010028 HeaderOptionsPanel* this = (HeaderOptionsPanel*) object;
Benny Baumann24b881c2025-02-20 09:18:47 +010029 Panel_done(&this->super);
Christian Göttsche9060a412020-12-25 16:42:35 +010030 free(this);
31}
32
33static HandlerResult HeaderOptionsPanel_eventHandler(Panel* super, int ch) {
34 HeaderOptionsPanel* this = (HeaderOptionsPanel*) super;
35
36 HandlerResult result = IGNORED;
Christian Göttsche9060a412020-12-25 16:42:35 +010037
Sohaib Mohamed21cb1c42021-11-04 21:40:39 +020038 switch (ch) {
Benny Baumann6aa9ef22023-11-23 12:22:02 +010039 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öttsche9060a412020-12-25 16:42:35 +010048
Benny Baumann6aa9ef22023-11-23 12:22:02 +010049 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öttsche9060a412020-12-25 16:42:35 +010052
Benny Baumann6aa9ef22023-11-23 12:22:02 +010053 Header_setLayout(this->scr->header, mark);
54 this->settings->changed = true;
55 this->settings->lastUpdate++;
Christian Göttsche9060a412020-12-25 16:42:35 +010056
Benny Baumann6aa9ef22023-11-23 12:22:02 +010057 ScreenManager_resize(this->scr);
Christian Göttsche9060a412020-12-25 16:42:35 +010058
Benny Baumann6aa9ef22023-11-23 12:22:02 +010059 result = HANDLED;
60 }
Christian Göttsche9060a412020-12-25 16:42:35 +010061 }
62
63 return result;
64}
65
66const PanelClass HeaderOptionsPanel_class = {
67 .super = {
68 .extends = Class(Panel),
69 .delete = HeaderOptionsPanel_delete
70 },
71 .eventHandler = HeaderOptionsPanel_eventHandler
72};
73
74HeaderOptionsPanel* HeaderOptionsPanel_new(Settings* settings, ScreenManager* scr) {
75 HeaderOptionsPanel* this = AllocThis(HeaderOptionsPanel);
Benny Baumann24b881c2025-02-20 09:18:47 +010076 Panel* super = &this->super;
77
Christian Göttsche9060a412020-12-25 16:42:35 +010078 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öttschedd885102021-09-10 17:00:50 +020088 CheckItem_set((CheckItem*)Panel_get(super, scr->header->headerLayout), true);
Christian Göttsche9060a412020-12-25 16:42:35 +010089 return this;
90}