Click here to Skip to main content
15,891,864 members
Articles / Desktop Programming / WTL

XONOR pointers: eXclusive Ownership & Non Owning Reference pointers

Rate me:
Please Sign up or sign in to vote.
4.96/5 (46 votes)
14 Apr 2014CPOL41 min read 127.2K   692   88  
A smart pointer system for safe application development in C++.
// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

using namespace WTL;

class CMainFrame : public CFrameWindowImpl<CMainFrame>
{
public:
	DECLARE_FRAME_WND_CLASS(NULL, IDR_MAINFRAME)

	CSPointersView m_view;

	CCommandBarCtrl m_CmdBar;

	owner_ptr<CObjectDlg> m_oObjectDlg;

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		// create command bar window
		HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
		// attach menu
		m_CmdBar.AttachMenu(GetMenu());
		// load command bar images
		//m_CmdBar.LoadImages(IDR_MAINFRAME);
		// remove old menu
		SetMenu(NULL);

		HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);

		CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
		AddSimpleReBarBand(hWndCmdBar);
		AddSimpleReBarBand(hWndToolBar, NULL, TRUE);

		CreateSimpleStatusBar();

		m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);

		

		return 0;
	}

	LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		PostMessage(WM_CLOSE);
		return 0;
	}

	LRESULT OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		// TODO: add code to initialize document
		m_view.m_oDocument=NULL;
		m_view.m_oDocument=new CDocument;
		m_view.OnNewDocument();
		return 0;
	}

	LRESULT OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		static BOOL bVisible = TRUE;	// initially visible
		bVisible = !bVisible;
		CReBarCtrl rebar = m_hWndToolBar;
		int nBandIndex = rebar.IdToIndex(ATL_IDW_BAND_FIRST + 1);	// toolbar is 2nd added band
		rebar.ShowBand(nBandIndex, bVisible);
		
		UpdateLayout();
		m_view.Invalidate();
		m_view.UpdateWindow();
		return 0;
	}

	LRESULT OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
		::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
		
		UpdateLayout();
		return 0;
	}

	LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CAboutDlg dlg;
		dlg.DoModal();
		return 0;
	}

	LRESULT OnSquare(UINT uiVal, int iVal, HWND hWnd)
	{
		m_view.SetMouseMode(Mode_Square);
		return 0;
	}

	LRESULT OnEllipse(UINT uiVal, int iVal, HWND hWnd)
	{
		m_view.SetMouseMode(Mode_Ellipse);
		return 0;
	}

	LRESULT OnLine(UINT uiVal, int iVal, HWND hWnd)
	{
		m_view.SetMouseMode(Mode_Line);
		return 0;
	}

	LRESULT OnMove(UINT uiVal, int iVal, HWND hWnd)
	{
		m_view.SetMouseMode(Mode_Move);
		return 0;
	}

	LRESULT OnDelete(UINT uiVal, int iVal, HWND hWnd)
	{
		m_view.DeleteSelected();
		return 0;
	}

	LRESULT OnProperties(UINT uiVal, int iVal, HWND hWnd)
	{
		if(NULL==m_oObjectDlg)
		{
			m_oObjectDlg=new CObjectDlg(m_oObjectDlg, m_view.m_oDocument);
			m_oObjectDlg->Create(m_hWnd);
		}
		m_oObjectDlg->ShowWindow(SW_SHOW);

		return 0;
	}

	LRESULT OnUpdateStatus(UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
#ifdef MONITOR_PTRS
		CString csString;
		reference_controler::SetMonitorOutput(csString);
		::SetWindowText(m_hWndStatusBar, csString);
#endif
		return 0;
	}

	LRESULT OnFileOpen(UINT uiVal, int iVal, HWND hWnd)
	{
		CFileDialog FileDlg(TRUE);
		FileDlg.m_ofn.lpstrDefExt=_T(".dgm");
		FileDlg.m_ofn.lpstrFilter=_T("Diagram Files\0*.dgm\0\0");
		if(IDOK==FileDlg.DoModal())
		{
			CFile File;
			if(File.Open(FileDlg.m_szFileName, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING))
			{
				m_view.m_oDocument=NULL;
				m_view.m_oDocument=new CDocument;
				CSerializer Serializer(&File);
				m_view.m_oDocument->Serialise(Serializer, false);
				m_view.OnNewDocument();
				m_view.m_oDocument->m_csFilename=FileDlg.m_szFileName;
			}
		}

		return 0;
	}
	LRESULT OnFileSave(UINT uiVal, int iVal, HWND hWnd)
	{
		if(m_view.m_oDocument->m_csFilename.IsEmpty())
			OnFileSaveAs(uiVal, iVal, hWnd);
		else
		{
			CFile File;
			if(File.Open(m_view.m_oDocument->m_csFilename, GENERIC_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL))
			{
				CSerializer Serializer(&File);
				m_view.m_oDocument->Serialise(Serializer, true);
			}
		}
		return 0;
	}
	LRESULT OnFileSaveAs(UINT uiVal, int iVal, HWND hWnd)
	{
		CFileDialog FileDlg(FALSE);
		FileDlg.m_ofn.lpstrDefExt=_T(".dgm");
		FileDlg.m_ofn.lpstrFilter=_T("Diagram Files\0*.dgm\0\0");
		if(IDOK==FileDlg.DoModal())
		{
			CFile File;
			if(File.Open(FileDlg.m_szFileName, GENERIC_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL))
			{
				CSerializer Serializer(&File);
				m_view.m_oDocument->Serialise(Serializer, true);
				m_view.m_oDocument->m_csFilename=FileDlg.m_szFileName;
			}
		}
		return 0;
	}
	
	
	BEGIN_EX_MSG_MAP(CMainFrame)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
		COMMAND_ID_HANDLER(ID_FILE_NEW, OnFileNew)
		COMMAND_ID_HANDLER(ID_VIEW_TOOLBAR, OnViewToolBar)
		COMMAND_ID_HANDLER(ID_VIEW_STATUS_BAR, OnViewStatusBar)
		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		CHAIN_MSG_MAP(CFrameWindowImpl<CMainFrame>)
		COMMAND_ID_HANDLER_EX(ID_SQUARE, OnSquare)
		COMMAND_ID_HANDLER_EX(ID_ELLIPSE, OnEllipse)
		COMMAND_ID_HANDLER_EX(ID_LINE, OnLine)
		COMMAND_ID_HANDLER_EX(ID_MOVE, OnMove)
		COMMAND_ID_HANDLER_EX(ID_DELETE, OnDelete)
		COMMAND_ID_HANDLER_EX(ID_PROPERTIES, OnProperties)
		COMMAND_ID_HANDLER_EX(ID_FILE_OPEN, OnFileOpen)
		COMMAND_ID_HANDLER_EX(ID_FILE_SAVE, OnFileSave)
		COMMAND_ID_HANDLER_EX(ID_FILE_SAVE_AS, OnFileSaveAs)
		MESSAGE_HANDLER_EX(WM_USER+21, OnUpdateStatus)
		
	END_MSG_MAP()

};

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
Retired
Spain Spain
Software Author with engineering, science and mathematical background.

Many years using C++ to develop responsive visualisations of fine grained dynamic information largely in the fields of public transport and supply logistics. Currently interested in what can be done to make the use of C++ cleaner, safer, and more comfortable.

Comments and Discussions