File: FileDialogPrivate.cpp

package info (click to toggle)
audacity 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 86,844 kB
  • sloc: ansic: 225,005; cpp: 221,240; sh: 27,327; python: 16,896; makefile: 8,186; lisp: 8,002; perl: 317; xml: 307; sed: 16
file content (1200 lines) | stat: -rw-r--r-- 33,739 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
//
// Copied from wxWidgets 3.0.2 and modified for Audacity
//
/////////////////////////////////////////////////////////////////////////////
// Name:        src/msw/filedlg.cpp
// Purpose:     wxFileDialog
// Author:      Julian Smart
// Modified by: Leland Lucius
// Created:     01/02/97
// Copyright:   (c) Julian Smart
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include <wx/msw/wrapcdlg.h>
    #include <wx/msw/missing.h>
    #include <wx/utils.h>
    #include <wx/msgdlg.h>
    #include <wx/filefn.h>
    #include <wx/intl.h>
    #include <wx/log.h>
    #include <wx/app.h>
    #include <wx/math.h>
#endif

#include <stdlib.h>
#include <string.h>

#include <wx/dynlib.h>
#include <wx/filename.h>
#include <wx/scopeguard.h>
#include <wx/tokenzr.h>
#include <wx/modalhook.h>

#include "../FileDialog.h"

#include <shlobj.h>

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

#ifdef __WIN32__
# define wxMAXPATH   65534
#else
# define wxMAXPATH   1024
#endif

# define wxMAXFILE   1024

# define wxMAXEXT    5

// ----------------------------------------------------------------------------
// globals
// ----------------------------------------------------------------------------

// standard dialog size
static wxRect gs_rectDialog(0, 0, 428, 266);

// ============================================================================
// implementation
// ============================================================================

IMPLEMENT_CLASS(FileDialog, wxFileDialogBase)

// ----------------------------------------------------------------------------

namespace
{

#if wxUSE_DYNLIB_CLASS

typedef BOOL (WINAPI *GetProcessUserModeExceptionPolicy_t)(LPDWORD);
typedef BOOL (WINAPI *SetProcessUserModeExceptionPolicy_t)(DWORD);

GetProcessUserModeExceptionPolicy_t gs_pfnGetProcessUserModeExceptionPolicy
    = (GetProcessUserModeExceptionPolicy_t) -1;

SetProcessUserModeExceptionPolicy_t gs_pfnSetProcessUserModeExceptionPolicy
    = (SetProcessUserModeExceptionPolicy_t) -1;

DWORD gs_oldExceptionPolicyFlags = 0;

bool gs_changedPolicy = false;

#endif // #if wxUSE_DYNLIB_CLASS

/*
Since Windows 7 by default (callback) exceptions aren't swallowed anymore
with native x64 applications. Exceptions can occur in a file dialog when
using the hook procedure in combination with third-party utilities.
Since Windows 7 SP1 the swallowing of exceptions can be enabled again
by using SetProcessUserModeExceptionPolicy.
*/
void ChangeExceptionPolicy()
{
#if wxUSE_DYNLIB_CLASS
    gs_changedPolicy = false;

    wxLoadedDLL dllKernel32(wxT("kernel32.dll"));

    if ( gs_pfnGetProcessUserModeExceptionPolicy
        == (GetProcessUserModeExceptionPolicy_t) -1)
    {
        wxDL_INIT_FUNC(gs_pfn, GetProcessUserModeExceptionPolicy, dllKernel32);
        wxDL_INIT_FUNC(gs_pfn, SetProcessUserModeExceptionPolicy, dllKernel32);
    }

    if ( !gs_pfnGetProcessUserModeExceptionPolicy
        || !gs_pfnSetProcessUserModeExceptionPolicy
        || !gs_pfnGetProcessUserModeExceptionPolicy(&gs_oldExceptionPolicyFlags) )
    {
        return;
    }

    if ( gs_pfnSetProcessUserModeExceptionPolicy(gs_oldExceptionPolicyFlags
        | 0x1 /* PROCESS_CALLBACK_FILTER_ENABLED */ ) )
    {
        gs_changedPolicy = true;
    }

#endif // wxUSE_DYNLIB_CLASS
}

void RestoreExceptionPolicy()
{
#if wxUSE_DYNLIB_CLASS
    if (gs_changedPolicy)
    {
        gs_changedPolicy = false;
        (void) gs_pfnSetProcessUserModeExceptionPolicy(gs_oldExceptionPolicyFlags);
    }
#endif // wxUSE_DYNLIB_CLASS
}

} // unnamed namespace

// ----------------------------------------------------------------------------
// hook function for moving the dialog
// ----------------------------------------------------------------------------

