#include "server.h"

#include XColor_i
#include XMessageBox_i
#include XMenuBar_i
#include XString_i
#include XDDE_i
#include XControlEvent_i

#include <stdlib.h>

SHORT volume = 50;

class MyDDE: public XDDE
{  
   public:      
      MyDDE( XWindow * w):XDDE(w, "Sample8", "SampleTopic", TRUE){clients=NULL;clientCount = 0;}
      void Connected( OOL_WINDOWHANDLE client );
      void AdviseRequested(char * itemName, SHORT format, OOL_WINDOWHANDLE client);
      void DisConnected();
      void UnAdviseRequested( OOL_WINDOWHANDLE c);
      SHORT clientCount;
      OOL_WINDOWHANDLE * clients;
};


void MyDDE :: UnAdviseRequested( OOL_WINDOWHANDLE c)
{
   int i;
   for(i=0; i < clientCount; i++)
      {
         if( clients[i] == c)
            clients[i] = 0;
      }
}


void MyDDE :: AdviseRequested(char * itemName, SHORT format, OOL_WINDOWHANDLE c)
{
   clientCount += 1;
   clients = (OOL_WINDOWHANDLE *) realloc( clients, clientCount * sizeof(OOL_WINDOWHANDLE));
   clients[clientCount -1] = c;
}


void MyDDE :: Connected( OOL_WINDOWHANDLE c )
{
   GetOwner()->SetText("DDEServer - connected");
}


void MyDDE :: DisConnected( )
{
   GetOwner()->SetText("DDEServer - not connected");
}


MyAppWindow :: MyAppWindow( XApplication * app, XResource * r ): XFrameWindow( r, "Sample8 - DDEServer", XFrameWindow::defaultDialogStyle | FRM_TASKLIST | FRM_CENTER, NULL, NULL, TRUE)
{
   Show();

   dde = new MyDDE(this );
   Activate();
}


MyAppWindow :: ~MyAppWindow()
{
   delete dde;
}


void MyAppWindow :: DoControl( XControlEvent * event)
{
   if( event->GetEventID() == WIN_KILLFOCUS)
      {
         char * item = NULL;
         switch( event->GetWindowID() )
            {
               case ENTRY_ONE:
                  item = "FIELD1";
                  break;
               case ENTRY_TWO:
                  item = "FIELD2";
                  break;
               case ENTRY_THREE:
                  item = "FIELD3";
                  break;
               case ENTRY_FOUR:
                  item = "FIELD4";
                  break;
            }
         if( item) //dde->IsConnected() && 
            {
               XString data;
               event->GetWindow()->GetText( &data );
               SHORT i;
               for(i=0; i < dde->clientCount; i++)
                  {                    
                     if( dde->clients[i] != 0)
                        dde->SendData( dde->clients[i], item, (char*) data, data.GetLength() + 1, 0);
                  }
            }
      }
}


MyApp :: MyApp(): XApplication()
{
   XResource r( IDM_MAIN, GetResourceLibrary());
   window = new MyAppWindow( this, &r );   //create new framewindow (see above)
}


void main ( void)
{
   MyApp * app = new MyApp();  //create a new application
   app->Start();               //let the application work
}
