GME  13
CompDlg.cpp
Go to the documentation of this file.
00001 // CompDlg.cpp : implementation file
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "MgaUtil.h"
00006 #include "CompDlg.h"
00007 #include "CompInfoDlg.h"
00008 #include "UACUtils.h"
00009 #include "Resource.h"
00010 
00011 #include <atlbase.h>
00012 #include <objbase.h>
00013 #include <comdef.h>
00014 
00015 
00016 #ifdef _DEBUG
00017 #define new DEBUG_NEW
00018 #undef THIS_FILE
00019 static char THIS_FILE[] = __FILE__;
00020 #endif
00021 
00023 // CCompDlg dialog
00024 
00025 
00026 CCompDlg::CCompDlg(CWnd* pParent /*=NULL*/)
00027         : CDialog(CCompDlg::IDD, pParent)
00028 {
00029         //{{AFX_DATA_INIT(CCompDlg)
00030         m_accessmode = 1;
00031         m_dispmode = 1;
00032         //}}AFX_DATA_INIT
00033 
00034         type = COMPONENTTYPE_ALL;
00035         onOKoper = _T("Close");
00036 
00037         firstResize = true;
00038 }
00039 
00040 
00041 void CCompDlg::DoDataExchange(CDataExchange* pDX)
00042 {
00043         CDialog::DoDataExchange(pDX);
00044         //{{AFX_DATA_MAP(CCompDlg)
00045         DDX_Control(pDX, IDC_ENABLE_DISABLE, m_enable_disable);
00046         DDX_Control(pDX, IDC_REMOVE, m_remove);
00047         DDX_Control(pDX, IDC_INSTALL, m_install);
00048         DDX_Control(pDX, IDC_LIST, m_list);
00049         DDX_Radio(pDX, IDC_RADIOSYS, m_accessmode);
00050         DDX_Radio(pDX, IDC_ACTIVEDISP, m_dispmode);
00051         //}}AFX_DATA_MAP
00052 }
00053 
00054 
00055 BEGIN_MESSAGE_MAP(CCompDlg, CDialog)
00056         //{{AFX_MSG_MAP(CCompDlg)
00057         ON_BN_CLICKED(IDC_REMOVE, OnRemove)
00058         ON_BN_CLICKED(IDC_INSTALL, OnInstall)
00059         ON_BN_CLICKED(IDC_ACTIVEDISP, OnActivedisp)
00060         ON_BN_CLICKED(IDC_ACTIVE_INACTIVE, OnActiveInactive)
00061         ON_BN_CLICKED(IDC_ALLCOMPS, OnAllcomps)
00062         ON_BN_CLICKED(IDC_ENABLE_DISABLE, OnEnableDisable)
00063         //}}AFX_MSG_MAP
00064         ON_WM_SIZE()
00065         ON_BN_CLICKED(IDC_RADIOSYS, &CCompDlg::OnBnClickedRadiosys)
00066         ON_BN_CLICKED(IDC_RADIOUSER, &CCompDlg::OnBnClickedRadiouser)
00067         ON_BN_CLICKED(IDC_RADIOBOTH, &CCompDlg::OnBnClickedRadioboth)
00068         ON_NOTIFY(HDN_ITEMCLICK, 0, OnAllCompsHeader)
00069 END_MESSAGE_MAP()
00070 
00071 
00072 struct SortParam {
00073         CCompDlg* dlg;
00074         int columnIndex;
00075 };
00076 
00077 int CALLBACK CCompDlg::SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) {
00078         SortParam& param = *(SortParam*)lParamSort;
00079         
00080         CString a = param.dlg->m_list.GetItemText(lParam1, param.columnIndex);
00081         CString b = param.dlg->m_list.GetItemText(lParam2, param.columnIndex);
00082 
00083         return _tcscmp(a, b);
00084 }
00085 
00086 
00087 void CCompDlg::OnAllCompsHeader(NMHDR* pNMHDR, LRESULT* pResult) 
00088 {
00089         NMLISTVIEW *pLV = (NMLISTVIEW *) pNMHDR;
00090         SortParam s = { this, pLV->iItem };
00091         m_list.SortItemsEx(SortFunc, (DWORD)(void*)&s);
00092         
00093         *pResult = 0;
00094 }
00095 
00096 
00098 // CCompDlg message handlers
00099 
00100 BOOL CCompDlg::OnInitDialog() 
00101 {
00102         CDialog::OnInitDialog();
00103 
00104         MSGTRY
00105         {
00106                 if( !CUACUtils::isVistaOrLater() ) {
00107                         CRegKey accessTest;
00108                         if (accessTest.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\GME"), KEY_READ | KEY_WRITE) == ERROR_ACCESS_DENIED) {
00109                                 GetDlgItem(IDC_RADIOSYS)->EnableWindow(false);
00110                                 GetDlgItem(IDC_RADIOBOTH)->EnableWindow(false);
00111                         }
00112                 }
00113                 
00114 
00115                 SetDlgItemText(IDOK, onOKoper);
00116                 
00117 
00118                 LV_COLUMN lvc;
00119                 lvc.mask = LVCF_WIDTH | LVCF_TEXT;
00120 
00121                 lvc.pszText = _T("Name");
00122                 lvc.cx = 150;
00123                 VERIFYTHROW( m_list.InsertColumn(0, &lvc) != -1 );
00124 
00125                 lvc.pszText = _T("Type");
00126                 lvc.cx = 80;
00127                 VERIFYTHROW( m_list.InsertColumn(1, &lvc) != -1 );
00128 
00129                 lvc.pszText = _T("ProgID");
00130                 lvc.cx = 200;
00131                 VERIFYTHROW( m_list.InsertColumn(2, &lvc) != -1 );
00132 
00133                 lvc.pszText = _T("Path");
00134                 lvc.cx = 300;
00135                 VERIFYTHROW( m_list.InsertColumn(3, &lvc) != -1 );
00136 
00137                 iconlist.Create(IDB_BITMAP1,16,0,15);
00138                 m_list.SetImageList(&iconlist,LVSIL_SMALL);
00139                 if(parameter.vt == VT_BSTR) {
00140                         sticonlist.Create(IDB_BITMAP2,10,0,15);
00141                         m_list.SetImageList(&sticonlist,LVSIL_STATE);
00142                 }
00143                 UpdateData(false);
00144                 ResetItems();
00145 
00146                 RefreshShieldIcons();
00147                 
00148         }
00149         MSGCATCH(_T("Error while initializing CompDlg"),;)
00150 
00151         return TRUE;  // return TRUE unless you set the focus to a control
00152                       // EXCEPTION: OCX Property Pages should return FALSE
00153 }
00154 
00155 void CCompDlg::RefreshShieldIcons()
00156 {
00157         if( CUACUtils::isVistaOrLater() ) {
00158                 UpdateData(TRUE);
00159                 bool shieldOn = regacc_translate(m_accessmode) != REGACCESS_USER;
00160                 CUACUtils::SetShieldIcon(m_install, true);
00161                 CUACUtils::SetShieldIcon(m_remove, true);
00162                 CUACUtils::SetShieldIcon(m_enable_disable, shieldOn);
00163         }
00164 }
00165 
00166 void CCompDlg::ResetItems()
00167 {
00168         CComPtr<IMgaRegistrar> registrar;
00169         registrar.CoCreateInstance(OLESTR("Mga.MgaRegistrar"));
00170         ASSERT( registrar != NULL );
00171 
00172         m_remove.EnableWindow(false);
00173         m_enable_disable.EnableWindow(false);
00174 
00175         UpdateData();
00176         VERIFYTHROW( m_list.DeleteAllItems() != 0 );
00177 
00178         CComVariant v;
00179         if(m_dispmode == 0) {
00180                 COMTHROW( registrar->get_AssociatedComponents(PutInBstr(paradigm), type, REGACCESS_BOTH, PutOut(v)) );
00181         }
00182         else {
00183                 COMTHROW( registrar->get_Components(REGACCESS_BOTH, PutOut(v)) );
00184         }
00185 
00186         CComBstrObjArray progids;
00187         MoveTo(v, progids);
00188 
00189 
00190 
00191         for(int i = 0; i < progids.GetSize(); ++i)
00192         {
00193                 componenttype_enum qtype;
00194                 CComBstrObj desc;
00195                 CComBstrObj localDllDispPath;
00196                 CString localDllDispPathStr;
00197 
00198                 bool err_ass = false;
00199                 VARIANT_BOOL is_ass, can_ass;
00200                 HRESULT hr = registrar->QueryComponent(progids[i], &qtype, PutOut(desc), REGACCESS_PRIORITY );
00201                 if(hr != S_OK) {
00202                         err_ass = true;
00203                         desc = CComBstrObj(L"???");
00204                         localDllDispPath = CComBstrObj(L"???");
00205                         qtype = COMPONENTTYPE_NONE;
00206                 }
00207                 else {
00208                         if(! (qtype & type) ) continue;
00209                         if(m_dispmode) {
00210                                 HRESULT hr = registrar->IsAssociated(progids[i], PutInBstr(paradigm), &is_ass, &can_ass, REGACCESS_PRIORITY );
00211                                 if(hr == E_NOTFOUND) {
00212                                         err_ass = true;
00213                                 }
00214                                 else COMTHROW(hr);
00215                                 if(m_dispmode == 1 && (is_ass == VARIANT_FALSE && can_ass == VARIANT_FALSE)) continue;
00216                         }
00217                         else is_ass = can_ass = VARIANT_TRUE;
00218 
00219                         HRESULT hr = registrar->GetLocalDllPathDisp(progids[i], PutOut(localDllDispPath));
00220                         if (SUCCEEDED(hr))
00221                                 localDllDispPathStr = (const TCHAR*)PutInCString(localDllDispPath);
00222 /*#define BUFSIZE 1024
00223                         TCHAR buffer[BUFSIZE]=TEXT("");
00224                         DWORD retval = GetFullPathName(localDllDispPathStr, BUFSIZE, buffer, NULL);
00225                         if (retval != 0)
00226                         {
00227                                 localDllDispPathStr = buffer;
00228                         } TODO: inspect this later*/
00229                 }
00230 
00231                 int index;
00232                 VERIFYTHROW( (index = m_list.InsertItem(i, PutInCString(desc), err_ass ? 3 : is_ass ? 0 : can_ass ? 1 : 2)) != -1 );
00233 
00234 
00235                 CString ctype;
00236                 switch(qtype & COMPONENTTYPE_ALL)
00237                 {
00238                 case COMPONENTTYPE_INTERPRETER:
00239                         ctype = _T("Interpreter");
00240                         break;
00241 
00242                 case COMPONENTTYPE_ADDON:
00243                         ctype = _T("Add-on");
00244 
00245                         if(parameter.vt == VT_BSTR) {
00246                                 CComBSTR l = parameter.bstrVal;
00247                                 WCHAR * f = wcstok(l,L" ");
00248                                 while(f) {
00249                                         if(!desc.Compare(CComBSTR(f)) || !progids[i].Compare(CComBSTR(f))) {
00250                                                 // FIXME: this component is running, would be nice to show to the user, but an exclamation mark means nothing
00251                                                 // m_list.SetItemState(index, INDEXTOSTATEIMAGEMASK(2), LVIS_STATEIMAGEMASK);
00252                                                 break;
00253                                         }
00254                                         f = wcstok(NULL, L" ");
00255                                 }
00256                         }
00257                         break;
00258 
00259                 case COMPONENTTYPE_PLUGIN:
00260                         ctype = _T("Plug-in");
00261                         break;
00262 
00263                 default:
00264                         ctype = _T("???");
00265                 }
00266 
00267                 VERIFYTHROW( m_list.SetItemText(index, 1, ctype) != 0 );
00268                 VERIFYTHROW( m_list.SetItemText(index, 2, PutInCString(progids[i])) != 0 );
00269                 VERIFYTHROW( m_list.SetItemText(index, 3, localDllDispPathStr) != 0 );
00270                 if(!desc.Compare( PutInBstr(to_select))) {
00271                         m_list.SetItemState(index, LVIS_SELECTED, LVIS_SELECTED);
00272                 }
00273         }
00274         UpdateEnableDisable();
00275 }
00276 
00277 void CCompDlg::UpdateEnableDisable() {
00278         POSITION pos = m_list.GetFirstSelectedItemPosition();
00279         if (pos != NULL) {
00280                 int i = m_list.GetNextSelectedItem(pos);
00281                 CString progid = m_list.GetItemText(i, 2);
00282                 CComPtr<IMgaRegistrar> registrar;
00283                 registrar.CoCreateInstance(L"Mga.MgaRegistrar");
00284                 ASSERT(registrar != NULL);
00285                 VARIANT_BOOL is_ass, can_ass;
00286                 HRESULT hr = registrar->IsAssociated(PutInBstr(progid), PutInBstr(paradigm), &is_ass, &can_ass, REGACCESS_PRIORITY);
00287                 ASSERT(SUCCEEDED(hr));
00288                 if (SUCCEEDED(hr) && is_ass == VARIANT_TRUE) {
00289                         m_enable_disable.SetWindowText(_T("Disable"));
00290                         return;
00291                 }
00292         }
00293         m_enable_disable.SetWindowText(_T("Enable"));
00294 }
00295 
00296 void CCompDlg::OnOK() 
00297 {
00298         POSITION pos = m_list.GetFirstSelectedItemPosition();
00299         if( pos != NULL )
00300         {
00301                 int i = m_list.GetNextSelectedItem(pos);
00302                 progid = m_list.GetItemText(i, 2);
00303         }
00304         else
00305         {
00306                 progid.Empty();
00307         }
00308 
00309         CDialog::OnOK();
00310 }
00311 
00312 BOOL CCompDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
00313 {
00314         if( wParam == IDC_LIST && ((NMHDR*)lParam)->code == LVN_ITEMCHANGED &&
00315                 ((NMLISTVIEW*)lParam)->uNewState == (LVIS_FOCUSED|LVIS_SELECTED))
00316         {
00317                 POSITION pos = m_list.GetFirstSelectedItemPosition();
00318                 m_remove.EnableWindow(pos != NULL);
00319                 m_enable_disable.EnableWindow(pos != NULL);
00320                 to_select = m_list.GetItemText(m_list.GetNextSelectedItem(pos),0);
00321                 UpdateEnableDisable();
00322                 return TRUE;
00323         } else if (wParam == IDC_LIST && ((NMHDR*)lParam)->code == NM_DBLCLK )
00324         {
00325                 if (onOKoper == _T("Interpret..."))
00326                 {
00327                         OnOK();
00328                         return TRUE;
00329                 } else {
00330                         OnEnableDisable();
00331                         return TRUE;
00332                 }
00333         }
00334         
00335         return CDialog::OnNotify(wParam, lParam, pResult);
00336 }
00337 
00338 void CCompDlg::OnRemove() 
00339 {
00340         UpdateData();
00341         MSGTRY
00342         {
00343                 POSITION pos = m_list.GetFirstSelectedItemPosition();
00344                 if( pos != NULL )
00345                 {
00346                         CString progid = m_list.GetItemText(m_list.GetNextSelectedItem(pos), 2);
00347 
00348                         CComPtr<IMgaRegistrar> registrar;
00349                         HRESULT registrarHr;
00350                         if (CUACUtils::isVistaOrLater()) {
00351                                 registrarHr = GetElevatedRegistrar(&registrar);
00352                         }
00353                         else {
00354                                 registrarHr = registrar.CoCreateInstance(OLESTR("Mga.MgaRegistrar"));
00355                         }
00356 
00357                         if (!registrar) {
00358                                 DisplayError(_T("Unable to remove component"), registrarHr);
00359                                 return;
00360                         }
00361 
00362                         COMTHROW( registrar->UnregisterComponent(PutInBstr(progid), regacc_translate(m_accessmode)) );
00363 
00364                         componenttype_enum type;
00365 
00366                         CComBstrObj desc;
00367                         switch(regacc_translate(m_accessmode)) {
00368                                 case REGACCESS_USER:
00369                                         if (S_OK == registrar->QueryComponent(PutInBstr(progid), &type, PutOut(desc), REGACCESS_SYSTEM)) {
00370                                                 AfxMessageBox(_T("Warning: Component is still present in system registry"));
00371                                         }
00372                                         break;
00373                                 case REGACCESS_SYSTEM:
00374                                         if (S_OK == registrar->QueryComponent(PutInBstr(progid), &type, PutOut(desc), REGACCESS_USER)) {
00375                                                 AfxMessageBox(_T("Warning: Component is still present in user registry"));
00376                                         }
00377                                         break;
00378                         }
00379                         ResetItems();
00380                 }
00381         }
00382         MSGCATCH(_T("Error while removing component"),;)
00383 }
00384 
00385 
00386 void CCompDlg::OnEnableDisable() 
00387 {
00388         UpdateData();
00389         MSGTRY
00390         {
00391                 POSITION pos = m_list.GetFirstSelectedItemPosition();
00392                 if( pos != NULL ) {
00393                         CComBSTR progid = m_list.GetItemText(m_list.GetNextSelectedItem(pos), 2);
00394 
00395                         VARIANT_BOOL is_ass, can_ass;
00396                         
00397                         CComPtr<IMgaRegistrar> registrar;
00398                         HRESULT registrarHr;
00399                         if (CUACUtils::isVistaOrLater() && (regacc_translate(m_accessmode) != REGACCESS_USER) ) {
00400                                 registrarHr = GetElevatedRegistrar(&registrar);
00401                         }
00402                         else {
00403                                 registrarHr = registrar.CoCreateInstance(OLESTR("Mga.MgaRegistrar"));
00404                         }
00405 
00406                         if (!registrar) {
00407                                 DisplayError(_T("Unable to enable/disable component"), registrarHr);
00408                                 return;
00409                         }
00410 
00411                         HRESULT hr = registrar->IsAssociated(progid, PutInBstr(paradigm), &is_ass, &can_ass, REGACCESS_PRIORITY);
00412                         if (hr != S_OK)
00413                                 DisplayError(_T("Cannot enable/disable this component"), hr);
00414                         CString enable_or_disable;
00415                         if (is_ass) {
00416                                 enable_or_disable = _T("disable");
00417                                 hr = registrar->Disassociate(progid, PutInBstr(paradigm), regacc_translate(m_accessmode));
00418                         } else {
00419                                 enable_or_disable = _T("enable");
00420                                 if(!can_ass) {
00421                                         if(AfxMessageBox(_T("This component reports to be incompatible with the paradigm\nAre you sure you want to proceed?"), MB_YESNO) != IDYES) return;
00422                                 }
00423                                 hr = (registrar->Associate(progid, PutInBstr(paradigm), regacc_translate(m_accessmode)) );
00424                         }
00425                         if(hr != S_OK) {
00426                                 DisplayError(_T("The ") + enable_or_disable + _T(" operation failed"), hr);
00427                         }
00428 
00429                         ResetItems();
00430                 }
00431         }
00432         MSGCATCH(_T("Error while (dis)associating component"),;)
00433 }
00434 
00435 static TCHAR filter[] = 
00436         _T("Component Files (*.dll; *.ocx)|*.dll; *.ocx|")
00437         _T("Pattern Files (*.pat)|*.pat|")
00438         _T("All Files (*.*)|*.*||");
00439 
00440 void CCompDlg::OnInstall() 
00441 {
00442         UpdateData();
00443         MSGTRY
00444         {
00445                 CFileDialog dlg(true, NULL, NULL, 
00446                         OFN_EXPLORER | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
00447                         filter);
00448 
00449                 if( dlg.DoModal() != IDOK )
00450                         return;
00451 
00452                 CString ext = dlg.GetFileExt();
00453 
00454                 if(ext.CompareNoCase(_T("DLL")) == 0) {
00455                                 RegisterDll(dlg.GetPathName());
00456                 }
00457                 else if(ext.CompareNoCase(_T("PAT")) == 0){
00458                                 RegisterPattern(dlg.GetPathName());
00459                 }
00460                 else {
00461                                 RegisterUnknownFile(dlg.GetPathName());
00462                 }
00463                 ResetItems();
00464         }
00465         MSGCATCH(_T("Error while installing component"),;)
00466 }
00467 
00468 void CCompDlg::RegisterDll(const CString &path)
00469 {
00470         CComPtr<IMgaRegistrar> registrar;
00471         HRESULT hr;
00472         if (CUACUtils::isVistaOrLater()) {
00473                 hr = GetElevatedRegistrar(&registrar);
00474         } else {
00475                 hr = registrar.CoCreateInstance(OLESTR("Mga.MgaRegistrar"));
00476         }
00477 
00478         if (!registrar) {
00479                 DisplayError(_T("Unable to create component registrar"), hr);
00480                 return;
00481         }
00482 
00483 
00484         MSGTRY
00485         {
00486                 hr = registrar->__RegisterComponentLibrary(_bstr_t(path), regacc_translate(m_accessmode));
00487         }
00488         MSGCATCH(_T("Unable to register component"),;)
00489 }
00490 
00491 HRESULT CCompDlg::GetElevatedRegistrar(IMgaRegistrar** registrar)
00492 {
00493         HRESULT hr = S_OK;
00494         if (m_elevatedRegistrar == NULL)
00495                 hr = CUACUtils::CreateElevatedInstance(__uuidof(MgaRegistrar), &m_elevatedRegistrar);
00496         if (m_elevatedRegistrar != NULL)
00497                 m_elevatedRegistrar.CopyTo(registrar);
00498         return hr;
00499 }
00500 
00501 
00502 void CCompDlg::RegisterPattern(const CString &path)
00503 {
00504 
00505         // FIXME: should use TCHAR
00506         char buf[300];
00507         {
00508                 // FIXME: will this read a UTF-16 file?
00509                 std::ifstream fin(path, std::ios::in);//z! ios::nocreate used previously, but if opened for read it won't create in this way
00510                 if(fin.good()) fin.getline(buf, 300);
00511                 if(!fin.good()) {
00512                         AfxMessageBox(_T("Could not open or read the specified file: ") + path);
00513                         return;
00514                 }
00515         }
00516 
00517         regaccessmode_enum acmode = regacc_translate(m_accessmode);
00518         CComPtr<IMgaRegistrar> registrar;
00519         HRESULT registrarHr;
00520         if (CUACUtils::isVistaOrLater() && (acmode != REGACCESS_USER) ) {
00521                 registrarHr = GetElevatedRegistrar(&registrar);
00522         }
00523         else {
00524                 registrarHr = registrar.CoCreateInstance(OLESTR("Mga.MgaRegistrar"));
00525         }
00526 
00527         if (!registrar) {
00528                 DisplayError(_T("Unable to register pattern component"), registrarHr);
00529                 return;
00530         }
00531 
00532         CCompInfoDlg dlg(registrar);
00533         dlg.m_filename = path;
00534 
00535         TCHAR paren, info[200] = {0};
00536         if (swscanf_s(CString(buf), _T(" $!COMMENT( %[^)]%lc"), info, (unsigned)(sizeof(info)/sizeof(info[0]))-1, &paren, 1) != 2 || paren != ')') {
00537 nothing_understood:
00538                 AfxMessageBox("Cannot read component info in file " + path);
00539         }
00540         else {
00541         
00542         
00543 // NAME=nnn, DESCRIPTION=ddd, PROGID=ii, PARADIGM=xx,yy, VERSION=vvv,
00544                 TCHAR *p = _tcstok(info, _T(","));
00545                 bool ready = false;
00546                 bool parsing_paradigm = false;
00547                 bool good = false;
00548                 while(p) {
00549                         CString item(p);
00550                         item.TrimLeft();
00551                         item.TrimRight();
00552                         int k1 = item.Find(';');
00553                         if(ready && !item.IsEmpty()) goto item_err;
00554                         if(k1 > 0) {
00555                                 ready = true;
00556                                 item = item.Left(k1);
00557                                 item.TrimRight();
00558                         }
00559                         k1 = item.Find('=');
00560                         if(k1 > 0) {
00561                                 parsing_paradigm = false;
00562                                 CString i_name = item.Left(k1);
00563                                 i_name.TrimRight();
00564                                 CString i_value = item.Mid(k1+1);
00565                                 i_value.TrimLeft();
00566                                 if(i_name.Compare(_T("NAME")) == 0) {
00567                                         dlg.m_name = i_value;
00568                                 }
00569                                 else if(i_name.Compare(_T("DESCRIPTION")) == 0 ||
00570                                         i_name.Compare(_T("DESC")) == 0) {
00571                                         dlg.m_description = i_value;
00572                                 }
00573                                 else if(i_name.Compare(_T("PROGID")) == 0) {
00574                                         dlg.m_progid = i_value;
00575                                 }
00576                                 else if(i_name.Compare(_T("PARADIGM")) == 0) {
00577                                         dlg.m_paradigm = i_value;
00578                                         parsing_paradigm  = true;
00579                                 }
00580                                 else if(i_name.Compare(_T("VERSION")) == 0) {
00581                                         dlg.m_version = i_value;
00582                                 }
00583                                 else goto item_err;
00584                                 good = true;
00585                         }
00586                         else {
00587                                 if(parsing_paradigm) {
00588                                         dlg.m_paradigm += _T(",") + item;
00589                                 }
00590                                 else {
00591         item_err:
00592                                         AfxMessageBox(_T("Syntax error parsing component info: ") + item); 
00593                                 }
00594                         }
00595                         p = _tcstok(NULL, _T(","));
00596                 }
00597                 if(!good) goto nothing_understood;
00598         }
00599 
00600         if(dlg.m_progid.IsEmpty() && !dlg.m_name.IsEmpty()) {
00601                 dlg.m_progid = _T("MGA.Pattern.") + dlg.m_name;
00602         }
00603         if(dlg.m_description.IsEmpty() && !dlg.m_name.IsEmpty()) {
00604                 dlg.m_description = dlg.m_name;
00605         }
00606         if(dlg.m_version.IsEmpty()) {
00607                 dlg.m_version = _T("1.0");
00608         }
00609 
00610         if(dlg.DoModal() != IDOK) return;
00611 
00612 
00613         CComBSTR progid(dlg.m_progid);
00614 
00615         
00616         COMTHROW(registrar->RegisterComponent(progid, 
00617                                 (componenttype_enum)(COMPONENTTYPE_INTERPRETER|COMPONENTTYPE_SCRIPT),
00618                                 PutInBstr(dlg.m_description), acmode));
00619         COMTHROW(registrar->put_ComponentExtraInfo(acmode, progid, CComBSTR(L"Name"), CComBSTR(dlg.m_name)));
00620         COMTHROW(registrar->put_ComponentExtraInfo(acmode, progid, CComBSTR(L"ExecEngine"), CComBSTR(dlg.engine)));
00621         COMTHROW(registrar->put_ComponentExtraInfo(acmode, progid, CComBSTR(L"ScriptFile"), CComBSTR(path)));
00622         COMTHROW(registrar->put_ComponentExtraInfo(acmode, progid, CComBSTR(L"ScriptVersion"), CComBSTR(dlg.m_version)));
00623 
00624         std::unique_ptr<TCHAR[]> mpardup(new TCHAR[dlg.m_paradigm.GetLength()+1]);
00625         _tcscpy(mpardup.get(), dlg.m_paradigm);
00626         const TCHAR *par = _tcstok(mpardup.get(), _T("\t ,"));
00627         while(par) {
00628                 COMTHROW(registrar->Associate(progid, CComBSTR(par), acmode));
00629                 par = _tcstok(NULL, _T("\t ,"));
00630         }
00631 }
00632 
00633 void CCompDlg::RegisterUnknownFile(const CString &path)
00634 {
00635 
00636 }
00637 
00638 void CCompDlg::OnActivedisp() 
00639 {
00640         ResetItems();
00641 }
00642 
00643 void CCompDlg::OnActiveInactive() 
00644 {
00645         ResetItems();
00646 }
00647 
00648 void CCompDlg::OnAllcomps() 
00649 {
00650         ResetItems();
00651 }
00652 
00653 
00654 void CCompDlg::OnSize(UINT nType, int cx, int cy)
00655 {
00656         long deltaWidth = 0;
00657         long deltaHeight = 0;
00658 
00659         if (firstResize) {
00660                 CRect rectInResource = GetWindowSizeFromResource();
00661                 CRect currentDlgRect;
00662                 deltaWidth = cx - rectInResource.Width();
00663                 deltaHeight = cy - rectInResource.Height();
00664                 firstResize = false;
00665         } else {
00666                 deltaWidth = cx - lastRect.Width();
00667                 deltaHeight = cy - lastRect.Height();
00668         }
00669 
00670         CDialog::OnSize(nType, cx, cy);
00671         GetClientRect(&lastRect);
00672 
00673         HDWP dwp = NULL;
00674         bool defer = (deltaHeight != 0);
00675         if (defer)
00676                 dwp = ::BeginDeferWindowPos(13);
00677 
00678         MoveControl(IDC_LIST,                                   0, 0, deltaWidth, deltaHeight, defer, &dwp);
00679         if (defer) {
00680                 MoveControl(IDC_REMOVE,                                 0, deltaHeight, 0, 0, defer, &dwp);
00681                 MoveControl(IDC_INSTALL,                                0, deltaHeight, 0, 0, defer, &dwp);
00682         MoveControl(IDC_ENABLE_DISABLE,                 0, deltaHeight, 0, 0, defer, &dwp);
00683                 MoveControl(IDC_RADIOSYS,                               0, deltaHeight, 0, 0, defer, &dwp);
00684                 MoveControl(IDC_RADIOUSER,                              0, deltaHeight, 0, 0, defer, &dwp);
00685                 MoveControl(IDC_RADIOBOTH,                              0, deltaHeight, 0, 0, defer, &dwp);
00686                 MoveControl(IDC_REGISTERSTATIC,                 0, deltaHeight, 0, 0, defer, &dwp);
00687                 MoveControl(IDOK,                                               0, deltaHeight, 0, 0, defer, &dwp);
00688         }
00689 
00690         if (defer) {
00691                 ASSERT(dwp != NULL);
00692                 if (dwp != NULL) {
00693                         BOOL success = ::EndDeferWindowPos(dwp);
00694                         ASSERT(success == TRUE);
00695                 }
00696         }
00697 }
00698 
00699 void CCompDlg::OnBnClickedRadiosys()
00700 {
00701         RefreshShieldIcons();
00702 }
00703 
00704 void CCompDlg::OnBnClickedRadiouser()
00705 {
00706         RefreshShieldIcons();
00707 }
00708 
00709 void CCompDlg::OnBnClickedRadioboth()
00710 {
00711         RefreshShieldIcons();
00712 }
00713 
00714 // CodeGuru: Finding Display Size of Dialog From Resource
00715 //              by Shridhar Guravannavar
00716 CRect CCompDlg::GetWindowSizeFromResource(void) const
00717 {
00718         CRect rectSize;
00719 
00720         // if the dialog resource resides in a DLL ...
00721         //
00722 
00723         HINSTANCE hInst = AfxFindResourceHandle(MAKEINTRESOURCE(IDD), RT_DIALOG);
00724 
00725         ASSERT(hInst != NULL);
00726 
00727         HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(IDD), RT_DIALOG);
00728         ASSERT(hRsrc != NULL);
00729 
00730         HGLOBAL hTemplate = ::LoadResource(hInst, hRsrc);
00731         ASSERT(hTemplate != NULL);
00732 
00733         DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)::LockResource(hTemplate);
00734 
00735         //Load coresponding DLGINIT resource
00736         //(if we have any ActiveX components)
00737         //
00738         void* lpDlgInit;
00739         HGLOBAL hDlgInit = NULL;
00740         CDialog dlg;
00741 
00742         HRSRC hsDlgInit = ::FindResource(hInst, MAKEINTRESOURCE(IDD), RT_DLGINIT);
00743         if (hsDlgInit != NULL) {
00744                 // load it
00745                 hDlgInit = ::LoadResource(hInst, hsDlgInit);
00746                 ASSERT(hDlgInit != NULL);
00747 
00748                 // lock it
00749                 lpDlgInit = ::LockResource(hDlgInit);
00750                 ASSERT(lpDlgInit != NULL);
00751 
00752                 dlg.CreateIndirect(pTemplate, NULL, lpDlgInit);
00753         } else {
00754                 dlg.CreateIndirect(pTemplate, NULL);
00755         }
00756 
00757         CRect rect;
00758         dlg.GetClientRect(rectSize);
00759 
00760         dlg.DestroyWindow();
00761 
00762         ::UnlockResource(hTemplate);
00763         ::FreeResource(hTemplate);
00764         if (hDlgInit) {
00765                 ::UnlockResource(hDlgInit);
00766                 ::FreeResource(hDlgInit);
00767         }
00768 
00769         return rectSize;
00770 }
00771 
00772 void CCompDlg::MoveControl(int nID, int offsetX, int offsetY, int deltaWidth, int deltaHeight, bool defer, HDWP* pdwp)
00773 {
00774         CWnd* controlWnd = GetDlgItem(nID);
00775         if (controlWnd != NULL && (offsetX != 0 || offsetY != 0 || deltaWidth != 0 || deltaHeight != 0)) {
00776                 CRect controlClientRect;
00777                 controlWnd->GetClientRect(&controlClientRect);
00778                 CRect controlWindowRect;
00779                 controlWnd->GetWindowRect(&controlWindowRect);
00780                 ScreenToClient(&controlWindowRect);
00781 
00782                 if (defer) {
00783                         ASSERT(pdwp != NULL);
00784                         if (*pdwp != NULL)
00785                                 *pdwp = ::DeferWindowPos(*pdwp, controlWnd->m_hWnd, NULL,
00786                                                                                  controlWindowRect.left + offsetX, controlWindowRect.top + offsetY,
00787                                                                                  controlWindowRect.Width() + deltaWidth, controlWindowRect.Height() + deltaHeight,
00788                                                                                  SWP_NOZORDER /*| SWP_NOCOPYBITS*/);
00789                 } else {
00790                         controlWnd->SetWindowPos(NULL, controlWindowRect.left + offsetX, controlWindowRect.top + offsetY,
00791                                                                          controlWindowRect.Width() + deltaWidth, controlWindowRect.Height() + deltaHeight,
00792                                                                          SWP_NOZORDER);
00793                 }
00794         }
00795 }