UINT_PTR APIENTRY FileDialog::ParentHook(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
   OPENFILENAME *pOfn = reinterpret_cast<OPENFILENAME *>(GetWindowLongPtr(hDlg, GWLP_USERDATA));
   return reinterpret_cast<FileDialog *>(pOfn->lCustData)->MSWParentHook(hDlg, iMsg, wParam, lParam, pOfn);
}

UINT_PTR FileDialog::MSWParentHook(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam, OPENFILENAME *pOfn)
{
   // Allow default handling to process first
   UINT_PTR ret = CallWindowProc(mParentProc, hDlg, iMsg, wParam, lParam);

   if (iMsg == WM_SIZE)
   {
      MSWOnSize(mParentDlg, pOfn);
   }

   if (iMsg == WM_GETMINMAXINFO)
   {
      MSWOnGetMinMaxInfo(mParentDlg, pOfn, reinterpret_cast<LPMINMAXINFO>(lParam));
   }

   return ret;
}

void FileDialog::MSWOnSize(HWND hDlg, LPOPENFILENAME pOfn)
{
   wxRect r;
   wxCopyRECTToRect(wxGetClientRect(hDlg), r);

   SetHWND(mChildDlg);

   SetWindowPos(mChildDlg,
                HWND_TOP,
                0,
                0,
                r.GetWidth(),
                r.GetHeight(),
                SWP_NOZORDER | SWP_NOMOVE);

   SetSize(r);

   if (mRoot)
   {
      mRoot->SetSize(r.GetWidth(), mRoot->GetSize().GetHeight());
   }

   SetHWND(NULL);
}

// Provide the minimum size of the dialog
//
// We've captured the full dialog size in MSWOnInitDone() below.  This will be returned
// as the minimum size.
//
// When the user tries to resize the dialog, for some unknown reason the common dialog control
// doesn't let the user resize it smaller than it was the last time the dialog was used.  This
// may be a problem in this code and/or may only be a concern under Windows 10.  Either way, we
// override the minimum size supplied by the common dialog control with our own size here.
void FileDialog::MSWOnGetMinMaxInfo(HWND hwnd, LPOPENFILENAME pOfn, LPMINMAXINFO pMmi)
{
   if (mMinSize.x > 0 && mMinSize.y > 0)
   {
      pMmi->ptMinTrackSize = mMinSize;
   }
}

UINT_PTR APIENTRY FileDialog::DialogHook(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
   OPENFILENAME *pOfn;

   if (iMsg == WM_INITDIALOG)
   {
      pOfn = reinterpret_cast<OPENFILENAME *>(lParam);
   }
   else
   {
      pOfn = reinterpret_cast<OPENFILENAME *>(GetWindowLongPtr(hDlg, GWLP_USERDATA));
   }

   return reinterpret_cast<FileDialog *>(pOfn->lCustData)->MSWDialogHook(hDlg, iMsg, wParam, lParam, pOfn);
}

UINT_PTR FileDialog::MSWDialogHook(HWND hDlg, UINT iMsg, WPARAM WXUNUSED(wParam), LPARAM lParam, OPENFILENAME *pOfn)
{
   switch (iMsg)
   {
      case WM_INITDIALOG:
         MSWOnInitDialog(hDlg, pOfn);
      break;

      case WM_DESTROY:
         MSWOnDestroy(hDlg, pOfn);
      break;

      case WM_NOTIFY:
      {
         NMHDR * const pNM = reinterpret_cast<NMHDR *>(lParam);
         if (pNM->code > CDN_LAST && pNM->code <= CDN_FIRST)
         {
            OFNOTIFY * const pNotifyCode = reinterpret_cast<OFNOTIFY *>(lParam);
            switch (pNotifyCode->hdr.code)
            {
               case CDN_INITDONE:
                  MSWOnInitDone(hDlg, pOfn);
               break;

               case CDN_FOLDERCHANGE:
                  MSWOnFolderChange(hDlg, pOfn);
               break;

               case CDN_SELCHANGE:
                  MSWOnSelChange(hDlg, pOfn);
               break;

               case CDN_TYPECHANGE:
                  MSWOnTypeChange(hDlg, pOfn);
               break;
            }
         }
      }
      break;
   }

   // do the default processing
   return 0;
}

