Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / ATL

Smart Grid

Rate me:
Please Sign up or sign in to vote.
4.33/5 (6 votes)
24 May 2000 388.3K   7.3K   72  
Build a grid using ATL, STL and Win32 API.
// SmartGrid.h : Declaration of the CSmartGrid

#ifndef __SMARTGRID_H_
#define __SMARTGRID_H_

#include "StdAfx.h"
#include "resource.h"       // main symbols
#include <atlctl.h>
#include <adoint.h>
#include <adoid.h>
#include "AlxGrdCP.h"

class CCell;
class CRow;
class CColumn;
class CSmartGrid;
class CManageData;


/////////////////////////////////////////////////////////////////////////////
// CMyWnd


class CMyWnd :
	public CWindowImpl< CMyWnd >
{
public:

	// Enumeration used for mouse position - used at resizing

	typedef enum tagMousePosition 
	{
		MousePosition_Unknown = 0,
		MousePosition_N,
		MousePosition_S,
		MousePosition_E,
		MousePosition_W,
		MousePosition_NE,
		MousePosition_SW,
		MousePosition_NW,
		MousePosition_SE,
		MousePosition_HorizontalSplit,
		MousePosition_VerticalSplit
	} MousePosition;

	static LPCTSTR m_pMouseCursors[];


	CSmartGrid* m_pSmartGrid;
	MousePosition m_MousePosition;
	POINT m_ptPreviousMousePosition;

	CMyWnd( CSmartGrid* pSmartGrid );

	virtual void SetMouseCursor();
	virtual LRESULT OnDraw( HDC hDC, RECT& rcPaint ) = 0;

BEGIN_MSG_MAP(CMyWnd)

	MESSAGE_HANDLER(WM_PAINT,OnPaint)
	MESSAGE_HANDLER(WM_KEYDOWN,OnKeyDown)
	MESSAGE_HANDLER(WM_KEYUP,OnKeyUp)
	MESSAGE_HANDLER(WM_CHAR,OnChar)
	MESSAGE_HANDLER(WM_MOUSEMOVE,OnMouseMove)
	MESSAGE_HANDLER(WM_LBUTTONDOWN,OnLButtonDown)
	MESSAGE_HANDLER(WM_LBUTTONUP,OnLButtonUp)
	MESSAGE_HANDLER(WM_CAPTURECHANGED,OnCaptureChanged)

END_MSG_MAP()

protected:
	
	LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnKeyUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnCaptureChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
};

	
///////////////////////////////////////////////////////////////
// CTopHeaderWnd

class CTopHeaderWnd :
	public CMyWnd
{
public:

	CTopHeaderWnd( CSmartGrid* pSmartGrid );
	LRESULT OnDraw( HDC hDC, RECT& rcPaint );

BEGIN_MSG_MAP(CTopHeaderWnd)	

	MESSAGE_HANDLER(WM_MOUSEMOVE,OnMouseMove)
	MESSAGE_HANDLER(WM_SETCURSOR,OnSetCursor)
	MESSAGE_HANDLER(WM_ERASEBKGND,OnEraseBkgnd)
	MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
	MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)	
	MESSAGE_HANDLER(WM_LBUTTONDBLCLK, OnLButtonDblClk)	

	CHAIN_MSG_MAP(CMyWnd)
END_MSG_MAP()

protected:

	LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnSetCursor(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonDblClk(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

	long m_nResizingColumn;
};

///////////////////////////////////////////////////////////////
// CLeftHeaderWnd

class CLeftHeaderWnd :
	public CMyWnd
{
public:

	CLeftHeaderWnd( CSmartGrid* pSmartGrid );
	LRESULT OnDraw( HDC hDC, RECT& rcPaint );

BEGIN_MSG_MAP(CLeftHeaderWnd)	

	MESSAGE_HANDLER(WM_MOUSEMOVE,OnMouseMove)
	MESSAGE_HANDLER(WM_ERASEBKGND,OnEraseBkgnd)
	MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
	MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)	
	MESSAGE_HANDLER(WM_RBUTTONDOWN, OnRButtonDown)
	MESSAGE_HANDLER(WM_RBUTTONUP, OnRButtonUp)	

	CHAIN_MSG_MAP(CMyWnd)

END_MSG_MAP()

protected:

	LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnRButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
};

///////////////////////////////////////////////////////////////
// CLeftUpCornerWnd

