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 if( !show_number ) return;
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 && show_number)
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());
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)
262 //printf("BC_Slider::cursor_enter_event 2\n");
266 int BC_Slider::cursor_leave_event()
268 if(status == SLIDER_HI)
277 int BC_Slider::deactivate()
283 int BC_Slider::activate()
285 top_level->active_subwindow = this;
290 void BC_Slider::enable()
296 void BC_Slider::disable()
302 void BC_Slider::enable_show_value(int v)
307 int BC_Slider::button_press_event()
310 if(is_event_win() && enabled)
312 if(!tooltip_on) top_level->hide_tooltip();
313 if(status == SLIDER_HI)
315 if(get_buttonpress() == 4)
319 show_value_tooltip();
323 if(get_buttonpress() == 5)
327 show_value_tooltip();
331 if(get_buttonpress() == 1)
336 init_selection(top_level->cursor_x, top_level->cursor_y);
337 top_level->deactivate();
339 show_value_tooltip();
347 int BC_Slider::button_release_event()
357 top_level->hide_tooltip();
365 int BC_Slider::cursor_motion_event()
369 int old_pixel = button_pixel;
370 int result = update_selection(top_level->cursor_x, top_level->cursor_y);
371 if(button_pixel != old_pixel) draw_face(1);
375 set_tooltip(get_caption());
382 int BC_Slider::reposition_window(int x, int y, int w, int h)
384 BC_SubWindow::reposition_window(x, y, w, h);
385 button_pixel = value_to_pixel();
391 int BC_Slider::get_pointer_motion_range()
393 return pointer_motion_range;
396 void BC_Slider::set_pointer_motion_range(int value)
398 pointer_motion_range = value;
405 BC_ISlider::BC_ISlider(int x,
409 int pointer_motion_range,
419 pointer_motion_range,
425 this->minvalue = minvalue;
426 this->maxvalue = maxvalue;
428 this->output = output;
431 int BC_ISlider::value_to_pixel()
433 if(maxvalue == minvalue) return 0;
437 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
438 (get_h() - get_button_pixels()));
440 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
441 (get_w() - get_button_pixels()));
445 int BC_ISlider::update(int64_t value)
447 if(this->value != value)
450 int old_pixel = button_pixel;
451 button_pixel = value_to_pixel();
452 if(button_pixel != old_pixel) draw_face(1);
457 int BC_ISlider::update(int pointer_motion_range,
462 this->minvalue = minvalue;
463 this->maxvalue = maxvalue;
465 this->pointer_motion_range = pointer_motion_range;
467 int old_pixel = button_pixel;
468 button_pixel = value_to_pixel();
469 if(button_pixel != old_pixel) draw_face(1);
474 int64_t BC_ISlider::get_value()
479 int64_t BC_ISlider::get_length()
481 return maxvalue - minvalue;
484 char* BC_ISlider::get_caption()
486 sprintf(caption, "%jd", value);
490 int BC_ISlider::increase_value()
493 if(value > maxvalue) value = maxvalue;
494 button_pixel = value_to_pixel();
498 int BC_ISlider::decrease_value()
501 if(value < minvalue) value = minvalue;
502 button_pixel = value_to_pixel();
506 int BC_ISlider::increase_value_big()
509 if(value > maxvalue) value = maxvalue;
510 button_pixel = value_to_pixel();
514 int BC_ISlider::decrease_value_big()
517 if(value < minvalue) value = minvalue;
518 button_pixel = value_to_pixel();
522 int BC_ISlider::init_selection(int cursor_x, int cursor_y)
526 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
527 min_pixel += cursor_y;
531 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
532 min_pixel += cursor_x;
534 max_pixel = min_pixel + pointer_motion_range;
538 int BC_ISlider::update_selection(int cursor_x, int cursor_y)
540 int64_t old_value = value;
544 value = (int64_t)((1.0 - (double)(cursor_y - min_pixel) /
545 pointer_motion_range) *
546 (maxvalue - minvalue) +
551 value = (int64_t)((double)(cursor_x - min_pixel) /
552 pointer_motion_range *
553 (maxvalue - minvalue) +
557 if(value > maxvalue) value = maxvalue;
558 if(value < minvalue) value = minvalue;
559 button_pixel = value_to_pixel();
561 if(old_value != value)
568 int BC_ISlider::handle_event()
570 if(output) *output = get_value();
581 BC_FSlider::BC_FSlider(int x,
585 int pointer_motion_range,
594 pointer_motion_range,
600 this->minvalue = minvalue;
601 this->maxvalue = maxvalue;
603 this->precision = 0.1;
604 this->small_change = 0.1;
605 this->big_change = 1.0;
608 int BC_FSlider::value_to_pixel()
610 //printf("BC_FSlider::value_to_pixel %f %f\n", maxvalue, minvalue);
611 if(maxvalue == minvalue) return 0;
614 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
615 (get_h() - get_button_pixels()));
617 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
618 (get_w() - get_button_pixels()));
622 int BC_FSlider::update(float value)
624 if(this->value != value)
627 int old_pixel = button_pixel;
628 button_pixel = value_to_pixel();
629 //printf("BC_FSlider::update 1 %f %d\n", value, button_pixel);
630 if(button_pixel != old_pixel) draw_face(1);
635 int BC_FSlider::update(int pointer_motion_range, float value, float minvalue, float maxvalue)
637 this->minvalue = minvalue;
638 this->maxvalue = maxvalue;
640 this->pointer_motion_range = pointer_motion_range;
641 int old_pixel = button_pixel;
642 button_pixel = value_to_pixel();
643 if(button_pixel != old_pixel) draw_face(1);
648 float BC_FSlider::get_value()
653 float BC_FSlider::get_length()
655 return maxvalue - minvalue;
658 char* BC_FSlider::get_caption()
660 sprintf(caption, "%.02f", value);
664 int BC_FSlider::increase_value()
666 value += small_change;
667 if(value > maxvalue) value = maxvalue;
668 button_pixel = value_to_pixel();
672 int BC_FSlider::decrease_value()
674 value -= small_change;
675 if(value < minvalue) value = minvalue;
676 button_pixel = value_to_pixel();
680 int BC_FSlider::increase_value_big()
683 if(value > maxvalue) value = maxvalue;
684 button_pixel = value_to_pixel();
688 int BC_FSlider::decrease_value_big()
691 if(value < minvalue) value = minvalue;
692 button_pixel = value_to_pixel();
696 int BC_FSlider::init_selection(int cursor_x, int cursor_y)
700 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
701 min_pixel += cursor_y;
705 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
706 min_pixel += cursor_x;
708 max_pixel = min_pixel + pointer_motion_range;
712 int BC_FSlider::update_selection(int cursor_x, int cursor_y)
714 float old_value = value;
719 value = ((1.0 - (double)(cursor_y - min_pixel) /
720 pointer_motion_range) *
721 (maxvalue - minvalue) +
726 value = ((double)(cursor_x - min_pixel) /
727 pointer_motion_range *
728 (maxvalue - minvalue) +
732 value = Units::quantize(value, precision);
733 if(value > maxvalue) value = maxvalue;
734 if(value < minvalue) value = minvalue;
735 button_pixel = value_to_pixel();
736 // printf("BC_FSlider::update_selection 1 %d %d %d %d %f %f\n",
737 // pointer_motion_range,
744 if(old_value != value)
751 void BC_FSlider::set_precision(float value)
753 this->precision = value;
756 void BC_FSlider::set_pagination(float small_change, float big_change)
758 this->small_change = small_change;
759 this->big_change = big_change;
763 BC_PercentageSlider::BC_PercentageSlider(int x,
767 int pointer_motion_range,
777 pointer_motion_range,
786 char* BC_PercentageSlider::get_caption()
788 sprintf(caption, "%.0f%%", floor((value - minvalue) / (maxvalue - minvalue) * 100));