void FileDialog::MSWOnInitDialog(HWND hDlg, LPOPENFILENAME pOfn)
{
   // Since we've specified the OFN_EXPLORER flag, the "real" dialog is the parent of this one
   mParentDlg = ::GetParent(hDlg);

   // Now we can initialize the disabler
   mDisabler.Init(this, mParentDlg);

   // This is the dialog were our controls will go
   mChildDlg = hDlg;

   // Store the OPENFILENAME pointer in each window
   SetWindowLongPtr(mParentDlg, GWLP_USERDATA, reinterpret_cast<LPARAM>(pOfn));
   SetWindowLongPtr(mChildDlg, GWLP_USERDATA, reinterpret_cast<LPARAM>(pOfn));

   // Subclass the parent dialog so we can receive WM_SIZE messages
   mParentProc = reinterpret_cast<WNDPROC>(SetWindowLongPtr(mParentDlg, GWLP_WNDPROC, reinterpret_cast<LPARAM>(&ParentHook)));

   // set HWND for wx
   SetHWND(mChildDlg);

   if (HasUserPaneCreator())
   {
      // Create the root window
      wxBoxSizer *verticalSizer = new wxBoxSizer(wxVERTICAL);
      mRoot = new wxPanel(this, wxID_ANY);
   
      wxPanel *userpane = new wxPanel(mRoot, wxID_ANY);
      CreateUserPane(userpane);

      wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
      horizontalSizer->Add(userpane, 1, wxEXPAND);
      verticalSizer->Add(horizontalSizer, 1, wxEXPAND);

      mRoot->SetSizer(verticalSizer);
      mRoot->Layout();
      mRoot->Fit();

      // This reserves space for the additional panel
      wxSize sz = mRoot->GetBestSize();
      SetWindowPos(mChildDlg,
                   HWND_TOP,
                   0,
                   0,
                   sz.GetWidth(),
                   sz.GetHeight(),
                   SWP_NOZORDER | SWP_NOMOVE);

   }

   SetHWND(NULL);
}

void FileDialog::MSWOnDestroy(HWND hDlg, LPOPENFILENAME WXUNUSED(pOfn))
{
   // Save final dialog position for next time
   wxCopyRECTToRect(wxGetWindowRect(mParentDlg), gs_rectDialog);

   // Must explicitly delete the root window.  Otherwise, wx will try to 
   // destroy it when the FileDialog is deleted.  But, the windows will
   // have already been deleted as a result of the OpenFile dialog being
   // destroyed.
   delete mRoot;
}

void FileDialog::MSWOnInitDone(HWND hDlg, LPOPENFILENAME pOfn)
{
   // set HWND so that our DoMoveWindow() works correctly
   SetHWND(mChildDlg);

   // capture the full initial size of the dialog to use as the minimum size
   RECT r;
   GetWindowRect(mParentDlg, &r);
   mMinSize = {r.right - r.left, r.bottom - r.top};

   if (m_centreDir)
   {
      // now we have the real dialog size, remember it
      RECT rect;
      GetWindowRect(mParentDlg, &rect);
      gs_rectDialog = wxRectFromRECT(rect);

      // and position the window correctly: notice that we must use the base
      // class version as our own doesn't do anything except setting flags
      wxFileDialogBase::DoCentre(m_centreDir);
   }
   else // need to just move it to the correct place
   {
      SetPosition(gs_rectDialog.GetPosition());
   }

   // Filter change event must be sent once initialized
   MSWOnTypeChange(hDlg, pOfn);

   // we shouldn't destroy this HWND
   SetHWND(NULL);
}

void FileDialog::MSWOnFolderChange(HWND hDlg, LPOPENFILENAME pOfn)
{
   FilterFiles(mParentDlg, true);

   wxChar path[wxMAXPATH];
   int result = CommDlg_OpenSave_GetFolderPath(::GetParent(hDlg), path, WXSIZEOF(path));
   if (result < 0 || result > WXSIZEOF(path))
   {
      return;
   }

   m_dir = path;

   wxFileCtrlEvent event(wxEVT_FILECTRL_FOLDERCHANGED, this, GetId());
   event.SetDirectory(m_dir);
   GetEventHandler()->ProcessEvent(event);
}

void FileDialog::MSWOnSelChange(HWND hDlg, LPOPENFILENAME pOfn)
{
   // set HWND for wx
   SetHWND(mChildDlg);

   // Get pointer to the ListView control
   HWND lv = ::GetDlgItem(::GetDlgItem(mParentDlg, lst2), 1);
   if (lv == NULL)
   {
      return;
   }

   wxChar path[wxMAXPATH];
   int result = CommDlg_OpenSave_GetFilePath(::GetParent(hDlg), path, WXSIZEOF(path));
   if (result < 0 || result > WXSIZEOF(path))
   {
      return;
   }

   m_path = path;
   m_fileName = wxFileNameFromPath(m_path);
   m_dir = wxPathOnly(m_path);

   m_fileNames.Clear();
   m_fileNames.Add(m_fileName);

   wxFileCtrlEvent event(wxEVT_FILECTRL_SELECTIONCHANGED, this, GetId());
   event.SetDirectory(m_dir);
   event.SetFiles(m_fileNames);
   GetEventHandler()->ProcessEvent(event);

   // we shouldn't destroy this HWND
   SetHWND(NULL);
}