class CLeftUpCornerWnd :
	public CMyWnd
{
public:

	CLeftUpCornerWnd( CSmartGrid* pSmartGrid );
	LRESULT OnDraw( HDC hDC, RECT& rcPaint );

BEGIN_MSG_MAP(CLeftUpCornerWnd)	
	
	MESSAGE_HANDLER(WM_MOUSEMOVE,OnMouseMove)
	MESSAGE_HANDLER(WM_ERASEBKGND,OnEraseBkgnd)
	MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
	MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
	MESSAGE_HANDLER(WM_RBUTTONDOWN, OnRButtonDown)
	MESSAGE_HANDLER(WM_RBUTTONUP, OnRButtonUp)
	MESSAGE_HANDLER(WM_RBUTTONDBLCLK, OnRButtonDblClick)

	CHAIN_MSG_MAP(CMyWnd)

END_MSG_MAP()

protected:

	LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);	
	LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnRButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnRButtonDblClick(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
};

///////////////////////////////////////////////////////////////
// CContentWnd

class CContentWnd :
	public CMyWnd
{
public:
	
	CContentWnd( CSmartGrid* pSmartGrid );
	LRESULT OnDraw( HDC hDC, RECT& rcPaint );
	void VerticalScroll( long nRows );
	void HorizontalScroll( long nPixels );
	void UpdateScrollbars();
	void UpdateContentPosition();
	long GetRowFromPosition( POINT& ptPosition );
	long GetColumnFromPosition( POINT& ptPosition );
	RECT GetRectOfCell( long nRow, long nColumn );

BEGIN_MSG_MAP(CContentWnd)	
	
	MESSAGE_HANDLER(WM_CREATE,OnCreate);
	MESSAGE_HANDLER(WM_MOUSEMOVE,OnMouseMove)
	MESSAGE_HANDLER(WM_ERASEBKGND,OnEraseBkgnd)
	MESSAGE_HANDLER(WM_HSCROLL, OnHScroll)
	MESSAGE_HANDLER(WM_VSCROLL, OnVScroll)
	MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
	MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
	MESSAGE_HANDLER(WM_LBUTTONDBLCLK, OnLButtonDblClk)
	MESSAGE_HANDLER(WM_RBUTTONDOWN, OnRButtonDown)
	MESSAGE_HANDLER(WM_RBUTTONUP, OnRButtonUp)
	MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)

	CHAIN_MSG_MAP(CMyWnd)

END_MSG_MAP()

protected:

	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnHScroll(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnVScroll(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnLButtonDblClk(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnRButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
};

/////////////////////////////////////////////////////////////////////////////
// CSmartGrid
class ATL_NO_VTABLE CSmartGrid : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CStockPropImpl<CSmartGrid, ISmartGrid, &IID_ISmartGrid, &LIBID_ALXGRDLib>,
	public CComControl<CSmartGrid>,
	public IPersistStreamInitImpl<CSmartGrid>,
	public IOleControlImpl<CSmartGrid>,
	public IOleObjectImpl<CSmartGrid>,
	public IOleInPlaceActiveObjectImpl<CSmartGrid>,
	public IViewObjectExImpl<CSmartGrid>,
	public IOleInPlaceObjectWindowlessImpl<CSmartGrid>,
	public ISupportErrorInfoImpl<&IID_ISmartGrid>,
	public IConnectionPointContainerImpl<CSmartGrid>,
	public IPersistStorageImpl<CSmartGrid>,
	public ISpecifyPropertyPagesImpl<CSmartGrid>,
	public IQuickActivateImpl<CSmartGrid>,
	public IDataObjectImpl<CSmartGrid>,
	public IProvideClassInfo2Impl<&CLSID_SmartGrid, &DIID__ISmartGridEvents, &LIBID_ALXGRDLib>,
	public IPropertyNotifySinkCP<CSmartGrid>,
	public CComCoClass<CSmartGrid, &CLSID_SmartGrid>,
	public CProxy_ISmartGridEvents< CSmartGrid >
{
public:

	CSmartGrid();
	virtual ~CSmartGrid();
	HRESULT OnDraw(ATL_DRAWINFO& di);

DECLARE_REGISTRY_RESOURCEID(IDR_SMARTGRID)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CSmartGrid)
	COM_INTERFACE_ENTRY(ISmartGrid)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(IViewObjectEx)
	COM_INTERFACE_ENTRY(IViewObject2)
	COM_INTERFACE_ENTRY(IViewObject)
	COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless)
	COM_INTERFACE_ENTRY(IOleInPlaceObject)
	COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless)
	COM_INTERFACE_ENTRY(IOleInPlaceActiveObject)
	COM_INTERFACE_ENTRY(IOleControl)
	COM_INTERFACE_ENTRY(IOleObject)
	COM_INTERFACE_ENTRY(IPersistStreamInit)
	COM_INTERFACE_ENTRY2(IPersist, IPersistStreamInit)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
	COM_INTERFACE_ENTRY(IConnectionPointContainer)
	COM_INTERFACE_ENTRY(ISpecifyPropertyPages)
	COM_INTERFACE_ENTRY(IQuickActivate)
	COM_INTERFACE_ENTRY(IPersistStorage)
	COM_INTERFACE_ENTRY(IDataObject)
	COM_INTERFACE_ENTRY(IProvideClassInfo)
	COM_INTERFACE_ENTRY(IProvideClassInfo2)
	COM_INTERFACE_ENTRY_IMPL(IConnectionPointContainer)
