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()
62 int SetFormat::handle_event()
64 if(!thread->running())
70 // window_lock has to be locked but window can't be locked until after
71 // it is known to exist, so we neglect window_lock for now
74 thread->window_lock->lock("SetFormat::handle_event");
75 thread->window->lock_window("SetFormat::handle_event");
76 thread->window->raise_window();
77 thread->window->unlock_window();
78 thread->window_lock->unlock();
84 SetFormatThread::SetFormatThread(MWindow *mwindow)
87 this->mwindow = mwindow;
88 window_lock = new Mutex("SetFormatThread::window_lock");
92 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[new_channels - 1],
155 new_settings->session->achannel_positions,
156 sizeof(mwindow->preferences->channel_positions[new_channels - 1]));
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->retrack();
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);
194 window->interlace_pulldown->update(new_settings->session->interlace_mode);
195 window->color_model->update_value(new_settings->session->color_model);
197 window->canvas->draw();
200 void SetFormatThread::update_window()
202 int i, result, modified_item = 0, dimension_modified = 0, ratio_modified = 0;
204 for(i = 0, result = 0; i < 2 && !result; i++)
211 dimension_modified = 1;
224 if(dimension_modified)
225 ratio[modified_item] = (float)dimension[modified_item] / orig_dimension[modified_item];
227 if(ratio_modified && !constrain_ratio)
229 dimension[modified_item] = (int)(orig_dimension[modified_item] * ratio[modified_item]);
230 window->dimension[modified_item]->update((int64_t)dimension[modified_item]);
233 for(i = 0; i < 2; i++)
235 if(dimension_modified ||
236 (i != modified_item && ratio_modified))
238 if(constrain_ratio) ratio[i] = ratio[modified_item];
239 window->ratio[i]->update(ratio[i]);
243 (i != modified_item && dimension_modified))
247 dimension[i] = (int)(orig_dimension[i] * ratio[modified_item]);
248 window->dimension[i]->update((int64_t)dimension[i]);
257 void SetFormatThread::update_aspect()
261 char string[BCTEXTLEN];
262 MWindow::create_aspect_ratio(new_settings->session->aspect_w,
263 new_settings->session->aspect_h,
266 sprintf(string, "%.02f", new_settings->session->aspect_w);
267 window->aspect_w->update(string);
268 sprintf(string, "%.02f", new_settings->session->aspect_h);
269 window->aspect_h->update(string);
274 SetFormatWindow::SetFormatWindow(MWindow *mwindow,
275 SetFormatThread *thread, int x, int y)
276 : BC_Window(_(PROGRAM_NAME ": Set Format"), x, y,
277 mwindow->theme->setformat_w, mwindow->theme->setformat_h,
280 this->mwindow = mwindow;
281 this->thread = thread;
284 SetFormatWindow::~SetFormatWindow()
289 void SetFormatWindow::create_objects()
291 lock_window("SetFormatWindow::create_objects");
292 int x = xS(10), y = mwindow->theme->setformat_y1;
295 mwindow->theme->draw_setformat_bg(this);
297 presets = new SetFormatPresets(mwindow, this, x, y);
298 presets->create_objects();
299 x = presets->x; // y = presets->y;
300 y = mwindow->theme->setformat_y2;
302 add_subwindow(new BC_Title(mwindow->theme->setformat_x1, y,
303 _("Audio"), LARGEFONT));
304 y = mwindow->theme->setformat_y3;
306 add_subwindow(new BC_Title(mwindow->theme->setformat_x1, y,
308 add_subwindow(sample_rate = new SetSampleRateTextBox(thread,
309 mwindow->theme->setformat_x2,
311 add_subwindow(new SampleRatePulldown(mwindow, sample_rate,
312 mwindow->theme->setformat_x2 + sample_rate->get_w(), y));
314 y += mwindow->theme->setformat_margin;
315 add_subwindow(new BC_Title(mwindow->theme->setformat_x1, y,
317 add_subwindow(channels = new SetChannelsTextBox(thread,
318 mwindow->theme->setformat_x2, y));
319 add_subwindow(new BC_ITumbler(channels, 1, MAXCHANNELS,
320 mwindow->theme->setformat_x2 + channels->get_w(), y));
322 y += mwindow->theme->setformat_margin;
323 add_subwindow(new BC_Title(mwindow->theme->setformat_x1, y,
324 _("Channel positions:")));
325 y += mwindow->theme->setformat_margin;
326 add_subwindow(channels_reset = new SetChannelsReset(thread,
327 mwindow->theme->setformat_x1, y,
329 add_subwindow(canvas = new SetChannelsCanvas(mwindow,
331 mwindow->theme->setformat_channels_x,
332 mwindow->theme->setformat_channels_y,
333 mwindow->theme->setformat_channels_w,
334 mwindow->theme->setformat_channels_h));
339 y = mwindow->theme->setformat_y2;
340 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
345 y = mwindow->theme->setformat_y3;
346 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
349 add_subwindow(frame_rate = new SetFrameRateTextBox(thread,
350 mwindow->theme->setformat_x4,
352 add_subwindow(new FrameRatePulldown(mwindow,
354 mwindow->theme->setformat_x4 + frame_rate->get_w(),
357 y += mwindow->theme->setformat_margin;
358 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
362 y += mwindow->theme->setformat_margin;
363 add_subwindow(title = new BC_Title(mwindow->theme->setformat_x3, y, _("Width:")));
364 add_subwindow(dimension[0] = new ScaleSizeText(mwindow->theme->setformat_x4,
367 &(thread->dimension[0])));
369 y += mwindow->theme->setformat_margin;
370 add_subwindow(new BC_Title(mwindow->theme->setformat_x3, y, _("Height:")));
371 add_subwindow(dimension[1] = new ScaleSizeText(mwindow->theme->setformat_x4,
374 &(thread->dimension[1])));
376 x = mwindow->theme->setformat_x4 + dimension[0]->get_w();
377 FrameSizePulldown *pulldown;
378 add_subwindow(pulldown = new FrameSizePulldown(mwindow->theme,
382 y - mwindow->theme->setformat_margin));
384 add_subwindow(new FormatSwapExtents(mwindow,
387 x + pulldown->get_w() + 5,
388 y - mwindow->theme->setformat_margin));
390 y += mwindow->theme->setformat_margin;
391 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
394 add_subwindow(ratio[0] = new ScaleRatioText(mwindow->theme->setformat_x4,
397 &(thread->ratio[0])));
399 y += mwindow->theme->setformat_margin;
400 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
403 add_subwindow(ratio[1] = new ScaleRatioText(mwindow->theme->setformat_x4,
406 &(thread->ratio[1])));
408 y += mwindow->theme->setformat_margin;
409 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
412 x = mwindow->theme->setformat_x4;
413 add_subwindow(textbox = new BC_TextBox(x, y, xS(100), 1, ""));
414 x += textbox->get_w();
415 add_subwindow(color_model = new ColormodelPulldown(mwindow, textbox,
416 &thread->new_settings->session->color_model, x, y));
418 y += mwindow->theme->setformat_margin;
419 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
421 _("Aspect ratio:")));
422 y += mwindow->theme->setformat_margin;
423 x = mwindow->theme->setformat_x3;
424 add_subwindow(aspect_w = new ScaleAspectText(x, y, thread,
425 &(thread->new_settings->session->aspect_w)));
426 x += aspect_w->get_w() + xS(5);
427 add_subwindow(new BC_Title(x, y, ":"));
429 add_subwindow(aspect_h = new ScaleAspectText(x,
432 &(thread->new_settings->session->aspect_h)));
433 x += aspect_h->get_w();
434 add_subwindow(new AspectPulldown(mwindow,
440 add_subwindow(auto_aspect = new ScaleAspectAuto(x, y, thread));
441 y += mwindow->theme->setformat_margin;
443 // --------------------
444 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
446 _("Interlace mode:")));
447 add_subwindow(textbox = new BC_TextBox(mwindow->theme->setformat_x4, y,
449 add_subwindow(interlace_pulldown = new InterlacemodePulldown(mwindow,
450 textbox, &(thread->new_settings->session->interlace_mode),
451 (ArrayList<BC_ListBoxItem*>*)&mwindow->interlace_project_modes,
452 mwindow->theme->setformat_x4 + textbox->get_w(), y));
453 y += mwindow->theme->setformat_margin;
456 BC_CancelTextButton *cancel;
457 add_subwindow(ok = new BC_OKTextButton(this));
458 add_subwindow(cancel = new BC_CancelTextButton(this));
459 add_subwindow(new SetFormatApply((ok->get_x() + cancel->get_x()) / 2,
460 ok->get_y(), thread));
466 const char* SetFormatWindow::get_preset_text()
472 SetFormatPresets::SetFormatPresets(MWindow *mwindow,
473 SetFormatWindow *gui,
476 : FormatPresets(mwindow, 0, gui, x, y)
481 SetFormatPresets::~SetFormatPresets()
485 int SetFormatPresets::handle_event()
487 format_gui->thread->update();
488 return format_gui->channels_reset->handle_event();
491 EDL* SetFormatPresets::get_edl()
493 return format_gui->thread->new_settings;
498 SetSampleRateTextBox::SetSampleRateTextBox(SetFormatThread *thread, int x, int y)
499 : BC_TextBox(x, y, xS(100), 1, (int64_t)thread->new_settings->session->sample_rate)
501 this->thread = thread;
503 int SetSampleRateTextBox::handle_event()
505 thread->new_settings->session->sample_rate = CLIP(atol(get_text()), 1, 1000000);
509 SetChannelsTextBox::SetChannelsTextBox(SetFormatThread *thread, int x, int y)
510 : BC_TextBox(x, y, xS(100), 1, thread->new_settings->session->audio_channels)
512 this->thread = thread;
514 int SetChannelsTextBox::handle_event()
516 int new_channels = CLIP(atoi(get_text()), 1, MAXCHANNELS);
518 thread->new_settings->session->audio_channels = new_channels;
523 memcpy(thread->new_settings->session->achannel_positions,
524 &thread->mwindow->preferences->channel_positions[new_channels - 1],
525 sizeof(thread->new_settings->session->achannel_positions));
529 thread->window->canvas->draw();
533 SetChannelsReset::SetChannelsReset(SetFormatThread *thread, int x, int y, const char *text)
534 : BC_GenericButton(x, y, text)
536 this->thread = thread;
539 int SetChannelsReset::handle_event()
541 int channels = thread->new_settings->session->audio_channels;
542 int *achannels = thread->new_settings->session->achannel_positions;
543 for( int i=0; i<MAX_CHANNELS; ++i )
544 achannels[i] = default_audio_channel_position(i, channels);
545 thread->window->canvas->draw();
549 SetChannelsCanvas::SetChannelsCanvas(MWindow *mwindow,
550 SetFormatThread *thread, int x, int y, int w, int h)
551 : BC_SubWindow(x, y, w, h)
553 this->thread = thread;
554 this->mwindow = mwindow;
556 box_r = mwindow->theme->channel_position_data->get_w() / 2;
557 temp_picon = new VFrame(
558 mwindow->theme->channel_position_data->get_w(),
559 mwindow->theme->channel_position_data->get_h(),
560 mwindow->theme->channel_position_data->get_color_model(),
562 rotater = new RotateFrame(mwindow->preferences->processors,
563 mwindow->theme->channel_position_data->get_w(),
564 mwindow->theme->channel_position_data->get_h());
566 SetChannelsCanvas::~SetChannelsCanvas()
572 int SetChannelsCanvas::draw(int angle)
575 //int real_w = get_w() - box_r * 2;
576 //int real_h = get_h() - box_r * 2;
577 //int real_x = box_r;
578 //int real_y = box_r;
580 draw_top_background(get_top_level(), 0, 0, get_w(), get_h());
581 // draw_vframe(mwindow->theme->channel_bg_data, 0, 0);
588 set_color(mwindow->theme->channel_position_color);
589 for(int i = 0; i < thread->new_settings->session->audio_channels; i++)
591 get_dimensions(thread->new_settings->session->achannel_positions[i],
593 double rotate_angle = thread->new_settings->session->achannel_positions[i];
594 rotate_angle = -rotate_angle;
595 while(rotate_angle < 0) rotate_angle += 360;
596 rotater->rotate(temp_picon,
597 mwindow->theme->channel_position_data,
601 BC_Pixmap temp_pixmap(this,
605 draw_pixmap(&temp_pixmap, x, y);
606 sprintf(string, "%d", i + 1);
607 draw_text(x + 2, y + box_r * 2 - 2, string);
612 sprintf(string, _("%d degrees"), angle);
613 draw_text(this->get_w() / 2 - 40, this->get_h() / 2, string);
620 int SetChannelsCanvas::get_dimensions(int channel_position,
621 int &x, int &y, int &w, int &h)
623 int xs10 = xS(10), ys10 = yS(10);
624 int real_w = this->get_w() - box_r * 2 - xs10;
625 int real_h = this->get_h() - box_r * 2 - ys10;
626 float corrected_position = channel_position;
627 if(corrected_position < 0) corrected_position += 360;
628 Units::polar_to_xy((float)corrected_position, real_w / 2, x, y);
629 x += real_w / 2 + xs10 / 2;
630 y += real_h / 2 + ys10 / 2;
636 int SetChannelsCanvas::button_press_event()
638 if(!cursor_inside()) return 0;
639 // get active channel
640 for( int i = 0; i < thread->new_settings->session->audio_channels; i++ ) {
642 get_dimensions(thread->new_settings->session->achannel_positions[i], x, y, w, h);
643 if( get_cursor_x() > x && get_cursor_y() > y &&
644 get_cursor_x() < x + w && get_cursor_y() < y + h ) {
646 degree_offset = (int)Units::xy_to_polar(get_cursor_x() - this->get_w() / 2, get_cursor_y() - this->get_h() / 2);
648 if(degree_offset >= 360) degree_offset -= 360;
649 degree_offset -= thread->new_settings->session->achannel_positions[i];
650 draw(thread->new_settings->session->achannel_positions[i]);
657 int SetChannelsCanvas::button_release_event()
659 if(active_channel >= 0)
668 int SetChannelsCanvas::cursor_motion_event()
670 if(active_channel >= 0)
672 // get degrees of new channel
674 new_d = (int)Units::xy_to_polar(get_cursor_x() - this->get_w() / 2, get_cursor_y() - this->get_h() / 2);
676 new_d -= degree_offset;
678 while(new_d >= 360) new_d -= 360;
679 while(new_d < 0) new_d += 360;
681 if(thread->new_settings->session->achannel_positions[active_channel] != new_d)
683 thread->new_settings->session->achannel_positions[active_channel] = new_d;
684 int new_channels = thread->new_settings->session->audio_channels;
685 memcpy(&thread->mwindow->preferences->channel_positions[new_channels - 1],
686 thread->new_settings->session->achannel_positions,
687 sizeof(thread->mwindow->preferences->channel_positions[new_channels - 1]));
688 draw(thread->new_settings->session->achannel_positions[active_channel]);
696 SetFrameRateTextBox::SetFrameRateTextBox(SetFormatThread *thread, int x, int y)
697 : BC_TextBox(x, y, xS(100), 1, (float)thread->new_settings->session->frame_rate)
699 this->thread = thread;
702 int SetFrameRateTextBox::handle_event()
704 thread->new_settings->session->frame_rate = Units::atoframerate(get_text());
710 // SetVChannels::SetVChannels(SetFormatThread *thread, int x, int y)
711 // : BC_TextBox(x, y, xS(100), 1, thread->channels)
713 // this->thread = thread;
715 // int SetVChannels::handle_event()
717 // thread->channels = atol(get_text());
724 ScaleSizeText::ScaleSizeText(int x, int y, SetFormatThread *thread, int *output)
725 : BC_TextBox(x, y, xS(100), 1, *output)
727 this->thread = thread;
728 this->output = output;
730 ScaleSizeText::~ScaleSizeText()
733 int ScaleSizeText::handle_event()
735 *output = atol(get_text());
736 *output /= 2; *output *= 2;
737 if(*output <= 0) *output = 2;
738 if(*output > 10000) *output = 10000;
740 thread->update_window();
746 ScaleRatioText::ScaleRatioText(int x,
748 SetFormatThread *thread,
750 : BC_TextBox(x, y, xS(100), 1, *output)
752 this->thread = thread;
753 this->output = output;
755 ScaleRatioText::~ScaleRatioText()
758 int ScaleRatioText::handle_event()
760 *output = atof(get_text());
761 //if(*output <= 0) *output = 1;
762 if(*output > 10000) *output = 10000;
763 if(*output < -10000) *output = -10000;
765 thread->update_window();
770 ScaleAspectAuto::ScaleAspectAuto(int x, int y, SetFormatThread *thread)
771 : BC_CheckBox(x, y, thread->auto_aspect, _("Auto"))
773 this->thread = thread;
776 ScaleAspectAuto::~ScaleAspectAuto()
780 int ScaleAspectAuto::handle_event()
782 thread->auto_aspect = get_value();
783 thread->update_aspect();
787 ScaleAspectText::ScaleAspectText(int x, int y, SetFormatThread *thread, float *output)
788 : BC_TextBox(x, y, xS(70), 1, *output)
790 this->output = output;
791 this->thread = thread;
793 ScaleAspectText::~ScaleAspectText()
797 int ScaleAspectText::handle_event()
799 *output = atof(get_text());
804 SetFormatApply::SetFormatApply(int x, int y, SetFormatThread *thread)
805 : BC_GenericButton(x, y, _("Apply"))
807 this->thread = thread;
810 int SetFormatApply::handle_event()
812 thread->apply_changes();
817 FormatSwapExtents::FormatSwapExtents(MWindow *mwindow,
818 SetFormatThread *thread,
819 SetFormatWindow *gui,
822 : BC_Button(x, y, mwindow->theme->get_image_set("swap_extents"))
824 this->mwindow = mwindow;
825 this->thread = thread;
827 set_tooltip(_("Swap dimensions"));
830 int FormatSwapExtents::handle_event()
832 int w = thread->dimension[0];
833 int h = thread->dimension[1];
834 thread->dimension[0] = -h;
835 gui->dimension[0]->update((int64_t)h);
836 gui->dimension[1]->update((int64_t)w);
837 thread->update_window();
838 thread->dimension[1] = -w;
839 thread->update_window();