void FileDialog::MSWOnTypeChange(HWND hDlg, LPOPENFILENAME pOfn)
{
   // set HWND for wx
   SetHWND(mChildDlg);

   ParseFilter(pOfn->nFilterIndex);
   FilterFiles(mParentDlg, true);

   m_filterIndex = pOfn->nFilterIndex - 1;

   wxFileCtrlEvent event(wxEVT_FILECTRL_FILTERCHANGED, this, GetId());
   event.SetFilterIndex(m_filterIndex);
   GetEventHandler()->ProcessEvent(event);

   // we shouldn't destroy this HWND
   SetHWND(NULL);
}

#define WM_GETISHELLBROWSER WM_USER + 7

void FileDialog::FilterFiles(HWND hwnd, bool refresh)
{
   IShellFolder *ishell = NULL;
   IShellBrowser *ishellbrowser = NULL;  // Does not have to be released
   IShellView *ishellview = NULL;
   IFolderView *ifolderview = NULL;
   LPMALLOC imalloc = NULL;
   HRESULT hr;
   
   // Get pointer to the ListView control
   HWND lv = ::GetDlgItem(::GetDlgItem(hwnd, lst2), 1);
   if (lv == NULL)
   {
      return;
   }
   
   // Get shell's memory allocation interface (must be Release()'d)
   hr = SHGetMalloc(&imalloc);
   if ((hr != NOERROR) || (imalloc == NULL))
   {
      wxASSERT((hr == NOERROR) && (imalloc != NULL));
      return;
   }

   // Get IShellBrowser interface for current dialog
   ishellbrowser = (IShellBrowser*)::SendMessage(hwnd, WM_GETISHELLBROWSER, 0, 0);
   if (ishellbrowser)
   {
      // Get IShellBrowser interface for returned browser
      if (ishellbrowser->QueryActiveShellView(&ishellview) == S_OK)
      {
         // Get the IFolderView interface...available on XP or greater
         ishellview->QueryInterface(IID_IFolderView, (void **)&ifolderview);
      }
   }

   // Init
   LVITEM lvi;
   wxZeroMemory(lvi);

   // Process all items
   int fltcnt = (int) m_Filters.GetCount();
   int itmcnt = ListView_GetItemCount(lv);
   for (int itm = 0; itm < itmcnt; itm++)
   {
      // Retrieve the file IDL
      lvi.iItem = itm;
      lvi.mask = LVIF_PARAM;
      if (ListView_GetItem(lv, &lvi) != TRUE)
      {
         wxASSERT(FALSE);
         break;
      }

      LPCITEMIDLIST fidl = (LPCITEMIDLIST) lvi.lParam;

      // On Vista, lParam no longer contains the pidl so retrieve it via the
      // IFolderView interface.  This interface is only available on XP or higher
      // so if that limitation isn't workable, use IShellView::GetItemObject() to
      // retrieve items.
      if (fidl == NULL && ifolderview != NULL)
      {
         ifolderview->Item(itm, (LPITEMIDLIST *) &fidl);
      }

      if (fidl == NULL)
      {
         wxASSERT(fidl != NULL);
         break;
      }

      // Retrieve the IShellFolder interface of the parent (must be Release()'d)
      if (ishell == NULL)
      {
         hr = SHBindToParent(fidl, IID_IShellFolder, (void **)&ishell, NULL);
         if (!SUCCEEDED(hr))
         {
            wxASSERT(SUCCEEDED(hr));
            break;
         }
      }
      
      // Get the attributes of the object
      DWORD attr = SFGAO_FOLDER | SFGAO_BROWSABLE;
      hr = ishell->GetAttributesOf(1, &fidl, &attr);
      if (!SUCCEEDED(hr))
      {
         wxASSERT(SUCCEEDED(hr));
         break;
      }
      
      // Allow all folders (things like zip files get filtered below)
      if ((attr & (SFGAO_FOLDER)) && !(attr & SFGAO_BROWSABLE))
      {
         continue;
      }
      
      // Retrieve the parsable name of the object (includes extension)
      STRRET str;
      hr = ishell->GetDisplayNameOf(fidl, SHGDN_INFOLDER | SHGDN_FORPARSING, &str);
      if (hr != NOERROR)
      {
         // For some objects, we get back an error of 80070057.  I'm assuming this
         // means there is no way to represent the name (like some sort of virtual name)
         // or I've not used the correct PIDL.  But, in either case, it "probably"
         // represents some sort of folder (at least in all cases I've seen), so we
         // simply allow it to display.
         continue;
      }
      
      // Convert result to wxString
      wxString filename;
      switch (str.uType)
      {
         case STRRET_WSTR:
            filename = str.pOleStr;
            imalloc->Free(str.pOleStr);
            break;
            
         case STRRET_OFFSET:
            filename = wxString(((char *)fidl) + str.uOffset, wxConvISO8859_1);
            break;
            
         case STRRET_CSTR:
            filename = wxString(str.cStr, wxConvISO8859_1);
            break;
      }
      
      // Convert the filename to lowercase (and remember to write filters in lowercase!)
      filename = filename.Lower();

      // Attempt to match it to all of our filters
      bool match = false;
      for (int flt = 0; flt < fltcnt; flt++)
      {
         if (wxMatchWild(m_Filters[flt], filename, false))
         {
            match = true;
            break;
         }
      }
      
      // Remove it from the display if it didn't match any of the filters.
      if (!match)
      {
         ListView_DeleteItem(lv, itm);
         itm--;
         itmcnt--;
      }
   }

   // On Vista and maybe XP, we seem to need to refresh the view after
   // changing the filters.  But, only refresh for type changes and not
   // selection changes since it causes additional selection change
   // events to occur.
   if (ishellview && refresh)
   {
      ishellview->Refresh();
   }

   // Release the interface
   if (ifolderview)
   {
      ifolderview->Release();
   }

   // Release the interface
   if (ishellview)
   {
      ishellview->Release();
   }

   // Release the interface
   if (ishell)
   {
      ishell->Release();
   }
   
   // Release the interface
   if (imalloc)
   {
      imalloc->Release();
   }
}

