/************************************************
 *
 *    GWindow and SignalResponder test
 *
 *    by Armin Vogt, EMail: armin@uni-paderborn.de
 *
 *    #UPDATE 03-Dec-1993 21:08:59
 *
 ************************************************/

#include <APlusPlus/exec/SignalResponder.h>
#include <APlusPlus/intuition/GWindow.h>

#include <dos/dos.h>


BOOL running = TRUE;
BOOL close2 = FALSE;
GWindow *stop_window;

class MySRSP : public SignalResponder
{
   public:
      MySRSP(BYTE signal) : SignalResponder(signal,0) {}

      // overload the virtual 'signal received' action callback method.
      void actionCallback()
      {
         cout << "**Break\n";
         running = FALSE;
      }
};

class MyWindow : public GWindow
{
   private:
      void init();

   public:
      MyWindow(OWNER,TAGLIST) : GWindow(owner,taglist) { init(); }
      MyWindow(OWNER,VARTAGS) : GWindow(owner,TAG1ADR) { init(); }

      void On_CLOSEWINDOW(const IntuiMessageC *msg)
      {
         cout << "CLOSEWINDOW.\n";
         if (this == stop_window) running = FALSE;
         else close2 = TRUE;
      }
      void On_ACTIVEWINDOW(const IntuiMessageC *msg)
      {
         cout << title() << " is ACTIVE.\n";
      }
      void On_SIZEVERIFY(const IntuiMessageC *msg)
      {
         cout << "SIZEVERIFY. \n";
      }
};


void MyWindow::init()
{
   _dout("init\n");
   onMessage(CLASS_CLOSEWINDOW,(OnIMessage)&On_CLOSEWINDOW);
   onMessage(CLASS_ACTIVEWINDOW,(OnIMessage)&On_ACTIVEWINDOW);
   _dout("done.\n");
}


int main(int argc,char *argv[])
{
   IntuiRoot::APPinitialise(argc,argv);

   MySRSP sr(SIGBREAKB_CTRL_C);

   MyWindow *little = new MyWindow(OWNER_NULL,
      WA_Title,"WindowC - close this to stop.",
      WA_Width,300,
      WA_Height,150,
      WA_MinHeight,20,
      WA_DragBar,TRUE,
      WA_SizeGadget,TRUE,
      WA_DepthGadget,TRUE,
      WA_CloseGadget,TRUE,
      WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_ACTIVEWINDOW,
      TAG_END);

   MyWindow *small = new MyWindow(little,
      WCV_SharePortWithWindow,little,
      WA_Title,"WindowC sharing",
      WA_Left,300,
      WA_Width,200,
      WA_Height,150,
      WA_DragBar,TRUE,
      WA_CloseGadget,TRUE,
      //WCV_ShareIDCMPMapper,FALSE,
      TAG_END);

   stop_window = little;

   cout << little << "\n"<<small<<endl;
   cout << "APPObject status: "<<little->status()
   <<"\t"<<little->GraphicObject::status()<<"\t"<<little->IntuiObject::status()<<endl;

   cout <<"sizeof(WindowCV) = "<<sizeof(WindowCV)<<"\n";
   cout <<"sizeof(IntuiObject) = "<<sizeof(IntuiObject)<<"\n";
   cout <<"sizeof(GraphicObject) = "<<sizeof(GraphicObject)<<"\n";

   while (running)
   {
      SignalResponder::WaitSignal();
      if (close2 && small->Ok())
      {
         delete small;
         cout << "small destroyed.\n";
      }
   }

   cout << "replying not yet replied messages and closing port. goodbye." << endl;
   IntuiRoot::APPexit();   // destroy all IntuiObjects
   return TRUE;
}
