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, int y, int pixels, int pointer_motion_range, VFrame **images,
39 int show_number, int vertical, int use_caption)
40 : BC_SubWindow(x, y, 0, 0, -1)
42 this->images = images;
43 this->show_number = show_number;
44 this->vertical = vertical;
45 this->pointer_motion_range = pointer_motion_range;
46 this->pixels = pixels;
47 this->button_pixel = button_pixel;
48 this->use_caption = use_caption;
51 pixmaps = new BC_Pixmap*[SLIDER_IMAGES];
52 for(int i = 0; i < SLIDER_IMAGES; i++)
61 BC_Slider::~BC_Slider()
63 for(int i = 0; i < SLIDER_IMAGES; i++) {
64 if(pixmaps[i]) delete pixmaps[i];
66 if(pixmaps) delete [] pixmaps;
69 int BC_Slider::initialize()
72 this->images = vertical ?
73 BC_WindowBase::get_resources()->vertical_slider_data :
74 BC_WindowBase::get_resources()->horizontal_slider_data;
80 w = images[SLIDER_BG_UP]->get_w();
86 h = images[SLIDER_BG_UP]->get_h();
89 text_height = get_text_height(SMALLFONT);
90 button_pixel = value_to_pixel();
92 BC_SubWindow::initialize();
98 int BC_Slider::get_span(int vertical)
102 return BC_WindowBase::get_resources()->vertical_slider_data[0]->get_w();
106 return BC_WindowBase::get_resources()->horizontal_slider_data[0]->get_h();
110 int BC_Slider::draw_face(int flush)
113 draw_top_background(parent_window, 0, 0, get_w(), get_h());
121 pixmaps[SLIDER_IMAGES / 2 + status]);
122 draw_pixmap(pixmaps[status], 0, button_pixel);
127 draw_text(0, h, get_caption());
132 //int y = get_h() / 2 - pixmaps[SLIDER_IMAGES / 2 + status]->get_h() / 2;
133 draw_3segmenth(0, 0, get_w(), pixmaps[SLIDER_IMAGES / 2 + status]);
134 draw_pixmap(pixmaps[status], button_pixel, 0);
139 draw_text(0, h, get_caption());
147 int BC_Slider::set_images(VFrame **images)
149 for(int i = 0; i < SLIDER_IMAGES; i++)
151 if(pixmaps[i]) delete pixmaps[i];
152 pixmaps[i] = new BC_Pixmap(parent_window, images[i], PIXMAP_ALPHA);
157 int BC_Slider::get_button_pixels()
159 return vertical ? pixmaps[SLIDER_UP]->get_h() :
160 pixmaps[SLIDER_UP]->get_w();
163 void BC_Slider::show_value_tooltip()
165 if( !show_number ) return;
166 //printf("BC_Slider::show_value_tooltip %s\n", get_caption());
167 set_tooltip(get_caption());
168 keypress_tooltip_timer = 2000;
169 show_tooltip(xS(50));
173 int BC_Slider::repeat_event(int64_t duration)
175 if(duration == top_level->get_resources()->tooltip_delay)
179 if(keypress_tooltip_timer > 0)
181 keypress_tooltip_timer -= get_resources()->tooltip_delay;
184 if(status != SLIDER_HI && status != SLIDER_DN)
190 if(status == SLIDER_HI && tooltip_text && show_number)
192 if(!tooltip_text[0] || isdigit(tooltip_text[0]))
194 set_tooltip(get_caption());
199 //printf("BC_Slider::repeat_event 1 %s\n", tooltip_text);
200 set_tooltip(get_caption());
210 int BC_Slider::keypress_event()
213 if(!active || !enabled) return 0;
214 if(ctrl_down() || shift_down()) return 0;
216 switch(get_keypress())
219 increase_value_big();
223 decrease_value_big();
239 show_value_tooltip();
245 int BC_Slider::cursor_enter_event()
247 //printf("BC_Slider::cursor_enter_event 1\n");
248 if(top_level->event_win == win && status == SLIDER_UP)
253 //printf("BC_Slider::cursor_enter_event 2\n");
257 int BC_Slider::cursor_leave_event()
259 if(status == SLIDER_HI)
268 int BC_Slider::deactivate()
274 int BC_Slider::activate()
276 top_level->active_subwindow = this;
281 void BC_Slider::enable()
287 void BC_Slider::disable()
293 void BC_Slider::enable_show_value(int v)
298 int BC_Slider::button_press_event()
301 if(is_event_win() && enabled)
303 if(!tooltip_on) top_level->hide_tooltip();
304 if(status == SLIDER_HI)
306 if(get_buttonpress() == 4)
310 show_value_tooltip();
314 if(get_buttonpress() == 5)
318 show_value_tooltip();
322 if(get_buttonpress() == 1)
327 init_selection(top_level->cursor_x, top_level->cursor_y);
328 top_level->deactivate();
330 show_value_tooltip();
338 int BC_Slider::button_release_event()
348 top_level->hide_tooltip();
356 int BC_Slider::cursor_motion_event()
360 int old_pixel = button_pixel;
361 int result = update_selection(top_level->cursor_x, top_level->cursor_y);
362 if(button_pixel != old_pixel) draw_face(1);
366 set_tooltip(get_caption());
373 int BC_Slider::reposition_window(int x, int y, int w, int h)
375 BC_SubWindow::reposition_window(x, y, w, h);
376 button_pixel = value_to_pixel();
382 int BC_Slider::get_pointer_motion_range()
384 return pointer_motion_range;
387 void BC_Slider::set_pointer_motion_range(int value)
389 pointer_motion_range = value;
393 BC_ISlider::BC_ISlider(int x, int y, int vertical, int pixels, int pointer_motion_range,
394 int64_t minvalue, int64_t maxvalue, int64_t value, int use_caption,
395 VFrame **data, int *output)
396 : BC_Slider(x, y, pixels, pointer_motion_range, data, 1, vertical, use_caption)
398 this->minvalue = minvalue;
399 this->maxvalue = maxvalue;
401 this->output = output;
404 int BC_ISlider::value_to_pixel()
406 if(maxvalue == minvalue) return 0;
410 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
411 (get_h() - get_button_pixels()));
413 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
414 (get_w() - get_button_pixels()));
418 int BC_ISlider::update(int64_t value)
420 if(this->value != value)
423 int old_pixel = button_pixel;
424 button_pixel = value_to_pixel();
425 if(button_pixel != old_pixel) draw_face(1);
430 int BC_ISlider::update(int pointer_motion_range,
435 this->minvalue = minvalue;
436 this->maxvalue = maxvalue;
438 this->pointer_motion_range = pointer_motion_range;
440 int old_pixel = button_pixel;
441 button_pixel = value_to_pixel();
442 if(button_pixel != old_pixel) draw_face(1);
447 int64_t BC_ISlider::get_value()
452 int64_t BC_ISlider::get_length()
454 return maxvalue - minvalue;
457 char* BC_ISlider::get_caption()
459 sprintf(caption, "%jd", value);
463 int BC_ISlider::increase_value()
466 if(value > maxvalue) value = maxvalue;
467 button_pixel = value_to_pixel();
471 int BC_ISlider::decrease_value()
474 if(value < minvalue) value = minvalue;
475 button_pixel = value_to_pixel();
479 int BC_ISlider::increase_value_big()
482 if(value > maxvalue) value = maxvalue;
483 button_pixel = value_to_pixel();
487 int BC_ISlider::decrease_value_big()
490 if(value < minvalue) value = minvalue;
491 button_pixel = value_to_pixel();
495 int BC_ISlider::init_selection(int cursor_x, int cursor_y)
499 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
500 min_pixel += cursor_y;
504 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
505 min_pixel += cursor_x;
507 max_pixel = min_pixel + pointer_motion_range;
511 int BC_ISlider::update_selection(int cursor_x, int cursor_y)
513 int64_t old_value = value;
517 value = (int64_t)((1.0 - (double)(cursor_y - min_pixel) /
518 pointer_motion_range) *
519 (maxvalue - minvalue) +
524 value = (int64_t)((double)(cursor_x - min_pixel) /
525 pointer_motion_range *
526 (maxvalue - minvalue) +
530 if(value > maxvalue) value = maxvalue;
531 if(value < minvalue) value = minvalue;
532 button_pixel = value_to_pixel();
534 if(old_value != value)
541 int BC_ISlider::handle_event()
543 if(output) *output = get_value();
548 BC_FSlider::BC_FSlider(int x, int y, int vertical, int pixels, int pointer_motion_range,
549 float minvalue, float maxvalue, float value, int use_caption,
551 : BC_Slider(x, y, pixels, pointer_motion_range, data, 1, vertical, use_caption)
553 this->minvalue = minvalue;
554 this->maxvalue = maxvalue;
556 this->precision = 0.1;
557 this->small_change = 0.1;
558 this->big_change = 1.0;
561 int BC_FSlider::value_to_pixel()
563 //printf("BC_FSlider::value_to_pixel %f %f\n", maxvalue, minvalue);
564 if(maxvalue == minvalue) return 0;
567 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
568 (get_h() - get_button_pixels()));
570 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
571 (get_w() - get_button_pixels()));
575 int BC_FSlider::update(float value)
577 if(this->value != value)
580 int old_pixel = button_pixel;
581 button_pixel = value_to_pixel();
582 //printf("BC_FSlider::update 1 %f %d\n", value, button_pixel);
583 if(button_pixel != old_pixel) draw_face(1);
588 int BC_FSlider::update(int pointer_motion_range, float value, float minvalue, float maxvalue)
590 this->minvalue = minvalue;
591 this->maxvalue = maxvalue;
593 this->pointer_motion_range = pointer_motion_range;
594 int old_pixel = button_pixel;
595 button_pixel = value_to_pixel();
596 if(button_pixel != old_pixel) draw_face(1);
601 float BC_FSlider::get_value()
606 float BC_FSlider::get_length()
608 return maxvalue - minvalue;
611 char* BC_FSlider::get_caption()
613 sprintf(caption, "%.02f", value);
617 int BC_FSlider::increase_value()
619 value += small_change;
620 if(value > maxvalue) value = maxvalue;
621 button_pixel = value_to_pixel();
625 int BC_FSlider::decrease_value()
627 value -= small_change;
628 if(value < minvalue) value = minvalue;
629 button_pixel = value_to_pixel();
633 int BC_FSlider::increase_value_big()
636 if(value > maxvalue) value = maxvalue;
637 button_pixel = value_to_pixel();
641 int BC_FSlider::decrease_value_big()
644 if(value < minvalue) value = minvalue;
645 button_pixel = value_to_pixel();
649 int BC_FSlider::init_selection(int cursor_x, int cursor_y)
653 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
654 min_pixel += cursor_y;
658 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
659 min_pixel += cursor_x;
661 max_pixel = min_pixel + pointer_motion_range;
665 int BC_FSlider::update_selection(int cursor_x, int cursor_y)
667 float old_value = value;
672 value = ((1.0 - (double)(cursor_y - min_pixel) /
673 pointer_motion_range) *
674 (maxvalue - minvalue) +
679 value = ((double)(cursor_x - min_pixel) /
680 pointer_motion_range *
681 (maxvalue - minvalue) +
685 value = Units::quantize(value, precision);
686 if(value > maxvalue) value = maxvalue;
687 if(value < minvalue) value = minvalue;
688 button_pixel = value_to_pixel();
689 // printf("BC_FSlider::update_selection 1 %d %d %d %d %f %f\n",
690 // pointer_motion_range, min_pixel, max_pixel, cursor_x, precision, value);
692 if(old_value != value)
699 void BC_FSlider::set_precision(float value)
701 this->precision = value;
704 void BC_FSlider::set_pagination(float small_change, float big_change)
706 this->small_change = small_change;
707 this->big_change = big_change;
711 BC_PercentageSlider::BC_PercentageSlider(int x, int y, int vertical, int pixels, int pointer_motion_range,
712 float minvalue, float maxvalue, float value, int use_caption,
714 : BC_FSlider(x, y, vertical, pixels, pointer_motion_range, minvalue, maxvalue, value, use_caption, data)
718 char* BC_PercentageSlider::get_caption()
720 sprintf(caption, "%.0f%%", floor((value - minvalue) / (maxvalue - minvalue) * 100));