GME  13
GuiMeta.cpp
Go to the documentation of this file.
00001 #include "stdafx.h"
00002 #include "GMEStd.h"
00003 
00004 #include "meta.h"
00005 #include "GuiMeta.h"
00006 #include "GuiObject.h"
00007 
00008 
00009 // helper functions
00010 
00011 static bool FindMenu(CMenu* menu, const CString& menuName, CMenu*& subMenu)
00012 {
00013         subMenu = 0;
00014         for(unsigned int i = 0; i < menu->GetMenuItemCount(); i++) {
00015                 CString label;
00016                 menu->GetMenuString(i, label, MF_BYPOSITION);
00017                 if (label == menuName) {
00018                         subMenu = menu->GetSubMenu(i);
00019                         return true;
00020                 }
00021         }
00022         return false;
00023 }
00024 
00025 static bool FindMenuItem(CMenu* menu, const CString& itemName, unsigned int& pos)
00026 {
00027         if (menu) {
00028                 CString label;
00029                 for(pos = 0; pos < menu->GetMenuItemCount(); pos++) {
00030                         CString label;
00031                         menu->GetMenuString(pos, label, MF_BYPOSITION);
00032                         if (label == itemName)
00033                                 return true;
00034                 }
00035         }
00036         return false;
00037 }
00038 
00039 static bool FindMenuItem(CMenu* menu, const CString& menuName, CMenu*& subMenu, const CString& itemName, unsigned int& pos)
00040 {
00041         FindMenu(menu, menuName, subMenu);
00042         return FindMenuItem(subMenu, itemName, pos);
00043 }
00044 
00045 // assumes that all constructors are called already inside a transaction
00046 
00048 
00049 CGuiMetaProject *CGuiMetaProject::theInstance = 0;
00050 
00051 CGuiMetaProject::CGuiMetaProject(CComPtr<IMgaMetaProject> &mgaPt)
00052 {
00053         ASSERT(theInstance == 0);
00054         theInstance = this;
00055         minMenuCmdID = maxMenuCmdID = GME_DYN_MENU_MINID;
00056         metaTable.InitHashTable(97);
00057         mgaMeta = mgaPt;
00058         try {
00059                 {
00060                         CComBSTR bstr;
00061                         COMTHROW(mgaMeta->get_Name(&bstr));
00062                         CopyTo(bstr,name);
00063                 }
00064                 {
00065                         CComBSTR bstr;
00066                         COMTHROW(mgaMeta->get_DisplayedName(&bstr));
00067                         CopyTo(bstr,displayedName);
00068                 }
00069 
00070                 CComPtr<IMgaMetaFolder> mmFolder;
00071                 COMTHROW(mgaMeta->get_RootFolder(&mmFolder));
00072                 new CGuiMetaFolder(mmFolder);
00073         }
00074         catch(hresult_exception &) {
00075         }
00076 }
00077 
00078 CGuiMetaProject::~CGuiMetaProject()
00079 {
00080         POSITION pos = metaTable.GetStartPosition();
00081         void *key;
00082         CGuiMetaBase *guiMeta;
00083         while(pos) {
00084                 metaTable.GetNextAssoc(pos,key,guiMeta);
00085                 delete guiMeta;
00086         }
00087 
00088         pos =  metaRemainingList.GetHeadPosition();
00089         while (pos) 
00090                 delete metaRemainingList.GetNext(pos);
00091 
00092         theInstance = 0;
00093 }
00094 
00095 bool CGuiMetaProject::CmdIDInRange(int id)
00096 {
00097         return (id >= minMenuCmdID && id < maxMenuCmdID);
00098 }
00099 
00100 GMECommandCode CGuiMetaProject::CmdType(int id,CString &/*cmdName*/)
00101 {
00102         ASSERT(CmdIDInRange(id));
00103         return GME_CMD_CONTEXT;
00104 }
00105 
00106 CGuiMetaBase *CGuiMetaProject::GetGuiMeta(metaref_type key)
00107 {
00108         CGuiMetaBase *base;
00109         metaTable.Lookup((void *)key,base);
00110         return base;
00111 }
00112 
00113 void CGuiMetaProject::RegisterMetaObject(metaref_type key, CGuiMetaBase *mobj)
00114 {
00115         CGuiMetaBase *base;
00116         if (metaTable.Lookup((void*) key, base))
00117                 metaRemainingList.AddTail(mobj);
00118         else
00119                 metaTable[(void *)key] = mobj;
00120 }
00121 
00122 
00123 CGuiMetaAttribute *CGuiMetaProject::GetGuiMetaAttribute(metaref_type key)
00124 {
00125         return dynamic_cast<CGuiMetaAttribute *>(GetGuiMeta(key));
00126 }
00127 
00128 CGuiMetaFco *CGuiMetaProject::GetGuiMetaFco(metaref_type key)
00129 {
00130         return dynamic_cast<CGuiMetaFco *>(GetGuiMeta(key));
00131 }
00132 
00133 CGuiMetaModel *CGuiMetaProject::GetGuiMetaModel(metaref_type key)
00134 {
00135         return dynamic_cast<CGuiMetaModel *>(GetGuiMeta(key));
00136 }
00137 
00139 
00140 void CGuiMetaBase::Init(CComPtr<IMgaMetaBase> &mgaPt)
00141 {
00142         mgaMeta = mgaPt;
00143         try {
00144                         {
00145                                 CComBSTR bstr;
00146                                 COMTHROW(mgaMeta->get_Name(&bstr));
00147                                 CopyTo(bstr,name);
00148                         }
00149                         {
00150                                 CComBSTR bstr;
00151                                 COMTHROW(mgaMeta->get_DisplayedName(&bstr));
00152                                 CopyTo(bstr,displayedName);
00153                                 if(displayedName.IsEmpty())
00154                                         displayedName = name;
00155                         }
00156                         COMTHROW(mgaMeta->get_MetaRef(&metaref));
00157         }
00158         catch(hresult_exception &) {
00159         }
00160         CGuiMetaProject::theInstance->RegisterMetaObject(metaref, this);
00161 }
00162 
00163 
00164 void CGuiMetaBase::CreateFcos(CComPtr<IMgaMetaFCOs> &mmFcos)
00165 {
00166         CComPtr<IMgaMetaFCO> mmFco;
00167         MGACOLL_ITERATE(IMgaMetaFCO,mmFcos) {
00168                 CComPtr<IMgaMetaModel> mmModel;
00169                 mmFco = MGACOLL_ITER;
00170                 HRESULT hr;
00171                 hr = mmFco.QueryInterface(&mmModel);
00172                 if(FAILED(hr))
00173                         new CGuiMetaFco(mmFco);
00174                 else
00175                         new CGuiMetaModel(mmModel);
00176         }
00177         MGACOLL_ITERATE_END;
00178 }
00179 
00181 
00182 CGuiMetaFolder::CGuiMetaFolder(CComPtr<IMgaMetaFolder> &mgaPt)
00183 {
00184         CComPtr<IMgaMetaBase> basePt;
00185         try {
00186                 mgaPt.QueryInterface(&basePt);
00187                 Init(basePt);
00188                 CComPtr<IMgaMetaFolders> mmFolders;
00189                 CComPtr<IMgaMetaFolder> mmFolder;
00190                 COMTHROW(mgaPt->get_DefinedFolders(&mmFolders));
00191                 MGACOLL_ITERATE(IMgaMetaFolder,mmFolders) {
00192                         mmFolder = MGACOLL_ITER;
00193                         new CGuiMetaFolder(mmFolder);
00194                 }
00195                 MGACOLL_ITERATE_END;
00196 
00197                 CComPtr<IMgaMetaFCOs> mmFcos;
00198                 COMTHROW(mgaPt->get_DefinedFCOs(&mmFcos));
00199                 CreateFcos(mmFcos);
00200         }
00201         catch(hresult_exception &) {
00202         }
00203 }
00204 
00206 
00207 #define X1                      20
00208 #define X2                      150
00209 #define X3                      250
00210 #define X4                      260
00211 #define ENABLEWIDTH     25
00212 #define Y1                      25
00213 #define Y2                      45
00214 #define Y3                      18
00215 #define GAP                     10
00216 #define NUDGE           5
00217 
00218 CGuiMetaAttribute::CGuiMetaAttribute(CComPtr<IMgaMetaAttribute> &mgaPt,CGuiMetaFco *o) : owner (o)
00219 {
00220         CComPtr<IMgaMetaBase> basePt;
00221         try {
00222                 mgaPt.QueryInterface(&basePt);
00223                 Init(basePt);
00224                 CComBSTR bstr;
00225                 COMTHROW(mgaPt->get_DisplayedName(&bstr));
00226                 CopyTo(bstr,prompt);
00227         }
00228         catch(hresult_exception &) {
00229         }
00230 }
00231 
00233 
00234 CGuiMetaFieldAttribute::CGuiMetaFieldAttribute(CComPtr<IMgaMetaAttribute> &mgaPt,CGuiMetaFco *o) : CGuiMetaAttribute(mgaPt,o)
00235 {
00236         attval_enum tp;
00237         COMTHROW(mgaPt->get_ValueType(&tp));
00238 
00239         int multiline;
00240         CComBSTR attrPref;
00241         CComBSTR attrPrefPath = "multiLine";
00242         COMTHROW(mgaPt->get_RegistryValue(attrPrefPath, &attrPref));
00243         CString lines;
00244         CopyTo(attrPref, lines);
00245         if (_stscanf(lines, _T("%d"), &multiline) != 1) 
00246                 multiline = 0;
00247 }
00248 
00250 
00251 CGuiMetaToggleAttribute::CGuiMetaToggleAttribute(CComPtr<IMgaMetaAttribute> &mgaPt,CGuiMetaFco *o) : CGuiMetaAttribute(mgaPt,o)
00252 {
00253 }
00254 
00256 
00257 CGuiMetaMenuAttribute::CGuiMetaMenuAttribute(CComPtr<IMgaMetaAttribute> &mgaPt,CGuiMetaFco *o) : CGuiMetaAttribute(mgaPt,o)
00258 {
00259         CComPtr<IMgaMetaEnumItems> mgaItems;
00260         COMTHROW(mgaPt->get_EnumItems(&mgaItems));
00261 
00262         CStringList names;
00263         CStringList values;
00264 
00265         int i = 0;
00266         MGACOLL_ITERATE(IMgaMetaEnumItem,mgaItems) {
00267                 CComPtr<IMgaMetaEnumItem> mgaItem;
00268                 mgaItem = MGACOLL_ITER;
00269                 CComBSTR lbl;
00270                 COMTHROW(mgaItem->get_DisplayedName(&lbl));
00271                 CString nm;
00272                 CopyTo(lbl,nm);
00273                 names.AddTail(nm);
00274                 CComBSTR val;
00275                 COMTHROW(mgaItem->get_Value(&val));
00276                 CString vl;
00277                 CopyTo(val,vl);
00278                 values.AddTail(vl);
00279                 i++;
00280         }
00281         MGACOLL_ITERATE_END;
00282 }
00283 
00285 
00286 CGuiMetaFco::CGuiMetaFco(CComPtr<IMgaMetaFCO> &mgaPt)
00287 {
00288         CComPtr<IMgaMetaBase> basePt;
00289         try {
00290                 mgaPt.QueryInterface(&basePt);
00291                 Init(basePt);
00292                 SetAttributes();
00293         }
00294         catch(hresult_exception &) {
00295         }
00296 }
00297 
00298 CGuiMetaFco::CGuiMetaFco(CComPtr<IMgaMetaModel> &mgaPt)
00299 {
00300         CComPtr<IMgaMetaBase> basePt;
00301         try {
00302                 mgaPt.QueryInterface(&basePt);
00303                 Init(basePt);
00304                 SetAttributes();
00305         }
00306         catch(hresult_exception &) {
00307         }
00308 }
00309 
00310 
00311 void CGuiMetaFco::SetAttributes()
00312 {
00313         try {
00314                 CComPtr<IMgaMetaAttributes> mmAttrs;
00315                 CComPtr<IMgaMetaAttribute> mmAttr;
00316                 CComPtr<IMgaMetaFCO> fco;
00317                 mgaMeta.QueryInterface(&fco);
00318                 COMTHROW(fco->get_Attributes(&mmAttrs));
00319                 MGACOLL_ITERATE(IMgaMetaAttribute,mmAttrs) {
00320                         mmAttr = MGACOLL_ITER;
00321                         attval_enum atype;
00322                         COMTHROW(mmAttr->get_ValueType(&atype));
00323                         CGuiMetaAttribute       *mAttr = 0;
00324                         switch(atype) {
00325                         case ATTVAL_STRING:
00326                         case ATTVAL_INTEGER:
00327                         case ATTVAL_DOUBLE:
00328                                 mAttr = new CGuiMetaFieldAttribute(mmAttr,this);
00329                                 break;
00330                         case ATTVAL_BOOLEAN:
00331                                 mAttr = new CGuiMetaToggleAttribute(mmAttr,this);
00332                                 break;
00333                         case _attval_enum:
00334                                 mAttr = new CGuiMetaMenuAttribute(mmAttr,this);
00335                                 break;
00336                         case ATTVAL_REFERENCE:
00337                         default:
00338                                 VERIFY(false);
00339                         }
00340                         attrs.AddTail(mAttr);
00341                 }
00342                 MGACOLL_ITERATE_END;
00343         }
00344         catch(hresult_exception &) {
00345         }
00346 }
00347 
00349 
00350 CGuiMetaModel::CGuiMetaModel(CComPtr<IMgaMetaModel> &mgaPt) : CGuiMetaFco(mgaPt), partsSet(false)
00351 {
00352         try {
00353                 CComPtr<IMgaMetaFCOs> mmFcos;
00354                 COMTHROW(mgaPt->get_DefinedFCOs(&mmFcos));
00355                 CreateFcos(mmFcos);
00356 
00357                 CComPtr<IMgaMetaAspects> mmAspects;
00358                 COMTHROW(mgaPt->get_Aspects(&mmAspects));
00359                 int index = 0;
00360                 MGACOLL_ITERATE(IMgaMetaAspect,mmAspects) {
00361                         CComPtr<IMgaMetaAspect> mmAspect;
00362                         mmAspect = MGACOLL_ITER;
00363                         CGuiMetaAspect *mAspect = new CGuiMetaAspect(mmAspect,this,index++);
00364                         aspects.AddTail(mAspect);
00365                 }
00366                 MGACOLL_ITERATE_END;
00367                 VERIFY(index); // must have at least one aspect defined...
00368 
00369         }
00370         catch(hresult_exception &) {
00371         }
00372 }
00373 
00374 CGuiMetaModel::~CGuiMetaModel()
00375 {
00376         DeleteParts();
00377 }
00378 
00379 void CGuiMetaModel::DeleteParts()
00380 {
00381         POSITION pos = parts.GetHeadPosition();
00382         while(pos)
00383                 delete parts.GetNext(pos);
00384         parts.RemoveAll();
00385 }
00386 
00387 void CGuiMetaModel::InitParts()
00388 {
00389         CComPtr<IMgaFCO>        nil;
00390         CComPtr<IMgaMetaModel> mmModel;
00391         mgaMeta.QueryInterface(&mmModel);
00392         CComPtr<IMgaMetaRoles> mmRoles;
00393         COMTHROW(mmModel->get_Roles(&mmRoles));
00394         MGACOLL_ITERATE(IMgaMetaRole,mmRoles) {
00395                 CComPtr<IMgaMetaRole> mmRole;
00396                 mmRole = MGACOLL_ITER;
00397                 CComPtr<IMgaMetaFCO> fco;
00398                 COMTHROW(mmRole->get_Kind(&fco));
00399                 objtype_enum tp;
00400                 COMTHROW(fco->get_ObjType(&tp));
00401                 CGuiObject *guiObj = 0;
00402                 if(tp == OBJTYPE_MODEL) {
00403                         guiObj = new CGuiModel(nil,mmRole,NULL,NumberOfAspects());
00404                         ((CGuiModel *)guiObj)->InitObject(NULL);
00405                         ((CGuiModel *)guiObj)->SetAspect(0);
00406                         CSize s;
00407                 }
00408                 else if(tp == OBJTYPE_REFERENCE) {
00409                         guiObj = new CGuiReference(nil,mmRole,NULL,NumberOfAspects(),nil,nil);
00410                         ((CGuiReference *)guiObj)->InitObject(NULL);
00411                         ((CGuiReference *)guiObj)->SetAspect(0);
00412                 }
00413                 else if(tp != OBJTYPE_CONNECTION) {
00414                         guiObj = new CGuiObject(nil,mmRole,NULL,NumberOfAspects());
00415                         ((CGuiObject *)guiObj)->InitObject(NULL);
00416                         ((CGuiObject *)guiObj)->SetAspect(0);
00417                 }
00418                 if(!guiObj)
00419                         continue;
00420                 CComBSTR bstr;
00421                 COMTHROW(mmRole->get_DisplayedName(&bstr));
00422                 CopyTo(bstr,guiObj->name);
00423                 guiObj->SetAllSizesToNative();
00424 
00425                 parts.AddTail(guiObj);
00426         }
00427         MGACOLL_ITERATE_END;
00428 }
00429         
00430 void CGuiMetaModel::ResetParts()
00431 {
00432         DeleteParts();
00433         InitParts();
00434 }
00435 
00436 CGuiObjectList &CGuiMetaModel::GetParts()
00437 {
00438         if(!partsSet) {
00439                 InitParts();
00440                 partsSet = true;
00441         }
00442         return parts;
00443 }
00444 
00445 void CGuiMetaModel::InitAspectBox(CComboBox *box)
00446 {
00447         POSITION pos = aspects.GetHeadPosition();
00448         while(pos) {
00449                 CGuiMetaAspect *asp = aspects.GetNext(pos);
00450                 box->AddString(asp->displayedName);
00451         }
00452 }
00453 
00454 void CGuiMetaModel::InitAspectList(CListBox *list)
00455 {
00456         POSITION pos = aspects.GetHeadPosition();
00457         while(pos) {
00458                 CGuiMetaAspect *asp = aspects.GetNext(pos);
00459                 list->AddString(asp->displayedName);
00460         }
00461 }
00462 
00463 CGuiMetaAspect *CGuiMetaModel::GetFirstAspect()
00464 {
00465         return aspects.GetHead();
00466 }
00467 
00468 CGuiMetaAspect *CGuiMetaModel::FindAspect(int ind)
00469 {
00470         POSITION pos = aspects.GetHeadPosition();
00471         while(pos) {
00472                 CGuiMetaAspect *asp = aspects.GetNext(pos);
00473                 if(asp->index == ind)
00474                         return asp;
00475         }
00476         return 0;
00477 }
00478 
00479 CGuiMetaAspect *CGuiMetaModel::FindAspect(CString nm)
00480 {
00481         POSITION pos = aspects.GetHeadPosition();
00482         while(pos) {
00483                 CGuiMetaAspect *asp = aspects.GetNext(pos);
00484                 if(asp->name == nm)
00485                         return asp;
00486         }
00487         return 0;
00488 }
00489 
00490 int CGuiMetaModel::NumberOfAspects()
00491 {
00492         return aspects.GetCount();
00493 }
00494 
00495 /* !!! Verify this code before using it !!! Never used before */
00496 int CGuiMetaModel::MapAspect(CComPtr<IMgaMetaRole> &role,int index)
00497 {
00498         CComPtr<IMgaMetaModel> parent;
00499         COMTHROW(role->get_ParentModel(&parent));
00500         metaref_type metaid;
00501         COMTHROW(parent->get_MetaRef(&metaid));
00502         CGuiMetaModel *guiParent = CGuiMetaProject::theInstance->GetGuiMetaModel(metaid);
00503         VERIFY(guiParent);
00504         CGuiMetaAspect *aspect = guiParent->FindAspect(index);
00505         VERIFY(aspect);
00506         CComPtr<IMgaMetaAspect> metaAspect;
00507         COMTHROW(aspect->mgaMeta.QueryInterface(&metaAspect));
00508 
00509         CString kindAspect;
00510         try {
00511                 CComPtr<IMgaMetaParts> mmParts;
00512                 CComPtr<IMgaMetaPart> mmPart;
00513                 COMTHROW(role->get_Parts(&mmParts));
00514 
00515                 MGACOLL_ITERATE(IMgaMetaPart,mmParts) {
00516                         mmPart = MGACOLL_ITER;
00517                         CComPtr<IMgaMetaAspect> mmAspect;
00518                         COMTHROW(mmPart->get_ParentAspect(&mmAspect));
00519                         if(IsEqualObject(metaAspect,mmAspect)) {
00520                                 CComBSTR bstr;
00521                                 COMTHROW(mmPart->get_KindAspect(&bstr));
00522                                 CopyTo(bstr,kindAspect);
00523                                 if(kindAspect.IsEmpty())
00524                                         kindAspect = aspect->name;
00525                                 break;
00526                         }
00527                 }
00528                 MGACOLL_ITERATE_END;
00529         }
00530         catch(hresult_exception &) {
00531         }
00532         VERIFY(!kindAspect.IsEmpty());
00533         CGuiMetaAspect *found = FindAspect(kindAspect);
00534         VERIFY(found);
00535         return found->index;
00536 }
00537 
00538 bool CGuiMetaModel::GetPartByRole(CComPtr<IMgaMetaRole> &theRole,CComPtr<IMgaMetaAspect> &theAspect,CComPtr<IMgaMetaPart> &thePart) 
00539 {
00540         bool res = false;
00541         try {
00542                 CComPtr<IMgaMetaParts> mmParts;
00543                 CComPtr<IMgaMetaPart> mmPart;
00544                 COMTHROW(theRole->get_Parts(&mmParts));
00545 
00546                 MGACOLL_ITERATE(IMgaMetaPart,mmParts) {
00547                         mmPart = MGACOLL_ITER;
00548                         CComPtr<IMgaMetaAspect> mmAspect;
00549                         COMTHROW(mmPart->get_ParentAspect(&mmAspect));
00550                         if( !res && mmAspect == theAspect) {
00551                                 thePart = mmPart;
00552                                 res = true;// we will not return any more from the middle of an iteration
00553                         }
00554                 }
00555                 MGACOLL_ITERATE_END;
00556         }
00557         catch(hresult_exception &) {
00558         }
00559         return res;
00560 }
00562 
00563 CGuiMetaAspect::CGuiMetaAspect(CComPtr<IMgaMetaAspect> &mgaPt, CGuiMetaModel* o, int ind):
00564         owner(o),
00565         index(ind)
00566 {
00567         insertModelMenu = 0;
00568         insertAtomMenu = 0;
00569         insertReferenceMenu = 0;
00570         insertSetMenu = 0;
00571         CComPtr<IMgaMetaBase> basePt;
00572         try {
00573                 mgaPt.QueryInterface(&basePt);
00574                 Init(basePt);
00575 
00576                 CComPtr<IMgaMetaParts> mmParts;
00577                 CComPtr<IMgaMetaPart> mmPart;
00578                 COMTHROW(mgaPt->get_Parts(&mmParts));
00579                 MGACOLL_ITERATE(IMgaMetaPart,mmParts) {
00580                         mmPart = MGACOLL_ITER;
00581                         VARIANT_BOOL prim;
00582                         COMTHROW(mmPart->get_IsPrimary(&prim));
00583                         if(prim != VARIANT_FALSE) {
00584 
00585                                 CString roleName = static_cast<const TCHAR*>(mmPart->Role->Name);
00586                                 CString label;
00587                                 if (mmPart->Role->Kind->Name == mmPart->Role->Name) {
00588                                         label = static_cast<const TCHAR*>(mmPart->Role->Kind->DisplayedName);
00589                                 }
00590                                 else {
00591                                         label = static_cast<const TCHAR*>(mmPart->Role->Name);
00592                                 }
00593 
00594                                 objtype_enum type = mmPart->Role->Kind->ObjType;
00595                                 switch(type) {
00596                                         case OBJTYPE_MODEL:
00597                                                 if (!insertModelMenu)
00598                                                         insertModelMenu = new CDynMenu(CGuiMetaProject::theInstance->GetNewMenuCmdID(), _T("Insert New Model"));
00599                                                 insertModelMenu->AddItem(CGuiMetaProject::theInstance->GetNewMenuCmdID(), std::move(roleName), std::move(label), CString(_T("Help")));
00600                                                 break;
00601                                         case OBJTYPE_ATOM:
00602                                                 if (!insertAtomMenu)
00603                                                         insertAtomMenu = new CDynMenu(CGuiMetaProject::theInstance->GetNewMenuCmdID(), _T("Insert New Atom"));
00604                                                 insertAtomMenu->AddItem(CGuiMetaProject::theInstance->GetNewMenuCmdID(), std::move(roleName), std::move(label), CString(_T("Help")));
00605                                                 break;
00606                                         case OBJTYPE_REFERENCE:
00607                                                 if (!insertReferenceMenu)
00608                                                         insertReferenceMenu = new CDynMenu(CGuiMetaProject::theInstance->GetNewMenuCmdID(), _T("Insert New Reference"));
00609                                                 insertReferenceMenu->AddItem(CGuiMetaProject::theInstance->GetNewMenuCmdID(), std::move(roleName), std::move(label), CString(_T("Help")));
00610                                                 break;
00611                                         case OBJTYPE_SET:
00612                                                 if (!insertSetMenu)
00613                                                         insertSetMenu = new CDynMenu(CGuiMetaProject::theInstance->GetNewMenuCmdID(), _T("Insert New Set"));
00614                                                 insertSetMenu->AddItem(CGuiMetaProject::theInstance->GetNewMenuCmdID(), std::move(roleName), std::move(label), CString(_T("Help")));
00615                                                 break;
00616                                 }
00617                         }
00618                 }
00619                 MGACOLL_ITERATE_END;
00620         }
00621         catch(hresult_exception &) {
00622                 // FIXME
00623         }
00624         catch (_com_error& ) {
00625                 // FIXME
00626         }
00627         if (insertModelMenu)
00628                 insertModelMenu->Sort();
00629         if (insertAtomMenu)
00630                 insertAtomMenu->Sort();
00631         if (insertReferenceMenu)
00632                 insertReferenceMenu->Sort();
00633         if (insertSetMenu)
00634                 insertSetMenu->Sort();
00635 
00636         try {
00637                 CComPtr<IMgaMetaAttributes> mmAttrs;
00638                 CComPtr<IMgaMetaAttribute> mmAttr;
00639                 COMTHROW(mgaPt->get_Attributes(&mmAttrs));
00640                 MGACOLL_ITERATE(IMgaMetaAttribute,mmAttrs) {
00641                         mmAttr = MGACOLL_ITER;
00642                         metaref_type iden;
00643                         COMTHROW(mmAttr->get_MetaRef(&iden));
00644                         CGuiMetaAttribute *guiAttr = CGuiMetaProject::theInstance->GetGuiMetaAttribute(iden);
00645                         VERIFY(guiAttr);
00646 // HACK seems to be screwed: same attribute contained in different objects???
00647 //                      VERIFY(guiAttr->owner == owner);
00648                         attrs.AddTail(guiAttr);
00649                 }
00650                 MGACOLL_ITERATE_END;
00651         }
00652         catch(hresult_exception &) {
00653         }
00654 }
00655 
00656 CGuiMetaAspect::~CGuiMetaAspect()
00657 {
00658         delete insertModelMenu;
00659         delete insertAtomMenu;
00660         delete insertReferenceMenu;
00661         delete insertSetMenu;
00662 }
00663 
00664 void CGuiMetaAspect::GetMetaAspect(CComPtr<IMgaMetaAspect>& mAspect)
00665 {
00666         mgaMeta.QueryInterface(&mAspect);
00667 }
00668 
00669 void CGuiMetaAspect::InitContextMenu(CMenu* menu, const CString& label, CDynMenu* dm)
00670 {
00671         if (!dm)
00672                 return;
00673 
00674         unsigned int pos = 0;
00675         if (FindMenuItem(menu, label, pos)) {
00676                 menu->RemoveMenu(pos, MF_BYPOSITION | MF_POPUP);
00677                 CMenu& dmm = dm->GetMenu();
00678                 menu->InsertMenu(pos, MF_BYPOSITION | MF_POPUP |
00679                         (dm->GetCount() > 0 ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)),
00680                         (UINT)dmm.m_hMenu, label);
00681         }
00682 
00683 }
00684 
00685 void CGuiMetaAspect::InitContextMenu(CMenu* menu)
00686 {
00687         InitContextMenu(menu,"Insert New Model", insertModelMenu);
00688         InitContextMenu(menu,"Insert New Atom", insertAtomMenu);
00689         InitContextMenu(menu,"Insert New Reference", insertReferenceMenu);
00690         InitContextMenu(menu,"Insert New Set", insertSetMenu);
00691 }
00692 
00693 void CGuiMetaAspect::ResetContextMenu(CMenu* menu)
00694 {
00695         unsigned int pos = 0;
00696         CString name = "Insert New Model";
00697         if (FindMenuItem(menu, name, pos))
00698                 menu->RemoveMenu(pos, MF_BYPOSITION | MF_POPUP);
00699         name = "Insert New Atom";
00700         if (FindMenuItem(menu, name, pos))
00701                 menu->RemoveMenu(pos, MF_BYPOSITION | MF_POPUP);
00702         name = "Insert New Reference";
00703         if (FindMenuItem(menu, name, pos))
00704                 menu->RemoveMenu(pos, MF_BYPOSITION | MF_POPUP);
00705         name = "Insert New Set";
00706         if (FindMenuItem(menu, name, pos))
00707                 menu->RemoveMenu(pos, MF_BYPOSITION | MF_POPUP);
00708 }
00709 
00710 bool CGuiMetaAspect::FindCommand(int id, CString& label)
00711 {
00712         CDynMenuItem *dmi = 0;
00713         if ((insertModelMenu && (dmi = insertModelMenu->FindItem(id)) != 0) ||
00714                 (insertAtomMenu && (dmi = insertAtomMenu->FindItem(id)) != 0) ||
00715                 (insertReferenceMenu && (dmi = insertReferenceMenu->FindItem(id)) != 0) ||
00716                 (insertSetMenu && (dmi = insertSetMenu->FindItem(id)) != 0))
00717         {
00718                 label = dmi->GetLabel();
00719                 return true;
00720         }
00721         return false;
00722 }
00723 
00724 bool CGuiMetaAspect::GetRoleByName(const CString& roleName, CComPtr<IMgaMetaRole>& theRole, bool dispName) 
00725 {
00726         CComPtr<IMgaMetaAspect> aspPt;
00727         try {
00728                 mgaMeta.QueryInterface(&aspPt);
00729 
00730                 CComPtr<IMgaMetaParts> mmParts;
00731                 CComPtr<IMgaMetaPart> mmPart;
00732                 COMTHROW(aspPt->get_Parts(&mmParts));
00733 
00734                 MGACOLL_ITERATE(IMgaMetaPart,mmParts) {
00735                         mmPart = MGACOLL_ITER;
00736                         CComPtr<IMgaMetaRole> mmRole;
00737                         COMTHROW(mmPart->get_Role(&mmRole));
00738                         CComBSTR bstr;
00739                         if (dispName) {
00740                                 COMTHROW(mmRole->get_DisplayedName(&bstr));
00741                         }
00742                         else {
00743                                 COMTHROW(mmRole->get_Name(&bstr));
00744                         }
00745                         CString nm;
00746                         CopyTo(bstr,nm);
00747                         if (nm == roleName) {
00748                                 theRole = mmRole;
00749                                 return true;
00750                         }
00751                 }
00752                 MGACOLL_ITERATE_END;
00753         }
00754         catch(hresult_exception &) {
00755         }
00756         return false;
00757 }
00758 
00759 bool CGuiMetaAspect::GetPartByRole(CComPtr<IMgaMetaRole>& theRole, CComPtr<IMgaMetaPart>& thePart) 
00760 {
00761         bool res = false;
00762         CComPtr<IMgaMetaAspect> aspPt;
00763         try {
00764                 mgaMeta.QueryInterface(&aspPt);
00765 
00766                 CComPtr<IMgaMetaParts> mmParts;
00767                 CComPtr<IMgaMetaPart> mmPart;
00768                 COMTHROW(aspPt->get_Parts(&mmParts));
00769 
00770                 MGACOLL_ITERATE(IMgaMetaPart,mmParts) {
00771                         mmPart = MGACOLL_ITER;
00772                         CComPtr<IMgaMetaRole> mmRole;
00773                         COMTHROW(mmPart->get_Role(&mmRole));
00774                         if (!res && mmRole == theRole) {
00775                                 thePart = mmPart;
00776                                 res = true; // we will not return any more from the middle of an iteration
00777                         }
00778                 }
00779                 MGACOLL_ITERATE_END;
00780         }
00781         catch(hresult_exception &) {
00782         }
00783         return res;
00784 }
00785 
00786 bool CGuiMetaAspect::IsLinkedRole(CComPtr<IMgaMetaRole>& theRole)
00787 {
00788         CComPtr<IMgaMetaParts> mmParts;
00789         COMTHROW(theRole->get_Parts(&mmParts));
00790         MGACOLL_ITERATE(IMgaMetaPart,mmParts) {
00791                 CComPtr<IMgaMetaPart> mmPart;
00792                 mmPart = MGACOLL_ITER;
00793                 VARIANT_BOOL linked;
00794                 COMTHROW(mmPart->get_IsLinked(&linked));
00795                 if (linked)
00796                         return true;
00797         }
00798         MGACOLL_ITERATE_END;
00799         return false;
00800 }
00801 
00802 bool CGuiMetaAspect::IsPrimary(CComPtr<IMgaFCO> fco)
00803 {
00804         VARIANT_BOOL prim = VARIANT_FALSE;
00805         CComPtr<IMgaMetaRole> role;
00806         COMTHROW(fco->get_MetaRole(&role));
00807         CComPtr<IMgaMetaPart> part;
00808         if (GetPartByRole(role, part))
00809                 COMTHROW(part->get_IsPrimary(&prim));
00810         return prim != VARIANT_FALSE;
00811 }
00812 
00813 bool CGuiMetaAspect::IsPrimaryByRoleName(CComPtr<IMgaFCO> fco)
00814 {
00815 //      VARIANT_BOOL prim = VARIANT_FALSE;
00816         CComPtr<IMgaMetaRole> role;
00817         COMTHROW(fco->get_MetaRole(&role));
00818         return role == 0 ? false : IsPrimaryByRoleName(role);
00819 }
00820 
00821 bool CGuiMetaAspect::IsPrimaryByRoleName(CComPtr<IMgaMetaRole> role)
00822 {
00823         VARIANT_BOOL prim = VARIANT_FALSE;
00824         CComBSTR bstr;
00825         COMTHROW(role->get_Name(&bstr));
00826         CString roleName;
00827         CopyTo(bstr,roleName);
00828         CComPtr<IMgaMetaPart> part;
00829         CComPtr<IMgaMetaRole> newRole;
00830         if (GetRoleByName(roleName, newRole) && GetPartByRole(newRole, part))
00831                 COMTHROW(part->get_IsPrimary(&prim));
00832         return prim != VARIANT_FALSE;
00833 }
00834 
00835 bool CGuiMetaAspect::CheckFcosBeforeInsertion(CComPtr<IMgaFCOs> fcos)
00836 {
00837         bool ok = true;
00838         MGACOLL_ITERATE(IMgaFCO,fcos) {
00839                 CComPtr<IMgaFCO> fco = MGACOLL_ITER;
00840                 if (!IsPrimary(fco) && !IsPrimaryByRoleName(fco)) {
00841                         ok = false;
00842                         break;
00843                 }
00844         }
00845         MGACOLL_ITERATE_END;
00846         return ok;
00847 }
00848