#! /usr/bin/perl -w

##########################################################
#
# Capture and display data from Standard Counter
#
# (C) 2011 Doug Coulter, GPL v2 license -- share and enjoy,
# but if you improve it, be sure and share back, to
# clab@swva.net, or www.coultersmithing.com/forums
#
#
# See use section below for any modules you'll need to install first
##########################################################
# It's probably obvious I'm coding this like I was a C programmer
# (because I am), and squishing fancy expressions into single liners
# would just make the inevitable changes this is going to get harder
# to figure out later -- for a C programmer (me).
#
# Anything I thought was dodgy is marked with an @@@ for searching
#
# If you're a perl purist or stylist, sorry 'bout that.  TMTOWTDI
# and, TANSTAAFL for good measure.
##########################################################

use strict; # make perl tell me about typos and how dumb I am

# bring in a pile of modules, this code is already written and tested by someone else (mostly)

use Gtk2 -init; # sets up GUI basics
use Glib qw(TRUE FALSE);
use Gtk2::GladeXML; # lets GUI come from a glade file
use Graphics::GnuplotIF qw(GnuplotIF); # gnuplot for plots
use File::Spec::Functions qw/:ALL/; # changes things like ./ to a real path
use File::Basename; # to get path to program for config files and logs

# the serial interface "requires" different modules for windows and perl, so they're not up here
# but are decided at runtime after perl tells us which opsys we have


################   Globals
my $gui; # main window
my $ssbut; # start-stop button
my $running = FALSE; # start off stopped...duh
my $makelogbut;  # button reference
my $makelog;     # boolean value
my $makeplotbut; # button ref
my $makeplot;    # button data
my $logwin;	 # text reference
my $logwinbuf;	 # the text buffer reference, special GTK stuff
my $logtext;	 # finally, the actual friggin text itself (text editor windows are complex and a PITA)

################   Subroutines

# to hook this up, in glade use gtk_main_quit as the handler in the mainwin/GtkWidget/destory-event
sub gtk_main_quit  { Gtk2->main_quit; }

# to make this work, I had to select the "after" option for the signal in glade
sub on_gostop_toggled
{
 if ($running)
 { # stop, write log file
  $running = FALSE; # internal boolean flag
  $ssbut->set_label('ReStart'); # relabel button
  print ("\nStopping\n");

 } else # time to start up the world
 { # setup and start things
  $running = TRUE;
  $ssbut->set_label('Stop');
  $makelog = $makelogbut->get_active() ? TRUE : FALSE; # heh, nice one-liner here	
  $makeplot = $makeplotbut->get_active() ? TRUE : FALSE; # this definition for FALSE seems to be "undef"
  print ("\nReset and startup. Makelog:$makelog Makeplot:$makeplot\n");


 } # end if running else "start case"

} # end on_gostop_toggled


sub poll
{ # we'll have GTK call this on quarter seconds to look for a line of input, 
  # do stuff about that, or return if nothing to do




}

################  Main

print ("\nprogram running\n");

$gui = Gtk2::GladeXML->new('StdC.glade');
$ssbut = $gui->get_widget('gostop'); # pointer to start button?
$makelogbut = $gui->get_widget('logfileb'); # pointer to make logfile check button
$makeplotbut = $gui->get_widget('plotb'); # pointer to do-plot check
$logwin = $gui->get_widget('logwin'); # pointer to text display window
$logwinbuf = $logwin->get_buffer(); # later use $logwinbuf->set_text($some_text);

$gui->signal_autoconnect_from_package('main'); # hook subs to signals if things are named right

# Enter the event loop
Gtk2->main;

__END__ 
