r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / mainprogress.C
blobd1b1cf183b952ed46b2234621c46bd785565ee2e
1 #include "bcprogressbox.h"
2 #include "mainprogress.h"
3 #include "mwindow.h"
4 #include "mwindowgui.h"
5 #include "statusbar.h"
6 #include "timer.h"
8 #include <string.h>
10 #include <libintl.h>
11 #define _(String) gettext(String)
12 #define gettext_noop(String) String
13 #define N_(String) gettext_noop (String)
15 MainProgressBar::MainProgressBar(MWindow *mwindow, MainProgress *mainprogress)
17         progress_box = 0;
18         progress_bar = 0;
19         this->mwindow = mwindow;
20         this->mainprogress = mainprogress;
21         eta_timer = new Timer;
24 MainProgressBar::~MainProgressBar()
26         if(progress_box) delete progress_box;
28         if(mainprogress->mwindow_progress == this)
29         {
30                 mainprogress->mwindow_progress = 0;
31         }
32         else
33         {
34                 mainprogress->progress_bars.remove(this);
35         }
36         delete eta_timer;
39 void MainProgressBar::start()
41         if(progress_box)
42         {
43                 progress_box->start();
44         }
45         eta_timer->update();
46         last_eta = 0;
49 void MainProgressBar::stop_progress()
51         if(progress_box)
52         {
53                 progress_box->stop_progress();
54         }
55         else
56         if(progress_bar)
57         {
58                 progress_bar->update(0);
59                 mwindow->gui->statusbar->default_message();
60         }
63 int MainProgressBar::is_cancelled()
65         if(progress_box)
66         {
67 //printf("MainProgressBar::is_cancelled 1 %d\n", progress_box->is_cancelled());
68                 return progress_box->is_cancelled();
69         }
70         else
71         if(progress_bar)
72         {
73                 return mainprogress->cancelled;
74         }
76         return 0;
79 void MainProgressBar::update_title(char *string, int default_)
81         if(default_) strcpy(default_title, string);
82 //printf("MainProgressBar::update_title %p %p %s\n", progress_bar, progress_box, string);
84         if(progress_box)
85         {
86                 progress_box->lock_window();
87                 progress_box->update_title(string);
88                 progress_box->unlock_window();
89         }
90         else
91         if(progress_bar)
92         {
93                 mwindow->gui->lock_window();
94                 mwindow->gui->show_message(string, BLACK);
95                 mwindow->gui->unlock_window();
96         }
99 void MainProgressBar::update_length(long length)
101         this->length = length;
102 //printf("MainProgressBar::update_length %d\n", length);
103         if(progress_box)
104         {
105                 progress_box->lock_window();
106                 progress_box->update_length(length);
107                 progress_box->unlock_window();
108         }
109         else
110         if(progress_bar)
111         {
112                 mwindow->gui->lock_window();
113                 progress_bar->update_length(length);
114                 mwindow->gui->unlock_window();
115         }
118 int MainProgressBar::update(long value)
120 // Print ETA on title
121         double current_eta = (double)eta_timer->get_scaled_difference(1000);
123 //printf("MainProgressBar::update %f %f %f\n", current_eta, last_eta, current_eta - last_eta);
124         if(current_eta - last_eta > 1000)
125         {
126                 char string[BCTEXTLEN];
127                 char time_string[BCTEXTLEN];
128                 double eta;
130                 if(value > 0.001)
131                         eta = (double)current_eta / 1000 * length / value - current_eta / 1000;
132                 else
133                         eta = 0;
135 //printf("MainProgressBar::update %f %d %d %f\n", current_eta, length, value, eta);
136                 Units::totext(time_string, 
137                         eta,
138                         TIME_HMS2);
140                 sprintf(string, _("%s ETA: %s"), 
141                         default_title, 
142                         time_string);
143                 update_title(string, 0);
145                 last_eta = (long)current_eta;
146         }
148         if(progress_box)
149         {
150                 progress_box->lock_window();
151                 progress_box->update(value);
152                 progress_box->unlock_window();
153         }
154         else
155         if(progress_bar)
156         {
157                 mwindow->gui->lock_window();
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);
185 MainProgress::MainProgress(MWindow *mwindow, MWindowGUI *gui)
187         this->mwindow = mwindow;
188         this->gui = gui;
189         mwindow_progress = 0;
190         cancelled = 0;
194 MainProgress::~MainProgress()
198 MainProgressBar* MainProgress::start_progress(char *text, long total_length)
200         MainProgressBar *result = 0;
202 // Default to main window
203         if(!mwindow_progress)
204         {
205                 mwindow_progress = new MainProgressBar(mwindow, this);
206                 mwindow_progress->progress_bar = gui->statusbar->main_progress;
207                 mwindow_progress->progress_bar->update_length(total_length);
208                 mwindow_progress->update_title(text);
209                 result = mwindow_progress;
210                 cancelled = 0;
211         }
213         if(!result)
214         {
215                 result = new MainProgressBar(mwindow, this);
216                 progress_bars.append(result);
217                 result->progress_box = new BC_ProgressBox(gui->get_abs_cursor_x(), 
218                         gui->get_abs_cursor_y(), 
219                         text, 
220                         total_length);
221         }
223         result->length = total_length;
224         strcpy(result->default_title, text);
225         result->start();
226         return result;
230 void MainProgress::end_progress(MainProgressBar* progress_bar)
232         if(mwindow_progress == progress_bar)
233         {
234                 delete mwindow_progress;
235                 mwindow_progress = 0;
236         }
237         else
238         {
239                 delete progress_bar;
240                 progress_bars.remove(progress_bar);
241         }