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
15 #pragma implementation
19 #include "FormForks.h"
20 #include "ControlForks.h"
21 #include "forms/form_forks.h"
23 #include "helper_funcs.h"
24 #include "xforms_helpers.h"
26 #include "support/lstrings.h"
27 #include FORMS_H_LOCATION
33 typedef FormCB<ControlForks, FormDB<FD_forks> > base_class;
35 FormForks::FormForks()
36 : base_class(_("Child processes"))
40 void FormForks::build() {
41 dialog_.reset(build_forks(this));
43 // It appears that the browsers aren't initialised properly.
44 // This fudge fixes tings.
45 fl_add_browser_line(dialog_->browser_children, " ");
46 fl_add_browser_line(dialog_->browser_kill, " ");
47 fl_clear_browser(dialog_->browser_children);
48 fl_clear_browser(dialog_->browser_kill);
50 // Manage the ok, apply, restore and cancel/close buttons
51 bc().setOK(dialog_->button_ok);
52 bc().setApply(dialog_->button_apply);
53 bc().setCancel(dialog_->button_close);
56 // Set up the tooltip mechanism
57 string str = _("All currently running child processes forked by LyX.");
58 tooltips().init(dialog_->browser_children, str);
60 str = _("A list of all child processes to kill.");
61 tooltips().init(dialog_->browser_kill, str);
63 str = _("Add all processes to the list of processes to kill.");
64 tooltips().init(dialog_->button_all, str);
66 str = _("Add the currently selected child process to the list of processes to kill.");
67 tooltips().init(dialog_->button_add, str);
69 str = _("Remove the currently selected item from the list of processes to kill.");
70 tooltips().init(dialog_->button_remove, str);
74 void FormForks::update()
79 string const current_pid_str =
80 getString(dialog_->browser_kill);
81 pid_t const current_pid = strToInt(current_pid_str);
83 vector<pid_t> pids = controller().getPIDs();
85 // No child processes.
87 if (fl_get_browser_maxline(dialog_->browser_kill) > 0)
88 fl_clear_browser(dialog_->browser_kill);
89 if (fl_get_browser_maxline(dialog_->browser_children) > 0)
90 fl_clear_browser(dialog_->browser_children);
92 setEnabled(dialog_->browser_children, false);
93 setEnabled(dialog_->browser_kill, false);
94 setEnabled(dialog_->button_all, false);
95 setEnabled(dialog_->button_add, false);
96 setEnabled(dialog_->button_remove, false);
101 // Remove any processes from the kill browser that aren't in the
102 // vector of existing PIDs.
103 for (int i = 1; i <= fl_get_browser_maxline(dialog_->browser_kill);
105 string const pid_str =
106 getString(dialog_->browser_kill, i);
107 pid_t const pid = strToInt(pid_str);
108 vector<pid_t>::const_iterator it =
109 find(pids.begin(), pids.end(), pid);
110 if (it == pids.end())
111 fl_delete_browser_line(dialog_->browser_kill, i);
114 // Build the children browser from scratch.
115 if (fl_get_browser_maxline(dialog_->browser_children) > 0)
116 fl_clear_browser(dialog_->browser_children);
118 for (vector<pid_t>::const_iterator it = pids.begin();
119 it != pids.end(); ++it) {
120 string const pid_str = tostr(*it);
121 string const command = controller().getCommand(*it);
122 string const line = pid_str + '\t' + command;
124 fl_add_browser_line(dialog_->browser_children, line.c_str());
126 if (*it == current_pid)
127 fl_select_browser_line(dialog_->browser_children, i);
131 setEnabled(dialog_->browser_children, true);
132 setEnabled(dialog_->button_all, true);
133 setEnabled(dialog_->button_add, true);
137 void FormForks::apply()
139 // Get the list of all processes to kill.
140 vector<string> const kill_vec =
141 getVector(dialog_->browser_kill);
143 if (kill_vec.empty())
146 // Remove these items from the vector of child processes.
147 for (int i = 1; i <= fl_get_browser_maxline(dialog_->browser_children);
149 string const selection =
150 getString(dialog_->browser_children, i);
152 split(selection, pid_str, '\t');
154 vector<string>::const_iterator it =
155 find(kill_vec.begin(), kill_vec.end(), pid_str);
157 if (it != kill_vec.end())
158 fl_delete_browser_line(dialog_->browser_children, i);
161 // Clear the kill browser and deactivate appropriately.
162 fl_clear_browser(dialog_->browser_kill);
163 setEnabled(dialog_->browser_kill, false);
164 setEnabled(dialog_->button_remove, false);
166 // Pass these pids to the controller for destruction.
167 for (vector<string>::const_iterator it = kill_vec.begin();
168 it != kill_vec.end(); ++it) {
169 pid_t const pid = strToInt(*it);
170 controller().kill(pid);
176 ButtonPolicy::SMInput FormForks::input(FL_OBJECT * ob, long)
178 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
180 if (ob == dialog_->browser_children) {
181 activate = input_browser_children();
183 } else if (ob == dialog_->browser_kill) {
184 activate = input_browser_kill();
186 } else if (ob == dialog_->button_all) {
187 activate = input_button_all();
189 } else if (ob == dialog_->button_add) {
190 activate = input_button_add();
192 } else if (ob == dialog_->button_remove) {
193 activate = input_button_remove();
199 ButtonPolicy::SMInput FormForks::input_browser_children()
201 // Selected an item in the browser containing a list of all child
204 // 1. Highlight this item in the browser of processes to kill
205 // if it is already there.
207 // 2. If it is there, enable the remove button so that it can
208 // be removed from this list, if so desired.
210 // 3. If it isn't there, activate the add button so that it can
211 // be added to this list if so desired.
213 string const selection =
214 getString(dialog_->browser_children);
216 split(selection, pid_str, '\t');
218 vector<string> const kill_vec =
219 getVector(dialog_->browser_kill);
221 vector<string>::const_iterator it =
222 find(kill_vec.begin(), kill_vec.end(), pid_str);
224 fl_deselect_browser(dialog_->browser_kill);
225 if (it != kill_vec.end()) {
226 int const n = int(it - kill_vec.begin());
227 fl_select_browser_line(dialog_->browser_kill, n+1);
228 fl_set_browser_topline(dialog_->browser_kill, n+1);
231 setEnabled(dialog_->button_remove, it != kill_vec.end());
232 setEnabled(dialog_->button_add, it == kill_vec.end());
234 return ButtonPolicy::SMI_NOOP;
242 FindPID(string const & pid) : pid_(pid) {}
243 bool operator()(string const & line)
249 split(line, pid_str, '\t');
250 return pid_str == pid_;
260 ButtonPolicy::SMInput FormForks::input_browser_kill()
262 // Selected an item in the browser containing a list of processes
265 // 1. Highlight this item in the browser of all child processes.
267 // 2. Enable the remove button so that it can removed from this list,
270 // 3. Disable the add button.
272 string const pid_str =
273 getString(dialog_->browser_kill);
275 // Find this string in the list of all child processes
276 vector<string> const child_vec =
277 getVector(dialog_->browser_children);
279 vector<string>::const_iterator it =
280 find_if(child_vec.begin(), child_vec.end(), FindPID(pid_str));
282 fl_deselect_browser(dialog_->browser_children);
283 if (it != child_vec.end()) {
284 int const n = int(it - child_vec.begin());
285 fl_select_browser_line(dialog_->browser_children, n+1);
286 fl_set_browser_topline(dialog_->browser_children, n+1);
289 setEnabled(dialog_->button_remove, true);
290 setEnabled(dialog_->button_add, false);
292 return ButtonPolicy::SMI_NOOP;
298 vector<string> const getPIDvector(FL_OBJECT * ob)
300 vector<string> vec = getVector(ob);
304 for (vector<string>::iterator it = vec.begin(); it != vec.end(); ++it) {
306 split(*it, pid_str, '\t');
316 ButtonPolicy::SMInput FormForks::input_button_all()
318 // Pressed the "All" button.
320 // 1. Check that the browser of processes to kill doesn't already
321 // contain the entire list.
323 // 2. If it doesn't, copy the PIDs of all child processes into the
324 // browser of processes to kill.
326 // 3. Deactivate the "children" browser and the "add" and "all" buttons
328 // 4. Activate the "kill" browser and the "remove" button"
330 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
332 vector<string> const pid_vec = getPIDvector(dialog_->browser_children);
334 // to resolve a warning about comparison between signed and unsigned.
335 int const pid_vec_size = int(pid_vec.size());
337 if (fl_get_browser_maxline(dialog_->browser_kill) != pid_vec_size) {
338 activate = ButtonPolicy::SMI_VALID;
340 fl_clear_browser(dialog_->browser_kill);
341 for (vector<string>::const_iterator it = pid_vec.begin();
342 it != pid_vec.end(); ++it) {
343 fl_add_browser_line(dialog_->browser_kill, it->c_str());
346 if (fl_get_browser_maxline(dialog_->browser_kill) >= 1)
347 fl_set_browser_topline(dialog_->browser_kill, 1);
350 setEnabled(dialog_->browser_children, false);
351 setEnabled(dialog_->button_add, false);
352 setEnabled(dialog_->button_all, false);
353 setEnabled(dialog_->browser_kill, true);
354 setEnabled(dialog_->button_remove, true);
360 ButtonPolicy::SMInput FormForks::input_button_add()
362 // Pressed the "Add" button.
364 // 1. Copy the PID of the selected item in the browser of all child
365 // processes over into the browser of processes to kill.
367 // 2. Activate the "kill" browser and the "remove" button.
369 // 3. Deactivate the "add" button.
371 string const selection = getString(dialog_->browser_children);
373 split(selection, pid_str, '\t');
375 vector<string> const kill_vec =
376 getVector(dialog_->browser_kill);
378 vector<string>::const_iterator it =
379 find(kill_vec.begin(), kill_vec.end(), pid_str);
381 if (it == kill_vec.end()) {
382 fl_add_browser_line(dialog_->browser_kill, pid_str.c_str());
383 int const n = fl_get_browser_maxline(dialog_->browser_kill);
384 fl_select_browser_line(dialog_->browser_kill, n);
387 setEnabled(dialog_->browser_kill, true);
388 setEnabled(dialog_->button_remove, true);
389 setEnabled(dialog_->button_add, false);
391 return ButtonPolicy::SMI_VALID;
395 ButtonPolicy::SMInput FormForks::input_button_remove()
397 // Pressed the "Remove" button.
399 // 1. Remove the selected item in the browser of processes to kill.
401 // 2. Activate the "add" button and "all" buttons.
403 // 3. Deactivate the "remove" button.
405 int const sel = fl_get_browser(dialog_->browser_kill);
406 fl_delete_browser_line(dialog_->browser_kill, sel);
408 setEnabled(dialog_->button_add, true);
409 setEnabled(dialog_->button_all, true);
410 setEnabled(dialog_->button_remove, false);
412 return ButtonPolicy::SMI_VALID;