END_COM_MAP()

BEGIN_PROP_MAP(CSmartGrid)

	PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
	PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
	
	PROP_ENTRY("NumberOfColumns", DISPID_ISmartGrid_NumberOfColumns, CLSID_NULL)
	PROP_ENTRY("NumberOfRows", DISPID_ISmartGrid_NumberOfRows, CLSID_NULL)
	PROP_ENTRY("GridMode", DISPID_ISmartGrid_GridMode, CLSID_SmartGridPP)
	PROP_ENTRY("DefaultColumnWidth", DISPID_ISmartGrid_DefaultColumnWidth, CLSID_SmartGridPP)
	PROP_ENTRY("DefaultRowHeight", DISPID_ISmartGrid_RowHeight, CLSID_SmartGridPP)
	PROP_ENTRY("TopHeaderHeight", DISPID_ISmartGrid_TopHeaderHeight, CLSID_SmartGridPP)
	PROP_ENTRY("LeftHeaderWidth", DISPID_ISmartGrid_LeftHeaderWidth, CLSID_SmartGridPP)
	PROP_ENTRY("HeaderForeColor", DISPID_ISmartGrid_HeaderForeColor, CLSID_StockColorPage)
	PROP_ENTRY("HeaderBkColor", DISPID_ISmartGrid_HeaderBkColor, CLSID_StockColorPage)
	PROP_ENTRY("HeaderLinesColor", DISPID_ISmartGrid_HeaderLinesColor, CLSID_StockColorPage)
	PROP_ENTRY("ContentLinesColor", DISPID_ISmartGrid_ContentLinesColor, CLSID_StockColorPage)
	PROP_ENTRY("ContentForeColor", DISPID_ISmartGrid_ContentForeColor, CLSID_StockColorPage)
	PROP_ENTRY("ContentBkColor", DISPID_ISmartGrid_ContentBkColor, CLSID_StockColorPage)
	PROP_ENTRY("ActiveRowColor", DISPID_ISmartGrid_ActiveRowColor, CLSID_StockColorPage)
	PROP_ENTRY("SelectedItemColor", DISPID_ISmartGrid_SelectedItemColor, CLSID_StockColorPage)
	PROP_ENTRY("TopHeaderVisible", DISPID_ISmartGrid_TopHeaderVisible, CLSID_SmartGridPP)
	PROP_ENTRY("LeftHeaderVisible", DISPID_ISmartGrid_LeftHeaderVisible, CLSID_SmartGridPP)
	PROP_ENTRY("Font", DISPID_FONT, CLSID_StockFontPage)
	PROP_ENTRY("DataSourceType", DISPID_ISmartGrid_DataSourceType, CLSID_SmartGridPP)
	PROP_ENTRY("DataSource", DISPID_ISmartGrid_DataSource, CLSID_SmartGridPP)
	PROP_ENTRY("ConnectionString", DISPID_ISmartGrid_ConnectionString, CLSID_SmartGridPP)
	PROP_ENTRY("CursorType", DISPID_ISmartGrid_CursorType, CLSID_SmartGridPP)
	PROP_ENTRY("LockType", DISPID_ISmartGrid_LockType, CLSID_SmartGridPP)
	PROP_ENTRY("ReadOnly", DISPID_ISmartGrid_ReadOnly, CLSID_SmartGridPP)

	// Example entries
	// PROP_ENTRY("Property Description", dispid, clsid)
	// PROP_PAGE(CLSID_StockColorPage)
