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, _("%s: %s Keyframe"), _(PROGRAM_NAME), 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 - BC_Title::calculate_h(this, (char*)"Py", LARGEFONT) -
483 theme->widget_border, _("Keyframe parameters:"), LARGEFONT));
484 add_subwindow(keyframe_list = new KeyFrameList(thread,
486 theme->keyframe_list_x,
487 theme->keyframe_list_y,
488 theme->keyframe_list_w,
489 theme->keyframe_list_h));
490 // add_subwindow(title2 = new BC_Title(theme->keyframe_text_x,
491 // theme->keyframe_text_y - BC_Title::calculate_h(this, "P") - theme->widget_border,
492 // _("Global Text:")));
493 // add_subwindow(keyframe_text = new KeyFrameText(thread,
495 // theme->keyframe_text_x,
496 // theme->keyframe_text_y,
497 // theme->keyframe_text_w));
498 add_subwindow(title3 = new BC_Title(theme->keyframe_value_x,
499 theme->keyframe_value_y - BC_Title::calculate_h(this, (char*)"P") -
500 theme->widget_border, _("Edit value:")));
501 add_subwindow(value_text = new KeyFrameValue(thread, this,
502 theme->keyframe_value_x, theme->keyframe_value_y, theme->keyframe_value_w));
503 add_subwindow(all_toggle = new KeyFrameAll(thread, this,
504 theme->keyframe_all_x, theme->keyframe_all_y));
507 add_subwindow(title4 = new BC_Title(theme->presets_list_x, theme->presets_list_y -
508 BC_Title::calculate_h(this, (char*)"Py", LARGEFONT) -
509 theme->widget_border, _("Presets:"), LARGEFONT));
510 add_subwindow(preset_list = new KeyFramePresetsList(thread, this,
511 theme->presets_list_x, theme->presets_list_y,
512 theme->presets_list_w, theme->presets_list_h));
513 add_subwindow(title5 = new BC_Title(theme->presets_text_x,
514 theme->presets_text_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border,
515 _("Preset title:")));
516 add_subwindow(preset_text = new KeyFramePresetsText(thread, this,
517 theme->presets_text_x, theme->presets_text_y, theme->presets_text_w));
518 add_subwindow(delete_preset = new KeyFramePresetsDelete(thread, this,
519 theme->presets_delete_x, theme->presets_delete_y));
520 add_subwindow(save_preset = new KeyFramePresetsSave(thread, this,
521 theme->presets_save_x, theme->presets_save_y));
522 add_subwindow(apply_preset = new KeyFramePresetsApply(thread, this,
523 theme->presets_apply_x, theme->presets_apply_y));
525 add_subwindow(new KeyFramePresetsOK(thread, this));
526 add_subwindow(new BC_CancelButton(this));
532 // called when going in & out of a factory preset
533 void KeyFrameWindow::update_editing()
535 if( thread->is_factory ) {
536 delete_preset->disable();
537 save_preset->disable();
540 delete_preset->enable();
541 save_preset->enable();
547 int KeyFrameWindow::resize_event(int w, int h)
549 Theme *theme = mwindow->theme;
550 mwindow->session->keyframedialog_w = w;
551 mwindow->session->keyframedialog_h = h;
552 theme->get_keyframedialog_sizes(this);
556 title1->reposition_window(theme->keyframe_list_x,
557 theme->keyframe_list_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
558 // title2->reposition_window(theme->keyframe_text_x,
559 // theme->keyframe_text_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
560 title3->reposition_window(theme->keyframe_value_x,
561 theme->keyframe_value_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
562 keyframe_list->reposition_window(theme->keyframe_list_x,
563 theme->keyframe_list_y,
564 theme->keyframe_list_w,
565 theme->keyframe_list_h);
566 // text->reposition_window(theme->keyframe_text_x,
567 // theme->keyframe_text_y,
568 // theme->keyframe_text_w);
569 value_text->reposition_window(theme->keyframe_value_x,
570 theme->keyframe_value_y,
571 theme->keyframe_value_w);
572 all_toggle->reposition_window(theme->keyframe_all_x,
573 theme->keyframe_all_y);
575 #endif // EDIT_KEYFRAME
580 title4->reposition_window(theme->presets_list_x,
581 theme->presets_list_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
582 title5->reposition_window(theme->presets_text_x,
583 theme->presets_text_y - BC_Title::calculate_h(this, (char*)"P") - theme->widget_border);
584 preset_list->reposition_window(theme->presets_list_x,
585 theme->presets_list_y,
586 theme->presets_list_w,
587 theme->presets_list_h);
588 preset_text->reposition_window(theme->presets_text_x,
589 theme->presets_text_y,
590 theme->presets_text_w);
591 delete_preset->reposition_window(theme->presets_delete_x,
592 theme->presets_delete_y);
593 save_preset->reposition_window(theme->presets_save_x,
594 theme->presets_save_y);
595 apply_preset->reposition_window(theme->presets_apply_x,
596 theme->presets_apply_y);
607 KeyFrameList::KeyFrameList(KeyFrameThread *thread,
608 KeyFrameWindow *window, int x, int y, int w, int h)
609 : BC_ListBox(x, y, w, h, LISTBOX_TEXT, thread->keyframe_data,
610 thread->column_titles, thread->column_width, KEYFRAME_COLUMNS)
612 this->thread = thread;
613 this->window = window;
614 set_master_column(1, 0);
617 int KeyFrameList::selection_changed()
619 window->value_text->update(
620 thread->keyframe_data[1].get(get_selection_number(0, 0))->get_text());
624 int KeyFrameList::handle_event()
630 int KeyFrameList::column_resize_event()
632 thread->mwindow->session->keyframedialog_column1 = get_column_width(0);
633 thread->mwindow->session->keyframedialog_column2 = get_column_width(1);
640 // KeyFrameText::KeyFrameText(KeyFrameThread *thread,
641 // KeyFrameWindow *window,
651 // this->thread = thread;
652 // this->window = window;
655 // int KeyFrameText::handle_event()
662 KeyFrameValue::KeyFrameValue(KeyFrameThread *thread,
663 KeyFrameWindow *window,
673 this->thread = thread;
674 this->window = window;
677 int KeyFrameValue::handle_event()
679 thread->apply_value();
687 KeyFrameAll::KeyFrameAll(KeyFrameThread *thread,
688 KeyFrameWindow *window,
693 thread->mwindow->session->keyframedialog_all,
694 _("Apply to all selected keyframes"))
696 this->thread = thread;
697 this->window = window;
700 int KeyFrameAll::handle_event()
702 thread->mwindow->session->keyframedialog_all = get_value();
706 #endif // EDIT_KEYFRAME
717 KeyFramePresetsList::KeyFramePresetsList(KeyFrameThread *thread,
718 KeyFrameWindow *window,
728 thread->presets_data)
730 this->thread = thread;
731 this->window = window;
734 int KeyFramePresetsList::selection_changed()
736 int number = get_selection_number(0, 0);
738 strcpy(thread->preset_text, thread->preset_titles.get(number));
739 thread->is_factory = thread->is_factories.get(number);
740 // show title without factory symbol in the textbox
741 window->preset_text->update(
742 thread->presets_data->get(number)->get_text());
743 window->update_editing();
749 int KeyFramePresetsList::handle_event()
751 thread->apply_preset(thread->preset_text, thread->is_factory);
757 KeyFramePresetsText::KeyFramePresetsText(KeyFrameThread *thread,
758 KeyFrameWindow *window, int x, int y, int w)
759 : BC_TextBox(x, y, w, 1, thread->preset_text)
761 this->thread = thread;
762 this->window = window;
765 // user entered a title
766 int KeyFramePresetsText::handle_event()
768 strcpy(thread->preset_text, get_text());
769 // once changed, it's now not a factory preset
770 thread->is_factory = 0;
771 window->update_editing();
776 KeyFramePresetsDelete::KeyFramePresetsDelete(KeyFrameThread *thread,
777 KeyFrameWindow *window,
780 : BC_GenericButton(x, y, _("Delete"))
782 this->thread = thread;
783 this->window = window;
786 int KeyFramePresetsDelete::handle_event()
788 if( !thread->is_factory ) {
789 thread->delete_preset(thread->preset_text, thread->is_factory);
795 KeyFramePresetsSave::KeyFramePresetsSave(KeyFrameThread *thread,
796 KeyFrameWindow *window,
799 : BC_GenericButton(x, y, C_("Save"))
801 this->thread = thread;
802 this->window = window;
805 int KeyFramePresetsSave::handle_event()
807 if( !thread->is_factory ) {
808 thread->save_preset(thread->preset_text, thread->is_factory);
820 KeyFramePresetsApply::KeyFramePresetsApply(KeyFrameThread *thread,
821 KeyFrameWindow *window,
824 : BC_GenericButton(x, y, _("Apply"))
826 this->thread = thread;
827 this->window = window;
830 int KeyFramePresetsApply::handle_event()
832 thread->apply_preset(thread->preset_text, thread->is_factory);
837 KeyFramePresetsOK::KeyFramePresetsOK(KeyFrameThread *thread,
838 KeyFrameWindow *window)
839 : BC_OKButton(window)
841 this->thread = thread;
842 this->window = window;
845 int KeyFramePresetsOK::keypress_event()
847 if( get_keypress() == RETURN ) {
849 if( thread->presets_db->preset_exists(thread->plugin_title,
850 thread->preset_text, thread->is_factory) ) {
855 if( !thread->is_factory ) {
856 thread->save_preset(thread->preset_text, thread->is_factory);