]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dirdlg.cpp
corrected use of Print Manager Session APIs for Carbon targets
[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 {
f6bcfd97 120 wxLogLastError(wxT("SHGetPathFromIDList"));
3f2711d5 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 {
6a097cd6
JS
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);
3f2711d5
VZ
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
171static 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 {
f6bcfd97 184 wxLogLastError(wxT("SHGetMalloc"));
3f2711d5
VZ
185 }
186 }
2bda0e17
KB
187}
188
54744d3a
VZ
189#else
190 #include "../generic/dirdlgg.cpp"
191#endif // compiler/platform on which the code here compiles