Click here to Skip to main content
15,897,187 members
Articles / Desktop Programming / WTL

Form Designer

26 Jul 2021CPOL24 min read 352.2K   82.5K   230  
Component for adding scriptable forms capabilities to an application.
// ValueEditor.cpp: implementation of the CValueEditor class.
//
// Author : David Shepherd
//			Copyright (c) 2002, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ValueEditor.h"

/////////////////////////////////////////////////////////////////////////////
// CValueEditor

CValueEditor::CValueEditor() : m_Edit(this,VE_MSG_MAP_EDIT)
{
	// initialise everything
	m_RunMessageLoop=TRUE;
}

CValueEditor::~CValueEditor()
{
	// clean up
}

BOOL CValueEditor::EditValue(const CWindow &Parent,const CRect &Rect)
{
TRY
	USES_CONVERSION;

	// reinitialise everything to allow object reuse
	m_RunMessageLoop=TRUE;
	// create the value editor window (invisible)
	if(Create(Parent,CRect(Rect),_T(""),WS_POPUP|WS_CLIPCHILDREN)==NULL)
	{
		throw std::exception();
	}
	// set the edit box text
	if(m_Edit.SetWindowText(W2CT(m_Value.c_str()))==FALSE)
	{
		throw std::exception();
	}
	// give focus to the edit box
	if(m_Edit.SetFocus()==NULL)
	{
		throw std::exception();
	}
	// show the value editor
	(void)ShowWindow(SW_SHOW);
	// run the message loop
	while(m_RunMessageLoop)
	{
		// get the next message
		// todo : check for the WM_QUIT message
		MSG Message;
		(void)GetMessage(&Message,NULL,0,0);
		// process the message
		(void)TranslateMessage(&Message);
		(void)DispatchMessage(&Message);
	}
CATCH_ALL
	// clean up
	if(IsWindow())
	{
		(void)DestroyWindow();
	}
	return Caught ? FALSE : TRUE;	// return TRUE on success
}

LRESULT CValueEditor::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
TRY
	// create the edit box
	if(m_Edit.Create(*this,CRect(0,0,0,0),_T(""),
		// standard window styles
		WS_CHILD|WS_VISIBLE|
		// edit box styles
		ES_AUTOHSCROLL,
		// extended window styles
		0,
		// id
		VE_IDC_EDIT)==NULL)
	{
		throw std::exception();
	}
	// set the edit box font
	CFontHandle hFont=(HFONT)GetStockObject(DEFAULT_GUI_FONT);
	if(hFont==NULL)
	{
		throw std::exception();
	}
	m_Edit.SetFont(hFont);
CATCH_ALL
	return Caught ? -1 : 0;	// fail creation on error
}

LRESULT CValueEditor::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
TRY
	// get the client rect
	CRect ClientRect(0,0,0,0);
	if(GetClientRect(ClientRect)==FALSE)
	{
		throw std::exception();
	}
	// size the edit box to fill the entire client area
	if(m_Edit.SetWindowPos(NULL,ClientRect,SWP_NOZORDER)==FALSE)
	{
		throw std::exception();
	}
CATCH_ALL
	return 0;
}

LRESULT CValueEditor::OnActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
TRY
	// if the window is being deactivated
	if(LOWORD(wParam)==WA_INACTIVE)
	{
		// get the window being activated
		CWindow ActiveWindow=(HWND)lParam;
		// if the value editor is no longer active
		if(ActiveWindow!=*this and ActiveWindow!=m_Edit)
		{
			// hide the value editor immediately
			(void)ShowWindow(SW_HIDE);
			// shut down
			m_RunMessageLoop=FALSE;
		}
	}
CATCH_ALL
	return 0;
}

LRESULT CValueEditor::OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
TRY
	// get the window receiving focus
	CWindow FocusWindow=(HWND)wParam;
	// if the value editor no longer has the focus
	if(FocusWindow!=*this and FocusWindow!=m_Edit)
	{
		// hide the value editor immediately
		(void)ShowWindow(SW_HIDE);
		// shut down
		m_RunMessageLoop=FALSE;
	}
CATCH_ALL
	return 0;
}

LRESULT CValueEditor::OnChangeEdit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
TRY
	// get the edit box text
	CComBSTR Text;
	if(m_Edit.GetWindowText(&Text)==FALSE)
	{
		throw std::exception();
	}
	// update the value
	m_Value=BSTR2W(Text);
CATCH_ALL
	return 0;
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions