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