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