home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / ActiveX / InaGrid_v2.0_32bit_ActiveX-FCN / data1.cab / InaEdit / InaEdit.cpp < prev    next >
C/C++ Source or Header  |  1999-07-19  |  9KB  |  328 lines

  1. // InaEdit.cpp : Implementation of InaEdit and DLL registration.
  2. #define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers
  3.  
  4. #include <afxctl.h>         // MFC support for ActiveX Controls
  5.  
  6. #include "resource.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CEditControl
  17. class CEditControl : public COleControl
  18. {
  19.     DECLARE_DYNCREATE(CEditControl)
  20.  
  21. // Constructor
  22. public:
  23.     CEditControl();
  24.  
  25. // Overrides
  26.     // ClassWizard generated virtual function overrides
  27.     //{{AFX_VIRTUAL(CEditControl)
  28.     public:
  29.     virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid);
  30.     //}}AFX_VIRTUAL
  31.  
  32. // Implementation
  33. protected:
  34.     DECLARE_OLECREATE_EX(CEditControl)    // Class factory and guid
  35.     DECLARE_OLETYPELIB(CEditControl)      // GetTypeInfo
  36.     DECLARE_OLECTLTYPE(CEditControl)        // Type name and misc status
  37.  
  38.     // Subclassed control support
  39.     BOOL PreCreateWindow(CREATESTRUCT& cs);
  40.     BOOL IsSubclassedControl();
  41.  
  42. // Message maps
  43.     //{{AFX_MSG(CEditControl)
  44.     afx_msg void OnKillFocus(CWnd* pNewWnd);
  45.     afx_msg UINT OnGetDlgCode();
  46.     afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
  47.     //}}AFX_MSG
  48.     DECLARE_MESSAGE_MAP()
  49.  
  50. // Dispatch maps
  51.     //{{AFX_DISPATCH(CEditControl)
  52.     afx_msg long GetWindowStyle();
  53.     afx_msg void SetWindowStyle(long nNewValue);
  54.     afx_msg BOOL RecreateControl();
  55.     afx_msg BSTR GetCaption();
  56.     afx_msg void SetCaption(LPCTSTR lpszNewValue);
  57.     //}}AFX_DISPATCH
  58.     DECLARE_DISPATCH_MAP()
  59.  
  60.     CEdit& GetEdit() const
  61.     { return *(CEdit*)this; }
  62.  
  63. // Event maps
  64.     //{{AFX_EVENT(CEditControl)
  65.     void FireOnNoFocus()
  66.         {FireEvent(eventidOnNoFocus,EVENT_PARAM(VTS_NONE));}
  67.     //}}AFX_EVENT
  68.     DECLARE_EVENT_MAP()
  69.  
  70. // Dispatch and event IDs
  71. public:
  72.     enum {
  73.     //{{AFX_DISP_ID(CEditControl)
  74.     dispidWindowStyle = 1L,
  75.     dispidRecreateControl = 2L,
  76.     eventidOnNoFocus = 1L,
  77.     //}}AFX_DISP_ID
  78.     };
  79.  
  80. protected:
  81.     long m_Style;
  82. };
  83.  
  84. COleControlModule NEAR theApp;
  85.  
  86. const GUID CDECL BASED_CODE _tlid =
  87.         { 0x73f8e94a, 0x692c, 0x11d1, { 0x95, 0x1e, 0, 0xa0, 0x24, 0x83, 0x4f, 0xe6 } };
  88. const WORD _wVerMajor = 2;
  89. const WORD _wVerMinor = 0;
  90.  
  91.  
  92. /////////////////////////////////////////////////////////////////////////////
  93. // DllRegisterServer - Adds entries to the system registry
  94.  
  95. STDAPI DllRegisterServer(void)
  96. {
  97.     AFX_MANAGE_STATE(_afxModuleAddrThis);
  98.  
  99.     if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
  100.         return ResultFromScode(SELFREG_E_TYPELIB);
  101.  
  102.     if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
  103.         return ResultFromScode(SELFREG_E_CLASS);
  104.  
  105.     return NOERROR;
  106. }
  107.  
  108.  
  109. /////////////////////////////////////////////////////////////////////////////
  110. // DllUnregisterServer - Removes entries from the system registry
  111.  
  112. STDAPI DllUnregisterServer(void)
  113. {
  114.     AFX_MANAGE_STATE(_afxModuleAddrThis);
  115.  
  116.     if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
  117.         return ResultFromScode(SELFREG_E_TYPELIB);
  118.  
  119.     if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
  120.         return ResultFromScode(SELFREG_E_CLASS);
  121.  
  122.     return NOERROR;
  123. }
  124.  
  125. IMPLEMENT_DYNCREATE(CEditControl, COleControl)
  126.  
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // Message map
  130.  
  131. BEGIN_MESSAGE_MAP(CEditControl, COleControl)
  132.     //{{AFX_MSG_MAP(CEditControl)
  133.     ON_WM_KILLFOCUS()
  134.     ON_WM_GETDLGCODE()
  135.     ON_WM_KEYDOWN()
  136.     //}}AFX_MSG_MAP
  137. END_MESSAGE_MAP()
  138.  
  139.  
  140. /////////////////////////////////////////////////////////////////////////////
  141. // Dispatch map
  142.  
  143. BEGIN_DISPATCH_MAP(CEditControl, COleControl)
  144.     //{{AFX_DISPATCH_MAP(CEditControl)
  145.     DISP_PROPERTY_EX(CEditControl, "Style", GetWindowStyle, SetWindowStyle, VT_I4)
  146.     DISP_FUNCTION(CEditControl, "RecreateControl", RecreateControl, VT_BOOL, VTS_NONE)
  147.     DISP_STOCKPROP_HWND()
  148.     DISP_PROPERTY_EX_ID(CEditControl, "Caption", DISPID_CAPTION, GetCaption, SetCaption, VT_BSTR)
  149.     //}}AFX_DISPATCH_MAP
  150. END_DISPATCH_MAP()
  151.  
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // Event map
  155.  
  156. BEGIN_EVENT_MAP(CEditControl, COleControl)
  157.     //{{AFX_EVENT_MAP(CEditControl)
  158.     EVENT_CUSTOM("OnNoFocus", FireOnNoFocus, VTS_NONE)
  159.     EVENT_STOCK_KEYDOWN()
  160.     //}}AFX_EVENT_MAP
  161. END_EVENT_MAP()
  162.  
  163.  
  164. /////////////////////////////////////////////////////////////////////////////
  165. // Initialize class factory and guid
  166.  
  167. IMPLEMENT_OLECREATE_EX(CEditControl, "INAEDIT.GridEditCtrl.1",
  168.     0x73f8e94d, 0x692c, 0x11d1, 0x95, 0x1e, 0, 0xa0, 0x24, 0x83, 0x4f, 0xe6)
  169.  
  170.  
  171. /////////////////////////////////////////////////////////////////////////////
  172. // Type library ID and version
  173.  
  174. IMPLEMENT_OLETYPELIB(CEditControl, _tlid, _wVerMajor, _wVerMinor)
  175.  
  176.  
  177. /////////////////////////////////////////////////////////////////////////////
  178. // Interface IDs
  179.  
  180. const IID BASED_CODE IID_DGridEdit =
  181.         { 0x73f8e94b, 0x692c, 0x11d1, { 0x95, 0x1e, 0, 0xa0, 0x24, 0x83, 0x4f, 0xe6 } };
  182. const IID BASED_CODE IID_DGridEditEvents =
  183.         { 0x73f8e94c, 0x692c, 0x11d1, { 0x95, 0x1e, 0, 0xa0, 0x24, 0x83, 0x4f, 0xe6 } };
  184.  
  185.  
  186. /////////////////////////////////////////////////////////////////////////////
  187. // Control type information
  188.  
  189. static const DWORD BASED_CODE _dwGridEditOleMisc =
  190.     OLEMISC_ACTIVATEWHENVISIBLE |
  191.     OLEMISC_SETCLIENTSITEFIRST |
  192.     OLEMISC_INSIDEOUT |
  193.     OLEMISC_CANTLINKINSIDE |
  194.     OLEMISC_RECOMPOSEONRESIZE;
  195.  
  196. IMPLEMENT_OLECTLTYPE(CEditControl, IDS_GRIDEDIT, _dwGridEditOleMisc)
  197.  
  198.  
  199. /////////////////////////////////////////////////////////////////////////////
  200. // CEditControl::CEditControlFactory::UpdateRegistry -
  201. // Adds or removes system registry entries for CEditControl
  202.  
  203. BOOL CEditControl::CEditControlFactory::UpdateRegistry(BOOL bRegister)
  204. {
  205.     // TODO: Verify that your control follows apartment-model threading rules.
  206.     // Refer to MFC TechNote 64 for more information.
  207.     // If your control does not conform to the apartment-model rules, then
  208.     // you must modify the code below, changing the 6th parameter from
  209.     // afxRegApartmentThreading to 0.
  210.  
  211.     if (bRegister)
  212.         return AfxOleRegisterControlClass(AfxGetInstanceHandle(), m_clsid, m_lpszProgID, IDS_GRIDEDIT, IDB_GRIDEDIT, 
  213.                                           afxRegApartmentThreading, _dwGridEditOleMisc, _tlid, _wVerMajor, _wVerMinor);
  214.     else
  215.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  216. }
  217.  
  218.  
  219. /////////////////////////////////////////////////////////////////////////////
  220. // CEditControl::CEditControl - Constructor
  221.  
  222. CEditControl::CEditControl()
  223. {
  224.     m_Style = 0;
  225.  
  226.     InitializeIIDs(&IID_DGridEdit, &IID_DGridEditEvents);
  227. }
  228.  
  229.  
  230. /////////////////////////////////////////////////////////////////////////////
  231. // CEditControl::OnDraw - Drawing function
  232. void CEditControl::OnDraw(
  233.             CDC* pdc, const CRect& rcBounds, const CRect& /*rcInvalid*/)
  234. {
  235.     DoSuperclassPaint(pdc, rcBounds);
  236. }
  237.  
  238.  
  239. /////////////////////////////////////////////////////////////////////////////
  240. // CEditControl::PreCreateWindow - Modify parameters for CreateWindowEx
  241. BOOL CEditControl::PreCreateWindow(CREATESTRUCT& cs)
  242. {
  243.     cs.lpszClass = _T("EDIT");
  244.  
  245.     if (m_Style != 0)
  246.         cs.style = m_Style;
  247.     else
  248.         cs.style |= ES_AUTOHSCROLL;
  249.  
  250.     cs.dwExStyle |= WS_EX_CLIENTEDGE;
  251.     return COleControl::PreCreateWindow(cs);
  252. }
  253.  
  254.  
  255. /////////////////////////////////////////////////////////////////////////////
  256. // CEditControl::IsSubclassedControl - This is a subclassed control
  257. BOOL CEditControl::IsSubclassedControl()
  258. {
  259.     return TRUE;
  260. }
  261.  
  262. /////////////////////////////////////////////////////////////////////////////
  263. // CEditControl message handlers
  264. void CEditControl::OnKillFocus(CWnd* pNewWnd) 
  265. {
  266.     COleControl::OnKillFocus(pNewWnd);
  267.     FireOnNoFocus();
  268. }
  269.  
  270.  
  271. BSTR CEditControl::GetCaption() 
  272. {
  273.     CString strResult;
  274.     GetWindowText(strResult);
  275.     return strResult.AllocSysString();
  276. }
  277.  
  278. void CEditControl::SetCaption(LPCTSTR lpszNewValue) 
  279. {
  280.     SetWindowText(lpszNewValue);
  281.     GetEdit().SetSel(0,-1);
  282.     SetModifiedFlag();
  283. }
  284.  
  285. UINT CEditControl::OnGetDlgCode() 
  286. {
  287.     return DLGC_WANTTAB | DLGC_WANTARROWS| DLGC_WANTALLKEYS | COleControl::OnGetDlgCode();
  288. }
  289.  
  290. void CEditControl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  291. {
  292.     COleControl::OnKeyDown(nChar, nRepCnt, nFlags);
  293.  
  294.     USHORT snChar = (USHORT)nChar;
  295.     short nShiftState = NULL;
  296.     if(GetKeyState(VK_SHIFT) < 0) nShiftState |= SHIFT_MASK;
  297.     if(GetKeyState(VK_CONTROL) < 0) nShiftState |= CTRL_MASK;
  298.     if(GetKeyState(VK_MENU) < 0) nShiftState |= ALT_MASK;
  299.  
  300.     FireKeyDown(&snChar, nShiftState);
  301. }
  302.  
  303. long CEditControl::GetWindowStyle() 
  304. {
  305.     // TODO: Add your property handler here
  306.  
  307.     return GetStyle();
  308. }
  309.  
  310. void CEditControl::SetWindowStyle(long nNewValue) 
  311. {
  312.     // TODO: Add your property handler here
  313.  
  314.     m_Style = nNewValue;
  315.  
  316. //    SetModifiedFlag();
  317. }
  318.  
  319. BOOL CEditControl::RecreateControl() 
  320. {
  321.     // TODO: Add your dispatch handler code here
  322.  
  323.     RecreateControlWindow();
  324.  
  325.     return m_hWnd != 0;
  326. }
  327.  
  328.