]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dirdlg.cpp
Added samples.inc to DATAFILES in Makefile.in - is that enough?
[wxWidgets.git] / src / msw / dirdlg.cpp
CommitLineData
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// ----------------------------------------------------------------------------
2bda0e17 19#ifdef __GNUG__
3f2711d5 20 #pragma implementation "dirdlg.h"
2bda0e17
KB
21#endif
22
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
3f2711d5 27 #pragma hdrstop
2bda0e17
KB
28#endif
29
c42404a5 30#if defined(__WIN95__) && !defined(__GNUWIN32_OLD__)
54744d3a 31
2bda0e17 32#ifndef WX_PRECOMP
3f2711d5
VZ
33 #include "wx/utils.h"
34 #include "wx/dialog.h"
35 #include "wx/dirdlg.h"
77902671 36 #include "wx/log.h"
2bda0e17
KB
37#endif
38
3f2711d5
VZ
39#include "wx/msw/private.h"
40
54744d3a 41#include "shlobj.h" // Win95 shell
2bda0e17 42
3f2711d5
VZ
43// ----------------------------------------------------------------------------
44// constants
45// ----------------------------------------------------------------------------
2bda0e17 46
3f2711d5 47#ifndef MAX_PATH
54744d3a 48 #define MAX_PATH 4096 // be generous
3f2711d5
VZ
49#endif
50
51// ----------------------------------------------------------------------------
52// wxWindows macros
53// ----------------------------------------------------------------------------
2bda0e17 54
54744d3a 55IMPLEMENT_CLASS(wxDirDialog, wxDialog)
2bda0e17 56
3f2711d5
VZ
57// ----------------------------------------------------------------------------
58// private functions prototypes
59// ----------------------------------------------------------------------------
60
61// free the parameter
62static void ItemListFree(LPITEMIDLIST pidl);
bfa2e032 63
3f2711d5
VZ
64// the callback proc for the dir dlg
65static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp,
66 LPARAM pData);
bfa2e032 67
54744d3a 68
3f2711d5
VZ
69// ============================================================================
70// implementation
71// ============================================================================
bfa2e032 72
3f2711d5
VZ
73// ----------------------------------------------------------------------------
74// wxDirDialog
75// ----------------------------------------------------------------------------
76
77wxDirDialog::wxDirDialog(wxWindow *parent,
78 const wxString& message,
79 const wxString& defaultPath,
80 long WXUNUSED(style),
81 const wxPoint& WXUNUSED(pos))
2bda0e17
KB
82{
83 m_message = message;
2bda0e17 84 m_parent = parent;
3f2711d5 85 m_path = defaultPath;
5bd3a2da 86 m_path.Replace(_T("/"), _T("\\")); // SHBrowseForFolder doesn't like '/'s
2bda0e17
KB
87}
88
3f2711d5 89int wxDirDialog::ShowModal()
2bda0e17 90{
2bda0e17 91 BROWSEINFO bi;
3f2711d5
VZ
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 )
2bda0e17 108 {
3f2711d5 109 // Cancel button pressed
2bda0e17
KB
110 return wxID_CANCEL;
111 }
112
3f2711d5
VZ
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("SHGetPathFromIDList");
121
2bda0e17 122 return wxID_CANCEL;
2bda0e17 123 }
3f2711d5
VZ
124
125 return wxID_OK;
3f2711d5
VZ
126}
127
128// ----------------------------------------------------------------------------
129// private functions
130// ----------------------------------------------------------------------------
131
132static int CALLBACK
133BrowseCallbackProc(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 SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szDir);
152 }
153 }
154 break;
155
156 //case BFFM_VALIDATEFAILED: -- might be used to provide custom message
157 // if the user types in invalid dir name
158 }
159
160 return 0;
161}
162
163
164static void ItemListFree(LPITEMIDLIST pidl)
165{
166 if ( pidl )
167 {
168 LPMALLOC pMalloc;
169 SHGetMalloc(&pMalloc);
170 if ( pMalloc )
171 {
172 pMalloc->Free(pidl);
173 pMalloc->Release();
174 }
175 else
176 {
177 wxLogLastError("SHGetMalloc");
178 }
179 }
2bda0e17
KB
180}
181
54744d3a
VZ
182#else
183 #include "../generic/dirdlgg.cpp"
184#endif // compiler/platform on which the code here compiles