blob: d8148dfe73c99846d7736082eda04238c392d379 [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
8#include "HeaderOptionsPanel.h"
9
Christian Göttsche2bf626c2021-08-24 17:27:43 +020010#include <assert.h>
Christian Göttsche9060a412020-12-25 16:42:35 +010011#include <stdbool.h>
12#include <stdlib.h>
13
14#include "CRT.h"
15#include "FunctionBar.h"
16#include "Header.h"
Christian Göttsche2bf626c2021-08-24 17:27:43 +020017#include "HeaderLayout.h"
Christian Göttsche9060a412020-12-25 16:42:35 +010018#include "Object.h"
19#include "OptionItem.h"
20#include "ProvideCurses.h"
Christian Göttsche9060a412020-12-25 16:42:35 +010021
22
23static const char* const HeaderOptionsFunctions[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", "Done ", NULL};
24
25static void HeaderOptionsPanel_delete(Object* object) {
26 Panel* super = (Panel*) object;
27 HeaderOptionsPanel* this = (HeaderOptionsPanel*) object;
28 Panel_done(super);
29 free(this);
30}
31
32static 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
64const PanelClass HeaderOptionsPanel_class = {
65 .super = {
66 .extends = Class(Panel),
67 .delete = HeaderOptionsPanel_delete
68 },
69 .eventHandler = HeaderOptionsPanel_eventHandler
70};
71
72HeaderOptionsPanel* 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öttschedd885102021-09-10 17:00:50 +020085 CheckItem_set((CheckItem*)Panel_get(super, scr->header->headerLayout), true);
Christian Göttsche9060a412020-12-25 16:42:35 +010086 return this;
87}