B15F Board 15 Famulus Edition

view_selection.cpp
1 #include "view_selection.h"
2
3 void ViewSelection::draw()
4 {
5 //curs_set(0); // hide cursor
6 for(size_t i = 0; i < choices.size(); i++)
7 {
8 if(selection == i)
9 wattron(win, A_REVERSE);
10 mvwprintw(win, i + choice_offset_y, choice_offset_x, "%s", choices[i].c_str());
11 if(selection == i)
12 wattroff(win, A_REVERSE);
13 }
14 }
15
16 void ViewSelection::addChoice(std::string name, call_t call)
17 {
18 choices.push_back(name);
19 calls.push_back(call);
20 }
21
22 call_t ViewSelection::keypress(int& key)
23 {
24 call_t ret = nullptr;
25 switch(key)
26 {
27 case KEY_UP:
28 do
29 selection = (selection - 1 + choices.size()) % choices.size();
30 while(!choices[selection].length() && choices.size());
31 break;
32
33 case '\t':
34 case KEY_DOWN:
35 do
36 selection = (selection + 1) % choices.size();
37 while(!choices[selection].length() && choices.size());
38 break;
39
40 case KEY_MOUSE:
41 {
42 // http://pronix.linuxdelta.de/C/Linuxprogrammierung/Linuxsystemprogrammieren_C_Kurs_Kapitel10b.shtml
43 MEVENT event;
44 bool hit = false;
45 if(getmouse(&event) == OK && event.bstate & (BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED))
46 {
47 size_t column_start = start_x + choice_offset_x;
48 size_t row_start = start_y + choice_offset_y;
49 size_t mouse_x = event.x, mouse_y = event.y;
50 for(size_t i = 0; i < choices.size(); i++)
51 if(choices[i].length() && mouse_y == row_start + i && mouse_x >= column_start && mouse_x < column_start + choices[i].length())
52 {
53 if(selection == i || event.bstate & BUTTON1_DOUBLE_CLICKED)
54 hit = true;
55 selection = i;
56 }
57 }
58 if(!hit)
59 break;
60
61 // fall through to next case
62 [[fallthrough]];
63 }
64
65 case KEY_ENT:
66 if(selection == choices.size() - 1) // exit
67 key = -1; // do return from view
68 else
69 ret = calls[selection];
70 break;
71 default:
72 break;
73 }
74 repaint();
75 return ret;
76 }