r793: Small API addon, so plugins can 'see' camera and projector automation
[cinelerra_cv/mob.git] / cinelerra / loadfile.C
blob04629c426ec6064cc42d3feea06b78846a00b096
1 #include "assets.h"
2 #include "defaults.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;
146         
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();
156         mwindow->restart_brender();
157 //      mwindow->undo->update_undo(_("load"), LOAD_ALL);
159         if(load_mode == LOAD_REPLACE || load_mode == LOAD_REPLACE_CONCATENATE)
160                 mwindow->session->changes_made = 0;
161         else
162                 mwindow->session->changes_made = 1;
164         return;
174 LoadFileWindow::LoadFileWindow(MWindow *mwindow, 
175         LoadFileThread *thread,
176         char *init_directory)
177  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
178                 mwindow->gui->get_abs_cursor_y(1) - BC_WindowBase::get_resources()->filebox_h / 2,
179                 init_directory, 
180                 PROGRAM_NAME ": Load",
181                 _("Select files to load:"), 
182                 0,
183                 0,
184                 1,
185                 mwindow->theme->loadfile_pad)
187         this->thread = thread;
188         this->mwindow = mwindow; 
191 LoadFileWindow::~LoadFileWindow() 
193         delete loadmode;
196 int LoadFileWindow::create_objects()
198         BC_FileBox::create_objects();
200         int x = get_w() / 2 - 200;
201         int y = get_cancel_button()->get_y() - 50;
202         loadmode = new LoadMode(mwindow, this, x, y, &thread->load_mode, 0);
203         loadmode->create_objects();
205         return 0;
208 int LoadFileWindow::resize_event(int w, int h)
210         int x = w / 2 - 200;
211         int y = get_cancel_button()->get_y() - 50;
212         draw_background(0, 0, w, h);
214         loadmode->reposition_window(x, y);
216         return BC_FileBox::resize_event(w, h);
225 NewTimeline::NewTimeline(int x, int y, LoadFileWindow *window)
226  : BC_Radial(x, 
227         y, 
228         window->thread->load_mode == LOAD_REPLACE,
229         _("Replace current project."))
231         this->window = window;
233 int NewTimeline::handle_event()
235         window->newtracks->set_value(0);
236         window->newconcatenate->set_value(0);
237         window->concatenate->set_value(0);
238         window->resourcesonly->set_value(0);
239         return 1;
242 NewConcatenate::NewConcatenate(int x, int y, LoadFileWindow *window)
243  : BC_Radial(x, 
244         y, 
245         window->thread->load_mode == LOAD_REPLACE_CONCATENATE,
246         _("Replace current project and concatenate tracks."))
248         this->window = window;
250 int NewConcatenate::handle_event()
252         window->newtimeline->set_value(0);
253         window->newtracks->set_value(0);
254         window->concatenate->set_value(0);
255         window->resourcesonly->set_value(0);
256         return 1;
259 AppendNewTracks::AppendNewTracks(int x, int y, LoadFileWindow *window)
260  : BC_Radial(x, 
261         y, 
262         window->thread->load_mode == LOAD_NEW_TRACKS,
263         _("Append in new tracks."))
265         this->window = window;
267 int AppendNewTracks::handle_event()
269         window->newtimeline->set_value(0);
270         window->newconcatenate->set_value(0);
271         window->concatenate->set_value(0);
272         window->resourcesonly->set_value(0);
273         return 1;
276 EndofTracks::EndofTracks(int x, int y, LoadFileWindow *window)
277  : BC_Radial(x, 
278         y, 
279         window->thread->load_mode == LOAD_CONCATENATE,
280         _("Concatenate to existing tracks."))
282         this->window = window;
284 int EndofTracks::handle_event()
286         window->newtimeline->set_value(0);
287         window->newconcatenate->set_value(0);
288         window->newtracks->set_value(0);
289         window->resourcesonly->set_value(0);
290         return 1;
293 ResourcesOnly::ResourcesOnly(int x, int y, LoadFileWindow *window)
294  : BC_Radial(x, 
295         y, 
296         window->thread->load_mode == LOAD_RESOURCESONLY,
297         _("Create new resources only."))
299         this->window = window;
301 int ResourcesOnly::handle_event()
303         set_value(1);
304         window->newtimeline->set_value(0);
305         window->newconcatenate->set_value(0);
306         window->newtracks->set_value(0);
307         window->concatenate->set_value(0);
308         return 1;
317 LocateFileWindow::LocateFileWindow(MWindow *mwindow, 
318         char *init_directory, 
319         char *old_filename)
320  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
321                 mwindow->gui->get_abs_cursor_y(1), 
322                 init_directory, 
323                 PROGRAM_NAME ": Locate file", 
324                 old_filename)
326         this->mwindow = mwindow; 
329 LocateFileWindow::~LocateFileWindow()
339 LoadPrevious::LoadPrevious(MWindow *mwindow)
340  : BC_MenuItem(""), Thread()
342         this->mwindow = mwindow;
343         this->loadfile = loadfile; 
346 int LoadPrevious::handle_event()
348         ArrayList<char*> path_list;
349         path_list.set_array_delete();
350         char *out_path;
351         int load_mode = mwindow->defaults->get("LOAD_MODE", LOAD_REPLACE);
354         path_list.append(out_path = new char[strlen(path) + 1]);
355         strcpy(out_path, path);
356         mwindow->load_filenames(&path_list, LOAD_REPLACE);
357         mwindow->gui->mainmenu->add_load(path_list.values[0]);
358         path_list.remove_all_objects();
361         mwindow->defaults->update("LOAD_MODE", load_mode);
362         mwindow->undo->update_undo(_("load previous"), LOAD_ALL);
363         mwindow->save_backup();
364         mwindow->session->changes_made = 0;
366         return 1;
370 void LoadPrevious::run()
372 //      loadfile->mwindow->load(path, loadfile->append);
375 int LoadPrevious::set_path(char *path)
377         strcpy(this->path, path);
387 LoadBackup::LoadBackup(MWindow *mwindow)
388  : BC_MenuItem(_("Load backup"))
390         this->mwindow = mwindow;
393 int LoadBackup::handle_event()
395         ArrayList<char*> path_list;
396         path_list.set_array_delete();
397         char *out_path;
398         char string[BCTEXTLEN];
399         strcpy(string, BACKUP_PATH);
400         FileSystem fs;
401         fs.complete_path(string);
402         
403         path_list.append(out_path = new char[strlen(string) + 1]);
404         strcpy(out_path, string);
405         
406         mwindow->load_filenames(&path_list, LOAD_REPLACE, 0);
407         mwindow->edl->local_session->clip_title[0] = 0;
408 // This is unique to backups since the path of the backup is different than the
409 // path of the project.
410         mwindow->set_filename(mwindow->edl->project_path);
411         path_list.remove_all_objects();
412         mwindow->undo->update_undo(_("load backup"), LOAD_ALL, 0, 0);
413         mwindow->save_backup();
414 // We deliberately mark the project changed, because the backup is most likely
415 // not identical to the project file that it refers to.
416         mwindow->session->changes_made = 1;
418         return 1;
420         
423 // Dialog to set reel number/name
425 ReelWindow::ReelWindow(MWindow *mwindow)
426  : BC_Window(_("Please enter the reel name and number"),
427         mwindow->gui->get_abs_cursor_x(1) - 375 / 2,
428         mwindow->gui->get_abs_cursor_y(1) - 150 / 2,
429         375,
430         150,
431         100,
432         100,
433         0,
434         0,
435         1)
437         this->mwindow = mwindow;
438         overwrite_reel = 0; // TODO: this should be loaded from previous time
441 ReelWindow::~ReelWindow()
443         delete reel_name_title;
444         delete reel_name;
445         delete reel_number_title;
446         delete reel_number;
447         delete checkbox;
450 int ReelWindow::create_objects()
452         int y = 10;
453         int x = 0;
455         add_subwindow(checkbox = new OverwriteReel(this, x, y, !overwrite_reel));       
456         y += 40;
457         
458         x = 10;
459         add_subwindow(reel_name_title = new BC_Title(x, y, _("Reel Name:")));
460         x += reel_name_title->get_w() + 20;
462         add_subwindow(reel_name = new BC_TextBox(x,
463                 y,
464                 250,
465                 1,
466                 "cin0000"));
467         
468         y += 30;
469         
470         x = 10;
471         
472         add_subwindow(reel_number_title = new BC_Title(x, y,
473                                                                                                                                         _("Reel Number:")));
474         // line up the text boxes
475         x += reel_name_title->get_w() + 20;
477         add_subwindow(reel_number = new BC_TextBox(x,
478                 y,
479                 50,
480                 1,
481                 "00"));
483         add_subwindow(ok_button = new BC_OKButton(this));
484         
485         add_subwindow(cancel_button = new BC_CancelButton(this));
487 // Disable reel_name and reel_number if the user doesn't want to overwrite
488 // (overwrite == accept default as well)
489         if(!overwrite_reel)
490         {
491                 reel_name->disable();
492                 reel_number->disable();
493         }
494         show_window();
496         return 0;       
499 int ReelWindow::resize_event(int w, int h)
501 // Doesn't resize
502         return 0;
505 OverwriteReel::OverwriteReel(ReelWindow *rwindow,
506         int x, int y, int value)
507  : BC_CheckBox(x, y, value, _("Use default or previous name and number"))
509         this->rwindow = rwindow;
512 int OverwriteReel::handle_event()
514         rwindow->overwrite_reel = !get_value();
515 // If the checkbox is not enabled, we want to enable the reel_name and
516 // reel_number text boxes
517         if(!get_value())
518         {
519                 rwindow->reel_name->enable();
520                 rwindow->reel_number->enable();
521         }
522         else
523         {
524                 rwindow->reel_name->disable();
525                 rwindow->reel_number->disable();
526         }