Prepare CenterIM 5.0.0 Beta2
[centerim5.git] / tests / colorpicker.cpp
blob8b8abd0b8421b0964c4f7998df06c4460d22945c
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 CppConsUI::Button *button;
44 addWidget(*(new CppConsUI::Label("Press F10 to quit.")), 1, 1);
46 button = new CppConsUI::Button("Open Colorpicker...");
47 addWidget(*button, 1, 3);
48 button->signal_activate.connect(sigc::bind(sigc::mem_fun(this,
49 &TestWindow::onButtonActivate), 0));
51 button = new CppConsUI::Button("Open Colorpicker: ansi only...");
52 addWidget(*button, 1, 4);
53 button->signal_activate.connect(sigc::bind(sigc::mem_fun(this,
54 &TestWindow::onButtonActivate),
55 CppConsUI::ColorPickerPalette::FLAG_HIDE_GRAYSCALE
56 | CppConsUI::ColorPickerPalette::FLAG_HIDE_COLORCUBE));
58 button = new CppConsUI::Button("Open Colorpicker: ansi + Grayscale...");
59 addWidget(*button, 1, 5);
60 button->signal_activate.connect(sigc::bind(sigc::mem_fun(this,
61 &TestWindow::onButtonActivate),
62 CppConsUI::ColorPickerPalette::FLAG_HIDE_COLORCUBE));
64 button = new CppConsUI::Button("Open Colorpicker: color cube only...");
65 addWidget(*button, 1, 6);
66 button->signal_activate.connect(sigc::bind(sigc::mem_fun(this,
67 &TestWindow::onButtonActivate),
68 CppConsUI::ColorPickerPalette::FLAG_HIDE_ANSI
69 | CppConsUI::ColorPickerPalette::FLAG_HIDE_GRAYSCALE));
71 char *text = g_strdup_printf("Supported nr of colors: %d",
72 CppConsUI::Curses::nrcolors());
73 label1 = new CppConsUI::Label(text);
74 g_free(text);
75 addWidget(*label1, 1, 8);
76 label2 = new CppConsUI::Label("...");
77 addWidget(*label2, 1, 10);
79 CppConsUI::Label *l = new CppConsUI::Label("ColorPickerComboBox:");
80 addWidget(*l, 1, 12);
82 l = new CppConsUI::Label;
83 text = g_strdup_printf("Supported nr of color pairs: %d",
84 CppConsUI::Curses::nrcolorpairs());
85 l->setText(text);
86 g_free(text);
87 addWidget(*l, 1, 9);
89 combo = new CppConsUI::ColorPickerComboBox (10, defaultcolor);
90 combo->signal_color_changed.connect(sigc::mem_fun(this,
91 &TestWindow::onComboColorChange));
92 addWidget(*combo, 1, 13);
94 CppConsUI::ColorPicker *picker;
96 addWidget(*(new CppConsUI::Label("ColorPicker:")), 1, 15);
97 picker = new CppConsUI::ColorPicker(7, 1, "Label:", false);
98 picker->signal_colorpair_selected.connect(sigc::mem_fun(this,
99 &TestWindow::onColerPickerChanged));
100 addWidget(*picker, 1, 16);
102 addWidget(*(new CppConsUI::Label("ColorPicker:")), 1, 18);
103 picker = new CppConsUI::ColorPicker(15, 8, "(with sample)", true);
104 picker->signal_colorpair_selected.connect(sigc::mem_fun(this,
105 &TestWindow::onColerPickerChanged));
106 addWidget(*picker, 1, 19);
109 void TestWindow::onButtonActivate(CppConsUI::Button& /*activator*/, int flags)
111 CppConsUI::ColorPickerDialog *dialog = new CppConsUI::ColorPickerDialog(
112 "Test Colorpicker", 0, flags);
113 dialog->signal_response.connect(sigc::mem_fun(this,
114 &TestWindow::onChangeColorResponseHandler));
115 dialog->show();
118 void TestWindow::onColerPickerChanged(CppConsUI::ColorPicker& /*activator*/,
119 int new_fg, int new_bg)
121 char *text = g_strdup_printf("Chosen color (%d,%d)", new_fg, new_bg);
122 label2->setText(text);
123 g_free(text);
125 combo->setColor(new_fg);
128 void TestWindow::onChangeColorResponseHandler(
129 CppConsUI::ColorPickerDialog& /*activator*/,
130 CppConsUI::AbstractDialog::ResponseType response,
131 int color)
133 if (response != CppConsUI::AbstractDialog::RESPONSE_OK)
134 return;
136 combo->setColor(color);
138 char *text = g_strdup_printf("Chosen color nr: %d", color);
139 label2->setText(text);
140 g_free(text);
143 void TestWindow::onComboColorChange(CppConsUI::ComboBox& /*activator*/,
144 intptr_t color)
146 char *text = g_strdup_printf("Chosen color nr: %d",
147 static_cast<int>(color));
148 label2->setText(text);
149 g_free(text);
152 // TestApp class
153 class TestApp
154 : public CppConsUI::InputProcessor
156 public:
157 TestApp();
158 virtual ~TestApp() {}
160 void run();
162 // ignore every message
163 static void g_log_func_(const gchar * /*log_domain*/,
164 GLogLevelFlags /*log_level*/, const gchar * /*message*/,
165 gpointer /*user_data*/)
168 protected:
170 private:
171 TestApp(const TestApp&);
172 TestApp& operator=(const TestApp&);
175 TestApp::TestApp()
177 KEYCONFIG->loadDefaultKeyConfig();
178 KEYCONFIG->bindKey("testapp", "quit", "F10");
180 g_log_set_default_handler(g_log_func_, this);
182 declareBindable("testapp", "quit", sigc::mem_fun(COREMANAGER,
183 &CppConsUI::CoreManager::quitMainLoop),
184 InputProcessor::BINDABLE_OVERRIDE);
187 void TestApp::run()
189 TestWindow *win = new TestWindow;
190 win->show();
192 COREMANAGER->setTopInputProcessor(*this);
193 COREMANAGER->enableResizing();
194 COREMANAGER->startMainLoop();
197 // main function
198 int main()
200 setlocale(LC_ALL, "");
202 // initialize CppConsUI
203 int consui_res = CppConsUI::initializeConsUI();
204 if (consui_res) {
205 fprintf(stderr, "CppConsUI initialization failed.\n");
206 return consui_res;
209 TestApp *app = new TestApp;
210 app->run();
211 delete app;
213 // finalize CppConsUI
214 consui_res = CppConsUI::finalizeConsUI();
215 if (consui_res) {
216 fprintf(stderr, "CppConsUI deinitialization failed.\n");
217 return consui_res;
220 return 0;
223 /* vim: set tabstop=2 shiftwidth=2 textwidth=78 expandtab : */