1 #include "bcwindowbase.h"
2 #include "bcdisplayinfo.h"
6 #include "shbtnprefs.h"
7 #include "preferences.h"
8 #include "preferencesthread.h"
12 ShBtnRun::ShBtnRun(const char *nm, const char *cmds, int warn)
15 strncpy(name, nm, sizeof(name)-1);
16 name[sizeof(name)-1] = 0;
17 strncpy(commands, cmds, sizeof(commands)-1);
18 commands[sizeof(commands)-1] = 0;
31 int stat; waitpid(pid, &stat, 0);
34 sprintf(msg, "%s: error exit status %d", name, stat);
35 MainError::show_error(msg);
39 char *const argv[4] = { (char*) "/bin/bash", (char*) "-c", commands, 0 };
40 execvp(argv[0], &argv[0]);
43 ShBtnPref::ShBtnPref(const char *nm, const char *cmds, int warn)
45 strncpy(name, nm, sizeof(name));
46 strncpy(commands, cmds, sizeof(commands));
50 ShBtnPref::~ShBtnPref()
54 void ShBtnPref::execute()
56 new ShBtnRun(name, commands, warn);
59 ShBtnEditDialog::ShBtnEditDialog(PreferencesWindow *pwindow)
62 this->pwindow = pwindow;
65 ShBtnEditDialog::~ShBtnEditDialog()
70 BC_Window* ShBtnEditDialog::new_gui()
72 BC_DisplayInfo display_info;
73 int x = display_info.get_abs_cursor_x();
74 int y = display_info.get_abs_cursor_y();
76 sb_window = new ShBtnEditWindow(this, x, y);
77 sb_window->create_objects();
81 void ShBtnEditDialog::handle_close_event(int result)
87 ShBtnEditWindow::ShBtnEditWindow(ShBtnEditDialog *shbtn_edit, int x, int y)
88 : BC_Window(_(PROGRAM_NAME ": Shell"), x, y, 300, 200, 300, 200, 0, 0, 1)
90 this->shbtn_edit = shbtn_edit;
94 ShBtnEditWindow::~ShBtnEditWindow()
99 int ShBtnEditWindow::list_update()
101 shbtn_items.remove_all_objects();
102 Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
103 for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
104 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
106 return op_list->update(&shbtn_items, 0, 0, 1);
109 ShBtnAddButton::ShBtnAddButton(ShBtnEditWindow *sb_window, int x, int y)
110 : BC_GenericButton(x, y, _("Add"))
112 this->sb_window = sb_window;
115 ShBtnAddButton::~ShBtnAddButton()
119 int ShBtnAddButton::handle_event()
122 Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
123 ShBtnPref *pref = new ShBtnPref(_("new"), "", 0);
124 preferences->shbtn_prefs.append(pref);
125 sb_window->list_update();
126 return sb_window->start_edit(pref);
129 ShBtnDelButton::ShBtnDelButton(ShBtnEditWindow *sb_window, int x, int y)
130 : BC_GenericButton(x, y, _("Del"))
132 this->sb_window = sb_window;
135 ShBtnDelButton::~ShBtnDelButton()
139 int ShBtnDelButton::handle_event()
141 ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
143 Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
144 preferences->shbtn_prefs.remove(sp->pref);
145 sb_window->list_update();
149 ShBtnEditButton::ShBtnEditButton(ShBtnEditWindow *sb_window, int x, int y)
150 : BC_GenericButton(x, y, _("Edit"))
152 this->sb_window = sb_window;
155 ShBtnEditButton::~ShBtnEditButton()
159 int ShBtnEditButton::handle_event()
161 ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
163 return sb_window->start_edit(sp->pref);
166 ShBtnTextDialog::ShBtnTextDialog(ShBtnEditWindow *sb_window)
169 this->sb_window = sb_window;
173 ShBtnTextDialog::~ShBtnTextDialog()
178 ShBtnTextWindow::ShBtnTextWindow(ShBtnEditWindow *sb_window, int x, int y)
179 : BC_Window(_(PROGRAM_NAME ": Commands"), x, y, 640, 160, 640, 150, 0, 0, 1)
181 this->sb_window = sb_window;
182 warn = sb_window->sb_dialog->pref->warn;
185 ShBtnTextWindow::~ShBtnTextWindow()
189 ShBtnErrWarn::ShBtnErrWarn(ShBtnTextWindow *st_window, int x, int y)
190 : BC_CheckBox(x, y, &st_window->warn, "Warn on err exit")
192 this->st_window = st_window;
195 ShBtnErrWarn::~ShBtnErrWarn()
199 void ShBtnTextWindow::create_objects()
203 BC_Title *title = new BC_Title(x, y, _("Label:"));
204 add_subwindow(title);
205 title = new BC_Title(x1, y, _("Commands:"));
206 add_subwindow(title);
207 y += title->get_h() + 8;
208 ShBtnPref *pref = sb_window->sb_dialog->pref;
209 cmd_name = new BC_TextBox(x, y, 140, 1, pref->name);
210 add_subwindow(cmd_name);
211 cmd_text = new BC_ScrollTextBox(this, x1, y, get_w()-x1-20, 4, pref->commands);
212 cmd_text->create_objects();
213 y += cmd_text->get_h() + 8;
214 add_subwindow(st_err_warn = new ShBtnErrWarn(this, x1, y));
215 y = get_h() - ShBtnTextOK::calculate_h() - 10;
216 add_subwindow(new ShBtnTextOK(this, x, y));
220 ShBtnTextOK::ShBtnTextOK(ShBtnTextWindow *st_window, int x, int y)
223 this->st_window = st_window;
226 ShBtnTextOK::~ShBtnTextOK()
230 int ShBtnTextOK::handle_event()
232 ShBtnPref *pref = st_window->sb_window->sb_dialog->pref;
233 strcpy(pref->name, st_window->cmd_name->get_text());
234 strcpy(pref->commands, st_window->cmd_text->get_text());
235 pref->warn = st_window->warn;
236 return BC_OKButton::handle_event();
240 BC_Window *ShBtnTextDialog::new_gui()
242 BC_DisplayInfo display_info;
243 int x = display_info.get_abs_cursor_x();
244 int y = display_info.get_abs_cursor_y();
246 st_window = new ShBtnTextWindow(sb_window, x, y);
247 st_window->create_objects();
251 void ShBtnTextDialog::handle_close_event(int result)
254 sb_window->list_update();
259 int ShBtnTextDialog::start_edit(ShBtnPref *pref)
266 void ShBtnEditWindow::create_objects()
268 Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
269 for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
270 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
273 add_subwindow(op_list = new ShBtnPrefList(this, x, y));
275 add_subwindow(add_button = new ShBtnAddButton(this, x, y));
276 y += add_button->get_h() + 8;
277 add_subwindow(del_button = new ShBtnDelButton(this, x, y));
278 y += del_button->get_h() + 8;
279 add_subwindow(edit_button = new ShBtnEditButton(this, x, y));
280 add_subwindow(new BC_OKButton(this));
284 int ShBtnEditWindow::start_edit(ShBtnPref *pref)
287 sb_dialog = new ShBtnTextDialog(this);
288 return sb_dialog->start_edit(pref);
292 ShBtnPrefItem::ShBtnPrefItem(ShBtnPref *pref)
293 : BC_ListBoxItem(pref->name)
298 ShBtnPrefItem::~ShBtnPrefItem()
302 ShBtnPrefList::ShBtnPrefList(ShBtnEditWindow *sb_window, int x, int y)
303 : BC_ListBox(x, y, 140, 100, LISTBOX_TEXT, &sb_window->shbtn_items, 0, 0)
305 this->sb_window = sb_window;
308 ShBtnPrefList::~ShBtnPrefList()
312 int ShBtnPrefList::handle_event()
317 MainShBtnItem::MainShBtnItem(MainShBtns *shbtns, ShBtnPref *pref)
318 : BC_MenuItem(pref->name)
320 this->shbtns = shbtns;
324 int MainShBtnItem::handle_event()
330 #include "data/shbtn_up_png.h"
331 #include "data/shbtn_hi_png.h"
332 #include "data/shbtn_dn_png.h"
334 static VFrame *shbtn_images[] = {
335 new VFrame(shbtn_up_png),
336 new VFrame(shbtn_hi_png),
337 new VFrame(shbtn_dn_png),
340 MainShBtns::MainShBtns(MWindow *mwindow, int x, int y)
341 : BC_PopupMenu(x, y, 0, "", -1, shbtn_images)
343 this->mwindow = mwindow;
344 set_tooltip(_("shell cmds"));
347 int MainShBtns::load(Preferences *preferences)
349 while( total_items() ) remove_item(get_item(0));
350 for( int i=0; i<preferences->shbtn_prefs.size(); ++i )
351 add_item(new MainShBtnItem(this, preferences->shbtn_prefs[i]));
355 int MainShBtns::handle_event()