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