// testView.cpp : implementation of the CTestView class
//

#include "stdafx.h"
#include "test.h"

#include "testDoc.h"
#include "testView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestView

IMPLEMENT_DYNCREATE(CTestView, CView)

BEGIN_MESSAGE_MAP(CTestView, CView)
  ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	//{{AFX_MSG_MAP(CTestView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

BEGIN_EVENTSINK_MAP(CTestView, CView)
	ON_EVENT(CTestView, IDC_KOMPAS_CTRL,  1 /*KgMouseDown*/, OnKgMouseDownKgaxctrl, VTS_I2 VTS_I2 VTS_I4 VTS_I4 VTS_PBOOL)
	ON_EVENT(CTestView, IDC_KOMPAS_CTRL, 10 /*KgKeyDown*/,   OnKgKeyDownKgaxctrl,   VTS_PI4 VTS_I2 )
	ON_EVENT(CTestView, IDC_KOMPAS_CTRL, 11 /*KgKeyUP*/,     OnKgKeyUpKgaxctrl,   VTS_PI4 VTS_I2 )
	ON_EVENT(CTestView, IDC_KOMPAS_CTRL, 12 /*KgKeyPress*/,   OnKgKeyPressKgaxctrl,   VTS_PI4 )
END_EVENTSINK_MAP()

void CTestView::OnKgMouseDownKgaxctrl( short nButton, short nShiftState, long x, long y, BOOL FAR* proceed ) {
  if ( nButton == 2 ) {
    ::MessageBox( 0, "OnKgMouseDownKgaxctrl", "EVENT", MB_OK );
    if ( proceed )
      *proceed = FALSE;
  }
  else {
    if ( proceed )
      *proceed = TRUE;
  }
}


void CTestView::OnKgKeyDownKgaxctrl(long FAR * nButton, short nShiftState )
{
  if ( nButton ) {
    ::MessageBox( 0, "OnKgKeyDownKgaxctrl", "EVENT", MB_OK );
    if ( *nButton == VK_F1 && nShiftState == 0 ) 
      *nButton = 0;
  }
}

void CTestView::OnKgKeyUpKgaxctrl(long FAR * nButton, short nShiftState )
{
  if ( nButton ) {
    ::MessageBox( 0, "OnKgKeyUpKgaxctrl", "EVENT", MB_OK );
    if ( *nButton == VK_F1 && nShiftState == 0 ) 
      *nButton = 0;
  }
}

void CTestView::OnKgKeyPressKgaxctrl(long FAR * nButton )
{
  if ( nButton ) {
    ::MessageBox( 0, "OnKgKeyPressKgaxctrl", "EVENT", MB_OK );
    if ( *nButton == VK_F1 ) 
      *nButton = 0;
  }
}

/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction

CTestView::CTestView()
{
	// TODO: add construction code here

}

CTestView::~CTestView()
{
}

BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
  cs.style &= ~WS_TABSTOP;
	cs.style |=  ( WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS );

  cs.dwExStyle &=  ~WS_EX_CLIENTEDGE;

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestView drawing

void CTestView::OnDraw(CDC* pDC)
{
	CTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTestView printing

BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics

#ifdef _DEBUG
void CTestView::AssertValid() const
{
	CView::AssertValid();
}

void CTestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTestDoc* CTestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
	return (CTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers

void CTestView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
}

int CTestView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// Create the style
	DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | LVS_REPORT;
  if ( m_KompasCtrl.Create("", dwStyle, CRect(0, 0, 0, 0), this, IDC_KOMPAS_CTRL) ) {
    m_KompasCtrl->PutDocumentType( vt_3DPart ); // 
    return 0;
  }
	else
	  return -1;
}

void CTestView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	if ( ::IsWindow(m_KompasCtrl.m_hWnd) )
		m_KompasCtrl.MoveWindow(0, 0, cx, cy, TRUE);
}


void CTestView::OnAppAbout()
{
	KompasObjectPtr pKompas = m_KompasCtrl->GetKompasObject();
	ksDocument3DPtr pDoc = pKompas->ActiveDocument3D();
	if (pDoc == 0)
		return;

	ksPartPtr part = pDoc->GetPart(pNew_Part);
	ksEntityPtr entitySketch = part->NewEntity(o3d_sketch);
	ksSketchDefinitionPtr sketchDef = entitySketch->GetDefinition();

	ksEntityPtr basePlane = part->GetDefaultEntity(o3d_planeXOY);
	sketchDef->SetPlane(basePlane);
	sketchDef->angle = 45;
	entitySketch->Create();

	ksDocument2DPtr sketchEdit = sketchDef->BeginEdit();
		sketchEdit->ksLineSeg(50, 50, -50, 50, 1);
		sketchEdit->ksLineSeg(50, -50, -50, -50, 1);
		sketchEdit->ksLineSeg(50, -50, 50, 50, 1);
		sketchEdit->ksLineSeg(-50, -50, -50, 50, 1);
	sketchDef->EndEdit();

	ksEntityPtr entityExtr = part->NewEntity(o3d_baseExtrusion);
	ksBaseExtrusionDefinitionPtr extrusionDef = entityExtr->GetDefinition();
	extrusionDef->directionType = dtNormal;
	extrusionDef->SetSideParam(TRUE, etBlind, 200, 0, FALSE);
	extrusionDef->SetThinParam(TRUE, dtBoth, 10, 10);
	extrusionDef->SetSketch(entitySketch);
	entityExtr->Create();
      
	m_KompasCtrl->ZoomEntireDocument();

}
