Make windows in tests non-closable
[centerim5.git] / tests / colorpicker.cpp
blob121daac21bf4b320b315288d773f9dc34cab5202
1 #include <cppconsui/Button.h>
2 #include <cppconsui/ConsUICurses.h>
3 #include <cppconsui/CoreManager.h>
4 #include <cppconsui/ColorPicker.h>
5 #include <cppconsui/ColorPickerComboBox.h>
6 #include <cppconsui/ColorPickerDialog.h>
7 #include <cppconsui/KeyConfig.h>
8 #include <cppconsui/Window.h>
10 #include <stdio.h>
12 // TestWindow class
13 class TestWindow
14 : public CppConsUI::Window
16 public:
17 TestWindow();
18 virtual ~TestWindow() {}
20 protected:
22 private:
23 CppConsUI::Label *label1;
24 CppConsUI::Label *label2;
25 int defaultcolor;
26 CppConsUI::ColorPickerComboBox *combo;
28 TestWindow(const TestWindow&);
29 TestWindow& operator=(const TestWindow&);
31 void onButtonActivate(CppConsUI::Button& activator, int flags);
32 void onChangeColorResponseHandler(CppConsUI::ColorPickerDialog& activator,
33 CppConsUI::AbstractDialog::ResponseType response, int color);
34 void onColerPickerChanged(CppConsUI::ColorPicker& activator, int new_fg,
35 int new_bg);
36 void onComboColorChange(CppConsUI::ComboBox& activator, intptr_t color);
39 TestWindow::TestWindow()
40 : CppConsUI::Window(0, 0, AUTOSIZE, AUTOSIZE), defaultcolor(0)
42 setClosable(false);
44 CppConsUI::Button *button;
46 addWidget(*(new CppConsUI::Label("Press F10 to quit.")), 1, 1);
48 button = new CppConsUI::Button("Open Colorpicker...");
49 addWidget(*button, 1, 3);
50 button->signal_activate.connect(sigc::bind(sigc::mem_fun(this,
51 &TestWindow::onButtonActivate), 0));
53 button = new CppConsUI::Button("Open Colorpicker: ansi only...");
54 addWidget(*button, 1, 4);
55 button->signal_activate.connect(sigc::bind(sigc::mem_fun(this,
56 &TestWindow::onButtonActivate),
57 CppConsUI::ColorPickerPalette::FLAG_HIDE_GRAYSCALE
58 | CppConsUI::ColorPickerPalette::FLAG_HIDE_COLORCUBE));
60 button = new CppConsUI::Button("Open Colorpicker: ansi + Grayscale...");
61 addWidget(*button, 1, 5);
62 button->signal_activate.connect(sigc::bind(sigc::mem_fun(this,
63 &TestWindow::onButtonActivate),
64 CppConsUI::ColorPickerPalette::FLAG_HIDE_COLORCUBE));
66 button = new CppConsUI::Button("Open Colorpicker: color cube only...");
67 addWidget(*button, 1, 6);
68 button->signal_activate.connect(sigc::bind(sigc::mem_fun(this,
69 &TestWindow::onButtonActivate),
70 CppConsUI::ColorPickerPalette::FLAG_HIDE_ANSI
71 | CppConsUI::ColorPickerPalette::FLAG_HIDE_GRAYSCALE));
73 char *text = g_strdup_printf("Supported nr of colors: %d",
74 CppConsUI::Curses::nrcolors());
75 label1 = new CppConsUI::Label(text);
76 g_free(text);
77 addWidget(*label1, 1, 8);
78 label2 = new CppConsUI::Label("...");
79 addWidget(*label2, 1, 10);
81 CppConsUI::Label *l = new CppConsUI::Label("ColorPickerComboBox:");
82 addWidget(*l, 1, 12);
84 l = new CppConsUI::Label;
85 text = g_strdup_printf("Supported nr of color pairs: %d",
86 CppConsUI::Curses::nrcolorpairs());
87 l->setText(text);
88 g_free(text);
89 addWidget(*l, 1, 9);
91 combo = new CppConsUI::ColorPickerComboBox (10, defaultcolor);
92 combo->signal_color_changed.connect(sigc::mem_fun(this,
93 &TestWindow::onComboColorChange));
94 addWidget(*combo, 1, 13);
96 CppConsUI::ColorPicker *picker;
98 addWidget(*(new CppConsUI::Label("ColorPicker:")), 1, 15);
99 picker = new CppConsUI::ColorPicker(7, 1, "Label:", false);
100 picker->signal_colorpair_selected.connect(sigc::mem_fun(this,
101 &TestWindow::onColerPickerChanged));
102 addWidget(*picker, 1, 16);
104 addWidget(*(new CppConsUI::Label("ColorPicker:")), 1, 18);
105 picker = new CppConsUI::ColorPicker(15, 8, "(with sample)", true);
106 picker->signal_colorpair_selected.connect(sigc::mem_fun(this,
107 &TestWindow::onColerPickerChanged));
108 addWidget(*picker, 1, 19);
111 void TestWindow::onButtonActivate(CppConsUI::Button& /*activator*/, int flags)
113 CppConsUI::ColorPickerDialog *dialog = new CppConsUI::ColorPickerDialog(
114 "Test Colorpicker", 0, flags);
115 dialog->signal_response.connect(sigc::mem_fun(this,
116 &TestWindow::onChangeColorResponseHandler));
117 dialog->show();
120 void TestWindow::onColerPickerChanged(CppConsUI::ColorPicker& /*activator*/,
121 int new_fg, int new_bg)
123 char *text = g_strdup_printf("Chosen color (%d,%d)", new_fg, new_bg);
124 label2->setText(text);
125 g_free(text);
127 combo->setColor(new_fg);
130 void TestWindow::onChangeColorResponseHandler(
131 CppConsUI::ColorPickerDialog& /*activator*/,
132 CppConsUI::AbstractDialog::ResponseType response,
133 int color)
135 if (response != CppConsUI::AbstractDialog::RESPONSE_OK)
136 return;
138 combo->setColor(color);
140 char *text = g_strdup_printf("Chosen color nr: %d", color);
141 label2->setText(text);
142 g_free(text);
145 void TestWindow::onComboColorChange(CppConsUI::ComboBox& /*activator*/,
146 intptr_t color)
148 char *text = g_strdup_printf("Chosen color nr: %d",
149 static_cast<int>(color));
150 label2->setText(text);
151 g_free(text);
154 // TestApp class
155 class TestApp
156 : public CppConsUI::InputProcessor
158 public:
159 TestApp();
160 virtual ~TestApp() {}
162 void run();
164 // ignore every message
165 static void g_log_func_(const gchar * /*log_domain*/,
166 GLogLevelFlags /*log_level*/, const gchar * /*message*/,
167 gpointer /*user_data*/)
170 protected:
172 private:
173 TestApp(const TestApp&);
174 TestApp& operator=(const TestApp&);
177 TestApp::TestApp()
179 KEYCONFIG->loadDefaultKeyConfig();
180 KEYCONFIG->bindKey("testapp", "quit", "F10");
182 g_log_set_default_handler(g_log_func_, this);
184 declareBindable("testapp", "quit", sigc::mem_fun(COREMANAGER,
185 &CppConsUI::CoreManager::quitMainLoop),
186 InputProcessor::BINDABLE_OVERRIDE);
189 void TestApp::run()
191 TestWindow *win = new TestWindow;
192 win->show();
194 COREMANAGER->setTopInputProcessor(*this);
195 COREMANAGER->enableResizing();
196 COREMANAGER->startMainLoop();
199 // main function
200 int main()
202 setlocale(LC_ALL, "");
204 // initialize CppConsUI
205 int consui_res = CppConsUI::initializeConsUI();
206 if (consui_res) {
207 fprintf(stderr, "CppConsUI initialization failed.\n");
208 return consui_res;
211 TestApp *app = new TestApp;
212 app->run();
213 delete app;
215 // finalize CppConsUI
216 consui_res = CppConsUI::finalizeConsUI();
217 if (consui_res) {
218 fprintf(stderr, "CppConsUI deinitialization failed.\n");
219 return consui_res;
222 return 0;
225 /* vim: set tabstop=2 shiftwidth=2 textwidth=78 expandtab : */