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.

195 lines
5.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Core Movie Maker UI API
  4. //
  5. //=============================================================================
  6. #include "vgui_controls/savedocumentquery.h"
  7. #include "vgui_controls/Button.h"
  8. #include "vgui_controls/Label.h"
  9. #include "vgui_controls/Frame.h"
  10. #include "vgui/ISurface.h"
  11. #include "vgui/IVGui.h"
  12. #include "tier1/KeyValues.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. using namespace vgui;
  16. //-----------------------------------------------------------------------------
  17. // This dialog asks if you want to save your work
  18. //-----------------------------------------------------------------------------
  19. class CSaveDocumentQuery : public vgui::Frame
  20. {
  21. DECLARE_CLASS_SIMPLE( CSaveDocumentQuery, vgui::Frame );
  22. public:
  23. CSaveDocumentQuery( vgui::Panel *pParent, const char *filename, const char *pFileType, int nContext,
  24. vgui::Panel *pActionSignalTarget = 0, KeyValues *pKeyValues = 0 );
  25. ~CSaveDocumentQuery();
  26. // Inherited from vgui::Frame
  27. virtual void OnCommand( char const *cmd );
  28. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  29. // Put the message box into a modal state
  30. void DoModal();
  31. private:
  32. // Posts commands to the action signal target
  33. void PostCommand( const char *pCommand );
  34. vgui::Label *m_pMessageLabel;
  35. vgui::Button *m_pYesButton;
  36. vgui::Button *m_pNoButton;
  37. vgui::Button *m_pCancelButton;
  38. vgui::Panel *m_pActionSignalTarget;
  39. char m_szFileName[ 256 ];
  40. char m_szFileType[ 256 ];
  41. int m_nContext;
  42. KeyValues* m_pPostSaveKeyValues;
  43. };
  44. //-----------------------------------------------------------------------------
  45. // Show the save document query dialog
  46. //-----------------------------------------------------------------------------
  47. void ShowSaveDocumentQuery( vgui::Panel *pParent, const char *pFileName, const char *pFileType, int nContext, vgui::Panel *pActionSignalTarget, KeyValues *pPostSaveCommand )
  48. {
  49. CSaveDocumentQuery *query = new CSaveDocumentQuery( pParent, pFileName, pFileType, nContext, pActionSignalTarget, pPostSaveCommand );
  50. query->SetSmallCaption( true );
  51. query->DoModal();
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Constructor
  55. //-----------------------------------------------------------------------------
  56. CSaveDocumentQuery::CSaveDocumentQuery( vgui::Panel *pParent, char const *pFileName, const char *pFileType, int nContext, vgui::Panel *pActionSignalTarget, KeyValues *pPostSaveCommand ) :
  57. BaseClass( pParent, "SaveDocumentQuery" ),
  58. m_nContext( nContext ),
  59. m_pActionSignalTarget( pActionSignalTarget )
  60. {
  61. if ( !pFileName || !pFileName[0] )
  62. {
  63. pFileName = "<untitled>";
  64. }
  65. Q_strncpy( m_szFileName, pFileName, sizeof( m_szFileName ) );
  66. Q_strncpy( m_szFileType, pFileType, sizeof( m_szFileType ) );
  67. m_pPostSaveKeyValues = pPostSaveCommand;
  68. SetDeleteSelfOnClose(true);
  69. SetMenuButtonResponsive(false);
  70. SetMinimizeButtonVisible(false);
  71. SetCloseButtonVisible(false);
  72. SetSizeable(false);
  73. SetTitle( "Save Changes", true );
  74. m_pMessageLabel = new Label( this, "FileNameLabel", "" );
  75. m_pYesButton = new Button( this, "Yes", "Yes", this, "yes" );
  76. m_pNoButton = new Button( this, "No", "No", this, "no" );
  77. m_pCancelButton = new Button( this, "Cancel", "Cancel", this, "cancel" );
  78. LoadControlSettings( "resource/ToolSaveDocumentQuery.res" );
  79. m_pMessageLabel->SetText( m_szFileName );
  80. }
  81. CSaveDocumentQuery::~CSaveDocumentQuery()
  82. {
  83. if ( m_pPostSaveKeyValues )
  84. {
  85. m_pPostSaveKeyValues->deleteThis();
  86. m_pPostSaveKeyValues = NULL;
  87. }
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Posts commands to the action signal target
  91. //-----------------------------------------------------------------------------
  92. void CSaveDocumentQuery::PostCommand( const char *pCommand )
  93. {
  94. KeyValues *kv = new KeyValues( pCommand );
  95. vgui::ivgui()->PostMessage( m_pActionSignalTarget->GetVPanel(), kv, 0 );
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Process commands
  99. //-----------------------------------------------------------------------------
  100. void CSaveDocumentQuery::OnCommand( char const *cmd )
  101. {
  102. if ( !Q_stricmp( cmd, "yes" ) )
  103. {
  104. KeyValues *kv = new KeyValues( "OnSaveFile" );
  105. kv->SetString( "filename", m_szFileName );
  106. kv->SetString( "filetype", m_szFileType );
  107. kv->SetInt( "context", m_nContext );
  108. kv->SetPtr( "actionTarget", m_pActionSignalTarget );
  109. if ( m_pPostSaveKeyValues )
  110. {
  111. kv->AddSubKey( m_pPostSaveKeyValues->MakeCopy() );
  112. }
  113. vgui::ivgui()->PostMessage( m_pActionSignalTarget->GetVPanel(), kv, 0 );
  114. MarkForDeletion();
  115. }
  116. else if ( !Q_stricmp( cmd, "no" ) )
  117. {
  118. PostCommand( "OnMarkNotDirty" );
  119. if ( m_pPostSaveKeyValues )
  120. {
  121. vgui::ivgui()->PostMessage( m_pActionSignalTarget->GetVPanel(), m_pPostSaveKeyValues->MakeCopy(), 0 );
  122. }
  123. MarkForDeletion();
  124. }
  125. else if ( !Q_stricmp( cmd, "cancel" ) )
  126. {
  127. PostCommand( "OnCancelSaveDocument" );
  128. MarkForDeletion();
  129. }
  130. else
  131. {
  132. BaseClass::OnCommand( cmd );
  133. }
  134. }
  135. //-----------------------------------------------------------------------------
  136. // Deal with scheme
  137. //-----------------------------------------------------------------------------
  138. void CSaveDocumentQuery::ApplySchemeSettings(IScheme *pScheme)
  139. {
  140. BaseClass::ApplySchemeSettings(pScheme);
  141. int wide, tall;
  142. GetSize( wide, tall );
  143. int swide, stall;
  144. surface()->GetScreenSize(swide, stall);
  145. // put the dialog in the middle of the screen
  146. SetPos((swide - wide) / 2, (stall - tall) / 2);
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Put the message box into a modal state
  150. //-----------------------------------------------------------------------------
  151. void CSaveDocumentQuery::DoModal()
  152. {
  153. SetVisible( true );
  154. SetEnabled( true );
  155. MoveToFront();
  156. RequestFocus();
  157. InvalidateLayout();
  158. }