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

Windows Kernel

Development Platform:

Visual C++

  1. /*
  2. **------------------------------------------------------------------------------
  3. ** Module:  Disk Cleanup Applet
  4. ** File:    seldrive.cpp
  5. **
  6. ** Purpose: Defines the IEmptyVoluemCacheCallback interface for 
  7. **          the cleanup manager.
  8. ** Notes:   
  9. ** Mod Log: Created by Jason Cobb (12/97)
  10. **
  11. ** Copyright (c)1997 Microsoft Corporation, All Rights Reserved
  12. **------------------------------------------------------------------------------
  13. */
  14. /*
  15. **------------------------------------------------------------------------------
  16. ** Project include files
  17. **------------------------------------------------------------------------------
  18. */
  19. #include "common.h"
  20. #include "seldrive.h"   
  21. #include "msprintf.h"
  22. #include <regstr.h>
  23. #include <help.h>
  24. /*
  25. **------------------------------------------------------------------------------
  26. ** Definitions
  27. **------------------------------------------------------------------------------
  28. */
  29. const DWORD aSelDriveHelpIDs[]=
  30. {
  31.     IDC_SELDRIVE_COMBO,             IDH_CLEANMGR_SELDRIVE,
  32.     IDOK,                           IDH_CLEANMGR_SELDRIVE_OK,
  33.     IDCANCEL,                       IDH_CLEANMGR_SELDRIVE_EXIT,                   
  34. IDC_SELDRIVE_TEXT,              ((DWORD)-1),
  35. IDC_SELDRIVE_TEXT2,             ((DWORD)-1),
  36. 0, 0
  37. };
  38. static struct tagDriveSelectionData
  39. {
  40.     drenum  dreDef;     // default drive to choose
  41.     drvlist dvl;        // type of drive list
  42.     drenum  dreChose;   // drive selected at end of dialog
  43. } dsd;
  44. /*
  45. **------------------------------------------------------------------------------
  46. ** Prototypes
  47. **------------------------------------------------------------------------------
  48. */
  49. INT_PTR CALLBACK
  50. SelectDriveProc(
  51. HWND hDlg,
  52. UINT Message,
  53. WPARAM wParam,
  54. LPARAM lParam
  55. );
  56. BOOL
  57. fillSelDriveList(
  58.     HWND hDlg
  59.     );
  60. WPARAM
  61. AddComboString(
  62.     HWND hDlg, 
  63.     int id, 
  64.     TCHAR *psz, 
  65.     DWORD val
  66.     );
  67. void 
  68. SelectComboItem(
  69.     HWND hDlg, 
  70.     int id, 
  71.     WPARAM w
  72.     );
  73. void 
  74. SelectDriveDlgDrawItem(
  75.     HWND hDlg, 
  76.     DRAWITEMSTRUCT *lpdis, 
  77.     BOOL bHighlightBackground    
  78.     );
  79. #ifdef NEC_98
  80. drenum
  81. GetNECBootDrive(void);
  82. #endif
  83. /*
  84. **------------------------------------------------------------------------------
  85. ** Routines
  86. **------------------------------------------------------------------------------
  87. */
  88. /*
  89. **------------------------------------------------------------------------------
  90. ** SelectDrive
  91. **
  92. ** Purpose:    Main entry point for the "Select Drive" dialog.
  93. ** Parameters:
  94. **    dre   -  default drive to choose
  95. **    dvl   -  type of drives to list
  96. ** Return:     TRUE if the user selected a drive, FALSE if a user selected Exit.
  97. **             If a user does select a drive then that drive is returned in the
  98. **             szDrive string pointer.
  99. ** Notes;
  100. ** Mod Log:    Created by Jason Cobb (12/97)
  101. **------------------------------------------------------------------------------
  102. */
  103. BOOL 
  104. SelectDrive(
  105.     PTSTR szDrive, 
  106.     drvlist dvl
  107.     )
  108. {
  109.     drenum   dre;
  110.     GetDriveFromString(szDrive, dre);
  111.     dsd.dreDef  = dre;
  112.     dsd.dvl     = dvl;
  113.     if (DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_SELDRIVE), NULL, SelectDriveProc) != IDOK)
  114.         return FALSE;
  115.     CreateStringFromDrive(dsd.dreChose, szDrive, 4);
  116.     
  117.     return TRUE;
  118. }
  119. /*
  120. **------------------------------------------------------------------------------
  121. ** SelectDriveProc
  122. **
  123. ** Purpose:    Callback for the "Select Drive" dialog.
  124. ** Parameters:
  125. **  hDlg
  126. **  Message
  127. **  wParam
  128. **  lParam
  129. **
  130. ** Return:     
  131. ** Notes;
  132. ** Mod Log:    Created by Jason Cobb (12/97)
  133. **------------------------------------------------------------------------------
  134. */
  135. INT_PTR CALLBACK
  136. SelectDriveProc(
  137. HWND hDlg,
  138. UINT Message,
  139. WPARAM wParam,
  140. LPARAM lParam
  141. )
  142. {
  143.     PAINTSTRUCT ps;
  144.     switch (Message)
  145.     {
  146.         case WM_INITDIALOG:
  147.             if (!fillSelDriveList (hDlg))
  148.             {
  149.                 EndDialog (hDlg, IDCANCEL);
  150.             }
  151.             SetFocus(GetDlgItem(hDlg, IDC_SELDRIVE_COMBO));
  152.             break;
  153.         case WM_DESTROY:
  154.             EndDialog (hDlg, IDCANCEL);
  155.             break;
  156.         case WM_HELP:
  157.             WinHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, NULL,
  158.                     HELP_WM_HELP, (DWORD_PTR)(LPTSTR)aSelDriveHelpIDs);
  159.             return TRUE;
  160.         case WM_CONTEXTMENU:
  161.             WinHelp((HWND) wParam, NULL, HELP_CONTEXTMENU,
  162.                     (DWORD_PTR)(LPVOID)aSelDriveHelpIDs);
  163.             return TRUE;
  164.         case WM_PAINT:
  165.             BeginPaint (hDlg, &ps);
  166.             EndPaint (hDlg, &ps);
  167.             break;
  168.         
  169.         case WM_DRAWITEM:
  170.             SelectDriveDlgDrawItem(hDlg, (DRAWITEMSTRUCT *)lParam, TRUE);
  171.             break;
  172.         case WM_COMMAND:
  173.             switch(wParam)
  174.             {
  175.                 case IDOK:
  176.                     dsd.dreChose = (drenum)SendDlgItemMessage(hDlg, IDC_SELDRIVE_COMBO, CB_GETITEMDATA, 
  177.                                   (WPARAM)SendDlgItemMessage(hDlg, IDC_SELDRIVE_COMBO, CB_GETCURSEL, 0, 0L), 0L);
  178.                     //Fall through
  179.                     
  180.                 case IDCANCEL:
  181.                     EndDialog (hDlg, wParam);
  182.                     break;
  183.             }
  184.             break;
  185.         default:
  186.             return FALSE;
  187.         }
  188.     return TRUE;
  189. }
  190. void 
  191. SelectDriveDlgDrawItem(
  192.     HWND hDlg, 
  193.     DRAWITEMSTRUCT *lpdis, 
  194.     BOOL bHighlightBackground    
  195.     )
  196. {
  197.     HDC             hdc = lpdis->hDC;
  198.     TCHAR           szText[MAX_DESC_LEN*2];
  199.     SIZE            size;
  200.     drenum          dre;
  201.     HICON           hIcon = NULL;
  202.     DWORD       dwExStyle = 0L;
  203.     UINT        fuETOOptions = 0;
  204.     if ((int)lpdis->itemID < 0)
  205.         return;
  206.     SendMessage(lpdis->hwndItem, CB_GETLBTEXT, lpdis->itemID, (DWORD_PTR)(LPTSTR)szText);
  207.     GetTextExtentPoint32(hdc, szText, lstrlen(szText), &size);
  208.     dre = (drenum)SendMessage(lpdis->hwndItem, CB_GETITEMDATA, lpdis->itemID, 0);
  209.     if (lpdis->itemAction != ODA_FOCUS)
  210.     {
  211.         int clrBackground = COLOR_WINDOW;
  212.         int clrText = COLOR_WINDOWTEXT;
  213.         if (bHighlightBackground && lpdis->itemState & ODS_SELECTED) {
  214.             clrBackground = COLOR_HIGHLIGHT;
  215.             clrText = COLOR_HIGHLIGHTTEXT;
  216.         }
  217.         //
  218.         //For multiple selection, we don't want to draw items as
  219.         //selected.  Just focus rect below.
  220.         //
  221.         SetBkColor(hdc, GetSysColor(clrBackground));
  222.         SetTextColor(hdc, GetSysColor(clrText));
  223.         //
  224.         //Fill in the background; do this before mini-icon is drawn
  225.         //
  226.         ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &(lpdis->rcItem), NULL, 0, NULL);
  227.         //
  228.         //Draw mini-icon for this item and move string accordingly
  229.         //
  230.         if ((hIcon = GetDriveIcon(dre, TRUE)) != NULL)
  231.         {
  232.             DrawIconEx(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top, hIcon,
  233.                        16, 16, 0, NULL, DI_NORMAL);
  234.             lpdis->rcItem.left += 16;
  235.         }
  236.         lpdis->rcItem.left += INDENT;
  237.         //
  238.         //Draw the cleanup client display name text transparently on top of the background
  239.         //
  240.         SetBkMode(hdc, TRANSPARENT);
  241.         dwExStyle = GetWindowLong(hDlg, GWL_EXSTYLE);
  242.         if(dwExStyle & WS_EX_RTLREADING)
  243.         {
  244.            fuETOOptions |= ETO_RTLREADING; 
  245.         }        
  246.         ExtTextOut(hdc, lpdis->rcItem.left, lpdis->rcItem.top +
  247.                    ((lpdis->rcItem.bottom - lpdis->rcItem.top) - size.cy) / 2,
  248.                    fuETOOptions, NULL, szText, lstrlen(szText), NULL);
  249.     }
  250.     if (lpdis->itemAction == ODA_FOCUS || (lpdis->itemState & ODS_FOCUS))
  251.         DrawFocusRect(hdc, &(lpdis->rcItem));
  252. }
  253. BOOL 
  254. fillSelDriveList(
  255.     HWND hDlg
  256.     )
  257. {
  258.     BOOL      bDoDrive[Drive_Z+1];
  259.     int       dre;
  260.     hardware  hw;
  261.     WPARAM    dw;
  262.     WPARAM    dwSelected = 0;
  263.     USHORT    nFound = 0;
  264.     drenum    dreSelected = Drive_INV;
  265.     TCHAR pszText[cbRESOURCE];
  266.     int cDrv;
  267. #ifdef NEC_98
  268.     drenum    dreBootDrive = Drive_A;
  269. #endif
  270.     for (dre = (int)Drive_A; dre <= (int)Drive_Z; dre++)
  271.     {
  272.         bDoDrive[dre] = FALSE;
  273.     }
  274.     cDrv = 0;
  275. //
  276. //First, figger out what drives to hit.
  277. //
  278.     for (dre = (int)Drive_A; dre <= (int)Drive_Z; dre++)
  279.     {
  280.         switch (dsd.dvl)
  281.         {
  282.             case dvlANYLOCAL:
  283.             {
  284.                 GetHardwareType((drenum)dre, hw);
  285.                 switch (hw)
  286.                 {
  287.                     case hwFixed:
  288.                         cDrv += 1;
  289.                         // Fall through - only count hard disks
  290.                     case hwRemoveable:
  291.                         bDoDrive[dre] = TRUE;
  292.                         break;
  293.                 }
  294.             }
  295.             break;
  296.       
  297.             default:            
  298.                 break;
  299.         }
  300.     }
  301. #ifdef NEC_98
  302. dreBootDrive = GetNECBootDrive();
  303. #endif
  304.     for (dre = (int)Drive_A; dre <= (int)Drive_Z; dre++)
  305.     {
  306.         if (!bDoDrive[dre])
  307.             continue;
  308.         GetDriveDescription((drenum)dre, pszText);
  309.         dw = AddComboString(hDlg, IDC_SELDRIVE_COMBO, pszText, (DWORD)dre);
  310.         nFound++;
  311.         
  312. #ifdef NEC_98
  313. if ( l.dreDef == (drenum)dre ||
  314. dreSelected == Drive_INV ||
  315. (dreSelected < dreBootDrive && l.dreDef == Drive_ALL) ||
  316. (dreSelected < dreBootDrive && l.dreDef == Drive_INV) )
  317. #else
  318.         if (dsd.dreDef == (drenum)dre ||
  319.             dreSelected == Drive_INV ||
  320.             (dreSelected < Drive_C && dsd.dreDef == Drive_ALL) ||
  321.             (dreSelected < Drive_C && dsd.dreDef == Drive_INV) )
  322. #endif
  323.         {
  324.             dwSelected = dw;
  325.             dreSelected = (drenum)dre;
  326.         }
  327.     }
  328.     //
  329.     //Found some drives? Pick one, and leave.
  330.     //
  331.     if (nFound != 0)
  332.     {
  333.         SelectComboItem(hDlg, IDC_SELDRIVE_COMBO, dwSelected);
  334.         dsd.dreDef = dreSelected;
  335.         return TRUE;
  336.     }
  337.     return FALSE;
  338. }
  339. /*
  340.  * SERVICE FUNCTIONS __________________________________________________________
  341.  *
  342.  */
  343. void 
  344. SelectComboItem(
  345.     HWND hDlg, 
  346.     int id, 
  347.     WPARAM w
  348.     )
  349. {
  350.    LPARAM  lParam;
  351.    lParam = MAKELONG((WORD)GetDlgItem(hDlg,id), (WORD)CBN_SELCHANGE );
  352.    SendDlgItemMessage(hDlg, id, CB_SETCURSEL, w, 0L);
  353.    SendMessage(hDlg, WM_COMMAND, id, lParam);
  354. }
  355. WPARAM
  356. AddComboString(
  357.     HWND hDlg, 
  358.     int id, 
  359.     TCHAR *psz, 
  360.     DWORD val
  361.     )
  362. {
  363.    WPARAM dw;
  364.    dw = SendDlgItemMessage(hDlg, id, CB_ADDSTRING, 0, (LPARAM)psz);
  365.    SendDlgItemMessage(hDlg, id, CB_SETITEMDATA, dw, (LPARAM)val);
  366.    return dw;
  367. }
  368. #ifdef NEC_98
  369. drenum
  370. GetNECBootDrive(
  371.     void
  372.     )
  373. {
  374.     drenum dre;
  375.     TCHAR szDrive[4];
  376.     GetBootDrive(szDrive, sizeof(szDrive));
  377.     dre = (drenum)(toupper(szDrive[0] - 'A'));
  378.     return dre;
  379. }
  380. #endif
  381. /*
  382. **------------------------------------------------------------------------------
  383. ** GetBootDrive
  384. **
  385. ** Purpose:    Gets the boot drive from the registry
  386. ** Parameters:
  387. **    pDrive Buffer that the drive string will be returned
  388. ** in, the format will be x:
  389. **   Size Size of the pDrive buffer
  390. ** Notes;
  391. ** Mod Log:    Created by Jason Cobb (7/97)
  392. **------------------------------------------------------------------------------
  393. */
  394. void
  395. GetBootDrive(
  396. PTCHAR pDrive,
  397. DWORD Size
  398. )
  399. {
  400. HKEY hKey;
  401. DWORD cbSize, dwType;
  402. pDrive[0] = '';
  403. if (RegOpenKey(HKEY_LOCAL_MACHINE, REGSTR_PATH_SETUP_SETUP, &hKey) == ERROR_SUCCESS)
  404. {
  405. cbSize = Size;
  406. dwType = REG_SZ;
  407. RegQueryValueEx(hKey, REGSTR_VAL_BOOTDIR, NULL, &dwType, (LPBYTE)pDrive, &cbSize);
  408. RegCloseKey(hKey);
  409. }
  410. if (pDrive[0] == '')
  411. lstrcpy(pDrive, SZ_DEFAULT_DRIVE);
  412. }