root/trunk/GUIComponents/FECFileDialog.cpp

Revision 5, 6.1 kB (checked in by qbert, 3 years ago)

Initial ( and last :( ) commit

Line 
1 #include "stdafx.h"
2 #include "FECFileDialog.h"
3
4 #ifndef _INC_CDERR
5 #include <cderr.h>     // for FNERR_BUFFERTOSMALL
6 #endif // _INC_CDERR
7
8 #ifdef _DEBUG
9 #define new DEBUG_NEW
10 #undef THIS_FILE
11 static char THIS_FILE[] = __FILE__;
12 #endif
13
14 /////////////////////////////////////////////////////////////////////////////
15 // CFECFileDialog
16
17 IMPLEMENT_DYNAMIC(CFECFileDialog, CFileDialog)
18 /////////////////////////////////////////////////////////////////////////////
19 //
20 //  CFECFileDialog constructor  (public member function)
21 //    Initializes the class variables
22 //
23 //  Parameters :
24 //    See CFileDialog::CFileDialog for parameter information
25 //
26 //  Returns :
27 //    Nothing
28 //
29 /////////////////////////////////////////////////////////////////////////////
30
31
32 CFECFileDialog::CFECFileDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
33                                DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
34 CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
35 {
36     Files = NULL;
37     Folder = NULL;
38     bParsed = FALSE;
39 }
40 /////////////////////////////////////////////////////////////////////////////
41 //
42 //  CFECFileDialog destructor  (public member function)
43 //    Cleans up the class variables
44 //
45 //  Parameters :
46 //    None
47 //
48 //  Returns :
49 //    Nothing
50 //
51 /////////////////////////////////////////////////////////////////////////////
52
53
54 CFECFileDialog::~CFECFileDialog()
55 {
56     if (Files)
57     {
58         delete[] Files;
59         delete[] Folder;
60     }
61 }
62
63 BEGIN_MESSAGE_MAP(CFECFileDialog, CFileDialog)
64 //{{AFX_MSG_MAP(CFECFileDialog)
65 //}}AFX_MSG_MAP
66 END_MESSAGE_MAP()
67
68 /////////////////////////////////////////////////////////////////////////////
69 //
70 //  CFECFileDialog::DoModal  (public member function)
71 //    Starts the CFileDialog and properly handles the return code
72 //
73 //  Parameters :
74 //    None
75 //
76 //  Returns :
77 //    IDOK if the user selected the OK button
78 //    IDCANCEL if the user selected the Cancel button or an error occured
79 //
80 /////////////////////////////////////////////////////////////////////////////
81
82 int CFECFileDialog::DoModal()
83 {
84     if (Files)
85     {
86         delete[] Files;
87         Files = NULL;
88         delete[] Folder;
89         Folder = NULL;
90     }
91
92     int ret = CFileDialog::DoModal();
93
94     if (ret == IDCANCEL)
95     {
96         DWORD err = CommDlgExtendedError();
97         if (err == FNERR_BUFFERTOOSMALL/*0x3003*/ && Files)
98             ret = IDOK;
99     }
100     return ret;
101 }
102
103 /////////////////////////////////////////////////////////////////////////////
104 //
105 //  CFECFileDialog::GetNextPathName  (public member function)
106 //    Call this function to retrieve the next file name from the group
107 //    selected in the dialog box.
108 //
109 //  Parameters :
110 //    pos - A reference to a POSITION value returned from a previous GetNextPathName
111 //          or GetStartPosition function call. Will be set to NULL when the end of
112 //          the list has been reached.
113 //
114 //  Returns :
115 //    The full path of the file
116 //
117 /////////////////////////////////////////////////////////////////////////////
118
119 CString CFECFileDialog::GetNextPathName(POSITION &pos) const
120 {
121     if (!Files)
122         return CFileDialog::GetNextPathName(pos);
123
124     ASSERT(pos);   
125     TCHAR *ptr = (TCHAR *)pos;
126
127     CString ret = Folder;
128     ret += _T("\\");
129     ret += ptr;
130
131     ptr += _tcslen(ptr) + 1;
132     if (*ptr)
133         pos = (POSITION)ptr;
134     else
135         pos = NULL;
136
137     return ret;
138 }
139
140 /////////////////////////////////////////////////////////////////////////////
141 //
142 //  CFECFileDialog::GetStartPosition  (public member function)
143 //    Call this member function to retrieve the POSITION of the first file
144 //    in a list of multiple selected files.
145 //
146 //  Parameters :
147 //    None
148 //
149 //  Returns :
150 //    A POSITION value that is used by the GetNextPathName function
151 //
152 /////////////////////////////////////////////////////////////////////////////
153
154 POSITION CFECFileDialog::GetStartPosition()
155 {
156     if (!Files)
157         return CFileDialog::GetStartPosition();
158
159     if (!bParsed)
160     {
161         CString temp = Files;
162         temp.Replace(_T("\" \""), _T("\""));
163         temp.Delete(0, 1);                      // remove leading quote mark
164         temp.Delete(temp.GetLength() - 1, 1);   // remove trailing space
165    
166         _tcscpy(Files, temp);
167    
168         TCHAR *ptr = Files;
169         while (*ptr)
170         {
171             if ('\"' == *ptr)
172                 *ptr = '\0';
173             ++ptr;
174         }
175         bParsed = TRUE;
176     }
177    
178     return (POSITION)Files;
179 }
180
181 /////////////////////////////////////////////////////////////////////////////
182 //
183 //  CFECFileDialog::OnFileNameChange  (protected member function)
184 //    If the lpstrFile and nMaxFile variables in the OPENFILENAME structure
185 //    are not large enough to handle the users selection, we handle the
186 //    selection ourselves.
187 //
188 //  Parameters :
189 //    None
190 //
191 //  Returns :
192 //    Nothing
193 //
194 //  Note :
195 //    Works only if the CFileDialog is created with the OFN_EXPLORER flag
196 //
197 /////////////////////////////////////////////////////////////////////////////
198
199 void CFECFileDialog::OnFileNameChange()
200 {
201     TCHAR dummy_buffer;
202    
203     // Get the required size for the 'files' buffer
204     UINT nfiles = CommDlg_OpenSave_GetSpec(GetParent()->m_hWnd, &dummy_buffer, 1);
205
206     // Get the required size for the 'folder' buffer
207     UINT nfolder = CommDlg_OpenSave_GetFolderPath(GetParent()->m_hWnd, &dummy_buffer, 1);
208
209     // Check if lpstrFile and nMaxFile are large enough
210     if (nfiles + nfolder > m_ofn.nMaxFile)
211     {
212         bParsed = FALSE;
213         if (Files)
214             delete[] Files;
215         Files = new TCHAR[nfiles + 1];
216         CommDlg_OpenSave_GetSpec(GetParent()->m_hWnd, Files, nfiles);
217
218         if (Folder)
219             delete[] Folder;
220         Folder = new TCHAR[nfolder + 1];
221         CommDlg_OpenSave_GetFolderPath(GetParent()->m_hWnd, Folder, nfolder);
222     }
223     else if (Files)
224     {
225         delete[] Files;
226         Files = NULL;
227         delete[] Folder;
228         Folder = NULL;
229     }
230
231     CFileDialog::OnFileNameChange();
232 }
Note: See TracBrowser for help on using the browser.