add BC_SCALE env var for hi def monitors, cleanup theme data
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / histogram_bezier / bistogramwindow.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "bcsignals.h"
24 #include "bistogram.h"
25 #include "bistogramconfig.h"
26 #include "bistogramwindow.h"
27 #include "keys.h"
28 #include "language.h"
29
30
31 #include <unistd.h>
32 #include <string.h>
33
34 HistogramWindow::HistogramWindow(HistogramMain *plugin)
35  : PluginClientWindow(plugin, xS(440), yS(480), xS(440), yS(480), 0)
36 {
37         this->plugin = plugin;
38         max_picon = 0;
39         mid_picon = 0;
40         min_picon = 0;
41 }
42
43 HistogramWindow::~HistogramWindow()
44 {
45         delete max_picon;
46         delete mid_picon;
47         delete min_picon;
48 }
49
50 #include "min_picon_png.h"
51 #include "mid_picon_png.h"
52 #include "max_picon_png.h"
53 static VFramePng max_picon_image(max_picon_png);
54 static VFramePng mid_picon_image(mid_picon_png);
55 static VFramePng min_picon_image(min_picon_png);
56
57 void HistogramWindow::create_objects()
58 {
59         int xs10 = xS(10), xs15 = xS(15), xs20 = xS(20), xs80 = xS(80), xs100 = xS(100), xs120 = xS(120), xs150 = xS(150);
60         int ys6 = yS(6), ys10 =  yS(10), ys20 = yS(20), ys30 = yS(30), ys40 = yS(40), ys170 = yS(170);
61         int x = xs10, y = ys10, x1 = xs10;
62         BC_Title *title = 0;
63
64         max_picon = create_pixmap(&max_picon_image);
65         mid_picon = create_pixmap(&mid_picon_image);
66         min_picon = create_pixmap(&min_picon_image);
67         add_subwindow(mode_v = new HistogramMode(plugin, x, y,
68                 HISTOGRAM_VALUE, _("Value")));
69         x += xs80;
70         add_subwindow(mode_r = new HistogramMode(plugin, x, y,
71                 HISTOGRAM_RED, _("Red")));
72         x += xs80;
73         add_subwindow(mode_g = new HistogramMode(plugin, x, y,
74                 HISTOGRAM_GREEN, _("Green")));
75         x += xs80;
76         add_subwindow(mode_b = new HistogramMode(plugin, x, y,
77                 HISTOGRAM_BLUE, _("Blue")));
78 //      x += xs80;
79 //      add_subwindow(mode_a = new HistogramMode(plugin, x, y,
80 //              HISTOGRAM_ALPHA, _("Alpha")));
81
82         x = get_w() - HistogramClear::calculate_w(this, _("Clear")) - xs15;
83         add_subwindow(clear = new HistogramClear(plugin, x, y, _("Clear")));
84
85         x = x1;
86         y += ys30;
87         add_subwindow(title = new BC_Title(x, y, _("Input X:")));
88         x += title->get_w() + xs10;
89         input_x = new HistogramInputText(plugin, this, x, y, 1);
90         input_x->create_objects();
91
92         x += input_x->get_w() + xs10;
93         add_subwindow(title = new BC_Title(x, y, _("Input Y:")));
94         x += title->get_w() + xs10;
95         input_y = new HistogramInputText(plugin, this, x, y, 0);
96         input_y->create_objects();
97
98         y += ys30;
99         x = x1;
100
101         canvas_w = get_w() - x - x;
102         canvas_h = get_h() - y - ys170;
103         title1_x = x;
104         title2_x = x + (int)(canvas_w * -HIST_MIN_INPUT / FLOAT_RANGE);
105         title3_x = x + (int)(canvas_w * (1.0 - HIST_MIN_INPUT) / FLOAT_RANGE);
106         title4_x = x + (int)(canvas_w);
107         add_subwindow(canvas = new HistogramCanvas(plugin, this,
108                         x, y, canvas_w, canvas_h));
109         draw_canvas_overlay();
110         canvas->flash();
111
112         y += canvas->get_h() + 1;
113         add_subwindow(new BC_Title(title1_x, y, "-10%"));
114         add_subwindow(new BC_Title(title2_x, y, "0%"));
115         add_subwindow(new BC_Title(title3_x - get_text_width(MEDIUMFONT, "100"), y, "100%"));
116         add_subwindow(new BC_Title(title4_x - get_text_width(MEDIUMFONT, "110"), y, "110%"));
117
118         y += ys20;
119         add_subwindow(title = new BC_Title(x, y, _("Output min:")));
120         x += title->get_w() + xs10;
121         output_min = new HistogramOutputText(plugin, this,
122                 x, y, &plugin->config.output_min[plugin->mode]);
123         output_min->create_objects();
124         x += output_min->get_w() + xs10;
125         add_subwindow(new BC_Title(x, y, _("Output Max:")));
126         x += title->get_w() + xs10;
127         output_max = new HistogramOutputText(plugin, this,
128                 x, y, &plugin->config.output_max[plugin->mode]);
129         output_max->create_objects();
130
131         x = x1;
132         y += ys30;
133
134         add_subwindow(output = new HistogramSlider(plugin, this,
135                 x, y, get_w() - xs20, ys30, 0));
136         output->update();
137         y += ys40;
138
139
140         add_subwindow(automatic = new HistogramAuto(plugin, x, y));
141
142         x += xs120;
143         add_subwindow(new HistogramReset(plugin, x, y));
144         x += xs100;
145         add_subwindow(new BC_Title(x, y, _("Threshold:")));
146         x += xs100;
147         threshold = new HistogramOutputText(plugin, this,
148                 x, y, &plugin->config.threshold);
149         threshold->create_objects();
150         x = x1;
151         y += ys40;
152         add_subwindow(split = new HistogramSplit(plugin, x, y));
153         y += ys6;
154         x += xs150;
155         add_subwindow(new BC_Title(x,y, _("Interpolation:")));
156         x += xs120;
157         add_subwindow(smoothModeChoser = new HistogramSmoothMode(plugin, this, x, y));
158         smoothModeChoser->create_objects();
159
160         show_window();
161 }
162
163 int HistogramWindow::delete_current_point()
164 {
165         if( plugin->current_point < 0 ) return 0;
166         HistogramPoint *current =
167                 plugin->config.points[plugin->mode].get_item_number(plugin->current_point);
168         delete current;
169         plugin->current_point = -1;
170         return 1;
171 }
172
173 int HistogramWindow::keypress_event()
174 {
175         int result = 0;
176         if( get_keypress() == BACKSPACE ||
177             get_keypress() == DELETE ) {
178                 if( delete_current_point() ) {
179                         update_input();
180                         update_canvas();
181                         plugin->send_configure_change();
182                         result = 1;
183                 }
184         }
185         return result;
186 }
187
188 void HistogramWindow::update(int do_input)
189 {
190         automatic->update(plugin->config.automatic);
191         threshold->update(plugin->config.threshold);
192         update_mode();
193         split->set_value(plugin->config.split);
194         if( do_input ) update_input();
195         update_output();
196         if( smoothModeChoser->set_mode(plugin->config.smoothMode) )
197                 update_canvas();
198 }
199
200 void HistogramWindow::update_input()
201 {
202         input_x->update();
203         input_y->update();
204 }
205
206 void HistogramWindow::update_output()
207 {
208         output->update();
209         output_min->update(plugin->config.output_min[plugin->mode]);
210         output_max->update(plugin->config.output_max[plugin->mode]);
211 }
212
213 void HistogramWindow::update_mode()
214 {
215         mode_v->update(plugin->mode == HISTOGRAM_VALUE ? 1 : 0);
216         mode_r->update(plugin->mode == HISTOGRAM_RED ? 1 : 0);
217         mode_g->update(plugin->mode == HISTOGRAM_GREEN ? 1 : 0);
218         mode_b->update(plugin->mode == HISTOGRAM_BLUE ? 1 : 0);
219         output_min->output = &plugin->config.output_min[plugin->mode];
220         output_max->output = &plugin->config.output_max[plugin->mode];
221 }
222
223 void HistogramWindow::draw_canvas_overlay()
224 {
225         int y1;
226
227 // Calculate output curve
228         plugin->tabulate_curve(plugin->mode, 0);
229
230 // Draw output line
231         canvas->set_color(0xffff00);
232         for( int i=0; i<canvas_w; ++i ) {
233                 float input = (float)i / canvas_w * FLOAT_RANGE + HIST_MIN_INPUT;
234                 float output = plugin->calculate_smooth(input, plugin->mode);
235
236                 int y2 = canvas_h - (int)(output * canvas_h);
237                 if( i > 0 ) {
238                         canvas->draw_line(i - 1, y1, i, y2);
239                 }
240                 y1 = y2;
241         }
242
243         int x0 = 0, y0 = 0;
244 // Draw output points
245         HistogramPoint *current = plugin->config.points[plugin->mode].first;
246         int number = 0;
247         while( current ) {
248                 canvas->set_color(0x00ff00);
249                 int x = (int)((current->x - HIST_MIN_INPUT) * canvas_w / FLOAT_RANGE);
250                 int y = (int)(canvas_h - current->y * canvas_h);
251                 if(number == plugin->current_point)
252                         canvas->draw_box(x - BOX_SIZE / 2, y - BOX_SIZE / 2, BOX_SIZE, BOX_SIZE);
253                 else
254                         canvas->draw_rectangle(x - BOX_SIZE / 2, y - BOX_SIZE / 2, BOX_SIZE, BOX_SIZE);
255
256                 if( number > 0 )
257                         canvas->draw_line(x0, y0, x, y);
258                 x0 = x;  y0 = y;
259 //Draw gradients
260                 if (plugin->config.smoothMode > HISTOGRAM_LINEAR) {
261                         int x1,x2,y1,y2;
262                         canvas->set_color(0xffffff);
263                         x2 = (int)((current->x + current->xoffset_right - HIST_MIN_INPUT) * canvas_w / FLOAT_RANGE);
264                         x1 = (int)((current->x + current->xoffset_left - HIST_MIN_INPUT) * canvas_w / FLOAT_RANGE);
265                         y2 = (int)(canvas_h - (current->y + current->xoffset_right * current->gradient) * canvas_h);
266                         y1 = (int)(canvas_h - (current->y + current->xoffset_left * current->gradient) * canvas_h);
267 /*                      x2 = x + (title3_x - title2_x)/20;
268                         x1 = x - (title3_x - title2_x)/20;
269                         y1 = y + (int)(current->gradient * (float)(canvas_h)/20.0);
270                         y2 = y - (int)(current->gradient * (float)(canvas_h)/20.0);
271                         int y2 = (int)(canvas_h - canvas_h * (current->y + current->gradient /10));*/
272                         canvas->draw_line(x1,y1,x2,y2);
273
274                         canvas->draw_circle(x1 - BOX_SIZE / 4, y1 - BOX_SIZE / 4, BOX_SIZE/2, BOX_SIZE/2);
275                         canvas->draw_circle(x2 - BOX_SIZE / 4, y2 - BOX_SIZE / 4, BOX_SIZE/2, BOX_SIZE/2);
276                 }
277
278                 current = NEXT;
279                 number++;
280         }
281
282
283 // Draw 0 and 100% lines.
284         canvas->set_color(0xff0000);
285         canvas->draw_line(title2_x - canvas->get_x(), 0,
286                 title2_x - canvas->get_x(), canvas_h);
287         canvas->draw_line(title3_x - canvas->get_x(), 0,
288                 title3_x - canvas->get_x(), canvas_h);
289 }
290
291 void HistogramWindow::update_canvas()
292 {
293         int bins = plugin->slots * (HISTOGRAM_MAX-HISTOGRAM_MIN)/100;
294         int *accum = plugin->accum[plugin->mode];
295         int normalize = 0;
296         int max = 0;
297
298         if( accum ) {
299                 for( int i=0; i<bins; ++i )
300                         if( accum[i] > normalize )
301                                 normalize = accum[i];
302         }
303
304
305         if( normalize ) {
306                 int accum_start = 0;
307                 for( int i=0; i<canvas_w; ++i ) {
308                         int accum_end = bins*i / canvas_w;
309                         max = 0;
310                         for( int j=accum_start; j<accum_end; ++j ) {
311                                 max = MAX(accum[j], max);
312                         }
313
314 //                      max = max * canvas_h / normalize;
315                         max = (int)(log(max) / log(normalize) * canvas_h);
316
317                         canvas->set_color(0x333333);
318                         canvas->draw_line(i, 0, i, canvas_h - max);
319                         canvas->set_color(0x848484);
320                         canvas->draw_line(i, canvas_h - max, i, canvas_h);
321                         accum_start = accum_end;
322                 }
323         }
324         else {
325                 canvas->set_color(0x333333);
326                 canvas->draw_box(0, 0, canvas_w, canvas_h);
327         }
328
329
330         draw_canvas_overlay();
331         canvas->flash();
332 }
333
334
335 HistogramCanvas::HistogramCanvas(HistogramMain *plugin, HistogramWindow *gui,
336         int x, int y, int w, int h)
337  : BC_SubWindow(x, y, w, h, 0x333333)
338 {
339         this->plugin = plugin;
340         this->gui = gui;
341 }
342
343 int HistogramCanvas::button_press_event()
344 {
345         int result = 0;
346         if( is_event_win() && cursor_inside() ) {
347                 if( !plugin->dragging_point ) {
348                         HistogramPoint *new_point = 0;
349                         gui->deactivate();
350 // Search for existing point under cursor
351                         HistogramPoint *current = plugin->config.points[plugin->mode].first;
352                         plugin->current_point = -1;
353                         int dragID = 0;
354                         while( current ) {
355                                 int x = (int)((current->x - HIST_MIN_INPUT) * gui->canvas_w / FLOAT_RANGE);
356                                 int y = (int)(gui->canvas_h - current->y * gui->canvas_h);
357
358 /* Check for click on main point */
359                                 if( get_cursor_x() >= x - BOX_SIZE / 2 &&
360                                     get_cursor_y() >= y - BOX_SIZE / 2 &&
361                                     get_cursor_x() < x + BOX_SIZE / 2 &&
362                                     get_cursor_y() < y + BOX_SIZE / 2 ) {
363                                         plugin->current_point =
364                                                 plugin->config.points[plugin->mode].number_of(current);
365                                         plugin->point_x_offset = get_cursor_x() - x;
366                                         plugin->point_y_offset = get_cursor_y() - y;
367                                         dragID = 1;
368                                         break;
369                                 }
370 //                              if (plugin->config.smoothMode == HISTOGRAM_LINEAR)
371 //                                break;
372
373                                 int xright =
374                                   (int)((current->x + current->xoffset_right - HIST_MIN_INPUT) * gui->canvas_w / FLOAT_RANGE);
375                                 int yright =
376                                   (int)(gui->canvas_h - (current->y + current->xoffset_right * current->gradient) *
377                                   gui->canvas_h);
378
379 /* Check for click on right handle */
380                                 if( get_cursor_x() >= xright - BOX_SIZE / 2 &&
381                                     get_cursor_y() >= yright - BOX_SIZE / 2 &&
382                                     get_cursor_x() <  xright + BOX_SIZE / 2 &&
383                                     get_cursor_y() <  yright + BOX_SIZE / 2 ) {
384                                         plugin->current_point =
385                                                 plugin->config.points[plugin->mode].number_of(current);
386                                         plugin->point_x_offset = get_cursor_x() - xright;
387                                         plugin->point_y_offset = get_cursor_y() - yright;
388                                         dragID = 2;
389                                         break;
390                                 }
391
392 /* Check for click on left handle */
393                                 int xleft =
394                                   (int)((current->x + current->xoffset_left - HIST_MIN_INPUT) * gui->canvas_w / FLOAT_RANGE);
395                                 int yleft =
396                                   (int)(gui->canvas_h - (current->y + current->xoffset_left * current->gradient) *
397                                   gui->canvas_h);
398                                 if( get_cursor_x() >= xleft - BOX_SIZE / 2 &&
399                                     get_cursor_y() >= yleft - BOX_SIZE / 2 &&
400                                     get_cursor_x() <  xleft + BOX_SIZE / 2 &&
401                                     get_cursor_y() <  yleft + BOX_SIZE / 2 ) {
402                                         plugin->current_point =
403                                                 plugin->config.points[plugin->mode].number_of(current);
404                                         plugin->point_x_offset = get_cursor_x() - xleft;
405                                         plugin->point_y_offset = get_cursor_y() - yleft;
406                                         dragID = 3;
407                                         break;
408                                 }
409
410                                 current = NEXT;
411                         }
412
413                         if( plugin->current_point < 0 ) {
414 // Create new point under cursor
415                                 float current_x = (float)get_cursor_x() * FLOAT_RANGE / get_w() + HIST_MIN_INPUT;
416                                 float current_y = 1.0 - (float)get_cursor_y() / get_h();
417                                 new_point =
418                                         plugin->config.points[plugin->mode].insert(current_x, current_y);
419                                 plugin->current_point =
420                                         plugin->config.points[plugin->mode].number_of(new_point);
421                                 plugin->point_x_offset = 0;
422                                 plugin->point_y_offset = 0;
423
424 // Default gradient
425 // Get 2 points surrounding current position
426                                 float x1 = 0, x2 = 1, y1 = 0, y2 = 1;
427
428                                 HistogramPoint *current = plugin->config.points[plugin->mode].first;
429                                 int done = 0;
430                                 while( current && !done ) {
431                                         if( current->x > current_x ) {
432                                                 x2 = current->x;
433                                                 y2 = current->y;
434                                                 done = 1;
435                                         }
436                                         else
437                                                 current = NEXT;
438                                 }
439
440                                 current = plugin->config.points[plugin->mode].last;
441                                 done = 0;
442                                 while( current && !done ) {
443                                         if( current->x <= current_x ) {
444                                                 x1 = current->x;
445                                                 y1 = current->y;
446                                                 done = 1;
447                                         }
448                                         else
449                                                 current = PREVIOUS;
450                                 }
451                                 new_point->gradient = (y2 - y1) / (x2 - x1);
452                                 dragID = 1;
453                         }
454                         else if( dragID == 1 && get_buttonpress() == RIGHT_BUTTON ) {
455                                 if( gui->delete_current_point() ) {
456                                         plugin->send_configure_change();
457                                         dragID = 0;
458                                 }
459                         }
460
461                         plugin->dragging_point = dragID;
462                         result = 1;
463
464                         plugin->config.boundaries();
465                         gui->update_input();
466                         gui->update_canvas();
467                         if( new_point ) {
468                                 plugin->send_configure_change();
469                         }
470                 }
471         }
472         return result;
473 }
474
475 int HistogramCanvas::cursor_motion_event()
476 {
477         if( plugin->dragging_point ) {
478                 HistogramPoints *mode_points = &plugin->config.points[plugin->mode];
479                 HistogramPoint *current_point = mode_points->get_item_number(plugin->current_point);
480
481                 float current_x =
482                         (float)(get_cursor_x() - plugin->point_x_offset) * FLOAT_RANGE / get_w() + HIST_MIN_INPUT;
483                 float current_y = 1.0 - (float)(get_cursor_y() - plugin->point_y_offset) / get_h();
484
485                 switch( plugin->dragging_point ) {
486                 case 1: /* Main point dragged */
487                         current_point->x = current_x;
488                         current_point->y = current_y;
489                         break;
490                 case 2: /* Right control point dragged */
491                         if( current_x - current_point->x > 0 ) {
492                                 current_point->xoffset_right = current_x - current_point->x;
493                                 current_point->gradient = (current_y - current_point->y) / (current_x - current_point->x);
494                         }
495                         break;
496                 case 3: /* Left control point dragged */
497                         if( current_x - current_point->x < 0 ) {
498                                 current_point->xoffset_left = current_x - current_point->x;
499                                 current_point->gradient = (current_point->y - current_y) / (current_point->x - current_x);
500                         }
501                         break;
502                 }
503
504                 plugin->config.boundaries();
505                 gui->update_input();
506                 gui->update_canvas();
507                 plugin->send_configure_change();
508                 return 1;
509         }
510         return 0;
511 }
512
513 int HistogramCanvas::button_release_event()
514 {
515         if(plugin->dragging_point)
516         {
517 // Test for out of order points to delete.
518                 HistogramPoint *current =
519                         plugin->config.points[plugin->mode].get_item_number(plugin->current_point);
520                 HistogramPoint *prev = PREVIOUS;
521                 HistogramPoint *next = NEXT;
522
523                 if( (prev && prev->x >= current->x) ||
524                     (next && next->x <= current->x) ) {
525                         delete current;
526                         plugin->current_point = -1;
527                         plugin->config.boundaries();
528                         gui->update_input();
529                         gui->update_canvas();
530                 }
531
532                 plugin->send_configure_change();
533                 plugin->dragging_point = 0;
534         }
535         return 0;
536 }
537
538
539 HistogramReset::HistogramReset(HistogramMain *plugin, int x, int y)
540  : BC_GenericButton(x, y, _("Reset"))
541 {
542         this->plugin = plugin;
543 }
544 int HistogramReset::handle_event()
545 {
546         plugin->config.reset(1);
547         HistogramWindow *window = (HistogramWindow *)plugin->thread->window;
548         window->update(1);
549         window->update_canvas();
550         plugin->send_configure_change();
551         return 1;
552 }
553
554
555 HistogramSlider::HistogramSlider(HistogramMain *plugin, HistogramWindow *gui,
556                 int x, int y, int w, int h, int is_input)
557  : BC_SubWindow(x, y, w, h)
558 {
559         this->plugin = plugin;
560         this->gui = gui;
561         this->is_input = is_input;
562         operation = NONE;
563 }
564
565 int HistogramSlider::input_to_pixel(float input)
566 {
567         return (int)((input - HIST_MIN_INPUT) / FLOAT_RANGE * get_w());
568 }
569
570 int HistogramSlider::button_press_event()
571 {
572         if( is_event_win() && cursor_inside() ) {
573 //              int w = get_w();
574                 int h = get_h();
575                 int half_h = get_h() / 2;
576
577                 gui->deactivate();
578
579                 if( operation == NONE ) {
580                         int x1 = input_to_pixel(plugin->config.output_min[plugin->mode]) -
581                                 gui->mid_picon->get_w() / 2;
582                         int x2 = x1 + gui->mid_picon->get_w();
583                         if( get_cursor_x() >= x1 && get_cursor_x() < x2 &&
584                             get_cursor_y() >= half_h && get_cursor_y() < h ) {
585                                 operation = DRAG_MIN_OUTPUT;
586                         }
587                 }
588
589                 if( operation == NONE ) {
590                         int x1 = input_to_pixel(plugin->config.output_max[plugin->mode]) -
591                                 gui->mid_picon->get_w() / 2;
592                         int x2 = x1 + gui->mid_picon->get_w();
593                         if( get_cursor_x() >= x1 && get_cursor_x() < x2 &&
594                             get_cursor_y() >= half_h && get_cursor_y() < h ) {
595                                 operation = DRAG_MAX_OUTPUT;
596                         }
597                 }
598                 return 1;
599         }
600         return 0;
601 }
602
603 int HistogramSlider::button_release_event()
604 {
605         if( operation == NONE ) return 0;
606         operation = NONE;
607         return 1;
608 }
609
610 int HistogramSlider::cursor_motion_event()
611 {
612         if( operation == NONE ) return 0;
613
614         float value = (float)get_cursor_x() / get_w() * FLOAT_RANGE + HIST_MIN_INPUT;
615         CLAMP(value, HIST_MIN_INPUT, HIST_MAX_INPUT);
616
617         switch( operation ) {
618         case DRAG_MIN_OUTPUT:
619                 value = MIN(plugin->config.output_max[plugin->mode], value);
620                 plugin->config.output_min[plugin->mode] = value;
621                 break;
622         case DRAG_MAX_OUTPUT:
623                 value = MAX(plugin->config.output_min[plugin->mode], value);
624                 plugin->config.output_max[plugin->mode] = value;
625                 break;
626         }
627
628         plugin->config.boundaries();
629         gui->update_output();
630
631         plugin->send_configure_change();
632         return 1;
633 }
634
635 void HistogramSlider::update()
636 {
637         int w = get_w(), h = get_h();
638         int half_h = get_h() / 2;
639 //      int quarter_h = get_h() / 4;
640         int mode = plugin->mode;
641         int r = 0, g = 0, b = 0;
642
643         clear_box(0, 0, w, h);
644
645         switch( mode ) {
646         case HISTOGRAM_RED:     r = 0xff;  break;
647         case HISTOGRAM_GREEN:   g = 0xff;  break;
648         case HISTOGRAM_BLUE:    b = 0xff;  break;
649         case HISTOGRAM_VALUE:   r = g = b = 0xff;  break;
650         }
651
652         for( int i=0; i<w; ++i ) {
653                 int color = (int)(i * 0xff / w);
654                 set_color(((r * color / 0xff) << 16) |
655                         ((g * color / 0xff) << 8) |
656                         (b * color / 0xff));
657
658                 draw_line(i, 0, i, half_h);
659         }
660
661         float min = plugin->config.output_min[plugin->mode];
662         draw_pixmap(gui->min_picon,
663                 input_to_pixel(min) - gui->min_picon->get_w() / 2,
664                 half_h + 1);
665         float max = plugin->config.output_max[plugin->mode];
666         draw_pixmap(gui->max_picon,
667                 input_to_pixel(max) - gui->max_picon->get_w() / 2,
668                 half_h + 1);
669
670         flash();
671         flush();
672 }
673
674
675 HistogramAuto::HistogramAuto(HistogramMain *plugin, int x, int y)
676  : BC_CheckBox(x, y, plugin->config.automatic, _("Automatic"))
677 {
678         this->plugin = plugin;
679 }
680
681 int HistogramAuto::handle_event()
682 {
683         plugin->config.automatic = get_value();
684         plugin->send_configure_change();
685         return 1;
686 }
687
688
689 HistogramSplit::HistogramSplit(HistogramMain *plugin, int x, int y)
690  : BC_CheckBox(x, y, plugin->config.split, _("Split picture"))
691 {
692         this->plugin = plugin;
693 }
694
695 int HistogramSplit::handle_event()
696 {
697         plugin->config.split = get_value();
698         plugin->send_configure_change();
699         return 1;
700 }
701
702
703 HistogramMode::HistogramMode(HistogramMain *plugin, int x, int y,
704         int value, const char *text)
705  : BC_Radial(x, y, plugin->mode == value, text)
706 {
707         this->plugin = plugin;
708         this->value = value;
709 }
710 int HistogramMode::handle_event()
711 {
712         plugin->mode = value;
713         plugin->current_point= -1;
714         HistogramWindow *window = (HistogramWindow *)plugin->thread->window;
715         window->update_mode();
716         window->update_canvas();
717         window->update_input();
718         window->update_output();
719         window->output->update();
720 //      plugin->send_configure_change();
721         return 1;
722 }
723
724
725 HistogramClear::HistogramClear(HistogramMain *plugin, int x, int y, const char *text)
726  : BC_GenericButton(x, y, text)
727 {
728         this->plugin = plugin;
729 }
730
731 int HistogramClear::handle_event()
732 {
733         HistogramPoints *mode_points = &plugin->config.points[plugin->mode];
734         mode_points->clear();
735         plugin->config.output_min[plugin->mode] = 0.0;
736         plugin->config.output_max[plugin->mode] = 1.0;
737         HistogramWindow *window = (HistogramWindow *)plugin->thread->window;
738         window->update(1);
739         window->update_canvas();
740         plugin->send_configure_change();
741         return 1;
742 }
743
744
745 HistogramOutputText::HistogramOutputText(HistogramMain *plugin,
746         HistogramWindow *gui, int x, int y, float *output)
747  : BC_TumbleTextBox(gui, output ? (float)*output : 0.0,
748                 (float)HIST_MIN_INPUT, (float)HIST_MAX_INPUT,
749                 x, y, xS(60))
750 {
751         this->plugin = plugin;
752         this->output = output;
753         set_precision(DIGITS);
754         set_increment(PRECISION);
755 }
756
757 int HistogramOutputText::handle_event()
758 {
759         if( output ) {
760                 *output = atof(get_text());
761         }
762
763         HistogramWindow *window = (HistogramWindow *)plugin->thread->window;
764         window->output->update();
765         plugin->send_configure_change();
766         return 1;
767 }
768
769
770 HistogramInputText::HistogramInputText(HistogramMain *plugin,
771         HistogramWindow *gui, int x, int y, int do_x)
772  : BC_TumbleTextBox(gui, 0.0,
773                 (float)HIST_MIN_INPUT, (float)HIST_MAX_INPUT,
774                 x, y, xS(60))
775 {
776         this->do_x = do_x;
777         this->plugin = plugin;
778         this->gui = gui;
779         set_precision(DIGITS);
780         set_increment(PRECISION);
781 }
782
783 int HistogramInputText::handle_event()
784 {
785         if( plugin->current_point >= 0 &&
786             plugin->current_point < plugin->config.points[plugin->mode].total() ) {
787                 HistogramPoints *mode_points = &plugin->config.points[plugin->mode];
788                 HistogramPoint *point = mode_points->get_item_number(plugin->current_point);
789
790                 if( point ) {
791                         if( do_x )
792                                 point->x = atof(get_text());
793                         else
794                                 point->y = atof(get_text());
795
796                         plugin->config.boundaries();
797                         gui->update_canvas();
798
799                         HistogramWindow *window = (HistogramWindow *)plugin->thread->window;
800                         window->output->update();
801                         plugin->send_configure_change();
802                 }
803         }
804         return 1;
805 }
806
807 void HistogramInputText::update()
808 {
809         if( plugin->current_point >= 0 &&
810             plugin->current_point < plugin->config.points[plugin->mode].total() ) {
811                 HistogramPoints *mode_points = &plugin->config.points[plugin->mode];
812                 HistogramPoint *point = mode_points->get_item_number(plugin->current_point);
813
814                 if( point ) {
815                         if( do_x )
816                                 BC_TumbleTextBox::update(point->x);
817                         else
818                                 BC_TumbleTextBox::update(point->y);
819                 }
820                 else {
821                         BC_TumbleTextBox::update((float)0.0);
822                 }
823         }
824         else {
825                 BC_TumbleTextBox::update((float)0.0);
826         }
827
828 }
829
830
831 HistogramSmoothMode::HistogramSmoothMode(HistogramMain*plugin,
832         HistogramWindow *gui, int x, int y)
833  : BC_PopupMenu(x, y, xS(120), to_text(plugin->config.smoothMode), 1)
834 {
835         this->plugin = plugin;
836         this->gui = gui;
837 }
838 void HistogramSmoothMode::create_objects()
839 {
840         add_item(new BC_MenuItem(to_text(HISTOGRAM_LINEAR)));
841         add_item(new BC_MenuItem(to_text(HISTOGRAM_POLYNOMINAL)));
842         add_item(new BC_MenuItem(to_text(HISTOGRAM_BEZIER)));
843 }
844
845 char* HistogramSmoothMode::to_text(int mode)
846 {
847         switch( mode ) {
848         case HISTOGRAM_LINEAR:          return _("Linear");
849         case HISTOGRAM_POLYNOMINAL:     return _("Polynominal");
850         case HISTOGRAM_BEZIER:          return _("Bezier");
851         }
852         return _("None");
853 }
854 int HistogramSmoothMode::set_mode(int mode)
855 {
856         const char *tp = to_text(mode);
857         if( strcmp(tp, get_text()) ) {
858                 set_text(tp);
859                 return 1;
860         }
861         return 0;
862 }
863
864
865 int HistogramSmoothMode::from_text(char *text)
866 {
867         if( !strcmp(text, to_text(HISTOGRAM_LINEAR)) ) return HISTOGRAM_LINEAR;
868         if( !strcmp(text, to_text(HISTOGRAM_POLYNOMINAL)) ) return HISTOGRAM_POLYNOMINAL;
869         if( !strcmp(text, to_text(HISTOGRAM_BEZIER)) ) return HISTOGRAM_BEZIER;
870         return HISTOGRAM_LINEAR;
871 }
872 int HistogramSmoothMode::get_mode()
873 {
874         return from_text(get_text());
875 }
876
877 int HistogramSmoothMode::handle_event()
878 {
879         plugin->config.smoothMode = get_mode();
880         gui->update_canvas();
881         plugin->send_configure_change();
882         return 1;
883 }
884