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