void FileDialog::ParseFilter(int index)
{
   m_Filters.Empty();
   
   wxStringTokenizer tokenWild(m_FilterGroups[index - 1], wxT(";"));
   
   while (tokenWild.HasMoreTokens())
   {
      wxString token = tokenWild.GetNextToken();
      if (m_Filters.Index(token, false) == wxNOT_FOUND)
      {
         m_Filters.Add(token);
      }
   }
}

// ----------------------------------------------------------------------------
// FileDialog
// ----------------------------------------------------------------------------

FileDialog::FileDialog()
:   FileDialogBase()
{
    Init();
}

FileDialog::FileDialog(wxWindow *parent,
                       const wxString& message,
                       const wxString& defaultDir,
                       const wxString& defaultFile,
                       const wxString& wildCard,
                       long style,
                       const wxPoint& pos,
                       const wxSize& sz,
                       const wxString& name)
:   FileDialogBase()
{
    Init();

    FileDialogBase::Create(parent,message,defaultDir,defaultFile,wildCard,style,pos,sz,name);
}

void FileDialog::Init()
{
   mRoot = NULL;
   mMinSize = {0, 0};

   // NB: all style checks are done by wxFileDialogBase::Create

   m_bMovedWindow = false;
   m_centreDir = 0;

   // Must set to zero, otherwise the wx routines won't size the window
   // the second time you call the file dialog, because it thinks it is
   // already at the requested size.. (when centering)
   gs_rectDialog.x =
   gs_rectDialog.y = 0;
}

void FileDialog::GetPaths(wxArrayString& paths) const
{
   paths.Empty();
   
   wxString dir(m_dir);
   if (m_dir.empty() || m_dir.Last() != wxT('\\'))
      dir += wxT('\\');
   
   size_t count = m_fileNames.GetCount();
   for (size_t n = 0; n < count; n++)
   {
      if (wxFileName(m_fileNames[n]).IsAbsolute())
         paths.Add(m_fileNames[n]);
      else
         paths.Add(dir + m_fileNames[n]);
   }
}

void FileDialog::GetFilenames(wxArrayString& files) const
{
   files = m_fileNames;
}

void FileDialog::DoGetPosition( int *x, int *y ) const
{
   if (x)
      *x = gs_rectDialog.x;
   if (y)
      *y = gs_rectDialog.y;
}

void FileDialog::DoGetSize(int *width, int *height) const
{
   if (width)
      *width = gs_rectDialog.width;
   if (height)
      *height = gs_rectDialog.height;
}

