dback.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 24k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. //#include <shellids.h>
  4. //#include "resource.h"
  5. //#include "deskhtml.h"
  6. //#include "dback.h"
  7. //#include "deskstat.h"
  8. //#include "dbackp.h"
  9. //#include "dutil.h"
  10. //#include "pattern.h"
  11. #include <mluisupp.h>
  12. #ifdef POSTSPLIT
  13. #define THISCLASS CBackPropSheetPage
  14. const TCHAR c_szSetup[] = REGSTR_PATH_SETUP TEXT("\Setup");
  15. const TCHAR c_szSharedDir[] = TEXT("SharedDir");
  16. static const LPCTSTR c_rgpszWallpaperExt[] = {
  17.     TEXT("BMP"), TEXT("GIF"),
  18.     TEXT("JPG"), TEXT("JPE"),
  19.     TEXT("JPEG"),TEXT("DIB"),
  20.     TEXT("PNG"), TEXT("HTM"),
  21.     TEXT("HTML")
  22. };
  23. #define c_szHelpFile    TEXT("Update.hlp")
  24. const static DWORD aBackHelpIDs[] = {  // Context Help IDs
  25.     IDC_BACK_GROUP,     IDH_GROUPBOX,
  26.     IDC_BACK_WPLIST,    IDH_WALLPAPER_LIST,
  27.     IDC_BACK_BROWSE,    IDH_BROWSE_WALLPAPER,
  28.     IDC_BACK_PATTERN,   IDH_DESKTOP_PATTERN,
  29.     IDC_BACK_DISPLAY,   IDH_DISPLAY_WALLPAPER,
  30.     IDC_BACK_WPSTYLE,   IDH_DISPLAY_WALLPAPER,
  31.     IDC_BACK_PREVIEW,   IDH_WALLPAPER_SAMPLE,
  32.     0, 0
  33. };
  34. THISCLASS::CBackPropSheetPage(void)
  35. {
  36.     //
  37.     // Initialize a bunch of propsheetpage variables.
  38.     //
  39.     dwSize = sizeof(CBackPropSheetPage);
  40.     dwFlags = PSP_DEFAULT | PSP_USECALLBACK;
  41.     hInstance = HINST_THISDLL;
  42.     pszTemplate = MAKEINTRESOURCE(IDD_BACKGROUND);
  43.     // hIcon = NULL; // unused (PSP_USEICON is not set)
  44.     // pszTitle = NULL; // unused (PSP_USETITLE is not set)
  45.     pfnDlgProc = (DLGPROC)_DlgProc;
  46.     // lParam   = 0;     // unused
  47.     pfnCallback = NULL;
  48.     // pcRefParent = NULL;
  49. }
  50. int THISCLASS::_AddAFileToLV(LPCTSTR pszDir, LPTSTR pszFile, UINT nBitmap)
  51. {
  52.     int index;
  53.     LPTSTR pszPath = (LPTSTR)LocalAlloc(LPTR,
  54.         ((pszDir ? lstrlen(pszDir) : 0) + lstrlen(pszFile) + 2) * sizeof(TCHAR));
  55.     if (pszPath)
  56.     {
  57.         if (pszDir)
  58.         {
  59.             lstrcpy(pszPath, pszDir );
  60.             lstrcat(pszPath, TEXT("\"));
  61.             lstrcat(pszPath, pszFile);
  62.         }
  63.         else if (pszFile && *pszFile && (lstrcmpi(pszFile, g_szNone) != 0))
  64.         {
  65.             lstrcpy(pszPath, pszFile);
  66.         }
  67.         else
  68.         {
  69.             *pszPath = TEXT('');
  70.         }
  71.         pszFile = PathFindFileName(pszFile);
  72.         PathRemoveExtension(pszFile);
  73.         PathMakePretty(pszFile);
  74.         LV_ITEM lvi = {0};
  75.         lvi.mask = LVIF_TEXT | LVIF_PARAM | (nBitmap != -1 ? LVIF_IMAGE : 0);
  76.         lvi.iItem = 0x7FFFFFFF;
  77.         lvi.pszText = pszFile;
  78.         lvi.iImage = nBitmap;
  79.         lvi.lParam = (LPARAM)pszPath;
  80.         index = ListView_InsertItem(_hwndLV, &lvi);
  81.         ListView_SetColumnWidth(_hwndLV, 0, LVSCW_AUTOSIZE);
  82.         if (index == -1)
  83.         {
  84.             LocalFree((HANDLE)pszPath);
  85.         }
  86.     }
  87.     return index;
  88. }
  89. void THISCLASS::_AddFilesToLV(LPCTSTR pszDir, LPCTSTR pszSpec, UINT nBitmap)
  90. {
  91.     WIN32_FIND_DATA fd;
  92.     HANDLE h;
  93.     TCHAR szBuf[MAX_PATH];
  94.     lstrcpy(szBuf, pszDir);
  95.     lstrcat(szBuf, TEXT("\*."));
  96.     lstrcat(szBuf, pszSpec);
  97.     h = FindFirstFile(szBuf, &fd);
  98.     if (h != INVALID_HANDLE_VALUE)
  99.     {
  100.         do
  101.         {
  102.             _AddAFileToLV(pszDir, fd.cFileName, nBitmap);
  103.         }
  104.         while (FindNextFile(h, &fd));
  105.         FindClose(h);
  106.     }
  107. }
  108. int THISCLASS::_FindWallpaper(LPCTSTR pszFile)
  109. {
  110.     int nItems = ListView_GetItemCount(_hwndLV);
  111.     int i;
  112.     for (i=0; i<nItems; i++)
  113.     {
  114.         LV_ITEM lvi = {0};
  115.         lvi.iItem = i;
  116.         lvi.mask = LVIF_PARAM;
  117.         ListView_GetItem(_hwndLV, &lvi);
  118.         if (lstrcmpi(pszFile, (LPCTSTR)lvi.lParam) == 0)
  119.         {
  120.             return i;
  121.         }
  122.     }
  123.     return -1;
  124. }
  125. void THISCLASS::_UpdatePreview(WPARAM flags)
  126. {
  127.     WALLPAPEROPT wpo;
  128.     wpo.dwSize = sizeof(WALLPAPEROPT);
  129.     g_pActiveDesk->GetWallpaperOptions(&wpo, 0);
  130.     if (wpo.dwStyle & WPSTYLE_TILE)
  131.     {
  132.         flags |= BP_TILE;
  133.     }
  134.     else if(wpo.dwStyle & WPSTYLE_STRETCH)
  135.             flags |= BP_STRETCH;
  136.     
  137.     SendDlgItemMessage(_hwnd, IDC_BACK_PREVIEW, WM_SETBACKINFO, flags, 0);
  138. }
  139. void THISCLASS::_EnableControls(void)
  140. {
  141.     if (_fAllowChanges)
  142.     {
  143.         BOOL fEnable;
  144.         WALLPAPEROPT wpo = { SIZEOF(wpo) };
  145.         g_pActiveDesk->GetWallpaperOptions(&wpo, 0);
  146.         WCHAR wszWallpaper[INTERNET_MAX_URL_LENGTH];
  147.         LPTSTR pszWallpaper;
  148.         g_pActiveDesk->GetWallpaper(wszWallpaper, ARRAYSIZE(wszWallpaper), 0);
  149. #ifndef UNICODE
  150.         CHAR szWallpaper[INTERNET_MAX_URL_LENGTH];
  151.         SHUnicodeToAnsi(wszWallpaper, szWallpaper, ARRAYSIZE(szWallpaper));
  152.         pszWallpaper = szWallpaper;
  153. #else
  154.         pszWallpaper = (LPTSTR)wszWallpaper;
  155. #endif
  156.         BOOL fIsPicture = IsWallpaperPicture(pszWallpaper);
  157.         //
  158.         // Pattern button only enabled when we are viewing
  159.         // a picture in centered mode, or when we have no
  160.         // wallpaper at all.
  161.         //
  162.         fEnable = ((wpo.dwStyle == WPSTYLE_CENTER) && fIsPicture) || !*pszWallpaper;
  163.         EnableWindow(GetDlgItem(_hwnd, IDC_BACK_PATTERN), fEnable);
  164.         //
  165.         // Style combo only enabled if a non-null picture
  166.         // is being viewed.
  167.         //
  168.         fEnable = fIsPicture && (*pszWallpaper);
  169.         EnableWindow(GetDlgItem(_hwnd, IDC_BACK_WPSTYLE), fEnable);
  170.     }
  171. }
  172. int THISCLASS::_GetImageIndex(LPCTSTR pszFile)
  173. {
  174.     int iRet = 0;
  175.     if (pszFile && *pszFile)
  176.     {
  177.         LPCTSTR pszExt = PathFindExtension(pszFile);
  178.         if (*pszExt == TEXT('.'))
  179.         {
  180.             pszExt++;
  181.             for (iRet=0; iRet<ARRAYSIZE(c_rgpszWallpaperExt); iRet++)
  182.             {
  183.                 if (lstrcmpi(pszExt, c_rgpszWallpaperExt[iRet]) == 0)
  184.                 {
  185.                     //
  186.                     // Add one because 'none' took the 0th slot.
  187.                     //
  188.                     iRet++;
  189.                     return(iRet);
  190.                 }
  191.             }
  192.             //
  193.             // If we fell off the end of the for loop here,
  194.             // this is a file with unknown extension. So, we assume that
  195.             // it is a normal wallpaper and it gets the Bitmap's icon
  196.             //
  197.             iRet = 1;
  198.         }
  199.         else
  200.         {
  201.             //
  202.             // Unknown files get Bitmap's icon.
  203.             //
  204.             iRet = 1;
  205.         }
  206.     }
  207.     return iRet;
  208. }
  209. void THISCLASS::_SetNewWallpaper(LPCTSTR pszFile)
  210. {
  211.     TCHAR szFile[INTERNET_MAX_URL_LENGTH];
  212.     TCHAR szTemp[INTERNET_MAX_URL_LENGTH];
  213.     //
  214.     // Make a copy of the file name.
  215.     //
  216.     lstrcpy(szFile, pszFile);
  217.     //
  218.     // Replace all "(none)" with empty strings.
  219.     //
  220.     if (!szFile || (lstrcmpi(szFile, g_szNone) == 0))
  221.     {
  222.         szFile[0] = TEXT('');
  223.     }
  224.     //
  225.     // Replace net drives with UNC names.
  226.     //
  227.     if(
  228. #ifndef UNICODE
  229.         !IsDBCSLeadByte(szFile[0]) &&
  230. #endif
  231.         (szFile[1] == TEXT(':')) )
  232.     {
  233.         TCHAR szDrive[3];
  234.         ULONG cchTemp = ARRAYSIZE(szTemp);
  235.         lstrcpyn(szDrive, szFile, ARRAYSIZE(szDrive));
  236.         if ((SHWNetGetConnection(szDrive, szTemp, &cchTemp) ==
  237.             NO_ERROR) && (szTemp[0] == TEXT('\')) && (szTemp[1] == TEXT('\')))
  238.         {
  239.             lstrcat(szTemp, szFile+2);
  240.             lstrcpy(szFile, szTemp);
  241.         }
  242.     }
  243.     WCHAR   wszTemp[INTERNET_MAX_URL_LENGTH];
  244.     LPTSTR  pszTemp;
  245.     //
  246.     // If necessary, update the desk state object.
  247.     //
  248.     g_pActiveDesk->GetWallpaper(wszTemp, ARRAYSIZE(wszTemp), 0);
  249. #ifndef UNICODE
  250.     SHUnicodeToAnsi(wszTemp, szTemp, ARRAYSIZE(szTemp));
  251.     pszTemp = szTemp;
  252. #else
  253.     pszTemp = (LPTSTR)wszTemp;
  254. #endif
  255.     if (lstrcmpi(pszTemp, szFile) != 0)
  256.     {
  257.         LPWSTR  pwszFile;
  258. #ifndef UNICODE
  259.         SHAnsiToUnicode(szFile, wszTemp, ARRAYSIZE(wszTemp));
  260.         pwszFile = wszTemp;
  261. #else
  262.         pwszFile = (LPWSTR)szFile;
  263. #endif
  264.         g_pActiveDesk->SetWallpaper(pwszFile, 0);
  265.     }
  266.     //
  267.     // Update the preview picture of the new wallpaper.
  268.     //
  269.     _UpdatePreview(0);
  270.     //
  271.     // If necessary, add the new item to the listview.
  272.     //
  273.     TCHAR   szTemp2[INTERNET_MAX_URL_LENGTH];
  274.     // If the wallpaper does not have a directory specified, (this may happen if other apps. change this value),
  275.     // we have to figure it out.
  276.     GetWallpaperWithPath(szFile, szTemp2, ARRAYSIZE(szTemp2));
  277.     int iSelectionNew = *szTemp2 ? _FindWallpaper(szTemp2) : 0;
  278.     if (iSelectionNew == -1)
  279.     {
  280.         iSelectionNew = _AddAFileToLV(NULL, szTemp2, _GetImageIndex(szTemp2));
  281.     }
  282.     //
  283.     // If necessary, select the item in the listview.
  284.     //
  285.     int iSelected = ListView_GetNextItem(_hwndLV, -1, LVNI_SELECTED);
  286.     if (iSelected != iSelectionNew)
  287.     {
  288.         ListView_SetItemState(_hwndLV, iSelectionNew, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
  289.     }
  290.     //
  291.     // Put all controls in correct enabled state.
  292.     //
  293.     _EnableControls();
  294.     //
  295.     // Make sure the selected item is visible.
  296.     //
  297.     ListView_EnsureVisible(_hwndLV, iSelectionNew, FALSE);
  298. }
  299. int CALLBACK THISCLASS::_SortBackgrounds(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
  300. {
  301.     TCHAR szFile1[MAX_PATH], szFile2[MAX_PATH];
  302.     lstrcpy(szFile1, (LPTSTR)lParam1);
  303.     PathFindFileName(szFile1);
  304.     PathRemoveExtension(szFile1);
  305.     PathMakePretty(szFile1);
  306.     lstrcpy(szFile2, (LPTSTR)lParam2);
  307.     PathFindFileName(szFile2);
  308.     PathRemoveExtension(szFile2);
  309.     PathMakePretty(szFile2);
  310.     return lstrcmpi(szFile1, szFile2);
  311. }
  312. void THISCLASS::_OnInitDialog(HWND hwnd)
  313. {
  314.     int i;
  315.     TCHAR szBuf[MAX_PATH];
  316.     
  317.     //
  318.     // Set some member variables.
  319.     //
  320.     _hwnd = hwnd;
  321.     _hwndLV = GetDlgItem(hwnd, IDC_BACK_WPLIST);
  322.     _hwndWPStyle = GetDlgItem(hwnd, IDC_BACK_WPSTYLE);
  323.     HWND hWndPrev = GetDlgItem(hwnd, IDC_BACK_PREVIEW);
  324.     if (hWndPrev) {
  325.         // Turn off mirroring for this control.
  326.         SetWindowBits(hWndPrev, GWL_EXSTYLE, RTL_MIRRORED_WINDOW, 0);
  327.     }
  328.     InitDeskHtmlGlobals();
  329.     if (!g_pActiveDesk)
  330.     {
  331.         HRESULT  hres;
  332.         IActiveDesktopP * piadp;
  333.         if (SUCCEEDED(hres = CActiveDesktop_InternalCreateInstance((LPUNKNOWN *)&piadp, IID_IActiveDesktopP)))
  334.         {
  335.             WCHAR wszScheme[MAX_PATH];
  336.             DWORD dwcch = ARRAYSIZE(wszScheme);
  337.             // Get the global "edit" scheme and set ourselves us to read from and edit that scheme
  338.             if (SUCCEEDED(piadp->GetScheme(wszScheme, &dwcch, SCHEME_GLOBAL | SCHEME_EDIT)))
  339.             {
  340.                 piadp->SetScheme(wszScheme, SCHEME_LOCAL);
  341.                 
  342.             }
  343.             hres = piadp->QueryInterface(IID_IActiveDesktop, (LPVOID *)&g_pActiveDesk);
  344.             piadp->Release();
  345.         }
  346.         if (FAILED(hres))
  347.         {
  348.             return;
  349.         }
  350.     }
  351.     else
  352.     {
  353.         g_pActiveDesk->AddRef();
  354.     }
  355.     //
  356.     // Read in the restrictions.
  357.     //
  358.     _fAllowAD = !SHRestricted(REST_NOACTIVEDESKTOP);
  359.     _fAllowChanges = !SHRestricted(REST_NOCHANGINGWALLPAPER);
  360.     if (_fAllowAD == FALSE)
  361.     {
  362.         _fAllowHtml = FALSE;
  363.     }
  364.     else
  365.     {
  366.         _fAllowHtml = !SHRestricted(REST_NOHTMLWALLPAPER);
  367.     }
  368.     //
  369.     // Get the images into the listview.
  370.     //
  371.     HIMAGELIST himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
  372.         GetSystemMetrics(SM_CYSMICON), ILC_MASK, ARRAYSIZE(c_rgpszWallpaperExt),
  373.         ARRAYSIZE(c_rgpszWallpaperExt));
  374.     if (himl)
  375.     {
  376.         SHFILEINFO sfi;
  377.         //
  378.         // Add the 'None' icon.
  379.         //
  380.         HICON hIconNone = (HICON)LoadImage(HINST_THISDLL, MAKEINTRESOURCE(IDI_BACK_NONE),
  381.             IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
  382.             GetSystemMetrics(SM_CYSMICON), 0);
  383.         ImageList_AddIcon(himl, hIconNone);
  384.         int iPrefixLen = lstrlen(TEXT("foo."));
  385.         lstrcpy(szBuf, TEXT("foo.")); //Pass "foo.bmp" etc., to SHGetFileInfo instead of ".bmp"
  386.         for (i=0; i<ARRAYSIZE(c_rgpszWallpaperExt); i++)
  387.         {
  388.             lstrcpy(szBuf+iPrefixLen, c_rgpszWallpaperExt[i]);
  389.             if (SHGetFileInfo(szBuf, 0, &sfi, SIZEOF(sfi), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES))
  390.             {
  391.                 ImageList_AddIcon(himl, sfi.hIcon);
  392.             }
  393.         }
  394.         ListView_SetImageList(_hwndLV, himl, LVSIL_SMALL);
  395.     }
  396.     //
  397.     // Get the directory with the wallpaper files.
  398.     //
  399.     GetWindowsDirectory(szBuf, ARRAYSIZE(szBuf));
  400.     GetStringFromReg(HKEY_LOCAL_MACHINE, c_szSetup, c_szSharedDir, NULL, szBuf, ARRAYSIZE(szBuf));
  401.     //
  402.     // Add the single column that we want.
  403.     //
  404.     LV_COLUMN lvc;
  405.     lvc.mask = LVCF_FMT | LVCF_SUBITEM;
  406.     lvc.fmt = LVCFMT_LEFT;
  407.     lvc.iSubItem = 0;
  408.     ListView_InsertColumn(_hwndLV, 0, &lvc);
  409.     //
  410.     // Add 'none' option.
  411.     //
  412.     _AddAFileToLV(NULL, g_szNone, 0);
  413.     //
  414.     // Add only the *.BMP files in the windows directory.
  415.     //
  416.     _AddFilesToLV(szBuf, c_rgpszWallpaperExt[0], 1);
  417.     if(_fAllowHtml)
  418.     {
  419.         //
  420.         // Get the wallpaper Directory name
  421.         //
  422.         GetWallpaperDirName(szBuf, ARRAYSIZE(szBuf));
  423.         for (i=0; i<ARRAYSIZE(c_rgpszWallpaperExt); i++)
  424.         {
  425.             // The .htm extension includes the .html files. Hence the .html extension is skipped.
  426.             // The .jpe extension includes the .jpeg files too. So, it .jpeg is skipped too!
  427.             if((lstrcmpi(c_rgpszWallpaperExt[i],TEXT("HTML")) == 0) ||
  428.                (lstrcmpi(c_rgpszWallpaperExt[i],TEXT("JPEG")) == 0))
  429.                 continue;
  430.             _AddFilesToLV(szBuf, c_rgpszWallpaperExt[i], i+1);
  431.         }
  432.     }
  433.     //
  434.     // Sort the standard items.
  435.     //
  436.     ListView_SortItems(_hwndLV, _SortBackgrounds, 0);
  437.     WCHAR   wszBuf[MAX_PATH];
  438.     LPTSTR  pszBuf;
  439.     //
  440.     // Add & select the current setting.
  441.     //
  442.     g_pActiveDesk->GetWallpaper(wszBuf, ARRAYSIZE(wszBuf), 0);
  443.     //Convert wszBuf to szBuf.
  444. #ifndef UNICODE
  445.     SHUnicodeToAnsi(wszBuf, szBuf, ARRAYSIZE(szBuf));
  446.     pszBuf = szBuf;
  447. #else
  448.     pszBuf = (LPTSTR)wszBuf;
  449. #endif
  450.     if (!_fAllowHtml && !IsNormalWallpaper(pszBuf))
  451.     {
  452.         *pszBuf = TEXT('');
  453.     }
  454.     _SetNewWallpaper(pszBuf);
  455.     //
  456.     // Fill and select the style combo.
  457.     // Note: We do NOT add "Stretch" item for Older Operating systems because they
  458.     // had a plus tab that had a "Stretch" check box that interfered with us. In the 
  459.     // new Plus tab, that check box had been removed. So, we support "Stretch" through
  460.     // our background page drop down combo box.
  461.     int iEndStyle;
  462.     iEndStyle = (g_bRunOnNT5 || g_bRunOnMemphis)? WPSTYLE_STRETCH : (WPSTYLE_STRETCH - 1);
  463.     
  464.     for (i=0; i<= iEndStyle; i++)
  465.     {
  466.         MLLoadString(IDS_WPSTYLE+i, szBuf, ARRAYSIZE(szBuf));
  467.         ComboBox_AddString(_hwndWPStyle, szBuf);
  468.     }
  469.     WALLPAPEROPT wpo;
  470.     wpo.dwSize = sizeof(WALLPAPEROPT);
  471.     g_pActiveDesk->GetWallpaperOptions(&wpo, 0);
  472.     ComboBox_SetCurSel(_hwndWPStyle, ((g_bRunOnNT5 || g_bRunOnMemphis) ? wpo.dwStyle : (wpo.dwStyle & WPSTYLE_TILE)));
  473.     //
  474.     // Adjust various UI components.
  475.     //
  476.     if (!_fAllowChanges)
  477.     {
  478.         EnableWindow(GetDlgItem(_hwnd, IDC_BACK_WPSTYLE), FALSE);
  479.         EnableWindow(GetDlgItem(_hwnd, IDC_BACK_BROWSE), FALSE);
  480.         EnableWindow(GetDlgItem(_hwnd, IDC_BACK_PATTERN), FALSE);
  481.         EnableWindow(GetDlgItem(_hwnd, IDC_BACK_WPLIST), FALSE);
  482.     }
  483.     COMPONENTSOPT co;
  484.     co.dwSize = sizeof(COMPONENTSOPT);
  485.     g_pActiveDesk->GetDesktopItemOptions(&co, 0);
  486.     if (!_fAllowAD)
  487.     {
  488.         if (co.fActiveDesktop)
  489.         {
  490.             co.fActiveDesktop = FALSE;
  491.             g_pActiveDesk->SetDesktopItemOptions(&co, 0);
  492.         }
  493.     }
  494.     _EnableControls();
  495. }
  496. // This function checks to see if the currently selected wallpaper is a HTML wallpaper
  497. // and if so, it makes sure that the active desktop is enabled. If it is disabled
  498. // then it prompts the user asking a question to see if the user wants to enable it.
  499. BOOL EnableADifHtmlWallpaper(HWND hwnd)
  500. {
  501.     BOOL    fRet = FALSE;
  502.     IADesktopP2 * piadp2;
  503.     DWORD   dwFlags = 0;
  504.     
  505.     if(FAILED(g_pActiveDesk->QueryInterface(IID_IADesktopP2, (LPVOID *)&piadp2)))
  506.         return(FALSE);
  507.     //See if the ActiveDesktop component is dirty.
  508.     piadp2->GetADObjectFlags(&dwFlags, GADOF_DIRTY);
  509.     //If the object is NOT dirty, that probably means another property sheet has
  510.     // already did the required work. We don't need to re-do it again!
  511.     if(!(dwFlags & GADOF_DIRTY))
  512.     {
  513.         piadp2->Release();
  514.         return(FALSE);      //Nothing to do!
  515.     }
  516.     
  517.     COMPONENTSOPT co;
  518.     co.dwSize = sizeof(COMPONENTSOPT);
  519.     g_pActiveDesk->GetDesktopItemOptions(&co, 0);
  520.     //If the active desktop is currently ON, then we can support any wallpaper!
  521.     if(co.fActiveDesktop)
  522.     {
  523.         //Re-read the wallpaper if needed for active desktop.
  524.         piadp2->ReReadWallpaper();
  525.         //Nothing more to check!
  526.     }
  527.     else
  528.     {
  529.         // Currently the active desktop is turned OFF. See, if we have a suitable
  530.         // wallpaper.
  531.         WCHAR wszWallpaper[INTERNET_MAX_URL_LENGTH];
  532.         LPTSTR pszWallpaper;
  533.         g_pActiveDesk->GetWallpaper(wszWallpaper, ARRAYSIZE(wszWallpaper), 0);
  534. #ifndef UNICODE
  535.         CHAR szWallpaper[INTERNET_MAX_URL_LENGTH];
  536.         SHUnicodeToAnsi(wszWallpaper, szWallpaper, ARRAYSIZE(szWallpaper));
  537.         pszWallpaper = szWallpaper;
  538. #else
  539.         pszWallpaper = (LPTSTR)wszWallpaper;
  540. #endif
  541.         //If this is a normal BMP file. Nothing to check.
  542.         if(!IsNormalWallpaper(pszWallpaper))
  543.         {
  544.             // This wallpaper is either a HTML file or an JPG etc., image.
  545.             // Since the active desktop is currently OFF, ask the user if he wants 
  546.             // to switch ON the active desktop!
  547.             TCHAR szMsg[MAX_PATH];
  548.             TCHAR szTitle[MAX_PATH];
  549.             MLLoadString(IDS_INTERNET_EXPLORER, szTitle, ARRAYSIZE(szTitle));
  550.             MLLoadString(IDS_CONFIRM_TURNINGON_AD, szMsg, ARRAYSIZE(szMsg));
  551.             if(MessageBox(hwnd, szMsg, szTitle, MB_YESNO) != IDNO)
  552.             {
  553.                 //The end-user agreed to turn ON the active desktop.
  554.                 co.fActiveDesktop = TRUE;
  555.                 g_pActiveDesk->SetDesktopItemOptions(&co, 0);
  556.                 fRet = TRUE;
  557.             }
  558.         }
  559.     }
  560.     piadp2->Release();
  561.     return(fRet);
  562. }
  563. void THISCLASS::_OnNotify(LPNMHDR lpnm)
  564. {
  565.     WCHAR   wszBuf[INTERNET_MAX_URL_LENGTH];
  566. #ifndef UNICODE
  567.     CHAR    szSelected[INTERNET_MAX_URL_LENGTH];
  568. #endif
  569.     switch (lpnm->code)
  570.     {
  571.     case PSN_SETACTIVE:
  572.         //
  573.         // Make sure the correct wallpaper is selected.
  574.         //
  575.         LPTSTR  pszBuf;
  576.         g_pActiveDesk->GetWallpaper(wszBuf, ARRAYSIZE(wszBuf), 0);
  577. #ifndef UNICODE
  578.         SHUnicodeToAnsi(wszBuf, szSelected, ARRAYSIZE(szSelected));
  579.         pszBuf = szSelected;
  580. #else
  581.         pszBuf = (LPTSTR)wszBuf;
  582. #endif
  583.         _SetNewWallpaper(pszBuf);
  584.         break;
  585.     case PSN_APPLY:
  586.         {
  587.             EnableADifHtmlWallpaper(_hwnd);
  588.             g_pActiveDesk->ApplyChanges(AD_APPLY_ALL);
  589.             SetWindowLong(_hwnd, DWL_MSGRESULT, PSNRET_NOERROR);
  590.         }
  591.         break;
  592.     case LVN_ITEMCHANGED:
  593.         NM_LISTVIEW *pnmlv = (NM_LISTVIEW *)lpnm;
  594.         if ((pnmlv->uChanged & LVIF_STATE) &&
  595.             (pnmlv->uNewState & LVIS_SELECTED))
  596.         {
  597.             LV_ITEM lvi = {0};
  598.             lvi.iItem = pnmlv->iItem;
  599.             lvi.mask = LVIF_PARAM;
  600.             ListView_GetItem(_hwndLV, &lvi);
  601.             LPCTSTR pszSelectedNew = (LPCTSTR)lvi.lParam;
  602.             LPCTSTR pszCurrent;
  603.             g_pActiveDesk->GetWallpaper(wszBuf, ARRAYSIZE(wszBuf), 0);
  604. #ifndef UNICODE
  605.             SHUnicodeToAnsi(wszBuf, szSelected, ARRAYSIZE(szSelected));
  606.             pszCurrent = szSelected;
  607. #else
  608.             pszCurrent = (LPTSTR)wszBuf;
  609. #endif
  610.             if (lstrcmp(pszSelectedNew, pszCurrent) != 0)
  611.             {
  612.                 _SetNewWallpaper(pszSelectedNew);
  613.                 EnableApplyButton(_hwnd);
  614.             }
  615.         }
  616.         break;
  617.     }
  618. }
  619. void THISCLASS::_OnCommand(WORD wNotifyCode, WORD wID, HWND hwndCtl)
  620. {
  621.     switch (wID)
  622.     {
  623.     case IDC_BACK_WPSTYLE:
  624.         switch (wNotifyCode)
  625.         {
  626.         case CBN_SELCHANGE:
  627.             WALLPAPEROPT wpo;
  628.             wpo.dwSize = sizeof(WALLPAPEROPT);
  629.             g_pActiveDesk->GetWallpaperOptions(&wpo, 0);
  630.             wpo.dwStyle = ComboBox_GetCurSel(_hwndWPStyle);
  631.             g_pActiveDesk->SetWallpaperOptions(&wpo, 0);
  632.             _EnableControls();
  633.             _UpdatePreview(0);
  634.             EnableApplyButton(_hwnd);
  635.             break;
  636.         }
  637.         break;
  638.     case IDC_BACK_BROWSE:
  639.         WCHAR wszFileName[INTERNET_MAX_URL_LENGTH];
  640.         g_pActiveDesk->GetWallpaper(wszFileName, ARRAYSIZE(wszFileName), 0);
  641.         DWORD dwFlags;
  642.         LPTSTR pszFileName;
  643. #ifndef UNICODE
  644.         CHAR  szFileName[INTERNET_MAX_URL_LENGTH];
  645.         SHUnicodeToAnsi(wszFileName, szFileName, ARRAYSIZE(szFileName));
  646.         pszFileName = szFileName;
  647. #else
  648.         pszFileName = (LPTSTR)wszFileName;
  649. #endif
  650.         dwFlags = GFN_PICTURE;
  651.         if (_fAllowHtml)
  652.         {
  653.             SetFlag(dwFlags, GFN_LOCALHTM);
  654.         }
  655.         if (*pszFileName == TEXT(''))
  656.         {
  657.             GetWindowsDirectory(pszFileName, ARRAYSIZE(wszFileName));
  658.             //
  659.             // GetFileName breaks up the string into a directory and file
  660.             // component, so we append a slash to make sure everything
  661.             // is considered part of the directory.
  662.             //
  663.             lstrcat(pszFileName, TEXT("\"));
  664.         }
  665.         if (GetFileName(_hwnd, pszFileName, ARRAYSIZE(wszFileName), IDS_BACK_FILETYPES, dwFlags) &&
  666.             ValidateFileName(_hwnd, pszFileName, IDS_BACK_TYPE1))
  667.         {
  668.             if (_fAllowHtml || IsNormalWallpaper(pszFileName))
  669.             {
  670.                 _SetNewWallpaper(pszFileName);
  671.                 EnableApplyButton(_hwnd);
  672.             }
  673.         }
  674.         break;
  675.     case IDC_BACK_PATTERN:
  676.         if (DialogBox(MLGetHinst(), MAKEINTRESOURCE(IDD_PATTERN), _hwnd, PatternDlgProc) >= 0)
  677.         {
  678.             _UpdatePreview(0);
  679.             EnableApplyButton(_hwnd);
  680.         }
  681.         break;
  682.     }
  683. }
  684. void THISCLASS::_OnDestroy()
  685. {
  686.     if (g_pActiveDesk)
  687.     {
  688.         if(g_pActiveDesk->Release() == 0)
  689.             g_pActiveDesk = NULL;
  690.     }
  691. }
  692. BOOL CALLBACK THISCLASS::_DlgProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  693. {
  694.     CBackPropSheetPage *pbpsp = (CBackPropSheetPage *)GetWindowLong(hdlg, DWL_USER);
  695.     switch(uMsg)
  696.     {
  697.     case WM_INITDIALOG:
  698.         pbpsp = (CBackPropSheetPage *)lParam;
  699.         SetWindowLong(hdlg, DWL_USER, (LPARAM)pbpsp);
  700.         pbpsp->_OnInitDialog(hdlg);
  701.         break;
  702.     case WM_NOTIFY:
  703.         pbpsp->_OnNotify((LPNMHDR)lParam);
  704.         break;
  705.     case WM_COMMAND:
  706.         pbpsp->_OnCommand(HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
  707.         break;
  708.     case WM_SYSCOLORCHANGE:
  709.     case WM_SETTINGCHANGE:
  710.     case WM_DISPLAYCHANGE:
  711.         SHPropagateMessage(hdlg, uMsg, wParam, lParam, TRUE);
  712.         break;
  713.     case WM_HELP:
  714.         SHWinHelpOnDemandWrap((HWND)((LPHELPINFO) lParam)->hItemHandle, c_szHelpFile,
  715.                 HELP_WM_HELP, (DWORD)aBackHelpIDs);
  716.         break;
  717.     case WM_CONTEXTMENU:
  718.         SHWinHelpOnDemandWrap((HWND) wParam, c_szHelpFile, HELP_CONTEXTMENU,
  719.                 (DWORD)(LPVOID) aBackHelpIDs);
  720.         break;
  721.     case WM_DESTROY:
  722.         {
  723.             TCHAR szFileName[MAX_PATH];
  724.             //Delete the tempoaray HTX file created for non-HTML wallpaper preview.
  725.             GetTempPath(ARRAYSIZE(szFileName), szFileName);
  726.             lstrcat(szFileName, PREVIEW_PICTURE_FILENAME);
  727.             DeleteFile(szFileName);
  728.             pbpsp->_OnDestroy();
  729.         }
  730.         break;
  731.     }
  732.     return FALSE;
  733. }
  734. #endif