initial commit
[goodguy/history.git] / cinelerra-5.0 / guicast / bcprogressbox.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 "bcbutton.h"
23 #include "bcdisplayinfo.h"
24 #include "bcprogress.h"
25 #include "bcprogressbox.h"
26 #include "bcresources.h"
27 #include "bctitle.h"
28 #include "bcwindow.h"
29 #include "vframe.h"
30
31 BC_ProgressBox::BC_ProgressBox(int x, int y, const char *text, int64_t length)
32  : Thread(1, 0, 0)
33 {
34 // Calculate default x, y
35         if(x < 0 || y < 0)
36         {
37                 BC_DisplayInfo display_info;
38                 x = display_info.get_abs_cursor_x();
39                 y = display_info.get_abs_cursor_y();
40                 // preloads boarder data (if not already loaded)
41                 //  without reopening display, also avoids a bug in X
42                 display_info.get_top_border();
43         }
44
45         pwindow = new BC_ProgressWindow(x, y);
46         pwindow->create_objects(text, length);
47         cancelled = 0;
48 }
49
50 BC_ProgressBox::~BC_ProgressBox()
51 {
52         delete pwindow;
53 }
54
55 void BC_ProgressBox::run()
56 {
57         int result = pwindow->run_window();
58         if(result) cancelled = 1;
59 }
60
61 int BC_ProgressBox::update(int64_t position, int lock_it)
62 {
63         if(!cancelled)
64         {
65                 if(lock_it) pwindow->lock_window("BC_ProgressBox::update");
66                 pwindow->bar->update(position);
67                 if(lock_it) pwindow->unlock_window();
68         }
69         return cancelled;
70 }
71
72 int BC_ProgressBox::update_title(const char *title, int lock_it)
73 {
74         if(lock_it) pwindow->lock_window("BC_ProgressBox::update_title");
75         pwindow->caption->update(title);
76         if(lock_it) pwindow->unlock_window();
77         return cancelled;
78 }
79
80 int BC_ProgressBox::update_length(int64_t length, int lock_it)
81 {
82         if(lock_it) pwindow->lock_window("BC_ProgressBox::update_length");
83         pwindow->bar->update_length(length);
84         if(lock_it) pwindow->unlock_window();
85         return cancelled;
86 }
87
88
89 int BC_ProgressBox::is_cancelled()
90 {
91         return cancelled;
92 }
93
94 int BC_ProgressBox::stop_progress()
95 {
96         pwindow->set_done(0);
97         Thread::join();
98         return 0;
99 }
100
101 void BC_ProgressBox::lock_window()
102 {
103         pwindow->lock_window("BC_ProgressBox::lock_window");
104 }
105
106 void BC_ProgressBox::unlock_window()
107 {
108         pwindow->unlock_window();
109 }
110
111
112
113 BC_ProgressWindow::BC_ProgressWindow(int x, int y)
114  : BC_Window("Progress",
115         x,
116         y,
117         340,
118         100 + get_resources()->ok_images[0]->get_h(),
119         0,
120         0,
121         0)
122 {
123 }
124
125 BC_ProgressWindow::~BC_ProgressWindow()
126 {
127 }
128
129 int BC_ProgressWindow::create_objects(const char *text, int64_t length)
130 {
131         int x = 10, y = 10;
132
133 // Recalculate width based on text
134         if(text)
135         {
136                 int text_w = get_text_width(MEDIUMFONT, text);
137                 int new_w = text_w + x + 10, scr_w = get_screen_w(0, -1);
138                 if(new_w > scr_w) new_w = scr_w;
139                 if(new_w > get_w()) {
140                         resize_window(new_w, get_h());
141                 }
142         }
143
144         this->text = text;
145         add_tool(caption = new BC_Title(x, y, text));
146         y += caption->get_h() + 20;
147         add_tool(bar = new BC_ProgressBar(x, y, get_w() - 20, length));
148         add_tool(new BC_CancelButton(this));
149         show_window(1);
150
151         return 0;
152 }
153
154
155
156