add missing GPL information in guicast program files
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / probeprefs.C
1 #include "bcwindowbase.h"
2 #include "bcdisplayinfo.h"
3 #include "bcdialog.h"
4 #include "language.h"
5 #include "mainerror.h"
6 #include "mwindow.h"
7 #include "probeprefs.h"
8 #include "preferences.h"
9 #include "preferencesthread.h"
10 #include "theme.h"
11
12 #include <sys/wait.h>
13
14 ProbePref::ProbePref(const char *nm, int armed)
15 {
16         strncpy(name, nm, sizeof(name));
17         this->armed = armed;
18 }
19
20 ProbePref::~ProbePref()
21 {
22 }
23
24 FileProbeDialog::FileProbeDialog(PreferencesWindow *pwindow)
25  : BC_DialogThread()
26 {
27         this->pwindow = pwindow;
28 }
29
30 FileProbeDialog::~FileProbeDialog()
31 {
32         close_window();
33 }
34
35 BC_Window* FileProbeDialog::new_gui()
36 {
37         BC_DisplayInfo display_info;
38         int x = display_info.get_abs_cursor_x();
39         int y = display_info.get_abs_cursor_y();
40
41         pb_window = new ProbeEditWindow(this, x, y);
42         pb_window->create_objects();
43         return pb_window;
44 }
45
46 void FileProbeDialog::handle_close_event(int result)
47 {
48         pb_window = 0;
49 }
50
51
52 ProbeEditWindow::ProbeEditWindow(FileProbeDialog *pb_dialog, int x, int y)
53  : BC_Window(_(PROGRAM_NAME ": Probes"), x, y, xS(300), yS(200), xS(300), yS(200), 0, 0, 1)
54 {
55         this->pb_dialog = pb_dialog;
56         probe_list = 0;
57         probe_up_button = 0;
58         probe_down_button = 0;
59         probe_enabled = 0;
60         pb_enabled = 0;
61         pb_disabled = 0;
62 // *** CONTEXT_HELP ***
63         context_help_set_keyword("Probe Order when Loading Media");
64 }
65
66 ProbeEditWindow::~ProbeEditWindow()
67 {
68         delete pb_enabled;
69         delete pb_disabled;
70 }
71
72 void ProbeEditWindow::create_objects()
73 {
74         int xs10 = xS(10), ys10 = yS(10);
75         lock_window("ProbeEditWindow::create_objects");
76         pb_enabled = new BC_Pixmap(this,
77                 BC_WindowBase::get_resources()->listbox_up,
78                 PIXMAP_ALPHA);
79         pb_disabled = new BC_Pixmap(this,
80                 BC_WindowBase::get_resources()->listbox_dn,
81                 PIXMAP_ALPHA);
82         Preferences *preferences = pb_dialog->pwindow->thread->preferences;
83         for( int i=0; i<preferences->file_probes.size(); ++i ) {
84                 probe_items.append(new ProbePrefItem(this, preferences->file_probes[i]));
85         }
86         int x = xs10, y = ys10;
87         add_subwindow(probe_list = new ProbePrefList(this, x, y));
88         y += probe_list->get_h();
89         int x1 = x, y1 = y;
90         add_subwindow(probe_up_button = new ProbeUpButton(this, x1, y1));
91         x1 += probe_up_button->get_w() + xs10;
92         add_subwindow(probe_down_button = new ProbeDownButton(this, x1, y1));
93         x1 += probe_down_button->get_w() + xs10;
94         add_subwindow(probe_enabled = new ProbeEnabled(this, x1, y1));
95         probe_enabled->disable();
96
97         add_subwindow(new ProbeEditOK(this));
98         add_subwindow(new BC_CancelButton(this));
99
100         list_update();
101         show_window();
102         unlock_window();
103 }
104
105 ProbeEditOK::ProbeEditOK(ProbeEditWindow *pb_window)
106  : BC_OKButton(pb_window)
107 {
108         this->pb_window = pb_window;
109 }
110
111 ProbeEditOK::~ProbeEditOK()
112 {
113 }
114
115 int ProbeEditOK::handle_event()
116 {
117         Preferences *preferences = pb_window->pb_dialog->pwindow->thread->preferences;
118         ArrayList<ProbePref *> &file_probes = preferences->file_probes;
119         file_probes.remove_all_objects();
120         ArrayList<ProbePrefItem *> &probe_items = pb_window->probe_items;
121         for( int i=0; i<probe_items.size(); ++i ) {
122                 ProbePrefItem *item = probe_items[i];
123                 file_probes.append(new ProbePref(item->get_text(), item->armed));
124         }
125         return BC_OKButton::handle_event();
126 }
127
128 int ProbeEditWindow::list_update()
129 {
130 //for( int i=0; i<probe_items.size(); ++i ) printf("%s, ",probe_items[i]->get_text()); printf("\n");
131         probe_list->update((ArrayList<BC_ListBoxItem*> *)&probe_items, 0, 0, 1,
132                 probe_list->get_xposition(), probe_list->get_yposition(),
133                 probe_list->get_highlighted_item(), 1, 0);
134         probe_list->center_selection();
135         return 1;
136 }
137
138 ProbeUpButton::ProbeUpButton(ProbeEditWindow *pb_window, int x, int y)
139  : BC_GenericButton(x, y, _("Up"))
140 {
141         this->pb_window = pb_window;
142 }
143
144 ProbeUpButton::~ProbeUpButton()
145 {
146 }
147
148 int ProbeUpButton::handle_event()
149 {
150         ArrayList<ProbePrefItem *> &probe_items = pb_window->probe_items;
151         int n = probe_items.size();
152         if( n > 1 ) {
153                 ProbePrefItem **pitem = &probe_items[0], *item0 = *pitem;
154                 for( int i=1; i<n; ++i ) {
155                         ProbePrefItem *&item = probe_items[i];
156                         if( item->get_selected() ) {
157                                 ProbePrefItem *t = item;  item = *pitem; *pitem = t;
158                         }
159                         pitem = &item;
160                 }
161                 if( item0->get_selected() ) {
162                         ProbePrefItem *t = *pitem;  *pitem = item0;  probe_items[0] = t;
163                 }
164         }
165         pb_window->list_update();
166         return 1;
167 }
168
169 ProbeDownButton::ProbeDownButton(ProbeEditWindow *pb_window, int x, int y)
170  : BC_GenericButton(x, y, _("Down"))
171 {
172         this->pb_window = pb_window;
173 }
174
175 ProbeDownButton::~ProbeDownButton()
176 {
177 }
178
179 ProbeEnabled::ProbeEnabled(ProbeEditWindow *pb_window, int x, int y)
180  : BC_CheckBox(x, y, 0, _("Enabled"))
181 {
182         this->pb_window = pb_window;
183 }
184 ProbeEnabled::~ProbeEnabled()
185 {
186 }
187
188 int ProbeEnabled::handle_event()
189 {
190         ProbePrefItem *item = (ProbePrefItem *)pb_window->probe_list->get_selection(0,0);
191         if( item ) {
192                 item->set_armed(get_value());
193                 pb_window->list_update();
194         }
195         return 1;
196 }
197
198 int ProbeDownButton::handle_event()
199 {
200         ArrayList<ProbePrefItem *> &probe_items = pb_window->probe_items;
201         int n = probe_items.size();
202         if( n > 1 ) {
203                 ProbePrefItem **pitem = &probe_items[n-1], *item1 = *pitem;
204                 for( int i=n-1; --i>=0; ) {
205                         ProbePrefItem *&item = probe_items[i];
206                         if( item->get_selected() ) {
207                                 ProbePrefItem *t = item;  item = *pitem; *pitem = t;
208                         }
209                         pitem = &item;
210                 }
211                 if( item1->get_selected() ) {
212                         ProbePrefItem *t = *pitem;  *pitem = item1;  probe_items[n-1] = t;
213                 }
214         }
215         pb_window->list_update();
216         return 1;
217 }
218
219 ProbePrefItem::ProbePrefItem(ProbeEditWindow *pb_window, ProbePref *pref)
220  : BC_ListBoxItem(pref->name, pref->armed ?
221                 pb_window->pb_enabled : pb_window->pb_disabled)
222 {
223         this->pb_window = pb_window;
224         this->armed = pref->armed;
225 }
226
227 ProbePrefItem::~ProbePrefItem()
228 {
229 }
230
231 void ProbePrefItem::set_armed(int armed)
232 {
233         this->armed = armed;
234         set_icon(armed ? pb_window->pb_enabled : pb_window->pb_disabled);
235 }
236
237 ProbePrefList::ProbePrefList(ProbeEditWindow *pb_window, int x, int y)
238  : BC_ListBox(x, y, pb_window->get_w()-x-xS(10), pb_window->get_h()-y-yS(80),
239         LISTBOX_ICON_LIST, (ArrayList<BC_ListBoxItem *>*) &pb_window->probe_items,
240         0, 0)
241 {
242         this->pb_window = pb_window;
243 }
244
245 ProbePrefList::~ProbePrefList()
246 {
247 }
248
249 int ProbePrefList::selection_changed()
250 {
251         ProbePrefItem *item = (ProbePrefItem *)pb_window->probe_list->get_selection(0,0);
252         if( item ) {
253                 pb_window->probe_enabled->set_value(item->armed);
254                 pb_window->probe_enabled->enable();
255         }
256         else {
257                 pb_window->probe_enabled->set_value(0);
258                 pb_window->probe_enabled->disable();
259         }
260         pb_window->list_update();
261         return 1;
262 }
263
264 int ProbePrefList::handle_event()
265 {
266         if( get_double_click() && get_buttonpress() == 1 ) {
267                 ProbePrefItem *item = (ProbePrefItem *)get_selection(0,0);
268                 int armed = item->armed ? 0 : 1;
269                 pb_window->probe_enabled->enable();
270                 pb_window->probe_enabled->set_value(armed);
271                 item->set_armed(armed);
272                 pb_window->list_update();
273         }
274         return 1;
275 }
276