1 #include "bcwindowbase.h"
2 #include "bcdisplayinfo.h"
5 #include "awindowgui.h"
10 #include "mainsession.h"
12 #include "shbtnprefs.h"
13 #include "preferences.h"
14 #include "preferencesthread.h"
19 ShBtnRun::ShBtnRun(const char *nm, const char *cmds, int warn)
22 argv.set_array_delete();
23 strncpy(name, nm, sizeof(name)-1);
24 name[sizeof(name)-1] = 0;
25 strncpy(commands, cmds, sizeof(commands)-1);
26 commands[sizeof(commands)-1] = 0;
31 argv.remove_all_objects();
34 void ShBtnRun::add_arg(const char *v)
36 argv.append(cstrdup(v));
47 int stat; waitpid(pid, &stat, 0);
50 sprintf(msg, "%s: error exit status %d", name, stat);
51 MainError::show_error(msg);
56 execvp(argv[0], &argv[0]);
59 ShBtnPref::ShBtnPref(const char *nm, const char *cmds, int warn, int run_script)
61 strncpy(name, nm, sizeof(name));
62 strncpy(commands, cmds, sizeof(commands));
64 this->run_script = run_script;
67 ShBtnPref::~ShBtnPref()
71 void ShBtnPref::execute(ArrayList<Indexable*> &args)
73 // thread async+autodelete, no explicit delete
74 ShBtnRun *job = new ShBtnRun(name, commands, warn);
75 job->add_arg("/bin/bash");
76 job->add_arg(commands);
78 for( int i=0; i<n; ++i ) {
79 Indexable *idxbl = args[i];
80 if( !idxbl->is_asset ) continue;
81 job->add_arg(idxbl->path);
86 void ShBtnPref::execute()
88 ShBtnRun *job = new ShBtnRun(name, commands, warn);
89 job->add_arg("/bin/bash");
91 job->add_arg(commands);
95 ShBtnEditDialog::ShBtnEditDialog(PreferencesWindow *pwindow)
98 this->pwindow = pwindow;
101 ShBtnEditDialog::~ShBtnEditDialog()
106 BC_Window* ShBtnEditDialog::new_gui()
108 BC_DisplayInfo display_info;
109 int x = display_info.get_abs_cursor_x();
110 int y = display_info.get_abs_cursor_y();
112 sb_window = new ShBtnEditWindow(this, x, y);
113 sb_window->create_objects();
117 void ShBtnEditDialog::handle_close_event(int result)
123 ShBtnEditWindow::ShBtnEditWindow(ShBtnEditDialog *shbtn_edit, int x, int y)
124 : BC_Window(_(PROGRAM_NAME ": Shell"), x, y, 300, 200, 300, 200, 0, 0, 1)
126 this->shbtn_edit = shbtn_edit;
130 ShBtnEditWindow::~ShBtnEditWindow()
135 int ShBtnEditWindow::list_update()
137 shbtn_items.remove_all_objects();
138 Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
139 for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
140 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
142 return op_list->update(&shbtn_items, 0, 0, 1);
145 ShBtnAddButton::ShBtnAddButton(ShBtnEditWindow *sb_window, int x, int y)
146 : BC_GenericButton(x, y, _("Add"))
148 this->sb_window = sb_window;
151 ShBtnAddButton::~ShBtnAddButton()
155 int ShBtnAddButton::handle_event()
158 Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
159 ShBtnPref *pref = new ShBtnPref(_("new"), "", 0, 0);
160 preferences->shbtn_prefs.append(pref);
161 sb_window->list_update();
162 return sb_window->start_edit(pref);
165 ShBtnDelButton::ShBtnDelButton(ShBtnEditWindow *sb_window, int x, int y)
166 : BC_GenericButton(x, y, _("Del"))
168 this->sb_window = sb_window;
171 ShBtnDelButton::~ShBtnDelButton()
175 int ShBtnDelButton::handle_event()
177 ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
179 Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
180 preferences->shbtn_prefs.remove(sp->pref);
181 sb_window->list_update();
185 ShBtnEditButton::ShBtnEditButton(ShBtnEditWindow *sb_window, int x, int y)
186 : BC_GenericButton(x, y, _("Edit"))
188 this->sb_window = sb_window;
191 ShBtnEditButton::~ShBtnEditButton()
195 int ShBtnEditButton::handle_event()
197 ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
199 return sb_window->start_edit(sp->pref);
202 ShBtnTextDialog::ShBtnTextDialog(ShBtnEditWindow *sb_window)
205 this->sb_window = sb_window;
209 ShBtnTextDialog::~ShBtnTextDialog()
214 ShBtnTextWindow::ShBtnTextWindow(ShBtnEditWindow *sb_window, int x, int y)
215 : BC_Window(_(PROGRAM_NAME ": Commands"), x, y, 640, 160, 640, 150, 0, 0, 1)
217 this->sb_window = sb_window;
218 warn = sb_window->sb_dialog->pref->warn;
219 run_script = sb_window->sb_dialog->pref->run_script;
222 ShBtnTextWindow::~ShBtnTextWindow()
226 ShBtnErrWarn::ShBtnErrWarn(ShBtnTextWindow *st_window, int x, int y)
227 : BC_CheckBox(x, y, &st_window->warn, _("Warn on err exit"))
229 this->st_window = st_window;
232 ShBtnErrWarn::~ShBtnErrWarn()
236 ShBtnRunScript::ShBtnRunScript(ShBtnTextWindow *st_window, int x, int y)
237 : BC_CheckBox(x, y, &st_window->run_script, _("run /path/script.sh + argvs"))
239 this->st_window = st_window;
242 ShBtnRunScript::~ShBtnRunScript()
246 void ShBtnTextWindow::create_objects()
248 lock_window("ShBtnTextWindow::create_objects");
251 BC_Title *title = new BC_Title(x, y, _("Label:"));
252 add_subwindow(title);
253 title = new BC_Title(x1, y, _("Commands:"));
254 add_subwindow(title);
255 y += title->get_h() + 8;
256 ShBtnPref *pref = sb_window->sb_dialog->pref;
257 cmd_name = new BC_TextBox(x, y, 140, 1, pref->name);
258 add_subwindow(cmd_name);
259 cmd_text = new BC_ScrollTextBox(this, x1, y, get_w()-x1-20, 4, pref->commands);
260 cmd_text->create_objects();
261 y += cmd_text->get_h() + 16;
262 add_subwindow(st_err_warn = new ShBtnErrWarn(this, x1, y));
263 x1 += st_err_warn->get_w() + 20;
264 add_subwindow(st_run_script = new ShBtnRunScript(this, x1, y));
265 y = get_h() - ShBtnTextOK::calculate_h() - 10;
266 add_subwindow(new ShBtnTextOK(this, x, y));
271 ShBtnTextOK::ShBtnTextOK(ShBtnTextWindow *st_window, int x, int y)
274 this->st_window = st_window;
277 ShBtnTextOK::~ShBtnTextOK()
281 int ShBtnTextOK::handle_event()
283 ShBtnPref *pref = st_window->sb_window->sb_dialog->pref;
284 strcpy(pref->name, st_window->cmd_name->get_text());
285 strcpy(pref->commands, st_window->cmd_text->get_text());
286 pref->warn = st_window->warn;
287 pref->run_script = st_window->run_script;
288 return BC_OKButton::handle_event();
292 BC_Window *ShBtnTextDialog::new_gui()
294 BC_DisplayInfo display_info;
295 int x = display_info.get_abs_cursor_x();
296 int y = display_info.get_abs_cursor_y();
298 st_window = new ShBtnTextWindow(sb_window, x, y);
299 st_window->create_objects();
303 void ShBtnTextDialog::handle_close_event(int result)
306 sb_window->lock_window("ShBtnTextDialog::handle_close_event");
307 sb_window->list_update();
308 sb_window->unlock_window();
313 int ShBtnTextDialog::start_edit(ShBtnPref *pref)
320 void ShBtnEditWindow::create_objects()
322 lock_window("ShBtnEditWindow::create_objects");
323 Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
324 for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
325 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
328 add_subwindow(op_list = new ShBtnPrefList(this, x, y));
330 add_subwindow(add_button = new ShBtnAddButton(this, x, y));
331 y += add_button->get_h() + 8;
332 add_subwindow(del_button = new ShBtnDelButton(this, x, y));
333 y += del_button->get_h() + 8;
334 add_subwindow(edit_button = new ShBtnEditButton(this, x, y));
335 add_subwindow(new BC_OKButton(this));
340 int ShBtnEditWindow::start_edit(ShBtnPref *pref)
343 sb_dialog = new ShBtnTextDialog(this);
344 return sb_dialog->start_edit(pref);
348 ShBtnPrefItem::ShBtnPrefItem(ShBtnPref *pref)
349 : BC_ListBoxItem(pref->name)
354 ShBtnPrefItem::~ShBtnPrefItem()
358 ShBtnPrefList::ShBtnPrefList(ShBtnEditWindow *sb_window, int x, int y)
359 : BC_ListBox(x, y, 140, 100, LISTBOX_TEXT, &sb_window->shbtn_items, 0, 0)
361 this->sb_window = sb_window;
364 ShBtnPrefList::~ShBtnPrefList()
368 int ShBtnPrefList::handle_event()
373 MainShBtnItem::MainShBtnItem(MainShBtns *shbtns, ShBtnPref *pref)
374 : BC_MenuItem(pref->name)
376 this->shbtns = shbtns;
380 int MainShBtnItem::handle_event()
382 MWindow *mwindow = shbtns->mwindow;
383 if( pref->run_script ) {
384 AWindowGUI *agui = mwindow->awindow->gui;
385 agui->lock_window("MainShBtnItem::handle_event");
386 mwindow->awindow->gui->collect_assets();
387 pref->execute(*mwindow->session->drag_assets);
388 agui->unlock_window();
395 MainShBtns::MainShBtns(MWindow *mwindow, int x, int y)
396 : BC_PopupMenu(x, y, 0, "", -1, mwindow->theme->shbtn_data)
398 this->mwindow = mwindow;
399 set_tooltip(_("shell cmds"));
402 int MainShBtns::load(Preferences *preferences)
404 while( total_items() ) del_item(0);
405 for( int i=0; i<preferences->shbtn_prefs.size(); ++i )
406 add_item(new MainShBtnItem(this, preferences->shbtn_prefs[i]));
410 int MainShBtns::handle_event()