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>
13 static const char *pic
[] = {
14 " ______ __ __ __ __ ",
15 "| |.-----.-----.-----.----.---.-.| |_.--.--.| |.---.-.| |_|__|.-----.-----.-----. ",
16 "| ---|| _ | | _ | _| _ || _| | || || _ || _| || _ | |__ --|__ ",
17 "|______||_____|__|__|___ |__| |___._||____|_____||__||___._||____|__||_____|__|__|_____| |",
19 " __ __ __ __ _______ __ ",
20 ".--.--.-----.--.--. |__|.--.--.-----.| |_ | |.-----.-----.| |_ |_ _| |--.-----. ",
21 "| | | _ | | | | || | |__ --|| _| | || _ |__ --|| _| | | | | -__| ",
22 "|___ |_____|_____| | ||_____|_____||____| |__||_____|_____||____| |___| |__|__|_____| ",
25 "| __|.---.-.--------.-----. ",
26 "| | || _ | | -__|__ ",
27 "|_______||___._|__|__|__|_____|__| "
32 class MyScrollPane
: public CppConsUI::ScrollPane
{
34 MyScrollPane(int w
, int h
, int scrollw
, int scrollh
);
35 virtual ~MyScrollPane() {}
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()
52 // scrollpane will clear the scroll (real) area
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]));
62 area
->mvaddstring(0, i
, pic
[i
]);
64 ScrollPane::drawEx(false);
68 class TestWindow
: public CppConsUI::Window
{
71 virtual ~TestWindow() {}
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
)
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()
112 pane
->getScrollPositionX(), pane
->getScrollPositionY() - 1);
115 void TestWindow::actionScrollDown()
118 pane
->getScrollPositionX(), pane
->getScrollPositionY() + 1);
121 void TestWindow::actionScrollLeft()
124 pane
->getScrollPositionX() - 1, pane
->getScrollPositionY());
127 void TestWindow::actionScrollRight()
130 pane
->getScrollPositionX() + 1, pane
->getScrollPositionY());
134 class TestApp
: public CppConsUI::InputProcessor
{
139 static TestApp
*my_instance
;
141 static void log_error_cppconsui(const char *message
);
144 virtual ~TestApp() {}
147 CONSUI_DISABLE_COPY(TestApp
);
150 TestApp
*TestApp::my_instance
= nullptr;
155 assert(!my_instance
);
156 my_instance
= new TestApp
;
159 int res
= my_instance
->runAll();
161 // finalize my instance
165 my_instance
= nullptr;
170 void TestApp::log_error_cppconsui(const char * /*message*/)
172 // ignore all messages
175 int TestApp::runAll()
178 bool mainloop_initialized
= false;
179 bool cppconsui_initialized
= false;
182 // init locale support
183 setlocale(LC_ALL
, "");
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
);
195 std::cerr
<< "CppConsUI initialization failed." << std::endl
;
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
;
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");
217 COREMANAGER
->setTopInputProcessor(*this);
218 COREMANAGER
->enableResizing();
221 // everything went ok
225 // finalize CppConsUI
226 if (cppconsui_initialized
) {
227 if (CppConsUI::finalizeConsUI())
228 std::cerr
<< "CppConsUI finalization failed." << std::endl
;
232 if (mainloop_initialized
)
233 MainLoop::finalize();
241 return TestApp::run();
244 /* vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab : */