Mercurial > otakunoraifu
comparison window/menuitem.cc @ 52:15a18fbe6f21
* Known bugs added to the README
* Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author | thib |
---|---|
date | Sat, 18 Apr 2009 18:35:39 +0000 |
parents | 223b71206888 |
children | 4416cfac86ae |
comparison
equal
deleted
inserted
replaced
51:cbb301016a4e | 52:15a18fbe6f21 |
---|---|
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 | 28 |
29 #include"menuitem.h" | 29 #include "menuitem.h" |
30 | 30 |
31 #define Button WidButton | 31 #define Button WidButton |
32 #define Scale WidScale | 32 #define Scale WidScale |
33 #define Label WidLabel | 33 #define Label WidLabel |
34 #define TextButton WidTextButton | 34 #define TextButton WidTextButton |
42 MenuItem::MenuItem(PicContainer* parent, const Rect& r_orig, int _x_size, int _y_size, int* _value_ptr) : | 42 MenuItem::MenuItem(PicContainer* parent, const Rect& r_orig, int _x_size, int _y_size, int* _value_ptr) : |
43 x_size(_x_size), y_size(_y_size), value_ptr(_value_ptr), set_func(0), set_pointer(0) { | 43 x_size(_x_size), y_size(_y_size), value_ptr(_value_ptr), set_func(0), set_pointer(0) { |
44 SetPic(parent->create_node(r_orig, 0)); | 44 SetPic(parent->create_node(r_orig, 0)); |
45 menu_width = r_orig.width(); | 45 menu_width = r_orig.width(); |
46 menu_height = r_orig.height(); | 46 menu_height = r_orig.height(); |
47 label = 0; | 47 label = NULL; |
48 lb_width = 0; lb_right = 0; | 48 lb_width = 0; lb_right = 0; |
49 lb_left = -1; lb_bottom = -1; | 49 lb_left = -1; lb_bottom = -1; |
50 int i; | 50 int i; |
51 for (i=0; i<x_size*y_size; i++) item.push_back(0); | 51 for (i=0; i<x_size*y_size; i++) item.push_back(0); |
52 }; | 52 } |
53 | |
53 void MenuItem::SetLabelLeft(PicWidget* lb, const Rect& min_rect, const Rect& min_margin) { | 54 void MenuItem::SetLabelLeft(PicWidget* lb, const Rect& min_rect, const Rect& min_margin) { |
54 lb_width = min_rect.width(); | 55 lb_width = min_rect.width(); |
55 lb_right = min_margin.width(); | 56 lb_right = min_margin.width(); |
56 lb_left = -1; | 57 lb_left = -1; |
57 lb_bottom = -1; | 58 lb_bottom = -1; |
58 label = lb; | 59 label = lb; |
59 } | 60 } |
61 | |
60 void MenuItem::SetLabelTop(PicWidget* lb, const Rect& left_margin, const Rect& bottom_margin) { | 62 void MenuItem::SetLabelTop(PicWidget* lb, const Rect& left_margin, const Rect& bottom_margin) { |
61 lb_left = left_margin.width(); | 63 lb_left = left_margin.width(); |
62 lb_bottom = bottom_margin.height(); | 64 lb_bottom = bottom_margin.height(); |
63 lb_width = -1; | 65 lb_width = -1; |
64 lb_right = -1; | 66 lb_right = -1; |
65 label = lb; | 67 label = lb; |
66 } | 68 } |
69 | |
67 void MenuItem::SetValue(int new_value) { | 70 void MenuItem::SetValue(int new_value) { |
68 SetValueImpl(new_value); | 71 SetValueImpl(new_value); |
69 if (value_ptr) *value_ptr = new_value; | 72 if (value_ptr) *value_ptr = new_value; |
70 if (set_func) (*set_func)(set_pointer, this); | 73 if (set_func) (*set_func)(set_pointer, this); |
71 } | 74 } |
75 | |
72 void MenuItem::activate(void) { | 76 void MenuItem::activate(void) { |
73 iterator it; | 77 iterator it; |
74 for (it=item.begin(); it!=item.end(); it++) { | 78 for (it=item.begin(); it!=item.end(); it++) { |
75 if (*it == 0) continue; | 79 if (*it == NULL) continue; |
76 (*it)->activate(); | 80 (*it)->activate(); |
77 } | 81 } |
78 } | 82 } |
83 | |
79 void MenuItem::deactivate(void) { | 84 void MenuItem::deactivate(void) { |
80 iterator it; | 85 iterator it; |
81 for (it=item.begin(); it!=item.end(); it++) { | 86 for (it=item.begin(); it!=item.end(); it++) { |
82 if (*it == 0) continue; | 87 if (*it == NULL) continue; |
83 (*it)->deactivate(); | 88 (*it)->deactivate(); |
84 } | 89 } |
85 } | 90 } |
91 | |
86 void MenuItem::pack(void) { | 92 void MenuItem::pack(void) { |
87 int x_min = 0, y_min = 0; | 93 int x_min = 0, y_min = 0; |
88 if (lb_width == -1) { // 上にラベルを貼る | 94 if (lb_width == -1) { // 上にラベルを貼る |
89 if (lb_left < 0) lb_left = 0; | 95 if (lb_left < 0) lb_left = 0; |
90 if (lb_bottom < 0) lb_bottom = 0; | 96 if (lb_bottom < 0) lb_bottom = 0; |
110 } | 116 } |
111 int* item_width = new int[x_size]; | 117 int* item_width = new int[x_size]; |
112 int* item_height = new int[y_size]; | 118 int* item_height = new int[y_size]; |
113 int* item_x = new int[x_size]; | 119 int* item_x = new int[x_size]; |
114 int* item_y = new int[y_size]; | 120 int* item_y = new int[y_size]; |
115 int i,j; | 121 int i, j; |
116 for (i=0; i<x_size; i++) item_width[i]=0; | 122 for (i=0; i<x_size; i++) item_width[i]=0; |
117 for (i=0; i<y_size; i++) item_height[i]=0; | 123 for (i=0; i<y_size; i++) item_height[i]=0; |
118 | 124 |
119 iterator it = item.begin(); | 125 iterator it = item.begin(); |
120 for (i=0; i<y_size; i++) { | 126 for (i=0; i<y_size; i++) { |
121 for (j=0; j<x_size; j++) { | 127 for (j=0; j<x_size; j++) { |
122 if (*it != 0 && (*it)->Pic() != 0) { | 128 if (*it != NULL && (*it)->Pic() != NULL) { |
123 PicBase* pic = (*it)->Pic(); | 129 PicBase* pic = (*it)->Pic(); |
124 if (item_width[j] < pic->Width()) item_width[j] = pic->Width(); | 130 if (item_width[j] < pic->Width()) item_width[j] = pic->Width(); |
125 if (item_height[i] < pic->Height()) item_height[i] = pic->Height(); | 131 if (item_height[i] < pic->Height()) item_height[i] = pic->Height(); |
126 } | 132 } |
127 it++; | 133 it++; |
173 } | 179 } |
174 // 位置の再割り当て | 180 // 位置の再割り当て |
175 it = item.begin(); | 181 it = item.begin(); |
176 for (i=0; i<y_size; i++) { | 182 for (i=0; i<y_size; i++) { |
177 for (j=0; j<x_size; j++) { | 183 for (j=0; j<x_size; j++) { |
178 if (*it != 0 && (*it)->Pic() != 0) { | 184 if (*it != NULL && (*it)->Pic() != NULL) { |
179 PicBase* pic = (*it)->Pic(); | 185 PicBase* pic = (*it)->Pic(); |
180 int x0 = item_x[j]-pic->Width()/2; | 186 int x0 = item_x[j]-pic->Width()/2; |
181 int y0 = item_y[i]-pic->Height()/2; | 187 int y0 = item_y[i]-pic->Height()/2; |
182 pic->Move(x0, y0); | 188 pic->Move(x0, y0); |
183 } | 189 } |
193 | 199 |
194 delete[] item_width; | 200 delete[] item_width; |
195 delete[] item_height; | 201 delete[] item_height; |
196 delete[] item_x; | 202 delete[] item_x; |
197 delete[] item_y; | 203 delete[] item_y; |
198 }; | 204 } |
199 | 205 |
200 RadioButton::RadioButton(Event::Container& _container, PicContainer* _parent, const Rect& r_orig, int _x_size, int _y_size, int* _value_ptr, const Rect& _button_r, int _text_size, const Color& _fore, const Color& _pressed, const Color& _back) : | 206 RadioButton::RadioButton(Event::Container& _container, PicContainer* _parent, const Rect& r_orig, int _x_size, int _y_size, int* _value_ptr, const Rect& _button_r, int _text_size, const Color& _fore, const Color& _pressed, const Color& _back) : |
201 MenuItem(_parent, r_orig, _x_size, _y_size,_value_ptr), | 207 MenuItem(_parent, r_orig, _x_size, _y_size,_value_ptr), |
202 container(_container), parent(_parent), text_size(_text_size), button_rect(_button_r), buttons(0), | 208 container(_container), parent(_parent), text_size(_text_size), button_rect(_button_r), buttons(0), |
203 fore_color(_fore), pressed_color(_pressed), back_color(_back) { | 209 fore_color(_fore), pressed_color(_pressed), back_color(_back) { |
204 } | 210 } |
211 | |
205 void RadioButton::Add(const char* s, bool is_center) { | 212 void RadioButton::Add(const char* s, bool is_center) { |
206 Add(s, fore_color, pressed_color, back_color, is_center); | 213 Add(s, fore_color, pressed_color, back_color, is_center); |
207 } | 214 } |
215 | |
208 void RadioButton::Add(const char* s, const Color& fore, const Color& pressed, const Color& back, bool is_center) { | 216 void RadioButton::Add(const char* s, const Color& fore, const Color& pressed, const Color& back, bool is_center) { |
209 if (buttons >= x_size*y_size) { | 217 if (buttons >= x_size*y_size) { |
210 fprintf(stderr,"too many buttons (%d/%d) in RadioButton::Add ; text = %s\n",x_size,y_size,s); | 218 fprintf(stderr,"too many buttons (%d/%d) in RadioButton::Add ; text = %s\n",x_size,y_size,s); |
211 return; | 219 return; |
212 } | 220 } |
219 int x_pos, y_pos; | 227 int x_pos, y_pos; |
220 if (y_size == 0) x_pos=buttons; | 228 if (y_size == 0) x_pos=buttons; |
221 else x_pos = buttons / y_size, y_pos = buttons % y_size; | 229 else x_pos = buttons / y_size, y_pos = buttons % y_size; |
222 item[x_pos + y_pos*x_size] = wid; | 230 item[x_pos + y_pos*x_size] = wid; |
223 buttons++; | 231 buttons++; |
224 }; | 232 } |
233 | |
225 void RadioButton::PressCallback(void* pointer, Button* from) { | 234 void RadioButton::PressCallback(void* pointer, Button* from) { |
226 RadioButton* wid = (RadioButton*)pointer; | 235 RadioButton* wid = (RadioButton*)pointer; |
227 int i; | 236 int i; |
228 for (i=0; i<wid->x_size*wid->y_size; i++) { | 237 for (i=0; i<wid->x_size*wid->y_size; i++) { |
229 if (from == wid->item[i]) { | 238 if (from == wid->item[i]) { |
230 wid->SetValue(i); | 239 wid->SetValue(i); |
231 return; | 240 return; |
232 } | 241 } |
233 } | 242 } |
234 return; | 243 } |
235 } | 244 |
236 void RadioButton::SetValueImpl(int new_value) { | 245 void RadioButton::SetValueImpl(int new_value) { |
237 int i; | 246 int i; |
238 for (i=0; i<x_size*y_size; i++) { | 247 for (i=0; i<x_size*y_size; i++) { |
239 Button* wid = dynamic_cast<Button*>(item[i]); | 248 Button* wid = dynamic_cast<Button*>(item[i]); |
240 if (wid) { | 249 if (wid != NULL) { |
241 if (i == new_value) wid->Toggle(true); | 250 if (i == new_value) wid->Toggle(true); |
242 else wid->Toggle(false); | 251 else wid->Toggle(false); |
243 } | 252 } |
244 } | 253 } |
245 } | 254 } |