- Get autochk, calc, cmd, devmgr, expand, format, gettype, hostname, lsass, msconfig...
[reactos.git] / reactos / subsys / system / regedit / framewnd.c
index 201bee0..01f271f 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include <windows.h>
-#include <tchar.h>
-#include <commctrl.h>
-#include <commdlg.h>
-#include <cderr.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <shellapi.h>
-#include <objsel.h>
-#include <objbase.h>
-
-#include "main.h"
-#include "regproc.h"
+#include <regedit.h>
 
 /********************************************************************************
  * Global and Local Variables:
  */
 
+#define FAVORITES_MENU_POSITION 3
+
+static TCHAR s_szFavoritesRegKey[] = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit\\Favorites");
+
 static BOOL bInMenuLoop = FALSE;        /* Tells us if we are in the menu loop */
 
 /*******************************************************************************
@@ -71,6 +63,59 @@ static void resize_frame_client(HWND hWnd)
 
 /********************************************************************************/
 
+static void OnInitMenu(HWND hWnd)
+{
+    LONG lResult;
+    HKEY hKey = NULL;
+    DWORD dwIndex, cbValueName, cbValueData, dwType;
+    TCHAR szValueName[256];
+    BYTE abValueData[256];
+    static int s_nFavoriteMenuSubPos = -1;
+    HMENU hMenu;
+    BOOL bDisplayedAny = FALSE;
+
+    /* Find Favorites menu and clear it out */
+    hMenu = GetSubMenu(GetMenu(hWnd), FAVORITES_MENU_POSITION);
+    if (!hMenu)
+        goto done;
+    if (s_nFavoriteMenuSubPos < 0)
+    {
+        s_nFavoriteMenuSubPos = GetMenuItemCount(hMenu);
+    }
+    else
+    {
+        while(RemoveMenu(hMenu, s_nFavoriteMenuSubPos, MF_BYPOSITION))
+            ;
+    }
+    
+    lResult = RegOpenKey(HKEY_CURRENT_USER, s_szFavoritesRegKey, &hKey);
+    if (lResult != ERROR_SUCCESS)
+        goto done;
+
+    dwIndex = 0;
+    do
+    {
+        cbValueName = sizeof(szValueName) / sizeof(szValueName[0]);
+        cbValueData = sizeof(abValueData);
+        lResult = RegEnumValue(hKey, dwIndex, szValueName, &cbValueName, NULL, &dwType, abValueData, &cbValueData);
+        if ((lResult == ERROR_SUCCESS) && (dwType == REG_SZ))
+        {
+            if (!bDisplayedAny)
+            {
+                AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
+                bDisplayedAny = TRUE;
+            }
+            AppendMenu(hMenu, 0, ID_FAVORITES_MIN + GetMenuItemCount(hMenu), szValueName);
+        }
+        dwIndex++;
+    }
+    while(lResult == ERROR_SUCCESS);
+
+done:
+    if (hKey)
+        RegCloseKey(hKey);
+}
+
 static void OnEnterMenuLoop(HWND hWnd)
 {
     int nParts;
@@ -343,7 +388,7 @@ static UINT_PTR CALLBACK ExportRegistryFile_OFNHookProc(HWND hdlg, UINT uiMsg, W
     return iResult;
 }
 
-static BOOL ExportRegistryFile(HWND hWnd)
+BOOL ExportRegistryFile(HWND hWnd)
 {
     OPENFILENAME ofn;
     TCHAR ExportKeyPath[_MAX_PATH];
@@ -365,10 +410,17 @@ static BOOL ExportRegistryFile(HWND hWnd)
     ofn.lpTemplateName = MAKEINTRESOURCE(IDD_EXPORTRANGE);
     if (GetSaveFileName(&ofn)) {
         BOOL result;
-        /* FIXME - convert strings to ascii! */
-        result = export_registry_key((CHAR*)ofn.lpstrFile, (CHAR*)ExportKeyPath);
-        /*result = export_registry_key(ofn.lpstrFile, NULL);*/
-        /*if (!export_registry_key(ofn.lpstrFile, NULL)) {*/
+        LPSTR pszExportKeyPath;
+#ifdef UNICODE
+        CHAR buffer[_MAX_PATH];
+
+        WideCharToMultiByte(CP_ACP, 0, ExportKeyPath, -1, buffer, sizeof(buffer), NULL, NULL);
+        pszExportKeyPath = buffer;
+#else
+        pszExportKeyPath = ExportKeyPath;
+#endif
+
+        result = export_registry_key(ofn.lpstrFile, pszExportKeyPath);
         if (!result) {
             /*printf("Can't open file \"%s\"\n", ofn.lpstrFile);*/
             return FALSE;
@@ -462,34 +514,65 @@ BOOL PrintRegistryHive(HWND hWnd, LPTSTR path)
     return TRUE;
 }
 
-static BOOL CopyKeyName(HWND hWnd, LPCTSTR keyName)
+static void ChooseFavorite(LPCTSTR pszFavorite)
 {
-    BOOL result;
+    HKEY hKey = NULL;
+    TCHAR szFavoritePath[512];
+    DWORD cbData, dwType;
 
-    result = OpenClipboard(hWnd);
-    if (result) {
-        result = EmptyClipboard();
-        if (result) {
+    if (RegOpenKeyEx(HKEY_CURRENT_USER, s_szFavoritesRegKey, 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
+        goto done;
 
-            /*HANDLE hClipData;*/
-            /*hClipData = SetClipboardData(UINT uFormat, HANDLE hMem);*/
+    cbData = (sizeof(szFavoritePath) / sizeof(szFavoritePath[0])) - 1;
+    memset(szFavoritePath, 0, sizeof(szFavoritePath));
+    if (RegQueryValueEx(hKey, pszFavorite, NULL, &dwType, (LPBYTE) szFavoritePath, &cbData) != ERROR_SUCCESS)
+        goto done;
 
-        } else {
-            /* error emptying clipboard*/
-            /* DWORD dwError = GetLastError(); */
-            ;
-        }
-        if (!CloseClipboard()) {
-            /* error closing clipboard*/
-            /* DWORD dwError = GetLastError(); */
-            ;
-        }
-    } else {
-        /* error opening clipboard*/
-        /* DWORD dwError = GetLastError(); */
-        ;
-    }
-    return result;
+    if (dwType == REG_SZ)
+        SelectNode(g_pChildWnd->hTreeWnd, szFavoritePath);
+
+done:
+    if (hKey)
+        RegCloseKey(hKey);
+}
+
+BOOL CopyKeyName(HWND hWnd, HKEY hRootKey, LPCTSTR keyName)
+{
+    BOOL bClipboardOpened = FALSE;
+    BOOL bSuccess = FALSE;
+    TCHAR szBuffer[512];
+    HGLOBAL hGlobal;
+    LPTSTR s;
+
+    if (!OpenClipboard(hWnd))
+        goto done;
+    bClipboardOpened = TRUE;
+
+    if (!EmptyClipboard())
+        goto done;
+
+    if (!RegKeyGetName(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), hRootKey, keyName))
+        goto done;
+
+    hGlobal = GlobalAlloc(GMEM_MOVEABLE, (_tcslen(szBuffer) + 1) * sizeof(TCHAR));
+    if (!hGlobal)
+        goto done;
+
+    s = GlobalLock(hGlobal);
+    _tcscpy(s, szBuffer);
+    GlobalUnlock(hGlobal);
+
+#ifdef UNICODE
+    SetClipboardData(CF_UNICODETEXT, hGlobal);
+#else
+    SetClipboardData(CF_TEXT, hGlobal);
+#endif
+    bSuccess = TRUE;
+
+done:
+    if (bClipboardOpened)
+        CloseClipboard();
+    return bSuccess;
 }
 
 static BOOL CreateNewValue(HKEY hRootKey, LPCTSTR pszKeyPath, DWORD dwType)
@@ -809,36 +892,48 @@ static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
         break;
     case ID_EDIT_DELETE:
     {
-        UINT nSelected = ListView_GetSelectedCount(g_pChildWnd->hListWnd);
-        if(nSelected >= 1)
+        if (GetFocus() == g_pChildWnd->hListWnd)
         {
-          TCHAR msg[128], caption[128];
-          LoadString(hInst, IDS_QUERY_DELETE_CONFIRM, caption, sizeof(caption)/sizeof(TCHAR));
-          LoadString(hInst, (nSelected == 1 ? IDS_QUERY_DELETE_ONE : IDS_QUERY_DELETE_MORE), msg, sizeof(msg)/sizeof(TCHAR));
-          if(MessageBox(g_pChildWnd->hWnd, msg, caption, MB_ICONQUESTION | MB_YESNO) == IDYES)
+          UINT nSelected = ListView_GetSelectedCount(g_pChildWnd->hListWnd);
+          if(nSelected >= 1)
           {
-            int ni, errs;
-
-           item = -1;
-           errs = 0;
-            while((ni = ListView_GetNextItem(g_pChildWnd->hListWnd, item, LVNI_SELECTED)) > -1)
+            TCHAR msg[128], caption[128];
+            LoadString(hInst, IDS_QUERY_DELETE_CONFIRM, caption, sizeof(caption)/sizeof(TCHAR));
+            LoadString(hInst, (nSelected == 1 ? IDS_QUERY_DELETE_ONE : IDS_QUERY_DELETE_MORE), msg, sizeof(msg)/sizeof(TCHAR));
+            if(MessageBox(g_pChildWnd->hWnd, msg, caption, MB_ICONQUESTION | MB_YESNO) == IDYES)
             {
-              valueName = GetValueName(g_pChildWnd->hListWnd, item);
-              if(RegDeleteValue(hKey, valueName) != ERROR_SUCCESS)
+              int ni, errs;
+
+              item = -1;
+              errs = 0;
+              while((ni = ListView_GetNextItem(g_pChildWnd->hListWnd, item, LVNI_SELECTED)) > -1)
               {
-                errs++;
+                valueName = GetValueName(g_pChildWnd->hListWnd, item);
+                if(RegDeleteValue(hKey, valueName) != ERROR_SUCCESS)
+                {
+                  errs++;
+                }
+                item = ni;
               }
-             item = ni;
-            }
 
-            RefreshListView(g_pChildWnd->hListWnd, hKeyRoot, keyPath);
-            if(errs > 0)
-            {
-              LoadString(hInst, IDS_ERR_DELVAL_CAPTION, caption, sizeof(caption)/sizeof(TCHAR));
-              LoadString(hInst, IDS_ERR_DELETEVALUE, msg, sizeof(msg)/sizeof(TCHAR));
-              MessageBox(g_pChildWnd->hWnd, msg, caption, MB_ICONSTOP);
+              RefreshListView(g_pChildWnd->hListWnd, hKeyRoot, keyPath);
+              if(errs > 0)
+              {
+                LoadString(hInst, IDS_ERR_DELVAL_CAPTION, caption, sizeof(caption)/sizeof(TCHAR));
+                LoadString(hInst, IDS_ERR_DELETEVALUE, msg, sizeof(msg)/sizeof(TCHAR));
+                MessageBox(g_pChildWnd->hWnd, msg, caption, MB_ICONSTOP);
+              }
             }
           }
+        } else 
+        if (GetFocus() == g_pChildWnd->hTreeWnd)
+        {
+          if (keyPath == 0 || *keyPath == 0)
+          {
+             MessageBeep(MB_ICONHAND); 
+          } else
+          if (DeleteKey(hWnd, hKeyRoot, keyPath))
+            DeleteNode(g_pChildWnd->hTreeWnd, 0);
         }
        break;
     case ID_EDIT_NEW_STRINGVALUE:
@@ -850,9 +945,22 @@ static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
     case ID_EDIT_NEW_DWORDVALUE:
         CreateNewValue(hKeyRoot, keyPath, REG_DWORD);
         break;
+       case ID_EDIT_NEW_MULTISTRINGVALUE:
+        CreateNewValue(hKeyRoot, keyPath, REG_MULTI_SZ);
+        break;
+       case ID_EDIT_NEW_EXPANDABLESTRINGVALUE:
+        CreateNewValue(hKeyRoot, keyPath, REG_EXPAND_SZ);
+        break;
+
     }
+    case ID_EDIT_FIND:
+        FindDialog(hWnd);
+        break;
+    case ID_EDIT_FINDNEXT:
+        FindNext(hWnd);
+        break;
     case ID_EDIT_COPYKEYNAME:
-        CopyKeyName(hWnd, _T(""));
+        CopyKeyName(hWnd, hKeyRoot, keyPath);
         break;
     case ID_EDIT_PERMISSIONS:
         if(keyPath != NULL && _tcslen(keyPath) > 0)
@@ -884,7 +992,31 @@ static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
         CreateNewKey(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd));
         break;
     default:
-        result = FALSE;
+        if ((LOWORD(wParam) >= ID_FAVORITES_MIN) && (LOWORD(wParam) <= ID_FAVORITES_MAX))
+        {
+            HMENU hMenu;
+            MENUITEMINFO mii;
+            TCHAR szFavorite[512];
+
+            hMenu = GetSubMenu(GetMenu(hWnd), FAVORITES_MENU_POSITION);
+
+            memset(&mii, 0, sizeof(mii));
+            mii.cbSize = sizeof(mii);
+            mii.fMask = MIIM_TYPE;
+            mii.fType = MFT_STRING;
+            mii.dwTypeData = szFavorite;
+            mii.cch = sizeof(szFavorite) / sizeof(szFavorite[0]);
+
+            if (GetMenuItemInfo(hMenu, LOWORD(wParam) - ID_FAVORITES_MIN, TRUE, &mii))
+            {
+                ChooseFavorite(szFavorite);
+            }
+        }
+        else
+        {
+            result = FALSE;
+        }
+        break;
     }
 
     if(hKey)
@@ -924,6 +1056,9 @@ LRESULT CALLBACK FrameWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
         break;
     case WM_TIMER:
         break;
+    case WM_INITMENU:
+        OnInitMenu(hWnd);
+        break;
     case WM_ENTERMENULOOP:
         OnEnterMenuLoop(hWnd);
         break;