Add back 2 patches for histogram and overlayframe that are working correctly and...
[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  *
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 "preferences.h"
40 #include "theme.h"
41
42
43
44 #include <string.h>
45
46 Load::Load(MWindow *mwindow, MainMenu *mainmenu)
47  : BC_MenuItem(_("Load files..."), "o", 'o')
48 {
49         this->mwindow = mwindow;
50         this->mainmenu = mainmenu;
51         this->thread = 0;
52 }
53
54 Load::~Load()
55 {
56         delete thread;
57 }
58
59 void Load::create_objects()
60 {
61         thread = new LoadFileThread(mwindow, this);
62 }
63
64 int Load::handle_event()
65 {
66         mwindow->gui->unlock_window();
67         thread->start();
68         mwindow->gui->lock_window("Load::handle_event");
69         return 1;
70 }
71
72
73
74
75
76
77 LoadFileThread::LoadFileThread(MWindow *mwindow, Load *load)
78  : BC_DialogThread()
79 {
80         this->mwindow = mwindow;
81         this->load = load;
82         this->window = 0;
83         load_mode = LOADMODE_RESOURCESONLY;
84         edl_mode = LOADMODE_EDL_CLIP;
85 }
86
87 LoadFileThread::~LoadFileThread()
88 {
89         close_window();
90 }
91
92 BC_Window* LoadFileThread::new_gui()
93 {
94         char default_path[BCTEXTLEN];
95
96         sprintf(default_path, "~");
97         mwindow->defaults->get("DEFAULT_LOADPATH", default_path);
98         load_mode = mwindow->defaults->get("LOAD_MODE", load_mode);
99
100         mwindow->gui->lock_window("LoadFileThread::new_gui");
101         window = new LoadFileWindow(mwindow, this, default_path);
102         mwindow->gui->unlock_window();
103
104         window->create_objects();
105         return window;
106 }
107
108 void LoadFileThread::handle_done_event(int result)
109 {
110         window->lock_window("LoadFileThread::handle_done_event");
111         window->hide_window();
112         window->unlock_window();
113
114         if( !result ) load_apply();
115 }
116
117 void LoadFileThread::load_apply()
118 {
119         mwindow->defaults->update("DEFAULT_LOADPATH", window->get_submitted_path());
120         mwindow->defaults->update("LOAD_MODE", load_mode);
121         if( edl_mode == LOADMODE_EDL_FILEREF )
122                 mwindow->show_warning(
123                         &mwindow->preferences->warn_fileref,
124                         _("Other projects can change this project\n"
125                           "and this can become a broken link"));
126         ArrayList<char*> path_list;
127         path_list.set_array_delete();
128
129 // Collect all selected files
130         char *in_path;
131         for( int i=0; (in_path = window->get_path(i))!=0; ++i ) {
132                 int k = path_list.size();
133                 while( --k >= 0 && strcmp(in_path, path_list.values[k]) );
134                 if( k < 0 ) path_list.append(cstrdup(in_path));
135         }
136
137 // No file selected
138         if( !path_list.size() ) return;
139         int replaced = load_mode == LOADMODE_REPLACE ||
140             load_mode == LOADMODE_REPLACE_CONCATENATE ? 1 : 0;
141
142         mwindow->interrupt_indexes();
143         mwindow->gui->lock_window("LoadFileThread::run");
144         mwindow->load_filenames(&path_list, load_mode, edl_mode, replaced);
145         mwindow->gui->mainmenu->add_load(path_list.values[0]);
146         mwindow->gui->unlock_window();
147         path_list.remove_all_objects();
148
149         mwindow->save_backup();
150         mwindow->restart_brender();
151         mwindow->session->changes_made = !replaced ? 1 : 0;
152 }
153
154
155 LoadFileWindow::LoadFileWindow(MWindow *mwindow,
156         LoadFileThread *thread,
157         char *init_directory)
158  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
159                 mwindow->gui->get_abs_cursor_y(1) -
160                         BC_WindowBase::get_resources()->filebox_h / 2,
161                 init_directory,
162                 _(PROGRAM_NAME ": Load"),
163                 _("Select files to load:"),
164                 0,
165                 0,
166                 1,
167                 mwindow->theme->loadfile_pad)
168 {
169         this->thread = thread;
170         this->mwindow = mwindow;
171 }
172
173 LoadFileWindow::~LoadFileWindow()
174 {
175         lock_window("LoadFileWindow::~LoadFileWindow");
176         delete loadmode;
177         unlock_window();
178 }
179
180 void LoadFileWindow::create_objects()
181 {
182         lock_window("LoadFileWindow::create_objects");
183         BC_FileBox::create_objects();
184
185         int x = get_w() / 2 - LoadMode::calculate_w(this, mwindow->theme) / 2;
186         int y = get_y_margin();
187 // always start as clip to match historical behavior
188         thread->edl_mode = LOADMODE_EDL_CLIP;
189         loadmode = new LoadMode(mwindow, this, x, y,
190                 &thread->load_mode, &thread->edl_mode, 0, 1);
191         loadmode->create_objects();
192         const char *apply =  _("Apply");
193         x = 3*get_w()/4 - BC_GenericButton::calculate_w(this, apply)/2;
194         y = get_h() - BC_CancelButton::calculate_h() - yS(16);
195         add_subwindow(load_file_apply = new LoadFileApply(this, x, y, apply));
196
197         show_window(1);
198         unlock_window();
199
200 }
201
202 int LoadFileWindow::resize_event(int w, int h)
203 {
204         draw_background(0, 0, w, h);
205         BC_FileBox::resize_event(w, h);
206         int x = w / 2 - LoadMode::calculate_w(this, mwindow->theme) / 2;
207         int y = get_y_margin();
208         loadmode->reposition_window(x, y);
209         const char *apply =  load_file_apply->get_text();
210         x = 3*get_w()/4 - BC_GenericButton::calculate_w(this, apply)/2;
211         y = get_h() - BC_CancelButton::calculate_h() - yS(16);
212         load_file_apply->reposition_window(x, y);
213         flush();
214         return 1;
215 }
216
217
218 LoadFileApply::LoadFileApply(LoadFileWindow *load_file_window,
219                 int x, int y, const char *text)
220  : BC_GenericButton(x, y, text)
221 {
222         this->load_file_window = load_file_window;
223 }
224
225 int LoadFileApply::handle_event()
226 {
227         load_file_window->thread->load_apply();
228         return 1;
229 }
230
231
232 LocateFileWindow::LocateFileWindow(MWindow *mwindow,
233         char *init_directory,
234         char *old_filename)
235  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
236                 mwindow->gui->get_abs_cursor_y(1),
237                 init_directory,
238                 _(PROGRAM_NAME ": Locate file"),
239                 old_filename)
240 {
241         this->mwindow = mwindow;
242 }
243
244 LocateFileWindow::~LocateFileWindow()
245 {
246 }
247
248
249
250
251
252
253
254 LoadPrevious::LoadPrevious(MWindow *mwindow, Load *loadfile)
255  : BC_MenuItem("")
256 {
257         this->mwindow = mwindow;
258         this->loadfile = loadfile;
259 }
260
261 int LoadPrevious::handle_event()
262 {
263         if( !path[0] ) return 1;
264         ArrayList<char*> path_list;
265         path_list.set_array_delete();
266         char *out_path;
267         int load_mode = mwindow->defaults->get("LOAD_MODE", LOADMODE_REPLACE);
268
269         path_list.append(out_path = new char[strlen(path) + 1]);
270         strcpy(out_path, path);
271
272         mwindow->load_filenames(&path_list, LOADMODE_REPLACE);
273         mwindow->gui->mainmenu->add_load(path_list.values[0]);
274         path_list.remove_all_objects();
275
276         mwindow->defaults->update("LOAD_MODE", load_mode);
277         mwindow->save_backup();
278         mwindow->session->changes_made = 0;
279         return 1;
280 }
281
282 int LoadPrevious::set_path(const char *path)
283 {
284         strcpy(this->path, path);
285         return 0;
286 }
287
288
289
290 LoadBackup::LoadBackup(MWindow *mwindow)
291  : BC_MenuItem(_("Load backup"))
292 {
293         this->mwindow = mwindow;
294 }
295
296 int LoadBackup::handle_event()
297 {
298         mwindow->load_backup();
299         mwindow->session->changes_made = 1;
300         return 1;
301 }
302
303