1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the Free
10 // Software Foundation; either version 2 of the License, or (at your option)
13 // This program is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc.,
20 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 // FolderDialog.cpp : implementation file
28 //===========================================================================
29 //#####DESCRIPTIONBEGIN####
36 // Description: This is the implementation of the _T("browse for folder") dialog class
43 //####DESCRIPTIONEND####
45 //===========================================================================
48 #include "FolderDialog.h"
51 #include "NewFolderDialog.h"
56 static char THIS_FILE[] = __FILE__;
59 /////////////////////////////////////////////////////////////////////////////
60 // CFolderDialog dialog
62 CFolderDialog::CFolderDialog(BOOL bAllowCreation/*=TRUE*/,UINT id/*=0*/)
63 : CeCosDialog(id?id:IDD_FOLDER_DIALOG, NULL),
64 m_bAllowCreation(bAllowCreation),
67 //{{AFX_DATA_INIT(CFolderDialog)
69 m_strTitle=_T("Choose Folder");
70 GetCurrentDirectory(MAX_PATH,m_strFolder.GetBuffer(MAX_PATH));
71 m_strFolder.ReleaseBuffer();
75 void CFolderDialog::DoDataExchange(CDataExchange* pDX)
77 CeCosDialog::DoDataExchange(pDX);
78 //{{AFX_DATA_MAP(CFolderDialog)
83 BEGIN_MESSAGE_MAP(CFolderDialog, CeCosDialog)
84 //{{AFX_MSG_MAP(CFolderDialog)
85 ON_BN_CLICKED(IDC_FOLDER_DIALOG_BROWSE, OnBrowse)
86 ON_EN_CHANGE(IDC_FOLDER, OnChangeFolder)
90 /////////////////////////////////////////////////////////////////////////////
91 // CFolderDialog message handlers
93 void CFolderDialog::OnBrowse()
96 GetDlgItemText(IDC_FOLDER,m_strFolder);
99 bi.hwndOwner = GetSafeHwnd();
101 bi.pszDisplayName = m_strFolder.GetBuffer(MAX_PATH);
102 bi.lpszTitle = _T("");
103 bi.ulFlags = BIF_RETURNONLYFSDIRS/*|0x0010 BIF_EDITBOX*/;
104 bi.lpfn = (BFFCALLBACK)CBBrowseCallbackProc;
105 bi.lParam = (LPARAM)this;
107 static const UINT arids[]={IDOK,IDCANCEL,IDC_FOLDER_DIALOG_BROWSE,IDC_FOLDER};
110 ASSERT(sizeof arids/sizeof arids[0] == sizeof arbEnabled/sizeof arbEnabled[0]);
112 for(int i=0;i<sizeof arids/sizeof arids[0];i++){
114 arbEnabled[i]=GetDlgItem(id)->IsWindowEnabled();
115 GetDlgItem(id)->EnableWindow(FALSE);
117 LPITEMIDLIST iil = SHBrowseForFolder(&bi);
118 m_strFolder.ReleaseBuffer();
121 SHGetPathFromIDList(iil,m_strFolder.GetBuffer(MAX_PATH));
122 m_strFolder.ReleaseBuffer();
123 SetDlgItemText(IDC_FOLDER,m_strFolder);
126 for(i=0;i<sizeof arids/sizeof arids[0];i++){
128 GetDlgItem(id)->EnableWindow(arbEnabled[i]);
130 GetDlgItem(IDOK)->EnableWindow(!m_strFolder.IsEmpty());
132 if(m_bAllowCreation){
137 // Use callback to expand folder in edit box at time of browse
138 int CALLBACK CFolderDialog::CBBrowseCallbackProc( HWND hwnd,
144 CFolderDialog *pDlg=(CFolderDialog *)lpData;
146 case BFFM_INITIALIZED:
147 if(pDlg->m_bAllowCreation){
148 CWnd *pWnd=CWnd::FromHandle(hwnd);
149 pDlg->m_pButton=new CButton();
150 // Get rect of IDOK button
152 pWnd->GetDlgItem(IDOK)->GetWindowRect(&rect1);
153 pWnd->ScreenToClient(&rect1);
155 // Get rect of IDCANCEL button to the right
157 pWnd->GetDlgItem(IDCANCEL)->GetWindowRect(&rect2);
158 pWnd->ScreenToClient(&rect2);
159 int nXDiff=rect2.left-rect1.left;
164 pDlg->m_pButton->Create(_T("&New..."), WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, rect, pWnd, ID_NEW_FOLDER);
165 pDlg->m_pButton->SendMessage(WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
166 m_wndProc = (WNDPROC)SetWindowLong(hwnd, GWL_WNDPROC, (long)WindowProcNew);
168 ::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)(LPCTSTR)pDlg->m_strFolder);
169 case BFFM_SELCHANGED:
171 // Change the cwd such that if the New button is used, we know where we are to start from
172 ITEMIDLIST *iil=(ITEMIDLIST *)lParam;
174 SHGetPathFromIDList(iil,strFolder.GetBuffer(MAX_PATH));
175 strFolder.ReleaseBuffer();
176 SetCurrentDirectory(strFolder);
187 WNDPROC CFolderDialog::m_wndProc=NULL;
188 LRESULT CALLBACK CFolderDialog::WindowProcNew(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam)
190 if (message == WM_COMMAND && wParam==MAKEWPARAM(ID_NEW_FOLDER,BN_CLICKED)){
191 CNewFolderDialog dlg;
192 // Start from the cwd (maintained by BFFM_SELCHANGED above)
193 GetCurrentDirectory(MAX_PATH,dlg.m_strFolder.GetBuffer(MAX_PATH));
194 dlg.m_strFolder.ReleaseBuffer();
196 if(IDOK==dlg.DoModal()){
197 // Feed the new directory back as the current selection in the browse dialog
198 ::SendMessage (hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)(LPCTSTR )dlg.m_strFolder);
202 return CallWindowProc(m_wndProc, hwnd, message, wParam, lParam);
206 void CFolderDialog::OnOK()
208 GetDlgItemText(IDC_FOLDER,m_strFolder);
209 if(!m_strFolder.IsDir()){
210 if(m_bAllowCreation){
211 if(!m_strFolder.CreateDirectory()){
212 CUtils::MessageBoxF(_T("Could not create folder %s"),m_strFolder);
216 CUtils::MessageBoxF(_T("Folder %s does not exist"),m_strFolder);
219 } else if (m_bAllowCreation && IDNO==CUtils::MessageBoxFT(MB_YESNO|MB_DEFBUTTON2, _T("Folder %s already exists - overwrite?"),m_strFolder)){
225 BOOL CFolderDialog::OnInitDialog()
227 CeCosDialog::OnInitDialog();
228 if(m_bAllowCreation&&m_strDesc.IsEmpty()){
229 m_strDesc.LoadString(IDS_DEFAULT_FOLDER_DIALOG_DESC);
231 SetDlgItemText(IDC_STATIC_DESC,m_strDesc);
232 SetDlgItemText(IDC_FOLDER,m_strFolder);
233 SetWindowText(m_strTitle);
234 SetCurrentDirectory(m_strFolder);
235 SetDlgItemText(IDC_FOLDER,m_strFolder);
236 GetDlgItem(IDC_FOLDER)->SetFocus();
237 GetDlgItem(IDC_FOLDER)->SendMessage(EM_SETSEL,0,-1);
238 GetDlgItem(IDOK)->EnableWindow(!m_strFolder.IsEmpty());
239 return FALSE; // return TRUE unless you set the focus to a control
240 // EXCEPTION: OCX Property Pages should return FALSE
243 void CFolderDialog::OnCancel()
245 CeCosDialog::OnCancel();
250 void CFolderDialog::OnChangeFolder()
253 GetDlgItemText(IDC_FOLDER,str);
254 GetDlgItem(IDOK)->EnableWindow(!str.IsEmpty());