// RnTemplateView.h : Declaration of the CRnTemplateView

#pragma once
#include "resource.h"       // main symbols

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

#define TEMPLATE_VIEW_WIDTH       270
#define TEMPLATE_VIEW_HEIGHT      650
#define TEMPLATE_VIEW_TITLE       L"Template view"



// IRnTemplateView
[
	object,
	uuid("4DD9334F-4808-440d-8D36-D154CFFB0EB6"),
	helpstring("IRnTemplateView Interface"),
	pointer_default(unique)
]
__interface IRnTemplateView : IUnknown
{
};


// CRnTemplateView
class ATL_NO_VTABLE CRnTemplateView : 
	public CComObjectRootEx<CComGlobalsThreadModel>,
	public IRnTemplateView,
	public IRsUnknownNode,
	public IRsViewNode,
	public IRsUnknownViewNode,
	public IRsNotify,
	public IRsSelectionNotification,
	public IRnWindowControlFrameControls
{
private:
	/**@name Attributes */
	//@{
	/// RsBase interface (aggregated).
	CComPtr<IUnknown>           m_pUnkAgg;
	/// hooked node
	CComPtr<IUnknown>			m_spScannerAgg;
	/// view control
	CComQIPtr<IRsGenView>		m_spGenWindowControl;
	/// frame window
	CComQIPtr<IRsGenFrame>		m_spGenWindowFrame;


	/// hooked node
	CComPtr<IUnknown>			m_spBaseControlUnkAgg;


	//@}

	/**@name Private Methods */
	//@{
	/** This method is called when node data are loaded. Node's windows are opened here.
	*/

	HRESULT CreateUpdateCmd(IRsNode** ppRefreshCmd);
	// activate scanning for selected object and return scanned object
	HRESULT GetScannedNode(IRsNode** ppNode);
	// create and fill render data cache for thread safe rendering
	HRESULT CreateAndFillRenderData(IRsNode* pNode, IRsViewRenderData** ppRenderData);
	// refresh view
	HRESULT RefreshView(void);
	//@}

public:
	/**@name Methods */
	//@{
	/// Constructor, this constructor does nothing important.
	CRnTemplateView()
	{

	}

	HRESULT FinalConstruct();
	void FinalRelease();
	
	/**@name Methods of interface IRsUnknownViewNode */
	//@{
	STDMETHOD(OnUpdateControl)(void);
	STDMETHOD(GetControl)(IRsGenView** pControl);
	//@}

	/**@name Methods of interface IRsUnknownNode */
	//@{
	STDMETHOD(OnGetValue)(CONID con_id, IRsDataCache* pDataCache, VARIANT* pVtData, RtBool* pHandled);
	STDMETHOD(OnSetValue)(CONID con_id, IRsDataCache* pDataCache, VARIANT* pvtData, RtBool* pHandled);
	STDMETHOD(OnDefaultValue)(CONID con_id, VARIANT* pVtData, RtBool* pHandled);
	STDMETHOD(OnInvalidate)(CONID con_id, RtBool* pHandled);
	STDMETHOD(OnInitialize)(void);
	STDMETHOD(OnLoadDefault)(void);
	STDMETHOD(OnDestroy)(void);
	//@}

	/**@name Methods of interface IRsNotify */
	//@{
	/** 
	Add documentation here
	*/
	STDMETHOD(OnNotifyEvent)(IRsNotifyEvent* pEvent);
	//@}

	/**@name Methods of interface IRsSelectionNotification */
	//@{
	/** 
	Add documentation here
	*/
	STDMETHOD(SelectionNotify)(GUID SelectionID, RtSelectionNotificationType eType, IRsBaseSelection *pSelection);
	//@}


	/**@name Methods of interface IRsViewNode */
	//@{
	/// This method allow host ActiveX control by window defined by given handle.
	STDMETHOD(AttachViewWindowHandle)(HWND hWnd);
	/// This method prepares node to work as viewer.
	STDMETHOD(Prepare)(BSTR bsRootNodeName);
	/// This method updates node when it is first time showed.
	STDMETHOD(PostShowUpdate)(void);
	/// Notification that container window is about to be destroyed
	STDMETHOD(OnWndDestroy)(void);
	/// Notification that new viewer root was set.
	STDMETHOD(OnSetRoot)(BSTR bsRootNodeName);
	/// Check if root is acceptable for view node.
	STDMETHOD(IsValidRoot)(IRsNode* pRootNode);
	/// Viewer dimension aspect. 
	STDMETHOD(get_Dimension)(RtDimension* pVal);
	/// Flag that view has Exit control.
	STDMETHOD(get_ExitControl)(RtBool* pVal);

	STDMETHOD(get_ShortName)(BSTR* pVal)
	{
		if ( !pVal)
			return E_INVALIDARG;
		CComBSTR bszName = TEMPLATE_VIEW_TITLE;
		*pVal = bszName.Detach();
		return S_OK;
	};

	STDMETHOD(CanSwitchToNow)(RtBool* pVal)
	{
		if ( !pVal)
			return E_INVALIDARG;
		*pVal = TRUE;
		return S_OK;
	};
	//@}

	/**@name Methods of interface IRnWindowControlFrameControls */
	//@{
	STDMETHODIMP HasTab(RtBool bIsActive, RtBool* pVal)
	{
		if ( !pVal)
			return E_INVALIDARG;

		*pVal = FALSE;

		return S_OK;
	}
	STDMETHODIMP HasClose(RtBool* pVal)
	{
		if ( !pVal)
			return E_INVALIDARG;

		*pVal = TRUE;

		return S_OK;
	}
	STDMETHODIMP AllowLibraryTabs(RtLong* pVal)
	{
		if ( !pVal)
			return E_INVALIDARG;

		*pVal = -1;

		return S_OK;
	}
	STDMETHODIMP HasAspectButton(RtBool* pVal)
	{
		if ( !pVal)
			return E_INVALIDARG;

		*pVal = FALSE;

		return S_OK;
	}
	STDMETHODIMP HasExitButton(RtBool* pVal)
	{
		if ( !pVal)
			return E_INVALIDARG;

		*pVal = FALSE;

		return S_OK;
	}
	STDMETHODIMP Title(BSTR* pVal) 
	{
		if ( !pVal)
			return E_INVALIDARG;

		CComBSTR bszTitle(TEMPLATE_VIEW_TITLE);
		*pVal = bszTitle.Detach();

		return S_OK;
	};

	STDMETHODIMP ExitCommand(BSTR* pVal) 
	{
		return S_FALSE;
	};

	STDMETHODIMP GetIcon(HICON* hIcon)
	{
		if ( !hIcon)
			return E_INVALIDARG;

		//*hIcon = (HICON)::LoadImage( _AtlBaseModule.GetResourceInstance(), MAKEINTRESOURCE( IDI_HISTORY_COMMANDS), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);

		return S_OK;
	}
	//@}

	

	//@}

	// Macros are reason of problems for Doc++ so they should be out of
	// scope of Doc++ comments

	// IRsObject
	RS_DECLARE_IRS_OBJECT(IRnTemplateView, L"TemplateView Node")

	BEGIN_COM_MAP(CRnTemplateView)
		COM_INTERFACE_ENTRY(IRsUnknownNode)
		COM_INTERFACE_ENTRY(IRsNotify)
		COM_INTERFACE_ENTRY(IRsSelectionNotification)
		COM_INTERFACE_ENTRY(IRnTemplateView)
		COM_INTERFACE_ENTRY(IRsViewNode)
		COM_INTERFACE_ENTRY(IRsUnknownViewNode)
		COM_INTERFACE_ENTRY(IRnWindowControlFrameControls)
		COM_INTERFACE_ENTRY2(IUnknown, IRsUnknownNode)
		COM_INTERFACE_ENTRY2(IRsObject, IRsUnknownNode)
		COM_INTERFACE_ENTRY_AGGREGATE(DEF_GUIDNAME(IRsBaseControl), m_spBaseControlUnkAgg.p)
		COM_INTERFACE_ENTRY_AGGREGATE(DEF_GUIDNAME(IRsScanner), m_spScannerAgg.p)
		COM_INTERFACE_ENTRY_AGGREGATE(DEF_GUIDNAME(IRsBaseNode), m_pUnkAgg.p)
		COM_INTERFACE_ENTRY_AGGREGATE_BLIND(m_pUnkAgg.p)    
	END_COM_MAP()

	DECLARE_GET_CONTROLLING_UNKNOWN()
	DECLARE_PROTECT_FINAL_CONSTRUCT()
};

