Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.git] / cinelerra-5.1 / cinelerra / loadfile.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 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 "assets.h"
23 #include "bcsignals.h"
24 #include "bchash.h"
25 #include "edl.h"
26 #include "errorbox.h"
27 #include "file.h"
28 #include "filesystem.h"
29 #include "indexfile.h"
30 #include "language.h"
31 #include "loadfile.h"
32 #include "loadmode.h"
33 #include "localsession.h"
34 #include "mainmenu.h"
35 #include "mainundo.h"
36 #include "mainsession.h"
37 #include "mwindow.h"
38 #include "mwindowgui.h"
39 #include "theme.h"
40
41
42
43 #include <string.h>
44
45 Load::Load(MWindow *mwindow, MainMenu *mainmenu)
46  : BC_MenuItem(_("Load files..."), "o", 'o')
47 {
48         this->mwindow = mwindow;
49         this->mainmenu = mainmenu;
50 }
51
52 Load::~Load()
53 {
54         delete thread;
55 }
56
57 void Load::create_objects()
58 {
59         thread = new LoadFileThread(mwindow, this);
60 }
61
62 int Load::handle_event()
63 {
64         mwindow->gui->unlock_window();
65         thread->start();
66         mwindow->gui->lock_window("Load::handle_event");
67         return 1;
68 }
69
70
71
72
73
74
75 LoadFileThread::LoadFileThread(MWindow *mwindow, Load *load)
76  : BC_DialogThread()
77 {
78         this->mwindow = mwindow;
79         this->load = load;
80 }
81
82 LoadFileThread::~LoadFileThread()
83 {
84         close_window();
85 }
86
87 BC_Window* LoadFileThread::new_gui()
88 {
89         char default_path[BCTEXTLEN];
90
91         sprintf(default_path, "~");
92         mwindow->defaults->get("DEFAULT_LOADPATH", default_path);
93         load_mode = mwindow->defaults->get("LOAD_MODE", LOADMODE_REPLACE);
94
95         mwindow->gui->lock_window("LoadFileThread::new_gui");
96         window = new LoadFileWindow(mwindow, this, default_path);
97         mwindow->gui->unlock_window();
98
99         window->create_objects();
100         return window;
101 }
102
103 void LoadFileThread::handle_done_event(int result)
104 {
105         ArrayList<char*> path_list;
106         path_list.set_array_delete();
107  
108 // Collect all selected files
109         if(!result)
110         {
111                 char *in_path, *out_path;
112                 int i = 0;
113                 window->lock_window("LoadFileThread::handle_done_event");
114                 window->hide_window();
115                 window->unlock_window();
116
117                 while((in_path = window->get_path(i)))
118                 {
119                         int j;
120                         for(j = 0; j < path_list.total; j++)
121                         {
122                                 if(!strcmp(in_path, path_list.values[j])) break;
123                         }
124
125                         if(j == path_list.total)
126                         {
127                                 path_list.append(out_path = new char[strlen(in_path) + 1]);
128                                 strcpy(out_path, in_path);
129                         }
130                         i++;
131                 }
132         }
133
134         mwindow->defaults->update("DEFAULT_LOADPATH",
135                 window->get_submitted_path());
136         mwindow->defaults->update("LOAD_MODE",
137                 load_mode);
138
139 // No file selected
140         if(path_list.total == 0 || result == 1)
141         {
142                 return;
143         }
144
145         if( load_mode == LOADMODE_REPLACE )
146                 mwindow->set_filename(path_list[0]);
147
148         mwindow->interrupt_indexes();
149         mwindow->gui->lock_window("LoadFileThread::run");
150         result = mwindow->load_filenames(&path_list, load_mode, 0);
151         mwindow->gui->mainmenu->add_load(path_list.values[0]);
152         mwindow->gui->unlock_window();
153         path_list.remove_all_objects();
154
155         mwindow->save_backup();
156
157         mwindow->restart_brender();
158
159         if(load_mode == LOADMODE_REPLACE || load_mode == LOADMODE_REPLACE_CONCATENATE)
160                 mwindow->session->changes_made = 0;
161         else
162                 mwindow->session->changes_made = 1;
163 }
164
165
166
167 LoadFileWindow::LoadFileWindow(MWindow *mwindow,
168         LoadFileThread *thread,
169         char *init_directory)
170  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
171                 mwindow->gui->get_abs_cursor_y(1) -
172                         BC_WindowBase::get_resources()->filebox_h / 2,
173                 init_directory,
174                 _(PROGRAM_NAME ": Load"),
175                 _("Select files to load:"),
176                 0,
177                 0,
178                 1,
179                 mwindow->theme->loadfile_pad)
180 {
181         this->thread = thread;
182         this->mwindow = mwindow;
183 }
184
185 LoadFileWindow::~LoadFileWindow()
186 {
187         lock_window("LoadFileWindow::~LoadFileWindow");
188         delete loadmode;
189         unlock_window();
190 }
191
192 void LoadFileWindow::create_objects()
193 {
194         lock_window("LoadFileWindow::create_objects");
195         BC_FileBox::create_objects();
196
197         BC_FileBox::create_objects();
198
199         int x = get_w() / 2 -
200                 LoadMode::calculate_w(this, mwindow->theme) / 2;
201         int y = get_cancel_button()->get_y() -
202                 LoadMode::calculate_h(this, mwindow->theme);
203         loadmode = new LoadMode(mwindow, this, x, y, &thread->load_mode, 0);
204         loadmode->create_objects();
205
206         show_window(1);
207         unlock_window();
208
209 }
210
211 int LoadFileWindow::resize_event(int w, int h)
212 {
213         int x = w / 2 - 200;
214         int y = get_cancel_button()->get_y() -
215                 LoadMode::calculate_h(this, mwindow->theme);
216         draw_background(0, 0, w, h);
217
218         loadmode->reposition_window(x, y);
219
220         return BC_FileBox::resize_event(w, h);
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235 LocateFileWindow::LocateFileWindow(MWindow *mwindow,
236         char *init_directory,
237         char *old_filename)
238  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
239                 mwindow->gui->get_abs_cursor_y(1),
240                 init_directory,
241                 _(PROGRAM_NAME ": Locate file"),
242                 old_filename)
243 {
244         this->mwindow = mwindow;
245 }
246
247 LocateFileWindow::~LocateFileWindow()
248 {
249 }
250
251
252
253
254
255
256
257 LoadPrevious::LoadPrevious(MWindow *mwindow, Load *loadfile)
258  : BC_MenuItem(""), Thread()
259 {
260         this->mwindow = mwindow;
261         this->loadfile = loadfile;
262 }
263
264 int LoadPrevious::handle_event()
265 {
266         ArrayList<char*> path_list;
267         path_list.set_array_delete();
268         char *out_path;
269         int load_mode = mwindow->defaults->get("LOAD_MODE", LOADMODE_REPLACE);
270
271         path_list.append(out_path = new char[strlen(path) + 1]);
272         strcpy(out_path, path);
273
274         mwindow->load_filenames(&path_list, LOADMODE_REPLACE);
275         mwindow->gui->mainmenu->add_load(path_list.values[0]);
276         path_list.remove_all_objects();
277
278         mwindow->defaults->update("LOAD_MODE", load_mode);
279         mwindow->save_backup();
280         mwindow->session->changes_made = 0;
281         return 1;
282 }
283
284
285
286 void LoadPrevious::run()
287 {
288 //      loadfile->mwindow->load(path, loadfile->append);
289 }
290
291 int LoadPrevious::set_path(char *path)
292 {
293         strcpy(this->path, path);
294         return 0;
295 }
296
297
298
299 LoadBackup::LoadBackup(MWindow *mwindow)
300  : BC_MenuItem(_("Load backup"))
301 {
302         this->mwindow = mwindow;
303 }
304
305 int LoadBackup::handle_event()
306 {
307         mwindow->load_backup();
308         mwindow->session->changes_made = 1;
309         return 1;
310 }
311
312