r886: Don't access widgets that are not present.
[cinelerra_cv/ct.git] / cinelerra / formattools.C
blob9178043333f8be9e5703e3578fdab910a07f92ab
1 #include "asset.h"
2 #include "guicast.h"
3 #include "file.h"
4 #include "formattools.h"
5 #include "language.h"
6 #include "maxchannels.h"
7 #include "mwindow.h"
8 #include "preferences.h"
9 #include "quicktime.h"
10 #include "theme.h"
11 #include "videodevice.inc"
12 #include <string.h>
13 #include "pipe.h"
16 FormatTools::FormatTools(MWindow *mwindow,
17                                 BC_WindowBase *window, 
18                                 Asset *asset)
20         this->mwindow = mwindow;
21         this->window = window;
22         this->asset = asset;
23         this->plugindb = mwindow->plugindb;
25         aparams_button = 0;
26         vparams_button = 0;
27         aparams_thread = 0;
28         vparams_thread = 0;
29         channels_tumbler = 0;
30         path_textbox = 0;
31         path_button = 0;
32         path_recent = 0;
33         pipe_status = 0;
34         w = 0;
37 FormatTools::~FormatTools()
39         delete pipe_status;
40         delete path_recent;
41         delete path_button;
42         delete path_textbox;
43         delete format_button;
45         if(aparams_button) delete aparams_button;
46         if(vparams_button) delete vparams_button;
47         if(aparams_thread) delete aparams_thread;
48         if(vparams_thread) delete vparams_thread;
49         if(channels_tumbler) delete channels_tumbler;
52 int FormatTools::create_objects(int &init_x, 
53                                                 int &init_y, 
54                                                 int do_audio,    // Include support for audio
55                                                 int do_video,   // Include support for video
56                                                 int prompt_audio,  // Include checkbox for audio
57                                                 int prompt_video,
58                                                 int prompt_audio_channels,
59                                                 int prompt_video_compression,
60                                                 char *locked_compressor,
61                                                 int recording,
62                                                 int *strategy,
63                                                 int brender)
65         int x = init_x;
66         int y = init_y;
68         this->locked_compressor = locked_compressor;
69         this->recording = recording;
70         this->use_brender = brender;
71         this->do_audio = do_audio;
72         this->do_video = do_video;
73         this->prompt_audio = prompt_audio;
74         this->prompt_audio_channels = prompt_audio_channels;
75         this->prompt_video = prompt_video;
76         this->prompt_video_compression = prompt_video_compression;
77         this->strategy = strategy;
79 //printf("FormatTools::create_objects 1\n");
81 // Modify strategy depending on render farm
82         if(strategy)
83         {
84                 if(mwindow->preferences->use_renderfarm)
85                 {
86                         if(*strategy == FILE_PER_LABEL)
87                                 *strategy = FILE_PER_LABEL_FARM;
88                         else
89                         if(*strategy == SINGLE_PASS)
90                                 *strategy = SINGLE_PASS_FARM;
91                 }
92                 else
93                 {
94                         if(*strategy == FILE_PER_LABEL_FARM)
95                                 *strategy = FILE_PER_LABEL;
96                         else
97                         if(*strategy == SINGLE_PASS_FARM)
98                                 *strategy = SINGLE_PASS;
99                 }
100         }
102 //printf("FormatTools::create_objects 1\n");
103         if(!recording)
104         {
105                 window->add_subwindow(path_textbox = new FormatPathText(x, y, this));
106                 x += 305;
107                 path_recent = new BC_RecentList("PATH", mwindow->defaults,
108                                         path_textbox, 10, x, y, 300, 100);
109                 window->add_subwindow(path_recent);
110                 path_recent->load_items(FILE_FORMAT_PREFIX(asset->format));
112                 x += 18;
113                 window->add_subwindow(path_button = new BrowseButton(
114                         mwindow,
115                         window,
116                         path_textbox, 
117                         x, 
118                         y, 
119                         asset->path,
120                         _("Output to file"),
121                         _("Select a file to write to:"),
122                         0));
124 // Set w for user.
125                 w = x + path_button->get_w() + 5;
126                 x -= 305;
128                 y += 25;
130                 pipe_status = new PipeStatus(x, y, "");
131                 window->add_subwindow(pipe_status);
132                 pipe_status->set_status(asset);
134                 y += 35;
135         }
136         else
137                 w = x + 305;
139         window->add_subwindow(format_title = new BC_Title(x, y, _("File Format:")));
140         x += 90;
141         window->add_subwindow(format_text = new BC_TextBox(x, 
142                 y, 
143                 200, 
144                 1, 
145                 File::formattostr(asset->format)));
146         x += format_text->get_w();
147         window->add_subwindow(format_button = new FormatFormat(x, 
148                 y, 
149                 this));
150         format_button->create_objects();
152         x = init_x;
153         y += format_button->get_h() + 10;
154         if(do_audio)
155         {
156                 window->add_subwindow(audio_title = new BC_Title(x, y, _("Audio:"), LARGEFONT,  BC_WindowBase::get_resources()->audiovideo_color));
157                 x += 80;
158                 window->add_subwindow(aparams_button = new FormatAParams(mwindow, this, x, y));
159                 x += aparams_button->get_w() + 10;
160                 if(prompt_audio) 
161                 {
162                         window->add_subwindow(audio_switch = new FormatAudio(x, y, this, asset->audio_data));
163                 }
164                 x = init_x;
165                 y += aparams_button->get_h() + 20;
167 // Audio channels only used for recording.
168 //              if(prompt_audio_channels)
169 //              {
170 //                      window->add_subwindow(channels_title = new BC_Title(x, y, _("Number of audio channels to record:")));
171 //                      x += 260;
172 //                      window->add_subwindow(channels_button = new FormatChannels(x, y, this));
173 //                      x += channels_button->get_w() + 5;
174 //                      window->add_subwindow(channels_tumbler = new BC_ITumbler(channels_button, 1, MAXCHANNELS, x, y));
175 //                      y += channels_button->get_h() + 20;
176 //                      x = init_x;
177 //              }
179 //printf("FormatTools::create_objects 6\n");
180                 aparams_thread = new FormatAThread(this);
181         }
183 //printf("FormatTools::create_objects 7\n");
184         if(do_video)
185         {
187 //printf("FormatTools::create_objects 8\n");
188                 window->add_subwindow(video_title = new BC_Title(x, y, _("Video:"), LARGEFONT,  BC_WindowBase::get_resources()->audiovideo_color));
189                 x += 80;
190                 if(prompt_video_compression)
191                 {
192                         window->add_subwindow(vparams_button = new FormatVParams(mwindow, this, x, y));
193                         x += vparams_button->get_w() + 10;
194                 }
196 //printf("FormatTools::create_objects 9\n");
197                 if(prompt_video)
198                 {
199                         window->add_subwindow(video_switch = new FormatVideo(x, y, this, asset->video_data));
200                         y += video_switch->get_h();
201                 }
202                 else
203                 {
204                         y += vparams_button->get_h();
205                 }
207 //printf("FormatTools::create_objects 10\n");
208                 y += 10;
209                 vparams_thread = new FormatVThread(this);
210         }
212 //printf("FormatTools::create_objects 11\n");
214         x = init_x;
215         if(strategy)
216         {
217                 window->add_subwindow(multiple_files = new FormatMultiple(mwindow, x, y, strategy));
218                 y += multiple_files->get_h() + 10;
219         }
221 //printf("FormatTools::create_objects 12\n");
223         init_y = y;
224         return 0;
227 void FormatTools::update_driver(int driver)
229         this->video_driver = driver;
231         switch(driver)
232         {
233                 case CAPTURE_DVB:
234 // Just give the user information about how the stream is going to be
235 // stored but don't change the asset.
236 // Want to be able to revert to user settings.
237                         if(asset->format != FILE_MPEG)
238                         {
239                                 format_text->update(_("MPEG transport stream"));
240                                 asset->format = FILE_MPEG;
241                         }
242                         locked_compressor = 0;
243                         audio_switch->update(1);
244                         video_switch->update(1);
245                         break;
247                 case CAPTURE_IEC61883:
248                 case CAPTURE_FIREWIRE:
249                         if(asset->format != FILE_AVI &&
250                                 asset->format != FILE_MOV)
251                         {
252                                 format_text->update(MOV_NAME);
253                                 asset->format = FILE_MOV;
254                         }
255                         else
256                                 format_text->update(File::formattostr(asset->format));
257                         locked_compressor = QUICKTIME_DVSD;
258                         strcpy(asset->vcodec, QUICKTIME_DVSD);
259                         audio_switch->update(asset->audio_data);
260                         video_switch->update(asset->video_data);
261                         break;
263                 case CAPTURE_BUZ:
264                 case VIDEO4LINUX2JPEG:
265                         if(asset->format != FILE_AVI &&
266                                 asset->format != FILE_MOV)
267                         {
268                                 format_text->update(MOV_NAME);
269                                 asset->format = FILE_MOV;
270                         }
271                         else
272                                 format_text->update(File::formattostr(asset->format));
273                         locked_compressor = QUICKTIME_MJPA;
274                         audio_switch->update(asset->audio_data);
275                         video_switch->update(asset->video_data);
276                         break;
278                 default:
279                         format_text->update(File::formattostr(asset->format));
280                         locked_compressor = 0;
281                         audio_switch->update(asset->audio_data);
282                         video_switch->update(asset->video_data);
283                         break;
284         }
285         close_format_windows();
290 int FormatTools::handle_event()
292         return 0;
295 Asset* FormatTools::get_asset()
297         return asset;
300 void FormatTools::update_extension()
302         char *extension = File::get_tag(asset->format);
303         if(extension)
304         {
305                 char *ptr = strrchr(asset->path, '.');
306                 if(!ptr)
307                 {
308                         ptr = asset->path + strlen(asset->path);
309                         *ptr = '.';
310                 }
311                 ptr++;
312                 sprintf(ptr, extension);
314                 int character1 = ptr - asset->path;
315                 int character2 = ptr - asset->path + strlen(extension);
316                 *(asset->path + character2) = 0;
317                 if(path_textbox) 
318                 {
319                         path_textbox->update(asset->path);
320                         path_textbox->set_selection(character1, character2, character2);
321                 }
322         }
325 void FormatTools::update(Asset *asset, int *strategy)
327         this->asset = asset;
328         this->strategy = strategy;
330         if(path_textbox) 
331                 path_textbox->update(asset->path);
332         format_text->update(File::formattostr(plugindb, asset->format));
333         if(do_audio && audio_switch) audio_switch->update(asset->audio_data);
334         if(do_video && video_switch) video_switch->update(asset->video_data);
335         if(strategy)
336         {
337                 multiple_files->update(strategy);
338         }
339         close_format_windows();
342 void FormatTools::close_format_windows()
344         if(aparams_thread) aparams_thread->file->close_window();
345         if(vparams_thread) vparams_thread->file->close_window();
348 int FormatTools::get_w()
350         return w;
353 void FormatTools::reposition_window(int &init_x, int &init_y)
355         int x = init_x;
356         int y = init_y;
358         if(path_textbox) 
359         {
360                 path_textbox->reposition_window(x, y);
361                 x += 305;
362                 path_button->reposition_window(x, y);
363                 x -= 305;
364                 y += 35;
365         }
367         format_title->reposition_window(x, y);
368         x += 90;
369         format_text->reposition_window(x, y);
370         x += format_text->get_w();
371         format_button->reposition_window(x, y);
373         x = init_x;
374         y += format_button->get_h() + 10;
376         if(do_audio)
377         {
378                 audio_title->reposition_window(x, y);
379                 x += 80;
380                 aparams_button->reposition_window(x, y);
381                 x += aparams_button->get_w() + 10;
382                 if(prompt_audio) audio_switch->reposition_window(x, y);
384                 x = init_x;
385                 y += aparams_button->get_h() + 20;
386                 if(prompt_audio_channels)
387                 {
388                         channels_title->reposition_window(x, y);
389                         x += 260;
390                         channels_button->reposition_window(x, y);
391                         x += channels_button->get_w() + 5;
392                         channels_tumbler->reposition_window(x, y);
393                         y += channels_button->get_h() + 20;
394                         x = init_x;
395                 }
396         }
399         if(do_video)
400         {
401                 video_title->reposition_window(x, y);
402                 x += 80;
403                 if(prompt_video_compression)
404                 {
405                         vparams_button->reposition_window(x, y);
406                         x += vparams_button->get_w() + 10;
407                 }
409                 if(prompt_video)
410                 {
411                         video_switch->reposition_window(x, y);
412                         y += video_switch->get_h();
413                 }
414                 else
415                 {
416                         y += vparams_button->get_h();
417                 }
419                 y += 10;
420                 x = init_x;
421         }
423         if(strategy)
424         {
425                 multiple_files->reposition_window(x, y);
426                 y += multiple_files->get_h() + 10;
427         }
429         init_y = y;
433 int FormatTools::set_audio_options()
435 //      if(video_driver == CAPTURE_DVB)
436 //      {
437 //              return 0;
438 //      }
440         if(!aparams_thread->running())
441         {
442                 aparams_thread->start();
443         }
444         else
445         {
446                 aparams_thread->file->raise_window();
447         }
448         return 0;
451 int FormatTools::set_video_options()
453 //      if(video_driver == CAPTURE_DVB)
454 //      {
455 //              return 0;
456 //      }
458         if(!vparams_thread->running())
459         {
460                 vparams_thread->start();
461         }
462         else
463         {
464                 vparams_thread->file->raise_window();
465         }
467         return 0;
474 FormatAParams::FormatAParams(MWindow *mwindow, FormatTools *format, int x, int y)
475  : BC_Button(x, y, mwindow->theme->get_image_set("wrench"))
477         this->format = format;
478         set_tooltip(_("Configure audio compression"));
480 FormatAParams::~FormatAParams() 
483 int FormatAParams::handle_event() 
485         format->set_audio_options(); 
488 FormatVParams::FormatVParams(MWindow *mwindow, FormatTools *format, int x, int y)
489  : BC_Button(x, y, mwindow->theme->get_image_set("wrench"))
491         this->format = format; 
492         set_tooltip(_("Configure video compression"));
494 FormatVParams::~FormatVParams() 
497 int FormatVParams::handle_event() 
499         format->set_video_options(); 
503 FormatAThread::FormatAThread(FormatTools *format)
504  : Thread()
506         this->format = format; 
507         file = new File;
510 FormatAThread::~FormatAThread() 
512         delete file;
515 void FormatAThread::run()
517         file->get_options(format, 1, 0);
523 FormatVThread::FormatVThread(FormatTools *format)
524  : Thread()
526         this->format = format;
527         file = new File;
530 FormatVThread::~FormatVThread() 
532         delete file;
535 void FormatVThread::run()
537         file->get_options(format, 0, 1);
540 FormatPathText::FormatPathText(int x, int y, FormatTools *format)
541  : BC_TextBox(x, y, 300, 1, format->asset->path) 
543         this->format = format; 
545 FormatPathText::~FormatPathText() 
548 int FormatPathText::handle_event() 
550         strcpy(format->asset->path, get_text());
551         format->handle_event();
557 FormatAudio::FormatAudio(int x, int y, FormatTools *format, int default_)
558  : BC_CheckBox(x, 
559         y, 
560         default_, 
561         (char*)(format->recording ? _("Record audio tracks") : _("Render audio tracks")))
563         this->format = format; 
565 FormatAudio::~FormatAudio() {}
566 int FormatAudio::handle_event()
568         format->asset->audio_data = get_value();
572 FormatVideo::FormatVideo(int x, int y, FormatTools *format, int default_)
573  : BC_CheckBox(x, 
574         y, 
575         default_, 
576         (char*)(format->recording ? _("Record video tracks") : _("Render video tracks")))
578 this->format = format; 
580 FormatVideo::~FormatVideo() {}
581 int FormatVideo::handle_event()
583         format->asset->video_data = get_value();
589 FormatFormat::FormatFormat(int x, 
590         int y, 
591         FormatTools *format)
592  : FormatPopup(format->plugindb, 
593         x, 
594         y,
595         format->use_brender)
597         this->format = format; 
599 FormatFormat::~FormatFormat() 
602 int FormatFormat::handle_event()
604         if(get_selection(0, 0) >= 0)
605         {
606                 int new_format = File::strtoformat(format->plugindb, get_selection(0, 0)->get_text());
607                 if(new_format != format->asset->format)
608                 {
609                         // save the state of the old format
610                         format->asset->save_format_defaults
611                                 (format->mwindow->defaults);
613                         // close the configure window if open
614                         format->close_format_windows();
616                         // change to the new format
617                         format->asset->format = new_format;
619                         // load the state for the new format
620                         format->asset->load_format_defaults
621                                 (format->mwindow->defaults);
623                         // update the render window to match
624                         format->format_text->update(get_selection(0, 0)->get_text());
625                         format->update_extension();
626                         if (format->path_textbox)
627                                 format->path_textbox->update(format->asset->path);
628                         if (format->pipe_status)
629                                 format->pipe_status->set_status(format->asset);
630                         if (format->path_recent)
631                                 format->path_recent->load_items
632                                         (FILE_FORMAT_PREFIX(format->asset->format));
633                 }
634         }
635         return 1;
640 FormatChannels::FormatChannels(int x, int y, FormatTools *format)
641  : BC_TextBox(x, y, 100, 1, format->asset->channels) 
643         this->format = format; 
645 FormatChannels::~FormatChannels() 
648 int FormatChannels::handle_event() 
650         format->asset->channels = atol(get_text());
651         return 1;
654 FormatToTracks::FormatToTracks(int x, int y, int *output)
655  : BC_CheckBox(x, y, *output, _("Overwrite project with output"))
657         this->output = output; 
659 FormatToTracks::~FormatToTracks() 
662 int FormatToTracks::handle_event()
664         *output = get_value();
665         return 1;
669 FormatMultiple::FormatMultiple(MWindow *mwindow, int x, int y, int *output)
670  : BC_CheckBox(x, 
671         y, 
672         (*output == FILE_PER_LABEL) || (*output == FILE_PER_LABEL_FARM), 
673         _("Create new file at each label"))
675         this->output = output;
676         this->mwindow = mwindow;
678 FormatMultiple::~FormatMultiple() 
681 int FormatMultiple::handle_event()
683         if(get_value())
684         {
685                 if(mwindow->preferences->use_renderfarm)
686                         *output = FILE_PER_LABEL_FARM;
687                 else
688                         *output = FILE_PER_LABEL;
689         }
690         else
691         {
692                 if(mwindow->preferences->use_renderfarm)
693                         *output = SINGLE_PASS_FARM;
694                 else
695                         *output = SINGLE_PASS;
696         }
697         return 1;
700 void FormatMultiple::update(int *output)
702         this->output = output;
703         if(*output == FILE_PER_LABEL_FARM ||
704                 *output ==FILE_PER_LABEL)
705                 set_value(1);
706         else
707                 set_value(0);