summaryrefslogtreecommitdiffstats
path: root/Source/MFCDialog/PathDialog.cpp
blob: d7bd4279836b198feb989b17a5909be33db9db90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//////////////////////////////////////////////////////////////////////////
//PathDialog.h file
//
//Written by Nguyen Tan Hung <tanhung@yahoo.com>
//////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PathDialog.h"
#include <io.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define IDC_FOLDERTREE          0x3741
#define IDC_TITLE                       0x3742
#define IDC_STATUSTEXT          0x3743

#define IDC_NEW_EDIT_PATH       0x3744

// Class CDlgWnd
BEGIN_MESSAGE_MAP(CPathDialogSub, CWnd)
  ON_BN_CLICKED(IDOK, OnOK)
  ON_EN_CHANGE(IDC_NEW_EDIT_PATH, OnChangeEditPath)
  END_MESSAGE_MAP()

  void CPathDialogSub::OnOK()
{
  ::GetWindowText(::GetDlgItem(m_hWnd, IDC_NEW_EDIT_PATH),
                  m_pPathDialog->m_szPathName, MAX_PATH);

  if(CPathDialog::MakeSurePathExists(m_pPathDialog->m_szPathName)==0)
    {
    m_pPathDialog->m_bGetSuccess=TRUE;
    ::EndDialog(m_pPathDialog->m_hWnd, IDOK);
    }
  else
    {
    ::SetFocus(::GetDlgItem(m_hWnd, IDC_NEW_EDIT_PATH));
    }
}

void CPathDialogSub::OnChangeEditPath()
{
  ::GetWindowText(::GetDlgItem(m_hWnd, IDC_NEW_EDIT_PATH),
                  m_pPathDialog->m_szPathName, MAX_PATH);
  BOOL bEnableOKButton = (_tcslen(m_pPathDialog->m_szPathName)>0);
  SendMessage(BFFM_ENABLEOK, 0, bEnableOKButton);
}
/////////////////////////////////////////////////////////////////////////////
// CPathDialog dialog


CPathDialog::CPathDialog(LPCTSTR lpszCaption, 
                         LPCTSTR lpszTitle, 
                         LPCTSTR lpszInitialPath, 
                         CWnd* pParent)
{
  m_hWnd=NULL;
  m_PathDialogSub.m_pPathDialog= this;
  m_bParentDisabled = FALSE;

  // Get the true parent of the dialog
  m_pParentWnd = CWnd::GetSafeOwner(pParent);

  m_lpszCaption = lpszCaption;
  m_lpszInitialPath = lpszInitialPath;

  memset(&m_bi, 0, sizeof(BROWSEINFO) );
  m_bi.hwndOwner = (m_pParentWnd==NULL)?NULL:m_pParentWnd->GetSafeHwnd();
  m_bi.pszDisplayName = 0;
  m_bi.pidlRoot = 0;
  m_bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
  m_bi.lpfn = BrowseCallbackProc;
  m_bi.lpszTitle = lpszTitle;
}


/////////////////////////////////////////////////////////////////////////////
// CPathDialog message handlers

CString CPathDialog::GetPathName()
{
  return CString(m_szPathName);
}

int CALLBACK CPathDialog::BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lParam, LPARAM pData) 
{
  CPathDialog* pDlg = (CPathDialog*)pData;

  switch(uMsg) 
    {
    case BFFM_INITIALIZED: 
    {
    RECT rc;
    HWND hEdit;
    HFONT hFont;

    pDlg->m_hWnd = hwnd;

    if(pDlg->m_lpszCaption!=NULL)
      {
      ::SetWindowText(hwnd, pDlg->m_lpszCaption);
      }

    VERIFY(pDlg->m_PathDialogSub.SubclassWindow(hwnd));
    ::ShowWindow(::GetDlgItem(hwnd, IDC_STATUSTEXT), SW_HIDE);
    ::GetWindowRect(::GetDlgItem(hwnd, IDC_FOLDERTREE), &rc);
    rc.bottom = rc.top - 4;
    rc.top = rc.bottom - 23;
    ::ScreenToClient(hwnd, (LPPOINT)&rc);
    ::ScreenToClient(hwnd, ((LPPOINT)&rc)+1);
    hEdit = ::CreateWindowEx(WS_EX_CLIENTEDGE, _T("EDIT"), _T(""),
                             WS_CHILD|WS_TABSTOP|WS_VISIBLE|ES_AUTOHSCROLL,
                             rc.left, rc.top, 
                             rc.right-rc.left, rc.bottom-rc.top, 
                             hwnd, NULL, NULL, NULL);
    ::SetWindowLong(hEdit, GWL_ID, IDC_NEW_EDIT_PATH);
    ::ShowWindow(hEdit, SW_SHOW);

    hFont = (HFONT)::SendMessage(hwnd, WM_GETFONT, 0, 0);
    ::SendMessage(hEdit, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));

    LPCTSTR lpszPath = pDlg->m_lpszInitialPath;
    TCHAR szTemp[MAX_PATH];
    if(lpszPath==NULL)
      {
      ::GetCurrentDirectory(MAX_PATH, szTemp );
      lpszPath = szTemp;
      }
    // WParam is TRUE since you are passing a path.
    // It would be FALSE if you were passing a pidl.
    ::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,
                  (LPARAM)lpszPath);
    break;
    }
    case BFFM_SELCHANGED:
    {
    char szSelection[MAX_PATH];
    if(!::SHGetPathFromIDList((LPITEMIDLIST)lParam, szSelection) ||
       (szSelection[1] !=':' && szSelection[1] != '\\'))
      {
      szSelection[0] = '\0';
      ::SendMessage(hwnd, BFFM_ENABLEOK, 0, FALSE);
      }
    else
      {
      ::SendMessage(hwnd, BFFM_ENABLEOK, 0, TRUE);
      }
    ::SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szSelection);
    ::SetWindowText(::GetDlgItem(hwnd, IDC_NEW_EDIT_PATH), szSelection);
    break;
    }
    default:
      break;
    }
  return 0;
}



