change thread join strategy, fix a few leaks, fix a few bugs
[goodguy/history.git] / cinelerra-5.1 / cinelerra / channeledit.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2011 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 "batch.h"
23 #include "bcprogressbox.h"
24 #include "bcsignals.h"
25 #include "channel.h"
26 #include "channeldb.h"
27 #include "channeledit.h"
28 #include "channelpicker.h"
29 #include "chantables.h"
30 #include "clip.h"
31 #include "condition.h"
32 #include "language.h"
33 #include "mainsession.h"
34 #include "mwindow.h"
35 #include "picture.h"
36 #include "theme.h"
37 #include "videodevice.h"
38 #include <ctype.h>
39 #include <string.h>
40 #include <unistd.h>
41
42
43 ChannelEditThread::ChannelEditThread(ChannelPicker *channel_picker,
44         ChannelDB *channeldb)
45  : Thread()
46 {
47         this->channel_picker = channel_picker;
48         this->channeldb = channeldb;
49         in_progress = 0;
50         this->window = 0;
51         new_channels = new ChannelDB;
52         completion = new Condition(1, "ChannelEditThread::completion");
53         scan_thread = 0;
54 }
55 ChannelEditThread::~ChannelEditThread()
56 {
57         close_threads();
58         delete scan_thread;
59         delete new_channels;
60         delete completion;
61 }
62
63 void ChannelEditThread::run()
64 {
65         if(in_progress)
66         {
67                 if(window)
68                 {
69                         window->lock_window("ChannelEditThread::run");
70                         window->raise_window(1);
71                         window->unlock_window();
72                 }
73                 return;
74         }
75         in_progress = 1;
76         completion->lock("ChannelEditThread::run");
77
78 // Copy master channel list to temporary.
79         new_channels->copy_from(channel_picker->channeldb);
80         Channel *channel = channel_picker->get_current_channel();
81         current_channel = channel_picker->channeldb->number_of(channel);
82 //printf("ChannelEditThread::run 1 %d\n", current_channel);
83
84 // Run the channel list window using the temporary list.
85         window = new ChannelEditWindow(this, channel_picker);
86         window->create_objects();
87         int result = window->run_window();
88         delete window;  window = 0;
89
90         if(!result) {
91 // Copy new channels to master list
92                 channel_picker->channeldb->clear();
93
94                 channel_picker->channeldb->copy_from(new_channels);
95                 channel_picker->update_channel_list();
96
97         }
98
99         channel_picker->handle_channel_edit(result);
100
101         completion->unlock();
102         in_progress = 0;
103
104 }
105
106 void ChannelEditThread::close_threads()
107 {
108         if(in_progress && window)
109         {
110                 window->set_done(1);
111                 completion->lock("ChannelEditThread::close_threads");
112                 completion->unlock();
113         }
114 }
115
116 char *ChannelEditThread::value_to_freqtable(int value)
117 {
118         switch(value)
119         {
120                 case NTSC_DVB: return _("NTSC_DVB");
121                 case CATV_DVB: return _("CATV_DVB");
122                 case NTSC_BCAST: return _("NTSC_BCAST");
123                 case NTSC_CABLE: return _("NTSC_CABLE");
124                 case NTSC_HRC: return _("NTSC_HRC");
125                 case NTSC_BCAST_JP: return _("NTSC_BCAST_JP");
126                 case NTSC_CABLE_JP: return _("NTSC_CABLE_JP");
127                 case PAL_AUSTRALIA: return _("PAL_AUSTRALIA");
128                 case PAL_EUROPE: return _("PAL_EUROPE");
129                 case PAL_E_EUROPE: return _("PAL_E_EUROPE");
130                 case PAL_ITALY: return _("PAL_ITALY");
131                 case PAL_IRELAND: return _("PAL_IRELAND");
132                 case PAL_NEWZEALAND: return _("PAL_NEWZEALAND");
133         }
134         return _("ERROR");
135 }
136
137 char* ChannelEditThread::value_to_norm(int value)
138 {
139         switch(value)
140         {
141                 case NTSC: return _("NTSC");
142                 case PAL: return _("PAL");
143                 case SECAM: return _("SECAM");
144         }
145         return _("ERROR");
146 }
147
148 char* ChannelEditThread::value_to_input(int value)
149 {
150         ArrayList<Channel*> *inputs = channel_picker->get_video_inputs();
151         return value >= inputs->total ? _("None") :
152                 inputs->values[value]->device_name;
153 }
154
155
156
157
158
159
160
161 ChannelEditWindow::ChannelEditWindow(ChannelEditThread *thread,
162         ChannelPicker *channel_picker)
163  : BC_Window(PROGRAM_NAME ": Channels",
164         channel_picker->mwindow->session->channels_x,
165         channel_picker->mwindow->session->channels_y,
166         350, 400, 350, 400, 0, 0, 1)
167 {
168         this->thread = thread;
169         this->channel_picker = channel_picker;
170         this->edit_thread = 0;
171         this->picture_thread = 0;
172         this->scan_confirm_thread = 0;
173 }
174
175 ChannelEditWindow::~ChannelEditWindow()
176 {
177         if( thread->scan_thread )
178                 thread->scan_thread->stop();
179         channel_list.remove_all_objects();
180         delete edit_thread;
181         delete picture_thread;
182         delete scan_confirm_thread;
183 }
184
185 void ChannelEditWindow::create_objects()
186 {
187         int x = 10, y = 10;
188
189 // Create channel list
190         for(int i = 0; i < thread->new_channels->size(); i++)
191         {
192                 channel_list.append(new BC_ListBoxItem(thread->new_channels->get(i)->title));
193         }
194
195         add_subwindow(list_box = new ChannelEditList(this, x, y));
196         x += 200;
197         if(channel_picker->use_select())
198         {
199                 add_subwindow(new ChannelEditSelect(this, x, y));
200                 y += 30;
201         }
202         add_subwindow(new ChannelEditAdd(this, x, y));
203         y += 30;
204         add_subwindow(new ChannelEdit(this, x, y));
205         y += 30;
206         add_subwindow(new ChannelEditMoveUp(this, x, y));
207         y += 30;
208         add_subwindow(new ChannelEditMoveDown(this, x, y));
209         y += 30;
210         add_subwindow(new ChannelEditSort(this, x, y));
211         y += 30;
212
213         Channel *channel_usage = channel_picker->get_channel_usage();
214         if(channel_usage && channel_usage->has_scanning)
215         {
216                 add_subwindow(new ChannelEditScan(this, x, y));
217                 y += 30;
218         }
219         add_subwindow(new ChannelEditDel(this, x, y));
220         y += 30;
221         add_subwindow(new ChannelEditPicture(this, x, y));
222         y += 100;
223         x = 10;
224         add_subwindow(new BC_OKButton(this));
225         x += 150;
226         add_subwindow(new BC_CancelButton(this));
227
228
229         edit_thread = new ChannelEditEditThread(this, channel_picker);
230         picture_thread = new ChannelEditPictureThread(channel_picker);
231         show_window();
232 }
233
234 int ChannelEditWindow::close_event()
235 {
236         if( thread->scan_thread )
237                 thread->scan_thread->stop();
238         return BC_WindowBase::close_event();
239 }
240
241 int ChannelEditWindow::translation_event()
242 {
243         channel_picker->mwindow->session->channels_x = get_x();
244         channel_picker->mwindow->session->channels_y = get_y();
245         return 0;
246 }
247
248
249 int ChannelEditWindow::add_channel()
250 {
251         Channel *new_channel;
252         Channel *prev_channel = 0;
253
254 // Create new channel
255         new_channel = new Channel;
256
257 // Reuse parameters from previous channel
258         if(thread->new_channels->size())
259         {
260                 prev_channel = thread->new_channels->get(
261                                 thread->new_channels->size() - 1);
262                 new_channel->copy_settings(prev_channel);
263         }
264         else
265 // Use default channel parameters
266         if(channel_picker->get_master_channel())
267         {
268                 new_channel->copy_settings(channel_picker->get_master_channel());
269         }
270
271 // Copy device usage.  Need the same thing for playback.
272         if(channel_picker->get_master_channel())
273         {
274                 new_channel->copy_usage(channel_picker->get_master_channel());
275         }
276
277 // Add to channel table
278         channel_list.append(new BC_ListBoxItem(new_channel->title));
279         thread->new_channels->append(new_channel);
280         update_list();
281
282 // Start common routing
283         edit_thread->edit_channel(new_channel, 0);
284         return 0;
285 }
286
287 void ChannelEditWindow::update_list()
288 {
289 // Create channel list
290         channel_list.remove_all_objects();
291         for(int i = 0; i < thread->new_channels->size(); i++)
292         {
293                 channel_list.append(
294                         new BC_ListBoxItem(
295                                 thread->new_channels->get(i)->title));
296         }
297
298         list_box->update(&channel_list, 0, 0, 1, list_box->get_yposition());
299 }
300
301 void ChannelEditWindow::update_list(Channel *channel)
302 {
303         int i;
304         for(i = 0; i < thread->new_channels->size(); i++)
305                 if(thread->new_channels->get(i) == channel) break;
306
307         if(i < thread->new_channels->size())
308         {
309                 channel_list.values[i]->set_text(channel->title);
310         }
311
312         update_list();
313 }
314
315
316 void ChannelEditWindow::edit_channel()
317 {
318         if(list_box->get_selection_number(0, 0) > -1)
319         {
320                 thread->current_channel = list_box->get_selection_number(0, 0);
321                 edit_thread->edit_channel(
322                         thread->new_channels->get(
323                                 list_box->get_selection_number(0, 0)),
324                         1);
325         }
326 }
327
328 void ChannelEditWindow::edit_picture()
329 {
330         picture_thread->edit_picture();
331 }
332
333 void ChannelEditWindow::scan_confirm()
334 {
335         channel_picker->load_scan_defaults(&thread->scan_params);
336         if(!scan_confirm_thread) scan_confirm_thread = new ConfirmScanThread(this);
337         unlock_window();
338         scan_confirm_thread->start();
339         lock_window("ChannelEditWindow::scan_confirm");
340 }
341
342 void ChannelEditWindow::scan()
343 {
344         thread->new_channels->clear();
345         update_list();
346
347         if( !thread->scan_thread )
348                 thread->scan_thread = new ScanThread(thread);
349         thread->scan_thread->start();
350 }
351
352
353 void ChannelEditWindow::get_chan_num(Channel *channel, int &chan, int &stream)
354 {
355         const char *cp = channel->title;
356         int chn = 0, str = 0;
357         while( isdigit(*cp) ) chn = 10*chn + (*cp++ -'0');
358         if( chn != 0 && *cp != 0 ) {
359                 if( *cp++ == '.' )
360                         while( isdigit(*cp) ) str = 10*str + (*cp++ -'0');
361                 if( !str ) chn = 0;
362         }
363         chan = chn;  stream = str;
364 }
365
366 void ChannelEditWindow::sort()
367 {
368         int done = 0;
369         while(!done) {
370                 done = 1;
371                 for(int i = 0; i < thread->new_channels->size() - 1; i++) {
372                         Channel *channel1 = thread->new_channels->get(i);
373                         Channel *channel2 = thread->new_channels->get(i + 1);
374                         int ch1, st1;  get_chan_num(channel1, ch1, st1);
375                         int ch2, st2;  get_chan_num(channel2, ch2, st2);
376                         if( (ch1 > 0 && ch2 > 0) ?
377                              (ch1 > ch2 || (ch1 == ch2 && (st1 > st2 || (st1 == st2 &&
378                                 strcasecmp(channel2->title, channel1->title) < 0)))) :
379                              strcasecmp(channel2->title, channel1->title) < 0) {
380                                 thread->new_channels->set(i, channel2);
381                                 thread->new_channels->set(i + 1, channel1);
382                                 done = 0;
383                         }
384                 }
385         }
386         update_list();
387 }
388
389
390 void ChannelEditWindow::delete_channel(int number)
391 {
392         delete thread->new_channels->get(number);
393         channel_list.remove_number(number);
394         thread->new_channels->remove_number(number);
395         update_list();
396 }
397
398 void ChannelEditWindow::delete_channel(Channel *channel)
399 {
400         int i;
401         for(i = 0; i < thread->new_channels->size(); i++)
402         {
403                 if(thread->new_channels->get(i) == channel)
404                 {
405                         break;
406                 }
407         }
408         if(i < thread->new_channels->size()) delete_channel(i);
409 }
410
411 int ChannelEditWindow::move_channel_up()
412 {
413         if(list_box->get_selection_number(0, 0) > -1)
414         {
415                 int number2 = list_box->get_selection_number(0, 0);
416                 int number1 = number2 - 1;
417                 Channel *temp;
418                 BC_ListBoxItem *temp_text;
419
420                 if(number1 < 0) number1 = thread->new_channels->size() - 1;
421
422                 temp = thread->new_channels->get(number1);
423                 thread->new_channels->set(number1, thread->new_channels->get(number2));
424                 thread->new_channels->set(number2, temp);
425
426                 temp_text = channel_list.values[number1];
427                 channel_list.values[number1] = channel_list.values[number2];
428                 channel_list.values[number2] = temp_text;
429                 list_box->update(&channel_list,
430                         0,
431                         0,
432                         1,
433                         list_box->get_xposition(),
434                         list_box->get_yposition(),
435                         number1,
436                         1);
437         }
438         return 0;
439 }
440
441 int ChannelEditWindow::move_channel_down()
442 {
443         if(list_box->get_selection_number(0, 0) > -1)
444         {
445                 int number2 = list_box->get_selection_number(0, 0);
446                 int number1 = number2 + 1;
447                 Channel *temp;
448                 BC_ListBoxItem *temp_text;
449
450                 if(number1 > thread->new_channels->size() - 1) number1 = 0;
451
452                 temp = thread->new_channels->get(number1);
453                 thread->new_channels->set(number1, thread->new_channels->get(number2));
454                 thread->new_channels->set(number2, temp);
455                 temp_text = channel_list.values[number1];
456                 channel_list.values[number1] = channel_list.values[number2];
457                 channel_list.values[number2] = temp_text;
458                 list_box->update(&channel_list,
459                         0,
460                         0,
461                         1,
462                         list_box->get_xposition(),
463                         list_box->get_yposition(),
464                         number1,
465                         1);
466         }
467         return 0;
468 }
469
470 int ChannelEditWindow::change_channel_from_list(int channel_number)
471 {
472         if(channel_number > -1 && channel_number < thread->new_channels->size())
473         {
474                 thread->current_channel = channel_number;
475                 channel_picker->set_channel(thread->new_channels->get(channel_number));
476         }
477         return 0;
478 }
479
480 ChannelEditSelect::ChannelEditSelect(ChannelEditWindow *window, int x, int y)
481  : BC_GenericButton(x, y, _("Select"))
482 {
483         this->window = window;
484 }
485 ChannelEditSelect::~ChannelEditSelect()
486 {
487 }
488 int ChannelEditSelect::handle_event()
489 {
490         window->change_channel_from_list(
491                 window->list_box->get_selection_number(0, 0));
492         return 1;
493 }
494
495 ChannelEditAdd::ChannelEditAdd(ChannelEditWindow *window, int x, int y)
496  : BC_GenericButton(x, y, _("Add..."))
497 {
498         this->window = window;
499 }
500 ChannelEditAdd::~ChannelEditAdd()
501 {
502 }
503 int ChannelEditAdd::handle_event()
504 {
505         window->add_channel();
506         return 1;
507 }
508
509 ChannelEditList::ChannelEditList(ChannelEditWindow *window, int x, int y)
510  : BC_ListBox(x,
511                         y,
512                         185,
513                         window->get_h() - BC_OKButton::calculate_h() - y - 10,
514                         LISTBOX_TEXT,
515                         &(window->channel_list))
516 {
517         this->window = window;
518 }
519 ChannelEditList::~ChannelEditList()
520 {
521 }
522 int ChannelEditList::handle_event()
523 {
524         window->edit_channel();
525         return 1;
526 }
527
528 ChannelEditMoveUp::ChannelEditMoveUp(ChannelEditWindow *window, int x, int y)
529  : BC_GenericButton(x, y, _("Move up"))
530 {
531         this->window = window;
532 }
533 ChannelEditMoveUp::~ChannelEditMoveUp()
534 {
535 }
536 int ChannelEditMoveUp::handle_event()
537 {
538         lock_window("ChannelEditMoveUp::handle_event");
539         window->move_channel_up();
540         unlock_window();
541         return 1;
542 }
543
544 ChannelEditMoveDown::ChannelEditMoveDown(ChannelEditWindow *window, int x, int y)
545  : BC_GenericButton(x, y, _("Move down"))
546 {
547         this->window = window;
548 }
549 ChannelEditMoveDown::~ChannelEditMoveDown()
550 {
551 }
552 int ChannelEditMoveDown::handle_event()
553 {
554         lock_window("ChannelEditMoveDown::handle_event");
555         window->move_channel_down();
556         unlock_window();
557         return 1;
558 }
559
560 ChannelEditSort::ChannelEditSort(ChannelEditWindow *window, int x, int y)
561  : BC_GenericButton(x, y, _("Sort"))
562 {
563         this->window = window;
564 }
565 int ChannelEditSort::handle_event()
566 {
567         lock_window("ChannelEditSort::handle_event");
568         window->sort();
569         unlock_window();
570         return 1;
571 }
572
573 ChannelEditScan::ChannelEditScan(ChannelEditWindow *window, int x, int y)
574  : BC_GenericButton(x, y, _("Scan"))
575 {
576         this->window = window;
577 }
578 int ChannelEditScan::handle_event()
579 {
580         window->scan_confirm();
581         return 1;
582 }
583
584 ChannelEditDel::ChannelEditDel(ChannelEditWindow *window, int x, int y)
585  : BC_GenericButton(x, y, _("Delete"))
586 {
587         this->window = window;
588 }
589 ChannelEditDel::~ChannelEditDel()
590 {
591 }
592 int ChannelEditDel::handle_event()
593 {
594         if(window->list_box->get_selection_number(0, 0) > -1)
595                 window->delete_channel(window->list_box->get_selection_number(0, 0));
596         return 1;
597 }
598
599 ChannelEdit::ChannelEdit(ChannelEditWindow *window, int x, int y)
600  : BC_GenericButton(x, y, _("Edit..."))
601 {
602         this->window = window;
603 }
604 ChannelEdit::~ChannelEdit()
605 {
606 }
607 int ChannelEdit::handle_event()
608 {
609         window->edit_channel();
610         return 1;
611 }
612
613 ChannelEditPicture::ChannelEditPicture(ChannelEditWindow *window, int x, int y)
614  : BC_GenericButton(x, y, _("Picture..."))
615 {
616         this->window = window;
617 }
618 ChannelEditPicture::~ChannelEditPicture()
619 {
620 }
621 int ChannelEditPicture::handle_event()
622 {
623         window->edit_picture();
624         return 1;
625 }
626
627
628
629
630
631
632
633
634
635
636
637
638 // ========================= confirm overwrite by channel scannin
639
640
641 ConfirmScan::ConfirmScan(ChannelEditWindow *gui, int x, int y)
642  : BC_Window(PROGRAM_NAME ": Scan confirm",
643         x,
644         y,
645         350,
646         BC_OKButton::calculate_h() + 130,
647         0,
648         0,
649         0,
650         0,
651         1)
652 {
653         this->gui = gui;
654 }
655
656 void ConfirmScan::create_objects()
657 {
658         int x = 10, y = 10;
659         int y2 = 0, x2 = 0;
660         BC_Title *title;
661         add_subwindow(title = new BC_Title(x, y, _("Set parameters for channel scanning.")));
662         y += title->get_h() + 10;
663         y2 = y;
664
665         add_subwindow(title = new BC_Title(x, y, _("Frequency table:")));
666         x2 = title->get_w();
667         y += BC_PopupMenu::calculate_h();
668         add_subwindow(title = new BC_Title(x, y, _("Norm:")));
669         x2 = MAX(x2, title->get_w());
670         y += BC_PopupMenu::calculate_h();
671         add_subwindow(title = new BC_Title(x, y, _("Input:")));
672         x2 = MAX(x2, title->get_w());
673         y += BC_PopupMenu::calculate_h();
674         x2 += x + 5;
675
676         y = y2;
677         x = x2;
678         ChannelEditEditFreqtable *table;
679         add_subwindow(table = new ChannelEditEditFreqtable(x,
680                 y,
681                 0,
682                 gui->thread));
683         table->add_items();
684         y += table->get_h() + 10;
685
686         ChannelEditEditNorm *norm;
687         add_subwindow(norm = new ChannelEditEditNorm(x,
688                 y,
689                 0,
690                 gui->thread));
691         norm->add_items();
692         y += norm->get_h() + 10;
693
694         ChannelEditEditInput *input;
695         add_subwindow(input = new ChannelEditEditInput(x,
696                 y,
697                 0,
698                 gui->thread));
699         input->add_items();
700
701
702         add_subwindow(new BC_OKButton(this));
703         add_subwindow(new BC_CancelButton(this));
704         show_window();
705 }
706
707
708
709
710
711
712
713 ConfirmScanThread::ConfirmScanThread(ChannelEditWindow *gui)
714  : BC_DialogThread()
715 {
716         this->gui = gui;
717 }
718
719 void ConfirmScanThread::handle_done_event(int result)
720 {
721         gui->channel_picker->save_scan_defaults(&gui->thread->scan_params);
722         if(!result)
723         {
724                 get_gui()->hide_window();
725                 gui->lock_window("ConfirmScanThread::handle_done_event");
726                 gui->scan();
727                 gui->unlock_window();
728         }
729 }
730
731 ConfirmScanThread::~ConfirmScanThread()
732 {
733         close_window();
734 }
735
736 BC_Window* ConfirmScanThread::new_gui()
737 {
738         int x = gui->get_abs_cursor_x(1);
739         int y = gui->get_abs_cursor_y(1);
740         ConfirmScan *result = new ConfirmScan(gui, x, y);
741         result->create_objects();
742         return result;
743 }
744
745
746
747
748
749
750 ScanThread::ScanThread(ChannelEditThread *edit)
751  : Thread(1, 0, 0)
752 {
753         this->edit = edit;
754         interrupt = 1;
755         progress = 0;
756 }
757
758 ScanThread::~ScanThread()
759 {
760         stop();
761 }
762
763 void ScanThread::stop()
764 {
765         if( !interrupt ) {
766 // Cancel previous job
767                 interrupt = 1;
768         }
769         Thread::join();
770         if( progress ) {
771                 progress->stop_progress();
772                 delete progress;  progress = 0;
773         }
774 }
775
776 void ScanThread::start()
777 {
778         stop();
779         interrupt = 0;
780
781         progress = new BC_ProgressBox(
782                 edit->channel_picker->parent_window->get_abs_cursor_x(1),
783                 edit->channel_picker->parent_window->get_abs_cursor_y(1),
784                 "Scanning",
785                 chanlists[edit->scan_params.freqtable].count);
786         progress->start();
787
788         Thread::start();
789 }
790
791 void ScanThread::run()
792 {
793         for(int i = 0; i < chanlists[edit->scan_params.freqtable].count &&
794                         !interrupt && !progress->is_cancelled(); i++) {
795                 edit->scan_params.entry = i;
796                 char string[BCTEXTLEN];
797                 sprintf(edit->scan_params.title, "%s",
798                         chanlists[edit->scan_params.freqtable].list[i].name);
799                 sprintf(string, "Scanning %s", edit->scan_params.title);
800                 progress->update_title(string, 1);
801                 progress->update(i, 1);
802                 edit->channel_picker->set_channel(&edit->scan_params);
803                 sleep(1); // has_signal should do whatever it takes
804                 if( interrupt ) break;
805                 int got_signal = edit->channel_picker->has_signal();
806                 if( interrupt ) break;
807                 if(!got_signal) continue;
808                 if(edit->channel_picker->get_channel_usage()->has_subchan) {
809                         ArrayList<Channel*> channels;
810                         edit->channel_picker->create_channeldb(&channels);
811                         if( channels.total ) {
812                                 edit->window->lock_window("ScanThread::run");
813                                 for( int i=0; i<channels.total; ++i ) {
814                                         Channel *new_channel = channels.values[i];
815                                         new_channel->copy_usage(&edit->scan_params);
816                                         edit->new_channels->append(new_channel);
817                                 }
818                                 edit->window->update_list();
819                                 edit->window->unlock_window();
820                                 continue;
821                         }
822                 }
823                 Channel *new_channel = new Channel;
824                 new_channel->copy_usage(&edit->scan_params);
825                 new_channel->copy_settings(&edit->scan_params);
826                 edit->window->lock_window("ScanThread::run");
827                 edit->new_channels->append(new_channel);
828                 edit->window->update_list();
829                 edit->window->unlock_window();
830         }
831 }
832
833
834
835
836
837
838
839 // ================================= Edit a single channel
840
841
842
843 ChannelEditEditThread::ChannelEditEditThread(ChannelEditWindow *window,
844         ChannelPicker *channel_picker)
845  : Thread()
846 {
847         this->window = window;
848         this->channel_picker = channel_picker;
849         in_progress = 0;
850         edit_window = 0;
851         editing = 0;
852         completion = new Condition(1, "ChannelEditEditThread::completion");
853 }
854
855 ChannelEditEditThread::~ChannelEditEditThread()
856 {
857         close_threads();
858         delete completion;
859 }
860
861 void ChannelEditEditThread::close_threads()
862 {
863         if(edit_window)
864         {
865                 edit_window->set_done(1);
866                 completion->lock("ChannelEditEditThread::close_threads");
867                 completion->unlock();
868         }
869 }
870
871 int ChannelEditEditThread::edit_channel(Channel *channel, int editing)
872 {
873         if(in_progress)
874         {
875                 edit_window->lock_window("ChannelEditEditThread::edit_channel");
876                 edit_window->raise_window(1);
877                 edit_window->unlock_window();
878                 return 1;
879         }
880         in_progress = 1;
881
882 // Copy the channel to edit into a temporary
883         completion->lock("ChannelEditEditThread::edit_channel");
884         this->editing = editing;
885         this->output_channel = channel;
886         new_channel.copy_settings(output_channel);
887         new_channel.copy_usage(output_channel);
888
889         if(editing && new_channel.title[0])
890                 user_title = 1;
891         else
892                 user_title = 0;
893         set_synchronous(0);
894         Thread::start();
895         return 0;
896 }
897
898
899 void ChannelEditEditThread::set_device()
900 {
901         channel_picker->set_channel(&new_channel);
902 }
903
904 void ChannelEditEditThread::change_source(const char *source_name)
905 {
906         for(int i = 0; i < chanlists[new_channel.freqtable].count; i++)
907         {
908                 if(!strcasecmp(chanlists[new_channel.freqtable].list[i].name, source_name))
909                 {
910                         new_channel.entry = i;
911                         i = chanlists[new_channel.freqtable].count;
912                         set_device();
913                 }
914         }
915         if(!user_title)
916         {
917                 strcpy(new_channel.title, source_name);
918                 if(edit_window->title_text)
919                 {
920                         edit_window->title_text->update(source_name);
921                 }
922         }
923 }
924
925 void ChannelEditEditThread::source_up()
926 {
927         new_channel.entry++;
928         if(new_channel.entry > chanlists[new_channel.freqtable].count - 1) new_channel.entry = 0;
929         source_text->update(chanlists[new_channel.freqtable].list[new_channel.entry].name);
930         set_device();
931 }
932
933 void ChannelEditEditThread::source_down()
934 {
935         new_channel.entry--;
936         if(new_channel.entry < 0) new_channel.entry = chanlists[new_channel.freqtable].count - 1;
937         source_text->update(chanlists[new_channel.freqtable].list[new_channel.entry].name);
938         set_device();
939 }
940
941 void ChannelEditEditThread::set_input(int value)
942 {
943         new_channel.input = value;
944         set_device();
945 }
946
947 void ChannelEditEditThread::set_norm(int value)
948 {
949         new_channel.norm = value;
950         set_device();
951 }
952
953 void ChannelEditEditThread::set_freqtable(int value)
954 {
955         new_channel.freqtable = value;
956         if(new_channel.entry > chanlists[new_channel.freqtable].count - 1) new_channel.entry = 0;
957         source_text->update(chanlists[new_channel.freqtable].list[new_channel.entry].name);
958         set_device();
959 }
960
961 void ChannelEditEditThread::run()
962 {
963         ChannelEditEditWindow edit_window(this, window, channel_picker);
964         edit_window.create_objects(&new_channel);
965         this->edit_window = &edit_window;
966         int result = edit_window.run_window();
967         this->edit_window = 0;
968
969 // Done editing channel.  Keep channel.
970         if(!result)
971         {
972                 output_channel->copy_settings(&new_channel);
973                 window->lock_window();
974                 window->update_list(output_channel);
975                 window->unlock_window();
976         }
977         else
978         {
979 // Discard channel.
980                 if(!editing)
981                 {
982                         window->lock_window();
983                         window->delete_channel(output_channel);
984                         window->unlock_window();
985                 }
986         }
987         editing = 0;
988         completion->unlock();
989         in_progress = 0;
990 }
991
992 ChannelEditEditWindow::ChannelEditEditWindow(ChannelEditEditThread *thread,
993         ChannelEditWindow *window,
994         ChannelPicker *channel_picker)
995  : BC_Window(PROGRAM_NAME ": Edit Channel",
996         channel_picker->parent_window->get_abs_cursor_x(1),
997         channel_picker->parent_window->get_abs_cursor_y(1),
998         390,
999         300,
1000         390,
1001         300,
1002         0,
1003         0,
1004         1)
1005 {
1006         this->channel_picker = channel_picker;
1007         this->window = window;
1008         this->thread = thread;
1009 }
1010 ChannelEditEditWindow::~ChannelEditEditWindow()
1011 {
1012 }
1013 void ChannelEditEditWindow::create_objects(Channel *channel)
1014 {
1015         this->new_channel = channel;
1016         Channel *channel_usage = channel_picker->get_channel_usage();
1017         title_text = 0;
1018
1019         int x = 10, y = 10;
1020 //      if(!channel_usage ||
1021 //              (!channel_usage->use_frequency &&
1022 //              !channel_usage->use_fine &&
1023 //              !channel_usage->use_norm &&
1024 //              !channel_usage->use_input))
1025 //      {
1026 //              add_subwindow(new BC_Title(x, y, "Device has no input selection."));
1027 //              y += 30;
1028 //      }
1029 //      else
1030 //      {
1031                 add_subwindow(new BC_Title(x, y, _("Title:")));
1032                 add_subwindow(title_text = new ChannelEditEditTitle(x, y + 20, thread));
1033                 y += 50;
1034 //      }
1035
1036         if(channel_usage && channel_usage->use_frequency)
1037         {
1038                 add_subwindow(new BC_Title(x, y, _("Channel:")));
1039                 y += 20;
1040                 add_subwindow(thread->source_text = new ChannelEditEditSource(x, y, thread));
1041                 add_subwindow(new ChannelEditEditSourceTumbler(x + 160, y, thread));
1042                 y += 40;
1043
1044                 add_subwindow(new BC_Title(x, y, _("Frequency table:")));
1045                 ChannelEditEditFreqtable *table;
1046                 add_subwindow(table = new ChannelEditEditFreqtable(x + 130,
1047                         y,
1048                         thread,
1049                         window->thread));
1050                 table->add_items();
1051                 y += 30;
1052         }
1053
1054         if(channel_usage && channel_usage->use_fine)
1055         {
1056                 add_subwindow(new BC_Title(x, y, _("Fine:")));
1057                 add_subwindow(new ChannelEditEditFine(x + 130, y, thread));
1058                 y += 30;
1059         }
1060
1061         if(channel_usage && channel_usage->use_norm)
1062         {
1063                 add_subwindow(new BC_Title(x, y, _("Norm:")));
1064                 ChannelEditEditNorm *norm;
1065                 add_subwindow(norm = new ChannelEditEditNorm(x + 130,
1066                         y,
1067                         thread,
1068                         window->thread));
1069                 norm->add_items();
1070                 y += 30;
1071         }
1072
1073         if( (channel_usage && channel_usage->use_input) ||
1074                 !channel_usage)
1075         {
1076                 add_subwindow(new BC_Title(x, y, _("Input:")));
1077                 ChannelEditEditInput *input;
1078                 add_subwindow(input = new ChannelEditEditInput(x + 130,
1079                         y,
1080                         thread,
1081                         window->thread));
1082                 input->add_items();
1083                 y += 30;
1084         }
1085
1086         add_subwindow(new BC_OKButton(this));
1087         x += 200;
1088         add_subwindow(new BC_CancelButton(this));
1089         show_window();
1090 }
1091
1092 ChannelEditEditTitle::ChannelEditEditTitle(int x,
1093         int y,
1094         ChannelEditEditThread *thread)
1095  : BC_TextBox(x, y, 150, 1, thread->new_channel.title)
1096 {
1097         this->thread = thread;
1098 }
1099 ChannelEditEditTitle::~ChannelEditEditTitle()
1100 {
1101 }
1102 int ChannelEditEditTitle::handle_event()
1103 {
1104         if(strlen(get_text()) < 1024)
1105         {
1106                 strcpy(thread->new_channel.title, get_text());
1107         }
1108         thread->user_title = get_text()[0] ? 1 : 0;
1109         return 1;
1110 }
1111
1112
1113 ChannelEditEditSource::ChannelEditEditSource(int x, int y, ChannelEditEditThread *thread)
1114  : BC_TextBox(x, y, 150, 1, chanlists[thread->new_channel.freqtable].list[thread->new_channel.entry].name)
1115 {
1116         this->thread = thread;
1117 }
1118
1119 ChannelEditEditSource::~ChannelEditEditSource()
1120 {
1121 }
1122 int ChannelEditEditSource::handle_event()
1123 {
1124         thread->change_source(get_text());
1125         return 1;
1126 }
1127
1128
1129 ChannelEditEditSourceTumbler::ChannelEditEditSourceTumbler(int x, int y, ChannelEditEditThread *thread)
1130  : BC_Tumbler(x, y)
1131 {
1132         this->thread = thread;
1133 }
1134 ChannelEditEditSourceTumbler::~ChannelEditEditSourceTumbler()
1135 {
1136 }
1137 int ChannelEditEditSourceTumbler::handle_up_event()
1138 {
1139         thread->source_up();
1140         return 1;
1141 }
1142 int ChannelEditEditSourceTumbler::handle_down_event()
1143 {
1144         thread->source_down();
1145         return 1;
1146 }
1147
1148 ChannelEditEditInput::ChannelEditEditInput(int x,
1149         int y,
1150         ChannelEditEditThread *thread,
1151         ChannelEditThread *edit)
1152  : BC_PopupMenu(x,
1153         y,
1154         150,
1155         edit->value_to_input(thread ? thread->new_channel.input : edit->scan_params.input))
1156 {
1157         this->thread = thread;
1158         this->edit = edit;
1159 }
1160 ChannelEditEditInput::~ChannelEditEditInput()
1161 {
1162 }
1163 void ChannelEditEditInput::add_items()
1164 {
1165         ArrayList<Channel*> *inputs;
1166         inputs = edit->channel_picker->get_video_inputs();
1167
1168         if(inputs)
1169         {
1170                 for(int i = 0; i < inputs->total; i++)
1171                 {
1172                         add_item(new ChannelEditEditInputItem(thread,
1173                                 edit,
1174                                 inputs->values[i]->device_name,
1175                                 i));
1176                 }
1177         }
1178 }
1179 int ChannelEditEditInput::handle_event()
1180 {
1181         return 0;
1182 }
1183
1184 ChannelEditEditInputItem::ChannelEditEditInputItem(ChannelEditEditThread *thread,
1185         ChannelEditThread *edit,
1186         char *text,
1187         int value)
1188  : BC_MenuItem(text)
1189 {
1190         this->thread = thread;
1191         this->edit = edit;
1192         this->value = value;
1193 }
1194 ChannelEditEditInputItem::~ChannelEditEditInputItem()
1195 {
1196 }
1197 int ChannelEditEditInputItem::handle_event()
1198 {
1199         get_popup_menu()->set_text(get_text());
1200         if(thread && !thread->user_title)
1201         {
1202                 strcpy(thread->new_channel.title, get_text());
1203                 if(thread->edit_window->title_text)
1204                 {
1205                         thread->edit_window->title_text->update(get_text());
1206                 }
1207         }
1208         if(thread)
1209                 thread->set_input(value);
1210         else
1211                 edit->scan_params.input = value;
1212         return 1;
1213 }
1214
1215 ChannelEditEditNorm::ChannelEditEditNorm(int x,
1216         int y,
1217         ChannelEditEditThread *thread,
1218         ChannelEditThread *edit)
1219  : BC_PopupMenu(x,
1220         y,
1221         100,
1222         edit->value_to_norm(thread ? thread->new_channel.norm : edit->scan_params.norm))
1223 {
1224         this->thread = thread;
1225         this->edit = edit;
1226 }
1227 ChannelEditEditNorm::~ChannelEditEditNorm()
1228 {
1229 }
1230 void ChannelEditEditNorm::add_items()
1231 {
1232         add_item(new ChannelEditEditNormItem(thread,
1233                 edit,
1234                 edit->value_to_norm(NTSC), NTSC));
1235         add_item(new ChannelEditEditNormItem(thread,
1236                 edit,
1237                 edit->value_to_norm(PAL), PAL));
1238         add_item(new ChannelEditEditNormItem(thread,
1239                 edit,
1240                 edit->value_to_norm(SECAM), SECAM));
1241 }
1242
1243
1244 ChannelEditEditNormItem::ChannelEditEditNormItem(ChannelEditEditThread *thread,
1245         ChannelEditThread *edit,
1246         char *text,
1247         int value)
1248  : BC_MenuItem(text)
1249 {
1250         this->value = value;
1251         this->edit = edit;
1252         this->thread = thread;
1253 }
1254 ChannelEditEditNormItem::~ChannelEditEditNormItem()
1255 {
1256 }
1257 int ChannelEditEditNormItem::handle_event()
1258 {
1259         get_popup_menu()->set_text(get_text());
1260         if(thread)
1261                 thread->set_norm(value);
1262         else
1263                 edit->scan_params.norm = value;
1264         return 1;
1265 }
1266
1267
1268 ChannelEditEditFreqtable::ChannelEditEditFreqtable(int x,
1269         int y,
1270         ChannelEditEditThread *thread,
1271         ChannelEditThread *edit)
1272  : BC_PopupMenu(x,
1273         y,
1274         150,
1275         edit->value_to_freqtable(thread ? thread->new_channel.freqtable : edit->scan_params.freqtable))
1276 {
1277         this->thread = thread;
1278         this->edit = edit;
1279 }
1280 ChannelEditEditFreqtable::~ChannelEditEditFreqtable()
1281 {
1282 }
1283 void ChannelEditEditFreqtable::add_items()
1284 {
1285         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(NTSC_DVB), NTSC_DVB));
1286         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(CATV_DVB), CATV_DVB));
1287         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(NTSC_BCAST), NTSC_BCAST));
1288         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(NTSC_CABLE), NTSC_CABLE));
1289         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(NTSC_HRC), NTSC_HRC));
1290         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(NTSC_BCAST_JP), NTSC_BCAST_JP));
1291         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(NTSC_CABLE_JP), NTSC_CABLE_JP));
1292         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(PAL_AUSTRALIA), PAL_AUSTRALIA));
1293         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(PAL_EUROPE), PAL_EUROPE));
1294         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(PAL_E_EUROPE), PAL_E_EUROPE));
1295         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(PAL_ITALY), PAL_ITALY));
1296         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(PAL_IRELAND), PAL_IRELAND));
1297         add_item(new ChannelEditEditFreqItem(thread, edit, edit->value_to_freqtable(PAL_NEWZEALAND), PAL_NEWZEALAND));
1298 }
1299
1300 ChannelEditEditFreqItem::ChannelEditEditFreqItem(ChannelEditEditThread *thread,
1301         ChannelEditThread *edit,
1302         char *text, int value)
1303  : BC_MenuItem(text)
1304 {
1305         this->value = value;
1306         this->edit = edit;
1307         this->thread = thread;
1308 }
1309 ChannelEditEditFreqItem::~ChannelEditEditFreqItem()
1310 {
1311 }
1312 int ChannelEditEditFreqItem::handle_event()
1313 {
1314         get_popup_menu()->set_text(get_text());
1315         if(thread)
1316                 thread->set_freqtable(value);
1317         else
1318                 edit->scan_params.freqtable = value;
1319         return 1;
1320 }
1321
1322
1323
1324 ChannelEditEditFine::ChannelEditEditFine(int x,
1325         int y,
1326         ChannelEditEditThread *thread)
1327  : BC_ISlider(x,
1328                 y,
1329                 0,
1330                 240,
1331                 240,
1332                 -100,
1333                 100,
1334                 thread->new_channel.fine_tune)
1335 {
1336         this->thread = thread;
1337 }
1338 ChannelEditEditFine::~ChannelEditEditFine()
1339 {
1340 }
1341 int ChannelEditEditFine::handle_event()
1342 {
1343         return 1;
1344 }
1345 int ChannelEditEditFine::button_release_event()
1346 {
1347         if(BC_Slider::button_release_event())
1348         {
1349                 thread->new_channel.fine_tune = get_value();
1350                 thread->set_device();
1351                 return 1;
1352         }
1353         return 0;
1354 }
1355
1356
1357 // ========================== picture quality
1358
1359 ChannelEditPictureThread::ChannelEditPictureThread(
1360         ChannelPicker *channel_picker)
1361  : BC_DialogThread()
1362 {
1363         this->channel_picker = channel_picker;
1364 }
1365 ChannelEditPictureThread::~ChannelEditPictureThread()
1366 {
1367 }
1368
1369 void ChannelEditPictureThread::handle_done_event(int result)
1370 {
1371         if(channel_picker->get_picture_usage())
1372                 channel_picker->get_picture_usage()->save_defaults();
1373 }
1374
1375 BC_Window* ChannelEditPictureThread::new_gui()
1376 {
1377         ChannelEditPictureWindow *edit_window = new ChannelEditPictureWindow(this,
1378                 channel_picker);
1379         edit_window->create_objects();
1380         return edit_window;
1381 }
1382
1383 void ChannelEditPictureThread::edit_picture()
1384 {
1385         start();
1386 }
1387
1388
1389 ChannelEditPictureWindow::ChannelEditPictureWindow(ChannelEditPictureThread *thread,
1390         ChannelPicker *channel_picker)
1391  : BC_Window(PROGRAM_NAME ": Picture",
1392         channel_picker->mwindow->session->picture_x,
1393         channel_picker->mwindow->session->picture_y,
1394         calculate_w(channel_picker),
1395         calculate_h(channel_picker),
1396         calculate_w(channel_picker),
1397         calculate_h(channel_picker))
1398 {
1399         this->thread = thread;
1400         this->channel_picker = channel_picker;
1401 }
1402 ChannelEditPictureWindow::~ChannelEditPictureWindow()
1403 {
1404 }
1405
1406 int ChannelEditPictureWindow::calculate_h(ChannelPicker *channel_picker)
1407 {
1408         PictureConfig *picture_usage = channel_picker->get_picture_usage();
1409         int pad = BC_Pot::calculate_h();
1410         int result = 20 +
1411                 channel_picker->parent_window->get_text_height(MEDIUMFONT) + 5 +
1412                 BC_OKButton::calculate_h();
1413
1414 // Only used for Video4Linux 1
1415         if(picture_usage)
1416         {
1417                 if(picture_usage->use_brightness)
1418                         result += pad;
1419                 if(picture_usage->use_contrast)
1420                         result += pad;
1421                 if(picture_usage->use_color)
1422                         result += pad;
1423                 if(picture_usage->use_hue)
1424                         result += pad;
1425                 if(picture_usage->use_whiteness)
1426                         result += pad;
1427         }
1428
1429         result += channel_picker->get_controls() * pad;
1430         return result;
1431 }
1432
1433 int ChannelEditPictureWindow::calculate_w(ChannelPicker *channel_picker)
1434 {
1435         PictureConfig *picture_usage = channel_picker->get_picture_usage();
1436         int widget_border = ((Theme*)channel_picker->get_theme())->widget_border;
1437         int pad = BC_Pot::calculate_w() + 4 * widget_border;
1438         int result = 0;
1439
1440 // Only used for Video4Linux 1
1441         if(!picture_usage ||
1442                 (!picture_usage->use_brightness &&
1443                 !picture_usage->use_contrast &&
1444                 !picture_usage->use_color &&
1445                 !picture_usage->use_hue &&
1446                 !picture_usage->use_whiteness &&
1447                 !channel_picker->get_controls()))
1448         {
1449                 result = BC_Title::calculate_w(channel_picker->parent_window,
1450                         "Device has no picture controls." +
1451                         2 * widget_border);
1452         }
1453
1454 // Only used for Video4Linux 1
1455         if(picture_usage)
1456         {
1457                 if(picture_usage->use_brightness)
1458                 {
1459                         int new_w = BC_Title::calculate_w(channel_picker->parent_window, "Brightness:") + pad;
1460                         result = MAX(result, new_w);
1461                 }
1462                 if(picture_usage->use_contrast)
1463                 {
1464                         int new_w = BC_Title::calculate_w(channel_picker->parent_window, "Contrast:") + pad;
1465                         result = MAX(result, new_w);
1466                 }
1467                 if(picture_usage->use_color)
1468                 {
1469                         int new_w = BC_Title::calculate_w(channel_picker->parent_window, "Color:") + pad;
1470                         result = MAX(result, new_w);
1471                 }
1472                 if(picture_usage->use_hue)
1473                 {
1474                         int new_w = BC_Title::calculate_w(channel_picker->parent_window, "Hue:") + pad;
1475                         result = MAX(result, new_w);
1476                 }
1477                 if(picture_usage->use_whiteness)
1478                 {
1479                         int new_w = BC_Title::calculate_w(channel_picker->parent_window, "Whiteness:") + pad;
1480                         result = MAX(result, new_w);
1481                 }
1482         }
1483
1484         for(int i = 0; i < channel_picker->get_controls(); i++)
1485         {
1486                 int new_w = BC_Title::calculate_w(channel_picker->parent_window,
1487                                 channel_picker->get_control(i)->name) +
1488                         pad;
1489                 result = MAX(result, new_w);
1490         }
1491
1492         return result;
1493 }
1494
1495
1496 void ChannelEditPictureWindow::create_objects()
1497 {
1498         int x = 10, y = 10;
1499         int widget_border = ((Theme*)channel_picker->get_theme())->widget_border;
1500         int x1 = get_w() - BC_Pot::calculate_w() * 2 - widget_border * 2;
1501         int x2 = get_w() - BC_Pot::calculate_w() - widget_border;
1502         int pad = BC_Pot::calculate_h();
1503
1504 #define SWAP_X x1 ^= x2; x2 ^= x1; x1 ^= x2;
1505
1506         PictureConfig *picture_usage = channel_picker->get_picture_usage();
1507
1508 // Only used for Video4Linux 1
1509         if(!picture_usage ||
1510                 (!picture_usage->use_brightness &&
1511                 !picture_usage->use_contrast &&
1512                 !picture_usage->use_color &&
1513                 !picture_usage->use_hue &&
1514                 !picture_usage->use_whiteness &&
1515                 !channel_picker->get_controls()))
1516         {
1517                 add_subwindow(new BC_Title(x, y, "Device has no picture controls."));
1518                 y += 50;
1519         }
1520
1521 // Only used for Video4Linux 1
1522         if(picture_usage && picture_usage->use_brightness)
1523         {
1524                 add_subwindow(new BC_Title(x, y + 10, _("Brightness:")));
1525                 add_subwindow(new ChannelEditBright(x1, y, channel_picker, channel_picker->get_brightness()));
1526                 y += pad;
1527                 SWAP_X
1528
1529         }
1530
1531         if(picture_usage && picture_usage->use_contrast)
1532         {
1533                 add_subwindow(new BC_Title(x, y + 10, _("Contrast:")));
1534                 add_subwindow(new ChannelEditContrast(x1, y, channel_picker, channel_picker->get_contrast()));
1535                 y += pad;
1536                 SWAP_X
1537         }
1538
1539         if(picture_usage && picture_usage->use_color)
1540         {
1541                 add_subwindow(new BC_Title(x, y + 10, _("Color:")));
1542                 add_subwindow(new ChannelEditColor(x1, y, channel_picker, channel_picker->get_color()));
1543                 y += pad;
1544                 SWAP_X
1545         }
1546
1547         if(picture_usage && picture_usage->use_hue)
1548         {
1549                 add_subwindow(new BC_Title(x, y + 10, _("Hue:")));
1550                 add_subwindow(new ChannelEditHue(x1, y, channel_picker, channel_picker->get_hue()));
1551                 y += pad;
1552                 SWAP_X
1553         }
1554
1555         if(picture_usage && picture_usage->use_whiteness)
1556         {
1557                 add_subwindow(new BC_Title(x, y + 10, _("Whiteness:")));
1558                 add_subwindow(new ChannelEditWhiteness(x1, y, channel_picker, channel_picker->get_whiteness()));
1559                 y += pad;
1560                 SWAP_X
1561         }
1562
1563         for(int i = 0; i < channel_picker->get_controls(); i++)
1564         {
1565                 BC_Title *title;
1566                 add_subwindow(title = new BC_Title(x,
1567                         y + 10,
1568                         _(channel_picker->get_control(i)->name)));
1569
1570                 int x3 = x1;
1571                 if(x3 < title->get_x() + title->get_w() + widget_border)
1572                         x3 = title->get_x() + title->get_w() + widget_border;
1573
1574                 add_subwindow(new ChannelEditCommon(x3,
1575                         y,
1576                         channel_picker,
1577                         channel_picker->get_control(i)));
1578                 y += pad;
1579                 SWAP_X
1580         }
1581
1582
1583         y += pad;
1584         add_subwindow(new BC_OKButton(this));
1585         show_window();
1586 }
1587
1588 int ChannelEditPictureWindow::translation_event()
1589 {
1590         channel_picker->mwindow->session->picture_x = get_x();
1591         channel_picker->mwindow->session->picture_y = get_y();
1592         return 0;
1593 }
1594
1595
1596
1597 ChannelEditBright::ChannelEditBright(int x, int y, ChannelPicker *channel_picker, int value)
1598  : BC_IPot(x,
1599                 y,
1600                 value,
1601                 -100,
1602                 100)
1603 {
1604         this->channel_picker = channel_picker;
1605 }
1606 ChannelEditBright::~ChannelEditBright() {}
1607 int ChannelEditBright::handle_event()
1608 {
1609         return 1;
1610 }
1611 int ChannelEditBright::button_release_event()
1612 {
1613         if(BC_Pot::button_release_event())
1614         {
1615                 channel_picker->set_brightness(get_value());
1616                 return 1;
1617         }
1618         return 0;
1619 }
1620
1621 ChannelEditContrast::ChannelEditContrast(int x, int y, ChannelPicker *channel_picker, int value)
1622  : BC_IPot(x,
1623                 y,
1624                 value,
1625                 -100,
1626                 100)
1627 {
1628         this->channel_picker = channel_picker;
1629 }
1630 ChannelEditContrast::~ChannelEditContrast() {}
1631 int ChannelEditContrast::handle_event()
1632 {
1633         return 1;
1634 }
1635 int ChannelEditContrast::button_release_event()
1636 {
1637         if(BC_Pot::button_release_event())
1638         {
1639                 channel_picker->set_contrast(get_value());
1640                 return 1;
1641         }
1642         return 0;
1643 }
1644
1645
1646 ChannelEditColor::ChannelEditColor(int x, int y, ChannelPicker *channel_picker, int value)
1647  : BC_IPot(x,
1648                 y,
1649                 value,
1650                 -100,
1651                 100)
1652 {
1653         this->channel_picker = channel_picker;
1654 }
1655 ChannelEditColor::~ChannelEditColor() {}
1656 int ChannelEditColor::handle_event()
1657 {
1658         return 1;
1659 }
1660 int ChannelEditColor::button_release_event()
1661 {
1662         if(BC_Pot::button_release_event())
1663         {
1664                 channel_picker->set_color(get_value());
1665                 return 1;
1666         }
1667         return 0;
1668 }
1669
1670 ChannelEditHue::ChannelEditHue(int x, int y, ChannelPicker *channel_picker, int value)
1671  : BC_IPot(x,
1672                 y,
1673                 value,
1674                 -100,
1675                 100)
1676 {
1677         this->channel_picker = channel_picker;
1678 }
1679 ChannelEditHue::~ChannelEditHue() {}
1680 int ChannelEditHue::handle_event()
1681 {
1682         return 1;
1683 }
1684 int ChannelEditHue::button_release_event()
1685 {
1686         if(BC_Pot::button_release_event())
1687         {
1688                 channel_picker->set_hue(get_value());
1689                 return 1;
1690         }
1691         return 0;
1692 }
1693
1694 ChannelEditWhiteness::ChannelEditWhiteness(int x, int y, ChannelPicker *channel_picker, int value)
1695  : BC_IPot(x,
1696                 y,
1697                 value,
1698                 -100,
1699                 100)
1700 {
1701         this->channel_picker = channel_picker;
1702 }
1703 ChannelEditWhiteness::~ChannelEditWhiteness()
1704 {
1705 }
1706 int ChannelEditWhiteness::handle_event()
1707 {
1708         return 1;
1709 }
1710 int ChannelEditWhiteness::button_release_event()
1711 {
1712         if(BC_Pot::button_release_event())
1713         {
1714                 channel_picker->set_whiteness(get_value());
1715                 return 1;
1716         }
1717         return 0;
1718 }
1719
1720
1721
1722 ChannelEditCommon::ChannelEditCommon(int x,
1723         int y,
1724         ChannelPicker *channel_picker,
1725         PictureItem *item)
1726  : BC_IPot(x,
1727                 y,
1728                 item->value,
1729                 item->min,
1730                 item->max)
1731 {
1732         this->channel_picker = channel_picker;
1733         this->device_id = item->device_id;
1734 }
1735
1736 ChannelEditCommon::~ChannelEditCommon()
1737 {
1738 }
1739
1740 int ChannelEditCommon::handle_event()
1741 {
1742         return 1;
1743 }
1744
1745 int ChannelEditCommon::button_release_event()
1746 {
1747         if(BC_Pot::button_release_event())
1748         {
1749                 channel_picker->set_picture(device_id, get_value());
1750                 return 1;
1751         }
1752         return 0;
1753 }
1754
1755 int ChannelEditCommon::keypress_event()
1756 {
1757         if(BC_Pot::keypress_event())
1758         {
1759                 channel_picker->set_picture(device_id, get_value());
1760                 return 1;
1761         }
1762         return 0;
1763 }
1764
1765
1766
1767
1768