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()
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));
222 ShBtnTextOK::ShBtnTextOK(ShBtnTextWindow *st_window, int x, int y)
225 this->st_window = st_window;
228 ShBtnTextOK::~ShBtnTextOK()
232 int ShBtnTextOK::handle_event()
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();
242 BC_Window *ShBtnTextDialog::new_gui()
244 BC_DisplayInfo display_info;
245 int x = display_info.get_abs_cursor_x();
246 int y = display_info.get_abs_cursor_y();
248 st_window = new ShBtnTextWindow(sb_window, x, y);
249 st_window->create_objects();
253 void ShBtnTextDialog::handle_close_event(int result)
256 sb_window->list_update();
261 int ShBtnTextDialog::start_edit(ShBtnPref *pref)
268 void ShBtnEditWindow::create_objects()
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]));
275 add_subwindow(op_list = new ShBtnPrefList(this, x, y));
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));
286 int ShBtnEditWindow::start_edit(ShBtnPref *pref)
289 sb_dialog = new ShBtnTextDialog(this);
290 return sb_dialog->start_edit(pref);
294 ShBtnPrefItem::ShBtnPrefItem(ShBtnPref *pref)
295 : BC_ListBoxItem(pref->name)
300 ShBtnPrefItem::~ShBtnPrefItem()
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)
307 this->sb_window = sb_window;
310 ShBtnPrefList::~ShBtnPrefList()
314 int ShBtnPrefList::handle_event()
319 MainShBtnItem::MainShBtnItem(MainShBtns *shbtns, ShBtnPref *pref)
320 : BC_MenuItem(pref->name)
322 this->shbtns = shbtns;
326 int MainShBtnItem::handle_event()
332 MainShBtns::MainShBtns(MWindow *mwindow, int x, int y)
333 : BC_PopupMenu(x, y, 0, "", -1, mwindow->theme->shbtn_data)
335 this->mwindow = mwindow;
336 set_tooltip(_("shell cmds"));
339 int MainShBtns::load(Preferences *preferences)
341 while( total_items() ) del_item(0);
342 for( int i=0; i<preferences->shbtn_prefs.size(); ++i )
343 add_item(new MainShBtnItem(this, preferences->shbtn_prefs[i]));
347 int MainShBtns::handle_event()