| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: dirdlg.cpp |
| 3 | // Purpose: wxDirDialog |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 01/02/97 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | #ifdef __GNUG__ |
| 20 | #pragma implementation "dirdlg.h" |
| 21 | #endif |
| 22 | |
| 23 | // For compilers that support precompilation, includes "wx.h". |
| 24 | #include "wx/wxprec.h" |
| 25 | |
| 26 | #ifdef __BORLANDC__ |
| 27 | #pragma hdrstop |
| 28 | #endif |
| 29 | |
| 30 | #if defined(__WIN95__) && !defined(__GNUWIN32_OLD__) |
| 31 | |
| 32 | #ifndef WX_PRECOMP |
| 33 | #include "wx/utils.h" |
| 34 | #include "wx/dialog.h" |
| 35 | #include "wx/dirdlg.h" |
| 36 | #include "wx/log.h" |
| 37 | #endif |
| 38 | |
| 39 | #include "wx/msw/private.h" |
| 40 | |
| 41 | #include "shlobj.h" // Win95 shell |
| 42 | |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | // constants |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | |
| 47 | #ifndef MAX_PATH |
| 48 | #define MAX_PATH 4096 // be generous |
| 49 | #endif |
| 50 | |
| 51 | // ---------------------------------------------------------------------------- |
| 52 | // wxWindows macros |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | |
| 55 | IMPLEMENT_CLASS(wxDirDialog, wxDialog) |
| 56 | |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | // private functions prototypes |
| 59 | // ---------------------------------------------------------------------------- |
| 60 | |
| 61 | // free the parameter |
| 62 | static void ItemListFree(LPITEMIDLIST pidl); |
| 63 | |
| 64 | // the callback proc for the dir dlg |
| 65 | static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, |
| 66 | LPARAM pData); |
| 67 | |
| 68 | |
| 69 | // ============================================================================ |
| 70 | // implementation |
| 71 | // ============================================================================ |
| 72 | |
| 73 | // ---------------------------------------------------------------------------- |
| 74 | // wxDirDialog |
| 75 | // ---------------------------------------------------------------------------- |
| 76 | |
| 77 | wxDirDialog::wxDirDialog(wxWindow *parent, |
| 78 | const wxString& message, |
| 79 | const wxString& defaultPath, |
| 80 | long WXUNUSED(style), |
| 81 | const wxPoint& WXUNUSED(pos)) |
| 82 | { |
| 83 | m_message = message; |
| 84 | m_parent = parent; |
| 85 | m_path = defaultPath; |
| 86 | m_path.Replace(_T("/"), _T("\\")); // SHBrowseForFolder doesn't like '/'s |
| 87 | } |
| 88 | |
| 89 | int wxDirDialog::ShowModal() |
| 90 | { |
| 91 | BROWSEINFO bi; |
| 92 | bi.hwndOwner = m_parent ? GetHwndOf(m_parent) : NULL; |
| 93 | bi.pidlRoot = NULL; |
| 94 | bi.pszDisplayName = NULL; |
| 95 | bi.lpszTitle = m_message.c_str(); |
| 96 | bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT; |
| 97 | bi.lpfn = BrowseCallbackProc; |
| 98 | bi.lParam = (LPARAM)m_path.c_str(); // param for the callback |
| 99 | |
| 100 | LPITEMIDLIST pidl = SHBrowseForFolder(&bi); |
| 101 | |
| 102 | if ( bi.pidlRoot ) |
| 103 | { |
| 104 | ItemListFree((LPITEMIDLIST)bi.pidlRoot); |
| 105 | } |
| 106 | |
| 107 | if ( !pidl ) |
| 108 | { |
| 109 | // Cancel button pressed |
| 110 | return wxID_CANCEL; |
| 111 | } |
| 112 | |
| 113 | BOOL ok = SHGetPathFromIDList(pidl, m_path.GetWriteBuf(MAX_PATH)); |
| 114 | m_path.UngetWriteBuf(); |
| 115 | |
| 116 | ItemListFree(pidl); |
| 117 | |
| 118 | if ( !ok ) |
| 119 | { |
| 120 | wxLogLastError(wxT("SHGetPathFromIDList")); |
| 121 | |
| 122 | return wxID_CANCEL; |
| 123 | } |
| 124 | |
| 125 | return wxID_OK; |
| 126 | } |
| 127 | |
| 128 | // ---------------------------------------------------------------------------- |
| 129 | // private functions |
| 130 | // ---------------------------------------------------------------------------- |
| 131 | |
| 132 | static int CALLBACK |
| 133 | BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) |
| 134 | { |
| 135 | switch(uMsg) |
| 136 | { |
| 137 | case BFFM_INITIALIZED: |
| 138 | // sent immediately after initialisation and so we may set the |
| 139 | // initial selection here |
| 140 | // |
| 141 | // wParam = TRUE => lParam is a string and not a PIDL |
| 142 | SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData); |
| 143 | break; |
| 144 | |
| 145 | case BFFM_SELCHANGED: |
| 146 | { |
| 147 | // Set the status window to the currently selected path. |
| 148 | TCHAR szDir[MAX_PATH]; |
| 149 | if ( SHGetPathFromIDList((LPITEMIDLIST)lp, szDir) ) |
| 150 | { |
| 151 | SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szDir); |
| 152 | } |
| 153 | } |
| 154 | break; |
| 155 | |
| 156 | //case BFFM_VALIDATEFAILED: -- might be used to provide custom message |
| 157 | // if the user types in invalid dir name |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | static void ItemListFree(LPITEMIDLIST pidl) |
| 165 | { |
| 166 | if ( pidl ) |
| 167 | { |
| 168 | LPMALLOC pMalloc; |
| 169 | SHGetMalloc(&pMalloc); |
| 170 | if ( pMalloc ) |
| 171 | { |
| 172 | pMalloc->Free(pidl); |
| 173 | pMalloc->Release(); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | wxLogLastError(wxT("SHGetMalloc")); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | #else |
| 183 | #include "../generic/dirdlgg.cpp" |
| 184 | #endif // compiler/platform on which the code here compiles |