END_PROP_MAP()

BEGIN_CONNECTION_POINT_MAP(CSmartGrid)
	CONNECTION_POINT_ENTRY(IID_IPropertyNotifySink)
	CONNECTION_POINT_ENTRY(DIID__ISmartGridEvents)
END_CONNECTION_POINT_MAP()

BEGIN_MSG_MAP(CSmartGrid)
	
//	CHAIN_MSG_MAP_MEMBER(m_wndTopHeader)
	
	MESSAGE_HANDLER(WM_CREATE, OnCreate)
	MESSAGE_HANDLER(WM_GETDLGCODE, OnGetDlgCode)
	MESSAGE_HANDLER(WM_SETCURSOR, OnSetCursor);
	MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove);
	MESSAGE_HANDLER(WM_SIZE,OnSize)
	MESSAGE_HANDLER(WM_ERASEBKGND,OnEraseBkgnd)
	
	MESSAGE_HANDLER(WM_KEYDOWN,OnKeyDown)
	MESSAGE_HANDLER(WM_KEYUP,OnKeyUp)
	
	CHAIN_MSG_MAP(CComControl<CSmartGrid>)
	
	DEFAULT_REFLECTION_HANDLER()

END_MSG_MAP()
// Handler prototypes:
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);



// ISupportsErrorInfo
	STDMETHOD(ISupportsErrorInfo)(REFIID riid)
	{
		static const IID* arr[] = 
		{
			&IID_ISmartGrid,
		};
		for (int i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
		{
			if (InlineIsEqualGUID(*arr[i], riid))
				return S_OK;
		}
		return S_FALSE;
	}

// IViewObjectEx
	DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE)

// ISmartGrid
public:
	
	STDMETHOD(get_HeaderLinesColor)(/*[out, retval]*/ OLE_COLOR *pVal);
	STDMETHOD(put_HeaderLinesColor)(/*[in]*/ OLE_COLOR newVal);	
	STDMETHOD(get_LeftHeaderVisible)(/*[out, retval]*/ BOOL *pVal);
	STDMETHOD(put_LeftHeaderVisible)(/*[in]*/ BOOL newVal);
	STDMETHOD(get_TopHeaderVisible)(/*[out, retval]*/ BOOL *pVal);
	STDMETHOD(put_TopHeaderVisible)(/*[in]*/ BOOL newVal);
	STDMETHOD(get_RowHeight)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_RowHeight)(/*[in]*/ long newVal);
	STDMETHOD(get_HeaderForeColor)(/*[out, retval]*/ OLE_COLOR *pVal);
	STDMETHOD(put_HeaderForeColor)(/*[in]*/ OLE_COLOR newVal);
	STDMETHOD(get_HeaderBkColor)(/*[out, retval]*/ OLE_COLOR *pVal);
	STDMETHOD(put_HeaderBkColor)(/*[in]*/ OLE_COLOR newVal);

	STDMETHOD(get_LeftHeaderWidth)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_LeftHeaderWidth)(/*[in]*/ long newVal);
	STDMETHOD(get_TopHeaderHeight)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_TopHeaderHeight)(/*[in]*/ long newVal);
	STDMETHOD(get_DefaultColumnWidth)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_DefaultColumnWidth)(/*[in]*/ long newVal);
	STDMETHOD(RemoveColumn)(long nPosition);
	STDMETHOD(RemoveRow)(long nPosition);
	STDMETHOD(get_GridMode)(/*[out, retval]*/ GridMode *pVal);
	STDMETHOD(put_GridMode)(/*[in]*/ GridMode newVal);
	STDMETHOD(get_NumberOfRows)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_NumberOfRows)(/*[in]*/ long newVal);
	STDMETHOD(InsertRow)(long nPosition, VARIANT* pvarRow );
	STDMETHOD(AddRow)( VARIANT* pvarRow );
	STDMETHOD(AddColumn)(BSTR bstrName, DataType vtType, VARIANT varReadOnly, VARIANT varRange, VARIANT* pvarColumn );
	STDMETHOD(get_NumberOfColumns)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_NumberOfColumns)(/*[in]*/ long newVal);
	STDMETHOD(InsertColumn)(BSTR bstrName, DataType vtType, long nPosition, VARIANT varReadOnly, VARIANT varRange, VARIANT* pvarColumn );
	STDMETHOD(get_UILocked)(/*[out, retval]*/ BOOL *pVal);
	STDMETHOD(put_UILocked)(/*[in]*/ BOOL newVal);
	STDMETHOD(get_ContentLinesColor)(/*[out, retval]*/ OLE_COLOR *pVal);
	STDMETHOD(put_ContentLinesColor)(/*[in]*/ OLE_COLOR newVal);
	STDMETHOD(get_ContentForeColor)(/*[out, retval]*/ OLE_COLOR *pVal);
	STDMETHOD(put_ContentForeColor)(/*[in]*/ OLE_COLOR newVal);
	STDMETHOD(get_ContentBkColor)(/*[out, retval]*/ OLE_COLOR *pVal);
	STDMETHOD(put_ContentBkColor)(/*[in]*/ OLE_COLOR newVal);

