[CRT] crtdefs.h: Wrap localeinfo_struct in ifdef
[reactos.git] / base / applications / regedit / settings.c
index 1d562ac..c82a3a0 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <regedit.h>
+#include "regedit.h"
 
 const WCHAR g_szGeneralRegKey[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit";
+DECLSPEC_IMPORT ULONG WINAPIV DbgPrint(PCCH Format,...);
 
-/* 
+/*
 VV,VV,VV,VV,WA,WA,WA,WA,WB,WB,WB,WB,R1,R1,R1,R1
 R2,R2,R2,R2,R3,R3,R3,R3,R4,R4,R4,r4,LL,LL,LL,LL
 TT,TT,TT,TT,RR,RR,RR,RR,BB,BB,BB,BB,SS,SS,SS,SS
@@ -66,6 +67,7 @@ extern void LoadSettings(void)
     {
         RegistryBinaryConfig tConfig;
         DWORD iBufferSize = sizeof(tConfig);
+        BOOL bVisible = FALSE;
 
         if (RegQueryValueExW(hKey, L"View", NULL, NULL, (LPBYTE)&tConfig, &iBufferSize) == ERROR_SUCCESS)
         {
@@ -89,19 +91,21 @@ extern void LoadSettings(void)
 
                 /* Apply program window settings */
                 tConfig.tPlacement.length = sizeof(WINDOWPLACEMENT);
-                if (SetWindowPlacement(hFrameWnd, &tConfig.tPlacement) == FALSE)
-                    /* In case we fail, show normal */
-                    ShowWindow(hFrameWnd, SW_SHOWNORMAL);
+                bVisible = SetWindowPlacement(hFrameWnd, &tConfig.tPlacement);
             }
         }
 
+        /* In case we fail to restore the window, or open the key, show normal */
+        if (!bVisible)
+            ShowWindow(hFrameWnd, SW_SHOWNORMAL);
+
         /* Restore key position */
-        if (QueryStringValue(HKEY_CURRENT_USER, g_szGeneralRegKey, L"LastKey", szBuffer, COUNT_OF(szBuffer)) == ERROR_SUCCESS)
+        if (QueryStringValue(HKEY_CURRENT_USER, g_szGeneralRegKey, L"LastKey", szBuffer, ARRAY_SIZE(szBuffer)) == ERROR_SUCCESS)
         {
             SelectNode(g_pChildWnd->hTreeWnd, szBuffer);
         }
 
-    RegCloseKey(hKey);
+        RegCloseKey(hKey);
     }
     else
     {
@@ -116,49 +120,54 @@ extern void SaveSettings(void)
 
     if (RegCreateKeyW(HKEY_CURRENT_USER, g_szGeneralRegKey, &hKey) == ERROR_SUCCESS)
     {
-        if (RegOpenKeyW(HKEY_CURRENT_USER, g_szGeneralRegKey, &hKey) == ERROR_SUCCESS)
+        RegistryBinaryConfig tConfig;
+        DWORD iBufferSize = sizeof(tConfig);
+        WCHAR szBuffer[MAX_PATH]; /* FIXME: a complete registry path can be longer than that */
+        LPCWSTR keyPath, rootName;
+        HKEY hRootKey;
+
+        /* Save key position */
+        keyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
+        rootName = get_root_key_name(hRootKey);
+
+        /* Load "My Computer" string and complete it */
+        if (LoadStringW(hInst, IDS_MY_COMPUTER, szBuffer, ARRAY_SIZE(szBuffer)) &&
+            SUCCEEDED(StringCbCatW(szBuffer, sizeof(szBuffer), L"\\")) &&
+            SUCCEEDED(StringCbCatW(szBuffer, sizeof(szBuffer), rootName)) &&
+            SUCCEEDED(StringCbCatW(szBuffer, sizeof(szBuffer), L"\\")))
         {
-            RegistryBinaryConfig tConfig;
-            DWORD iBufferSize = sizeof(tConfig);
-            WCHAR szBuffer[MAX_PATH];
-            LPCWSTR keyPath, rootName;
-            HKEY hRootKey;
-
-            /* Save key position */
-            keyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
+            HRESULT hr = S_OK;
             if (keyPath)
-            {
-                rootName = get_root_key_name(hRootKey);
-
-                /* Load "My Computer" string and complete it */
-                LoadStringW(hInst, IDS_MY_COMPUTER, szBuffer, COUNT_OF(szBuffer));
-                wcscat(szBuffer, L"\\"); wcscat(szBuffer, rootName);
-                wcscat(szBuffer, L"\\"); wcscat(szBuffer, keyPath);
-
+                hr = StringCbCatW(szBuffer, sizeof(szBuffer), keyPath);
+            if (SUCCEEDED(hr))
                 RegSetValueExW(hKey, L"LastKey", 0, REG_SZ, (LPBYTE)szBuffer, (DWORD)wcslen(szBuffer) * sizeof(WCHAR));
-            }
+            else
+                DbgPrint("err: (%s:%d): Buffer not big enough for '%S + %S'\n", __FILE__, __LINE__, rootName, keyPath);
+        }
+        else
+        {
+            DbgPrint("err: (%s:%d): Buffer not big enough for '%S'\n", __FILE__, __LINE__, rootName);
+        }
 
-            /* Get statusbar settings */
-            tConfig.StatusBarVisible = ((GetMenuState(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND) & MF_CHECKED) ? 1 : 0);
+        /* Get statusbar settings */
+        tConfig.StatusBarVisible = ((GetMenuState(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND) & MF_CHECKED) ? 1 : 0);
 
-            /* Get splitter position */
-            tConfig.TreeViewSize = g_pChildWnd->nSplitPos;
+        /* Get splitter position */
+        tConfig.TreeViewSize = g_pChildWnd->nSplitPos;
 
-            /* Get list view column width*/
-            tConfig.NameColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 0);
-            tConfig.TypeColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 1);
-            tConfig.DataColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 2);
+        /* Get list view column width*/
+        tConfig.NameColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 0);
+        tConfig.TypeColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 1);
+        tConfig.DataColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 2);
 
-            /* Get program window settings */
-            tConfig.tPlacement.length = sizeof(WINDOWPLACEMENT);
-            GetWindowPlacement(hFrameWnd , &tConfig.tPlacement);
+        /* Get program window settings */
+        tConfig.tPlacement.length = sizeof(WINDOWPLACEMENT);
+        GetWindowPlacement(hFrameWnd, &tConfig.tPlacement);
 
-            /* Save all the data */
-            RegSetValueExW(hKey, L"View", 0, REG_BINARY, (LPBYTE)&tConfig, iBufferSize);
+        /* Save all the data */
+        RegSetValueExW(hKey, L"View", 0, REG_BINARY, (LPBYTE)&tConfig, iBufferSize);
 
-            RegCloseKey(hKey);
-        }
+        RegCloseKey(hKey);
     }
 }
-
 /* EOF */