Credit ffmpeg team with upgrade to 6.1
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / probeprefs.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2016-2020 William Morrow
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #include "bcwindowbase.h"
22 #include "bcdisplayinfo.h"
23 #include "bcdialog.h"
24 #include "language.h"
25 #include "mainerror.h"
26 #include "mwindow.h"
27 #include "probeprefs.h"
28 #include "preferences.h"
29 #include "preferencesthread.h"
30 #include "theme.h"
31
32 #include <sys/wait.h>
33
34 ProbePref::ProbePref(const char *nm, int armed)
35 {
36         strncpy(name, nm, sizeof(name));
37         this->armed = armed;
38 }
39
40 ProbePref::~ProbePref()
41 {
42 }
43
44 FileProbeDialog::FileProbeDialog(PreferencesWindow *pwindow)
45  : BC_DialogThread()
46 {
47         this->pwindow = pwindow;
48 }
49
50 FileProbeDialog::~FileProbeDialog()
51 {
52         close_window();
53 }
54
55 BC_Window* FileProbeDialog::new_gui()
56 {
57         BC_DisplayInfo display_info;
58         int x = display_info.get_abs_cursor_x();
59         int y = display_info.get_abs_cursor_y();
60
61         pb_window = new ProbeEditWindow(this, x, y);
62         pb_window->create_objects();
63         return pb_window;
64 }
65
66 void FileProbeDialog::handle_close_event(int result)
67 {
68         pb_window = 0;
69 }
70
71
72 ProbeEditWindow::ProbeEditWindow(FileProbeDialog *pb_dialog, int x, int y)
73  : BC_Window(_(PROGRAM_NAME ": Probes"), x, y, xS(300), yS(200), xS(300), yS(200), 0, 0, 1)
74 {
75         this->pb_dialog = pb_dialog;
76         probe_list = 0;
77         probe_up_button = 0;
78         probe_down_button = 0;
79         probe_enabled = 0;
80         pb_enabled = 0;
81         pb_disabled = 0;
82 // *** CONTEXT_HELP ***
83         context_help_set_keyword("Probe Order when Loading Media");
84 }
85
86 ProbeEditWindow::~ProbeEditWindow()
87 {
88         delete pb_enabled;
89         delete pb_disabled;
90 }
91
92 void ProbeEditWindow::create_objects()
93 {
94         int xs10 = xS(10), ys10 = yS(10);
95         lock_window("ProbeEditWindow::create_objects");
96         pb_enabled = new BC_Pixmap(this,
97                 BC_WindowBase::get_resources()->listbox_up,
98                 PIXMAP_ALPHA);
99         pb_disabled = new BC_Pixmap(this,
100                 BC_WindowBase::get_resources()->listbox_dn,
101                 PIXMAP_ALPHA);
102         Preferences *preferences = pb_dialog->pwindow->thread->preferences;
103         for( int i=0; i<preferences->file_probes.size(); ++i ) {
104                 probe_items.append(new ProbePrefItem(this, preferences->file_probes[i]));
105         }
106         int x = xs10, y = ys10;
107         add_subwindow(probe_list = new ProbePrefList(this, x, y));
108         y += probe_list->get_h();
109         int x1 = x, y1 = y;
110         add_subwindow(probe_up_button = new ProbeUpButton(this, x1, y1));
111         x1 += probe_up_button->get_w() + xs10;
112         add_subwindow(probe_down_button = new ProbeDownButton(this, x1, y1));
113         x1 += probe_down_button->get_w() + xs10;
114         add_subwindow(probe_enabled = new ProbeEnabled(this, x1, y1));
115         probe_enabled->disable();
116
117         add_subwindow(new ProbeEditOK(this));
118         add_subwindow(new BC_CancelButton(this));
119
120         list_update();
121         show_window();
122         unlock_window();
123 }
124
125 ProbeEditOK::ProbeEditOK(ProbeEditWindow *pb_window)
126  : BC_OKButton(pb_window)
127 {
128         this->pb_window = pb_window;
129 }
130
131 ProbeEditOK::~ProbeEditOK()
132 {
133 }
134
135 int ProbeEditOK::handle_event()
136 {
137         Preferences *preferences = pb_window->pb_dialog->pwindow->thread->preferences;
138         ArrayList<ProbePref *> &file_probes = preferences->file_probes;
139         file_probes.remove_all_objects();
140         ArrayList<ProbePrefItem *> &probe_items = pb_window->probe_items;
141         for( int i=0; i<probe_items.size(); ++i ) {
142                 ProbePrefItem *item = probe_items[i];
143                 file_probes.append(new ProbePref(item->get_text(), item->armed));
144         }
145         return BC_OKButton::handle_event();
146 }
147
148 int ProbeEditWindow::list_update()
149 {
150 //for( int i=0; i<probe_items.size(); ++i ) printf("%s, ",probe_items[i]->get_text()); printf("\n");
151         probe_list->update((ArrayList<BC_ListBoxItem*> *)&probe_items, 0, 0, 1,
152                 probe_list->get_xposition(), probe_list->get_yposition(),
153                 probe_list->get_highlighted_item(), 1, 0);
154         probe_list->center_selection();
155         return 1;
156 }
157
158 ProbeUpButton::ProbeUpButton(ProbeEditWindow *pb_window, int x, int y)
159  : BC_GenericButton(x, y, _("Up"))
160 {
161         this->pb_window = pb_window;
162 }
163
164 ProbeUpButton::~ProbeUpButton()
165 {
166 }
167
168 int ProbeUpButton::handle_event()
169 {
170         ArrayList<ProbePrefItem *> &probe_items = pb_window->probe_items;
171         int n = probe_items.size();
172         if( n > 1 ) {
173                 ProbePrefItem **pitem = &probe_items[0], *item0 = *pitem;
174                 for( int i=1; i<n; ++i ) {
175                         ProbePrefItem *&item = probe_items[i];
176                         if( item->get_selected() ) {
177                                 ProbePrefItem *t = item;  item = *pitem; *pitem = t;
178                         }
179                         pitem = &item;
180                 }
181                 if( item0->get_selected() ) {
182                         ProbePrefItem *t = *pitem;  *pitem = item0;  probe_items[0] = t;
183                 }
184         }
185         pb_window->list_update();
186         return 1;
187 }
188
189 ProbeDownButton::ProbeDownButton(ProbeEditWindow *pb_window, int x, int y)
190  : BC_GenericButton(x, y, _("Down"))
191 {
192         this->pb_window = pb_window;
193 }
194
195 ProbeDownButton::~ProbeDownButton()
196 {
197 }
198
199 ProbeEnabled::ProbeEnabled(ProbeEditWindow *pb_window, int x, int y)
200  : BC_CheckBox(x, y, 0, _("Enabled"))
201 {
202         this->pb_window = pb_window;
203 }
204 ProbeEnabled::~ProbeEnabled()
205 {
206 }
207
208 int ProbeEnabled::handle_event()
209 {
210         ProbePrefItem *item = (ProbePrefItem *)pb_window->probe_list->get_selection(0,0);
211         if( item ) {
212                 item->set_armed(get_value());
213                 pb_window->list_update();
214         }
215         return 1;
216 }
217
218 int ProbeDownButton::handle_event()
219 {
220         ArrayList<ProbePrefItem *> &probe_items = pb_window->probe_items;
221         int n = probe_items.size();
222         if( n > 1 ) {
223                 ProbePrefItem **pitem = &probe_items[n-1], *item1 = *pitem;
224                 for( int i=n-1; --i>=0; ) {
225                         ProbePrefItem *&item = probe_items[i];
226                         if( item->get_selected() ) {
227                                 ProbePrefItem *t = item;  item = *pitem; *pitem = t;
228                         }
229                         pitem = &item;
230                 }
231                 if( item1->get_selected() ) {
232                         ProbePrefItem *t = *pitem;  *pitem = item1;  probe_items[n-1] = t;
233                 }
234         }
235         pb_window->list_update();
236         return 1;
237 }
238
239 ProbePrefItem::ProbePrefItem(ProbeEditWindow *pb_window, ProbePref *pref)
240  : BC_ListBoxItem(pref->name, pref->armed ?
241                 pb_window->pb_enabled : pb_window->pb_disabled)
242 {
243         this->pb_window = pb_window;
244         this->armed = pref->armed;
245 }
246
247 ProbePrefItem::~ProbePrefItem()
248 {
249 }
250
251 void ProbePrefItem::set_armed(int armed)
252 {
253         this->armed = armed;
254         set_icon(armed ? pb_window->pb_enabled : pb_window->pb_disabled);
255 }
256
257 ProbePrefList::ProbePrefList(ProbeEditWindow *pb_window, int x, int y)
258  : BC_ListBox(x, y, pb_window->get_w()-x-xS(10), pb_window->get_h()-y-yS(80),
259         LISTBOX_ICON_LIST, (ArrayList<BC_ListBoxItem *>*) &pb_window->probe_items,
260         0, 0)
261 {
262         this->pb_window = pb_window;
263 }
264
265 ProbePrefList::~ProbePrefList()
266 {
267 }
268
269 int ProbePrefList::selection_changed()
270 {
271         ProbePrefItem *item = (ProbePrefItem *)pb_window->probe_list->get_selection(0,0);
272         if( item ) {
273                 pb_window->probe_enabled->set_value(item->armed);
274                 pb_window->probe_enabled->enable();
275         }
276         else {
277                 pb_window->probe_enabled->set_value(0);
278                 pb_window->probe_enabled->disable();
279         }
280         pb_window->list_update();
281         return 1;
282 }
283
284 int ProbePrefList::handle_event()
285 {
286         if( get_double_click() && get_buttonpress() == 1 ) {
287                 ProbePrefItem *item = (ProbePrefItem *)get_selection(0,0);
288                 int armed = item->armed ? 0 : 1;
289                 pb_window->probe_enabled->enable();
290                 pb_window->probe_enabled->set_value(armed);
291                 item->set_armed(armed);
292                 pb_window->list_update();
293         }
294         return 1;
295 }
296