scopewindow layout tweak
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / scopewindow.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "bcsignals.h"
22 #include "bccolors.h"
23 #include "boxblur.h"
24 #include "clip.h"
25 #include "cursors.h"
26 #include "file.h"
27 #include "filesystem.h"
28 #include "language.h"
29 #include "overlayframe.h"
30 #include "scopewindow.h"
31 #include "theme.h"
32
33 #include <string.h>
34
35 ScopePackage::ScopePackage()
36  : LoadPackage()
37 {
38 }
39
40 ScopeUnit::ScopeUnit(ScopeGUI *gui,
41         ScopeEngine *server)
42  : LoadClient(server)
43 {
44         this->gui = gui;
45 }
46
47
48 #define incr_point(rows,h, iv,comp) { \
49 if(iy >= 0 && iy < h) { \
50   uint8_t *vp = rows[iy] + ix*3 + (comp); \
51   int v = *vp+(iv);  *vp = v>0xff ? 0xff : v; } \
52 }
53 #define incr_points(rows,h, rv,gv,bv, comps) { \
54 if(iy >= 0 && iy < h) { \
55   uint8_t *vp = rows[iy] + ix*comps; \
56   int v = *vp+(rv);  *vp++ = v>0xff ? 0xff : v; \
57   v = *vp+(gv);  *vp++ = v>0xff ? 0xff : v; \
58   v = *vp+(bv);  *vp = v>0xff ? 0xff : v; } \
59 }
60 #define decr_points(rows,h, rv,gv,bv, comps) { \
61 if(iy >= 0 && iy < h) { \
62   uint8_t *vp = rows[iy] + ix*comps; \
63   int v = *vp-(rv);  *vp++ = v<0 ? 0 : v; \
64   v = *vp-(gv);  *vp++ = v<0 ? 0 : v; \
65   v = *vp-(bv);  *vp = v<0 ? 0 : v; } \
66 }
67 #define clip_points(rows,h, rv,gv,bv) { \
68 if(iy >= 0 && iy < h) { \
69   uint8_t *vp = rows[iy] + ix*3; \
70   int v = *vp+(rv);  *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
71   v = *vp+(gv);  *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
72   v = *vp+(bv);  *vp = v>0xff ? 0xff : v<0 ? 0 : v; } \
73 }
74
75 #define PROCESS_PIXEL(column) { \
76 /* Calculate histogram */ \
77         if(use_hist) { \
78                 int v_i = (intensity - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
79                 CLAMP(v_i, 0, TOTAL_BINS - 1); \
80                 bins[3][v_i]++; \
81         } \
82         if(use_hist_parade) { \
83                 int r_i = (r - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
84                 int g_i = (g - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
85                 int b_i = (b - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
86                 CLAMP(r_i, 0, TOTAL_BINS - 1); \
87                 CLAMP(g_i, 0, TOTAL_BINS - 1); \
88                 CLAMP(b_i, 0, TOTAL_BINS - 1); \
89                 bins[0][r_i]++; \
90                 bins[1][g_i]++; \
91                 bins[2][b_i]++; \
92         } \
93 /* Calculate waveform */ \
94         if(use_wave || use_wave_parade) { \
95                 int ix = column * wave_w / dat_w; \
96                 if(ix >= 0 && ix < wave_w-1) { \
97                         if(use_wave_parade > 0) { \
98                                 ix /= 3; \
99                                 int iy = wave_h - ((r - FLOAT_MIN) /  \
100                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
101                                 incr_point(waveform_rows,wave_h, winc,0); \
102                                 ix += wave_w/3; \
103                                 iy = wave_h - ((g - FLOAT_MIN) /  \
104                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
105                                 incr_point(waveform_rows,wave_h, winc,1); \
106                                 ix += wave_w/3; \
107                                 iy = wave_h - ((b - FLOAT_MIN) /  \
108                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
109                                 incr_point(waveform_rows,wave_h, winc,2); \
110                         } \
111                         else if(use_wave_parade < 0) { \
112                                 int iy = wave_h - ((r - FLOAT_MIN) /  \
113                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
114                                 clip_points(waveform_rows,wave_h, winc,-winc,-winc); \
115                                 iy = wave_h - ((g - FLOAT_MIN) /  \
116                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
117                                 clip_points(waveform_rows,wave_h, -winc,winc,-winc); \
118                                 iy = wave_h - ((b - FLOAT_MIN) /  \
119                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
120                                 clip_points(waveform_rows,wave_h, -winc,-winc,winc); \
121                         } \
122                         else { float yinc = 3*winc; \
123                                 float rinc = yinc*(r-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
124                                 float ginc = yinc*(g-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
125                                 float binc = yinc*(b-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
126                                 int iy = wave_h - ((intensity - FLOAT_MIN) /  \
127                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
128                                 incr_points(waveform_rows,wave_h, rinc, ginc, binc, 3); \
129                         } \
130                 } \
131         } \
132 /* Calculate vectorscope */ \
133         if(use_vector > 0) { \
134                 double t = TO_RAD(-h); \
135                 float adjacent = sin(t), opposite = cos(t); \
136                 int ix = vector_cx + adjacent * (s) / (FLOAT_MAX) * radius; \
137                 int iy = vector_cy - opposite * (s) / (FLOAT_MAX) * radius; \
138                 CLAMP(ix, 0, vector_w - 1); \
139                 HSV::hsv_to_rgb(r, g, b, h, s, vincf); \
140                 int rv = r + 3; \
141                 int gv = g + 3; \
142                 int bv = b + 3; \
143                 incr_points(vector_rows,vector_h, rv,gv,bv, 4); \
144         } \
145         else if(use_vector < 0) { \
146                 double t = TO_RAD(-h); \
147                 float adjacent = sin(t), opposite = cos(t); \
148                 int ix = vector_cx + adjacent * (s) / (FLOAT_MAX) * radius; \
149                 int iy = vector_cy - opposite * (s) / (FLOAT_MAX) * radius; \
150                 CLAMP(ix, 0, vector_w - 1); \
151                 decr_points(vector_rows,vector_h, vinc,vinc,vinc, 4); \
152         } \
153 }
154
155 #define PROCESS_RGB_PIXEL(type,max, column) { \
156         type *rp = (type *)row; \
157         r = (float)rp[0] / max; \
158         g = (float)rp[1] / max; \
159         b = (float)rp[2] / max; \
160         HSV::rgb_to_hsv(r, g, b, h, s, v); \
161         intensity = v; \
162         PROCESS_PIXEL(column) \
163 }
164
165 #define PROCESS_BGR_PIXEL(type,max, column) { \
166         type *rp = (type *)row; \
167         b = (float)rp[0] / max; \
168         g = (float)rp[1] / max; \
169         r = (float)rp[2] / max; \
170         HSV::rgb_to_hsv(r, g, b, h, s, v); \
171         intensity = v; \
172         PROCESS_PIXEL(column) \
173 }
174
175 #define PROCESS_YUV_PIXEL(column, y_in, u_in, v_in) { \
176         YUV::yuv.yuv_to_rgb_f(r, g, b, y_in, u_in, v_in); \
177         HSV::rgb_to_hsv(r, g, b, h, s, v); \
178         intensity = v; \
179         PROCESS_PIXEL(column) \
180 }
181
182 void ScopeUnit::process_package(LoadPackage *package)
183 {
184         ScopePackage *pkg = (ScopePackage*)package;
185
186         float r, g, b;
187         float h, s, v;
188         float intensity;
189         int use_hist = gui->use_hist;
190         int use_hist_parade = gui->use_hist_parade;
191         int use_vector = gui->use_vector;
192         int use_wave = gui->use_wave;
193         int use_wave_parade = gui->use_wave_parade;
194         int vector_cx = gui->vector_cx;
195         int vector_cy = gui->vector_cy;
196         int radius = gui->radius;
197         VFrame *waveform_vframe = gui->waveform_vframe;
198         VFrame *vector_vframe = gui->vector_vframe;
199         int wave_h = waveform_vframe->get_h();
200         int wave_w = waveform_vframe->get_w();
201         int vector_h = vector_vframe->get_h();
202         int vector_w = vector_vframe->get_w();
203         int dat_w = gui->data_frame->get_w();
204         int dat_h = gui->data_frame->get_h();
205         int winc = (wave_w * wave_h) / (dat_w * dat_h);
206         if( use_wave_parade ) winc *= 3;
207         winc += 2;  winc = (winc << gui->use_wave_gain) / 4;
208         int vinc = 3*(vector_w * vector_h) / (dat_w * dat_h);
209         vinc += 2;  vinc = (vinc << gui->use_vect_gain) / 4;
210         float vincf = vinc;
211         uint8_t **waveform_rows = waveform_vframe->get_rows();
212         uint8_t **vector_rows = vector_vframe->get_rows();
213         uint8_t **rows = gui->data_frame->get_rows();
214
215         switch( gui->data_frame->get_color_model() ) {
216         case BC_RGB888:
217                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
218                         uint8_t *row = rows[y];
219                         for( int x=0; x<dat_w; ++x ) {
220                                 PROCESS_RGB_PIXEL(uint8_t,0xff, x)
221                                 row += 3*sizeof(uint8_t);
222                         }
223                 }
224                 break;
225         case BC_RGBA8888:
226                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
227                         uint8_t *row = rows[y];
228                         for( int x=0; x<dat_w; ++x ) {
229                                 PROCESS_RGB_PIXEL(uint8_t,0xff, x)
230                                 row += 4*sizeof(uint8_t);
231                         }
232                 }
233                 break;
234         case BC_RGB161616:
235                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
236                         uint8_t *row = rows[y];
237                         for( int x=0; x<dat_w; ++x ) {
238                                 PROCESS_RGB_PIXEL(uint16_t,0xffff, x)
239                                 row += 3*sizeof(uint16_t);
240                         }
241                 }
242                 break;
243         case BC_RGBA16161616:
244                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
245                         uint8_t *row = rows[y];
246                         for( int x=0; x<dat_w; ++x ) {
247                                 PROCESS_RGB_PIXEL(uint16_t,0xffff, x)
248                                 row += 4*sizeof(uint16_t);
249                         }
250                 }
251                 break;
252         case BC_BGR888:
253                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
254                         uint8_t *row = rows[y];
255                         for( int x=0; x<dat_w; ++x ) {
256                                 PROCESS_BGR_PIXEL(uint8_t,0xff, x)
257                                 row += 3*sizeof(uint8_t);
258                         }
259                 }
260                 break;
261         case BC_BGR8888:
262                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
263                         uint8_t *row = rows[y];
264                         for( int x=0; x<dat_w; ++x ) {
265                                 PROCESS_BGR_PIXEL(uint8_t,0xff, x)
266                                 row += 4*sizeof(uint8_t);
267                         }
268                 }
269                 break;
270         case BC_RGB_FLOAT:
271                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
272                         uint8_t *row = rows[y];
273                         for( int x=0; x<dat_w; ++x ) {
274                                 PROCESS_RGB_PIXEL(float,1.f, x)
275                                 row += 3*sizeof(float);
276                         }
277                 }
278                 break;
279         case BC_RGBA_FLOAT:
280                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
281                         uint8_t *row = rows[y];
282                         for( int x=0; x<dat_w; ++x ) {
283                                 PROCESS_RGB_PIXEL(float,1.f, x)
284                                 row += 4*sizeof(float);
285                         }
286                 }
287                 break;
288         case BC_YUV888:
289                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
290                         uint8_t *row = rows[y];
291                         for( int x=0; x<dat_w; ++x ) {
292                                 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
293                                 row += 3*sizeof(uint8_t);
294                         }
295                 }
296                 break;
297
298         case BC_YUVA8888:
299                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
300                         uint8_t *row = rows[y];
301                         for( int x=0; x<dat_w; ++x ) {
302                                 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
303                                 row += 4*sizeof(uint8_t);
304                         }
305                 }
306                 break;
307         case BC_YUV420P: {
308                 uint8_t *yp = gui->data_frame->get_y();
309                 uint8_t *up = gui->data_frame->get_u();
310                 uint8_t *vp = gui->data_frame->get_v();
311                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
312                         uint8_t *y_row = yp + y * dat_w;
313                         uint8_t *u_row = up + (y / 2) * (dat_w / 2);
314                         uint8_t *v_row = vp + (y / 2) * (dat_w / 2);
315                         for( int x=0; x<dat_w; x+=2 ) {
316                                 PROCESS_YUV_PIXEL(x, *y_row, *u_row, *v_row);
317                                 ++y_row;
318                                 PROCESS_YUV_PIXEL(x + 1, *y_row, *u_row, *v_row);
319                                 ++y_row;  ++u_row;  ++v_row;
320                         }
321                 }
322                 break; }
323         case BC_YUV422:
324                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
325                         uint8_t *row = rows[y];
326                         for( int x=0; x<dat_w; x+=2 ) {
327                                 PROCESS_YUV_PIXEL(x, row[0], row[1], row[3]);
328                                 PROCESS_YUV_PIXEL(x + 1, row[2], row[1], row[3]);
329                                 row += 4*sizeof(uint8_t);
330                         }
331                 }
332                 break;
333
334         default:
335                 printf("ScopeUnit::process_package %d: color_model=%d unrecognized\n",
336                         __LINE__, gui->data_frame->get_color_model());
337                 break;
338         }
339 }
340
341
342 ScopeEngine::ScopeEngine(ScopeGUI *gui, int cpus)
343  : LoadServer(cpus, cpus)
344 {
345 //printf("ScopeEngine::ScopeEngine %d cpus=%d\n", __LINE__, cpus);
346         this->gui = gui;
347 }
348
349 ScopeEngine::~ScopeEngine()
350 {
351 }
352
353 void ScopeEngine::init_packages()
354 {
355         int y = 0, h = gui->data_frame->get_h();
356         for( int i=0,n=LoadServer::get_total_packages(); i<n; ) {
357                 ScopePackage *pkg = (ScopePackage*)get_package(i);
358                 pkg->row1 = y;
359                 pkg->row2 = y = (++i * h) / n;
360         }
361
362         for( int i=0,n=LoadServer::get_total_packages(); i<n; ++i ) {
363                 ScopeUnit *unit = (ScopeUnit*)get_client(i);
364                 for( int j=0; j<HIST_SECTIONS; ++j )
365                         bzero(unit->bins[j], sizeof(int) * TOTAL_BINS);
366         }
367 }
368
369
370 LoadClient* ScopeEngine::new_client()
371 {
372         return new ScopeUnit(gui, this);
373 }
374
375 LoadPackage* ScopeEngine::new_package()
376 {
377         return new ScopePackage;
378 }
379
380 void ScopeEngine::process()
381 {
382         process_packages();
383
384         for(int i = 0; i < HIST_SECTIONS; i++)
385                 bzero(gui->bins[i], sizeof(int) * TOTAL_BINS);
386
387         for(int i=0,n=get_total_clients(); i<n; ++i ) {
388                 ScopeUnit *unit = (ScopeUnit*)get_client(i);
389                 for( int j=0; j<HIST_SECTIONS; ++j ) {
390                         int *bp = gui->bins[j], *up = unit->bins[j];
391                         for( int k=TOTAL_BINS; --k>=0; ++bp,++up ) *bp += *up;
392                 }
393         }
394 }
395
396
397 ScopeGUI::ScopeGUI(Theme *theme,
398         int x, int y, int w, int h, int cpus)
399  : PluginClientWindow(_(PROGRAM_NAME ": Scopes"),
400         x, y, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
401 {
402         this->x = x;
403         this->y = y;
404         this->w = w;
405         this->h = h;
406         this->theme = theme;
407         this->cpus = cpus;
408         reset();
409 }
410
411 ScopeGUI::ScopeGUI(PluginClient *plugin, int w, int h)
412  : PluginClientWindow(plugin, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
413 {
414         this->x = get_x();
415         this->y = get_y();
416         this->w = w;
417         this->h = h;
418         this->theme = plugin->get_theme();
419         this->cpus = plugin->PluginClient::smp + 1;
420         reset();
421 }
422
423 ScopeGUI::~ScopeGUI()
424 {
425         delete waveform_vframe;
426         delete vector_vframe;
427         delete wheel_vframe;
428         delete engine;
429         delete box_blur;
430         delete temp_frame;
431         delete wave_slider;
432         delete vect_slider;
433         delete grat_image;
434         delete overlay;
435 }
436
437 void ScopeGUI::reset()
438 {
439         waveform_vframe = 0;
440         vector_vframe = 0;
441         wheel_vframe = 0;
442         engine = 0;
443         box_blur = 0;
444         temp_frame = 0;
445         wave_slider = 0;
446         vect_slider = 0;
447         grat_image = 0;
448         overlay = 0;
449         grat_idx = 0;
450         vect_grats = 0;
451
452         output_frame = 0;
453         data_frame = 0;
454         frame_w = 1;
455         use_smooth = 1;
456         use_wave_gain = 5;
457         use_vect_gain = 5;
458         use_hist = 0;
459         use_wave = 1;
460         use_vector = 1;
461         use_hist_parade = 0;
462         use_wave_parade = 0;
463         use_graticule = 0;
464         waveform = 0;
465         vectorscope = 0;
466         histogram = 0;
467         wave_w = wave_h = 0;
468         vector_w = vector_h = 0;
469         vector_cx = vector_cy = 0;
470         radius = 0;
471         text_color = GREEN;
472         dark_color = (text_color>>2) & 0x3f3f3f;
473         BC_Resources *resources = BC_WindowBase::get_resources();
474         if( resources->bg_color == 0xffffff )
475                 text_color = dark_color;
476 }
477
478 void ScopeGUI::create_objects()
479 {
480         if( use_hist && use_hist_parade )
481                 use_hist = 0;
482         if( use_wave && use_wave_parade )
483                 use_wave = 0;
484         if( !engine ) engine = new ScopeEngine(this, cpus);
485         grat_idx = use_graticule; // last graticule
486         use_graticule = 0;
487
488         lock_window("ScopeGUI::create_objects");
489         int margin = theme->widget_border;
490         int x = margin;
491         int y = margin;
492         add_subwindow(scope_menu = new ScopeMenu(this, x, y));
493         scope_menu->create_objects();
494         int x1 = x + scope_menu->get_w() + 2*margin;
495         add_subwindow(smooth = new ScopeSmooth(this, x1, y));
496
497         create_panels();
498         update_toggles();
499         show_window();
500         update_scope();
501         unlock_window();
502 }
503
504 void ScopeGUI::create_panels()
505 {
506         calculate_sizes(get_w(), get_h());
507         int slider_w = xS(100);
508         int margin = theme->widget_border;
509         if( (use_wave || use_wave_parade) ) {
510                 int px = wave_x + wave_w - slider_w - xS(5);
511                 int py = wave_y - ScopeGain::calculate_h() - yS(5);
512                 if( !waveform ) {
513                         add_subwindow(waveform = new ScopeWaveform(this,
514                                 wave_x, wave_y, wave_w, wave_h));
515                         waveform->create_objects();
516                         wave_slider = new ScopeWaveSlider(this, px, py, slider_w);
517                         wave_slider->create_objects();
518                 }
519                 else {
520                         waveform->reposition_window(
521                                 wave_x, wave_y, wave_w, wave_h);
522                         waveform->clear_box(0, 0, wave_w, wave_h);
523                         wave_slider->reposition_window(px, py);
524                 }
525         }
526         else if( !(use_wave || use_wave_parade) && waveform ) {
527                 delete waveform;     waveform = 0;
528                 delete wave_slider;  wave_slider = 0;
529         }
530
531         if( use_vector ) {
532                 int vx = vector_x + vector_w - slider_w - xS(5);
533                 int vy = vector_y - ScopeGain::calculate_h() - yS(5);
534                 if( !vectorscope ) {
535                         add_subwindow(vectorscope = new ScopeVectorscope(this,
536                                 vector_x, vector_y, vector_w, vector_h));
537                         vectorscope->create_objects();
538                         vect_slider = new ScopeVectSlider(this, vx, vy, slider_w);
539                         vect_slider->create_objects();
540                         if( use_vector < 0 ) {
541                                 vect_grats = new ScopeVectGrats(this, vx, 2*margin);
542                                 add_subwindow(vect_grats);
543                                 vect_grats->create_objects();
544                         }
545                 }
546                 else {
547                         vectorscope->reposition_window(
548                                 vector_x, vector_y, vector_w, vector_h);
549                         vectorscope->clear_box(0, 0, vector_w, vector_h);
550                         vect_slider->reposition_window(vx, vy);
551                         if( use_vector > 0 ) {
552                                 delete vect_grats;  vect_grats = 0;
553                         }
554                         else if( !vect_grats ) {
555                                 vect_grats = new ScopeVectGrats(this, vx, 2*margin);
556                                 add_subwindow(vect_grats);
557                                 vect_grats->create_objects();
558                         }
559                         else
560                                 vect_grats->reposition_window(vx, 2*margin);
561                 }
562         }
563         else if( !use_vector && vectorscope ) {
564                 delete vectorscope;  vectorscope = 0;
565                 delete vect_slider;  vect_slider = 0;
566                 delete vect_grats;   vect_grats = 0;
567         }
568
569         if( (use_hist || use_hist_parade) ) {
570                 if( !histogram ) {
571 // printf("ScopeGUI::create_panels %d %d %d %d %d\n", __LINE__,
572 //  hist_x, hist_y, hist_w, hist_h);
573                         add_subwindow(histogram = new ScopeHistogram(this,
574                                 hist_x, hist_y, hist_w, hist_h));
575                         histogram->create_objects();
576                 }
577                 else {
578                         histogram->reposition_window(
579                                 hist_x, hist_y, hist_w, hist_h);
580                         histogram->clear_box(0, 0, hist_w, hist_h);
581                 }
582         }
583         else if( !(use_hist || use_hist_parade) ) {
584                 delete histogram;  histogram = 0;
585         }
586
587         allocate_vframes();
588         clear_points(0);
589         draw_overlays(1, 1, 0);
590 }
591
592 void ScopeGUI::clear_points(int flash)
593 {
594         if( histogram )
595                 histogram->clear_point();
596         if( waveform )
597                 waveform->clear_point();
598         if( vectorscope )
599                 vectorscope->clear_point();
600         if( histogram && flash )
601                 histogram->flash(0);
602         if( waveform && flash )
603                 waveform->flash(0);
604         if( vectorscope && flash )
605                 vectorscope->flash(0);
606 }
607
608 void ScopeGUI::toggle_event()
609 {
610 }
611
612 void ScopeGUI::calculate_sizes(int w, int h)
613 {
614         int margin = theme->widget_border;
615         int menu_h = smooth->get_h() + scope_menu->get_h() + margin * 3;
616         int text_w = get_text_width(SMALLFONT, "000") + margin * 2;
617         int total_panels = ((use_hist || use_hist_parade) ? 1 : 0) +
618                 ((use_wave || use_wave_parade) ? 1 : 0) +
619                 (use_vector ? 1 : 0);
620         int x = margin;
621
622         int panel_w = (w - margin) / (total_panels > 0 ? total_panels : 1);
623 // Vectorscope determines the size of everything else
624 // Always last panel
625         vector_w = 0;
626         if( use_vector ) {
627                 vector_x = w - panel_w + text_w;
628                 vector_w = w - margin - vector_x;
629                 vector_y = menu_h;
630                 vector_h = h - vector_y - margin;
631
632                 if( vector_w > vector_h ) {
633                         vector_w = vector_h;
634                         vector_x = w - margin - vector_w;
635                 }
636                 --total_panels;
637                 if(total_panels > 0)
638                         panel_w = (vector_x - text_w - margin) / total_panels;
639         }
640         vector_cx = vector_w / 2;
641         vector_cy = vector_h / 2;
642         radius = bmin(vector_cx, vector_cy);
643
644 // Histogram is always 1st panel
645         if( use_hist || use_hist_parade ) {
646                 hist_x = x;
647                 hist_y = menu_h;
648                 hist_w = panel_w - margin;
649                 hist_h = h - hist_y - margin;
650
651                 --total_panels;
652                 x += panel_w;
653         }
654
655         if( use_wave || use_wave_parade ) {
656                 wave_x = x + text_w;
657                 wave_y = menu_h;
658                 wave_w = panel_w - margin - text_w;
659                 wave_h = h - wave_y - margin;
660         }
661
662 }
663
664
665 void ScopeGUI::allocate_vframes()
666 {
667         if(waveform_vframe) delete waveform_vframe;
668         if(vector_vframe) delete vector_vframe;
669         if(wheel_vframe) delete wheel_vframe;
670
671         int w, h;
672 //printf("ScopeGUI::allocate_vframes %d %d %d %d %d\n", __LINE__,
673 // wave_w, wave_h, vector_w, vector_h);
674         int xs16 = xS(16), ys16 = yS(16);
675         w = MAX(wave_w, xs16);
676         h = MAX(wave_h, ys16);
677         waveform_vframe = new VFrame(w, h, BC_RGB888);
678         w = MAX(vector_w, xs16);
679         h = MAX(vector_h, ys16);
680         vector_vframe = new VFrame(w, h, BC_RGBA8888);
681         vector_vframe->set_clear_color(BLACK, 0xff);
682         wheel_vframe = 0;
683 }
684
685
686 int ScopeGUI::resize_event(int w, int h)
687 {
688         clear_box(0, 0, w, h);
689         this->w = w;
690         this->h = h;
691         calculate_sizes(w, h);
692         int margin = theme->widget_border;
693
694         if( waveform ) {
695                 waveform->reposition_window(wave_x, wave_y, wave_w, wave_h);
696                 waveform->clear_box(0, 0, wave_w, wave_h);
697                 int px = wave_x + wave_w - wave_slider->get_w() - margin;
698                 int py = wave_y - wave_slider->get_h() - margin;
699                 wave_slider->reposition_window(px, py);
700         }
701
702         if( histogram ) {
703                 histogram->reposition_window(hist_x, hist_y, hist_w, hist_h);
704                 histogram->clear_box(0, 0, hist_w, hist_h);
705         }
706
707         if( vectorscope ) {
708                 vectorscope->reposition_window(vector_x, vector_y, vector_w, vector_h);
709                 vectorscope->clear_box(0, 0, vector_w, vector_h);
710                 int vx = vector_x + vector_w - vect_slider->get_w() - margin;
711                 int vy = vector_y - vect_slider->get_h() - margin;
712                 vect_slider->reposition_window(vx, vy);
713                 if( vect_grats )
714                         vect_grats->reposition_window(vx, 2*margin);
715         }
716
717         allocate_vframes();
718         clear_points(0);
719         draw_overlays(1, 1, 1);
720         update_scope();
721         return 1;
722 }
723
724 int ScopeGUI::translation_event()
725 {
726         x = get_x();
727         y = get_y();
728         PluginClientWindow::translation_event();
729         return 0;
730 }
731
732
733 void ScopeGUI::draw_overlays(int overlays, int borders, int flush)
734 {
735         int margin = theme->widget_border;
736         if( overlays && borders ) {
737                 clear_box(0, 0, get_w(), get_h());
738         }
739
740         if( overlays ) {
741                 set_line_dashes(1);
742                 set_color(text_color);
743                 set_font(SMALLFONT);
744
745                 if( histogram && (use_hist || use_hist_parade) ) {
746                         histogram->draw_line(hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), 0,
747                                         hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), hist_h);
748                         histogram->draw_line(hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), 0,
749                                         hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), hist_h);
750                         set_line_dashes(0);
751                         histogram->draw_point();
752                         set_line_dashes(1);
753                         histogram->flash(0);
754                 }
755
756 // Waveform overlay
757                 if( waveform && (use_wave || use_wave_parade) ) {
758                         for( int i=0; i<=WAVEFORM_DIVISIONS; ++i ) {
759                                 int y = wave_h * i / WAVEFORM_DIVISIONS;
760                                 int text_y = y + wave_y + get_text_ascent(SMALLFONT) / 2;
761                                 CLAMP(text_y, waveform->get_y() + get_text_ascent(SMALLFONT), waveform->get_y() + waveform->get_h() - 1);
762                                 char string[BCTEXTLEN];
763                                 sprintf(string, "%d", (int)lround((FLOAT_MAX -
764                                         i * (FLOAT_MAX - FLOAT_MIN) / WAVEFORM_DIVISIONS) * 100));
765                                 int text_x = wave_x - get_text_width(SMALLFONT, string) - margin;
766                                 set_color(text_color);
767                                 draw_text(text_x, text_y, string);
768                                 CLAMP(y, 0, waveform->get_h() - 1);
769                                 set_color(dark_color);
770                                 waveform->draw_line(0, y, wave_w, y);
771                                 waveform->draw_rectangle(0, 0, wave_w, wave_h);
772                         }
773                         set_line_dashes(0);
774                         waveform->draw_point();
775                         set_line_dashes(1);
776                         waveform->flash(0);
777                 }
778 // Vectorscope overlay
779                 if( vectorscope && use_vector ) {
780                         set_color(text_color);
781                         vectorscope->draw_point();
782                         vectorscope->flash(0);
783                         set_line_dashes(1);
784                 }
785
786                 set_font(MEDIUMFONT);
787                 set_line_dashes(0);
788         }
789
790         if( borders ) {
791                 if( use_hist || use_hist_parade ) {
792                         draw_3d_border(hist_x - 2, hist_y - 2, hist_w + 4, hist_h + 4,
793                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
794                 }
795                 if( use_wave || use_wave_parade ) {
796                         draw_3d_border(wave_x - 2, wave_y - 2, wave_w + 4, wave_h + 4,
797                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
798                 }
799                 if( use_vector ) {
800                         draw_3d_border(vector_x - 2, vector_y - 2, vector_w + 4, vector_h + 4,
801                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
802                 }
803         }
804
805         flash(0);
806         if(flush) this->flush();
807 }
808
809 void ScopeGUI::draw_scope()
810 {
811         int graticule = use_vector < 0 ? grat_idx : 0;
812         if( grat_image && use_graticule != graticule ) {
813                 delete grat_image;   grat_image = 0;
814         }
815         if( !grat_image && graticule > 0 )
816                 grat_image = VFramePng::vframe_png(grat_paths[graticule]);
817         if( grat_image ) {
818                 if( !overlay )
819                         overlay = new OverlayFrame(1);
820                 int cx = vector_cx, cy = vector_cy, r = radius;
821                 int iw = grat_image->get_w(), ih = grat_image->get_h();
822                 overlay->overlay(vector_vframe, grat_image,
823                         0,0, iw, ih, cx-r,cy-r, cx+r, cy+r,
824                         1, TRANSFER_NORMAL, CUBIC_CUBIC);
825         }
826         use_graticule = graticule;
827         vectorscope->draw_vframe(vector_vframe);
828         if( use_vector > 0 ) {
829                 int margin = theme->widget_border;
830                 set_line_dashes(1);
831                 for( int i=1; i<=VECTORSCOPE_DIVISIONS; i+=2 ) {
832                         int y = vector_cy - radius * i / VECTORSCOPE_DIVISIONS;
833                         int text_y = y + vector_y + get_text_ascent(SMALLFONT) / 2;
834                         set_color(text_color);
835                         char string[BCTEXTLEN];
836                         sprintf(string, "%d",
837                                 (int)((FLOAT_MAX / VECTORSCOPE_DIVISIONS * i) * 100));
838                         int text_x = vector_x - get_text_width(SMALLFONT, string) - margin;
839                         draw_text(text_x, text_y, string);
840                         int x = vector_cx - radius * i / VECTORSCOPE_DIVISIONS;
841                         int w = radius * i / VECTORSCOPE_DIVISIONS * 2;
842                         int h = radius * i / VECTORSCOPE_DIVISIONS * 2;
843                         if( i+2 > VECTORSCOPE_DIVISIONS )
844                                 set_line_dashes(0);
845                         set_color(dark_color);
846                         vectorscope->draw_circle(x, y, w, h);
847                 }
848                 float th = TO_RAD(90 + 32.875);
849                 vectorscope->draw_radient(th, 0.1f, .75f, dark_color);
850         }
851 }
852
853
854 void ScopeGUI::update_graticule(int idx)
855 {
856         grat_idx = idx;
857         update_scope();
858         toggle_event();
859 }
860
861 void ScopeGUI::draw_colorwheel(VFrame *dst, int bg_color)
862 {
863 // downfactor radius to prevent extreme edge from showing behind graticule
864         float cx = vector_cx, cy = vector_cy, rad = radius * 0.99;
865         int color_model = dst->get_color_model();
866         int bpp = BC_CModels::calculate_pixelsize(color_model);
867         int bg_r = (bg_color>>16) & 0xff;
868         int bg_g = (bg_color>> 8) & 0xff;
869         int bg_b = (bg_color>> 0) & 0xff;
870         int w = dst->get_w(), h = dst->get_h();
871         unsigned char **rows = dst->get_rows();
872         for( int y=0; y<h; ++y ) {
873                 unsigned char *row = rows[y];
874                 for( int x=0; x<w; ++x,row+=bpp ) {
875                         int dx = cx-x, dy = cy-y;
876                         float d = sqrt(dx*dx + dy*dy);
877                         float r, g, b;
878                         if( d < rad ) {
879                                 float h = TO_DEG(atan2(cx-x, cy-y));
880                                 if( h < 0 ) h += 360;
881                                 float s = d / rad, v = 255;
882                                 HSV::hsv_to_rgb(r, g, b, h, s, v);
883                         }
884                         else {
885                                  r = bg_r; g = bg_g; b = bg_b;
886                         }
887                         row[0] = r; row[1] = g; row[2] = b;  row[3] = 0xff;
888                 }
889         }
890 }
891
892
893 void ScopeGUI::process(VFrame *output_frame)
894 {
895         lock_window("ScopeGUI::process");
896         this->output_frame = output_frame;
897         int ow = output_frame->get_w(), oh = output_frame->get_h();
898         if( use_smooth ) {
899                 VFrame::get_temp(temp_frame, ow, oh, BC_RGB161616);
900                 temp_frame->transfer_from(output_frame);
901                 if( !box_blur ) box_blur = new BoxBlur(cpus);
902                 box_blur->blur(temp_frame, temp_frame, 2, 2);
903                 data_frame = temp_frame;
904         }
905         else
906                 data_frame = output_frame;
907         frame_w = data_frame->get_w();
908         bzero(waveform_vframe->get_data(), waveform_vframe->get_data_size());
909         if( use_vector > 0 )
910                 vector_vframe->clear_frame();
911         else if( use_vector < 0 ) {
912                 if( wheel_vframe && (
913                      wheel_vframe->get_w() != vector_w ||
914                      wheel_vframe->get_h() != vector_h ) ) {
915                         delete wheel_vframe;  wheel_vframe = 0;
916                 }
917                 if( !wheel_vframe ) {
918                         wheel_vframe = new VFrame(vector_w, vector_h, BC_RGBA8888);
919                         draw_colorwheel(wheel_vframe, BLACK);
920                 }
921                 vector_vframe->copy_from(wheel_vframe);
922         }
923         engine->process();
924
925         if( histogram )
926                 histogram->draw(0, 0);
927         if( waveform )
928                 waveform->draw_vframe(waveform_vframe);
929         if( vectorscope )
930                 draw_scope();
931
932         draw_overlays(1, 0, 1);
933         unlock_window();
934 }
935
936 void ScopeGUI::update_toggles()
937 {
938         scope_menu->update_toggles();
939 }
940
941 ScopePanel::ScopePanel(ScopeGUI *gui, int x, int y, int w, int h)
942  : BC_SubWindow(x, y, w, h, BLACK)
943 {
944         this->gui = gui;
945         is_dragging = 0;
946 }
947
948 void ScopePanel::create_objects()
949 {
950         set_cursor(CROSS_CURSOR, 0, 0);
951         clear_box(0, 0, get_w(), get_h());
952 }
953
954 void ScopePanel::update_point(int x, int y)
955 {
956 }
957
958 void ScopePanel::draw_point()
959 {
960 }
961
962 void ScopePanel::clear_point()
963 {
964 }
965
966 int ScopePanel::button_press_event()
967 {
968         if( is_event_win() && cursor_inside() ) {
969                 gui->clear_points(1);
970                 is_dragging = 1;
971                 int x = get_cursor_x();
972                 int y = get_cursor_y();
973                 CLAMP(x, 0, get_w() - 1);
974                 CLAMP(y, 0, get_h() - 1);
975                 update_point(x, y);
976                 return 1;
977         }
978         return 0;
979 }
980
981
982 int ScopePanel::cursor_motion_event()
983 {
984         if( is_dragging ) {
985                 int x = get_cursor_x();
986                 int y = get_cursor_y();
987                 CLAMP(x, 0, get_w() - 1);
988                 CLAMP(y, 0, get_h() - 1);
989                 update_point(x, y);
990                 return 1;
991         }
992         return 0;
993 }
994
995
996 int ScopePanel::button_release_event()
997 {
998         if( is_dragging ) {
999                 is_dragging = 0;
1000                 hide_tooltip();
1001                 return 1;
1002         }
1003         return 0;
1004 }
1005
1006
1007 ScopeWaveform::ScopeWaveform(ScopeGUI *gui,
1008                 int x, int y, int w, int h)
1009  : ScopePanel(gui, x, y, w, h)
1010 {
1011         drag_x = -1;
1012         drag_y = -1;
1013 }
1014
1015 void ScopeWaveform::update_point(int x, int y)
1016 {
1017         draw_point();
1018         drag_x = x;
1019         drag_y = y;
1020         int frame_x = x * gui->frame_w / get_w();
1021
1022         if( gui->use_wave_parade ) {
1023                 if( x > get_w() / 3 * 2 )
1024                         frame_x = (x - get_w() / 3 * 2) * gui->frame_w / (get_w() / 3);
1025                 else if( x > get_w() / 3 )
1026                         frame_x = (x - get_w() / 3) * gui->frame_w / (get_w() / 3);
1027                 else
1028                         frame_x = x * gui->frame_w / (get_w() / 3);
1029         }
1030
1031         float value = ((float)get_h() - y) / get_h() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1032         char string[BCTEXTLEN];
1033         sprintf(string, "X: %d Value: %.3f", frame_x, value);
1034         show_tooltip(string);
1035
1036         draw_point();
1037         flash(1);
1038 }
1039
1040 void ScopeWaveform::draw_point()
1041 {
1042         if( drag_x >= 0 ) {
1043                 set_inverse();
1044                 set_color(0xffffff);
1045                 set_line_width(2);
1046                 draw_line(0, drag_y, get_w(), drag_y);
1047                 draw_line(drag_x, 0, drag_x, get_h());
1048                 set_line_width(1);
1049                 set_opaque();
1050         }
1051 }
1052
1053 void ScopeWaveform::clear_point()
1054 {
1055         draw_point();
1056         drag_x = -1;
1057         drag_y = -1;
1058 }
1059
1060 ScopeVectorscope::ScopeVectorscope(ScopeGUI *gui,
1061                 int x, int y, int w, int h)
1062  : ScopePanel(gui, x, y, w, h)
1063 {
1064         drag_radius = 0;
1065         drag_angle = 0;
1066 }
1067
1068 void ScopeVectorscope::clear_point()
1069 {
1070 // Hide it
1071         draw_point();
1072         drag_radius = 0;
1073         drag_angle = 0;
1074 }
1075
1076 void ScopeVectorscope::update_point(int x, int y)
1077 {
1078 // Hide it
1079         draw_point();
1080         int dx = x - gui->vector_cx, dy = y - gui->vector_cy;
1081         drag_radius = sqrt(dx*dx + dy*dy);
1082         if( drag_radius > gui->radius ) drag_radius = gui->radius;
1083         float saturation = (float)drag_radius / gui->radius * FLOAT_MAX;
1084         drag_angle = atan2(-dy, dx);
1085         float hue = TO_DEG(drag_angle) - 90;
1086         if( hue < 0 ) hue += 360;
1087
1088         char string[BCTEXTLEN];
1089         sprintf(string, "Hue: %.3f Sat: %.3f", hue, saturation);
1090         show_tooltip(string);
1091
1092 // Show it
1093         draw_point();
1094         flash(1);
1095 }
1096
1097 void ScopeVectorscope::draw_point()
1098 {
1099         set_inverse();
1100         draw_point(drag_angle, drag_radius, RED);
1101         set_opaque();
1102 }
1103
1104 void ScopeVectorscope::draw_point(float th, float r, int color)
1105 {
1106         if( r > 0 ) {
1107                 set_color(color);
1108                 set_line_width(2);
1109                 int cx = gui->vector_cx, cy = gui->vector_cy;
1110                 draw_circle(cx-r, cy-r, r*2, r*2);
1111                 set_line_width(1);
1112                 float radius = gui->radius;
1113                 draw_radient(th, 0.f, drag_radius/radius, color);
1114         }
1115 }
1116
1117 void ScopeVectorscope::draw_radient(float th, float r1, float r2, int color)
1118 {
1119         if( r2 > 0 && r2 >= r1 ) {
1120                 set_color(color);
1121                 set_line_width(2);
1122                 int r = gui->radius;
1123                 double cth = r*cos(th), sth = -r*sin(th);
1124                 int cx = gui->vector_cx, cy = gui->vector_cy;
1125                 draw_line(cx+r1*cth, cy+r1*sth, cx+r2*cth, cy+r2*sth);
1126                 set_line_width(1);
1127         }
1128 }
1129
1130
1131 ScopeHistogram::ScopeHistogram(ScopeGUI *gui,
1132                 int x, int y, int w, int h)
1133  : ScopePanel(gui, x, y, w, h)
1134 {
1135         drag_x = -1;
1136 }
1137
1138 void ScopeHistogram::clear_point()
1139 {
1140 // Hide it
1141         draw_point();
1142         drag_x = -1;
1143 }
1144
1145 void ScopeHistogram::draw_point()
1146 {
1147         if( drag_x >= 0 ) {
1148                 set_inverse();
1149                 set_color(0xffffff);
1150                 set_line_width(2);
1151                 draw_line(drag_x, 0, drag_x, get_h());
1152                 set_line_width(1);
1153                 set_opaque();
1154         }
1155 }
1156
1157 void ScopeHistogram::update_point(int x, int y)
1158 {
1159         draw_point();
1160         drag_x = x;
1161         float value = (float)x / get_w() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1162
1163         char string[BCTEXTLEN];
1164         sprintf(string, "Value: %.3f", value);
1165         show_tooltip(string);
1166
1167         draw_point();
1168         flash(1);
1169 }
1170
1171
1172
1173 void ScopeHistogram::draw_mode(int mode, int color, int y, int h)
1174 {
1175 // Highest of all bins
1176         int normalize = 1, w = get_w();
1177         int *bin = gui->bins[mode], v;
1178         for( int i=0; i<TOTAL_BINS; ++i )
1179                 if( (v=bin[i]) > normalize ) normalize = v;
1180         double norm = normalize>1 ? log(normalize) : 1e-4;
1181         double vnorm = h / norm;
1182         set_color(color);
1183         for( int x=0; x<w; ++x ) {
1184                 int accum_start = (int)(x * TOTAL_BINS / w);
1185                 int accum_end = (int)((x + 1) * TOTAL_BINS / w);
1186                 CLAMP(accum_start, 0, TOTAL_BINS);
1187                 CLAMP(accum_end, 0, TOTAL_BINS);
1188                 int max = 0;
1189                 for(int i=accum_start; i<accum_end; ++i )
1190                         if( (v=bin[i]) > max ) max = v;
1191 //              max = max * h / normalize;
1192                 max = log(max) * vnorm;
1193                 draw_line(x,y+h - max, x,y+h);
1194         }
1195 }
1196 void ScopeHistogram::draw(int flash, int flush)
1197 {
1198         clear_box(0, 0, get_w(), get_h());
1199         if( gui->use_hist_parade ) {
1200                 draw_mode(0, 0xff0000, 0, get_h() / 3);
1201                 draw_mode(1, 0x00ff00, get_h() / 3, get_h() / 3);
1202                 draw_mode(2, 0x0000ff, get_h() / 3 * 2, get_h() / 3);
1203         }
1204         else {
1205                 draw_mode(3, LTGREY, 0, get_h());
1206         }
1207
1208         if(flash) this->flash(0);
1209         if(flush) this->flush();
1210 }
1211
1212
1213 ScopeScopesOn::ScopeScopesOn(ScopeMenu *scope_menu, const char *text, int id)
1214  : BC_MenuItem(text)
1215 {
1216         this->scope_menu = scope_menu;
1217         this->id = id;
1218 }
1219
1220 int ScopeScopesOn::handle_event()
1221 {
1222         int v = get_checked() ? 0 : 1;
1223         set_checked(v);
1224         ScopeGUI *gui = scope_menu->gui;
1225         switch( id ) {
1226         case SCOPE_HISTOGRAM:
1227                 gui->use_hist = v;
1228                 if( v ) gui->use_hist_parade = 0;
1229                 break;
1230         case SCOPE_HISTOGRAM_RGB:
1231                 gui->use_hist_parade = v;
1232                 if( v ) gui->use_hist = 0;
1233                 break;
1234         case SCOPE_WAVEFORM:
1235                 gui->use_wave = v;
1236                 if( v ) gui->use_wave_parade = 0;
1237                 break;
1238         case SCOPE_WAVEFORM_RGB:
1239                 gui->use_wave_parade = v;
1240                 if( v ) gui->use_wave = 0;
1241                 break;
1242         case SCOPE_WAVEFORM_PLY:
1243                 gui->use_wave_parade = -v;
1244                 if( v ) gui->use_wave = 0;
1245                 break;
1246         case SCOPE_VECTORSCOPE:
1247                 gui->use_vector = v;
1248                 break;
1249         case SCOPE_VECTORWHEEL:
1250                 gui->use_vector = -v;
1251                 break;
1252         }
1253         gui->toggle_event();
1254         gui->update_toggles();
1255         gui->create_panels();
1256         gui->update_scope();
1257         gui->show_window();
1258         return 1;
1259 }
1260
1261 ScopeMenu::ScopeMenu(ScopeGUI *gui, int x, int y)
1262  : BC_PopupMenu(x, y, xS(100), _("Scopes"))
1263 {
1264         this->gui = gui;
1265 }
1266
1267 void ScopeMenu::create_objects()
1268 {
1269         add_item(hist_on =
1270                 new ScopeScopesOn(this, _("Histogram"), SCOPE_HISTOGRAM));
1271         add_item(hist_rgb_on =
1272                 new ScopeScopesOn(this, _("Histogram RGB"), SCOPE_HISTOGRAM_RGB));
1273         add_item(new BC_MenuItem("-"));
1274         add_item(wave_on =
1275                 new ScopeScopesOn(this, _("Waveform"), SCOPE_WAVEFORM));
1276         add_item(wave_rgb_on =
1277                 new ScopeScopesOn(this, _("Waveform RGB"), SCOPE_WAVEFORM_RGB));
1278         add_item(wave_ply_on =
1279                 new ScopeScopesOn(this, _("Waveform ply"), SCOPE_WAVEFORM_PLY));
1280         add_item(new BC_MenuItem("-"));
1281         add_item(vect_on =
1282                 new ScopeScopesOn(this, _("Vectorscope"), SCOPE_VECTORSCOPE));
1283         add_item(vect_wheel =
1284                 new ScopeScopesOn(this, _("VectorWheel"), SCOPE_VECTORWHEEL));
1285 }
1286
1287 void ScopeMenu::update_toggles()
1288 {
1289         hist_on->set_checked(gui->use_hist);
1290         hist_rgb_on->set_checked(gui->use_hist_parade);
1291         wave_on->set_checked(gui->use_wave);
1292         wave_rgb_on->set_checked(gui->use_wave_parade>0);
1293         wave_ply_on->set_checked(gui->use_wave_parade<0);
1294         vect_on->set_checked(gui->use_vector>0);
1295         vect_wheel->set_checked(gui->use_vector<0);
1296 }
1297
1298
1299 ScopeVectGrats::ScopeVectGrats(ScopeGUI *gui, int x, int y)
1300  : BC_PopupMenu(x, y, _("Overlay"))
1301 {
1302         this->gui = gui;
1303 }
1304
1305 #define SCOPE_SEARCHPATH "/scopes"
1306 void ScopeVectGrats::create_objects()
1307 {
1308         gui->grat_paths.remove_all_objects();
1309         ScopeGratItem *item;
1310         add_item(item = new ScopeGratItem(this, _("none"), 0));
1311         if( item->idx == gui->grat_idx ) item->set_checked(1);
1312         gui->grat_paths.append(0);
1313         FileSystem fs;
1314         fs.set_filter("[*.png]");
1315         char scope_path[BCTEXTLEN];
1316         sprintf(scope_path, "%s%s", File::get_plugin_path(), SCOPE_SEARCHPATH);
1317         fs.update(scope_path);
1318         for( int i=0; i<fs.total_files(); ++i ) {
1319                 FileItem *file_item = fs.get_entry(i);
1320                 if( file_item->get_is_dir() ) continue;
1321                 strcpy(scope_path, file_item->get_name());
1322                 char *cp = strrchr(scope_path, '.');
1323                 if( cp ) *cp = 0;
1324                 add_item(item = new ScopeGratItem(this, scope_path, gui->grat_paths.size()));
1325                 if( item->idx == gui->grat_idx ) item->set_checked(1);
1326                 gui->grat_paths.append(cstrdup(file_item->get_path()));
1327         }
1328 }
1329
1330 ScopeGratItem::ScopeGratItem(ScopeVectGrats *vect_grats, const char *text, int idx)
1331  : BC_MenuItem(text)
1332 {
1333         this->vect_grats = vect_grats;
1334         this->idx = idx;
1335 }
1336
1337 int ScopeGratItem::handle_event()
1338 {
1339         for( int i=0,n=vect_grats->total_items(); i<n; ++i ) {
1340                 ScopeGratItem *item = (ScopeGratItem *)vect_grats->get_item(i);
1341                 item->set_checked(item->idx == idx);
1342         }       
1343         vect_grats->gui->update_graticule(idx);
1344         return 1;
1345 }
1346
1347
1348 ScopeGainReset::ScopeGainReset(ScopeGain *gain, int x, int y)
1349  : BC_Button(x, y, gain->gui->theme->get_image_set("reset_button"))
1350 {
1351         this->gain = gain;
1352 }
1353
1354 int ScopeGainReset::calculate_w(BC_Theme *theme)
1355 {
1356         VFrame *vfrm = *theme->get_image_set("reset_button");
1357         return vfrm->get_w();
1358 }
1359
1360 int ScopeGainReset::handle_event()
1361 {
1362         gain->slider->update(5);
1363         return gain->handle_event();
1364 }
1365
1366 ScopeGainSlider::ScopeGainSlider(ScopeGain *gain, int x, int y, int w)
1367  : BC_ISlider(x, y, 0, w, w, 1, 9, *gain->value)
1368 {
1369         this->gain = gain;
1370 }
1371
1372 int ScopeGainSlider::handle_event()
1373 {
1374         return gain->handle_event();
1375 }
1376
1377 ScopeGain::ScopeGain(ScopeGUI *gui, int x, int y, int w, int *value)
1378 {
1379         this->gui = gui;
1380         this->x = x;
1381         this->y = y;
1382         this->w = w;
1383         this->value = value;
1384
1385         slider = 0;
1386         reset = 0;
1387 }
1388 ScopeGain::~ScopeGain()
1389 {
1390         delete reset;
1391         delete slider;
1392 }
1393
1394 int ScopeGain::calculate_h()
1395 {
1396         return BC_ISlider::get_span(0);
1397 }
1398
1399 void ScopeGain::create_objects()
1400 {
1401         reset_w = ScopeGainReset::calculate_w(gui->theme);
1402         gui->add_subwindow(slider = new ScopeGainSlider(this, x, y, w-reset_w-xS(5)));
1403         gui->add_subwindow(reset = new ScopeGainReset(this, x+w-reset_w, y));
1404 }
1405
1406 int ScopeGain::handle_event()
1407 {
1408         *value = slider->get_value();
1409         gui->update_scope();
1410         gui->toggle_event();
1411         return 1;
1412 }
1413
1414 void ScopeGain::reposition_window(int x, int y)
1415 {
1416         this->x = x;  this->y = y;
1417         slider->reposition_window(x, y);
1418         reset->reposition_window(x+w-reset_w, y);
1419 }
1420
1421 ScopeWaveSlider::ScopeWaveSlider(ScopeGUI *gui, int x, int y, int w)
1422  : ScopeGain(gui, x, y, w, &gui->use_wave_gain)
1423 {
1424 }
1425
1426 ScopeVectSlider::ScopeVectSlider(ScopeGUI *gui, int x, int y, int w)
1427  : ScopeGain(gui, x, y, w, &gui->use_vect_gain)
1428 {
1429 }
1430
1431 ScopeSmooth::ScopeSmooth(ScopeGUI *gui, int x, int y)
1432  : BC_CheckBox(x, y, gui->use_smooth, _("Smooth"))
1433 {
1434         this->gui = gui;
1435 }
1436
1437 int ScopeSmooth::handle_event()
1438 {
1439         gui->use_smooth = get_value();
1440         gui->update_scope();
1441         gui->toggle_event();
1442         return 1;
1443 }
1444