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));
49 execvp(argv[0], &argv[0]);
52 // warn <0:always, =0:never, >0:on err
54 int stat; waitpid(pid, &stat, 0);
56 if( warn > 0 ) return;
57 sprintf(msg, "%s: completed", name);
60 sprintf(msg, "%s: error exit status %d", name, stat);
61 MainError::show_error(msg);
64 ShBtnPref::ShBtnPref(const char *nm, const char *cmds, int warn, int run_script)
66 strncpy(name, nm, sizeof(name));
67 strncpy(commands, cmds, sizeof(commands));
69 this->run_script = run_script;
72 ShBtnPref::~ShBtnPref()
76 void ShBtnPref::execute(ArrayList<Indexable*> &args)
78 // thread async+autodelete, no explicit delete
79 ShBtnRun *job = new ShBtnRun(name, commands, warn);
80 job->add_arg("/bin/bash");
81 job->add_arg(commands);
83 for( int i=0; i<n; ++i ) {
84 Indexable *idxbl = args[i];
85 if( !idxbl->is_asset ) continue;
86 job->add_arg(idxbl->path);
91 void ShBtnPref::execute()
93 ShBtnRun *job = new ShBtnRun(name, commands, warn);
94 job->add_arg("/bin/bash");
96 job->add_arg(commands);
100 ShBtnEditDialog::ShBtnEditDialog(PreferencesWindow *pwindow)
103 this->pwindow = pwindow;
106 ShBtnEditDialog::~ShBtnEditDialog()
111 BC_Window* ShBtnEditDialog::new_gui()
113 BC_DisplayInfo display_info;
114 int x = display_info.get_abs_cursor_x();
115 int y = display_info.get_abs_cursor_y();
117 sb_window = new ShBtnEditWindow(this, x, y);
118 sb_window->create_objects();
122 void ShBtnEditDialog::handle_close_event(int result)
128 ShBtnEditWindow::ShBtnEditWindow(ShBtnEditDialog *shbtn_edit, int x, int y)
129 : BC_Window(_(PROGRAM_NAME ": Shell"), x, y,
130 xS(300), yS(200), xS(300), yS(200), 0, 0, 1)
132 this->shbtn_edit = shbtn_edit;
134 // *** CONTEXT_HELP ***
135 context_help_set_keyword("Menu Bar Shell Commands");
138 ShBtnEditWindow::~ShBtnEditWindow()
143 int ShBtnEditWindow::list_update()
145 shbtn_items.remove_all_objects();
146 Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
147 for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
148 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
150 return op_list->update(&shbtn_items, 0, 0, 1);
153 ShBtnAddButton::ShBtnAddButton(ShBtnEditWindow *sb_window, int x, int y)
154 : BC_GenericButton(x, y, _("Add"))
156 this->sb_window = sb_window;
159 ShBtnAddButton::~ShBtnAddButton()
163 int ShBtnAddButton::handle_event()
166 Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
167 ShBtnPref *pref = new ShBtnPref(_("new"), "", 0, 0);
168 preferences->shbtn_prefs.append(pref);
169 sb_window->list_update();
170 return sb_window->start_edit(pref);
173 ShBtnDelButton::ShBtnDelButton(ShBtnEditWindow *sb_window, int x, int y)
174 : BC_GenericButton(x, y, _("Del"))
176 this->sb_window = sb_window;
179 ShBtnDelButton::~ShBtnDelButton()
183 int ShBtnDelButton::handle_event()
185 ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
187 Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
188 preferences->shbtn_prefs.remove(sp->pref);
189 sb_window->list_update();
193 ShBtnEditButton::ShBtnEditButton(ShBtnEditWindow *sb_window, int x, int y)
194 : BC_GenericButton(x, y, _("Edit"))
196 this->sb_window = sb_window;
199 ShBtnEditButton::~ShBtnEditButton()
203 int ShBtnEditButton::handle_event()
205 ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
207 return sb_window->start_edit(sp->pref);
210 ShBtnTextDialog::ShBtnTextDialog(ShBtnEditWindow *sb_window)
213 this->sb_window = sb_window;
217 ShBtnTextDialog::~ShBtnTextDialog()
222 ShBtnTextWindow::ShBtnTextWindow(ShBtnEditWindow *sb_window, int x, int y)
223 : BC_Window(_(PROGRAM_NAME ": Commands"), x, y,
224 xS(640), yS(160), xS(640), yS(150), 0, 0, 1)
226 this->sb_window = sb_window;
227 warn = sb_window->sb_dialog->pref->warn;
228 run_script = sb_window->sb_dialog->pref->run_script;
229 // *** CONTEXT_HELP ***
230 context_help_set_keyword("Menu Bar Shell Commands");
233 ShBtnTextWindow::~ShBtnTextWindow()
238 ShBtnErrWarnItem::ShBtnErrWarnItem(ShBtnErrWarn *popup,
239 const char *text, int warn)
246 int ShBtnErrWarnItem::handle_event()
248 popup->set_text(get_text());
249 popup->st_window->warn = warn;
253 ShBtnErrWarn::ShBtnErrWarn(ShBtnTextWindow *st_window, int x, int y)
254 : BC_PopupMenu(x, y, xS(120), st_window->warn < 0 ? _("Always"):
255 !st_window->warn ? _("Never") : _("On Error"))
257 this->st_window = st_window;
259 ShBtnErrWarn::~ShBtnErrWarn()
262 int ShBtnErrWarn::handle_event()
267 void ShBtnErrWarn::create_objects()
269 add_item(new ShBtnErrWarnItem(this,_("Always"), -1));
270 add_item(new ShBtnErrWarnItem(this,_("Never"), 0));
271 add_item(new ShBtnErrWarnItem(this,_("On Error"), 1));
275 ShBtnRunScript::ShBtnRunScript(ShBtnTextWindow *st_window, int x, int y)
276 : BC_CheckBox(x, y, &st_window->run_script, _("run /path/script.sh + argvs"))
278 this->st_window = st_window;
281 ShBtnRunScript::~ShBtnRunScript()
285 void ShBtnTextWindow::create_objects()
287 lock_window("ShBtnTextWindow::create_objects");
288 int x = xS(10), y = yS(10);
290 BC_Title *title = new BC_Title(x, y, _("Label:"));
291 add_subwindow(title);
292 title = new BC_Title(x1, y, _("Commands:"));
293 add_subwindow(title);
294 y += title->get_h() + yS(8);
295 ShBtnPref *pref = sb_window->sb_dialog->pref;
296 cmd_name = new BC_TextBox(x, y, xS(140), 1, pref->name);
297 add_subwindow(cmd_name);
298 cmd_text = new BC_ScrollTextBox(this, x1, y, get_w()-x1-xS(20), 4, pref->commands);
299 cmd_text->create_objects();
300 y += cmd_text->get_h() + yS(16);
301 add_subwindow(title = new BC_Title(x1,y, _("OnExit Notify:")));
302 x1 += title->get_w() + xS(10);
303 add_subwindow(st_err_warn = new ShBtnErrWarn(this, x1, y));
304 st_err_warn->create_objects();
305 x1 += st_err_warn->get_w() + xS(20);
306 add_subwindow(st_run_script = new ShBtnRunScript(this, x1, y));
307 y = get_h() - ShBtnTextOK::calculate_h() - yS(10);
308 add_subwindow(new ShBtnTextOK(this, x, y));
313 ShBtnTextOK::ShBtnTextOK(ShBtnTextWindow *st_window, int x, int y)
316 this->st_window = st_window;
319 ShBtnTextOK::~ShBtnTextOK()
323 int ShBtnTextOK::handle_event()
325 ShBtnPref *pref = st_window->sb_window->sb_dialog->pref;
326 strcpy(pref->name, st_window->cmd_name->get_text());
327 strcpy(pref->commands, st_window->cmd_text->get_text());
328 pref->warn = st_window->warn;
329 pref->run_script = st_window->run_script;
330 return BC_OKButton::handle_event();
334 BC_Window *ShBtnTextDialog::new_gui()
336 BC_DisplayInfo display_info;
337 int x = display_info.get_abs_cursor_x();
338 int y = display_info.get_abs_cursor_y();
340 st_window = new ShBtnTextWindow(sb_window, x, y);
341 st_window->create_objects();
345 void ShBtnTextDialog::handle_close_event(int result)
348 sb_window->lock_window("ShBtnTextDialog::handle_close_event");
349 sb_window->list_update();
350 sb_window->unlock_window();
355 int ShBtnTextDialog::start_edit(ShBtnPref *pref)
362 void ShBtnEditWindow::create_objects()
364 lock_window("ShBtnEditWindow::create_objects");
365 Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
366 for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
367 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
369 int x = xS(10), y = yS(10);
370 add_subwindow(op_list = new ShBtnPrefList(this, x, y));
372 add_subwindow(add_button = new ShBtnAddButton(this, x, y));
373 y += add_button->get_h() + yS(8);
374 add_subwindow(del_button = new ShBtnDelButton(this, x, y));
375 y += del_button->get_h() + yS(8);
376 add_subwindow(edit_button = new ShBtnEditButton(this, x, y));
377 add_subwindow(new BC_OKButton(this));
382 int ShBtnEditWindow::start_edit(ShBtnPref *pref)
385 sb_dialog = new ShBtnTextDialog(this);
386 return sb_dialog->start_edit(pref);
390 ShBtnPrefItem::ShBtnPrefItem(ShBtnPref *pref)
391 : BC_ListBoxItem(pref->name)
396 ShBtnPrefItem::~ShBtnPrefItem()
400 ShBtnPrefList::ShBtnPrefList(ShBtnEditWindow *sb_window, int x, int y)
401 : BC_ListBox(x, y, xS(140), yS(100), LISTBOX_TEXT, &sb_window->shbtn_items, 0, 0)
403 this->sb_window = sb_window;
406 ShBtnPrefList::~ShBtnPrefList()
410 int ShBtnPrefList::handle_event()
415 MainShBtnItem::MainShBtnItem(MainShBtns *shbtns, ShBtnPref *pref)
416 : BC_MenuItem(pref->name)
418 this->shbtns = shbtns;
422 int MainShBtnItem::handle_event()
424 MWindow *mwindow = shbtns->mwindow;
425 if( pref->run_script ) {
426 AWindowGUI *agui = mwindow->awindow->gui;
427 agui->lock_window("MainShBtnItem::handle_event");
428 mwindow->awindow->gui->collect_assets();
429 pref->execute(*mwindow->session->drag_assets);
430 agui->unlock_window();
437 MainShBtns::MainShBtns(MWindow *mwindow, int x, int y)
438 : BC_PopupMenu(x, y, 0, "", -1, mwindow->theme->shbtn_data)
440 this->mwindow = mwindow;
441 set_tooltip(_("shell cmds"));
442 // *** CONTEXT_HELP ***
443 context_help_set_keyword("Menu Bar Shell Commands");
446 int MainShBtns::load(Preferences *preferences)
448 while( total_items() ) del_item(0);
449 for( int i=0; i<preferences->shbtn_prefs.size(); ++i )
450 add_item(new MainShBtnItem(this, preferences->shbtn_prefs[i]));
454 int MainShBtns::handle_event()