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::get_span(int vertical)
89 return BC_WindowBase::get_resources()->vertical_slider_data[0]->get_w();
93 return BC_WindowBase::get_resources()->horizontal_slider_data[0]->get_h();
97 int BC_Slider::draw_face()
100 draw_top_background(parent_window, 0, 0, get_w(), get_h());
108 pixmaps[SLIDER_IMAGES / 2 + status]);
109 draw_pixmap(pixmaps[status], 0, button_pixel);
114 draw_text(0, h, get_caption());
119 int y = get_h() / 2 - pixmaps[SLIDER_IMAGES / 2 + status]->get_h() / 2;
123 pixmaps[SLIDER_IMAGES / 2 + status]);
124 draw_pixmap(pixmaps[status], button_pixel, 0);
129 draw_text(0, h, get_caption());
137 int BC_Slider::set_images(VFrame **images)
139 for(int i = 0; i < SLIDER_IMAGES; i++)
141 if(pixmaps[i]) delete pixmaps[i];
142 pixmaps[i] = new BC_Pixmap(parent_window, images[i], PIXMAP_ALPHA);
147 int BC_Slider::get_button_pixels()
149 return vertical ? pixmaps[SLIDER_UP]->get_h() :
150 pixmaps[SLIDER_UP]->get_w();
153 void BC_Slider::show_value_tooltip()
155 //printf("BC_Slider::show_value_tooltip %s\n", get_caption());
156 set_tooltip(get_caption());
157 keypress_tooltip_timer = 2000;
162 int BC_Slider::repeat_event(int64_t duration)
164 if(duration == top_level->get_resources()->tooltip_delay)
168 if(keypress_tooltip_timer > 0)
170 keypress_tooltip_timer -= get_resources()->tooltip_delay;
173 if(status != SLIDER_HI && status != SLIDER_DN)
179 if(status == SLIDER_HI)
181 if(!tooltip_text[0] || isdigit(tooltip_text[0]))
183 set_tooltip(get_caption());
188 //printf("BC_Slider::repeat_event 1 %s\n", tooltip_text);
189 set_tooltip(get_caption());
200 int BC_Slider::keypress_event()
203 if(!active || !enabled) return 0;
204 if(ctrl_down() || shift_down()) return 0;
206 switch(get_keypress())
229 show_value_tooltip();
235 int BC_Slider::cursor_enter_event()
237 //printf("BC_Slider::cursor_enter_event 1\n");
238 if(top_level->event_win == win && status == SLIDER_UP)
244 //printf("BC_Slider::cursor_enter_event 2\n");
248 int BC_Slider::cursor_leave_event()
250 if(status == SLIDER_HI)
259 int BC_Slider::deactivate()
265 int BC_Slider::activate()
267 top_level->active_subwindow = this;
271 int BC_Slider::button_press_event()
276 if(!tooltip_on) top_level->hide_tooltip();
277 if(status == SLIDER_HI)
279 if(get_buttonpress() == 4)
283 show_value_tooltip();
287 if(get_buttonpress() == 5)
291 show_value_tooltip();
295 if(get_buttonpress() == 1)
300 init_selection(top_level->cursor_x, top_level->cursor_y);
301 top_level->deactivate();
303 show_value_tooltip();
311 int BC_Slider::button_release_event()
321 top_level->hide_tooltip();
329 int BC_Slider::cursor_motion_event()
333 int old_pixel = button_pixel;
334 int result = update_selection(top_level->cursor_x, top_level->cursor_y);
335 if(button_pixel != old_pixel) draw_face();
339 set_tooltip(get_caption());
346 int BC_Slider::reposition_window(int x, int y, int w, int h)
348 BC_SubWindow::reposition_window(x, y, w, h);
349 button_pixel = value_to_pixel();
355 int BC_Slider::get_pointer_motion_range()
357 return pointer_motion_range;
364 BC_ISlider::BC_ISlider(int x,
368 int pointer_motion_range,
378 pointer_motion_range,
384 this->minvalue = minvalue;
385 this->maxvalue = maxvalue;
387 this->output = output;
390 int BC_ISlider::value_to_pixel()
392 if(maxvalue == minvalue) return 0;
396 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
397 (get_h() - get_button_pixels()));
399 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
400 (get_w() - get_button_pixels()));
404 int BC_ISlider::update(int64_t value)
406 if(this->value != value)
409 int old_pixel = button_pixel;
410 button_pixel = value_to_pixel();
411 if(button_pixel != old_pixel) draw_face();
416 int BC_ISlider::update(int pointer_motion_range,
421 this->minvalue = minvalue;
422 this->maxvalue = maxvalue;
424 this->pointer_motion_range = pointer_motion_range;
426 int old_pixel = button_pixel;
427 button_pixel = value_to_pixel();
428 if(button_pixel != old_pixel) draw_face();
433 int64_t BC_ISlider::get_value()
438 int64_t BC_ISlider::get_length()
440 return maxvalue - minvalue;
443 char* BC_ISlider::get_caption()
445 sprintf(caption, "%ld", value);
449 int BC_ISlider::increase_value()
452 if(value > maxvalue) value = maxvalue;
453 button_pixel = value_to_pixel();
457 int BC_ISlider::decrease_value()
460 if(value < minvalue) value = minvalue;
461 button_pixel = value_to_pixel();
465 int BC_ISlider::init_selection(int cursor_x, int cursor_y)
469 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
470 min_pixel += cursor_y;
474 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
475 min_pixel += cursor_x;
477 max_pixel = min_pixel + pointer_motion_range;
481 int BC_ISlider::update_selection(int cursor_x, int cursor_y)
483 int64_t old_value = value;
487 value = (int64_t)((1.0 - (double)(cursor_y - min_pixel) /
488 pointer_motion_range) *
489 (maxvalue - minvalue) +
494 value = (int64_t)((double)(cursor_x - min_pixel) /
495 pointer_motion_range *
496 (maxvalue - minvalue) +
500 if(value > maxvalue) value = maxvalue;
501 if(value < minvalue) value = minvalue;
502 button_pixel = value_to_pixel();
504 if(old_value != value)
511 int BC_ISlider::handle_event()
513 if(output) *output = get_value();
524 BC_FSlider::BC_FSlider(int x,
528 int pointer_motion_range,
537 pointer_motion_range,
543 this->minvalue = minvalue;
544 this->maxvalue = maxvalue;
546 this->precision = 0.1;
549 int BC_FSlider::value_to_pixel()
551 //printf("BC_FSlider::value_to_pixel %f %f\n", maxvalue, minvalue);
552 if(maxvalue == minvalue) return 0;
555 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
556 (get_h() - get_button_pixels()));
558 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
559 (get_w() - get_button_pixels()));
563 int BC_FSlider::update(float value)
565 if(this->value != value)
568 int old_pixel = button_pixel;
569 button_pixel = value_to_pixel();
570 //printf("BC_FSlider::update 1 %f %d\n", value, button_pixel);
571 if(button_pixel != old_pixel) draw_face();
576 int BC_FSlider::update(int pointer_motion_range, float value, float minvalue, float maxvalue)
578 this->minvalue = minvalue;
579 this->maxvalue = maxvalue;
581 this->pointer_motion_range = pointer_motion_range;
582 int old_pixel = button_pixel;
583 button_pixel = value_to_pixel();
584 if(button_pixel != old_pixel) draw_face();
589 float BC_FSlider::get_value()
594 float BC_FSlider::get_length()
596 return maxvalue - minvalue;
599 char* BC_FSlider::get_caption()
601 sprintf(caption, "%.02f", value);
605 int BC_FSlider::increase_value()
608 if(value > maxvalue) value = maxvalue;
609 button_pixel = value_to_pixel();
613 int BC_FSlider::decrease_value()
616 if(value < minvalue) value = minvalue;
617 button_pixel = value_to_pixel();
621 int BC_FSlider::init_selection(int cursor_x, int cursor_y)
625 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
626 min_pixel += cursor_y;
630 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
631 min_pixel += cursor_x;
633 max_pixel = min_pixel + pointer_motion_range;
637 int BC_FSlider::update_selection(int cursor_x, int cursor_y)
639 float old_value = value;
644 value = ((1.0 - (double)(cursor_y - min_pixel) /
645 pointer_motion_range) *
646 (maxvalue - minvalue) +
651 value = ((double)(cursor_x - min_pixel) /
652 pointer_motion_range *
653 (maxvalue - minvalue) +
657 value = Units::quantize(value, precision);
658 if(value > maxvalue) value = maxvalue;
659 if(value < minvalue) value = minvalue;
660 button_pixel = value_to_pixel();
661 // printf("BC_FSlider::update_selection 1 %d %d %d %d %f %f\n",
662 // pointer_motion_range,
669 if(old_value != value)
676 void BC_FSlider::set_precision(float value)
678 this->precision = value;
683 BC_PercentageSlider::BC_PercentageSlider(int x,
687 int pointer_motion_range,
697 pointer_motion_range,
706 char* BC_PercentageSlider::get_caption()
708 sprintf(caption, "%.0f%%", floor((value - minvalue) / (maxvalue - minvalue) * 100));
712 int BC_PercentageSlider::increase_value()
715 if(value > maxvalue) value = maxvalue;
716 button_pixel = value_to_pixel();
717 //printf("BC_PercentageSlider::increase_value %f\n", value);
721 int BC_PercentageSlider::decrease_value()
724 if(value < minvalue) value = minvalue;
725 button_pixel = value_to_pixel();
726 //printf("BC_PercentageSlider::decrease_value %f\n", value);