-
Notifications
You must be signed in to change notification settings - Fork 0
/
qwindowsdialoghelpers.cpp
2177 lines (1906 loc) · 75.5 KB
/
qwindowsdialoghelpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#define QT_NO_URL_CAST_FROM_STRING 1
#define _WIN32_WINNT 0x0600
#include "qwindowsdialoghelpers.h"
#include "qwindowscontext.h"
#include "qwindowswindow.h"
#include "qwindowsintegration.h"
#include "qwindowstheme.h" // Color conversion helpers
#include <QtGui/QGuiApplication>
#include <QtGui/QColor>
#include <QtCore/QDebug>
#include <QtCore/QRegExp>
#include <QtCore/QTimer>
#include <QtCore/QDir>
#include <QtCore/QScopedArrayPointer>
#include <QtCore/QSharedPointer>
#include <QtCore/QObject>
#include <QtCore/QThread>
#include <QtCore/QSysInfo>
#include <QtCore/QSharedData>
#include <QtCore/QExplicitlySharedDataPointer>
#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>
#include <QtCore/private/qsystemlibrary_p.h>
#include <algorithm>
#include "qtwindows_additional.h"
#define STRICT_TYPED_ITEMIDS
#include <shlobj.h>
#include <shlwapi.h>
// #define USE_NATIVE_COLOR_DIALOG /* Testing purposes only */
#ifdef Q_CC_MINGW /* Add missing declarations for MinGW */
#ifndef __IShellLibrary_FWD_DEFINED__
/* Constants obtained by running the below stream operator for
* CLSID, IID on the constants in the Windows SDK libraries. */
static const IID IID_IFileOpenDialog = {0xd57c7288, 0xd4ad, 0x4768, {0xbe, 0x02, 0x9d, 0x96, 0x95, 0x32, 0xd9, 0x60}};
static const IID IID_IFileSaveDialog = {0x84bccd23, 0x5fde, 0x4cdb,{0xae, 0xa4, 0xaf, 0x64, 0xb8, 0x3d, 0x78, 0xab}};
#ifdef __MINGW64_VERSION_MAJOR
static const IID q_IID_IShellItem = {0x43826d1e, 0xe718, 0x42ee, {0xbc, 0x55, 0xa1, 0xe2, 0x61, 0xc3, 0x7b, 0xfe}};
#define IID_IShellItem q_IID_IShellItem
#else
static const IID IID_IShellItem = {0x43826d1e, 0xe718, 0x42ee, {0xbc, 0x55, 0xa1, 0xe2, 0x61, 0xc3, 0x7b, 0xfe}};
static const IID IID_IShellItemArray = {0xb63ea76d, 0x1f85, 0x456f, {0xa1, 0x9c, 0x48, 0x15, 0x9e, 0xfa, 0x85, 0x8b}};
# define LFF_FORCEFILESYSTEM 1
#endif
static const IID IID_IFileDialogEvents = {0x973510db, 0x7d7f, 0x452b,{0x89, 0x75, 0x74, 0xa8, 0x58, 0x28, 0xd3, 0x54}};
static const CLSID CLSID_FileOpenDialog = {0xdc1c5a9c, 0xe88a, 0x4dde, {0xa5, 0xa1, 0x60, 0xf8, 0x2a, 0x20, 0xae, 0xf7}};
static const CLSID CLSID_FileSaveDialog = {0xc0b4e2f3, 0xba21, 0x4773,{0x8d, 0xba, 0x33, 0x5e, 0xc9, 0x46, 0xeb, 0x8b}};
typedef struct _COMDLG_FILTERSPEC
{
LPCWSTR pszName;
LPCWSTR pszSpec;
} COMDLG_FILTERSPEC;
#define FOS_OVERWRITEPROMPT 0x2
#define FOS_STRICTFILETYPES 0x4
#define FOS_NOCHANGEDIR 0x8
#define FOS_PICKFOLDERS 0x20
#define FOS_FORCEFILESYSTEM 0x40
#define FOS_ALLNONSTORAGEITEMS 0x80
#define FOS_NOVALIDATE 0x100
#define FOS_ALLOWMULTISELECT 0x200
#define FOS_PATHMUSTEXIST 0x800
#define FOS_FILEMUSTEXIST 0x1000
#define FOS_CREATEPROMPT 0x2000
#define FOS_SHAREAWARE 0x4000
#define FOS_NOREADONLYRETURN 0x8000
#define FOS_NOTESTFILECREATE 0x10000
#define FOS_HIDEMRUPLACES 0x20000
#define FOS_HIDEPINNEDPLACES 0x40000
#define FOS_NODEREFERENCELINKS 0x100000
#define FOS_DONTADDTORECENT 0x2000000
#define FOS_FORCESHOWHIDDEN 0x10000000
#define FOS_DEFAULTNOMINIMODE 0x20000000
#define FOS_FORCEPREVIEWPANEON 0x40000000
#if __MINGW64_VERSION_MAJOR < 2
typedef int GETPROPERTYSTOREFLAGS;
#define GPS_DEFAULT 0x00000000
#define GPS_HANDLERPROPERTIESONLY 0x00000001
#define GPS_READWRITE 0x00000002
#define GPS_TEMPORARY 0x00000004
#define GPS_FASTPROPERTIESONLY 0x00000008
#define GPS_OPENSLOWITEM 0x00000010
#define GPS_DELAYCREATION 0x00000020
#define GPS_BESTEFFORT 0x00000040
#define GPS_MASK_VALID 0x0000007F
#endif
typedef int (QT_WIN_CALLBACK* BFFCALLBACK)(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData);
// message from browser
#define BFFM_INITIALIZED 1
#define BFFM_SELCHANGED 2
#define BFFM_ENABLEOK (WM_USER + 101)
// Browsing for directory.
#define BIF_NONEWFOLDERBUTTON 0x0200
#define BIF_NOTRANSLATETARGETS 0x0400
#define BIF_BROWSEFORCOMPUTER 0x1000
#define BIF_BROWSEFORPRINTER 0x2000
#define BIF_BROWSEINCLUDEFILES 0x4000
#define BIF_SHAREABLE 0x8000
//the enums
typedef enum {
SIATTRIBFLAGS_AND = 0x1,
SIATTRIBFLAGS_OR = 0x2,
SIATTRIBFLAGS_APPCOMPAT = 0x3,
SIATTRIBFLAGS_MASK = 0x3
} SIATTRIBFLAGS;
#ifndef __MINGW64_VERSION_MAJOR
typedef enum {
SIGDN_NORMALDISPLAY = 0x00000000,
SIGDN_PARENTRELATIVEPARSING = 0x80018001,
SIGDN_PARENTRELATIVEFORADDRESSBAR = 0x8001c001,
SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000,
SIGDN_PARENTRELATIVEEDITING = 0x80031001,
SIGDN_DESKTOPABSOLUTEEDITING = 0x8004c000,
SIGDN_FILESYSPATH = 0x80058000,
SIGDN_URL = 0x80068000
} SIGDN;
#endif
typedef enum {
FDAP_BOTTOM = 0x00000000,
FDAP_TOP = 0x00000001
} FDAP;
typedef enum {
FDESVR_DEFAULT = 0x00000000,
FDESVR_ACCEPT = 0x00000001,
FDESVR_REFUSE = 0x00000002
} FDE_SHAREVIOLATION_RESPONSE;
typedef FDE_SHAREVIOLATION_RESPONSE FDE_OVERWRITE_RESPONSE;
//the structs
typedef struct {
LPCWSTR pszName;
LPCWSTR pszSpec;
} qt_COMDLG_FILTERSPEC;
typedef struct {
GUID fmtid;
DWORD pid;
} qt_PROPERTYKEY;
typedef struct {
USHORT cb;
BYTE abID[1];
} qt_SHITEMID, *qt_LPSHITEMID;
typedef struct {
qt_SHITEMID mkid;
} qt_ITEMIDLIST, *qt_LPITEMIDLIST;
typedef const qt_ITEMIDLIST *qt_LPCITEMIDLIST;
typedef struct {
HWND hwndOwner;
qt_LPCITEMIDLIST pidlRoot;
LPWSTR pszDisplayName;
LPCWSTR lpszTitle;
UINT ulFlags;
BFFCALLBACK lpfn;
LPARAM lParam;
int iImage;
} qt_BROWSEINFO;
#endif // __IShellLibrary_FWD_DEFINED__
#ifndef __IFileDialogEvents_FWD_DEFINED__
DECLARE_INTERFACE(IFileDialogEvents);
#endif
#ifndef __IShellItem_INTERFACE_DEFINED__
DECLARE_INTERFACE_(IShellItem, IUnknown)
{
STDMETHOD(BindToHandler)(THIS_ IBindCtx *pbc, REFGUID bhid, REFIID riid, void **ppv) PURE;
STDMETHOD(GetParent)(THIS_ IShellItem **ppsi) PURE;
STDMETHOD(GetDisplayName)(THIS_ SIGDN sigdnName, LPWSTR *ppszName) PURE;
STDMETHOD(GetAttributes)(THIS_ ULONG sfgaoMask, ULONG *psfgaoAttribs) PURE;
STDMETHOD(Compare)(THIS_ IShellItem *psi, DWORD hint, int *piOrder) PURE;
};
#endif
#ifndef __IShellItemFilter_INTERFACE_DEFINED__
DECLARE_INTERFACE_(IShellItemFilter, IUnknown)
{
STDMETHOD(IncludeItem)(THIS_ IShellItem *psi) PURE;
STDMETHOD(GetEnumFlagsForItem)(THIS_ IShellItem *psi, DWORD *pgrfFlags) PURE;
};
#endif
#ifndef __IEnumShellItems_INTERFACE_DEFINED__
DECLARE_INTERFACE_(IEnumShellItems, IUnknown)
{
STDMETHOD(Next)(THIS_ ULONG celt, IShellItem **rgelt, ULONG *pceltFetched) PURE;
STDMETHOD(Skip)(THIS_ ULONG celt) PURE;
STDMETHOD(Reset)(THIS_) PURE;
STDMETHOD(Clone)(THIS_ IEnumShellItems **ppenum) PURE;
};
#endif
#ifndef __IShellItemArray_INTERFACE_DEFINED__
DECLARE_INTERFACE_(IShellItemArray, IUnknown)
{
STDMETHOD(BindToHandler)(THIS_ IBindCtx *pbc, REFGUID rbhid, REFIID riid, void **ppvOut) PURE;
STDMETHOD(GetPropertyStore)(THIS_ GETPROPERTYSTOREFLAGS flags, REFIID riid, void **ppv) PURE;
STDMETHOD(GetPropertyDescriptionList)(THIS_ const qt_PROPERTYKEY *keyType, REFIID riid, void **ppv) PURE;
STDMETHOD(GetAttributes)(THIS_ SIATTRIBFLAGS dwAttribFlags, ULONG sfgaoMask, ULONG *psfgaoAttribs) PURE;
STDMETHOD(GetCount)(THIS_ DWORD *pdwNumItems) PURE;
STDMETHOD(GetItemAt)(THIS_ DWORD dwIndex, IShellItem **ppsi) PURE;
STDMETHOD(EnumItems)(THIS_ IEnumShellItems **ppenumShellItems) PURE;
};
#endif
#ifndef __IShellLibrary_INTERFACE_DEFINED__
enum LIBRARYOPTIONFLAGS {};
enum DEFAULTSAVEFOLDERTYPE { DSFT_DETECT = 1 };
enum LIBRARYSAVEFLAGS {};
DECLARE_INTERFACE_(IShellLibrary, IUnknown)
{
STDMETHOD(LoadLibraryFromItem)(THIS_ IShellItem *psiLibrary, DWORD grfMode) PURE;
STDMETHOD(LoadLibraryFromKnownFolder)(THIS_ const GUID &kfidLibrary, DWORD grfMode) PURE;
STDMETHOD(AddFolder)(THIS_ IShellItem *psiLocation) PURE;
STDMETHOD(RemoveFolder)(THIS_ IShellItem *psiLocation) PURE;
STDMETHOD(GetFolders)(THIS_ int lff, REFIID riid, void **ppv) PURE;
STDMETHOD(ResolveFolder)(THIS_ IShellItem *psiFolderToResolve, DWORD dwTimeout, REFIID riid, void **ppv) PURE;
STDMETHOD(GetDefaultSaveFolder)(THIS_ DEFAULTSAVEFOLDERTYPE dsft, REFIID riid, void **ppv) PURE;
STDMETHOD(SetDefaultSaveFolder)(THIS_ DEFAULTSAVEFOLDERTYPE dsft, IShellItem *psi) PURE;
STDMETHOD(GetOptions)(THIS_ LIBRARYOPTIONFLAGS *plofOptions) PURE;
STDMETHOD(SetOptions)(THIS_ LIBRARYOPTIONFLAGS lofMask, LIBRARYOPTIONFLAGS lofOptions) PURE;
STDMETHOD(GetFolderType)(THIS_ GUID *pftid) PURE;
STDMETHOD(SetFolderType)(THIS_ const GUID &ftid) PURE;
STDMETHOD(GetIcon)(THIS_ LPWSTR *ppszIcon) PURE;
STDMETHOD(SetIcon)(THIS_ LPCWSTR pszIcon) PURE;
STDMETHOD(Commit)(THIS_) PURE;
STDMETHOD(Save)(THIS_ IShellItem *psiFolderToSaveIn, LPCWSTR pszLibraryName, LIBRARYSAVEFLAGS lsf, IShellItem **ppsiSavedTo) PURE;
STDMETHOD(SaveInKnownFolder)(THIS_ const GUID &kfidToSaveIn, LPCWSTR pszLibraryName, LIBRARYSAVEFLAGS lsf,IShellItem **ppsiSavedTo) PURE;
};
#endif
#ifndef __IModalWindow_INTERFACE_DEFINED__
DECLARE_INTERFACE_(IModalWindow, IUnknown)
{
STDMETHOD(Show)(THIS_ HWND hwndParent) PURE;
};
#endif
#ifndef __IFileDialog_INTERFACE_DEFINED__
DECLARE_INTERFACE_(IFileDialog, IModalWindow)
{
STDMETHOD(SetFileTypes)(THIS_ UINT cFileTypes, const COMDLG_FILTERSPEC *rgFilterSpec) PURE;
STDMETHOD(SetFileTypeIndex)(THIS_ UINT iFileType) PURE;
STDMETHOD(GetFileTypeIndex)(THIS_ UINT *piFileType) PURE;
STDMETHOD(Advise)(THIS_ IFileDialogEvents *pfde, DWORD *pdwCookie) PURE;
STDMETHOD(Unadvise)(THIS_ DWORD dwCookie) PURE;
STDMETHOD(SetOptions)(THIS_ DWORD fos) PURE;
STDMETHOD(GetOptions)(THIS_ DWORD *pfos) PURE;
STDMETHOD(SetDefaultFolder)(THIS_ IShellItem *psi) PURE;
STDMETHOD(SetFolder)(THIS_ IShellItem *psi) PURE;
STDMETHOD(GetFolder)(THIS_ IShellItem **ppsi) PURE;
STDMETHOD(GetCurrentSelection)(THIS_ IShellItem **ppsi) PURE;
STDMETHOD(SetFileName)(THIS_ LPCWSTR pszName) PURE;
STDMETHOD(GetFileName)(THIS_ LPWSTR *pszName) PURE;
STDMETHOD(SetTitle)(THIS_ LPCWSTR pszTitle) PURE;
STDMETHOD(SetOkButtonLabel)(THIS_ LPCWSTR pszText) PURE;
STDMETHOD(SetFileNameLabel)(THIS_ LPCWSTR pszLabel) PURE;
STDMETHOD(GetResult)(THIS_ IShellItem **ppsi) PURE;
STDMETHOD(AddPlace)(THIS_ IShellItem *psi, FDAP fdap) PURE;
STDMETHOD(SetDefaultExtension)(THIS_ LPCWSTR pszDefaultExtension) PURE;
STDMETHOD(Close)(THIS_ HRESULT hr) PURE;
STDMETHOD(SetClientGuid)(THIS_ REFGUID guid) PURE;
STDMETHOD(ClearClientData)(THIS_) PURE;
STDMETHOD(SetFilter)(THIS_ IShellItemFilter *pFilter) PURE;
};
#endif
#ifndef __IFileDialogEvents_INTERFACE_DEFINED__
DECLARE_INTERFACE_(IFileDialogEvents, IUnknown)
{
STDMETHOD(OnFileOk)(THIS_ IFileDialog *pfd) PURE;
STDMETHOD(OnFolderChanging)(THIS_ IFileDialog *pfd, IShellItem *psiFolder) PURE;
STDMETHOD(OnFolderChange)(THIS_ IFileDialog *pfd) PURE;
STDMETHOD(OnSelectionChange)(THIS_ IFileDialog *pfd) PURE;
STDMETHOD(OnShareViolation)(THIS_ IFileDialog *pfd, IShellItem *psi, FDE_SHAREVIOLATION_RESPONSE *pResponse) PURE;
STDMETHOD(OnTypeChange)(THIS_ IFileDialog *pfd) PURE;
STDMETHOD(OnOverwrite)(THIS_ IFileDialog *pfd, IShellItem *psi, FDE_OVERWRITE_RESPONSE *pResponse) PURE;
};
#endif
#ifndef __IFileOpenDialog_INTERFACE_DEFINED__
DECLARE_INTERFACE_(IFileOpenDialog, IFileDialog)
{
STDMETHOD(GetResults)(THIS_ IShellItemArray **ppenum) PURE;
STDMETHOD(GetSelectedItems)(THIS_ IShellItemArray **ppsai) PURE;
};
#endif
#ifndef __IPropertyStore_FWD_DEFINED__
typedef IUnknown IPropertyStore;
#endif
#ifndef __IFileOperationProgressSink_FWD_DEFINED__
typedef IUnknown IFileOperationProgressSink;
#endif
#ifndef __IFileSaveDialog_INTERFACE_DEFINED__
DECLARE_INTERFACE_(IFileSaveDialog, IFileDialog)
{
public:
STDMETHOD(SetSaveAsItem)(THIS_ IShellItem *psi) PURE;
STDMETHOD(SetProperties)(THIS_ IPropertyStore *pStore) PURE;
STDMETHOD(SetCollectedProperties)(THIS_ IPropertyStore *pStore) PURE;
STDMETHOD(GetProperties)(THIS_ IPropertyStore **ppStore) PURE;
STDMETHOD(ApplyProperties)(THIS_ IShellItem *psi, IPropertyStore *pStore, HWND hwnd, IFileOperationProgressSink *pSink) PURE;
};
#endif
#endif // Q_CC_MINGW
QT_BEGIN_NAMESPACE
/* Output UID (IID, CLSID) as C++ constants.
* The constants are contained in the Windows SDK libs, but not for MinGW. */
static inline QString guidToString(const GUID &g)
{
QString rc;
QTextStream str(&rc);
str.setIntegerBase(16);
str.setNumberFlags(str.numberFlags() | QTextStream::ShowBase);
str << '{' << g.Data1 << ", " << g.Data2 << ", " << g.Data3;
str.setFieldWidth(2);
str.setFieldAlignment(QTextStream::AlignRight);
str.setPadChar(QLatin1Char('0'));
str << ",{" << g.Data4[0] << ", " << g.Data4[1] << ", " << g.Data4[2] << ", " << g.Data4[3]
<< ", " << g.Data4[4] << ", " << g.Data4[5] << ", " << g.Data4[6] << ", " << g.Data4[7]
<< "}};";
return rc;
}
inline QDebug operator<<(QDebug d, const GUID &g)
{ d.nospace() << guidToString(g); return d; }
// Return an allocated wchar_t array from a QString, reserve more memory if desired.
static wchar_t *qStringToWCharArray(const QString &s, size_t reserveSize = 0)
{
const size_t stringSize = s.size();
wchar_t *result = new wchar_t[qMax(stringSize + 1, reserveSize)];
s.toWCharArray(result);
result[stringSize] = 0;
return result;
}
namespace QWindowsDialogs
{
/*!
\fn eatMouseMove()
After closing a windows dialog with a double click (i.e. open a file)
the message queue still contains a dubious WM_MOUSEMOVE message where
the left button is reported to be down (wParam != 0).
remove all those messages (usually 1) and post the last one with a
reset button state.
\ingroup qt-lighthouse-win
*/
void eatMouseMove()
{
MSG msg = {0, 0, 0, 0, 0, {0, 0} };
while (PeekMessage(&msg, 0, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE))
;
if (msg.message == WM_MOUSEMOVE)
PostMessage(msg.hwnd, msg.message, 0, msg.lParam);
if (QWindowsContext::verboseDialogs)
qDebug("%s triggered=%d" , __FUNCTION__, msg.message == WM_MOUSEMOVE);
}
} // namespace QWindowsDialogs
/*!
\class QWindowsNativeDialogBase
\brief Base class for Windows native dialogs.
Base classes for native dialogs (using the CLSID-based
dialog interfaces "IFileDialog", etc. available from Windows
Vista on) that mimick the behaviour of their QDialog
counterparts as close as possible.
Instances of derived classes are controlled by
QWindowsDialogHelperBase-derived classes.
A major difference is that there is only an exec(), which
is a modal, blocking call; there is no non-blocking show().
There 2 types of native dialogs:
\list
\li Dialogs provided by the Comdlg32 library (ChooseColor,
ChooseFont). They only provide a modal, blocking
function call (with idle processing).
\li File dialogs are classes derived from IFileDialog. They
inherit IModalWindow and their exec() method (calling
IModalWindow::Show()) is similarly blocking, but methods
like close() can be called on them from event handlers.
\endlist
\sa QWindowsDialogHelperBase
\internal
\ingroup qt-lighthouse-win
*/
class QWindowsNativeDialogBase : public QObject
{
Q_OBJECT
public:
virtual void setWindowTitle(const QString &title) = 0;
bool executed() const { return m_executed; }
void exec(HWND owner = 0) { doExec(owner); m_executed = true; }
signals:
void accepted();
void rejected();
public slots:
virtual void close() = 0;
protected:
QWindowsNativeDialogBase() : m_executed(false) {}
private:
virtual void doExec(HWND owner = 0) = 0;
bool m_executed;
};
/*!
\class QWindowsDialogHelperBase
\brief Helper for native Windows dialogs.
Provides basic functionality and introduces new virtuals.
The native dialog is created in setVisible_sys() since
then modality and the state of DontUseNativeDialog is known.
Modal dialogs are then run by exec(). Non-modal dialogs are shown using a
separate thread started in show() should they support it.
\sa QWindowsDialogThread, QWindowsNativeDialogBase
\internal
\ingroup qt-lighthouse-win
*/
template <class BaseClass>
QWindowsDialogHelperBase<BaseClass>::QWindowsDialogHelperBase() :
m_nativeDialog(0),
m_ownerWindow(0),
m_timerId(0)
{
}
template <class BaseClass>
QWindowsNativeDialogBase *QWindowsDialogHelperBase<BaseClass>::nativeDialog() const
{
if (m_nativeDialog.isNull()) {
qWarning("%s invoked with no native dialog present.", __FUNCTION__);
return 0;
}
return m_nativeDialog.data();
}
template <class BaseClass>
void QWindowsDialogHelperBase<BaseClass>::timerEvent(QTimerEvent *)
{
startDialogThread();
}
template <class BaseClass>
QWindowsNativeDialogBase *QWindowsDialogHelperBase<BaseClass>::ensureNativeDialog()
{
// Create dialog and apply common settings. Check "executed" flag as well
// since for example IFileDialog::Show() works only once.
if (m_nativeDialog.isNull() || m_nativeDialog->executed())
m_nativeDialog = QWindowsNativeDialogBasePtr(createNativeDialog());
return m_nativeDialog.data();
}
/*!
\class QWindowsDialogThread
\brief Run a non-modal native dialog in a separate thread.
\sa QWindowsDialogHelperBase
\internal
\ingroup qt-lighthouse-win
*/
class QWindowsDialogThread : public QThread
{
public:
typedef QSharedPointer<QWindowsNativeDialogBase> QWindowsNativeDialogBasePtr;
explicit QWindowsDialogThread(const QWindowsNativeDialogBasePtr &d, HWND owner)
: m_dialog(d), m_owner(owner) {}
void run();
private:
const QWindowsNativeDialogBasePtr m_dialog;
const HWND m_owner;
};
void QWindowsDialogThread::run()
{
if (QWindowsContext::verboseDialogs)
qDebug(">%s" , __FUNCTION__);
m_dialog->exec(m_owner);
deleteLater();
if (QWindowsContext::verboseDialogs)
qDebug("<%s" , __FUNCTION__);
}
template <class BaseClass>
bool QWindowsDialogHelperBase<BaseClass>::show(Qt::WindowFlags,
Qt::WindowModality windowModality,
QWindow *parent)
{
const bool modal = (windowModality != Qt::NonModal);
if (parent) {
m_ownerWindow = QWindowsWindow::handleOf(parent);
} else {
m_ownerWindow = 0;
}
if (QWindowsContext::verboseDialogs)
qDebug("%s modal=%d modal supported? %d native=%p parent=%p" ,
__FUNCTION__, modal, supportsNonModalDialog(parent), m_nativeDialog.data(), m_ownerWindow);
if (!modal && !supportsNonModalDialog(parent))
return false; // Was it changed in-between?
if (!ensureNativeDialog())
return false;
// Start a background thread to show the dialog. For modal dialogs,
// a subsequent call to exec() may follow. So, start an idle timer
// which will start the dialog thread. If exec() is then called, the
// timer is stopped and dialog->exec() is called directly.
if (modal) {
m_timerId = this->startTimer(0);
} else {
startDialogThread();
}
return true;
}
template <class BaseClass>
void QWindowsDialogHelperBase<BaseClass>::startDialogThread()
{
Q_ASSERT(!m_nativeDialog.isNull());
QWindowsDialogThread *thread = new QWindowsDialogThread(m_nativeDialog, m_ownerWindow);
thread->start();
stopTimer();
}
template <class BaseClass>
void QWindowsDialogHelperBase<BaseClass>::stopTimer()
{
if (m_timerId) {
this->killTimer(m_timerId);
m_timerId = 0;
}
}
#ifndef Q_OS_WINCE
// Find a file dialog window created by IFileDialog by process id, window
// title and class, which starts with a hash '#'.
struct FindDialogContext
{
explicit FindDialogContext(const QString &titleIn)
: title(qStringToWCharArray(titleIn)), processId(GetCurrentProcessId()), hwnd(0) {}
const QScopedArrayPointer<wchar_t> title;
const DWORD processId;
HWND hwnd; // contains the HWND of the window found.
};
static BOOL CALLBACK findDialogEnumWindowsProc(HWND hwnd, LPARAM lParam)
{
FindDialogContext *context = reinterpret_cast<FindDialogContext *>(lParam);
DWORD winPid = 0;
GetWindowThreadProcessId(hwnd, &winPid);
if (winPid != context->processId)
return TRUE;
wchar_t buf[256];
if (!RealGetWindowClass(hwnd, buf, sizeof(buf)/sizeof(wchar_t)) || buf[0] != L'#')
return TRUE;
if (!GetWindowTextW(hwnd, buf, sizeof(buf)/sizeof(wchar_t)) || wcscmp(buf, context->title.data()))
return TRUE;
context->hwnd = hwnd;
return FALSE;
}
static inline HWND findDialogWindow(const QString &title)
{
FindDialogContext context(title);
EnumWindows(findDialogEnumWindowsProc, reinterpret_cast<LPARAM>(&context));
return context.hwnd;
}
#endif // !Q_OS_WINCE
template <class BaseClass>
void QWindowsDialogHelperBase<BaseClass>::hide()
{
if (m_nativeDialog)
m_nativeDialog->close();
m_ownerWindow = 0;
}
template <class BaseClass>
void QWindowsDialogHelperBase<BaseClass>::exec()
{
if (QWindowsContext::verboseDialogs)
qDebug("%s" , __FUNCTION__);
stopTimer();
if (QWindowsNativeDialogBase *nd = nativeDialog()) {
nd->exec(m_ownerWindow);
m_nativeDialog.clear();
}
}
/*!
\class QWindowsFileDialogSharedData
\brief Explicitly shared file dialog parameters that are not in QFileDialogOptions.
Contain Parameters that need to be cached while the native dialog does not
exist yet. In addition, the data are updated by the change notifications of the
IFileDialogEvent, as querying them after the dialog has closed
does not reliably work. Provides thread-safe setters (for the non-modal case).
\internal
\ingroup qt-lighthouse-win
\sa QFileDialogOptions
*/
class QWindowsFileDialogSharedData
{
public:
QWindowsFileDialogSharedData() : m_data(new Data) {}
void fromOptions(const QSharedPointer<QFileDialogOptions> &o);
QUrl directory() const;
void setDirectory(const QUrl &);
QString selectedNameFilter() const;
void setSelectedNameFilter(const QString &);
QList<QUrl> selectedFiles() const;
void setSelectedFiles(const QList<QUrl> &);
QString selectedFile() const;
private:
class Data : public QSharedData {
public:
QUrl directory;
QString selectedNameFilter;
QList<QUrl> selectedFiles;
QMutex mutex;
};
QExplicitlySharedDataPointer<Data> m_data;
};
inline QUrl QWindowsFileDialogSharedData::directory() const
{
m_data->mutex.lock();
const QUrl result = m_data->directory;
m_data->mutex.unlock();
return result;
}
inline void QWindowsFileDialogSharedData::setDirectory(const QUrl &d)
{
QMutexLocker (&m_data->mutex);
m_data->directory = d;
}
inline QString QWindowsFileDialogSharedData::selectedNameFilter() const
{
m_data->mutex.lock();
const QString result = m_data->selectedNameFilter;
m_data->mutex.unlock();
return result;
}
inline void QWindowsFileDialogSharedData::setSelectedNameFilter(const QString &f)
{
QMutexLocker (&m_data->mutex);
m_data->selectedNameFilter = f;
}
inline QList<QUrl> QWindowsFileDialogSharedData::selectedFiles() const
{
m_data->mutex.lock();
const QList<QUrl> result = m_data->selectedFiles;
m_data->mutex.unlock();
return result;
}
inline QString QWindowsFileDialogSharedData::selectedFile() const
{
const QList<QUrl> files = selectedFiles();
return files.isEmpty() ? QString() : files.front().toLocalFile();
}
inline void QWindowsFileDialogSharedData::setSelectedFiles(const QList<QUrl> &urls)
{
QMutexLocker (&m_data->mutex);
m_data->selectedFiles = urls;
}
inline void QWindowsFileDialogSharedData::fromOptions(const QSharedPointer<QFileDialogOptions> &o)
{
QMutexLocker (&m_data->mutex);
m_data->directory = o->initialDirectory();
m_data->selectedFiles = o->initiallySelectedFiles();
m_data->selectedNameFilter = o->initiallySelectedNameFilter();
}
/*!
\class QWindowsNativeFileDialogEventHandler
\brief Listens to IFileDialog events and forwards them to QWindowsNativeFileDialogBase
Events like 'folder change' that have an equivalent signal
in QFileDialog are forwarded.
\sa QWindowsNativeFileDialogBase, QWindowsFileDialogHelper
\internal
\ingroup qt-lighthouse-win
*/
class QWindowsNativeFileDialogBase;
class QWindowsNativeFileDialogEventHandler : public IFileDialogEvents
{
public:
static IFileDialogEvents *create(QWindowsNativeFileDialogBase *nativeFileDialog);
// IUnknown methods
IFACEMETHODIMP QueryInterface(REFIID riid, void** ppv)
{
if (riid != IID_IUnknown && riid != IID_IFileDialogEvents) {
*ppv = NULL;
return ResultFromScode(E_NOINTERFACE);
}
*ppv = this;
AddRef();
return NOERROR;
}
IFACEMETHODIMP_(ULONG) AddRef() { return InterlockedIncrement(&m_ref); }
IFACEMETHODIMP_(ULONG) Release()
{
const long ref = InterlockedDecrement(&m_ref);
if (!ref)
delete this;
return ref;
}
// IFileDialogEvents methods
IFACEMETHODIMP OnFileOk(IFileDialog *);
IFACEMETHODIMP OnFolderChange(IFileDialog *) { return S_OK; }
IFACEMETHODIMP OnFolderChanging(IFileDialog *, IShellItem *);
IFACEMETHODIMP OnHelp(IFileDialog *) { return S_OK; }
IFACEMETHODIMP OnSelectionChange(IFileDialog *);
IFACEMETHODIMP OnShareViolation(IFileDialog *, IShellItem *, FDE_SHAREVIOLATION_RESPONSE *) { return S_OK; }
IFACEMETHODIMP OnTypeChange(IFileDialog *);
IFACEMETHODIMP OnOverwrite(IFileDialog *, IShellItem *, FDE_OVERWRITE_RESPONSE *) { return S_OK; }
QWindowsNativeFileDialogEventHandler(QWindowsNativeFileDialogBase *nativeFileDialog) :
m_ref(1), m_nativeFileDialog(nativeFileDialog) {}
virtual ~QWindowsNativeFileDialogEventHandler() {}
private:
long m_ref;
QWindowsNativeFileDialogBase *m_nativeFileDialog;
};
IFileDialogEvents *QWindowsNativeFileDialogEventHandler::create(QWindowsNativeFileDialogBase *nativeFileDialog)
{
IFileDialogEvents *result;
QWindowsNativeFileDialogEventHandler *eventHandler = new QWindowsNativeFileDialogEventHandler(nativeFileDialog);
if (FAILED(eventHandler->QueryInterface(IID_IFileDialogEvents, reinterpret_cast<void **>(&result)))) {
qErrnoWarning("Unable to obtain IFileDialogEvents");
return 0;
}
eventHandler->Release();
return result;
}
/*!
\class QWindowsNativeFileDialogBase
\brief Windows native file dialog wrapper around IFileOpenDialog, IFileSaveDialog.
Provides convenience methods.
Note that only IFileOpenDialog has multi-file functionality.
\sa QWindowsNativeFileDialogEventHandler, QWindowsFileDialogHelper
\internal
\ingroup qt-lighthouse-win
*/
class QWindowsNativeFileDialogBase : public QWindowsNativeDialogBase
{
Q_OBJECT
Q_PROPERTY(bool hideFiltersDetails READ hideFiltersDetails WRITE setHideFiltersDetails)
public:
~QWindowsNativeFileDialogBase();
inline static QWindowsNativeFileDialogBase *create(QFileDialogOptions::AcceptMode am, const QWindowsFileDialogSharedData &data);
virtual void setWindowTitle(const QString &title);
inline void setMode(QFileDialogOptions::FileMode mode, QFileDialogOptions::FileDialogOptions options);
inline void setDirectory(const QString &directory);
inline void updateDirectory() { setDirectory(m_data.directory().toLocalFile()); }
inline QString directory() const;
virtual void doExec(HWND owner = 0);
virtual void setNameFilters(const QStringList &f);
inline void selectNameFilter(const QString &filter);
inline void updateSelectedNameFilter() { selectNameFilter(m_data.selectedNameFilter()); }
inline QString selectedNameFilter() const;
void selectFile(const QString &fileName) const;
bool hideFiltersDetails() const { return m_hideFiltersDetails; }
void setHideFiltersDetails(bool h) { m_hideFiltersDetails = h; }
void setDefaultSuffix(const QString &s);
inline bool hasDefaultSuffix() const { return m_hasDefaultSuffix; }
inline void setLabelText(QFileDialogOptions::DialogLabel l, const QString &text);
// Return the selected files for tracking in OnSelectionChanged().
virtual QList<QUrl> selectedFiles() const = 0;
// Return the result for tracking in OnFileOk(). Differs from selection for
// example by appended default suffixes, etc.
virtual QList<QUrl> dialogResult() const = 0;
inline void onFolderChange(IShellItem *);
inline void onSelectionChange();
inline void onTypeChange();
inline bool onFileOk();
signals:
void directoryEntered(const QUrl &directory);
void currentChanged(const QUrl &file);
void filterSelected(const QString & filter);
public slots:
virtual void close();
protected:
explicit QWindowsNativeFileDialogBase(const QWindowsFileDialogSharedData &data);
bool init(const CLSID &clsId, const IID &iid);
void setDefaultSuffixSys(const QString &s);
inline IFileDialog * fileDialog() const { return m_fileDialog; }
static QString itemPath(IShellItem *item);
static QList<QUrl> libraryItemFolders(IShellItem *item);
static QString libraryItemDefaultSaveFolder(IShellItem *item);
static int itemPaths(IShellItemArray *items, QList<QUrl> *fileResult = 0);
static IShellItem *shellItem(const QString &path);
const QWindowsFileDialogSharedData &data() const { return m_data; }
QWindowsFileDialogSharedData &data() { return m_data; }
private:
IFileDialog *m_fileDialog;
IFileDialogEvents *m_dialogEvents;
DWORD m_cookie;
QStringList m_nameFilters;
bool m_hideFiltersDetails;
bool m_hasDefaultSuffix;
QWindowsFileDialogSharedData m_data;
QString m_title;
};
QWindowsNativeFileDialogBase::QWindowsNativeFileDialogBase(const QWindowsFileDialogSharedData &data) :
m_fileDialog(0), m_dialogEvents(0), m_cookie(0), m_hideFiltersDetails(false),
m_hasDefaultSuffix(false), m_data(data)
{
}
QWindowsNativeFileDialogBase::~QWindowsNativeFileDialogBase()
{
if (m_dialogEvents && m_fileDialog)
m_fileDialog->Unadvise(m_cookie);
if (m_dialogEvents)
m_dialogEvents->Release();
if (m_fileDialog)
m_fileDialog->Release();
}
bool QWindowsNativeFileDialogBase::init(const CLSID &clsId, const IID &iid)
{
HRESULT hr = CoCreateInstance(clsId, NULL, CLSCTX_INPROC_SERVER,
iid, reinterpret_cast<void **>(&m_fileDialog));
if (FAILED(hr)) {
qErrnoWarning("CoCreateInstance failed");
return false;
}
m_dialogEvents = QWindowsNativeFileDialogEventHandler::create(this);
if (!m_dialogEvents)
return false;
// Register event handler
hr = m_fileDialog->Advise(m_dialogEvents, &m_cookie);
if (FAILED(hr)) {
qErrnoWarning("IFileDialog::Advise failed");
return false;
}
if (QWindowsContext::verboseDialogs)
qDebug("%s %p %p cookie=%lu" , __FUNCTION__, m_fileDialog, m_dialogEvents, m_cookie);
return true;
}
void QWindowsNativeFileDialogBase::setWindowTitle(const QString &title)
{
m_title = title;
m_fileDialog->SetTitle(reinterpret_cast<const wchar_t *>(title.utf16()));
}
IShellItem *QWindowsNativeFileDialogBase::shellItem(const QString &path)
{
#ifndef Q_OS_WINCE
if (QWindowsContext::shell32dll.sHCreateItemFromParsingName) {
IShellItem *result = 0;
const QString native = QDir::toNativeSeparators(path);
const HRESULT hr =
QWindowsContext::shell32dll.sHCreateItemFromParsingName(reinterpret_cast<const wchar_t *>(native.utf16()),
NULL, IID_IShellItem,
reinterpret_cast<void **>(&result));
if (SUCCEEDED(hr))
return result;
}
#endif
qErrnoWarning("%s: SHCreateItemFromParsingName(%s)) failed", __FUNCTION__, qPrintable(path));
return 0;
}
void QWindowsNativeFileDialogBase::setDirectory(const QString &directory)
{
if (!directory.isEmpty()) {
if (IShellItem *psi = QWindowsNativeFileDialogBase::shellItem(directory)) {
m_fileDialog->SetFolder(psi);
psi->Release();
}
}
}
QString QWindowsNativeFileDialogBase::directory() const
{
#ifndef Q_OS_WINCE
IShellItem *item = 0;