home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow_WinXP / VMR / VMRPlayer / persist.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  8.6 KB  |  402 lines

  1. //------------------------------------------------------------------------------
  2. // File: persist.cpp
  3. //
  4. // Desc: DirectShow sample code
  5. //       - State persistence helper functions
  6. //
  7. // Copyright (c) 1994 - 2001, Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10. #include <streams.h>
  11. #include <atlbase.h>
  12. #include <atlconv.cpp>
  13. #include <mmreg.h>
  14.  
  15. #include "project.h"
  16.  
  17. // Constants
  18. const int CX_DEFAULT = 400;     /* Default window width */
  19. const int CY_DEFAULT = 400;     /* Default window height */
  20.  
  21. // Global data
  22. RECENTFILES aRecentFiles[MAX_RECENT_FILES];
  23. int         nRecentFiles;
  24.  
  25. // Global static data
  26. static TCHAR cszWindow[] = TEXT("Window");
  27. static TCHAR cszAppKey[] = TEXT("Software\\Microsoft\\Multimedia Tools\\VMRPlayer");
  28.  
  29.  
  30. /*+ GetAppKey
  31.  *
  32.  *-=================================================================*/
  33.  
  34. HKEY
  35. GetAppKey(
  36.     BOOL fCreate
  37.     )
  38. {
  39.     HKEY hKey = 0;
  40.  
  41.     if(fCreate)
  42.     {
  43.         if(RegCreateKey(HKEY_CURRENT_USER, cszAppKey, &hKey) == ERROR_SUCCESS)
  44.             return hKey;
  45.     }
  46.     else
  47.     {
  48.         if(RegOpenKey(HKEY_CURRENT_USER, cszAppKey, &hKey) == ERROR_SUCCESS)
  49.             return hKey;
  50.     }
  51.  
  52.     return NULL;
  53. }
  54.  
  55.  
  56. /*+ ProfileIntIn
  57.  *
  58.  *-=================================================================*/
  59.  
  60. int
  61. ProfileIntIn(
  62.     const TCHAR *szKey,
  63.     int iDefault
  64.     )
  65. {
  66.     DWORD dwType=0;
  67.     int   iValue=0;
  68.     BYTE  aData[20];
  69.     DWORD cb;
  70.     HKEY  hKey;
  71.  
  72.     if((hKey = GetAppKey(TRUE)) == 0)
  73.     {
  74.         return iDefault;
  75.     }
  76.  
  77.     *(UINT *)&aData = 0;
  78.     cb = sizeof(aData);
  79.  
  80.     if(RegQueryValueEx(hKey, szKey, NULL, &dwType, aData, &cb))
  81.     {
  82.         iValue = iDefault;
  83.     }
  84.     else
  85.     {
  86.         if(dwType == REG_DWORD || dwType == REG_BINARY)
  87.         {
  88.             iValue = *(int *)&aData;
  89.         }
  90.         else if(dwType == REG_SZ)
  91.         {
  92.             iValue = atoi((LPSTR)aData);
  93.         }
  94.     }
  95.  
  96.     RegCloseKey(hKey);
  97.     return iValue;
  98. }
  99.  
  100.  
  101. /*+ ProfileIntOut
  102.  *
  103.  *-=================================================================*/
  104.  
  105. BOOL
  106. ProfileIntOut(
  107.     const TCHAR *szKey,
  108.     int iVal
  109.     )
  110. {
  111.     HKEY  hKey;
  112.     BOOL  bRet = FALSE;
  113.  
  114.     hKey = GetAppKey(TRUE);
  115.     if(hKey)
  116.     {
  117.         RegSetValueEx(hKey, szKey, 0, REG_DWORD, (LPBYTE)&iVal, sizeof(DWORD));
  118.         RegCloseKey(hKey);
  119.         bRet = TRUE;
  120.     }
  121.     return bRet;
  122. }
  123.  
  124.  
  125. /*+ ProfileStringIn
  126.  *
  127.  *-=================================================================*/
  128.  
  129. UINT
  130. ProfileStringIn(
  131.     LPTSTR  szKey,
  132.     LPTSTR  szDef,
  133.     LPTSTR  sz,
  134.     DWORD   cb
  135.     )
  136. {
  137.     HKEY  hKey;
  138.     DWORD dwType;
  139.  
  140.     if((hKey = GetAppKey(FALSE)) == 0)
  141.     {
  142.         lstrcpy(sz, szDef);
  143.         return lstrlen(sz);
  144.     }
  145.  
  146.     if(RegQueryValueEx(hKey, szKey, NULL, &dwType, (LPBYTE)sz, &cb) || dwType != REG_SZ)
  147.     {
  148.         lstrcpy(sz, szDef);
  149.         cb = lstrlen(sz);
  150.     }
  151.  
  152.     RegCloseKey(hKey);
  153.     return cb;
  154. }
  155.  
  156.  
  157. /*+ ProfileStringOut
  158.  *
  159.  *-=================================================================*/
  160.  
  161. void
  162. ProfileStringOut(
  163.     LPTSTR  szKey,
  164.     LPTSTR  sz
  165.     )
  166. {
  167.     HKEY  hKey;
  168.  
  169.     hKey = GetAppKey(TRUE);
  170.     if(hKey)
  171.         RegSetValueEx(hKey, szKey, 0, REG_SZ, (LPBYTE)sz, sizeof(TCHAR) * (lstrlen(sz)+1));
  172.  
  173.     RegCloseKey(hKey);
  174. }
  175.  
  176.  
  177. /*+ LoadWindowPos
  178.  *
  179.  * retrieve the window position information from dragn.ini
  180.  *
  181.  *-=================================================================*/
  182.  
  183. #ifndef SPI_GETWORKAREA
  184.  #define SPI_GETWORKAREA 48  // because NT doesnt have this define yet
  185. #endif
  186.  
  187. BOOL
  188. LoadWindowPos(
  189.     LPRECT lprc
  190.     )
  191. {
  192.     static RECT rcDefault = {0,0,CX_DEFAULT,CY_DEFAULT};
  193.     RECT  rcScreen;
  194.     RECT  rc;
  195.     HKEY  hKey = GetAppKey(FALSE);
  196.  
  197.     // read window placement from the registry.
  198.     //
  199.     *lprc = rcDefault;
  200.  
  201.     if(hKey)
  202.     {
  203.         DWORD cb;
  204.         DWORD dwType;
  205.  
  206.         cb = sizeof(rc);
  207.         if(! RegQueryValueEx(hKey, cszWindow, NULL, &dwType, (LPBYTE)&rc, &cb)
  208.             && dwType == REG_BINARY && cb == sizeof(RECT))
  209.         {
  210.             *lprc = rc;
  211.         }
  212.  
  213.         RegCloseKey(hKey);
  214.     }
  215.  
  216.     // if we fail to get the working area (screen-tray), then assume
  217.     // the screen is 640x480
  218.     //
  219.     if(! SystemParametersInfo(SPI_GETWORKAREA, 0, &rcScreen, FALSE))
  220.     {
  221.         rcScreen.top = rcScreen.left = 0;
  222.         rcScreen.right = 640;
  223.         rcScreen.bottom = 480;
  224.     }
  225.  
  226.     // if the proposed window position is outside the screen,
  227.     // use the default placement
  228.     //
  229.     if(! IntersectRect(&rc, &rcScreen, lprc))
  230.     {
  231.         *lprc = rcDefault;
  232.     }
  233.  
  234.     return ! IsRectEmpty(lprc);
  235. }
  236.  
  237.  
  238. /*+ SaveWindowPos
  239.  *
  240.  * store the window position information in dragn.ini
  241.  *
  242.  *-=================================================================*/
  243.  
  244. BOOL
  245. SaveWindowPos(
  246.     HWND hwnd
  247.     )
  248. {
  249.     WINDOWPLACEMENT wpl;
  250.     HKEY  hKey = GetAppKey(TRUE);
  251.  
  252.     if(!hKey)
  253.     {
  254.         return FALSE;
  255.     }
  256.  
  257.     // save the current size and position of the window to the registry
  258.     //
  259.     ZeroMemory(&wpl, sizeof(wpl));
  260.     wpl.length = sizeof(wpl);
  261.     GetWindowPlacement(hwnd, &wpl);
  262.  
  263.     RegSetValueEx(hKey, cszWindow, 0, REG_BINARY,
  264.         (LPBYTE)&wpl.rcNormalPosition,
  265.         sizeof(wpl.rcNormalPosition));
  266.  
  267.     RegCloseKey(hKey);
  268.     return TRUE;
  269. }
  270.  
  271.  
  272. /*****************************Private*Routine******************************\
  273. * GetRecentFiles
  274. *
  275. * Reads at most MAX_RECENT_FILES from vcdplyer.ini. Returns the number
  276. * of files actually read.  Updates the File menu to show the "recent" files.
  277. *
  278. \**************************************************************************/
  279.  
  280. int
  281. GetRecentFiles(
  282.     int iLastCount
  283.     )
  284. {
  285.     int     i;
  286.     TCHAR   FileName[MAX_PATH];
  287.     TCHAR   szKey[32];
  288.     HMENU   hSubMenu;
  289.  
  290.     //
  291.     // Delete the files from the menu
  292.     //
  293.     hSubMenu = GetSubMenu(GetMenu(hwndApp), 0);
  294.  
  295.     // Delete the separator at slot 3 and all the other recent file entries
  296.     if(iLastCount != 0)
  297.     {
  298.         DeleteMenu(hSubMenu, 3, MF_BYPOSITION);
  299.  
  300.         for(i = 1; i <= iLastCount; i++)
  301.         {
  302.             DeleteMenu(hSubMenu, ID_RECENT_FILE_BASE + i, MF_BYCOMMAND);
  303.         }
  304.     }
  305.  
  306.     for(i = 1; i <= MAX_RECENT_FILES; i++)
  307.     {
  308.         DWORD   len;
  309.         TCHAR   szMenuName[MAX_PATH + 3];
  310.  
  311.         wsprintf(szKey, TEXT("File %d"), i);
  312.         len = ProfileStringIn(szKey, TEXT(""), FileName, MAX_PATH * sizeof(TCHAR));
  313.         if(len == 0)
  314.         {
  315.             i = i - 1;
  316.             break;
  317.         }
  318.  
  319.         lstrcpy(aRecentFiles[i - 1], FileName);
  320.         wsprintf(szMenuName, TEXT("&%d %s"), i, FileName);
  321.  
  322.         if(i == 1)
  323.         {
  324.             InsertMenu(hSubMenu, 3, MF_SEPARATOR | MF_BYPOSITION, (UINT)-1, NULL);
  325.         }
  326.  
  327.         InsertMenu(hSubMenu, 3 + i, MF_STRING | MF_BYPOSITION,
  328.             ID_RECENT_FILE_BASE + i, szMenuName);
  329.     }
  330.  
  331.     //
  332.     // i is the number of recent files in the array.
  333.     //
  334.     return i;
  335. }
  336.  
  337.  
  338. /*****************************Private*Routine******************************\
  339. * SetRecentFiles
  340. *
  341. * Writes the most recent files to the vcdplyer.ini file.  Purges the oldest
  342. * file if necessary.
  343. *
  344. \**************************************************************************/
  345. int
  346. SetRecentFiles(
  347.     TCHAR *FileName,    // File name to add
  348.     int iCount          // Current count of files
  349.     )
  350. {
  351.     TCHAR   FullPathFileName[MAX_PATH];
  352.     TCHAR   *lpFile;
  353.     TCHAR   szKey[32];
  354.     int     iCountNew;
  355.     int     i;
  356.  
  357.     //
  358.     // Check for dupes - we don't allow them !
  359.     //
  360.     for(i = 0; i < iCount; i++)
  361.     {
  362.         if(0 == lstrcmpi(FileName, aRecentFiles[i]))
  363.         {
  364.             return iCount;
  365.         }
  366.     }
  367.  
  368.     //
  369.     // Throw away the oldest entry
  370.     //
  371.     MoveMemory(&aRecentFiles[1], &aRecentFiles[0],
  372.         sizeof(aRecentFiles) - sizeof(aRecentFiles[1]));
  373.  
  374.     //
  375.     // Copy in the full path of the new file.
  376.     //
  377.     GetFullPathName(FileName, MAX_PATH, FullPathFileName, &lpFile);
  378.     lstrcpy(aRecentFiles[0], FullPathFileName);
  379.  
  380.     //
  381.     // Update the count of files, saturate to MAX_RECENT_FILES.
  382.     //
  383.     iCountNew = min(iCount + 1, MAX_RECENT_FILES);
  384.  
  385.     //
  386.     // Clear the old stuff and the write out the recent files to disk
  387.     //
  388.     for(i = 1; i <= iCountNew; i++)
  389.     {
  390.         wsprintf(szKey, TEXT("File %d"), i);
  391.         ProfileStringOut(szKey, aRecentFiles[i - 1]);
  392.     }
  393.  
  394.     //
  395.     // Update the file menu
  396.     //
  397.     GetRecentFiles(iCount);
  398.  
  399.     return iCountNew;  // the updated count of files.
  400. }
  401.  
  402.