]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dirdlg.cpp
fixed paper size calculation (convert from 10ths of mm to mm correctly)
[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// ----------------------------------------------------------------------------
1e6feb95 19
2bda0e17 20#ifdef __GNUG__
3f2711d5 21 #pragma implementation "dirdlg.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
3f2711d5 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_DIRDLG
32
7fc0bd1c 33#if defined(__WIN95__) && !defined(__GNUWIN32_OLD__) && wxUSE_OLE
54744d3a 34
2bda0e17 35#ifndef WX_PRECOMP
3f2711d5
VZ
36 #include "wx/utils.h"
37 #include "wx/dialog.h"
38 #include "wx/dirdlg.h"
77902671 39 #include "wx/log.h"
2bda0e17
KB
40#endif
41
3f2711d5
VZ
42#include "wx/msw/private.h"
43
e7b0dcd9 44#include <shlobj.h> // Win95 shell
2bda0e17 45
3f2711d5
VZ
46// ----------------------------------------------------------------------------
47// constants
48// ----------------------------------------------------------------------------
2bda0e17 49
3f2711d5 50#ifndef MAX_PATH
54744d3a 51 #define MAX_PATH 4096 // be generous
3f2711d5
VZ
52#endif
53
54// ----------------------------------------------------------------------------
55// wxWindows macros
56// ----------------------------------------------------------------------------
2bda0e17 57
54744d3a 58IMPLEMENT_CLASS(wxDirDialog, wxDialog)
2bda0e17 59
3f2711d5
VZ
60// ----------------------------------------------------------------------------
61// private functions prototypes
62// ----------------------------------------------------------------------------
63
64// free the parameter
65static void ItemListFree(LPITEMIDLIST pidl);
bfa2e032 66
3f2711d5
VZ
67// the callback proc for the dir dlg
68static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp,
69 LPARAM pData);
bfa2e032 70
54744d3a 71
3f2711d5
VZ
72// ============================================================================
73// implementation
74// ============================================================================
bfa2e032 75
3f2711d5
VZ
76// ----------------------------------------------------------------------------
77// wxDirDialog
78// ----------------------------------------------------------------------------
79
80wxDirDialog::wxDirDialog(wxWindow *parent,
81 const wxString& message,
82 const wxString& defaultPath,
83 long WXUNUSED(style),
84 const wxPoint& WXUNUSED(pos))
2bda0e17
KB
85{
86 m_message = message;
2bda0e17 87 m_parent = parent;
3f2711d5 88 m_path = defaultPath;
e7b0dcd9
VZ
89
90 // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes
91 m_path.Replace(_T("/"), _T("\\"));
92 if ( *m_path.end() == _T('\\') )
93 {
94 m_path.erase(m_path.length() - 1);
95 }
2bda0e17
KB
96}
97
3f2711d5 98int wxDirDialog::ShowModal()
2bda0e17 99{
2bda0e17 100 BROWSEINFO bi;
3f2711d5
VZ
101 bi.hwndOwner = m_parent ? GetHwndOf(m_parent) : NULL;
102 bi.pidlRoot = NULL;
103 bi.pszDisplayName = NULL;
104 bi.lpszTitle = m_message.c_str();
105 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
106 bi.lpfn = BrowseCallbackProc;
107 bi.lParam = (LPARAM)m_path.c_str(); // param for the callback
108
109 LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
110
111 if ( bi.pidlRoot )
112 {
113 ItemListFree((LPITEMIDLIST)bi.pidlRoot);
114 }
115
116 if ( !pidl )
2bda0e17 117 {
3f2711d5 118 // Cancel button pressed
2bda0e17
KB
119 return wxID_CANCEL;
120 }
121
3f2711d5
VZ
122 BOOL ok = SHGetPathFromIDList(pidl, m_path.GetWriteBuf(MAX_PATH));
123 m_path.UngetWriteBuf();
124
125 ItemListFree(pidl);
126
127 if ( !ok )
128 {
f6bcfd97 129 wxLogLastError(wxT("SHGetPathFromIDList"));
3f2711d5 130
2bda0e17 131 return wxID_CANCEL;
2bda0e17 132 }
3f2711d5
VZ
133
134 return wxID_OK;
3f2711d5
VZ
135}
136
137// ----------------------------------------------------------------------------
138// private functions
139// ----------------------------------------------------------------------------
140
141static int CALLBACK
142BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
143{
144 switch(uMsg)
145 {
146 case BFFM_INITIALIZED:
147 // sent immediately after initialisation and so we may set the
148 // initial selection here
149 //
150 // wParam = TRUE => lParam is a string and not a PIDL
151 SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
152 break;
153
154 case BFFM_SELCHANGED:
155 {
156 // Set the status window to the currently selected path.
157 TCHAR szDir[MAX_PATH];
158 if ( SHGetPathFromIDList((LPITEMIDLIST)lp, szDir) )
159 {
6a097cd6
JS
160 wxString strDir(szDir);
161 int maxChars = 40; // Have to truncate string else it displays incorrectly
162 if (strDir.Len() > (size_t) (maxChars - 3))
163 {
164 strDir = strDir.Right(maxChars - 3);
165 strDir = wxString(wxT("...")) + strDir;
166 }
167 SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM) (const wxChar*) strDir);
3f2711d5
VZ
168 }
169 }
170 break;
171
172 //case BFFM_VALIDATEFAILED: -- might be used to provide custom message
173 // if the user types in invalid dir name
174 }
175
176 return 0;
177}
178
179
180static void ItemListFree(LPITEMIDLIST pidl)
181{
182 if ( pidl )
183 {
184 LPMALLOC pMalloc;
185 SHGetMalloc(&pMalloc);
186 if ( pMalloc )
187 {
188 pMalloc->Free(pidl);
189 pMalloc->Release();
190 }
191 else
192 {
f6bcfd97 193 wxLogLastError(wxT("SHGetMalloc"));
3f2711d5
VZ
194 }
195 }
2bda0e17
KB
196}
197
54744d3a
VZ
198#else
199 #include "../generic/dirdlgg.cpp"
200#endif // compiler/platform on which the code here compiles
1e6feb95
VZ
201
202#endif // wxUSE_DIRDLG