#!/usr/local/bin/perl -w
# -*- perl -*-

#
# $Id: bbbikeapplet,v 1.3 2001/03/25 17:12:53 eserte Exp $
# Author: Slaven Rezic
#
# Copyright (C) 2001 Slaven Rezic. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: eserte@cs.tu-berlin.de
# WWW:  http://user.cs.tu-berlin.de/~eserte/
#

# XXX kann noch nicht aus dem Panel heraus aktiviert werden
# vgl. Sourcen von fish_applet: APPLET_(DE)ACTIVATE müsste irgendwie
# implementiert werden

# I need to switch to Gnome2::PanelApplet. But the discussion here
# http://mail.gnome.org/archives/gtk-perl-list/2007-February/msg00000.html
# suggests that it does not work. But this was before release 0.02.
# Now at least the sample applet works, but the nmea log trial, which
# was basically copied from the sample, does not?!

use Gtk2;
use Getopt::Long;
use FindBin;
use strict;

use vars qw($mw
	    $is_applet $goad_server $goad_fd);
$is_applet = 1;
# XXX is_applet is not actually necessary... try Gnome::Applet with eval
if (!GetOptions("applet!" => \$is_applet,
		"activate-goad-server=s" => \$goad_server,
		"goad-fd=s" => \$goad_fd,
	       )) {
    die "usage";
}

#warn $goad_server;
#warn $goad_fd;

if ($is_applet) {
    require Gnome;
    Gnome::Panel::AppletWidget->init('BBBike applet');
    $mw = Gnome::Panel::AppletWidget->new('BBBike applet');
    realize $mw;
} else {
    Gtk2->init;
    $mw = Gtk2::Window->new(-toplevel);
}

$mw->set_title("BBBike applet");

my $vbox = new Gtk2::VBox(0,0);
$mw->add($vbox);
my $hbox_start = new Gtk2::HBox(0,0);
$vbox->pack_start($hbox_start, 1, 1, 0);
my $hbox_ziel  = new Gtk2::HBox(0,0);
$vbox->pack_start($hbox_ziel, 1, 1, 0);

my $sl = new Gtk2::Label("Start:");
$hbox_start->pack_start($sl, 1, 1, 0);
my $se = new Gtk2::Entry;
$hbox_start->pack_start($se, 1, 1, 0);

my $zl = new Gtk2::Label("Ziel:");
$hbox_ziel->pack_start($zl, 1, 1, 0);
my $ze = new Gtk2::Entry;
$hbox_ziel->pack_start($ze, 1, 1, 0);

$se->signal_connect('activate', \&set_start);
$ze->signal_connect('activate', \&start_bbbike);

$se->grab_focus;
$mw->show_all;

if ($is_applet) {
    Gnome::Panel::AppletWidget->gtk_main;
} else {
    Gtk2->main;
}

sub set_start {
    if (fork == 0) {
	exec $^X, "$FindBin::RealBin/bbbikeclient",
	    "-nostartserver",
	    -from => $se->get_text;
	warn $!;
	CORE::exit();
    }
    $ze->grab_focus;
}

sub start_bbbike {
    if (fork == 0) {
	exec $^X, "$FindBin::RealBin/bbbikeclient",
	    -from => $se->get_text, -to => $ze->get_text;
	warn $!;
	CORE::exit();
    }
    $se->grab_focus;
}

__END__

=head1 NAME

bbbikeapplet - GNOME applet for searching bbbike routes

=head1 SYNOPSIS

    bbbikeapplet [-noapplet]

=head1 DESCRIPTION

=head1 AUTHOR

Slaven Rezic <slaven@rezic.de>

=head1 SEE ALSO

bbbike(1), Gtk2(3), Gnome(3)

=cut
