]>
Commit | Line | Data |
---|---|---|
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 | #ifndef WX_PRECOMP | |
31 | #include "wx/utils.h" | |
32 | #include "wx/dialog.h" | |
33 | #include "wx/dirdlg.h" | |
34 | #include "wx/log.h" | |
35 | #endif | |
36 | ||
37 | #include "wx/msw/private.h" | |
38 | ||
39 | #if defined(__WIN95__) && \ | |
40 | (!defined(__GNUWIN32__) || defined(wxUSE_NORLANDER_HEADERS)) | |
41 | #define CAN_COMPILE_DIRDLG | |
42 | //#else: we provide a stub version which doesn't do anything | |
43 | #endif | |
44 | ||
45 | #ifdef CAN_COMPILE_DIRDLG | |
46 | #include "shlobj.h" // Win95 shell | |
47 | #endif | |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // constants | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | #ifndef MAX_PATH | |
54 | #define MAX_PATH 4096 // be generuous | |
55 | #endif | |
56 | ||
57 | // ---------------------------------------------------------------------------- | |
58 | // wxWindows macros | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | #if !USE_SHARED_LIBRARY | |
62 | IMPLEMENT_CLASS(wxDirDialog, wxDialog) | |
63 | #endif | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // private functions prototypes | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | // free the parameter | |
70 | static void ItemListFree(LPITEMIDLIST pidl); | |
71 | ||
72 | // the callback proc for the dir dlg | |
73 | static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, | |
74 | LPARAM pData); | |
75 | ||
76 | // ============================================================================ | |
77 | // implementation | |
78 | // ============================================================================ | |
79 | ||
80 | // ---------------------------------------------------------------------------- | |
81 | // wxDirDialog | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
84 | wxDirDialog::wxDirDialog(wxWindow *parent, | |
85 | const wxString& message, | |
86 | const wxString& defaultPath, | |
87 | long WXUNUSED(style), | |
88 | const wxPoint& WXUNUSED(pos)) | |
89 | { | |
90 | m_message = message; | |
91 | m_parent = parent; | |
92 | m_path = defaultPath; | |
93 | } | |
94 | ||
95 | int wxDirDialog::ShowModal() | |
96 | { | |
97 | #ifdef CAN_COMPILE_DIRDLG | |
98 | BROWSEINFO bi; | |
99 | bi.hwndOwner = m_parent ? GetHwndOf(m_parent) : NULL; | |
100 | bi.pidlRoot = NULL; | |
101 | bi.pszDisplayName = NULL; | |
102 | bi.lpszTitle = m_message.c_str(); | |
103 | bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT; | |
104 | bi.lpfn = BrowseCallbackProc; | |
105 | bi.lParam = (LPARAM)m_path.c_str(); // param for the callback | |
106 | ||
107 | LPITEMIDLIST pidl = SHBrowseForFolder(&bi); | |
108 | ||
109 | if ( bi.pidlRoot ) | |
110 | { | |
111 | ItemListFree((LPITEMIDLIST)bi.pidlRoot); | |
112 | } | |
113 | ||
114 | if ( !pidl ) | |
115 | { | |
116 | // Cancel button pressed | |
117 | return wxID_CANCEL; | |
118 | } | |
119 | ||
120 | BOOL ok = SHGetPathFromIDList(pidl, m_path.GetWriteBuf(MAX_PATH)); | |
121 | m_path.UngetWriteBuf(); | |
122 | ||
123 | ItemListFree(pidl); | |
124 | ||
125 | if ( !ok ) | |
126 | { | |
127 | wxLogLastError("SHGetPathFromIDList"); | |
128 | ||
129 | return wxID_CANCEL; | |
130 | } | |
131 | ||
132 | return wxID_OK; | |
133 | #else // !CAN_COMPILE_DIRDLG | |
134 | return wxID_CANCEL; | |
135 | #endif // CAN_COMPILE_DIRDLG/!CAN_COMPILE_DIRDLG | |
136 | } | |
137 | ||
138 | // ---------------------------------------------------------------------------- | |
139 | // private functions | |
140 | // ---------------------------------------------------------------------------- | |
141 | ||
142 | static int CALLBACK | |
143 | BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) | |
144 | { | |
145 | switch(uMsg) | |
146 | { | |
147 | case BFFM_INITIALIZED: | |
148 | // sent immediately after initialisation and so we may set the | |
149 | // initial selection here | |
150 | // | |
151 | // wParam = TRUE => lParam is a string and not a PIDL | |
152 | SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData); | |
153 | break; | |
154 | ||
155 | case BFFM_SELCHANGED: | |
156 | { | |
157 | // Set the status window to the currently selected path. | |
158 | TCHAR szDir[MAX_PATH]; | |
159 | if ( SHGetPathFromIDList((LPITEMIDLIST)lp, szDir) ) | |
160 | { | |
161 | SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szDir); | |
162 | } | |
163 | } | |
164 | break; | |
165 | ||
166 | //case BFFM_VALIDATEFAILED: -- might be used to provide custom message | |
167 | // if the user types in invalid dir name | |
168 | } | |
169 | ||
170 | return 0; | |
171 | } | |
172 | ||
173 | ||
174 | static void ItemListFree(LPITEMIDLIST pidl) | |
175 | { | |
176 | if ( pidl ) | |
177 | { | |
178 | LPMALLOC pMalloc; | |
179 | SHGetMalloc(&pMalloc); | |
180 | if ( pMalloc ) | |
181 | { | |
182 | pMalloc->Free(pidl); | |
183 | pMalloc->Release(); | |
184 | } | |
185 | else | |
186 | { | |
187 | wxLogLastError("SHGetMalloc"); | |
188 | } | |
189 | } | |
190 | } | |
191 |