Credit Andrew - improve in-tree documentation
[goodguy/cinelerra.git] / 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, 300, 200, 300, 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 }
63
64 ProbeEditWindow::~ProbeEditWindow()
65 {
66         delete pb_enabled;
67         delete pb_disabled;
68 }
69
70 void ProbeEditWindow::create_objects()
71 {
72         lock_window("ProbeEditWindow::create_objects");
73         pb_enabled = new BC_Pixmap(this,
74                 BC_WindowBase::get_resources()->listbox_up,
75                 PIXMAP_ALPHA);
76         pb_disabled = new BC_Pixmap(this,
77                 BC_WindowBase::get_resources()->listbox_dn,
78                 PIXMAP_ALPHA);
79         Preferences *preferences = pb_dialog->pwindow->thread->preferences;
80         for( int i=0; i<preferences->file_probes.size(); ++i ) {
81                 probe_items.append(new ProbePrefItem(this, preferences->file_probes[i]));
82         }
83         int x = 10, y = 10;
84         add_subwindow(probe_list = new ProbePrefList(this, x, y));
85         y += probe_list->get_h();
86         int x1 = x, y1 = y;
87         add_subwindow(probe_up_button = new ProbeUpButton(this, x1, y1));
88         x1 += probe_up_button->get_w() + 10;
89         add_subwindow(probe_down_button = new ProbeDownButton(this, x1, y1));
90         x1 += probe_down_button->get_w() + 10;
91         add_subwindow(probe_enabled = new ProbeEnabled(this, x1, y1));
92         probe_enabled->disable();
93
94         add_subwindow(new ProbeEditOK(this));
95         add_subwindow(new BC_CancelButton(this));
96
97         list_update();
98         show_window();
99         unlock_window();
100 }
101
102 ProbeEditOK::ProbeEditOK(ProbeEditWindow *pb_window)
103  : BC_OKButton(pb_window)
104 {
105         this->pb_window = pb_window;
106 }
107
108 ProbeEditOK::~ProbeEditOK()
109 {
110 }
111
112 int ProbeEditOK::handle_event()
113 {
114         Preferences *preferences = pb_window->pb_dialog->pwindow->thread->preferences;
115         ArrayList<ProbePref *> &file_probes = preferences->file_probes;
116         file_probes.remove_all_objects();
117         ArrayList<ProbePrefItem *> &probe_items = pb_window->probe_items;
118         for( int i=0; i<probe_items.size(); ++i ) {
119                 ProbePrefItem *item = probe_items[i];
120                 file_probes.append(new ProbePref(item->get_text(), item->armed));
121         }
122         return BC_OKButton::handle_event();
123 }
124
125 int ProbeEditWindow::list_update()
126 {
127 //for( int i=0; i<probe_items.size(); ++i ) printf("%s, ",probe_items[i]->get_text()); printf("\n");
128         probe_list->update((ArrayList<BC_ListBoxItem*> *)&probe_items, 0, 0, 1,
129                 probe_list->get_xposition(), probe_list->get_yposition(),
130                 probe_list->get_highlighted_item(), 1, 0);
131         probe_list->center_selection();
132         return 1;
133 }
134
135 ProbeUpButton::ProbeUpButton(ProbeEditWindow *pb_window, int x, int y)
136  : BC_GenericButton(x, y, _("Up"))
137 {
138         this->pb_window = pb_window;
139 }
140
141 ProbeUpButton::~ProbeUpButton()
142 {
143 }
144
145 int ProbeUpButton::handle_event()
146 {
147         ArrayList<ProbePrefItem *> &probe_items = pb_window->probe_items;
148         int n = probe_items.size();
149         if( n > 1 ) {
150                 ProbePrefItem **pitem = &probe_items[0], *item0 = *pitem;
151                 for( int i=1; i<n; ++i ) {
152                         ProbePrefItem *&item = probe_items[i];
153                         if( item->get_selected() ) {
154                                 ProbePrefItem *t = item;  item = *pitem; *pitem = t;
155                         }
156                         pitem = &item;
157                 }
158                 if( item0->get_selected() ) {
159                         ProbePrefItem *t = *pitem;  *pitem = item0;  probe_items[0] = t;
160                 }
161         }
162         pb_window->list_update();
163         return 1;
164 }
165
166 ProbeDownButton::ProbeDownButton(ProbeEditWindow *pb_window, int x, int y)
167  : BC_GenericButton(x, y, _("Down"))
168 {
169         this->pb_window = pb_window;
170 }
171
172 ProbeDownButton::~ProbeDownButton()
173 {
174 }
175
176 ProbeEnabled::ProbeEnabled(ProbeEditWindow *pb_window, int x, int y)
177  : BC_CheckBox(x, y, 0, _("Enabled"))
178 {
179         this->pb_window = pb_window;
180 }
181 ProbeEnabled::~ProbeEnabled()
182 {
183 }
184
185 int ProbeEnabled::handle_event()
186 {
187         ProbePrefItem *item = (ProbePrefItem *)pb_window->probe_list->get_selection(0,0);
188         if( item ) {
189                 item->set_armed(get_value());
190                 pb_window->list_update();
191         }
192         return 1;
193 }
194
195 int ProbeDownButton::handle_event()
196 {
197         ArrayList<ProbePrefItem *> &probe_items = pb_window->probe_items;
198         int n = probe_items.size();
199         if( n > 1 ) {
200                 ProbePrefItem **pitem = &probe_items[n-1], *item1 = *pitem;
201                 for( int i=n-1; --i>=0; ) {
202                         ProbePrefItem *&item = probe_items[i];
203                         if( item->get_selected() ) {
204                                 ProbePrefItem *t = item;  item = *pitem; *pitem = t;
205                         }
206                         pitem = &item;
207                 }
208                 if( item1->get_selected() ) {
209                         ProbePrefItem *t = *pitem;  *pitem = item1;  probe_items[n-1] = t;
210                 }
211         }
212         pb_window->list_update();
213         return 1;
214 }
215
216 ProbePrefItem::ProbePrefItem(ProbeEditWindow *pb_window, ProbePref *pref)
217  : BC_ListBoxItem(pref->name, pref->armed ?
218                 pb_window->pb_enabled : pb_window->pb_disabled)
219 {
220         this->pb_window = pb_window;
221         this->armed = pref->armed;
222 }
223
224 ProbePrefItem::~ProbePrefItem()
225 {
226 }
227
228 void ProbePrefItem::set_armed(int armed)
229 {
230         this->armed = armed;
231         set_icon(armed ? pb_window->pb_enabled : pb_window->pb_disabled);
232 }
233
234 ProbePrefList::ProbePrefList(ProbeEditWindow *pb_window, int x, int y)
235  : BC_ListBox(x, y, pb_window->get_w()-x-10, pb_window->get_h()-y-80, LISTBOX_ICON_LIST,
236         (ArrayList<BC_ListBoxItem *>*) &pb_window->probe_items, 0, 0)
237 {
238         this->pb_window = pb_window;
239 }
240
241 ProbePrefList::~ProbePrefList()
242 {
243 }
244
245 int ProbePrefList::selection_changed()
246 {
247         ProbePrefItem *item = (ProbePrefItem *)pb_window->probe_list->get_selection(0,0);
248         if( item ) {
249                 pb_window->probe_enabled->set_value(item->armed);
250                 pb_window->probe_enabled->enable();
251         }
252         else {
253                 pb_window->probe_enabled->set_value(0);
254                 pb_window->probe_enabled->disable();
255         }
256         pb_window->list_update();
257         return 1;
258 }
259
260 int ProbePrefList::handle_event()
261 {
262         if( get_double_click() && get_buttonpress() == 1 ) {
263                 ProbePrefItem *item = (ProbePrefItem *)get_selection(0,0);
264                 int armed = item->armed ? 0 : 1;
265                 pb_window->probe_enabled->enable();
266                 pb_window->probe_enabled->set_value(armed);
267                 item->set_armed(armed);
268                 pb_window->list_update();
269         }
270         return 1;
271 }
272