61a0b052aa3cdf34f74eebedfa76984b61dd4a2d
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / keyframegui.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2017 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #include "bchash.h"
23 #include "bcsignals.h"
24 #include "edl.h"
25 #include "file.h"
26 #include "filesystem.h"
27 #include "keyframe.h"
28 #include "keyframes.h"
29 #include "keyframegui.h"
30 #include "keys.h"
31 #include "language.h"
32 #include "localsession.h"
33 #include "mainsession.h"
34 #include "mainundo.h"
35 #include "mwindow.h"
36 #include "mwindowgui.h"
37 #include "plugin.h"
38 #include "preferences.h"
39 #include "presets.h"
40 #include "theme.h"
41 #include "trackcanvas.h"
42 #include "tracks.h"
43
44
45 KeyFrameThread::KeyFrameThread(MWindow *mwindow)
46  : BC_DialogThread()
47 {
48         this->mwindow = mwindow;
49         plugin_id = -1;
50         keyframe = 0;
51         keyframe_data = new ArrayList<BC_ListBoxItem*>[KEYFRAME_COLUMNS];
52         plugin_title[0] = 0;
53         is_factory = 0;
54         preset_text[0] = 0;
55         window_title[0] = 0;
56         column_titles[0] = (char*)_("Parameter");
57         column_titles[1] = (char*)_("Value");
58         column_width[0] = 0;
59         column_width[1] = 0;
60         presets_data = new ArrayList<BC_ListBoxItem*>;
61         presets_db = new PresetsDB;
62 }
63
64 KeyFrameThread::~KeyFrameThread()
65 {
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();
70         delete presets_data;
71         is_factories.remove_all();
72         preset_titles.remove_all_objects();
73 }
74
75
76 #ifdef EDIT_KEYFRAME
77
78 void KeyFrameThread::update_values()
79 {
80 // Get the current selection before deleting the tables
81         int selection = -1;
82         for( int i=0; i<keyframe_data[0].size(); ++i ) {
83                 if( keyframe_data[0].get(i)->get_selected() ) {
84                         selection = i;
85                         break;
86                 }
87         }
88
89         for( int i=0; i<KEYFRAME_COLUMNS; ++i )
90                 keyframe_data[i].remove_all_objects();
91
92
93 // Must lock main window to read keyframe
94         mwindow->gui->lock_window("KeyFrameThread::update_values");
95         Plugin *plugin = mwindow->edl->tracks->plugin_exists(plugin_id);
96         if( !plugin ) {
97                 mwindow->gui->unlock_window();
98                 return;
99         }
100
101         KeyFrame *keyframe = 0;
102         if( this->keyframe && plugin->keyframe_exists(this->keyframe) ) {
103 // If user edited a specific keyframe, use it.
104                 keyframe = this->keyframe;
105         }
106         else
107         if( plugin->track ) {
108 // Use currently highlighted keyframe
109                 keyframe = plugin->get_prev_keyframe(
110                         plugin->track->to_units(
111                                 mwindow->edl->local_session->get_selectionstart(1), 0),
112                         PLAY_FORWARD);
113         }
114
115         if( keyframe ) {
116                 BC_Hash hash;
117                 char *text = 0, *data = 0;
118                 keyframe->get_contents(&hash, &text, &data);
119                 
120                 for( int i=0; i<hash.size(); ++i ) {
121                         keyframe_data[0].append(new BC_ListBoxItem(hash.get_key(i)));
122                         keyframe_data[1].append(new BC_ListBoxItem(hash.get_value(i)));
123                 }
124                 if( text ) {
125                         keyframe_data[0].append(new BC_ListBoxItem((char*)"TEXT"));
126                         keyframe_data[1].append(new BC_ListBoxItem(text));
127                 }
128                 if( data ) {
129                         keyframe_data[0].append(new BC_ListBoxItem((char*)"DATA"));
130                         keyframe_data[1].append(new BC_ListBoxItem(data));
131                 }
132                 delete [] text;
133                 delete [] data;
134         }
135
136         column_width[0] = mwindow->session->keyframedialog_column1;
137         column_width[1] = mwindow->session->keyframedialog_column2;
138         if( selection >= 0 && selection < keyframe_data[0].size() ) {
139                 for( int i=0; i<KEYFRAME_COLUMNS; ++i )
140                         keyframe_data[i].get(selection)->set_selected(1);
141         }
142         mwindow->gui->unlock_window();
143 }
144
145 #endif
146
147 void KeyFrameThread::start_window(Plugin *plugin, KeyFrame *keyframe)
148 {
149
150         if( !BC_DialogThread::is_running() ) {
151                 if( !mwindow->edl->tracks->plugin_exists(plugin->orig_id) ) return;
152                 this->keyframe = keyframe;
153                 this->plugin_id = plugin->orig_id;
154                 this->preset_text[0] = 0;
155                 plugin->calculate_title(plugin_title, 0);
156                 sprintf(window_title, _("%s: %s Keyframe"), _(PROGRAM_NAME), plugin_title);
157
158 // Load all the presets from disk
159                 char path[BCTEXTLEN];
160                 FileSystem fs;
161 // system wide presets
162                 sprintf(path, "%s/%s", File::get_cindat_path(), FACTORY_FILE);
163                 fs.complete_path(path);
164                 presets_db->load_from_file(path, 1, 1);
165 // user presets
166                 sprintf(path, "%s/%s", File::get_config_path(), PRESETS_FILE);
167                 fs.complete_path(path);
168                 presets_db->load_from_file(path, 0, 0);
169
170                 calculate_preset_list();
171
172 #ifdef EDIT_KEYFRAME
173                 update_values();
174 #endif
175                 mwindow->gui->unlock_window();
176                 BC_DialogThread::start();
177                 mwindow->gui->lock_window("KeyFrameThread::start_window");
178         }
179         else {
180                 BC_DialogThread::start();
181         }
182 }
183
184 BC_Window* KeyFrameThread::new_gui()
185 {
186         mwindow->gui->lock_window("KeyFrameThread::new_gui");
187         
188         int x = mwindow->gui->get_abs_cursor_x(0) - 
189                 mwindow->session->plugindialog_w / 2;
190         int y = mwindow->gui->get_abs_cursor_y(0) - 
191                 mwindow->session->plugindialog_h / 2;
192
193         KeyFrameWindow *window = new KeyFrameWindow(mwindow, 
194                 this, 
195                 x, 
196                 y,
197                 window_title);
198
199         window->create_objects();
200         
201         
202         mwindow->gui->unlock_window();
203
204         return window;
205 }
206
207 void KeyFrameThread::handle_done_event(int result)
208 {
209 // Apply the preset
210         if( !result ) {
211                 get_gui()->lock_window("KeyFrameThread::handle_done_event");
212                 apply_preset(preset_text, is_factory);
213                 get_gui()->unlock_window();
214         }
215 }
216
217 void KeyFrameThread::handle_close_event(int result)
218 {
219         plugin_id = -1;
220         keyframe = 0;
221 }
222
223 void KeyFrameThread::close_window()
224 {
225         lock_dialog("KeyFrameThread::close_window");
226         if( get_gui() ) {
227                 get_gui()->lock_window("KeyFrameThread::close_window");
228                 get_gui()->set_done(1);
229                 get_gui()->unlock_window();
230         }
231         unlock_dialog();
232 }
233
234
235
236 void KeyFrameThread::calculate_preset_list()
237 {
238         presets_data->remove_all_objects();
239         is_factories.remove_all();
240         preset_titles.remove_all_objects();
241         int total_presets = presets_db->get_total_presets(plugin_title, 0);
242
243 // sort the list
244         presets_db->sort(plugin_title);
245         
246         for( int i=0; i<total_presets; ++i ) {
247                 char text[BCTEXTLEN];
248                 char *orig_title = presets_db->get_preset_title( plugin_title, i);
249                 if( !orig_title ) continue;
250                 int is_factory = presets_db->get_is_factory(plugin_title, i);
251                 sprintf(text, "%s%s", is_factory ? "*" : "", orig_title);
252                 presets_data->append(new BC_ListBoxItem(text));
253
254                 preset_titles.append(strdup(orig_title));
255                 is_factories.append(is_factory);
256         }
257 }
258
259
260 void KeyFrameThread::update_gui(int update_value_text)
261 {
262 #ifdef EDIT_KEYFRAME
263         if( BC_DialogThread::is_running() ) {
264                 mwindow->gui->lock_window("KeyFrameThread::update_gui");
265                 update_values();
266                 mwindow->gui->unlock_window();
267
268                 lock_dialog("KeyFrameThread::update_gui");
269                 KeyFrameWindow *window = (KeyFrameWindow*)get_gui();
270                 if( window ) {
271                         window->lock_window("KeyFrameThread::update_gui");
272                         window->keyframe_list->update(keyframe_data,
273                                 column_titles,
274                                 column_width,
275                                 KEYFRAME_COLUMNS,
276                                 window->keyframe_list->get_xposition(),
277                                 window->keyframe_list->get_yposition(),
278                                 window->keyframe_list->get_highlighted_item());
279                         if( update_value_text &&
280                                 window->keyframe_list->get_selection_number(0, 0) >= 0 &&
281                                 window->keyframe_list->get_selection_number(0, 0) < keyframe_data[1].size() ) {
282                                 window->value_text->update(
283                                         keyframe_data[1].get(window->keyframe_list->get_selection_number(0, 0))->get_text());
284                         }
285                         window->unlock_window();
286                 }
287                 unlock_dialog();
288         }
289 #endif
290 }
291
292 void KeyFrameThread::save_preset(const char *title, int is_factory)
293 {
294         get_gui()->unlock_window();
295         mwindow->gui->lock_window("KeyFrameThread::save_preset");
296         
297 // Test EDL for plugin existence
298         Plugin *plugin = mwindow->edl->tracks->plugin_exists(plugin_id);
299         if( !plugin ) {
300                 mwindow->gui->unlock_window();
301                 get_gui()->lock_window("KeyFrameThread::save_preset 2");
302                 return;
303         }
304
305 // Get current plugin keyframe
306         EDL *edl = mwindow->edl;
307         Track *track = plugin->track;
308         KeyFrame *keyframe = plugin->get_prev_keyframe(
309                         track->to_units(edl->local_session->get_selectionstart(1), 0), 
310                         PLAY_FORWARD);
311
312 // Send to database
313         presets_db->save_preset(plugin_title, title, keyframe->get_data());
314
315         mwindow->gui->unlock_window();
316         get_gui()->lock_window("KeyFrameThread::save_preset 2");
317
318 // Update list
319         calculate_preset_list();
320         ((KeyFrameWindow*)get_gui())->preset_list->update(presets_data,
321                 0, 0, 1);
322 }
323
324 void KeyFrameThread::delete_preset(const char *title, int is_factory)
325 {
326         get_gui()->unlock_window();
327         mwindow->gui->lock_window("KeyFrameThread::save_preset");
328         
329 // Test EDL for plugin existence
330         Plugin *plugin = mwindow->edl->tracks->plugin_exists(plugin_id);
331         if( !plugin ) {
332                 mwindow->gui->unlock_window();
333                 get_gui()->lock_window("KeyFrameThread::delete_preset 1");
334                 return;
335         }
336
337         presets_db->delete_preset(plugin_title, title, is_factory);
338         
339         mwindow->gui->unlock_window();
340         get_gui()->lock_window("KeyFrameThread::delete_preset 2");
341
342 // Update list
343         calculate_preset_list();
344         ((KeyFrameWindow*)get_gui())->preset_list->update(presets_data,
345                 0, 0, 1);
346 }
347
348
349 void KeyFrameThread::apply_preset(const char *title, int is_factory)
350 {
351         if( presets_db->preset_exists(plugin_title, title, is_factory) ) {
352                 get_gui()->unlock_window();
353                 mwindow->gui->lock_window("KeyFrameThread::apply_preset");
354
355 // Test EDL for plugin existence
356                 Plugin *plugin = mwindow->edl->tracks->plugin_exists(plugin_id);
357                 if( !plugin ) {
358                         mwindow->gui->unlock_window();
359                         get_gui()->lock_window("KeyFrameThread::apply_preset 1");
360                         return;
361                 }
362
363                 mwindow->undo->update_undo_before();
364
365 #ifdef USE_KEYFRAME_SPANNING
366                 KeyFrame keyframe;
367                 presets_db->load_preset(plugin_title, title, &keyframe, is_factory);
368                 plugin->keyframes->update_parameter(&keyframe);
369 #else
370                 KeyFrame *keyframe = plugin->get_keyframe();
371                 presets_db->load_preset(plugin_title, title, keyframe, is_factory);
372 #endif
373                 mwindow->save_backup();
374                 mwindow->undo->update_undo_after(_("apply preset"), LOAD_AUTOMATION); 
375
376                 mwindow->update_plugin_guis(0);
377                 mwindow->gui->draw_overlays(1);
378                 mwindow->sync_parameters(CHANGE_PARAMS);
379
380
381                 update_gui(1);
382                 mwindow->gui->unlock_window();
383                 get_gui()->lock_window("KeyFrameThread::apply_preset");
384         }
385 }
386
387 #ifdef EDIT_KEYFRAME
388
389 void KeyFrameThread::apply_value()
390 {
391         const char *text = 0, *data = 0;
392         BC_Hash *hash = 0;
393         KeyFrameWindow *window = (KeyFrameWindow*)get_gui();
394         int selection = window->keyframe_list->get_selection_number(0, 0);
395 //printf("KeyFrameThread::apply_value %d %d\n", __LINE__, selection);
396         if( selection < 0 ) return;
397         
398         if( selection == keyframe_data[0].size() - 2 )
399                 text = window->value_text->get_text();
400         else if( selection == keyframe_data[0].size() - 1 )
401                 data = window->value_text->get_text();
402         else {
403                 const char *key = keyframe_data[0].get(selection)->get_text();
404                 const char *value = window->value_text->get_text();
405                 hash = new BC_Hash();
406                 hash->update(key, value);
407         }
408
409         get_gui()->unlock_window();
410         mwindow->gui->lock_window("KeyFrameThread::apply_value");
411         Plugin *plugin = mwindow->edl->tracks->plugin_exists(plugin_id);
412         if( plugin ) {
413                 mwindow->undo->update_undo_before();
414                 if( mwindow->session->keyframedialog_all ) {
415 // Search for all keyframes in selection but don't create a new one.
416                         Track *track = plugin->track;
417                         int64_t start = track->to_units(mwindow->edl->local_session->get_selectionstart(0), 0);
418                         int64_t end = track->to_units(mwindow->edl->local_session->get_selectionend(0), 0);
419                         int got_it = 0;
420                         KeyFrame *current = (KeyFrame*)plugin->keyframes->last;
421                         for( ; current; current=(KeyFrame*)PREVIOUS ) {
422                                 got_it = 1;
423                                 if( current && current->position < end ) {
424                                     current->update_parameter(hash, text, data);
425 // Stop at beginning of range
426                                         if( current->position <= start ) break;
427                                 }
428                         }
429
430                         if( !got_it ) {
431                                 current = (KeyFrame*)plugin->keyframes->default_auto;
432                                 current->update_parameter(hash, text, data);
433                         }
434                 }
435                 else {
436 // Create new keyframe if enabled
437                         KeyFrame *keyframe = plugin->get_keyframe();
438                         keyframe->update_parameter(hash, text, data);
439                 }
440         }
441         else {
442 printf("KeyFrameThread::apply_value %d: plugin doesn't exist\n", __LINE__);
443         }
444
445         mwindow->save_backup();
446         mwindow->undo->update_undo_after(_("edit keyframe"), LOAD_AUTOMATION); 
447
448         mwindow->update_plugin_guis(0);
449         mwindow->gui->draw_overlays(1);
450         mwindow->sync_parameters(CHANGE_PARAMS);
451         mwindow->gui->unlock_window();
452
453         update_gui(0);
454
455         get_gui()->lock_window("KeyFrameThread::apply_value");
456         delete hash;
457 }
458
459 #endif
460
461
462 KeyFrameWindow::KeyFrameWindow(MWindow *mwindow, KeyFrameThread *thread,
463                 int x, int y, char *title_string)
464  : BC_Window(title_string, x, y,
465                 mwindow->session->keyframedialog_w, 
466                 mwindow->session->keyframedialog_h, 
467                 xS(320), yS(240), 1, 0, 1)
468 {
469         this->mwindow = mwindow;
470         this->thread = thread;
471 }
472
473 void KeyFrameWindow::create_objects()
474 {
475         Theme *theme = mwindow->theme;
476
477         theme->get_keyframedialog_sizes(this);
478         thread->column_width[0] = mwindow->session->keyframedialog_column1;
479         thread->column_width[1] = mwindow->session->keyframedialog_column2;
480         lock_window("KeyFrameWindow::create_objects");
481
482 #ifdef EDIT_KEYFRAME
483
484         int ky = theme->keyframe_list_y - theme->widget_border -
485                         BC_Title::calculate_h(this, (char*)"Py", LARGEFONT);
486         add_subwindow(title1 = new BC_Title(theme->keyframe_list_x, ky,
487                         _("Keyframe parameters:"), LARGEFONT));
488         add_subwindow(keyframe_list = new KeyFrameList(thread, this,
489                 theme->keyframe_list_x, theme->keyframe_list_y,
490                 theme->keyframe_list_w, theme->keyframe_list_h));
491 //      add_subwindow(title2 = new BC_Title(theme->keyframe_text_x,
492 //              theme->keyframe_text_y - theme->widget_border -
493 //                      BC_Title::calculate_h(this, "P"), _("Global Text:")));
494 //      add_subwindow(keyframe_text = new KeyFrameText(thread, this,
495 //              theme->keyframe_text_x, theme->keyframe_text_y,
496 //              theme->keyframe_text_w));
497         int ey = theme->keyframe_value_y - theme->widget_border -
498                         BC_Title::calculate_h(this, "P");
499         add_subwindow(title3 = new BC_Title(theme->keyframe_value_x, ey,
500                         _("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));
505
506 #endif
507         int ty = theme->presets_list_y - theme->widget_border -
508                         BC_Title::calculate_h(this, (char*)"Py", LARGEFONT);
509         add_subwindow(title4 = new BC_Title(theme->presets_list_x, ty,
510                         _("Presets:"), LARGEFONT));
511         add_subwindow(preset_list = new KeyFramePresetsList(thread, this,
512                 theme->presets_list_x, theme->presets_list_y,
513                 theme->presets_list_w, theme->presets_list_h));
514         int py = theme->presets_text_y - theme->widget_border -
515                         BC_Title::calculate_h(this, "Py");
516         add_subwindow(title5 = new BC_Title(theme->presets_text_x, py,
517                         _("Preset title:")));
518         add_subwindow(preset_text = new KeyFramePresetsText(thread, this,
519                 theme->presets_text_x, theme->presets_text_y, theme->presets_text_w));
520         add_subwindow(delete_preset = new KeyFramePresetsDelete(thread, this,
521                 theme->presets_delete_x, theme->presets_delete_y));
522         add_subwindow(save_preset = new KeyFramePresetsSave(thread, this,
523                 theme->presets_save_x, theme->presets_save_y));
524         add_subwindow(apply_preset = new KeyFramePresetsApply(thread, this,
525                 theme->presets_apply_x, theme->presets_apply_y));
526
527         add_subwindow(new KeyFramePresetsOK(thread, this));
528         add_subwindow(new BC_CancelButton(this));
529
530         show_window();
531         unlock_window();
532 }
533
534 // called when going in & out of a factory preset
535 void KeyFrameWindow::update_editing()
536 {
537         if( thread->is_factory ) {
538                 delete_preset->disable();
539                 save_preset->disable();
540         }
541         else {
542                 delete_preset->enable();
543                 save_preset->enable();
544         }
545 }
546
547 int KeyFrameWindow::resize_event(int w, int h)
548 {
549         Theme *theme = mwindow->theme;
550         mwindow->session->keyframedialog_w = w;
551         mwindow->session->keyframedialog_h = h;
552         theme->get_keyframedialog_sizes(this);
553
554 #ifdef EDIT_KEYFRAME
555         int ky = theme->keyframe_list_y - theme->widget_border -
556                         BC_Title::calculate_h(this, (char*)"Py", LARGEFONT);
557         title1->reposition_window(theme->keyframe_list_x, ky);
558 //      title2->reposition_window(theme->keyframe_text_x,
559 //              theme->keyframe_text_y - theme->widget_border -;
560 //                      BC_Title::calculate_h(this, (char*)"P"));
561         int ey = theme->keyframe_value_y - theme->widget_border -
562                         BC_Title::calculate_h(this, "P");
563         title3->reposition_window(theme->keyframe_value_x, ey);
564         keyframe_list->reposition_window(theme->keyframe_list_x, theme->keyframe_list_y,
565                         theme->keyframe_list_w, theme->keyframe_list_h);
566 //      text->reposition_window(theme->keyframe_text_x, theme->keyframe_text_y,
567 //                      theme->keyframe_text_w);
568         value_text->reposition_window(theme->keyframe_value_x,
569                 theme->keyframe_value_y,
570                 theme->keyframe_value_w);
571         all_toggle->reposition_window(theme->keyframe_all_x,
572                 theme->keyframe_all_y);
573
574         int ty = theme->presets_list_y - theme->widget_border -
575                         BC_Title::calculate_h(this, (char*)"Py", LARGEFONT);
576         title4->reposition_window(theme->presets_list_x, ty);
577         int py = theme->presets_text_y - theme->widget_border -
578                         BC_Title::calculate_h(this, "Py");
579         title5->reposition_window(theme->presets_text_x, py);
580 #endif
581
582         preset_list->reposition_window(theme->presets_list_x,
583                 theme->presets_list_y,
584                 theme->presets_list_w, 
585                 theme->presets_list_h);
586         preset_text->reposition_window(theme->presets_text_x,
587                 theme->presets_text_y,
588                 theme->presets_text_w);
589         delete_preset->reposition_window(theme->presets_delete_x,
590                 theme->presets_delete_y);
591         save_preset->reposition_window(theme->presets_save_x,
592                 theme->presets_save_y);
593         apply_preset->reposition_window(theme->presets_apply_x,
594                 theme->presets_apply_y);
595
596         return 0;
597 }
598
599
600
601
602 #ifdef EDIT_KEYFRAME
603
604 KeyFrameList::KeyFrameList(KeyFrameThread *thread,
605         KeyFrameWindow *window, int x, int y, int w, int h)
606  : BC_ListBox(x, y, w, h, LISTBOX_TEXT, thread->keyframe_data,
607         thread->column_titles, thread->column_width, KEYFRAME_COLUMNS)
608 {
609         this->thread = thread;
610         this->window = window;
611         set_master_column(1, 0);
612 }
613
614 int KeyFrameList::selection_changed()
615 {
616         window->value_text->update(
617                 thread->keyframe_data[1].get(get_selection_number(0, 0))->get_text());
618         return 0;
619 }
620
621 int KeyFrameList::handle_event()
622 {
623         window->set_done(0);
624         return 0;
625 }
626
627 int KeyFrameList::column_resize_event()
628 {
629         thread->mwindow->session->keyframedialog_column1 = get_column_width(0);
630         thread->mwindow->session->keyframedialog_column2 = get_column_width(1);
631         return 1;
632 }
633
634
635 // KeyFrameText::KeyFrameText(KeyFrameThread *thread,
636 //      KeyFrameWindow *window,
637 //      int x,
638 //      int y,
639 //      int w)
640 //  : BC_TextBox(x, 
641 //      y, 
642 //      w, 
643 //      1, 
644 //      "")
645 // {
646 //      this->thread = thread;
647 //      this->window = window;
648 // }
649 // 
650 // int KeyFrameText::handle_event()
651 // {
652 //      return 0;
653 // }
654
655 KeyFrameValue::KeyFrameValue(KeyFrameThread *thread,
656         KeyFrameWindow *window,
657         int x,
658         int y,
659         int w)
660  : BC_TextBox(x, 
661         y, 
662         w, 
663         1, 
664         (char*)"")
665 {
666         this->thread = thread;
667         this->window = window;
668 }
669
670 int KeyFrameValue::handle_event()
671 {
672         thread->apply_value();
673         return 0;
674 }
675
676 KeyFrameAll::KeyFrameAll(KeyFrameThread *thread,
677                 KeyFrameWindow *window, int x, int y)
678  : BC_CheckBox(x, y, 
679         thread->mwindow->session->keyframedialog_all, 
680         _("Apply to all selected keyframes"))
681 {
682         this->thread = thread;
683         this->window = window;
684 }
685
686 int KeyFrameAll::handle_event()
687 {
688         thread->mwindow->session->keyframedialog_all = get_value();
689         return 1;
690 }
691
692 #endif // EDIT_KEYFRAME
693
694
695 KeyFramePresetsList::KeyFramePresetsList(KeyFrameThread *thread,
696         KeyFrameWindow *window, int x, int y, int w, int h)
697  : BC_ListBox(x, y, w, h, LISTBOX_TEXT, thread->presets_data)
698 {
699         this->thread = thread;
700         this->window = window;
701 }
702
703 int KeyFramePresetsList::selection_changed()
704 {
705         int number = get_selection_number(0, 0);
706         if( number >= 0 ) {
707                 strcpy(thread->preset_text, thread->preset_titles.get(number));
708                 thread->is_factory = thread->is_factories.get(number);
709 // show title without factory symbol in the textbox
710                 window->preset_text->update(
711                         thread->presets_data->get(number)->get_text());
712                 window->update_editing();
713         }
714         
715         return 0;
716 }
717
718 int KeyFramePresetsList::handle_event()
719 {
720         thread->apply_preset(thread->preset_text, thread->is_factory);
721         window->set_done(0);
722         return 0;
723 }
724
725
726 KeyFramePresetsText::KeyFramePresetsText(KeyFrameThread *thread,
727         KeyFrameWindow *window, int x, int y, int w)
728  : BC_TextBox(x, y, w, 1, thread->preset_text)
729 {
730         this->thread = thread;
731         this->window = window;
732 }
733
734 // user entered a title
735 int KeyFramePresetsText::handle_event()
736 {
737         strcpy(thread->preset_text, get_text());
738 // once changed, it's now not a factory preset
739         thread->is_factory = 0;
740         window->update_editing();
741         return 0;
742 }
743
744
745 KeyFramePresetsDelete::KeyFramePresetsDelete(KeyFrameThread *thread,
746         KeyFrameWindow *window,
747         int x,
748         int y)
749  : BC_GenericButton(x, y, _("Delete"))
750 {
751         this->thread = thread;
752         this->window = window;
753 }
754
755 int KeyFramePresetsDelete::handle_event()
756 {
757         if( !thread->is_factory ) {
758                 thread->delete_preset(thread->preset_text, thread->is_factory);
759         }
760         return 1;
761 }
762
763
764 KeyFramePresetsSave::KeyFramePresetsSave(KeyFrameThread *thread,
765         KeyFrameWindow *window,
766         int x,
767         int y)
768 : BC_GenericButton(x, y, C_("Save"))
769 {
770         this->thread = thread;
771         this->window = window;
772 }
773
774 int KeyFramePresetsSave::handle_event()
775 {
776         if( !thread->is_factory ) {
777                 thread->save_preset(thread->preset_text, thread->is_factory);
778         }
779         return 1;
780 }
781
782
783 KeyFramePresetsApply::KeyFramePresetsApply(KeyFrameThread *thread,
784         KeyFrameWindow *window,
785         int x,
786         int y)
787  : BC_GenericButton(x, y, _("Apply"))
788 {
789         this->thread = thread;
790         this->window = window;
791 }
792
793 int KeyFramePresetsApply::handle_event()
794 {
795         thread->apply_preset(thread->preset_text, thread->is_factory);
796         return 1;
797 }
798
799
800 KeyFramePresetsOK::KeyFramePresetsOK(KeyFrameThread *thread,
801         KeyFrameWindow *window)
802  : BC_OKButton(window)
803 {
804         this->thread = thread;
805         this->window = window;
806 }
807
808 int KeyFramePresetsOK::keypress_event()
809 {
810         if( get_keypress() == RETURN ) {
811 // Apply the preset
812                 if( thread->presets_db->preset_exists(thread->plugin_title, 
813                         thread->preset_text, thread->is_factory) ) {
814                         window->set_done(0);
815                         return 1;
816                 }
817                 else {
818                         if( !thread->is_factory ) {
819                                 thread->save_preset(thread->preset_text, thread->is_factory);
820                                 return 1;
821                         }
822                 }
823         }
824         return 0;
825 }
826