4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcprogressbox.h"
24 #include "mainprogress.h"
26 #include "mwindowgui.h"
27 #include "statusbar.h"
32 MainProgressBar::MainProgressBar(MWindow *mwindow, MainProgress *mainprogress)
36 this->mwindow = mwindow;
37 this->mainprogress = mainprogress;
38 eta_timer = new Timer;
41 MainProgressBar::~MainProgressBar()
43 if(progress_box) delete progress_box;
45 if(mainprogress->mwindow_progress == this)
47 mainprogress->mwindow_progress = 0;
51 mainprogress->progress_bars.remove(this);
56 void MainProgressBar::start()
60 progress_box->start();
66 void MainProgressBar::stop_progress()
70 progress_box->stop_progress();
75 mwindow->gui->lock_window("MainProgressBar::stop_progress");
76 progress_bar->update(0);
77 mwindow->gui->statusbar->default_message();
78 mwindow->gui->unlock_window();
82 int MainProgressBar::is_cancelled()
86 //printf("MainProgressBar::is_cancelled 1 %d\n", progress_box->is_cancelled());
87 return progress_box->is_cancelled();
92 return mainprogress->cancelled;
98 void MainProgressBar::update_title(char *string, int default_)
100 if(default_) strcpy(default_title, string);
101 //printf("MainProgressBar::update_title %p %p %s\n", progress_bar, progress_box, string);
105 progress_box->update_title(string, 1);
110 mwindow->gui->lock_window("MainProgressBar::update_title");
111 mwindow->gui->show_message(string);
112 mwindow->gui->unlock_window();
116 void MainProgressBar::update_length(int64_t length)
118 this->length = length;
119 //printf("MainProgressBar::update_length %d\n", length);
122 progress_box->update_length(length, 1);
127 mwindow->gui->lock_window("MainProgressBar::update_length");
128 progress_bar->update_length(length);
129 mwindow->gui->unlock_window();
133 int MainProgressBar::update(int64_t value)
135 // Print ETA on title
136 double current_eta = (double)eta_timer->get_scaled_difference(1000);
138 //printf("MainProgressBar::update %f %f %f\n", current_eta, last_eta, current_eta - last_eta);
139 if(current_eta - last_eta > 1000)
141 char string[BCTEXTLEN];
142 char time_string[BCTEXTLEN];
146 eta = (double)current_eta /
155 //printf("MainProgressBar::update %f %d %d %f\n", current_eta, length, value, eta);
156 Units::totext(time_string,
159 // sprintf(time_string,
161 // (int64_t)eta / 3600,
162 // ((int64_t)eta / 60) % 60,
163 // (int64_t)eta % 60);
165 sprintf(string, _("%s ETA: %s"),
168 update_title(string, 0);
170 last_eta = (int64_t)current_eta;
175 progress_box->update(value, 1);
180 mwindow->gui->lock_window("MainProgressBar::update");
181 progress_bar->update(value);
182 mwindow->gui->unlock_window();
185 return is_cancelled();
188 void MainProgressBar::get_time(char *text)
190 double current_time = (double)eta_timer->get_scaled_difference(1);
191 //printf("MainProgressBar::get_time %f\n", current_time);
197 double MainProgressBar::get_time()
199 return (double)eta_timer->get_scaled_difference(1);
212 MainProgress::MainProgress(MWindow *mwindow, MWindowGUI *gui)
214 this->mwindow = mwindow;
216 mwindow_progress = 0;
221 MainProgress::~MainProgress()
225 MainProgressBar* MainProgress::start_progress(char *text,
226 int64_t total_length,
229 MainProgressBar *result = 0;
231 // Default to main window
232 if(!mwindow_progress && !use_window)
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;
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),
252 result->length = total_length;
253 strcpy(result->default_title, text);
259 void MainProgress::end_progress(MainProgressBar* progress_bar)
261 if(mwindow_progress == progress_bar)
263 delete mwindow_progress;
264 mwindow_progress = 0;
269 progress_bars.remove(progress_bar);