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()
103 void SetFormatThread::run()
105 orig_dimension[0] = dimension[0] = mwindow->edl->session->output_w;
106 orig_dimension[1] = dimension[1] = mwindow->edl->session->output_h;
107 auto_aspect = mwindow->defaults->get("AUTOASPECT", 0);
109 ratio[0] = ratio[1] = 1;
111 new_settings = new EDL;
112 new_settings->create_objects();
113 new_settings->copy_session(mwindow->edl);
115 // This locks mwindow, so it must be done outside window_lock
116 int x = mwindow->gui->get_abs_cursor_x(1) - mwindow->theme->setformat_w / 2;
117 int y = mwindow->gui->get_abs_cursor_y(1) - mwindow->theme->setformat_h / 2;
119 window_lock->lock("SetFormatThread::run 1");
120 window = new SetFormatWindow(mwindow, this, x, y);
121 window->create_objects();
122 window_lock->unlock();
124 int result = window->run_window();
127 window_lock->lock("SetFormatThread::run 2");
130 window_lock->unlock();
138 mwindow->defaults->update("AUTOASPECT", auto_aspect);
139 new_settings->Garbage::remove_user();
142 void SetFormatThread::apply_changes()
144 double new_samplerate = new_settings->session->sample_rate;
145 double old_samplerate = mwindow->edl->session->sample_rate;
146 double new_framerate = new_settings->session->frame_rate;
147 double old_framerate = mwindow->edl->session->frame_rate;
148 int new_channels = new_settings->session->audio_channels;
149 CLAMP(new_channels, 1, MAXCHANNELS);
153 mwindow->undo->update_undo_before();
155 memcpy(&mwindow->preferences->channel_positions[MAXCHANNELS * (new_channels - 1)],
156 new_settings->session->achannel_positions,
157 sizeof(int) * MAXCHANNELS);
160 mwindow->edl->copy_session(new_settings, 1);
161 mwindow->edl->session->output_w = dimension[0];
162 mwindow->edl->session->output_h = dimension[1];
163 mwindow->edl->rechannel();
164 mwindow->edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
165 mwindow->edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
166 mwindow->save_backup();
167 mwindow->undo->update_undo_after(_("set format"), LOAD_ALL);
169 mwindow->resync_guis();
172 void SetFormatThread::update()
174 window->sample_rate->update(new_settings->session->sample_rate);
175 window->channels->update((int64_t)new_settings->session->audio_channels);
176 window->frame_rate->update((float)new_settings->session->frame_rate);
179 window->auto_aspect->update(0);
182 dimension[0] = new_settings->session->output_w;
183 window->dimension[0]->update((int64_t)dimension[0]);
184 dimension[1] = new_settings->session->output_h;
185 window->dimension[1]->update((int64_t)dimension[1]);
187 ratio[0] = (float)dimension[0] / orig_dimension[0];
188 window->ratio[0]->update(ratio[0]);
189 ratio[1] = (float)dimension[1] / orig_dimension[1];
190 window->ratio[1]->update(ratio[1]);
192 window->aspect_w->update(new_settings->session->aspect_w);
193 window->aspect_h->update(new_settings->session->aspect_h);
195 window->canvas->draw();
198 void SetFormatThread::update_window()
200 int i, result, modified_item = 0, dimension_modified = 0, ratio_modified = 0;
202 for(i = 0, result = 0; i < 2 && !result; i++)
209 dimension_modified = 1;
222 if(dimension_modified)
223 ratio[modified_item] = (float)dimension[modified_item] / orig_dimension[modified_item];
225 if(ratio_modified && !constrain_ratio)
227 dimension[modified_item] = (int)(orig_dimension[modified_item] * ratio[modified_item]);
228 window->dimension[modified_item]->update((int64_t)dimension[modified_item]);
231 for(i = 0; i < 2; i++)
233 if(dimension_modified ||
234 (i != modified_item && ratio_modified))
236 if(constrain_ratio) ratio[i] = ratio[modified_item];
237 window->ratio[i]->update(ratio[i]);
241 (i != modified_item && dimension_modified))
245 dimension[i] = (int)(orig_dimension[i] * ratio[modified_item]);
246 window->dimension[i]->update((int64_t)dimension[i]);
255 void SetFormatThread::update_aspect()
259 char string[BCTEXTLEN];
260 MWindow::create_aspect_ratio(new_settings->session->aspect_w,
261 new_settings->session->aspect_h,
264 sprintf(string, "%.02f", new_settings->session->aspect_w);
265 window->aspect_w->update(string);
266 sprintf(string, "%.02f", new_settings->session->aspect_h);
267 window->aspect_h->update(string);
279 SetFormatWindow::SetFormatWindow(MWindow *mwindow,
280 SetFormatThread *thread,
283 : BC_Window(_(PROGRAM_NAME ": Set Format"),
286 mwindow->theme->setformat_w,
287 mwindow->theme->setformat_h,
294 this->mwindow = mwindow;
295 this->thread = thread;
298 void SetFormatWindow::create_objects()
300 int x = 10, y = mwindow->theme->setformat_y1;
303 lock_window("SetFormatWindow::create_objects");
304 mwindow->theme->draw_setformat_bg(this);
308 presets = new SetFormatPresets(mwindow,
312 presets->create_objects();
316 y = mwindow->theme->setformat_y2;
318 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
322 y = mwindow->theme->setformat_y3;
323 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
327 y += mwindow->theme->setformat_margin;
328 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
331 add_subwindow(sample_rate = new SetSampleRateTextBox(thread,
332 mwindow->theme->setformat_x2,
334 add_subwindow(new SampleRatePulldown(mwindow,
336 mwindow->theme->setformat_x2 + sample_rate->get_w(),
339 y += mwindow->theme->setformat_margin;
340 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
343 add_subwindow(channels = new SetChannelsTextBox(thread,
344 mwindow->theme->setformat_x2,
346 add_subwindow(new BC_ITumbler(channels,
349 mwindow->theme->setformat_x2 + channels->get_w(),
352 y += mwindow->theme->setformat_margin;
353 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
355 _("Channel positions:")));
356 y += mwindow->theme->setformat_margin;
357 add_subwindow(canvas = new SetChannelsCanvas(mwindow,
359 mwindow->theme->setformat_channels_x,
360 mwindow->theme->setformat_channels_y,
361 mwindow->theme->setformat_channels_w,
362 mwindow->theme->setformat_channels_h));
373 y = mwindow->theme->setformat_y2;
374 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
379 y = mwindow->theme->setformat_y3;
380 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
383 add_subwindow(frame_rate = new SetFrameRateTextBox(thread,
384 mwindow->theme->setformat_x4,
386 add_subwindow(new FrameRatePulldown(mwindow,
388 mwindow->theme->setformat_x4 + frame_rate->get_w(),
391 y += mwindow->theme->setformat_margin;
392 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
396 y += mwindow->theme->setformat_margin;
397 add_subwindow(title = new BC_Title(mwindow->theme->setformat_x3, y, _("Width:")));
398 add_subwindow(dimension[0] = new ScaleSizeText(mwindow->theme->setformat_x4,
401 &(thread->dimension[0])));
403 y += mwindow->theme->setformat_margin;
404 add_subwindow(new BC_Title(mwindow->theme->setformat_x3, y, _("Height:")));
405 add_subwindow(dimension[1] = new ScaleSizeText(mwindow->theme->setformat_x4,
408 &(thread->dimension[1])));
410 x = mwindow->theme->setformat_x4 + dimension[0]->get_w();
411 FrameSizePulldown *pulldown;
412 add_subwindow(pulldown = new FrameSizePulldown(mwindow->theme,
416 y - mwindow->theme->setformat_margin));
418 add_subwindow(new FormatSwapExtents(mwindow,
421 x + pulldown->get_w() + 5,
422 y - mwindow->theme->setformat_margin));
424 y += mwindow->theme->setformat_margin;
425 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
428 add_subwindow(ratio[0] = new ScaleRatioText(mwindow->theme->setformat_x4,
431 &(thread->ratio[0])));
433 y += mwindow->theme->setformat_margin;
434 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
437 add_subwindow(ratio[1] = new ScaleRatioText(mwindow->theme->setformat_x4,
440 &(thread->ratio[1])));
442 y += mwindow->theme->setformat_margin;
443 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
446 x = mwindow->theme->setformat_x4;
447 add_subwindow(color_model = new BC_TextBox(x,
452 x += color_model->get_w();
453 add_subwindow(new ColormodelPulldown(mwindow,
455 &thread->new_settings->session->color_model,
459 y += mwindow->theme->setformat_margin;
460 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
462 _("Aspect ratio:")));
463 y += mwindow->theme->setformat_margin;
464 x = mwindow->theme->setformat_x3;
465 add_subwindow(aspect_w = new ScaleAspectText(x,
468 &(thread->new_settings->session->aspect_w)));
469 x += aspect_w->get_w() + 5;
470 add_subwindow(new BC_Title(x, y, _(":")));
472 add_subwindow(aspect_h = new ScaleAspectText(x,
475 &(thread->new_settings->session->aspect_h)));
476 x += aspect_h->get_w();
477 add_subwindow(new AspectPulldown(mwindow,
483 add_subwindow(auto_aspect = new ScaleAspectAuto(x, y, thread));
488 BC_CancelTextButton *cancel;
489 add_subwindow(ok = new BC_OKTextButton(this));
490 add_subwindow(cancel = new BC_CancelTextButton(this));
491 add_subwindow(new SetFormatApply((ok->get_x() + cancel->get_x()) / 2,
499 const char* SetFormatWindow::get_preset_text()
518 SetFormatPresets::SetFormatPresets(MWindow *mwindow,
519 SetFormatWindow *gui,
522 : FormatPresets(mwindow, 0, gui, x, y)
527 SetFormatPresets::~SetFormatPresets()
531 int SetFormatPresets::handle_event()
533 format_gui->thread->update();
537 EDL* SetFormatPresets::get_edl()
539 return format_gui->thread->new_settings;
556 SetSampleRateTextBox::SetSampleRateTextBox(SetFormatThread *thread, int x, int y)
557 : BC_TextBox(x, y, 100, 1, (int64_t)thread->new_settings->session->sample_rate)
559 this->thread = thread;
561 int SetSampleRateTextBox::handle_event()
563 thread->new_settings->session->sample_rate = CLIP(atol(get_text()), 1, 1000000);
567 SetChannelsTextBox::SetChannelsTextBox(SetFormatThread *thread, int x, int y)
568 : BC_TextBox(x, y, 100, 1, thread->new_settings->session->audio_channels)
570 this->thread = thread;
572 int SetChannelsTextBox::handle_event()
574 int new_channels = CLIP(atoi(get_text()), 1, MAXCHANNELS);
576 thread->new_settings->session->audio_channels = new_channels;
581 memcpy(thread->new_settings->session->achannel_positions,
582 &thread->mwindow->preferences->channel_positions[MAXCHANNELS * (new_channels - 1)],
583 sizeof(int) * MAXCHANNELS);
587 thread->window->canvas->draw();
592 SetChannelsCanvas::SetChannelsCanvas(MWindow *mwindow,
593 SetFormatThread *thread,
603 this->thread = thread;
604 this->mwindow = mwindow;
606 box_r = mwindow->theme->channel_position_data->get_w() / 2;
607 temp_picon = new VFrame(0,
609 mwindow->theme->channel_position_data->get_w(),
610 mwindow->theme->channel_position_data->get_h(),
611 mwindow->theme->channel_position_data->get_color_model(),
613 rotater = new RotateFrame(mwindow->preferences->processors,
614 mwindow->theme->channel_position_data->get_w(),
615 mwindow->theme->channel_position_data->get_h());
617 SetChannelsCanvas::~SetChannelsCanvas()
623 int SetChannelsCanvas::draw(int angle)
626 //int real_w = get_w() - box_r * 2;
627 //int real_h = get_h() - box_r * 2;
628 //int real_x = box_r;
629 //int real_y = box_r;
631 draw_top_background(get_top_level(), 0, 0, get_w(), get_h());
632 // draw_vframe(mwindow->theme->channel_bg_data, 0, 0);
639 set_color(mwindow->theme->channel_position_color);
640 for(int i = 0; i < thread->new_settings->session->audio_channels; i++)
642 get_dimensions(thread->new_settings->session->achannel_positions[i],
647 double rotate_angle = thread->new_settings->session->achannel_positions[i];
648 rotate_angle = -rotate_angle;
649 while(rotate_angle < 0) rotate_angle += 360;
650 rotater->rotate(temp_picon,
651 mwindow->theme->channel_position_data,
655 BC_Pixmap temp_pixmap(this,
659 draw_pixmap(&temp_pixmap, x, y);
660 sprintf(string, "%d", i + 1);
661 draw_text(x + 2, y + box_r * 2 - 2, string);
666 sprintf(string, _("%d degrees"), angle);
667 draw_text(this->get_w() / 2 - 40, this->get_h() / 2, string);
674 int SetChannelsCanvas::get_dimensions(int channel_position,
681 int real_w = this->get_w() - box_r * 2 - MARGIN;
682 int real_h = this->get_h() - box_r * 2 - MARGIN;
683 float corrected_position = channel_position;
684 if(corrected_position < 0) corrected_position += 360;
685 Units::polar_to_xy((float)corrected_position, real_w / 2, x, y);
686 x += real_w / 2 + MARGIN / 2;
687 y += real_h / 2 + MARGIN / 2;
693 int SetChannelsCanvas::button_press_event()
695 if(!cursor_inside()) return 0;
696 // get active channel
698 i < thread->new_settings->session->audio_channels;
702 get_dimensions(thread->new_settings->session->achannel_positions[i],
707 if(get_cursor_x() > x && get_cursor_y() > y &&
708 get_cursor_x() < x + w && get_cursor_y() < y + h)
711 degree_offset = (int)Units::xy_to_polar(get_cursor_x() - this->get_w() / 2, get_cursor_y() - this->get_h() / 2);
713 if(degree_offset >= 360) degree_offset -= 360;
714 degree_offset -= thread->new_settings->session->achannel_positions[i];
715 draw(thread->new_settings->session->achannel_positions[i]);
722 int SetChannelsCanvas::button_release_event()
724 if(active_channel >= 0)
733 int SetChannelsCanvas::cursor_motion_event()
735 if(active_channel >= 0)
737 // get degrees of new channel
739 new_d = (int)Units::xy_to_polar(get_cursor_x() - this->get_w() / 2, get_cursor_y() - this->get_h() / 2);
741 new_d -= degree_offset;
743 while(new_d >= 360) new_d -= 360;
744 while(new_d < 0) new_d += 360;
746 if(thread->new_settings->session->achannel_positions[active_channel] != new_d)
748 thread->new_settings->session->achannel_positions[active_channel] = new_d;
749 int new_channels = thread->new_settings->session->audio_channels;
750 memcpy(&thread->mwindow->preferences->channel_positions[MAXCHANNELS * (new_channels - 1)],
751 thread->new_settings->session->achannel_positions,
752 sizeof(int) * MAXCHANNELS);
753 draw(thread->new_settings->session->achannel_positions[active_channel]);
768 SetFrameRateTextBox::SetFrameRateTextBox(SetFormatThread *thread, int x, int y)
769 : BC_TextBox(x, y, 100, 1, (float)thread->new_settings->session->frame_rate)
771 this->thread = thread;
774 int SetFrameRateTextBox::handle_event()
776 thread->new_settings->session->frame_rate = Units::atoframerate(get_text());
782 // SetVChannels::SetVChannels(SetFormatThread *thread, int x, int y)
783 // : BC_TextBox(x, y, 100, 1, thread->channels)
785 // this->thread = thread;
787 // int SetVChannels::handle_event()
789 // thread->channels = atol(get_text());
796 ScaleSizeText::ScaleSizeText(int x, int y, SetFormatThread *thread, int *output)
797 : BC_TextBox(x, y, 100, 1, *output)
799 this->thread = thread;
800 this->output = output;
802 ScaleSizeText::~ScaleSizeText()
805 int ScaleSizeText::handle_event()
807 *output = atol(get_text());
810 if(*output <= 0) *output = 2;
811 if(*output > 10000) *output = 10000;
813 thread->update_window();
819 ScaleRatioText::ScaleRatioText(int x,
821 SetFormatThread *thread,
823 : BC_TextBox(x, y, 100, 1, *output)
825 this->thread = thread;
826 this->output = output;
828 ScaleRatioText::~ScaleRatioText()
831 int ScaleRatioText::handle_event()
833 *output = atof(get_text());
834 //if(*output <= 0) *output = 1;
835 if(*output > 10000) *output = 10000;
836 if(*output < -10000) *output = -10000;
838 thread->update_window();
844 ScaleAspectAuto::ScaleAspectAuto(int x, int y, SetFormatThread *thread)
845 : BC_CheckBox(x, y, thread->auto_aspect, _("Auto"))
847 this->thread = thread;
850 ScaleAspectAuto::~ScaleAspectAuto()
854 int ScaleAspectAuto::handle_event()
856 thread->auto_aspect = get_value();
857 thread->update_aspect();
861 ScaleAspectText::ScaleAspectText(int x, int y, SetFormatThread *thread, float *output)
862 : BC_TextBox(x, y, 70, 1, *output)
864 this->output = output;
865 this->thread = thread;
867 ScaleAspectText::~ScaleAspectText()
871 int ScaleAspectText::handle_event()
873 *output = atof(get_text());
882 SetFormatApply::SetFormatApply(int x, int y, SetFormatThread *thread)
883 : BC_GenericButton(x, y, _("Apply"))
885 this->thread = thread;
888 int SetFormatApply::handle_event()
890 thread->apply_changes();
906 FormatSwapExtents::FormatSwapExtents(MWindow *mwindow,
907 SetFormatThread *thread,
908 SetFormatWindow *gui,
911 : BC_Button(x, y, mwindow->theme->get_image_set("swap_extents"))
913 this->mwindow = mwindow;
914 this->thread = thread;
916 set_tooltip(_("Swap dimensions"));
919 int FormatSwapExtents::handle_event()
921 int w = thread->dimension[0];
922 int h = thread->dimension[1];
923 thread->dimension[0] = -h;
924 gui->dimension[0]->update((int64_t)h);
925 gui->dimension[1]->update((int64_t)w);
926 thread->update_window();
927 thread->dimension[1] = -w;
928 thread->update_window();