#!/usr/bin/perl -w

use 5.003;
use strict;
use Gtk;
init Gtk;

use lib `fvwm-perllib dir`;
use FVWM::Module::Gtk;

my $module = new FVWM::Module::Gtk(
	Name => "FvwmPerlGtkWinList",
	Mask => M_WINDOW_NAME | M_END_WINDOWLIST | M_STRING | M_ERROR,
);

my $dialog = new Gtk::Dialog;
$dialog->set_title("Window List");
$dialog->set_border_width(4);

my $smode = 'single';  # initial selection mode, may be changed by user
my $clist = new_with_titles Gtk::CList(" Id" . " " x 13, " Name" . " " x 20);
$clist->set_selection_mode($smode);
$clist->signal_connect("click_column", \&sortListByColumn);
$dialog->vbox->add($clist);

my $creatingWindowList = 0;
sub requestWindowList () {
	$creatingWindowList = 1;
	$clist->freeze;
	$clist->clear;
	$module->send("Send_WindowList");
}

sub doCommand ($) {
	my $command = shift;
	foreach ($clist->selection) {
		$module->send($command, hex($clist->get_text($_, 0)));
	}
}

sub sortListByColumn ($$) {
	my $clist = shift;
	my $column = shift;

	if ($column == $clist->sort_column()) {
		if ($clist->sort_type() eq 'ascending') {
			$clist->set_sort_type('descending');
		} else {
			$clist->set_sort_type('ascending');
		}
	} else {
		$clist->set_sort_column($column);
	}
	$clist->sort;
}

my $button = new Gtk::CheckButton "Enable multiple selection";
$dialog->vbox->add($button);
$button->signal_connect("clicked", sub { $clist->set_selection_mode(
	$smode = $smode eq 'multiple'? 'single': 'multiple'); });

my $hbox = new Gtk::HBox(1, 0);
$dialog->vbox->pack_end($hbox, 1, 1, 8);

$button = new Gtk::Button "Iconify";
$hbox->pack_start($button, 1, 1, 4);
$button->signal_connect("clicked", sub { doCommand("Iconify"); });

$button = new Gtk::Button "WindowShade";
$hbox->pack_start($button, 1, 1, 4);
$button->signal_connect("clicked", sub { doCommand("WindowShade"); });

$button = new Gtk::Button "Refresh";
$dialog->action_area->pack_start($button, 1, 1, 0);
$button->signal_connect("clicked", sub { requestWindowList(); });
 
$button = new Gtk::Button "Quit";
$dialog->action_area->pack_start($button, 1, 1, 0);
$button->signal_connect("clicked", sub { Gtk->main_quit; });

$dialog->show_all;

$module->addHandler(M_WINDOW_NAME, sub {
	my ($self, $event) = @_;
	return unless $creatingWindowList;
	$clist->append(sprintf("0x%07lx", $event->_win_id), $event->_name);
});

$module->addHandler(M_END_WINDOWLIST, sub {
	$creatingWindowList = 0;
	$clist->thaw;
});

$module->addHandler(M_STRING, sub {
	my $string = $_[1]->_line;
	my $winid = $dialog->window->XWINDOW();
	if ($string =~ /^close|stop|quit|exit/i) {
		Gtk->exit(0);
	} elsif ($string =~ /^iconify|maximize|windowshade/i) {
		$module->send($string, $winid)
	} else {
		$module->openErrorDialog("Unsupported message: $string");
	}
});

$module->addDefaultErrorHandler;

requestWindowList();
$module->eventLoop;
