#include <xt++/Intrinsic.h>
//#include <xt++/AppContext.h>
//#include <xt++/Core.h>
//#include <xt++/Composite.h>

#include <xt++/Shell.h>
#include <xt++/Xaw/Label.h>
#include <xt++/Xaw/Command.h>
#include <xt++/Xaw/Form.h>
//#include <xt++/Xaw/Dialog.h>
#include <xt++/Xaw/Clock.h>
#include "swatch.h"
//#include "debug.h"
#include <X11/StringDefs.h>
#include <libc.h>
#include <stream.h>

void QuitCB(Core* , XtPointer p, XtPointer)
{
   StopWatch* sw = (StopWatch*)p;
   sw->Stop();
   sw->Save();
   exit(0);
}

void SaveCB(Core*  , XtPointer p, XtPointer)
{
   StopWatch* sw = (StopWatch*)p;
   sw->Save();
}

void StopGoCB(Core* w , XtPointer p, XtPointer)
{
   CommandWidget* cw = (CommandWidget*)w;
   StopWatch* sw = (StopWatch*)p;

//   sw->PrintState("StopGo");

   if(sw->IsRunning() == False){
      cw->SetValues(XtNlabel,(XtArgVal)"Stop",XtNlabel,(XtArgVal)"Stop",NULL);
      sw->Go();
   }
   else{
      cw->SetValues(XtNlabel,(XtArgVal)"Go",XtNlabel,(XtArgVal)"Go",NULL);
      sw->Stop();
   }
   XBell(w->XtDisplay(),50);
}

main(unsigned int argc, char** argv)
{
   ofstream  f("hours.save",ios::out|ios::app);

   f << "Hours\n";

   ApplicationContext appContext;

//   appContext.AddActions(actions,XtNumber(actions));

   Display *dpy =  appContext.OpenDisplay(NULL, argv[0], "Watch",
                        NULL,0,
                        &argc, argv);

   ApplicationShellWidget aw( argv[0], "Watch", dpy);

   FormWidget    form("form", aw);
   form.Manage();

   StopWatch sw(&form, f, 1);
   sw.Manage();
//   sw->Open(f);
//   sw->PrintState("main 1");

   ClockWidget clock("clock", form);
   clock.Manage();
	
   CommandWidget quit("quit", form);
   quit.AddCallback(XtNcallback, QuitCB, sw);
   quit.Manage();

   CommandWidget stopgo("stopgo", form);
   stopgo.AddCallback(XtNcallback, StopGoCB, sw);
   stopgo.Manage();

   CommandWidget save("save", form);
   save.AddCallback(XtNcallback, SaveCB, sw);
   save.Manage();

   aw.Realize();

   sw.Stop();

   //w->PrintState("main 2");

   appContext.MainLoop();

}

								     
