3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
14 #include "FormForks.h"
15 #include "ControlForks.h"
16 #include "forms/form_forks.h"
19 #include "xforms_helpers.h"
22 #include "controllers/ButtonController.h"
24 #include "support/lstrings.h"
25 #include "support/tostr.h"
27 #include "lyx_forms.h"
29 using namespace lyx::support;
37 typedef FormCB<ControlForks, FormDB<FD_forks> > base_class;
39 FormForks::FormForks()
40 : base_class(_("Child Processes"))
44 void FormForks::build() {
45 dialog_.reset(build_forks(this));
47 // It appears that the browsers aren't initialised properly.
48 // This fudge fixes tings.
49 fl_add_browser_line(dialog_->browser_children, " ");
50 fl_add_browser_line(dialog_->browser_kill, " ");
51 fl_clear_browser(dialog_->browser_children);
52 fl_clear_browser(dialog_->browser_kill);
54 // Manage the ok, apply, restore and cancel/close buttons
55 bcview().setOK(dialog_->button_ok);
56 bcview().setApply(dialog_->button_apply);
57 bcview().setCancel(dialog_->button_close);
60 // Set up the tooltip mechanism
61 string str = _("All currently running child processes forked by LyX.");
62 tooltips().init(dialog_->browser_children, str);
63 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
64 // Work-around xforms' bug; enable tooltips for browser widgets.
65 setPrehandler(dialog_->browser_children);
68 str = _("A list of all child processes to kill.");
69 tooltips().init(dialog_->browser_kill, str);
70 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
71 // Work-around xforms' bug; enable tooltips for browser widgets.
72 setPrehandler(dialog_->browser_kill);
75 str = _("Add all processes to the list of processes to kill.");
76 tooltips().init(dialog_->button_all, str);
78 str = _("Add the currently selected child process to the list of processes to kill.");
79 tooltips().init(dialog_->button_add, str);
81 str = _("Remove the currently selected item from the list of processes to kill.");
82 tooltips().init(dialog_->button_remove, str);
86 void FormForks::update()
91 string const current_pid_str =
92 getString(dialog_->browser_kill);
93 pid_t const current_pid = strToInt(current_pid_str);
95 vector<pid_t> pids = controller().getPIDs();
97 // No child processes.
99 if (fl_get_browser_maxline(dialog_->browser_kill) > 0)
100 fl_clear_browser(dialog_->browser_kill);
101 if (fl_get_browser_maxline(dialog_->browser_children) > 0)
102 fl_clear_browser(dialog_->browser_children);
104 setEnabled(dialog_->browser_children, false);
105 setEnabled(dialog_->browser_kill, false);
106 setEnabled(dialog_->button_all, false);
107 setEnabled(dialog_->button_add, false);
108 setEnabled(dialog_->button_remove, false);
113 // Remove any processes from the kill browser that aren't in the
114 // vector of existing PIDs.
115 for (int i = 1; i <= fl_get_browser_maxline(dialog_->browser_kill);
117 string const pid_str =
118 getString(dialog_->browser_kill, i);
119 pid_t const pid = strToInt(pid_str);
120 vector<pid_t>::const_iterator it =
121 find(pids.begin(), pids.end(), pid);
122 if (it == pids.end())
123 fl_delete_browser_line(dialog_->browser_kill, i);
126 // Build the children browser from scratch.
127 if (fl_get_browser_maxline(dialog_->browser_children) > 0)
128 fl_clear_browser(dialog_->browser_children);
130 for (vector<pid_t>::const_iterator it = pids.begin();
131 it != pids.end(); ++it) {
132 string const pid_str = tostr(*it);
133 string const command = controller().getCommand(*it);
134 string const line = pid_str + '\t' + command;
136 fl_add_browser_line(dialog_->browser_children, line.c_str());
138 if (*it == current_pid)
139 fl_select_browser_line(dialog_->browser_children, i);
143 setEnabled(dialog_->browser_children, true);
144 setEnabled(dialog_->button_all, true);
145 setEnabled(dialog_->button_add, true);
149 void FormForks::apply()
151 // Get the list of all processes to kill.
152 vector<string> const kill_vec =
153 getVector(dialog_->browser_kill);
155 if (kill_vec.empty())
158 // Remove these items from the vector of child processes.
159 for (int i = 1; i <= fl_get_browser_maxline(dialog_->browser_children);
161 string const selection =
162 getString(dialog_->browser_children, i);
164 split(selection, pid_str, '\t');
166 vector<string>::const_iterator it =
167 find(kill_vec.begin(), kill_vec.end(), pid_str);
169 if (it != kill_vec.end())
170 fl_delete_browser_line(dialog_->browser_children, i);
173 // Clear the kill browser and deactivate appropriately.
174 fl_clear_browser(dialog_->browser_kill);
175 setEnabled(dialog_->browser_kill, false);
176 setEnabled(dialog_->button_remove, false);
178 // Pass these pids to the controller for destruction.
179 for (vector<string>::const_iterator it = kill_vec.begin();
180 it != kill_vec.end(); ++it) {
181 pid_t const pid = strToInt(*it);
182 controller().kill(pid);
188 ButtonPolicy::SMInput FormForks::input(FL_OBJECT * ob, long)
190 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
192 if (ob == dialog_->browser_children) {
193 activate = input_browser_children();
195 } else if (ob == dialog_->browser_kill) {
196 activate = input_browser_kill();
198 } else if (ob == dialog_->button_all) {
199 activate = input_button_all();
201 } else if (ob == dialog_->button_add) {
202 activate = input_button_add();
204 } else if (ob == dialog_->button_remove) {
205 activate = input_button_remove();
211 ButtonPolicy::SMInput FormForks::input_browser_children()
213 // Selected an item in the browser containing a list of all child
216 // 1. Highlight this item in the browser of processes to kill
217 // if it is already there.
219 // 2. If it is there, enable the remove button so that it can
220 // be removed from this list, if so desired.
222 // 3. If it isn't there, activate the add button so that it can
223 // be added to this list if so desired.
225 string const selection =
226 getString(dialog_->browser_children);
228 split(selection, pid_str, '\t');
230 vector<string> const kill_vec =
231 getVector(dialog_->browser_kill);
233 vector<string>::const_iterator it =
234 find(kill_vec.begin(), kill_vec.end(), pid_str);
236 fl_deselect_browser(dialog_->browser_kill);
237 if (it != kill_vec.end()) {
238 int const n = int(it - kill_vec.begin());
239 fl_select_browser_line(dialog_->browser_kill, n+1);
240 fl_set_browser_topline(dialog_->browser_kill, n+1);
243 setEnabled(dialog_->button_remove, it != kill_vec.end());
244 setEnabled(dialog_->button_add, it == kill_vec.end());
246 return ButtonPolicy::SMI_NOOP;
254 FindPID(string const & pid) : pid_(pid) {}
255 bool operator()(string const & line)
261 split(line, pid_str, '\t');
262 return pid_str == pid_;
272 ButtonPolicy::SMInput FormForks::input_browser_kill()
274 // Selected an item in the browser containing a list of processes
277 // 1. Highlight this item in the browser of all child processes.
279 // 2. Enable the remove button so that it can removed from this list,
282 // 3. Disable the add button.
284 string const pid_str =
285 getString(dialog_->browser_kill);
287 // Find this string in the list of all child processes
288 vector<string> const child_vec =
289 getVector(dialog_->browser_children);
291 vector<string>::const_iterator it =
292 find_if(child_vec.begin(), child_vec.end(), FindPID(pid_str));
294 fl_deselect_browser(dialog_->browser_children);
295 if (it != child_vec.end()) {
296 int const n = int(it - child_vec.begin());
297 fl_select_browser_line(dialog_->browser_children, n+1);
298 fl_set_browser_topline(dialog_->browser_children, n+1);
301 setEnabled(dialog_->button_remove, true);
302 setEnabled(dialog_->button_add, false);
304 return ButtonPolicy::SMI_NOOP;
310 vector<string> const getPIDvector(FL_OBJECT * ob)
312 vector<string> vec = getVector(ob);
316 for (vector<string>::iterator it = vec.begin(); it != vec.end(); ++it) {
318 split(*it, pid_str, '\t');
328 ButtonPolicy::SMInput FormForks::input_button_all()
330 // Pressed the "All" button.
332 // 1. Check that the browser of processes to kill doesn't already
333 // contain the entire list.
335 // 2. If it doesn't, copy the PIDs of all child processes into the
336 // browser of processes to kill.
338 // 3. Deactivate the "children" browser and the "add" and "all" buttons
340 // 4. Activate the "kill" browser and the "remove" button"
342 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
344 vector<string> const pid_vec = getPIDvector(dialog_->browser_children);
346 // to resolve a warning about comparison between signed and unsigned.
347 int const pid_vec_size = int(pid_vec.size());
349 if (fl_get_browser_maxline(dialog_->browser_kill) != pid_vec_size) {
350 activate = ButtonPolicy::SMI_VALID;
352 fl_clear_browser(dialog_->browser_kill);
353 for (vector<string>::const_iterator it = pid_vec.begin();
354 it != pid_vec.end(); ++it) {
355 fl_add_browser_line(dialog_->browser_kill, it->c_str());
358 if (fl_get_browser_maxline(dialog_->browser_kill) >= 1)
359 fl_set_browser_topline(dialog_->browser_kill, 1);
362 setEnabled(dialog_->browser_children, false);
363 setEnabled(dialog_->button_add, false);
364 setEnabled(dialog_->button_all, false);
365 setEnabled(dialog_->browser_kill, true);
366 setEnabled(dialog_->button_remove, true);
372 ButtonPolicy::SMInput FormForks::input_button_add()
374 // Pressed the "Add" button.
376 // 1. Copy the PID of the selected item in the browser of all child
377 // processes over into the browser of processes to kill.
379 // 2. Activate the "kill" browser and the "remove" button.
381 // 3. Deactivate the "add" button.
383 string const selection = getString(dialog_->browser_children);
385 split(selection, pid_str, '\t');
387 vector<string> const kill_vec =
388 getVector(dialog_->browser_kill);
390 vector<string>::const_iterator it =
391 find(kill_vec.begin(), kill_vec.end(), pid_str);
393 if (it == kill_vec.end()) {
394 fl_add_browser_line(dialog_->browser_kill, pid_str.c_str());
395 int const n = fl_get_browser_maxline(dialog_->browser_kill);
396 fl_select_browser_line(dialog_->browser_kill, n);
399 setEnabled(dialog_->browser_kill, true);
400 setEnabled(dialog_->button_remove, true);
401 setEnabled(dialog_->button_add, false);
403 return ButtonPolicy::SMI_VALID;
407 ButtonPolicy::SMInput FormForks::input_button_remove()
409 // Pressed the "Remove" button.
411 // 1. Remove the selected item in the browser of processes to kill.
413 // 2. Activate the "add" button and "all" buttons.
415 // 3. Deactivate the "remove" button.
417 int const sel = fl_get_browser(dialog_->browser_kill);
418 fl_delete_browser_line(dialog_->browser_kill, sel);
420 setEnabled(dialog_->button_add, true);
421 setEnabled(dialog_->button_all, true);
422 setEnabled(dialog_->button_remove, false);
424 return ButtonPolicy::SMI_VALID;