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

Windows Kernel

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "icwcfg.h"
  3. #pragma hdrstop
  4. EXTERN_C const TCHAR c_szControlIni[] = TEXT("control.ini");
  5. EXTERN_C const TCHAR c_szPatterns[] = TEXT("patterns");
  6. EXTERN_C const TCHAR c_szBackgroundPreview2[] = TEXT("BackgroundPreview2");
  7. EXTERN_C const TCHAR c_szComponentPreview[] = TEXT("ComponentPreview");
  8. EXTERN_C const TCHAR c_szRegDeskHtmlProp[] = TEXT("Software\Microsoft\Windows\CurrentVersion\Controls Folder\Display\shellex\PropertySheetHandlers\DeskHtmlExt");
  9. EXTERN_C const TCHAR c_szWallPaperDir[] = TEXT("WallPaperDir");
  10. //  98/10/01 vtan: Added local function prototypes.
  11. //  Some of these functions are commented out. The linker may not be smart
  12. //  enough to strip the dead code so this is done manually. These prototypes
  13. //  will allow the code to compile but it won't link. If you get linker
  14. //  errors, uncomment the desired function and recompile. It should then link.
  15. //  Point arithmetic
  16. void    SetPt (POINT& pt, LONG x, LONG y);
  17. void    OffsetPt (POINT& pt, LONG dh, LONG dv);
  18. //  Virtual screen calculation
  19. BOOL    CALLBACK    GDIToTridentEnumProc (HMONITOR hMonitor, HDC hDC, RECT* rcMonitor, LPARAM lpUserData);
  20. void    CalculateVirtualScreen (RECT& rcVirtualScreen);
  21. //  GDI point to Trident point co-ordinate mapping
  22. void    GDIToTrident (int& leftCoordinate, int& topCoordinate);
  23. void    GDIToTrident (POINT& pt);
  24. void    GDIToTrident (RECT& r);
  25. void    GDIToTrident (HRGN hRgn);
  26. void    TridentToGDI (int& leftCoordinate, int& topCoordinate);
  27. void    TridentToGDI (POINT& pt);
  28. void    TridentToGDI (RECT& r);
  29. void    TridentToGDI (HRGN hRgn);
  30. //  Component position validation
  31. BOOL    CALLBACK    ValidateComponentPositionEnumProc (HMONITOR hMonitor, HDC hdcMonitor, RECT* r, LPARAM lParam);
  32. void    GetNextComponentPosition (COMPPOS *pcp)
  33. {
  34.     int     iScreenWidth, iScreenHeight, iBorderSize;
  35.     DWORD   dwComponentPosition, dwComponentLayer, dwRegDataScratch;
  36.     HKEY    hKey;
  37.     RECT    rcScreen;
  38.     TCHAR   lpszDeskcomp[MAX_PATH];
  39.     TBOOL(SystemParametersInfo(SPI_GETWORKAREA, 0, &rcScreen, false));
  40.     // 99/04/13 vtan: A result of zero-width or zero-height occurred on a machine.
  41.     // Make a defensive stand against this and assert that this happened but also
  42.     // handle this cause so that division by zero doesn't happen.
  43.     iScreenWidth = rcScreen.right - rcScreen.left;
  44.     iScreenHeight = rcScreen.bottom - rcScreen.top;
  45.     iBorderSize = GetSystemMetrics(SM_CYSMCAPTION);
  46.     ASSERT(iScreenWidth > 0);       // get vtan
  47.     ASSERT(iScreenHeight > 0);      // if any of
  48.     ASSERT(iBorderSize > 0);        // these occur
  49.     if ((iScreenWidth <= 0) || (iScreenHeight <= 0) || (iBorderSize <= 0))
  50.     {
  51.         pcp->iLeft = pcp->iTop = 0;
  52.         pcp->dwWidth = MYCURHOME_WIDTH;
  53.         pcp->dwHeight = MYCURHOME_HEIGHT;
  54.     }
  55.     else
  56.     {
  57.         // Get the number of components positioned. If no such registry key exists
  58.         // or an error occurs then use 0.
  59.         GetRegLocation(lpszDeskcomp, SIZECHARS(lpszDeskcomp), REG_DESKCOMP_GENERAL, NULL);
  60.         dwComponentPosition = 0;
  61.         if (RegCreateKeyEx(HKEY_CURRENT_USER, lpszDeskcomp, 0, NULL, 0, KEY_READ | KEY_WRITE, NULL, &hKey, &dwRegDataScratch) == ERROR_SUCCESS)
  62.         {
  63.             DWORD   regDataSize;
  64.             regDataSize = sizeof(dwComponentPosition);
  65.             TW32(SHQueryValueEx(hKey, REG_VAL_GENERAL_CCOMPPOS, NULL, &dwRegDataScratch, &dwComponentPosition, &regDataSize));
  66.             TW32(RegCloseKey(hKey));
  67.         }
  68.         // Compute the layer we live on (see below).
  69.         dwComponentLayer = dwComponentPosition / (COMPONENT_PER_ROW * COMPONENT_PER_COL);
  70.         if (((dwComponentLayer * iBorderSize) > (DWORD)(iScreenWidth / (COMPONENT_PER_ROW + 1))) ||
  71.             ((dwComponentLayer * iBorderSize) > (DWORD)(iScreenHeight / (COMPONENT_PER_COL + 1))))
  72.         {
  73.             int     iLayerModulo;
  74.             // 99/04/29 vtan: It's possible for SystemParametersInfo(SPI_GETWORKAREA) to
  75.             // return a work area that's small horizontally. Here's a repro scenario for
  76.             // that.
  77.             // 1. Set screen resolution 1280 x 1024.
  78.             // 2. Move the taskbar to the left of the screen.
  79.             // 3. Grow the taskbar to the right until the center of the screen.
  80.             // 4. Open display control panel.
  81.             // 5. Go to "Settings" tab.
  82.             // 6. Change monitor resolution to 640x480.
  83.             // 7. Click either "OK" or "Apply".
  84.             // 8. BOOM - divide zero.
  85.             iLayerModulo = (iScreenWidth / (COMPONENT_PER_ROW + 1) / iBorderSize);
  86.             if (iLayerModulo != 0)
  87.                 dwComponentLayer %= iLayerModulo;
  88.         }
  89.         // Compute the position.  Assuming 3 components per row,
  90.         // and 2 per column, we position components thusly:
  91.         //
  92.         //       +-------+
  93.         //       |x 4 2 0|
  94.         //       |x 5 3 1| <-- screen, divided into 4x3 block coordinates
  95.         //       |x x x x|
  96.         //       +-------+
  97.         //
  98.         // The 6th component sits in a new layer, offset down
  99.         // and to the left of component 0 by the amount iBorder.
  100.         //
  101.         // The first calculation for iLeft and iTop determines the
  102.         // block coordinate value (for instance, component 0 would
  103.         // be at block coordinate value [3,0] and component 1 at [3,1]).
  104.         //
  105.         // The second calculation turns the block coordinate into
  106.         // a screen coordinate.
  107.         //
  108.         // The third calculation adjusts for the border (always down and
  109.         // to the right) and the layers (always down and to the left).
  110.         pcp->iLeft = COMPONENT_PER_ROW - ((dwComponentPosition / COMPONENT_PER_COL) % COMPONENT_PER_ROW); // 3 3 2 2 1 1 3 3 2 2 1 1 ...
  111.         pcp->iLeft *= (iScreenWidth / (COMPONENT_PER_ROW + 1));
  112.         pcp->iLeft += iBorderSize - (dwComponentLayer * iBorderSize);
  113.         pcp->iTop = dwComponentPosition % COMPONENT_PER_COL;  // 0 1 0 1 0 1 ...
  114.         pcp->iTop *= (iScreenHeight / (COMPONENT_PER_COL + 1));
  115.         pcp->iTop += iBorderSize + (dwComponentLayer * iBorderSize);
  116.         pcp->iTop += GET_CYCAPTION;          //vtan: Added this to allow for the title area of the component window
  117.         pcp->dwWidth = (iScreenWidth / (COMPONENT_PER_ROW + 1)) - 2 * iBorderSize;
  118.         pcp->dwHeight = (iScreenHeight / (COMPONENT_PER_COL + 1)) - 2 * iBorderSize;
  119.     }
  120.     if (IS_BIDI_LOCALIZED_SYSTEM())
  121.     {
  122.        pcp->iLeft = iScreenWidth - (pcp->iLeft + pcp->dwWidth);
  123.     }
  124.     
  125. }
  126. void    IncrementComponentsPositioned (void)
  127. {
  128.     DWORD   dwRegDataScratch;
  129.     HKEY    hKey;
  130.     TCHAR   lpszDeskcomp[MAX_PATH];
  131.     // Increment the registry count. If no such count exists create it.
  132.     GetRegLocation(lpszDeskcomp, SIZECHARS(lpszDeskcomp), REG_DESKCOMP_GENERAL, NULL);
  133.     if (RegCreateKeyEx(HKEY_CURRENT_USER, lpszDeskcomp, 0, NULL, 0, KEY_READ | KEY_WRITE, NULL, &hKey, &dwRegDataScratch) == ERROR_SUCCESS)
  134.     {
  135.         DWORD   dwComponentPosition, regDataSize;
  136.         regDataSize = sizeof(dwComponentPosition);
  137.         dwComponentPosition = 0;
  138.         TW32(SHQueryValueEx(hKey, REG_VAL_GENERAL_CCOMPPOS, NULL, &dwRegDataScratch, &dwComponentPosition, &regDataSize));
  139.         ++dwComponentPosition;
  140.         TW32(RegSetValueEx(hKey, REG_VAL_GENERAL_CCOMPPOS, 0, REG_DWORD, reinterpret_cast<unsigned char*>(&dwComponentPosition), sizeof(dwComponentPosition)));
  141.         TW32(RegCloseKey(hKey));
  142.     }
  143. }
  144. //  vtan: Point arithmetic functions. Simple. It may be worth
  145. //  converting these to inline C++ functions or macros if they
  146. //  get used a lot.
  147. void    SetPt (POINT& pt, LONG x, LONG y)
  148. {
  149.     pt.x = x;
  150.     pt.y = y;
  151. }
  152. void    OffsetPt (POINT& pt, LONG dh, LONG dv)
  153. {
  154.     pt.x += dh;
  155.     pt.y += dv;
  156. }
  157. BOOL    CALLBACK    GDIToTridentEnumProc (HMONITOR hMonitor, HDC hDC, RECT* rcMonitor, LPARAM lpUserData)
  158. {
  159.     RECT*   prcNew, rcOld;
  160.     prcNew = reinterpret_cast<RECT*>(lpUserData);
  161.     // Documentation for UnionRect does not specify whether the
  162.     // RECT structures passed must be distinct. To be safe they
  163.     // are passed as distinct structures.
  164.     TBOOL(CopyRect(&rcOld, prcNew));
  165.     TBOOL(UnionRect(prcNew, &rcOld, rcMonitor));
  166.     return(TRUE);
  167. }
  168. void    CalculateVirtualScreen (RECT& rcVirtualScreen)
  169. //  vtan: Calculates the virtual screen in GDI co-ordinates for
  170. //  use in converting co-ordinates from trident scheme to GDI
  171. //  scheme.
  172. {
  173.     TBOOL(SetRectEmpty(&rcVirtualScreen));
  174.     TBOOL(EnumDisplayMonitors(NULL, NULL, GDIToTridentEnumProc, reinterpret_cast<LPARAM>(&rcVirtualScreen)));
  175. }
  176. void    GDIToTrident (int& leftCoordinate, int& topCoordinate)
  177. {
  178.     RECT    rcVirtualScreen;
  179.     CalculateVirtualScreen(rcVirtualScreen);
  180.     leftCoordinate -= rcVirtualScreen.left;
  181.     topCoordinate -= rcVirtualScreen.top;
  182. }
  183. /*
  184. void    GDIToTrident (POINT& pt)
  185. {
  186.     RECT    rcVirtualScreen;
  187.     CalculateVirtualScreen(rcVirtualScreen);
  188.     OffsetPt(pt, -rcVirtualScreen.left, -rcVirtualScreen.top);
  189. }
  190. */
  191. void    GDIToTrident (RECT& rc)
  192. {
  193.     RECT    rcVirtualScreen;
  194.     CalculateVirtualScreen(rcVirtualScreen);
  195.     TBOOL(OffsetRect(&rc, -rcVirtualScreen.left, -rcVirtualScreen.top));
  196. }
  197. void    GDIToTrident (HRGN hRgn)
  198. {
  199.     RECT    rcVirtualScreen;
  200.     CalculateVirtualScreen(rcVirtualScreen);
  201.     TBOOL(OffsetRgn(hRgn, -rcVirtualScreen.left, -rcVirtualScreen.top));
  202. }
  203. /*
  204. void    TridentToGDI (int& leftCoordinate, int& topCoordinate)
  205. {
  206.     RECT    rcVirtualScreen;
  207.     CalculateVirtualScreen(rcVirtualScreen);
  208.     leftCoordinate += rcVirtualScreen.left;
  209.     topCoordinate += rcVirtualScreen.top;
  210. }
  211. */
  212. /*
  213. void    TridentToGDI (POINT& pt)
  214. {
  215.     RECT    rcVirtualScreen;
  216.     CalculateVirtualScreen(rcVirtualScreen);
  217.     OffsetPt(pt, +rcVirtualScreen.left, +rcVirtualScreen.top);
  218. }
  219. */
  220. void    TridentToGDI (RECT& rc)
  221. {
  222.     RECT    rcVirtualScreen;
  223.     CalculateVirtualScreen(rcVirtualScreen);
  224.     TBOOL(OffsetRect(&rc, +rcVirtualScreen.left, +rcVirtualScreen.top));
  225. }
  226. /*
  227. void    TridentToGDI (HRGN hRgn)
  228. {
  229.     RECT    rcVirtualScreen;
  230.     CalculateVirtualScreen(rcVirtualScreen);
  231.     (BOOL)OffsetRgn(hRgn, +rcVirtualScreen.left, +rcVirtualScreen.top);
  232. }
  233. */
  234. //  98/08/14 vtan #196180, #196185: The following code validates
  235. //  a new component's position within the current desktop area. This
  236. //  allows a component to have co-ordinates that seem to be unusual
  237. //  on a single monitor system (such as negative co-ordinates).
  238. class   CRGN
  239. {
  240.     public:
  241.                 CRGN (void)                     {   mRgn = CreateRectRgn(0, 0, 0, 0);                               }
  242.                 CRGN (const RECT& rc)           {   mRgn = CreateRectRgnIndirect(&rc);                              }
  243.                 ~CRGN (void)                    {   TBOOL(DeleteObject(mRgn));                                      }
  244.                 operator HRGN (void)    const   {   return(mRgn);                                                   }
  245.         void    SetRegion (const RECT& rc)      {   TBOOL(SetRectRgn(mRgn, rc.left, rc.top, rc.right, rc.bottom));  }
  246.     private:
  247.         HRGN    mRgn;
  248. };
  249. typedef struct
  250. {
  251.     bool    bAllowEntireDesktopRegion;
  252.     int     iMonitorCount;
  253.     CRGN    hRgn;
  254.     int     iWorkAreaCount;
  255.     RECT    *prcWorkAreaRects;
  256. } tDesktopRegion;
  257. void    ListView_GetWorkAreasAsGDI (HWND hWndListView, int iWorkAreaCount, RECT *prcWorkAreas)
  258. {
  259.     int     i;
  260.     ListView_GetWorkAreas(hWndListView, iWorkAreaCount, prcWorkAreas);
  261.     for (i = 0; i < iWorkAreaCount; ++i)
  262.     {
  263.         TridentToGDI(prcWorkAreas[i]);
  264.     }
  265. }
  266. int     CopyMostSuitableListViewWorkAreaRect (const RECT *pcrcMonitor, int iListViewWorkAreaCount, const RECT *pcrcListViewWorkAreaRects, RECT *prcWorkArea)
  267. {
  268.     int         i, iResult;
  269.     const RECT  *pcrcRects;
  270.     // This function given a rectangle for a GDI monitor (typically the monitor's
  271.     // work area) as well as given the desktop's list view work area rectangle
  272.     // array (obtained by ListView_GetWorkArea()) will search the list view
  273.     // work area array to find a match for the GDI monitor and use the list view
  274.     // work area rectangle instead as this has docked toolbar information which
  275.     // GDI does not have access to.
  276.     // This function works on the principle that the list view rectangle is
  277.     // always a complete subset of the GDI monitor rectangle which is true.
  278.     // The list view rectangle may be smaller but it should never be bigger.
  279.     // It's ok to pass a NULL pcrcListViewWorkAreaRects as long as
  280.     // iListViewWorkAreaCount is 0.
  281.     pcrcRects = pcrcListViewWorkAreaRects;
  282.     iResult = -1;
  283.     i = 0;
  284.     while ((iResult == -1) && (i < iListViewWorkAreaCount))
  285.     {
  286.         RECT    rcIntersection;
  287.         TBOOL(IntersectRect(&rcIntersection, pcrcMonitor, pcrcRects));
  288.         if (EqualRect(&rcIntersection, pcrcRects) != 0)
  289.         {
  290.             iResult = i;
  291.         }
  292.         else
  293.         {
  294.             ++pcrcRects;
  295.             ++i;
  296.         }
  297.     }
  298.     if (iResult < 0)
  299.     {
  300.         TraceMsg(TF_WARNING, "CopyMostSuitableListViewWorkAreaRect() unable to find matching list view rectangle for GDI monitor rectangle");
  301.         TBOOL(CopyRect(prcWorkArea, pcrcMonitor));
  302.     }
  303.     else
  304.     {
  305.         TBOOL(CopyRect(prcWorkArea, &pcrcListViewWorkAreaRects[iResult]));
  306.     }
  307.     return(iResult);
  308. }
  309. BOOL    GetMonitorInfoWithCompensation (int iMonitorCount, HMONITOR hMonitor, MONITORINFO *pMonitorInfo)
  310. {
  311.     BOOL    fResult;
  312.     // 99/05/20 #338585 vtan: Transplanted the logic explained in the
  313.     // comment below for #211510 from GetZoomRect to here so that other
  314.     // functions can share the behavior. Remember that this ONLY applies
  315.     // a single monitor system where there is part of the monitor's
  316.     // rectangle excluded by a docked toolbar on the left or top of the
  317.     // monitor. A very specific case.
  318.     // 98/10/30 #211510 vtan: Oops. If the task bar is at the top of the
  319.     // screen and there is only one monitor then the shell returns a work
  320.     // area starting at (0, 0) instead of (0, 28); the same applies when
  321.     // the task bar is at the left of the screen; this does NOT occur in
  322.     // a multiple monitor setting. In the single monitor case GDI returns
  323.     // a work area starting at (0, 28) so this code checks for the case
  324.     // where there is a single monitor and offsets the GDI information to
  325.     // (0, 0) so that it matches the shell work area which is compared
  326.     // against in the while loop.
  327.     fResult = GetMonitorInfo(hMonitor, pMonitorInfo);
  328.     if ((fResult != 0) && (iMonitorCount == 1))
  329.     {
  330.         TBOOL(OffsetRect(&pMonitorInfo->rcWork, -pMonitorInfo->rcWork.left, -pMonitorInfo->rcWork.top));
  331.     }
  332.     return(fResult);
  333. }
  334. //  MonitorCountEnumProc()'s body is located in adjust.cpp
  335. BOOL    CALLBACK    MonitorCountEnumProc (HMONITOR hMonitor, HDC dc, RECT *rc, LPARAM data);
  336. BOOL    CALLBACK    ValidateComponentPositionEnumProc (HMONITOR hMonitor, HDC hdcMonitor, RECT* prc, LPARAM lpUserData)
  337. {
  338.     HRGN            hRgnDesktop;
  339.     HMONITOR        hMonitorTopLeft, hMonitorTopRight;
  340.     POINT           ptAbove;
  341.     RECT            rcMonitor;
  342.     MONITORINFO     monitorInfo;
  343.     tDesktopRegion  *pDesktopRegion;
  344.     pDesktopRegion = reinterpret_cast<tDesktopRegion*>(lpUserData);
  345.     monitorInfo.cbSize = sizeof(monitorInfo);
  346.     if (GetMonitorInfoWithCompensation(pDesktopRegion->iMonitorCount, hMonitor, &monitorInfo) != 0)
  347.     {
  348.         TINT(CopyMostSuitableListViewWorkAreaRect(&monitorInfo.rcWork, pDesktopRegion->iWorkAreaCount, pDesktopRegion->prcWorkAreaRects, &rcMonitor));
  349.     }
  350.     else
  351.     {
  352.         TBOOL(CopyRect(&rcMonitor, prc));
  353.     }
  354.     // If this monitor does not have a monitor above it then
  355.     // make the monitor rectangle one pixel lower from the
  356.     // top.
  357.     CRGN    hRgnMonitor(rcMonitor);
  358.     if (!pDesktopRegion->bAllowEntireDesktopRegion)
  359.     {
  360.         // This bizarre little algorithm calculates the margins of the current
  361.         // monitor that do not have a monitor above them. The rcExclude is the
  362.         // the final rectangle that contains this information and is one pixel
  363.         // high. This calculation is only valid if the entire desktop region
  364.         // has been DISALLOWED (not zooming a component).
  365.         // Note that the algorithm fails if there is a monitor that is above
  366.         // this one but is contained within the confines of it. For example,
  367.         // this monitor is at 1024x768 and the one above is at 640x480 and
  368.         // centered. In this case it should be possible to drop the component
  369.         // on the exact zero pixel point but this case is disallowed due to
  370.         // this fault. No big deal.
  371.         SetPt(ptAbove, rcMonitor.left, rcMonitor.top - 1);
  372.         hMonitorTopLeft = MonitorFromPoint(ptAbove, MONITOR_DEFAULTTONULL);
  373.         SetPt(ptAbove, rcMonitor.right, rcMonitor.top - 1);
  374.         hMonitorTopRight = MonitorFromPoint(ptAbove, MONITOR_DEFAULTTONULL);
  375.         if ((hMonitorTopLeft == NULL) && (hMonitorTopRight == NULL))
  376.         {
  377.             // No monitor above this one
  378.             ++rcMonitor.top;
  379.             hRgnMonitor.SetRegion(rcMonitor);
  380.         }
  381.         else if (hMonitorTopLeft != hMonitorTopRight)
  382.         {
  383.             RECT    rcExclude;
  384.             // Either one or two different monitors above this one
  385.             // == case is the same monitor completely covers this
  386.             // monitor.
  387.             TBOOL(SetRect(&rcExclude, rcMonitor.left, rcMonitor.top, rcMonitor.right, rcMonitor.top + 1));
  388.             if (hMonitorTopLeft != NULL)
  389.             {
  390.                 TBOOL(GetMonitorInfoWithCompensation(pDesktopRegion->iMonitorCount, hMonitorTopLeft, &monitorInfo));
  391.                 rcExclude.left = monitorInfo.rcWork.right + 1;
  392.             }
  393.             if (hMonitorTopRight != NULL)
  394.             {
  395.                 TBOOL(GetMonitorInfoWithCompensation(pDesktopRegion->iMonitorCount, hMonitorTopRight, &monitorInfo));
  396.                 rcExclude.right = monitorInfo.rcWork.left;
  397.             }
  398.             CRGN    hRgnExclude(rcExclude);
  399.             TINT(CombineRgn(hRgnMonitor, hRgnMonitor, hRgnExclude, RGN_DIFF));
  400.         }
  401.     }
  402.     hRgnDesktop = pDesktopRegion->hRgn;
  403.     TINT(CombineRgn(hRgnDesktop, hRgnDesktop, hRgnMonitor, RGN_OR));
  404.     return(true);
  405. }
  406. void    ValidateComponentPosition (COMPPOS *pcp, DWORD dwComponentState, int iComponentType, bool *pbChangedPosition, bool *pbChangedSize)
  407. {
  408.     bool            bChangedPosition, bChangedSize;
  409.     HRGN            hRgnDesktop;
  410.     HWND            hWndDesktopListView;
  411.     RECT            rcComponent, rcComponentTop;
  412.     tDesktopRegion  desktopRegion;
  413.     COMPPOS         defaultComponentPosition;
  414.     bChangedPosition = bChangedSize = false;
  415.     GetNextComponentPosition(&defaultComponentPosition);
  416.     GDIToTrident(defaultComponentPosition.iLeft, defaultComponentPosition.iTop);
  417.     // If the component has default left or top then give it the next
  418.     // default component position.
  419.     if ((pcp->iLeft == COMPONENT_DEFAULT_LEFT) && (pcp->iTop == COMPONENT_DEFAULT_TOP))
  420.     {
  421.         pcp->iLeft = defaultComponentPosition.iLeft;
  422.         pcp->iTop = defaultComponentPosition.iTop;
  423.         IncrementComponentsPositioned();
  424.         bChangedPosition = true;
  425.     }
  426.     // If the component has default width or height then give it the
  427.     // next default component size unless it is type COMP_TYPE_PICTURE
  428.     // 98/10/02 #222449 vtan: Only change the size of an unpositioned
  429.     // component if it's not a picture.
  430.     if ((pcp->dwWidth == COMPONENT_DEFAULT_WIDTH) && (pcp->dwHeight == COMPONENT_DEFAULT_HEIGHT) && (iComponentType != COMP_TYPE_PICTURE))
  431.     {
  432.         pcp->dwWidth = defaultComponentPosition.dwWidth;
  433.         pcp->dwHeight = defaultComponentPosition.dwHeight;
  434.         bChangedSize = false;
  435.     }
  436.     // Make sure that the top line of the component is visible or at
  437.     // least one pixel below the top most part of a virtual screen.
  438.     // Check to see if the component has a negative width and height or
  439.     // a width and height that is too small. The only exception to this
  440.     // is if the component is a picture.
  441.     desktopRegion.bAllowEntireDesktopRegion = IsZoomedState(dwComponentState);
  442.     if (iComponentType != COMP_TYPE_PICTURE)
  443.     {
  444.         if (static_cast<int>(pcp->dwWidth) < 10)
  445.         {
  446.             pcp->dwWidth = defaultComponentPosition.dwWidth;
  447.             bChangedSize = false;
  448.         }
  449.         if (static_cast<int>(pcp->dwHeight) < 10)
  450.         {
  451.             pcp->dwHeight= defaultComponentPosition.dwHeight;
  452.             bChangedSize = false;
  453.         }
  454.     }
  455.     TBOOL(SetRect(&rcComponent, pcp->iLeft, pcp->iTop, pcp->iLeft + pcp->dwWidth, pcp->iTop + pcp->dwHeight));
  456.     TBOOL(CopyRect(&rcComponentTop, &rcComponent));
  457.     rcComponentTop.bottom = rcComponentTop.top + 1;
  458.     // Before calculating the desktopRegion as a region by using GDI calls
  459.     // get the List View work area which will have information about docked
  460.     // toolbars in addition to the taskbar which is the only thing that GDI
  461.     // has. This will allow this function to invalidate regions occupied by
  462.     // toolbars also.
  463.     desktopRegion.iWorkAreaCount = 0;
  464.     desktopRegion.prcWorkAreaRects = NULL;
  465.     hWndDesktopListView = FindWindowEx(GetWindow(GetShellWindow(), GW_CHILD), NULL, WC_LISTVIEW, NULL);
  466.     if (hWndDesktopListView != NULL)
  467.     {
  468.         DWORD   dwProcessID;
  469.         GetWindowThreadProcessId(hWndDesktopListView, &dwProcessID);
  470.         if (GetCurrentProcessId() == dwProcessID)
  471.         {
  472.             ListView_GetNumberOfWorkAreas(hWndDesktopListView, &desktopRegion.iWorkAreaCount);
  473.             desktopRegion.prcWorkAreaRects = reinterpret_cast<RECT*>(LocalAlloc(GPTR, desktopRegion.iWorkAreaCount * sizeof(desktopRegion.prcWorkAreaRects[0])));
  474.             ListView_GetWorkAreasAsGDI(hWndDesktopListView, desktopRegion.iWorkAreaCount, desktopRegion.prcWorkAreaRects);
  475.         }
  476.     }
  477.     CRGN    hRgnComponentTop(rcComponentTop), hRgnResult;
  478.     desktopRegion.iMonitorCount = 0;
  479.     TBOOL(EnumDisplayMonitors(NULL, NULL, MonitorCountEnumProc, reinterpret_cast<LPARAM>(&desktopRegion.iMonitorCount)));
  480.     TBOOL(EnumDisplayMonitors(NULL, NULL, ValidateComponentPositionEnumProc, reinterpret_cast<LPARAM>(&desktopRegion)));
  481.     hRgnDesktop = desktopRegion.hRgn;
  482.     GDIToTrident(hRgnDesktop);
  483.     // 99/03/23 #266412 vtan: Make sure that the top pixel of the component is within
  484.     // the visible desktop. This allows the deskmovr to be positioned over the
  485.     // component and therefore allows it to be moved. If the deskmovr cannot be
  486.     // positioned over it then "snap" the component back into the visible region
  487.     // to a maximum best fit algorithm.
  488.     if (CombineRgn(hRgnResult, hRgnDesktop, hRgnComponentTop, RGN_AND) == NULLREGION)
  489.     {
  490.         LONG        lDeltaX, lDeltaY;
  491.         HMONITOR    hMonitorNearest;
  492.         RECT        rcComponentGDI, rcMonitorWork, rcIntersection;
  493.         MONITORINFO monitorInfo;
  494.         TBOOL(CopyRect(&rcComponentGDI, &rcComponent));
  495.         TridentToGDI(rcComponentGDI);
  496.         hMonitorNearest = MonitorFromRect(&rcComponentGDI, MONITOR_DEFAULTTONEAREST);
  497.         ASSERT(hMonitorNearest != NULL);
  498.         monitorInfo.cbSize = sizeof(monitorInfo);
  499.         TBOOL(GetMonitorInfoWithCompensation(desktopRegion.iMonitorCount, hMonitorNearest, &monitorInfo));
  500.         TINT(CopyMostSuitableListViewWorkAreaRect(&monitorInfo.rcWork, desktopRegion.iWorkAreaCount, desktopRegion.prcWorkAreaRects, &rcMonitorWork));
  501.         ++rcMonitorWork.top;
  502.         lDeltaX = lDeltaY = 0;
  503.         if (rcComponentGDI.left < rcMonitorWork.left)
  504.             lDeltaX = rcMonitorWork.left - rcComponentGDI.left;
  505.         if (rcComponentGDI.top < rcMonitorWork.top)
  506.             lDeltaY = rcMonitorWork.top - rcComponentGDI.top;
  507.         if (rcComponentGDI.right > rcMonitorWork.right)
  508.             lDeltaX = rcMonitorWork.right - rcComponentGDI.right;
  509.         if (rcComponentGDI.bottom > rcMonitorWork.bottom)
  510.             lDeltaY = rcMonitorWork.bottom - rcComponentGDI.bottom;
  511.         TBOOL(OffsetRect(&rcComponentGDI, lDeltaX, lDeltaY));
  512.         TBOOL(IntersectRect(&rcIntersection, &rcComponentGDI, &rcMonitorWork));
  513.         GDIToTrident(rcIntersection);
  514.         pcp->iLeft = rcIntersection.left;
  515.         pcp->iTop = rcIntersection.top;
  516.         pcp->dwWidth = rcIntersection.right - rcIntersection.left;
  517.         pcp->dwHeight = rcIntersection.bottom - rcIntersection.top;
  518.         bChangedPosition = bChangedSize = true;
  519.     }
  520.     if (desktopRegion.prcWorkAreaRects != NULL)
  521.         LocalFree(desktopRegion.prcWorkAreaRects);
  522.     if (pbChangedPosition != NULL)
  523.         *pbChangedPosition = bChangedPosition;
  524.     if (pbChangedSize != NULL)
  525.         *pbChangedSize = bChangedSize;
  526. }
  527. //  98/12/11 #250938 vtan: these two functions are lifted from
  528. //  SHBrows2.cpp which is part of browseui.dll.
  529. EXTERN_C    DWORD   WINAPI  IsSmartStart (void);
  530. BOOL    IsICWCompleted (void)
  531. {
  532.     DWORD   dwICWCompleted, dwICWSize;
  533.     dwICWCompleted = 0;
  534.     dwICWSize = sizeof(dwICWCompleted);
  535.     TW32(SHGetValue(HKEY_CURRENT_USER, TEXT(ICW_REGPATHSETTINGS), TEXT(ICW_REGKEYCOMPLETED), NULL, &dwICWCompleted, &dwICWSize));
  536.     // 99/01/15 #272829 vtan: This is a horrible hack!!! If ICW has
  537.     // not been run but settings have been made manually then values
  538.     // in HKCUSoftwareMicrosoftWindowsCurrentVersionInternet SettingsConnections
  539.     // exists with the values given. Look for the presence of a key
  540.     // to resolve that settings are present but that ICW hasn't been
  541.     // launched.
  542.     // The ideal solution is to get ICW to make this determination
  543.     // for us BUT TO NOT LAUNCH ICWCONN1.EXE IN THE PROCESS.
  544.     // Currently it will only launch. There is no way to get the
  545.     // desired result without a launch.
  546.     // 99/02/01 #280138 vtan: Well the solution put in for #272829
  547.     // doesn't work. So peeking at the CheckConnectionWizard()
  548.     // source in inetcfgexport.cpp shows that it uses a
  549.     // wininet.dll function to determine whether manually configured
  550.     // internet settings exist. It also exports this function so
  551.     // look for it and bind to it dynamically. This uses the
  552.     // DELAY_LOAD macros in dllload.c
  553.     if (dwICWCompleted == 0)
  554.     {
  555.         #define SMART_RUNICW    TRUE
  556.         #define SMART_QUITICW   FALSE
  557.         dwICWCompleted = BOOLIFY(IsSmartStart() == SMART_QUITICW);
  558.     }
  559.     return(dwICWCompleted != 0);
  560. }
  561. void    LaunchICW (void)
  562. {
  563.     static  bool    sbCheckedICW = false;
  564.     if (!sbCheckedICW && !IsICWCompleted())
  565.     {
  566.         HINSTANCE   hICWInst;
  567.         // Prevent an error in finding the ICW from causing this to
  568.         // execute again and again and again.
  569.         sbCheckedICW = true;
  570.         hICWInst = LoadLibrary(TEXT("inetcfg.dll"));
  571.         if (hICWInst != NULL)
  572.         {
  573.             PFNCHECKCONNECTIONWIZARD    pfnCheckConnectionWizard;
  574.             pfnCheckConnectionWizard = reinterpret_cast<PFNCHECKCONNECTIONWIZARD>(GetProcAddress(hICWInst, "CheckConnectionWizard"));
  575.             if (pfnCheckConnectionWizard != NULL)
  576.             {
  577.                 DWORD   dwICWResult;
  578.                 // If the user cancels ICW then it needs to be launched
  579.                 // again. Allow this case.
  580.                 sbCheckedICW = false;
  581.                 pfnCheckConnectionWizard(ICW_LAUNCHFULL | ICW_LAUNCHMANUAL, &dwICWResult);
  582.             }
  583.             TBOOL(FreeLibrary(hICWInst));
  584.         }
  585.     }
  586. }
  587. BOOL    IsLocalPicture (LPCTSTR pszURL)
  588. {
  589.     return(!PathIsURL(pszURL) && IsUrlPicture(pszURL));
  590. }
  591. BOOL    DisableUndisplayableComponents (IActiveDesktop *pIAD)
  592. {
  593.     bool    bHasVisibleNonLocalPicture;
  594.     int     iItemCount;
  595.     // 98/12/16 vtan #250938: If ICW has not been run to completion then only
  596.     // allow the user to show components that are local pictures of some sort.
  597.     // If any components are not local pictures then hide these components,
  598.     // tell the user why it happened and launch ICW.
  599.     bHasVisibleNonLocalPicture = false;
  600.     if (SUCCEEDED(pIAD->GetDesktopItemCount(&iItemCount, 0)))
  601.     {
  602.         int     i;
  603.         for (i = 0; i < iItemCount; ++i)
  604.         {
  605.             COMPONENT   component;
  606.             component.dwSize = sizeof(component);
  607.             if (SUCCEEDED(pIAD->GetDesktopItem(i, &component, 0)) && (component.fChecked != 0))
  608.             {
  609.                bool    bIsVisibleNonLocalPicture;
  610.                TCHAR   szComponentSource[INTERNET_MAX_URL_LENGTH];
  611.                SHUnicodeToTChar(component.wszSource, szComponentSource, ARRAYSIZE(szComponentSource));
  612.                bIsVisibleNonLocalPicture = !IsLocalPicture(szComponentSource);
  613.                bHasVisibleNonLocalPicture = bHasVisibleNonLocalPicture || bIsVisibleNonLocalPicture;
  614.                if (bIsVisibleNonLocalPicture)
  615.                {
  616.                    component.fChecked = FALSE;
  617.                    THR(pIAD->ModifyDesktopItem(&component, COMP_ELEM_CHECKED));
  618.                }
  619.             }
  620.          }
  621.     }
  622.     if (bHasVisibleNonLocalPicture)
  623.     {
  624.         // Apply the changes. This should recurse to CActiveDesktop::_SaveSettings()
  625.         // but this code path is NOT taken because AD_APPLY_REFRESH is not passed in.
  626.         // CActiveDesktop::_SaveSettings() calls this function!
  627.         bHasVisibleNonLocalPicture = FAILED(pIAD->ApplyChanges(AD_APPLY_SAVE | AD_APPLY_HTMLGEN));
  628.         // Notify the user what happened and launch ICW.
  629.         ShellMessageBox(HINST_THISDLL, NULL, MAKEINTRESOURCE(IDS_COMP_ICW_DISABLE), MAKEINTRESOURCE(IDS_COMP_ICW_TITLE), MB_OK);
  630.         LaunchICW();
  631.     }
  632.     return(bHasVisibleNonLocalPicture);
  633. }
  634. int GetIconCountForWorkArea(HWND hwndLV, LPCRECT prect, int crect, int iWorkAreaIndex)
  635. {
  636.     int iCount;
  637.     iCount = ListView_GetItemCount(hwndLV);
  638.     if (crect > 1) 
  639.     {
  640.         int i, iCountWorkArea = 0;
  641.         for (i = 0; i < iCount; i++)
  642.         {
  643.             POINT pt;
  644.             ListView_GetItemPosition(hwndLV, i, &pt);
  645.             if (iWorkAreaIndex == GetWorkAreaIndexFromPoint(pt, prect, crect))
  646.                 iCountWorkArea++;
  647.         }
  648.         iCount = iCountWorkArea;
  649.     }
  650.     return iCount;
  651. }
  652. BOOL GetZoomRect(BOOL fFullScreen, BOOL fAdjustListview, int iTridentLeft, int iTridentTop, DWORD dwComponentWidth, DWORD dwComponentHeight, LPRECT prcZoom, LPRECT prcWork)
  653. {
  654.     HWND hwndLV = FindWindowEx(GetWindow(GetShellWindow(), GW_CHILD), NULL, WC_LISTVIEW, NULL);
  655.     int icWorkAreas = 0, iWAC;
  656.     RECT rcWork[LV_MAX_WORKAREAS];
  657.     //
  658.     // First calculate the Work Areas and Work Area index for the component, then perform the
  659.     // particular operation based on lCommand.
  660.     //
  661.     if (hwndLV) {
  662.         DWORD dwpid;
  663.         GetWindowThreadProcessId(hwndLV, &dwpid);
  664.         // This sucks, but the listview doesn't thunk these messages so we can't do
  665.         // this inter-process!
  666.         if (dwpid == GetCurrentProcessId())
  667.         {
  668.             ListView_GetNumberOfWorkAreas(hwndLV, &icWorkAreas);
  669.             if (icWorkAreas <= LV_MAX_WORKAREAS)
  670.                 ListView_GetWorkAreas(hwndLV, icWorkAreas, &rcWork);
  671.             else
  672.                 hwndLV = NULL;
  673.         } else {
  674.             return FALSE;
  675.         }
  676.     }
  677.     // 98/10/07 vtan: This used to use a variable icWorkAreasAdd.
  678.     // Removed this variable and directly increment icWorkAreas.
  679.     // This doesn't affect the call to ListView_SetWorkAreas()
  680.     // below because in this case hwndLV is NULL.
  681.     if (icWorkAreas == 0)
  682.     {
  683.         RECT rc;
  684.         ++icWorkAreas;
  685.         SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&rc, 0);
  686.         rcWork[0] = rc;
  687.         hwndLV = NULL;
  688.     }
  689.     // 98/10/02 #212654 vtan: Changed the calculation code to find a
  690.     // rectangle to zoom the component to based on GDI co-ordinates.
  691.     // The component is passed in trident co-ordinates which are
  692.     // stored in a RECT and converted to GDI co-ordinates. The system
  693.     // then locates the monitor which the component is on and if it
  694.     // cannot find the monitor then defaults to the primary. The
  695.     // dimensions of the monitor are used before converting back to
  696.     // trident co-ordinates.
  697.     int             i, iMonitorCount;
  698.     HMONITOR        hMonitor;
  699.     RECT            rcComponentRect;
  700.     MONITORINFO     monitorInfo;
  701.     iMonitorCount = 0;
  702.     TBOOL(EnumDisplayMonitors(NULL, NULL, MonitorCountEnumProc, reinterpret_cast<LPARAM>(&iMonitorCount)));
  703.     TBOOL(SetRect(&rcComponentRect, iTridentLeft, iTridentTop, iTridentLeft + dwComponentWidth, iTridentTop + dwComponentHeight));
  704.     TridentToGDI(rcComponentRect);
  705.     hMonitor = MonitorFromRect(&rcComponentRect, MONITOR_DEFAULTTOPRIMARY);
  706.     ASSERT(hMonitor != NULL);
  707.     monitorInfo.cbSize = sizeof(monitorInfo);
  708.     TBOOL(GetMonitorInfoWithCompensation(iMonitorCount, hMonitor, &monitorInfo));
  709.     GDIToTrident(monitorInfo.rcWork);
  710.     // 99/05/19 #340772 vtan: Always try to key off work areas returned
  711.     // by ListView_GetWorkAreas because these take into account docked
  712.     // toolbars which GDI does not. In this case the listview work areas
  713.     // will always be the same rectangle when intersected with the GDI
  714.     // work area. Use this rule to determine which listview work area
  715.     // to use as the basis for the zoom rectangle.
  716.     i = CopyMostSuitableListViewWorkAreaRect(&monitorInfo.rcWork, icWorkAreas, rcWork, prcZoom);
  717.     if (i < 0)
  718.     {
  719.         i = 0;
  720.     }
  721.     if (prcWork != NULL)
  722.     {
  723.         TBOOL(CopyRect(prcWork, prcZoom));
  724.     }
  725.     iWAC = i;
  726.     if (!fFullScreen)
  727.     {
  728.         // For the split case we shrink the work area down temporarily to the smallest rectangle
  729.         // that can bound the current number of icons.  This will force the icons into that rectangle,
  730.         // then restore it back to the way it was before.  Finally, we set the size of the split
  731.         // component to fill the rest of the space.
  732.         if (hwndLV) {
  733.             int iCount, iItemsPerColumn, icxWidth, iRightOld;
  734.             DWORD dwSpacing;
  735.             iCount = GetIconCountForWorkArea(hwndLV, rcWork, icWorkAreas, iWAC);
  736.             // Decrement the count so that rounding works right
  737.             if (iCount)     
  738.                 iCount--;
  739.             // Calculate the new width for the view rectangle
  740.             dwSpacing = ListView_GetItemSpacing(hwndLV, FALSE);
  741.             iItemsPerColumn = (rcWork[iWAC].bottom - rcWork[iWAC].top) / (HIWORD(dwSpacing));
  742.             if (iItemsPerColumn)
  743.                 icxWidth = ((iCount / iItemsPerColumn) + 1) * (LOWORD(dwSpacing));
  744.             else
  745.                 icxWidth = LOWORD(dwSpacing);
  746.             // Don't let it get smaller than half the screen
  747.             if (icxWidth > ((rcWork[iWAC].right - rcWork[iWAC].left) / 2))
  748.                 icxWidth = (rcWork[iWAC].right - rcWork[iWAC].left) / 2;
  749.             if (fAdjustListview)
  750.             {
  751.                 // Now take the old work area rectangle and shrink it to our new width
  752.                 iRightOld = rcWork[iWAC].right;
  753.                 rcWork[iWAC].right = rcWork[iWAC].left + icxWidth;
  754.                 ListView_SetWorkAreas(hwndLV, icWorkAreas, &rcWork);
  755.                 // Finally restore the old work area
  756.                 rcWork[iWAC].right = iRightOld;
  757.                 ListView_SetWorkAreas(hwndLV, icWorkAreas, &rcWork);
  758.             }
  759.             // Adjust the left coordinate of the zoom rect to reflect our calculated split amount
  760.             // the rest of the screen.
  761.             prcZoom->left += icxWidth;
  762.         } else {
  763.             // Fallback case, if there is no listview use 20% of the screen for the icons.
  764.             prcZoom->left += ((prcZoom->right - prcZoom->left) * 2 / 10);
  765.         }
  766.     }
  767.     return TRUE;
  768. }
  769. void ZoomComponent(COMPPOS * pcp, DWORD dwCurItemState, BOOL fAdjustListview)
  770. {
  771.     RECT rcZoom;
  772.     if (GetZoomRect((dwCurItemState & IS_FULLSCREEN), fAdjustListview, pcp->iLeft, pcp->iTop, pcp->dwWidth, pcp->dwHeight, &rcZoom, NULL))
  773.     {
  774.         // Copy the new Zoom rectangle over and put it on the bottom
  775.         pcp->iLeft = rcZoom.left;
  776.         pcp->iTop = rcZoom.top;
  777.         pcp->dwWidth = rcZoom.right - rcZoom.left;
  778.         pcp->dwHeight = rcZoom.bottom - rcZoom.top;
  779.         pcp->izIndex = 0;
  780.     }
  781.     else
  782.     {
  783.         // Failure implies we couldn't get the zoom rectangle through inter-process calls.  Set the
  784.         // COMPONENTS_ZOOMDIRTY bit here so that when the desktop is refreshed we will recalculate
  785.         // the zoom rectangles in-process inside of EnsureUpdateHTML.
  786.         SetDesktopFlags(COMPONENTS_ZOOMDIRTY, COMPONENTS_ZOOMDIRTY);
  787.     }
  788. }
  789. //
  790. // PositionComponent will assign a screen position and
  791. // make sure it fits on the screen.
  792. //
  793. void PositionComponent(COMPONENTA *pcomp, COMPPOS *pcp, int iCompType, BOOL fCheckItemState)
  794. {
  795. //  vtan: Vastly simplified routine. The work is now done in
  796. //  ValidateComponentPosition.
  797.     if (ISZOOMED(pcomp))
  798.     {
  799.         if (fCheckItemState)
  800.         {
  801.             SetStateInfo(&pcomp->csiRestored, pcp, IS_NORMAL);
  802.             SetStateInfo(&pcomp->csiOriginal, pcp, pcomp->dwCurItemState);
  803.         }
  804.         ZoomComponent(pcp, pcomp->dwCurItemState, FALSE);
  805.     }
  806.     else
  807.     {
  808.         ValidateComponentPosition(pcp, pcomp->dwCurItemState, iCompType, NULL, NULL);
  809.         if (fCheckItemState)
  810.             SetStateInfo(&pcomp->csiOriginal, pcp, pcomp->dwCurItemState);
  811.     }
  812. }
  813. typedef struct _tagFILETYPEENTRY {
  814.     DWORD dwFlag;
  815.     int iFilterId;
  816. } FILETYPEENTRY;
  817. FILETYPEENTRY afte[] = {
  818.     { GFN_URL, IDS_URL_FILTER, },
  819.     { GFN_CDF, IDS_CDF_FILTER, },
  820.     { GFN_LOCALHTM, IDS_HTMLDOC_FILTER, },
  821.     { GFN_PICTURE,  IDS_IMAGES_FILTER, },
  822.     { GFN_LOCALMHTML, IDS_MHTML_FILTER, },
  823. };
  824. //
  825. // Opens either an HTML page or a picture.
  826. //
  827. BOOL GetFileName(HWND hdlg, LPTSTR pszFileName, int iSize, int iTypeId[], DWORD dwFlags[])
  828. {
  829.     BOOL fRet = FALSE;
  830.     if (dwFlags)
  831.     {
  832.         int i, iIndex, cchRead;
  833.         TCHAR szFilter[MAX_PATH*4];
  834.         //
  835.         // Set the friendly name.
  836.         //
  837.         LPTSTR pchFilter = szFilter;
  838.         int cchFilter = ARRAYSIZE(szFilter) - 2;    // room for term chars
  839.         for(iIndex = 0; dwFlags[iIndex]; iIndex++)
  840.         {
  841.             cchRead = LoadString(HINST_THISDLL, iTypeId[iIndex], pchFilter, cchFilter);
  842.             pchFilter += cchRead + 1;
  843.             cchFilter -= cchRead + 1;
  844.             //
  845.             // Append the file filters.
  846.             //
  847.             BOOL fAddedToString = FALSE;
  848.             for (i=0; (cchFilter>0) && (i<ARRAYSIZE(afte)); i++)
  849.             {
  850.                 if (dwFlags[iIndex] & afte[i].dwFlag)
  851.                 {
  852.                     if (fAddedToString)
  853.                     {
  854.                         *pchFilter++ = TEXT(';');
  855.                         cchFilter--;
  856.                     }
  857.                     cchRead = LoadString(HINST_THISDLL, afte[i].iFilterId,
  858.                                      pchFilter, cchFilter);
  859.                     pchFilter += cchRead;
  860.                     cchFilter -= cchRead;
  861.                     fAddedToString = TRUE;
  862.                 }
  863.             }
  864.             *pchFilter++ = TEXT('');
  865.         }
  866.         //
  867.         // Double-NULL terminate the string.
  868.         //
  869.         *pchFilter = TEXT('');
  870.         TCHAR szBrowserDir[MAX_PATH];
  871.         lstrcpy(szBrowserDir, pszFileName);
  872.         PathRemoveArgs(szBrowserDir);
  873.         PathRemoveFileSpec(szBrowserDir);
  874.         TCHAR szBuf[MAX_PATH];
  875.         LoadString(HINST_THISDLL, IDS_BROWSE, szBuf, ARRAYSIZE(szBuf));
  876.         *pszFileName = TEXT('');
  877.         OPENFILENAME ofn;
  878.         ofn.lStructSize       = SIZEOF(ofn);
  879.         ofn.hwndOwner         = hdlg;
  880.         ofn.hInstance         = NULL;
  881.         ofn.lpstrFilter       = szFilter;
  882.         ofn.lpstrCustomFilter = NULL;
  883.         ofn.nFilterIndex      = 1;
  884.         ofn.nMaxCustFilter    = 0;
  885.         ofn.lpstrFile         = pszFileName;
  886.         ofn.nMaxFile          = iSize;
  887.         ofn.lpstrInitialDir   = szBrowserDir;
  888.         ofn.lpstrTitle        = szBuf;
  889.         ofn.Flags             = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_NODEREFERENCELINKS;
  890.         ofn.lpfnHook          = NULL;
  891.         ofn.lpstrDefExt       = NULL;
  892.         ofn.lpstrFileTitle    = NULL;
  893.         fRet = GetOpenFileName(&ofn);
  894.     }
  895.     return fRet;
  896. }
  897. //
  898. // Convert a pattern string to a bottom-up array of DWORDs,
  899. // useful for BMP format files.
  900. //
  901. void PatternToDwords(LPTSTR psz, DWORD *pdwBits)
  902. {
  903.     DWORD i, dwVal;
  904.     //
  905.     // Get eight groups of numbers separated by non-numeric characters.
  906.     //
  907.     for (i=0; i<8; i++)
  908.     {
  909.         dwVal = 0;
  910.         if (*psz != TEXT(''))
  911.         {
  912.             //
  913.             // Skip over any non-numeric characters.
  914.             //
  915.             while (*psz && (!(*psz >= TEXT('0') && *psz <= TEXT('9'))))
  916.             {
  917.                 psz++;
  918.             }
  919.             //
  920.             // Get the next series of digits.
  921.             //
  922.             while (*psz && (*psz >= TEXT('0') && *psz <= TEXT('9')))
  923.             {
  924.                 dwVal = dwVal*10 + *psz++ - TEXT('0');
  925.             }
  926.         }
  927.         pdwBits[7-i] = dwVal;
  928.     }
  929. }
  930. //
  931. // Convert a pattern string to a top-down array of WORDs,
  932. // useful for CreateBitmap().
  933. //
  934. void PatternToWords(LPTSTR psz, WORD *pwBits)
  935. {
  936.     WORD i, wVal;
  937.     //
  938.     // Get eight groups of numbers separated by non-numeric characters.
  939.     //
  940.     for (i=0; i<8; i++)
  941.     {
  942.         wVal = 0;
  943.         if (*psz != TEXT(''))
  944.         {
  945.             //
  946.             // Skip over any non-numeric characters.
  947.             //
  948.             while (*psz && (!(*psz >= TEXT('0') && *psz <= TEXT('9'))))
  949.             {
  950.                 psz++;
  951.             }
  952.             //
  953.             // Get the next series of digits.
  954.             //
  955.             while (*psz && ((*psz >= TEXT('0') && *psz <= TEXT('9'))))
  956.             {
  957.                 wVal = wVal*10 + *psz++ - TEXT('0');
  958.             }
  959.         }
  960.         pwBits[i] = wVal;
  961.     }
  962. }
  963. BOOL IsValidPattern(LPCTSTR pszPat)
  964. {
  965.     BOOL fSawANumber = FALSE;
  966.     //
  967.     // We're mainly trying to filter multilingual upgrade cases
  968.     // where the text for "(None)" is unpredictable.
  969.     // Yes, I want to strangle the bastard who decided to
  970.     // write out the none entry.
  971.     //
  972.     while (*pszPat)
  973.     {
  974.         if ((*pszPat < TEXT('0')) || (*pszPat > TEXT('9')))
  975.         {
  976.             //
  977.             // It's not a number, it better be a space.
  978.             //
  979.             if (*pszPat != TEXT(' '))
  980.             {
  981.                 return FALSE;
  982.             }
  983.         }
  984.         else
  985.         {
  986.             fSawANumber = TRUE;
  987.         }
  988.         //
  989.         // We avoid the need for AnsiNext by only advancing on US TCHARs.
  990.         //
  991.         pszPat++;
  992.     }
  993.     //
  994.     // TRUE if we saw at least one digit and there were only digits and spaces.
  995.     //
  996.     return fSawANumber;
  997. }
  998. //
  999. // Determines if the wallpaper can be supported in non-active desktop mode.
  1000. //
  1001. BOOL IsNormalWallpaper(LPCTSTR pszFileName)
  1002. {
  1003.     BOOL fRet = TRUE;
  1004.     if (pszFileName[0] == TEXT(''))
  1005.     {
  1006.         fRet = TRUE;
  1007.     }
  1008.     else
  1009.     {
  1010.         LPTSTR pszExt = PathFindExtension(pszFileName);
  1011.         //Check for specific files that can be shown only in ActiveDesktop mode!
  1012.         if((lstrcmpi(pszExt, TEXT(".GIF")) == 0) ||
  1013.            (lstrcmpi(pszExt, TEXT(".JPG")) == 0) ||
  1014.            (lstrcmpi(pszExt, TEXT(".JPE")) == 0) ||
  1015.            (lstrcmpi(pszExt, TEXT(".JPEG")) == 0) ||
  1016.            (lstrcmpi(pszExt, TEXT(".PNG")) == 0) ||
  1017.            (lstrcmpi(pszExt, TEXT(".HTM")) == 0) ||
  1018.            (lstrcmpi(pszExt, TEXT(".HTML")) == 0) ||
  1019.            (lstrcmpi(pszExt, TEXT(".HTT")) == 0))
  1020.            return FALSE;
  1021.         //Everything else (including *.BMP files) are "normal" wallpapers
  1022.     }
  1023.     return fRet;
  1024. }
  1025. //
  1026. // Determines if the wallpaper is a picture (vs. HTML).
  1027. //
  1028. BOOL IsWallpaperPicture(LPCTSTR pszWallpaper)
  1029. {
  1030.     BOOL fRet = TRUE;
  1031.     if (pszWallpaper[0] == TEXT(''))
  1032.     {
  1033.         //
  1034.         // Empty wallpapers count as empty pictures.
  1035.         //
  1036.         fRet = TRUE;
  1037.     }
  1038.     else
  1039.     {
  1040.         LPTSTR pszExt = PathFindExtension(pszWallpaper);
  1041.         if ((lstrcmpi(pszExt, TEXT(".HTM")) == 0) ||
  1042.             (lstrcmpi(pszExt, TEXT(".HTML")) == 0) ||
  1043.             (lstrcmpi(pszExt, TEXT(".HTT")) == 0))
  1044.         {
  1045.             fRet = FALSE;
  1046.         }
  1047.     }
  1048.     return fRet;
  1049. }
  1050. void OnDesktopSysColorChange(void)
  1051. {
  1052.     static COLORREF clrBackground = 0xffffffff;
  1053.     static COLORREF clrWindowText = 0xffffffff;
  1054.     //Get the new colors!
  1055.     COLORREF    clrNewBackground = GetSysColor(COLOR_BACKGROUND);
  1056.     COLORREF    clrNewWindowText = GetSysColor(COLOR_WINDOWTEXT);
  1057.     //Have we initialized these before?
  1058.     if(clrBackground != 0xffffffff)  //Have we initialized the statics yet?
  1059.     {
  1060.         // Our HTML file depends only on these two system colors.
  1061.         // Check if either of them has changed!
  1062.         // If not, no need to regenerate HTML file. 
  1063.         // This avoids infinite loop. And this is a nice optimization.
  1064.         if((clrBackground == clrNewBackground) &&
  1065.            (clrWindowText == clrNewWindowText))
  1066.             return; //No need to do anything. Just return.
  1067.     }
  1068.     // Remember the new colors in the statics.
  1069.     clrBackground = clrNewBackground;
  1070.     clrWindowText = clrNewWindowText;
  1071.     //
  1072.     // The desktop got a WM_SYSCOLORCHANGE.  We need to
  1073.     // regenerate the HTML if there are any system colors
  1074.     // showing on the desktop.  Patterns and the desktop
  1075.     // color are both based on system colors.
  1076.     //
  1077.     IActiveDesktop *pad;
  1078.     if (SUCCEEDED(CActiveDesktop_InternalCreateInstance((LPUNKNOWN *)&pad, IID_IActiveDesktop)))
  1079.     {
  1080.         BOOL fRegenerateHtml = FALSE;
  1081.         WCHAR szWallpaperW[INTERNET_MAX_URL_LENGTH];
  1082.         if (SUCCEEDED(pad->GetWallpaper(szWallpaperW, ARRAYSIZE(szWallpaperW), 0)))
  1083.         {
  1084.             if (!*szWallpaperW)
  1085.             {
  1086.                 //
  1087.                 // No wallpaper means the desktop color
  1088.                 // or a pattern is showing - we need to
  1089.                 // regenerate the desktop HTML.
  1090.                 //
  1091.                 fRegenerateHtml = TRUE;
  1092.             }
  1093.             else
  1094.             {
  1095.                 TCHAR *pszWallpaper;
  1096. #ifdef UNICODE
  1097.                 pszWallpaper = szWallpaperW;
  1098. #else
  1099.                 CHAR szWallpaperA[INTERNET_MAX_URL_LENGTH];
  1100.                 SHUnicodeToAnsi(szWallpaperW, szWallpaperA, ARRAYSIZE(szWallpaperA));
  1101.                 pszWallpaper = szWallpaperA;
  1102. #endif
  1103.                 if (IsWallpaperPicture(pszWallpaper))
  1104.                 {
  1105.                     WALLPAPEROPT wpo = { SIZEOF(wpo) };
  1106.                     if (SUCCEEDED(pad->GetWallpaperOptions(&wpo, 0)))
  1107.                     {
  1108.                         if (wpo.dwStyle == WPSTYLE_CENTER)
  1109.                         {
  1110.                             //
  1111.                             // We have a centered picture,
  1112.                             // the pattern or desktop color
  1113.                             // could be leaking around the edges.
  1114.                             // We need to regenerate the desktop
  1115.                             // HTML.
  1116.                             //
  1117.                             fRegenerateHtml = TRUE;
  1118.                         }
  1119.                     }
  1120.                     else
  1121.                     {
  1122.                         TraceMsg(TF_WARNING, "SYSCLRCHG: Could not get wallpaper options!");
  1123.                     }
  1124.                 }
  1125.             }
  1126.         }
  1127.         else
  1128.         {
  1129.             TraceMsg(TF_WARNING, "SYSCLRCHG: Could not get selected wallpaper!");
  1130.         }
  1131.         
  1132.         if (fRegenerateHtml)
  1133.         {
  1134.             DWORD  dwFlags = AD_APPLY_FORCE | AD_APPLY_HTMLGEN | AD_APPLY_REFRESH | AD_APPLY_DYNAMICREFRESH;
  1135.             WCHAR   wszPattern[MAX_PATH];
  1136.             //If we have a pattern, then we need to force a AD_APPLY_COMPLETEREFRESH
  1137.             // because we need to re-generate the pattern.bmp file which can not be 
  1138.             // done through dynamic HTML.
  1139.             if(SUCCEEDED(pad->GetPattern(wszPattern, sizeof(wszPattern), 0)))
  1140.             {
  1141. #ifdef UNICODE
  1142.                 LPTSTR  szPattern = (LPTSTR)wszPattern;
  1143. #else
  1144.                 CHAR   szPattern[MAX_PATH];
  1145.                 SHUnicodeToAnsi(wszPattern, szPattern, sizeof(szPattern));
  1146. #endif //UNICODE
  1147.                 if(IsValidPattern(szPattern))           //Does this have a pattern?
  1148.                     dwFlags &= ~(AD_APPLY_DYNAMICREFRESH);  //Then force a complete refresh!
  1149.                     
  1150.             }
  1151.             pad->ApplyChanges(dwFlags);
  1152.         }
  1153.         pad->Release();
  1154.     }
  1155.     else
  1156.     {
  1157.         TraceMsg(TF_WARNING, "SYSCLRCHG: Could not create CActiveDesktop!");
  1158.     }
  1159. }
  1160. //
  1161. // Convert a .URL file into its target.
  1162. //
  1163. void CheckAndResolveLocalUrlFile(LPTSTR pszFileName, int cchFileName)
  1164. {
  1165.     //
  1166.     // This function only works on *.URL files.
  1167.     //
  1168.     if (!PathIsURL(pszFileName))
  1169.     {
  1170.         LPTSTR pszExt;
  1171.         //
  1172.         // Check if the extension of this file is *.URL
  1173.         //
  1174.         pszExt = PathFindExtension(pszFileName);
  1175.         if (pszExt && *pszExt)
  1176.         {
  1177.             TCHAR  szUrl[15];
  1178.         
  1179.             LoadString(HINST_THISDLL, IDS_URL_EXTENSION, szUrl, ARRAYSIZE(szUrl));
  1180.             if (lstrcmpi(pszExt, szUrl) == 0)
  1181.             {
  1182.                 HRESULT  hr;
  1183.                 IUniformResourceLocator *purl;
  1184.                 hr = CoCreateInstance(CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER,
  1185.                               IID_IUniformResourceLocator,
  1186.                               (LPVOID *)&purl);
  1187.                 if (EVAL(SUCCEEDED(hr)))  // This works for both Ansi and Unicode
  1188.                 {
  1189.                     ASSERT(purl);
  1190.                     IPersistFile  *ppf;
  1191.                     hr = purl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);
  1192.                     if (SUCCEEDED(hr))
  1193.                     {
  1194.                         WCHAR szFileW[MAX_PATH];
  1195.                         LPTSTR pszTemp;
  1196.                         SHTCharToUnicode(pszFileName, szFileW, ARRAYSIZE(szFileW));
  1197.                         ppf->Load(szFileW, STGM_READ);
  1198.                         hr = purl->GetURL(&pszTemp);    // Wow, an ANSI/UNICODE COM interface!
  1199.                         if (EVAL(SUCCEEDED(hr)))
  1200.                         {
  1201.                             StrCpyN(pszFileName, pszTemp, cchFileName);
  1202.                             CoTaskMemFree(pszTemp);
  1203.                         }
  1204.                         ppf->Release();
  1205.                     }
  1206.                     purl->Release();
  1207.                 }
  1208.             }
  1209.         }
  1210.     }
  1211. }
  1212. void GetMyCurHomePageStartPos(int *piLeft, int *piTop, DWORD *pdwWidth, DWORD *pdwHeight)
  1213. {
  1214. #define INVALID_POS 0x80000000
  1215.     HKEY hkey;
  1216.     //
  1217.     // Assume nothing.
  1218.     //
  1219.     *piLeft = INVALID_POS;
  1220.     *piTop = INVALID_POS;
  1221.     *pdwWidth = INVALID_POS;
  1222.     *pdwHeight = INVALID_POS;
  1223.     //
  1224.     // Read from registry first.
  1225.     //
  1226.     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\Microsoft\Internet Explorer\Main"), 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS)
  1227.     {
  1228.         DWORD dwType, cbData;
  1229.         cbData = SIZEOF(*piLeft);
  1230.         SHQueryValueEx(hkey, TEXT("MyCurHome_Left"), NULL, &dwType, (LPBYTE)piLeft, &cbData);
  1231.         cbData = SIZEOF(*piTop);
  1232.         SHQueryValueEx(hkey, TEXT("MyCurHome_Top"), NULL, &dwType, (LPBYTE)piTop, &cbData);
  1233.         cbData = SIZEOF(*pdwWidth);
  1234.         SHQueryValueEx(hkey, TEXT("MyCurHome_Width"), NULL, &dwType, (LPBYTE)pdwWidth, &cbData);
  1235.         cbData = SIZEOF(*pdwHeight);
  1236.         SHQueryValueEx(hkey, TEXT("MyCurHome_Height"), NULL, &dwType, (LPBYTE)pdwHeight, &cbData);
  1237.         RegCloseKey(hkey);
  1238.     }
  1239.     //
  1240.     // Fill in defaults when registry provides no info.
  1241.     //
  1242.     if (*piLeft == INVALID_POS)
  1243.     {
  1244.         *piLeft = -MYCURHOME_WIDTH;
  1245.     }
  1246.     if (*piTop == INVALID_POS)
  1247.     {
  1248.         *piTop = MYCURHOME_TOP;
  1249.     }
  1250.     if (*pdwWidth == INVALID_POS)
  1251.     {
  1252.         *pdwWidth = MYCURHOME_WIDTH;
  1253.     }
  1254.     if (*pdwHeight == INVALID_POS)
  1255.     {
  1256.         *pdwHeight = MYCURHOME_HEIGHT;
  1257.     }
  1258.     //
  1259.     // Convert negative values into positive ones.
  1260.     //
  1261.     RECT rect;
  1262.     SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, FALSE);
  1263.     if (*piLeft < 0)
  1264.     {
  1265.         *piLeft += (rect.right - rect.left) - *pdwWidth;
  1266.     }
  1267.     if (*piTop < 0)
  1268.     {
  1269.         *piTop += (rect.bottom - rect.top) - *pdwHeight;
  1270.     }
  1271.     // Find the virtual dimensions.
  1272.     EnumMonitorsArea ema;
  1273.     GetMonitorSettings(&ema);
  1274.     // Position it in the primary monitor.
  1275.     *piLeft -= ema.rcVirtualMonitor.left;
  1276.     *piTop -= ema.rcVirtualMonitor.top;
  1277. #undef INVALID_POS
  1278. }
  1279. //
  1280. // Silently adds a specified component to the desktop and use the given
  1281. //  apply flags using which you can avoid nested unnecessary HTML generation, 
  1282. //  or refreshing which may lead to racing conditions.
  1283. // 
  1284. //
  1285. BOOL AddDesktopComponentNoUI(DWORD dwApplyFlags, LPCTSTR pszUrl, LPCTSTR pszFriendlyName, int iCompType, int iLeft, int iTop, int iWidth, int iHeight, BOOL fChecked, DWORD dwCurItemState, BOOL fNoScroll, BOOL fCanResize)
  1286. {
  1287.     COMPONENTA Comp;
  1288.     BOOL    fRet = FALSE;
  1289.     HRESULT hres;
  1290.     //
  1291.     // Build the pcomp structure.
  1292.     //
  1293.     Comp.dwSize = sizeof(COMPONENTA);
  1294.     Comp.dwID = -1;
  1295.     Comp.iComponentType = iCompType;
  1296.     Comp.fChecked = fChecked;
  1297.     Comp.fDirty = FALSE;
  1298.     Comp.fNoScroll = fNoScroll;
  1299.     Comp.cpPos.dwSize = SIZEOF(COMPPOS);
  1300.     Comp.cpPos.iLeft = iLeft;
  1301.     Comp.cpPos.iTop = iTop;
  1302.     Comp.cpPos.dwWidth = iWidth;
  1303.     Comp.cpPos.dwHeight = iHeight;
  1304.     Comp.cpPos.izIndex = (dwCurItemState & IS_NORMAL) ? COMPONENT_TOP : 0;
  1305.     Comp.cpPos.fCanResize = fCanResize;
  1306.     Comp.cpPos.fCanResizeX = fCanResize;
  1307.     Comp.cpPos.fCanResizeY = fCanResize;
  1308.     Comp.cpPos.iPreferredLeftPercent = 0;
  1309.     Comp.cpPos.iPreferredTopPercent = 0;
  1310.     Comp.dwCurItemState = dwCurItemState;
  1311.     lstrcpyn(Comp.szSource, pszUrl, ARRAYSIZE(Comp.szSource));
  1312.     lstrcpyn(Comp.szSubscribedURL, pszUrl, ARRAYSIZE(Comp.szSource));
  1313.     if (pszFriendlyName)
  1314.     {
  1315.         lstrcpyn(Comp.szFriendlyName, pszFriendlyName, ARRAYSIZE(Comp.szFriendlyName));
  1316.     }
  1317.     else
  1318.     {
  1319.         Comp.szFriendlyName[0] = TEXT('');
  1320.     }
  1321.     IActiveDesktop *pActiveDesk;
  1322.     //
  1323.     // Add it to the system.
  1324.     //
  1325.     hres = CActiveDesktop_InternalCreateInstance((LPUNKNOWN *)&pActiveDesk, IID_IActiveDesktop);
  1326.     if (SUCCEEDED(hres))
  1327.     {
  1328.         COMPONENT  CompW;
  1329.         CompW.dwSize = sizeof(CompW);  //Required for the MultiCompToWideComp to work properly.
  1330.         MultiCompToWideComp(&Comp, &CompW);
  1331.         pActiveDesk->AddDesktopItem(&CompW, 0);
  1332.         pActiveDesk->ApplyChanges(dwApplyFlags);
  1333.         pActiveDesk->Release();
  1334.         fRet = TRUE;
  1335.     }
  1336.     return fRet;
  1337. }
  1338. // Little helper function used to change the safemode state
  1339. void SetSafeMode(DWORD dwFlags)
  1340. {
  1341.     IActiveDesktopP * piadp;
  1342.     if (SUCCEEDED(CActiveDesktop_InternalCreateInstance((LPUNKNOWN *)&piadp, IID_IActiveDesktopP)))
  1343.     {
  1344.         piadp->SetSafeMode(dwFlags);
  1345.         piadp->Release();
  1346.     }
  1347. }
  1348. /****************************************************************************
  1349.  *
  1350.  *  RefreshWebViewDesktop - regenerates desktop HTML from registry and updates
  1351.  *                          the screen
  1352.  *
  1353.  *  ENTRY:
  1354.  *      none
  1355.  *
  1356.  *  RETURNS:
  1357.  *      TRUE on success
  1358.  *      
  1359.  ****************************************************************************/
  1360. BOOL PokeWebViewDesktop(DWORD dwFlags)
  1361. {
  1362.     IActiveDesktop *pad;
  1363.     HRESULT     hres;
  1364.     BOOL        fRet = FALSE;
  1365.     hres = CActiveDesktop_InternalCreateInstance((LPUNKNOWN *)&pad, IID_IActiveDesktop);
  1366.     if (SUCCEEDED(hres))
  1367.     {
  1368.         pad->ApplyChanges(dwFlags);
  1369.         pad->Release();
  1370.         fRet = TRUE;
  1371.     }
  1372.     return (fRet);
  1373. }
  1374. #ifdef WE_WANT_DEFAULT_WALLPAPER
  1375. //The following is needed only if we add a default wallpaper.
  1376. void SetDefaultWallpaper()
  1377. {
  1378.     IActiveDesktop *pad;
  1379.     if (SUCCEEDED(CActiveDesktop_InternalCreateInstance((LPUNKNOWN *)&pad, IID_IActiveDesktop)))
  1380.     {
  1381.         DWORD dwWallpaperStyle;
  1382.         WALLPAPEROPT wpo;
  1383.         // If we have a wallpaper at the old location in the registry, we use it as the default
  1384.         TCHAR szWallpaper[INTERNET_MAX_URL_LENGTH];
  1385.         // Read the Wallpaper from the Old location.
  1386.         if(!GetStringFromReg(HKEY_CURRENT_USER, REGSTR_PATH_DESKTOP, REG_VAL_GENERAL_WALLPAPER, c_szNULL, szWallpaper, ARRAYSIZE(szWallpaper)))
  1387.             szWallpaper[0] = TEXT('');
  1388.         TrimWhiteSpace(szWallpaper);
  1389.         
  1390.         // Read the wallpaper style
  1391.         ReadWallpaperStyleFromReg(REGSTR_PATH_DESKTOP, &dwWallpaperStyle, FALSE);
  1392.         wpo.dwSize = SIZEOF(WALLPAPEROPT);
  1393.         pad->GetWallpaperOptions(&wpo, 0);
  1394.         wpo.dwStyle = dwWallpaperStyle;     //Set the wallpaper style!
  1395.         pad->SetWallpaperOptions(&wpo, 0);  //Set the wallpaper style options.
  1396.         
  1397.         TCHAR szDefaultWallpaper[MAX_PATH];
  1398.         GetDefaultWallpaper(szDefaultWallpaper, SIZECHARS(szDefaultWallpaper));
  1399.         
  1400.         if(szWallpaper[0] == TEXT('') || lstrcmpi(szWallpaper, g_szNone) == 0)
  1401.         {
  1402.             lstrcpy(szWallpaper, szDefaultWallpaper);
  1403.         }
  1404.         
  1405.         WCHAR *pwszWallpaper;
  1406. #ifndef UNICODE
  1407.         WCHAR   wszWallpaper[INTERNET_MAX_URL_LENGTH];
  1408.         SHAnsiToUnicode(szWallpaper, wszWallpaper, ARRAYSIZE(wszWallpaper));
  1409.         pwszWallpaper = wszWallpaper;
  1410. #else
  1411.         pwszWallpaper = szWallpaper;
  1412. #endif
  1413.         pad->SetWallpaper(pwszWallpaper, 0);
  1414.         pad->ApplyChanges(AD_APPLY_SAVE);
  1415.         pad->Release();
  1416.         // This is a kinda hack, but the best possible solution right now. The scenario is as follows.
  1417.         // The Memphis setup guys replace what the user specifies as the wallpaper in the old location
  1418.         // and restore it after setup is complete. But, SetDefaultWallpaper() gets called bet. these
  1419.         // two times and we are supposed to take a decision on whether to set the default htm wallpaper or not,
  1420.         // depending on what the user had set before the installation. The solution is to delay making
  1421.         // this decision until after the setup guys have restored the user's wallpaper. We do this in
  1422.         // CActiveDesktop::_ReadWallpaper(). We specify that SetDefaultWallpaper() was called by setting
  1423.         // the backup wallpaper in the new location to the default wallpaper.
  1424.         TCHAR szDeskcomp[MAX_PATH];
  1425.         wsprintf(szDeskcomp, REG_DESKCOMP_GENERAL, TEXT("\"));
  1426.         SHSetValue(HKEY_CURRENT_USER, szDeskcomp,
  1427.             REG_VAL_GENERAL_BACKUPWALLPAPER, REG_SZ, (LPBYTE)szDefaultWallpaper,
  1428.             SIZEOF(TCHAR)*(lstrlen(szDefaultWallpaper)+1));
  1429.     }
  1430. }
  1431. #endif // IF_WE_WANT_DEFAULT_WALLPAPER
  1432. #define CCH_NONE 20 //big enough for "(None)" in german
  1433. TCHAR g_szNone[CCH_NONE] = {0};
  1434. void InitDeskHtmlGlobals(void)
  1435. {
  1436.     static fGlobalsInited = FALSE;
  1437.     if (fGlobalsInited == FALSE)
  1438.     {
  1439.         LoadString(HINST_THISDLL, IDS_WPNONE, g_szNone, ARRAYSIZE(g_szNone));
  1440.         fGlobalsInited = TRUE;
  1441.     }
  1442. }
  1443. //
  1444. // Loads the preview bitmap for property sheet pages.
  1445. //
  1446. HBITMAP LoadMonitorBitmap(void)
  1447. {
  1448.     HBITMAP hbm,hbmT;
  1449.     BITMAP bm;
  1450.     HBRUSH hbrT;
  1451.     HDC hdc;
  1452.     COLORREF c3df = GetSysColor(COLOR_3DFACE);
  1453.     hbm = LoadBitmap(HINST_THISDLL, MAKEINTRESOURCE(IDB_MONITOR));
  1454.     if (hbm == NULL)
  1455.     {
  1456.         return NULL;
  1457.     }
  1458.     //
  1459.     // Convert the "base" of the monitor to the right color.
  1460.     //
  1461.     // The lower left of the bitmap has a transparent color
  1462.     // we fixup using FloodFill
  1463.     //
  1464.     hdc = CreateCompatibleDC(NULL);
  1465.     hbmT = (HBITMAP)SelectObject(hdc, hbm);
  1466.     hbrT = (HBRUSH)SelectObject(hdc, GetSysColorBrush(COLOR_3DFACE));
  1467.     GetObject(hbm, sizeof(bm), &bm);
  1468.     ExtFloodFill(hdc, 0, bm.bmHeight-1, GetPixel(hdc, 0, bm.bmHeight-1), FLOODFILLSURFACE);
  1469.     //
  1470.     // Round off the corners.
  1471.     // The bottom two were done by the floodfill above.
  1472.     // The top left is important since SS_CENTERIMAGE uses it to fill gaps.
  1473.     // The top right should be rounded because the other three are.
  1474.     //
  1475.     SetPixel( hdc, 0, 0, c3df );
  1476.     SetPixel( hdc, bm.bmWidth-1, 0, c3df );
  1477.     //
  1478.     // Fill in the desktop here.
  1479.     //
  1480.     HBRUSH hbrOld = (HBRUSH)SelectObject(hdc, GetSysColorBrush(COLOR_DESKTOP));
  1481.     PatBlt(hdc, MON_X, MON_Y, MON_DX, MON_DY, PATCOPY);
  1482.     SelectObject(hdc, hbrOld);
  1483.     //
  1484.     // Clean up after ourselves.
  1485.     //
  1486.     SelectObject(hdc, hbrT);
  1487.     SelectObject(hdc, hbmT);
  1488.     DeleteDC(hdc);
  1489.     return hbm;
  1490. }
  1491. STDAPI_(DWORD) GetDesktopFlags(void)
  1492. {
  1493.     DWORD dwFlags = 0, dwType, cbSize = SIZEOF(dwFlags);
  1494.     TCHAR lpszDeskcomp[MAX_PATH];
  1495.     GetRegLocation(lpszDeskcomp, SIZECHARS(lpszDeskcomp), REG_DESKCOMP_COMPONENTS, NULL);
  1496.     SHGetValue(HKEY_CURRENT_USER, lpszDeskcomp, REG_VAL_COMP_GENFLAGS, &dwType, &dwFlags, &cbSize);
  1497.     return dwFlags;
  1498. }
  1499. STDAPI_(BOOL) SetDesktopFlags(DWORD dwMask, DWORD dwNewFlags)
  1500. {
  1501.     BOOL  fRet = FALSE;
  1502.     HKEY  hkey;
  1503.     DWORD dwDisposition;
  1504.     TCHAR lpszDeskcomp[MAX_PATH];
  1505.     GetRegLocation(lpszDeskcomp, SIZECHARS(lpszDeskcomp), REG_DESKCOMP_COMPONENTS, NULL);
  1506.     if (RegCreateKeyEx(HKEY_CURRENT_USER, (LPCTSTR)lpszDeskcomp, 
  1507.                        0, NULL, 0, KEY_READ | KEY_WRITE, NULL, &hkey,
  1508.                        &dwDisposition) == ERROR_SUCCESS)
  1509.     {
  1510.         DWORD dwFlags;
  1511.         DWORD cbSize = SIZEOF(dwFlags);
  1512.         DWORD dwType;
  1513.         if (SHQueryValueEx(hkey, REG_VAL_COMP_GENFLAGS, NULL, &dwType,
  1514.                             (LPBYTE)&dwFlags, &cbSize) != ERROR_SUCCESS)
  1515.         {
  1516.             dwFlags = 0;
  1517.         }
  1518.         dwFlags = (dwFlags & ~dwMask) | (dwNewFlags & dwMask);
  1519.         if (RegSetValueEx(hkey, REG_VAL_COMP_GENFLAGS, 0, REG_DWORD,
  1520.                           (LPBYTE)&dwFlags, sizeof(dwFlags)) == ERROR_SUCCESS)
  1521.         {
  1522.             fRet = TRUE;
  1523.         }
  1524.     
  1525.         RegCloseKey(hkey);
  1526.     }
  1527.     return fRet;
  1528. }
  1529. BOOL UpdateComponentFlags(LPCTSTR pszCompId, DWORD dwMask, DWORD dwNewFlags)
  1530. {
  1531.     BOOL fRet = FALSE;
  1532.     TCHAR szRegPath[MAX_PATH];
  1533.     HKEY hkey;
  1534.     GetRegLocation(szRegPath, SIZECHARS(szRegPath), REG_DESKCOMP_COMPONENTS, NULL);
  1535.     lstrcat(szRegPath, TEXT("\"));
  1536.     lstrcat(szRegPath, pszCompId);
  1537.     //Don't use RegCreateKeyEx here. It will result in Null components to be added.
  1538.     if (RegOpenKeyEx(HKEY_CURRENT_USER, szRegPath, 0,
  1539.                        KEY_READ | KEY_WRITE, &hkey) == ERROR_SUCCESS)
  1540.     {
  1541.         DWORD dwType, dwFlags, dwDataLength;
  1542.         dwDataLength = sizeof(DWORD);
  1543.         if(SHQueryValueEx(hkey, REG_VAL_COMP_FLAGS, NULL, &dwType, (LPBYTE)&dwFlags, &dwDataLength) != ERROR_SUCCESS)
  1544.         {
  1545.             dwFlags = 0;
  1546.         }        
  1547.         dwNewFlags = (dwFlags & ~dwMask) | (dwNewFlags & dwMask);
  1548.         if (RegSetValueEx(hkey, REG_VAL_COMP_FLAGS, 0, REG_DWORD, (LPBYTE)&dwNewFlags,
  1549.                           SIZEOF(DWORD)) == ERROR_SUCCESS)
  1550.         {
  1551.             fRet = TRUE;
  1552.         }
  1553.         SetDesktopFlags(COMPONENTS_DIRTY, COMPONENTS_DIRTY);
  1554.         RegCloseKey(hkey);
  1555.     }
  1556.     else
  1557.     {
  1558.         TraceMsg(TF_WARNING, "DS: Unable to UpdateComponentFlags");
  1559.     }
  1560.     return fRet;
  1561. }
  1562. DWORD GetCurrentState(LPTSTR pszCompId)
  1563. {
  1564.     TCHAR szRegPath[MAX_PATH];
  1565.     DWORD cbSize, dwType, dwCurState;
  1566.     GetRegLocation(szRegPath, SIZECHARS(szRegPath), REG_DESKCOMP_COMPONENTS, NULL);
  1567.     lstrcat(szRegPath, TEXT("\"));
  1568.     lstrcat(szRegPath, pszCompId);
  1569.     cbSize = sizeof(dwCurState);
  1570.     if (SHGetValue(HKEY_CURRENT_USER, szRegPath, REG_VAL_COMP_CURSTATE, &dwType, &dwCurState, &cbSize) != ERROR_SUCCESS)
  1571.         dwCurState = IS_NORMAL;
  1572.     return dwCurState;
  1573. }
  1574. BOOL GetSavedStateInfo(LPTSTR pszCompId, LPCOMPSTATEINFO    pCompState, BOOL  fRestoredState)
  1575. {
  1576.     BOOL fRet = FALSE;
  1577.     TCHAR szRegPath[MAX_PATH];
  1578.     HKEY hkey;
  1579.     LPTSTR  lpValName = (fRestoredState ? REG_VAL_COMP_RESTOREDSTATEINFO : REG_VAL_COMP_ORIGINALSTATEINFO);
  1580.     GetRegLocation(szRegPath, SIZECHARS(szRegPath), REG_DESKCOMP_COMPONENTS, NULL);
  1581.     lstrcat(szRegPath, TEXT("\"));
  1582.     lstrcat(szRegPath, pszCompId);
  1583.     //No need to use RegCreateKeyEx here. Use RegOpenKeyEx instead.
  1584.     if (RegOpenKeyEx(HKEY_CURRENT_USER, szRegPath, 0,
  1585.                        KEY_READ, &hkey) == ERROR_SUCCESS)
  1586.     {
  1587.         DWORD   cbSize, dwType;
  1588.         
  1589.         cbSize = SIZEOF(*pCompState);
  1590.         dwType = REG_BINARY;
  1591.         
  1592.         if (SHQueryValueEx(hkey, lpValName, NULL, &dwType, (LPBYTE)pCompState, &cbSize) != ERROR_SUCCESS)
  1593.         {
  1594.             //If the item state is missing, read the item current position and
  1595.             // and return that as the saved state.
  1596.             COMPPOS cpPos;
  1597.             cbSize = SIZEOF(cpPos);
  1598.             dwType = REG_BINARY;
  1599.             if (SHQueryValueEx(hkey, REG_VAL_COMP_POSITION, NULL, &dwType, (LPBYTE)&cpPos, &cbSize) != ERROR_SUCCESS)
  1600.             {
  1601.                 ZeroMemory(&cpPos, SIZEOF(cpPos));
  1602.             }            
  1603.             SetStateInfo(pCompState, &cpPos, IS_NORMAL);
  1604.         }
  1605.         RegCloseKey(hkey);
  1606.     }
  1607.     else
  1608.         TraceMsg(TF_WARNING, "DS: Unable to get SavedStateInfo()");
  1609.     return fRet;
  1610.  }
  1611. BOOL UpdateDesktopPosition(LPTSTR pszCompId, int iLeft, int iTop, DWORD dwWidth, DWORD dwHeight, int izIndex,
  1612.                             BOOL    fSaveRestorePos, BOOL fSaveOriginal, DWORD dwCurState)
  1613. {
  1614.     BOOL fRet = FALSE;
  1615.     TCHAR szRegPath[MAX_PATH];
  1616.     HKEY hkey;
  1617.     GetRegLocation(szRegPath, SIZECHARS(szRegPath), REG_DESKCOMP_COMPONENTS, NULL);
  1618.     lstrcat(szRegPath, TEXT("\"));
  1619.     lstrcat(szRegPath, pszCompId);
  1620.     //Don't use RegCreateKeyEx here; It will result in a NULL component being added.
  1621.     if (RegOpenKeyEx(HKEY_CURRENT_USER, szRegPath, 0,
  1622.                        KEY_READ | KEY_WRITE, &hkey) == ERROR_SUCCESS)
  1623.     {
  1624.         COMPPOS         cp;
  1625.         DWORD           dwType;
  1626.         DWORD           dwDataLength;
  1627.         COMPSTATEINFO   csi;
  1628.         dwType = REG_BINARY;
  1629.         dwDataLength = sizeof(COMPPOS);
  1630.         if(SHQueryValueEx(hkey, REG_VAL_COMP_POSITION, NULL, &dwType, (LPBYTE)&cp, &dwDataLength) != ERROR_SUCCESS)
  1631.         {
  1632.             cp.fCanResize = cp.fCanResizeX = cp.fCanResizeY = TRUE;
  1633.             cp.iPreferredLeftPercent = cp.iPreferredTopPercent = 0;
  1634.         }
  1635.         //Read the current State
  1636.         dwType = REG_DWORD;
  1637.         dwDataLength = SIZEOF(csi.dwItemState);
  1638.         if (SHQueryValueEx(hkey, REG_VAL_COMP_CURSTATE, NULL, &dwType, (LPBYTE)&csi.dwItemState, &dwDataLength) != ERROR_SUCCESS)
  1639.         {
  1640.             csi.dwItemState = IS_NORMAL;
  1641.         }
  1642.         if(fSaveRestorePos)
  1643.         {
  1644.             //We have just read the current position; Let's save it as the restore position.
  1645.             SetStateInfo(&csi, &cp, csi.dwItemState);
  1646.             //Now that we know the complete current state, save it as the restore state!
  1647.             RegSetValueEx(hkey, REG_VAL_COMP_RESTOREDSTATEINFO, 0, REG_BINARY, (LPBYTE)&csi, SIZEOF(csi));
  1648.         }
  1649.         //Save the current state too!
  1650.         if(dwCurState)
  1651.             RegSetValueEx(hkey, REG_VAL_COMP_CURSTATE, 0, REG_DWORD, (LPBYTE)&dwCurState, SIZEOF(dwCurState));
  1652.             
  1653.         cp.dwSize = sizeof(COMPPOS);
  1654.         cp.iLeft = iLeft;
  1655.         cp.iTop = iTop;
  1656.         cp.dwWidth = dwWidth;
  1657.         cp.dwHeight = dwHeight;
  1658.         cp.izIndex = izIndex;
  1659.         if (fSaveOriginal) {
  1660.             SetStateInfo(&csi, &cp, csi.dwItemState);
  1661.             RegSetValueEx(hkey, REG_VAL_COMP_ORIGINALSTATEINFO, 0, REG_BINARY, (LPBYTE)&csi, SIZEOF(csi));
  1662.         }
  1663.         if (RegSetValueEx(hkey, REG_VAL_COMP_POSITION, 0, REG_BINARY, (LPBYTE)&cp,
  1664.                           SIZEOF(cp)) == ERROR_SUCCESS)
  1665.         {
  1666.             fRet = TRUE;
  1667.         }
  1668.         // Don't need to mark as dirty if we're just saving the original pos
  1669.         if (!fSaveOriginal)
  1670.             SetDesktopFlags(COMPONENTS_DIRTY, COMPONENTS_DIRTY);
  1671.         RegCloseKey(hkey);
  1672.     }
  1673.     else
  1674.     {
  1675.         TraceMsg(TF_WARNING, "DS: Unable to UpdateDesktopPosition");
  1676.     }
  1677.     return fRet;
  1678. }
  1679. void    EnsureFilePathIsPresent (LPCTSTR pszFilePath)
  1680. //  Recursively process (in reverse) a path and create the first
  1681. //  directory that does not exist and when unwinding the calling
  1682. //  chain create each subsequent directory in the path until the
  1683. //  whole original path is created.
  1684. {
  1685.     TCHAR   szDesktopFileDirectory[MAX_PATH];
  1686.     lstrcpy(szDesktopFileDirectory, pszFilePath);
  1687.     ASSERT(lstrlen(szDesktopFileDirectory) > lstrlen(TEXT("C:\")));      // something wrong if the root directory is hit
  1688.     if ((PathRemoveFileSpec(szDesktopFileDirectory) != 0) && (GetFileAttributes(szDesktopFileDirectory) == 0xFFFFFFFF) && (CreateDirectory(szDesktopFileDirectory, NULL) == 0))
  1689.     {
  1690.         EnsureFilePathIsPresent(szDesktopFileDirectory);
  1691.         TBOOL(CreateDirectory(szDesktopFileDirectory, NULL));
  1692.     }
  1693. }
  1694. void GetPerUserFileName(LPTSTR pszOutputFileName, DWORD dwSize, LPTSTR pszPartialFileName)
  1695. {
  1696.     LPITEMIDLIST    pidlAppData;
  1697.     *pszOutputFileName = TEXT('');
  1698.     if(dwSize < MAX_PATH)
  1699.     {
  1700.         ASSERT(FALSE);
  1701.         return;
  1702.     }
  1703.     if(EVAL(SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidlAppData))))
  1704.     {
  1705.         SHGetPathFromIDList(pidlAppData, pszOutputFileName);
  1706.         PathAppend(pszOutputFileName, pszPartialFileName);
  1707.         ILFree(pidlAppData);
  1708.     }
  1709.     EnsureFilePathIsPresent(pszOutputFileName);
  1710. }
  1711. void GetRegLocation(LPTSTR lpszResult, DWORD cchResult, LPCTSTR lpszKey, LPCTSTR lpszScheme)
  1712. {
  1713.     TCHAR szSubkey[MAX_PATH];
  1714.     DWORD dwDataLength = sizeof(szSubkey) - 2 * sizeof(TCHAR);
  1715.     DWORD dwType;
  1716.     lstrcpy(szSubkey, TEXT("\"));
  1717.     //  use what was given or get it from the registry
  1718.     if (lpszScheme)
  1719.         StrCatBuff(szSubkey, lpszScheme, SIZECHARS(szSubkey));
  1720.     else
  1721.         SHGetValue(HKEY_CURRENT_USER, REG_DESKCOMP_SCHEME, REG_VAL_SCHEME_DISPLAY, &dwType,
  1722.             (LPBYTE)(szSubkey) + sizeof(TCHAR), &dwDataLength);
  1723.     if (szSubkey[1])
  1724.         StrCatBuff(szSubkey, TEXT("\"), SIZECHARS(szSubkey));
  1725.     wnsprintf(lpszResult, cchResult, lpszKey, szSubkey);
  1726. }
  1727. BOOL ValidateFileName(HWND hwnd, LPCTSTR pszFilename, int iTypeString)
  1728. {
  1729.     BOOL fRet = TRUE;
  1730.     DWORD dwAttributes = GetFileAttributes(pszFilename);
  1731.     if ((dwAttributes != 0xFFFFFFFF) &&
  1732.         (dwAttributes & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)))
  1733.     {
  1734.         TCHAR szType1[64];
  1735.         TCHAR szType2[64];
  1736.         LoadString(HINST_THISDLL, iTypeString, szType1, ARRAYSIZE(szType1));
  1737.         LoadString(HINST_THISDLL, iTypeString+1, szType2, ARRAYSIZE(szType2));
  1738.         if (ShellMessageBox(HINST_THISDLL, hwnd,
  1739.                             MAKEINTRESOURCE(IDS_VALIDFN_FMT),
  1740.                             MAKEINTRESOURCE(IDS_VALIDFN_TITLE),
  1741.                             MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2,
  1742.                             szType1, szType2) == IDNO)
  1743.         {
  1744.             fRet = FALSE;
  1745.         }
  1746.     }
  1747.     return fRet;
  1748. }
  1749. void GetWallpaperDirName(LPTSTR lpszWallPaperDir, int iBuffSize)
  1750. {
  1751.     //Compute the default wallpaper name.
  1752.     GetWindowsDirectory(lpszWallPaperDir, iBuffSize);
  1753.     lstrcat(lpszWallPaperDir, DESKTOPHTML_WEB_DIR);
  1754.     //Read it from the registry key, if it is set!
  1755.     DWORD dwType;
  1756.     DWORD cbData = (DWORD)iBuffSize;
  1757.     SHGetValue(HKEY_LOCAL_MACHINE, REGSTR_PATH_SETUP, c_szWallPaperDir, &dwType, (LPVOID)lpszWallPaperDir, &cbData);
  1758.     // BUGBUG: Currently the type is wrongly being set to REG_SZ. When this is changed by setup, uncomment the if() below.
  1759.     // if (dwType == REG_EXPAND_SZ)
  1760.     {
  1761.         TCHAR szExp[MAX_PATH];
  1762.         SHExpandEnvironmentStrings(lpszWallPaperDir, szExp, ARRAYSIZE(szExp));
  1763.         lstrcpyn(lpszWallPaperDir, szExp, iBuffSize);
  1764.     }
  1765. }
  1766. BOOL CALLBACK MultiMonEnumAreaCallBack(HMONITOR hMonitor, HDC hdc, LPRECT lprc, LPARAM lData)
  1767. {
  1768.     EnumMonitorsArea* pEMA = (EnumMonitorsArea*)lData;
  1769.     
  1770.     if (pEMA->iMonitors > LV_MAX_WORKAREAS - 1)
  1771.     {
  1772.         //ignore the other monitors because we can only handle up to LV_MAX_WORKAREAS
  1773.         //BUGBUG: should we dynamically allocate this?
  1774.         return FALSE;
  1775.     }
  1776.     GetMonitorRect(hMonitor, &pEMA->rcMonitor[pEMA->iMonitors]);
  1777.     GetMonitorWorkArea(hMonitor, &pEMA->rcWorkArea[pEMA->iMonitors]);
  1778.     if(pEMA->iMonitors == 0)
  1779.     {
  1780.         pEMA->rcVirtualMonitor.left = pEMA->rcMonitor[0].left;
  1781.         pEMA->rcVirtualMonitor.top = pEMA->rcMonitor[0].top;
  1782.         pEMA->rcVirtualMonitor.right = pEMA->rcMonitor[0].right;
  1783.         pEMA->rcVirtualMonitor.bottom = pEMA->rcMonitor[0].bottom;
  1784.         pEMA->rcVirtualWorkArea.left = pEMA->rcWorkArea[0].left;
  1785.         pEMA->rcVirtualWorkArea.top = pEMA->rcWorkArea[0].top;
  1786.         pEMA->rcVirtualWorkArea.right = pEMA->rcWorkArea[0].right;
  1787.         pEMA->rcVirtualWorkArea.bottom = pEMA->rcWorkArea[0].bottom;
  1788.     }
  1789.     else
  1790.     {
  1791.         if(pEMA->rcMonitor[pEMA->iMonitors].left < pEMA->rcVirtualMonitor.left)
  1792.         {
  1793.             pEMA->rcVirtualMonitor.left = pEMA->rcMonitor[pEMA->iMonitors].left;
  1794.         }
  1795.         if(pEMA->rcMonitor[pEMA->iMonitors].top < pEMA->rcVirtualMonitor.top)
  1796.         {
  1797.             pEMA->rcVirtualMonitor.top = pEMA->rcMonitor[pEMA->iMonitors].top;
  1798.         }
  1799.         if(pEMA->rcMonitor[pEMA->iMonitors].right > pEMA->rcVirtualMonitor.right)
  1800.         {
  1801.             pEMA->rcVirtualMonitor.right = pEMA->rcMonitor[pEMA->iMonitors].right;
  1802.         }
  1803.         if(pEMA->rcMonitor[pEMA->iMonitors].bottom > pEMA->rcVirtualMonitor.bottom)
  1804.         {
  1805.             pEMA->rcVirtualMonitor.bottom = pEMA->rcMonitor[pEMA->iMonitors].bottom;
  1806.         }
  1807.         if(pEMA->rcWorkArea[pEMA->iMonitors].left < pEMA->rcVirtualWorkArea.left)
  1808.         {
  1809.             pEMA->rcVirtualWorkArea.left = pEMA->rcWorkArea[pEMA->iMonitors].left;
  1810.         }
  1811.         if(pEMA->rcWorkArea[pEMA->iMonitors].top < pEMA->rcVirtualWorkArea.top)
  1812.         {
  1813.             pEMA->rcVirtualWorkArea.top = pEMA->rcWorkArea[pEMA->iMonitors].top;
  1814.         }
  1815.         if(pEMA->rcWorkArea[pEMA->iMonitors].right > pEMA->rcVirtualWorkArea.right)
  1816.         {
  1817.             pEMA->rcVirtualWorkArea.right = pEMA->rcWorkArea[pEMA->iMonitors].right;
  1818.         }
  1819.         if(pEMA->rcWorkArea[pEMA->iMonitors].bottom > pEMA->rcVirtualWorkArea.bottom)
  1820.         {
  1821.             pEMA->rcVirtualWorkArea.bottom = pEMA->rcWorkArea[pEMA->iMonitors].bottom;
  1822.         }
  1823.     }
  1824.     pEMA->iMonitors++;
  1825.     return TRUE;
  1826. }
  1827. void GetMonitorSettings(EnumMonitorsArea* ema)
  1828. {
  1829.     ema->iMonitors = 0;
  1830.     ema->rcVirtualMonitor.left = 0;
  1831.     ema->rcVirtualMonitor.top = 0;
  1832.     ema->rcVirtualMonitor.right = 0;
  1833.     ema->rcVirtualMonitor.bottom = 0;
  1834.     ema->rcVirtualWorkArea.left = 0;
  1835.     ema->rcVirtualWorkArea.top = 0;
  1836.     ema->rcVirtualWorkArea.right = 0;
  1837.     ema->rcVirtualWorkArea.bottom = 0;
  1838.     EnumDisplayMonitors(NULL, NULL, MultiMonEnumAreaCallBack, (LPARAM)ema);
  1839. }
  1840. int _GetWorkAreaIndexWorker(POINT pt, LPCRECT prect, int crect)
  1841. {
  1842.     int iIndex;
  1843.     for (iIndex = 0; iIndex < crect; iIndex++)
  1844.     {
  1845.         if (PtInRect(&prect[iIndex], pt))
  1846.         {
  1847.             return iIndex;
  1848.         }
  1849.     }
  1850.     return -1;
  1851. }
  1852. int GetWorkAreaIndexFromPoint(POINT pt, LPCRECT prect, int crect)
  1853. {
  1854.     ASSERT(crect);
  1855.     // Map to correct coords...
  1856.     pt.x += prect[0].left;
  1857.     pt.y += prect[0].top;
  1858.     return _GetWorkAreaIndexWorker(pt, prect, crect);
  1859. }
  1860. int GetWorkAreaIndex(COMPPOS *pcp, LPCRECT prect, int crect, LPPOINT lpptVirtualTopLeft)
  1861. {
  1862.     POINT ptComp;
  1863.     ptComp.x = pcp->iLeft + lpptVirtualTopLeft->x;
  1864.     ptComp.y = pcp->iTop + lpptVirtualTopLeft->y;
  1865.     return _GetWorkAreaIndexWorker(ptComp, prect, crect);
  1866. }
  1867. // Prepends the Web wallpaper directory or the system directory to szWallpaper, if necessary
  1868. // (i.e., if the path is not specified). The return value is in szWallpaperWithPath, which is iBufSize
  1869. // bytes long
  1870. void GetWallpaperWithPath(LPCTSTR szWallpaper, LPTSTR szWallpaperWithPath, int iBufSize)
  1871. {
  1872.     if(szWallpaper[0] && lstrcmpi(szWallpaper, g_szNone) != 0 && !StrChr(szWallpaper, TEXT('\'))
  1873.             && !StrChr(szWallpaper, TEXT(':'))) // The file could be d:foo.bmp
  1874.     {
  1875.         // If the file is a normal wallpaper, we prepend the windows directory to the filename
  1876.         if(IsNormalWallpaper(szWallpaper))
  1877.         {
  1878.             GetWindowsDirectory(szWallpaperWithPath, iBufSize);
  1879.         }
  1880.         // else we prepend the wallpaper directory to the filename
  1881.         else
  1882.         {
  1883.             GetWallpaperDirName(szWallpaperWithPath, iBufSize);
  1884.         }
  1885.         lstrcat(szWallpaperWithPath, TEXT("\"));
  1886.         lstrcat(szWallpaperWithPath, szWallpaper);
  1887.     }
  1888.     else
  1889.     {
  1890.         lstrcpyn(szWallpaperWithPath, szWallpaper, iBufSize);
  1891.     }
  1892. }
  1893. #ifdef WE_WANT_DEFAULT_WALLPAPER
  1894. // The default wallpaper's name might be different depending on the platform. Hence this function.
  1895. void GetDefaultWallpaper(LPTSTR psz, DWORD cch)
  1896. {
  1897.     GetWallpaperDirName(psz, cch);
  1898.     StrCatBuff(psz, TEXT("\"), cch);
  1899.     if (g_bRunOnMemphis)
  1900.     {
  1901.         StrCatBuff(psz, DESKTOPHTML_DEFAULT_MEMPHIS_WALLPAPER, cch);
  1902.     }
  1903.     else if (g_bRunOnNT5)
  1904.     {
  1905.         StrCatBuff(psz, DESKTOPHTML_DEFAULT_NT5_WALLPAPER, cch);
  1906.     }
  1907.     else
  1908.     {
  1909.         StrCatBuff(psz, DESKTOPHTML_DEFAULT_WALLPAPER, cch);
  1910.     }
  1911. }
  1912. #endif //WE_WANT_DEFAULT_WALLPAPER
  1913. BOOL GetViewAreas(LPRECT lprcViewAreas, int* pnViewAreas)
  1914. {
  1915.     BOOL bRet = FALSE;
  1916.     HWND hwndDesktop = GetShellWindow();    // This is the "normal" desktop
  1917.     
  1918.     if (hwndDesktop && IsWindow(hwndDesktop))
  1919.     {
  1920.         DWORD dwProcID, dwCurrentProcID;
  1921.         
  1922.         GetWindowThreadProcessId(hwndDesktop, &dwProcID);
  1923.         dwCurrentProcID = GetCurrentProcessId();
  1924.         if (dwCurrentProcID == dwProcID) {
  1925.             SendMessage(hwndDesktop, DTM_GETVIEWAREAS, (WPARAM)pnViewAreas, (LPARAM)lprcViewAreas);
  1926.             if (*pnViewAreas <= 0)
  1927.             {
  1928.                 bRet = FALSE;
  1929.             }
  1930.             else
  1931.             {
  1932.                 bRet = TRUE;
  1933.             }
  1934.         }
  1935.         else
  1936.         {
  1937.             bRet = FALSE;
  1938.         }
  1939.     }
  1940.     return bRet;
  1941. }
  1942. // We need to enforce a minimum size for the deskmovr caption since it doesn't look
  1943. // right drawn any smaller
  1944. int GetcyCaption()
  1945. {
  1946.     int cyCaption = GetSystemMetrics(SM_CYSMCAPTION);
  1947.     if (cyCaption < 15)
  1948.         cyCaption = 15;
  1949.     cyCaption -= GetSystemMetrics(SM_CYBORDER);
  1950.     return cyCaption;
  1951. }