Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.1 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Core Movie Maker UI API
  4. //
  5. //=============================================================================
  6. #include "toolutils/recentfilelist.h"
  7. #include "vgui_controls/menu.h"
  8. #include "iregistry.h"
  9. #include "tier1/keyvalues.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. //-----------------------------------------------------------------------------
  13. // Adds a file to the list of recent files
  14. //-----------------------------------------------------------------------------
  15. void CToolsRecentFileList::Add( const char *pFileName, const char *pFileFormat )
  16. {
  17. RecentFileInfo_t info;
  18. info.m_pFileName = pFileName;
  19. int idx = m_RecentFiles.Find( info );
  20. if ( idx != m_RecentFiles.InvalidIndex() )
  21. {
  22. // Remove from current slot so it gets added to head (most recent) below...
  23. m_RecentFiles.Remove( idx );
  24. }
  25. while ( m_RecentFiles.Count() >= MAX_RECENT_FILES )
  26. {
  27. // Oldest is at last slot
  28. m_RecentFiles.Remove( m_RecentFiles.Count() - 1 );
  29. }
  30. int i = m_RecentFiles.AddToHead( );
  31. m_RecentFiles[i].m_pFileName = pFileName;
  32. m_RecentFiles[i].m_pFileFormat = pFileFormat;
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Removes all files from the list
  36. //-----------------------------------------------------------------------------
  37. void CToolsRecentFileList::Clear()
  38. {
  39. m_RecentFiles.RemoveAll();
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Returns true if there's no files in the file list
  43. //-----------------------------------------------------------------------------
  44. bool CToolsRecentFileList::IsEmpty() const
  45. {
  46. return m_RecentFiles.Count() == 0;
  47. }
  48. //-----------------------------------------------------------------------------
  49. // Gets the file in a particular slot
  50. //-----------------------------------------------------------------------------
  51. const char *CToolsRecentFileList::GetFile( int slot ) const
  52. {
  53. if ( slot < 0 || slot >= m_RecentFiles.Count() )
  54. return NULL;
  55. return m_RecentFiles[slot].m_pFileName;
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Gets the file in a particular slot
  59. //-----------------------------------------------------------------------------
  60. const char *CToolsRecentFileList::GetFileFormat( int slot ) const
  61. {
  62. if ( slot < 0 || slot >= m_RecentFiles.Count() )
  63. return NULL;
  64. return m_RecentFiles[slot].m_pFileFormat;
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Loads the file list from the registry
  68. //-----------------------------------------------------------------------------
  69. void CToolsRecentFileList::LoadFromRegistry( const char *pToolKeyName )
  70. {
  71. Clear();
  72. // Iterate in reverse order so most recent files goes to top
  73. for ( int i = MAX_RECENT_FILES; i >= 0; --i )
  74. {
  75. char sz[ 128 ];
  76. char szType[ 128 ];
  77. Q_snprintf( sz, sizeof( sz ), "%s\\history%02i", pToolKeyName, i );
  78. Q_snprintf( szType, sizeof( szType ), "%s\\history_fileformat%02i", pToolKeyName, i );
  79. // NOTE: Can't call registry->ReadString twice in a row!
  80. char pFileName[MAX_PATH];
  81. Q_strncpy( pFileName, registry->ReadString( sz, "" ), sizeof(pFileName) );
  82. if ( pFileName && pFileName[ 0 ] )
  83. {
  84. const char *valType = registry->ReadString( szType, "" );
  85. const char *pFormat = (valType && valType[0]) ? valType : "dmx";
  86. Add( pFileName, pFormat );
  87. }
  88. }
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Saves file list into the registry
  92. //-----------------------------------------------------------------------------
  93. void CToolsRecentFileList::SaveToRegistry( const char *pToolKeyName ) const
  94. {
  95. char sz[ 128 ];
  96. int i, c;
  97. c = m_RecentFiles.Count();
  98. for ( i = 0 ; i < c; ++i )
  99. {
  100. Q_snprintf( sz, sizeof( sz ), "%s\\history%02i", pToolKeyName, i );
  101. registry->WriteString( sz, m_RecentFiles[i].m_pFileName );
  102. Q_snprintf( sz, sizeof( sz ), "%s\\history_fileformat%02i", pToolKeyName, i );
  103. registry->WriteString( sz, m_RecentFiles[i].m_pFileFormat );
  104. }
  105. // Clear out all other registry settings
  106. for ( ; i < MAX_RECENT_FILES; ++i )
  107. {
  108. Q_snprintf( sz, sizeof( sz ), "%s\\history%02i", pToolKeyName, i );
  109. registry->WriteString( sz, "" );
  110. Q_snprintf( sz, sizeof( sz ), "%s\\history_fileformat%02i", pToolKeyName, i );
  111. registry->WriteString( sz, "" );
  112. }
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Adds the list of files to a particular menu
  116. //-----------------------------------------------------------------------------
  117. void CToolsRecentFileList::AddToMenu( vgui::Menu *menu, vgui::Panel *pActionTarget, const char *pCommandName ) const
  118. {
  119. int i, c;
  120. c = m_RecentFiles.Count();
  121. for ( i = 0 ; i < c; ++i )
  122. {
  123. char sz[ 32 ];
  124. Q_snprintf( sz, sizeof( sz ), "%s%02i", pCommandName, i );
  125. char const *fn = m_RecentFiles[i].m_pFileName;
  126. menu->AddMenuItem( fn, new KeyValues( "Command", "command", sz ), pActionTarget );
  127. }
  128. menu->AddSeparator();
  129. menu->AddMenuItem( "clearrecent", "#ToolFileClearRecent", new KeyValues ( "OnClearRecent" ), pActionTarget );
  130. }