00001 #include <stdio.h>
00002 #include <unistd.h>
00003
00004 #define PROGRESS_UP 0
00005 #define PROGRESS_HI 1
00006
00007 #include "colors.h"
00008 #include "fonts.h"
00009 #include "bcprogress.h"
00010 #include "bcpixmap.h"
00011 #include "bcresources.h"
00012
00013 BC_ProgressBar::BC_ProgressBar(int x, int y, int w, int64_t length, int do_text)
00014 : BC_SubWindow(x, y, w, 0, -1)
00015 {
00016 this->length = length;
00017 this->do_text = do_text;
00018 position = 0;
00019 pixel = 0;
00020 for(int i = 0; i < 2; i++) images[i] = 0;
00021 do_text = 1;
00022 }
00023
00024 BC_ProgressBar::~BC_ProgressBar()
00025 {
00026 for(int i = 0; i < 2; i++)
00027 if (images[i]) delete images[i];
00028 }
00029
00030 int BC_ProgressBar::initialize()
00031 {
00032 set_images();
00033 h = images[PROGRESS_UP]->get_h();
00034
00035 BC_SubWindow::initialize();
00036 draw(1);
00037 return 0;
00038 }
00039
00040 int BC_ProgressBar::reposition_window(int x, int y, int w, int h)
00041 {
00042 if(w < 0) w = get_w();
00043 if(h < 0) h = get_h();
00044 BC_WindowBase::reposition_window(x, y, w, h);
00045 draw(1);
00046 }
00047
00048 void BC_ProgressBar::set_do_text(int value)
00049 {
00050 this->do_text = value;
00051 }
00052
00053 int BC_ProgressBar::set_images()
00054 {
00055 for(int i = 0; i < 2; i++)
00056 if(images[i]) delete images[i];
00057
00058 for(int i = 0; i < 2; i++)
00059 {
00060 images[i] = new BC_Pixmap(parent_window,
00061 get_resources()->progress_images[i],
00062 PIXMAP_ALPHA);
00063 }
00064 return 0;
00065 }
00066
00067
00068 int BC_ProgressBar::draw(int force)
00069 {
00070 char string[32];
00071 int new_pixel;
00072
00073 new_pixel = (int)(((float)position / length) * get_w());
00074
00075 if(new_pixel != pixel || force)
00076 {
00077 pixel = new_pixel;
00078
00079 draw_top_background(parent_window, 0, 0, get_w(), get_h());
00080 draw_3segmenth(0, 0, pixel, 0, get_w(), images[PROGRESS_HI]);
00081 draw_3segmenth(pixel, 0, get_w() - pixel, 0, get_w(), images[PROGRESS_UP]);
00082
00083
00084 if(do_text)
00085 {
00086 set_font(MEDIUMFONT);
00087 set_color(get_resources()->progress_text);
00088 sprintf(string, "%d%%", (int)(100 * (float)position / length + 0.5 / w));
00089 draw_center_text(w / 2, h / 2 + get_text_ascent(MEDIUMFONT) / 2, string);
00090 }
00091 flash();
00092 }
00093 return 0;
00094 }
00095
00096 int BC_ProgressBar::update(int64_t position)
00097 {
00098 this->position = position;
00099 draw();
00100 return 0;
00101 }
00102
00103 int BC_ProgressBar::update_length(int64_t length)
00104 {
00105 this->length = length;
00106 position = 0;
00107
00108 draw();
00109 return 0;
00110 }
00111