prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / 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->statusbar->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                 mwindow->gui->lock_window("MainProgressBar::update_title");
111                 mwindow->gui->show_message(string);
112                 mwindow->gui->unlock_window();
113         }
114 }
115
116 void MainProgressBar::update_length(int64_t length)
117 {
118         this->length = length;
119 //printf("MainProgressBar::update_length %d\n", length);
120         if(progress_box)
121         {
122                 progress_box->update_length(length, 1);
123         }
124         else
125         if(progress_bar)
126         {
127                 mwindow->gui->lock_window("MainProgressBar::update_length");
128                 progress_bar->update_length(length);
129                 mwindow->gui->unlock_window();
130         }
131 }
132
133 int MainProgressBar::update(int64_t value)
134 {
135 // Print ETA on title
136         double current_eta = (double)eta_timer->get_scaled_difference(1000);
137
138 //printf("MainProgressBar::update %f %f %f\n", current_eta, last_eta, current_eta - last_eta);
139         if(current_eta - last_eta > 1000)
140         {
141                 char string[BCTEXTLEN];
142                 char time_string[BCTEXTLEN];
143                 double eta;
144
145                 if(value > 0.001)
146                         eta = (double)current_eta / 
147                                 1000 * 
148                                 length / 
149                                 value - 
150                                 current_eta / 
151                                 1000;
152                 else
153                         eta = 0;
154
155 //printf("MainProgressBar::update %f %d %d %f\n", current_eta, length, value, eta);
156                 Units::totext(time_string, 
157                         eta,
158                         TIME_HMS2);
159 //              sprintf(time_string, 
160 //                      "%dh%dm%ds", 
161 //                      (int64_t)eta / 3600,
162 //                      ((int64_t)eta / 60) % 60,
163 //                      (int64_t)eta % 60);
164
165                 sprintf(string, _("%s ETA: %s"), 
166                         default_title, 
167                         time_string);
168                 update_title(string, 0);
169
170                 last_eta = (int64_t)current_eta;
171         }
172
173         if(progress_box)
174         {
175                 progress_box->update(value, 1);
176         }
177         else
178         if(progress_bar)
179         {
180                 mwindow->gui->lock_window("MainProgressBar::update");
181                 progress_bar->update(value);
182                 mwindow->gui->unlock_window();
183         }
184
185         return is_cancelled();
186 }
187
188 void MainProgressBar::get_time(char *text)
189 {
190         double current_time = (double)eta_timer->get_scaled_difference(1);
191 //printf("MainProgressBar::get_time %f\n", current_time);
192         Units::totext(text, 
193                         current_time,
194                         TIME_HMS2);
195 }
196
197 double MainProgressBar::get_time()
198 {
199         return (double)eta_timer->get_scaled_difference(1);
200 }
201
202
203
204
205
206
207
208
209
210
211
212 MainProgress::MainProgress(MWindow *mwindow, MWindowGUI *gui)
213 {
214         this->mwindow = mwindow;
215         this->gui = gui;
216         mwindow_progress = 0;
217         cancelled = 0;
218 }
219
220
221 MainProgress::~MainProgress()
222 {
223 }
224
225 MainProgressBar* MainProgress::start_progress(char *text, 
226         int64_t total_length,
227         int use_window)
228 {
229         MainProgressBar *result = 0;
230
231 // Default to main window
232         if(!mwindow_progress && !use_window)
233         {
234                 mwindow_progress = new MainProgressBar(mwindow, this);
235                 mwindow_progress->progress_bar = gui->statusbar->main_progress;
236                 mwindow_progress->progress_bar->update_length(total_length);
237                 mwindow_progress->update_title(text);
238                 result = mwindow_progress;
239                 cancelled = 0;
240         }
241
242         if(!result)
243         {
244                 result = new MainProgressBar(mwindow, this);
245                 progress_bars.append(result);
246                 result->progress_box = new BC_ProgressBox(gui->get_abs_cursor_x(1), 
247                         gui->get_abs_cursor_y(1), 
248                         text, 
249                         total_length);
250         }
251
252         result->length = total_length;
253         strcpy(result->default_title, text);
254         result->start();
255         return result;
256 }
257
258
259 void MainProgress::end_progress(MainProgressBar* progress_bar)
260 {
261         if(mwindow_progress == progress_bar)
262         {
263                 delete mwindow_progress;
264                 mwindow_progress = 0;
265         }
266         else
267         {
268                 delete progress_bar;
269                 progress_bars.remove(progress_bar);
270         }
271 }