2 #include "bcresources.h"
17 BC_Slider::BC_Slider(int x,
20 int pointer_motion_range,
25 : BC_SubWindow(x, y, 0, 0, -1)
27 this->images = images;
28 this->show_number = show_number;
29 this->vertical = vertical;
30 this->pointer_motion_range = pointer_motion_range;
31 this->pixels = pixels;
32 this->button_pixel = button_pixel;
33 this->use_caption = use_caption;
36 pixmaps = new BC_Pixmap*[SLIDER_IMAGES];
37 for(int i = 0; i < SLIDER_IMAGES; i++)
46 BC_Slider::~BC_Slider()
48 for(int i = 0; i < SLIDER_IMAGES; i++)
50 if(pixmaps[i]) delete pixmaps[i];
52 if(pixmaps) delete [] pixmaps;
55 int BC_Slider::initialize()
59 this->images = vertical ?
60 BC_WindowBase::get_resources()->vertical_slider_data :
61 BC_WindowBase::get_resources()->horizontal_slider_data;
68 w = images[SLIDER_BG_UP]->get_w();
74 h = images[SLIDER_BG_UP]->get_h();
77 text_height = get_text_height(SMALLFONT);
78 button_pixel = value_to_pixel();
80 BC_SubWindow::initialize();
85 int BC_Slider::draw_face()
88 draw_top_background(parent_window, 0, 0, get_w(), get_h());
96 pixmaps[SLIDER_IMAGES / 2 + status]);
97 draw_pixmap(pixmaps[status], 0, button_pixel);
102 draw_text(0, h, get_caption());
107 int y = get_h() / 2 - pixmaps[SLIDER_IMAGES / 2 + status]->get_h() / 2;
111 pixmaps[SLIDER_IMAGES / 2 + status]);
112 draw_pixmap(pixmaps[status], button_pixel, 0);
117 draw_text(0, h, get_caption());
125 int BC_Slider::set_images(VFrame **images)
127 for(int i = 0; i < SLIDER_IMAGES; i++)
129 if(pixmaps[i]) delete pixmaps[i];
130 pixmaps[i] = new BC_Pixmap(parent_window, images[i], PIXMAP_ALPHA);
135 int BC_Slider::get_button_pixels()
137 return vertical ? pixmaps[SLIDER_UP]->get_h() :
138 pixmaps[SLIDER_UP]->get_w();
141 void BC_Slider::show_value_tooltip()
143 //printf("BC_Slider::show_value_tooltip %s\n", get_caption());
144 set_tooltip(get_caption());
145 keypress_tooltip_timer = 2000;
150 int BC_Slider::repeat_event(int64_t duration)
152 if(duration == top_level->get_resources()->tooltip_delay)
156 if(keypress_tooltip_timer > 0)
158 keypress_tooltip_timer -= get_resources()->tooltip_delay;
161 if(status != SLIDER_HI && status != SLIDER_DN)
167 if(status == SLIDER_HI)
169 if(!tooltip_text[0] || isdigit(tooltip_text[0]))
171 set_tooltip(get_caption());
176 //printf("BC_Slider::repeat_event 1 %s\n", tooltip_text);
177 set_tooltip(get_caption());
188 int BC_Slider::keypress_event()
191 if(!active || !enabled) return 0;
192 if(ctrl_down() || shift_down()) return 0;
194 switch(get_keypress())
216 show_value_tooltip();
223 int BC_Slider::cursor_enter_event()
225 //printf("BC_Slider::cursor_enter_event 1\n");
226 if(top_level->event_win == win && status == SLIDER_UP)
232 //printf("BC_Slider::cursor_enter_event 2\n");
236 int BC_Slider::cursor_leave_event()
238 if(status == SLIDER_HI)
247 int BC_Slider::deactivate()
253 int BC_Slider::activate()
255 top_level->active_subwindow = this;
259 int BC_Slider::button_press_event()
264 if(!tooltip_on) top_level->hide_tooltip();
265 if(status == SLIDER_HI)
267 if(get_buttonpress() == 4)
270 show_value_tooltip();
275 if(get_buttonpress() == 5)
278 show_value_tooltip();
283 if(get_buttonpress() == 1)
288 init_selection(top_level->cursor_x, top_level->cursor_y);
289 top_level->deactivate();
291 show_value_tooltip();
299 int BC_Slider::button_release_event()
309 top_level->hide_tooltip();
317 int BC_Slider::cursor_motion_event()
321 int old_pixel = button_pixel;
322 int result = update_selection(top_level->cursor_x, top_level->cursor_y);
323 if(button_pixel != old_pixel) draw_face();
326 set_tooltip(get_caption());
334 int BC_Slider::reposition_window(int x, int y, int w, int h)
336 BC_SubWindow::reposition_window(x, y, w, h);
337 button_pixel = value_to_pixel();
343 int BC_Slider::get_pointer_motion_range()
345 return pointer_motion_range;
352 BC_ISlider::BC_ISlider(int x,
356 int pointer_motion_range,
366 pointer_motion_range,
372 this->minvalue = minvalue;
373 this->maxvalue = maxvalue;
375 this->output = output;
378 int BC_ISlider::value_to_pixel()
380 if(maxvalue == minvalue) return 0;
384 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
385 (get_h() - get_button_pixels()));
387 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
388 (get_w() - get_button_pixels()));
392 int BC_ISlider::update(int64_t value)
394 if(this->value != value)
397 int old_pixel = button_pixel;
398 button_pixel = value_to_pixel();
399 if(button_pixel != old_pixel) draw_face();
404 int BC_ISlider::update(int pointer_motion_range,
409 this->minvalue = minvalue;
410 this->maxvalue = maxvalue;
412 this->pointer_motion_range = pointer_motion_range;
414 int old_pixel = button_pixel;
415 button_pixel = value_to_pixel();
416 if(button_pixel != old_pixel) draw_face();
421 int64_t BC_ISlider::get_value()
426 int64_t BC_ISlider::get_length()
428 return maxvalue - minvalue;
431 char* BC_ISlider::get_caption()
433 sprintf(caption, "%ld", value);
437 int BC_ISlider::increase_value()
440 if(value > maxvalue) value = maxvalue;
441 button_pixel = value_to_pixel();
445 int BC_ISlider::decrease_value()
448 if(value < minvalue) value = minvalue;
449 button_pixel = value_to_pixel();
453 int BC_ISlider::init_selection(int cursor_x, int cursor_y)
457 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
458 min_pixel += cursor_y;
462 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
463 min_pixel += cursor_x;
465 max_pixel = min_pixel + pointer_motion_range;
469 int BC_ISlider::update_selection(int cursor_x, int cursor_y)
471 int64_t old_value = value;
475 value = (int64_t)((1.0 - (double)(cursor_y - min_pixel) /
476 pointer_motion_range) *
477 (maxvalue - minvalue) +
482 value = (int64_t)((double)(cursor_x - min_pixel) /
483 pointer_motion_range *
484 (maxvalue - minvalue) +
488 if(value > maxvalue) value = maxvalue;
489 if(value < minvalue) value = minvalue;
490 button_pixel = value_to_pixel();
492 if(old_value != value)
499 int BC_ISlider::handle_event()
501 if(output) *output = get_value();
512 BC_FSlider::BC_FSlider(int x,
516 int pointer_motion_range,
525 pointer_motion_range,
531 this->minvalue = minvalue;
532 this->maxvalue = maxvalue;
534 this->precision = 0.1;
537 int BC_FSlider::value_to_pixel()
539 //printf("BC_FSlider::value_to_pixel %f %f\n", maxvalue, minvalue);
540 if(maxvalue == minvalue) return 0;
543 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
544 (get_h() - get_button_pixels()));
546 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
547 (get_w() - get_button_pixels()));
551 int BC_FSlider::update(float value)
553 if(this->value != value)
556 int old_pixel = button_pixel;
557 button_pixel = value_to_pixel();
558 //printf("BC_FSlider::update 1 %f %d\n", value, button_pixel);
559 if(button_pixel != old_pixel) draw_face();
564 int BC_FSlider::update(int pointer_motion_range, float value, float minvalue, float maxvalue)
566 this->minvalue = minvalue;
567 this->maxvalue = maxvalue;
569 this->pointer_motion_range = pointer_motion_range;
570 int old_pixel = button_pixel;
571 button_pixel = value_to_pixel();
572 if(button_pixel != old_pixel) draw_face();
577 float BC_FSlider::get_value()
582 float BC_FSlider::get_length()
584 return maxvalue - minvalue;
587 char* BC_FSlider::get_caption()
589 sprintf(caption, "%.02f", value);
593 int BC_FSlider::increase_value()
596 if(value > maxvalue) value = maxvalue;
597 button_pixel = value_to_pixel();
601 int BC_FSlider::decrease_value()
604 if(value < minvalue) value = minvalue;
605 button_pixel = value_to_pixel();
609 int BC_FSlider::init_selection(int cursor_x, int cursor_y)
613 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
614 min_pixel += cursor_y;
618 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
619 min_pixel += cursor_x;
621 max_pixel = min_pixel + pointer_motion_range;
625 int BC_FSlider::update_selection(int cursor_x, int cursor_y)
627 float old_value = value;
632 value = ((1.0 - (double)(cursor_y - min_pixel) /
633 pointer_motion_range) *
634 (maxvalue - minvalue) +
639 value = ((double)(cursor_x - min_pixel) /
640 pointer_motion_range *
641 (maxvalue - minvalue) +
645 value = Units::quantize(value, precision);
646 if(value > maxvalue) value = maxvalue;
647 if(value < minvalue) value = minvalue;
648 button_pixel = value_to_pixel();
649 // printf("BC_FSlider::update_selection 1 %d %d %d %d %f\n",
650 // pointer_motion_range,
656 if(old_value != value)
663 void BC_FSlider::set_precision(float value)
665 this->precision = value;
670 BC_PercentageSlider::BC_PercentageSlider(int x,
674 int pointer_motion_range,
684 pointer_motion_range,
693 char* BC_PercentageSlider::get_caption()
695 sprintf(caption, "%.0f%%", floor((value - minvalue) / (maxvalue - minvalue) * 100));
699 int BC_PercentageSlider::increase_value()
702 if(value > maxvalue) value = maxvalue;
703 button_pixel = value_to_pixel();
704 //printf("BC_PercentageSlider::increase_value %f\n", value);
708 int BC_PercentageSlider::decrease_value()
711 if(value < minvalue) value = minvalue;
712 button_pixel = value_to_pixel();
713 //printf("BC_PercentageSlider::decrease_value %f\n", value);