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,
41 : BC_SubWindow(x, y, 0, 0, -1)
44 for(int i = 0; i < TOGGLE_IMAGES; i++)
47 status = value ? BC_Toggle::TOGGLE_CHECKED : BC_Toggle::TOGGLE_UP;
49 strcpy(this->caption, caption);
50 this->bottom_justify = bottom_justify;
55 this->color = get_resources()->default_text_color;
63 BC_Toggle::~BC_Toggle()
65 for(int i = 0; i < TOGGLE_IMAGES; i++) if(images[i]) delete images[i];
70 int BC_Toggle::initialize()
74 calculate_extents(this,
86 has_caption() ? caption : 0,
89 // Create the subwindow
90 BC_SubWindow::initialize();
91 set_cursor(UPRIGHT_ARROW_CURSOR, 0, 0);
99 void BC_Toggle::calculate_extents(BC_WindowBase *gui,
114 BC_Resources *resources = get_resources();
115 VFrame *frame = images[0];
127 *text_w = gui->get_text_width(font, caption);
128 *text_h = gui->get_text_height(font);
130 if(resources->toggle_highlight_bg)
132 *text_w += resources->toggle_text_margin * 2;
133 *text_h = MAX(*text_h, resources->toggle_highlight_bg->get_h());
138 *toggle_y = (*text_h - *h) >> 1;
142 *text_y = (*h - *text_h) >> 1;
146 *text_y = *h - *text_h;
147 *text_line = *h - gui->get_text_descent(font);
150 *text_line = *text_y + gui->get_text_ascent(font);
152 *w = *text_x + *text_w;
158 void BC_Toggle::set_radial(int value)
163 int BC_Toggle::set_images(VFrame **data)
167 for(int i = 0; i < TOGGLE_IMAGES; i++)
169 if(images[i]) delete images[i];
170 images[i] = new BC_Pixmap(top_level, data[i], PIXMAP_ALPHA);
172 BC_Resources *resources = get_resources();
173 if(resources->toggle_highlight_bg)
175 bg_image = new BC_Pixmap(top_level,
176 resources->toggle_highlight_bg,
182 void BC_Toggle::set_underline(int number)
184 this->underline = number;
188 void BC_Toggle::set_select_drag(int value)
190 this->select_drag = value;
193 int BC_Toggle::draw_face(int flash, int flush)
195 BC_Resources *resources = get_resources();
196 draw_top_background(parent_window, 0, 0, get_w(), get_h());
200 (status == BC_Toggle::TOGGLE_UPHI ||
201 status == BC_Toggle::TOGGLE_DOWN ||
202 status == BC_Toggle::TOGGLE_CHECKEDHI))
204 // Draw highlight image
208 int y = text_line - get_text_ascent(font) / 2 -
209 bg_image->get_h() / 2;
222 text_line - get_text_ascent(font),
224 get_text_height(font));
232 set_color(get_resources()->disabled_text_color);
234 draw_text(text_x + resources->toggle_text_margin,
241 int x = text_x + resources->toggle_text_margin;
242 int y = text_line + 1;
243 int x1 = get_text_width(current_font, caption, underline) + x;
244 int x2 = get_text_width(current_font, caption, underline + 1) + x;
245 draw_line(x1, y, x2, y);
246 draw_line(x1, y + 1, (x2 + x1) / 2, y + 1);
250 draw_pixmap(images[status]);
251 if(flash) this->flash(0);
252 if(flush) this->flush();
256 void BC_Toggle::enable()
259 if(parent_window) draw_face(1, 1);
262 void BC_Toggle::disable()
265 if(parent_window) draw_face(1, 1);
268 void BC_Toggle::set_status(int value)
270 this->status = value;
274 int BC_Toggle::repeat_event(int64_t duration)
276 if(tooltip_text && tooltip_text[0] != 0 &&
277 duration == top_level->get_resources()->tooltip_delay &&
278 (status == BC_Toggle::TOGGLE_UPHI || status == BC_Toggle::TOGGLE_CHECKEDHI))
286 int BC_Toggle::cursor_enter_event()
288 if(top_level->event_win == win && enabled)
290 if(top_level->button_down)
291 status = BC_Toggle::TOGGLE_DOWN;
293 status = value ? BC_Toggle::TOGGLE_CHECKEDHI : BC_Toggle::TOGGLE_UPHI;
299 int BC_Toggle::cursor_leave_event()
302 if(!value && status == BC_Toggle::TOGGLE_UPHI)
304 status = BC_Toggle::TOGGLE_UP;
308 if(status == BC_Toggle::TOGGLE_CHECKEDHI)
310 status = BC_Toggle::TOGGLE_CHECKED;
316 int BC_Toggle::button_press_event()
319 if(top_level->event_win == win && get_buttonpress() == 1 && enabled)
321 status = BC_Toggle::TOGGLE_DOWN;
323 // Change value now for select drag mode.
324 // Radial always goes to 1
331 top_level->toggle_drag = 1;
332 top_level->toggle_value = value;
342 int BC_Toggle::button_release_event()
347 if(top_level->event_win == win)
349 // Keep value regardless of status if drag mode.
353 status = BC_Toggle::TOGGLE_CHECKEDHI;
355 status = BC_Toggle::TOGGLE_UPHI;
356 top_level->toggle_drag = 0;
359 // Change value only if button down for default mode.
360 if(status == BC_Toggle::TOGGLE_DOWN)
362 // Radial always goes to 1.
363 if(!value || is_radial)
365 status = BC_Toggle::TOGGLE_CHECKEDHI;
370 status = BC_Toggle::TOGGLE_UPHI;
373 result = handle_event();
381 int BC_Toggle::cursor_motion_event()
383 if(top_level->button_down &&
384 top_level->event_win == win &&
387 if(status == BC_Toggle::TOGGLE_DOWN)
390 status = BC_Toggle::TOGGLE_CHECKED;
392 status = BC_Toggle::TOGGLE_UP;
396 if(status == BC_Toggle::TOGGLE_UPHI)
398 status = BC_Toggle::TOGGLE_CHECKEDHI;
405 int BC_Toggle::get_value()
410 int BC_Toggle::set_value(int value, int draw)
412 if(value != this->value)
419 case BC_Toggle::TOGGLE_UP:
420 status = BC_Toggle::TOGGLE_CHECKED;
422 case BC_Toggle::TOGGLE_UPHI:
423 status = BC_Toggle::TOGGLE_CHECKEDHI;
430 case BC_Toggle::TOGGLE_CHECKED:
431 status = BC_Toggle::TOGGLE_UP;
433 case BC_Toggle::TOGGLE_CHECKEDHI:
434 status = BC_Toggle::TOGGLE_UPHI;
437 if(draw) draw_face(1, 1);
442 int BC_Toggle::update(int value, int draw)
444 return set_value(value, draw);
447 void BC_Toggle::reposition_window(int x, int y)
449 BC_WindowBase::reposition_window(x, y);
454 int BC_Toggle::has_caption()
456 return (caption != 0 && caption[0] != 0);
459 BC_Radial::BC_Radial(int x,
467 BC_WindowBase::get_resources()->radial_images,
477 BC_CheckBox::BC_CheckBox(int x,
485 BC_WindowBase::get_resources()->checkbox_images,
495 BC_CheckBox::BC_CheckBox(int x,
503 BC_WindowBase::get_resources()->checkbox_images,
513 void BC_CheckBox::calculate_extents(BC_WindowBase *gui, int *w, int *h,
514 const char *caption, int font)
516 int text_line, toggle_x, toggle_y;
517 int text_x, text_y, text_w, text_h;
519 BC_Toggle::calculate_extents(gui,
520 BC_WindowBase::get_resources()->checkbox_images,
521 1, &text_line, w, h, &toggle_x, &toggle_y,
522 &text_x, &text_y, &text_w, &text_h, caption, font);
525 int BC_CheckBox::handle_event()
528 *value = get_value();
534 BC_Label::BC_Label(int x,
541 BC_WindowBase::get_resources()->label_images,