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"
38 BC_Slider::BC_Slider(int x,
41 int pointer_motion_range,
46 : BC_SubWindow(x, y, 0, 0, -1)
48 this->images = images;
49 this->show_number = show_number;
50 this->vertical = vertical;
51 this->pointer_motion_range = pointer_motion_range;
52 this->pixels = pixels;
53 this->button_pixel = button_pixel;
54 this->use_caption = use_caption;
57 pixmaps = new BC_Pixmap*[SLIDER_IMAGES];
58 for(int i = 0; i < SLIDER_IMAGES; i++)
67 BC_Slider::~BC_Slider()
69 for(int i = 0; i < SLIDER_IMAGES; i++)
71 if(pixmaps[i]) delete pixmaps[i];
73 if(pixmaps) delete [] pixmaps;
76 int BC_Slider::initialize()
80 this->images = vertical ?
81 BC_WindowBase::get_resources()->vertical_slider_data :
82 BC_WindowBase::get_resources()->horizontal_slider_data;
89 w = images[SLIDER_BG_UP]->get_w();
95 h = images[SLIDER_BG_UP]->get_h();
98 text_height = get_text_height(SMALLFONT);
99 button_pixel = value_to_pixel();
101 BC_SubWindow::initialize();
107 int BC_Slider::get_span(int vertical)
111 return BC_WindowBase::get_resources()->vertical_slider_data[0]->get_w();
115 return BC_WindowBase::get_resources()->horizontal_slider_data[0]->get_h();
119 int BC_Slider::draw_face(int flush)
122 draw_top_background(parent_window, 0, 0, get_w(), get_h());
130 pixmaps[SLIDER_IMAGES / 2 + status]);
131 draw_pixmap(pixmaps[status], 0, button_pixel);
136 draw_text(0, h, get_caption());
141 //int y = get_h() / 2 - pixmaps[SLIDER_IMAGES / 2 + status]->get_h() / 2;
142 draw_3segmenth(0, 0, get_w(), pixmaps[SLIDER_IMAGES / 2 + status]);
143 draw_pixmap(pixmaps[status], button_pixel, 0);
148 draw_text(0, h, get_caption());
156 int BC_Slider::set_images(VFrame **images)
158 for(int i = 0; i < SLIDER_IMAGES; i++)
160 if(pixmaps[i]) delete pixmaps[i];
161 pixmaps[i] = new BC_Pixmap(parent_window, images[i], PIXMAP_ALPHA);
166 int BC_Slider::get_button_pixels()
168 return vertical ? pixmaps[SLIDER_UP]->get_h() :
169 pixmaps[SLIDER_UP]->get_w();
172 void BC_Slider::show_value_tooltip()
174 //printf("BC_Slider::show_value_tooltip %s\n", get_caption());
175 set_tooltip(get_caption());
176 keypress_tooltip_timer = 2000;
181 int BC_Slider::repeat_event(int64_t duration)
183 if(duration == top_level->get_resources()->tooltip_delay)
187 if(keypress_tooltip_timer > 0)
189 keypress_tooltip_timer -= get_resources()->tooltip_delay;
192 if(status != SLIDER_HI && status != SLIDER_DN)
198 if(status == SLIDER_HI && tooltip_text)
200 if(!tooltip_text[0] || isdigit(tooltip_text[0]))
202 set_tooltip(get_caption());
207 //printf("BC_Slider::repeat_event 1 %s\n", tooltip_text);
208 set_tooltip(get_caption());
219 int BC_Slider::keypress_event()
222 if(!active || !enabled) return 0;
223 if(ctrl_down() || shift_down()) return 0;
225 switch(get_keypress())
228 increase_value_big();
232 decrease_value_big();
248 show_value_tooltip();
254 int BC_Slider::cursor_enter_event()
256 //printf("BC_Slider::cursor_enter_event 1\n");
257 if(top_level->event_win == win && status == SLIDER_UP)
263 //printf("BC_Slider::cursor_enter_event 2\n");
267 int BC_Slider::cursor_leave_event()
269 if(status == SLIDER_HI)
278 int BC_Slider::deactivate()
284 int BC_Slider::activate()
286 top_level->active_subwindow = this;
291 void BC_Slider::enable()
297 void BC_Slider::disable()
303 int BC_Slider::button_press_event()
306 if(is_event_win() && enabled)
308 if(!tooltip_on) top_level->hide_tooltip();
309 if(status == SLIDER_HI)
311 if(get_buttonpress() == 4)
315 show_value_tooltip();
319 if(get_buttonpress() == 5)
323 show_value_tooltip();
327 if(get_buttonpress() == 1)
332 init_selection(top_level->cursor_x, top_level->cursor_y);
333 top_level->deactivate();
335 show_value_tooltip();
343 int BC_Slider::button_release_event()
353 top_level->hide_tooltip();
361 int BC_Slider::cursor_motion_event()
365 int old_pixel = button_pixel;
366 int result = update_selection(top_level->cursor_x, top_level->cursor_y);
367 if(button_pixel != old_pixel) draw_face(1);
371 set_tooltip(get_caption());
378 int BC_Slider::reposition_window(int x, int y, int w, int h)
380 BC_SubWindow::reposition_window(x, y, w, h);
381 button_pixel = value_to_pixel();
387 int BC_Slider::get_pointer_motion_range()
389 return pointer_motion_range;
392 void BC_Slider::set_pointer_motion_range(int value)
394 pointer_motion_range = value;
401 BC_ISlider::BC_ISlider(int x,
405 int pointer_motion_range,
415 pointer_motion_range,
421 this->minvalue = minvalue;
422 this->maxvalue = maxvalue;
424 this->output = output;
427 int BC_ISlider::value_to_pixel()
429 if(maxvalue == minvalue) return 0;
433 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
434 (get_h() - get_button_pixels()));
436 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
437 (get_w() - get_button_pixels()));
441 int BC_ISlider::update(int64_t value)
443 if(this->value != value)
446 int old_pixel = button_pixel;
447 button_pixel = value_to_pixel();
448 if(button_pixel != old_pixel) draw_face(1);
453 int BC_ISlider::update(int pointer_motion_range,
458 this->minvalue = minvalue;
459 this->maxvalue = maxvalue;
461 this->pointer_motion_range = pointer_motion_range;
463 int old_pixel = button_pixel;
464 button_pixel = value_to_pixel();
465 if(button_pixel != old_pixel) draw_face(1);
470 int64_t BC_ISlider::get_value()
475 int64_t BC_ISlider::get_length()
477 return maxvalue - minvalue;
480 char* BC_ISlider::get_caption()
482 sprintf(caption, "%jd", value);
486 int BC_ISlider::increase_value()
489 if(value > maxvalue) value = maxvalue;
490 button_pixel = value_to_pixel();
494 int BC_ISlider::decrease_value()
497 if(value < minvalue) value = minvalue;
498 button_pixel = value_to_pixel();
502 int BC_ISlider::increase_value_big()
505 if(value > maxvalue) value = maxvalue;
506 button_pixel = value_to_pixel();
510 int BC_ISlider::decrease_value_big()
513 if(value < minvalue) value = minvalue;
514 button_pixel = value_to_pixel();
518 int BC_ISlider::init_selection(int cursor_x, int cursor_y)
522 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
523 min_pixel += cursor_y;
527 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
528 min_pixel += cursor_x;
530 max_pixel = min_pixel + pointer_motion_range;
534 int BC_ISlider::update_selection(int cursor_x, int cursor_y)
536 int64_t old_value = value;
540 value = (int64_t)((1.0 - (double)(cursor_y - min_pixel) /
541 pointer_motion_range) *
542 (maxvalue - minvalue) +
547 value = (int64_t)((double)(cursor_x - min_pixel) /
548 pointer_motion_range *
549 (maxvalue - minvalue) +
553 if(value > maxvalue) value = maxvalue;
554 if(value < minvalue) value = minvalue;
555 button_pixel = value_to_pixel();
557 if(old_value != value)
564 int BC_ISlider::handle_event()
566 if(output) *output = get_value();
577 BC_FSlider::BC_FSlider(int x,
581 int pointer_motion_range,
590 pointer_motion_range,
596 this->minvalue = minvalue;
597 this->maxvalue = maxvalue;
599 this->precision = 0.1;
600 this->small_change = 0.1;
601 this->big_change = 1.0;
604 int BC_FSlider::value_to_pixel()
606 //printf("BC_FSlider::value_to_pixel %f %f\n", maxvalue, minvalue);
607 if(maxvalue == minvalue) return 0;
610 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
611 (get_h() - get_button_pixels()));
613 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
614 (get_w() - get_button_pixels()));
618 int BC_FSlider::update(float value)
620 if(this->value != value)
623 int old_pixel = button_pixel;
624 button_pixel = value_to_pixel();
625 //printf("BC_FSlider::update 1 %f %d\n", value, button_pixel);
626 if(button_pixel != old_pixel) draw_face(1);
631 int BC_FSlider::update(int pointer_motion_range, float value, float minvalue, float maxvalue)
633 this->minvalue = minvalue;
634 this->maxvalue = maxvalue;
636 this->pointer_motion_range = pointer_motion_range;
637 int old_pixel = button_pixel;
638 button_pixel = value_to_pixel();
639 if(button_pixel != old_pixel) draw_face(1);
644 float BC_FSlider::get_value()
649 float BC_FSlider::get_length()
651 return maxvalue - minvalue;
654 char* BC_FSlider::get_caption()
656 sprintf(caption, "%.02f", value);
660 int BC_FSlider::increase_value()
662 value += small_change;
663 if(value > maxvalue) value = maxvalue;
664 button_pixel = value_to_pixel();
668 int BC_FSlider::decrease_value()
670 value -= small_change;
671 if(value < minvalue) value = minvalue;
672 button_pixel = value_to_pixel();
676 int BC_FSlider::increase_value_big()
679 if(value > maxvalue) value = maxvalue;
680 button_pixel = value_to_pixel();
684 int BC_FSlider::decrease_value_big()
687 if(value < minvalue) value = minvalue;
688 button_pixel = value_to_pixel();
692 int BC_FSlider::init_selection(int cursor_x, int cursor_y)
696 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
697 min_pixel += cursor_y;
701 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
702 min_pixel += cursor_x;
704 max_pixel = min_pixel + pointer_motion_range;
708 int BC_FSlider::update_selection(int cursor_x, int cursor_y)
710 float old_value = value;
715 value = ((1.0 - (double)(cursor_y - min_pixel) /
716 pointer_motion_range) *
717 (maxvalue - minvalue) +
722 value = ((double)(cursor_x - min_pixel) /
723 pointer_motion_range *
724 (maxvalue - minvalue) +
728 value = Units::quantize(value, precision);
729 if(value > maxvalue) value = maxvalue;
730 if(value < minvalue) value = minvalue;
731 button_pixel = value_to_pixel();
732 // printf("BC_FSlider::update_selection 1 %d %d %d %d %f %f\n",
733 // pointer_motion_range,
740 if(old_value != value)
747 void BC_FSlider::set_precision(float value)
749 this->precision = value;
752 void BC_FSlider::set_pagination(float small_change, float big_change)
754 this->small_change = small_change;
755 this->big_change = big_change;
759 BC_PercentageSlider::BC_PercentageSlider(int x,
763 int pointer_motion_range,
773 pointer_motion_range,
782 char* BC_PercentageSlider::get_caption()
784 sprintf(caption, "%.0f%%", floor((value - minvalue) / (maxvalue - minvalue) * 100));