public:
	STDMETHOD(get_ReadOnly)(/*[out, retval]*/ BOOL *pVal);
	STDMETHOD(put_ReadOnly)(/*[in]*/ BOOL newVal);
	STDMETHOD(get_LockType)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_LockType)(/*[in]*/ long newVal);
	STDMETHOD(get_CursorType)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_CursorType)(/*[in]*/ long newVal);
	STDMETHOD(get_ConnectionString)(/*[out, retval]*/ BSTR *pVal);
	STDMETHOD(put_ConnectionString)(/*[in]*/ BSTR newVal);
	STDMETHOD(Requery)();
	STDMETHOD(get_DataSource)(/*[out, retval]*/ VARIANT* pVal);
	STDMETHOD(put_DataSource)(/*[in]*/ VARIANT newVal);
	STDMETHOD(get_DataSourceType)(/*[out, retval]*/ DataSourceType *pVal);
	STDMETHOD(put_DataSourceType)(/*[in]*/ DataSourceType newVal);
	STDMETHOD(UnselectAllRows)();
	STDMETHOD(SelectAllRows)();
	STDMETHOD(get_SelectedItemColor)(/*[out, retval]*/ OLE_COLOR *pVal);
	STDMETHOD(put_SelectedItemColor)(/*[in]*/ OLE_COLOR newVal);
	STDMETHOD(get_Column)(long nIndex, /*[out, retval]*/ IColumn* *pVal);
	STDMETHOD(get_Row)(long nIndex, /*[out, retval]*/ IRow* *pVal);
	STDMETHOD(get_ActiveRow)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_ActiveRow)(/*[in]*/ long newVal);
	STDMETHOD(get_ActiveColumn)(/*[out, retval]*/ VARIANT *pVal);
	STDMETHOD(put_ActiveColumn)(/*[in]*/ VARIANT newVal);
	STDMETHOD(get_ActiveRowColor)(/*[out, retval]*/ OLE_COLOR *pVal);
	STDMETHOD(put_ActiveRowColor)(/*[in]*/ OLE_COLOR newVal);

	///////////////////////
	// Data types
	///////////////////////


	typedef vector< CComObject< CCell >* > Row; // A row contains cells
	typedef vector< Row* > Grid;  // The grid is build from rows
	typedef pair< long, long > Position; 

	///////////////////////
	// Variables
	///////////////////////

	Grid m_vctGrid;  // Grid

	Position m_posTopLeftCell; // Cell's coordinates that is positioned on top left

	vector< ColumnDefinition > m_vctColumnsDefinitions;
	long m_nDefaultColumnWidth, m_nRowHeight; // Default column width ang common row height
	BOOL m_bTopHeaderVisible, m_bLeftHeaderVisible; // Tells if headers are visible or not
	long m_nTopHeaderHeight, m_nLeftHeaderWidth; // Header's sizes
	COLORREF m_clrHeaderBk, m_clrHeaderFore, m_clrHeaderLines, m_clrContentBk, m_clrContentFore, m_clrContentLines, m_clrActiveRow, m_clrSelectedItem; // Colors
	long m_nGridMode;
	long m_nUILocked; // If is true we do not make any refresh when data is changed
	long m_nActiveRow; // Number of the active row
	CComVariant m_varActiveColumn; // Number or label of the active column
	set< long > m_setSelectedRows; // Set of selected rows
	BOOL m_bLeftHeaderAutoNumber; // Tell if left header must be autonumbered
	long m_nSortedColumn;
	
	CComQIPtr<IPictureDisp, &IID_IPictureDisp> m_spPicture;
	DataSourceType m_DataSourceType;
	CComVariant m_varDataSource;
	CComBSTR m_bstrConnectionString;
	CComPtr< ADORecordset > m_spRecordset;

	CursorTypeEnum m_CursorType;
	LockTypeEnum m_LockType;

	// Stock properties
	CComPtr<IFontDisp> m_pFont;

	long m_nTolerance; // Used as tolerance when computing point positions
	BOOL m_bReadOnly;

	CManageData* m_pManageData;

	CContentWnd m_wndContent;
	CTopHeaderWnd m_wndTopHeader;
	CLeftHeaderWnd m_wndLeftHeader;
	CLeftUpCornerWnd m_wndLeftUpCorner;	

	Position m_posEditedCell;

	//////////////////////////////////////////////
	// Public methos used by grid's elements
	//////////////////////////////////////////////

	// Events
	void Event_ValueNotInRange( long nRow, long nColumn );
	void Event_CellChanged( Position& posPrevious, Position& posNow );
	void Event_RowChanged( long nPrevious, long nNow );
	void Event_ColumnChanged( long nPrevious, long nNow );
	void Event_SelectionChanged( long nRow );;
	void Event_ContentSizesChanged();
	BOOL Event_BeginCellEdit( long nRow, long nColumn );
	void Event_EndCellEdit( long nRow, long nColumn );
	void Event_CellUpdated( long nRow, long nColumn );
	void Event_RowInserted( long nPosition );
	void Event_ColumnInserted( long nPosition );
	
	long GetFirstVisibleColumn();
	long GetLastVisibleColumn();
	long GetFirstVisibleRow();
	long GetLastVisibleRow();

	void EnsureActiveCellVisible();
	void VerticalScroll( long nRows );
	void HorizontalScroll( long nPixels);
	long NumberOfColumns();
	long NumberOfRows();
	void Invalidate();
	void LayoutChildWindows();
	long ActiveColumnAsLong(); // Always as long
	CComBSTR ActiveColumnAsBSTR(); // Always as BSTR
	BOOL PreTranslateAccelerator(LPMSG pMsg, HRESULT& hRet);
	void EditCell( long nRow, long nColumn );

	void Requery_Recordset();
	DataType ADOType2DataType(DataTypeEnum dt);
	VARTYPE DataType2VARTYPE( DataType dt );

	void Sort( long nColumn );

