Team Fortress 2 Source Code as on 22/4/2020
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.

251 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Standard file menu
  4. //
  5. //=============================================================================
  6. #include "toolutils/toolfilemenubutton.h"
  7. #include "toolutils/toolmenubutton.h"
  8. #include "tier1/KeyValues.h"
  9. #include "tier1/utlstring.h"
  10. #include "vgui_controls/menu.h"
  11. #include "vgui_controls/frame.h"
  12. #include "vgui_controls/button.h"
  13. #include "vgui_controls/listpanel.h"
  14. #include "toolutils/enginetools_int.h"
  15. #include "p4lib/ip4.h"
  16. #include "vgui_controls/perforcefilelistframe.h"
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. //-----------------------------------------------------------------------------
  20. // Global function to create the file menu
  21. //-----------------------------------------------------------------------------
  22. CToolMenuButton* CreateToolFileMenuButton( vgui::Panel *parent, const char *panelName,
  23. const char *text, vgui::Panel *pActionTarget, IFileMenuCallbacks *pCallbacks )
  24. {
  25. return new CToolFileMenuButton( parent, panelName, text, pActionTarget, pCallbacks );
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Constructor
  29. //-----------------------------------------------------------------------------
  30. CToolFileMenuButton::CToolFileMenuButton( vgui::Panel *pParent, const char *panelName, const char *text, vgui::Panel *pActionSignalTarget, IFileMenuCallbacks *pFileMenuCallback ) :
  31. BaseClass( pParent, panelName, text, pActionSignalTarget ), m_pFileMenuCallback( pFileMenuCallback )
  32. {
  33. Assert( pFileMenuCallback );
  34. AddMenuItem( "new", "#ToolFileNew", new KeyValues( "OnNew" ), pActionSignalTarget, NULL, "file_new" );
  35. AddMenuItem( "open", "#ToolFileOpen", new KeyValues( "OnOpen" ), pActionSignalTarget, NULL, "file_open" );
  36. AddMenuItem( "save", "#ToolFileSave", new KeyValues( "OnSave" ), pActionSignalTarget, NULL, "file_save" );
  37. AddMenuItem( "saveas", "#ToolFileSaveAs", new KeyValues( "OnSaveAs" ), pActionSignalTarget );
  38. AddMenuItem( "close", "#ToolFileClose", new KeyValues( "OnClose" ), pActionSignalTarget );
  39. AddSeparator();
  40. // Add the Perforce menu options only if there is a valid P4 interface (SDK users won't have this)
  41. if ( p4 )
  42. {
  43. m_pPerforce = new vgui::Menu( this, "Perforce" );
  44. m_pMenu->AddCascadingMenuItem( "#ToolPerforce", this, m_pPerforce );
  45. m_nPerforceAdd = m_pPerforce->AddMenuItem( "perforce_add", "#ToolPerforceAdd", new KeyValues( "OnPerforceAdd" ), this );
  46. m_nPerforceOpen = m_pPerforce->AddMenuItem( "perforce_open", "#ToolPerforceOpen", new KeyValues( "OnPerforceOpen" ), this );
  47. m_nPerforceRevert = m_pPerforce->AddMenuItem( "perforce_revert", "#ToolPerforceRevert", new KeyValues( "OnPerforceRevert" ), this );
  48. m_nPerforceSubmit = m_pPerforce->AddMenuItem( "perforce_submit", "#ToolPerforceSubmit", new KeyValues( "OnPerforceSubmit" ), this );
  49. m_pPerforce->AddSeparator();
  50. m_nPerforceP4Win = m_pPerforce->AddMenuItem( "perforce_p4win", "#ToolPerforceP4Win", new KeyValues( "OnPerforceP4Win" ), this );
  51. m_nPerforceListOpenFiles = m_pPerforce->AddMenuItem( "perforce_listopenfiles", "#ToolPerforceListOpenFiles", new KeyValues( "OnPerforceListOpenFiles" ), this );
  52. }
  53. m_pRecentFiles = new vgui::Menu( this, "RecentFiles" );
  54. m_nRecentFiles = m_pMenu->AddCascadingMenuItem( "#ToolFileRecent", pActionSignalTarget, m_pRecentFiles );
  55. AddMenuItem( "clearrecent", "#ToolFileClearRecent", new KeyValues ( "OnClearRecent" ), pActionSignalTarget );
  56. AddSeparator();
  57. AddMenuItem( "exit", "#ToolFileExit", new KeyValues ( "OnExit" ), pActionSignalTarget );
  58. SetMenu( m_pMenu );
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Gets called when the menu is shown
  62. //-----------------------------------------------------------------------------
  63. void CToolFileMenuButton::OnShowMenu( vgui::Menu *menu )
  64. {
  65. BaseClass::OnShowMenu( menu );
  66. // Update the menu
  67. int nEnableMask = m_pFileMenuCallback->GetFileMenuItemsEnabled();
  68. int id = m_Items.Find( "new" );
  69. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_NEW) != 0 );
  70. id = m_Items.Find( "open" );
  71. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_OPEN) != 0 );
  72. id = m_Items.Find( "save" );
  73. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_SAVE) != 0 );
  74. id = m_Items.Find( "saveas" );
  75. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_SAVEAS) != 0 );
  76. id = m_Items.Find( "close" );
  77. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_CLOSE) != 0 );
  78. id = m_Items.Find( "clearrecent" );
  79. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_CLEAR_RECENT) != 0 );
  80. m_pRecentFiles->DeleteAllItems();
  81. if ( (nEnableMask & IFileMenuCallbacks::FILE_RECENT) == 0 )
  82. {
  83. m_pMenu->SetItemEnabled( m_nRecentFiles, false );
  84. }
  85. else
  86. {
  87. m_pMenu->SetItemEnabled( m_nRecentFiles, true );
  88. m_pFileMenuCallback->AddRecentFilesToMenu( m_pRecentFiles );
  89. }
  90. // We only have the Perforce menu items if we have valid p4 interface
  91. if ( p4 )
  92. {
  93. bool bP4Connected = p4->IsConnectedToServer();
  94. char pPerforceFile[MAX_PATH];
  95. if ( bP4Connected && m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  96. {
  97. bool bIsUnnamed = !Q_IsAbsolutePath( pPerforceFile );
  98. bool bOpenedForEdit = p4->GetFileState( pPerforceFile ) != P4FILE_UNOPENED;
  99. bool bFileInPerforce = p4->IsFileInPerforce( pPerforceFile );
  100. m_pPerforce->SetItemEnabled( m_nPerforceAdd, !bIsUnnamed && !bFileInPerforce && !bOpenedForEdit );
  101. m_pPerforce->SetItemEnabled( m_nPerforceOpen, !bIsUnnamed && bFileInPerforce && !bOpenedForEdit );
  102. m_pPerforce->SetItemEnabled( m_nPerforceRevert, !bIsUnnamed && bOpenedForEdit );
  103. m_pPerforce->SetItemEnabled( m_nPerforceSubmit, !bIsUnnamed && bOpenedForEdit );
  104. m_pPerforce->SetItemEnabled( m_nPerforceP4Win, !bIsUnnamed && bFileInPerforce || bOpenedForEdit );
  105. m_pPerforce->SetItemEnabled( m_nPerforceListOpenFiles, true );
  106. }
  107. else
  108. {
  109. m_pPerforce->SetItemEnabled( m_nPerforceAdd, false );
  110. m_pPerforce->SetItemEnabled( m_nPerforceOpen, false );
  111. m_pPerforce->SetItemEnabled( m_nPerforceRevert, false );
  112. m_pPerforce->SetItemEnabled( m_nPerforceSubmit, false );
  113. m_pPerforce->SetItemEnabled( m_nPerforceP4Win, false );
  114. m_pPerforce->SetItemEnabled( m_nPerforceListOpenFiles, bP4Connected );
  115. }
  116. }
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Perforce functions
  120. //-----------------------------------------------------------------------------
  121. void CToolFileMenuButton::OnPerforceAdd( )
  122. {
  123. char pPerforceFile[MAX_PATH];
  124. if ( m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  125. {
  126. CPerforceFileListFrame *pPerforceFrame = new CPerforceFileListFrame( m_pFileMenuCallback->GetRootPanel(), "Add Movie File to Perforce?", "Movie File", PERFORCE_ACTION_FILE_ADD );
  127. pPerforceFrame->AddFile( pPerforceFile );
  128. pPerforceFrame->DoModal( );
  129. }
  130. }
  131. //-----------------------------------------------------------------------------
  132. // Check out a file
  133. //-----------------------------------------------------------------------------
  134. void CToolFileMenuButton::OnPerforceOpen( )
  135. {
  136. char pPerforceFile[MAX_PATH];
  137. if ( m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  138. {
  139. CPerforceFileListFrame *pPerforceFrame = new CPerforceFileListFrame( m_pFileMenuCallback->GetRootPanel(), "Check Out Movie File from Perforce?", "Movie File", PERFORCE_ACTION_FILE_EDIT );
  140. pPerforceFrame->AddFile( pPerforceFile );
  141. pPerforceFrame->DoModal( );
  142. }
  143. }
  144. //-----------------------------------------------------------------------------
  145. // Revert a file
  146. //-----------------------------------------------------------------------------
  147. void CToolFileMenuButton::OnPerforceRevert( )
  148. {
  149. char pPerforceFile[MAX_PATH];
  150. if ( m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  151. {
  152. CPerforceFileListFrame *pPerforceFrame = new CPerforceFileListFrame( m_pFileMenuCallback->GetRootPanel(), "Revert Movie File Changes from Perforce?", "Movie File", PERFORCE_ACTION_FILE_REVERT );
  153. pPerforceFrame->AddFile( pPerforceFile );
  154. pPerforceFrame->DoModal( );
  155. }
  156. }
  157. //-----------------------------------------------------------------------------
  158. // Submit a file
  159. //-----------------------------------------------------------------------------
  160. void CToolFileMenuButton::OnPerforceSubmit( )
  161. {
  162. char pPerforceFile[MAX_PATH];
  163. if ( m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  164. {
  165. CPerforceFileListFrame *pPerforceFrame = new CPerforceFileListFrame( m_pFileMenuCallback->GetRootPanel(), "Submit Movie File Changes to Perforce?", "Movie File", PERFORCE_ACTION_FILE_SUBMIT );
  166. pPerforceFrame->AddFile( pPerforceFile );
  167. pPerforceFrame->DoModal( );
  168. }
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Open a file in p4win
  172. //-----------------------------------------------------------------------------
  173. void CToolFileMenuButton::OnPerforceP4Win( )
  174. {
  175. char pPerforceFile[MAX_PATH];
  176. if ( m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  177. {
  178. if ( p4->IsFileInPerforce( pPerforceFile ) )
  179. {
  180. p4->OpenFileInP4Win( pPerforceFile );
  181. }
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Show a file in p4win
  186. //-----------------------------------------------------------------------------
  187. void CToolFileMenuButton::OnPerforceListOpenFiles( )
  188. {
  189. CUtlVector<P4File_t> openedFiles;
  190. p4->GetOpenedFileListInPath( "GAME", openedFiles );
  191. COperationFileListFrame *pOpenedFiles = new COperationFileListFrame( m_pFileMenuCallback->GetRootPanel(), "Opened Files In Perforce", "File Name", false, true );
  192. int nCount = openedFiles.Count();
  193. for ( int i = 0; i < nCount; ++i )
  194. {
  195. const char *pOpenType = NULL;
  196. switch( openedFiles[i].m_eOpenState )
  197. {
  198. case P4FILE_OPENED_FOR_ADD:
  199. pOpenType = "Add";
  200. break;
  201. case P4FILE_OPENED_FOR_EDIT:
  202. pOpenType = "Edit";
  203. break;
  204. case P4FILE_OPENED_FOR_DELETE:
  205. pOpenType = "Delete";
  206. break;
  207. case P4FILE_OPENED_FOR_INTEGRATE:
  208. pOpenType = "Integrate";
  209. break;
  210. }
  211. if ( pOpenType )
  212. {
  213. pOpenedFiles->AddOperation( pOpenType, p4->String( openedFiles[i].m_sLocalFile ) );
  214. }
  215. }
  216. pOpenedFiles->DoModal( );
  217. }