r793: Small API addon, so plugins can 'see' camera and projector automation
[cinelerra_cv/mob.git] / cinelerra / plugindialog.C
blob82dba0db0f82ab1c725bb48c6381dcb62917ada5
1 #include "condition.h"
2 #include "edl.h"
3 #include "language.h"
4 #include "localsession.h"
5 #include "mainsession.h"
6 #include "mainundo.h"
7 #include "mwindow.h"
8 #include "mwindowgui.h"
9 #include "module.h"
10 #include "mutex.h"
11 #include "plugin.h"
12 #include "plugindialog.h"
13 #include "pluginserver.h"
14 #include "theme.h"
15 #include "track.h"
16 #include "tracks.h"
17 #include "transition.h"
20 PluginDialogThread::PluginDialogThread(MWindow *mwindow)
21  : Thread()
23         this->mwindow = mwindow;
24         window = 0;
25         plugin = 0;
26         Thread::set_synchronous(0);
27         window_lock = new Mutex("PluginDialogThread::window_lock");
28         completion = new Condition(1, "PluginDialogThread::completion");
31 PluginDialogThread::~PluginDialogThread()
33         if(window)
34         {
35                 window->set_done(1);
36                 completion->lock("PluginDialogThread::~PluginDialogThread");
37                 completion->unlock();
38         }
39         delete window_lock;
40         delete completion;
43 void PluginDialogThread::start_window(Track *track,
44         Plugin *plugin, 
45         char *title)
47         if(Thread::running())
48         {
49                 window_lock->lock("PluginDialogThread::start_window");
50                 if(window)
51                 {
52                         window->lock_window("PluginDialogThread::start_window");
53                         window->raise_window();
54                         window->flush();
55                         window->unlock_window();
56                 }
57                 window_lock->unlock();
58         }
59         else
60         {
61                 this->track = track;
62                 this->data_type = track->data_type;
63                 this->plugin = plugin;
65                 if(plugin)
66                 {
67                         plugin->calculate_title(plugin_title, 0);
68                         this->shared_location = plugin->shared_location;
69                         this->plugin_type = plugin->plugin_type;
70                 }
71                 else
72                 {
73                         this->plugin_title[0] = 0;
74                         this->shared_location.plugin = -1;
75                         this->shared_location.module = -1;
76                         this->plugin_type = PLUGIN_NONE;
77                 }
79                 strcpy(this->window_title, title);
80                 completion->lock("PluginDialogThread::start_window");
81                 Thread::start();
82         }
86 int PluginDialogThread::set_dialog(Transition *transition, char *title)
88         return 0;
91 void PluginDialogThread::run()
93         int result = 0;
95         int x = mwindow->gui->get_abs_cursor_x(1) - mwindow->session->plugindialog_w / 2;
96         int y = mwindow->gui->get_abs_cursor_y(1) - mwindow->session->plugindialog_h / 2;
98         window_lock->lock("PluginDialogThread::run 1"); 
99         window = new PluginDialog(mwindow, this, window_title, x, y);
100         window->create_objects();
101         window_lock->unlock();
103         result = window->run_window();
106         window_lock->lock("PluginDialogThread::run 2");
107         delete window;
108         window = 0;
109         window_lock->unlock();
111         completion->unlock();
113 // Done at closing
114         if(!result)
115         {
116                 if(plugin_type)
117                 {
118                         mwindow->gui->lock_window("PluginDialogThread::run 3");
121                         if(plugin)
122                         {
123                                 plugin->change_plugin(plugin_title,
124                                         &shared_location,
125                                         plugin_type);
126                         }
127                         else
128                         {
129                                 mwindow->insert_effect(plugin_title, 
130                                                                 &shared_location,
131                                                                 track,
132                                                                 0,
133                                                                 0,
134                                                                 0,
135                                                                 plugin_type);
136                         }
138                         
139                         mwindow->save_backup();
140                         mwindow->undo->update_undo(_("attach effect"), LOAD_EDITS | LOAD_PATCHES);
141                         mwindow->restart_brender();
142                         mwindow->update_plugin_states();
143                         mwindow->sync_parameters(CHANGE_EDL);
144                         mwindow->gui->update(1,
145                                 1,
146                                 0,
147                                 0,
148                                 1, 
149                                 0,
150                                 0);
152                         mwindow->gui->unlock_window();
153                 }
154         }
155         plugin = 0;
166 PluginDialog::PluginDialog(MWindow *mwindow, 
167         PluginDialogThread *thread, 
168         char *window_title,
169         int x,
170         int y)
171  : BC_Window(window_title, 
172         x,
173         y,
174         mwindow->session->plugindialog_w, 
175         mwindow->session->plugindialog_h, 
176         510, 
177         415,
178         1,
179         0,
180         1)
182         this->mwindow = mwindow;  
183         this->thread = thread;
184         standalone_attach = 0;
185         shared_attach = 0;
186         module_attach = 0;
187         standalone_change = 0;
188         shared_change = 0;
189         module_change = 0;
190         inoutthru = 0;
193 PluginDialog::~PluginDialog()
195         int i;
196         standalone_data.remove_all_objects();
197         
198         shared_data.remove_all_objects();
199         
200         module_data.remove_all_objects();
202         plugin_locations.remove_all_objects();
204         module_locations.remove_all_objects();
206 //      delete title;
207 //      delete detach;
208         delete standalone_list;
209         delete shared_list;
210         delete module_list;
211         if(standalone_attach) delete standalone_attach;
212         if(shared_attach) delete shared_attach;
213         if(module_attach) delete module_attach;
214         if(standalone_change) delete standalone_change;
215         if(shared_change) delete shared_change;
216         if(module_change) delete module_change;
217 //      delete in;
218 //      delete out;
221 int PluginDialog::create_objects()
223         int use_default = 1;
224         char string[BCTEXTLEN];
225         int module_number;
226         mwindow->theme->get_plugindialog_sizes();
228         if(thread->plugin)
229         {
230                 strcpy(string, thread->plugin->title);
231                 use_default = 1;
232         }
233         else
234         {
235 // no plugin
236                 sprintf(string, _("None"));
237         }
244 // GET A LIST OF ALL THE PLUGINS AVAILABLE
245         mwindow->create_plugindb(thread->data_type == TRACK_AUDIO, 
246                 thread->data_type == TRACK_VIDEO, 
247                 1, 
248                 0,
249                 0,
250                 plugindb);
252         mwindow->edl->get_shared_plugins(thread->track,
253                 &plugin_locations);
255         mwindow->edl->get_shared_tracks(thread->track,
256                 &module_locations);
265 // Construct listbox items
266         for(int i = 0; i < plugindb.total; i++)
267                 standalone_data.append(new BC_ListBoxItem(_(plugindb.values[i]->title)));
268         for(int i = 0; i < plugin_locations.total; i++)
269         {
270                 Track *track = mwindow->edl->tracks->number(plugin_locations.values[i]->module);
271                 char *track_title = track->title;
272                 int number = plugin_locations.values[i]->plugin;
273                 Plugin *plugin = track->get_current_plugin(mwindow->edl->local_session->get_selectionstart(1), 
274                         number, 
275                         PLAY_FORWARD,
276                         1,
277                         0);
278                 char *plugin_title = plugin->title;
279                 char string[BCTEXTLEN];
281                 sprintf(string, "%s: %s", track_title, _(plugin_title));
282                 shared_data.append(new BC_ListBoxItem(string));
283         }
284         for(int i = 0; i < module_locations.total; i++)
285         {
286                 Track *track = mwindow->edl->tracks->number(module_locations.values[i]->module);
287                 module_data.append(new BC_ListBoxItem(track->title));
288         }
294 // Create widgets
295         add_subwindow(standalone_title = new BC_Title(mwindow->theme->plugindialog_new_x, 
296                 mwindow->theme->plugindialog_new_y - 20, 
297                 _("Plugins:")));
298         add_subwindow(standalone_list = new PluginDialogNew(this, 
299                 &standalone_data, 
300                 mwindow->theme->plugindialog_new_x, 
301                 mwindow->theme->plugindialog_new_y,
302                 mwindow->theme->plugindialog_new_w,
303                 mwindow->theme->plugindialog_new_h));
305         if(thread->plugin)
306                 add_subwindow(standalone_change = new PluginDialogChangeNew(mwindow,
307                         this,
308                         mwindow->theme->plugindialog_newattach_x,
309                         mwindow->theme->plugindialog_newattach_y));
310         else
311                 add_subwindow(standalone_attach = new PluginDialogAttachNew(mwindow, 
312                         this, 
313                         mwindow->theme->plugindialog_newattach_x, 
314                         mwindow->theme->plugindialog_newattach_y));
323         add_subwindow(shared_title = new BC_Title(mwindow->theme->plugindialog_shared_x, 
324                 mwindow->theme->plugindialog_shared_y - 20, 
325                 _("Shared effects:")));
326         add_subwindow(shared_list = new PluginDialogShared(this, 
327                 &shared_data, 
328                 mwindow->theme->plugindialog_shared_x, 
329                 mwindow->theme->plugindialog_shared_y,
330                 mwindow->theme->plugindialog_shared_w,
331                 mwindow->theme->plugindialog_shared_h));
332         if(thread->plugin)
333       add_subwindow(shared_change = new PluginDialogChangeShared(mwindow,
334          this,
335          mwindow->theme->plugindialog_sharedattach_x,
336          mwindow->theme->plugindialog_sharedattach_y));
337    else
338                 add_subwindow(shared_attach = new PluginDialogAttachShared(mwindow, 
339                         this, 
340                         mwindow->theme->plugindialog_sharedattach_x, 
341                         mwindow->theme->plugindialog_sharedattach_y));
351         add_subwindow(module_title = new BC_Title(mwindow->theme->plugindialog_module_x, 
352                 mwindow->theme->plugindialog_module_y - 20, 
353                 _("Shared tracks:")));
354         add_subwindow(module_list = new PluginDialogModules(this, 
355                 &module_data, 
356                 mwindow->theme->plugindialog_module_x, 
357                 mwindow->theme->plugindialog_module_y,
358                 mwindow->theme->plugindialog_module_w,
359                 mwindow->theme->plugindialog_module_h));
360         if(thread->plugin)
361       add_subwindow(module_change = new PluginDialogChangeModule(mwindow,
362          this,
363          mwindow->theme->plugindialog_moduleattach_x,
364          mwindow->theme->plugindialog_moduleattach_y));
365    else
366                 add_subwindow(module_attach = new PluginDialogAttachModule(mwindow, 
367                         this, 
368                         mwindow->theme->plugindialog_moduleattach_x, 
369                         mwindow->theme->plugindialog_moduleattach_y));
377         add_subwindow(new BC_CancelButton(this));
379         selected_available = -1;
380         selected_shared = -1;
381         selected_modules = -1;
382         
383         show_window();
384         flush();
385         return 0;
388 int PluginDialog::resize_event(int w, int h)
390         mwindow->session->plugindialog_w = w;
391         mwindow->session->plugindialog_h = h;
392         mwindow->theme->get_plugindialog_sizes();
395         standalone_title->reposition_window(mwindow->theme->plugindialog_new_x, 
396                 mwindow->theme->plugindialog_new_y - 20);
397         standalone_list->reposition_window(mwindow->theme->plugindialog_new_x, 
398                 mwindow->theme->plugindialog_new_y,
399                 mwindow->theme->plugindialog_new_w,
400                 mwindow->theme->plugindialog_new_h);
401         if(standalone_attach)
402                 standalone_attach->reposition_window(mwindow->theme->plugindialog_newattach_x, 
403                         mwindow->theme->plugindialog_newattach_y);
404         else
405                 standalone_change->reposition_window(mwindow->theme->plugindialog_newattach_x,
406                         mwindow->theme->plugindialog_newattach_y);
412         shared_title->reposition_window(mwindow->theme->plugindialog_shared_x, 
413                 mwindow->theme->plugindialog_shared_y - 20);
414         shared_list->reposition_window(mwindow->theme->plugindialog_shared_x, 
415                 mwindow->theme->plugindialog_shared_y,
416                 mwindow->theme->plugindialog_shared_w,
417                 mwindow->theme->plugindialog_shared_h);
418         if(shared_attach)
419                 shared_attach->reposition_window(mwindow->theme->plugindialog_sharedattach_x, 
420                         mwindow->theme->plugindialog_sharedattach_y);
421         else
422                 shared_change->reposition_window(mwindow->theme->plugindialog_sharedattach_x,
423                         mwindow->theme->plugindialog_sharedattach_y);
429         module_title->reposition_window(mwindow->theme->plugindialog_module_x, 
430                 mwindow->theme->plugindialog_module_y - 20);
431         module_list->reposition_window(mwindow->theme->plugindialog_module_x, 
432                 mwindow->theme->plugindialog_module_y,
433                 mwindow->theme->plugindialog_module_w,
434                 mwindow->theme->plugindialog_module_h);
435         if(module_attach)
436                 module_attach->reposition_window(mwindow->theme->plugindialog_moduleattach_x, 
437                         mwindow->theme->plugindialog_moduleattach_y);
438         else
439                 module_change->reposition_window(mwindow->theme->plugindialog_moduleattach_x,
440                         mwindow->theme->plugindialog_moduleattach_y);
441         flush();
444 int PluginDialog::attach_new(int number)
446         if(number > -1 && number < standalone_data.total) 
447         {
448                 strcpy(thread->plugin_title, plugindb.values[number]->title);
449                 thread->plugin_type = PLUGIN_STANDALONE;         // type is plugin
450         }
451         return 0;
454 int PluginDialog::attach_shared(int number)
456         if(number > -1 && number < shared_data.total) 
457         {
458                 thread->plugin_type = PLUGIN_SHAREDPLUGIN;         // type is shared plugin
459                 thread->shared_location = *(plugin_locations.values[number]); // copy location
460         }
461         return 0;
464 int PluginDialog::attach_module(int number)
466         if(number > -1 && number < module_data.total) 
467         {
468 //              title->update(module_data.values[number]->get_text());
469                 thread->plugin_type = PLUGIN_SHAREDMODULE;         // type is module
470                 thread->shared_location = *(module_locations.values[number]); // copy location
471         }
472         return 0;
475 int PluginDialog::save_settings()
486 PluginDialogTextBox::PluginDialogTextBox(PluginDialog *dialog, char *text, int x, int y)
487  : BC_TextBox(x, y, 200, 1, text) 
489         this->dialog = dialog;
491 PluginDialogTextBox::~PluginDialogTextBox() 
492 { }
493 int PluginDialogTextBox::handle_event() 
494 { }
496 PluginDialogDetach::PluginDialogDetach(MWindow *mwindow, PluginDialog *dialog, int x, int y)
497  : BC_GenericButton(x, y, _("Detach")) 
499         this->dialog = dialog; 
501 PluginDialogDetach::~PluginDialogDetach() 
502 { }
503 int PluginDialogDetach::handle_event() 
505 //      dialog->title->update(_("None"));
506         dialog->thread->plugin_type = 0;         // type is none
507         dialog->thread->plugin_title[0] = 0;
508         return 1;
524 PluginDialogNew::PluginDialogNew(PluginDialog *dialog, 
525         ArrayList<BC_ListBoxItem*> *standalone_data, 
526         int x,
527         int y,
528         int w,
529         int h)
530  : BC_ListBox(x, 
531         y, 
532         w, 
533         h, 
534         LISTBOX_TEXT,
535         standalone_data) 
537         this->dialog = dialog; 
539 PluginDialogNew::~PluginDialogNew() { }
540 int PluginDialogNew::handle_event() 
542         dialog->attach_new(get_selection_number(0, 0)); 
543         deactivate();
545         set_done(0); 
546         return 1;
548 int PluginDialogNew::selection_changed()
550         dialog->selected_available = get_selection_number(0, 0);
551         return 1;
554 PluginDialogAttachNew::PluginDialogAttachNew(MWindow *mwindow, PluginDialog *dialog, int x, int y)
555  : BC_GenericButton(x, y, _("Attach")) 
557         this->dialog = dialog; 
559 PluginDialogAttachNew::~PluginDialogAttachNew() 
562 int PluginDialogAttachNew::handle_event() 
564         dialog->attach_new(dialog->selected_available); 
565         set_done(0);
566         return 1;
569 PluginDialogChangeNew::PluginDialogChangeNew(MWindow *mwindow, PluginDialog *dialog, int x, int y)
570  : BC_GenericButton(x, y, _("Change"))
572    this->dialog = dialog;
574 PluginDialogChangeNew::~PluginDialogChangeNew()
577 int PluginDialogChangeNew::handle_event() 
578 {  
579    dialog->attach_new(dialog->selected_available);
580    set_done(0);
581    return 1;
593 PluginDialogShared::PluginDialogShared(PluginDialog *dialog, 
594         ArrayList<BC_ListBoxItem*> *shared_data, 
595         int x,
596         int y,
597         int w,
598         int h)
599  : BC_ListBox(x, 
600         y, 
601         w, 
602         h, 
603         LISTBOX_TEXT, 
604         shared_data) 
606         this->dialog = dialog; 
608 PluginDialogShared::~PluginDialogShared() { }
609 int PluginDialogShared::handle_event()
611         dialog->attach_shared(get_selection_number(0, 0)); 
612         deactivate();
613         set_done(0); 
614         return 1;
616 int PluginDialogShared::selection_changed()
618         dialog->selected_shared = get_selection_number(0, 0);
619         return 1;
622 PluginDialogAttachShared::PluginDialogAttachShared(MWindow *mwindow, 
623         PluginDialog *dialog, 
624         int x,
625         int y)
626  : BC_GenericButton(x, y, _("Attach")) 
628         this->dialog = dialog; 
630 PluginDialogAttachShared::~PluginDialogAttachShared() { }
631 int PluginDialogAttachShared::handle_event() 
633         dialog->attach_shared(dialog->selected_shared); 
634         set_done(0);
635         return 1;
638 PluginDialogChangeShared::PluginDialogChangeShared(MWindow *mwindow,
639    PluginDialog *dialog,
640    int x,
641    int y)
642  : BC_GenericButton(x, y, _("Change"))
644    this->dialog = dialog;
646 PluginDialogChangeShared::~PluginDialogChangeShared() { }
647 int PluginDialogChangeShared::handle_event()
649    dialog->attach_shared(dialog->selected_shared);
650    set_done(0);
651    return 1;
666 PluginDialogModules::PluginDialogModules(PluginDialog *dialog, 
667         ArrayList<BC_ListBoxItem*> *module_data, 
668         int x, 
669         int y,
670         int w,
671         int h)
672  : BC_ListBox(x, 
673         y, 
674         w, 
675         h, 
676         LISTBOX_TEXT, 
677         module_data) 
679         this->dialog = dialog; 
681 PluginDialogModules::~PluginDialogModules() { }
682 int PluginDialogModules::handle_event()
684         dialog->attach_module(get_selection_number(0, 0)); 
685         deactivate();
687         set_done(0); 
688         return 1;
690 int PluginDialogModules::selection_changed()
692         dialog->selected_modules = get_selection_number(0, 0);
693         return 1;
697 PluginDialogAttachModule::PluginDialogAttachModule(MWindow *mwindow, 
698         PluginDialog *dialog, 
699         int x, 
700         int y)
701  : BC_GenericButton(x, y, _("Attach")) 
703         this->dialog = dialog; 
705 PluginDialogAttachModule::~PluginDialogAttachModule() { }
706 int PluginDialogAttachModule::handle_event() 
708         dialog->attach_module(dialog->selected_modules);
709         set_done(0);
710         return 1;
713 PluginDialogChangeModule::PluginDialogChangeModule(MWindow *mwindow,
714    PluginDialog *dialog,
715    int x,
716    int y)
717  : BC_GenericButton(x, y, _("Change"))
719    this->dialog = dialog;
721 PluginDialogChangeModule::~PluginDialogChangeModule() { }
722 int PluginDialogChangeModule::handle_event()
724    dialog->attach_module(dialog->selected_modules);
725    set_done(0);
726    return 1;
743 PluginDialogIn::PluginDialogIn(PluginDialog *dialog, int setting, int x, int y)
744  : BC_CheckBox(x, y, setting, _("Send")) 
746         this->dialog = dialog; 
748 PluginDialogIn::~PluginDialogIn() { }
749 int PluginDialogIn::handle_event() 
751 //      dialog->thread->in = get_value();
752         return 1;
754 int PluginDialogIn::button_press()
756         dialog->inoutthru = 1;
757         dialog->new_value = get_value();
758         return 1;
760 int PluginDialogIn::button_release()
762         if(dialog->inoutthru) dialog->inoutthru = 0;
764 int PluginDialogIn::cursor_moved_over()
766         if(dialog->inoutthru && get_value() != dialog->new_value)
767         {
768                 update(dialog->new_value);
769         }
772 PluginDialogOut::PluginDialogOut(PluginDialog *dialog, int setting, int x, int y)
773  : BC_CheckBox(x, y, setting, _("Receive")) 
775         this->dialog = dialog; 
777 PluginDialogOut::~PluginDialogOut() { }
778 int PluginDialogOut::handle_event() 
780 //      dialog->thread->out = get_value();
781         return 1;
783 int PluginDialogOut::button_press()
785         dialog->inoutthru = 1;
786         dialog->new_value = get_value();
787         return 1;
789 int PluginDialogOut::button_release()
791         if(dialog->inoutthru) dialog->inoutthru = 0;
793 int PluginDialogOut::cursor_moved_over()
795         if(dialog->inoutthru && get_value() != dialog->new_value)
796         {
797                 update(dialog->new_value);
798         }
801 PluginDialogThru::PluginDialogThru(PluginDialog *dialog, int setting)
802  : BC_CheckBox(300, 350, setting, _("Thru")) 
804         this->dialog = dialog; 
806 PluginDialogThru::~PluginDialogThru() { }
807 int PluginDialogThru::handle_event() { }
808 int PluginDialogThru::button_press()
810         dialog->inoutthru = 1;
811         dialog->new_value = get_value();
812         return 1;
814 int PluginDialogThru::button_release()
816         if(dialog->inoutthru) dialog->inoutthru = 0;
818 int PluginDialogThru::cursor_moved_over()
820         if(dialog->inoutthru && get_value() != dialog->new_value)
821         {
822                 update(dialog->new_value);
823         }