add grouping, default proxy vcodec h264.mp4, default titlebar alpha=1, green bar bug
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / shbtnprefs.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 "shbtnprefs.h"
8 #include "preferences.h"
9 #include "preferencesthread.h"
10 #include "theme.h"
11
12 #include <sys/wait.h>
13
14 ShBtnRun::ShBtnRun(const char *nm, const char *cmds, int warn)
15  : Thread(0, 0, 1)
16 {
17         strncpy(name, nm, sizeof(name)-1);
18         name[sizeof(name)-1] = 0;
19         strncpy(commands, cmds, sizeof(commands)-1);
20         commands[sizeof(commands)-1] = 0;
21         this->warn = warn;
22         start();
23 }
24
25 void ShBtnRun::run()
26 {
27         pid_t pid = vfork();
28         if( pid < 0 ) {
29                 perror("fork");
30                 return;
31         }
32         if( pid > 0 ) {
33                 int stat;  waitpid(pid, &stat, 0);
34                 if( warn && stat ) {
35                         char msg[BCTEXTLEN];
36                         sprintf(msg, "%s: error exit status %d", name, stat);
37                         MainError::show_error(msg);
38                 }
39                 return;
40         }
41         char *const argv[4] = { (char*) "/bin/bash", (char*) "-c", commands, 0 };
42         execvp(argv[0], &argv[0]);
43 }
44
45 ShBtnPref::ShBtnPref(const char *nm, const char *cmds, int warn)
46 {
47         strncpy(name, nm, sizeof(name));
48         strncpy(commands, cmds, sizeof(commands));
49         this->warn = warn;
50 }
51
52 ShBtnPref::~ShBtnPref()
53 {
54 }
55
56 void ShBtnPref::execute()
57 {
58         new ShBtnRun(name, commands, warn);
59 }
60
61 ShBtnEditDialog::ShBtnEditDialog(PreferencesWindow *pwindow)
62  : BC_DialogThread()
63 {
64         this->pwindow = pwindow;
65 }
66
67 ShBtnEditDialog::~ShBtnEditDialog()
68 {
69         close_window();
70 }
71
72 BC_Window* ShBtnEditDialog::new_gui()
73 {
74         BC_DisplayInfo display_info;
75         int x = display_info.get_abs_cursor_x();
76         int y = display_info.get_abs_cursor_y();
77
78         sb_window = new ShBtnEditWindow(this, x, y);
79         sb_window->create_objects();
80         return sb_window;
81 }
82
83 void ShBtnEditDialog::handle_close_event(int result)
84 {
85         sb_window = 0;
86 }
87
88
89 ShBtnEditWindow::ShBtnEditWindow(ShBtnEditDialog *shbtn_edit, int x, int y)
90  : BC_Window(_(PROGRAM_NAME ": Shell"), x, y, 300, 200, 300, 200, 0, 0, 1)
91 {
92         this->shbtn_edit = shbtn_edit;
93         sb_dialog = 0;
94 }
95
96 ShBtnEditWindow::~ShBtnEditWindow()
97 {
98         delete sb_dialog;
99 }
100
101 int ShBtnEditWindow::list_update()
102 {
103         shbtn_items.remove_all_objects();
104         Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
105         for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
106                 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
107         }
108         return op_list->update(&shbtn_items, 0, 0, 1);
109 }
110
111 ShBtnAddButton::ShBtnAddButton(ShBtnEditWindow *sb_window, int x, int y)
112  : BC_GenericButton(x, y, _("Add"))
113 {
114         this->sb_window = sb_window;
115 }
116
117 ShBtnAddButton::~ShBtnAddButton()
118 {
119 }
120
121 int ShBtnAddButton::handle_event()
122 {
123
124         Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
125         ShBtnPref *pref = new ShBtnPref(_("new"), "", 0);
126         preferences->shbtn_prefs.append(pref);
127         sb_window->list_update();
128         return sb_window->start_edit(pref);
129 }
130
131 ShBtnDelButton::ShBtnDelButton(ShBtnEditWindow *sb_window, int x, int y)
132  : BC_GenericButton(x, y, _("Del"))
133 {
134         this->sb_window = sb_window;
135 }
136
137 ShBtnDelButton::~ShBtnDelButton()
138 {
139 }
140
141 int ShBtnDelButton::handle_event()
142 {
143         ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
144         if( !sp ) return 0;
145         Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
146         preferences->shbtn_prefs.remove(sp->pref);
147         sb_window->list_update();
148         return 1;
149 }
150
151 ShBtnEditButton::ShBtnEditButton(ShBtnEditWindow *sb_window, int x, int y)
152  : BC_GenericButton(x, y, _("Edit"))
153 {
154         this->sb_window = sb_window;
155 }
156
157 ShBtnEditButton::~ShBtnEditButton()
158 {
159 }
160
161 int ShBtnEditButton::handle_event()
162 {
163         ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
164         if( !sp ) return 0;
165         return sb_window->start_edit(sp->pref);
166 }
167
168 ShBtnTextDialog::ShBtnTextDialog(ShBtnEditWindow *sb_window)
169  : BC_DialogThread()
170 {
171         this->sb_window = sb_window;
172         this->pref = 0;
173 }
174
175 ShBtnTextDialog::~ShBtnTextDialog()
176 {
177         close_window();
178 }
179
180 ShBtnTextWindow::ShBtnTextWindow(ShBtnEditWindow *sb_window, int x, int y)
181  : BC_Window(_(PROGRAM_NAME ": Commands"), x, y, 640, 160, 640, 150, 0, 0, 1)
182 {
183         this->sb_window = sb_window;
184         warn = sb_window->sb_dialog->pref->warn;
185 }
186
187 ShBtnTextWindow::~ShBtnTextWindow()
188 {
189 }
190
191 ShBtnErrWarn::ShBtnErrWarn(ShBtnTextWindow *st_window, int x, int y)
192  : BC_CheckBox(x, y, &st_window->warn, _("Warn on err exit"))
193 {
194         this->st_window = st_window;
195 }
196
197 ShBtnErrWarn::~ShBtnErrWarn()
198 {
199 }
200
201 void ShBtnTextWindow::create_objects()
202 {
203         lock_window("ShBtnTextWindow::create_objects");
204         int x = 10, y = 10;
205         int x1 = 160;
206         BC_Title *title = new BC_Title(x, y, _("Label:"));
207         add_subwindow(title);
208         title = new BC_Title(x1, y, _("Commands:"));
209         add_subwindow(title);
210         y += title->get_h() + 8;
211         ShBtnPref *pref = sb_window->sb_dialog->pref;
212         cmd_name = new BC_TextBox(x, y, 140, 1, pref->name);
213         add_subwindow(cmd_name);
214         cmd_text = new BC_ScrollTextBox(this, x1, y, get_w()-x1-20, 4, pref->commands);
215         cmd_text->create_objects();
216         y += cmd_text->get_h() + 16;
217         add_subwindow(st_err_warn = new ShBtnErrWarn(this, x1, y));
218         y = get_h() - ShBtnTextOK::calculate_h() - 10;
219         add_subwindow(new ShBtnTextOK(this, x, y));
220         show_window();
221         unlock_window();
222 }
223
224 ShBtnTextOK::ShBtnTextOK(ShBtnTextWindow *st_window, int x, int y)
225  : BC_OKButton(x, y)
226 {
227         this->st_window = st_window;
228 }
229
230 ShBtnTextOK::~ShBtnTextOK()
231 {
232 }
233
234 int ShBtnTextOK::handle_event()
235 {
236         ShBtnPref *pref = st_window->sb_window->sb_dialog->pref;
237         strcpy(pref->name, st_window->cmd_name->get_text());
238         strcpy(pref->commands, st_window->cmd_text->get_text());
239         pref->warn = st_window->warn;
240         return BC_OKButton::handle_event();
241 }
242
243
244 BC_Window *ShBtnTextDialog::new_gui()
245 {
246         BC_DisplayInfo display_info;
247         int x = display_info.get_abs_cursor_x();
248         int y = display_info.get_abs_cursor_y();
249
250         st_window = new ShBtnTextWindow(sb_window, x, y);
251         st_window->create_objects();
252         return st_window;
253 }
254
255 void ShBtnTextDialog::handle_close_event(int result)
256 {
257         if( !result ) {
258                 sb_window->lock_window("ShBtnTextDialog::handle_close_event");
259                 sb_window->list_update();
260                 sb_window->unlock_window();
261         }
262         st_window = 0;
263 }
264
265 int ShBtnTextDialog::start_edit(ShBtnPref *pref)
266 {
267         this->pref = pref;
268         start();
269         return 1;
270 }
271
272 void ShBtnEditWindow::create_objects()
273 {
274         lock_window("ShBtnEditWindow::create_objects");
275         Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
276         for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
277                 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
278         }
279         int x = 10, y = 10;
280         add_subwindow(op_list = new ShBtnPrefList(this, x, y));
281         x = 190;
282         add_subwindow(add_button = new ShBtnAddButton(this, x, y));
283         y += add_button->get_h() + 8;
284         add_subwindow(del_button = new ShBtnDelButton(this, x, y));
285         y += del_button->get_h() + 8;
286         add_subwindow(edit_button = new ShBtnEditButton(this, x, y));
287         add_subwindow(new BC_OKButton(this));
288         show_window();
289         unlock_window();
290 }
291
292 int ShBtnEditWindow::start_edit(ShBtnPref *pref)
293 {
294         if( !sb_dialog )
295                 sb_dialog = new ShBtnTextDialog(this);
296         return sb_dialog->start_edit(pref);
297 }
298
299
300 ShBtnPrefItem::ShBtnPrefItem(ShBtnPref *pref)
301  : BC_ListBoxItem(pref->name)
302 {
303         this->pref = pref;
304 }
305
306 ShBtnPrefItem::~ShBtnPrefItem()
307 {
308 }
309
310 ShBtnPrefList::ShBtnPrefList(ShBtnEditWindow *sb_window, int x, int y)
311  : BC_ListBox(x, y, 140, 100, LISTBOX_TEXT, &sb_window->shbtn_items, 0, 0)
312 {
313         this->sb_window = sb_window;
314 }
315
316 ShBtnPrefList::~ShBtnPrefList()
317 {
318 }
319
320 int ShBtnPrefList::handle_event()
321 {
322         return 1;
323 }
324
325 MainShBtnItem::MainShBtnItem(MainShBtns *shbtns, ShBtnPref *pref)
326  : BC_MenuItem(pref->name)
327 {
328         this->shbtns = shbtns;
329         this->pref = pref;
330 }
331
332 int MainShBtnItem::handle_event()
333 {
334         pref->execute();
335         return 1;
336 }
337
338 MainShBtns::MainShBtns(MWindow *mwindow, int x, int y)
339  : BC_PopupMenu(x, y, 0, "", -1, mwindow->theme->shbtn_data)
340 {
341         this->mwindow = mwindow;
342         set_tooltip(_("shell cmds"));
343 }
344
345 int MainShBtns::load(Preferences *preferences)
346 {
347         while( total_items() ) del_item(0);
348         for( int i=0; i<preferences->shbtn_prefs.size(); ++i )
349                 add_item(new MainShBtnItem(this, preferences->shbtn_prefs[i]));
350         return 0;
351 }
352
353 int MainShBtns::handle_event()
354 {
355         return 1;
356 }
357