/*
* Filename: D:\Studienarbeit\Beispiel3\Beispiel3.cpp
* Version: 1.0
* Date: Tue Sept 8
* Author: junge
*/

#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#endif /*WIN32*/

#ifdef __linux
#include <typeinfo>
#else
#include <typeinfo.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>



#include "XGraph/XGraph.h"
#include "XGraph/AllPrims.h"
#include "XGraph/AllViews.h"

#include <tcl.h>
#include <tk.h>


#include "Messager.h"

#include "Vec3D.h"
#include "Angle.h"
#include "HMatrx3D.h"


Tk_TimerToken timerToken = NULL;
Tcl_Interp *MainInterp = NULL;

RGBAColor bgCol(1.0, 1.0, 1.0);

ToglView *Client;
Space *MySpace = new Space ();

GeoObject *wuerfel = new GeoObject ();



unsigned isInit = 0;

int MyMsgHandleFunc (int msgType, const char* txt)
{
  char buf[255];
  if (msgType == MSG_WARNING)
    sprintf(buf, "tk_messageBox -title Warning -icon warning "
                 "-type ok -message {%s}", txt);
  else
    sprintf(buf, "tk_messageBox -title Error -icon error "
                 "-type ok -message {%s}", txt);
  Tcl_Eval (MainInterp, buf);
  return 1;
}



void newView ()
{
  char buf[255];
  Ustring win (".top");
  win += Ustring (isInit);
  sprintf(buf, "makeTopLevel %s", win.str ());
  Tcl_Eval (MainInterp, buf);
  isInit++;
  Client = new ToglView (100, 100, 300, 300, bgCol,win.str (),(win + ".menu.look").str ());
  Client->lightOn ();
  Client->smoothOn ();
  MySpace->insertView (*Client);
}



void InitModell ()
{
  if (!isInit)
  {

    Messager::setMsgHdlFunc (MyMsgHandleFunc);

//######################################
//        Attribute
//######################################
    Attribute *BlueAttrib   = ColorAttribute::create (RGBAColor(0.4, 0.4, 0.7));

//######################################
//        Primitiven
//######################################
    Primitive *Cubus   = Cube::create (Vec3D (-1.0, -1.0, -1.0), Vec3D (1.0, 1.0, 1.0));

//######################################
//        GeoPattern
//######################################
    GeoPattern *GPcubus = GeoPattern::create(*Cubus, *BlueAttrib);

//######################################
//        Pattern
//######################################
    Pattern *Pwuerfel = Pattern::create ();
    Pwuerfel->insert (*GPcubus);

//######################################
//        Einfuegen in GeoObjects
//        Positionierung
//######################################
    wuerfel->insert (*Pwuerfel);
    wuerfel->setParent (*MySpace);


//######################################
//     Initialisierung der ToglView-Umgebung
//######################################

    ToglView::setup (MainInterp);
    newView();

  }

  else
  {
    newView();
  }
}

//===========================================================================

int MainTclCmd (ClientData /*clientData*/, Tcl_Interp *interp,
                int /*argc*/, char *argv[] )
{
  MainInterp = interp;
  if ( !strcmp("Init", argv[1]) )
  {
    InitModell ();
  }
  else if ( !strcmp("NewView",  argv[1]) )
  {
    newView();
  }
  else if ( !strcmp("ExitTest", argv[1]) )
  {
    if (timerToken)
    {
      Tk_DeleteTimerHandler (timerToken);
      timerToken = NULL;
    }
  }
  return TCL_OK;
}

