prevent popup deactivation while button_down
[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 "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         int x = 10, y = 10;
204         int x1 = 160;
205         BC_Title *title = new BC_Title(x, y, _("Label:"));
206         add_subwindow(title);
207         title = new BC_Title(x1, y, _("Commands:"));
208         add_subwindow(title);
209         y += title->get_h() + 8;
210         ShBtnPref *pref = sb_window->sb_dialog->pref;
211         cmd_name = new BC_TextBox(x, y, 140, 1, pref->name);
212         add_subwindow(cmd_name);
213         cmd_text = new BC_ScrollTextBox(this, x1, y, get_w()-x1-20, 4, pref->commands);
214         cmd_text->create_objects();
215         y += cmd_text->get_h() + 8;
216         add_subwindow(st_err_warn = new ShBtnErrWarn(this, x1, y));
217         y = get_h() - ShBtnTextOK::calculate_h() - 10;
218         add_subwindow(new ShBtnTextOK(this, x, y));
219         show_window();
220 }
221
222 ShBtnTextOK::ShBtnTextOK(ShBtnTextWindow *st_window, int x, int y)
223  : BC_OKButton(x, y)
224 {
225         this->st_window = st_window;
226 }
227
228 ShBtnTextOK::~ShBtnTextOK()
229 {
230 }
231
232 int ShBtnTextOK::handle_event()
233 {
234         ShBtnPref *pref = st_window->sb_window->sb_dialog->pref;
235         strcpy(pref->name, st_window->cmd_name->get_text());
236         strcpy(pref->commands, st_window->cmd_text->get_text());
237         pref->warn = st_window->warn;
238         return BC_OKButton::handle_event();
239 }
240
241
242 BC_Window *ShBtnTextDialog::new_gui()
243 {
244         BC_DisplayInfo display_info;
245         int x = display_info.get_abs_cursor_x();
246         int y = display_info.get_abs_cursor_y();
247
248         st_window = new ShBtnTextWindow(sb_window, x, y);
249         st_window->create_objects();
250         return st_window;
251 }
252
253 void ShBtnTextDialog::handle_close_event(int result)
254 {
255         if( !result ) {
256                 sb_window->list_update();
257         }
258         st_window = 0;
259 }
260
261 int ShBtnTextDialog::start_edit(ShBtnPref *pref)
262 {
263         this->pref = pref;
264         start();
265         return 1;
266 }
267
268 void ShBtnEditWindow::create_objects()
269 {
270         Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
271         for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
272                 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
273         }
274         int x = 10, y = 10;
275         add_subwindow(op_list = new ShBtnPrefList(this, x, y));
276         x = 190;
277         add_subwindow(add_button = new ShBtnAddButton(this, x, y));
278         y += add_button->get_h() + 8;
279         add_subwindow(del_button = new ShBtnDelButton(this, x, y));
280         y += del_button->get_h() + 8;
281         add_subwindow(edit_button = new ShBtnEditButton(this, x, y));
282         add_subwindow(new BC_OKButton(this));
283         show_window();
284 }
285
286 int ShBtnEditWindow::start_edit(ShBtnPref *pref)
287 {
288         if( !sb_dialog )
289                 sb_dialog = new ShBtnTextDialog(this);
290         return sb_dialog->start_edit(pref);
291 }
292
293
294 ShBtnPrefItem::ShBtnPrefItem(ShBtnPref *pref)
295  : BC_ListBoxItem(pref->name)
296 {
297         this->pref = pref;
298 }
299
300 ShBtnPrefItem::~ShBtnPrefItem()
301 {
302 }
303
304 ShBtnPrefList::ShBtnPrefList(ShBtnEditWindow *sb_window, int x, int y)
305  : BC_ListBox(x, y, 140, 100, LISTBOX_TEXT, &sb_window->shbtn_items, 0, 0)
306 {
307         this->sb_window = sb_window;
308 }
309
310 ShBtnPrefList::~ShBtnPrefList()
311 {
312 }
313
314 int ShBtnPrefList::handle_event()
315 {
316         return 1;
317 }
318
319 MainShBtnItem::MainShBtnItem(MainShBtns *shbtns, ShBtnPref *pref)
320  : BC_MenuItem(pref->name)
321 {
322         this->shbtns = shbtns;
323         this->pref = pref;
324 }
325
326 int MainShBtnItem::handle_event()
327 {
328         pref->execute();
329         return 1;
330 }
331
332 MainShBtns::MainShBtns(MWindow *mwindow, int x, int y)
333  : BC_PopupMenu(x, y, 0, "", -1, mwindow->theme->shbtn_data)
334 {
335         this->mwindow = mwindow;
336         set_tooltip(_("shell cmds"));
337 }
338
339 int MainShBtns::load(Preferences *preferences)
340 {
341         while( total_items() ) remove_item(get_item(0));
342         for( int i=0; i<preferences->shbtn_prefs.size(); ++i )
343                 add_item(new MainShBtnItem(this, preferences->shbtn_prefs[i]));
344         return 0;
345 }
346
347 int MainShBtns::handle_event()
348 {
349         return 1;
350 }
351