b9e9d8fdffaadba1696e9e5808ed041f95e3bdc5
[goodguy/history.git] / cinelerra-5.1 / plugins / scaleratio / scaleratiowin.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "clip.h"
24 #include "language.h"
25 #include "rescale.h"
26 #include "scaleratio.h"
27 #include "scaleratiowin.h"
28 #include "mwindow.h"
29
30
31 ScaleRatioWin::ScaleRatioWin(ScaleRatioMain *client)
32  : PluginClientWindow(client, 400, 300, 400, 300, 0)
33 {
34         this->client = client;
35 }
36
37 ScaleRatioWin::~ScaleRatioWin()
38 {
39 }
40
41 void ScaleRatioWin::create_objects()
42 {
43         int x = 10, y = 10;
44         int x0 = x, x1 = x0 + 50;
45         int y0 = y;
46         client->load_configuration();
47
48         add_tool(new BC_Title(x0, y0, _("In R:")));
49         add_tool(in_r = new ScaleRatioRatio(this, client, x1, y0, &client->config.in_r));
50         in_r->create_objects();
51         y0 += in_r->get_th() + 10;
52
53         add_tool(new BC_Title(x0, y0, _("In W:")));
54         in_w = new ScaleRatioCoord(this, client, x1, y0, 0, &client->config.in_w);
55         in_w->create_objects();
56         y0 += in_w->get_h() + 8;
57
58         add_tool(new BC_Title(x0, y0, _("In H:")));
59         in_h = new ScaleRatioCoord(this, client, x1, y0, 0, &client->config.in_h);
60         in_h->create_objects();
61
62         x0 = get_w()/2;  y0 = y;
63         x1 = x0 + 50;
64
65         add_tool(new BC_Title(x0, y0, _("Out R:")));
66         add_tool(out_r = new ScaleRatioRatio(this, client, x1, y0, &client->config.out_r));
67         out_r->create_objects();
68         y0 += out_r->get_th() + 10;
69
70         add_tool(new BC_Title(x0, y0, _("Out W:")));
71         out_w = new ScaleRatioCoord(this, client, x1, y0, 0, &client->config.out_w);
72         out_w->create_objects();
73         y0 += out_w->get_h() + 8;
74
75         add_tool(new BC_Title(x0, y0, _("Out H:")));
76         out_h = new ScaleRatioCoord(this, client, x1, y0, 0, &client->config.out_h);
77         out_h->create_objects();
78         y0 += out_h->get_h() + 8;
79
80         y = y0 + 5;
81         add_tool(apply_button = new ScaleRatioApply(this, x, y));
82         int x2 = x + apply_button->get_w() + 50;
83         add_tool(type_popup = new ScaleRatioType(this, x2, y, &client->config.type));
84         type_popup->create_objects();
85         y += apply_button->get_h() + 30;
86
87         x0 = x;  y0 = y;
88         x1 = x0 + 50;
89
90         add_tool(new BC_Title(x0, y0, _("Src X:")));
91         src_x = new ScaleRatioCoord(this, client, x1, y0, 1, &client->config.src_x);
92         src_x->create_objects();
93         y0 += 30;
94
95         add_tool(new BC_Title(x0, y0, _("Src Y:")));
96         src_y = new ScaleRatioCoord(this, client, x1, y0, 1, &client->config.src_y);
97         src_y->create_objects();
98         y0 += 30;
99
100
101         add_tool(new BC_Title(x0, y0, _("Src W:")));
102         src_w = new ScaleRatioCoord(this, client, x1, y0, 0, &client->config.src_w);
103         src_w->create_objects();
104         y0 += 30;
105
106         add_tool(new BC_Title(x0, y0, _("Src H:")));
107         src_h = new ScaleRatioCoord(this, client, x1, y0, 0, &client->config.src_h);
108         src_h->create_objects();
109         y0 += 30;
110
111         x0 = get_w()/2;
112         x1 = x0 + 50;
113         y0 = y;
114         add_tool(new BC_Title(x0, y0, _("Dst X:")));
115         dst_x = new ScaleRatioCoord(this, client, x1, y0, 1, &client->config.dst_x);
116         dst_x->create_objects();
117         y0 += 30;
118
119         add_tool(new BC_Title(x0, y0, _("Dst Y:")));
120         dst_y = new ScaleRatioCoord(this, client, x1, y0, 1, &client->config.dst_y);
121         dst_y->create_objects();
122         y0 += 30;
123
124         add_tool(new BC_Title(x0, y0, _("Dst W:")));
125         dst_w = new ScaleRatioCoord(this, client, x1, y0, 0, &client->config.dst_w);
126         dst_w->create_objects();
127         y0 += 30;
128
129         add_tool(new BC_Title(x0, y0, _("Dst H:")));
130         dst_h = new ScaleRatioCoord(this, client, x1, y0, 0, &client->config.dst_h);
131         dst_h->create_objects();
132         y0 += 30;
133
134         show_window();
135         flush();
136 }
137
138
139
140 ScaleRatioCoord::ScaleRatioCoord(ScaleRatioWin *win,
141         ScaleRatioMain *client, int x, int y, int s, float *value)
142  : BC_TumbleTextBox(win, (int)*value, (int)-10000*s, (int)10000, x, y, 100)
143 {
144         this->client = client;
145         this->win = win;
146         this->value = value;
147 }
148
149 ScaleRatioCoord::~ScaleRatioCoord()
150 {
151 }
152
153 int ScaleRatioCoord::handle_event()
154 {
155         *value = atof(get_text());
156         client->send_configure_change();
157         return 1;
158 }
159
160
161 ScaleRatioTumbler::ScaleRatioTumbler(ScaleRatioRatio *ratio, int value, int x, int y)
162  : BC_TumbleTextBox(ratio->win, value, 0, 10000, x, y, 45)
163 {
164         this->ratio = ratio;
165 }
166
167 ScaleRatioTumbler::~ScaleRatioTumbler()
168 {
169 }
170
171 int ScaleRatioTumbler::handle_event()
172 {
173         ratio->update_ratio();
174         return 1;
175 }
176
177 ScaleRatioRatio::ScaleRatioRatio(ScaleRatioWin *win,
178         ScaleRatioMain *client, int x, int y, float *value)
179  : BC_TextBox(x, y, 100, 1, *value)
180 {
181         this->client = client;
182         this->win = win;
183         this->value = value;
184         this->ah = 0;
185         this->aw = 0;
186 }
187
188 ScaleRatioRatio::~ScaleRatioRatio()
189 {
190         delete ah;
191         delete aw;
192 }
193
194 int ScaleRatioRatio::handle_event()
195 {
196         *value = atof(get_text());
197         float fah = 0, faw = 0;
198         MWindow::create_aspect_ratio(faw, fah, *value*1000000, 1000000);
199         ah->update((int64_t)fah);
200         aw->update((int64_t)faw);
201         win->update_config();
202         win->update_gui();
203         return 1;
204 }
205
206 void ScaleRatioRatio::create_objects()
207 {
208         int tx = BC_TextBox::get_x();
209         int ty = BC_TextBox::get_y();
210         int x = tx;
211         int y = ty + BC_TextBox::get_h() + 5;
212         float faw = 0, fah = 0;
213         MWindow::create_aspect_ratio(faw, fah, *value*1000000, 1000000);
214         aw = new ScaleRatioTumbler(this, faw, x, y);
215         aw->create_objects();
216         x += aw->get_w() + 5;
217         ah = new ScaleRatioTumbler(this, fah, x, y);
218         ah->create_objects();
219         x += ah->get_w();
220         y += ah->get_h();
221         tw = x - tx;
222         int w = BC_TextBox::get_w();
223         if( tw < w ) tw = w;
224         th = y - ty;
225 }
226
227 void ScaleRatioRatio::update_ratio()
228 {
229         float fah = atof(ah->get_text());
230         float faw = atof(aw->get_text());
231         *value = fah > 0 ? faw / fah : 1.f;
232         win->update_config();
233         win->update_gui();
234 }
235
236 ScaleRatioTypeItem::ScaleRatioTypeItem(ScaleRatioType *popup, int type, const char *text)
237  : BC_MenuItem(text)
238 {
239         this->popup = popup;
240         this->type = type;
241 }
242
243 ScaleRatioTypeItem::~ScaleRatioTypeItem()
244 {
245 }
246
247 int ScaleRatioTypeItem::handle_event()
248 {
249         popup->win->client->config.type = type;
250         popup->set_value(type);
251         return popup->handle_event();
252 }
253
254
255 ScaleRatioType::ScaleRatioType(ScaleRatioWin *win, int x, int y, int *value)
256  : BC_PopupMenu(x, y, 180, "", 1)
257 {
258         this->win = win;
259         this->value = value;
260 }
261
262 ScaleRatioType::~ScaleRatioType()
263 {
264 }
265
266 void ScaleRatioType::create_objects()
267 {
268         for( int i=1; i<Rescale::n_scale_types; ++i )
269                 add_item(new ScaleRatioTypeItem(this, i, Rescale::scale_types[i]));
270         set_value(*value);
271 }
272
273 void ScaleRatioType::update(int v)
274 {
275         set_value(*value = v);
276 }
277
278 int ScaleRatioType::handle_event()
279 {
280         win->update_config();
281         win->update_gui();
282         return 1;
283 }
284
285 int ScaleRatioApply::handle_event()
286 {
287         win->update_config();
288         win->update_gui();
289         win->client->send_configure_change();
290         return 1;
291 }
292
293 void ScaleRatioWin::update_config()
294 {
295         ScaleRatioConfig *conf = &client->config;
296         Rescale in(conf->in_w, conf->in_h, conf->in_r);
297         Rescale out(conf->out_w, conf->out_h, conf->out_r);
298         in.rescale(out,conf->type, conf->src_w,conf->src_h, conf->dst_w,conf->dst_h);
299 }
300
301 void ScaleRatioWin::update_gui()
302 {
303         ScaleRatioConfig *conf = &client->config;
304         type_popup->update(conf->type);
305         in_r->update(conf->in_r);
306         out_r->update(conf->out_r);
307         in_w->update(conf->in_w);
308         in_h->update(conf->in_h);
309         src_x->update(conf->src_x);
310         src_y->update(conf->src_y);
311         src_w->update(conf->src_w);
312         src_h->update(conf->src_h);
313         out_w->update(conf->out_w);
314         out_h->update(conf->out_h);
315         dst_x->update(conf->dst_x);
316         dst_y->update(conf->dst_y);
317         dst_w->update(conf->dst_w);
318         dst_h->update(conf->dst_h);
319 }
320