3 * Read the file COPYING
5 * \author Angus Leeming
7 * Full author contact details are available in file CREDITS
14 #pragma implementation
18 #include "FormForks.h"
19 #include "ControlForks.h"
20 #include "forms/form_forks.h"
22 #include "helper_funcs.h"
23 #include "xforms_helpers.h"
25 #include "support/lstrings.h"
26 #include FORMS_H_LOCATION
32 typedef FormCB<ControlForks, FormDB<FD_forks> > base_class;
34 FormForks::FormForks()
35 : base_class(_("Child processes"))
39 void FormForks::build() {
40 dialog_.reset(build_forks(this));
42 // It appears that the browsers aren't initialised properly.
43 // This fudge fixes tings.
44 fl_add_browser_line(dialog_->browser_children, " ");
45 fl_add_browser_line(dialog_->browser_kill, " ");
46 fl_clear_browser(dialog_->browser_children);
47 fl_clear_browser(dialog_->browser_kill);
49 // Manage the ok, apply, restore and cancel/close buttons
50 bc().setOK(dialog_->button_ok);
51 bc().setApply(dialog_->button_apply);
52 bc().setCancel(dialog_->button_close);
55 // Set up the tooltip mechanism
56 string str = _("All currently running child processes forked by LyX.");
57 tooltips().init(dialog_->browser_children, str);
59 str = _("A list of all child processes to kill.");
60 tooltips().init(dialog_->browser_kill, str);
62 str = _("Add all processes to the list of processes to kill.");
63 tooltips().init(dialog_->button_all, str);
65 str = _("Add the currently selected child process to the list of processes to kill.");
66 tooltips().init(dialog_->button_add, str);
68 str = _("Remove the currently selected item from the list of processes to kill.");
69 tooltips().init(dialog_->button_remove, str);
73 void FormForks::update()
78 string const current_pid_str =
79 getString(dialog_->browser_kill);
80 pid_t const current_pid = strToInt(current_pid_str);
82 vector<pid_t> pids = controller().getPIDs();
84 // No child processes.
86 if (fl_get_browser_maxline(dialog_->browser_kill) > 0)
87 fl_clear_browser(dialog_->browser_kill);
88 if (fl_get_browser_maxline(dialog_->browser_children) > 0)
89 fl_clear_browser(dialog_->browser_children);
91 setEnabled(dialog_->browser_children, false);
92 setEnabled(dialog_->browser_kill, false);
93 setEnabled(dialog_->button_all, false);
94 setEnabled(dialog_->button_add, false);
95 setEnabled(dialog_->button_remove, false);
100 // Remove any processes from the kill browser that aren't in the
101 // vector of existing PIDs.
102 for (int i = 1; i <= fl_get_browser_maxline(dialog_->browser_kill);
104 string const pid_str =
105 getString(dialog_->browser_kill, i);
106 pid_t const pid = strToInt(pid_str);
107 vector<pid_t>::const_iterator it =
108 find(pids.begin(), pids.end(), pid);
109 if (it == pids.end())
110 fl_delete_browser_line(dialog_->browser_kill, i);
113 // Build the children browser from scratch.
114 if (fl_get_browser_maxline(dialog_->browser_children) > 0)
115 fl_clear_browser(dialog_->browser_children);
117 for (vector<pid_t>::const_iterator it = pids.begin();
118 it != pids.end(); ++it) {
119 string const pid_str = tostr(*it);
120 string const command = controller().getCommand(*it);
121 string const line = pid_str + '\t' + command;
123 fl_add_browser_line(dialog_->browser_children, line.c_str());
125 if (*it == current_pid)
126 fl_select_browser_line(dialog_->browser_children, i);
130 setEnabled(dialog_->browser_children, true);
131 setEnabled(dialog_->button_all, true);
132 setEnabled(dialog_->button_add, true);
136 void FormForks::apply()
138 // Get the list of all processes to kill.
139 vector<string> const kill_vec =
140 getVector(dialog_->browser_kill);
142 if (kill_vec.empty())
145 // Remove these items from the vector of child processes.
146 for (int i = 1; i <= fl_get_browser_maxline(dialog_->browser_children);
148 string const selection =
149 getString(dialog_->browser_children, i);
151 split(selection, pid_str, '\t');
153 vector<string>::const_iterator it =
154 find(kill_vec.begin(), kill_vec.end(), pid_str);
156 if (it != kill_vec.end())
157 fl_delete_browser_line(dialog_->browser_children, i);
160 // Clear the kill browser and deactivate appropriately.
161 fl_clear_browser(dialog_->browser_kill);
162 setEnabled(dialog_->browser_kill, false);
163 setEnabled(dialog_->button_remove, false);
165 // Pass these pids to the controller for destruction.
166 for (vector<string>::const_iterator it = kill_vec.begin();
167 it != kill_vec.end(); ++it) {
168 pid_t const pid = strToInt(*it);
169 controller().kill(pid);
175 ButtonPolicy::SMInput FormForks::input(FL_OBJECT * ob, long)
177 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
179 if (ob == dialog_->browser_children) {
180 activate = input_browser_children();
182 } else if (ob == dialog_->browser_kill) {
183 activate = input_browser_kill();
185 } else if (ob == dialog_->button_all) {
186 activate = input_button_all();
188 } else if (ob == dialog_->button_add) {
189 activate = input_button_add();
191 } else if (ob == dialog_->button_remove) {
192 activate = input_button_remove();
198 ButtonPolicy::SMInput FormForks::input_browser_children()
200 // Selected an item in the browser containing a list of all child
203 // 1. Highlight this item in the browser of processes to kill
204 // if it is already there.
206 // 2. If it is there, enable the remove button so that it can
207 // be removed from this list, if so desired.
209 // 3. If it isn't there, activate the add button so that it can
210 // be added to this list if so desired.
212 string const selection =
213 getString(dialog_->browser_children);
215 split(selection, pid_str, '\t');
217 vector<string> const kill_vec =
218 getVector(dialog_->browser_kill);
220 vector<string>::const_iterator it =
221 find(kill_vec.begin(), kill_vec.end(), pid_str);
223 fl_deselect_browser(dialog_->browser_kill);
224 if (it != kill_vec.end()) {
225 int const n = int(it - kill_vec.begin());
226 fl_select_browser_line(dialog_->browser_kill, n+1);
227 fl_set_browser_topline(dialog_->browser_kill, n+1);
230 setEnabled(dialog_->button_remove, it != kill_vec.end());
231 setEnabled(dialog_->button_add, it == kill_vec.end());
233 return ButtonPolicy::SMI_NOOP;
241 FindPID(string const & pid) : pid_(pid) {}
242 bool operator()(string const & line)
248 split(line, pid_str, '\t');
249 return pid_str == pid_;
259 ButtonPolicy::SMInput FormForks::input_browser_kill()
261 // Selected an item in the browser containing a list of processes
264 // 1. Highlight this item in the browser of all child processes.
266 // 2. Enable the remove button so that it can removed from this list,
269 // 3. Disable the add button.
271 string const pid_str =
272 getString(dialog_->browser_kill);
274 // Find this string in the list of all child processes
275 vector<string> const child_vec =
276 getVector(dialog_->browser_children);
278 vector<string>::const_iterator it =
279 find_if(child_vec.begin(), child_vec.end(), FindPID(pid_str));
281 fl_deselect_browser(dialog_->browser_children);
282 if (it != child_vec.end()) {
283 int const n = int(it - child_vec.begin());
284 fl_select_browser_line(dialog_->browser_children, n+1);
285 fl_set_browser_topline(dialog_->browser_children, n+1);
288 setEnabled(dialog_->button_remove, true);
289 setEnabled(dialog_->button_add, false);
291 return ButtonPolicy::SMI_NOOP;
297 vector<string> const getPIDvector(FL_OBJECT * ob)
299 vector<string> vec = getVector(ob);
303 for (vector<string>::iterator it = vec.begin(); it != vec.end(); ++it) {
305 split(*it, pid_str, '\t');
315 ButtonPolicy::SMInput FormForks::input_button_all()
317 // Pressed the "All" button.
319 // 1. Check that the browser of processes to kill doesn't already
320 // contain the entire list.
322 // 2. If it doesn't, copy the PIDs of all child processes into the
323 // browser of processes to kill.
325 // 3. Deactivate the "children" browser and the "add" and "all" buttons
327 // 4. Activate the "kill" browser and the "remove" button"
329 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
331 vector<string> const pid_vec = getPIDvector(dialog_->browser_children);
333 // to resolve a warning about comparison between signed and unsigned.
334 int const pid_vec_size = int(pid_vec.size());
336 if (fl_get_browser_maxline(dialog_->browser_kill) != pid_vec_size) {
337 activate = ButtonPolicy::SMI_VALID;
339 fl_clear_browser(dialog_->browser_kill);
340 for (vector<string>::const_iterator it = pid_vec.begin();
341 it != pid_vec.end(); ++it) {
342 fl_add_browser_line(dialog_->browser_kill, it->c_str());
345 if (fl_get_browser_maxline(dialog_->browser_kill) >= 1)
346 fl_set_browser_topline(dialog_->browser_kill, 1);
349 setEnabled(dialog_->browser_children, false);
350 setEnabled(dialog_->button_add, false);
351 setEnabled(dialog_->button_all, false);
352 setEnabled(dialog_->browser_kill, true);
353 setEnabled(dialog_->button_remove, true);
359 ButtonPolicy::SMInput FormForks::input_button_add()
361 // Pressed the "Add" button.
363 // 1. Copy the PID of the selected item in the browser of all child
364 // processes over into the browser of processes to kill.
366 // 2. Activate the "kill" browser and the "remove" button.
368 // 3. Deactivate the "add" button.
370 string const selection = getString(dialog_->browser_children);
372 split(selection, pid_str, '\t');
374 vector<string> const kill_vec =
375 getVector(dialog_->browser_kill);
377 vector<string>::const_iterator it =
378 find(kill_vec.begin(), kill_vec.end(), pid_str);
380 if (it == kill_vec.end()) {
381 fl_add_browser_line(dialog_->browser_kill, pid_str.c_str());
382 int const n = fl_get_browser_maxline(dialog_->browser_kill);
383 fl_select_browser_line(dialog_->browser_kill, n);
386 setEnabled(dialog_->browser_kill, true);
387 setEnabled(dialog_->button_remove, true);
388 setEnabled(dialog_->button_add, false);
390 return ButtonPolicy::SMI_VALID;
394 ButtonPolicy::SMInput FormForks::input_button_remove()
396 // Pressed the "Remove" button.
398 // 1. Remove the selected item in the browser of processes to kill.
400 // 2. Activate the "add" button and "all" buttons.
402 // 3. Deactivate the "remove" button.
404 int const sel = fl_get_browser(dialog_->browser_kill);
405 fl_delete_browser_line(dialog_->browser_kill, sel);
407 setEnabled(dialog_->button_add, true);
408 setEnabled(dialog_->button_all, true);
409 setEnabled(dialog_->button_remove, false);
411 return ButtonPolicy::SMI_VALID;