ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

background.c
Go to the documentation of this file.
00001 /* $Id: background.c 55583 2012-02-13 20:52:20Z rharabien $
00002  *
00003  * COPYRIGHT:       See COPYING in the top level directory
00004  * PROJECT:         ReactOS Display Control Panel
00005  * FILE:            dll/cpl/desk/background.c
00006  * PURPOSE:         Background property page
00007  *
00008  * PROGRAMMERS:     Trevor McCort (lycan359@gmail.com)
00009  *                  Alexey Minnekhanov (minlexx@rambler.ru)
00010  */
00011 
00012 #include "desk.h"
00013 
00014 #define MAX_BACKGROUNDS     100
00015 
00016 #define PLACEMENT_CENTER    0
00017 #define PLACEMENT_STRETCH   1
00018 #define PLACEMENT_TILE      2
00019 
00020 /* The values in these macros are dependant on the
00021  * layout of the monitor image and they must be adjusted
00022  * if that image will be changed.
00023  */
00024 #define MONITOR_LEFT        18
00025 #define MONITOR_TOP         18
00026 #define MONITOR_RIGHT       168
00027 #define MONITOR_BOTTOM      128
00028 
00029 #define MONITOR_WIDTH       (MONITOR_RIGHT-MONITOR_LEFT)
00030 #define MONITOR_HEIGHT      (MONITOR_BOTTOM-MONITOR_TOP)
00031 
00032 typedef struct
00033 {
00034     BOOL bWallpaper; /* Is this background a wallpaper */
00035 
00036     TCHAR szFilename[MAX_PATH];
00037     TCHAR szDisplayName[256];
00038 
00039 } BackgroundItem;
00040 
00041 typedef struct _DATA
00042 {
00043     BOOL bWallpaperChanged;
00044     BOOL bClrBackgroundChanged;
00045 
00046     BackgroundItem backgroundItems[MAX_BACKGROUNDS];
00047 
00048     PDIBITMAP pWallpaperBitmap;
00049 
00050     int placementSelection;
00051     int backgroundSelection;
00052 
00053     COLORREF custom_colors[16];
00054 
00055     int listViewItemCount;
00056 
00057     HBITMAP hBitmap;
00058     int cxSource;
00059     int cySource;
00060 } DATA, *PDATA;
00061 
00062 GLOBAL_DATA g_GlobalData;
00063 
00064 
00065 /* Add the images in the C:\ReactOS directory and the current wallpaper if any */
00066 static VOID
00067 AddListViewItems(HWND hwndDlg, PDATA pData)
00068 {
00069     WIN32_FIND_DATA fd;
00070     HANDLE hFind;
00071     TCHAR szSearchPath[MAX_PATH];
00072     TCHAR szFileTypes[MAX_PATH];
00073     LV_ITEM listItem;
00074     LV_COLUMN dummy;
00075     RECT clientRect;
00076     HKEY regKey;
00077     SHFILEINFO sfi;
00078     HIMAGELIST himl;
00079     TCHAR wallpaperFilename[MAX_PATH];
00080     DWORD bufferSize = sizeof(wallpaperFilename);
00081     TCHAR buffer[MAX_PATH];
00082     DWORD varType = REG_SZ;
00083     LONG result;
00084     UINT i = 0;
00085     BackgroundItem *backgroundItem = NULL;
00086     TCHAR separators[] = TEXT(";");
00087     TCHAR *token;
00088     HWND hwndBackgroundList;
00089     TCHAR *p;
00090 
00091     hwndBackgroundList = GetDlgItem(hwndDlg, IDC_BACKGROUND_LIST);
00092 
00093     GetClientRect(hwndBackgroundList, &clientRect);
00094 
00095     /* Add a new column to the list */
00096     ZeroMemory(&dummy, sizeof(LV_COLUMN));
00097     dummy.mask      = LVCF_SUBITEM | LVCF_WIDTH;
00098     dummy.iSubItem  = 0;
00099     dummy.cx        = (clientRect.right - clientRect.left) - GetSystemMetrics(SM_CXVSCROLL);
00100     (void)ListView_InsertColumn(hwndBackgroundList, 0, &dummy);
00101 
00102     /* Add the "None" item */
00103     backgroundItem = &pData->backgroundItems[pData->listViewItemCount];
00104     backgroundItem->bWallpaper = FALSE;
00105     LoadString(hApplet,
00106                IDS_NONE,
00107                backgroundItem->szDisplayName,
00108                sizeof(backgroundItem->szDisplayName) / sizeof(TCHAR));
00109 
00110     ZeroMemory(&listItem, sizeof(LV_ITEM));
00111     listItem.mask       = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
00112     listItem.state      = 0;
00113     listItem.pszText    = backgroundItem->szDisplayName;
00114     listItem.iImage     = -1;
00115     listItem.iItem      = pData->listViewItemCount;
00116     listItem.lParam     = pData->listViewItemCount;
00117 
00118     (void)ListView_InsertItem(hwndBackgroundList, &listItem);
00119     ListView_SetItemState(hwndBackgroundList, 
00120                           pData->listViewItemCount,
00121                           LVIS_SELECTED,
00122                           LVIS_SELECTED);
00123 
00124     pData->listViewItemCount++;
00125 
00126     /* Add current wallpaper if any */
00127     RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop"), 0, KEY_ALL_ACCESS, &regKey);
00128 
00129     result = RegQueryValueEx(regKey, TEXT("Wallpaper"), 0, &varType, (LPBYTE)wallpaperFilename, &bufferSize);
00130     if ((result == ERROR_SUCCESS) && (_tcslen(wallpaperFilename) > 0))
00131     {
00132         /* Allow environment variables in file name */
00133         if (ExpandEnvironmentStrings(wallpaperFilename, buffer, MAX_PATH))
00134         {
00135             _tcscpy(wallpaperFilename, buffer);
00136         }
00137 
00138         himl = (HIMAGELIST)SHGetFileInfo(wallpaperFilename,
00139                                          0,
00140                                          &sfi,
00141                                          sizeof(sfi),
00142                                          SHGFI_SYSICONINDEX | SHGFI_SMALLICON |
00143                                          SHGFI_DISPLAYNAME);
00144 
00145         if (himl != NULL)
00146         {
00147             if (i++ == 0)
00148             {
00149                 (void)ListView_SetImageList(hwndBackgroundList, himl, LVSIL_SMALL);
00150             }
00151 
00152             backgroundItem = &pData->backgroundItems[pData->listViewItemCount];
00153 
00154             backgroundItem->bWallpaper = TRUE;
00155 
00156             _tcscpy(backgroundItem->szDisplayName, sfi.szDisplayName);
00157             p = _tcsrchr(backgroundItem->szDisplayName, _T('.'));
00158             if (p)
00159                 *p = (TCHAR)0;
00160             _tcscpy(backgroundItem->szFilename, wallpaperFilename);
00161 
00162             ZeroMemory(&listItem, sizeof(LV_ITEM));
00163             listItem.mask       = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
00164             listItem.state      = 0;
00165             listItem.pszText    = backgroundItem->szDisplayName;
00166             listItem.iImage     = sfi.iIcon;
00167             listItem.iItem      = pData->listViewItemCount;
00168             listItem.lParam     = pData->listViewItemCount;
00169 
00170             (void)ListView_InsertItem(hwndBackgroundList, &listItem);
00171             ListView_SetItemState(hwndBackgroundList,
00172                                   pData->listViewItemCount,
00173                                   LVIS_SELECTED,
00174                                   LVIS_SELECTED);
00175 
00176             pData->listViewItemCount++;
00177         }
00178     }
00179 
00180     RegCloseKey(regKey);
00181 
00182     /* Add all the images in the C:\ReactOS directory. */
00183 
00184     LoadString(hApplet, IDS_SUPPORTED_EXT, szFileTypes, sizeof(szFileTypes) / sizeof(TCHAR));
00185 
00186     token = _tcstok(szFileTypes, separators);
00187     while (token != NULL)
00188     {
00189         GetWindowsDirectory(szSearchPath, MAX_PATH);
00190         _tcscat(szSearchPath, TEXT("\\"));
00191         _tcscat(szSearchPath, token);
00192 
00193         hFind = FindFirstFile(szSearchPath, &fd);
00194         while (hFind != INVALID_HANDLE_VALUE)
00195         {
00196             TCHAR filename[MAX_PATH];
00197 
00198             GetWindowsDirectory(filename, MAX_PATH);
00199 
00200             _tcscat(filename, TEXT("\\"));
00201             _tcscat(filename, fd.cFileName);
00202 
00203             /* Don't add any hidden bitmaps. Also don't add current wallpaper once more. */
00204             if (((fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) == 0) && (_tcscmp(wallpaperFilename, filename) != 0))
00205             {
00206                 himl = (HIMAGELIST)SHGetFileInfo(filename,
00207                                                 0,
00208                                                 &sfi,
00209                                                 sizeof(sfi),
00210                                                 SHGFI_SYSICONINDEX | SHGFI_SMALLICON |
00211                                                 SHGFI_DISPLAYNAME);
00212 
00213                 if (himl == NULL)
00214                     break;
00215 
00216                 if (i++ == 0)
00217                 {
00218                     (void)ListView_SetImageList(hwndBackgroundList, himl, LVSIL_SMALL);
00219                 }
00220 
00221                 backgroundItem = &pData->backgroundItems[pData->listViewItemCount];
00222 
00223                 backgroundItem->bWallpaper = TRUE;
00224 
00225                 _tcscpy(backgroundItem->szDisplayName, sfi.szDisplayName);
00226                 p = _tcsrchr(backgroundItem->szDisplayName, _T('.'));
00227                 if (p)
00228                     *p = (TCHAR)0;
00229                 _tcscpy(backgroundItem->szFilename, filename);
00230 
00231                 ZeroMemory(&listItem, sizeof(LV_ITEM));
00232                 listItem.mask       = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
00233                 listItem.pszText    = backgroundItem->szDisplayName;
00234                 listItem.state      = 0;
00235                 listItem.iImage     = sfi.iIcon;
00236                 listItem.iItem      = pData->listViewItemCount;
00237                 listItem.lParam     = pData->listViewItemCount;
00238 
00239                 (void)ListView_InsertItem(hwndBackgroundList, &listItem);
00240 
00241                 pData->listViewItemCount++;
00242             }
00243 
00244             if(!FindNextFile(hFind, &fd))
00245                 hFind = INVALID_HANDLE_VALUE;
00246         }
00247 
00248         token = _tcstok(NULL, separators);
00249     }
00250 }
00251 
00252 
00253 static VOID
00254 InitBackgroundDialog(HWND hwndDlg, PDATA pData)
00255 {
00256     TCHAR szString[256];
00257     HKEY regKey;
00258     TCHAR szBuffer[2];
00259     DWORD bufferSize = sizeof(szBuffer);
00260     DWORD varType = REG_SZ;
00261     LONG result;
00262     BITMAP bitmap;
00263 
00264     AddListViewItems(hwndDlg, pData);
00265 
00266     LoadString(hApplet, IDS_CENTER, szString, sizeof(szString) / sizeof(TCHAR));
00267     SendDlgItemMessage(hwndDlg, IDC_PLACEMENT_COMBO, CB_INSERTSTRING, PLACEMENT_CENTER, (LPARAM)szString);
00268 
00269     LoadString(hApplet, IDS_STRETCH, szString, sizeof(szString) / sizeof(TCHAR));
00270     SendDlgItemMessage(hwndDlg, IDC_PLACEMENT_COMBO, CB_INSERTSTRING, PLACEMENT_STRETCH, (LPARAM)szString);
00271 
00272     LoadString(hApplet, IDS_TILE, szString, sizeof(szString) / sizeof(TCHAR));
00273     SendDlgItemMessage(hwndDlg, IDC_PLACEMENT_COMBO, CB_INSERTSTRING, PLACEMENT_TILE, (LPARAM)szString);
00274 
00275     /* Load the default settings from the registry */
00276     result = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop"), 0, KEY_ALL_ACCESS, &regKey);
00277     if (result != ERROR_SUCCESS)
00278     {
00279         /* reg key open failed; maybe it does not exist? create it! */
00280         DWORD dwDisposition = 0;
00281         result = RegCreateKeyEx( HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop"), 0, NULL, 0, KEY_ALL_ACCESS, NULL,
00282             &regKey, &dwDisposition );
00283         /* Now the key must be created & opened and regKey points to opened key */
00284         /* On error result will not contain ERROR_SUCCESS. I don't know how to handle */
00285         /* this case :( */
00286     }
00287 
00288     result = RegQueryValueEx(regKey, TEXT("WallpaperStyle"), 0, &varType, (LPBYTE)szBuffer, &bufferSize);
00289     if (result == ERROR_SUCCESS)
00290     {
00291         if (_ttoi(szBuffer) == 0)
00292         {
00293             SendDlgItemMessage(hwndDlg, IDC_PLACEMENT_COMBO, CB_SETCURSEL, PLACEMENT_CENTER, 0);
00294             pData->placementSelection = PLACEMENT_CENTER;
00295         }
00296 
00297         if (_ttoi(szBuffer) == 2)
00298         {
00299             SendDlgItemMessage(hwndDlg, IDC_PLACEMENT_COMBO, CB_SETCURSEL, PLACEMENT_STRETCH, 0);
00300             pData->placementSelection = PLACEMENT_STRETCH;
00301         }
00302     }
00303     else
00304     {
00305         SendDlgItemMessage(hwndDlg, IDC_PLACEMENT_COMBO, CB_SETCURSEL, PLACEMENT_CENTER, 0);
00306         pData->placementSelection = PLACEMENT_CENTER;
00307     }
00308 
00309     result = RegQueryValueEx(regKey, TEXT("TileWallpaper"), 0, &varType, (LPBYTE)szBuffer, &bufferSize);
00310     if (result == ERROR_SUCCESS)
00311     {
00312         if (_ttoi(szBuffer) == 1)
00313         {
00314             SendDlgItemMessage(hwndDlg, IDC_PLACEMENT_COMBO, CB_SETCURSEL, PLACEMENT_TILE, 0);
00315             pData->placementSelection = PLACEMENT_TILE;
00316         }
00317     }
00318 
00319     RegCloseKey(regKey);
00320 
00321     pData->hBitmap = (HBITMAP) LoadImage(hApplet, MAKEINTRESOURCE(IDC_MONITOR), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
00322     if (pData->hBitmap != NULL)
00323     {
00324         GetObject(pData->hBitmap, sizeof(BITMAP), &bitmap);
00325 
00326         pData->cxSource = bitmap.bmWidth;
00327         pData->cySource = bitmap.bmHeight;
00328     }
00329 }
00330 
00331 
00332 static VOID
00333 OnColorButton(HWND hwndDlg, PDATA pData)
00334 {
00335     /* Load custom colors from Registry */
00336     HKEY hKey = NULL;
00337     LONG res = ERROR_SUCCESS;
00338     CHOOSECOLOR cc;
00339 
00340     res = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Appearance"), 0, NULL, 0,
00341         KEY_ALL_ACCESS, NULL, &hKey, NULL);
00342     /* Now the key is either created or opened existing, if res == ERROR_SUCCESS */
00343     if (res == ERROR_SUCCESS)
00344     {
00345         /* Key opened */
00346         DWORD dwType = REG_BINARY;
00347         DWORD cbData = sizeof(pData->custom_colors);
00348         res = RegQueryValueEx(hKey, TEXT("CustomColors"), NULL, &dwType,
00349             (LPBYTE)pData->custom_colors, &cbData);
00350         RegCloseKey(hKey);
00351         hKey = NULL;
00352     }
00353 
00354     /* Launch ChooseColor() dialog */
00355 
00356     cc.lStructSize = sizeof(CHOOSECOLOR);
00357     cc.hwndOwner = hwndDlg;
00358     cc.hInstance = NULL;
00359     cc.rgbResult = g_GlobalData.desktop_color;
00360     cc.lpCustColors = pData->custom_colors;
00361     cc.Flags = CC_ANYCOLOR | /* Causes the dialog box to display all available colors in the set of basic colors.  */
00362                CC_FULLOPEN | /* opens dialog in full size */
00363                CC_RGBINIT ;  /* init chosen color by rgbResult value */
00364     cc.lCustData = 0;
00365     cc.lpfnHook = NULL;
00366     cc.lpTemplateName = NULL;
00367     if (ChooseColor(&cc))
00368     {
00369         /* Save selected color to var */
00370         g_GlobalData.desktop_color = cc.rgbResult;
00371         pData->bClrBackgroundChanged = TRUE;
00372 
00373         /* Apply button will be activated */
00374         PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
00375 
00376         /* Window will be updated :) */
00377         InvalidateRect(GetDlgItem(hwndDlg, IDC_BACKGROUND_PREVIEW), NULL, TRUE);
00378 
00379         /* Save custom colors to reg. To this moment key must be created already. See above */
00380         res = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Appearance"), 0,
00381             KEY_WRITE, &hKey);
00382         if (res == ERROR_SUCCESS)
00383         {
00384             /* Key opened */
00385             RegSetValueEx(hKey, TEXT("CustomColors"), 0, REG_BINARY,
00386                 (const BYTE *)pData->custom_colors, sizeof(pData->custom_colors));
00387             RegCloseKey(hKey);
00388             hKey = NULL;
00389         }
00390     }
00391 }
00392 
00393 
00394 /*
00395  * ListView_FindItem() Macro: Searches for a list-view item with the specified
00396  * characteristics. Returns the index of the item if successful, or -1 otherwise
00397  */
00398 static BOOL
00399 CheckListViewFilenameExists(HWND hwndList, LPCTSTR tszFileName)
00400 {
00401     LVFINDINFO lvfi;
00402     int retVal;
00403 
00404     lvfi.flags = LVFI_STRING; /* Search item by EXACT string */
00405     lvfi.psz   = tszFileName; /* String to search */
00406 
00407     /* Other items of this structure are not valid, besacuse flags are not set. */
00408     retVal = ListView_FindItem(hwndList, -1, &lvfi);
00409     if (retVal != -1)
00410         return TRUE; /* item found! */
00411 
00412     return FALSE; /* item not found. */
00413 }
00414 
00415 
00416 static VOID
00417 OnBrowseButton(HWND hwndDlg, PDATA pData)
00418 {
00419     OPENFILENAME ofn;
00420     TCHAR filename[MAX_PATH];
00421     TCHAR fileTitle[256];
00422     TCHAR filter[MAX_PATH];
00423     BackgroundItem *backgroundItem = NULL;
00424     SHFILEINFO sfi;
00425     LV_ITEM listItem;
00426     HWND hwndBackgroundList;
00427     TCHAR *p;
00428 
00429     hwndBackgroundList = GetDlgItem(hwndDlg, IDC_BACKGROUND_LIST);
00430 
00431     ZeroMemory(&ofn, sizeof(OPENFILENAME));
00432 
00433     ofn.lStructSize = sizeof(OPENFILENAME);
00434     ofn.hwndOwner = hwndDlg;
00435     ofn.lpstrFile = filename;
00436 
00437     LoadString(hApplet, IDS_BACKGROUND_COMDLG_FILTER, filter, sizeof(filter) / sizeof(TCHAR));
00438 
00439     /* Set lpstrFile[0] to '\0' so that GetOpenFileName does not
00440      * use the contents of szFile to initialize itself */
00441     ofn.lpstrFile[0] = TEXT('\0');
00442     ofn.nMaxFile = MAX_PATH;
00443     ofn.lpstrFilter = filter;
00444     ofn.nFilterIndex = 0;
00445     ofn.lpstrFileTitle = fileTitle;
00446     ofn.nMaxFileTitle = 256;
00447     ofn.lpstrInitialDir = NULL;
00448     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
00449 
00450     if (GetOpenFileName(&ofn) == TRUE)
00451     {
00452         /* Check if there is already a entry that holds this filename */
00453         if (CheckListViewFilenameExists(hwndBackgroundList, ofn.lpstrFileTitle) == TRUE)
00454             return;
00455 
00456         if (pData->listViewItemCount > (MAX_BACKGROUNDS - 1))
00457             return;
00458 
00459         SHGetFileInfo(filename,
00460                       0,
00461                       &sfi,
00462                       sizeof(sfi),
00463                       SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_DISPLAYNAME);
00464 
00465         backgroundItem = &pData->backgroundItems[pData->listViewItemCount];
00466 
00467         backgroundItem->bWallpaper = TRUE;
00468 
00469         _tcscpy(backgroundItem->szDisplayName, sfi.szDisplayName);
00470         p = _tcsrchr(backgroundItem->szDisplayName, _T('.'));
00471         if (p)
00472             *p = (TCHAR)0;
00473         _tcscpy(backgroundItem->szFilename, filename);
00474 
00475         ZeroMemory(&listItem, sizeof(LV_ITEM));
00476         listItem.mask       = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
00477         listItem.state      = 0;
00478         listItem.pszText    = backgroundItem->szDisplayName;
00479         listItem.iImage     = sfi.iIcon;
00480         listItem.iItem      = pData->listViewItemCount;
00481         listItem.lParam     = pData->listViewItemCount;
00482 
00483         (void)ListView_InsertItem(hwndBackgroundList, &listItem);
00484         ListView_SetItemState(hwndBackgroundList,
00485                               pData->listViewItemCount,
00486                               LVIS_SELECTED,
00487                               LVIS_SELECTED);
00488         SendMessage(hwndBackgroundList, WM_VSCROLL, SB_BOTTOM, 0);
00489 
00490         pData->listViewItemCount++;
00491     }
00492 }
00493 
00494 
00495 static VOID
00496 ListViewItemChanged(HWND hwndDlg, PDATA pData, int itemIndex)
00497 {
00498     BackgroundItem *backgroundItem = NULL;
00499 
00500     pData->backgroundSelection = itemIndex;
00501     backgroundItem = &pData->backgroundItems[pData->backgroundSelection];
00502 
00503     if (pData->pWallpaperBitmap != NULL)
00504     {
00505         DibFreeImage(pData->pWallpaperBitmap);
00506         pData->pWallpaperBitmap = NULL;
00507     }
00508 
00509     if (backgroundItem->bWallpaper == TRUE)
00510     {
00511         pData->pWallpaperBitmap = DibLoadImage(backgroundItem->szFilename);
00512 
00513         if (pData->pWallpaperBitmap == NULL)
00514             return;
00515     }
00516 
00517     pData->bWallpaperChanged = TRUE;
00518 
00519     InvalidateRect(GetDlgItem(hwndDlg, IDC_BACKGROUND_PREVIEW),
00520                    NULL, TRUE);
00521 
00522     EnableWindow(GetDlgItem(hwndDlg, IDC_PLACEMENT_COMBO),
00523                  backgroundItem->bWallpaper);
00524 
00525     PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
00526 }
00527 
00528 
00529 static VOID
00530 DrawBackgroundPreview(LPDRAWITEMSTRUCT draw, PDATA pData)
00531 {
00532     float scaleX;
00533     float scaleY;
00534     int scaledWidth;
00535     int scaledHeight;
00536     int posX, desX;
00537     int posY, desY;
00538     HBRUSH hBrush;
00539     int x;
00540     int y;
00541     HDC hDC;
00542     HGDIOBJ hOldObj;
00543     RECT rcItem = {
00544         MONITOR_LEFT,
00545         MONITOR_TOP,
00546         MONITOR_RIGHT,
00547         MONITOR_BOTTOM
00548     };
00549 
00550     hDC = CreateCompatibleDC(draw->hDC);
00551     hOldObj = SelectObject(hDC, pData->hBitmap);
00552 
00553     if (pData->backgroundItems[pData->backgroundSelection].bWallpaper == FALSE)
00554     {
00555         /* Update desktop background color image */
00556         hBrush = CreateSolidBrush(g_GlobalData.desktop_color);
00557         FillRect(hDC, &rcItem, hBrush);
00558         DeleteObject(hBrush);
00559     }
00560     else
00561     if (pData->pWallpaperBitmap != NULL)
00562     {
00563         scaleX = ((float)GetSystemMetrics(SM_CXSCREEN) - 1) / (float)MONITOR_WIDTH;
00564         scaleY = ((float)GetSystemMetrics(SM_CYSCREEN) - 1) / (float)MONITOR_HEIGHT;
00565 
00566         scaledWidth = (int)(pData->pWallpaperBitmap->width / scaleX);
00567         scaledHeight = (int)(pData->pWallpaperBitmap->height / scaleY);
00568 
00569         FillRect(hDC, &rcItem, GetSysColorBrush(COLOR_BACKGROUND));
00570 
00571         SetStretchBltMode(hDC, COLORONCOLOR);
00572 
00573         switch (pData->placementSelection)
00574         {
00575             case PLACEMENT_CENTER:
00576                 posX = (MONITOR_WIDTH - scaledWidth + 1) / 2;
00577                 posY = (MONITOR_HEIGHT - scaledHeight + 1) / 2;
00578                 desX = 0;
00579                 desY = 0;
00580 
00581                 if (posX < 0) { desX = -posX / 2; posX = 0; }
00582                 if (posY < 0) { desY = -posY / 2; posY = 0; }
00583 
00584                 if (scaledWidth > MONITOR_WIDTH)
00585                     scaledWidth = MONITOR_WIDTH;
00586 
00587                 if (scaledHeight > MONITOR_HEIGHT)
00588                     scaledHeight = MONITOR_HEIGHT;
00589 
00590                 StretchDIBits(hDC,
00591                               MONITOR_LEFT+posX,
00592                               MONITOR_TOP+posY,
00593                               scaledWidth,
00594                               scaledHeight,
00595                               desX,
00596                               desY,
00597                               pData->pWallpaperBitmap->width - (int)(desX * scaleX),
00598                               pData->pWallpaperBitmap->height - (int)(desY * scaleY),
00599                               pData->pWallpaperBitmap->bits,
00600                               pData->pWallpaperBitmap->info,
00601                               DIB_RGB_COLORS,
00602                               SRCCOPY);
00603                 break;
00604 
00605             case PLACEMENT_STRETCH:
00606                 StretchDIBits(hDC,
00607                               MONITOR_LEFT,
00608                               MONITOR_TOP,
00609                               MONITOR_WIDTH,
00610                               MONITOR_HEIGHT,
00611                               0,
00612                               0,
00613                               pData->pWallpaperBitmap->width,
00614                               pData->pWallpaperBitmap->height,
00615                               pData->pWallpaperBitmap->bits,
00616                               pData->pWallpaperBitmap->info,
00617                               DIB_RGB_COLORS,
00618                               SRCCOPY);
00619                 break;
00620 
00621             case PLACEMENT_TILE:
00622                 for (y = 0; y < MONITOR_HEIGHT; y += scaledHeight)
00623                 {
00624                     for (x = 0; x < MONITOR_WIDTH; x += scaledWidth)
00625                     {
00626                         if ((MONITOR_WIDTH-x) >= scaledWidth)
00627                             posX = scaledWidth;
00628                         else
00629                             posX = MONITOR_WIDTH-x;
00630 
00631 
00632                         if ((MONITOR_HEIGHT-y) >= scaledHeight)
00633                             posY = scaledHeight;
00634                         else
00635                             posY = MONITOR_HEIGHT-y;
00636 
00637                         StretchDIBits(hDC,
00638                                       MONITOR_LEFT + x,
00639                                       MONITOR_TOP + y,
00640                                       posX,
00641                                       posY,
00642                                       0,
00643                                       0,
00644                                       pData->pWallpaperBitmap->width * posX / scaledWidth,
00645                                       pData->pWallpaperBitmap->height * posY / scaledHeight,
00646                                       pData->pWallpaperBitmap->bits,
00647                                       pData->pWallpaperBitmap->info,
00648                                       DIB_RGB_COLORS,
00649                                       SRCCOPY);
00650                     }
00651 
00652                 }
00653 
00654                 break;
00655         }
00656     }
00657 
00658     GdiTransparentBlt(draw->hDC,
00659                       draw->rcItem.left, draw->rcItem.top,
00660                       draw->rcItem.right-draw->rcItem.left+1,
00661                       draw->rcItem.bottom-draw->rcItem.top+1,
00662                       hDC,
00663                       0, 0,
00664                       pData->cxSource, pData->cySource,
00665                       0xFF00FF);
00666 
00667     SelectObject(hDC, hOldObj);
00668     DeleteDC(hDC);
00669 }
00670 
00671 
00672 static VOID
00673 SetWallpaper(PDATA pData)
00674 {
00675     HKEY regKey;
00676 
00677     RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop"), 0, KEY_ALL_ACCESS, &regKey);
00678 
00679     if (pData->placementSelection == PLACEMENT_TILE)
00680     {
00681         RegSetValueEx(regKey, TEXT("TileWallpaper"), 0, REG_SZ, (BYTE *)TEXT("1"), sizeof(TCHAR) * 2);
00682         RegSetValueEx(regKey, TEXT("WallpaperStyle"), 0, REG_SZ, (BYTE *)TEXT("0"), sizeof(TCHAR) * 2);
00683     }
00684 
00685     if (pData->placementSelection == PLACEMENT_CENTER)
00686     {
00687         RegSetValueEx(regKey, TEXT("TileWallpaper"), 0, REG_SZ, (BYTE *)TEXT("0"), sizeof(TCHAR) * 2);
00688         RegSetValueEx(regKey, TEXT("WallpaperStyle"), 0, REG_SZ, (BYTE *)TEXT("0"), sizeof(TCHAR) * 2);
00689     }
00690 
00691     if (pData->placementSelection == PLACEMENT_STRETCH)
00692     {
00693         RegSetValueEx(regKey, TEXT("TileWallpaper"), 0, REG_SZ, (BYTE *)TEXT("0"), sizeof(TCHAR) * 2);
00694         RegSetValueEx(regKey, TEXT("WallpaperStyle"), 0, REG_SZ, (BYTE *)TEXT("2"), sizeof(TCHAR) * 2);
00695     }
00696 
00697     RegCloseKey(regKey);
00698 
00699     if (pData->backgroundItems[pData->backgroundSelection].bWallpaper == TRUE)
00700     {
00701         SystemParametersInfo(SPI_SETDESKWALLPAPER,
00702                              0,
00703                              pData->backgroundItems[pData->backgroundSelection].szFilename,
00704                              SPIF_UPDATEINIFILE);
00705     }
00706     else
00707     {
00708         SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*) TEXT(""), SPIF_UPDATEINIFILE);
00709     }
00710 }
00711 
00712 
00713 /* Change system color */
00714 static VOID
00715 SetDesktopBackColor(HWND hwndDlg, DATA *pData)
00716 {
00717     INT iElement = COLOR_BACKGROUND;
00718     HKEY hKey;
00719     LONG result;
00720     TCHAR clText[16];
00721     BYTE red, green, blue;
00722     DWORD dwDispostion;
00723 
00724     if( !SetSysColors( 1, &iElement, &g_GlobalData.desktop_color ) )
00725         MessageBox(hwndDlg, TEXT("SetSysColor() failed!"), /* these error texts can need internationalization? */
00726             TEXT("Error!"), MB_ICONSTOP );
00727 
00728     result = RegCreateKeyEx( HKEY_CURRENT_USER, TEXT("Control Panel\\Colors"), 0, NULL, 0,
00729         KEY_ALL_ACCESS, NULL, &hKey, &dwDispostion );
00730     if (result != ERROR_SUCCESS)
00731     {
00732         red   = GetRValue(g_GlobalData.desktop_color);
00733         green = GetGValue(g_GlobalData.desktop_color);
00734         blue  = GetBValue(g_GlobalData.desktop_color);
00735         /* Format string to be set to registry */
00736         wsprintf(clText, TEXT("%d %d %d"), red, green, blue);
00737         RegSetValueEx(hKey, TEXT("Background"), 0, REG_SZ, (BYTE *)clText,
00738                       (lstrlen(clText) + 1) * sizeof(TCHAR));
00739         RegCloseKey(hKey);
00740     }
00741 }
00742 
00743 INT_PTR CALLBACK
00744 BackgroundPageProc(HWND hwndDlg,
00745                    UINT uMsg,
00746                    WPARAM wParam,
00747                    LPARAM lParam)
00748 {
00749     PDATA pData;
00750 
00751     pData = (PDATA)GetWindowLongPtr(hwndDlg, DWLP_USER);
00752 
00753     switch (uMsg)
00754     {
00755         case WM_INITDIALOG:
00756             pData = (DATA*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DATA));
00757             SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pData);
00758             InitBackgroundDialog(hwndDlg, pData);
00759             break;
00760 
00761         case WM_COMMAND:
00762             {
00763                 DWORD controlId = LOWORD(wParam);
00764                 DWORD command   = HIWORD(wParam);
00765 
00766                 switch (controlId)
00767                 {
00768                     case IDC_COLOR_BUTTON:
00769                         if (command == BN_CLICKED)
00770                             OnColorButton(hwndDlg, pData);
00771                         break;
00772 
00773                     case IDC_BROWSE_BUTTON:
00774                         if (command == BN_CLICKED)
00775                             OnBrowseButton(hwndDlg, pData);
00776                         break;
00777 
00778                     case IDC_PLACEMENT_COMBO:
00779                         if (command == CBN_SELCHANGE)
00780                         {
00781                             pData->placementSelection = (int)SendDlgItemMessage(hwndDlg, IDC_PLACEMENT_COMBO, CB_GETCURSEL, 0, 0);
00782 
00783                             InvalidateRect(GetDlgItem(hwndDlg, IDC_BACKGROUND_PREVIEW), NULL, TRUE);
00784 
00785                             PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
00786                         }
00787                         break;
00788                 }
00789             } break;
00790 
00791         case WM_DRAWITEM:
00792             {
00793                 LPDRAWITEMSTRUCT drawItem;
00794                 drawItem = (LPDRAWITEMSTRUCT)lParam;
00795 
00796                 if (drawItem->CtlID == IDC_BACKGROUND_PREVIEW)
00797                 {
00798                     DrawBackgroundPreview(drawItem, pData);
00799                 }
00800 
00801             }
00802             break;
00803 
00804         case WM_NOTIFY:
00805             {
00806                 LPNMHDR lpnm = (LPNMHDR)lParam;
00807 
00808                 switch(lpnm->code)
00809                 {
00810                     case PSN_APPLY:
00811                         if(pData->bWallpaperChanged)
00812                             SetWallpaper(pData);
00813                         if(pData->bClrBackgroundChanged)
00814                             SetDesktopBackColor(hwndDlg, pData);
00815                         SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)_T(""));
00816                         return TRUE;
00817 
00818                     case LVN_ITEMCHANGED:
00819                         {
00820                             LPNMLISTVIEW nm = (LPNMLISTVIEW)lParam;
00821 
00822                             if ((nm->uNewState & LVIS_SELECTED) == 0)
00823                                 return FALSE;
00824 
00825                             ListViewItemChanged(hwndDlg, pData, nm->iItem);
00826                         }
00827                         break;
00828                 }
00829             }
00830             break;
00831 
00832         case WM_DESTROY:
00833             if (pData->pWallpaperBitmap != NULL)
00834                 DibFreeImage(pData->pWallpaperBitmap);
00835 
00836             DeleteObject(pData->hBitmap);
00837             HeapFree(GetProcessHeap(), 0, pData);
00838             break;
00839     }
00840 
00841     return FALSE;
00842 }

Generated on Sat May 26 2012 04:19:42 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.