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 context_help_check_and_show();
214 if(ctrl_down() || shift_down()) return context_help_check_and_show();
216 switch(get_keypress())
219 increase_value_big();
223 decrease_value_big();
239 show_value_tooltip();
244 return context_help_check_and_show();
247 int BC_Slider::cursor_enter_event()
249 //printf("BC_Slider::cursor_enter_event 1\n");
250 if(top_level->event_win == win && status == SLIDER_UP)
255 //printf("BC_Slider::cursor_enter_event 2\n");
259 int BC_Slider::cursor_leave_event()
261 if(status == SLIDER_HI)
270 int BC_Slider::deactivate()
276 int BC_Slider::activate()
278 top_level->active_subwindow = this;
283 void BC_Slider::enable()
289 void BC_Slider::disable()
295 void BC_Slider::enable_show_value(int v)
300 int BC_Slider::button_press_event()
303 if(is_event_win() && enabled)
305 if(!tooltip_on) top_level->hide_tooltip();
306 if(status == SLIDER_HI)
308 if(get_buttonpress() == 4)
312 show_value_tooltip();
316 if(get_buttonpress() == 5)
320 show_value_tooltip();
324 if(get_buttonpress() == 1)
329 init_selection(top_level->cursor_x, top_level->cursor_y);
330 top_level->deactivate();
332 show_value_tooltip();
340 int BC_Slider::button_release_event()
350 top_level->hide_tooltip();
358 int BC_Slider::cursor_motion_event()
362 int old_pixel = button_pixel;
363 int result = update_selection(top_level->cursor_x, top_level->cursor_y);
364 if(button_pixel != old_pixel) draw_face(1);
368 set_tooltip(get_caption());
375 int BC_Slider::reposition_window(int x, int y, int w, int h)
377 BC_SubWindow::reposition_window(x, y, w, h);
378 button_pixel = value_to_pixel();
384 int BC_Slider::get_pointer_motion_range()
386 return pointer_motion_range;
389 void BC_Slider::set_pointer_motion_range(int value)
391 pointer_motion_range = value;
395 BC_ISlider::BC_ISlider(int x, int y, int vertical, int pixels, int pointer_motion_range,
396 int64_t minvalue, int64_t maxvalue, int64_t value, int use_caption,
397 VFrame **data, int *output)
398 : BC_Slider(x, y, pixels, pointer_motion_range, data, 1, vertical, use_caption)
400 this->minvalue = minvalue;
401 this->maxvalue = maxvalue;
403 this->output = output;
406 int BC_ISlider::value_to_pixel()
408 if(maxvalue == minvalue) return 0;
412 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
413 (get_h() - get_button_pixels()));
415 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
416 (get_w() - get_button_pixels()));
420 int BC_ISlider::update(int64_t value)
422 if(this->value != value)
425 int old_pixel = button_pixel;
426 button_pixel = value_to_pixel();
427 if(button_pixel != old_pixel) draw_face(1);
432 int BC_ISlider::update(int pointer_motion_range,
437 this->minvalue = minvalue;
438 this->maxvalue = maxvalue;
440 this->pointer_motion_range = pointer_motion_range;
442 int old_pixel = button_pixel;
443 button_pixel = value_to_pixel();
444 if(button_pixel != old_pixel) draw_face(1);
449 int64_t BC_ISlider::get_value()
454 int64_t BC_ISlider::get_length()
456 return maxvalue - minvalue;
459 char* BC_ISlider::get_caption()
461 sprintf(caption, "%jd", value);
465 int BC_ISlider::increase_value()
468 if(value > maxvalue) value = maxvalue;
469 button_pixel = value_to_pixel();
473 int BC_ISlider::decrease_value()
476 if(value < minvalue) value = minvalue;
477 button_pixel = value_to_pixel();
481 int BC_ISlider::increase_value_big()
484 if(value > maxvalue) value = maxvalue;
485 button_pixel = value_to_pixel();
489 int BC_ISlider::decrease_value_big()
492 if(value < minvalue) value = minvalue;
493 button_pixel = value_to_pixel();
497 int BC_ISlider::init_selection(int cursor_x, int cursor_y)
501 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
502 min_pixel += cursor_y;
506 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
507 min_pixel += cursor_x;
509 max_pixel = min_pixel + pointer_motion_range;
513 int BC_ISlider::update_selection(int cursor_x, int cursor_y)
515 int64_t old_value = value;
519 value = (int64_t)((1.0 - (double)(cursor_y - min_pixel) /
520 pointer_motion_range) *
521 (maxvalue - minvalue) +
526 value = (int64_t)((double)(cursor_x - min_pixel) /
527 pointer_motion_range *
528 (maxvalue - minvalue) +
532 if(value > maxvalue) value = maxvalue;
533 if(value < minvalue) value = minvalue;
534 button_pixel = value_to_pixel();
536 if(old_value != value)
543 int BC_ISlider::handle_event()
545 if(output) *output = get_value();
550 BC_FSlider::BC_FSlider(int x, int y, int vertical, int pixels, int pointer_motion_range,
551 float minvalue, float maxvalue, float value, int use_caption,
553 : BC_Slider(x, y, pixels, pointer_motion_range, data, 1, vertical, use_caption)
555 this->minvalue = minvalue;
556 this->maxvalue = maxvalue;
558 this->precision = 0.1;
559 this->small_change = 0.1;
560 this->big_change = 1.0;
563 int BC_FSlider::value_to_pixel()
565 //printf("BC_FSlider::value_to_pixel %f %f\n", maxvalue, minvalue);
566 if(maxvalue == minvalue) return 0;
569 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
570 (get_h() - get_button_pixels()));
572 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
573 (get_w() - get_button_pixels()));
577 int BC_FSlider::update(float value)
579 if(this->value != value)
582 int old_pixel = button_pixel;
583 button_pixel = value_to_pixel();
584 //printf("BC_FSlider::update 1 %f %d\n", value, button_pixel);
585 if(button_pixel != old_pixel) draw_face(1);
590 int BC_FSlider::update(int pointer_motion_range, float value, float minvalue, float maxvalue)
592 this->minvalue = minvalue;
593 this->maxvalue = maxvalue;
595 this->pointer_motion_range = pointer_motion_range;
596 int old_pixel = button_pixel;
597 button_pixel = value_to_pixel();
598 if(button_pixel != old_pixel) draw_face(1);
603 float BC_FSlider::get_value()
608 float BC_FSlider::get_length()
610 return maxvalue - minvalue;
613 char* BC_FSlider::get_caption()
615 sprintf(caption, "%.02f", value);
619 int BC_FSlider::increase_value()
621 value += small_change;
622 if(value > maxvalue) value = maxvalue;
623 button_pixel = value_to_pixel();
627 int BC_FSlider::decrease_value()
629 value -= small_change;
630 if(value < minvalue) value = minvalue;
631 button_pixel = value_to_pixel();
635 int BC_FSlider::increase_value_big()
638 if(value > maxvalue) value = maxvalue;
639 button_pixel = value_to_pixel();
643 int BC_FSlider::decrease_value_big()
646 if(value < minvalue) value = minvalue;
647 button_pixel = value_to_pixel();
651 int BC_FSlider::init_selection(int cursor_x, int cursor_y)
655 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
656 min_pixel += cursor_y;
660 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
661 min_pixel += cursor_x;
663 max_pixel = min_pixel + pointer_motion_range;
667 int BC_FSlider::update_selection(int cursor_x, int cursor_y)
669 float old_value = value;
674 value = ((1.0 - (double)(cursor_y - min_pixel) /
675 pointer_motion_range) *
676 (maxvalue - minvalue) +
681 value = ((double)(cursor_x - min_pixel) /
682 pointer_motion_range *
683 (maxvalue - minvalue) +
687 value = Units::quantize(value, precision);
688 if(value > maxvalue) value = maxvalue;
689 if(value < minvalue) value = minvalue;
690 button_pixel = value_to_pixel();
691 // printf("BC_FSlider::update_selection 1 %d %d %d %d %f %f\n",
692 // pointer_motion_range, min_pixel, max_pixel, cursor_x, precision, value);
694 if(old_value != value)
701 void BC_FSlider::set_precision(float value)
703 this->precision = value;
706 void BC_FSlider::set_pagination(float small_change, float big_change)
708 this->small_change = small_change;
709 this->big_change = big_change;
713 BC_PercentageSlider::BC_PercentageSlider(int x, int y, int vertical, int pixels, int pointer_motion_range,
714 float minvalue, float maxvalue, float value, int use_caption,
716 : BC_FSlider(x, y, vertical, pixels, pointer_motion_range, minvalue, maxvalue, value, use_caption, data)
720 char* BC_PercentageSlider::get_caption()
722 sprintf(caption, "%.0f%%", floor((value - minvalue) / (maxvalue - minvalue) * 100));