00001 #include "bcpixmap.h"
00002 #include "bcresources.h"
00003 #include "bcslider.h"
00004 #include "colors.h"
00005 #include "fonts.h"
00006 #include "keys.h"
00007 #include "units.h"
00008 #include "vframe.h"
00009
00010
00011
00012 #include <ctype.h>
00013 #include <string.h>
00014
00015
00016
00017 BC_Slider::BC_Slider(int x,
00018 int y,
00019 int pixels,
00020 int pointer_motion_range,
00021 VFrame **images,
00022 int show_number,
00023 int vertical,
00024 int use_caption)
00025 : BC_SubWindow(x, y, 0, 0, -1)
00026 {
00027 this->images = images;
00028 this->show_number = show_number;
00029 this->vertical = vertical;
00030 this->pointer_motion_range = pointer_motion_range;
00031 this->pixels = pixels;
00032 this->button_pixel = button_pixel;
00033 this->use_caption = use_caption;
00034
00035 status = SLIDER_UP;
00036 pixmaps = new BC_Pixmap*[SLIDER_IMAGES];
00037 for(int i = 0; i < SLIDER_IMAGES; i++)
00038 {
00039 pixmaps[i] = 0;
00040 }
00041 button_down = 0;
00042 enabled = 1;
00043 active = 0;
00044 }
00045
00046 BC_Slider::~BC_Slider()
00047 {
00048 for(int i = 0; i < SLIDER_IMAGES; i++)
00049 {
00050 if(pixmaps[i]) delete pixmaps[i];
00051 }
00052 if(pixmaps) delete [] pixmaps;
00053 }
00054
00055 int BC_Slider::initialize()
00056 {
00057 if(!images)
00058 {
00059 this->images = vertical ?
00060 BC_WindowBase::get_resources()->vertical_slider_data :
00061 BC_WindowBase::get_resources()->horizontal_slider_data;
00062 }
00063
00064 set_images(images);
00065
00066 if(vertical)
00067 {
00068 w = images[SLIDER_BG_UP]->get_w();
00069 h = pixels;
00070 }
00071 else
00072 {
00073 w = pixels;
00074 h = images[SLIDER_BG_UP]->get_h();
00075 }
00076
00077 text_height = get_text_height(SMALLFONT);
00078 button_pixel = value_to_pixel();
00079
00080 BC_SubWindow::initialize();
00081 draw_face();
00082 return 0;
00083 }
00084
00085 int BC_Slider::get_span(int vertical)
00086 {
00087 if(vertical)
00088 {
00089 return BC_WindowBase::get_resources()->vertical_slider_data[0]->get_w();
00090 }
00091 else
00092 {
00093 return BC_WindowBase::get_resources()->horizontal_slider_data[0]->get_h();
00094 }
00095 }
00096
00097 int BC_Slider::draw_face()
00098 {
00099
00100 draw_top_background(parent_window, 0, 0, get_w(), get_h());
00101
00102
00103 if(vertical)
00104 {
00105 draw_3segmentv(0,
00106 0,
00107 get_h(),
00108 pixmaps[SLIDER_IMAGES / 2 + status]);
00109 draw_pixmap(pixmaps[status], 0, button_pixel);
00110 if(use_caption)
00111 {
00112 set_color(RED);
00113 set_font(SMALLFONT);
00114 draw_text(0, h, get_caption());
00115 }
00116 }
00117 else
00118 {
00119 int y = get_h() / 2 - pixmaps[SLIDER_IMAGES / 2 + status]->get_h() / 2;
00120 draw_3segmenth(0,
00121 0,
00122 get_w(),
00123 pixmaps[SLIDER_IMAGES / 2 + status]);
00124 draw_pixmap(pixmaps[status], button_pixel, 0);
00125 if(use_caption)
00126 {
00127 set_color(RED);
00128 set_font(SMALLFONT);
00129 draw_text(0, h, get_caption());
00130 }
00131 }
00132
00133 flash();
00134 return 0;
00135 }
00136
00137 int BC_Slider::set_images(VFrame **images)
00138 {
00139 for(int i = 0; i < SLIDER_IMAGES; i++)
00140 {
00141 if(pixmaps[i]) delete pixmaps[i];
00142 pixmaps[i] = new BC_Pixmap(parent_window, images[i], PIXMAP_ALPHA);
00143 }
00144 return 0;
00145 }
00146
00147 int BC_Slider::get_button_pixels()
00148 {
00149 return vertical ? pixmaps[SLIDER_UP]->get_h() :
00150 pixmaps[SLIDER_UP]->get_w();
00151 }
00152
00153 void BC_Slider::show_value_tooltip()
00154 {
00155
00156 set_tooltip(get_caption());
00157 keypress_tooltip_timer = 2000;
00158 show_tooltip(50);
00159 }
00160
00161
00162 int BC_Slider::repeat_event(int64_t duration)
00163 {
00164 if(duration == top_level->get_resources()->tooltip_delay)
00165 {
00166 if(tooltip_on)
00167 {
00168 if(keypress_tooltip_timer > 0)
00169 {
00170 keypress_tooltip_timer -= get_resources()->tooltip_delay;
00171 }
00172 else
00173 if(status != SLIDER_HI && status != SLIDER_DN)
00174 {
00175 hide_tooltip();
00176 }
00177 }
00178 else
00179 if(status == SLIDER_HI)
00180 {
00181 if(!tooltip_text[0] || isdigit(tooltip_text[0]))
00182 {
00183 set_tooltip(get_caption());
00184 show_tooltip(50);
00185 }
00186 else
00187 {
00188
00189 set_tooltip(get_caption());
00190 show_tooltip();
00191 }
00192 tooltip_done = 1;
00193 return 1;
00194 }
00195 }
00196 return 0;
00197 }
00198
00199
00200 int BC_Slider::keypress_event()
00201 {
00202 int result = 0;
00203 if(!active || !enabled) return 0;
00204 if(ctrl_down() || shift_down()) return 0;
00205
00206 switch(get_keypress())
00207 {
00208 case UP:
00209 increase_value_big();
00210 result = 1;
00211 break;
00212 case DOWN:
00213 decrease_value_big();
00214 result = 1;
00215 break;
00216 case LEFT:
00217 decrease_value();
00218 result = 1;
00219 break;
00220 case RIGHT:
00221 increase_value();
00222 result = 1;
00223 break;
00224 }
00225
00226 if(result)
00227 {
00228 handle_event();
00229 show_value_tooltip();
00230 draw_face();
00231 }
00232 return result;
00233 }
00234
00235 int BC_Slider::cursor_enter_event()
00236 {
00237
00238 if(top_level->event_win == win && status == SLIDER_UP)
00239 {
00240 tooltip_done = 0;
00241 status = SLIDER_HI;
00242 draw_face();
00243 }
00244
00245 return 0;
00246 }
00247
00248 int BC_Slider::cursor_leave_event()
00249 {
00250 if(status == SLIDER_HI)
00251 {
00252 status = SLIDER_UP;
00253 draw_face();
00254 hide_tooltip();
00255 }
00256 return 0;
00257 }
00258
00259 int BC_Slider::deactivate()
00260 {
00261 active = 0;
00262 return 0;
00263 }
00264
00265 int BC_Slider::activate()
00266 {
00267 top_level->active_subwindow = this;
00268 active = 1;
00269 }
00270
00271 int BC_Slider::button_press_event()
00272 {
00273 int result = 0;
00274 if(is_event_win())
00275 {
00276 if(!tooltip_on) top_level->hide_tooltip();
00277 if(status == SLIDER_HI)
00278 {
00279 if(get_buttonpress() == 4)
00280 {
00281 increase_value();
00282 handle_event();
00283 show_value_tooltip();
00284 draw_face();
00285 }
00286 else
00287 if(get_buttonpress() == 5)
00288 {
00289 decrease_value();
00290 handle_event();
00291 show_value_tooltip();
00292 draw_face();
00293 }
00294 else
00295 if(get_buttonpress() == 1)
00296 {
00297 button_down = 1;
00298 status = SLIDER_DN;
00299 draw_face();
00300 init_selection(top_level->cursor_x, top_level->cursor_y);
00301 top_level->deactivate();
00302 activate();
00303 show_value_tooltip();
00304 }
00305 result = 1;
00306 }
00307 }
00308 return result;
00309 }
00310
00311 int BC_Slider::button_release_event()
00312 {
00313 if(button_down)
00314 {
00315 button_down = 0;
00316 if(cursor_inside())
00317 status = SLIDER_HI;
00318 else
00319 {
00320 status = SLIDER_UP;
00321 top_level->hide_tooltip();
00322 }
00323 draw_face();
00324 return 1;
00325 }
00326 return 0;
00327 }
00328
00329 int BC_Slider::cursor_motion_event()
00330 {
00331 if(button_down)
00332 {
00333 int old_pixel = button_pixel;
00334 int result = update_selection(top_level->cursor_x, top_level->cursor_y);
00335 if(button_pixel != old_pixel) draw_face();
00336 if(result)
00337 {
00338 handle_event();
00339 set_tooltip(get_caption());
00340 }
00341 return 1;
00342 }
00343 return 0;
00344 }
00345
00346 int BC_Slider::reposition_window(int x, int y, int w, int h)
00347 {
00348 BC_SubWindow::reposition_window(x, y, w, h);
00349 button_pixel = value_to_pixel();
00350 draw_face();
00351 return 0;
00352 }
00353
00354
00355 int BC_Slider::get_pointer_motion_range()
00356 {
00357 return pointer_motion_range;
00358 }
00359
00360
00361
00362
00363
00364 BC_ISlider::BC_ISlider(int x,
00365 int y,
00366 int vertical,
00367 int pixels,
00368 int pointer_motion_range,
00369 int64_t minvalue,
00370 int64_t maxvalue,
00371 int64_t value,
00372 int use_caption,
00373 VFrame **data,
00374 int *output)
00375 : BC_Slider(x,
00376 y,
00377 pixels,
00378 pointer_motion_range,
00379 data,
00380 1,
00381 vertical,
00382 use_caption)
00383 {
00384 this->minvalue = minvalue;
00385 this->maxvalue = maxvalue;
00386 this->value = value;
00387 this->output = output;
00388 }
00389
00390 int BC_ISlider::value_to_pixel()
00391 {
00392 if(maxvalue == minvalue) return 0;
00393 else
00394 {
00395 if(vertical)
00396 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
00397 (get_h() - get_button_pixels()));
00398 else
00399 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
00400 (get_w() - get_button_pixels()));
00401 }
00402 }
00403
00404 int BC_ISlider::update(int64_t value)
00405 {
00406 if(this->value != value)
00407 {
00408 this->value = value;
00409 int old_pixel = button_pixel;
00410 button_pixel = value_to_pixel();
00411 if(button_pixel != old_pixel) draw_face();
00412 }
00413 return 0;
00414 }
00415
00416 int BC_ISlider::update(int pointer_motion_range,
00417 int64_t value,
00418 int64_t minvalue,
00419 int64_t maxvalue)
00420 {
00421 this->minvalue = minvalue;
00422 this->maxvalue = maxvalue;
00423 this->value = value;
00424 this->pointer_motion_range = pointer_motion_range;
00425
00426 int old_pixel = button_pixel;
00427 button_pixel = value_to_pixel();
00428 if(button_pixel != old_pixel) draw_face();
00429 return 0;
00430 }
00431
00432
00433 int64_t BC_ISlider::get_value()
00434 {
00435 return value;
00436 }
00437
00438 int64_t BC_ISlider::get_length()
00439 {
00440 return maxvalue - minvalue;
00441 }
00442
00443 char* BC_ISlider::get_caption()
00444 {
00445 sprintf(caption, "%ld", value);
00446 return caption;
00447 }
00448
00449 int BC_ISlider::increase_value()
00450 {
00451 value++;
00452 if(value > maxvalue) value = maxvalue;
00453 button_pixel = value_to_pixel();
00454 return 0;
00455 }
00456
00457 int BC_ISlider::decrease_value()
00458 {
00459 value-=10;
00460 if(value < minvalue) value = minvalue;
00461 button_pixel = value_to_pixel();
00462 return 0;
00463 }
00464
00465 int BC_ISlider::increase_value_big()
00466 {
00467 value+=10;
00468 if(value > maxvalue) value = maxvalue;
00469 button_pixel = value_to_pixel();
00470 return 0;
00471 }
00472
00473 int BC_ISlider::decrease_value_big()
00474 {
00475 value--;
00476 if(value < minvalue) value = minvalue;
00477 button_pixel = value_to_pixel();
00478 return 0;
00479 }
00480
00481 int BC_ISlider::init_selection(int cursor_x, int cursor_y)
00482 {
00483 if(vertical)
00484 {
00485 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
00486 min_pixel += cursor_y;
00487 }
00488 else
00489 {
00490 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
00491 min_pixel += cursor_x;
00492 }
00493 max_pixel = min_pixel + pointer_motion_range;
00494 return 0;
00495 }
00496
00497 int BC_ISlider::update_selection(int cursor_x, int cursor_y)
00498 {
00499 int64_t old_value = value;
00500
00501 if(vertical)
00502 {
00503 value = (int64_t)((1.0 - (double)(cursor_y - min_pixel) /
00504 pointer_motion_range) *
00505 (maxvalue - minvalue) +
00506 minvalue);
00507 }
00508 else
00509 {
00510 value = (int64_t)((double)(cursor_x - min_pixel) /
00511 pointer_motion_range *
00512 (maxvalue - minvalue) +
00513 minvalue);
00514 }
00515
00516 if(value > maxvalue) value = maxvalue;
00517 if(value < minvalue) value = minvalue;
00518 button_pixel = value_to_pixel();
00519
00520 if(old_value != value)
00521 {
00522 return 1;
00523 }
00524 return 0;
00525 }
00526
00527 int BC_ISlider::handle_event()
00528 {
00529 if(output) *output = get_value();
00530 return 1;
00531 }
00532
00533
00534
00535
00536
00537
00538
00539
00540 BC_FSlider::BC_FSlider(int x,
00541 int y,
00542 int vertical,
00543 int pixels,
00544 int pointer_motion_range,
00545 float minvalue,
00546 float maxvalue,
00547 float value,
00548 int use_caption,
00549 VFrame **data)
00550 : BC_Slider(x,
00551 y,
00552 pixels,
00553 pointer_motion_range,
00554 data,
00555 1,
00556 vertical,
00557 use_caption)
00558 {
00559 this->minvalue = minvalue;
00560 this->maxvalue = maxvalue;
00561 this->value = value;
00562 this->precision = 0.1;
00563 this->small_change = 0.1;
00564 this->big_change = 1.0;
00565 }
00566
00567 int BC_FSlider::value_to_pixel()
00568 {
00569
00570 if(maxvalue == minvalue) return 0;
00571 {
00572 if(vertical)
00573 return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) *
00574 (get_h() - get_button_pixels()));
00575 else
00576 return (int)((double)(value - minvalue) / (maxvalue - minvalue) *
00577 (get_w() - get_button_pixels()));
00578 }
00579 }
00580
00581 int BC_FSlider::update(float value)
00582 {
00583 if(this->value != value)
00584 {
00585 this->value = value;
00586 int old_pixel = button_pixel;
00587 button_pixel = value_to_pixel();
00588
00589 if(button_pixel != old_pixel) draw_face();
00590 }
00591 return 0;
00592 }
00593
00594 int BC_FSlider::update(int pointer_motion_range, float value, float minvalue, float maxvalue)
00595 {
00596 this->minvalue = minvalue;
00597 this->maxvalue = maxvalue;
00598 this->value = value;
00599 this->pointer_motion_range = pointer_motion_range;
00600 int old_pixel = button_pixel;
00601 button_pixel = value_to_pixel();
00602 if(button_pixel != old_pixel) draw_face();
00603 return 0;
00604 }
00605
00606
00607 float BC_FSlider::get_value()
00608 {
00609 return value;
00610 }
00611
00612 float BC_FSlider::get_length()
00613 {
00614 return maxvalue - minvalue;
00615 }
00616
00617 char* BC_FSlider::get_caption()
00618 {
00619 sprintf(caption, "%.02f", value);
00620 return caption;
00621 }
00622
00623 int BC_FSlider::increase_value()
00624 {
00625 value += small_change;
00626 if(value > maxvalue) value = maxvalue;
00627 button_pixel = value_to_pixel();
00628 return 0;
00629 }
00630
00631 int BC_FSlider::decrease_value()
00632 {
00633 value -= small_change;
00634 if(value < minvalue) value = minvalue;
00635 button_pixel = value_to_pixel();
00636 return 0;
00637 }
00638
00639 int BC_FSlider::increase_value_big()
00640 {
00641 value += big_change;
00642 if(value > maxvalue) value = maxvalue;
00643 button_pixel = value_to_pixel();
00644 return 0;
00645 }
00646
00647 int BC_FSlider::decrease_value_big()
00648 {
00649 value -= big_change;
00650 if(value < minvalue) value = minvalue;
00651 button_pixel = value_to_pixel();
00652 return 0;
00653 }
00654
00655 int BC_FSlider::init_selection(int cursor_x, int cursor_y)
00656 {
00657 if(vertical)
00658 {
00659 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
00660 min_pixel += cursor_y;
00661 }
00662 else
00663 {
00664 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
00665 min_pixel += cursor_x;
00666 }
00667 max_pixel = min_pixel + pointer_motion_range;
00668 return 0;
00669 }
00670
00671 int BC_FSlider::update_selection(int cursor_x, int cursor_y)
00672 {
00673 float old_value = value;
00674
00675
00676 if(vertical)
00677 {
00678 value = ((1.0 - (double)(cursor_y - min_pixel) /
00679 pointer_motion_range) *
00680 (maxvalue - minvalue) +
00681 minvalue);
00682 }
00683 else
00684 {
00685 value = ((double)(cursor_x - min_pixel) /
00686 pointer_motion_range *
00687 (maxvalue - minvalue) +
00688 minvalue);
00689 }
00690
00691 value = Units::quantize(value, precision);
00692 if(value > maxvalue) value = maxvalue;
00693 if(value < minvalue) value = minvalue;
00694 button_pixel = value_to_pixel();
00695
00696
00697
00698
00699
00700
00701
00702
00703 if(old_value != value)
00704 {
00705 return 1;
00706 }
00707 return 0;
00708 }
00709
00710 void BC_FSlider::set_precision(float value)
00711 {
00712 this->precision = value;
00713 }
00714
00715 void BC_FSlider::set_pagination(float small_change, float big_change)
00716 {
00717 this->small_change = small_change;
00718 this->big_change = big_change;
00719 }
00720
00721
00722
00723 BC_PercentageSlider::BC_PercentageSlider(int x,
00724 int y,
00725 int vertical,
00726 int pixels,
00727 int pointer_motion_range,
00728 float minvalue,
00729 float maxvalue,
00730 float value,
00731 int use_caption,
00732 VFrame **data)
00733 : BC_FSlider(x,
00734 y,
00735 vertical,
00736 pixels,
00737 pointer_motion_range,
00738 minvalue,
00739 maxvalue,
00740 value,
00741 use_caption,
00742 data)
00743 {
00744 }
00745
00746 char* BC_PercentageSlider::get_caption()
00747 {
00748 sprintf(caption, "%.0f%%", floor((value - minvalue) / (maxvalue - minvalue) * 100));
00749 return caption;
00750 }
00751