switch move/swap tracks, add mv trk shortcut, update msg
[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_release = 0;
459         use_wave_gain = 5;
460         use_vect_gain = 5;
461         use_hist = 0;
462         use_wave = 1;
463         use_vector = 1;
464         use_hist_parade = 0;
465         use_wave_parade = 0;
466         use_graticule = 0;
467         waveform = 0;
468         vectorscope = 0;
469         histogram = 0;
470         wave_w = wave_h = 0;
471         vector_w = vector_h = 0;
472         vector_cx = vector_cy = 0;
473         radius = 0;
474         text_color = GREEN;
475         dark_color = (text_color>>2) & 0x3f3f3f;
476         BC_Resources *resources = BC_WindowBase::get_resources();
477         if( resources->bg_color == 0xffffff )
478                 text_color = dark_color;
479 }
480
481 void ScopeGUI::create_objects()
482 {
483         if( use_hist && use_hist_parade )
484                 use_hist = 0;
485         if( use_wave && use_wave_parade )
486                 use_wave = 0;
487         if( !engine ) engine = new ScopeEngine(this, cpus);
488         grat_idx = use_graticule; // last graticule
489         use_graticule = 0;
490
491         lock_window("ScopeGUI::create_objects");
492         int margin = theme->widget_border;
493         int x = margin, y = margin;
494         add_subwindow(scope_menu = new ScopeMenu(this, x, y));
495         scope_menu->create_objects();
496         int x1 = x + scope_menu->get_w() + 2*margin;
497         add_subwindow(settings = new ScopeSettings(this, x1, y));
498         settings->create_objects();
499
500         create_panels();
501         update_toggles();
502         show_window();
503         update_scope();
504         unlock_window();
505 }
506
507 void ScopeGUI::create_panels()
508 {
509         calculate_sizes(get_w(), get_h());
510         int slider_w = xS(100);
511         if( (use_wave || use_wave_parade) ) {
512                 int px = wave_x + wave_w - slider_w - xS(5);
513                 int py = wave_y - ScopeGain::calculate_h() - yS(5);
514                 if( !waveform ) {
515                         add_subwindow(waveform = new ScopeWaveform(this,
516                                 wave_x, wave_y, wave_w, wave_h));
517                         waveform->create_objects();
518                         wave_slider = new ScopeWaveSlider(this, px, py, slider_w);
519                         wave_slider->create_objects();
520                 }
521                 else {
522                         waveform->reposition_window(
523                                 wave_x, wave_y, wave_w, wave_h);
524                         waveform->clear_box(0, 0, wave_w, wave_h);
525                         wave_slider->reposition_window(px, py);
526                 }
527         }
528         else if( !(use_wave || use_wave_parade) && waveform ) {
529                 delete waveform;     waveform = 0;
530                 delete wave_slider;  wave_slider = 0;
531         }
532
533         if( use_vector ) {
534                 int vx = vector_x + vector_w - slider_w - xS(5);
535                 int vy = vector_y - ScopeGain::calculate_h() - yS(5);
536                 if( !vectorscope ) {
537                         add_subwindow(vectorscope = new ScopeVectorscope(this,
538                                 vector_x, vector_y, vector_w, vector_h));
539                         vectorscope->create_objects();
540                         vect_slider = new ScopeVectSlider(this, vx, vy, slider_w);
541                         vect_slider->create_objects();
542                 }
543                 else {
544                         vectorscope->reposition_window(
545                                 vector_x, vector_y, vector_w, vector_h);
546                         vectorscope->clear_box(0, 0, vector_w, vector_h);
547                         vect_slider->reposition_window(vx, vy);
548                 }
549         }
550         else if( !use_vector && vectorscope ) {
551                 delete vectorscope;  vectorscope = 0;
552                 delete vect_slider;  vect_slider = 0;
553         }
554
555         if( (use_hist || use_hist_parade) ) {
556                 if( !histogram ) {
557 // printf("ScopeGUI::create_panels %d %d %d %d %d\n", __LINE__,
558 //  hist_x, hist_y, hist_w, hist_h);
559                         add_subwindow(histogram = new ScopeHistogram(this,
560                                 hist_x, hist_y, hist_w, hist_h));
561                         histogram->create_objects();
562                 }
563                 else {
564                         histogram->reposition_window(
565                                 hist_x, hist_y, hist_w, hist_h);
566                         histogram->clear_box(0, 0, hist_w, hist_h);
567                 }
568         }
569         else if( !(use_hist || use_hist_parade) ) {
570                 delete histogram;  histogram = 0;
571         }
572
573         allocate_vframes();
574         clear_points(0);
575         draw_overlays(1, 1, 0);
576 }
577
578 void ScopeGUI::clear_points(int flash)
579 {
580         if( histogram )
581                 histogram->clear_point();
582         if( waveform )
583                 waveform->clear_point();
584         if( vectorscope )
585                 vectorscope->clear_point();
586         if( histogram && flash )
587                 histogram->flash(0);
588         if( waveform && flash )
589                 waveform->flash(0);
590         if( vectorscope && flash )
591                 vectorscope->flash(0);
592 }
593
594 void ScopeGUI::toggle_event()
595 {
596 }
597
598 void ScopeGUI::calculate_sizes(int w, int h)
599 {
600         int margin = theme->widget_border;
601         int menu_h = scope_menu->get_h() + ScopeGain::calculate_h() + margin * 3;
602         int text_w = get_text_width(SMALLFONT, "000") + margin * 2;
603         int total_panels = ((use_hist || use_hist_parade) ? 1 : 0) +
604                 ((use_wave || use_wave_parade) ? 1 : 0) +
605                 (use_vector ? 1 : 0);
606         int x = margin;
607
608         int panel_w = (w - margin) / (total_panels > 0 ? total_panels : 1);
609 // Vectorscope determines the size of everything else
610 // Always last panel
611         vector_w = 0;
612         if( use_vector ) {
613                 vector_x = w - panel_w + text_w;
614                 vector_w = w - margin - vector_x;
615                 vector_y = menu_h;
616                 vector_h = h - vector_y - margin;
617
618                 if( vector_w > vector_h ) {
619                         vector_w = vector_h;
620                         vector_x = w - margin - vector_w;
621                 }
622                 --total_panels;
623                 if(total_panels > 0)
624                         panel_w = (vector_x - text_w - margin) / total_panels;
625         }
626         vector_cx = vector_w / 2;
627         vector_cy = vector_h / 2;
628         radius = bmin(vector_cx, vector_cy);
629
630 // Histogram is always 1st panel
631         if( use_hist || use_hist_parade ) {
632                 hist_x = x;
633                 hist_y = menu_h;
634                 hist_w = panel_w - margin;
635                 hist_h = h - hist_y - margin;
636
637                 --total_panels;
638                 x += panel_w;
639         }
640
641         if( use_wave || use_wave_parade ) {
642                 wave_x = x + text_w;
643                 wave_y = menu_h;
644                 wave_w = panel_w - margin - text_w;
645                 wave_h = h - wave_y - margin;
646         }
647
648 }
649
650
651 void ScopeGUI::allocate_vframes()
652 {
653         if(waveform_vframe) delete waveform_vframe;
654         if(vector_vframe) delete vector_vframe;
655         if(wheel_vframe) delete wheel_vframe;
656
657         int w, h;
658 //printf("ScopeGUI::allocate_vframes %d %d %d %d %d\n", __LINE__,
659 // wave_w, wave_h, vector_w, vector_h);
660         int xs16 = xS(16), ys16 = yS(16);
661         w = MAX(wave_w, xs16);
662         h = MAX(wave_h, ys16);
663         waveform_vframe = new VFrame(w, h, BC_RGB888);
664         w = MAX(vector_w, xs16);
665         h = MAX(vector_h, ys16);
666         vector_vframe = new VFrame(w, h, BC_RGBA8888);
667         vector_vframe->set_clear_color(BLACK, 0xff);
668         wheel_vframe = 0;
669 }
670
671
672 int ScopeGUI::resize_event(int w, int h)
673 {
674         clear_box(0, 0, w, h);
675         this->w = w;
676         this->h = h;
677         calculate_sizes(w, h);
678         int margin = theme->widget_border;
679
680         if( waveform ) {
681                 waveform->reposition_window(wave_x, wave_y, wave_w, wave_h);
682                 waveform->clear_box(0, 0, wave_w, wave_h);
683                 int px = wave_x + wave_w - wave_slider->get_w() - margin;
684                 int py = wave_y - wave_slider->get_h() - margin;
685                 wave_slider->reposition_window(px, py);
686         }
687
688         if( histogram ) {
689                 histogram->reposition_window(hist_x, hist_y, hist_w, hist_h);
690                 histogram->clear_box(0, 0, hist_w, hist_h);
691         }
692
693         if( vectorscope ) {
694                 vectorscope->reposition_window(vector_x, vector_y, vector_w, vector_h);
695                 vectorscope->clear_box(0, 0, vector_w, vector_h);
696                 int vx = vector_x + vector_w - vect_slider->get_w() - margin;
697                 int vy = vector_y - vect_slider->get_h() - margin;
698                 vect_slider->reposition_window(vx, vy);
699         }
700
701         allocate_vframes();
702         clear_points(0);
703         draw_overlays(1, 1, 1);
704         update_scope();
705         return 1;
706 }
707
708 int ScopeGUI::translation_event()
709 {
710         x = get_x();
711         y = get_y();
712         PluginClientWindow::translation_event();
713         return 0;
714 }
715
716
717 void ScopeGUI::draw_overlays(int overlays, int borders, int flush)
718 {
719         int margin = theme->widget_border;
720         if( overlays && borders ) {
721                 clear_box(0, 0, get_w(), get_h());
722         }
723
724         if( overlays ) {
725                 set_line_dashes(1);
726                 set_color(text_color);
727                 set_font(SMALLFONT);
728
729                 if( histogram && (use_hist || use_hist_parade) ) {
730                         histogram->draw_line(hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), 0,
731                                         hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), hist_h);
732                         histogram->draw_line(hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), 0,
733                                         hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), hist_h);
734                         set_line_dashes(0);
735                         histogram->draw_point();
736                         set_line_dashes(1);
737                         histogram->flash(0);
738                 }
739
740 // Waveform overlay
741                 if( waveform && (use_wave || use_wave_parade) ) {
742                         for( int i=0; i<=WAVEFORM_DIVISIONS; ++i ) {
743                                 int y = wave_h * i / WAVEFORM_DIVISIONS;
744                                 int text_y = y + wave_y + get_text_ascent(SMALLFONT) / 2;
745                                 CLAMP(text_y, waveform->get_y() + get_text_ascent(SMALLFONT), waveform->get_y() + waveform->get_h() - 1);
746                                 char string[BCTEXTLEN];
747                                 sprintf(string, "%d", (int)lround((FLOAT_MAX -
748                                         i * (FLOAT_MAX - FLOAT_MIN) / WAVEFORM_DIVISIONS) * 100));
749                                 int text_x = wave_x - get_text_width(SMALLFONT, string) - margin;
750                                 set_color(text_color);
751                                 draw_text(text_x, text_y, string);
752                                 CLAMP(y, 0, waveform->get_h() - 1);
753                                 set_color(dark_color);
754                                 waveform->draw_line(0, y, wave_w, y);
755                                 waveform->draw_rectangle(0, 0, wave_w, wave_h);
756                         }
757                         set_line_dashes(0);
758                         waveform->draw_point();
759                         set_line_dashes(1);
760                         waveform->flash(0);
761                 }
762 // Vectorscope overlay
763                 if( vectorscope && use_vector ) {
764                         set_color(text_color);
765                         vectorscope->draw_point();
766                         vectorscope->flash(0);
767                         set_line_dashes(1);
768                 }
769
770                 set_font(MEDIUMFONT);
771                 set_line_dashes(0);
772         }
773
774         if( borders ) {
775                 if( use_hist || use_hist_parade ) {
776                         draw_3d_border(hist_x - 2, hist_y - 2, hist_w + 4, hist_h + 4,
777                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
778                 }
779                 if( use_wave || use_wave_parade ) {
780                         draw_3d_border(wave_x - 2, wave_y - 2, wave_w + 4, wave_h + 4,
781                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
782                 }
783                 if( use_vector ) {
784                         draw_3d_border(vector_x - 2, vector_y - 2, vector_w + 4, vector_h + 4,
785                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
786                 }
787         }
788
789         flash(0);
790         if(flush) this->flush();
791 }
792
793 void ScopeGUI::draw_scope()
794 {
795         int graticule = use_vector < 0 ? grat_idx : 0;
796         if( grat_image && use_graticule != graticule ) {
797                 delete grat_image;   grat_image = 0;
798         }
799         if( !grat_image && graticule > 0 )
800                 grat_image = VFramePng::vframe_png(grat_paths[graticule]);
801         if( grat_image ) {
802                 if( !overlay )
803                         overlay = new OverlayFrame(1);
804                 int cx = vector_cx, cy = vector_cy, r = radius;
805                 int iw = grat_image->get_w(), ih = grat_image->get_h();
806                 overlay->overlay(vector_vframe, grat_image,
807                         0,0, iw, ih, cx-r,cy-r, cx+r, cy+r,
808                         1, TRANSFER_NORMAL, CUBIC_CUBIC);
809         }
810         use_graticule = graticule;
811         vectorscope->draw_vframe(vector_vframe);
812         if( use_vector > 0 ) {
813                 int margin = theme->widget_border;
814                 set_line_dashes(1);
815                 for( int i=1; i<=VECTORSCOPE_DIVISIONS; i+=2 ) {
816                         int y = vector_cy - radius * i / VECTORSCOPE_DIVISIONS;
817                         int text_y = y + vector_y + get_text_ascent(SMALLFONT) / 2;
818                         set_color(text_color);
819                         char string[BCTEXTLEN];
820                         sprintf(string, "%d",
821                                 (int)((FLOAT_MAX / VECTORSCOPE_DIVISIONS * i) * 100));
822                         int text_x = vector_x - get_text_width(SMALLFONT, string) - margin;
823                         draw_text(text_x, text_y, string);
824                         int x = vector_cx - radius * i / VECTORSCOPE_DIVISIONS;
825                         int w = radius * i / VECTORSCOPE_DIVISIONS * 2;
826                         int h = radius * i / VECTORSCOPE_DIVISIONS * 2;
827                         if( i+2 > VECTORSCOPE_DIVISIONS )
828                                 set_line_dashes(0);
829                         set_color(dark_color);
830                         vectorscope->draw_circle(x, y, w, h);
831                 }
832                 float th = TO_RAD(90 + 32.875);
833                 vectorscope->draw_radient(th, 0.1f, .75f, dark_color);
834         }
835 }
836
837
838 void ScopeGUI::update_graticule(int idx)
839 {
840         grat_idx = idx;
841         update_scope();
842         toggle_event();
843 }
844
845 void ScopeGUI::draw_colorwheel(VFrame *dst, int bg_color)
846 {
847 // downfactor radius to prevent extreme edge from showing behind graticule
848         float cx = vector_cx, cy = vector_cy, rad = radius * 0.99;
849         int color_model = dst->get_color_model();
850         int bpp = BC_CModels::calculate_pixelsize(color_model);
851         int bg_r = (bg_color>>16) & 0xff;
852         int bg_g = (bg_color>> 8) & 0xff;
853         int bg_b = (bg_color>> 0) & 0xff;
854         int w = dst->get_w(), h = dst->get_h();
855         unsigned char **rows = dst->get_rows();
856         for( int y=0; y<h; ++y ) {
857                 unsigned char *row = rows[y];
858                 for( int x=0; x<w; ++x,row+=bpp ) {
859                         int dx = cx-x, dy = cy-y;
860                         float d = sqrt(dx*dx + dy*dy);
861                         float r, g, b;
862                         if( d < rad ) {
863                                 float h = TO_DEG(atan2(cx-x, cy-y));
864                                 if( h < 0 ) h += 360;
865                                 float s = d / rad, v = 255;
866                                 HSV::hsv_to_rgb(r, g, b, h, s, v);
867                         }
868                         else {
869                                  r = bg_r; g = bg_g; b = bg_b;
870                         }
871                         row[0] = r; row[1] = g; row[2] = b;  row[3] = 0xff;
872                 }
873         }
874 }
875
876
877 void ScopeGUI::process(VFrame *output_frame)
878 {
879         lock_window("ScopeGUI::process");
880         this->output_frame = output_frame;
881         int ow = output_frame->get_w(), oh = output_frame->get_h();
882         if( use_smooth ) {
883                 VFrame::get_temp(temp_frame, ow, oh, BC_RGB161616);
884                 temp_frame->transfer_from(output_frame);
885                 if( !box_blur ) box_blur = new BoxBlur(cpus);
886                 box_blur->blur(temp_frame, temp_frame, 2, 2);
887                 data_frame = temp_frame;
888         }
889         else
890                 data_frame = output_frame;
891         frame_w = data_frame->get_w();
892         bzero(waveform_vframe->get_data(), waveform_vframe->get_data_size());
893         if( use_vector > 0 )
894                 vector_vframe->clear_frame();
895         else if( use_vector < 0 ) {
896                 if( wheel_vframe && (
897                      wheel_vframe->get_w() != vector_w ||
898                      wheel_vframe->get_h() != vector_h ) ) {
899                         delete wheel_vframe;  wheel_vframe = 0;
900                 }
901                 if( !wheel_vframe ) {
902                         wheel_vframe = new VFrame(vector_w, vector_h, BC_RGBA8888);
903                         draw_colorwheel(wheel_vframe, BLACK);
904                 }
905                 vector_vframe->copy_from(wheel_vframe);
906         }
907         engine->process();
908
909         if( histogram )
910                 histogram->draw(0, 0);
911         if( waveform )
912                 waveform->draw_vframe(waveform_vframe);
913         if( vectorscope )
914                 draw_scope();
915
916         draw_overlays(1, 0, 1);
917         unlock_window();
918 }
919
920 void ScopeGUI::update_toggles()
921 {
922         scope_menu->update_toggles();
923         settings->update_toggles();
924 }
925
926 ScopePanel::ScopePanel(ScopeGUI *gui, int x, int y, int w, int h)
927  : BC_SubWindow(x, y, w, h, BLACK)
928 {
929         this->gui = gui;
930         is_dragging = 0;
931 }
932
933 void ScopePanel::create_objects()
934 {
935         set_cursor(CROSS_CURSOR, 0, 0);
936         clear_box(0, 0, get_w(), get_h());
937 }
938
939 void ScopePanel::update_point(int x, int y)
940 {
941 }
942
943 void ScopePanel::draw_point()
944 {
945 }
946
947 void ScopePanel::clear_point()
948 {
949 }
950
951 int ScopePanel::button_press_event()
952 {
953         if( is_event_win() && cursor_inside() ) {
954                 gui->clear_points(1);
955                 is_dragging = 1;
956                 int x = get_cursor_x();
957                 int y = get_cursor_y();
958                 CLAMP(x, 0, get_w() - 1);
959                 CLAMP(y, 0, get_h() - 1);
960                 update_point(x, y);
961                 return 1;
962         }
963         return 0;
964 }
965
966
967 int ScopePanel::cursor_motion_event()
968 {
969         if( is_dragging ) {
970                 int x = get_cursor_x();
971                 int y = get_cursor_y();
972                 CLAMP(x, 0, get_w() - 1);
973                 CLAMP(y, 0, get_h() - 1);
974                 update_point(x, y);
975                 return 1;
976         }
977         return 0;
978 }
979
980
981 int ScopePanel::button_release_event()
982 {
983         if( is_dragging ) {
984                 is_dragging = 0;
985                 hide_tooltip();
986                 return 1;
987         }
988         return 0;
989 }
990
991
992 ScopeWaveform::ScopeWaveform(ScopeGUI *gui,
993                 int x, int y, int w, int h)
994  : ScopePanel(gui, x, y, w, h)
995 {
996         drag_x = -1;
997         drag_y = -1;
998 }
999
1000 void ScopeWaveform::update_point(int x, int y)
1001 {
1002         draw_point();
1003         drag_x = x;
1004         drag_y = y;
1005         int frame_x = x * gui->frame_w / get_w();
1006
1007         if( gui->use_wave_parade ) {
1008                 if( x > get_w() / 3 * 2 )
1009                         frame_x = (x - get_w() / 3 * 2) * gui->frame_w / (get_w() / 3);
1010                 else if( x > get_w() / 3 )
1011                         frame_x = (x - get_w() / 3) * gui->frame_w / (get_w() / 3);
1012                 else
1013                         frame_x = x * gui->frame_w / (get_w() / 3);
1014         }
1015
1016         float value = ((float)get_h() - y) / get_h() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1017         char string[BCTEXTLEN];
1018         sprintf(string, "X: %d Value: %.3f", frame_x, value);
1019         show_tooltip(string);
1020
1021         draw_point();
1022         flash(1);
1023 }
1024
1025 void ScopeWaveform::draw_point()
1026 {
1027         if( drag_x >= 0 ) {
1028                 set_inverse();
1029                 set_color(0xffffff);
1030                 set_line_width(2);
1031                 draw_line(0, drag_y, get_w(), drag_y);
1032                 draw_line(drag_x, 0, drag_x, get_h());
1033                 set_line_width(1);
1034                 set_opaque();
1035         }
1036 }
1037
1038 void ScopeWaveform::clear_point()
1039 {
1040         draw_point();
1041         drag_x = -1;
1042         drag_y = -1;
1043 }
1044
1045 ScopeVectorscope::ScopeVectorscope(ScopeGUI *gui,
1046                 int x, int y, int w, int h)
1047  : ScopePanel(gui, x, y, w, h)
1048 {
1049         drag_radius = 0;
1050         drag_angle = 0;
1051 }
1052
1053 void ScopeVectorscope::clear_point()
1054 {
1055 // Hide it
1056         draw_point();
1057         drag_radius = 0;
1058         drag_angle = 0;
1059 }
1060
1061 void ScopeVectorscope::update_point(int x, int y)
1062 {
1063 // Hide it
1064         draw_point();
1065         int dx = x - gui->vector_cx, dy = y - gui->vector_cy;
1066         drag_radius = sqrt(dx*dx + dy*dy);
1067         if( drag_radius > gui->radius ) drag_radius = gui->radius;
1068         float saturation = (float)drag_radius / gui->radius * FLOAT_MAX;
1069         drag_angle = atan2(-dy, dx);
1070         float hue = TO_DEG(drag_angle) - 90;
1071         if( hue < 0 ) hue += 360;
1072
1073         char string[BCTEXTLEN];
1074         sprintf(string, "Hue: %.3f Sat: %.3f", hue, saturation);
1075         show_tooltip(string);
1076
1077 // Show it
1078         draw_point();
1079         flash(1);
1080 }
1081
1082 void ScopeVectorscope::draw_point()
1083 {
1084         set_inverse();
1085         draw_point(drag_angle, drag_radius, RED);
1086         set_opaque();
1087 }
1088
1089 void ScopeVectorscope::draw_point(float th, float r, int color)
1090 {
1091         if( r > 0 ) {
1092                 set_color(color);
1093                 set_line_width(2);
1094                 int cx = gui->vector_cx, cy = gui->vector_cy;
1095                 draw_circle(cx-r, cy-r, r*2, r*2);
1096                 set_line_width(1);
1097                 float radius = gui->radius;
1098                 draw_radient(th, 0.f, drag_radius/radius, color);
1099         }
1100 }
1101
1102 void ScopeVectorscope::draw_radient(float th, float r1, float r2, int color)
1103 {
1104         if( r2 > 0 && r2 >= r1 ) {
1105                 set_color(color);
1106                 set_line_width(2);
1107                 int r = gui->radius;
1108                 double cth = r*cos(th), sth = -r*sin(th);
1109                 int cx = gui->vector_cx, cy = gui->vector_cy;
1110                 draw_line(cx+r1*cth, cy+r1*sth, cx+r2*cth, cy+r2*sth);
1111                 set_line_width(1);
1112         }
1113 }
1114
1115
1116 ScopeHistogram::ScopeHistogram(ScopeGUI *gui,
1117                 int x, int y, int w, int h)
1118  : ScopePanel(gui, x, y, w, h)
1119 {
1120         drag_x = -1;
1121 }
1122
1123 void ScopeHistogram::clear_point()
1124 {
1125 // Hide it
1126         draw_point();
1127         drag_x = -1;
1128 }
1129
1130 void ScopeHistogram::draw_point()
1131 {
1132         if( drag_x >= 0 ) {
1133                 set_inverse();
1134                 set_color(0xffffff);
1135                 set_line_width(2);
1136                 draw_line(drag_x, 0, drag_x, get_h());
1137                 set_line_width(1);
1138                 set_opaque();
1139         }
1140 }
1141
1142 void ScopeHistogram::update_point(int x, int y)
1143 {
1144         draw_point();
1145         drag_x = x;
1146         float value = (float)x / get_w() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1147
1148         char string[BCTEXTLEN];
1149         sprintf(string, "Value: %.3f", value);
1150         show_tooltip(string);
1151
1152         draw_point();
1153         flash(1);
1154 }
1155
1156
1157
1158 void ScopeHistogram::draw_mode(int mode, int color, int y, int h)
1159 {
1160 // Highest of all bins
1161         int normalize = 1, w = get_w();
1162         int *bin = gui->bins[mode], v;
1163         for( int i=0; i<TOTAL_BINS; ++i )
1164                 if( (v=bin[i]) > normalize ) normalize = v;
1165         double norm = normalize>1 ? log(normalize) : 1e-4;
1166         double vnorm = h / norm;
1167         set_color(color);
1168         for( int x=0; x<w; ++x ) {
1169                 int accum_start = (int)(x * TOTAL_BINS / w);
1170                 int accum_end = (int)((x + 1) * TOTAL_BINS / w);
1171                 CLAMP(accum_start, 0, TOTAL_BINS);
1172                 CLAMP(accum_end, 0, TOTAL_BINS);
1173                 int max = 0;
1174                 for(int i=accum_start; i<accum_end; ++i )
1175                         if( (v=bin[i]) > max ) max = v;
1176 //              max = max * h / normalize;
1177                 max = log(max) * vnorm;
1178                 draw_line(x,y+h - max, x,y+h);
1179         }
1180 }
1181 void ScopeHistogram::draw(int flash, int flush)
1182 {
1183         clear_box(0, 0, get_w(), get_h());
1184         if( gui->use_hist_parade ) {
1185                 draw_mode(0, 0xff0000, 0, get_h() / 3);
1186                 draw_mode(1, 0x00ff00, get_h() / 3, get_h() / 3);
1187                 draw_mode(2, 0x0000ff, get_h() / 3 * 2, get_h() / 3);
1188         }
1189         else {
1190                 draw_mode(3, LTGREY, 0, get_h());
1191         }
1192
1193         if(flash) this->flash(0);
1194         if(flush) this->flush();
1195 }
1196
1197
1198 ScopeScopesOn::ScopeScopesOn(ScopeMenu *scope_menu, const char *text, int id)
1199  : BC_MenuItem(text)
1200 {
1201         this->scope_menu = scope_menu;
1202         this->id = id;
1203 }
1204
1205 int ScopeScopesOn::handle_event()
1206 {
1207         int v = get_checked() ? 0 : 1;
1208         set_checked(v);
1209         ScopeGUI *gui = scope_menu->gui;
1210         switch( id ) {
1211         case SCOPE_HISTOGRAM:
1212                 gui->use_hist = v;
1213                 if( v ) gui->use_hist_parade = 0;
1214                 break;
1215         case SCOPE_HISTOGRAM_RGB:
1216                 gui->use_hist_parade = v;
1217                 if( v ) gui->use_hist = 0;
1218                 break;
1219         case SCOPE_WAVEFORM:
1220                 gui->use_wave = v;
1221                 if( v ) gui->use_wave_parade = 0;
1222                 break;
1223         case SCOPE_WAVEFORM_RGB:
1224                 gui->use_wave_parade = v;
1225                 if( v ) gui->use_wave = 0;
1226                 break;
1227         case SCOPE_WAVEFORM_PLY:
1228                 gui->use_wave_parade = -v;
1229                 if( v ) gui->use_wave = 0;
1230                 break;
1231         case SCOPE_VECTORSCOPE:
1232                 gui->use_vector = v;
1233                 break;
1234         case SCOPE_VECTORWHEEL:
1235                 gui->use_vector = -v;
1236                 break;
1237         }
1238         gui->toggle_event();
1239         gui->update_toggles();
1240         gui->create_panels();
1241         gui->update_scope();
1242         gui->show_window();
1243         return 1;
1244 }
1245
1246 ScopeMenu::ScopeMenu(ScopeGUI *gui, int x, int y)
1247  : BC_PopupMenu(x, y, xS(110), _("Scopes"))
1248 {
1249         this->gui = gui;
1250 }
1251
1252 void ScopeMenu::create_objects()
1253 {
1254         add_item(hist_on =
1255                 new ScopeScopesOn(this, _("Histogram"), SCOPE_HISTOGRAM));
1256         add_item(hist_rgb_on =
1257                 new ScopeScopesOn(this, _("Histogram RGB"), SCOPE_HISTOGRAM_RGB));
1258         add_item(new BC_MenuItem("-"));
1259         add_item(wave_on =
1260                 new ScopeScopesOn(this, _("Waveform"), SCOPE_WAVEFORM));
1261         add_item(wave_rgb_on =
1262                 new ScopeScopesOn(this, _("Waveform RGB"), SCOPE_WAVEFORM_RGB));
1263         add_item(wave_ply_on =
1264                 new ScopeScopesOn(this, _("Waveform ply"), SCOPE_WAVEFORM_PLY));
1265         add_item(new BC_MenuItem("-"));
1266         add_item(vect_on =
1267                 new ScopeScopesOn(this, _("Vectorscope"), SCOPE_VECTORSCOPE));
1268         add_item(vect_wheel =
1269                 new ScopeScopesOn(this, _("VectorWheel"), SCOPE_VECTORWHEEL));
1270 }
1271
1272 void ScopeMenu::update_toggles()
1273 {
1274         hist_on->set_checked(gui->use_hist);
1275         hist_rgb_on->set_checked(gui->use_hist_parade);
1276         wave_on->set_checked(gui->use_wave);
1277         wave_rgb_on->set_checked(gui->use_wave_parade>0);
1278         wave_ply_on->set_checked(gui->use_wave_parade<0);
1279         vect_on->set_checked(gui->use_vector>0);
1280         vect_wheel->set_checked(gui->use_vector<0);
1281 }
1282
1283
1284 ScopeSettingOn::ScopeSettingOn(ScopeSettings *settings, const char *text, int id)
1285  : BC_MenuItem(text)
1286 {
1287         this->settings = settings;
1288         this->id = id;
1289 }
1290
1291 int ScopeSettingOn::handle_event()
1292 {
1293         int v = get_checked() ? 0 : 1;
1294         set_checked(v);
1295         ScopeGUI *gui = settings->gui;
1296         switch( id ) {
1297         case SCOPE_SMOOTH:
1298                 gui->use_smooth = v;
1299                 break;
1300         case SCOPE_REFRESH:
1301                 gui->use_refresh = v;
1302                 gui->use_release = 0;
1303                 break;
1304         case SCOPE_RELEASE:
1305                 gui->use_release = v;
1306                 gui->use_refresh = 0;
1307         }
1308         gui->toggle_event();
1309         gui->update_toggles();
1310         gui->update_scope();
1311         gui->show_window();
1312         return 1;
1313 }
1314
1315 ScopeSettings::ScopeSettings(ScopeGUI *gui, int x, int y)
1316  : BC_PopupMenu(x, y, xS(150), _("Settings"))
1317 {
1318         this->gui = gui;
1319         refresh_on = 0;
1320         release_on = 0;
1321 }
1322
1323 void ScopeSettings::create_objects()
1324 {
1325         add_item(smooth_on =
1326                 new ScopeSettingOn(this, _("Smooth"), SCOPE_SMOOTH));
1327         smooth_on->set_checked(gui->use_smooth);
1328         if( gui->use_refresh >= 0 ) {
1329                 add_item(refresh_on =
1330                         new ScopeSettingOn(this, _("Refresh on Stop"), SCOPE_REFRESH));
1331                 add_item(release_on =
1332                         new ScopeSettingOn(this, _("Refresh on Release"), SCOPE_RELEASE));
1333                 refresh_on->set_checked(gui->use_refresh);
1334                 release_on->set_checked(gui->use_release);
1335         }
1336         add_item(new BC_MenuItem(_("-VectorWheel Grids-")));
1337
1338         gui->grat_paths.remove_all_objects();
1339         ScopeGratItem *item;
1340         add_item(item = new ScopeGratItem(this, _("None"), 0));
1341         if( item->idx == gui->grat_idx ) item->set_checked(1);
1342         gui->grat_paths.append(0);
1343         FileSystem fs;
1344         fs.set_filter("[*.png]");
1345         char scope_path[BCTEXTLEN];
1346         sprintf(scope_path, "%s%s", File::get_plugin_path(), SCOPE_SEARCHPATH);
1347         fs.update(scope_path);
1348         for( int i=0; i<fs.total_files(); ++i ) {
1349                 FileItem *file_item = fs.get_entry(i);
1350                 if( file_item->get_is_dir() ) continue;
1351                 strcpy(scope_path, file_item->get_name());
1352                 char *cp = strrchr(scope_path, '.');
1353                 if( cp ) *cp = 0;
1354                 add_item(item = new ScopeGratItem(this, scope_path, gui->grat_paths.size()));
1355                 if( item->idx == gui->grat_idx ) item->set_checked(1);
1356                 gui->grat_paths.append(cstrdup(file_item->get_path()));
1357         }
1358 }
1359
1360 void ScopeSettings::update_toggles()
1361 {
1362         if( refresh_on )
1363                 refresh_on->set_checked(gui->use_refresh);
1364         if( release_on )
1365                 release_on->set_checked(gui->use_release);
1366 }
1367
1368 ScopeGratItem::ScopeGratItem(ScopeSettings *settings, const char *text, int idx)
1369  : BC_MenuItem(text)
1370 {
1371         this->settings = settings;
1372         this->idx = idx;
1373 }
1374
1375 int ScopeGratItem::handle_event()
1376 {
1377         for( int i=0,n=settings->total_items(); i<n; ++i ) {
1378                 ScopeGratItem *item = (ScopeGratItem *)settings->get_item(i);
1379                 item->set_checked(item->idx == idx);
1380         }       
1381         settings->gui->update_graticule(idx);
1382         return 1;
1383 }
1384
1385
1386 ScopeGainReset::ScopeGainReset(ScopeGain *gain, int x, int y)
1387  : BC_Button(x, y, gain->gui->theme->get_image_set("reset_button"))
1388 {
1389         this->gain = gain;
1390 }
1391
1392 int ScopeGainReset::calculate_w(BC_Theme *theme)
1393 {
1394         VFrame *vfrm = *theme->get_image_set("reset_button");
1395         return vfrm->get_w();
1396 }
1397
1398 int ScopeGainReset::handle_event()
1399 {
1400         gain->slider->update(5);
1401         return gain->handle_event();
1402 }
1403
1404 ScopeGainSlider::ScopeGainSlider(ScopeGain *gain, int x, int y, int w)
1405  : BC_ISlider(x, y, 0, w, w, 1, 9, *gain->value)
1406 {
1407         this->gain = gain;
1408 }
1409
1410 int ScopeGainSlider::handle_event()
1411 {
1412         return gain->handle_event();
1413 }
1414
1415 ScopeGain::ScopeGain(ScopeGUI *gui, int x, int y, int w, int *value)
1416 {
1417         this->gui = gui;
1418         this->x = x;
1419         this->y = y;
1420         this->w = w;
1421         this->value = value;
1422
1423         slider = 0;
1424         reset = 0;
1425 }
1426 ScopeGain::~ScopeGain()
1427 {
1428         delete reset;
1429         delete slider;
1430 }
1431
1432 int ScopeGain::calculate_h()
1433 {
1434         return BC_ISlider::get_span(0);
1435 }
1436
1437 void ScopeGain::create_objects()
1438 {
1439         reset_w = ScopeGainReset::calculate_w(gui->theme);
1440         gui->add_subwindow(slider = new ScopeGainSlider(this, x, y, w-reset_w-xS(5)));
1441         gui->add_subwindow(reset = new ScopeGainReset(this, x+w-reset_w, y));
1442 }
1443
1444 int ScopeGain::handle_event()
1445 {
1446         *value = slider->get_value();
1447         gui->update_scope();
1448         gui->toggle_event();
1449         return 1;
1450 }
1451
1452 void ScopeGain::reposition_window(int x, int y)
1453 {
1454         this->x = x;  this->y = y;
1455         slider->reposition_window(x, y);
1456         reset->reposition_window(x+w-reset_w, y);
1457 }
1458
1459 ScopeWaveSlider::ScopeWaveSlider(ScopeGUI *gui, int x, int y, int w)
1460  : ScopeGain(gui, x, y, w, &gui->use_wave_gain)
1461 {
1462 }
1463
1464 ScopeVectSlider::ScopeVectSlider(ScopeGUI *gui, int x, int y, int w)
1465  : ScopeGain(gui, x, y, w, &gui->use_vect_gain)
1466 {
1467 }
1468