license header for review
[cinelerra_cv/ct.git] / cinelerra / mainprogress.C
blob5cac5f58ad2bbf6c3d2a759931929503ad007096
1 #include "bcprogressbox.h"
2 #include "language.h"
3 #include "mainprogress.h"
4 #include "mwindow.h"
5 #include "mwindowgui.h"
6 #include "statusbar.h"
7 #include "bctimer.h"
9 #include <string.h>
11 MainProgressBar::MainProgressBar(MWindow *mwindow, MainProgress *mainprogress)
13         progress_box = 0;
14         progress_bar = 0;
15         this->mwindow = mwindow;
16         this->mainprogress = mainprogress;
17         eta_timer = new Timer;
20 MainProgressBar::~MainProgressBar()
22         if(progress_box) delete progress_box;
24         if(mainprogress->mwindow_progress == this)
25         {
26                 mainprogress->mwindow_progress = 0;
27         }
28         else
29         {
30                 mainprogress->progress_bars.remove(this);
31         }
32         delete eta_timer;
35 void MainProgressBar::start()
37         if(progress_box)
38         {
39                 progress_box->start();
40         }
41         eta_timer->update();
42         last_eta = 0;
45 void MainProgressBar::stop_progress()
47         if(progress_box)
48         {
49                 progress_box->stop_progress();
50         }
51         else
52         if(progress_bar)
53         {
54                 progress_bar->update(0);
55                 mwindow->gui->statusbar->default_message();
56         }
59 int MainProgressBar::is_cancelled()
61         if(progress_box)
62         {
63 //printf("MainProgressBar::is_cancelled 1 %d\n", progress_box->is_cancelled());
64                 return progress_box->is_cancelled();
65         }
66         else
67         if(progress_bar)
68         {
69                 return mainprogress->cancelled;
70         }
72         return 0;
75 void MainProgressBar::update_title(char *string, int default_)
77         if(default_) strcpy(default_title, string);
78 //printf("MainProgressBar::update_title %p %p %s\n", progress_bar, progress_box, string);
80         if(progress_box)
81         {
82                 progress_box->update_title(string, 1);
83         }
84         else
85         if(progress_bar)
86         {
87                 mwindow->gui->lock_window("MainProgressBar::update_title");
88                 mwindow->gui->show_message(string);
89                 mwindow->gui->unlock_window();
90         }
93 void MainProgressBar::update_length(int64_t length)
95         this->length = length;
96 //printf("MainProgressBar::update_length %d\n", length);
97         if(progress_box)
98         {
99                 progress_box->update_length(length, 1);
100         }
101         else
102         if(progress_bar)
103         {
104                 mwindow->gui->lock_window("MainProgressBar::update_length");
105                 progress_bar->update_length(length);
106                 mwindow->gui->unlock_window();
107         }
110 int MainProgressBar::update(int64_t value)
112 // Print ETA on title
113         double current_eta = (double)eta_timer->get_scaled_difference(1000);
115 //printf("MainProgressBar::update %f %f %f\n", current_eta, last_eta, current_eta - last_eta);
116         if(current_eta - last_eta > 1000)
117         {
118                 char string[BCTEXTLEN];
119                 char time_string[BCTEXTLEN];
120                 double eta;
122                 if(value > 0.001)
123                         eta = (double)current_eta / 
124                                 1000 * 
125                                 length / 
126                                 value - 
127                                 current_eta / 
128                                 1000;
129                 else
130                         eta = 0;
132 //printf("MainProgressBar::update %f %d %d %f\n", current_eta, length, value, eta);
133                 Units::totext(time_string, 
134                         eta,
135                         TIME_HMS2);
136 //              sprintf(time_string, 
137 //                      "%dh%dm%ds", 
138 //                      (int64_t)eta / 3600,
139 //                      ((int64_t)eta / 60) % 60,
140 //                      (int64_t)eta % 60);
142                 sprintf(string, _("%s ETA: %s"), 
143                         default_title, 
144                         time_string);
145                 update_title(string, 0);
147                 last_eta = (int64_t)current_eta;
148         }
150         if(progress_box)
151         {
152                 progress_box->update(value, 1);
153         }
154         else
155         if(progress_bar)
156         {
157                 mwindow->gui->lock_window("MainProgressBar::update");
158                 progress_bar->update(value);
159                 mwindow->gui->unlock_window();
160         }
162         return is_cancelled();
165 void MainProgressBar::get_time(char *text)
167         double current_time = (double)eta_timer->get_scaled_difference(1);
168 //printf("MainProgressBar::get_time %f\n", current_time);
169         Units::totext(text, 
170                         current_time,
171                         TIME_HMS2);
174 double MainProgressBar::get_time()
176         return (double)eta_timer->get_scaled_difference(1);
189 MainProgress::MainProgress(MWindow *mwindow, MWindowGUI *gui)
191         this->mwindow = mwindow;
192         this->gui = gui;
193         mwindow_progress = 0;
194         cancelled = 0;
198 MainProgress::~MainProgress()
202 MainProgressBar* MainProgress::start_progress(char *text, 
203         int64_t total_length,
204         int use_window)
206         MainProgressBar *result = 0;
208 // Default to main window
209         if(!mwindow_progress && !use_window)
210         {
211                 mwindow_progress = new MainProgressBar(mwindow, this);
212                 mwindow_progress->progress_bar = gui->statusbar->main_progress;
213                 mwindow_progress->progress_bar->update_length(total_length);
214                 mwindow_progress->update_title(text);
215                 result = mwindow_progress;
216                 cancelled = 0;
217         }
219         if(!result)
220         {
221                 result = new MainProgressBar(mwindow, this);
222                 progress_bars.append(result);
223                 result->progress_box = new BC_ProgressBox(gui->get_abs_cursor_x(1), 
224                         gui->get_abs_cursor_y(1), 
225                         text, 
226                         total_length);
227         }
229         result->length = total_length;
230         strcpy(result->default_title, text);
231         result->start();
232         return result;
236 void MainProgress::end_progress(MainProgressBar* progress_bar)
238         if(mwindow_progress == progress_bar)
239         {
240                 delete mwindow_progress;
241                 mwindow_progress = 0;
242         }
243         else
244         {
245                 delete progress_bar;
246                 progress_bars.remove(progress_bar);
247         }
250 //      Local Variables:
251 //      mode: C++
252 //      c-file-style: "linux"
253 //      End: