#!/usr/bin/perl -w
#-*-perl-*-
# Autogenerated by fd2pl from fdesign file ProcCntl.fd
#

use Xforms;

fl_initialize('ProcCntl');
fl_set_defaults(FL_PDBorderWidth, -2);
$ProcCntl = undef;
@Processes = undef;
$SelectedProcs = undef;
$Browser = undef;
$Menu = undef;
$autoupdate = 1;
$menuautoupdate = 1;
create_form_ProcCntl();
PopulateBrowser();
fl_show_form($ProcCntl, FL_PLACE_FREE, FL_FULLBORDER, "Processes");
fl_add_timeout(10000, "AutoUpdate", 0);
fl_do_forms();
exit 0;

sub PopulateBrowser {

	$top = fl_get_browser_topline($Browser);
	$newtop = -1;
	if (!defined($topproc = $Processes[$top-1])) {
		$topproc = 0;
	}
	@Processes = ();
	@SelectedProcs = ();
	fl_freeze_form($ProcCntl); 
	fl_clear_browser($Browser);
	open (PROCS, 'ps -xafw |');
	$linenum = 0;
	while ($procline = <PROCS>) {
		if ($procline =~ /\s*(\d\d*).*:\d\d (.*)/) {
			$linenum++;
			$procnum = $1;
			$proctext = $2;
			push (@Processes,$procnum);
			$newtop = $linenum if ($procnum == $topproc);
			fl_add_browser_line($Browser,$proctext);
		}
	}
	if ($newtop == -1) {
		if ($top <= $linenum) {
			$newtop = $top;
		} else {
			$newtop = $linenum;
		}
	}
	fl_set_browser_topline($Browser, $newtop);
	fl_set_menu_item_mode($Menu, 2, FL_PUP_GRAY);
	fl_unfreeze_form($ProcCntl);
	close PROCS;
}
			
sub SelectBrows {

	$selline = fl_get_browser($Browser);

	if ($selline > 0) {
		push (@SelectedProcs, $Processes[$selline-1]);
	} else {
		for ($i = 0; $SelectedProcs[$i] != $Processes[(-$selline)-1]; $i++) {}
		splice (@SelectedProcs, $i, 1);
	}
	if (@SelectedProcs) {
		fl_set_menu_item_mode($Menu, 2, FL_PUP_NONE);
		$autoupdate = 0;
	} else { 		
		fl_set_menu_item_mode($Menu, 2, FL_PUP_GRAY);
		$autoupdate = $menuautoupdate;
	}

}

sub AutoUpdate {
	my($num, $obj) = @_;
	PopulateBrowser() if ($autoupdate);
	fl_add_timeout(10000, "AutoUpdate", 0);
}

sub ProcMenu {

	$menuitem = fl_get_menu($Menu);
	if ($menuitem == 5) {
		fl_finish();
		exit;
	} elsif ($menuitem == 4) {
		$menuautoupdate = $autoupdate = !$menuautoupdate;
	} elsif ($menuitem == 3) {
		PopulateBrowser();
		$autoupdate = $menuautoupdate;
	} elsif ($menuitem == 2 && @SelectedProcs) {
		kill (9, @SelectedProcs);  
		PopulateBrowser();
		$autoupdate = $menuautoupdate;
	}

}

sub create_form_ProcCntl {
  $obj = undef;
  $ProcCntl = fl_bgn_form(FL_NO_BOX, 300, 260);
  $obj = fl_add_box(FL_FLAT_BOX, 0, 0, 300, 260, "");
  $obj = $Browser = fl_add_browser(FL_MULTI_BROWSER, 0, 22, 300, 237, "");
  fl_set_object_gravity($obj, FL_NorthWest, FL_SouthEast);
  fl_set_object_lstyle($obj, FL_BOLD_STYLE);
  fl_set_object_callback($obj, "SelectBrows", 0);
  fl_set_browser_fontsize($obj, FL_NORMAL_SIZE);
  fl_set_browser_fontstyle($obj, FL_FIXED_STYLE);
  fl_set_object_resize($obj, FL_RESIZE_X);
  $obj = $Menu = fl_add_menu(FL_PULLDOWN_MENU, 2, 5, 60, 15, "Process");
  fl_set_object_resize($obj,FL_RESIZE_NONE);
  fl_set_object_gravity($obj, FL_NorthWest, FL_NorthWest);
  fl_set_object_shortcut($obj, "P", 1);
  fl_set_object_boxtype($obj, FL_FLAT_BOX);
  fl_set_object_lsize($obj, FL_NORMAL_SIZE);
  fl_set_object_lstyle($obj, FL_BOLD_STYLE);
  fl_setpup_fontstyle(FL_BOLD_STYLE);
  fl_set_menu($obj, "Details|Kill%l|Refresh Now|Auto Refresh%l|Exit");
  fl_set_menu_item_mode($obj, 1, FL_PUP_GRAY);
  fl_set_menu_item_mode($obj, 2, FL_PUP_GRAY);
  fl_set_menu_item_mode($obj, 4, FL_PUP_CHECK | FL_PUP_TOGGLE);
  fl_set_object_callback($obj, "ProcMenu", 0);

  fl_end_form();
}

sub create_the_forms {
  create_form_ProcCntl();
}
1;
