GME  13
RegistryBrowserDlg.cpp
Go to the documentation of this file.
00001 // RegistryBrowserDlg.cpp : implementation file
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "mgautil.h"
00006 #include "RegistryBrowserDlg.h"
00007 
00008 EXTERN_C IMAGE_DOS_HEADER __ImageBase;
00009 #define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
00010 
00011 #ifdef _DEBUG
00012 #define new DEBUG_NEW
00013 #undef THIS_FILE
00014 static char THIS_FILE[] = __FILE__;
00015 #endif
00016 
00018 // CRegistryBrowserDlg dialog
00019 
00020 
00021 CRegistryBrowserDlg::CRegistryBrowserDlg(const CComPtr<IMgaObject> &object, CWnd* pParent /*=NULL*/)
00022         : CDialog(CRegistryBrowserDlg::IDD, pParent)
00023 {
00024         //{{AFX_DATA_INIT(CRegistryBrowserDlg)
00025         m_objectKind = _T("");
00026         m_objectName = _T("");
00027         m_objectRole = _T("");
00028         m_regnodePath = _T("");
00029         m_regnodeStatus = _T("");
00030         m_regnodeValue = _T("");
00031         //}}AFX_DATA_INIT
00032         m_object = object;
00033 }
00034 
00035 CRegistryBrowserDlg::~CRegistryBrowserDlg()
00036 {
00037         POSITION pos = m_nodes.GetHeadPosition();
00038         while (pos) {
00039                 delete m_nodes.GetNext(pos);
00040         }
00041         m_nodes.RemoveAll();
00042 }
00043 
00044 
00045 void CRegistryBrowserDlg::DoDataExchange(CDataExchange* pDX)
00046 {
00047         CDialog::DoDataExchange(pDX);
00048         //{{AFX_DATA_MAP(CRegistryBrowserDlg)
00049         DDX_Control(pDX, IDC_REGNODE, m_wndRegNode);
00050         DDX_Control(pDX, IDC_REGISTRY_TREE, m_wndRegistryTree);
00051         DDX_Text(pDX, IDC_OBJECTKIND, m_objectKind);
00052         DDX_Text(pDX, IDC_OBJECTNAME, m_objectName);
00053         DDX_Text(pDX, IDC_OBJECTROLE, m_objectRole);
00054         DDX_Text(pDX, IDC_REGNODE_PATH, m_regnodePath);
00055         DDX_Text(pDX, IDC_REGNODE_STATUS, m_regnodeStatus);
00056         DDX_Text(pDX, IDC_REGNODE_VALUE, m_regnodeValue);
00057         //}}AFX_DATA_MAP
00058 }
00059 
00060 
00061 BEGIN_MESSAGE_MAP(CRegistryBrowserDlg, CDialog)
00062         //{{AFX_MSG_MAP(CRegistryBrowserDlg)
00063         ON_WM_SIZE()
00064         ON_EN_CHANGE(IDC_REGNODE_VALUE, OnChangeRegnodeValue)
00065         //}}AFX_MSG_MAP
00066 END_MESSAGE_MAP()
00067 
00068 
00070 // CRegistryBrowserDlg message handlers
00071 
00072 BOOL CRegistryBrowserDlg::OnInitDialog() 
00073 {
00074         CDialog::OnInitDialog();
00075         
00076         // TODO: Add extra initialization here
00077 
00078         CRect rect(0,0,0,0);
00079 
00080 
00081         m_wndSplitterBar.Create(WS_CHILD|WS_BORDER|WS_DLGFRAME|WS_VISIBLE, rect,this,999);
00082         m_wndSplitterBar.SetPanes(&m_wndRegistryTree,&m_wndRegNode);
00083 
00084 
00085         CWinApp *pApp = AfxGetApp();
00086         int cx = ::GetSystemMetrics(SM_CXSMICON);
00087         int cy = ::GetSystemMetrics(SM_CYSMICON);
00088         m_imageList.Create(cx, cy, ILC_COLOR4 | ILC_MASK, 5, 1);
00089         m_imageMap.InitHashTable(10);
00090         
00091         HICON hIcon;
00092 
00093         hIcon = (HICON)::LoadImage(HINST_THISCOMPONENT, 
00094                 MAKEINTRESOURCE(IDI_ICON_REGHERE), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
00095         ASSERT(hIcon);
00096         if (hIcon) {
00097                 m_imageMap[IDI_ICON_REGHERE] = m_imageList.Add(hIcon);
00098                 ::DeleteObject(hIcon);
00099         }
00100 
00101         hIcon = (HICON)::LoadImage(HINST_THISCOMPONENT, 
00102                 MAKEINTRESOURCE(IDI_ICON_REGINHERIT), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
00103         ASSERT(hIcon);
00104         if (hIcon) {
00105                 m_imageMap[IDI_ICON_REGINHERIT] = m_imageList.Add(hIcon);
00106                 ::DeleteObject(hIcon);
00107         }
00108 
00109         hIcon = (HICON)::LoadImage(HINST_THISCOMPONENT, 
00110                 MAKEINTRESOURCE(IDI_ICON_REGINVALID), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
00111         if (hIcon) {
00112                 m_imageMap[IDI_ICON_REGINVALID] = m_imageList.Add(hIcon);
00113                 ::DeleteObject(hIcon);
00114         }
00115 
00116         hIcon = (HICON)::LoadImage(HINST_THISCOMPONENT, 
00117                 MAKEINTRESOURCE(IDI_ICON_REGMETA), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
00118         if (hIcon) {
00119                 m_imageMap[IDI_ICON_REGMETA] = m_imageList.Add(hIcon);
00120                 ::DeleteObject(hIcon);
00121         }
00122         
00123         hIcon = (HICON)::LoadImage(HINST_THISCOMPONENT, 
00124                 MAKEINTRESOURCE(IDI_ICON_REGUNDEF), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
00125         if (hIcon) {
00126                 m_imageMap[IDI_ICON_REGUNDEF] = m_imageList.Add(hIcon);
00127                 ::DeleteObject(hIcon);
00128         }
00129 
00130         m_wndRegistryTree.SetImageList(&m_imageList,TVSIL_NORMAL);
00131 
00132         try {
00133                 CComBSTR bstr_tmp;
00134                 COMTHROW(m_object->get_Name(&bstr_tmp));
00135                 CopyTo(bstr_tmp, m_objectName);
00136 
00137                 CComPtr<IMgaMetaBase> metaBase;
00138                 COMTHROW(m_object->get_MetaBase(&metaBase));
00139                 if (metaBase) {
00140                         bstr_tmp.Empty();
00141                         COMTHROW(metaBase->get_DisplayedName(&bstr_tmp));
00142                         CopyTo(bstr_tmp,m_objectKind);
00143                 }
00144                 else {
00145                         m_objectKind = "N/A";
00146                 }
00147 
00148                 CComPtr<IMgaMetaRole> metaRole;
00149                 
00150                 CComPtr<IMgaFCO> mgaFCO;
00151                 if (SUCCEEDED(m_object.QueryInterface(&mgaFCO))) {
00152                         COMTHROW(mgaFCO->get_MetaRole(&metaRole) );
00153                 }
00154                 if (metaRole) {
00155                         bstr_tmp.Empty();
00156                         COMTHROW(metaRole->get_Name(&bstr_tmp));
00157                         CopyTo(bstr_tmp, m_objectRole);
00158                 }
00159                 else {
00160                         m_objectRole = "N/A";
00161                 }
00162         }
00163         catch (hresult_exception &){
00164                 ASSERT(("Error while initializing Registry Browser.", false));
00165         }
00166         UpdateData(FALSE);
00167 
00168         if (m_object) {
00169                 FillNodes();
00170         }
00171 
00172         return TRUE;  // return TRUE unless you set the focus to a control
00173                       // EXCEPTION: OCX Property Pages should return FALSE
00174 }
00175 
00176 void CRegistryBrowserDlg::OnSize(UINT nType, int cx, int cy) 
00177 {
00178         CDialog::OnSize(nType, cx, cy);
00179 
00180         // TODO: Add your message handler code here
00181         CRect origTopRect(0, 0, 0+406, 0+18), origBottomRect(0, 200, 0+406, 200+20 ); 
00182         MapDialogRect(&origTopRect);
00183         MapDialogRect(&origBottomRect);
00184 
00185         CRect rectTree, rectValue;
00186         if (m_wndRegistryTree.GetSafeHwnd()) {
00187                 m_wndRegistryTree.GetWindowRect(&rectTree);
00188                 ScreenToClient(&rectTree);
00189         }
00190         if (m_wndRegNode.GetSafeHwnd()) {
00191                 m_wndRegNode.GetWindowRect(&rectValue);
00192                 ScreenToClient(&rectValue);
00193         }
00194 
00195         // Not perfect....
00196         double ratio = (double)rectTree.Width()/((double)rectValue.Width()+(double)rectTree.Width());
00197 
00198         if (m_wndRegNode.GetSafeHwnd() && m_wndRegistryTree.GetSafeHwnd()) {
00199                 m_wndRegistryTree.SetWindowPos(NULL, 0, origTopRect.bottom, static_cast<int>(cx*ratio), cy - (origBottomRect.bottom-origBottomRect.top) - origTopRect.bottom, SWP_NOZORDER);
00200                 m_wndRegNode.SetWindowPos(NULL, static_cast<int>(cx*ratio)+GetSystemMetrics(SM_CYFRAME), origTopRect.bottom, cx - (static_cast<int>(cx*ratio)+GetSystemMetrics(SM_CYFRAME)), cy - (origBottomRect.bottom-origBottomRect.top) - origTopRect.bottom, SWP_NOZORDER);
00201                 m_wndSplitterBar.SetPanes(&m_wndRegistryTree,&m_wndRegNode);            
00202         }
00203 
00204         CRect rect3(0, 204, 0+406, 204+16);
00205         MapDialogRect(&rect3);
00206         CRect rectDlg;
00207         GetClientRect(&rectDlg);
00208         ratio = ((double)(rectDlg.Width())/ 406.0 );
00209 
00210         CWnd* wndOK = this->GetDlgItem(IDOK);
00211         if (wndOK) {
00212                 CRect origWnd;
00213                 wndOK->GetClientRect(&origWnd);
00214                 wndOK->MoveWindow(static_cast<int>(112.0 * ratio), rectDlg.bottom - (rect3.bottom - rect3.top), origWnd.Width(), origWnd.Height());
00215         }
00216         CWnd* wndCANCEL = this->GetDlgItem(IDCANCEL);
00217         if (wndCANCEL) {
00218                 CRect origWnd;
00219                 wndCANCEL->GetClientRect(&origWnd);
00220                 wndCANCEL->MoveWindow(static_cast<int>(242.0 * ratio), rectDlg.bottom - (rect3.bottom - rect3.top), origWnd.Width(), origWnd.Height());
00221         }
00222 
00223         
00224 }
00225 
00226 void CRegistryBrowserDlg::FillNodes(void)
00227 {
00228         CComPtr<IMgaRegNodes>   regNodes;
00229         CComPtr<IMgaFolder> mgaFolder;
00230         CComPtr<IMgaFCO> mgaFCO;
00231         if (SUCCEEDED(m_object.QueryInterface(&mgaFCO))) {
00232                 COMTHROW(mgaFCO->get_Registry(VARIANT_TRUE, &regNodes));
00233         }
00234         else if (SUCCEEDED(m_object.QueryInterface(&mgaFolder))) {
00235                 COMTHROW(mgaFolder->get_Registry(VARIANT_TRUE, &regNodes));
00236         }
00237         else {
00238                 ASSERT(("Unexpected MGA object.", false));
00239                 return;
00240         }
00241         AddNodes(NULL, regNodes);
00242 }
00243 
00244 void CRegistryBrowserDlg::AddNodes(CRegBrwNode *parent, CComPtr<IMgaRegNodes> &regNodes)
00245 {
00246         MGACOLL_ITERATE(IMgaRegNode, regNodes) {
00247                 CComPtr<IMgaRegNode> regNode = MGACOLL_ITER;
00248 
00249                 CRegBrwNode *brNode = new CRegBrwNode();
00250                 m_nodes.AddTail(brNode);
00251 
00252                 brNode->parent = parent;
00253 
00254                 CComBSTR bstr;
00255                 COMTHROW(regNode->get_Name(&bstr));
00256                 CopyTo(bstr, brNode->name);
00257 
00258                 bstr.Empty();
00259                 COMTHROW(regNode->get_Path(&bstr));
00260                 CopyTo(bstr, brNode->path);
00261 
00262                 bstr.Empty();
00263                 COMTHROW(regNode->get_Value(&bstr));
00264                 CopyTo(bstr, brNode->value);
00265 
00266                 COMTHROW(regNode->get_Status(&(brNode->status)));
00267 
00268                 HTREEITEM hnd;
00269                 if (parent) {
00270                         hnd = m_wndRegistryTree.InsertItem(brNode->name, (HTREEITEM)(parent->handle));
00271                 }
00272                 else {
00273                         hnd = m_wndRegistryTree.InsertItem(brNode->name);
00274                 }
00275 
00276                 brNode->handle = (void*)hnd;
00277                 m_wndRegistryTree.SetItemData(hnd,(DWORD)brNode);
00278                 
00279                 int imageNum;
00280                 switch (brNode->status) {
00281                 case ATTSTATUS_HERE:
00282                         m_imageMap.Lookup(IDI_ICON_REGHERE, imageNum);
00283                         break;
00284                 case ATTSTATUS_METADEFAULT:
00285                         m_imageMap.Lookup(IDI_ICON_REGMETA, imageNum);
00286                         break;
00287                 case ATTSTATUS_UNDEFINED:
00288                         m_imageMap.Lookup(IDI_ICON_REGUNDEF, imageNum);
00289                         break;
00290                 case ATTSTATUS_INVALID:
00291                         m_imageMap.Lookup(IDI_ICON_REGINVALID, imageNum);
00292                         break;
00293                 default:
00294                         m_imageMap.Lookup(IDI_ICON_REGINHERIT, imageNum);
00295                         break;
00296                 }
00297                 
00298 
00299                 VERIFY(m_wndRegistryTree.SetItemImage(hnd,imageNum,imageNum));
00300                 
00301                 CComPtr<IMgaRegNodes> subRegNodes;
00302                 COMTHROW(regNode->get_SubNodes(VARIANT_TRUE, &subRegNodes));
00303                 AddNodes(brNode, subRegNodes);
00304         }
00305         MGACOLL_ITERATE_END;
00306 }
00307 
00308 void CRegistryBrowserDlg::OnChangeRegnodeValue() 
00309 {
00310         // TODO: If this is a RICHEDIT control, the control will not
00311         // send this notification unless you override the CDialog::OnInitDialog()
00312         // function and call CRichEditCtrl().SetEventMask()
00313         // with the ENM_CHANGE flag ORed into the mask.
00314         
00315         // TODO: Add your control notification handler code here
00316         HTREEITEM hItem = m_wndRegistryTree.GetSelectedItem();
00317         if (hItem) {
00318                 CRegBrwNode *node = (CRegBrwNode *)m_wndRegistryTree.GetItemData(hItem);
00319                 if (node) {
00320                         node->status = ATTSTATUS_HERE;
00321                 }
00322                 int imageNum;
00323                 m_imageMap.Lookup(IDI_ICON_REGHERE, imageNum);
00324                 m_wndRegistryTree.SetItemImage(hItem, imageNum, imageNum);
00325                 
00326                 UpdateData(TRUE);
00327                 m_regnodeStatus = "HERE";
00328                 UpdateData(FALSE);
00329         }
00330         
00331 }
00332 
00333 void CRegistryBrowserDlg::UpdateRegistry()
00334 {
00335         CComPtr<IMgaRegNodes>   regNodes;
00336         CComPtr<IMgaFolder> mgaFolder;
00337         CComPtr<IMgaFCO> mgaFCO;
00338         
00339         if (SUCCEEDED(m_object.QueryInterface(&mgaFCO))) {
00340                 COMTHROW(mgaFCO->get_Registry(VARIANT_TRUE, &regNodes));
00341         }
00342         else if (SUCCEEDED(m_object.QueryInterface(&mgaFolder))) {
00343                 COMTHROW(mgaFolder->get_Registry(VARIANT_TRUE, &regNodes));
00344         }
00345         else {
00346                 ASSERT(("Unexpected MGA object.", false));
00347                 return;
00348         }
00349 
00350         MGACOLL_ITERATE(IMgaRegNode, regNodes) {
00351                 CComPtr<IMgaRegNode> regNode = MGACOLL_ITER;
00352                 COMTHROW(regNode->RemoveTree());
00353         }
00354         MGACOLL_ITERATE_END;
00355 
00356         POSITION pos = m_nodes.GetHeadPosition();
00357         while (pos) {
00358                 CRegBrwNode * node = m_nodes.GetNext(pos);
00359 
00360                 if (node->status == ATTSTATUS_HERE) {
00361                         CComBSTR path, value;
00362                         CopyTo(node->path, path);
00363                         CopyTo(node->value, value);
00364                         if (mgaFCO) {
00365                                 COMTHROW(mgaFCO->put_RegistryValue(path, value));
00366                         }
00367                         else {
00368                                 ASSERT(mgaFolder);
00369                                 COMTHROW(mgaFolder->put_RegistryValue(path, value));
00370                         }
00371                         
00372                 }
00373 
00374         }
00375 
00376 }
00377 
00378 void CRegistryBrowserDlg::OnOK() 
00379 {
00380         // TODO: Add extra validation here
00381         CRegBrwNode *oldnode;
00382         HTREEITEM hItem = m_wndRegistryTree.GetSelectedItem();
00383         if (hItem) {
00384                 oldnode = (CRegBrwNode*)m_wndRegistryTree.GetItemData(hItem);
00385                 if (oldnode) {
00386                         UpdateData(TRUE);
00387                         if (oldnode->status == ATTSTATUS_HERE) {
00388                                 oldnode->value = m_regnodeValue;
00389                                 oldnode->value.Replace(_T("\r\n"),_T("\n")); // Remove Win32 GUI line ends
00390                         }
00391                 }
00392         }
00393 
00394         CDialog::OnOK();
00395 }
00396 
00397 BOOL CRegistryBrowserDlg::PreTranslateMessage(MSG* pMsg) 
00398 {
00399         if (pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
00400         {
00401                 DWORD def_id=GetDefID();
00402                 if (def_id!=0)
00403                 {
00404             if (HIBYTE(GetKeyState(VK_CONTROL))) {
00405                 OnOK();
00406                 return TRUE;
00407             }
00408 
00409             if (pMsg->hwnd == m_wndRegistryTree.m_hWnd)
00410                         {
00411                                 GetDlgItem(LOWORD(def_id))->SetFocus();
00412                                 return TRUE;
00413                                 // discard the message!
00414                         }
00415 
00416 
00417                         CWnd *wnd=FromHandle(pMsg->hwnd);
00418                         if (wnd->GetParent()->m_hWnd == m_wndRegistryTree.m_hWnd) {
00419                                 GetDlgItem(LOWORD(def_id))->SetFocus();
00420                                 return TRUE;
00421                                 // discard the message!
00422                         }
00423                 }
00424         }
00425         // be a good citizen - call the base class
00426         return CDialog::PreTranslateMessage(pMsg);
00427 }