Qt 4.8
Typedefs | Functions | Variables
qfiledialog_win.cpp File Reference
#include "qfiledialog.h"
#include <private/qfiledialog_p.h>
#include <qapplication.h>
#include <private/qapplication_p.h>
#include <qt_windows.h>
#include <qglobal.h>
#include <qregexp.h>
#include <qbuffer.h>
#include <qdir.h>
#include <qstringlist.h>
#include <private/qsystemlibrary_p.h>
#include "qfiledialog_win_p.h"
#include <commdlg.h>

Go to the source code of this file.

Typedefs

typedef qt_LPITEMIDLIST(WINAPI * PtrSHBrowseForFolder) (qt_BROWSEINFO *)
 
typedef HRESULT(WINAPI * PtrSHGetMalloc) (LPMALLOC *)
 
typedef BOOL(WINAPI * PtrSHGetPathFromIDList) (qt_LPITEMIDLIST, LPWSTR)
 

Functions

QStringList qt_make_filter_list (const QString &filter)
 
static void qt_win_clean_up_OFN (OPENFILENAME **ofn)
 
void qt_win_eatMouseMove ()
 
static QString qt_win_extract_filter (const QString &rawFilter)
 
static QString qt_win_filter (const QString &filter, bool hideFiltersDetails)
 
QString qt_win_get_existing_directory (const QFileDialogArgs &args)
 
QString qt_win_get_open_file_name (const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter)
 
QStringList qt_win_get_open_file_names (const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter)
 
QString qt_win_get_save_file_name (const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter)
 
static QStringList qt_win_make_filters_list (const QString &filter)
 
static OPENFILENAME * qt_win_make_OFN (QWidget *parent, const QString &initialSelection, const QString &initialDirectory, const QString &title, const QString &filters, QFileDialog::FileMode mode, QFileDialog::Options options)
 
static void qt_win_resolve_libs ()
 
static QString qt_win_selected_filter (const QString &filter, DWORD idx)
 
