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.

224 lines
6.4 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose:
  4. //=============================================================================//
  5. #ifndef PANORAMA_FILEOPENDIALOG_H
  6. #define PANORAMA_FILEOPENDIALOG_H
  7. #ifdef _WIN32
  8. #pragma once
  9. #endif
  10. #include "panel2d.h"
  11. #include "../uievent.h"
  12. #include "panorama/controls/button.h"
  13. namespace panorama
  14. {
  15. class CDropDown;
  16. class CLabel;
  17. class CTextEntry;
  18. class CFileOpenDialogEntry;
  19. DECLARE_PANORAMA_EVENT0( FileOpenDialogOpen );
  20. DECLARE_PANORAMA_EVENT0( FileOpenDialogCancel );
  21. DECLARE_PANORAMA_EVENT0( FileOpenDialogClose );
  22. DECLARE_PANORAMA_EVENT0( FileOpenDialogFolderUp );
  23. DECLARE_PANEL_EVENT1( FileOpenDialogSortByColumn, int );
  24. DECLARE_PANEL_EVENT1( FileOpenDialogSelectFile, uint32 );
  25. DECLARE_PANEL_EVENT1( FileOpenDialogDoubleClickFile, uint32 );
  26. DECLARE_PANORAMA_EVENT0( FileOpenDialogFullPathChanged );
  27. DECLARE_PANORAMA_EVENT0( FileOpenDialogFilterChanged );
  28. DECLARE_PANEL_EVENT1( FileOpenDialogFilesSelected, const char * );
  29. //-----------------------------------------------------------------------------
  30. // Purpose: generic open/save as file dialog, by default deletes itself on close
  31. //-----------------------------------------------------------------------------
  32. enum FileOpenDialogType_t
  33. {
  34. FOD_SAVE = 0,
  35. FOD_OPEN,
  36. FOD_SELECT_DIRECTORY,
  37. FOD_OPEN_MULTIPLE,
  38. };
  39. struct FileData_t
  40. {
  41. CUtlString m_FileAttributes;
  42. CUtlString m_CreationTime;
  43. int64 m_nCreationTime;
  44. CUtlString m_LastAccessTime;
  45. CUtlString m_LastWriteTime;
  46. int64 m_nLastWriteTime;
  47. int64 m_nFileSize;
  48. CUtlString m_FileName;
  49. CUtlString m_FullPath;
  50. CUtlString m_FileType;
  51. bool m_bDirectory;
  52. };
  53. enum FileOpenDialogSorting_t
  54. {
  55. FOD_SORT_NAME = 0,
  56. FOD_SORT_SIZE,
  57. FOD_SORT_TYPE,
  58. FOD_SORT_DATE_MODIFIED
  59. };
  60. //-----------------------------------------------------------------------------
  61. // Purpose: FileOpenDialog
  62. //-----------------------------------------------------------------------------
  63. class CFileOpenDialog : public CPanel2D
  64. {
  65. DECLARE_PANEL2D( CFileOpenDialog, CPanel2D );
  66. public:
  67. CFileOpenDialog( CPanel2D *parent, const char * pchPanelID, FileOpenDialogType_t type );
  68. CFileOpenDialog( panorama::IUIWindow *pParent, const char * pchPanelID, FileOpenDialogType_t type );
  69. virtual ~CFileOpenDialog();
  70. // Set the directory the file search starts in
  71. void SetStartDirectory(const char *dir);
  72. // Sets the start directory context (and resets the start directory in the process)
  73. // NOTE: If you specify a startdir context, then if you've already opened
  74. // a file with that same start dir context before, it will start in the
  75. // same directory it ended up in.
  76. void SetStartDirectoryContext( const char *pContext, const char *pDefaultDir );
  77. // Add filters for the drop down combo box
  78. // The filter info, if specified, gets sent back to the app in the FileSelected message
  79. void AddFilter( const char *filter, const char *filterName, bool bActive, const char *pFilterInfo = NULL );
  80. // Get the directory this is currently in
  81. void GetDirectory( char *buf, int bufSize );
  82. // Get the last selected file name
  83. void GetSelectedFileName( char *buf, int bufSize );
  84. /*
  85. messages sent:
  86. "FileSelected"
  87. "fullpath" // specifies the fullpath of the file
  88. "filterinfo" // Returns the filter info associated with the active filter
  89. "FileSelectionCancelled"
  90. */
  91. static bool FileNameWildCardMatch( char const *pchFileName, char const *pchPattern );
  92. // event handlers
  93. bool EventOpen();
  94. bool EventCancel();
  95. bool EventClose();
  96. bool EventFolderUp();
  97. bool EventColumnSortingChanged( const CPanelPtr< IUIPanel > &pPanel, int nColumn );
  98. bool EventSelectFile( const CPanelPtr< IUIPanel > &pPanel, uint32 unModifiers );
  99. bool EventDoubleClickFile( const CPanelPtr< IUIPanel > &pPanel, uint32 unModifiers );
  100. bool EventFullPathChanged();
  101. bool EventFilterChanged();
  102. protected:
  103. void Init();
  104. void PopulateFileList();
  105. void PopulateDriveList();
  106. void OnOpen();
  107. // TODO: needs message? hooked up to buttons/rows?
  108. void OnSelectFolder();
  109. void OnMatchStringSelected();
  110. // moves the directory structure up
  111. void MoveUpFolder();
  112. // validates that the current path is valid
  113. void ValidatePath();
  114. private:
  115. // Does the specified extension match something in the filter list?
  116. bool ExtensionMatchesFilter( const char *pExt );
  117. // Choose the first non *.* filter in the filter list
  118. void ChooseExtension( char *pExt, int nBufLen );
  119. // Saves the file to the start dir context
  120. void SaveFileToStartDirContext( const char *pFullPath );
  121. // Posts a file selected message
  122. void PostFileSelectedMessage( const char *pFileName );
  123. // Posts a multiple file selected message
  124. void PostMultiFileSelectedMessage();
  125. void BuildFileList();
  126. void FilterFileList();
  127. void SortEntries();
  128. bool PassesFilter( FileData_t *fd );
  129. int CountSubstringMatches();
  130. void DeselectAllEntries();
  131. CDropDown *m_pFullPathDropDown;
  132. CPanel2D *m_pFileList; // TODO: custom spreadsheet style control?
  133. CTextEntry *m_pFileNameTextEntry;
  134. CDropDown *m_pFileTypeCombo;
  135. CButton *m_pOpenButton;
  136. CButton *m_pCancelButton;
  137. CButton *m_pFolderUpButton;
  138. CLabel *m_pFileTypeLabel;
  139. CUtlVector<CPanel2D*> m_vecColumnHeaders;
  140. KeyValues *m_pContextKeyValues;
  141. FileOpenDialogSorting_t m_nSorting;
  142. bool m_bSortingReversed;
  143. char m_szLastPath[1024];
  144. unsigned short m_nStartDirContext;
  145. FileOpenDialogType_t m_DialogType;
  146. bool m_bFileSelected : 1;
  147. CUtlVector< FileData_t > m_Files;
  148. CUtlVector< FileData_t * > m_Filtered;
  149. CUtlVector< CFileOpenDialogEntry* > m_vecSelectedEntries;
  150. CUtlString m_CurrentSubstringFilter;
  151. };
  152. //-----------------------------------------------------------------------------
  153. // Purpose: CFileOpenDialogEntry - single row in the dialog, represents one file
  154. //-----------------------------------------------------------------------------
  155. class CFileOpenDialogEntry : public CButton
  156. {
  157. DECLARE_PANEL2D( CFileOpenDialogEntry, CButton );
  158. public:
  159. CFileOpenDialogEntry( CPanel2D *parent, const char * pchPanelID );
  160. virtual ~CFileOpenDialogEntry();
  161. void SetFileData( FileData_t *pFileData );
  162. const FileData_t* GetFileData() const { return &m_FileData; }
  163. virtual bool OnMouseButtonUp( const panorama::MouseData_t &code ) OVERRIDE;
  164. virtual bool OnMouseButtonDoubleClick( const panorama::MouseData_t &code ) OVERRIDE;
  165. bool OnScrolledIntoView( const CPanelPtr< IUIPanel > &panelPtr );
  166. private:
  167. FileData_t m_FileData;
  168. bool m_bCreatedControls;
  169. };
  170. } // namespace panorama
  171. #endif // PANORAMA_FILEOPENDIALOG_H