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)));
123 keyframe_data[0].append(new BC_ListBoxItem((char*)"TEXT"));
124 keyframe_data[1].append(new BC_ListBoxItem(text));
127 keyframe_data[0].append(new BC_ListBoxItem((char*)"DATA"));
128 keyframe_data[1].append(new BC_ListBoxItem(data));
134 column_width[0] = mwindow->session->keyframedialog_column1;
135 column_width[1] = mwindow->session->keyframedialog_column2;
136 if( selection >= 0 && selection < keyframe_data[0].size() ) {
137 for( int i=0; i<KEYFRAME_COLUMNS; ++i )
138 keyframe_data[i].get(selection)->set_selected(1);
140 mwindow->gui->unlock_window();
145 void KeyFrameThread::start_window(Plugin *plugin, KeyFrame *keyframe)
148 if( !BC_DialogThread::is_running() ) {
149 if( !mwindow->edl->tracks->plugin_exists(plugin) ) return;
150 this->keyframe = keyframe;
151 this->plugin = plugin;
152 plugin->calculate_title(plugin_title, 0);
153 sprintf(window_title, _("%s: %s Keyframe"), _(PROGRAM_NAME), plugin_title);
155 // Load all the presets from disk
156 char path[BCTEXTLEN];
158 // system wide presets
159 sprintf(path, "%s/%s", File::get_cindat_path(), FACTORY_FILE);
160 fs.complete_path(path);
161 presets_db->load_from_file(path, 1, 1);
163 sprintf(path, "%s/%s", File::get_config_path(), PRESETS_FILE);
164 fs.complete_path(path);
165 presets_db->load_from_file(path, 0, 0);
167 calculate_preset_list();
172 mwindow->gui->unlock_window();
173 BC_DialogThread::start();
174 mwindow->gui->lock_window("KeyFrameThread::start_window");
177 BC_DialogThread::start();
181 BC_Window* KeyFrameThread::new_gui()
183 mwindow->gui->lock_window("KeyFrameThread::new_gui");
185 int x = mwindow->gui->get_abs_cursor_x(0) -
186 mwindow->session->plugindialog_w / 2;
187 int y = mwindow->gui->get_abs_cursor_y(0) -
188 mwindow->session->plugindialog_h / 2;
190 KeyFrameWindow *window = new KeyFrameWindow(mwindow,
196 window->create_objects();
199 mwindow->gui->unlock_window();
204 void KeyFrameThread::handle_done_event(int result)
208 apply_preset(preset_text, is_factory);
212 void KeyFrameThread::handle_close_event(int result)
218 void KeyFrameThread::close_window()
220 lock_window("KeyFrameThread::close_window");
222 get_gui()->lock_window("KeyFrameThread::close_window");
223 get_gui()->set_done(1);
224 get_gui()->unlock_window();
231 void KeyFrameThread::calculate_preset_list()
233 presets_data->remove_all_objects();
234 is_factories.remove_all();
235 preset_titles.remove_all_objects();
236 int total_presets = presets_db->get_total_presets(plugin_title, 0);
239 presets_db->sort(plugin_title);
241 for( int i=0; i<total_presets; ++i ) {
242 char text[BCTEXTLEN];
243 char *orig_title = presets_db->get_preset_title( plugin_title, i);
244 if( !orig_title ) continue;
245 int is_factory = presets_db->get_is_factory(plugin_title, i);
246 sprintf(text, "%s%s", is_factory ? "*" : "", orig_title);
247 presets_data->append(new BC_ListBoxItem(text));
249 preset_titles.append(strdup(orig_title));
250 is_factories.append(is_factory);
255 void KeyFrameThread::update_gui(int update_value_text)
258 if( BC_DialogThread::is_running() ) {
259 mwindow->gui->lock_window("KeyFrameThread::update_gui");
261 mwindow->gui->unlock_window();
263 lock_window("KeyFrameThread::update_gui");
264 KeyFrameWindow *window = (KeyFrameWindow*)get_gui();
266 window->lock_window("KeyFrameThread::update_gui");
267 window->keyframe_list->update(keyframe_data,
271 window->keyframe_list->get_xposition(),
272 window->keyframe_list->get_yposition(),
273 window->keyframe_list->get_highlighted_item());
274 if( update_value_text &&
275 window->keyframe_list->get_selection_number(0, 0) >= 0 &&
276 window->keyframe_list->get_selection_number(0, 0) < keyframe_data[1].size() ) {
277 window->value_text->update(
278 keyframe_data[1].get(window->keyframe_list->get_selection_number(0, 0))->get_text());
280 window->unlock_window();
287 void KeyFrameThread::save_preset(const char *title, int is_factory)
289 get_gui()->unlock_window();
290 mwindow->gui->lock_window("KeyFrameThread::save_preset");
292 // Test EDL for plugin existence
293 if( !mwindow->edl->tracks->plugin_exists(plugin) ) {
294 mwindow->gui->unlock_window();
295 get_gui()->lock_window("KeyFrameThread::save_preset 2");
299 // Get current plugin keyframe
300 EDL *edl = mwindow->edl;
301 Track *track = plugin->track;
302 KeyFrame *keyframe = plugin->get_prev_keyframe(
303 track->to_units(edl->local_session->get_selectionstart(1), 0),
307 presets_db->save_preset(plugin_title, title, keyframe->get_data());
309 mwindow->gui->unlock_window();
310 get_gui()->lock_window("KeyFrameThread::save_preset 2");
313 calculate_preset_list();
314 ((KeyFrameWindow*)get_gui())->preset_list->update(presets_data,
318 void KeyFrameThread::delete_preset(const char *title, int is_factory)
320 get_gui()->unlock_window();
321 mwindow->gui->lock_window("KeyFrameThread::save_preset");
323 // Test EDL for plugin existence
324 if( !mwindow->edl->tracks->plugin_exists(plugin) ) {
325 mwindow->gui->unlock_window();
326 get_gui()->lock_window("KeyFrameThread::delete_preset 1");
330 presets_db->delete_preset(plugin_title, title, is_factory);
332 mwindow->gui->unlock_window();
333 get_gui()->lock_window("KeyFrameThread::delete_preset 2");
336 calculate_preset_list();
337 ((KeyFrameWindow*)get_gui())->preset_list->update(presets_data,
342 void KeyFrameThread::apply_preset(const char *title, int is_factory)
344 if( presets_db->preset_exists(plugin_title, title, is_factory) ) {
345 get_gui()->unlock_window();
346 mwindow->gui->lock_window("KeyFrameThread::apply_preset");
348 // Test EDL for plugin existence
349 if( !mwindow->edl->tracks->plugin_exists(plugin) ) {
350 mwindow->gui->unlock_window();
351 get_gui()->lock_window("KeyFrameThread::apply_preset 1");
355 mwindow->undo->update_undo_before();
357 #ifdef USE_KEYFRAME_SPANNING
359 presets_db->load_preset(plugin_title, title, &keyframe, is_factory);
360 plugin->keyframes->update_parameter(&keyframe);
362 KeyFrame *keyframe = plugin->get_keyframe();
363 presets_db->load_preset(plugin_title, title, keyframe, is_factory);
365 mwindow->save_backup();
366 mwindow->undo->update_undo_after(_("apply preset"), LOAD_AUTOMATION);
368 mwindow->update_plugin_guis(0);
369 mwindow->gui->draw_overlays(1);
370 mwindow->sync_parameters(CHANGE_PARAMS);
374 mwindow->gui->unlock_window();
375 get_gui()->lock_window("KeyFrameThread::apply_preset");
380 void KeyFrameThread::apply_value()
382 const char *text = 0, *data = 0;
384 KeyFrameWindow *window = (KeyFrameWindow*)get_gui();
385 int selection = window->keyframe_list->get_selection_number(0, 0);
386 //printf("KeyFrameThread::apply_value %d %d\n", __LINE__, selection);
387 if( selection < 0 ) return;
389 if( selection == keyframe_data[0].size() - 2 )
390 text = window->value_text->get_text();
391 else if( selection == keyframe_data[0].size() - 1 )
392 data = window->value_text->get_text();
394 const char *key = keyframe_data[0].get(selection)->get_text();
395 const char *value = window->value_text->get_text();
396 hash = new BC_Hash();
397 hash->update(key, value);
400 get_gui()->unlock_window();
401 mwindow->gui->lock_window("KeyFrameThread::apply_value");
402 if( plugin && mwindow->edl->tracks->plugin_exists(plugin) ) {
403 mwindow->undo->update_undo_before();
404 if( mwindow->session->keyframedialog_all ) {
405 // Search for all keyframes in selection but don't create a new one.
406 Track *track = plugin->track;
407 int64_t start = track->to_units(mwindow->edl->local_session->get_selectionstart(0), 0);
408 int64_t end = track->to_units(mwindow->edl->local_session->get_selectionend(0), 0);
410 KeyFrame *current = (KeyFrame*)plugin->keyframes->last;
411 for( ; current; current=(KeyFrame*)PREVIOUS ) {
413 if( current && current->position < end ) {
414 current->update_parameter(hash, text, data);
415 // Stop at beginning of range
416 if( current->position <= start ) break;
421 current = (KeyFrame*)plugin->keyframes->default_auto;
422 current->update_parameter(hash, text, data);
426 // Create new keyframe if enabled
427 KeyFrame *keyframe = plugin->get_keyframe();
428 keyframe->update_parameter(hash, text, data);
432 printf("KeyFrameThread::apply_value %d: plugin doesn't exist\n", __LINE__);
435 mwindow->save_backup();
436 mwindow->undo->update_undo_after(_("edit keyframe"), LOAD_AUTOMATION);
438 mwindow->update_plugin_guis(0);
439 mwindow->gui->draw_overlays(1);
440 mwindow->sync_parameters(CHANGE_PARAMS);
441 mwindow->gui->unlock_window();
445 get_gui()->lock_window("KeyFrameThread::apply_value");
452 KeyFrameWindow::KeyFrameWindow(MWindow *mwindow,
453 KeyFrameThread *thread,
457 : BC_Window(title_string,
460 mwindow->session->keyframedialog_w,
461 mwindow->session->keyframedialog_h,
468 this->mwindow = mwindow;
469 this->thread = thread;
472 void KeyFrameWindow::create_objects()
474 Theme *theme = mwindow->theme;
476 theme->get_keyframedialog_sizes(this);
477 thread->column_width[0] = mwindow->session->keyframedialog_column1;
478 thread->column_width[1] = mwindow->session->keyframedialog_column2;
479 lock_window("KeyFrameWindow::create_objects");
484 add_subwindow(title1 = new BC_Title(theme->keyframe_list_x,
485 theme->keyframe_list_y - BC_Title::calculate_h(this, (char*)"Py", LARGEFONT) -
486 theme->widget_border, _("Keyframe parameters:"), LARGEFONT));
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") -
503 theme->widget_border, _("Edit value:")));
504 add_subwindow(value_text = new KeyFrameValue(thread, this,
505 theme->keyframe_value_x, theme->keyframe_value_y, theme->keyframe_value_w));
506 add_subwindow(all_toggle = new KeyFrameAll(thread, this,
507 theme->keyframe_all_x, theme->keyframe_all_y));
510 add_subwindow(title4 = new BC_Title(theme->presets_list_x, theme->presets_list_y -
511 BC_Title::calculate_h(this, (char*)"Py", LARGEFONT) -
512 theme->widget_border, _("Presets:"), LARGEFONT));
513 add_subwindow(preset_list = new KeyFramePresetsList(thread, this,
514 theme->presets_list_x, theme->presets_list_y,
515 theme->presets_list_w, theme->presets_list_h));
516 add_subwindow(title5 = new BC_Title(theme->presets_text_x,
517 theme->presets_text_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border,
518 _("Preset title:")));
519 add_subwindow(preset_text = new KeyFramePresetsText(thread, this,
520 theme->presets_text_x, theme->presets_text_y, theme->presets_text_w));
521 add_subwindow(delete_preset = new KeyFramePresetsDelete(thread, this,
522 theme->presets_delete_x, theme->presets_delete_y));
523 add_subwindow(save_preset = new KeyFramePresetsSave(thread, this,
524 theme->presets_save_x, theme->presets_save_y));
525 add_subwindow(apply_preset = new KeyFramePresetsApply(thread, this,
526 theme->presets_apply_x, theme->presets_apply_y));
528 add_subwindow(new KeyFramePresetsOK(thread, this));
529 add_subwindow(new BC_CancelButton(this));
535 // called when going in & out of a factory preset
536 void KeyFrameWindow::update_editing()
538 if( thread->is_factory ) {
539 delete_preset->disable();
540 save_preset->disable();
543 delete_preset->enable();
544 save_preset->enable();
550 int KeyFrameWindow::resize_event(int w, int h)
552 Theme *theme = mwindow->theme;
553 mwindow->session->keyframedialog_w = w;
554 mwindow->session->keyframedialog_h = h;
555 theme->get_keyframedialog_sizes(this);
559 title1->reposition_window(theme->keyframe_list_x,
560 theme->keyframe_list_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
561 // title2->reposition_window(theme->keyframe_text_x,
562 // theme->keyframe_text_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
563 title3->reposition_window(theme->keyframe_value_x,
564 theme->keyframe_value_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
565 keyframe_list->reposition_window(theme->keyframe_list_x,
566 theme->keyframe_list_y,
567 theme->keyframe_list_w,
568 theme->keyframe_list_h);
569 // text->reposition_window(theme->keyframe_text_x,
570 // theme->keyframe_text_y,
571 // theme->keyframe_text_w);
572 value_text->reposition_window(theme->keyframe_value_x,
573 theme->keyframe_value_y,
574 theme->keyframe_value_w);
575 all_toggle->reposition_window(theme->keyframe_all_x,
576 theme->keyframe_all_y);
578 #endif // EDIT_KEYFRAME
583 title4->reposition_window(theme->presets_list_x,
584 theme->presets_list_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
585 title5->reposition_window(theme->presets_text_x,
586 theme->presets_text_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
587 preset_list->reposition_window(theme->presets_list_x,
588 theme->presets_list_y,
589 theme->presets_list_w,
590 theme->presets_list_h);
591 preset_text->reposition_window(theme->presets_text_x,
592 theme->presets_text_y,
593 theme->presets_text_w);
594 delete_preset->reposition_window(theme->presets_delete_x,
595 theme->presets_delete_y);
596 save_preset->reposition_window(theme->presets_save_x,
597 theme->presets_save_y);
598 apply_preset->reposition_window(theme->presets_apply_x,
599 theme->presets_apply_y);
610 KeyFrameList::KeyFrameList(KeyFrameThread *thread,
611 KeyFrameWindow *window, int x, int y, int w, int h)
612 : BC_ListBox(x, y, w, h, LISTBOX_TEXT, thread->keyframe_data,
613 thread->column_titles, thread->column_width, KEYFRAME_COLUMNS)
615 this->thread = thread;
616 this->window = window;
617 set_master_column(1, 0);
620 int KeyFrameList::selection_changed()
622 window->value_text->update(
623 thread->keyframe_data[1].get(get_selection_number(0, 0))->get_text());
627 int KeyFrameList::handle_event()
633 int KeyFrameList::column_resize_event()
635 thread->mwindow->session->keyframedialog_column1 = get_column_width(0);
636 thread->mwindow->session->keyframedialog_column2 = get_column_width(1);
643 // KeyFrameText::KeyFrameText(KeyFrameThread *thread,
644 // KeyFrameWindow *window,
654 // this->thread = thread;
655 // this->window = window;
658 // int KeyFrameText::handle_event()
665 KeyFrameValue::KeyFrameValue(KeyFrameThread *thread,
666 KeyFrameWindow *window,
676 this->thread = thread;
677 this->window = window;
680 int KeyFrameValue::handle_event()
682 thread->apply_value();
690 KeyFrameAll::KeyFrameAll(KeyFrameThread *thread,
691 KeyFrameWindow *window,
696 thread->mwindow->session->keyframedialog_all,
697 _("Apply to all selected keyframes"))
699 this->thread = thread;
700 this->window = window;
703 int KeyFrameAll::handle_event()
705 thread->mwindow->session->keyframedialog_all = get_value();
709 #endif // EDIT_KEYFRAME
720 KeyFramePresetsList::KeyFramePresetsList(KeyFrameThread *thread,
721 KeyFrameWindow *window,
731 thread->presets_data)
733 this->thread = thread;
734 this->window = window;
737 int KeyFramePresetsList::selection_changed()
739 int number = get_selection_number(0, 0);
741 strcpy(thread->preset_text, thread->preset_titles.get(number));
742 thread->is_factory = thread->is_factories.get(number);
743 // show title without factory symbol in the textbox
744 window->preset_text->update(
745 thread->presets_data->get(number)->get_text());
746 window->update_editing();
752 int KeyFramePresetsList::handle_event()
754 thread->apply_preset(thread->preset_text, thread->is_factory);
760 KeyFramePresetsText::KeyFramePresetsText(KeyFrameThread *thread,
761 KeyFrameWindow *window, int x, int y, int w)
762 : BC_TextBox(x, y, w, 1, thread->preset_text)
764 this->thread = thread;
765 this->window = window;
768 // user entered a title
769 int KeyFramePresetsText::handle_event()
771 strcpy(thread->preset_text, get_text());
772 // once changed, it's now not a factory preset
773 thread->is_factory = 0;
774 window->update_editing();
779 KeyFramePresetsDelete::KeyFramePresetsDelete(KeyFrameThread *thread,
780 KeyFrameWindow *window,
783 : BC_GenericButton(x, y, _("Delete"))
785 this->thread = thread;
786 this->window = window;
789 int KeyFramePresetsDelete::handle_event()
791 if( !thread->is_factory ) {
792 thread->delete_preset(thread->preset_text, thread->is_factory);
798 KeyFramePresetsSave::KeyFramePresetsSave(KeyFrameThread *thread,
799 KeyFrameWindow *window,
802 : BC_GenericButton(x, y, C_("Save"))
804 this->thread = thread;
805 this->window = window;
808 int KeyFramePresetsSave::handle_event()
810 if( !thread->is_factory ) {
811 thread->save_preset(thread->preset_text, thread->is_factory);
823 KeyFramePresetsApply::KeyFramePresetsApply(KeyFrameThread *thread,
824 KeyFrameWindow *window,
827 : BC_GenericButton(x, y, _("Apply"))
829 this->thread = thread;
830 this->window = window;
833 int KeyFramePresetsApply::handle_event()
835 thread->apply_preset(thread->preset_text, thread->is_factory);
840 KeyFramePresetsOK::KeyFramePresetsOK(KeyFrameThread *thread,
841 KeyFrameWindow *window)
842 : BC_OKButton(window)
844 this->thread = thread;
845 this->window = window;
848 int KeyFramePresetsOK::keypress_event()
850 if( get_keypress() == RETURN ) {
852 if( thread->presets_db->preset_exists(thread->plugin_title,
853 thread->preset_text, thread->is_factory) ) {
858 if( !thread->is_factory ) {
859 thread->save_preset(thread->preset_text, thread->is_factory);