enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / interthread_progress_window.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <glibmm/main.h>
21 #include <gtkmm/stock.h>
22 #include "gtkmm2ext/utils.h"
23 #include "ardour/import_status.h"
24 #include "interthread_progress_window.h"
25 #include "pbd/i18n.h"
26
27 using namespace std;
28 using namespace Gtk;
29
30 /** @param i Status information.
31  *  @param t Window title.
32  *  @param c Label to use for Cancel button.
33  */
34 InterthreadProgressWindow::InterthreadProgressWindow (ARDOUR::InterThreadInfo* i, string const & t, string const & c)
35         : ArdourDialog (t, true)
36         , _interthread_info (i)
37 {
38         _bar.set_orientation (Gtk::PROGRESS_LEFT_TO_RIGHT);
39
40         set_border_width (12);
41         get_vbox()->set_spacing (6);
42
43         get_vbox()->pack_start (_bar, false, false);
44
45         Button* b = add_button (Stock::CANCEL, RESPONSE_CANCEL);
46         b->signal_clicked().connect (sigc::mem_fun (*this, &InterthreadProgressWindow::cancel_clicked));
47
48         _cancel_label.set_text (c);
49         _cancel_button.add (_cancel_label);
50
51         set_default_size (200, 100);
52         show_all ();
53         hide ();
54         _interthread_info->cancel = false; // override on_hide
55
56         Glib::signal_timeout().connect (sigc::mem_fun (*this, &InterthreadProgressWindow::update), 100);
57 }
58
59 void
60 InterthreadProgressWindow::on_hide ()
61 {
62         if (_interthread_info && !_interthread_info->done) {
63                 //catch user pressing 'esc' or WM close
64                 _interthread_info->cancel = true;
65         }
66 }
67
68 void
69 InterthreadProgressWindow::cancel_clicked ()
70 {
71         _interthread_info->cancel = true;
72 }
73
74 bool
75 InterthreadProgressWindow::update ()
76 {
77         _bar.set_fraction (_interthread_info->progress);
78         return !(_interthread_info->done || _interthread_info->cancel);
79 }
80
81 /** @param i Status information.
82  *  @param t Window title.
83  *  @param c Label to use for Cancel button.
84  */
85 ImportProgressWindow::ImportProgressWindow (ARDOUR::ImportStatus* s, string const & t, string const & c)
86         : InterthreadProgressWindow (s, t, c)
87         , _import_status (s)
88 {
89         _label.set_alignment (0, 0.5);
90         _label.set_use_markup (true);
91
92         get_vbox()->pack_start (_label, false, false);
93
94         _label.show ();
95 }
96
97 bool
98 ImportProgressWindow::update ()
99 {
100         _cancel_button.set_sensitive (!_import_status->freeze);
101         _label.set_markup ("<i>" + Gtkmm2ext::markup_escape_text (_import_status->doing_what) + "</i>");
102
103         /* use overall progress for the bar, rather than that for individual files */
104         _bar.set_fraction ((_import_status->current - 1 + _import_status->progress) / _import_status->total);
105
106         /* some of the code which sets up _import_status->current may briefly increment it too far
107            at the end of an import, so check for that to avoid a visual glitch
108         */
109         uint32_t c = _import_status->current;
110         if (c > _import_status->total) {
111                 c = _import_status->total;
112         }
113
114         _bar.set_text (string_compose (_("Importing file: %1 of %2"), c, _import_status->total));
115
116         return !(_import_status->all_done || _import_status->cancel);
117 }