Debian Codegraph @

Examples/GUIStyle/MenuSystem/NS_MessageBox.cpp

raw
1 // NS_MessageBox.cpp: implementation of the NS_MessageBox class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
5 #include "NS_MessageBox.h"
6
7 #include <ClanLib/signals.h>
8 #include <ClanLib/signals.h>
9 #include <ClanLib/display.h>
10 #include <ClanLib/display.h>
11 #include <ClanLib/core.h>
12 #include <ClanLib/core.h>
13
14 //////////////////////////////////////////////////////////////////////
15 // Construction/Destruction
16 //////////////////////////////////////////////////////////////////////
17
18 NS_MessageBox::NS_MessageBox(CL_Component * parent,
19 std::string const & title,
20 std::string const & text,
21 std::string const & button1,
22 std::string const & button2,
23 std::string const & button3)
24 : CL_Window(parent, parent->get_style_manager()),
25 result(-1),
26 title(title),
27 text(text),
28 button1_text(button1),
29 button2_text(button2),
30 button3_text(button3),
31 font(0)
32 {
33 font = new CL_Font("my_fnt_4", get_style_manager()->get_resources());
34
35 int width = 400;
36 int height = 50 + 40 + font->get_height(text, CL_Size(width - 15, 0));
37
38 // Calc dialog position
39 set_position(CL_Rect(
40 CL_Display::get_width() / 2 - width / 2,
41 CL_Display::get_height() / 2 - height / 2,
42 CL_Display::get_width() / 2 + width / 2,
43 CL_Display::get_height() / 2 + height / 2));
44
45 set_title(title);
46
47 int num_buttons = 1;
48 if (!button2_text.empty())
49 num_buttons = 2;
50 if (!button3_text.empty())
51 num_buttons = 3;
52
53 int btn_pos_y = height - 40;
54
55 switch(num_buttons) {
56 case 1:
57 {
58 CL_Button * btn_1 = new CL_Button(CL_Point(width / 2, btn_pos_y), button1_text, get_client_area());
59 btn_1->set_position(width / 2 - btn_1->get_width() / 2, btn_pos_y);
60 slots.connect(btn_1->sig_clicked(), this, &NS_MessageBox::on_button1);
61 btn_1->find_preferred_size();
62 components["btn_1"] = btn_1;
63 }
64 break;
65 case 2:
66 {
67 CL_Button * btn_1 = new CL_Button(CL_Point(width / 2, btn_pos_y), button1_text, get_client_area());
68 CL_Button * btn_2 = new CL_Button(CL_Point(width / 2, btn_pos_y), button2_text, get_client_area());
69 btn_1->find_preferred_size();
70 btn_2->find_preferred_size();
71 {
72 int const total_size = btn_1->get_width() + 20 + btn_2->get_width();
73 btn_1->set_position(width / 2 - total_size / 2, btn_pos_y);
74 btn_2->set_position(width / 2 - total_size / 2 + btn_1->get_width() + 20, btn_pos_y);
75 }
76 slots.connect(btn_1->sig_clicked(), this, &NS_MessageBox::on_button1);
77 slots.connect(btn_2->sig_clicked(), this, &NS_MessageBox::on_button2);
78 components["btn_1"] = btn_1;
79 components["btn_2"] = btn_2;
80 }
81 break;
82 case 3:
83 {
84 CL_Button * btn_1 = new CL_Button(CL_Point(width / 2, btn_pos_y), button1_text, get_client_area());
85 CL_Button * btn_2 = new CL_Button(CL_Point(width / 2, btn_pos_y), button2_text, get_client_area());
86 CL_Button * btn_3 = new CL_Button(CL_Point(width / 2, btn_pos_y), button3_text, get_client_area());
87 btn_1->find_preferred_size();
88 btn_2->find_preferred_size();
89 btn_3->find_preferred_size();
90 {
91 int const total_size = btn_1->get_width() + 20 +
92 btn_2->get_width() + 20 + btn_3->get_width();
93 btn_1->set_position(width / 2 - total_size / 2, btn_pos_y);
94 btn_2->set_position(width / 2 - total_size / 2 +
95 btn_1->get_width() + 20, btn_pos_y);
96 btn_3->set_position(width / 2 - total_size / 2 +
97 btn_1->get_width() + 20 +
98 btn_2->get_width() + 20, btn_pos_y);
99 }
100 slots.connect(btn_1->sig_clicked(), this, &NS_MessageBox::on_button1);
101 slots.connect(btn_2->sig_clicked(), this, &NS_MessageBox::on_button2);
102 slots.connect(btn_3->sig_clicked(), this, &NS_MessageBox::on_button3);
103 components["btn_1"] = btn_1;
104 components["btn_2"] = btn_2;
105 components["btn_3"] = btn_3;
106 }
107 break;
108 }
109
110 // Connect signals
111 slots.connect(CL_Keyboard::sig_key_down(), this,
112 &NS_MessageBox::on_key_down);
113 slots.connect(this->sig_end_paint(), this, &NS_MessageBox::on_end_paint);
114 }
115
116 NS_MessageBox::~NS_MessageBox()
117 {
118 std::map<std::string, CL_Component * >::iterator it = components.begin();
119 for(; it != components.end(); ++it)
120 delete it->second;
121 delete font;
122 }
123
124 void NS_MessageBox::on_key_down(CL_InputEvent const & key)
125 {
126 if (key.id == CL_KEY_ESCAPE)
127 {
128 result = -1;
129 quit();
130 }
131 else
132 if (key.id == CL_KEY_RETURN)
133 {
134 result = 1;
135 quit();
136 }
137 }
138
139 int NS_MessageBox::info(CL_Component * parent,
140 std::string const & title,
141 std::string const & text,
142 std::string const & button1,
143 std::string const & button2,
144 std::string const & button3)
145 {
146 NS_MessageBox dlg(parent,
147 title,
148 text,
149 button1,
150 button2,
151 button3);
152 dlg.run();
153 return dlg.get_result();
154 }
155
156 int NS_MessageBox::get_result() const
157 {
158 return result;
159 }
160
161 void NS_MessageBox::on_button1()
162 {
163 result = 1;
164 quit();
165 }
166
167 void NS_MessageBox::on_button2()
168 {
169 result = 2;
170 quit();
171 }
172
173 void NS_MessageBox::on_button3()
174 {
175 result = 3;
176 quit();
177 }
178
179 void NS_MessageBox::on_end_paint()
180 {
181 CL_Rect rect = get_position();
182 rect.left += 15 + 15;
183 rect.right -= 15;
184 rect.top += 50;
185 rect.bottom -= 40;
186 font->draw(rect, text);
187 }