4 * Copyright (C) 2017 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bcsignals.h"
26 #include "filesystem.h"
28 #include "keyframes.h"
29 #include "keyframegui.h"
32 #include "localsession.h"
33 #include "mainsession.h"
36 #include "mwindowgui.h"
38 #include "preferences.h"
41 #include "trackcanvas.h"
45 KeyFrameThread::KeyFrameThread(MWindow *mwindow)
48 this->mwindow = mwindow;
51 keyframe_data = new ArrayList<BC_ListBoxItem*>[KEYFRAME_COLUMNS];
56 column_titles[0] = (char*)"Parameter";
57 column_titles[1] = (char*)"Value";
60 presets_data = new ArrayList<BC_ListBoxItem*>;
61 presets_db = new PresetsDB;
64 KeyFrameThread::~KeyFrameThread()
66 for( int i=0; i<KEYFRAME_COLUMNS; ++i )
67 keyframe_data[i].remove_all_objects();
68 delete [] keyframe_data;
69 presets_data->remove_all_objects();
71 is_factories.remove_all();
72 preset_titles.remove_all_objects();
77 void KeyFrameThread::update_values()
79 // Get the current selection before deleting the tables
81 for( int i=0; i<keyframe_data[0].size(); ++i ) {
82 if( keyframe_data[0].get(i)->get_selected() ) {
88 for( int i=0; i<KEYFRAME_COLUMNS; ++i )
89 keyframe_data[i].remove_all_objects();
92 // Must lock main window to read keyframe
93 mwindow->gui->lock_window("KeyFrameThread::update_values");
94 if( !plugin || !mwindow->edl->tracks->plugin_exists(plugin) ) {
95 mwindow->gui->unlock_window();
99 KeyFrame *keyframe = 0;
100 if( this->keyframe && plugin->keyframe_exists(this->keyframe) ) {
101 // If user edited a specific keyframe, use it.
102 keyframe = this->keyframe;
105 if( plugin->track ) {
106 // Use currently highlighted keyframe
107 keyframe = plugin->get_prev_keyframe(
108 plugin->track->to_units(
109 mwindow->edl->local_session->get_selectionstart(1), 0),
115 char *text = 0, *data = 0;
116 keyframe->get_contents(&hash, &text, &data);
118 for( int i=0; i<hash.size(); ++i ) {
119 keyframe_data[0].append(new BC_ListBoxItem(hash.get_key(i)));
120 keyframe_data[1].append(new BC_ListBoxItem(hash.get_value(i)));
122 keyframe_data[0].append(new BC_ListBoxItem((char*)"TEXT"));
123 keyframe_data[1].append(new BC_ListBoxItem(text));
124 keyframe_data[0].append(new BC_ListBoxItem((char*)"DATA"));
125 keyframe_data[1].append(new BC_ListBoxItem(data));
131 column_width[0] = mwindow->session->keyframedialog_column1;
132 column_width[1] = mwindow->session->keyframedialog_column2;
133 if( selection >= 0 && selection < keyframe_data[0].size() ) {
134 for( int i=0; i<KEYFRAME_COLUMNS; ++i )
135 keyframe_data[i].get(selection)->set_selected(1);
137 mwindow->gui->unlock_window();
142 void KeyFrameThread::start_window(Plugin *plugin, KeyFrame *keyframe)
145 if( !BC_DialogThread::is_running() ) {
146 if( !mwindow->edl->tracks->plugin_exists(plugin) ) return;
147 this->keyframe = keyframe;
148 this->plugin = plugin;
149 plugin->calculate_title(plugin_title, 0);
150 sprintf(window_title, PROGRAM_NAME ": %s Keyframe", plugin_title);
152 // Load all the presets from disk
153 char path[BCTEXTLEN];
155 // system wide presets
156 sprintf(path, "%s/%s", File::get_cindat_path(), FACTORY_FILE);
157 fs.complete_path(path);
158 presets_db->load_from_file(path, 1, 1);
160 sprintf(path, "%s/%s", File::get_config_path(), PRESETS_FILE);
161 fs.complete_path(path);
162 presets_db->load_from_file(path, 0, 0);
164 calculate_preset_list();
169 mwindow->gui->unlock_window();
170 BC_DialogThread::start();
171 mwindow->gui->lock_window("KeyFrameThread::start_window");
174 BC_DialogThread::start();
178 BC_Window* KeyFrameThread::new_gui()
180 mwindow->gui->lock_window("KeyFrameThread::new_gui");
182 int x = mwindow->gui->get_abs_cursor_x(0) -
183 mwindow->session->plugindialog_w / 2;
184 int y = mwindow->gui->get_abs_cursor_y(0) -
185 mwindow->session->plugindialog_h / 2;
187 KeyFrameWindow *window = new KeyFrameWindow(mwindow,
193 window->create_objects();
196 mwindow->gui->unlock_window();
201 void KeyFrameThread::handle_done_event(int result)
205 apply_preset(preset_text, is_factory);
209 void KeyFrameThread::handle_close_event(int result)
215 void KeyFrameThread::close_window()
217 lock_window("KeyFrameThread::close_window");
219 get_gui()->lock_window("KeyFrameThread::close_window");
220 get_gui()->set_done(1);
221 get_gui()->unlock_window();
228 void KeyFrameThread::calculate_preset_list()
230 presets_data->remove_all_objects();
231 is_factories.remove_all();
232 preset_titles.remove_all_objects();
233 int total_presets = presets_db->get_total_presets(plugin_title, 0);
236 presets_db->sort(plugin_title);
238 for( int i=0; i<total_presets; ++i ) {
239 char text[BCTEXTLEN];
240 char *orig_title = presets_db->get_preset_title( plugin_title, i);
241 if( !orig_title ) continue;
242 int is_factory = presets_db->get_is_factory(plugin_title, i);
243 sprintf(text, "%s%s", is_factory ? "*" : "", orig_title);
244 presets_data->append(new BC_ListBoxItem(text));
246 preset_titles.append(strdup(orig_title));
247 is_factories.append(is_factory);
252 void KeyFrameThread::update_gui(int update_value_text)
255 if( BC_DialogThread::is_running() ) {
256 mwindow->gui->lock_window("KeyFrameThread::update_gui");
258 mwindow->gui->unlock_window();
260 lock_window("KeyFrameThread::update_gui");
261 KeyFrameWindow *window = (KeyFrameWindow*)get_gui();
263 window->lock_window("KeyFrameThread::update_gui");
264 window->keyframe_list->update(keyframe_data,
268 window->keyframe_list->get_xposition(),
269 window->keyframe_list->get_yposition(),
270 window->keyframe_list->get_highlighted_item());
271 if( update_value_text &&
272 window->keyframe_list->get_selection_number(0, 0) >= 0 &&
273 window->keyframe_list->get_selection_number(0, 0) < keyframe_data[1].size() ) {
274 window->value_text->update(
275 keyframe_data[1].get(window->keyframe_list->get_selection_number(0, 0))->get_text());
277 window->unlock_window();
284 void KeyFrameThread::save_preset(const char *title, int is_factory)
286 get_gui()->unlock_window();
287 mwindow->gui->lock_window("KeyFrameThread::save_preset");
289 // Test EDL for plugin existence
290 if( !mwindow->edl->tracks->plugin_exists(plugin) ) {
291 mwindow->gui->unlock_window();
292 get_gui()->lock_window("KeyFrameThread::save_preset 2");
296 // Get current plugin keyframe
297 EDL *edl = mwindow->edl;
298 Track *track = plugin->track;
299 KeyFrame *keyframe = plugin->get_prev_keyframe(
300 track->to_units(edl->local_session->get_selectionstart(1), 0),
304 presets_db->save_preset(plugin_title, title, keyframe->get_data());
306 mwindow->gui->unlock_window();
307 get_gui()->lock_window("KeyFrameThread::save_preset 2");
310 calculate_preset_list();
311 ((KeyFrameWindow*)get_gui())->preset_list->update(presets_data,
315 void KeyFrameThread::delete_preset(const char *title, int is_factory)
317 get_gui()->unlock_window();
318 mwindow->gui->lock_window("KeyFrameThread::save_preset");
320 // Test EDL for plugin existence
321 if( !mwindow->edl->tracks->plugin_exists(plugin) ) {
322 mwindow->gui->unlock_window();
323 get_gui()->lock_window("KeyFrameThread::delete_preset 1");
327 presets_db->delete_preset(plugin_title, title, is_factory);
329 mwindow->gui->unlock_window();
330 get_gui()->lock_window("KeyFrameThread::delete_preset 2");
333 calculate_preset_list();
334 ((KeyFrameWindow*)get_gui())->preset_list->update(presets_data,
339 void KeyFrameThread::apply_preset(const char *title, int is_factory)
341 if( presets_db->preset_exists(plugin_title, title, is_factory) ) {
342 get_gui()->unlock_window();
343 mwindow->gui->lock_window("KeyFrameThread::apply_preset");
345 // Test EDL for plugin existence
346 if( !mwindow->edl->tracks->plugin_exists(plugin) ) {
347 mwindow->gui->unlock_window();
348 get_gui()->lock_window("KeyFrameThread::apply_preset 1");
352 mwindow->undo->update_undo_before();
354 #ifdef USE_KEYFRAME_SPANNING
356 presets_db->load_preset(plugin_title, title, &keyframe, is_factory);
357 plugin->keyframes->update_parameter(&keyframe);
359 KeyFrame *keyframe = plugin->get_keyframe();
360 presets_db->load_preset(plugin_title, title, keyframe, is_factory);
362 mwindow->save_backup();
363 mwindow->undo->update_undo_after(_("apply preset"), LOAD_AUTOMATION);
365 mwindow->update_plugin_guis(0);
366 mwindow->gui->draw_overlays(1);
367 mwindow->sync_parameters(CHANGE_PARAMS);
371 mwindow->gui->unlock_window();
372 get_gui()->lock_window("KeyFrameThread::apply_preset");
377 void KeyFrameThread::apply_value()
379 const char *text = 0, *data = 0;
381 KeyFrameWindow *window = (KeyFrameWindow*)get_gui();
382 int selection = window->keyframe_list->get_selection_number(0, 0);
383 //printf("KeyFrameThread::apply_value %d %d\n", __LINE__, selection);
384 if( selection < 0 ) return;
386 if( selection == keyframe_data[0].size() - 2 )
387 text = window->value_text->get_text();
388 else if( selection == keyframe_data[0].size() - 1 )
389 data = window->value_text->get_text();
391 const char *key = keyframe_data[0].get(selection)->get_text();
392 const char *value = window->value_text->get_text();
393 hash = new BC_Hash();
394 hash->update(key, value);
397 get_gui()->unlock_window();
398 mwindow->gui->lock_window("KeyFrameThread::apply_value");
399 if( plugin && mwindow->edl->tracks->plugin_exists(plugin) ) {
400 mwindow->undo->update_undo_before();
401 if( mwindow->session->keyframedialog_all ) {
402 // Search for all keyframes in selection but don't create a new one.
403 Track *track = plugin->track;
404 int64_t start = track->to_units(mwindow->edl->local_session->get_selectionstart(0), 0);
405 int64_t end = track->to_units(mwindow->edl->local_session->get_selectionend(0), 0);
407 KeyFrame *current = (KeyFrame*)plugin->keyframes->last;
408 for( ; current; current=(KeyFrame*)PREVIOUS ) {
410 if( current && current->position < end ) {
411 current->update_parameter(hash, text, data);
412 // Stop at beginning of range
413 if( current->position <= start ) break;
418 current = (KeyFrame*)plugin->keyframes->default_auto;
419 current->update_parameter(hash, text, data);
423 // Create new keyframe if enabled
424 KeyFrame *keyframe = plugin->get_keyframe();
425 keyframe->update_parameter(hash, text, data);
429 printf("KeyFrameThread::apply_value %d: plugin doesn't exist\n", __LINE__);
432 mwindow->save_backup();
433 mwindow->undo->update_undo_after(_("edit keyframe"), LOAD_AUTOMATION);
435 mwindow->update_plugin_guis(0);
436 mwindow->gui->draw_overlays(1);
437 mwindow->sync_parameters(CHANGE_PARAMS);
438 mwindow->gui->unlock_window();
442 get_gui()->lock_window("KeyFrameThread::apply_value");
449 KeyFrameWindow::KeyFrameWindow(MWindow *mwindow,
450 KeyFrameThread *thread,
454 : BC_Window(title_string,
457 mwindow->session->keyframedialog_w,
458 mwindow->session->keyframedialog_h,
465 this->mwindow = mwindow;
466 this->thread = thread;
469 void KeyFrameWindow::create_objects()
471 Theme *theme = mwindow->theme;
473 theme->get_keyframedialog_sizes(this);
474 thread->column_width[0] = mwindow->session->keyframedialog_column1;
475 thread->column_width[1] = mwindow->session->keyframedialog_column2;
476 lock_window("KeyFrameWindow::create_objects");
481 add_subwindow(title1 = new BC_Title(theme->keyframe_list_x,
482 theme->keyframe_list_y -
483 BC_Title::calculate_h(this, (char*)"Py", LARGEFONT) -
484 theme->widget_border,
485 _("Keyframe parameters:"),
487 add_subwindow(keyframe_list = new KeyFrameList(thread,
489 theme->keyframe_list_x,
490 theme->keyframe_list_y,
491 theme->keyframe_list_w,
492 theme->keyframe_list_h));
493 // add_subwindow(title2 = new BC_Title(theme->keyframe_text_x,
494 // theme->keyframe_text_y - BC_Title::calculate_h(this, "P") - theme->widget_border,
495 // _("Global Text:")));
496 // add_subwindow(keyframe_text = new KeyFrameText(thread,
498 // theme->keyframe_text_x,
499 // theme->keyframe_text_y,
500 // theme->keyframe_text_w));
501 add_subwindow(title3 = new BC_Title(theme->keyframe_value_x,
502 theme->keyframe_value_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border,
504 add_subwindow(value_text = new KeyFrameValue(thread,
506 theme->keyframe_value_x,
507 theme->keyframe_value_y,
508 theme->keyframe_value_w));
509 add_subwindow(all_toggle = new KeyFrameAll(thread,
511 theme->keyframe_all_x,
512 theme->keyframe_all_y));
518 add_subwindow(title4 = new BC_Title(theme->presets_list_x,
519 theme->presets_list_y -
520 BC_Title::calculate_h(this, (char*)"Py", LARGEFONT) -
521 theme->widget_border,
524 add_subwindow(preset_list = new KeyFramePresetsList(thread,
526 theme->presets_list_x,
527 theme->presets_list_y,
528 theme->presets_list_w,
529 theme->presets_list_h));
530 add_subwindow(title5 = new BC_Title(theme->presets_text_x,
531 theme->presets_text_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border,
532 _("Preset title:")));
533 add_subwindow(preset_text = new KeyFramePresetsText(thread,
535 theme->presets_text_x,
536 theme->presets_text_y,
537 theme->presets_text_w));
538 add_subwindow(delete_preset = new KeyFramePresetsDelete(thread,
540 theme->presets_delete_x,
541 theme->presets_delete_y));
542 add_subwindow(save_preset = new KeyFramePresetsSave(thread,
544 theme->presets_save_x,
545 theme->presets_save_y));
546 add_subwindow(apply_preset = new KeyFramePresetsApply(thread,
548 theme->presets_apply_x,
549 theme->presets_apply_y));
554 add_subwindow(new KeyFramePresetsOK(thread, this));
555 add_subwindow(new BC_CancelButton(this));
561 // called when going in & out of a factory preset
562 void KeyFrameWindow::update_editing()
564 if( thread->is_factory ) {
565 delete_preset->disable();
566 save_preset->disable();
569 delete_preset->enable();
570 save_preset->enable();
576 int KeyFrameWindow::resize_event(int w, int h)
578 Theme *theme = mwindow->theme;
579 mwindow->session->keyframedialog_w = w;
580 mwindow->session->keyframedialog_h = h;
581 theme->get_keyframedialog_sizes(this);
585 title1->reposition_window(theme->keyframe_list_x,
586 theme->keyframe_list_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
587 // title2->reposition_window(theme->keyframe_text_x,
588 // theme->keyframe_text_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
589 title3->reposition_window(theme->keyframe_value_x,
590 theme->keyframe_value_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
591 keyframe_list->reposition_window(theme->keyframe_list_x,
592 theme->keyframe_list_y,
593 theme->keyframe_list_w,
594 theme->keyframe_list_h);
595 // text->reposition_window(theme->keyframe_text_x,
596 // theme->keyframe_text_y,
597 // theme->keyframe_text_w);
598 value_text->reposition_window(theme->keyframe_value_x,
599 theme->keyframe_value_y,
600 theme->keyframe_value_w);
601 all_toggle->reposition_window(theme->keyframe_all_x,
602 theme->keyframe_all_y);
604 #endif // EDIT_KEYFRAME
609 title4->reposition_window(theme->presets_list_x,
610 theme->presets_list_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
611 title5->reposition_window(theme->presets_text_x,
612 theme->presets_text_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
613 preset_list->reposition_window(theme->presets_list_x,
614 theme->presets_list_y,
615 theme->presets_list_w,
616 theme->presets_list_h);
617 preset_text->reposition_window(theme->presets_text_x,
618 theme->presets_text_y,
619 theme->presets_text_w);
620 delete_preset->reposition_window(theme->presets_delete_x,
621 theme->presets_delete_y);
622 save_preset->reposition_window(theme->presets_save_x,
623 theme->presets_save_y);
624 apply_preset->reposition_window(theme->presets_apply_x,
625 theme->presets_apply_y);
636 KeyFrameList::KeyFrameList(KeyFrameThread *thread,
637 KeyFrameWindow *window,
647 thread->keyframe_data,
648 thread->column_titles,
649 thread->column_width,
652 this->thread = thread;
653 this->window = window;
656 int KeyFrameList::selection_changed()
658 window->value_text->update(
659 thread->keyframe_data[1].get(get_selection_number(0, 0))->get_text());
663 int KeyFrameList::handle_event()
669 int KeyFrameList::column_resize_event()
671 thread->mwindow->session->keyframedialog_column1 = get_column_width(0);
672 thread->mwindow->session->keyframedialog_column2 = get_column_width(1);
679 // KeyFrameText::KeyFrameText(KeyFrameThread *thread,
680 // KeyFrameWindow *window,
690 // this->thread = thread;
691 // this->window = window;
694 // int KeyFrameText::handle_event()
701 KeyFrameValue::KeyFrameValue(KeyFrameThread *thread,
702 KeyFrameWindow *window,
712 this->thread = thread;
713 this->window = window;
716 int KeyFrameValue::handle_event()
718 thread->apply_value();
726 KeyFrameAll::KeyFrameAll(KeyFrameThread *thread,
727 KeyFrameWindow *window,
732 thread->mwindow->session->keyframedialog_all,
733 _("Apply to all selected keyframes"))
735 this->thread = thread;
736 this->window = window;
739 int KeyFrameAll::handle_event()
741 thread->mwindow->session->keyframedialog_all = get_value();
745 #endif // EDIT_KEYFRAME
756 KeyFramePresetsList::KeyFramePresetsList(KeyFrameThread *thread,
757 KeyFrameWindow *window,
767 thread->presets_data)
769 this->thread = thread;
770 this->window = window;
773 int KeyFramePresetsList::selection_changed()
775 int number = get_selection_number(0, 0);
777 strcpy(thread->preset_text, thread->preset_titles.get(number));
778 thread->is_factory = thread->is_factories.get(number);
779 // show title without factory symbol in the textbox
780 window->preset_text->update(
781 thread->presets_data->get(number)->get_text());
782 window->update_editing();
788 int KeyFramePresetsList::handle_event()
790 thread->apply_preset(thread->preset_text, thread->is_factory);
796 KeyFramePresetsText::KeyFramePresetsText(KeyFrameThread *thread,
797 KeyFrameWindow *window, int x, int y, int w)
798 : BC_TextBox(x, y, w, 1, thread->preset_text)
800 this->thread = thread;
801 this->window = window;
804 // user entered a title
805 int KeyFramePresetsText::handle_event()
807 strcpy(thread->preset_text, get_text());
808 // once changed, it's now not a factory preset
809 thread->is_factory = 0;
810 window->update_editing();
815 KeyFramePresetsDelete::KeyFramePresetsDelete(KeyFrameThread *thread,
816 KeyFrameWindow *window,
819 : BC_GenericButton(x, y, _("Delete"))
821 this->thread = thread;
822 this->window = window;
825 int KeyFramePresetsDelete::handle_event()
827 if( !thread->is_factory ) {
828 thread->delete_preset(thread->preset_text, thread->is_factory);
834 KeyFramePresetsSave::KeyFramePresetsSave(KeyFrameThread *thread,
835 KeyFrameWindow *window,
838 : BC_GenericButton(x, y, _("Save"))
840 this->thread = thread;
841 this->window = window;
844 int KeyFramePresetsSave::handle_event()
846 if( !thread->is_factory ) {
847 thread->save_preset(thread->preset_text, thread->is_factory);
859 KeyFramePresetsApply::KeyFramePresetsApply(KeyFrameThread *thread,
860 KeyFrameWindow *window,
863 : BC_GenericButton(x, y, _("Apply"))
865 this->thread = thread;
866 this->window = window;
869 int KeyFramePresetsApply::handle_event()
871 thread->apply_preset(thread->preset_text, thread->is_factory);
876 KeyFramePresetsOK::KeyFramePresetsOK(KeyFrameThread *thread,
877 KeyFrameWindow *window)
878 : BC_OKButton(window)
880 this->thread = thread;
881 this->window = window;
884 int KeyFramePresetsOK::keypress_event()
886 if( get_keypress() == RETURN ) {
888 if( thread->presets_db->preset_exists(thread->plugin_title,
889 thread->preset_text, thread->is_factory) ) {
894 if( !thread->is_factory ) {
895 thread->save_preset(thread->preset_text, thread->is_factory);