3 #include "cwindowgui.h"
7 #include "edlsession.h"
8 #include "formatpresets.h"
10 #include "levelwindow.h"
11 #include "levelwindowgui.h"
12 #include "mainerror.h"
16 #include "mwindowgui.h"
18 #include "preferences.h"
19 #include "rotateframe.h"
20 #include "setformat.h"
24 #include "vwindowgui.h"
28 SetFormat::SetFormat(MWindow *mwindow)
29 : BC_MenuItem(_("Format..."), "Shift-F", 'F')
32 this->mwindow = mwindow;
33 thread = new SetFormatThread(mwindow);
36 int SetFormat::handle_event()
38 if(!thread->running())
44 // window_lock has to be locked but window can't be locked until after
45 // it is known to exist, so we neglect window_lock for now
48 thread->window_lock->lock("SetFormat::handle_event");
49 thread->window->lock_window("SetFormat::handle_event");
50 thread->window->raise_window();
51 thread->window->unlock_window();
52 thread->window_lock->unlock();
58 SetFormatThread::SetFormatThread(MWindow *mwindow)
61 this->mwindow = mwindow;
62 window_lock = new Mutex("SetFormatThread::window_lock");
66 void SetFormatThread::run()
68 orig_dimension[0] = dimension[0] = mwindow->edl->session->output_w;
69 orig_dimension[1] = dimension[1] = mwindow->edl->session->output_h;
70 auto_aspect = mwindow->defaults->get("AUTOASPECT", 0);
72 ratio[0] = ratio[1] = 1;
74 new_settings = new EDL;
75 new_settings->create_objects();
76 new_settings->copy_session(mwindow->edl);
78 // This locks mwindow, so it must be done outside window_lock
79 int x = mwindow->gui->get_abs_cursor_x(1) - mwindow->theme->setformat_w / 2;
80 int y = mwindow->gui->get_abs_cursor_y(1) - mwindow->theme->setformat_h / 2;
82 window_lock->lock("SetFormatThread::run 1");
83 window = new SetFormatWindow(mwindow, this, x, y);
84 window->create_objects();
85 window_lock->unlock();
87 int result = window->run_window();
90 window_lock->lock("SetFormatThread::run 2");
93 window_lock->unlock();
101 mwindow->defaults->update("AUTOASPECT", auto_aspect);
105 void SetFormatThread::apply_changes()
107 double new_samplerate = new_settings->session->sample_rate;
108 double old_samplerate = mwindow->edl->session->sample_rate;
109 double new_framerate = new_settings->session->frame_rate;
110 double old_framerate = mwindow->edl->session->frame_rate;
111 int new_channels = new_settings->session->audio_channels;
112 CLAMP(new_channels, 1, MAXCHANNELS);
114 memcpy(&mwindow->preferences->channel_positions[MAXCHANNELS * (new_channels - 1)],
115 new_settings->session->achannel_positions,
116 sizeof(int) * MAXCHANNELS);
119 mwindow->edl->copy_session(new_settings, 1);
120 mwindow->edl->session->output_w = dimension[0];
121 mwindow->edl->session->output_h = dimension[1];
122 mwindow->edl->rechannel();
123 mwindow->edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
124 mwindow->edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
125 mwindow->save_backup();
126 mwindow->undo->update_undo(_("set format"), LOAD_ALL);
129 mwindow->restart_brender();
130 mwindow->gui->lock_window("SetFormatThread::apply_changes");
131 mwindow->gui->update(1,
138 mwindow->gui->unlock_window();
140 mwindow->cwindow->gui->lock_window("SetFormatThread::apply_changes");
141 mwindow->cwindow->gui->resize_event(mwindow->cwindow->gui->get_w(),
142 mwindow->cwindow->gui->get_h());
143 mwindow->cwindow->gui->meters->set_meters(new_channels, 1);
144 mwindow->cwindow->gui->slider->set_position();
145 mwindow->cwindow->gui->flush();
146 mwindow->cwindow->gui->unlock_window();
148 mwindow->vwindow->gui->lock_window("SetFormatThread::apply_changes");
149 mwindow->vwindow->gui->resize_event(mwindow->vwindow->gui->get_w(),
150 mwindow->vwindow->gui->get_h());
151 mwindow->vwindow->gui->meters->set_meters(new_channels, 1);
152 mwindow->vwindow->gui->flush();
153 mwindow->vwindow->gui->unlock_window();
155 mwindow->lwindow->gui->lock_window("SetFormatThread::apply_changes");
156 mwindow->lwindow->gui->panel->set_meters(new_channels, 1);
157 mwindow->lwindow->gui->flush();
158 mwindow->lwindow->gui->unlock_window();
161 if(((mwindow->edl->session->output_w % 4) ||
162 (mwindow->edl->session->output_h % 4)) &&
163 mwindow->edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL)
165 MainError::show_error(
166 _("This project's dimensions are not multiples of 4 so\n"
167 "it can't be rendered by OpenGL."));
172 mwindow->sync_parameters(CHANGE_ALL);
175 void SetFormatThread::update()
177 window->sample_rate->update(new_settings->session->sample_rate);
178 window->channels->update((int64_t)new_settings->session->audio_channels);
179 window->frame_rate->update((float)new_settings->session->frame_rate);
182 window->auto_aspect->update(0);
185 dimension[0] = new_settings->session->output_w;
186 window->dimension[0]->update((int64_t)dimension[0]);
187 dimension[1] = new_settings->session->output_h;
188 window->dimension[1]->update((int64_t)dimension[1]);
190 ratio[0] = (float)dimension[0] / orig_dimension[0];
191 window->ratio[0]->update(ratio[0]);
192 ratio[1] = (float)dimension[1] / orig_dimension[1];
193 window->ratio[1]->update(ratio[1]);
195 window->aspect_w->update(new_settings->session->aspect_w);
196 window->aspect_h->update(new_settings->session->aspect_h);
198 window->canvas->draw();
201 void SetFormatThread::update_window()
203 int i, result, modified_item, dimension_modified = 0, ratio_modified = 0;
205 for(i = 0, result = 0; i < 2 && !result; i++)
212 dimension_modified = 1;
225 if(dimension_modified)
226 ratio[modified_item] = (float)dimension[modified_item] / orig_dimension[modified_item];
228 if(ratio_modified && !constrain_ratio)
230 dimension[modified_item] = (int)(orig_dimension[modified_item] * ratio[modified_item]);
231 window->dimension[modified_item]->update((int64_t)dimension[modified_item]);
234 for(i = 0; i < 2; i++)
236 if(dimension_modified ||
237 (i != modified_item && ratio_modified))
239 if(constrain_ratio) ratio[i] = ratio[modified_item];
240 window->ratio[i]->update(ratio[i]);
244 (i != modified_item && dimension_modified))
248 dimension[i] = (int)(orig_dimension[i] * ratio[modified_item]);
249 window->dimension[i]->update((int64_t)dimension[i]);
258 void SetFormatThread::update_aspect()
262 char string[BCTEXTLEN];
263 MWindow::create_aspect_ratio(new_settings->session->aspect_w,
264 new_settings->session->aspect_h,
267 sprintf(string, "%.02f", new_settings->session->aspect_w);
268 window->aspect_w->update(string);
269 sprintf(string, "%.02f", new_settings->session->aspect_h);
270 window->aspect_h->update(string);
282 SetFormatWindow::SetFormatWindow(MWindow *mwindow,
283 SetFormatThread *thread,
286 : BC_Window(PROGRAM_NAME ": Set Format",
289 mwindow->theme->setformat_w,
290 mwindow->theme->setformat_h,
297 this->mwindow = mwindow;
298 this->thread = thread;
301 void SetFormatWindow::create_objects()
303 int x = 10, y = mwindow->theme->setformat_y1;
307 mwindow->theme->draw_setformat_bg(this);
311 presets = new SetFormatPresets(mwindow,
315 presets->create_objects();
319 y = mwindow->theme->setformat_y2;
321 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
325 y = mwindow->theme->setformat_y3;
326 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
330 y += mwindow->theme->setformat_margin;
331 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
334 add_subwindow(sample_rate = new SetSampleRateTextBox(thread,
335 mwindow->theme->setformat_x2,
337 add_subwindow(new SampleRatePulldown(mwindow,
339 mwindow->theme->setformat_x2 + sample_rate->get_w(),
342 y += mwindow->theme->setformat_margin;
343 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
346 add_subwindow(channels = new SetChannelsTextBox(thread,
347 mwindow->theme->setformat_x2,
349 add_subwindow(new BC_ITumbler(channels,
352 mwindow->theme->setformat_x2 + channels->get_w(),
355 y += mwindow->theme->setformat_margin;
356 add_subwindow(new BC_Title(mwindow->theme->setformat_x1,
358 _("Channel positions:")));
359 y += mwindow->theme->setformat_margin;
360 add_subwindow(canvas = new SetChannelsCanvas(mwindow,
362 mwindow->theme->setformat_channels_x,
363 mwindow->theme->setformat_channels_y,
364 mwindow->theme->setformat_channels_w,
365 mwindow->theme->setformat_channels_h));
376 y = mwindow->theme->setformat_y2;
377 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
382 y = mwindow->theme->setformat_y3;
383 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
386 add_subwindow(frame_rate = new SetFrameRateTextBox(thread,
387 mwindow->theme->setformat_x4,
389 add_subwindow(new FrameRatePulldown(mwindow,
391 mwindow->theme->setformat_x4 + frame_rate->get_w(),
394 y += mwindow->theme->setformat_margin;
395 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
399 y += mwindow->theme->setformat_margin;
400 add_subwindow(title = new BC_Title(mwindow->theme->setformat_x3, y, _("Width:")));
401 add_subwindow(dimension[0] = new ScaleSizeText(mwindow->theme->setformat_x4,
404 &(thread->dimension[0])));
406 y += mwindow->theme->setformat_margin;
407 add_subwindow(new BC_Title(mwindow->theme->setformat_x3, y, _("Height:")));
408 add_subwindow(dimension[1] = new ScaleSizeText(mwindow->theme->setformat_x4,
411 &(thread->dimension[1])));
413 x = mwindow->theme->setformat_x4 + dimension[0]->get_w();
414 FrameSizePulldown *pulldown;
415 add_subwindow(pulldown = new FrameSizePulldown(mwindow,
419 y - mwindow->theme->setformat_margin));
421 add_subwindow(new FormatSwapExtents(mwindow,
424 x + pulldown->get_w() + 5,
425 y - mwindow->theme->setformat_margin));
427 y += mwindow->theme->setformat_margin;
428 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
431 add_subwindow(ratio[0] = new ScaleRatioText(mwindow->theme->setformat_x4,
434 &(thread->ratio[0])));
436 y += mwindow->theme->setformat_margin;
437 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
440 add_subwindow(ratio[1] = new ScaleRatioText(mwindow->theme->setformat_x4,
443 &(thread->ratio[1])));
445 y += mwindow->theme->setformat_margin;
446 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
449 x = mwindow->theme->setformat_x4;
450 add_subwindow(color_model = new BC_TextBox(x,
455 x += color_model->get_w();
456 add_subwindow(new ColormodelPulldown(mwindow,
458 &thread->new_settings->session->color_model,
462 y += mwindow->theme->setformat_margin;
463 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
465 _("Aspect ratio:")));
466 y += mwindow->theme->setformat_margin;
467 x = mwindow->theme->setformat_x3;
468 add_subwindow(aspect_w = new ScaleAspectText(x,
471 &(thread->new_settings->session->aspect_w)));
472 x += aspect_w->get_w() + 5;
473 add_subwindow(new BC_Title(x, y, _(":")));
475 add_subwindow(aspect_h = new ScaleAspectText(x,
478 &(thread->new_settings->session->aspect_h)));
479 x += aspect_h->get_w();
480 add_subwindow(new AspectPulldown(mwindow,
486 add_subwindow(auto_aspect = new ScaleAspectAuto(x, y, thread));
487 y += mwindow->theme->setformat_margin;
489 // --------------------
490 add_subwindow(new BC_Title(mwindow->theme->setformat_x3,
492 _("Interlace mode:")));
493 add_subwindow(textbox = new BC_TextBox(mwindow->theme->setformat_x4,
498 add_subwindow(new InterlacemodePulldown(mwindow,
500 &(thread->new_settings->session->interlace_mode),
501 (ArrayList<BC_ListBoxItem*>*)&mwindow->interlace_project_modes,
502 mwindow->theme->setformat_x4 + textbox->get_w(),
504 y += mwindow->theme->setformat_margin;
508 BC_CancelTextButton *cancel;
509 add_subwindow(ok = new BC_OKTextButton(this));
510 add_subwindow(cancel = new BC_CancelTextButton(this));
511 add_subwindow(new SetFormatApply((ok->get_x() + cancel->get_x()) / 2,
518 char* SetFormatWindow::get_preset_text()
537 SetFormatPresets::SetFormatPresets(MWindow *mwindow,
538 SetFormatWindow *gui,
541 : FormatPresets(mwindow, 0, gui, x, y)
546 SetFormatPresets::~SetFormatPresets()
550 int SetFormatPresets::handle_event()
552 format_gui->thread->update();
556 EDL* SetFormatPresets::get_edl()
558 return format_gui->thread->new_settings;
575 SetSampleRateTextBox::SetSampleRateTextBox(SetFormatThread *thread, int x, int y)
576 : BC_TextBox(x, y, 100, 1, (int64_t)thread->new_settings->session->sample_rate)
578 this->thread = thread;
580 int SetSampleRateTextBox::handle_event()
582 thread->new_settings->session->sample_rate = CLIP(atol(get_text()), 1, 1000000);
586 SetChannelsTextBox::SetChannelsTextBox(SetFormatThread *thread, int x, int y)
587 : BC_TextBox(x, y, 100, 1, thread->new_settings->session->audio_channels)
589 this->thread = thread;
591 int SetChannelsTextBox::handle_event()
593 int new_channels = CLIP(atoi(get_text()), 1, MAXCHANNELS);
595 thread->new_settings->session->audio_channels = new_channels;
600 memcpy(thread->new_settings->session->achannel_positions,
601 &thread->mwindow->preferences->channel_positions[MAXCHANNELS * (new_channels - 1)],
602 sizeof(int) * MAXCHANNELS);
606 thread->window->canvas->draw();
611 SetChannelsCanvas::SetChannelsCanvas(MWindow *mwindow,
612 SetFormatThread *thread,
622 this->thread = thread;
623 this->mwindow = mwindow;
625 box_r = mwindow->theme->channel_position_data->get_w() / 2;
626 temp_picon = new VFrame(0,
627 mwindow->theme->channel_position_data->get_w(),
628 mwindow->theme->channel_position_data->get_h(),
629 mwindow->theme->channel_position_data->get_color_model());
630 rotater = new RotateFrame(mwindow->preferences->processors,
631 mwindow->theme->channel_position_data->get_w(),
632 mwindow->theme->channel_position_data->get_h());
634 SetChannelsCanvas::~SetChannelsCanvas()
640 int SetChannelsCanvas::draw(int angle)
643 int real_w = get_w() - box_r * 2;
644 int real_h = get_h() - box_r * 2;
648 draw_top_background(get_top_level(), 0, 0, get_w(), get_h());
649 // draw_vframe(mwindow->theme->channel_bg_data, 0, 0);
656 set_color(mwindow->theme->channel_position_color);
657 for(int i = 0; i < thread->new_settings->session->audio_channels; i++)
659 get_dimensions(thread->new_settings->session->achannel_positions[i],
664 double rotate_angle = thread->new_settings->session->achannel_positions[i];
665 rotate_angle = -rotate_angle;
666 while(rotate_angle < 0) rotate_angle += 360;
667 rotater->rotate(temp_picon,
668 mwindow->theme->channel_position_data,
672 BC_Pixmap temp_pixmap(this,
676 draw_pixmap(&temp_pixmap, x, y);
677 sprintf(string, "%d", i + 1);
678 draw_text(x + 2, y + box_r * 2 - 2, string);
683 sprintf(string, _("%d degrees"), angle);
684 draw_text(this->get_w() / 2 - 40, this->get_h() / 2, string);
691 int SetChannelsCanvas::get_dimensions(int channel_position,
698 int real_w = this->get_w() - box_r * 2 - MARGIN;
699 int real_h = this->get_h() - box_r * 2 - MARGIN;
700 float corrected_position = channel_position;
701 if(corrected_position < 0) corrected_position += 360;
702 Units::polar_to_xy((float)corrected_position, real_w / 2, x, y);
703 x += real_w / 2 + MARGIN / 2;
704 y += real_h / 2 + MARGIN / 2;
710 int SetChannelsCanvas::button_press_event()
712 if(!cursor_inside()) return 0;
713 // get active channel
715 i < thread->new_settings->session->audio_channels;
719 get_dimensions(thread->new_settings->session->achannel_positions[i],
724 if(get_cursor_x() > x && get_cursor_y() > y &&
725 get_cursor_x() < x + w && get_cursor_y() < y + h)
728 degree_offset = (int)Units::xy_to_polar(get_cursor_x() - this->get_w() / 2, get_cursor_y() - this->get_h() / 2);
730 if(degree_offset >= 360) degree_offset -= 360;
731 degree_offset -= thread->new_settings->session->achannel_positions[i];
732 draw(thread->new_settings->session->achannel_positions[i]);
739 int SetChannelsCanvas::button_release_event()
741 if(active_channel >= 0)
750 int SetChannelsCanvas::cursor_motion_event()
752 if(active_channel >= 0)
754 // get degrees of new channel
756 new_d = (int)Units::xy_to_polar(get_cursor_x() - this->get_w() / 2, get_cursor_y() - this->get_h() / 2);
758 new_d -= degree_offset;
760 while(new_d >= 360) new_d -= 360;
761 while(new_d < 0) new_d += 360;
763 if(thread->new_settings->session->achannel_positions[active_channel] != new_d)
765 thread->new_settings->session->achannel_positions[active_channel] = new_d;
766 int new_channels = thread->new_settings->session->audio_channels;
767 memcpy(&thread->mwindow->preferences->channel_positions[MAXCHANNELS * (new_channels - 1)],
768 thread->new_settings->session->achannel_positions,
769 sizeof(int) * MAXCHANNELS);
770 draw(thread->new_settings->session->achannel_positions[active_channel]);
785 SetFrameRateTextBox::SetFrameRateTextBox(SetFormatThread *thread, int x, int y)
786 : BC_TextBox(x, y, 100, 1, (float)thread->new_settings->session->frame_rate)
788 this->thread = thread;
791 int SetFrameRateTextBox::handle_event()
793 thread->new_settings->session->frame_rate = Units::atoframerate(get_text());
799 // SetVChannels::SetVChannels(SetFormatThread *thread, int x, int y)
800 // : BC_TextBox(x, y, 100, 1, thread->channels)
802 // this->thread = thread;
804 // int SetVChannels::handle_event()
806 // thread->channels = atol(get_text());
813 ScaleSizeText::ScaleSizeText(int x, int y, SetFormatThread *thread, int *output)
814 : BC_TextBox(x, y, 100, 1, *output)
816 this->thread = thread;
817 this->output = output;
819 ScaleSizeText::~ScaleSizeText()
822 int ScaleSizeText::handle_event()
824 *output = atol(get_text());
827 if(*output <= 0) *output = 2;
828 if(*output > 10000) *output = 10000;
830 thread->update_window();
835 ScaleRatioText::ScaleRatioText(int x,
837 SetFormatThread *thread,
839 : BC_TextBox(x, y, 100, 1, *output)
841 this->thread = thread;
842 this->output = output;
844 ScaleRatioText::~ScaleRatioText()
847 int ScaleRatioText::handle_event()
849 *output = atof(get_text());
850 //if(*output <= 0) *output = 1;
851 if(*output > 10000) *output = 10000;
852 if(*output < -10000) *output = -10000;
854 thread->update_window();
860 ScaleAspectAuto::ScaleAspectAuto(int x, int y, SetFormatThread *thread)
861 : BC_CheckBox(x, y, thread->auto_aspect, _("Auto"))
863 this->thread = thread;
866 ScaleAspectAuto::~ScaleAspectAuto()
870 int ScaleAspectAuto::handle_event()
872 thread->auto_aspect = get_value();
873 thread->update_aspect();
876 ScaleAspectText::ScaleAspectText(int x, int y, SetFormatThread *thread, float *output)
877 : BC_TextBox(x, y, 70, 1, *output)
879 this->output = output;
880 this->thread = thread;
882 ScaleAspectText::~ScaleAspectText()
886 int ScaleAspectText::handle_event()
888 *output = atof(get_text());
897 SetFormatApply::SetFormatApply(int x, int y, SetFormatThread *thread)
898 : BC_GenericButton(x, y, _("Apply"))
900 this->thread = thread;
903 int SetFormatApply::handle_event()
905 thread->apply_changes();
921 FormatSwapExtents::FormatSwapExtents(MWindow *mwindow,
922 SetFormatThread *thread,
923 SetFormatWindow *gui,
926 : BC_Button(x, y, mwindow->theme->get_image_set("swap_extents"))
928 this->mwindow = mwindow;
929 this->thread = thread;
931 set_tooltip("Swap dimensions");
934 int FormatSwapExtents::handle_event()
936 int w = thread->dimension[0];
937 int h = thread->dimension[1];
938 thread->dimension[0] = -h;
939 gui->dimension[0]->update((int64_t)h);
940 gui->dimension[1]->update((int64_t)w);
941 thread->update_window();
942 thread->dimension[1] = -w;
943 thread->update_window();