static int __stdcall winGetExistDirCallbackProc (HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
 

Variables

const int maxMultiLen = 65535
 
const int maxNameLen = 1023
 
static PtrSHBrowseForFolder ptrSHBrowseForFolder = 0
 
static PtrSHGetMalloc ptrSHGetMalloc = 0
 
static PtrSHGetPathFromIDList ptrSHGetPathFromIDList = 0
 
const char * qt_file_dialog_filter_reg_exp
 
bool qt_priv_ptr_valid = false
 
static QString tFilters
 
static QString tInitDir
 
static QString tTitle
 

Typedef Documentation

◆ PtrSHBrowseForFolder

typedef qt_LPITEMIDLIST(WINAPI * PtrSHBrowseForFolder) (qt_BROWSEINFO *)

Definition at line 69 of file qfiledialog_win.cpp.

◆ PtrSHGetMalloc

typedef HRESULT(WINAPI * PtrSHGetMalloc) (LPMALLOC *)

Definition at line 73 of file qfiledialog_win.cpp.

◆ PtrSHGetPathFromIDList

typedef BOOL(WINAPI * PtrSHGetPathFromIDList) (qt_LPITEMIDLIST, LPWSTR)

Definition at line 71 of file qfiledialog_win.cpp.

Function Documentation

◆ qt_make_filter_list()

QStringList qt_make_filter_list ( const QString filter)

Definition at line 1075 of file qfiledialog.cpp.

Referenced by qt_win_make_filters_list(), and qt_win_resolve_libs().

1076 {
1077  QString f(filter);
1078 
1079  if (f.isEmpty())
1080  return QStringList();
1081 
1082  QString sep(QLatin1String(";;"));
1083  int i = f.indexOf(sep, 0);
1084  if (i == -1) {
1085  if (f.indexOf(QLatin1Char('\n'), 0) != -1) {
1086  sep = QLatin1Char('\n');
1087  i = f.indexOf(sep, 0);
1088  }
1089  }
1090 
1091  return f.split(sep);
1092 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qt_win_clean_up_OFN()

static void qt_win_clean_up_OFN ( OPENFILENAME **  ofn)
static

Definition at line 222 of file qfiledialog_win.cpp.

Referenced by qt_win_get_open_file_name(), qt_win_get_open_file_names(), and qt_win_get_save_file_name().

223 {
224  delete [] (*ofn)->lpstrFile;
225  delete *ofn;
226  *ofn = 0;
227 }

◆ qt_win_eatMouseMove()

void qt_win_eatMouseMove ( )

Definition at line 3016 of file qapplication_win.cpp.

Referenced by qt_win_clean_up_OFN(), qt_win_get_existing_directory(), qt_win_get_open_file_name(), qt_win_get_open_file_names(), and qt_win_get_save_file_name().

3017 {
3018  // after closing a windows dialog with a double click (i.e. open a file)
3019  // the message queue still contains a dubious WM_MOUSEMOVE message where
3020  // the left button is reported to be down (wParam != 0).
3021  // remove all those messages (usually 1) and post the last one with a
3022  // reset button state
3023 
3024  MSG msg = {0, 0, 0, 0, 0, {0, 0} };
3025  while (PeekMessage(&msg, 0, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE))
3026  ;
3027  if (msg.message == WM_MOUSEMOVE)
3028  PostMessage(msg.hwnd, msg.message, 0, msg.lParam);
3029 }
struct tagMSG MSG

◆ qt_win_extract_filter()

static QString qt_win_extract_filter ( const QString rawFilter)
static

Definition at line 109 of file qfiledialog_win.cpp.

Referenced by qt_win_filter(), and qt_win_get_save_file_name().

110 {
111  QString result = rawFilter;
113  int index = r.indexIn(result);
114  if (index >= 0)
115  result = r.cap(2);
116  QStringList list = result.split(QLatin1Char(' '));
117  for(QStringList::iterator it = list.begin(); it < list.end(); ++it) {
118  if (*it == QLatin1String("*")) {
119  *it = QLatin1String("*.*");
120  break;
121  }
122  }
123  return list.join(QLatin1String(";"));
124 }
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
#define it(className, varName)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
friend class iterator
Definition: qlist.h:226
const char * qt_file_dialog_filter_reg_exp
QString join(const QString &sep) const
Joins all the string list&#39;s strings into a single string with each element separated by the given sep...
Definition: qstringlist.h:162
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
quint16 index
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qt_win_filter()

static QString qt_win_filter ( const QString filter,
bool  hideFiltersDetails 
)
static

Definition at line 137 of file qfiledialog_win.cpp.

Referenced by qt_win_get_open_file_name(), qt_win_get_open_file_names(), and qt_win_get_save_file_name().

138 {
139  QStringList filterLst = qt_win_make_filters_list(filter);
140  QStringList::Iterator it = filterLst.begin();
141  QString winfilters;
143  for (; it != filterLst.end(); ++it) {
144  QString subfilter = *it;
145  if (!subfilter.isEmpty()) {
146  if (hideFiltersDetails) {
147  int index = r.indexIn(subfilter);
148  if (index >= 0)
149  winfilters += r.cap(1);
150  } else {
151  winfilters += subfilter;
152  }
153  winfilters += QChar();
154  winfilters += qt_win_extract_filter(subfilter);
155  winfilters += QChar();
156  }
157  }
158  winfilters += QChar();
159  return winfilters;
160 }
static QStringList qt_win_make_filters_list(const QString &filter)
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
#define it(className, varName)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
iterator Iterator
Qt-style synonym for QList::iterator.
Definition: qlist.h:278
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
static QString qt_win_extract_filter(const QString &rawFilter)
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
const char * qt_file_dialog_filter_reg_exp
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
quint16 index

◆ qt_win_get_existing_directory()

QString qt_win_get_existing_directory ( const QFileDialogArgs args)

Definition at line 741 of file qfiledialog_win.cpp.

Referenced by QFileDialog::getExistingDirectory(), and QFileDialog::labelText().

742 {
743 #ifndef Q_WS_WINCE
745  return qt_win_CID_get_existing_directory(args);
746 #endif
747 
748  QString currentDir = QDir::currentPath();
749  QString result;
750  QWidget *parent = args.parent;
751  if (parent)
752  parent = parent->window();
753  else
754  parent = QApplication::activeWindow();
755  if (parent)
756  parent->createWinId();
757 
758  QDialog modal_widget;
759  modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true);
760  modal_widget.setParent(parent, Qt::Window);
761  QApplicationPrivate::enterModal(&modal_widget);
762 
764  wchar_t path[MAX_PATH];
765  wchar_t initPath[MAX_PATH];
766  initPath[0] = 0;
767  path[0] = 0;
768  tTitle = args.caption;
769 
770  qt_BROWSEINFO bi;
771 
772  Q_ASSERT(!parent ||parent->testAttribute(Qt::WA_WState_Created));
773  bi.hwndOwner = (parent ? parent->winId() : 0);
774  bi.pidlRoot = NULL;
775  //### This does not seem to be respected? - the dialog always displays "Browse for folder"
776  bi.lpszTitle = (wchar_t*)tTitle.utf16();
777  bi.pszDisplayName = initPath;
780  bi.lParam = LPARAM(&initDir);
781 
783  if (ptrSHBrowseForFolder) {
784  qt_LPITEMIDLIST pItemIDList = ptrSHBrowseForFolder(&bi);
785  if (pItemIDList) {
786  ptrSHGetPathFromIDList(pItemIDList, path);
787  IMalloc *pMalloc;
788  if (ptrSHGetMalloc(&pMalloc) == NOERROR) {
789  pMalloc->Free(pItemIDList);
790  pMalloc->Release();
791  result = QString::fromWCharArray(path);
792  }
793  }
794  }
795  tTitle = QString();
796 
797  QApplicationPrivate::leaveModal(&modal_widget);
798 
800 
801  if (!result.isEmpty())
802  result.replace(QLatin1Char('\\'), QLatin1Char('/'));
803  return result;
804 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
The QDialog class is the base class of dialog windows.
Definition: qdialog.h:56
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
static QWidget * activeWindow()
Returns the application top-level window that has the keyboard input focus, or 0 if no application wi...
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
#define BIF_NEWDIALOGSTYLE
void qt_win_eatMouseMove()
static const WinVersion WindowsVersion
the version of the Windows operating system on which the application is run (Windows only) ...
Definition: qglobal.h:1613
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString currentPath()
Returns the absolute path of the application&#39;s current directory.
Definition: qdir.cpp:1875
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
BFFCALLBACK lpfn
static QString tTitle
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static void qt_win_resolve_libs()
#define BIF_STATUSTEXT
static int __stdcall winGetExistDirCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
QWidget * parent
static PtrSHGetMalloc ptrSHGetMalloc
qt_LPCITEMIDLIST pidlRoot
static PtrSHBrowseForFolder ptrSHBrowseForFolder
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
void createWinId()
Definition: qwidget.cpp:2626
WId winId() const
Returns the window system identifier of the widget.
Definition: qwidget.cpp:2557
static PtrSHGetPathFromIDList ptrSHGetPathFromIDList
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812
#define BIF_RETURNONLYFSDIRS
static void leaveModal(QWidget *)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static void enterModal(QWidget *)
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290

◆ qt_win_get_open_file_name()

QString qt_win_get_open_file_name ( const QFileDialogArgs args,
QString initialDirectory,
QString selectedFilter 
)

Definition at line 231 of file qfiledialog_win.cpp.

Referenced by QFileDialog::getOpenFileName(), and QFileDialog::labelText().

234 {
235  QString result;
236 
237  QString isel = args.selection;
238 
239  if (initialDirectory && initialDirectory->left(5) == QLatin1String("file:"))
240  initialDirectory->remove(0, 5);
241  QFileInfo fi(*initialDirectory);
242 
243  if (initialDirectory && !fi.isDir()) {
244  *initialDirectory = fi.absolutePath();
245  if (isel.isEmpty())
246  isel = fi.fileName();
247  }
248 
249  if (!fi.exists())
250  *initialDirectory = QDir::homePath();
251 
252  DWORD selFilIdx = 0;
253 
254  int idx = 0;
255  if (selectedFilter) {
256  QStringList filterLst = qt_win_make_filters_list(args.filter);
257  idx = filterLst.indexOf(*selectedFilter);
258  }
259 
260  QDialog modal_widget;
261  modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true);
262  modal_widget.setParent(args.parent, Qt::Window);
263  QApplicationPrivate::enterModal(&modal_widget);
264 
265  bool hideFiltersDetails = args.options & QFileDialog::HideNameFilterDetails;
266  OPENFILENAME* ofn = qt_win_make_OFN(args.parent, args.selection,
267  args.directory, args.caption,
268  qt_win_filter(args.filter, hideFiltersDetails),
270  args.options);
271  if (idx)
272  ofn->nFilterIndex = idx + 1;
273  if (GetOpenFileName(ofn)) {
274  result = QString::fromWCharArray(ofn->lpstrFile);
275  selFilIdx = ofn->nFilterIndex;
276  }
277  qt_win_clean_up_OFN(&ofn);
278 
279  QApplicationPrivate::leaveModal(&modal_widget);
280 
282 
283  if (result.isEmpty())
284  return result;
285 
286  fi = result;
287  *initialDirectory = fi.path();
288  if (selectedFilter)
289  *selectedFilter = qt_win_selected_filter(args.filter, selFilIdx);
290  return fi.absoluteFilePath();
291 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
static QStringList qt_win_make_filters_list(const QString &filter)
static OPENFILENAME * qt_win_make_OFN(QWidget *parent, const QString &initialSelection, const QString &initialDirectory, const QString &title, const QString &filters, QFileDialog::FileMode mode, QFileDialog::Options options)
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
The QDialog class is the base class of dialog windows.
Definition: qdialog.h:56
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void qt_win_eatMouseMove()
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString qt_win_filter(const QString &filter, bool hideFiltersDetails)
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QWidget * parent
QFileDialog::Options options
static void qt_win_clean_up_OFN(OPENFILENAME **ofn)
static QString qt_win_selected_filter(const QString &filter, DWORD idx)
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static void leaveModal(QWidget *)
static void enterModal(QWidget *)
static QString homePath()
Returns the absolute path of the user&#39;s home directory.
Definition: qdir.cpp:1942

◆ qt_win_get_open_file_names()

QStringList qt_win_get_open_file_names ( const QFileDialogArgs args,
QString initialDirectory,
QString selectedFilter 
)

Definition at line 627 of file qfiledialog_win.cpp.

Referenced by QFileDialog::getOpenFileNames(), and QFileDialog::labelText().

630 {
631  QFileInfo fi;
632  QDir dir;
633 
634  if (initialDirectory && initialDirectory->left(5) == QLatin1String("file:"))
635  initialDirectory->remove(0, 5);
636  fi = QFileInfo(*initialDirectory);
637 
638  if (initialDirectory && !fi.isDir()) {
639  *initialDirectory = fi.absolutePath();
640  }
641 
642  if (!fi.exists())
643  *initialDirectory = QDir::homePath();
644 
645  DWORD selFilIdx = 0;
646 
647  QStringList filterLst = qt_win_make_filters_list(args.filter);
648  int idx = 0;
649  if (selectedFilter) {
650  idx = filterLst.indexOf(*selectedFilter);
651  }
652  // Windows Vista (& above) allows users to search from file dialogs. If user selects
653  // multiple files belonging to different folders from these search results, the
654  // GetOpenFileName() will return only one folder name for all the files. To retrieve
655  // the correct path for all selected files, we have to use Common Item Dialog interfaces.
656 #ifndef Q_WS_WINCE
658  return qt_win_CID_get_open_file_names(args, initialDirectory, filterLst, selectedFilter, idx);
659 #endif
660 
661  QStringList result;
662  QDialog modal_widget;
663  modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true);
664  modal_widget.setParent(args.parent, Qt::Window);
665  QApplicationPrivate::enterModal(&modal_widget);
666 
667  bool hideFiltersDetails = args.options & QFileDialog::HideNameFilterDetails;
668  OPENFILENAME* ofn = qt_win_make_OFN(args.parent, args.selection,
669  args.directory, args.caption,
670  qt_win_filter(args.filter, hideFiltersDetails),
672  args.options);
673  if (idx)
674  ofn->nFilterIndex = idx + 1;
675  if (GetOpenFileName(ofn)) {
676  QString fileOrDir = QString::fromWCharArray(ofn->lpstrFile);
677  selFilIdx = ofn->nFilterIndex;
678  int offset = fileOrDir.length() + 1;
679  if (ofn->lpstrFile[offset] == 0) {
680  // Only one file selected; has full path
681  fi.setFile(fileOrDir);
682  QString res = fi.absoluteFilePath();
683  if (!res.isEmpty())
684  result.append(res);
685  }
686  else {
687  // Several files selected; first string is path
688  dir.setPath(fileOrDir);
689  QString f;
690  while(!(f = QString::fromWCharArray(ofn->lpstrFile + offset)).isEmpty()) {
691  fi.setFile(dir, f);
692  QString res = fi.absoluteFilePath();
693  if (!res.isEmpty())
694  result.append(res);
695  offset += f.length() + 1;
696  }
697  }
698  }
699  qt_win_clean_up_OFN(&ofn);
700 
701  QApplicationPrivate::leaveModal(&modal_widget);
702 
704 
705  if (!result.isEmpty()) {
706  *initialDirectory = fi.path(); // only save the path if there is a result
707  if (selectedFilter)
708  *selectedFilter = qt_win_selected_filter(args.filter, selFilIdx);
709  }
710  return result;
711 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
static QStringList qt_win_make_filters_list(const QString &filter)
static OPENFILENAME * qt_win_make_OFN(QWidget *parent, const QString &initialSelection, const QString &initialDirectory, const QString &title, const QString &filters, QFileDialog::FileMode mode, QFileDialog::Options options)
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
The QDialog class is the base class of dialog windows.
Definition: qdialog.h:56
void setPath(const QString &path)
Sets the path of the directory to path.
Definition: qdir.cpp:590
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QString absoluteFilePath() const
Returns an absolute path including the file name.
Definition: qfileinfo.cpp:534
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void qt_win_eatMouseMove()
bool exists() const
Returns true if the file exists; otherwise returns false.
Definition: qfileinfo.cpp:675
static const WinVersion WindowsVersion
the version of the Windows operating system on which the application is run (Windows only) ...
Definition: qglobal.h:1613
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString qt_win_filter(const QString &filter, bool hideFiltersDetails)
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory; otherwise ret...
Definition: qfileinfo.cpp:990
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void setFile(const QString &file)
Sets the file that the QFileInfo provides information about to file.
Definition: qfileinfo.cpp:468
QWidget * parent
QFileDialog::Options options
static void qt_win_clean_up_OFN(OPENFILENAME **ofn)
static QString qt_win_selected_filter(const QString &filter, DWORD idx)
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QString path() const
Returns the file&#39;s path.
Definition: qfileinfo.cpp:615
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static void leaveModal(QWidget *)
QString absolutePath() const
Returns a file&#39;s path absolute path.
Definition: qfileinfo.cpp:577
static void enterModal(QWidget *)
static QString homePath()
Returns the absolute path of the user&#39;s home directory.
Definition: qdir.cpp:1942

◆ qt_win_get_save_file_name()

QString qt_win_get_save_file_name ( const QFileDialogArgs args,
QString initialDirectory,
QString selectedFilter 
)

Definition at line 293 of file qfiledialog_win.cpp.

Referenced by QFileDialog::getSaveFileName(), and QFileDialog::labelText().

296 {
297  QString result;
298 
299  QString isel = args.selection;
300  if (initialDirectory && initialDirectory->left(5) == QLatin1String("file:"))
301  initialDirectory->remove(0, 5);
302  QFileInfo fi(*initialDirectory);
303 
304  if (initialDirectory && !fi.isDir()) {
305  *initialDirectory = fi.absolutePath();
306  if (isel.isEmpty())
307  isel = fi.fileName();
308  }
309 
310  if (!fi.exists())
311  *initialDirectory = QDir::homePath();
312 
313  DWORD selFilIdx = 0;
314 
315  int idx = 0;
316  if (selectedFilter) {
317  QStringList filterLst = qt_win_make_filters_list(args.filter);
318  idx = filterLst.indexOf(*selectedFilter);
319  }
320 
321  QDialog modal_widget;
322  modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true);
323  modal_widget.setParent(args.parent, Qt::Window);
324  QApplicationPrivate::enterModal(&modal_widget);
325  bool hideFiltersDetails = args.options & QFileDialog::HideNameFilterDetails;
326  // This block is used below for the lpstrDefExt member.
327  // Note that the current MSDN docs document this member wrong.
328  // It should rather be documented as "the default extension if no extension was given and if the
329  // current filter does not have a extension (e.g (*)). If the current filter have an extension, use
330  // the extension of the current filter"
331  QString defaultSaveExt;
332  if (selectedFilter && !selectedFilter->isEmpty()) {
333  defaultSaveExt = qt_win_extract_filter(*selectedFilter);
334  // make sure we only have the extension
335  int firstDot = defaultSaveExt.indexOf(QLatin1Char('.'));
336  if (firstDot != -1) {
337  defaultSaveExt.remove(0, firstDot + 1);
338  } else {
339  defaultSaveExt.clear();
340  }
341  }
342 
343  OPENFILENAME *ofn = qt_win_make_OFN(args.parent, args.selection,
344  args.directory, args.caption,
345  qt_win_filter(args.filter, hideFiltersDetails),
347  args.options);
348 
349  ofn->lpstrDefExt = (wchar_t*)defaultSaveExt.utf16();
350 
351  if (idx)
352  ofn->nFilterIndex = idx + 1;
353  if (GetSaveFileName(ofn)) {
354  result = QString::fromWCharArray(ofn->lpstrFile);
355  selFilIdx = ofn->nFilterIndex;
356  }
357  qt_win_clean_up_OFN(&ofn);
358 
359 #if defined(Q_WS_WINCE)
360  int semIndex = result.indexOf(QLatin1Char(';'));
361  if (semIndex >= 0)
362  result = result.left(semIndex);
363 #endif
364 
365  QApplicationPrivate::leaveModal(&modal_widget);
366 
368 
369  if (result.isEmpty())
370  return result;
371 
372  fi = result;
373  *initialDirectory = fi.path();
374  if (selectedFilter)
375  *selectedFilter = qt_win_selected_filter(args.filter, selFilIdx);
376  return fi.absoluteFilePath();
377 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
static QStringList qt_win_make_filters_list(const QString &filter)
static OPENFILENAME * qt_win_make_OFN(QWidget *parent, const QString &initialSelection, const QString &initialDirectory, const QString &title, const QString &filters, QFileDialog::FileMode mode, QFileDialog::Options options)
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
The QDialog class is the base class of dialog windows.
Definition: qdialog.h:56
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void qt_win_eatMouseMove()
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString qt_win_filter(const QString &filter, bool hideFiltersDetails)
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QString qt_win_extract_filter(const QString &rawFilter)
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QWidget * parent
QFileDialog::Options options
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
static void qt_win_clean_up_OFN(OPENFILENAME **ofn)
static QString qt_win_selected_filter(const QString &filter, DWORD idx)
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static void leaveModal(QWidget *)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static void enterModal(QWidget *)
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290
static QString homePath()
Returns the absolute path of the user&#39;s home directory.
Definition: qdir.cpp:1942

◆ qt_win_make_filters_list()

static QStringList qt_win_make_filters_list ( const QString filter)
static

Definition at line 126 of file qfiledialog_win.cpp.

Referenced by qt_win_filter(), qt_win_get_open_file_name(), qt_win_get_open_file_names(), qt_win_get_save_file_name(), and qt_win_selected_filter().

127 {
128  QString f(filter);
129 
130  if (f.isEmpty())
131  f = QFileDialog::tr("All Files (*.*)");
132 
133  return qt_make_filter_list(f);
134 }
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
The QString class provides a Unicode character string.
Definition: qstring.h:83
QStringList qt_make_filter_list(const QString &filter)

◆ qt_win_make_OFN()

static OPENFILENAME* qt_win_make_OFN ( QWidget parent,
const QString initialSelection,
const QString initialDirectory,
const QString title,
const QString filters,
QFileDialog::FileMode  mode,
QFileDialog::Options  options 
)
static

Definition at line 169 of file qfiledialog_win.cpp.

Referenced by qt_win_get_open_file_name(), qt_win_get_open_file_names(), and qt_win_get_save_file_name().

176 {
177  if (parent)
178  parent = parent->window();
179  else
180  parent = QApplication::activeWindow();
181 
182  tInitDir = QDir::toNativeSeparators(initialDirectory);
183  tFilters = filters;
184  tTitle = title;
185  QString initSel = QDir::toNativeSeparators(initialSelection);
186  if (!initSel.isEmpty()) {
187  initSel.remove(QLatin1Char('<'));
188  initSel.remove(QLatin1Char('>'));
189  initSel.remove(QLatin1Char('\"'));
190  initSel.remove(QLatin1Char('|'));
191  }
192 
193  int maxLen = mode == QFileDialog::ExistingFiles ? maxMultiLen : maxNameLen;
194  wchar_t *tInitSel = new wchar_t[maxLen + 1];
195  if (initSel.length() > 0 && initSel.length() <= maxLen)
196  memcpy(tInitSel, initSel.utf16(), (initSel.length()+1)*sizeof(QChar));
197  else
198  tInitSel[0] = 0;
199 
200  OPENFILENAME* ofn = new OPENFILENAME;
201  memset(ofn, 0, sizeof(OPENFILENAME));
202 
203  ofn->lStructSize = sizeof(OPENFILENAME);
204  ofn->hwndOwner = parent ? parent->winId() : 0;
205  ofn->lpstrFilter = (wchar_t*)tFilters.utf16();
206  ofn->lpstrFile = tInitSel;
207  ofn->nMaxFile = maxLen;
208  ofn->lpstrInitialDir = (wchar_t*)tInitDir.utf16();
209  ofn->lpstrTitle = (wchar_t*)tTitle.utf16();
210  ofn->Flags = (OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_EXPLORER | OFN_PATHMUSTEXIST);
211  if (mode == QFileDialog::ExistingFile ||
213  ofn->Flags |= (OFN_FILEMUSTEXIST);
214  if (mode == QFileDialog::ExistingFiles)
215  ofn->Flags |= (OFN_ALLOWMULTISELECT);
216  if (!(options & QFileDialog::DontConfirmOverwrite))
217  ofn->Flags |= OFN_OVERWRITEPROMPT;
218 
219  return ofn;
220 }
static QString tInitDir
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static QWidget * activeWindow()
Returns the application top-level window that has the keyboard input focus, or 0 if no application wi...
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
const int maxMultiLen
static QString tTitle
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static const char *const filters[3]
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
static QString tFilters
WId winId() const
Returns the window system identifier of the widget.
Definition: qwidget.cpp:2557
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
const int maxNameLen
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290

◆ qt_win_resolve_libs()

static void qt_win_resolve_libs ( )
static

Definition at line 79 of file qfiledialog_win.cpp.

Referenced by qt_win_get_existing_directory(), and winGetExistDirCallbackProc().

80 {
81  static bool triedResolve = false;
82  if (!triedResolve) {
83 #if !defined(Q_WS_WINCE)
84  QSystemLibrary lib(QLatin1String("shell32"));
85  ptrSHBrowseForFolder = (PtrSHBrowseForFolder)lib.resolve("SHBrowseForFolderW");
86  ptrSHGetPathFromIDList = (PtrSHGetPathFromIDList)lib.resolve("SHGetPathFromIDListW");
87  ptrSHGetMalloc = (PtrSHGetMalloc)lib.resolve("SHGetMalloc");
88 #else
89  // CE stores them in a different lib and does not use unicode version
90  QSystemLibrary lib(QLatin1String("Ceshell"));
91  ptrSHBrowseForFolder = (PtrSHBrowseForFolder)lib.resolve("SHBrowseForFolder");
92  ptrSHGetPathFromIDList = (PtrSHGetPathFromIDList)lib.resolve("SHGetPathFromIDList");
93  ptrSHGetMalloc = (PtrSHGetMalloc)lib.resolve("SHGetMalloc");
95  qt_priv_ptr_valid = true;
96 #endif
97 
98  triedResolve = true;
99  }
100 }
BOOL(WINAPI * PtrSHGetPathFromIDList)(qt_LPITEMIDLIST, LPWSTR)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool qt_priv_ptr_valid
HRESULT(WINAPI * PtrSHGetMalloc)(LPMALLOC *)
static PtrSHGetMalloc ptrSHGetMalloc
static PtrSHBrowseForFolder ptrSHBrowseForFolder
qt_LPITEMIDLIST(WINAPI * PtrSHBrowseForFolder)(qt_BROWSEINFO *)
static PtrSHGetPathFromIDList ptrSHGetPathFromIDList

◆ qt_win_selected_filter()

static QString qt_win_selected_filter ( const QString filter,
DWORD  idx 
)
static

Definition at line 162 of file qfiledialog_win.cpp.

Referenced by qt_win_get_open_file_name(), qt_win_get_open_file_names(), and qt_win_get_save_file_name().

163 {
164  return qt_win_make_filters_list(filter).at((int)idx - 1);
165 }
static QStringList qt_win_make_filters_list(const QString &filter)
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468

◆ winGetExistDirCallbackProc()

static int __stdcall winGetExistDirCallbackProc ( HWND  hwnd,
UINT  uMsg,
LPARAM  lParam,
LPARAM  lpData 
)
static

Definition at line 715 of file qfiledialog_win.cpp.

Referenced by qt_win_get_existing_directory().

719 {
720  if (uMsg == BFFM_INITIALIZED && lpData != 0) {
721  QString *initDir = (QString *)(lpData);
722  if (!initDir->isEmpty()) {
723  SendMessage(hwnd, BFFM_SETSELECTION, TRUE, LPARAM(initDir->utf16()));
724  }
725  } else if (uMsg == BFFM_SELCHANGED) {
728  wchar_t path[MAX_PATH];
730  QString tmpStr = QString::fromWCharArray(path);
731  if (!tmpStr.isEmpty())
732  SendMessage(hwnd, BFFM_ENABLEOK, 1, 1);
733  else
734  SendMessage(hwnd, BFFM_ENABLEOK, 0, 0);
735  SendMessage(hwnd, BFFM_SETSTATUSTEXT, 1, LPARAM(path));
736  }
737  }
738  return 0;
739 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
#define BFFM_SETSELECTION
#define BFFM_SETSTATUSTEXT
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static void qt_win_resolve_libs()
#define BFFM_SELCHANGED
#define TRUE
Synonym for true.
Definition: qglobal.h:1018
struct qt_ITEMIDLIST * qt_LPITEMIDLIST
#define BFFM_INITIALIZED
static PtrSHGetPathFromIDList ptrSHGetPathFromIDList
#define BFFM_ENABLEOK
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290

Variable Documentation

◆ maxMultiLen

const int maxMultiLen = 65535

Definition at line 106 of file qfiledialog_win.cpp.

Referenced by qt_win_make_OFN().

◆ maxNameLen

const int maxNameLen = 1023

Definition at line 105 of file qfiledialog_win.cpp.

Referenced by qt_win_make_OFN().

◆ ptrSHBrowseForFolder

PtrSHBrowseForFolder ptrSHBrowseForFolder = 0
static

Definition at line 70 of file qfiledialog_win.cpp.

Referenced by qt_win_get_existing_directory(), and qt_win_resolve_libs().

◆ ptrSHGetMalloc

PtrSHGetMalloc ptrSHGetMalloc = 0
static

Definition at line 74 of file qfiledialog_win.cpp.

Referenced by qt_win_get_existing_directory(), and qt_win_resolve_libs().

◆ ptrSHGetPathFromIDList

PtrSHGetPathFromIDList ptrSHGetPathFromIDList = 0
static

◆ qt_file_dialog_filter_reg_exp

const char* qt_file_dialog_filter_reg_exp

Definition at line 438 of file qfiledialog.cpp.

Referenced by qt_win_extract_filter(), qt_win_filter(), and qt_win_resolve_libs().

◆ qt_priv_ptr_valid

bool qt_priv_ptr_valid = false

Definition at line 60 of file qfiledialog_win.cpp.

Referenced by QFileDialog::getExistingDirectory(), and qt_win_resolve_libs().

◆ tFilters

QString tFilters
static

Definition at line 167 of file qfiledialog_win.cpp.

◆ tInitDir

QString tInitDir
static

Definition at line 167 of file qfiledialog_win.cpp.

◆ tTitle

QString tTitle
static

Definition at line 167 of file qfiledialog_win.cpp.