protected:

	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnSetCursor(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnKeyUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnGetDlgCode(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

private:

	// Predicates used for sorting
	
	struct pr_I2 : public binary_function< Row*, Row*, BOOL >
	{		
		pr_I2( long nColumn );		
		BOOL operator()( const const Row* a, const Row* b ) const;

	private:
		long m_nColumn;
	};

	struct pr_I4 : public binary_function< Row*, Row*, BOOL >
	{		
		pr_I4( long nColumn );		
		BOOL operator()( const const Row* a, const Row* b ) const;

	private:
		long m_nColumn;
	};

	struct pr_R4 : public binary_function< Row*, Row*, BOOL >
	{		
		pr_R4( long nColumn );		
		BOOL operator()( const const Row* a, const Row* b ) const;

	private:
		long m_nColumn;
	};

	struct pr_R8 : public binary_function< Row*, Row*, BOOL >
	{		
		pr_R8( long nColumn );		
		BOOL operator()( const const Row* a, const Row* b ) const;

	private:
		long m_nColumn;
	};

	struct pr_Cy : public binary_function< Row*, Row*, BOOL >
	{		
		pr_Cy( long nColumn );		
		BOOL operator()( const const Row* a, const Row* b ) const;

	private:
		long m_nColumn;
	};

	struct pr_Date : public binary_function< Row*, Row*, BOOL >
	{		
		pr_Date( long nColumn );		
		BOOL operator()( const const Row* a, const Row* b ) const;

	private:
		long m_nColumn;
	};

	struct pr_BStr : public binary_function< Row*, Row*, BOOL >
	{		
		pr_BStr( long nColumn );		
		BOOL operator()( const const Row* a, const Row* b ) const;

	private:
		long m_nColumn;
	};

	struct pr_Code : public binary_function< Row*, Row*, BOOL >
	{		
		pr_Code( long nColumn );		
		BOOL operator()( const const Row* a, const Row* b ) const;

	private:
		long m_nColumn;
	};

	struct pr_Bool : public binary_function< Row*, Row*, BOOL >
	{		
		pr_Bool( long nColumn );
		BOOL operator()( const const Row* a, const Row* b ) const;

	private:
		long m_nColumn;
	};

};

#endif //__SMARTGRID_H_

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions