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 / bcprogress.C < prev    next >
C/C++ Source or Header  |  2000-11-29  |  3KB  |  140 lines

  1. #include <stdio.h>
  2.  
  3.  
  4. #define PROGRESS_LEFT 0
  5. #define PROGRESS_MID 1
  6. #define PROGRESS_RIGHT 2
  7. #define PROGRESS_LEFT_HI 3
  8. #define PROGRESS_MID_HI 4
  9. #define PROGRESS_RIGHT_HI 5
  10. #define PROGRESS_STATES 6
  11.  
  12. #include "colors.h"
  13. #include "fonts.h"
  14. #include "bcprogress.h"
  15. #include "bcpixmap.h"
  16. #include "bcresources.h"
  17.  
  18. BC_ProgressBar::BC_ProgressBar(int x, int y, int w, long length)
  19.  : BC_SubWindow(x, y, w, 0, -1)
  20. {
  21.     this->length = length;
  22.     position = 0;
  23.     pixel = 0;
  24.     for(int i = 0; i < PROGRESS_STATES; i++) images[i] = 0;
  25. }
  26.  
  27. int BC_ProgressBar::initialize()
  28. {
  29.     set_images();
  30.     h = images[PROGRESS_MID]->get_h();
  31.  
  32.     BC_SubWindow::initialize();
  33.     draw(1);
  34.     return 0;
  35. }
  36.  
  37. int BC_ProgressBar::set_images()
  38. {
  39.     for(int i = 0; i < PROGRESS_STATES; i++)
  40.         if(images[i]) delete images[i];
  41.  
  42.     for(int i = 0; i < PROGRESS_STATES; i++)
  43.         images[i] = new BC_Pixmap(parent_window, get_resources()->progress_images[i], PIXMAP_ALPHA);
  44.     return 0;
  45. }
  46.  
  47.  
  48. int BC_ProgressBar::draw(int force)
  49. {
  50.     char string[32];
  51.     int new_pixel;
  52.     int left_division = images[PROGRESS_LEFT]->get_w_fixed();
  53.     int right_division = get_w() - images[PROGRESS_RIGHT]->get_w_fixed();
  54.  
  55.     new_pixel = (int)(((float)position / length) * get_w());
  56.  
  57.     if(new_pixel != pixel || force)
  58.     {
  59.         pixel = new_pixel;
  60. // Clear background
  61.         draw_top_background(parent_window, 0, 0, get_w(), get_h());
  62.  
  63.         for(int x = 0; x < get_w(); )
  64.         {
  65.             int image_number;
  66.             int source_x, source_w;
  67.  
  68.             if(x < left_division) 
  69.             {
  70.                 image_number = PROGRESS_LEFT;
  71.                 source_x = x;
  72.                 source_w = left_division - source_x;
  73.             }
  74.             else
  75.             if(x >= right_division) 
  76.             {
  77.                 image_number = PROGRESS_RIGHT;
  78.                 source_x = x - right_division;
  79.                 source_w = get_w() - x;
  80.             }
  81.             else
  82.             {
  83.                 image_number = PROGRESS_MID;
  84.                 source_x = 0;
  85.                 source_w = right_division - x;
  86.                 if(source_w > images[PROGRESS_MID]->get_w_fixed())
  87.                     source_w = images[PROGRESS_MID]->get_w_fixed();
  88.             }
  89.  
  90. // Completely in highlighted region
  91.             if(pixel >= x + source_w)
  92.             {
  93.                 image_number += 3;
  94.             }
  95.             else
  96. // Partially in highlighted region
  97.             if(pixel > x)
  98.             {
  99.                 image_number += 3;
  100.                 if(pixel - x < source_w)
  101.                     source_w = pixel - x;
  102.             }
  103.  
  104.             images[image_number]->write_drawable(pixmap, 
  105.                 x, 
  106.                 0, 
  107.                 source_w + 1, 
  108.                 images[image_number]->get_h(),
  109.                 source_x,
  110.                 0);
  111.  
  112.             x += source_w;
  113.         }
  114.  
  115.         set_font(MEDIUMFONT);
  116.         set_color(BLACK);     // draw decimal percentage
  117.         sprintf(string, "%d%%", (int)(100 * (float)position / length + 0.5 / w));
  118.         draw_center_text(w / 2, h / 2 + get_text_ascent(MEDIUMFONT) / 2, string);
  119.         flash();
  120.     }
  121.     return 0;
  122. }
  123.  
  124. int BC_ProgressBar::update(long position)
  125. {
  126.     this->position = position;
  127.     draw();
  128.     return 0;
  129. }
  130.  
  131. int BC_ProgressBar::update_length(long length)
  132. {
  133.     this->length = length;
  134.     position = 0;
  135.  
  136.     draw();
  137.     return 0;
  138. }
  139.  
  140.