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.

248 lines
9.9 KiB

  1. //====== Copyright � 1996-2005, 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. AddSeparator();
  56. AddMenuItem( "exit", "#ToolFileExit", new KeyValues ( "OnExit" ), pActionSignalTarget );
  57. SetMenu( m_pMenu );
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Gets called when the menu is shown
  61. //-----------------------------------------------------------------------------
  62. void CToolFileMenuButton::OnShowMenu( vgui::Menu *menu )
  63. {
  64. BaseClass::OnShowMenu( menu );
  65. // Update the menu
  66. int nEnableMask = m_pFileMenuCallback->GetFileMenuItemsEnabled();
  67. int id = m_Items.Find( "new" );
  68. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_NEW) != 0 );
  69. id = m_Items.Find( "open" );
  70. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_OPEN) != 0 );
  71. id = m_Items.Find( "save" );
  72. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_SAVE) != 0 );
  73. id = m_Items.Find( "saveas" );
  74. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_SAVEAS) != 0 );
  75. id = m_Items.Find( "close" );
  76. SetItemEnabled( id, (nEnableMask & IFileMenuCallbacks::FILE_CLOSE) != 0 );
  77. m_pRecentFiles->DeleteAllItems();
  78. if ( (nEnableMask & IFileMenuCallbacks::FILE_RECENT) == 0 )
  79. {
  80. m_pMenu->SetItemEnabled( m_nRecentFiles, false );
  81. }
  82. else
  83. {
  84. m_pMenu->SetItemEnabled( m_nRecentFiles, true );
  85. m_pFileMenuCallback->AddRecentFilesToMenu( m_pRecentFiles );
  86. }
  87. // We only have the Perforce menu items if we have valid p4 interface
  88. if ( p4 )
  89. {
  90. bool bP4Connected = p4->IsConnectedToServer( false );
  91. char pPerforceFile[MAX_PATH];
  92. if ( bP4Connected && m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  93. {
  94. bool bIsUnnamed = !Q_IsAbsolutePath( pPerforceFile );
  95. bool bOpenedForEdit = p4->GetFileState( pPerforceFile ) != P4FILE_UNOPENED;
  96. bool bFileInPerforce = p4->IsFileInPerforce( pPerforceFile );
  97. m_pPerforce->SetItemEnabled( m_nPerforceAdd, !bIsUnnamed && !bFileInPerforce && !bOpenedForEdit );
  98. m_pPerforce->SetItemEnabled( m_nPerforceOpen, !bIsUnnamed && bFileInPerforce && !bOpenedForEdit );
  99. m_pPerforce->SetItemEnabled( m_nPerforceRevert, !bIsUnnamed && bOpenedForEdit );
  100. m_pPerforce->SetItemEnabled( m_nPerforceSubmit, !bIsUnnamed && bOpenedForEdit );
  101. m_pPerforce->SetItemEnabled( m_nPerforceP4Win, !bIsUnnamed && bFileInPerforce || bOpenedForEdit );
  102. m_pPerforce->SetItemEnabled( m_nPerforceListOpenFiles, true );
  103. }
  104. else
  105. {
  106. m_pPerforce->SetItemEnabled( m_nPerforceAdd, false );
  107. m_pPerforce->SetItemEnabled( m_nPerforceOpen, false );
  108. m_pPerforce->SetItemEnabled( m_nPerforceRevert, false );
  109. m_pPerforce->SetItemEnabled( m_nPerforceSubmit, false );
  110. m_pPerforce->SetItemEnabled( m_nPerforceP4Win, false );
  111. m_pPerforce->SetItemEnabled( m_nPerforceListOpenFiles, bP4Connected );
  112. }
  113. }
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Perforce functions
  117. //-----------------------------------------------------------------------------
  118. void CToolFileMenuButton::OnPerforceAdd( )
  119. {
  120. char pPerforceFile[MAX_PATH];
  121. if ( m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  122. {
  123. CPerforceFileListFrame *pPerforceFrame = new CPerforceFileListFrame( m_pFileMenuCallback->GetRootPanel(), "Add Movie File to Perforce?", "Movie File", PERFORCE_ACTION_FILE_ADD );
  124. pPerforceFrame->AddFile( pPerforceFile );
  125. pPerforceFrame->DoModal( );
  126. }
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Check out a file
  130. //-----------------------------------------------------------------------------
  131. void CToolFileMenuButton::OnPerforceOpen( )
  132. {
  133. char pPerforceFile[MAX_PATH];
  134. if ( m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  135. {
  136. CPerforceFileListFrame *pPerforceFrame = new CPerforceFileListFrame( m_pFileMenuCallback->GetRootPanel(), "Check Out Movie File from Perforce?", "Movie File", PERFORCE_ACTION_FILE_EDIT );
  137. pPerforceFrame->AddFile( pPerforceFile );
  138. pPerforceFrame->DoModal( );
  139. }
  140. }
  141. //-----------------------------------------------------------------------------
  142. // Revert a file
  143. //-----------------------------------------------------------------------------
  144. void CToolFileMenuButton::OnPerforceRevert( )
  145. {
  146. char pPerforceFile[MAX_PATH];
  147. if ( m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  148. {
  149. CPerforceFileListFrame *pPerforceFrame = new CPerforceFileListFrame( m_pFileMenuCallback->GetRootPanel(), "Revert Movie File Changes from Perforce?", "Movie File", PERFORCE_ACTION_FILE_REVERT );
  150. pPerforceFrame->AddFile( pPerforceFile );
  151. pPerforceFrame->DoModal( );
  152. }
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Submit a file
  156. //-----------------------------------------------------------------------------
  157. void CToolFileMenuButton::OnPerforceSubmit( )
  158. {
  159. char pPerforceFile[MAX_PATH];
  160. if ( m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  161. {
  162. CPerforceFileListFrame *pPerforceFrame = new CPerforceFileListFrame( m_pFileMenuCallback->GetRootPanel(), "Submit Movie File Changes to Perforce?", "Movie File", PERFORCE_ACTION_FILE_SUBMIT );
  163. pPerforceFrame->AddFile( pPerforceFile );
  164. pPerforceFrame->DoModal( );
  165. }
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Open a file in p4win
  169. //-----------------------------------------------------------------------------
  170. void CToolFileMenuButton::OnPerforceP4Win( )
  171. {
  172. char pPerforceFile[MAX_PATH];
  173. if ( m_pFileMenuCallback->GetPerforceFileName( pPerforceFile, sizeof(pPerforceFile) ) )
  174. {
  175. if ( p4->IsFileInPerforce( pPerforceFile ) )
  176. {
  177. p4->OpenFileInP4Win( pPerforceFile );
  178. }
  179. }
  180. }
  181. //-----------------------------------------------------------------------------
  182. // Show a file in p4win
  183. //-----------------------------------------------------------------------------
  184. void CToolFileMenuButton::OnPerforceListOpenFiles( )
  185. {
  186. CUtlVector<P4File_t> openedFiles;
  187. p4->GetOpenedFileListInPath( "GAME", openedFiles );
  188. COperationFileListFrame *pOpenedFiles = new COperationFileListFrame( m_pFileMenuCallback->GetRootPanel(), "Opened Files In Perforce", "File Name", false, true );
  189. int nCount = openedFiles.Count();
  190. for ( int i = 0; i < nCount; ++i )
  191. {
  192. const char *pOpenType = NULL;
  193. switch( openedFiles[i].m_eOpenState )
  194. {
  195. case P4FILE_OPENED_FOR_ADD:
  196. pOpenType = "Add";
  197. break;
  198. case P4FILE_OPENED_FOR_EDIT:
  199. pOpenType = "Edit";
  200. break;
  201. case P4FILE_OPENED_FOR_DELETE:
  202. pOpenType = "Delete";
  203. break;
  204. case P4FILE_OPENED_FOR_INTEGRATE:
  205. pOpenType = "Integrate";
  206. break;
  207. }
  208. if ( pOpenType )
  209. {
  210. pOpenedFiles->AddOperation( pOpenType, p4->String( openedFiles[i].m_sLocalFile ) );
  211. }
  212. }
  213. pOpenedFiles->DoModal( );
  214. }