Index: dll/cpl/desk/background.c
===================================================================
--- dll/cpl/desk/background.c	(revision 66479)
+++ dll/cpl/desk/background.c	(working copy)
@@ -163,15 +163,198 @@
 }
 
 
-/* Add the images in the C:\ReactOS directory and the current wallpaper if any */
+DWORD
+GetWallpaperDirectory(LPTSTR lpBuffer, DWORD uSize)
+{
+    HKEY hKey;
+    DWORD dwSize;
+    DWORD dwRet;
+    LPTSTR lpValue;
+
+    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"), 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
+    {
+        return 0;
+    }
+
+    if (RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dwSize, NULL, NULL) != ERROR_SUCCESS)
+    {
+        RegCloseKey(hKey);
+        return 0;
+    }
+
+    lpValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize + sizeof(TCHAR));
+    if (!lpValue)
+    {
+        RegCloseKey(hKey);
+        return 0;
+    }
+
+    if (RegQueryValueEx(hKey, TEXT("WallpaperDir"), NULL, NULL, (LPBYTE)lpValue, &dwSize) != ERROR_SUCCESS)
+    {
+        RegCloseKey(hKey);
+        HeapFree(GetProcessHeap(), 0, lpValue);
+        return 0;
+    }
+
+    dwRet = ExpandEnvironmentStrings(lpValue, lpBuffer, uSize);
+    
+    RegCloseKey(hKey);
+    HeapFree(GetProcessHeap(), 0, lpValue);
+
+    if (dwRet == 0)
+    {
+        return 0;
+    }
+
+    if (dwRet > uSize)
+    {
+        return dwRet;
+    }
+
+    return dwRet - 1;
+}
+
+
 static VOID