void FileDialog::DoMoveWindow(int x, int y, int WXUNUSED(width), int WXUNUSED(height))
{
   m_bMovedWindow = true;
   
   gs_rectDialog.x = x;
   gs_rectDialog.y = y;
   
   // our HWND is only set when we're called from MSWOnInitDone(), test if
   // this is the case
   HWND hwnd = GetHwnd();
   if (hwnd)
   {
      // size of the dialog can't be changed because the controls are not
      // laid out correctly then
      ::SetWindowPos(hwnd, HWND_TOP, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
   }
   else // just remember that we were requested to move the window
   {
      m_bMovedWindow = true;

      // if Centre() had been called before, it shouldn't be taken into
      // account now
      m_centreDir = 0;
   }
}

void FileDialog::DoCentre(int dir)
{
   m_centreDir = dir;
   m_bMovedWindow = true;

   // it's unnecessary to do anything else at this stage as we'll redo it in
   // MSWOnInitDone() anyhow
}

// helper used below in ShowCommFileDialog(): style is used to determine
// whether to show the "Save file" dialog (if it contains wxFD_SAVE bit) or
// "Open file" one; returns true on success or false on failure in which case
// err is filled with the CDERR_XXX constant
static bool DoShowCommFileDialog(OPENFILENAME *of, long style, DWORD *err)
{
   if (style & wxFD_SAVE ? GetSaveFileName(of) : GetOpenFileName(of))
      return true;

   if (err)
   {
      *err = CommDlgExtendedError();
   }

   return false;
}

// We want to use OPENFILENAME struct version 5 (Windows 2000/XP) but we don't
// know if the OPENFILENAME declared in the currently used headers is a V5 or
// V4 (smaller) one so we try to manually extend the struct in case it is the
// old one.
//
// We don't do this on Windows CE nor under Win64, however, as there are no
// compilers with old headers for these architectures
#if defined(__WXWINCE__) || defined(__WIN64__)
typedef OPENFILENAME wxOPENFILENAME;

static const DWORD gs_ofStructSize = sizeof(OPENFILENAME);
#else // !__WXWINCE__ || __WIN64__
#define wxTRY_SMALLER_OPENFILENAME

struct wxOPENFILENAME : public OPENFILENAME
{
   // fields added in Windows 2000/XP comdlg32.dll version
   void *pVoid;
   DWORD dw1;
   DWORD dw2;
};

// hardcoded sizeof(OPENFILENAME) in the Platform SDK: we have to do it
// because sizeof(OPENFILENAME) in the headers we use when compiling the
// library could be less if _WIN32_WINNT is not >= 0x500
static const DWORD wxOPENFILENAME_V5_SIZE = 88;

// this is hardcoded sizeof(OPENFILENAME_NT4) from Platform SDK
static const DWORD wxOPENFILENAME_V4_SIZE = 76;

// always try the new one first
static DWORD gs_ofStructSize = wxOPENFILENAME_V5_SIZE;
#endif // __WXWINCE__ || __WIN64__/!...

static bool ShowCommFileDialog(OPENFILENAME *of, long style)
{
   DWORD errCode;
   bool success = DoShowCommFileDialog(of, style, &errCode);

#ifdef wxTRY_SMALLER_OPENFILENAME
   // the system might be too old to support the new version file dialog
   // boxes, try with the old size
   if (!success && errCode == CDERR_STRUCTSIZE &&
      of->lStructSize != wxOPENFILENAME_V4_SIZE)
   {
      of->lStructSize = wxOPENFILENAME_V4_SIZE;

      success = DoShowCommFileDialog(of, style, &errCode);

      if (success || !errCode)
      {
         // use this struct size for subsequent dialogs
         gs_ofStructSize = of->lStructSize;
      }
   }
#endif // wxTRY_SMALLER_OPENFILENAME

   if (!success && errCode == FNERR_INVALIDFILENAME && of->lpstrFile[0])
   {
      // this can happen if the default file name is invalid, try without it
      // now
      of->lpstrFile[0] = wxT('\0');
      success = DoShowCommFileDialog(of, style, &errCode);
   }

   if (!success)
   {
      // common dialog failed - why?
      if (errCode != 0)
      {
         wxLogError(_("File dialog failed with error code %0lx."), errCode);
      }
      //else: it was just cancelled

      return false;
   }

   return true;
}

int FileDialog::ShowModal()
{
   WX_HOOK_MODAL_DIALOG();

   HWND hWnd = 0;
   if (m_parent) hWnd = (HWND) m_parent->GetHWND();
   if (!hWnd && wxTheApp->GetTopWindow())
      hWnd = (HWND) wxTheApp->GetTopWindow()->GetHWND();
   
   static wxChar fileNameBuffer [ wxMAXPATH ];           // the file-name
   wxChar        titleBuffer    [ wxMAXFILE+1+wxMAXEXT ];  // the file-name, without path
   
   *fileNameBuffer = wxT('\0');
   *titleBuffer    = wxT('\0');
   
   // We always need EXPLORER and ENABLEHOOK to use our filtering code
   DWORD msw_flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING | OFN_ENABLETEMPLATEHANDLE;

   if (HasFdFlag(wxFD_FILE_MUST_EXIST))
      msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

   /*
      If the window has been moved the programmer is probably
      trying to center or position it.  Thus we set the callback
      or hook function so that we can actually adjust the position.
      Without moving or centering the dlg, it will just stay
      in the upper left of the frame, it does not center
      automatically.
   */
   if (m_bMovedWindow || HasExtraControlCreator()) // we need these flags.
   {
      ChangeExceptionPolicy();
      msw_flags |= OFN_EXPLORER|OFN_ENABLEHOOK;
#ifndef __WXWINCE__
      msw_flags |= OFN_ENABLESIZING;
#endif
   }

   wxON_BLOCK_EXIT0(RestoreExceptionPolicy);

   if (HasFdFlag(wxFD_MULTIPLE))
   {
      // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT
      msw_flags |= OFN_EXPLORER | OFN_ALLOWMULTISELECT;
   }

   // if wxCHANGE_DIR flag is not given we shouldn't change the CWD which the
   // standard dialog does by default (notice that under NT it does it anyhow, 
   // OFN_NOCHANGEDIR or not, see below)
   if (!HasFdFlag(wxFD_CHANGE_DIR))
   {
      msw_flags |= OFN_NOCHANGEDIR;
   }
   
   if (HasFdFlag(wxFD_OVERWRITE_PROMPT))
   {
      msw_flags |= OFN_OVERWRITEPROMPT;
   }
   
   // Define a dummy dialog box template
   GlobalPtr hgbl(256, GMEM_ZEROINIT);
   GlobalPtrLock hgblLock(hgbl);
   LPDLGTEMPLATE lpdt = static_cast<LPDLGTEMPLATE>((void *)hgblLock);
   lpdt->style = DS_CONTROL | WS_CHILD | WS_CLIPSIBLINGS;
   lpdt->cdit = 0;         // Number of controls
   lpdt->x = 0;
   lpdt->y = 0;
   lpdt->cx = 0;
   lpdt->cy = 0;

   OPENFILENAME of;
   wxZeroMemory(of);
   
   // Allow Places bar to show on supported platforms
   of.lStructSize       = sizeof(OPENFILENAME);
   of.hwndOwner         = hWnd;
   of.lpstrTitle        = m_message.t_str();
   of.lpstrFileTitle    = titleBuffer;
   of.nMaxFileTitle     = wxMAXFILE + 1 + wxMAXEXT;
   of.hInstance         = (HINSTANCE) lpdt;

   // Convert forward slashes to backslashes (file selector doesn't like
   // forward slashes) and also squeeze multiple consecutive slashes into one
   // as it doesn't like two backslashes in a row neither
   
   wxString  dir;
   size_t    i, len = m_dir.length();
   dir.reserve(len);
   for ( i = 0; i < len; i++ )
   {
      wxChar ch = m_dir[i];
      switch ( ch )
      {
         case wxT('/'):
            // convert to backslash
            ch = wxT('\\');
            
            // fall through
            
         case wxT('\\'):
            while ( i < len - 1 )
            {
               wxChar chNext = m_dir[i + 1];
               if ( chNext != wxT('\\') && chNext != wxT('/') )
                  break;
               
               // ignore the next one, unless it is at the start of a UNC path
               if (i > 0)
                  i++;
               else
                  break;
            }
            // fall through
            
            default:
            // normal char
            dir += ch;
      }
   }
   
   of.lpstrInitialDir   = dir.c_str();
   
   of.Flags             = msw_flags;
   of.lpfnHook          = DialogHook;
   of.lCustData         = (LPARAM) this;

   wxArrayString wildDescriptions;
   
   size_t items = wxParseCommonDialogsFilter(m_wildCard, wildDescriptions, m_FilterGroups);
   
   wxASSERT_MSG( items > 0 , wxT("empty wildcard list") );
   
   wxString filterBuffer;
   
   for (i = 0; i < items ; i++)
   {
      filterBuffer += wildDescriptions[i];
      filterBuffer += wxT("|");
      filterBuffer += m_FilterGroups[i];
      filterBuffer += wxT("|");
   }
   
   // Replace | with \0
   for (i = 0; i < filterBuffer.Len(); i++ )
   {
      if ( filterBuffer.GetChar(i) == wxT('|') )
      {
         filterBuffer[i] = wxT('\0');
      }
   }
   
   of.lpstrFilter  = (LPCTSTR)filterBuffer.c_str();
   of.nFilterIndex = m_filterIndex + 1;
   
   ParseFilter(of.nFilterIndex);
   
   //=== Setting defaultFileName >>=========================================
   
   wxStrlcpy(fileNameBuffer, m_fileName.c_str(), WXSIZEOF(fileNameBuffer));
   
   of.lpstrFile = fileNameBuffer;  // holds returned filename
   of.nMaxFile  = wxMAXPATH;
   
   // we must set the default extension because otherwise Windows would check
   // for the existing of a wrong file with wxOVERWRITE_PROMPT (i.e. if the
   // user types "foo" and the default extension is ".bar" we should force it
   // to check for "foo.bar" existence and not "foo")
   wxString defextBuffer; // we need it to be alive until GetSaveFileName()!
   if (HasFdFlag(wxFD_SAVE))
   {
      const wxChar* extension = filterBuffer;
      int maxFilter = (int)(of.nFilterIndex*2L) - 1;
      
      for( int i = 0; i < maxFilter; i++ )           // get extension
         extension = extension + wxStrlen( extension ) + 1;
      
      // use dummy name a to avoid assert in AppendExtension
      defextBuffer = AppendExtension(wxT("a"), extension);
      if (defextBuffer.StartsWith(wxT("a.")))
      {
         defextBuffer = defextBuffer.Mid(2); // remove "a."
         of.lpstrDefExt = defextBuffer.c_str();
      }
   }
   
   // store off before the standard windows dialog can possibly change it 
   const wxString cwdOrig = wxGetCwd(); 
   
   //== Execute FileDialog >>=================================================

   if (!ShowCommFileDialog(&of, m_windowStyle))
      return wxID_CANCEL;

   // GetOpenFileName will always change the current working directory on
   // (according to MSDN) "Windows NT 4.0/2000/XP" because the flag
   // OFN_NOCHANGEDIR has no effect.  If the user did not specify
   // wxFD_CHANGE_DIR let's restore the current working directory to what it
   // was before the dialog was shown.
   if (msw_flags & OFN_NOCHANGEDIR)
   {
      wxSetWorkingDirectory(cwdOrig);
   }

   m_fileNames.Empty();
      
   if ((HasFdFlag(wxFD_MULTIPLE)) &&
#if defined(OFN_EXPLORER)
         ( fileNameBuffer[of.nFileOffset-1] == wxT('\0') )
#else
         ( fileNameBuffer[of.nFileOffset-1] == wxT(' ') )
#endif // OFN_EXPLORER
         )
   {
#if defined(OFN_EXPLORER)
      m_dir = fileNameBuffer;
      i = of.nFileOffset;
      m_fileName = &fileNameBuffer[i];
      m_fileNames.Add(m_fileName);
      i += m_fileName.Len() + 1;
         
      while (fileNameBuffer[i] != wxT('\0'))
      {
         m_fileNames.Add(&fileNameBuffer[i]);
         i += wxStrlen(&fileNameBuffer[i]) + 1;
      }
#else
      wxStringTokenizer toke(fileNameBuffer, wxT(" \t\r\n"));
      m_dir = toke.GetNextToken();
      m_fileName = toke.GetNextToken();
      m_fileNames.Add(m_fileName);
         
      while (toke.HasMoreTokens())
         m_fileNames.Add(toke.GetNextToken());
#endif // OFN_EXPLORER
         
      wxString dir(m_dir);
      if ( m_dir.Last() != wxT('\\') )
         dir += wxT('\\');
         
      m_path = dir + m_fileName;
      m_filterIndex = (int)of.nFilterIndex - 1;
   }
   else
   {
      //=== Adding the correct extension >>=================================
      m_filterIndex = (int)of.nFilterIndex - 1;

      if ( !of.nFileExtension ||
            (of.nFileExtension && fileNameBuffer[of.nFileExtension] == wxT('\0')) )
      {
         // User has typed a filename without an extension:
         const wxChar* extension = filterBuffer;
         int   maxFilter = (int)(of.nFilterIndex*2L) - 1;
         
         for( int i = 0; i < maxFilter; i++ )           // get extension
            extension = extension + wxStrlen( extension ) + 1;
         
         m_fileName = AppendExtension(fileNameBuffer, extension);
         wxStrlcpy(fileNameBuffer, m_fileName.c_str(), WXSIZEOF(fileNameBuffer));
      }

      m_path = fileNameBuffer;
      m_fileName = wxFileNameFromPath(fileNameBuffer);
      m_fileNames.Add(m_fileName);
      m_dir = wxPathOnly(fileNameBuffer);
   }
   
   return wxID_OK;
}

FileDialog::Disabler::Disabler()
{
   mRoot = NULL;
   mHwnd = (HWND) INVALID_HANDLE_VALUE;
   mModalCount = 0;

   Register();
}

void FileDialog::Disabler::Init(wxWindow *root, HWND hwnd)
{
   mRoot = root;
   mHwnd = hwnd;
}

int FileDialog::Disabler::Enter(wxDialog *dialog)
{
   if (mHwnd != (HWND) INVALID_HANDLE_VALUE)
   {
      if (IsChild(dialog)) {
         ::EnableWindow(mHwnd, FALSE);
         mModalCount++;
      }
   }

   return wxID_NONE;
}

void FileDialog::Disabler::Exit(wxDialog *dialog)
{
   if (mHwnd != (HWND) INVALID_HANDLE_VALUE)
   {
      if (IsChild(dialog))
      {
         mModalCount--;
         if (mModalCount == 0)
         {
            ::EnableWindow(mHwnd, TRUE);
            ::SetWindowPos(mHwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
         }
      }
   }
}

bool FileDialog::Disabler::IsChild(const wxDialog *dialog) const
{
   if (!dialog)
   {
      return false;
   }

   for (const wxWindow *w = dialog->GetParent(); w != NULL; w = w->GetParent())
   {
      if (w == mRoot)
      {
         return true;
      }
   }

   return false;
}