]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dirdlg.cpp
patch from Dimitri fixing a few memory leaks and unTABbing the sources
[wxWidgets.git] / src / msw / dirdlg.cpp
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
20 #ifdef __GNUG__
21 #pragma implementation "dirdlg.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/setup.h"
32
33 #if wxUSE_DIRDLG
34
35 #if defined(__WIN95__) && !defined(__GNUWIN32_OLD__) && wxUSE_OLE
36
37 #ifndef WX_PRECOMP
38 #include "wx/utils.h"
39 #include "wx/dialog.h"
40 #include "wx/dirdlg.h"
41 #include "wx/log.h"
42 #endif
43
44 #include "wx/msw/private.h"
45
46 #include "shlobj.h" // Win95 shell
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 #ifndef MAX_PATH
53 #define MAX_PATH 4096 // be generous
54 #endif
55
56 // ----------------------------------------------------------------------------
57 // wxWindows macros
58 // ----------------------------------------------------------------------------
59
60 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
61
62 // ----------------------------------------------------------------------------
63 // private functions prototypes
64 // ----------------------------------------------------------------------------
65
66 // free the parameter
67 static void ItemListFree(LPITEMIDLIST pidl);
68
69 // the callback proc for the dir dlg
70 static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp,
71 LPARAM pData);
72
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
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))
87 {
88 m_message = message;
89 m_parent = parent;
90 m_path = defaultPath;
91 m_path.Replace(_T("/"), _T("\\")); // SHBrowseForFolder doesn't like '/'s
92 }
93
94 int wxDirDialog::ShowModal()
95 {
96 BROWSEINFO bi;
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 )
113 {
114 // Cancel button pressed
115 return wxID_CANCEL;
116 }
117
118 BOOL ok = SHGetPathFromIDList(pidl, m_path.GetWriteBuf(MAX_PATH));
119 m_path.UngetWriteBuf();
120
121 ItemListFree(pidl);
122
123 if ( !ok )
124 {
125 wxLogLastError(wxT("SHGetPathFromIDList"));
126
127 return wxID_CANCEL;
128 }
129
130 return wxID_OK;
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 {
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);
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 {
189 wxLogLastError(wxT("SHGetMalloc"));
190 }
191 }
192 }
193
194 #else
195 #include "../generic/dirdlgg.cpp"
196 #endif // compiler/platform on which the code here compiles
197
198 #endif // wxUSE_DIRDLG