-AddListViewItems(HWND hwndDlg, PDATA pData)
+AddWallpapersFromDirectory(HIMAGELIST himl, HWND hwndBackgroundList, BackgroundItem *backgroundItem, PDATA pData, LPCTSTR wallpaperFilename, LPCTSTR wallpaperDirectory)
 {
     WIN32_FIND_DATA fd;
     HANDLE hFind;
     TCHAR szSearchPath[MAX_PATH];
     LPTSTR szFileTypes = NULL;
+    TCHAR separators[] = TEXT(";");
+    TCHAR *token;
+    HRESULT hr;
+    SHFILEINFO sfi;
+    UINT i = 0;
+    TCHAR *p;
     LV_ITEM listItem;
+
+
+    szFileTypes = GdipGetSupportedFileExtensions();
+    if (!szFileTypes)
+    {
+        return;
+    }
+
+    token = _tcstok(szFileTypes, separators);
+    while (token != NULL)
+    {
+        StringCbCopy(szSearchPath, sizeof(szSearchPath), wallpaperDirectory);
+
+        hr = StringCbCat(szSearchPath, sizeof(szSearchPath), TEXT("\\"));
+        if (FAILED(hr))
+        {
+            HeapFree(GetProcessHeap(), 0, szFileTypes);
+            return;
+        }
+
+        hr = StringCbCat(szSearchPath, sizeof(szSearchPath), token);
+        if (FAILED(hr))
+        {
+            HeapFree(GetProcessHeap(), 0, szFileTypes);
+            return;
+        }
+
+        hFind = FindFirstFile(szSearchPath, &fd);
+        while (hFind != INVALID_HANDLE_VALUE)
+        {
+            TCHAR filename[MAX_PATH];
+
+            hr = StringCbCopy(filename, sizeof(filename), wallpaperDirectory);
+            if (FAILED(hr))
+            {
+                FindClose(hFind);
+                HeapFree(GetProcessHeap(), 0, szFileTypes);
+                return;
+            }
+
+            hr = StringCbCat(filename, sizeof(filename), TEXT("\\"));
+            if (FAILED(hr))
+            {
+                FindClose(hFind);
+                HeapFree(GetProcessHeap(), 0, szFileTypes);
+                return;
+            }
+            hr = StringCbCat(filename, sizeof(filename), fd.cFileName);
+            if (FAILED(hr))
+            {
+                FindClose(hFind);
+                HeapFree(GetProcessHeap(), 0, szFileTypes);
+                return;
+            }
+
+            /* Don't add any hidden bitmaps. Also don't add current wallpaper once more. */
+            if (((fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) == 0) && (_tcsicmp(wallpaperFilename, filename) != 0))
+            {
+                himl = (HIMAGELIST)SHGetFileInfo(filename,
+                                                0,
+                                                &sfi,
+                                                sizeof(sfi),
+                                                SHGFI_SYSICONINDEX | SHGFI_SMALLICON |
+                                                SHGFI_DISPLAYNAME);
+
+                if (himl == NULL)
+                    break;
+
+                if (i++ == 0)
+                {
+                    (void)ListView_SetImageList(hwndBackgroundList, himl, LVSIL_SMALL);
+                }
+
+                backgroundItem = &pData->backgroundItems[pData->listViewItemCount];
+
+                backgroundItem->bWallpaper = TRUE;
+
+                hr = StringCbCopy(backgroundItem->szDisplayName, sizeof(backgroundItem->szDisplayName), sfi.szDisplayName);
+                if (FAILED(hr))
+                {
+                    FindClose(hFind);
+                    HeapFree(GetProcessHeap(), 0, szFileTypes);
+                    return;
+                }
+                p = _tcsrchr(backgroundItem->szDisplayName, _T('.'));
+                if (p)
+                    *p = (TCHAR)0;
+                hr = StringCbCopy(backgroundItem->szFilename, sizeof(backgroundItem->szFilename), filename);
+                if (FAILED(hr))
+                {
+                    FindClose(hFind);
+                    HeapFree(GetProcessHeap(), 0, szFileTypes);
+                    return;
+                }
+
+                ZeroMemory(&listItem, sizeof(LV_ITEM));
+                listItem.mask       = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
+                listItem.pszText    = backgroundItem->szDisplayName;
+                listItem.state      = 0;
+                listItem.iImage     = sfi.iIcon;
+                listItem.iItem      = pData->listViewItemCount;
+                listItem.lParam     = pData->listViewItemCount;
+
+                (void)ListView_InsertItem(hwndBackgroundList, &listItem);
+
+                pData->listViewItemCount++;
+            }
+
+            if(!FindNextFile(hFind, &fd))
+                break;
+        }
+
+        token = _tcstok(NULL, separators);
+        FindClose(hFind);
+    }
+
+    HeapFree(GetProcessHeap(), 0, szFileTypes);
+}
+
+
+/* Add the images in the C:\ReactOS, the wallpaper directory and the current wallpaper if any */
+static VOID
+AddListViewItems(HWND hwndDlg, PDATA pData)
+{
+    TCHAR szSearchPath[MAX_PATH];
+    LV_ITEM listItem;
     LV_COLUMN dummy;
     RECT clientRect;
     HKEY regKey;
@@ -185,8 +368,6 @@
     LONG result;
     UINT i = 0;
     BackgroundItem *backgroundItem = NULL;
-    TCHAR separators[] = TEXT(";");
-    TCHAR *token;
     HWND hwndBackgroundList;
     TCHAR *p;
     HRESULT hr;
@@ -323,116 +504,16 @@
     }
 
     /* Add all the images in the C:\ReactOS directory. */
-
-    szFileTypes = GdipGetSupportedFileExtensions();
-    if (!szFileTypes)
+    if (GetWindowsDirectory(szSearchPath, MAX_PATH))
     {
-        return;
+        AddWallpapersFromDirectory(himl, hwndBackgroundList, backgroundItem, pData, wallpaperFilename, szSearchPath);
     }
 
