r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / setvideo.C
blobbb9a4fb786fb4284b476aab71638a8ac6f608b27
1 #include "cplayback.h"
2 #include "cwindow.h"
3 #include "cwindowgui.h"
4 #include "datatype.h"
5 #include "defaults.h"
6 #include "edl.h"
7 #include "edlsession.h"
8 #include "mainundo.h"
9 #include "mwindow.h"
10 #include "mwindowgui.h"
11 #include "new.h"
12 #include "playbackengine.h"
13 #include "setvideo.h"
14 #include "transportque.h"
16 #include <libintl.h>
17 #define _(String) gettext(String)
18 #define gettext_noop(String) String
19 #define N_(String) gettext_noop (String)
21 SetVideo::SetVideo(MWindow *mwindow)
22  :BC_MenuItem(_("Video Setup..."))
24         this->mwindow = mwindow;
25         thread = new SetVideoThread(mwindow);
27 SetVideo::~SetVideo()
29         delete thread;
32 int SetVideo::handle_event()
34         if(!thread->running())
35         {
36                 thread->start();
37         }
38         else
39         {
40                 thread->window->raise_window();
41         }
42         return 1;
52 SetVideoThread::SetVideoThread(MWindow *mwindow)
53  : Thread()
55         this->mwindow = mwindow;
56         window = 0;
58 SetVideoThread::~SetVideoThread()
60         if(window)
61         {
62                 window->set_done(1);
63                 join();
64                 delete window;
65         }
68 void SetVideoThread::load_params()
70         orig_dimension[0] = dimension[0] = mwindow->edl->session->track_w;
71         orig_dimension[1] = dimension[1] = mwindow->edl->session->track_h;
72         orig_dimension[2] = dimension[2] = mwindow->edl->session->output_w;
73         orig_dimension[3] = dimension[3] = mwindow->edl->session->output_h;
74         offsets[0] = offsets[1] = offsets[2] = offsets[3] = 0;
75         ratio[0] = ratio[1] = ratio[2] = ratio[3] = 1;
76         aspect_w = mwindow->edl->session->aspect_w;
77         aspect_h = mwindow->edl->session->aspect_h;
78         color_model = mwindow->edl->session->color_model;
79         frame_rate = mwindow->edl->session->frame_rate;
80         channels = mwindow->edl->session->video_channels;
83 void SetVideoThread::save_params()
85         double old_rate = mwindow->edl->session->frame_rate;
86         mwindow->edl->session->track_w = dimension[0];
87         mwindow->edl->session->track_h = dimension[1];
88         mwindow->edl->session->output_w = dimension[2];
89         mwindow->edl->session->output_h = dimension[3];
90         mwindow->edl->session->aspect_w = aspect_w;
91         mwindow->edl->session->aspect_h = aspect_h;
92         mwindow->edl->session->color_model = color_model;
93         mwindow->edl->session->frame_rate = frame_rate;
94         mwindow->edl->session->video_channels = channels;
95         mwindow->edl->resample(old_rate, frame_rate, TRACK_VIDEO);
98 void SetVideoThread::run()
100         auto_aspect = mwindow->defaults->get("AUTOASPECT", 0);
101         constrain_ratio = mwindow->defaults->get("SCALECONSTRAIN", 0);
102         load_params();
104         window = new SetVideoWindow(mwindow, this);
105         window->create_objects();
106         int result = window->run_window();
108         mwindow->defaults->update("AUTOASPECT", auto_aspect);
109         mwindow->defaults->update("SCALECONSTRAIN", constrain_ratio);
111         if(!result)
112         {
113                 mwindow->undo->update_undo_before(_("set video"), LOAD_ALL);
114                 save_params();
115                 mwindow->undo->update_undo_after();
116                 mwindow->save_backup();
118                 mwindow->gui->lock_window();
119                 mwindow->gui->update(1,
120                         1,
121                         1,
122                         1,
123                         0, 
124                         1,
125                         0);
126                 mwindow->gui->unlock_window();
128                 mwindow->sync_parameters(CHANGE_EDL);
129         }
131         delete window;
132         window = 0;
135 void SetVideoThread::update_window()
137         int pair_start = 0;
138         int i, result, modified_item, dimension_modified = 0, ratio_modified = 0;
140         for(i = 0, result = 0; i < 4 && !result; i++)
141         {
142                 if(i == 2) pair_start = 2;
143                 if(dimension[i] < 0)
144                 {
145                         dimension[i] *= -1;
146                         result = 1;
147                         modified_item = i;
148                         dimension_modified = 1;
149                 }
150                 if(ratio[i] < 0)
151                 {
152                         ratio[i] *= -1;
153                         result = 1;
154                         modified_item = i;
155                         ratio_modified = 1;
156                 }
157         }
159         if(result)
160         {
161                 if(dimension_modified)
162                         ratio[modified_item] = (float)dimension[modified_item] / orig_dimension[modified_item];
164                 if(ratio_modified && !constrain_ratio)
165                 {
166                         dimension[modified_item] = (int)(orig_dimension[modified_item] * ratio[modified_item]);
167                         window->dimension[modified_item]->update((long)dimension[modified_item]);
168                 }
170                 for(i = pair_start; i < pair_start + 2/*  && constrain_ratio */; i++)
171                 {
172                         if(dimension_modified ||
173                                 (i != modified_item && ratio_modified))
174                         {
175                                 if(constrain_ratio) ratio[i] = ratio[modified_item];
176                                 window->ratio[i]->update(ratio[i]);
177                         }
179                         if(ratio_modified ||
180                                 (i != modified_item && dimension_modified))
181                         {
182                                 if(constrain_ratio) 
183                                 {
184                                         dimension[i] = (int)(orig_dimension[i] * ratio[modified_item]);
185                                         window->dimension[i]->update((long)dimension[i]);
186                                 }
187                         }
188                 }
189         }
191         update_aspect();
194 void SetVideoThread::update_aspect()
196         if(auto_aspect)
197         {
198                 char string[1024];
199                 MWindow::create_aspect_ratio(aspect_w, 
200                         aspect_h, 
201                         dimension[2], 
202                         dimension[3]);
203                 sprintf(string, "%.02f", aspect_w);
204                 window->aspect_w->update(string);
205                 sprintf(string, "%.02f", aspect_h);
206                 window->aspect_h->update(string);
207         }
220 SetVideoWindow::SetVideoWindow(MWindow *mwindow, SetVideoThread *thread)
221  : BC_Window(PROGRAM_NAME ": Video Setup",
222         mwindow->gui->get_abs_cursor_x(),
223         mwindow->gui->get_abs_cursor_y(),
224         400,
225         460,
226         -1,
227         -1,
228         1,
229         0,
230         1)
232         this->mwindow = mwindow;
233         this->thread = thread;
236 SetVideoWindow::~SetVideoWindow()
240 int SetVideoWindow::create_objects()
242         int x = 10, y = 10, x1, y1, x2, y2;
243         BC_TextBox *textbox;
245         x1 = x;
246         add_subwindow(new BC_Title(x1, y, _("Frame rate:")));
247         x1 += 100;
248         add_subwindow(textbox = new SetFrameRateTextBox(thread, x1, y));
249         x1 += textbox->get_w();
250         add_subwindow(new FrameRatePulldown(mwindow, textbox, x1, y));
252 //      x1 = x;
253 //      y += textbox->get_h() + 5;
254 //      add_subwindow(new BC_Title(x1, y, _("Channels:")));
255 //      x1 += 100;
256 //      add_subwindow(textbox = new SetVChannels(thread, x1, y));
257 //      x1 += textbox->get_w();
258 //      add_subwindow(new BC_ITumbler(textbox, 1, MAXCHANNELS, x1, y));
260         y += textbox->get_h() + 40;
261         add_subwindow(new BC_Title(x, y, _("Default track size:")));
262         x1 = x + 150;
263         y1 = y;
264         add_subwindow(new BC_Title(x + 200, y, _("Canvas size:")));
265         x2 = x + 200 + 150;
266         y2 = y;
267         y += 30;
268         add_subwindow(new BC_Title(x, y, _("Width:")));
269         x += 70;
270         add_subwindow(dimension[0] = new ScaleSizeText(x, y, thread, &(thread->dimension[0])));
271         x += 110;
272         add_subwindow(new BC_Title(x, y, _("Width:")));
273         x += 70;
274         add_subwindow(dimension[2] = new ScaleSizeText(x, y, thread, &(thread->dimension[2])));
276         y += dimension[0]->get_h() + 5;
277         x = 10;
278         add_subwindow(new BC_Title(x, y, _("Height:")));
279         x += 70;
280         add_subwindow(dimension[1] = new ScaleSizeText(x, y, thread, &(thread->dimension[1])));
281         x += 110;
282         add_subwindow(new BC_Title(x, y, _("Height:")));
283         x += 70;
284         add_subwindow(dimension[3] = new ScaleSizeText(x, y, thread, &(thread->dimension[3])));
286         y += dimension[1]->get_h() + 5;
287         x = 10;
288         add_subwindow(new BC_Title(x, y, _("W Ratio:")));
289         x += 70;
290         add_subwindow(ratio[0] = new ScaleRatioText(x, y, thread, &(thread->ratio[0])));
291         x += 110;
292         add_subwindow(new BC_Title(x, y, _("W Ratio:")));
293         x += 70;
294         add_subwindow(ratio[2] = new ScaleRatioText(x, y, thread, &(thread->ratio[2])));
296         y += ratio[0]->get_h() + 5;
297         x = 10;
298         add_subwindow(new BC_Title(x, y, _("H Ratio:")));
299         x += 70;
300         add_subwindow(ratio[1] = new ScaleRatioText(x, y, thread, &(thread->ratio[1])));
301         x += 110;
302         add_subwindow(new BC_Title(x, y, _("H Ratio:")));
303         x += 70;
304         add_subwindow(ratio[3] = new ScaleRatioText(x, y, thread, &(thread->ratio[3])));
306         add_subwindow(new FrameSizePulldown(mwindow, 
307                 dimension[0], 
308                 dimension[1], 
309                 x1, 
310                 y1));
312         add_subwindow(new FrameSizePulldown(mwindow, 
313                 dimension[2], 
314                 dimension[3], 
315                 x2, 
316                 y2));
318         x = 10;
319         y += ratio[1]->get_h() + 40;
320 //      add_subwindow(new ScaleConstrain(x, y, thread));
321 //      x = 10;
322 //      y += 80;
323         add_subwindow(new BC_Title(x, y, _("Aspect ratio:")));
324         x += 100;
325         add_subwindow(aspect_w = new ScaleAspectText(x, 
326                 y, 
327                 thread, 
328                 &(thread->aspect_w)));
329         x += aspect_w->get_w() + 5;
330         add_subwindow(new BC_Title(x, y, _(":")));
331         x += 10;
332         add_subwindow(aspect_h = new ScaleAspectText(x, 
333                 y, 
334                 thread, 
335                 &(thread->aspect_h)));
336         x += aspect_h->get_w() + 5;
337         add_subwindow(new AspectPulldown(mwindow, 
338                 aspect_w, 
339                 aspect_h, 
340                 x, 
341                 y));
342         x += 30;
343         add_subwindow(new ScaleAspectAuto(x, y - 5, thread));
345         y += 40;
346         x = 10;
347         add_subwindow(new BC_Title(x, y, _("Color model:")));
348         x += 100;
349         add_subwindow(textbox = new BC_TextBox(x, y, 200, 1, ""));
350         x += textbox->get_w();
351         add_subwindow(new ColormodelPulldown(mwindow, 
352                 textbox, 
353                 &thread->color_model,
354                 x, 
355                 y));
359         add_subwindow(new BC_OKButton(this));
360         add_subwindow(new BC_CancelButton(this));
361         show_window();
362         return 0;
366 SetFrameRateTextBox::SetFrameRateTextBox(SetVideoThread *thread, int x, int y)
367  : BC_TextBox(x, y, 100, 1, (float)thread->frame_rate)
369         this->thread = thread;
372 int SetFrameRateTextBox::handle_event()
374         thread->frame_rate = Units::atoframerate(get_text());
375         return 1;
380 SetVChannels::SetVChannels(SetVideoThread *thread, int x, int y)
381  : BC_TextBox(x, y, 100, 1, thread->channels)
383         this->thread = thread;
385 int SetVChannels::handle_event()
387         thread->channels = atol(get_text());
388         return 1;
394 ScaleSizeText::ScaleSizeText(int x, int y, SetVideoThread *thread, int *output)
395  : BC_TextBox(x, y, 100, 1, *output)
397         this->thread = thread; 
398         this->output = output; 
400 ScaleSizeText::~ScaleSizeText()
402 int ScaleSizeText::handle_event()
404         *output = atol(get_text());
405         *output /= 2;
406         *output *= 2;
407         if(*output <= 0) *output = 2;
408         if(*output > 10000) *output = 10000;
409         *output *= -1;
410         thread->update_window();
415 ScaleRatioText::ScaleRatioText(int x, int y, SetVideoThread *thread, float *output)
416  : BC_TextBox(x, y, 100, 1, *output)
418         this->thread = thread; 
419         this->output = output; 
421 ScaleRatioText::~ScaleRatioText()
423 int ScaleRatioText::handle_event()
425         *output = atof(get_text());
426         //if(*output <= 0) *output = 1;
427         if(*output > 10000) *output = 10000;
428         if(*output < -10000) *output = -10000;
429         *output *= -1;
430         thread->update_window();
431         return 1;
435 ScaleConstrain::ScaleConstrain(int x, int y, SetVideoThread *thread)
436  : BC_CheckBox(x, y, thread->constrain_ratio, _("Constrain ratio"))
438         this->thread = thread; 
440 ScaleConstrain::~ScaleConstrain() {}
441 int ScaleConstrain::handle_event()
443         thread->constrain_ratio = get_value();
447 ScaleAspectAuto::ScaleAspectAuto(int x, int y, SetVideoThread *thread)
448  : BC_CheckBox(x, y, thread->auto_aspect, _("Auto"))
450         this->thread = thread; 
453 ScaleAspectAuto::~ScaleAspectAuto()
457 int ScaleAspectAuto::handle_event()
459         thread->auto_aspect = get_value();
460         thread->update_aspect();
463 ScaleAspectText::ScaleAspectText(int x, int y, SetVideoThread *thread, float *output)
464  : BC_TextBox(x, y, 70, 1, *output)
466         this->output = output;
467         this->thread = thread;
469 ScaleAspectText::~ScaleAspectText()
473 int ScaleAspectText::handle_event()
475         *output = atof(get_text());
476         return 1;