4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcresources.h"
24 #include "bcsignals.h"
34 BC_Toggle::BC_Toggle(int x, int y, VFrame **data, int value,
35 const char *caption, int bottom_justify, int font, int color)
36 : BC_SubWindow(x, y, 0, 0, -1)
39 for( int i=0; i<TOGGLE_IMAGES; ++i ) images[i] = 0;
41 status = value ? BC_Toggle::TOGGLE_CHECKED : BC_Toggle::TOGGLE_UP;
43 strcpy(this->caption, caption);
44 this->bottom_justify = bottom_justify;
46 this->color = color >= 0 ? color : get_resources()->default_text_color;
54 BC_Toggle::~BC_Toggle()
56 for( int i=0; i<TOGGLE_IMAGES; ++i ) delete images[i];
61 int BC_Toggle::initialize()
65 calculate_extents(this, data, bottom_justify,
66 &text_line, &w, &h, &toggle_x, &toggle_y,
67 &text_x, &text_y, &text_w, &text_h,
68 has_caption() ? caption : 0,
71 // Create the subwindow
72 BC_SubWindow::initialize();
73 set_cursor(UPRIGHT_ARROW_CURSOR, 0, 0);
81 void BC_Toggle::calculate_extents(BC_WindowBase *gui,
82 VFrame **images, int bottom_justify,
83 int *text_line, int *w, int *h, int *toggle_x, int *toggle_y,
84 int *text_x, int *text_y, int *text_w, int *text_h,
85 const char *caption, int font)
87 BC_Resources *resources = get_resources();
88 VFrame *frame = images[0];
99 *text_w = gui->get_text_width(font, caption);
100 *text_h = gui->get_text_height(font, caption);
102 if( resources->toggle_highlight_bg ) {
103 *text_w += resources->toggle_text_margin * 2;
104 *text_h = MAX(*text_h, resources->toggle_highlight_bg->get_h());
108 *toggle_y = (*text_h - *h) >> 1;
112 *text_y = (*h - *text_h) >> 1;
114 if( bottom_justify ) {
115 *text_y = *h - *text_h;
116 *text_line = *h - gui->get_text_descent(font);
119 *text_line = *text_y + gui->get_text_ascent(font);
121 *w = *text_x + *text_w;
127 void BC_Toggle::set_radial(int value)
132 int BC_Toggle::set_images(VFrame **data)
136 for( int i=0; i<TOGGLE_IMAGES; ++i ) {
138 images[i] = new BC_Pixmap(top_level, data[i], PIXMAP_ALPHA);
140 BC_Resources *resources = get_resources();
141 if( resources->toggle_highlight_bg ) {
142 bg_image = new BC_Pixmap(top_level,
143 resources->toggle_highlight_bg,
149 void BC_Toggle::set_underline(int number)
151 this->underline = number;
155 void BC_Toggle::set_select_drag(int value)
157 this->select_drag = value;
160 int BC_Toggle::draw_face(int flash, int flush)
162 BC_Resources *resources = get_resources();
163 draw_top_background(parent_window, 0, 0, get_w(), get_h());
164 if( has_caption() ) {
166 (status == BC_Toggle::TOGGLE_UPHI ||
167 status == BC_Toggle::TOGGLE_DOWN ||
168 status == BC_Toggle::TOGGLE_CHECKEDHI) ) {
169 // Draw highlight image
172 int y = text_line - get_text_ascent(font) / 2 -
173 bg_image->get_h() / 2;
186 text_line - get_text_ascent(font),
188 get_text_height(font));
196 set_color(get_resources()->disabled_text_color);
198 draw_text(text_x + resources->toggle_text_margin,
203 if( underline >= 0 ) {
204 int x = text_x + resources->toggle_text_margin;
205 int y = text_line + 1;
206 int x1 = get_text_width(current_font, caption, underline) + x;
207 int x2 = get_text_width(current_font, caption, underline + 1) + x;
208 draw_line(x1, y, x2, y);
209 draw_line(x1, y + 1, (x2 + x1) / 2, y + 1);
213 draw_pixmap(images[status]);
214 if( flash ) this->flash(0);
215 if( flush ) this->flush();
219 void BC_Toggle::enable()
222 if( parent_window ) draw_face(1, 1);
225 void BC_Toggle::disable()
228 if( parent_window ) draw_face(1, 1);
231 void BC_Toggle::set_status(int value)
233 this->status = value;
237 int BC_Toggle::repeat_event(int64_t duration)
239 if( tooltip_text && tooltip_text[0] != 0 &&
240 duration == top_level->get_resources()->tooltip_delay &&
241 (status == BC_Toggle::TOGGLE_UPHI || status == BC_Toggle::TOGGLE_CHECKEDHI) ) {
248 int BC_Toggle::cursor_enter_event()
250 if( top_level->event_win == win && enabled ) {
251 if( top_level->button_down )
252 status = BC_Toggle::TOGGLE_DOWN;
254 status = value ? BC_Toggle::TOGGLE_CHECKEDHI : BC_Toggle::TOGGLE_UPHI;
260 int BC_Toggle::cursor_leave_event()
263 if( !value && status == BC_Toggle::TOGGLE_UPHI ) {
264 status = BC_Toggle::TOGGLE_UP;
268 if( status == BC_Toggle::TOGGLE_CHECKEDHI ) {
269 status = BC_Toggle::TOGGLE_CHECKED;
275 int BC_Toggle::button_press_event()
278 if( top_level->event_win == win && get_buttonpress() == 1 && enabled ) {
279 status = BC_Toggle::TOGGLE_DOWN;
281 // Change value now for select drag mode.
282 // Radial always goes to 1
288 top_level->toggle_drag = 1;
289 top_level->toggle_value = value;
299 int BC_Toggle::button_release_event()
304 if( top_level->event_win == win ) {
305 // Keep value regardless of status if drag mode.
308 status = BC_Toggle::TOGGLE_CHECKEDHI;
310 status = BC_Toggle::TOGGLE_UPHI;
311 top_level->toggle_drag = 0;
314 // Change value only if button down for default mode.
315 if( status == BC_Toggle::TOGGLE_DOWN ) {
316 // Radial always goes to 1.
317 if( !value || is_radial ) {
318 status = BC_Toggle::TOGGLE_CHECKEDHI;
322 status = BC_Toggle::TOGGLE_UPHI;
325 result = handle_event();
333 int BC_Toggle::cursor_motion_event()
335 if( top_level->button_down &&
336 top_level->event_win == win &&
338 if( status == BC_Toggle::TOGGLE_DOWN ) {
340 status = BC_Toggle::TOGGLE_CHECKED;
342 status = BC_Toggle::TOGGLE_UP;
346 if( status == BC_Toggle::TOGGLE_UPHI ) {
347 status = BC_Toggle::TOGGLE_CHECKEDHI;
354 int BC_Toggle::get_value()
359 int BC_Toggle::set_value(int value, int draw)
361 if( value != this->value ) {
365 case BC_Toggle::TOGGLE_UP:
366 status = BC_Toggle::TOGGLE_CHECKED;
368 case BC_Toggle::TOGGLE_UPHI:
369 status = BC_Toggle::TOGGLE_CHECKEDHI;
375 case BC_Toggle::TOGGLE_CHECKED:
376 status = BC_Toggle::TOGGLE_UP;
378 case BC_Toggle::TOGGLE_CHECKEDHI:
379 status = BC_Toggle::TOGGLE_UPHI;
383 if( draw ) draw_face(1, 1);
388 int BC_Toggle::update(int value, int draw)
390 return set_value(value, draw);
393 void BC_Toggle::reposition_window(int x, int y)
395 BC_WindowBase::reposition_window(x, y);
400 int BC_Toggle::has_caption()
402 return (caption != 0 && caption[0] != 0);
405 BC_Radial::BC_Radial(int x, int y, int value,
406 const char *caption, int font, int color)
408 BC_WindowBase::get_resources()->radial_images,
409 value, caption, 0, font, color)
414 BC_CheckBox::BC_CheckBox(int x, int y, int value,
415 const char *caption, int font, int color)
417 BC_WindowBase::get_resources()->checkbox_images,
418 value, caption, 0, font, color)
423 BC_CheckBox::BC_CheckBox(int x, int y, int *value,
424 const char *caption, int font, int color)
426 BC_WindowBase::get_resources()->checkbox_images,
427 *value, caption, 1, font, color)
432 void BC_CheckBox::calculate_extents(BC_WindowBase *gui, int *w, int *h,
433 const char *caption, int font)
435 int text_line, toggle_x, toggle_y;
436 int text_x, text_y, text_w, text_h;
438 BC_Toggle::calculate_extents(gui,
439 BC_WindowBase::get_resources()->checkbox_images,
440 1, &text_line, w, h, &toggle_x, &toggle_y,
441 &text_x, &text_y, &text_w, &text_h, caption, font);
444 int BC_CheckBox::handle_event()
447 *value = get_value();
451 int BC_CheckBox::calculate_h(BC_WindowBase *gui, int font)
454 calculate_extents(gui, &w, &h, "X", font);
459 BC_Label::BC_Label(int x, int y, int value, int font, int color)
461 BC_WindowBase::get_resources()->label_images, value,