no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / loadfile.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  * Copyright (C) 2003-2016 Cinelerra CV contributors
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include "assets.h"
24 #include "bcsignals.h"
25 #include "bchash.h"
26 #include "edl.h"
27 #include "errorbox.h"
28 #include "file.h"
29 #include "filesystem.h"
30 #include "indexfile.h"
31 #include "language.h"
32 #include "loadfile.h"
33 #include "loadmode.h"
34 #include "localsession.h"
35 #include "mainmenu.h"
36 #include "mainundo.h"
37 #include "mainsession.h"
38 #include "mwindow.h"
39 #include "mwindowgui.h"
40 #include "preferences.h"
41 #include "theme.h"
42
43
44
45 #include <string.h>
46
47 Load::Load(MWindow *mwindow, MainMenu *mainmenu)
48  : BC_MenuItem(_("Load files..."), "o", 'o')
49 {
50         this->mwindow = mwindow;
51         this->mainmenu = mainmenu;
52         this->thread = 0;
53 }
54
55 Load::~Load()
56 {
57         delete thread;
58 }
59
60 void Load::create_objects()
61 {
62         thread = new LoadFileThread(mwindow, this);
63 }
64
65 int Load::handle_event()
66 {
67         mwindow->gui->unlock_window();
68         thread->start();
69         mwindow->gui->lock_window("Load::handle_event");
70         return 1;
71 }
72
73
74
75
76
77
78 LoadFileThread::LoadFileThread(MWindow *mwindow, Load *load)
79  : BC_DialogThread()
80 {
81         this->mwindow = mwindow;
82         this->load = load;
83         this->window = 0;
84         load_mode = LOADMODE_RESOURCESONLY;
85         edl_mode = LOADMODE_EDL_CLIP;
86 }
87
88 LoadFileThread::~LoadFileThread()
89 {
90         close_window();
91 }
92
93 BC_Window* LoadFileThread::new_gui()
94 {
95         char default_path[BCTEXTLEN];
96
97         sprintf(default_path, "~");
98         mwindow->defaults->get("DEFAULT_LOADPATH", default_path);
99         load_mode = mwindow->defaults->get("LOAD_MODE", load_mode);
100
101         mwindow->gui->lock_window("LoadFileThread::new_gui");
102         window = new LoadFileWindow(mwindow, this, default_path);
103         mwindow->gui->unlock_window();
104
105         window->create_objects();
106         return window;
107 }
108
109 void LoadFileThread::handle_done_event(int result)
110 {
111         window->lock_window("LoadFileThread::handle_done_event");
112         window->hide_window();
113         window->unlock_window();
114
115         if( !result ) load_apply();
116 }
117
118 void LoadFileThread::load_apply()
119 {
120         mwindow->defaults->update("DEFAULT_LOADPATH", window->get_submitted_path());
121         mwindow->defaults->update("LOAD_MODE", load_mode);
122         if( edl_mode == LOADMODE_EDL_FILEREF )
123                 mwindow->show_warning(
124                         &mwindow->preferences->warn_fileref,
125                         _("Other projects can change this project\n"
126                           "and this can become a broken link"));
127         ArrayList<char*> path_list;
128         path_list.set_array_delete();
129
130 // Collect all selected files
131         char *in_path;
132         for( int i=0; (in_path = window->get_path(i))!=0; ++i ) {
133                 int k = path_list.size();
134                 while( --k >= 0 && strcmp(in_path, path_list.values[k]) );
135                 if( k < 0 ) path_list.append(cstrdup(in_path));
136         }
137
138 // No file selected
139         if( !path_list.size() ) return;
140         int replaced = load_mode == LOADMODE_REPLACE ||
141             load_mode == LOADMODE_REPLACE_CONCATENATE ? 1 : 0;
142
143         mwindow->interrupt_indexes();
144         mwindow->gui->lock_window("LoadFileThread::run");
145         mwindow->load_filenames(&path_list, load_mode, edl_mode, replaced);
146         mwindow->gui->mainmenu->add_load(path_list.values[0]);
147         mwindow->gui->unlock_window();
148         path_list.remove_all_objects();
149
150         mwindow->save_backup();
151         mwindow->restart_brender();
152         mwindow->session->changes_made = !replaced ? 1 : 0;
153 }
154
155
156 LoadFileWindow::LoadFileWindow(MWindow *mwindow,
157         LoadFileThread *thread,
158         char *init_directory)
159  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
160                 mwindow->gui->get_abs_cursor_y(1) -
161                         BC_WindowBase::get_resources()->filebox_h / 2,
162                 init_directory,
163                 _(PROGRAM_NAME ": Load"),
164                 _("Select files to load:"),
165                 0,
166                 0,
167                 1,
168                 mwindow->theme->loadfile_pad)
169 {
170         this->thread = thread;
171         this->mwindow = mwindow;
172 // *** CONTEXT_HELP ***
173         context_help_set_keyword("Loading Files");
174 }
175
176 LoadFileWindow::~LoadFileWindow()
177 {
178         lock_window("LoadFileWindow::~LoadFileWindow");
179         delete loadmode;
180         unlock_window();
181 }
182
183 void LoadFileWindow::create_objects()
184 {
185         lock_window("LoadFileWindow::create_objects");
186         BC_FileBox::create_objects();
187
188         int x = get_w() / 2 - LoadMode::calculate_w(this, mwindow->theme) / 2;
189         int y = get_y_margin();
190 // always start as clip to match historical behavior
191         thread->edl_mode = LOADMODE_EDL_CLIP;
192         loadmode = new LoadMode(mwindow, this, x, y,
193                 &thread->load_mode, &thread->edl_mode, 0, 1);
194         loadmode->create_objects();
195         const char *apply =  _("Apply");
196         x = 3*get_w()/4 - BC_GenericButton::calculate_w(this, apply)/2;
197         y = get_h() - BC_CancelButton::calculate_h() - yS(16);
198         add_subwindow(load_file_apply = new LoadFileApply(this, x, y, apply));
199
200         show_window(1);
201         unlock_window();
202
203 }
204
205 int LoadFileWindow::resize_event(int w, int h)
206 {
207         draw_background(0, 0, w, h);
208         BC_FileBox::resize_event(w, h);
209         int x = w / 2 - LoadMode::calculate_w(this, mwindow->theme) / 2;
210         int y = get_y_margin();
211         loadmode->reposition_window(x, y);
212         const char *apply =  load_file_apply->get_text();
213         x = 3*get_w()/4 - BC_GenericButton::calculate_w(this, apply)/2;
214         y = get_h() - BC_CancelButton::calculate_h() - yS(16);
215         load_file_apply->reposition_window(x, y);
216         flush();
217         return 1;
218 }
219
220
221 LoadFileApply::LoadFileApply(LoadFileWindow *load_file_window,
222                 int x, int y, const char *text)
223  : BC_GenericButton(x, y, text)
224 {
225         this->load_file_window = load_file_window;
226 }
227
228 int LoadFileApply::handle_event()
229 {
230         load_file_window->thread->load_apply();
231         return 1;
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("")
259 {
260         this->mwindow = mwindow;
261         this->loadfile = loadfile;
262 }
263
264 int LoadPrevious::handle_event()
265 {
266         if( !path[0] ) return 1;
267         ArrayList<char*> path_list;
268         path_list.set_array_delete();
269         char *out_path;
270         int load_mode = mwindow->defaults->get("LOAD_MODE", LOADMODE_REPLACE);
271
272         path_list.append(out_path = new char[strlen(path) + 1]);
273         strcpy(out_path, path);
274
275         mwindow->load_filenames(&path_list, LOADMODE_REPLACE);
276         mwindow->gui->mainmenu->add_load(path_list.values[0]);
277         path_list.remove_all_objects();
278
279         mwindow->defaults->update("LOAD_MODE", load_mode);
280         mwindow->save_backup();
281         mwindow->session->changes_made = 0;
282         return 1;
283 }
284
285 int LoadPrevious::set_path(const char *path)
286 {
287         strcpy(this->path, path);
288         return 0;
289 }
290
291
292
293 LoadBackup::LoadBackup(MWindow *mwindow)
294  : BC_MenuItem(_("Load backup"))
295 {
296         this->mwindow = mwindow;
297 }
298
299 int LoadBackup::handle_event()
300 {
301         mwindow->load_backup();
302         mwindow->session->changes_made = 1;
303         return 1;
304 }
305
306