X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/6627a4b0497f89221ea9f7848990f8830cf81b7e..b404a8f3b072129c107c6d9a5e0f6f53cd34807b:/src/msw/dirdlg.cpp diff --git a/src/msw/dirdlg.cpp b/src/msw/dirdlg.cpp index 5434b5d0d7..9d3b472058 100644 --- a/src/msw/dirdlg.cpp +++ b/src/msw/dirdlg.cpp @@ -1,159 +1,249 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: dirdlg.cpp +// Name: src/msw/dirdlg.cpp // Purpose: wxDirDialog // Author: Julian Smart // Modified by: // Created: 01/02/97 // RCS-ID: $Id$ -// Copyright: (c) Julian Smart and Markus Holzem -// Licence: wxWindows licence +// Copyright: (c) Julian Smart +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation "dirdlg.h" -#endif +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif -#ifndef WX_PRECOMP -#include -#include "wx/defs.h" -#include "wx/utils.h" -#include "wx/dialog.h" +#if wxUSE_DIRDLG + +#if wxUSE_OLE && !defined(__GNUWIN32_OLD__) && (!defined(__WXWINCE__) || \ + (defined(__HANDHELDPC__) && (_WIN32_WCE >= 500))) + #include "wx/dirdlg.h" -#endif -#if (defined(__WIN95__) && !defined(__GNUWIN32__))||defined(wxUSE_NORLANDER_HEADERS) -#include "shlobj.h" // Win95 shell +#ifndef WX_PRECOMP + #include "wx/utils.h" + #include "wx/dialog.h" + #include "wx/log.h" + #include "wx/app.h" // for GetComCtl32Version() #endif #include "wx/msw/private.h" -#include "wx/cmndata.h" +#include "wx/msw/wrapshl.h" -#include -#include -#include +// ---------------------------------------------------------------------------- +// constants +// ---------------------------------------------------------------------------- -#define wxDIALOG_DEFAULT_X 300 -#define wxDIALOG_DEFAULT_Y 300 +#ifndef BIF_NEWDIALOGSTYLE + #define BIF_NEWDIALOGSTYLE 0x0040 +#endif -#if !USE_SHARED_LIBRARY -IMPLEMENT_CLASS(wxDirDialog, wxDialog) +#ifndef BIF_NONEWFOLDERBUTTON + #define BIF_NONEWFOLDERBUTTON 0x0200 #endif -static int CALLBACK -BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData) - { - TCHAR szDir[MAX_PATH]; - - switch(uMsg) - { - case BFFM_INITIALIZED: - // We have put m_path into pData. - // TRUE -> passing char *, not dir id. - SendMessage(hwnd,BFFM_SETSELECTION,TRUE,pData); - break; - - case BFFM_SELCHANGED: - // Set the status window to the currently selected path. - if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szDir)) - { - SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir); - } - break; +#ifndef BIF_EDITBOX + #define BIF_EDITBOX 16 +#endif + +// ---------------------------------------------------------------------------- +// wxWidgets macros +// ---------------------------------------------------------------------------- + +IMPLEMENT_CLASS(wxDirDialog, wxDialog) + +// ---------------------------------------------------------------------------- +// private functions prototypes +// ---------------------------------------------------------------------------- + +// the callback proc for the dir dlg +static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, + LPARAM pData); + - default: - break; - } - return 0; - } +// ============================================================================ +// implementation +// ============================================================================ +// ---------------------------------------------------------------------------- +// wxDirDialog +// ---------------------------------------------------------------------------- -wxDirDialog::wxDirDialog(wxWindow *parent, const wxString& message, -// const wxString& caption, - const wxString& defaultPath, - long style, const wxPoint& pos) +wxDirDialog::wxDirDialog(wxWindow *parent, + const wxString& message, + const wxString& defaultPath, + long style, + const wxPoint& WXUNUSED(pos), + const wxSize& WXUNUSED(size), + const wxString& WXUNUSED(name)) { m_message = message; -// m_caption = caption; - m_dialogStyle = style; m_parent = parent; - m_path = defaultPath; + + SetWindowStyle(style); + SetPath(defaultPath); } -int wxDirDialog::ShowModal(void) +void wxDirDialog::SetPath(const wxString& path) { - // Unfortunately Gnu-Win32 doesn't yet have COM support -#if (defined(__WIN95__) && !defined(__GNUWIN32__))||defined(wxUSE_NORLANDER_HEADERS) - HWND hWnd = 0; - if (m_parent) hWnd = (HWND) m_parent->GetHWND(); + m_path = path; - BROWSEINFO bi; - LPTSTR lpBuffer; -// LPITEMIDLIST pidlPrograms; // PIDL for Programs folder - LPITEMIDLIST pidlBrowse; // PIDL selected by user - LPMALLOC pMalloc = NULL; - - HRESULT result = ::SHGetMalloc(&pMalloc); - - if (result != NOERROR) - return wxID_CANCEL; - - // Allocate a buffer to receive browse information. - if ((lpBuffer = (LPTSTR) pMalloc->Alloc(MAX_PATH)) == NULL) + // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes + m_path.Replace(wxT("/"), wxT("\\")); + + while ( !m_path.empty() && (*(m_path.end() - 1) == wxT('\\')) ) { - pMalloc->Release(); - return wxID_CANCEL; + m_path.erase(m_path.length() - 1); } -/* - // Get the PIDL for the Programs folder. - if (!SUCCEEDED(SHGetSpecialFolderLocation( - parent->GetSafeHwnd(), CSIDL_PROGRAMS, &pidlPrograms))) { - pMalloc->Free(lpBuffer); - pMalloc->Release(); - return wxID_CANCEL; - } -*/ - - // Fill in the BROWSEINFO structure. - bi.hwndOwner = hWnd; - bi.pidlRoot = NULL; // pidlPrograms; - bi.pszDisplayName = lpBuffer; - bi.lpszTitle = m_message; // BC++ 4.52 says LPSTR, not LPTSTR? - bi.ulFlags = 0; - bi.lpfn = BrowseCallbackProc; - bi.lParam = (LPARAM)m_path.c_str(); - - // Browse for a folder and return its PIDL. - pidlBrowse = SHBrowseForFolder(&bi); - - int id = wxID_OK; - if (pidlBrowse != NULL) { - - // Show the display name, title, and file system path. - if (SHGetPathFromIDList(pidlBrowse, lpBuffer)) - m_path = lpBuffer; - - // Free the PIDL returned by SHBrowseForFolder. - pMalloc->Free(pidlBrowse); + // but the root drive should have a trailing slash (again, this is just + // the way the native dialog works) + if ( !m_path.empty() && (*(m_path.end() - 1) == wxT(':')) ) + { + m_path += wxT('\\'); } - else - id = wxID_CANCEL; - - // Clean up. -// pMalloc->Free(pidlPrograms); - pMalloc->Free(lpBuffer); - pMalloc->Release(); - - return id; +} + +int wxDirDialog::ShowModal() +{ + wxWindow *parent = GetParent(); + + BROWSEINFO bi; + bi.hwndOwner = parent ? GetHwndOf(parent) : NULL; + bi.pidlRoot = NULL; + bi.pszDisplayName = NULL; + // Please don't change this without checking it compiles + // with eVC++ first. +#if defined(__POCKETPC__) || defined(__SMARTPHONE__) + bi.lpszTitle = m_message.mb_str(); #else - return wxID_CANCEL; + bi.lpszTitle = m_message.c_str(); #endif + bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT; + bi.lpfn = BrowseCallbackProc; + bi.lParam = (LPARAM)m_path.wx_str(); // param for the callback + + static const int verComCtl32 = wxApp::GetComCtl32Version(); + + // we always add the edit box (it doesn't hurt anybody, does it?) if it is + // supported by the system + if ( verComCtl32 >= 471 ) + { + bi.ulFlags |= BIF_EDITBOX; + } + + // to have the "New Folder" button we must use the "new" dialog style which + // is also the only way to have a resizable dialog + // + // "new" style is only available in the version 5.0+ of comctl32.dll + const bool needNewDir = !HasFlag(wxDD_DIR_MUST_EXIST); + if ( (needNewDir || HasFlag(wxRESIZE_BORDER)) && (verComCtl32 >= 500) ) + { + if (needNewDir) + { + bi.ulFlags |= BIF_NEWDIALOGSTYLE; + } + else + { + // Versions < 600 doesn't support BIF_NONEWFOLDERBUTTON + // The only way to get rid of the Make New Folder button is use + // the old dialog style which doesn't have the button thus we + // simply don't set the New Dialog Style for such comctl versions. + if (verComCtl32 >= 600) + { + bi.ulFlags |= BIF_NEWDIALOGSTYLE; + bi.ulFlags |= BIF_NONEWFOLDERBUTTON; + } + } + } + + // do show the dialog + wxItemIdList pidl(SHBrowseForFolder(&bi)); + + wxItemIdList::Free((LPITEMIDLIST)bi.pidlRoot); + + if ( !pidl ) + { + // Cancel button pressed + return wxID_CANCEL; + } + + m_path = pidl.GetPath(); + + // change current working directory if asked so + if (HasFlag(wxDD_CHANGE_DIR)) + wxSetWorkingDirectory(m_path); + + return m_path.empty() ? wxID_CANCEL : wxID_OK; } +// ---------------------------------------------------------------------------- +// private functions +// ---------------------------------------------------------------------------- + +static int CALLBACK +BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) +{ + switch(uMsg) + { +#ifdef BFFM_SETSELECTION + case BFFM_INITIALIZED: + // sent immediately after initialisation and so we may set the + // initial selection here + // + // wParam = TRUE => lParam is a string and not a PIDL + ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData); + break; +#endif // BFFM_SETSELECTION + + + case BFFM_SELCHANGED: + // note that this doesn't work with the new style UI (MSDN doesn't + // say anything about it, but the comments in shlobj.h do!) but we + // still execute this code in case it starts working again with the + // "new new UI" (or would it be "NewUIEx" according to tradition?) + { + // Set the status window to the currently selected path. + wxString strDir; + if ( SHGetPathFromIDList((LPITEMIDLIST)lp, + wxStringBuffer(strDir, MAX_PATH)) ) + { + // NB: this shouldn't be necessary with the new style box + // (which is resizable), but as for now it doesn't work + // anyhow (see the comment above) no harm in doing it + + // need to truncate or it displays incorrectly + static const size_t maxChars = 37; + if ( strDir.length() > maxChars ) + { + strDir = strDir.Right(maxChars); + strDir = wxString(wxT("...")) + strDir; + } + + SendMessage(hwnd, BFFM_SETSTATUSTEXT, + 0, (LPARAM)strDir.wx_str()); + } + } + break; + + //case BFFM_VALIDATEFAILED: -- might be used to provide custom message + // if the user types in invalid dir name + } + + return 0; +} + +#endif // compiler/platform on which the code here compiles + +#endif // wxUSE_DIRDLG