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