-    token = _tcstok(szFileTypes, separators);
-    while (token != NULL)
+    /* Add all the images in the wallpaper directory. */
+    if (GetWallpaperDirectory(szSearchPath, MAX_PATH))
     {
-        GetWindowsDirectory(szSearchPath, MAX_PATH);
-
-        hr = StringCbCat(szSearchPath, sizeof(szSearchPath), TEXT("\\"));
-        if (FAILED(hr))
-        {
-            HeapFree(GetProcessHeap(), 0, szFileTypes);
-            return;
-        }
-
-        hr = StringCbCat(szSearchPath, sizeof(szSearchPath), token);
-        if (FAILED(hr))
-        {
-            HeapFree(GetProcessHeap(), 0, szFileTypes);
-            return;
-        }
-
-        hFind = FindFirstFile(szSearchPath, &fd);
-        while (hFind != INVALID_HANDLE_VALUE)
-        {
-            TCHAR filename[MAX_PATH];
-
-            GetWindowsDirectory(filename, MAX_PATH);
-
-            hr = StringCbCat(filename, sizeof(filename), TEXT("\\"));
-            if (FAILED(hr))
-            {
-                FindClose(hFind);
-                HeapFree(GetProcessHeap(), 0, szFileTypes);
-                return;
-            }
-            hr = StringCbCat(filename, sizeof(filename), fd.cFileName);
-            if (FAILED(hr))
-            {
-                FindClose(hFind);
-                HeapFree(GetProcessHeap(), 0, szFileTypes);
-                return;
-            }
-
-            /* Don't add any hidden bitmaps. Also don't add current wallpaper once more. */
-            if (((fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) == 0) && (_tcsicmp(wallpaperFilename, filename) != 0))
-            {
-                himl = (HIMAGELIST)SHGetFileInfo(filename,
-                                                0,
-                                                &sfi,
-                                                sizeof(sfi),
-                                                SHGFI_SYSICONINDEX | SHGFI_SMALLICON |
-                                                SHGFI_DISPLAYNAME);
-
-                if (himl == NULL)
-                    break;
-
-                if (i++ == 0)
-                {
-                    (void)ListView_SetImageList(hwndBackgroundList, himl, LVSIL_SMALL);
-                }
-
-                backgroundItem = &pData->backgroundItems[pData->listViewItemCount];
-
-                backgroundItem->bWallpaper = TRUE;
-
-                hr = StringCbCopy(backgroundItem->szDisplayName, sizeof(backgroundItem->szDisplayName), sfi.szDisplayName);
-                if (FAILED(hr))
-                {
-                    FindClose(hFind);
-                    HeapFree(GetProcessHeap(), 0, szFileTypes);
-                    return;
-                }
-                p = _tcsrchr(backgroundItem->szDisplayName, _T('.'));
-                if (p)
-                    *p = (TCHAR)0;
-                hr = StringCbCopy(backgroundItem->szFilename, sizeof(backgroundItem->szFilename), filename);
-                if (FAILED(hr))
-                {
-                    FindClose(hFind);
-                    HeapFree(GetProcessHeap(), 0, szFileTypes);
-                    return;
-                }
-
-                ZeroMemory(&listItem, sizeof(LV_ITEM));
-                listItem.mask       = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
-                listItem.pszText    = backgroundItem->szDisplayName;
-                listItem.state      = 0;
-                listItem.iImage     = sfi.iIcon;
-                listItem.iItem      = pData->listViewItemCount;
-                listItem.lParam     = pData->listViewItemCount;
-
-                (void)ListView_InsertItem(hwndBackgroundList, &listItem);
-
-                pData->listViewItemCount++;
-            }
-
-            if(!FindNextFile(hFind, &fd))
-                break;
-        }
-
-        token = _tcstok(NULL, separators);
-        FindClose(hFind);
+        AddWallpapersFromDirectory(himl, hwndBackgroundList, backgroundItem, pData, wallpaperFilename, szSearchPath);
     }
-
-    HeapFree(GetProcessHeap(), 0, szFileTypes);
 }
 
 
