tweak zoom/fullscr to remember cwdw scale after fullscr
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcmeter.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcbutton.h"
23 #include "bcmeter.h"
24 #include "bcpixmap.h"
25 #include "bcresources.h"
26 #include "bcwindow.h"
27 #include "bccolors.h"
28 #include "fonts.h"
29 #include "vframe.h"
30 #include <string.h>
31
32 // Which source image to replicate
33 #define METER_NORMAL 0
34 #define METER_GREEN 1
35 #define METER_RED 2
36 #define METER_YELLOW 3
37 #define METER_WHITE 4
38 #define METER_OVER 5
39 #define METER_DOWNMIX 6
40
41 // Region of source image to replicate
42 #define METER_LEFT 0
43 #define METER_MID 1
44 #define METER_RIGHT 3
45
46
47 BC_Meter::BC_Meter(int x,
48         int y,
49         int orientation,
50         int pixels,
51         int min,
52         int max,
53         int mode,
54         int use_titles,
55         int span,
56         int downmix)
57  : BC_SubWindow(x, y, -1, -1)
58 {
59         this->over_delay = 150;
60         this->peak_delay = 15;
61         this->use_titles = use_titles;
62         this->min = min;
63         this->max = max;
64         this->mode = mode;
65         this->orientation = orientation;
66         this->pixels = pixels;
67         this->span = span;
68         this->downmix = downmix;
69
70 //printf("BC_Meter::draw_face %d w=%d pixels=%d\n", __LINE__, w, pixels);
71         for(int i = 0; i < TOTAL_METER_IMAGES; i++) images[i] = 0;
72         db_titles.set_array_delete();
73 }
74
75 BC_Meter::~BC_Meter()
76 {
77         db_titles.remove_all_objects();
78         title_pixels.remove_all();
79         tick_pixels.remove_all();
80         for(int i = 0; i < TOTAL_METER_IMAGES; i++) delete images[i];
81 }
82
83 int BC_Meter::get_title_w()
84 {
85         return get_resources()->meter_title_w;
86 }
87
88 int BC_Meter::get_meter_w()
89 {
90         return get_resources()->ymeter_images[0]->get_w();
91 }
92
93
94 int BC_Meter::set_delays(int over_delay, int peak_delay)
95 {
96         this->over_delay = over_delay;
97         this->peak_delay = peak_delay;
98         return 0;
99 }
100
101 int BC_Meter::initialize()
102 {
103         peak_timer = 0;
104         level_pixel = peak_pixel = 0;
105         over_timer = 0;
106         over_count = 0;
107         peak = level = -100;
108
109         if(orientation == METER_VERT)
110         {
111                 set_images(get_resources()->ymeter_images);
112                 h = pixels;
113                 if(span < 0)
114                 {
115                         w = images[0]->get_w();
116                         if(use_titles) w += get_title_w();
117                 }
118                 else
119                         w = span;
120         }
121         else
122         {
123                 set_images(get_resources()->xmeter_images);
124                 h = images[0]->get_h();
125                 w = pixels;
126                 if(use_titles) h += get_title_w();
127         }
128
129 // calibrate the db titles
130         get_divisions();
131
132         BC_SubWindow::initialize();
133         draw_titles(0);
134         draw_face(0);
135         show_window(0);
136         return 0;
137 }
138
139 void BC_Meter::set_images(VFrame **data)
140 {
141         for(int i = 0; i < TOTAL_METER_IMAGES; i++) delete images[i];
142         for(int i = 0; i < TOTAL_METER_IMAGES; i++)
143                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
144 }
145
146 int BC_Meter::reposition_window(int x, int y, int span, int pixels)
147 {
148         if(pixels < 0) pixels = this->pixels;
149         this->span = span;
150         this->pixels = pixels;
151         if(orientation == METER_VERT)
152                 BC_SubWindow::reposition_window(x,
153                         y,
154                         this->span < 0 ? w : span,
155                         pixels);
156         else
157                 BC_SubWindow::reposition_window(x, y, pixels, get_h());
158
159 //printf("BC_Meter::reposition_window 1 %d %d %d %d\n", x, y, w, h);
160         get_divisions();
161
162 //set_color(WHITE);
163 //draw_box(0, 0, w, h);
164 //flash();
165 //return 0;
166         draw_titles(0);
167         draw_face(0);
168         return 0;
169 }
170
171 int BC_Meter::reset(int dmix)
172 {
173         level = min;
174         peak = min;
175         level_pixel = peak_pixel = 0;
176         peak_timer = 0;
177         over_timer = 0;
178         over_count = 0;
179         if(dmix >= 0) downmix = dmix;
180         draw_face(1);
181         return 0;
182 }
183
184 int BC_Meter::button_press_event()
185 {
186         if(cursor_inside() && is_event_win())
187         {
188                 reset_over();
189                 return 1;
190         }
191         return 0;
192 }
193
194
195 int BC_Meter::reset_over()
196 {
197         over_timer = 0;
198         return 0;
199 }
200
201 int BC_Meter::change_format(int mode, int min, int max)
202 {
203         this->mode = mode;
204         this->min = min;
205         this->max = max;
206         reposition_window(get_x(), get_y(), w, pixels);
207         return 0;
208 }
209
210 int BC_Meter::level_to_pixel(float level)
211 {
212         int result;
213         if(mode == METER_DB)
214         {
215                 result = (int)(pixels *
216                         (level - min) /
217                         (max - min));
218                 if(level <= min) result = 0;
219         }
220         else
221         {
222 // Not implemented anymore
223                 result = 0;
224         }
225
226         return result;
227 }
228
229
230 void BC_Meter::get_divisions()
231 {
232         char string[BCTEXTLEN];
233         char *new_string;
234
235
236         db_titles.remove_all_objects();
237         title_pixels.remove_all();
238         tick_pixels.remove_all();
239
240         low_division = 0;
241         medium_division = 0;
242         high_division = pixels;
243
244         int current_pixel;
245 // Create tick marks and titles in one pass
246         for(int current = min; current <= max; current++)
247         {
248                 if(orientation == METER_VERT)
249                 {
250 // Create tick mark
251                         current_pixel = (pixels - METER_MARGIN * 2 - 2) *
252                                 (current - min) / (max - min) + 2;
253                         tick_pixels.append(current_pixel);
254
255 // Create titles in selected positions
256                         if(current == min ||
257                                 current == max ||
258                                 current == 0 ||
259                                 (current - min > 4 && max - current > 4 && !(current % 5)))
260                         {
261                                 int title_pixel = (pixels -
262                                         METER_MARGIN * 2) * (current - min) / (max - min);
263                                 sprintf(string, "%ld", labs(current));
264                                 new_string = new char[strlen(string) + 1];
265                                 strcpy(new_string, string);
266                                 db_titles.append(new_string);
267                                 title_pixels.append(title_pixel);
268                         }
269                 }
270                 else
271                 {
272                         current_pixel = (pixels - METER_MARGIN * 2) *
273                                 (current - min) /
274                                 (max - min);
275                         tick_pixels.append(current_pixel);
276 // Titles not supported for horizontal
277                 }
278
279 // Create color divisions
280                 if(current == -20)
281                 {
282                         low_division = current_pixel;
283                 }
284                 else
285                 if(current == -5)
286                 {
287                         medium_division = current_pixel;
288                 }
289                 else
290                 if(current == 0)
291                 {
292                         high_division = current_pixel;
293                 }
294         }
295 // if(orientation == METER_VERT)
296 // printf("BC_Meter::get_divisions %d %d %d %d\n",
297 // low_division, medium_division, high_division, pixels);
298 }
299
300 void BC_Meter::draw_titles(int flush)
301 {
302         if(!use_titles) return;
303
304         set_font(get_resources()->meter_font);
305
306         if(orientation == METER_HORIZ)
307         {
308                 draw_top_background(parent_window, 0, 0, get_w(), get_title_w());
309
310                 for(int i = 0; i < db_titles.total; i++)
311                 {
312                         draw_text(0, title_pixels.values[i], db_titles.values[i]);
313                 }
314
315                 flash(0, 0, get_w(), get_title_w(), flush);
316         }
317         else
318         if(orientation == METER_VERT)
319         {
320                 draw_top_background(parent_window, 0, 0, get_title_w(), get_h());
321
322 // Titles
323                 for(int i = 0; i < db_titles.total; i++)
324                 {
325                         int title_y = pixels -
326                                 title_pixels.values[i];
327                         if(i == 0)
328                                 title_y -= get_text_descent(SMALLFONT_3D);
329                         else
330                         if(i == db_titles.total - 1)
331                                 title_y += get_text_ascent(SMALLFONT_3D);
332                         else
333                                 title_y += get_text_ascent(SMALLFONT_3D) / 2;
334
335                         set_color(get_resources()->meter_font_color);
336                         draw_text(0,
337                                 title_y,
338                                 db_titles.values[i]);
339                 }
340
341                 for(int i = 0; i < tick_pixels.total; i++)
342                 {
343 // Tick marks
344                         int tick_y = pixels - tick_pixels.values[i] - METER_MARGIN;
345                         set_color(get_resources()->meter_font_color);
346                         draw_line(get_title_w() - xS(10) - 1, tick_y, get_title_w() - 1, tick_y);
347                         if(get_resources()->meter_3d)
348                         {
349                                 set_color(BLACK);
350                                 draw_line(get_title_w() - xS(10), tick_y + 1, get_title_w(), tick_y + 1);
351                         }
352                 }
353
354                 flash(0, 0, get_title_w(), get_h(), flush);
355         }
356 }
357
358 int BC_Meter::region_pixel(int region)
359 {
360         VFrame **reference_images = get_resources()->xmeter_images;
361         int result;
362
363         if(region == METER_RIGHT)
364                 result = region * reference_images[0]->get_w() / 4;
365         else
366                 result = region * reference_images[0]->get_w() / 4;
367
368         return result;
369 }
370
371 int BC_Meter::region_pixels(int region)
372 {
373         int x1;
374         int x2;
375         int result;
376         VFrame **reference_images = get_resources()->xmeter_images;
377
378         x1 = region * reference_images[0]->get_w() / 4;
379         x2 = (region + 1) * reference_images[0]->get_w() / 4;
380         if(region == METER_MID)
381                 result = (x2 - x1) * 2;
382         else
383                 result = x2 - x1;
384         return result;
385 }
386
387 void BC_Meter::draw_face(int flush)
388 {
389         int level_pixel = level_to_pixel(level);
390         int peak_pixel2 = level_to_pixel(peak);
391         int peak_pixel1 = peak_pixel2 - 2;
392         int left_pixel = region_pixel(METER_MID);
393         int right_pixel = pixels - region_pixels(METER_RIGHT);
394         int pixel = 0;
395         int image_number = 0, region = 0;
396         int in_span, in_start;
397         int x = use_titles ? get_title_w() : 0;
398         int w = use_titles ? (this->w - get_title_w()) : this->w;
399
400         draw_top_background(parent_window, x, 0, w, h);
401
402 // printf("BC_Meter::draw_face %d span=%d this->w=%d get_title_w()=%d %d %d\n",
403 // __LINE__,
404 // span,
405 // this->w,
406 // get_title_w(),
407 // w,
408 // h);
409
410         while(pixel < pixels)
411         {
412 // Select image to draw
413                 if(pixel < level_pixel ||
414                         (pixel >= peak_pixel1 && pixel < peak_pixel2))
415                 {
416                         if(pixel < low_division)
417                                 image_number = METER_GREEN;
418                         else
419                         if(pixel < medium_division)
420                                 image_number = METER_YELLOW;
421                         else
422                         if(pixel < high_division)
423                                 image_number = METER_RED;
424                         else
425                                 image_number = METER_WHITE;
426                 }
427                 else
428                 {
429                         image_number = METER_NORMAL;
430                 }
431
432 // Select region of image to duplicate
433                 if(pixel < left_pixel)
434                 {
435                         region = METER_LEFT;
436                         in_start = pixel + region_pixel(region);
437                         in_span = region_pixels(region) - (in_start - region_pixel(region));
438                 }
439                 else
440                 if(pixel < right_pixel)
441                 {
442                         region = METER_MID;
443                         in_start = region_pixel(region);
444                         in_span = region_pixels(region);
445                 }
446                 else
447                 {
448                         region = METER_RIGHT;
449                         in_start = (pixel - right_pixel) + region_pixel(region);
450                         in_span = region_pixels(region) - (in_start - region_pixel(region));;
451                 }
452
453 //printf("BC_Meter::draw_face region %d pixel %d pixels %d in_start %d in_span %d\n", region, pixel, pixels, in_start, in_span);
454                 if(in_span > 0)
455                 {
456 // Clip length to peaks
457                         if(pixel < level_pixel && pixel + in_span > level_pixel)
458                                 in_span = level_pixel - pixel;
459                         else
460                         if(pixel < peak_pixel1 && pixel + in_span > peak_pixel1)
461                                 in_span = peak_pixel1 - pixel;
462                         else
463                         if(pixel < peak_pixel2 && pixel + in_span > peak_pixel2)
464                                 in_span = peak_pixel2 - pixel;
465
466 // Clip length to color changes
467                         if(image_number == METER_GREEN && pixel + in_span > low_division)
468                                 in_span = low_division - pixel;
469                         else
470                         if(image_number == METER_YELLOW && pixel + in_span > medium_division)
471                                 in_span = medium_division - pixel;
472                         else
473                         if(image_number == METER_RED && pixel + in_span > high_division)
474                                 in_span = high_division - pixel;
475
476 // Clip length to regions
477                         if(pixel < left_pixel && pixel + in_span > left_pixel)
478                                 in_span = left_pixel - pixel;
479                         else
480                         if(pixel < right_pixel && pixel + in_span > right_pixel)
481                                 in_span = right_pixel - pixel;
482
483 //printf("BC_Meter::draw_face image_number %d pixel %d pixels %d in_start %d in_span %d\n", image_number, pixel, pixels, in_start, in_span);
484 //printf("BC_Meter::draw_face %d %d %d %d\n", orientation, region, images[image_number]->get_h() - in_start - in_span);
485                         if(orientation == METER_HORIZ)
486                         {
487                                 draw_pixmap(images[image_number],
488                                         pixel,
489                                         x,
490                                         in_span + 1,
491                                         get_h(),
492                                         in_start,
493                                         0);
494                         }
495                         else
496                         {
497 //printf("BC_Meter::draw_face %d %d\n", __LINE__, span);
498                                 if(span < 0)
499                                 {
500                                         draw_pixmap(images[image_number],
501                                                 x,
502                                                 get_h() - pixel - in_span,
503                                                 get_w(),
504                                                 in_span + 1,
505                                                 0,
506                                                 images[image_number]->get_h() - in_start - in_span);
507                                 }
508                                 else
509                                 {
510                                         int total_w = get_w() - x;
511                                         int third = images[image_number]->get_w() / 3 + 1;
512
513
514                                         for(int x1 = 0; x1 < total_w; x1 += third)
515                                         {
516                                                 int in_x = 0;
517                                                 int in_w = third;
518                                                 if(x1 >= third) in_x = third;
519                                                 if(x1 >= total_w - third)
520                                                 {
521                                                         in_x = images[image_number]->get_w() -
522                                                                 (total_w - x1);
523                                                         in_w = total_w - x1;
524                                                 }
525
526                                                 int in_y = images[image_number]->get_h() - in_start - in_span;
527 //printf("BC_Meter::draw_face %d %d %d\n", __LINE__, get_w(), x + x1 + in_w, in_x, in_y, in_w, span);
528
529
530                                                 draw_pixmap(images[image_number],
531                                                         x + x1, get_h() - pixel - in_span,
532                                                         in_w, in_span + 1, in_x, in_y);
533                                         }
534                                 }
535                         }
536
537                         pixel += in_span;
538                 }
539                 else
540                 {
541 // Sanity check
542                         break;
543                 }
544         }
545
546         if(downmix) {
547                 if(orientation == METER_HORIZ)
548                         draw_pixmap(images[METER_DOWNMIX], 0, 0);
549                 else
550                         draw_pixmap(images[METER_DOWNMIX], x,
551                                 get_h() - images[METER_DOWNMIX]->get_h()-1);
552         }
553
554         if(over_timer)
555         {
556                 if(orientation == METER_HORIZ)
557                         draw_pixmap(images[METER_OVER], xS(20), yS(2));
558                 else
559                         draw_pixmap(images[METER_OVER], x, get_h() - yS(100));
560
561                 over_timer--;
562         }
563
564         if(orientation == METER_HORIZ)
565                 flash(0, 0, pixels, get_h(), flush);
566         else
567                 flash(x, 0, w, pixels, flush);
568 }
569
570 int BC_Meter::update(float new_value, int over, int dmix)
571 {
572         peak_timer++;
573
574         if(mode == METER_DB)
575         {
576                 if(new_value == 0)
577                         level = min;
578                 else
579                         level = db.todb(new_value);        // db value
580         }
581
582         if(level > peak || peak_timer > peak_delay)
583         {
584                 peak = level;
585                 peak_timer = 0;
586         }
587
588 // if(orientation == METER_HORIZ)
589 // printf("BC_Meter::update %f\n", level);
590         if(over) over_timer = over_delay;
591 // only draw if window is visible
592         if(dmix >= 0) downmix = dmix;
593
594         draw_face(1);
595         return 0;
596 }