Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C++

Gecko! Embedded C++ scripting for your applications

Rate me:
Please Sign up or sign in to vote.
4.93/5 (23 votes)
31 Aug 20036 min read 240.1K   2.6K   102  
Embed a C++ compiler in your project, use C++ as a compiled "scripting" language!
// GeckoTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GeckoTest.h"
#include "GeckoTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CGeckoTestDlg dialog



CGeckoTestDlg::CGeckoTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGeckoTestDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGeckoTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BMP, m_stcBitmap);
}

BEGIN_MESSAGE_MAP(CGeckoTestDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
	ON_BN_CLICKED(IDCANCEL3, OnBnClickedCancel3)
	ON_BN_CLICKED(IDCANCEL2, OnBnClickedCancel2)
END_MESSAGE_MAP()


// CGeckoTestDlg message handlers

BOOL CGeckoTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGeckoTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGeckoTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGeckoTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

CGeckoTestDlg *g_pDlg = NULL; // Not a very beautiful way, but I'm out of time...

GECKO_NATIVE void GECKO_NATIVE_CALL AlertDamage(int nDamage)
{
	if (!g_pDlg)
		return;
	if (nDamage)
	{
		char buf[256];
		sprintf(buf, "Took %d damage!\n", nDamage);
		g_pDlg->AppendOutput(buf);
	}
	else
		g_pDlg->AppendOutput("Miss!\n");
}

GECKO_NATIVE void GECKO_NATIVE_CALL SetPlayerHealth(int nNewHealth)
{
	if (g_pDlg)
		g_pDlg->SetDlgItemInt(IDC_HEALTH, nNewHealth);
}

GECKO_NATIVE void GECKO_NATIVE_CALL AppendOutput(LPCSTR szText)
{
	if (g_pDlg)
		g_pDlg->AppendOutput(szText);
}

GECKO_NATIVE int GECKO_NATIVE_CALL foo(int i)
{
	return 0;
}

GECKO_NATIVE const char* GECKO_NATIVE_CALL bar(LPCSTR a, float d)
{
	return "";
}

void CGeckoTestDlg::AppendOutput(LPCSTR szText)
{
	GetDlgItem(IDC_OUT)->SetFocus();
	int n = GetDlgItem(IDC_OUT)->GetWindowTextLength();
	CHARRANGE cr = {n, n};
	GetDlgItem(IDC_OUT)->SendMessage(EM_EXSETSEL, 0, (LPARAM)&cr);
	GetDlgItem(IDC_OUT)->SendMessage(EM_REPLACESEL, 0, (LPARAM)szText);
	GetDlgItem(IDC_OUT)->RedrawWindow();
}

void CGeckoTestDlg::OnCommandStart(LPCSTR lpszCmdLine)
{
	CString strTemp("[");
	strTemp += lpszCmdLine;
	strTemp += "]\n";
	AppendOutput(strTemp);
};

void CGeckoTestDlg::OnCommandSTDOUT(LPCSTR lpszOutput)
{ AppendOutput(lpszOutput); };

void CGeckoTestDlg::OnCommandSTDERR(LPCSTR lpszOutput)
{ AppendOutput(lpszOutput); };

//void CGeckoTestDlg::OnCommandEnd(bool bSuccessful)
//{ AppendOutput("---- Done ----\n"); };


void CGeckoTestDlg::OnBnClickedOk()
{
	// TODO : ajoutez ici le code de votre gestionnaire de notification de contr�le
	OnOK();
}

void CGeckoTestDlg::OnBnClickedCancel()
{
	// TODO : ajoutez ici le code de votre gestionnaire de notification de contr�le
	OnCancel();
}

void CGeckoTestDlg::OnBnClickedCancel3() // run ; forgot to rename button ID...
{
	GetDlgItem(IDCANCEL2)->EnableWindow(false);
	GetDlgItem(IDCANCEL)->EnableWindow(false);
	GetDlgItem(IDCANCEL3)->EnableWindow(false);

	if (eckIsUpToDate("test"))
	{
		AppendOutput("'test.eck' is up-to-date.\n");
	}
	else
	{
		AppendOutput("'test.eck' isn't up-to-date. Compiling 'test.cpp'.\n");
		DWORD dwStart = ::GetTickCount();
		if (eckCompile("test"))
		{
			CString strTemp;
			strTemp.Format("Compilation took %d msecs.\n", ::GetTickCount()-dwStart);
			AppendOutput(strTemp);
		}
		else
		{
			GetDlgItem(IDCANCEL3)->EnableWindow(true);
			GetDlgItem(IDCANCEL)->EnableWindow(true);
			AppendOutput("Compilation failed.\n");
			return;
		}
	}

	if (!eckLoad("test"))
	{
		GetDlgItem(IDCANCEL3)->EnableWindow(true);
		AppendOutput("Load of 'test.eck' failed.\n");
	}
	else
	{
		GetDlgItem(IDCANCEL2)->EnableWindow(true);

		g_pDlg = this;

		AppendOutput("Running 'test.eck'...\n");
		if (!eckRunThreaded())
			AppendOutput("Error while launching thread.\n");
	}
	
	GetDlgItem(IDCANCEL)->EnableWindow(true);
}

void CGeckoTestDlg::OnBnClickedCancel2() // abort
{
	if (eckAbortThread())
	{
		GetDlgItem(IDCANCEL3)->EnableWindow(true);
		GetDlgItem(IDCANCEL2)->EnableWindow(false);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
France France
Bouh

Comments and Discussions