Make windows in tests non-closable
[centerim5.git] / tests / scrollpane.cpp
blob5565c6ed218469627725fda18506b25a5451f476
1 #include <cppconsui/Button.h>
2 #include <cppconsui/CoreManager.h>
3 #include <cppconsui/KeyConfig.h>
4 #include <cppconsui/Label.h>
5 #include <cppconsui/ScrollPane.h>
6 #include <cppconsui/Window.h>
8 #include <stdio.h>
10 static const char *pic[] =
12 " ______ __ __ __ __ ",
13 "| |.-----.-----.-----.----.---.-.| |_.--.--.| |.---.-.| |_|__|.-----.-----.-----. ",
14 "| ---|| _ | | _ | _| _ || _| | || || _ || _| || _ | |__ --|__ ",
15 "|______||_____|__|__|___ |__| |___._||____|_____||__||___._||____|__||_____|__|__|_____| |",
16 " |_____| |_|",
17 " __ __ __ __ _______ __ ",
18 ".--.--.-----.--.--. |__|.--.--.-----.| |_ | |.-----.-----.| |_ |_ _| |--.-----. ",
19 "| | | _ | | | | || | |__ --|| _| | || _ |__ --|| _| | | | | -__| ",
20 "|___ |_____|_____| | ||_____|_____||____| |__||_____|_____||____| |___| |__|__|_____| ",
21 "|_____| |___| ",
22 " _______ ",
23 "| __|.---.-.--------.-----. ",
24 "| | || _ | | -__|__ ",
25 "|_______||___._|__|__|__|_____|__| "
29 // MyScrollPane class
30 class MyScrollPane
31 : 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 protected:
42 private:
43 MyScrollPane(const MyScrollPane&);
44 MyScrollPane& operator=(const MyScrollPane&);
47 MyScrollPane::MyScrollPane(int w, int h, int scrollw, int scrollh)
48 : CppConsUI::ScrollPane(w, h, scrollw, scrollh)
52 void MyScrollPane::draw()
54 proceedUpdateArea();
55 proceedUpdateVirtualArea();
57 if (!area) {
58 // scrollpane will clear the scroll (real) area
59 ScrollPane::draw();
60 return;
63 area->fill(getColorPair("container", "background"));
65 int real_height = area->getmaxy();
66 for (int i = 0; i < real_height && i < (int) (sizeof(pic) / sizeof(pic[0]));
67 i++)
68 area->mvaddstring(0, i, pic[i]);
70 ScrollPane::drawEx(false);
73 // TestWindow class
74 class TestWindow
75 : public CppConsUI::Window
77 public:
78 TestWindow();
79 virtual ~TestWindow() {}
81 protected:
82 MyScrollPane *pane;
84 private:
85 TestWindow(const TestWindow&);
86 TestWindow& operator=(const TestWindow&);
88 void actionScrollUp();
89 void actionScrollDown();
90 void actionScrollLeft();
91 void actionScrollRight();
94 TestWindow::TestWindow()
95 : CppConsUI::Window(0, 0, AUTOSIZE, AUTOSIZE)
97 setClosable(false);
99 addWidget(*(new CppConsUI::Label(25, 1, "Press F10 to quit.")), 1, 1);
100 addWidget(*(new CppConsUI::Label(25, 1, "WASD to move the picture.")),
101 1, 2);
103 pane = new MyScrollPane(20, 10, 111, 23);
104 addWidget(*pane, 1, 4);
106 declareBindable("scrollpanewindow", "scroll-up",
107 sigc::mem_fun(this, &TestWindow::actionScrollUp),
108 InputProcessor::BINDABLE_NORMAL);
109 declareBindable("scrollpanewindow", "scroll-down",
110 sigc::mem_fun(this, &TestWindow::actionScrollDown),
111 InputProcessor::BINDABLE_NORMAL);
112 declareBindable("scrollpanewindow", "scroll-left",
113 sigc::mem_fun(this, &TestWindow::actionScrollLeft),
114 InputProcessor::BINDABLE_NORMAL);
115 declareBindable("scrollpanewindow", "scroll-right",
116 sigc::mem_fun(this, &TestWindow::actionScrollRight),
117 InputProcessor::BINDABLE_NORMAL);
120 void TestWindow::actionScrollUp()
122 pane->adjustScroll(pane->getScrollPositionX(),
123 pane->getScrollPositionY() - 1);
126 void TestWindow::actionScrollDown()
128 pane->adjustScroll(pane->getScrollPositionX(),
129 pane->getScrollPositionY() + 1);
132 void TestWindow::actionScrollLeft()
134 pane->adjustScroll(pane->getScrollPositionX() - 1,
135 pane->getScrollPositionY());
138 void TestWindow::actionScrollRight()
140 pane->adjustScroll(pane->getScrollPositionX() + 1,
141 pane->getScrollPositionY());
144 // TestApp class
145 class TestApp
146 : public CppConsUI::InputProcessor
148 public:
149 TestApp();
150 virtual ~TestApp() {}
152 void run();
154 // ignore every message
155 static void g_log_func_(const gchar * /*log_domain*/,
156 GLogLevelFlags /*log_level*/, const gchar * /*message*/,
157 gpointer /*user_data*/)
160 protected:
162 private:
163 TestApp(const TestApp&);
164 TestApp& operator=(const TestApp&);
167 TestApp::TestApp()
169 KEYCONFIG->loadDefaultKeyConfig();
170 KEYCONFIG->bindKey("testapp", "quit", "F10");
171 KEYCONFIG->bindKey("scrollpanewindow", "scroll-up", "w");
172 KEYCONFIG->bindKey("scrollpanewindow", "scroll-down", "s");
173 KEYCONFIG->bindKey("scrollpanewindow", "scroll-left", "a");
174 KEYCONFIG->bindKey("scrollpanewindow", "scroll-right", "d");
176 g_log_set_default_handler(g_log_func_, this);
178 declareBindable("testapp", "quit", sigc::mem_fun(COREMANAGER,
179 &CppConsUI::CoreManager::quitMainLoop),
180 InputProcessor::BINDABLE_OVERRIDE);
183 void TestApp::run()
185 TestWindow *win = new TestWindow;
186 win->show();
188 COREMANAGER->setTopInputProcessor(*this);
189 COREMANAGER->enableResizing();
190 COREMANAGER->startMainLoop();
193 // main function
194 int main()
196 setlocale(LC_ALL, "");
198 // initialize CppConsUI
199 int consui_res = CppConsUI::initializeConsUI();
200 if (consui_res) {
201 fprintf(stderr, "CppConsUI initialization failed.\n");
202 return consui_res;
205 TestApp *app = new TestApp;
206 app->run();
207 delete app;
209 // finalize CppConsUI
210 consui_res = CppConsUI::finalizeConsUI();
211 if (consui_res) {
212 fprintf(stderr, "CppConsUI deinitialization failed.\n");
213 return consui_res;
216 return 0;
219 /* vim: set tabstop=2 shiftwidth=2 textwidth=78 expandtab : */