GME  13
GMEViewCtrl.cpp
Go to the documentation of this file.
00001 // GMEViewCtrl.cpp : Implementation of the CGMEViewCtrl ActiveX Control class.
00002 
00003 #include "stdafx.h"
00004 #include "GMEView.h"
00005 #include "GMEViewCtrl.h"
00006 #include "GMEViewPropPage.h"
00007 #include "..\Interfaces\Mga_i.c"
00008 #include "GMEViewStd.h"
00009 //#include <comdef.h> // _bstr_t
00010 
00011 
00012 #ifdef _DEBUG
00013 #define new DEBUG_NEW
00014 #undef THIS_FILE
00015 static char THIS_FILE[] = __FILE__;
00016 #endif
00017 
00018 
00019 
00020 IMPLEMENT_DYNCREATE(CGMEViewCtrl, COleControl)
00021 
00022 
00023 
00024 // Message map
00025 
00026 BEGIN_MESSAGE_MAP(CGMEViewCtrl, COleControl)
00027         //{{AFX_MSG_MAP(CGMEViewCtrl)
00028         ON_WM_CREATE()
00029         //}}AFX_MSG_MAP
00030         ON_OLEVERB(AFX_IDS_VERB_EDIT, OnEdit)
00031         ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
00032 END_MESSAGE_MAP()
00033 
00034 
00035 
00036 // Dispatch map
00037 
00038 BEGIN_DISPATCH_MAP(CGMEViewCtrl, COleControl)
00039         //{{AFX_DISPATCH_MAP(CGMEViewCtrl)
00040         DISP_FUNCTION_ID(CGMEViewCtrl, "SetCurrentProject", dispidSetCurrentProject, SetCurrentProject, VT_EMPTY, VTS_UNKNOWN)
00041         DISP_FUNCTION_ID(CGMEViewCtrl, "SetMetaModel", dispidSetMetaModel, SetMetaModel, VT_EMPTY, VTS_UNKNOWN)
00042         DISP_FUNCTION_ID(CGMEViewCtrl, "SetModel", dispidSetModel, SetModel, VT_EMPTY, VTS_UNKNOWN)
00043         DISP_FUNCTION_ID(CGMEViewCtrl, "ChangeAspect", dispidChangeAspect, ChangeAspect, VT_EMPTY, VTS_I4)
00044         DISP_FUNCTION_ID(CGMEViewCtrl, "CycleAspect", dispidCycleAspect, CycleAspect, VT_EMPTY, VTS_NONE)
00045         DISP_FUNCTION_ID(CGMEViewCtrl, "Invalidate", dispidInvalidate, Invalidate, VT_EMPTY, VTS_NONE)
00046         //}}AFX_DISPATCH_MAP
00047         DISP_FUNCTION_ID(CGMEViewCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
00048 END_DISPATCH_MAP()
00049 
00050 
00051 
00052 // Event map
00053 
00054 BEGIN_EVENT_MAP(CGMEViewCtrl, COleControl)
00055         //{{AFX_EVENT_MAP(CGMEViewCtrl)
00056         // NOTE - ClassWizard will add and remove event map entries
00057         //    DO NOT EDIT what you see in these blocks of generated code !
00058         //}}AFX_EVENT_MAP
00059 END_EVENT_MAP()
00060 
00061 
00062 
00064 // Native COM interface map - peter
00065 BEGIN_INTERFACE_MAP(CGMEViewCtrl, COleControl)
00066         INTERFACE_PART(CGMEViewCtrl, IID_IMgaEventSink, EventSink)
00067 END_INTERFACE_MAP()
00068 
00069 
00070 // Property pages
00071 
00072 // TODO: Add more property pages as needed.  Remember to increase the count!
00073 BEGIN_PROPPAGEIDS(CGMEViewCtrl, 1)
00074         PROPPAGEID(CGMEViewPropPage::guid)
00075 END_PROPPAGEIDS(CGMEViewCtrl)
00076 
00077 
00078 
00079 // Initialize class factory and guid
00080 
00081 IMPLEMENT_OLECREATE_EX(CGMEViewCtrl, "GMEVIEW.GMEViewCtrl.1",
00082         0x1758edff, 0x969c, 0x4f6e, 0xaf, 0x81, 0xe8, 0xaf, 0x7, 0xa3, 0x37, 0x99)
00083 
00084 
00085 
00086 // Type library ID and version
00087 
00088 IMPLEMENT_OLETYPELIB(CGMEViewCtrl, _tlid, _wVerMajor, _wVerMinor)
00089 
00090 
00091 
00092 // Interface IDs
00093 
00094 const IID BASED_CODE IID_DGMEView =
00095                 { 0xA038BDA8, 0xD878, 0x41CF, { 0xA5, 0x91, 0x60, 0xA2, 0xDF, 0x3E, 0xDE, 0x70 } };
00096 const IID BASED_CODE IID_DGMEViewEvents =
00097                 { 0xDF9490E3, 0x4BF6, 0x4DAD, { 0x95, 0x34, 0xAF, 0x8F, 0xD8, 0x1E, 0x68, 0x39 } };
00098 
00099 
00100 
00101 // Control type information
00102 
00103 static const DWORD BASED_CODE _dwGMEViewOleMisc =
00104         OLEMISC_ACTIVATEWHENVISIBLE |
00105         OLEMISC_SETCLIENTSITEFIRST |
00106         OLEMISC_INSIDEOUT |
00107         OLEMISC_CANTLINKINSIDE |
00108         OLEMISC_RECOMPOSEONRESIZE;
00109 
00110 IMPLEMENT_OLECTLTYPE(CGMEViewCtrl, IDS_GMEVIEW, _dwGMEViewOleMisc)
00111 
00112 
00113 
00114 // CGMEViewCtrl::CGMEViewCtrlFactory::UpdateRegistry -
00115 // Adds or removes system registry entries for CGMEViewCtrl
00116 
00117 BOOL CGMEViewCtrl::CGMEViewCtrlFactory::UpdateRegistry(BOOL bRegister)
00118 {
00119         // TODO: Verify that your control follows apartment-model threading rules.
00120         // Refer to MFC TechNote 64 for more information.
00121         // If your control does not conform to the apartment-model rules, then
00122         // you must modify the code below, changing the 6th parameter from
00123         // afxRegApartmentThreading to 0.
00124 
00125         if (bRegister)
00126                 return AfxOleRegisterControlClass(
00127                         AfxGetInstanceHandle(),
00128                         m_clsid,
00129                         m_lpszProgID,
00130                         IDS_GMEVIEW,
00131                         IDB_GMEVIEW,
00132                         afxRegInsertable | afxRegApartmentThreading,
00133                         _dwGMEViewOleMisc,
00134                         _tlid,
00135                         _wVerMajor,
00136                         _wVerMinor);
00137         else
00138                 return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
00139 }
00140 
00141 
00142 
00144 // CGMEViewCtrl::XEventSink - native COM interface - peter
00145 STDMETHODIMP_(ULONG) CGMEViewCtrl::XEventSink::AddRef()
00146 {
00147         METHOD_PROLOGUE(CGMEViewCtrl,EventSink)
00148         return pThis->ExternalAddRef();
00149 }
00150 
00151 STDMETHODIMP_(ULONG) CGMEViewCtrl::XEventSink::Release()
00152 {
00153         METHOD_PROLOGUE(CGMEViewCtrl,EventSink)
00154         return pThis->ExternalRelease();
00155 }
00156 
00157 STDMETHODIMP CGMEViewCtrl::XEventSink::QueryInterface(REFIID riid, void** ppv)
00158 {
00159         METHOD_PROLOGUE(CGMEViewCtrl,EventSink)
00160         return pThis->ExternalQueryInterface(&riid, ppv);
00161 }
00162 
00163 STDMETHODIMP CGMEViewCtrl::XEventSink::GlobalEvent(globalevent_enum event)
00164 {
00165         METHOD_PROLOGUE(CGMEViewCtrl,EventSink)
00166         // TODO: Handle global events here - peter
00167         pThis->OnMgaGlobalEvent(event);
00168         return S_OK;
00169 }
00170 
00171 STDMETHODIMP CGMEViewCtrl::XEventSink::ObjectEvent(IMgaObject* obj, unsigned long eventmask, VARIANT v)
00172 {
00173         METHOD_PROLOGUE(CGMEViewCtrl,EventSink)
00174         // TODO: Handle object events here - peter
00175         pThis->m_MgaObjectEventList.Add(obj,eventmask);
00176         return S_OK;
00177 }
00178 
00179 
00180 // CGMEViewCtrl::CGMEViewCtrl - Constructor
00181 
00182 CGMEViewCtrl::CGMEViewCtrl():
00183         m_gmeViewFrame(NULL),
00184         aspect(0)
00185 {
00186         InitializeIIDs(&IID_DGMEView, &IID_DGMEViewEvents);
00187         // TODO: Initialize your control's instance data here.
00188 }
00189 
00190 
00191 
00192 // CGMEViewCtrl::~CGMEViewCtrl - Destructor
00193 
00194 CGMEViewCtrl::~CGMEViewCtrl()
00195 {
00196         // TODO: Cleanup your control's instance data here.
00197 
00198         m_gmeViewFrame = NULL;
00199 }
00200 
00201 
00202 
00203 // CGMEViewCtrl::OnDraw - Drawing function
00204 
00205 void CGMEViewCtrl::OnDraw(
00206                         CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
00207 {
00208         if (!pdc)
00209                 return;
00210 
00211         if (m_gmeViewFrame != NULL) {
00212                 m_gmeViewFrame->MoveWindow(rcBounds, TRUE);
00213         } else {
00214                 AFX_MANAGE_STATE(AfxGetStaticModuleState());
00215                 CBrush brush(RGB(255, 255, 255));
00216                 pdc->FillRect(rcBounds, &brush);
00217 
00218                 CRect edge(rcBounds);
00219                 pdc->DrawEdge(&edge, EDGE_BUMP, BF_RECT);
00220                 
00221                 CString label("GMEView OCX");
00222 
00223                 BITMAP bm;
00224                 CBitmap bitmap;
00225                 bitmap.LoadBitmap(IDB_GMEVIEW);
00226                 bitmap.GetBitmap(&bm);
00227                 CSize size(bm.bmWidth, bm.bmHeight);
00228                 pdc->DPtoLP(&size);
00229                 CPoint org(0,0);
00230                 pdc->DPtoLP(&org);
00231                 CPoint pos(rcBounds.Width() / 2, rcBounds.Height() / 2 - (pdc->GetTextExtent(label).cy));
00232                 CDC dcMem;
00233                 dcMem.CreateCompatibleDC(pdc);
00234                 CBitmap* oldbitmap = dcMem.SelectObject(&bitmap);
00235                 dcMem.SetMapMode(pdc->GetMapMode());
00236                 pdc->BitBlt(pos.x, pos.y, size.cx, size.cy, &dcMem, org.x, org.y, SRCCOPY);
00237                 dcMem.SelectObject(oldbitmap);
00238 
00239                 pdc->SetTextAlign(TA_CENTER);
00240                 pdc->TextOut(rcBounds.Width() / 2, rcBounds.Height() / 2, CString("GMEView ActiveX Control"));
00241         }
00242 }
00243 
00244 
00245 
00246 // CGMEViewCtrl::DoPropExchange - Persistence support
00247 
00248 void CGMEViewCtrl::DoPropExchange(CPropExchange* pPX)
00249 {
00250         ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
00251         COleControl::DoPropExchange(pPX);
00252 
00253         // TODO: Call PX_ functions for each persistent custom property.
00254 }
00255 
00256 
00257 
00258 // CGMEViewCtrl::OnResetState - Reset control to default state
00259 
00260 void CGMEViewCtrl::OnResetState()
00261 {
00262         COleControl::OnResetState();  // Resets defaults found in DoPropExchange
00263 
00264         // TODO: Reset any other control state here.
00265 }
00266 
00267 
00268 
00269 // CGMEViewCtrl::AboutBox - Display an "About" box to the user
00270 
00271 void CGMEViewCtrl::AboutBox()
00272 {
00273         CDialog dlgAbout(IDD_ABOUTBOX_GMEVIEW);
00274         dlgAbout.DoModal();
00275 }
00276 
00277 
00278 
00279 // CGMEViewCtrl message handlers
00280 
00281 int CGMEViewCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
00282 {
00283         if (COleControl::OnCreate(lpCreateStruct) == -1)
00284                 return -1;
00285 
00286 //      if (m_gmeViewFrame->Create(IDD_GMEVIEW_DIALOG, this) == FALSE)
00287 //              return -1;
00288 
00289         if (m_gmeViewFrame == NULL) {
00290                 m_gmeViewFrame = new CGMEViewFrame;
00291                 CRect rect(0, 0, 0, 0);
00292                 CString strTitle;
00293 //              VERIFY(strTitle.LoadString(IDS_PROPSHT_CAPTION));
00294                 strTitle = "DummyProbe Title";
00295 
00296                 if (!m_gmeViewFrame->Create(NULL, strTitle, WS_CHILD, rect, this)) {
00297                         delete m_gmeViewFrame;
00298                         m_gmeViewFrame = NULL;
00299                         return -1;
00300                 } else {
00301                         if (m_gmeViewFrame != NULL) {
00302                                 m_gmeViewFrame->SetCurrentProject(mgaProject);
00303                                 m_gmeViewFrame->SetMetaModel(mgaMetaModel);
00304                                 m_gmeViewFrame->SetModel(mgaModel);
00305                                 m_gmeViewFrame->ChangeAspect(aspect);
00306 
00307                                 if (!m_gmeViewFrame->IsWindowVisible())
00308                                         m_gmeViewFrame->ShowWindow(SW_SHOW);
00309                                 m_gmeViewFrame->Invalidate();
00310                         }
00311                 }
00312         }
00313 
00314         return 0;
00315 }
00316 
00317 BOOL CGMEViewCtrl::PreCreateWindow(CREATESTRUCT& cs) 
00318 {
00319         cs.dwExStyle |= WS_EX_CONTROLPARENT;
00320         return COleControl::PreCreateWindow(cs);
00321 }
00322 
00323 void CGMEViewCtrl::SetCurrentProject(IUnknown* project)
00324 {
00325         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00326 
00327         if (mgaProject != NULL)
00328                 mgaProject.Release();
00329 
00330         CComQIPtr<IMgaProject> ccpMgaProject(project);
00331 
00332         if (project != NULL && ccpMgaProject == NULL) {
00333                 ThrowError(CTL_E_INVALIDPROPERTYVALUE, _T("Unable to cast MgaProject."));
00334                 return;
00335         }
00336 
00337         if (m_gmeViewFrame != NULL)
00338                 m_gmeViewFrame->SetCurrentProject(ccpMgaProject);
00339         else
00340                 mgaProject = ccpMgaProject;
00341 }
00342 
00343 void CGMEViewCtrl::SetMetaModel(IUnknown* meta)
00344 {
00345         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00346 
00347         if (mgaMetaModel != NULL)
00348                 mgaMetaModel.Release();
00349 
00350         CComQIPtr<IMgaMetaModel> ccpMgaMetaModel(meta);
00351 
00352         if (meta != NULL && ccpMgaMetaModel == NULL) {
00353                 ThrowError(CTL_E_INVALIDPROPERTYVALUE, _T("Unable to cast MgaMetaModel."));
00354                 return;
00355         }
00356 
00357         if (m_gmeViewFrame != NULL)
00358                 m_gmeViewFrame->SetMetaModel(ccpMgaMetaModel);
00359         else
00360                 mgaMetaModel = ccpMgaMetaModel;
00361 }
00362 
00363 void CGMEViewCtrl::SetModel(IUnknown* model)
00364 {
00365         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00366 
00367         if (mgaModel != NULL)
00368                 mgaModel.Release();
00369 
00370         CComQIPtr<IMgaModel> ccpMgaModel(model);
00371 
00372         if (model != NULL && ccpMgaModel == NULL) {
00373                 ThrowError(CTL_E_INVALIDPROPERTYVALUE, _T("Unable to cast MgaModel."));
00374                 return;
00375         }
00376 
00377         if (m_gmeViewFrame != NULL)
00378                 m_gmeViewFrame->SetModel(ccpMgaModel);
00379         else
00380                 mgaModel = ccpMgaModel;
00381 }
00382 
00383 void CGMEViewCtrl::ChangeAspect(LONG index)
00384 {
00385         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00386 
00387         if (m_gmeViewFrame != NULL)
00388                 m_gmeViewFrame->ChangeAspect(index);
00389         else
00390                 aspect = index;
00391 }
00392 
00393 void CGMEViewCtrl::CycleAspect(void)
00394 {
00395         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00396 
00397         // TODO
00398 /*      if (m_gmeViewFrame != NULL)
00399                 m_gmeViewFrame->CycleAspect();
00400         else
00401                 aspect = index;*/
00402 }
00403 
00404 void CGMEViewCtrl::Invalidate(void)
00405 {
00406         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00407 
00408         if (m_gmeViewFrame != NULL)
00409                 m_gmeViewFrame->Invalidate();
00410 }
00411 
00412 void CGMEViewCtrl::OnMgaObjectEvent(IMgaObject* obj, unsigned long eventmask)
00413 {
00414         CComPtr<IMgaObject> ccpMgaObject(obj);
00415         // Handling Object Events
00416         EVENT_TRACE("\n__________ Object Event Begin _________\n");
00417 
00418 #ifdef _DEBUG
00419         if (eventmask & OBJEVENT_DESTROYED) {
00420                 EVENT_TRACE("OBJEVENT_DESTROYED\n");
00421         }
00422         if (eventmask & OBJEVENT_REGISTRY) {
00423                 EVENT_TRACE("OBJEVENT_REGISTRY\n");
00424         }
00425         if (eventmask & OBJEVENT_PROPERTIES) {
00426                 EVENT_TRACE("OBJEVENT_PROPERTIES\n");
00427         }
00428         if (eventmask & OBJEVENT_CREATED) {
00429                 EVENT_TRACE("OBJEVENT_CREATED\n");
00430         }
00431         if (eventmask & OBJEVENT_ATTR) {
00432                 EVENT_TRACE("OBJEVENT_ATTR\n");
00433         }
00434         if(eventmask & OBJEVENT_NEWCHILD) {
00435                 EVENT_TRACE("OBJEVENT_NEWCHILD\n");
00436         }
00437         if(eventmask & OBJEVENT_RELATION) {
00438                 EVENT_TRACE("OBJEVENT_RELATION\n");
00439         }
00440         if(eventmask & OBJEVENT_SUBT_INST) {
00441                 EVENT_TRACE("OBJEVENT_SUBT_INST\n");
00442         }
00443         if(eventmask & OBJEVENT_PARENT) {
00444                 EVENT_TRACE("OBJEVENT_PARENT\n");
00445         }
00446         if(eventmask & OBJEVENT_LOSTCHILD) {
00447                 EVENT_TRACE("OBJEVENT_LOSTCHILD\n");
00448         }
00449         if(eventmask & OBJEVENT_REFERENCED) {
00450                 EVENT_TRACE("OBJEVENT_REFERENCED\n");
00451         }
00452         if(eventmask & OBJEVENT_CONNECTED) {
00453                 EVENT_TRACE("OBJEVENT_CONNECTED\n");
00454         }
00455         if(eventmask & OBJEVENT_SETINCLUDED) {
00456                 EVENT_TRACE("OBJEVENT_SETINCLUDED\n");
00457         }
00458         if(eventmask & OBJEVENT_REFRELEASED) {
00459                 EVENT_TRACE("OBJEVENT_REFRELEASED\n");
00460         }
00461         if(eventmask & OBJEVENT_DISCONNECTED) {
00462                 EVENT_TRACE("OBJEVENT_DISCONNECTED\n");
00463         }
00464         if(eventmask & OBJEVENT_SETEXCLUDED) {
00465                 EVENT_TRACE("OBJEVENT_SETEXCLUDED\n");
00466         }
00467         if(eventmask & OBJEVENT_USERBITS) {
00468                 EVENT_TRACE("OBJEVENT_USERBITS\n");
00469         }
00470         if(eventmask & OBJEVENT_CLOSEMODEL) {
00471                 EVENT_TRACE("OBJEVENT_CLOSEMODEL\n");
00472         }
00473 #endif /*_DEBUG */
00474 
00475         EVENT_TRACE("_________ Object Event End _______\n");
00476 }
00477 
00478 void CGMEViewCtrl::OnMgaGlobalEvent(globalevent_enum event)
00479 {
00480         // Handling Global Events
00481         if (event == GLOBALEVENT_COMMIT_TRANSACTION) {
00482                 EVENT_TRACE("GLOBALEVENT_COMMIT_TRANSACTION\n");
00483 
00484                 // Send message to UI
00485                 PropagateMgaMessages();
00486         } else if (event == GLOBALEVENT_NOTIFICATION_READY) {
00487                 EVENT_TRACE("GLOBALEVENT_NOTIFICATION_READY\n");
00488 
00489                 // Send message to UI
00490                 PropagateMgaMessages();
00491         } else if (event == GLOBALEVENT_ABORT_TRANSACTION) {
00492                 EVENT_TRACE("GLOBALEVENT_ABORT_TRANSACTION\n");
00493 
00494                 // Removing the elements
00495                 m_MgaObjectEventList.RemoveAll();
00496         } else if (event == GLOBALEVENT_UNDO) {
00497                 EVENT_TRACE("GLOBALEVENT_UNDO\n");
00498                 // Refresh
00499         } else if (event==GLOBALEVENT_REDO) {
00500                 EVENT_TRACE("GLOBALEVENT_REDO\n");
00501                 // Refresh
00502         }
00503 
00504         // Not handled messages are not in the release
00505         // but useful for development
00506 #ifdef _DEBUG
00507         if (event == GLOBALEVENT_PROJECT_PROPERTIES) {
00508                 EVENT_TRACE("GLOBALEVENT_PROJECT_PROPERTIES\n");
00509         }
00510         if (event == GLOBALEVENT_OPEN_PROJECT) {
00511                 EVENT_TRACE("GLOBALEVENT_OPEN_PROJECT\n");
00512         }
00513         if (event == GLOBALEVENT_CLOSE_PROJECT) {
00514                 EVENT_TRACE("GLOBALEVENT_CLOSE_PROJECT\n");
00515         }
00516         if (event == GLOBALEVENT_NEW_TERRITORY) {
00517                 EVENT_TRACE("GLOBALEVENT_NEW_TERRITORY\n");
00518         }
00519         if (event == GLOBALEVENT_DESTROY_TERRITORY) {
00520                 EVENT_TRACE("GLOBALEVENT_DESTROY_TERRITORY\n");
00521         }
00522 
00523 #endif /* _DEBUG */
00524 
00525         EVENT_TRACE("_________ Global Event End _________\n");
00526 }
00527 
00528 void CGMEViewCtrl::SendAspectChanged(LONG index)
00529 {
00530         this->FireAspectChanged(index);
00531 }
00532 
00533 void CGMEViewCtrl::SendZoomChanged(LONG index)
00534 {
00535         this->FireZoomChanged(index);
00536 }
00537 
00538 void CGMEViewCtrl::SendWriteStatusZoom(LONG zoomVal)
00539 {
00540         this->FireWriteStatusZoom(zoomVal);
00541 }
00542 
00543 void CGMEViewCtrl::PropagateMgaMessages()
00544 {
00545         // Iterating through the list
00546         while (!m_MgaObjectEventList.IsEmpty()) {
00547                 CMgaObjectEventWrapper* pEventWrapper =
00548                         (CMgaObjectEventWrapper*)m_MgaObjectEventList.RemoveHead();
00549                 // Propagating message to AggregatePropertyPage
00550 
00551                 OnMgaObjectEvent(pEventWrapper->m_ccpMgaObject, pEventWrapper->m_eventmask);
00552 
00553                 delete pEventWrapper;
00554         }
00555 }
00556