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());
218 int BC_Slider::keypress_event()
221 if(!active || !enabled) return 0;
222 if(ctrl_down() || shift_down()) return 0;
224 switch(get_keypress())
227 increase_value_big();
231 decrease_value_big();
247 show_value_tooltip();
253 int BC_Slider::cursor_enter_event()
255 //printf("BC_Slider::cursor_enter_event 1\n");
256 if(top_level->event_win == win && status == SLIDER_UP)
261 //printf("BC_Slider::cursor_enter_event 2\n");
265 int BC_Slider::cursor_leave_event()
267 if(status == SLIDER_HI)
276 int BC_Slider::deactivate()
282 int BC_Slider::activate()
284 top_level->active_subwindow = this;
289 void BC_Slider::enable()
295 void BC_Slider::disable()
301 int BC_Slider::button_press_event()
304 if(is_event_win() && enabled)
306 if(!tooltip_on) top_level->hide_tooltip();
307 if(status == SLIDER_HI)
309 if(get_buttonpress() == 4)
313 show_value_tooltip();
317 if(get_buttonpress() == 5)
321 show_value_tooltip();
325 if(get_buttonpress() == 1)
330 init_selection(top_level->cursor_x, top_level->cursor_y);
331 top_level->deactivate();
333 show_value_tooltip();
341 int BC_Slider::button_release_event()
351 top_level->hide_tooltip();
359 int BC_Slider::cursor_motion_event()
363 int old_pixel = button_pixel;
364 int result = update_selection(top_level->cursor_x, top_level->cursor_y);
365 if(button_pixel != old_pixel) draw_face(1);
369 set_tooltip(get_caption());
376 int BC_Slider::reposition_window(int x, int y, int w, int h)
378 BC_SubWindow::reposition_window(x, y, w, h);
379 button_pixel = value_to_pixel();
385 int BC_Slider::get_pointer_motion_range()
387 return pointer_motion_range;
390 void BC_Slider::set_pointer_motion_range(int value)
392 pointer_motion_range = value;
399 BC_ISlider::BC_ISlider(int x,
403 int pointer_motion_range,
413 pointer_motion_range,
419 this->minvalue = minvalue;
420 this->maxvalue = maxvalue;
422 this->output = output;
425 int BC_ISlider::value_to_pixel()
427 if(maxvalue == minvalue) return 0;
431 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
432 (get_h() - get_button_pixels()));
434 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
435 (get_w() - get_button_pixels()));
439 int BC_ISlider::update(int64_t value)
441 if(this->value != value)
444 int old_pixel = button_pixel;
445 button_pixel = value_to_pixel();
446 if(button_pixel != old_pixel) draw_face(1);
451 int BC_ISlider::update(int pointer_motion_range,
456 this->minvalue = minvalue;
457 this->maxvalue = maxvalue;
459 this->pointer_motion_range = pointer_motion_range;
461 int old_pixel = button_pixel;
462 button_pixel = value_to_pixel();
463 if(button_pixel != old_pixel) draw_face(1);
468 int64_t BC_ISlider::get_value()
473 int64_t BC_ISlider::get_length()
475 return maxvalue - minvalue;
478 char* BC_ISlider::get_caption()
480 sprintf(caption, "%jd", value);
484 int BC_ISlider::increase_value()
487 if(value > maxvalue) value = maxvalue;
488 button_pixel = value_to_pixel();
492 int BC_ISlider::decrease_value()
495 if(value < minvalue) value = minvalue;
496 button_pixel = value_to_pixel();
500 int BC_ISlider::increase_value_big()
503 if(value > maxvalue) value = maxvalue;
504 button_pixel = value_to_pixel();
508 int BC_ISlider::decrease_value_big()
511 if(value < minvalue) value = minvalue;
512 button_pixel = value_to_pixel();
516 int BC_ISlider::init_selection(int cursor_x, int cursor_y)
520 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
521 min_pixel += cursor_y;
525 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
526 min_pixel += cursor_x;
528 max_pixel = min_pixel + pointer_motion_range;
532 int BC_ISlider::update_selection(int cursor_x, int cursor_y)
534 int64_t old_value = value;
538 value = (int64_t)((1.0 - (double)(cursor_y - min_pixel) /
539 pointer_motion_range) *
540 (maxvalue - minvalue) +
545 value = (int64_t)((double)(cursor_x - min_pixel) /
546 pointer_motion_range *
547 (maxvalue - minvalue) +
551 if(value > maxvalue) value = maxvalue;
552 if(value < minvalue) value = minvalue;
553 button_pixel = value_to_pixel();
555 if(old_value != value)
562 int BC_ISlider::handle_event()
564 if(output) *output = get_value();
575 BC_FSlider::BC_FSlider(int x,
579 int pointer_motion_range,
588 pointer_motion_range,
594 this->minvalue = minvalue;
595 this->maxvalue = maxvalue;
597 this->precision = 0.1;
598 this->small_change = 0.1;
599 this->big_change = 1.0;
602 int BC_FSlider::value_to_pixel()
604 //printf("BC_FSlider::value_to_pixel %f %f\n", maxvalue, minvalue);
605 if(maxvalue == minvalue) return 0;
608 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
609 (get_h() - get_button_pixels()));
611 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
612 (get_w() - get_button_pixels()));
616 int BC_FSlider::update(float value)
618 if(this->value != value)
621 int old_pixel = button_pixel;
622 button_pixel = value_to_pixel();
623 //printf("BC_FSlider::update 1 %f %d\n", value, button_pixel);
624 if(button_pixel != old_pixel) draw_face(1);
629 int BC_FSlider::update(int pointer_motion_range, float value, float minvalue, float maxvalue)
631 this->minvalue = minvalue;
632 this->maxvalue = maxvalue;
634 this->pointer_motion_range = pointer_motion_range;
635 int old_pixel = button_pixel;
636 button_pixel = value_to_pixel();
637 if(button_pixel != old_pixel) draw_face(1);
642 float BC_FSlider::get_value()
647 float BC_FSlider::get_length()
649 return maxvalue - minvalue;
652 char* BC_FSlider::get_caption()
654 sprintf(caption, "%.02f", value);
658 int BC_FSlider::increase_value()
660 value += small_change;
661 if(value > maxvalue) value = maxvalue;
662 button_pixel = value_to_pixel();
666 int BC_FSlider::decrease_value()
668 value -= small_change;
669 if(value < minvalue) value = minvalue;
670 button_pixel = value_to_pixel();
674 int BC_FSlider::increase_value_big()
677 if(value > maxvalue) value = maxvalue;
678 button_pixel = value_to_pixel();
682 int BC_FSlider::decrease_value_big()
685 if(value < minvalue) value = minvalue;
686 button_pixel = value_to_pixel();
690 int BC_FSlider::init_selection(int cursor_x, int cursor_y)
694 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
695 min_pixel += cursor_y;
699 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
700 min_pixel += cursor_x;
702 max_pixel = min_pixel + pointer_motion_range;
706 int BC_FSlider::update_selection(int cursor_x, int cursor_y)
708 float old_value = value;
713 value = ((1.0 - (double)(cursor_y - min_pixel) /
714 pointer_motion_range) *
715 (maxvalue - minvalue) +
720 value = ((double)(cursor_x - min_pixel) /
721 pointer_motion_range *
722 (maxvalue - minvalue) +
726 value = Units::quantize(value, precision);
727 if(value > maxvalue) value = maxvalue;
728 if(value < minvalue) value = minvalue;
729 button_pixel = value_to_pixel();
730 // printf("BC_FSlider::update_selection 1 %d %d %d %d %f %f\n",
731 // pointer_motion_range,
738 if(old_value != value)
745 void BC_FSlider::set_precision(float value)
747 this->precision = value;
750 void BC_FSlider::set_pagination(float small_change, float big_change)
752 this->small_change = small_change;
753 this->big_change = big_change;
757 BC_PercentageSlider::BC_PercentageSlider(int x,
761 int pointer_motion_range,
771 pointer_motion_range,
780 char* BC_PercentageSlider::get_caption()
782 sprintf(caption, "%.0f%%", floor((value - minvalue) / (maxvalue - minvalue) * 100));