Source code of Windows XP (NT5)
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.

167 lines
4.2 KiB

  1. //
  2. // FileChooser.h
  3. //
  4. #ifndef _FILE_CHOOSER_H
  5. #define _FILE_CHOOSER_H
  6. #pragma warning(disable : 4275)
  7. #if _MSC_VER > 1000
  8. #pragma once
  9. #endif // _MSC_VER > 1000
  10. class CFileChooser;
  11. class CFileChooserEdit;
  12. class CFilterEntry
  13. {
  14. public:
  15. CFilterEntry()
  16. {
  17. }
  18. CFilterEntry(LPCTSTR text, LPCTSTR ext)
  19. : m_text(text), m_ext(ext)
  20. {
  21. }
  22. CString m_text;
  23. CString m_ext;
  24. };
  25. #define FC_UNDEFINED 0x00000000
  26. #define FC_FORWRITE 0x00000001
  27. #define FC_AUTOCOMPLETION 0x00000002
  28. // Put "*.ext" to edit initially if no file with default
  29. // extensions was found in the initial directory
  30. #define FC_WILDCARD_DEFAULT 0x00000004
  31. // Set "description (*.ext)" to FileDialog filter
  32. #define FC_WILDCARD_DESC 0x00000008
  33. // prefill the path edit with default file name
  34. #define FC_PREPARE_DEFAULT 0x00000010
  35. // supress file existance check
  36. #define FC_PATH_CHECK 0x00000020
  37. // Check if entered filename with any of default extensions
  38. // are available in the current directory. If yes, choose it
  39. #define FC_CHECK_FILENAME_ONLY 0x00000040
  40. #define FC_DIRECTORY_ONLY 0x00000080
  41. #define FC_HIDEREADONLY 0x00000100
  42. #define FC_COMMANDLINE 0x00000200
  43. #define FC_DEFAULT\
  44. FC_AUTOCOMPLETION | FC_WILDCARD_DESC | FC_WILDCARD_DEFAULT | FC_PATH_CHECK
  45. #define FC_DEFAULT_READ\
  46. FC_DEFAULT | FC_HIDEREADONLY
  47. #define FC_DEFAULT_WRITE\
  48. FC_DEFAULT | FC_FORWRITE
  49. #define FC_SUCCESS 0x00000000
  50. #define FC_FILE_DOES_NOT_EXIST 0x00000001
  51. #define FC_FILENAME_IS_DIRECTORY 0x00000002
  52. #define FC_FILENAME_IS_FILE 0x00000003
  53. #define FC_TEXT_IS_INVALID 0x00000004
  54. #define FC_WRONG_FORMAT 0x00000005
  55. #define FC_NO_CLOSING_QUOTE 0x00000006
  56. class _EXPORT CFileChooser :
  57. public CWindowImpl<CFileChooser>
  58. {
  59. friend class CFileChooserEdit;
  60. friend class CFileChooserButton;
  61. public:
  62. CFileChooser()
  63. : m_pParent(NULL),
  64. m_bDoReplaceFile(FALSE),
  65. m_bEditDirty(FALSE),
  66. m_bTextValid(TRUE),
  67. m_bDialogActive(FALSE),
  68. m_dwStyle(FC_UNDEFINED),
  69. m_ofn_Flags(0),
  70. m_edit(NULL),
  71. m_button(NULL)
  72. {
  73. }
  74. ~CFileChooser();
  75. BEGIN_MSG_MAP_EX(CFileChooser)
  76. END_MSG_MAP()
  77. BOOL Init(CWindow * pParent, DWORD dwStyle, UINT idEdit, UINT idButton);
  78. DWORD GetStyle() const
  79. {
  80. return m_dwStyle;
  81. }
  82. DWORD SetStyle(DWORD dwStyle)
  83. {
  84. DWORD dw = m_dwStyle;
  85. m_dwStyle = dwStyle;
  86. return dw;
  87. }
  88. BOOL StyleBitSet(DWORD bit)
  89. {
  90. return 0 != (m_dwStyle & bit);
  91. }
  92. BOOL OpenForRead()
  93. {
  94. return !StyleBitSet(FC_FORWRITE);
  95. }
  96. void AddStyle(DWORD dwStyle)
  97. {
  98. m_dwStyle |= dwStyle;
  99. }
  100. void RemoveStyle(DWORD dwStyle)
  101. {
  102. m_dwStyle &= ~dwStyle;
  103. }
  104. void SetOfnFlags(DWORD flags)
  105. {
  106. m_ofn_Flags = flags;
  107. }
  108. DWORD GetOfnFlags()
  109. {
  110. return m_ofn_Flags;
  111. }
  112. void SetDialogTitle(LPCTSTR strTitle)
  113. {
  114. m_strTitle = strTitle;
  115. }
  116. DWORD GetFileName(CString& str);
  117. void SetPath(const CString& str);
  118. void AddExtension(LPCTSTR text, LPCTSTR ext);
  119. void AddExtension(HINSTANCE hInst, UINT idText, UINT idExt);
  120. int BrowseForFolderCallback(HWND hwnd, UINT uMsg, LPARAM lParam);
  121. protected:
  122. void OnBrowseBtn();
  123. void CreateFilter(CString& strFilter, CString& strDefExt);
  124. void CreateDefaultPathForRead();
  125. BOOL BrowseForFile(CString& strPath, CString& strFile);
  126. BOOL BrowseForFolder(CString& strPath);
  127. BOOL OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
  128. void OnPaste();
  129. void OnSetEditFocus();
  130. void OnKillEditFocus();
  131. BOOL HasEditFocus();
  132. BOOL IsValidChar(UINT nChar, BOOL bExcludeWildcards = FALSE);
  133. BOOL IsValidPath(LPCTSTR);
  134. void SetCompactedPath(LPCTSTR path);
  135. void SetPathToEdit(LPCTSTR path);
  136. int ExtractPath(LPTSTR path);
  137. int ExtractArgs(LPTSTR buf);
  138. void GetText(LPTSTR buf);
  139. int GetFilterIndex(const CString& fileName);
  140. protected:
  141. DWORD m_ofn_Flags;
  142. CWindow * m_pParent;
  143. CFileChooserEdit * m_edit;
  144. CFileChooserButton * m_button;
  145. DWORD m_dwStyle;
  146. CString m_strPath;
  147. LPTSTR m_pPathTemp;
  148. CString m_strTitle;
  149. std::list<CFilterEntry> m_ext;
  150. BOOL m_bDoReplaceFile;
  151. BOOL m_bEditDirty;
  152. BOOL m_bTextValid;
  153. BOOL m_bDialogActive;
  154. };
  155. #endif //_FILE_CHOOSER_H