int CPathDialog::DoModal() 
{

  /////////////////////////////////////////////////////////
  TCHAR szPathTemp[MAX_PATH];
  m_bi.lpfn = BrowseCallbackProc;  // address of callback function
  m_bi.lParam = (LPARAM)this;      // pass address of object to callback function
  m_bi.pszDisplayName = szPathTemp;

  LPITEMIDLIST pidl;
  LPMALLOC pMalloc;

  int iResult=-1;
  if(SUCCEEDED(SHGetMalloc(&pMalloc)))
    {
    m_bGetSuccess = FALSE;
    pidl = SHBrowseForFolder(&m_bi);
    if (pidl!=NULL) 
      {
#if defined(_WIN64) && defined(__INTEL_COMPILER)
# pragma warning ( disable : 167)
#endif
      // In C++: 
      pMalloc->Free(pidl);
      //In C:
      //pMalloc->lpVtbl->Free(pMalloc,pidl);
      //pMalloc->lpVtbl->Release(pMalloc);
      }
    if(m_bGetSuccess)
      {
      iResult = IDOK;
      }
    pMalloc->Release();
    }

  if(m_bParentDisabled && (m_pParentWnd!=NULL))
    {
    m_pParentWnd->EnableWindow(TRUE);
    }
  m_bParentDisabled=FALSE;

  return iResult;
}

BOOL CPathDialog::IsFileNameValid(LPCTSTR /* lpFileName */)
{
  return TRUE;
}

const TCHAR c_FolderDoesNotExist[] = _T(
  "The folder:\n\n"
  "%s\n\n"
  "does not exist. Do you want the folder to be created?");
const TCHAR c_szErrInvalidPath[] = _T(
  "The folder:"
  "\n\n"
  "%s\n\n"
  "is invalid. Please reenter.");
const TCHAR c_szErrCreatePath[] = _T(
  "The folder:"
  "\n\n"
  "%s"
  "\n\ncan not be created. Please double check.");

//return -1: user break;
//return 0: no error
//return 1: lpPath is invalid
//return 2: can not create lpPath
int CPathDialog::MakeSurePathExists(LPCTSTR lpPath)
{
  CString strMsg;
  int iRet = -1;
  try
    {
    //validate path
    iRet=Touch(lpPath, TRUE);
    if(iRet!=0)
      {
      throw iRet;
      }

    if(_taccess(lpPath, 0)==0)
      {
      return (int)0;
      }

    strMsg.Format(c_FolderDoesNotExist, lpPath);
    if(AfxMessageBox(strMsg, MB_YESNO|MB_ICONQUESTION) == IDYES)
      {
      //create path
      iRet=Touch(lpPath, FALSE);
      if(iRet!=0)
        {
        throw iRet; 
        }
      }
    return 0;
    }
  catch(int nErrCode)
    {
    switch(nErrCode)
      {
      case 1:
        strMsg.Format(c_szErrInvalidPath, lpPath);
        break;
      case 2:
      default:
        strMsg.Format(c_szErrCreatePath, lpPath);
        break;
      }

    AfxMessageBox(strMsg, MB_OK|MB_ICONEXCLAMATION);
    }

  return iRet;
}

//return 0: no error
//return 1: lpPath is invalid
//return 2: lpPath can not be created(bValidate==FALSE)
int CPathDialog::Touch(LPCTSTR lpPath, BOOL bValidate)
{
  if(lpPath==NULL)
    {
    return 1;
    }

  TCHAR szPath[MAX_PATH];
  _tcscpy(szPath, lpPath);
  size_t nLen = _tcslen(szPath);

  int i;
  if(nLen==3)
    {
    if(!bValidate)
      {
      if(_access(szPath, 0)!=0)
        {
        return 2;
        }
      }
    return 0;
    }

  i = 3;
  BOOL bLastOne=TRUE;
  LPTSTR lpCurrentName;
  while(szPath[i]!=0)
    {
    lpCurrentName = &szPath[i];
    while( (szPath[i]!=0) && (szPath[i]!=_T('\\')) )
      {
      i++;
      }

    bLastOne =(szPath[i]==0);
    szPath[i] = 0;

    if(!bValidate)
      {
      CreateDirectory(szPath, NULL);
      if(_taccess(szPath, 0)!=0)
        {
        return 2;
        }
      }

    if(bLastOne)
      {
      break; //it's done
      }
    else
      {
      szPath[i] = _T('\\');
      }

    i++;
    }

  return (bLastOne?0:1);
}

//return 0: ok
//return 1: error
int CPathDialog::ConcatPath(LPTSTR lpRoot, LPCTSTR lpMorePath)
{
  if(lpRoot==NULL)
    {
    return 1;
    }

  size_t nLen = _tcslen(lpRoot);

  if(nLen<3)
    {
    return 1;
    }

  if(lpMorePath==NULL)
    {
    return 0;
    }

  if(nLen==3)
    {
    _tcscat(lpRoot, lpMorePath);
    return 0;
    }

  _tcscat(lpRoot, _T("\\"));
  _tcscat(lpRoot, lpMorePath);

  return 0;
}