4 * Copyright (C) 1997-2012 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
24 #include "cwindowgui.h"
28 #include "edlsession.h"
29 #include "formatpresets.h"
31 #include "levelwindow.h"
32 #include "levelwindowgui.h"
33 #include "mainerror.h"
37 #include "mwindowgui.h"
39 #include "preferences.h"
40 #include "rotateframe.h"
41 #include "setformat.h"
45 #include "vwindowgui.h"
49 SetFormat::SetFormat(MWindow *mwindow)
50 : BC_MenuItem(_("Format..."), "Shift-F", 'F')
53 this->mwindow = mwindow;
54 thread = new SetFormatThread(mwindow);
57 SetFormat::~SetFormat()
63 int SetFormat::handle_event()
65 if(!thread->running())
71 // window_lock has to be locked but window can't be locked until after
72 // it is known to exist, so we neglect window_lock for now
75 thread->window_lock->lock("SetFormat::handle_event");
76 thread->window->lock_window("SetFormat::handle_event");
77 thread->window->raise_window();
78 thread->window->unlock_window();
79 thread->window_lock->unlock();
85 SetFormatThread::SetFormatThread(MWindow *mwindow)
88 this->mwindow = mwindow;
89 window_lock = new Mutex("SetFormatThread::window_lock");
93 SetFormatThread::~SetFormatThread()
102 void SetFormatThread::run()
104 orig_dimension[0] = dimension[0] = mwindow->edl->session->output_w;
105 orig_dimension[1] = dimension[1] = mwindow->edl->session->output_h;
106 auto_aspect = mwindow->defaults->get("AUTOASPECT", 0);
108 ratio[0] = ratio[1] = 1;
110 new_settings = new EDL;
111 new_settings->create_objects();
112 new_settings->copy_session(mwindow->edl);
114 // This locks mwindow, so it must be done outside window_lock
115 int x = mwindow->gui->get_abs_cursor_x(1) - mwindow->theme->setformat_w / 2;
116 int y = mwindow->gui->get_abs_cursor_y(1) - mwindow->theme->setformat_h / 2;
118 window_lock->lock("SetFormatThread::run 1");
119 window = new SetFormatWindow(mwindow, this, x, y);
120 window->create_objects();
121 window_lock->unlock();
123 int result = window->run_window();
126 window_lock->lock("SetFormatThread::run 2");
129 window_lock->unlock();
137 mwindow->defaults->update("AUTOASPECT", auto_aspect);
138 new_settings->Garbage::remove_user();
141 void SetFormatThread::apply_changes()
143 double new_samplerate = new_settings->session->sample_rate;
144 double old_samplerate = mwindow->edl->session->sample_rate;
145 double new_framerate = new_settings->session->frame_rate;
146 double old_framerate = mwindow->edl->session->frame_rate;
147 int new_channels = new_settings->session->audio_channels;
148 CLAMP(new_channels, 1, MAXCHANNELS);
152 mwindow->undo->update_undo_before();
154 memcpy(&mwindow->preferences->channel_positions[MAXCHANNELS * (new_channels - 1)],
155 new_settings->session->achannel_positions,
156 sizeof(int) * MAXCHANNELS);
159 mwindow->edl->copy_session(new_settings, 1);
160 mwindow->edl->session->output_w = dimension[0];
161 mwindow->edl->session->output_h = dimension[1];
162 mwindow->edl->rechannel();
163 mwindow->edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
164 mwindow->edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
165 mwindow->save_backup();
166 mwindow->undo->update_undo_after(_("set format"), LOAD_ALL);
168 mwindow->resync_guis();
171 void SetFormatThread::update()
173 window->sample_rate->update(new_settings->session->sample_rate);
174 window->channels->update((int64_t)new_settings->session->audio_channels);
175 window->frame_rate->update((float)new_settings->session->frame_rate);
178 window->auto_aspect->update(0);
181 dimension[0] = new_settings->session->output_w;
182 window->dimension[0]->update((int64_t)dimension[0]);
183 dimension[1] = new_settings->session->output_h;
184 window->dimension[1]->update((int64_t)dimension[1]);
186 ratio[0] = (float)dimension[0] / orig_dimension[0];
187 window->ratio[0]->update(ratio[0]);
188 ratio[1] = (float)dimension[1] / orig_dimension[1];
189 window->ratio[1]->update(ratio[1]);
191 window->aspect_w->update(new_settings->session->aspect_w);
192 window->aspect_h->update(new_settings->session->aspect_h);
194 window->canvas->draw();
197 void SetFormatThread::update_window()
199 int i, result, modified_item = 0, dimension_modified = 0, ratio_modified = 0;
201 for(i = 0, result = 0; i < 2 && !result; i++)
208 dimension_modified = 1;
221 if(dimension_modified)
222 ratio[modified_item] = (float)dimension[modified_item] / orig_dimension[modified_item];
224 if(ratio_modified && !constrain_ratio)
226 dimension[modified_item] = (int)(orig_dimension[modified_item] * ratio[modified_item]);
227 window->dimension[modified_item]->update((int64_t)dimension[modified_item]);
230 for(i = 0; i < 2; i++)
232 if(dimension_modified ||
233 (i != modified_item && ratio_modified))
235 if(constrain_ratio) ratio[i] = ratio[modified_item];
236 window->ratio[i]->update(ratio[i]);
240 (i != modified_item && dimension_modified))
244 dimension[i] = (int)(orig_dimension[i] * ratio[modified_item]);
245 window->dimension[i]->update((int64_t)dimension[i]);
254 void SetFormatThread::update_aspect()
258 char string[BCTEXTLEN];
259 MWindow::create_aspect_ratio(new_settings->session->aspect_w,
260 new_settings->session->aspect_h,
263 sprintf(string, "%.02f", new_settings->session->aspect_w);
264 window->aspect_w->update(string);
265 sprintf(string, "%.02f", new_settings->session->aspect_h);
266 window->aspect_h->update(string);
278 SetFormatWindow::SetFormatWindow(MWindow *mwindow,
279 SetFormatThread *thread,
282 : BC_Window(PROGRAM_NAME ": Set Format",
285 mwindow->theme->setformat_w,
286 mwindow->theme->setformat_h,
293 this->mwindow = mwindow;
294 this->thread = thread;
297 void SetFormatWindow::create_objects()
299 int x = 10, y = mwindow->theme->setformat_y1;
302 lock_window("SetFormatWindow::create_objects");
303 mwindow->theme->draw_setformat_bg(this);
307 presets = new SetFormatPresets(mwindow,
311 presets->create_objects();
315 y = mwindow->theme->setformat_y2;
317 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
321 y = mwindow->theme->setformat_y3;
322 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
326 y += mwindow->theme->setformat_margin;
327 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
330 add_subwindow(sample_rate = new SetSampleRateTextBox(thread,
331 mwindow->theme->setformat_x2,
333 add_subwindow(new SampleRatePulldown(mwindow,
335 mwindow->theme->setformat_x2 + sample_rate->get_w(),
338 y += mwindow->theme->setformat_margin;
339 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
342 add_subwindow(channels = new SetChannelsTextBox(thread,
343 mwindow->theme->setformat_x2,
345 add_subwindow(new BC_ITumbler(channels,
348 mwindow->theme->setformat_x2 + channels->get_w(),
351 y += mwindow->theme->setformat_margin;
352 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
354 _("Channel positions:")));
355 y += mwindow->theme->setformat_margin;
356 add_subwindow(canvas = new SetChannelsCanvas(mwindow,
358 mwindow->theme->setformat_channels_x,
359 mwindow->theme->setformat_channels_y,
360 mwindow->theme->setformat_channels_w,
361 mwindow->theme->setformat_channels_h));
372 y = mwindow->theme->setformat_y2;
373 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
378 y = mwindow->theme->setformat_y3;
379 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
382 add_subwindow(frame_rate = new SetFrameRateTextBox(thread,
383 mwindow->theme->setformat_x4,
385 add_subwindow(new FrameRatePulldown(mwindow,
387 mwindow->theme->setformat_x4 + frame_rate->get_w(),
390 y += mwindow->theme->setformat_margin;
391 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
395 y += mwindow->theme->setformat_margin;
396 add_subwindow(title = new BC_Title(mwindow->theme->setformat_x3, y, _("Width:")));
397 add_subwindow(dimension[0] = new ScaleSizeText(mwindow->theme->setformat_x4,
400 &(thread->dimension[0])));
402 y += mwindow->theme->setformat_margin;
403 add_subwindow(new BC_Title(mwindow->theme->setformat_x3, y, _("Height:")));
404 add_subwindow(dimension[1] = new ScaleSizeText(mwindow->theme->setformat_x4,
407 &(thread->dimension[1])));
409 x = mwindow->theme->setformat_x4 + dimension[0]->get_w();
410 FrameSizePulldown *pulldown;
411 add_subwindow(pulldown = new FrameSizePulldown(mwindow->theme,
415 y - mwindow->theme->setformat_margin));
417 add_subwindow(new FormatSwapExtents(mwindow,
420 x + pulldown->get_w() + 5,
421 y - mwindow->theme->setformat_margin));
423 y += mwindow->theme->setformat_margin;
424 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
427 add_subwindow(ratio[0] = new ScaleRatioText(mwindow->theme->setformat_x4,
430 &(thread->ratio[0])));
432 y += mwindow->theme->setformat_margin;
433 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
436 add_subwindow(ratio[1] = new ScaleRatioText(mwindow->theme->setformat_x4,
439 &(thread->ratio[1])));
441 y += mwindow->theme->setformat_margin;
442 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
445 x = mwindow->theme->setformat_x4;
446 add_subwindow(color_model = new BC_TextBox(x,
451 x += color_model->get_w();
452 add_subwindow(new ColormodelPulldown(mwindow,
454 &thread->new_settings->session->color_model,
458 y += mwindow->theme->setformat_margin;
459 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
461 _("Aspect ratio:")));
462 y += mwindow->theme->setformat_margin;
463 x = mwindow->theme->setformat_x3;
464 add_subwindow(aspect_w = new ScaleAspectText(x,
467 &(thread->new_settings->session->aspect_w)));
468 x += aspect_w->get_w() + 5;
469 add_subwindow(new BC_Title(x, y, _(":")));
471 add_subwindow(aspect_h = new ScaleAspectText(x,
474 &(thread->new_settings->session->aspect_h)));
475 x += aspect_h->get_w();
476 add_subwindow(new AspectPulldown(mwindow,
482 add_subwindow(auto_aspect = new ScaleAspectAuto(x, y, thread));
487 BC_CancelTextButton *cancel;
488 add_subwindow(ok = new BC_OKTextButton(this));
489 add_subwindow(cancel = new BC_CancelTextButton(this));
490 add_subwindow(new SetFormatApply((ok->get_x() + cancel->get_x()) / 2,
498 const char* SetFormatWindow::get_preset_text()
517 SetFormatPresets::SetFormatPresets(MWindow *mwindow,
518 SetFormatWindow *gui,
521 : FormatPresets(mwindow, 0, gui, x, y)
526 SetFormatPresets::~SetFormatPresets()
530 int SetFormatPresets::handle_event()
532 format_gui->thread->update();
536 EDL* SetFormatPresets::get_edl()
538 return format_gui->thread->new_settings;
555 SetSampleRateTextBox::SetSampleRateTextBox(SetFormatThread *thread, int x, int y)
556 : BC_TextBox(x, y, 100, 1, (int64_t)thread->new_settings->session->sample_rate)
558 this->thread = thread;
560 int SetSampleRateTextBox::handle_event()
562 thread->new_settings->session->sample_rate = CLIP(atol(get_text()), 1, 1000000);
566 SetChannelsTextBox::SetChannelsTextBox(SetFormatThread *thread, int x, int y)
567 : BC_TextBox(x, y, 100, 1, thread->new_settings->session->audio_channels)
569 this->thread = thread;
571 int SetChannelsTextBox::handle_event()
573 int new_channels = CLIP(atoi(get_text()), 1, MAXCHANNELS);
575 thread->new_settings->session->audio_channels = new_channels;
580 memcpy(thread->new_settings->session->achannel_positions,
581 &thread->mwindow->preferences->channel_positions[MAXCHANNELS * (new_channels - 1)],
582 sizeof(int) * MAXCHANNELS);
586 thread->window->canvas->draw();
591 SetChannelsCanvas::SetChannelsCanvas(MWindow *mwindow,
592 SetFormatThread *thread,
602 this->thread = thread;
603 this->mwindow = mwindow;
605 box_r = mwindow->theme->channel_position_data->get_w() / 2;
606 temp_picon = new VFrame(0,
608 mwindow->theme->channel_position_data->get_w(),
609 mwindow->theme->channel_position_data->get_h(),
610 mwindow->theme->channel_position_data->get_color_model(),
612 rotater = new RotateFrame(mwindow->preferences->processors,
613 mwindow->theme->channel_position_data->get_w(),
614 mwindow->theme->channel_position_data->get_h());
616 SetChannelsCanvas::~SetChannelsCanvas()
622 int SetChannelsCanvas::draw(int angle)
625 //int real_w = get_w() - box_r * 2;
626 //int real_h = get_h() - box_r * 2;
627 //int real_x = box_r;
628 //int real_y = box_r;
630 draw_top_background(get_top_level(), 0, 0, get_w(), get_h());
631 // draw_vframe(mwindow->theme->channel_bg_data, 0, 0);
638 set_color(mwindow->theme->channel_position_color);
639 for(int i = 0; i < thread->new_settings->session->audio_channels; i++)
641 get_dimensions(thread->new_settings->session->achannel_positions[i],
646 double rotate_angle = thread->new_settings->session->achannel_positions[i];
647 rotate_angle = -rotate_angle;
648 while(rotate_angle < 0) rotate_angle += 360;
649 rotater->rotate(temp_picon,
650 mwindow->theme->channel_position_data,
654 BC_Pixmap temp_pixmap(this,
658 draw_pixmap(&temp_pixmap, x, y);
659 sprintf(string, "%d", i + 1);
660 draw_text(x + 2, y + box_r * 2 - 2, string);
665 sprintf(string, _("%d degrees"), angle);
666 draw_text(this->get_w() / 2 - 40, this->get_h() / 2, string);
673 int SetChannelsCanvas::get_dimensions(int channel_position,
680 int real_w = this->get_w() - box_r * 2 - MARGIN;
681 int real_h = this->get_h() - box_r * 2 - MARGIN;
682 float corrected_position = channel_position;
683 if(corrected_position < 0) corrected_position += 360;
684 Units::polar_to_xy((float)corrected_position, real_w / 2, x, y);
685 x += real_w / 2 + MARGIN / 2;
686 y += real_h / 2 + MARGIN / 2;
692 int SetChannelsCanvas::button_press_event()
694 if(!cursor_inside()) return 0;
695 // get active channel
697 i < thread->new_settings->session->audio_channels;
701 get_dimensions(thread->new_settings->session->achannel_positions[i],
706 if(get_cursor_x() > x && get_cursor_y() > y &&
707 get_cursor_x() < x + w && get_cursor_y() < y + h)
710 degree_offset = (int)Units::xy_to_polar(get_cursor_x() - this->get_w() / 2, get_cursor_y() - this->get_h() / 2);
712 if(degree_offset >= 360) degree_offset -= 360;
713 degree_offset -= thread->new_settings->session->achannel_positions[i];
714 draw(thread->new_settings->session->achannel_positions[i]);
721 int SetChannelsCanvas::button_release_event()
723 if(active_channel >= 0)
732 int SetChannelsCanvas::cursor_motion_event()
734 if(active_channel >= 0)
736 // get degrees of new channel
738 new_d = (int)Units::xy_to_polar(get_cursor_x() - this->get_w() / 2, get_cursor_y() - this->get_h() / 2);
740 new_d -= degree_offset;
742 while(new_d >= 360) new_d -= 360;
743 while(new_d < 0) new_d += 360;
745 if(thread->new_settings->session->achannel_positions[active_channel] != new_d)
747 thread->new_settings->session->achannel_positions[active_channel] = new_d;
748 int new_channels = thread->new_settings->session->audio_channels;
749 memcpy(&thread->mwindow->preferences->channel_positions[MAXCHANNELS * (new_channels - 1)],
750 thread->new_settings->session->achannel_positions,
751 sizeof(int) * MAXCHANNELS);
752 draw(thread->new_settings->session->achannel_positions[active_channel]);
767 SetFrameRateTextBox::SetFrameRateTextBox(SetFormatThread *thread, int x, int y)
768 : BC_TextBox(x, y, 100, 1, (float)thread->new_settings->session->frame_rate)
770 this->thread = thread;
773 int SetFrameRateTextBox::handle_event()
775 thread->new_settings->session->frame_rate = Units::atoframerate(get_text());
781 // SetVChannels::SetVChannels(SetFormatThread *thread, int x, int y)
782 // : BC_TextBox(x, y, 100, 1, thread->channels)
784 // this->thread = thread;
786 // int SetVChannels::handle_event()
788 // thread->channels = atol(get_text());
795 ScaleSizeText::ScaleSizeText(int x, int y, SetFormatThread *thread, int *output)
796 : BC_TextBox(x, y, 100, 1, *output)
798 this->thread = thread;
799 this->output = output;
801 ScaleSizeText::~ScaleSizeText()
804 int ScaleSizeText::handle_event()
806 *output = atol(get_text());
809 if(*output <= 0) *output = 2;
810 if(*output > 10000) *output = 10000;
812 thread->update_window();
818 ScaleRatioText::ScaleRatioText(int x,
820 SetFormatThread *thread,
822 : BC_TextBox(x, y, 100, 1, *output)
824 this->thread = thread;
825 this->output = output;
827 ScaleRatioText::~ScaleRatioText()
830 int ScaleRatioText::handle_event()
832 *output = atof(get_text());
833 //if(*output <= 0) *output = 1;
834 if(*output > 10000) *output = 10000;
835 if(*output < -10000) *output = -10000;
837 thread->update_window();
843 ScaleAspectAuto::ScaleAspectAuto(int x, int y, SetFormatThread *thread)
844 : BC_CheckBox(x, y, thread->auto_aspect, _("Auto"))
846 this->thread = thread;
849 ScaleAspectAuto::~ScaleAspectAuto()
853 int ScaleAspectAuto::handle_event()
855 thread->auto_aspect = get_value();
856 thread->update_aspect();
860 ScaleAspectText::ScaleAspectText(int x, int y, SetFormatThread *thread, float *output)
861 : BC_TextBox(x, y, 70, 1, *output)
863 this->output = output;
864 this->thread = thread;
866 ScaleAspectText::~ScaleAspectText()
870 int ScaleAspectText::handle_event()
872 *output = atof(get_text());
881 SetFormatApply::SetFormatApply(int x, int y, SetFormatThread *thread)
882 : BC_GenericButton(x, y, _("Apply"))
884 this->thread = thread;
887 int SetFormatApply::handle_event()
889 thread->apply_changes();
905 FormatSwapExtents::FormatSwapExtents(MWindow *mwindow,
906 SetFormatThread *thread,
907 SetFormatWindow *gui,
910 : BC_Button(x, y, mwindow->theme->get_image_set("swap_extents"))
912 this->mwindow = mwindow;
913 this->thread = thread;
915 set_tooltip("Swap dimensions");
918 int FormatSwapExtents::handle_event()
920 int w = thread->dimension[0];
921 int h = thread->dimension[1];
922 thread->dimension[0] = -h;
923 gui->dimension[0]->update((int64_t)h);
924 gui->dimension[1]->update((int64_t)w);
925 thread->update_window();
926 thread->dimension[1] = -w;
927 thread->update_window();