Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

bcslider.C

Go to the documentation of this file.
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 // Clear background
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 //printf("BC_Slider::show_value_tooltip %s\n", get_caption());
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 //printf("BC_Slider::repeat_event 1 %s\n", tooltip_text);
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();
00210                         result = 1;
00211                         break;
00212                 case DOWN:
00213                         decrease_value();
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                 show_value_tooltip();
00229                 draw_face();
00230                 handle_event();
00231         }
00232         return result;
00233 }
00234 
00235 int BC_Slider::cursor_enter_event()
00236 {
00237 //printf("BC_Slider::cursor_enter_event 1\n");
00238         if(top_level->event_win == win && status == SLIDER_UP)
00239         {
00240                 tooltip_done = 0;
00241                 status = SLIDER_HI;
00242                 draw_face();
00243         }
00244 //printf("BC_Slider::cursor_enter_event 2\n");
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                                 show_value_tooltip();
00283                                 draw_face();
00284                                 handle_event();
00285                         }
00286                         else
00287                         if(get_buttonpress() == 5)
00288                         {
00289                                 decrease_value();
00290                                 show_value_tooltip();
00291                                 draw_face();
00292                                 handle_event();
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                         set_tooltip(get_caption());
00339                         handle_event();
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--;
00460         if(value < minvalue) value = minvalue;
00461         button_pixel = value_to_pixel();
00462         return 0;
00463 }
00464 
00465 int BC_ISlider::init_selection(int cursor_x, int cursor_y)
00466 {
00467         if(vertical)
00468         {
00469                 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
00470                 min_pixel += cursor_y;
00471         }
00472         else
00473         {
00474                 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
00475                 min_pixel += cursor_x;
00476         }
00477         max_pixel = min_pixel + pointer_motion_range;
00478         return 0;
00479 }
00480 
00481 int BC_ISlider::update_selection(int cursor_x, int cursor_y)
00482 {
00483         int64_t old_value = value;
00484 
00485         if(vertical)
00486         {
00487                 value = (int64_t)((1.0 - (double)(cursor_y - min_pixel) / 
00488                         pointer_motion_range) * 
00489                         (maxvalue - minvalue) +
00490                         minvalue);
00491         }
00492         else
00493         {
00494                 value = (int64_t)((double)(cursor_x - min_pixel) / 
00495                         pointer_motion_range * 
00496                         (maxvalue - minvalue) +
00497                         minvalue);
00498         }
00499 
00500         if(value > maxvalue) value = maxvalue;
00501         if(value < minvalue) value = minvalue;
00502         button_pixel = value_to_pixel();
00503 
00504         if(old_value != value)
00505         {
00506                 return 1;
00507         }
00508         return 0;
00509 }
00510 
00511 int BC_ISlider::handle_event()
00512 {
00513         if(output) *output = get_value();
00514         return 1;
00515 }
00516 
00517 
00518 
00519 
00520 
00521 
00522 
00523 
00524 BC_FSlider::BC_FSlider(int x, 
00525                         int y,
00526                         int vertical,
00527                         int pixels, 
00528                         int pointer_motion_range, 
00529                         float minvalue, 
00530                         float maxvalue, 
00531                         float value,
00532                         int use_caption,
00533                         VFrame **data)
00534  : BC_Slider(x, 
00535                 y, 
00536                 pixels, 
00537                 pointer_motion_range,  
00538                 data,
00539                 1, 
00540                 vertical,
00541                 use_caption)
00542 {
00543         this->minvalue = minvalue;
00544         this->maxvalue = maxvalue;
00545         this->value = value;
00546         this->precision = 0.1;
00547 }
00548 
00549 int BC_FSlider::value_to_pixel()
00550 {
00551 //printf("BC_FSlider::value_to_pixel %f %f\n", maxvalue, minvalue);
00552         if(maxvalue == minvalue) return 0;
00553         {
00554                 if(vertical)
00555                         return (int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * 
00556                                 (get_h() - get_button_pixels()));
00557                 else
00558                         return (int)((double)(value - minvalue) / (maxvalue - minvalue) * 
00559                                 (get_w() - get_button_pixels()));
00560         }
00561 }
00562 
00563 int BC_FSlider::update(float value)
00564 {
00565         if(this->value != value)
00566         {
00567                 this->value = value;
00568                 int old_pixel = button_pixel;
00569                 button_pixel = value_to_pixel();
00570 //printf("BC_FSlider::update 1 %f %d\n", value, button_pixel);
00571                 if(button_pixel != old_pixel) draw_face();
00572         }
00573         return 0;
00574 }
00575 
00576 int BC_FSlider::update(int pointer_motion_range, float value, float minvalue, float maxvalue)
00577 {
00578         this->minvalue = minvalue;
00579         this->maxvalue = maxvalue;
00580         this->value = value;
00581         this->pointer_motion_range = pointer_motion_range;
00582         int old_pixel = button_pixel;
00583         button_pixel = value_to_pixel();
00584         if(button_pixel != old_pixel) draw_face();
00585         return 0;
00586 }
00587 
00588 
00589 float BC_FSlider::get_value()
00590 {
00591         return value;
00592 }
00593 
00594 float BC_FSlider::get_length()
00595 {
00596         return maxvalue - minvalue;
00597 }
00598 
00599 char* BC_FSlider::get_caption()
00600 {
00601         sprintf(caption, "%.02f", value);
00602         return caption;
00603 }
00604 
00605 int BC_FSlider::increase_value()
00606 {
00607         value += 0.1;
00608         if(value > maxvalue) value = maxvalue;
00609         button_pixel = value_to_pixel();
00610         return 0;
00611 }
00612 
00613 int BC_FSlider::decrease_value()
00614 {
00615         value -= 0.1;
00616         if(value < minvalue) value = minvalue;
00617         button_pixel = value_to_pixel();
00618         return 0;
00619 }
00620 
00621 int BC_FSlider::init_selection(int cursor_x, int cursor_y)
00622 {
00623         if(vertical)
00624         {
00625                 min_pixel = -(int)((1.0 - (double)(value - minvalue) / (maxvalue - minvalue)) * pointer_motion_range);
00626                 min_pixel += cursor_y;
00627         }
00628         else
00629         {
00630                 min_pixel = -(int)((double)(value - minvalue) / (maxvalue - minvalue) * pointer_motion_range);
00631                 min_pixel += cursor_x;
00632         }
00633         max_pixel = min_pixel + pointer_motion_range;
00634         return 0;
00635 }
00636 
00637 int BC_FSlider::update_selection(int cursor_x, int cursor_y)
00638 {
00639         float old_value = value;
00640 
00641 
00642         if(vertical)
00643         {
00644                 value = ((1.0 - (double)(cursor_y - min_pixel) / 
00645                         pointer_motion_range) * 
00646                         (maxvalue - minvalue) +
00647                         minvalue);
00648         }
00649         else
00650         {
00651                 value = ((double)(cursor_x - min_pixel) / 
00652                         pointer_motion_range * 
00653                         (maxvalue - minvalue) +
00654                         minvalue);
00655         }
00656 
00657         value = Units::quantize(value, precision);
00658         if(value > maxvalue) value = maxvalue;
00659         if(value < minvalue) value = minvalue;
00660         button_pixel = value_to_pixel();
00661 // printf("BC_FSlider::update_selection 1 %d %d %d %d %f\n", 
00662 // pointer_motion_range, 
00663 // min_pixel,
00664 // max_pixel,
00665 // cursor_x, 
00666 // value);
00667 
00668         if(old_value != value)
00669         {
00670                 return 1;
00671         }
00672         return 0;
00673 }
00674 
00675 void BC_FSlider::set_precision(float value)
00676 {
00677         this->precision = value;
00678 }
00679 
00680 
00681 
00682 BC_PercentageSlider::BC_PercentageSlider(int x, 
00683                         int y,
00684                         int vertical,
00685                         int pixels, 
00686                         int pointer_motion_range, 
00687                         float minvalue, 
00688                         float maxvalue, 
00689                         float value,
00690                         int use_caption,
00691                         VFrame **data)
00692  : BC_FSlider(x, 
00693                         y,
00694                         vertical,
00695                         pixels, 
00696                         pointer_motion_range, 
00697                         minvalue, 
00698                         maxvalue, 
00699                         value,
00700                         use_caption,
00701                         data)
00702 {
00703 }
00704 
00705 char* BC_PercentageSlider::get_caption()
00706 {
00707         sprintf(caption, "%.0f%%", floor((value - minvalue) / (maxvalue - minvalue) * 100));
00708         return caption;
00709 }
00710 
00711 int BC_PercentageSlider::increase_value()
00712 {
00713         value += 1;
00714         if(value > maxvalue) value = maxvalue;
00715         button_pixel = value_to_pixel();
00716 //printf("BC_PercentageSlider::increase_value %f\n", value);
00717         return 0;
00718 }
00719 
00720 int BC_PercentageSlider::decrease_value()
00721 {
00722         value -= 1;
00723         if(value < minvalue) value = minvalue;
00724         button_pixel = value_to_pixel();
00725 //printf("BC_PercentageSlider::decrease_value %f\n", value);
00726         return 0;
00727 }
00728 
00729 

Generated on Sun Jan 8 13:26:33 2006 for Guicast-svn by  doxygen 1.4.4