add missing GPL information in guicast program files
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / mainprogress.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 "bcprogressbox.h"
23 #include "language.h"
24 #include "mainprogress.h"
25 #include "mwindow.h"
26 #include "mwindowgui.h"
27 #include "statusbar.h"
28 #include "bctimer.h"
29
30 #include <string.h>
31
32 MainProgressBar::MainProgressBar(MWindow *mwindow, MainProgress *mainprogress)
33 {
34         progress_box = 0;
35         progress_bar = 0;
36         this->mwindow = mwindow;
37         this->mainprogress = mainprogress;
38         eta_timer = new Timer;
39 }
40
41 MainProgressBar::~MainProgressBar()
42 {
43         if(progress_box) delete progress_box;
44
45         if(mainprogress->mwindow_progress == this)
46         {
47                 mainprogress->mwindow_progress = 0;
48         }
49         else
50         {
51                 mainprogress->progress_bars.remove(this);
52         }
53         delete eta_timer;
54 }
55
56 void MainProgressBar::start()
57 {
58         if(progress_box)
59         {
60                 progress_box->start();
61         }
62         eta_timer->update();
63         last_eta = 0;
64 }
65
66 void MainProgressBar::stop_progress()
67 {
68         if(progress_box)
69         {
70                 progress_box->stop_progress();
71         }
72         else
73         if(progress_bar)
74         {
75                 mwindow->gui->lock_window("MainProgressBar::stop_progress");
76                 progress_bar->update(0);
77                 mwindow->gui->default_message();
78                 mwindow->gui->unlock_window();
79         }
80 }
81
82 int MainProgressBar::is_cancelled()
83 {
84         if(progress_box)
85         {
86 //printf("MainProgressBar::is_cancelled 1 %d\n", progress_box->is_cancelled());
87                 return progress_box->is_cancelled();
88         }
89         else
90         if(progress_bar)
91         {
92                 return mainprogress->cancelled;
93         }
94
95         return 0;
96 }
97
98 void MainProgressBar::update_title(char *string, int default_)
99 {
100         if(default_) strcpy(default_title, string);
101 //printf("MainProgressBar::update_title %p %p %s\n", progress_bar, progress_box, string);
102
103         if(progress_box)
104         {
105                 progress_box->update_title(string, 1);
106         }
107         else
108         if(progress_bar)
109         {
110                 char text[BCTEXTLEN], *cp = text, *ep = cp+sizeof(text)-1;
111                 for( const unsigned char *bp = (const unsigned char *)string; *bp && cp<ep; ++bp )
112                         *cp++ = *bp >= ' ' ? *bp : ' ';
113                 *cp = 0;
114                 mwindow->gui->lock_window("MainProgressBar::update_title");
115                 mwindow->gui->show_message(text);
116                 mwindow->gui->unlock_window();
117         }
118 }
119
120 void MainProgressBar::update_length(int64_t length)
121 {
122         this->length = length;
123 //printf("MainProgressBar::update_length %d\n", length);
124         if(progress_box)
125         {
126                 progress_box->update_length(length, 1);
127         }
128         else
129         if(progress_bar)
130         {
131                 mwindow->gui->lock_window("MainProgressBar::update_length");
132                 progress_bar->update_length(length);
133                 mwindow->gui->unlock_window();
134         }
135 }
136
137 int MainProgressBar::update(int64_t value)
138 {
139 // Print ETA on title
140         double current_eta = (double)eta_timer->get_scaled_difference(1000);
141
142 //printf("MainProgressBar::update %f %f %f\n", current_eta, last_eta, current_eta - last_eta);
143         if(current_eta - last_eta > 1000)
144         {
145                 char string[BCTEXTLEN];
146                 char time_string[BCTEXTLEN];
147                 double eta;
148
149                 if(value > 0.001)
150                         eta = (double)current_eta /
151                                 1000 *
152                                 length /
153                                 value -
154                                 current_eta /
155                                 1000;
156                 else
157                         eta = 0;
158
159 //printf("MainProgressBar::update %f %d %d %f\n", current_eta, length, value, eta);
160                 Units::totext(time_string,
161                         eta,
162                         TIME_HMS2);
163 //              sprintf(time_string,
164 //                      "%dh%dm%ds",
165 //                      (int64_t)eta / 3600,
166 //                      ((int64_t)eta / 60) % 60,
167 //                      (int64_t)eta % 60);
168
169                 sprintf(string, _("%s ETA: %s"),
170                         default_title,
171                         time_string);
172                 update_title(string, 0);
173
174                 last_eta = (int64_t)current_eta;
175         }
176
177         if(progress_box)
178         {
179                 progress_box->update(value, 1);
180         }
181         else
182         if(progress_bar)
183         {
184                 mwindow->gui->lock_window("MainProgressBar::update");
185                 progress_bar->update(value);
186                 mwindow->gui->unlock_window();
187         }
188
189         return is_cancelled();
190 }
191
192 void MainProgressBar::get_time(char *text)
193 {
194         double current_time = (double)eta_timer->get_scaled_difference(1);
195 //printf("MainProgressBar::get_time %f\n", current_time);
196         Units::totext(text,
197                         current_time,
198                         TIME_HMS2);
199 }
200
201 double MainProgressBar::get_time()
202 {
203         return (double)eta_timer->get_scaled_difference(1);
204 }
205
206
207
208
209
210
211
212
213
214
215
216 MainProgress::MainProgress(MWindow *mwindow, MWindowGUI *gui)
217 {
218         this->mwindow = mwindow;
219         this->gui = gui;
220         mwindow_progress = 0;
221         cancelled = 0;
222 }
223
224
225 MainProgress::~MainProgress()
226 {
227 }
228
229 MainProgressBar* MainProgress::start_progress(char *text,
230         int64_t total_length,
231         int use_window)
232 {
233         MainProgressBar *result = 0;
234
235 // Default to main window
236         if(!mwindow_progress && !use_window)
237         {
238                 mwindow_progress = new MainProgressBar(mwindow, this);
239                 mwindow_progress->progress_bar = gui->statusbar->main_progress;
240                 mwindow_progress->progress_bar->update_length(total_length);
241                 mwindow_progress->update_title(text);
242                 result = mwindow_progress;
243                 cancelled = 0;
244         }
245
246         if(!result)
247         {
248                 result = new MainProgressBar(mwindow, this);
249                 progress_bars.append(result);
250                 result->progress_box = new BC_ProgressBox(gui->get_abs_cursor_x(1),
251                         gui->get_abs_cursor_y(1),
252                         text,
253                         total_length);
254         }
255
256         result->length = total_length;
257         strcpy(result->default_title, text);
258         result->start();
259         return result;
260 }
261
262
263 void MainProgress::end_progress(MainProgressBar* progress_bar)
264 {
265         if(mwindow_progress == progress_bar)
266         {
267                 delete mwindow_progress;
268                 mwindow_progress = 0;
269         }
270         else
271         {
272                 delete progress_bar;
273                 progress_bars.remove(progress_bar);
274         }
275 }