// jclView.cpp : implementation of the CJclView class
//

#include "stdafx.h"
#include "jcl.h"

#include "jclDoc.h"
#include "jclView.h"

#include "jsvr.h"	// J Server

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

/////////////////////////////////////////////////////////////////////////////
// CJclView

IMPLEMENT_DYNCREATE(CJclView, CFormView)

BEGIN_MESSAGE_MAP(CJclView, CFormView)
	//{{AFX_MSG_MAP(CJclView)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJclView construction/destruction

CJclView::CJclView()
	: CFormView(CJclView::IDD)
{
	//{{AFX_DATA_INIT(CJclView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CJclView::~CJclView()
{
}

void CJclView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CJclView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CJclView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CJclView printing

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

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

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

void CJclView::OnPrint(CDC* pDC, CPrintInfo*)
{
	// TODO: add code to print the controls
}

/////////////////////////////////////////////////////////////////////////////
// CJclView diagnostics

#ifdef _DEBUG
void CJclView::AssertValid() const
{
	CFormView::AssertValid();
}

void CJclView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CJclView message handlers

// J Server
// variant has no direct representation of J scalar box
// J scalar box is a VT_ARRAY | VT_VARAINT variant with
//   rank 1, shape 1, elements 1, AND lower bound -1
// return 0 if scalar, 1 if array
int checkarray(VARIANT* pv)
{
	if( (!(pv->vt & VT_ARRAY)) ||
	( (pv->vt & (VT_ARRAY | VT_VARIANT)) &&
		(pv->parray->cDims == 1) &&
		(pv->parray->rgsabound->cElements == 1) &&
		(pv->parray->rgsabound->lLbound == -1))) return 0; // scalar box
	return 1;
}

// J Server
void CJclView::OnButton1() 
{
	char d[1000];
	char type[32];
	VARIANT v;
	int i,rc,rank;
	SAFEARRAYBOUND* pshape;
	LPSTR p;

	strcpy(d, "result=:");
	GetDlgItemText(IDC_EDIT1, d+strlen(d), sizeof(d) - strlen(d));
	rc = jsdo(d);
	p = d;
	strcpy(p, "Result is:\r\n");
	if(rc)
	{
		strcat(p, "  error number - ");
		p = p + strlen(p);
		itoa(rc, p, 10);
		VariantInit(&v);		// Must init variant before use
		jserror(rc, &v);
		ASSERT(v.vt == (VT_ARRAY  | VT_UI1));
		strcat(p, "\r\n  error text - ");
		strcat(p, (char*)v.parray->pvData);
		VariantClear(&v);	// MUST free the variant
	}
	else
	{
		VariantInit(&v);		// Must init variant before use
		rc = jsget("result", &v);
		switch(v.vt & ~VT_ARRAY)
		{
		case VT_BOOL: strcpy(type, "boolean"); break;
		case VT_UI1: strcpy(type, "character"); break;
		case VT_I4: strcpy(type, "integer"); break;
		case VT_R8: strcpy(type, "real"); break;
		case VT_VARIANT: strcpy(type, "box"); break;
		default: strcpy(type, "unknown");
		}
		if(checkarray(&v))
		{
			strcat(p, "  an array of type - ");
			strcat(p, type);
			strcat(p, "\r\n  rank - ");
			p = p + strlen(p);
			rank = v.parray->cDims;
			itoa(rank, p, 10);
			strcat(p, "\r\n  shape - ");
			pshape = v.parray->rgsabound;
			for(i=0; i<rank; ++i)
			{
				p = p + strlen(p);
				itoa(pshape->cElements, p, 10);
				strcat(p, "  ");
				++pshape;
			}
		}
		else
		{
			strcat(p, "  a scalar ");
			strcat(p, type);
		}
		VariantClear(&v);	// MUST free the variant
	}
	SetDlgItemText(IDC_EDIT2, d);
}
