Update git-version-gen from gnulib to version 2017-01-09.19
[centerim5.git] / tests / scrollpane.cpp
blobed16ee3273a3516bb7bac54d5a86ccadc13d80a3
1 #include <MainLoop.h>
3 #include <cppconsui/Button.h>
4 #include <cppconsui/CoreManager.h>
5 #include <cppconsui/KeyConfig.h>
6 #include <cppconsui/Label.h>
7 #include <cppconsui/ScrollPane.h>
8 #include <cppconsui/Window.h>
10 #include <iostream>
12 // clang-format off
13 static const char *pic[] = {
14 " ______ __ __ __ __ ",
15 "| |.-----.-----.-----.----.---.-.| |_.--.--.| |.---.-.| |_|__|.-----.-----.-----. ",
16 "| ---|| _ | | _ | _| _ || _| | || || _ || _| || _ | |__ --|__ ",
17 "|______||_____|__|__|___ |__| |___._||____|_____||__||___._||____|__||_____|__|__|_____| |",
18 " |_____| |_|",
19 " __ __ __ __ _______ __ ",
20 ".--.--.-----.--.--. |__|.--.--.-----.| |_ | |.-----.-----.| |_ |_ _| |--.-----. ",
21 "| | | _ | | | | || | |__ --|| _| | || _ |__ --|| _| | | | | -__| ",
22 "|___ |_____|_____| | ||_____|_____||____| |__||_____|_____||____| |___| |__|__|_____| ",
23 "|_____| |___| ",
24 " _______ ",
25 "| __|.---.-.--------.-----. ",
26 "| | || _ | | -__|__ ",
27 "|_______||___._|__|__|__|_____|__| "
29 // clang-format on
31 // MyScrollPane class
32 class MyScrollPane : public CppConsUI::ScrollPane {
33 public:
34 MyScrollPane(int w, int h, int scrollw, int scrollh);
35 virtual ~MyScrollPane() {}
37 // Widget
38 virtual void draw();
40 private:
41 CONSUI_DISABLE_COPY(MyScrollPane);
44 MyScrollPane::MyScrollPane(int w, int h, int scrollw, int scrollh)
45 : CppConsUI::ScrollPane(w, h, scrollw, scrollh)
49 void MyScrollPane::draw()
51 if (!area) {
52 // scrollpane will clear the scroll (real) area
53 ScrollPane::draw();
54 return;
57 area->fill(getColorPair("container", "background"));
59 int real_height = area->getmaxy();
60 for (int i = 0; i < real_height && i < (int)(sizeof(pic) / sizeof(pic[0]));
61 i++)
62 area->mvaddstring(0, i, pic[i]);
64 ScrollPane::drawEx(false);
67 // TestWindow class
68 class TestWindow : public CppConsUI::Window {
69 public:
70 TestWindow();
71 virtual ~TestWindow() {}
73 protected:
74 MyScrollPane *pane;
76 private:
77 void actionScrollUp();
78 void actionScrollDown();
79 void actionScrollLeft();
80 void actionScrollRight();
82 CONSUI_DISABLE_COPY(TestWindow);
85 TestWindow::TestWindow() : CppConsUI::Window(0, 0, AUTOSIZE, AUTOSIZE)
87 setClosable(false);
89 addWidget(*(new CppConsUI::Label(25, 1, "Press F10 to quit.")), 1, 1);
90 addWidget(*(new CppConsUI::Label(25, 1, "WASD to move the picture.")), 1, 2);
92 pane = new MyScrollPane(20, 10, 111, 23);
93 addWidget(*pane, 1, 4);
95 declareBindable("scrollpanewindow", "scroll-up",
96 sigc::mem_fun(this, &TestWindow::actionScrollUp),
97 InputProcessor::BINDABLE_NORMAL);
98 declareBindable("scrollpanewindow", "scroll-down",
99 sigc::mem_fun(this, &TestWindow::actionScrollDown),
100 InputProcessor::BINDABLE_NORMAL);
101 declareBindable("scrollpanewindow", "scroll-left",
102 sigc::mem_fun(this, &TestWindow::actionScrollLeft),
103 InputProcessor::BINDABLE_NORMAL);
104 declareBindable("scrollpanewindow", "scroll-right",
105 sigc::mem_fun(this, &TestWindow::actionScrollRight),
106 InputProcessor::BINDABLE_NORMAL);
109 void TestWindow::actionScrollUp()
111 pane->adjustScroll(
112 pane->getScrollPositionX(), pane->getScrollPositionY() - 1);
115 void TestWindow::actionScrollDown()
117 pane->adjustScroll(
118 pane->getScrollPositionX(), pane->getScrollPositionY() + 1);
121 void TestWindow::actionScrollLeft()
123 pane->adjustScroll(
124 pane->getScrollPositionX() - 1, pane->getScrollPositionY());
127 void TestWindow::actionScrollRight()
129 pane->adjustScroll(
130 pane->getScrollPositionX() + 1, pane->getScrollPositionY());
133 // TestApp class
134 class TestApp : public CppConsUI::InputProcessor {
135 public:
136 static int run();
138 private:
139 static TestApp *my_instance;
141 static void log_error_cppconsui(const char *message);
143 TestApp() {}
144 virtual ~TestApp() {}
145 int runAll();
147 CONSUI_DISABLE_COPY(TestApp);
150 TestApp *TestApp::my_instance = nullptr;
152 int TestApp::run()
154 // init my instance
155 assert(!my_instance);
156 my_instance = new TestApp;
158 // run the program
159 int res = my_instance->runAll();
161 // finalize my instance
162 assert(my_instance);
164 delete my_instance;
165 my_instance = nullptr;
167 return res;
170 void TestApp::log_error_cppconsui(const char * /*message*/)
172 // ignore all messages
175 int TestApp::runAll()
177 int res = 1;
178 bool mainloop_initialized = false;
179 bool cppconsui_initialized = false;
180 TestWindow *win;
182 // init locale support
183 setlocale(LC_ALL, "");
185 // init mainloop
186 MainLoop::init();
187 mainloop_initialized = true;
189 // initialize CppConsUI
190 CppConsUI::AppInterface interface = {MainLoop::timeout_add_cppconsui,
191 MainLoop::timeout_remove_cppconsui, MainLoop::input_add_cppconsui,
192 MainLoop::input_remove_cppconsui, log_error_cppconsui};
193 int consui_res = CppConsUI::initializeConsUI(interface);
194 if (consui_res) {
195 std::cerr << "CppConsUI initialization failed." << std::endl;
196 goto out;
198 cppconsui_initialized = true;
200 // declare local bindables
201 declareBindable("testapp", "quit", sigc::ptr_fun(MainLoop::quit),
202 InputProcessor::BINDABLE_OVERRIDE);
204 // create the main window
205 win = new TestWindow;
206 win->show();
208 // setup key binds
209 KEYCONFIG->loadDefaultKeyConfig();
210 KEYCONFIG->bindKey("testapp", "quit", "F10");
211 KEYCONFIG->bindKey("scrollpanewindow", "scroll-up", "w");
212 KEYCONFIG->bindKey("scrollpanewindow", "scroll-down", "s");
213 KEYCONFIG->bindKey("scrollpanewindow", "scroll-left", "a");
214 KEYCONFIG->bindKey("scrollpanewindow", "scroll-right", "d");
216 // run the main loop
217 COREMANAGER->setTopInputProcessor(*this);
218 COREMANAGER->enableResizing();
219 MainLoop::run();
221 // everything went ok
222 res = 0;
224 out:
225 // finalize CppConsUI
226 if (cppconsui_initialized) {
227 if (CppConsUI::finalizeConsUI())
228 std::cerr << "CppConsUI finalization failed." << std::endl;
231 // finalize mainloop
232 if (mainloop_initialized)
233 MainLoop::finalize();
235 return res;
238 // main function
239 int main()
241 return TestApp::run();
244 /* vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab : */