add binfolder path relative filters, fix gbrp color model, vwdw timebar tweaks, title...
[goodguy/history.git] / cinelerra-5.1 / guicast / bcprogress.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <stdio.h>
23 #include <unistd.h>
24
25 #define PROGRESS_UP 0
26 #define PROGRESS_HI 1
27
28 #include "bccolors.h"
29 #include "fonts.h"
30 #include "bcprogress.h"
31 #include "bcpixmap.h"
32 #include "bcresources.h"
33 #include "vframe.h"
34
35 BC_ProgressBar::BC_ProgressBar(int x, int y, int w, int64_t length, int do_text)
36  : BC_SubWindow(x, y, w, 0, -1)
37 {
38         this->length = length;
39         this->do_text = do_text;
40         position = 0;
41         pixel = 0;
42         for(int i = 0; i < 2; i++) images[i] = 0;
43         do_text = 1;
44 }
45
46 BC_ProgressBar::~BC_ProgressBar()
47 {
48     for(int i = 0; i < 2; i++)
49             if (images[i]) delete images[i];
50 }
51
52 int BC_ProgressBar::calculate_h()
53 {
54         return BC_WindowBase::get_resources()->
55                 progress_images[PROGRESS_UP]->get_h();
56 }
57
58
59 int BC_ProgressBar::initialize()
60 {
61         set_images();
62         h = calculate_h();
63
64         BC_SubWindow::initialize();
65         draw(1, 0);
66         return 0;
67 }
68
69 int BC_ProgressBar::reposition_window(int x, int y, int w, int h)
70 {
71         if(w < 0) w = get_w();
72         if(h < 0) h = get_h();
73         BC_WindowBase::reposition_window(x, y, w, h);
74         draw(1, 0);
75         return 0;
76 }
77
78 void BC_ProgressBar::set_do_text(int value)
79 {
80         this->do_text = value;
81 }
82
83 int BC_ProgressBar::set_images()
84 {
85         for(int i = 0; i < 2; i++)
86                 if(images[i]) delete images[i];
87
88         for(int i = 0; i < 2; i++)
89         {
90                 images[i] = new BC_Pixmap(parent_window,
91                         get_resources()->progress_images[i],
92                         PIXMAP_ALPHA);
93         }
94         return 0;
95 }
96
97
98 int BC_ProgressBar::draw(int force, int flush)
99 {
100         char string[32];
101         int new_pixel;
102
103         new_pixel = (int)(((float)position / length) * get_w());
104
105         if(new_pixel != pixel || force)
106         {
107                 pixel = new_pixel;
108 // Clear background
109                 draw_top_background(parent_window, 0, 0, get_w(), get_h());
110                 draw_3segmenth(0, 0, pixel, 0, get_w(), images[PROGRESS_HI]);
111                 draw_3segmenth(pixel, 0, get_w() - pixel, 0, get_w(), images[PROGRESS_UP]);
112
113
114                 if(do_text)
115                 {
116                         float pos = position > length ? (float)1 :
117                                 length > 0 && position > 0 ? (float)position / length :
118                                 (float)0;
119                         set_font(MEDIUMFONT);
120                         set_color(get_resources()->progress_text);     // draw decimal percentage
121                         sprintf(string, "%d%%", (int)(100 * pos  + 0.5f));
122                         draw_center_text(w / 2, h / 2 + get_text_ascent(MEDIUMFONT) / 2, string);
123                 }
124                 flash(flush);
125         }
126         return 0;
127 }
128
129 int BC_ProgressBar::update(int64_t position)
130 {
131         this->position = position;
132         draw(0, 1);
133         return 0;
134 }
135
136 int BC_ProgressBar::update_length(int64_t length)
137 {
138         this->length = length;
139         position = 0;
140
141         draw(0, 1);
142         return 0;
143 }
144