1 #include "bcwindowbase.h"
2 #include "bcdisplayinfo.h"
7 #include "shbtnprefs.h"
8 #include "preferences.h"
9 #include "preferencesthread.h"
14 ShBtnRun::ShBtnRun(const char *nm, const char *cmds, int warn)
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;
33 int stat; waitpid(pid, &stat, 0);
36 sprintf(msg, "%s: error exit status %d", name, stat);
37 MainError::show_error(msg);
41 char *const argv[4] = { (char*) "/bin/bash", (char*) "-c", commands, 0 };
42 execvp(argv[0], &argv[0]);
45 ShBtnPref::ShBtnPref(const char *nm, const char *cmds, int warn)
47 strncpy(name, nm, sizeof(name));
48 strncpy(commands, cmds, sizeof(commands));
52 ShBtnPref::~ShBtnPref()
56 void ShBtnPref::execute()
58 new ShBtnRun(name, commands, warn);
61 ShBtnEditDialog::ShBtnEditDialog(PreferencesWindow *pwindow)
64 this->pwindow = pwindow;
67 ShBtnEditDialog::~ShBtnEditDialog()
72 BC_Window* ShBtnEditDialog::new_gui()
74 BC_DisplayInfo display_info;
75 int x = display_info.get_abs_cursor_x();
76 int y = display_info.get_abs_cursor_y();
78 sb_window = new ShBtnEditWindow(this, x, y);
79 sb_window->create_objects();
83 void ShBtnEditDialog::handle_close_event(int result)
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)
92 this->shbtn_edit = shbtn_edit;
96 ShBtnEditWindow::~ShBtnEditWindow()
101 int ShBtnEditWindow::list_update()
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]));
108 return op_list->update(&shbtn_items, 0, 0, 1);
111 ShBtnAddButton::ShBtnAddButton(ShBtnEditWindow *sb_window, int x, int y)
112 : BC_GenericButton(x, y, _("Add"))
114 this->sb_window = sb_window;
117 ShBtnAddButton::~ShBtnAddButton()
121 int ShBtnAddButton::handle_event()
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);
131 ShBtnDelButton::ShBtnDelButton(ShBtnEditWindow *sb_window, int x, int y)
132 : BC_GenericButton(x, y, _("Del"))
134 this->sb_window = sb_window;
137 ShBtnDelButton::~ShBtnDelButton()
141 int ShBtnDelButton::handle_event()
143 ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
145 Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
146 preferences->shbtn_prefs.remove(sp->pref);
147 sb_window->list_update();
151 ShBtnEditButton::ShBtnEditButton(ShBtnEditWindow *sb_window, int x, int y)
152 : BC_GenericButton(x, y, _("Edit"))
154 this->sb_window = sb_window;
157 ShBtnEditButton::~ShBtnEditButton()
161 int ShBtnEditButton::handle_event()
163 ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
165 return sb_window->start_edit(sp->pref);
168 ShBtnTextDialog::ShBtnTextDialog(ShBtnEditWindow *sb_window)
171 this->sb_window = sb_window;
175 ShBtnTextDialog::~ShBtnTextDialog()
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)
183 this->sb_window = sb_window;
184 warn = sb_window->sb_dialog->pref->warn;
187 ShBtnTextWindow::~ShBtnTextWindow()
191 ShBtnErrWarn::ShBtnErrWarn(ShBtnTextWindow *st_window, int x, int y)
192 : BC_CheckBox(x, y, &st_window->warn, _("Warn on err exit"))
194 this->st_window = st_window;
197 ShBtnErrWarn::~ShBtnErrWarn()
201 void ShBtnTextWindow::create_objects()
203 lock_window("ShBtnTextWindow::create_objects");
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));
224 ShBtnTextOK::ShBtnTextOK(ShBtnTextWindow *st_window, int x, int y)
227 this->st_window = st_window;
230 ShBtnTextOK::~ShBtnTextOK()
234 int ShBtnTextOK::handle_event()
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();
244 BC_Window *ShBtnTextDialog::new_gui()
246 BC_DisplayInfo display_info;
247 int x = display_info.get_abs_cursor_x();
248 int y = display_info.get_abs_cursor_y();
250 st_window = new ShBtnTextWindow(sb_window, x, y);
251 st_window->create_objects();
255 void ShBtnTextDialog::handle_close_event(int result)
258 sb_window->lock_window("ShBtnTextDialog::handle_close_event");
259 sb_window->list_update();
260 sb_window->unlock_window();
265 int ShBtnTextDialog::start_edit(ShBtnPref *pref)
272 void ShBtnEditWindow::create_objects()
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]));
280 add_subwindow(op_list = new ShBtnPrefList(this, x, y));
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));
292 int ShBtnEditWindow::start_edit(ShBtnPref *pref)
295 sb_dialog = new ShBtnTextDialog(this);
296 return sb_dialog->start_edit(pref);
300 ShBtnPrefItem::ShBtnPrefItem(ShBtnPref *pref)
301 : BC_ListBoxItem(pref->name)
306 ShBtnPrefItem::~ShBtnPrefItem()
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)
313 this->sb_window = sb_window;
316 ShBtnPrefList::~ShBtnPrefList()
320 int ShBtnPrefList::handle_event()
325 MainShBtnItem::MainShBtnItem(MainShBtns *shbtns, ShBtnPref *pref)
326 : BC_MenuItem(pref->name)
328 this->shbtns = shbtns;
332 int MainShBtnItem::handle_event()
338 MainShBtns::MainShBtns(MWindow *mwindow, int x, int y)
339 : BC_PopupMenu(x, y, 0, "", -1, mwindow->theme->shbtn_data)
341 this->mwindow = mwindow;
342 set_tooltip(_("shell cmds"));
345 int MainShBtns::load(Preferences *preferences)
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]));
353 int MainShBtns::handle_event()