#!/usr/bin/perl -w

use strict;
use Gtk2;
use Glib qw/TRUE FALSE/;

init Gtk2;

sub quit_event {
	Gtk2->main_quit;
	return FALSE;
}

sub get_data {
	my @record;
	my $store=Gtk2::ListStore->new(("Glib::String")x7);
	open(PASSWD, "< /etc/passwd");
	while ( <PASSWD> )
	{
		chomp();
		my($pw_user,$pw_password,$pw_id,$pw_gid,$pw_rn,$pw_home,$pw_shell) = split /:/;

		$store->set($store->append,
			0=>$pw_user, 1=>$pw_password, 2=>$pw_id,
			3=>$pw_gid, 4=>$pw_rn, 5=>$pw_home, 6=>$pw_shell);
	}
	close(PASSWD);
	$store;
}



my $window = Gtk2::Window->new('toplevel');
$window->set_title("/etc/passwd");
$window->set_default_size(850,400);
$window->signal_connect(delete_event => \&quit_event);

my $vbox = Gtk2::VBox->new(FALSE,0);
my $menu_file = Gtk2::Menu->new();
my $menu_item_quit = Gtk2::ImageMenuItem->new_from_stock('gtk-quit', undef);
$menu_item_quit->signal_connect(activate => \&quit_event);
$menu_file->append($menu_item_quit);

my $menu_bar = Gtk2::MenuBar->new;

my $menu_item_file= Gtk2::MenuItem->new('_File');
$menu_item_file->set_submenu($menu_file);
$menu_bar->append($menu_item_file);

$vbox->pack_start($menu_bar,FALSE,FALSE,0);

my $hbox = Gtk2::HBox->new(FALSE,0);

my $scroll = Gtk2::ScrolledWindow->new();
$scroll->set_policy('never', 'automatic');
my $model=get_data();
my $tview=Gtk2::TreeView->new($model);

my $renderer=Gtk2::CellRendererText->new;
my $n=0;
foreach my $name qw(AccountName Password UID GID Comment HomeDir Shell) {
	my $col=Gtk2::TreeViewColumn->new_with_attributes(
		$name, $renderer, 'text'=>$n);
	$col->set_sort_column_id($n);
 
 	$tview->append_column($col);
 	$n++;
}

$tview->set_grid_lines('GTK_TREE_VIEW_GRID_LINES_HORIZONTAL');

my $clipboard = Gtk2::Clipboard->get(Gtk2::Gdk->SELECTION_CLIPBOARD);

sub button_pressed_event {

	my ($widget, $event) = @_;

	my $treeselection = $tview->get_selection;

	my ($x, $y)=$event->get_coords;
	my $mouse_path=$tview->get_path_at_pos($x, $y);
	if($mouse_path) {
		$treeselection->select_path($mouse_path);
	}

	my $treeiter = $treeselection->get_selected;
	if ( $treeiter ) {
		if ( $event->button == 3) {
			my $value1 = $model->get($treeiter,0);
			my $value2 = $model->get($treeiter,4);
			printf "first: " . $value1 . " second: " . $value2 . "\n";
			my $menu = Gtk2::Menu->new();
			my $menuitem1 = Gtk2::ImageMenuItem->new_from_stock('gtk-copy');
			$menuitem1->signal_connect(activate => sub {
				printf "selected :" . $value1 . "\n";
				$clipboard->set_text($value1);
			});
			my $menuitem2 = Gtk2::MenuItem->new("$value2");
	
			$menu->add($menuitem1);
			$menu->add($menuitem2);
			$menu->show_all;
			$menu->popup(
				undef, # parent menu shell
				undef, # parent menu item
				undef, # menu pos func
				undef, # data
				3,
				$event->time
			);
			return TRUE;
		}
	} else {
		printf "No row selected\n";
	}
	return FALSE;
}


$tview->signal_connect("button-press-event",\&button_pressed_event );

$scroll->add($tview);
$hbox->add($scroll);
$vbox->add($hbox);

$window->add($vbox);

$window->show_all;

Gtk2->main;

0;
