]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dirdlg.cpp
corrected eventual endless loop
[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 #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 wxString strDir(szDir);
152 int maxChars = 40; // Have to truncate string else it displays incorrectly
153 if (strDir.Len() > (size_t) (maxChars - 3))
154 {
155 strDir = strDir.Right(maxChars - 3);
156 strDir = wxString(wxT("...")) + strDir;
157 }
158 SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM) (const wxChar*) strDir);
159 }
160 }
161 break;
162
163 //case BFFM_VALIDATEFAILED: -- might be used to provide custom message
164 // if the user types in invalid dir name
165 }
166
167 return 0;
168 }
169
170
171 static void ItemListFree(LPITEMIDLIST pidl)
172 {
173 if ( pidl )
174 {
175 LPMALLOC pMalloc;
176 SHGetMalloc(&pMalloc);
177 if ( pMalloc )
178 {
179 pMalloc->Free(pidl);
180 pMalloc->Release();
181 }
182 else
183 {
184 wxLogLastError(wxT("SHGetMalloc"));
185 }
186 }
187 }
188
189 #else
190 #include "../generic/dirdlgg.cpp"
191 #endif // compiler/platform on which the code here compiles