r999: maintainers added to README_en.
[cinelerra_cv/mob.git] / cinelerra / loadfile.C
blob35db266015045b6d18654c1334b1e0d7027de2e9
1 #include "assets.h"
2 #include "bchash.h"
3 #include "edl.h"
4 #include "errorbox.h"
5 #include "file.h"
6 #include "filesystem.h"
7 #include "indexfile.h"
8 #include "language.h"
9 #include "loadfile.h"
10 #include "loadmode.h"
11 #include "localsession.h"
12 #include "mainmenu.h"
13 #include "mainundo.h"
14 #include "mainsession.h"
15 #include "mwindow.h"
16 #include "mwindowgui.h"
17 #include "theme.h"
21 #include <string.h>
23 Load::Load(MWindow *mwindow, MainMenu *mainmenu)
24  : BC_MenuItem(_("Load files..."), "o", 'o')
25
26         this->mwindow = mwindow;
27         this->mainmenu = mainmenu;
30 Load::~Load()
32         delete thread;
35 int Load::create_objects()
37         thread = new LoadFileThread(mwindow, this);
38         return 0;
41 int Load::handle_event() 
43 //printf("Load::handle_event 1\n");
44         if(!thread->running())
45         {
46 //printf("Load::handle_event 2\n");
47                 thread->start();
48         }
49         return 1;
57 LoadFileThread::LoadFileThread(MWindow *mwindow, Load *load)
58  : Thread()
60         this->mwindow = mwindow;
61         this->load = load;
64 LoadFileThread::~LoadFileThread()
68 void LoadFileThread::run()
70         int result;
71         ArrayList<BC_ListBoxItem*> *dirlist;
72         FileSystem fs;
73         ArrayList<char*> path_list;
74         path_list.set_array_delete();
75         char default_path[BCTEXTLEN];
76         char *reel_name = 0;
77         int reel_number = 0;
78         int overwrite_reel = 0;
79         
80         sprintf(default_path, "~");
81         mwindow->defaults->get("DEFAULT_LOADPATH", default_path);
82         load_mode = mwindow->defaults->get("LOAD_MODE", LOAD_REPLACE);
84         {
85                 LoadFileWindow window(mwindow, this, default_path);
86                 window.create_objects();
87                 result = window.run_window();
89                 if ((!result) && (load_mode == LOAD_REPLACE)) {
90                         mwindow->set_filename(window.get_path(0));
91                 }
93 // Collect all selected files
94                 if(!result)
95                 {
96                         char *in_path, *out_path;
97                         int i = 0;
99                         while((in_path = window.get_path(i)))
100                         {
101                                 int j;
102                                 for(j = 0; j < path_list.total; j++)
103                                 {
104                                         if(!strcmp(in_path, path_list.values[j])) break;
105                                 }
106                                 
107                                 if(j == path_list.total)
108                                 {
109                                         path_list.append(out_path = new char[strlen(in_path) + 1]);
110                                         strcpy(out_path, in_path);
111                                 }
112                                 i++;
113                         }
114                 }
116                 mwindow->defaults->update("DEFAULT_LOADPATH", 
117                         window.get_submitted_path());
118                 mwindow->defaults->update("LOAD_MODE", 
119                         load_mode);
120         }
122 // No file selected
123         if(path_list.total == 0 || result == 1)
124         {
125                 return;
126         }
128 //      {
129 //              ReelWindow rwindow(mwindow);
130 //              rwindow.create_objects();
131 //              result = rwindow.run_window();
133 //              if(result)
134 //              {
135 //                      return;
136 //              }
137                 
138 //              reel_name = rwindow.reel_name->get_text();
139 //              reel_number = atol(rwindow.reel_number->get_text());
140 //              overwrite_reel = rwindow.overwrite_reel;
141 //      }
143         reel_name = "none";
144         reel_number = 0;
145         overwrite_reel = 0;
147         mwindow->interrupt_indexes();
148         mwindow->gui->lock_window("LoadFileThread::run");
149         result = mwindow->load_filenames(&path_list, load_mode, 0, reel_name, reel_number, overwrite_reel);
150         mwindow->gui->mainmenu->add_load(path_list.values[0]);
151         mwindow->gui->unlock_window();
152         path_list.remove_all_objects();
155         mwindow->save_backup();
157         mwindow->restart_brender();
158 //      mwindow->undo->update_undo(_("load"), LOAD_ALL);
160         if(load_mode == LOAD_REPLACE || load_mode == LOAD_REPLACE_CONCATENATE)
161                 mwindow->session->changes_made = 0;
162         else
163                 mwindow->session->changes_made = 1;
165         return;
175 LoadFileWindow::LoadFileWindow(MWindow *mwindow, 
176         LoadFileThread *thread,
177         char *init_directory)
178  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
179                 mwindow->gui->get_abs_cursor_y(1) - BC_WindowBase::get_resources()->filebox_h / 2,
180                 init_directory, 
181                 PROGRAM_NAME ": Load",
182                 _("Select files to load:"), 
183                 0,
184                 0,
185                 1,
186                 mwindow->theme->loadfile_pad)
188         this->thread = thread;
189         this->mwindow = mwindow; 
192 LoadFileWindow::~LoadFileWindow() 
194         delete loadmode;
197 int LoadFileWindow::create_objects()
199         BC_FileBox::create_objects();
201         int x = get_w() / 2 - 200;
202         int y = get_cancel_button()->get_y() - 50;
203         loadmode = new LoadMode(mwindow, this, x, y, &thread->load_mode, 0);
204         loadmode->create_objects();
206         return 0;
209 int LoadFileWindow::resize_event(int w, int h)
211         int x = w / 2 - 200;
212         int y = get_cancel_button()->get_y() - 50;
213         draw_background(0, 0, w, h);
215         loadmode->reposition_window(x, y);
217         return BC_FileBox::resize_event(w, h);
226 NewTimeline::NewTimeline(int x, int y, LoadFileWindow *window)
227  : BC_Radial(x, 
228         y, 
229         window->thread->load_mode == LOAD_REPLACE,
230         _("Replace current project."))
232         this->window = window;
234 int NewTimeline::handle_event()
236         window->newtracks->set_value(0);
237         window->newconcatenate->set_value(0);
238         window->concatenate->set_value(0);
239         window->resourcesonly->set_value(0);
240         return 1;
243 NewConcatenate::NewConcatenate(int x, int y, LoadFileWindow *window)
244  : BC_Radial(x, 
245         y, 
246         window->thread->load_mode == LOAD_REPLACE_CONCATENATE,
247         _("Replace current project and concatenate tracks."))
249         this->window = window;
251 int NewConcatenate::handle_event()
253         window->newtimeline->set_value(0);
254         window->newtracks->set_value(0);
255         window->concatenate->set_value(0);
256         window->resourcesonly->set_value(0);
257         return 1;
260 AppendNewTracks::AppendNewTracks(int x, int y, LoadFileWindow *window)
261  : BC_Radial(x, 
262         y, 
263         window->thread->load_mode == LOAD_NEW_TRACKS,
264         _("Append in new tracks."))
266         this->window = window;
268 int AppendNewTracks::handle_event()
270         window->newtimeline->set_value(0);
271         window->newconcatenate->set_value(0);
272         window->concatenate->set_value(0);
273         window->resourcesonly->set_value(0);
274         return 1;
277 EndofTracks::EndofTracks(int x, int y, LoadFileWindow *window)
278  : BC_Radial(x, 
279         y, 
280         window->thread->load_mode == LOAD_CONCATENATE,
281         _("Concatenate to existing tracks."))
283         this->window = window;
285 int EndofTracks::handle_event()
287         window->newtimeline->set_value(0);
288         window->newconcatenate->set_value(0);
289         window->newtracks->set_value(0);
290         window->resourcesonly->set_value(0);
291         return 1;
294 ResourcesOnly::ResourcesOnly(int x, int y, LoadFileWindow *window)
295  : BC_Radial(x, 
296         y, 
297         window->thread->load_mode == LOAD_RESOURCESONLY,
298         _("Create new resources only."))
300         this->window = window;
302 int ResourcesOnly::handle_event()
304         set_value(1);
305         window->newtimeline->set_value(0);
306         window->newconcatenate->set_value(0);
307         window->newtracks->set_value(0);
308         window->concatenate->set_value(0);
309         return 1;
318 LocateFileWindow::LocateFileWindow(MWindow *mwindow, 
319         char *init_directory, 
320         char *old_filename)
321  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
322                 mwindow->gui->get_abs_cursor_y(1), 
323                 init_directory, 
324                 PROGRAM_NAME ": Locate file", 
325                 old_filename)
327         this->mwindow = mwindow; 
330 LocateFileWindow::~LocateFileWindow()
340 LoadPrevious::LoadPrevious(MWindow *mwindow)
341  : BC_MenuItem(""), Thread()
343         this->mwindow = mwindow;
344         this->loadfile = loadfile; 
347 int LoadPrevious::handle_event()
349         ArrayList<char*> path_list;
350         path_list.set_array_delete();
351         char *out_path;
352         int load_mode = mwindow->defaults->get("LOAD_MODE", LOAD_REPLACE);
355         path_list.append(out_path = new char[strlen(path) + 1]);
356         strcpy(out_path, path);
357         mwindow->load_filenames(&path_list, LOAD_REPLACE);
358         mwindow->gui->mainmenu->add_load(path_list.values[0]);
359         path_list.remove_all_objects();
362         mwindow->defaults->update("LOAD_MODE", load_mode);
363         mwindow->undo->update_undo(_("load previous"), LOAD_ALL);
364         mwindow->save_backup();
365         mwindow->session->changes_made = 0;
366         return 1;
375 void LoadPrevious::run()
377 //      loadfile->mwindow->load(path, loadfile->append);
380 int LoadPrevious::set_path(char *path)
382         strcpy(this->path, path);
392 LoadBackup::LoadBackup(MWindow *mwindow)
393  : BC_MenuItem(_("Load backup"))
395         this->mwindow = mwindow;
398 int LoadBackup::handle_event()
400         ArrayList<char*> path_list;
401         path_list.set_array_delete();
402         char *out_path;
403         char string[BCTEXTLEN];
404         strcpy(string, BACKUP_PATH);
405         FileSystem fs;
406         fs.complete_path(string);
407         
408         path_list.append(out_path = new char[strlen(string) + 1]);
409         strcpy(out_path, string);
410         
411         mwindow->load_filenames(&path_list, LOAD_REPLACE, 0);
412         mwindow->edl->local_session->clip_title[0] = 0;
413 // This is unique to backups since the path of the backup is different than the
414 // path of the project.
415         mwindow->set_filename(mwindow->edl->project_path);
416         path_list.remove_all_objects();
417         mwindow->undo->update_undo(_("load backup"), LOAD_ALL, 0, 0);
418         mwindow->save_backup();
419 // We deliberately mark the project changed, because the backup is most likely
420 // not identical to the project file that it refers to.
421         mwindow->session->changes_made = 1;
423         return 1;
425         
428 // Dialog to set reel number/name
430 ReelWindow::ReelWindow(MWindow *mwindow)
431  : BC_Window(_("Please enter the reel name and number"),
432         mwindow->gui->get_abs_cursor_x(1) - 375 / 2,
433         mwindow->gui->get_abs_cursor_y(1) - 150 / 2,
434         375,
435         150,
436         100,
437         100,
438         0,
439         0,
440         1)
442         this->mwindow = mwindow;
443         overwrite_reel = 0; // TODO: this should be loaded from previous time
446 ReelWindow::~ReelWindow()
448         delete reel_name_title;
449         delete reel_name;
450         delete reel_number_title;
451         delete reel_number;
452         delete checkbox;
455 int ReelWindow::create_objects()
457         int y = 10;
458         int x = 0;
460         add_subwindow(checkbox = new OverwriteReel(this, x, y, !overwrite_reel));       
461         y += 40;
462         
463         x = 10;
464         add_subwindow(reel_name_title = new BC_Title(x, y, _("Reel Name:")));
465         x += reel_name_title->get_w() + 20;
467         add_subwindow(reel_name = new BC_TextBox(x,
468                 y,
469                 250,
470                 1,
471                 "cin0000"));
472         
473         y += 30;
474         
475         x = 10;
476         
477         add_subwindow(reel_number_title = new BC_Title(x, y,
478                                                                                                                                         _("Reel Number:")));
479         // line up the text boxes
480         x += reel_name_title->get_w() + 20;
482         add_subwindow(reel_number = new BC_TextBox(x,
483                 y,
484                 50,
485                 1,
486                 "00"));
488         add_subwindow(ok_button = new BC_OKButton(this));
489         
490         add_subwindow(cancel_button = new BC_CancelButton(this));
492 // Disable reel_name and reel_number if the user doesn't want to overwrite
493 // (overwrite == accept default as well)
494         if(!overwrite_reel)
495         {
496                 reel_name->disable();
497                 reel_number->disable();
498         }
499         show_window();
501         return 0;       
504 int ReelWindow::resize_event(int w, int h)
506 // Doesn't resize
507         return 0;
510 OverwriteReel::OverwriteReel(ReelWindow *rwindow,
511         int x, int y, int value)
512  : BC_CheckBox(x, y, value, _("Use default or previous name and number"))
514         this->rwindow = rwindow;
517 int OverwriteReel::handle_event()
519         rwindow->overwrite_reel = !get_value();
520 // If the checkbox is not enabled, we want to enable the reel_name and
521 // reel_number text boxes
522         if(!get_value())
523         {
524                 rwindow->reel_name->enable();
525                 rwindow->reel_number->enable();
526         }
527         else
528         {
529                 rwindow->reel_name->disable();
530                 rwindow->reel_number->disable();
531         }