home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / guicast / bcscrollbar.C < prev    next >
C/C++ Source or Header  |  2000-11-29  |  11KB  |  460 lines

  1. #include "bcresources.h"
  2. #include "bcscrollbar.h"
  3. #include "colors.h"
  4.  
  5. BC_ScrollBar::BC_ScrollBar(int x, 
  6.     int y, 
  7.     int orientation, 
  8.     int pixels, 
  9.     long length, 
  10.     long position, 
  11.     long handlelength)
  12.  : BC_SubWindow(x, y, 0, 0, -1)
  13. {
  14.     this->length = length;
  15.     this->position = position;
  16.     this->handlelength = handlelength;
  17.     this->selection_status = 0;
  18.     this->highlight_status = 0;
  19.     this->orientation = orientation;
  20.     this->pixels = pixels;
  21.     handle_pixel = 0;
  22.     handle_pixels = 0;
  23.     bound_to = 0;
  24.     repeat_count = 0;
  25. }
  26.  
  27. BC_ScrollBar::~BC_ScrollBar()
  28. {
  29. }
  30.  
  31. int BC_ScrollBar::initialize()
  32. {
  33.     switch(orientation)
  34.     {
  35.         case SCROLL_HORIZ:
  36.             w = pixels;
  37.             h = SCROLL_SPAN;
  38.             break;
  39.  
  40.         case SCROLL_VERT:
  41.             w = SCROLL_SPAN;
  42.             h = pixels;
  43.             break;
  44.     }
  45.  
  46.     BC_SubWindow::initialize();
  47.     draw();
  48.     return 0;
  49. }
  50.  
  51. void BC_ScrollBar::draw()
  52. {
  53.     get_handle_dimensions();
  54.     switch(orientation)
  55.     {
  56.         case SCROLL_HORIZ:
  57.             if(get_w() < SCROLL_SPAN * 2 + 5)
  58.             {
  59.                 draw_colored_box(0, 0, get_w(), get_h(), 0, 0);
  60.             }
  61.             else
  62.             {
  63. // background box
  64.                 draw_colored_box(0, 0, get_w(), get_h(), 1, 0);
  65. // handle
  66.                 draw_colored_box(handle_pixel, 
  67.                     SCROLL_MARGIN, 
  68.                     handle_pixels, 
  69.                     SCROLL_SPAN - SCROLL_MARGIN * 2, 
  70.                     selection_status == SCROLL_HANDLE, 
  71.                     highlight_status == SCROLL_HANDLE && selection_status != SCROLL_HANDLE);
  72.  
  73. // back arrow
  74.                 if(selection_status == SCROLL_BACKARROW)
  75.                     draw_triangle_left(SCROLL_MARGIN, 
  76.                         SCROLL_MARGIN, 
  77.                         SCROLL_SPAN - SCROLL_MARGIN, 
  78.                         SCROLL_SPAN - SCROLL_MARGIN * 2, 
  79.                         get_resources()->button_shadow, 
  80.                         BLACK, 
  81.                         get_resources()->button_down, 
  82.                         get_resources()->button_down, 
  83.                         get_resources()->button_light);
  84.                 else
  85.                 if(highlight_status == SCROLL_BACKARROW)
  86.                     draw_triangle_left(SCROLL_MARGIN, 
  87.                         SCROLL_MARGIN, 
  88.                         SCROLL_SPAN - SCROLL_MARGIN, 
  89.                         SCROLL_SPAN - SCROLL_MARGIN * 2, 
  90.                         get_resources()->button_light, 
  91.                         get_resources()->button_highlighted, 
  92.                         get_resources()->button_highlighted, 
  93.                         get_resources()->button_down, 
  94.                         BLACK);
  95.                 else
  96.                     draw_triangle_left(SCROLL_MARGIN, 
  97.                         SCROLL_MARGIN, 
  98.                         SCROLL_SPAN - SCROLL_MARGIN, 
  99.                         SCROLL_SPAN - SCROLL_MARGIN * 2, 
  100.                         get_resources()->button_light, 
  101.                         get_resources()->button_up, 
  102.                         get_resources()->button_up, 
  103.                         get_resources()->button_down, 
  104.                         BLACK);
  105.  
  106. // forward arrow
  107.                 if(selection_status == SCROLL_FWDARROW)
  108.                     draw_triangle_right(get_w() - SCROLL_SPAN, 
  109.                         SCROLL_MARGIN, 
  110.                         SCROLL_SPAN - SCROLL_MARGIN, 
  111.                         SCROLL_SPAN - SCROLL_MARGIN * 2, 
  112.                         get_resources()->button_shadow, 
  113.                         BLACK, 
  114.                         get_resources()->button_down, 
  115.                         get_resources()->button_down, 
  116.                         get_resources()->button_light);
  117.                 else
  118.                 if(highlight_status == SCROLL_FWDARROW)
  119.                     draw_triangle_right(get_w() - SCROLL_SPAN, 
  120.                         SCROLL_MARGIN, 
  121.                         SCROLL_SPAN - SCROLL_MARGIN, 
  122.                         SCROLL_SPAN - SCROLL_MARGIN * 2, 
  123.                         get_resources()->button_light, 
  124.                         get_resources()->button_highlighted, 
  125.                         get_resources()->button_highlighted, 
  126.                         get_resources()->button_down, 
  127.                         BLACK);
  128.                 else
  129.                     draw_triangle_right(get_w() - SCROLL_SPAN, 
  130.                         SCROLL_MARGIN, 
  131.                         SCROLL_SPAN - SCROLL_MARGIN, 
  132.                         SCROLL_SPAN - SCROLL_MARGIN * 2, 
  133.                         get_resources()->button_light, 
  134.                         get_resources()->button_up, 
  135.                         get_resources()->button_up, 
  136.                         get_resources()->button_down, 
  137.                         BLACK);
  138.             }
  139.             break;
  140.         
  141.         case SCROLL_VERT:
  142.             if(get_h() < SCROLL_SPAN * 2 + 5)
  143.             {
  144.                 draw_colored_box(0, 0, get_w(), get_h(), 0, 0);
  145.             }
  146.             else
  147.             {
  148. // background box
  149.                 draw_colored_box(0, 0, get_w(), get_h(), 1, 0);
  150. // handle
  151.                 draw_colored_box(SCROLL_MARGIN, 
  152.                     handle_pixel, 
  153.                     SCROLL_SPAN - SCROLL_MARGIN * 2, 
  154.                     handle_pixels, 
  155.                     selection_status == SCROLL_HANDLE, 
  156.                     highlight_status == SCROLL_HANDLE && selection_status != SCROLL_HANDLE);
  157.  
  158. // back arrow
  159.                 if(selection_status == SCROLL_BACKARROW)
  160.                     draw_triangle_up(SCROLL_MARGIN, SCROLL_MARGIN, SCROLL_SPAN - SCROLL_MARGIN * 2, SCROLL_SPAN - SCROLL_MARGIN, 
  161.                         get_resources()->button_shadow, 
  162.                         BLACK, 
  163.                         get_resources()->button_down, 
  164.                         get_resources()->button_down, 
  165.                         get_resources()->button_light);
  166.                 else
  167.                 if(highlight_status == SCROLL_BACKARROW)
  168.                     draw_triangle_up(SCROLL_MARGIN, SCROLL_MARGIN, SCROLL_SPAN - SCROLL_MARGIN * 2, SCROLL_SPAN - SCROLL_MARGIN, 
  169.                         get_resources()->button_light, 
  170.                         get_resources()->button_highlighted, 
  171.                         get_resources()->button_highlighted, 
  172.                         get_resources()->button_down, 
  173.                         BLACK);
  174.                 else
  175.                     draw_triangle_up(SCROLL_MARGIN, SCROLL_MARGIN, SCROLL_SPAN - SCROLL_MARGIN * 2, SCROLL_SPAN - SCROLL_MARGIN, 
  176.                         get_resources()->button_light, 
  177.                         get_resources()->button_up, 
  178.                         get_resources()->button_up, 
  179.                         get_resources()->button_down, 
  180.                         BLACK);
  181.  
  182. // forward arrow
  183.                 if(selection_status == SCROLL_FWDARROW)
  184.                     draw_triangle_down(SCROLL_MARGIN, 
  185.                         get_h() - SCROLL_SPAN, 
  186.                         SCROLL_SPAN - SCROLL_MARGIN * 2, 
  187.                         SCROLL_SPAN - SCROLL_MARGIN, 
  188.                         get_resources()->button_shadow, 
  189.                         BLACK, 
  190.                         get_resources()->button_down, 
  191.                         get_resources()->button_down, 
  192.                         get_resources()->button_light);
  193.                 else
  194.                 if(highlight_status == SCROLL_FWDARROW)
  195.                     draw_triangle_down(SCROLL_MARGIN, 
  196.                         get_h() - SCROLL_SPAN, 
  197.                         SCROLL_SPAN - SCROLL_MARGIN * 2, 
  198.                         SCROLL_SPAN - SCROLL_MARGIN, 
  199.                         get_resources()->button_light, 
  200.                         get_resources()->button_highlighted, 
  201.                         get_resources()->button_highlighted, 
  202.                         get_resources()->button_down, 
  203.                         BLACK);
  204.                 else
  205.                     draw_triangle_down(SCROLL_MARGIN, 
  206.                         get_h() - SCROLL_SPAN, 
  207.                         SCROLL_SPAN - SCROLL_MARGIN * 2, 
  208.                         SCROLL_SPAN - SCROLL_MARGIN, 
  209.                         get_resources()->button_light, 
  210.                         get_resources()->button_up, 
  211.                         get_resources()->button_up, 
  212.                         get_resources()->button_down, 
  213.                         BLACK);
  214.             }
  215.             break;
  216.     }
  217.     flash();
  218. }
  219.  
  220. void BC_ScrollBar::get_handle_dimensions()
  221. {
  222.     double total_pixels = pixels - SCROLL_SPAN * 2;
  223.     handle_pixels = (long)((double)handlelength / length * total_pixels + .5);
  224.     if(handle_pixels < MINHANDLE) handle_pixels = MINHANDLE;
  225.     handle_pixel = (long)((double)position / length * total_pixels + .5) + SCROLL_SPAN;
  226.     if(handle_pixel > pixels - SCROLL_SPAN - handle_pixels)
  227.         handle_pixel = pixels - SCROLL_SPAN - handle_pixels;
  228. }
  229.  
  230. int BC_ScrollBar::cursor_enter_event()
  231. {
  232.     if(top_level->event_win == win)
  233.     {
  234.         if(!highlight_status)
  235.         {
  236.             highlight_status = get_cursor_zone(top_level->cursor_x, top_level->cursor_y);
  237.             draw();
  238.             flash();
  239.         }
  240.         return 1;
  241.     }
  242.     return 0;
  243. }
  244.  
  245. int BC_ScrollBar::cursor_leave_event()
  246. {
  247.     if(highlight_status)
  248.     {
  249.         highlight_status = 0;
  250.         draw();
  251.         flash();
  252.     }
  253.     return 0;
  254. }
  255.  
  256. int BC_ScrollBar::cursor_motion_event()
  257. {
  258.     if(top_level->event_win == win)
  259.     {
  260.         if(highlight_status && !selection_status)
  261.         {
  262.             int new_highlight_status = get_cursor_zone(top_level->cursor_x, top_level->cursor_y);
  263.             if(new_highlight_status != highlight_status)
  264.             {
  265.                 highlight_status = new_highlight_status;
  266.                 draw();
  267.                 flash();
  268.             }
  269.         }
  270.         else
  271.         if(selection_status == SCROLL_HANDLE)
  272.         {
  273.             
  274.             double total_pixels = pixels - SCROLL_SPAN * 2;
  275.             long cursor_pixel = (orientation == SCROLL_HORIZ) ? top_level->cursor_x : top_level->cursor_y;
  276.             long new_position = (long)((double)(cursor_pixel - min_pixel) / total_pixels * length);
  277.             if(new_position > length - handlelength) new_position = length - handlelength;
  278.             else
  279.             if(new_position < 0) new_position = 0;
  280.             if(new_position != position)
  281.             {
  282.                 position = new_position;
  283.                 draw();
  284.                 flash();
  285.                 handle_event();
  286.             }
  287.         }
  288.         return 1;
  289.     }
  290.     return 0;
  291. }
  292.  
  293. int BC_ScrollBar::button_press_event()
  294. {
  295.     if(top_level->event_win == win)
  296.     {
  297.         if(!bound_to)
  298.         {
  299.             top_level->deactivate();
  300.             activate();
  301.         }
  302.         selection_status = get_cursor_zone(top_level->cursor_x, top_level->cursor_y);
  303.         if(selection_status == SCROLL_HANDLE)
  304.         {
  305.             double total_pixels = pixels - SCROLL_SPAN * 2;
  306.             long cursor_pixel = (orientation == SCROLL_HORIZ) ? top_level->cursor_x : top_level->cursor_y;
  307.             min_pixel = cursor_pixel - (long)((double)position / length * total_pixels + .5);
  308.             max_pixel = (int)(cursor_pixel + total_pixels);
  309.             draw();
  310.             flash();
  311.         }
  312.         else
  313.         if(selection_status)
  314.         {
  315.             top_level->set_repeat(top_level->get_resources()->scroll_repeat);
  316.             repeat_count = 0;
  317.             repeat_event(top_level->get_resources()->scroll_repeat);
  318.         }
  319.         return 1;
  320.     }
  321.     return 0;
  322. }
  323.  
  324. int BC_ScrollBar::repeat_event(long duration)
  325. {
  326.     if(duration == top_level->get_resources()->scroll_repeat && 
  327.         selection_status)
  328.     {
  329.         repeat_count++;
  330.         if(repeat_count == 2) return 0;
  331.         long new_position = position;
  332.         switch(selection_status)
  333.         {
  334.             case SCROLL_BACKPAGE:
  335.                 new_position -= handlelength;
  336.                 break;
  337.             case SCROLL_FWDPAGE:
  338.                 new_position += handlelength;
  339.                 break;
  340.             case SCROLL_BACKARROW:
  341.                 new_position -= handlelength / 10;
  342.                 break;
  343.             case SCROLL_FWDARROW:
  344.                 new_position += handlelength / 10;
  345.                 break;
  346.         }
  347.         if(new_position > length - handlelength) new_position = length - handlelength;
  348.         else
  349.         if(new_position < 0) new_position = 0;
  350.         if(new_position != position)
  351.         {
  352.             position = new_position;
  353.             draw();
  354.             flash();
  355.             handle_event();
  356.         }
  357.         return 1;
  358.     }
  359.     return 0;
  360. }
  361.  
  362. int BC_ScrollBar::button_release_event()
  363. {
  364.     if(selection_status)
  365.     {
  366.         if(selection_status != SCROLL_HANDLE)
  367.             top_level->unset_repeat(top_level->get_resources()->scroll_repeat);
  368.  
  369.         selection_status = 0;
  370.         draw();
  371.         flash();
  372.         return 1;
  373.     }
  374.     return 0;
  375. }
  376.  
  377. int BC_ScrollBar::get_cursor_zone(int cursor_x, int cursor_y)
  378. {
  379.     if(orientation == SCROLL_VERT)
  380.     {
  381.         cursor_x ^= cursor_y;
  382.         cursor_y ^= cursor_x;
  383.         cursor_x ^= cursor_y;
  384.     }
  385.  
  386.     if(cursor_x >= pixels - SCROLL_SPAN)
  387.         return SCROLL_FWDARROW;
  388.     else
  389.     if(cursor_x >= SCROLL_SPAN)
  390.     {
  391.         if(cursor_x > handle_pixel + handle_pixels)
  392.             return SCROLL_FWDPAGE;
  393.         else
  394.         if(cursor_x >= handle_pixel)
  395.             return SCROLL_HANDLE;
  396.         else
  397.             return SCROLL_BACKPAGE;
  398.     }
  399.     else
  400.         return SCROLL_BACKARROW;
  401.     
  402.     return 0;
  403. }
  404.  
  405. int BC_ScrollBar::activate()
  406. {
  407.     top_level->active_subwindow = this;
  408.     return 0;
  409. }
  410.  
  411. long BC_ScrollBar::get_value()
  412. {
  413.     return position;
  414. }
  415.  
  416. long BC_ScrollBar::get_position()
  417. {
  418.     return position;
  419. }
  420.  
  421. long BC_ScrollBar::get_length()
  422. {
  423.     return length;
  424. }
  425.  
  426. int BC_ScrollBar::in_use()
  427. {
  428.     return selection_status != 0;
  429. }
  430.  
  431. long BC_ScrollBar::get_handlelength()
  432. {
  433.     return handlelength;
  434. }
  435.  
  436. int BC_ScrollBar::update_value(long value)
  437. {
  438.     this->position = value;
  439.     draw();
  440.     return 0;
  441. }
  442.  
  443. int BC_ScrollBar::update_length(long length, long position, long handlelength)
  444. {
  445.     this->length = length;
  446.     this->position = position;
  447.     this->handlelength = handlelength;
  448.     draw();
  449.     return 0;
  450. }
  451.  
  452. int BC_ScrollBar::reposition_window(int x, int y, int w, int h)
  453. {
  454.     BC_WindowBase::reposition_window(x, y, w, h);
  455.     this->pixels = orientation == SCROLL_VERT ? h : w;
  456.     draw();
  457.     return 0;
  458. }
  459.  
  460.