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"
39 BC_Slider::BC_Slider(int x,
42 int pointer_motion_range,
47 : BC_SubWindow(x, y, 0, 0, -1)
49 this->images = images;
50 this->show_number = show_number;
51 this->vertical = vertical;
52 this->pointer_motion_range = pointer_motion_range;
53 this->pixels = pixels;
54 this->button_pixel = button_pixel;
55 this->use_caption = use_caption;
58 pixmaps = new BC_Pixmap*[SLIDER_IMAGES];
59 for(int i = 0; i < SLIDER_IMAGES; i++)
68 BC_Slider::~BC_Slider()
70 for(int i = 0; i < SLIDER_IMAGES; i++)
72 if(pixmaps[i]) delete pixmaps[i];
74 if(pixmaps) delete [] pixmaps;
77 int BC_Slider::initialize()
81 this->images = vertical ?
82 BC_WindowBase::get_resources()->vertical_slider_data :
83 BC_WindowBase::get_resources()->horizontal_slider_data;
90 w = images[SLIDER_BG_UP]->get_w();
96 h = images[SLIDER_BG_UP]->get_h();
99 text_height = get_text_height(SMALLFONT);
100 button_pixel = value_to_pixel();
102 BC_SubWindow::initialize();
108 int BC_Slider::get_span(int vertical)
112 return BC_WindowBase::get_resources()->vertical_slider_data[0]->get_w();
116 return BC_WindowBase::get_resources()->horizontal_slider_data[0]->get_h();
120 int BC_Slider::draw_face(int flush)
123 draw_top_background(parent_window, 0, 0, get_w(), get_h());
131 pixmaps[SLIDER_IMAGES / 2 + status]);
132 draw_pixmap(pixmaps[status], 0, button_pixel);
137 draw_text(0, h, get_caption());
142 //int y = get_h() / 2 - pixmaps[SLIDER_IMAGES / 2 + status]->get_h() / 2;
143 draw_3segmenth(0, 0, get_w(), pixmaps[SLIDER_IMAGES / 2 + status]);
144 draw_pixmap(pixmaps[status], button_pixel, 0);
149 draw_text(0, h, get_caption());
157 int BC_Slider::set_images(VFrame **images)
159 for(int i = 0; i < SLIDER_IMAGES; i++)
161 if(pixmaps[i]) delete pixmaps[i];
162 pixmaps[i] = new BC_Pixmap(parent_window, images[i], PIXMAP_ALPHA);
167 int BC_Slider::get_button_pixels()
169 return vertical ? pixmaps[SLIDER_UP]->get_h() :
170 pixmaps[SLIDER_UP]->get_w();
173 void BC_Slider::show_value_tooltip()
175 //printf("BC_Slider::show_value_tooltip %s\n", get_caption());
176 set_tooltip(get_caption());
177 keypress_tooltip_timer = 2000;
182 int BC_Slider::repeat_event(int64_t duration)
184 if(duration == top_level->get_resources()->tooltip_delay)
188 if(keypress_tooltip_timer > 0)
190 keypress_tooltip_timer -= get_resources()->tooltip_delay;
193 if(status != SLIDER_HI && status != SLIDER_DN)
199 if(status == SLIDER_HI && tooltip_text)
201 if(!tooltip_text[0] || isdigit(tooltip_text[0]))
203 set_tooltip(get_caption());
208 //printf("BC_Slider::repeat_event 1 %s\n", tooltip_text);
209 set_tooltip(get_caption());
220 int BC_Slider::keypress_event()
223 if(!active || !enabled) return 0;
224 if(ctrl_down() || shift_down()) return 0;
226 switch(get_keypress())
229 increase_value_big();
233 decrease_value_big();
249 show_value_tooltip();
255 int BC_Slider::cursor_enter_event()
257 //printf("BC_Slider::cursor_enter_event 1\n");
258 if(top_level->event_win == win && status == SLIDER_UP)
264 //printf("BC_Slider::cursor_enter_event 2\n");
268 int BC_Slider::cursor_leave_event()
270 if(status == SLIDER_HI)
279 int BC_Slider::deactivate()
285 int BC_Slider::activate()
287 top_level->active_subwindow = this;
292 void BC_Slider::enable()
298 void BC_Slider::disable()
304 int BC_Slider::button_press_event()
307 if(is_event_win() && enabled)
309 if(!tooltip_on) top_level->hide_tooltip();
310 if(status == SLIDER_HI)
312 if(get_buttonpress() == 4)
316 show_value_tooltip();
320 if(get_buttonpress() == 5)
324 show_value_tooltip();
328 if(get_buttonpress() == 1)
333 init_selection(top_level->cursor_x, top_level->cursor_y);
334 top_level->deactivate();
336 show_value_tooltip();
344 int BC_Slider::button_release_event()
354 top_level->hide_tooltip();
362 int BC_Slider::cursor_motion_event()
366 int old_pixel = button_pixel;
367 int result = update_selection(top_level->cursor_x, top_level->cursor_y);
368 if(button_pixel != old_pixel) draw_face(1);
372 set_tooltip(get_caption());
379 int BC_Slider::reposition_window(int x, int y, int w, int h)
381 BC_SubWindow::reposition_window(x, y, w, h);
382 button_pixel = value_to_pixel();
388 int BC_Slider::get_pointer_motion_range()
390 return pointer_motion_range;
393 void BC_Slider::set_pointer_motion_range(int value)
395 pointer_motion_range = value;
402 BC_ISlider::BC_ISlider(int x,
406 int pointer_motion_range,
416 pointer_motion_range,
422 this->minvalue = minvalue;
423 this->maxvalue = maxvalue;
425 this->output = output;
428 int BC_ISlider::value_to_pixel()
430 if(maxvalue == minvalue) return 0;
434 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
435 (get_h() - get_button_pixels()));
437 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
438 (get_w() - get_button_pixels()));
442 int BC_ISlider::update(int64_t value)
444 if(this->value != value)
447 int old_pixel = button_pixel;
448 button_pixel = value_to_pixel();
449 if(button_pixel != old_pixel) draw_face(1);
454 int BC_ISlider::update(int pointer_motion_range,
459 this->minvalue = minvalue;
460 this->maxvalue = maxvalue;
462 this->pointer_motion_range = pointer_motion_range;
464 int old_pixel = button_pixel;
465 button_pixel = value_to_pixel();
466 if(button_pixel != old_pixel) draw_face(1);
471 int64_t BC_ISlider::get_value()
476 int64_t BC_ISlider::get_length()
478 return maxvalue - minvalue;
481 char* BC_ISlider::get_caption()
483 sprintf(caption, "%jd", value);
487 int BC_ISlider::increase_value()
490 if(value > maxvalue) value = maxvalue;
491 button_pixel = value_to_pixel();
495 int BC_ISlider::decrease_value()
498 if(value < minvalue) value = minvalue;
499 button_pixel = value_to_pixel();
503 int BC_ISlider::increase_value_big()
506 if(value > maxvalue) value = maxvalue;
507 button_pixel = value_to_pixel();
511 int BC_ISlider::decrease_value_big()
514 if(value < minvalue) value = minvalue;
515 button_pixel = value_to_pixel();
519 int BC_ISlider::init_selection(int cursor_x, int cursor_y)
523 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
524 min_pixel += cursor_y;
528 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
529 min_pixel += cursor_x;
531 max_pixel = min_pixel + pointer_motion_range;
535 int BC_ISlider::update_selection(int cursor_x, int cursor_y)
537 int64_t old_value = value;
541 value = (int64_t)((1.0 - (double)(cursor_y - min_pixel) /
542 pointer_motion_range) *
543 (maxvalue - minvalue) +
548 value = (int64_t)((double)(cursor_x - min_pixel) /
549 pointer_motion_range *
550 (maxvalue - minvalue) +
554 if(value > maxvalue) value = maxvalue;
555 if(value < minvalue) value = minvalue;
556 button_pixel = value_to_pixel();
558 if(old_value != value)
565 int BC_ISlider::handle_event()
567 if(output) *output = get_value();
578 BC_FSlider::BC_FSlider(int x,
582 int pointer_motion_range,
591 pointer_motion_range,
597 this->minvalue = minvalue;
598 this->maxvalue = maxvalue;
600 this->precision = 0.1;
601 this->small_change = 0.1;
602 this->big_change = 1.0;
605 int BC_FSlider::value_to_pixel()
607 //printf("BC_FSlider::value_to_pixel %f %f\n", maxvalue, minvalue);
608 if(maxvalue == minvalue) return 0;
611 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
612 (get_h() - get_button_pixels()));
614 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
615 (get_w() - get_button_pixels()));
619 int BC_FSlider::update(float value)
621 if(this->value != value)
624 int old_pixel = button_pixel;
625 button_pixel = value_to_pixel();
626 //printf("BC_FSlider::update 1 %f %d\n", value, button_pixel);
627 if(button_pixel != old_pixel) draw_face(1);
632 int BC_FSlider::update(int pointer_motion_range, float value, float minvalue, float maxvalue)
634 this->minvalue = minvalue;
635 this->maxvalue = maxvalue;
637 this->pointer_motion_range = pointer_motion_range;
638 int old_pixel = button_pixel;
639 button_pixel = value_to_pixel();
640 if(button_pixel != old_pixel) draw_face(1);
645 float BC_FSlider::get_value()
650 float BC_FSlider::get_length()
652 return maxvalue - minvalue;
655 char* BC_FSlider::get_caption()
657 sprintf(caption, "%.02f", value);
661 int BC_FSlider::increase_value()
663 value += small_change;
664 if(value > maxvalue) value = maxvalue;
665 button_pixel = value_to_pixel();
669 int BC_FSlider::decrease_value()
671 value -= small_change;
672 if(value < minvalue) value = minvalue;
673 button_pixel = value_to_pixel();
677 int BC_FSlider::increase_value_big()
680 if(value > maxvalue) value = maxvalue;
681 button_pixel = value_to_pixel();
685 int BC_FSlider::decrease_value_big()
688 if(value < minvalue) value = minvalue;
689 button_pixel = value_to_pixel();
693 int BC_FSlider::init_selection(int cursor_x, int cursor_y)
697 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
698 min_pixel += cursor_y;
702 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
703 min_pixel += cursor_x;
705 max_pixel = min_pixel + pointer_motion_range;
709 int BC_FSlider::update_selection(int cursor_x, int cursor_y)
711 float old_value = value;
716 value = ((1.0 - (double)(cursor_y - min_pixel) /
717 pointer_motion_range) *
718 (maxvalue - minvalue) +
723 value = ((double)(cursor_x - min_pixel) /
724 pointer_motion_range *
725 (maxvalue - minvalue) +
729 value = Units::quantize(value, precision);
730 if(value > maxvalue) value = maxvalue;
731 if(value < minvalue) value = minvalue;
732 button_pixel = value_to_pixel();
733 // printf("BC_FSlider::update_selection 1 %d %d %d %d %f %f\n",
734 // pointer_motion_range,
741 if(old_value != value)
748 void BC_FSlider::set_precision(float value)
750 this->precision = value;
753 void BC_FSlider::set_pagination(float small_change, float big_change)
755 this->small_change = small_change;
756 this->big_change = big_change;
760 BC_PercentageSlider::BC_PercentageSlider(int x,
764 int pointer_motion_range,
774 pointer_motion_range,
783 char* BC_PercentageSlider::get_caption()
785 sprintf(caption, "%.0f%%", floor((value - minvalue) / (maxvalue - minvalue) * 100));