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.

172 lines
6.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // This is a helper class designed to help with the chains of modal dialogs
  4. // encountered when trying to open or save a particular file
  5. //
  6. //=============================================================================
  7. #ifndef FILEOPENSTATEMACHINE_H
  8. #define FILEOPENSTATEMACHINE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "vgui_controls/Panel.h"
  13. #include "tier1/utlstring.h"
  14. //-----------------------------------------------------------------------------
  15. // Forward declarations
  16. //-----------------------------------------------------------------------------
  17. namespace vgui
  18. {
  19. //-----------------------------------------------------------------------------
  20. // Interface for things using the file open state machine
  21. //-----------------------------------------------------------------------------
  22. abstract_class IFileOpenStateMachineClient
  23. {
  24. public:
  25. // Called by to allow clients to set up the save dialog
  26. virtual void SetupFileOpenDialog( vgui::FileOpenDialog *pDialog, bool bOpenFile, const char *pFileFormat, KeyValues *pContextKeyValues ) = 0;
  27. // Called by to allow clients to actually read the file in
  28. virtual bool OnReadFileFromDisk( const char *pFileName, const char *pFileFormat, KeyValues *pContextKeyValues ) = 0;
  29. // Called by to allow clients to actually write the file out
  30. virtual bool OnWriteFileToDisk( const char *pFileName, const char *pFileFormat, KeyValues *pContextKeyValues ) = 0;
  31. };
  32. //-----------------------------------------------------------------------------
  33. // This is a helper class designed to help with chains of modal dialogs
  34. //-----------------------------------------------------------------------------
  35. enum FileOpenStateMachineFlags_t
  36. {
  37. FOSM_SHOW_PERFORCE_DIALOGS = 0x1,
  38. FOSM_SHOW_SAVE_QUERY = 0x2,
  39. };
  40. class FileOpenStateMachine : public Panel
  41. {
  42. DECLARE_CLASS_SIMPLE( FileOpenStateMachine, Panel );
  43. public:
  44. enum CompletionState_t
  45. {
  46. IN_PROGRESS = 0, // Still not finished, not successful or error
  47. SUCCESSFUL, // Operation finished successfully
  48. FILE_SAVE_CANCELLED, // The user chose 'cancel' in the dialog asking if he wanted to save
  49. FILE_SAVE_NAME_NOT_SPECIFIED, // User hit cancel in the SaveAs dialog
  50. FILE_NOT_OVERWRITTEN, // Operation aborted; existed file and user chose to not write over it
  51. FILE_NOT_CHECKED_OUT, // Operation aborted; file wasn't checked out so couldn't be written over
  52. ERROR_WRITING_FILE, // Error occurred writing the file out
  53. ERROR_MAKING_FILE_WRITEABLE, // Error occurred when making the file writeable
  54. FILE_NOT_MADE_WRITEABLE, // User chose to not make the file be writeable
  55. FILE_OPEN_NAME_NOT_SPECIFIED, // User hit cancel in the Open dialog
  56. ERROR_READING_FILE, // Error occurred reading the file in
  57. };
  58. FileOpenStateMachine( vgui::Panel *pParent, IFileOpenStateMachineClient *pClient );
  59. virtual ~FileOpenStateMachine();
  60. // Opens a file, saves an existing one if necessary
  61. void OpenFile( const char *pOpenFileType, KeyValues *pContextKeyValues, const char *pSaveFileName = NULL, const char *pSaveFileType = NULL, int nFlags = 0 );
  62. // Version of OpenFile that skips browsing for a particular file to open
  63. void OpenFile( const char *pOpenFileName, const char *pOpenFileType, KeyValues *pContextKeyValues, const char *pSaveFileName = NULL, const char *pSaveFileType = NULL, int nFlags = 0 );
  64. // Used to save a specified file, and deal with all the lovely dialogs
  65. // Pass in NULL to get a dialog to choose a filename to save
  66. void SaveFile( KeyValues *pContextKeyValues, const char *pFileName, const char *pFileType, int nFlags = FOSM_SHOW_PERFORCE_DIALOGS );
  67. // Returns the state machine completion state
  68. CompletionState_t GetCompletionState();
  69. /* MESSAGES SENT
  70. "FileStateMachineFinished" - Called when we exit the state machine for any reason
  71. "completionState" - See the CompletionState_t enum above
  72. "wroteFile" - Indicates whether a file was written or not
  73. "fullPath" - Indicates the full path of the file read for OpenFile or written for SaveFile
  74. "fileType" - Indicates the file type of the file read for OpenFile or written for SaveFile
  75. Use GetFirstTrueSubKey() to get the context passed into the OpenFile/SaveFile methods
  76. */
  77. private:
  78. enum FOSMState_t
  79. {
  80. STATE_NONE = -1,
  81. STATE_SHOWING_SAVE_DIRTY_FILE_DIALOG = 0,
  82. STATE_SHOWING_SAVE_DIALOG,
  83. STATE_SHOWING_OVERWRITE_DIALOG,
  84. STATE_SHOWING_CHECK_OUT_DIALOG,
  85. STATE_SHOWING_MAKE_FILE_WRITEABLE_DIALOG,
  86. STATE_WRITING_FILE,
  87. STATE_SHOWING_PERFORCE_ADD_DIALOG,
  88. STATE_SHOWING_OPEN_DIALOG,
  89. STATE_READING_FILE,
  90. };
  91. MESSAGE_FUNC_PARAMS( OnFileSelected, "FileSelected", pKeyValues );
  92. MESSAGE_FUNC( OnFileSelectionCancelled, "FileSelectionCancelled" );
  93. MESSAGE_FUNC_PARAMS( OnPerforceQueryCompleted, "PerforceQueryCompleted", pKeyValues );
  94. MESSAGE_FUNC( OnMakeFileWriteable, "MakeFileWriteable" );
  95. MESSAGE_FUNC( OnCancelMakeFileWriteable, "CancelMakeFileWriteable" );
  96. // These messages are related to the dialog in OverwriteFileDialog
  97. MESSAGE_FUNC( OnOverwriteFile, "OverwriteFile" );
  98. MESSAGE_FUNC( OnCancelOverwriteFile, "CancelOverwriteFile" );
  99. // These messages come from the savedocumentquery dialog
  100. MESSAGE_FUNC( OnSaveFile, "OnSaveFile" );
  101. MESSAGE_FUNC( OnMarkNotDirty, "OnMarkNotDirty" );
  102. MESSAGE_FUNC( OnCancelSaveDocument, "OnCancelSaveDocument" );
  103. // Cleans up keyvalues
  104. void CleanUpContextKeyValues();
  105. // Utility to set the completion state
  106. void SetCompletionState( CompletionState_t state );
  107. // Show the save document query dialog
  108. void ShowSaveQuery( );
  109. // Shows the overwrite existing file dialog
  110. void OverwriteFileDialog( );
  111. // Shows the open file for edit dialog
  112. void CheckOutDialog( );
  113. // Shows the make file writeable dialog
  114. void MakeFileWriteableDialog( );
  115. // Writes the file out
  116. void WriteFile();
  117. // Shows the open file dialog
  118. void OpenFileDialog( );
  119. // Reads the file in
  120. void ReadFile();
  121. IFileOpenStateMachineClient *m_pClient;
  122. KeyValues *m_pContextKeyValues;
  123. FOSMState_t m_CurrentState;
  124. CompletionState_t m_CompletionState;
  125. CUtlString m_FileName;
  126. CUtlString m_SaveFileType;
  127. CUtlString m_OpenFileType;
  128. CUtlString m_OpenFileName;
  129. bool m_bShowPerforceDialogs : 1;
  130. bool m_bShowSaveQuery : 1;
  131. bool m_bIsOpeningFile : 1;
  132. bool m_bWroteFile : 1;
  133. };
  134. } // end namespace vgui
  135. #endif // FILEOPENSTATEMACHINE_H