Tk Source Code

Check-in [92b2061e]
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Implemented Vista+ tk_chooseDirectory dialogs
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apn-win-filedialogs
Files: files | file ages | folders
SHA1: 92b2061e1b77f2876598e3d8b39a83a8776d5a39
User & Date: ashok 2014-09-17 17:18:53
Context
2014-09-20
02:56
Make -xpstyle a hidden option check-in: e162b207 user: ashok tags: apn-win-filedialogs
2014-09-17
17:18
Implemented Vista+ tk_chooseDirectory dialogs check-in: 92b2061e user: ashok tags: apn-win-filedialogs
2014-09-14
17:32
Implemented -filetypes and -typevariable for new Vista file dialogs. check-in: d988edef user: ashok tags: apn-win-filedialogs
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to win/tkWinDialog.c.

175
176
177
178
179
180
181
182

183
184
185
186
187
188
189
190
191








192
193
194
195
196
197
198
...
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
...
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
....
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
....
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
....
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240

1241
1242
1243
1244
1245
1246
1247
....
1264
1265
1266
1267
1268
1269
1270









1271
1272






1273
1274
1275
1276
1277
1278
1279
....
1338
1339
1340
1341
1342
1343
1344






1345
1346
1347
1348
1349
1350
1351
....
1425
1426
1427
1428
1429
1430
1431
1432

1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443

1444
1445
1446
1447


1448
1449
1450
1451
1452
1453
1454
....
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500










1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513







1514
1515
1516
1517
1518
1519
1520
....
1558
1559
1560
1561
1562
1563
1564

1565


1566
1567
1568
1569
1570
1571
1572
1573
1574
....
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
....
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
....
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
....
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
....
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526

2527


2528
2529

2530
2531
2532
2533
2534



2535
2536




2537
2538
2539
2540

2541
2542
2543
2544
2545
2546
2547
2548

2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600

2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
....
2655
2656
2657
2658
2659
2660
2661




2662
2663
2664
2665
2666
2667
2668
....
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
    Tcl_Obj *extObj;            /* Default extension */
    Tcl_Obj *titleObj;          /* Title for dialog */
    Tcl_Obj *filterObj;         /* File type filter list */
    Tcl_Obj *typeVariableObj;   /* Variable in which to store type selected */
    Tcl_Obj *initialTypeObj;    /* Initial value of above, or NULL */
    Tcl_DString utfDirString;   /* Initial dir */
    int multi;                  /* Multiple selection enabled */
    int confirmOverwrite;       /* Multiple selection enabled */

    int forceXPStyle;          /* XXX - Force XP style even on newer systems */
    TCHAR file[TK_MULTI_MAX_PATH]; /* File name
                                      XXX - fixed size because it was so
                                      historically. Why not malloc'ed ?
                                      XXX - also, TCHAR should really be WCHAR
                                      because TkWinGetUnicodeEncoding is always
                                      UCS2.
                                   */
} OFNOpts;









/*
 * The following definitions are required when using older versions of
 * Visual C++ (like 6.0) and possibly MingW. Those headers do not contain
 * required definitions for interfaces new to Vista that we need for
 * the new file dialogs. Duplicating definitions is OK because they
 * should forever remain unchanged.
................................................................................
} SIATTRIBFLAGS;
typedef struct IShellItemArray IShellItemArray;
typedef struct IShellItemArrayVtbl
{
    BEGIN_INTERFACE
        
    HRESULT ( STDMETHODCALLTYPE *QueryInterface )( 
        IShellItemArray * This,
        /* [in] */  REFIID riid,
        /* [annotation][iid_is][out] */ 
        void **ppvObject);
        
    ULONG ( STDMETHODCALLTYPE *AddRef )( 
         IShellItemArray * This);
        
    ULONG ( STDMETHODCALLTYPE *Release )( 
         IShellItemArray * This);
        
    HRESULT ( STDMETHODCALLTYPE *BindToHandler )( 
         IShellItemArray * This,
        /* [unique][in] */ IBindCtx *pbc,
        /* [in] */  REFGUID bhid,
        /* [in] */  REFIID riid,
        /* [iid_is][out] */ void **ppvOut);
        


    HRESULT ( STDMETHODCALLTYPE *GetPropertyStore )( 
         IShellItemArray * This,
         /* Actually enum GETPROPERTYSTOREFLAGS, but we do not use this call */
        /* [in] */ int flags,
        /* [in] */  REFIID riid,
        /* [iid_is][out] */ void **ppv);
        

    HRESULT ( STDMETHODCALLTYPE *GetPropertyDescriptionList )( 
         IShellItemArray * This,
         /* Actually REFPROPERTYKEY, but this call is not used */
        /* [in] */   void* keyType,
        /* [in] */  REFIID riid,
        /* [iid_is][out] */  void **ppv);
        
    HRESULT ( STDMETHODCALLTYPE *GetAttributes )( 
         IShellItemArray * This,
        /* [in] */ SIATTRIBFLAGS AttribFlags,
        /* [in] */ SFGAOF sfgaoMask,
        /* [out] */ SFGAOF *psfgaoAttribs);
        
    HRESULT ( STDMETHODCALLTYPE *GetCount )( 
         IShellItemArray * This,
        /* [out] */ DWORD *pdwNumItems);
        
    HRESULT ( STDMETHODCALLTYPE *GetItemAt )( 
         IShellItemArray * This,
        /* [in] */ DWORD dwIndex,
        /* [out] */ IShellItem **ppsi);
        

    HRESULT ( STDMETHODCALLTYPE *EnumItems )( 
         IShellItemArray * This,
         /* Actually IEnumShellItems **, but we do not use this call */
        /* [out] */ void **ppenumShellItems);
        
    END_INTERFACE
} IShellItemArrayVtbl;

struct IShellItemArray
{
    CONST_VTBL struct IShellItemArrayVtbl *lpVtbl;
};

#endif /* __IShellItemArray_INTERFACE_DEFINED__ */

#ifndef __IFileDialog_INTERFACE_DEFINED__

................................................................................

typedef struct IFileDialog IFileDialog;
typedef struct IFileDialogVtbl
{
    BEGIN_INTERFACE
        
    HRESULT ( STDMETHODCALLTYPE *QueryInterface )( 
         IFileDialog * This,
        /* [in] */  REFIID riid,
        /* [annotation][iid_is][out] */ 
          void **ppvObject);
        
    ULONG ( STDMETHODCALLTYPE *AddRef )( 
         IFileDialog * This);
        
    ULONG ( STDMETHODCALLTYPE *Release )( 
         IFileDialog * This);
        
    /* [local] */ HRESULT ( STDMETHODCALLTYPE *Show )( 
        IFileDialog * This,
        /* [annotation][unique][in] */ 
        HWND hwndOwner);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileTypes )( 
         IFileDialog * This,
        /* [in] */ UINT cFileTypes,
        /* [size_is][in] */ const COMDLG_FILTERSPEC *rgFilterSpec);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileTypeIndex )( 
         IFileDialog * This,
        /* [in] */ UINT iFileType);
        
    HRESULT ( STDMETHODCALLTYPE *GetFileTypeIndex )( 
         IFileDialog * This,
        /* [out] */  UINT *piFileType);
        
    HRESULT ( STDMETHODCALLTYPE *Advise )( 
         IFileDialog * This,
         /* XXX - Actually pfde is IFileDialogEvents* but we do not use
            this call and do not want to define IFileDialogEvents as that
            pulls in a whole bunch of other stuff. */
        /* [in] */  void *pfde,
        /* [out] */  DWORD *pdwCookie);
        
    HRESULT ( STDMETHODCALLTYPE *Unadvise )( 
         IFileDialog * This,
        /* [in] */ DWORD dwCookie);
        

    HRESULT ( STDMETHODCALLTYPE *SetOptions )( 
         IFileDialog * This,
        /* [in] */ FILEOPENDIALOGOPTIONS fos);
        
    HRESULT ( STDMETHODCALLTYPE *GetOptions )( 
         IFileDialog * This,
        /* [out] */  FILEOPENDIALOGOPTIONS *pfos);
        
    HRESULT ( STDMETHODCALLTYPE *SetDefaultFolder )( 
         IFileDialog * This,
        /* [in] */  IShellItem *psi);
        
    HRESULT ( STDMETHODCALLTYPE *SetFolder )( 
         IFileDialog * This,
        /* [in] */  IShellItem *psi);
        
    HRESULT ( STDMETHODCALLTYPE *GetFolder )( 
         IFileDialog * This,
        /* [out] */  IShellItem **ppsi);
        
    HRESULT ( STDMETHODCALLTYPE *GetCurrentSelection )( 
         IFileDialog * This,
        /* [out] */  IShellItem **ppsi);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileName )( 
         IFileDialog * This,
        /* [string][in] */  LPCWSTR pszName);
        
    HRESULT ( STDMETHODCALLTYPE *GetFileName )( 
         IFileDialog * This,
        /* [string][out] */  LPWSTR *pszName);
        
    HRESULT ( STDMETHODCALLTYPE *SetTitle )( 
         IFileDialog * This,
        /* [string][in] */  LPCWSTR pszTitle);
        
    HRESULT ( STDMETHODCALLTYPE *SetOkButtonLabel )( 
         IFileDialog * This,
        /* [string][in] */  LPCWSTR pszText);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileNameLabel )( 
         IFileDialog * This,
        /* [string][in] */  LPCWSTR pszLabel);
        
    HRESULT ( STDMETHODCALLTYPE *GetResult )( 
         IFileDialog * This,
        /* [out] */  IShellItem **ppsi);
        
    HRESULT ( STDMETHODCALLTYPE *AddPlace )( 
         IFileDialog * This,
        /* [in] */  IShellItem *psi,
        /* [in] */ FDAP fdap);
        
    HRESULT ( STDMETHODCALLTYPE *SetDefaultExtension )( 
         IFileDialog * This,
        /* [string][in] */  LPCWSTR pszDefaultExtension);
        
    HRESULT ( STDMETHODCALLTYPE *Close )( 
         IFileDialog * This,
        /* [in] */ HRESULT hr);
        
    HRESULT ( STDMETHODCALLTYPE *SetClientGuid )( 
         IFileDialog * This,
        /* [in] */  REFGUID guid);
        
    HRESULT ( STDMETHODCALLTYPE *ClearClientData )( 
         IFileDialog * This);
        
    HRESULT ( STDMETHODCALLTYPE *SetFilter )( 
         IFileDialog * This,
         /* XXX - Actually IShellItemFilter. But deprecated in Win7 AND we do
            not use it anyways. So define as void* */

        /* [in] */  void *pFilter);
        
    END_INTERFACE
} IFileDialogVtbl;

struct IFileDialog {
    CONST_VTBL struct IFileDialogVtbl *lpVtbl;
};
................................................................................


typedef struct IFileSaveDialog IFileSaveDialog;
typedef struct IFileSaveDialogVtbl {
    BEGIN_INTERFACE
        
    HRESULT ( STDMETHODCALLTYPE *QueryInterface )( 
         IFileSaveDialog * This,
        /* [in] */  REFIID riid,
        /* [annotation][iid_is][out] */ 
          void **ppvObject);
        
    ULONG ( STDMETHODCALLTYPE *AddRef )( 
         IFileSaveDialog * This);
        
    ULONG ( STDMETHODCALLTYPE *Release )( 
         IFileSaveDialog * This);
        
    /* [local] */ HRESULT ( STDMETHODCALLTYPE *Show )( 
        IFileSaveDialog * This,
        /* [annotation][unique][in] */ 
        HWND hwndOwner);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileTypes )( 
         IFileSaveDialog * This,
        /* [in] */ UINT cFileTypes,
        /* [size_is][in] */ const COMDLG_FILTERSPEC *rgFilterSpec);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileTypeIndex )( 
         IFileSaveDialog * This,
        /* [in] */ UINT iFileType);
        
    HRESULT ( STDMETHODCALLTYPE *GetFileTypeIndex )( 
         IFileSaveDialog * This,
        /* [out] */  UINT *piFileType);
        

    HRESULT ( STDMETHODCALLTYPE *Advise )( 
         IFileSaveDialog * This,
         /* XXX - Actually pfde is IFileSaveDialogEvents* but we do not use
            this call and do not want to define IFileSaveDialogEvents as that
            pulls in a whole bunch of other stuff. */
        /* [in] */  void *pfde,
        /* [out] */  DWORD *pdwCookie);
        
    HRESULT ( STDMETHODCALLTYPE *Unadvise )( 
         IFileSaveDialog * This,
        /* [in] */ DWORD dwCookie);
        
    HRESULT ( STDMETHODCALLTYPE *SetOptions )( 
         IFileSaveDialog * This,
        /* [in] */ FILEOPENDIALOGOPTIONS fos);
        
    HRESULT ( STDMETHODCALLTYPE *GetOptions )( 
         IFileSaveDialog * This,
        /* [out] */  FILEOPENDIALOGOPTIONS *pfos);
        
    HRESULT ( STDMETHODCALLTYPE *SetDefaultFolder )( 
         IFileSaveDialog * This,
        /* [in] */  IShellItem *psi);
        
    HRESULT ( STDMETHODCALLTYPE *SetFolder )( 
         IFileSaveDialog * This,
        /* [in] */  IShellItem *psi);
        
    HRESULT ( STDMETHODCALLTYPE *GetFolder )( 
         IFileSaveDialog * This,
        /* [out] */  IShellItem **ppsi);
        
    HRESULT ( STDMETHODCALLTYPE *GetCurrentSelection )( 
         IFileSaveDialog * This,
        /* [out] */  IShellItem **ppsi);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileName )( 
         IFileSaveDialog * This,
        /* [string][in] */  LPCWSTR pszName);
        
    HRESULT ( STDMETHODCALLTYPE *GetFileName )( 
         IFileSaveDialog * This,
        /* [string][out] */  LPWSTR *pszName);
        
    HRESULT ( STDMETHODCALLTYPE *SetTitle )( 
         IFileSaveDialog * This,
        /* [string][in] */  LPCWSTR pszTitle);
        
    HRESULT ( STDMETHODCALLTYPE *SetOkButtonLabel )( 
         IFileSaveDialog * This,
        /* [string][in] */  LPCWSTR pszText);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileNameLabel )( 
         IFileSaveDialog * This,
        /* [string][in] */  LPCWSTR pszLabel);
        
    HRESULT ( STDMETHODCALLTYPE *GetResult )( 
         IFileSaveDialog * This,
        /* [out] */  IShellItem **ppsi);
        
    HRESULT ( STDMETHODCALLTYPE *AddPlace )( 
         IFileSaveDialog * This,
        /* [in] */  IShellItem *psi,
        /* [in] */ FDAP fdap);
        
    HRESULT ( STDMETHODCALLTYPE *SetDefaultExtension )( 
         IFileSaveDialog * This,
        /* [string][in] */  LPCWSTR pszDefaultExtension);
        
    HRESULT ( STDMETHODCALLTYPE *Close )( 
         IFileSaveDialog * This,
        /* [in] */ HRESULT hr);
        
    HRESULT ( STDMETHODCALLTYPE *SetClientGuid )( 
         IFileSaveDialog * This,
        /* [in] */  REFGUID guid);
        
    HRESULT ( STDMETHODCALLTYPE *ClearClientData )( 
         IFileSaveDialog * This);
        

    HRESULT ( STDMETHODCALLTYPE *SetFilter )( 
         IFileSaveDialog * This,
         /* XXX - Actually IShellItemFilter. But deprecated in Win7 AND we do
            not use it anyways. So define as void* */
        /* [in] */  void *pFilter);
        
    HRESULT ( STDMETHODCALLTYPE *SetSaveAsItem )( 
        IFileSaveDialog * This,
        /* [in] */ IShellItem *psi);
        
    HRESULT ( STDMETHODCALLTYPE *SetProperties )( 
        IFileSaveDialog * This,
        /* [in] */ IPropertyStore *pStore);
        
    HRESULT ( STDMETHODCALLTYPE *SetCollectedProperties )( 
        IFileSaveDialog * This,
        /* [in] */ IPropertyDescriptionList *pList,
        /* [in] */ BOOL fAppendDefault);
        
    HRESULT ( STDMETHODCALLTYPE *GetProperties )( 
        IFileSaveDialog * This,
        /* [out] */ IPropertyStore **ppStore);
        
    HRESULT ( STDMETHODCALLTYPE *ApplyProperties )( 
        IFileSaveDialog * This,
        /* [in] */  IShellItem *psi,
        /* [in] */  IPropertyStore *pStore,
            /* [unique][in] */  HWND hwnd,
            /* [unique][in] */  IFileOperationProgressSink *pSink);

    END_INTERFACE

} IFileSaveDialogVtbl;

struct IFileSaveDialog {
    CONST_VTBL struct IFileSaveDialogVtbl *lpVtbl;
................................................................................
};

typedef struct IFileOpenDialog IFileOpenDialog;
typedef struct IFileOpenDialogVtbl {
    BEGIN_INTERFACE
        
    HRESULT ( STDMETHODCALLTYPE *QueryInterface )( 
         IFileOpenDialog * This,
        /* [in] */  REFIID riid,
        /* [annotation][iid_is][out] */ 
          void **ppvObject);
        
    ULONG ( STDMETHODCALLTYPE *AddRef )( 
         IFileOpenDialog * This);
        
    ULONG ( STDMETHODCALLTYPE *Release )( 
         IFileOpenDialog * This);
        
    /* [local] */ HRESULT ( STDMETHODCALLTYPE *Show )( 
        IFileOpenDialog * This,
        /* [annotation][unique][in] */ 
        HWND hwndOwner);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileTypes )( 
         IFileOpenDialog * This,
        /* [in] */ UINT cFileTypes,
        /* [size_is][in] */ const COMDLG_FILTERSPEC *rgFilterSpec);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileTypeIndex )( 
         IFileOpenDialog * This,
        /* [in] */ UINT iFileType);
        
    HRESULT ( STDMETHODCALLTYPE *GetFileTypeIndex )( 
         IFileOpenDialog * This,
        /* [out] */  UINT *piFileType);
        

    HRESULT ( STDMETHODCALLTYPE *Advise )( 
         IFileOpenDialog * This,
         /* XXX - Actually pfde is IFileDialogEvents* but we do not use
            this call and do not want to define IFileDialogEvents as that
            pulls in a whole bunch of other stuff. */
        /* [in] */  void *pfde,
        /* [out] */  DWORD *pdwCookie);
        
    HRESULT ( STDMETHODCALLTYPE *Unadvise )( 
         IFileOpenDialog * This,
        /* [in] */ DWORD dwCookie);
        
    HRESULT ( STDMETHODCALLTYPE *SetOptions )( 
         IFileOpenDialog * This,
        /* [in] */ FILEOPENDIALOGOPTIONS fos);
        
    HRESULT ( STDMETHODCALLTYPE *GetOptions )( 
         IFileOpenDialog * This,
        /* [out] */  FILEOPENDIALOGOPTIONS *pfos);
        
    HRESULT ( STDMETHODCALLTYPE *SetDefaultFolder )( 
         IFileOpenDialog * This,
        /* [in] */  IShellItem *psi);
        
    HRESULT ( STDMETHODCALLTYPE *SetFolder )( 
         IFileOpenDialog * This,
        /* [in] */  IShellItem *psi);
        
    HRESULT ( STDMETHODCALLTYPE *GetFolder )( 
         IFileOpenDialog * This,
        /* [out] */  IShellItem **ppsi);
        
    HRESULT ( STDMETHODCALLTYPE *GetCurrentSelection )( 
         IFileOpenDialog * This,
        /* [out] */  IShellItem **ppsi);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileName )( 
         IFileOpenDialog * This,
        /* [string][in] */  LPCWSTR pszName);
        
    HRESULT ( STDMETHODCALLTYPE *GetFileName )( 
         IFileOpenDialog * This,
        /* [string][out] */  LPWSTR *pszName);
        
    HRESULT ( STDMETHODCALLTYPE *SetTitle )( 
         IFileOpenDialog * This,
        /* [string][in] */  LPCWSTR pszTitle);
        
    HRESULT ( STDMETHODCALLTYPE *SetOkButtonLabel )( 
         IFileOpenDialog * This,
        /* [string][in] */  LPCWSTR pszText);
        
    HRESULT ( STDMETHODCALLTYPE *SetFileNameLabel )( 
         IFileOpenDialog * This,
        /* [string][in] */  LPCWSTR pszLabel);
        
    HRESULT ( STDMETHODCALLTYPE *GetResult )( 
         IFileOpenDialog * This,
        /* [out] */  IShellItem **ppsi);
        
    HRESULT ( STDMETHODCALLTYPE *AddPlace )( 
         IFileOpenDialog * This,
        /* [in] */  IShellItem *psi,
        /* [in] */ FDAP fdap);
        
    HRESULT ( STDMETHODCALLTYPE *SetDefaultExtension )( 
         IFileOpenDialog * This,
        /* [string][in] */  LPCWSTR pszDefaultExtension);
        
    HRESULT ( STDMETHODCALLTYPE *Close )( 
         IFileOpenDialog * This,
        /* [in] */ HRESULT hr);
        
    HRESULT ( STDMETHODCALLTYPE *SetClientGuid )( 
         IFileOpenDialog * This,
        /* [in] */  REFGUID guid);
        
    HRESULT ( STDMETHODCALLTYPE *ClearClientData )( 
         IFileOpenDialog * This);
        
    HRESULT ( STDMETHODCALLTYPE *SetFilter )( 
         IFileOpenDialog * This,
         /* XXX - Actually IShellItemFilter. But deprecated in Win7 AND we do
            not use it anyways. So define as void* */
        /* [in] */  void *pFilter);
        
    HRESULT ( STDMETHODCALLTYPE *GetResults )( 
        IFileOpenDialog * This,
        /* [out] */ IShellItemArray **ppenum);
        
    HRESULT ( STDMETHODCALLTYPE *GetSelectedItems )( 
        IFileOpenDialog * This,
        /* [out] */ IShellItemArray **ppsai);

    END_INTERFACE
} IFileOpenDialogVtbl;

struct IFileOpenDialog
{
    CONST_VTBL struct IFileOpenDialogVtbl *lpVtbl;
................................................................................
int
Tk_GetOpenFileObjCmd(
    ClientData clientData,	/* Main window associated with interpreter. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    return GetFileName(clientData, interp, objc, objv, 1);
}
 
/*
 *----------------------------------------------------------------------
 *
 * Tk_GetSaveFileCmd --
 *
................................................................................
int
Tk_GetSaveFileObjCmd(
    ClientData clientData,	/* Main window associated with interpreter. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    return GetFileName(clientData, interp, objc, objv, 0);
}
 
/*
 *----------------------------------------------------------------------
 *
 * CleanupOFNOptions --
 *
................................................................................

static int
ParseOFNOptions(
    ClientData clientData,	/* Main window associated with interpreter. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[],	/* Argument objects. */
    int open,			/* 1 for Open, 0 for Save */
    OFNOpts *optsPtr)           /* Output, uninitialized on entry */
{
    int i;
    Tcl_DString ds;
    enum options {
	FILE_DEFAULT, FILE_TYPES, FILE_INITDIR, FILE_INITFILE, FILE_PARENT,
	FILE_TITLE, FILE_TYPEVARIABLE, FILE_MULTIPLE, FILE_CONFIRMOW,

        FILE_UNDOCUMENTED_XP_STYLE /* XXX - force XP - style dialogs */
    };
    struct Options {
	const char *name;
	enum options value;
    };
    static const struct Options saveOptions[] = {
................................................................................
	{"-multiple",		FILE_MULTIPLE},
	{"-parent",		FILE_PARENT},
	{"-title",		FILE_TITLE},
	{"-typevariable",	FILE_TYPEVARIABLE},
        {"-xpstyle",            FILE_UNDOCUMENTED_XP_STYLE}, /* XXX */
	{NULL,			FILE_DEFAULT/*ignored*/ }
    };









    const struct Options *const options = open ? openOptions : saveOptions;







    ZeroMemory(optsPtr, sizeof(*optsPtr));
    optsPtr->tkwin = clientData;
    optsPtr->confirmOverwrite = 1; /* By default we ask for confirmation */
    Tcl_DStringInit(&optsPtr->utfDirString);
    optsPtr->file[0] = 0;

    for (i = 1; i < objc; i += 2) {
................................................................................
	    break;
	case FILE_CONFIRMOW:
	    if (Tcl_GetBooleanFromObj(interp, valuePtr,
                                      &optsPtr->confirmOverwrite) != TCL_OK) {
		return TCL_ERROR;
	    }
	    break;






        case FILE_UNDOCUMENTED_XP_STYLE:
	    if (Tcl_GetBooleanFromObj(interp, valuePtr,
                                      &optsPtr->forceXPStyle) != TCL_OK) {
		return TCL_ERROR;
	    }
            break;
	}
................................................................................
 *	TCL_OK - dialog was successfully displayed, results returned in interp
 *      TCL_ERROR - error return
 *
 * Side effects:
 *      Dialogs is displayed
 *----------------------------------------------------------------------
 */
static int GetFileNameVista(Tcl_Interp *interp, OFNOpts *optsPtr, int open)

{
    HRESULT hr;
    HWND hWnd;
    DWORD flags, nfilters, defaultFilterIndex;
    COMDLG_FILTERSPEC *filterPtr = NULL;
    IFileDialog *fdlgIf = NULL;
    IShellItem *dirIf = NULL;
    LPWSTR wstr;
    Tcl_Obj *resultObj = NULL;
    ThreadSpecificData *tsdPtr =
	    Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));


    if (tsdPtr->newFileDialogsState != FDLG_STATE_USE_NEW) {
        /* Should not be called in this condition but be nice about it */
        return GetFileNameXP(interp, optsPtr, open);


    }

    /*
     * At this point new interfaces are supposed to be available.
     * fdlgIf is actually a IFileOpenDialog or IFileSaveDialog
     * both of which inherit from IFileDialog. We use the common
     * IFileDialog interface for the most part, casting only for
................................................................................
        return TCL_ERROR;

    /*
     * Beyond this point, do not just return on error as there will be
     * resources that need to be released/freed.
     */

    if (open)
        hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL,
                 CLSCTX_INPROC_SERVER, &IID_IFileOpenDialog, &fdlgIf);
    else
        hr = CoCreateInstance(&CLSID_FileSaveDialog, NULL,
                 CLSCTX_INPROC_SERVER, &IID_IFileSaveDialog, &fdlgIf);

    if (FAILED(hr))
        goto vamoose;

    if (filterPtr) {
        hr = fdlgIf->lpVtbl->SetFileTypes(fdlgIf, nfilters, filterPtr);
        if (FAILED(hr))
            goto vamoose;
        hr = fdlgIf->lpVtbl->SetFileTypeIndex(fdlgIf, defaultFilterIndex);
        if (FAILED(hr))
            goto vamoose;
    }        
        
    /*
     * Get current settings first because we want to preserve existing
     * settings like whether to show hidden files etc. based on the
     * user's existing preference
     */
    hr = fdlgIf->lpVtbl->GetOptions(fdlgIf, &flags);
    if (FAILED(hr))
        goto vamoose;











    /* Flags are equivalent to those we used in the older API */

    /*
     * Following flags must be set irrespective of original setting
     * XXX - should FOS_NOVALIDATE be there ? Note FOS_NOVALIDATE has different
     * semantics than OFN_NOVALIDATE in the old API.
     */
    flags |=
        FOS_FORCEFILESYSTEM | /* Only want files, not other shell items */
        FOS_NOVALIDATE |           /* Don't check for access denied etc. */
        FOS_PATHMUSTEXIST;           /* The *directory* path must exist */









    if (optsPtr->multi)
        flags |= FOS_ALLOWMULTISELECT;
    else
        flags &= ~FOS_ALLOWMULTISELECT;

    if (optsPtr->confirmOverwrite)
        flags |= FOS_OVERWRITEPROMPT;
................................................................................
        if (SUCCEEDED(hr)) {
            /* Note we use SetFolder, not SetDefaultFolder - see MSDN docs */
            fdlgIf->lpVtbl->SetFolder(fdlgIf, dirIf); /* Ignore errors */
        }
        Tcl_DStringFree(&dirString);
    }    


    hr = fdlgIf->lpVtbl->Show(fdlgIf, hWnd);


    if (SUCCEEDED(hr)) {
        if (open && optsPtr->multi) {
            IShellItemArray *multiIf;
            DWORD dw, count;
            IFileOpenDialog *fodIf = (IFileOpenDialog *) fdlgIf;
            hr = fodIf->lpVtbl->GetResults(fodIf, &multiIf);
            if (SUCCEEDED(hr)) {
                Tcl_Obj *multiObj;
                hr = multiIf->lpVtbl->GetCount(multiIf, &count);
................................................................................
 *	TCL_OK - if dialog was successfully displayed
 *      TCL_ERROR - error return
 *
 * Side effects:
 *      See user documentation.
 *----------------------------------------------------------------------
 */
static int GetFileNameXP(Tcl_Interp *interp, OFNOpts *optsPtr, int open)
{
    OPENFILENAME ofn;
    OFNData ofnData;
    int cdlgerr;
    int filterIndex = 0, result = TCL_ERROR, winCode, oldMode;
    HWND hWnd;
    Tcl_DString utfFilterString, ds;
................................................................................
    ofn.lpstrFile = optsPtr->file;
    ofn.nMaxFile = TK_MULTI_MAX_PATH;
    ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR
	    | OFN_EXPLORER| OFN_ENABLEHOOK| OFN_ENABLESIZING;
    ofn.lpfnHook = (LPOFNHOOKPROC) OFNHookProc;
    ofn.lCustData = (LPARAM) &ofnData;

    if (open != 0) {
	ofn.Flags |= OFN_FILEMUSTEXIST;
    } else if (optsPtr->confirmOverwrite) {
	ofn.Flags |= OFN_OVERWRITEPROMPT;
    }
    if (tsdPtr->debugFlag != 0) {
	ofnData.interp = interp;
    }
................................................................................
    }

    /*
     * Popup the dialog.
     */

    oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
    if (open != 0) {
	winCode = GetOpenFileName(&ofn);
    } else {
	winCode = GetSaveFileName(&ofn);
    }
    Tcl_SetServiceMode(oldMode);
    EatSpuriousMessageBugFix();

................................................................................

static int
GetFileName(
    ClientData clientData,	/* Main window associated with interpreter. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[],	/* Argument objects. */
    int open)			/* 1 to call GetOpenFileName(), 0 to call
				 * GetSaveFileName(). */
{
    OFNOpts ofnOpts;
    int result;

    /* Parse the arguments. */
    result = ParseOFNOptions(clientData, interp, objc, objv, open, &ofnOpts);
    if (result != TCL_OK)
        return result;

    if (VistaFileDialogsAvailable() && ! ofnOpts.forceXPStyle)
        result = GetFileNameVista(interp, &ofnOpts, open);
    else
        result = GetFileNameXP(interp, &ofnOpts, open);

    CleanupOFNOptions(&ofnOpts);
    return result;
}

 
/*
................................................................................
Tk_ChooseDirectoryObjCmd(
    ClientData clientData,	/* Main window associated with interpreter. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    TCHAR path[MAX_PATH];
    int oldMode, result = TCL_ERROR, i;
    LPCITEMIDLIST pidl;		/* Returned by browser */
    BROWSEINFO bInfo;		/* Used by browser */
    ChooseDir cdCBData;	    /* Structure to pass back and forth */
    LPMALLOC pMalloc;		/* Used by shell */
    Tk_Window tkwin = clientData;
    HWND hWnd;
    const char *utfTitle = NULL;/* Title for window */
    TCHAR saveDir[MAX_PATH];
    Tcl_DString titleString;	/* Title */
    Tcl_DString initDirString;	/* Initial directory */
    Tcl_DString tempString;	/* temporary */
    Tcl_Obj *objPtr;

    static const char *const optionStrings[] = {


	"-initialdir", "-mustexist",  "-parent",  "-title", NULL
    };

    enum options {
	DIR_INITIAL,   DIR_EXIST,  DIR_PARENT, FILE_TITLE
    };

    /*



     * Initialize
     */





    path[0] = '\0';
    ZeroMemory(&cdCBData, sizeof(ChooseDir));
    cdCBData.interp = interp;


    /*
     * Process the command line options
     */

    for (i = 1; i < objc; i += 2) {
	int index;
	const char *string;

	const TCHAR *uniStr;
	Tcl_Obj *optionPtr, *valuePtr;

	optionPtr = objv[i];
	valuePtr = objv[i + 1];

	if (Tcl_GetIndexFromObjStruct(interp, optionPtr, optionStrings,
		sizeof(char *), "option", 0, &index) != TCL_OK) {
	    goto cleanup;
	}
	if (i + 1 == objc) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		    "value for \"%s\" missing", Tcl_GetString(optionPtr)));
	    Tcl_SetErrorCode(interp, "TK", "DIRDIALOG", "VALUE", NULL);
	    goto cleanup;
	}

	string = Tcl_GetString(valuePtr);
	switch ((enum options) index) {
	case DIR_INITIAL:
	    if (Tcl_TranslateFileName(interp,string,&initDirString) == NULL) {
		goto cleanup;
	    }
	    Tcl_WinUtfToTChar(Tcl_DStringValue(&initDirString), -1,
		    &tempString);
	    uniStr = (TCHAR *) Tcl_DStringValue(&tempString);

	    /*
	     * Convert possible relative path to full path to keep dialog
	     * happy.
	     */

	    GetFullPathName(uniStr, MAX_PATH, saveDir, NULL);
	    _tcsncpy(cdCBData.initDir, saveDir, MAX_PATH);
	    Tcl_DStringFree(&initDirString);
	    Tcl_DStringFree(&tempString);
	    break;
	case DIR_EXIST:
	    if (Tcl_GetBooleanFromObj(interp, valuePtr,
		    &cdCBData.mustExist) != TCL_OK) {
		goto cleanup;
	    }
	    break;
	case DIR_PARENT:
	    tkwin = Tk_NameToWindow(interp, string, tkwin);
	    if (tkwin == NULL) {
		goto cleanup;
	    }
	    break;
	case FILE_TITLE:
	    utfTitle = string;
	    break;

	}
    }

    /*
     * Get ready to call the browser
     */

    Tk_MakeWindowExist(tkwin);
    hWnd = Tk_GetHWND(Tk_WindowId(tkwin));

    /*
     * Setup the parameters used by SHBrowseForFolder
     */

    bInfo.hwndOwner = hWnd;
    bInfo.pszDisplayName = path;
    bInfo.pidlRoot = NULL;
    if (_tcslen(cdCBData.initDir) == 0) {
	GetCurrentDirectory(MAX_PATH, cdCBData.initDir);
    }
    bInfo.lParam = (LPARAM) &cdCBData;

    if (utfTitle != NULL) {
	Tcl_WinUtfToTChar(utfTitle, -1, &titleString);
	bInfo.lpszTitle = (LPTSTR) Tcl_DStringValue(&titleString);
    } else {
	bInfo.lpszTitle = TEXT("Please choose a directory, then select OK.");
    }

    /*
     * Set flags to add edit box, status text line and use the new ui. Allow
................................................................................
     * user a chance to change their mind on an invalid folder if mustexist is
     * 0.
     */

    oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
    GetCurrentDirectory(MAX_PATH, saveDir);
    if (SHGetMalloc(&pMalloc) == NOERROR) {




	pidl = SHBrowseForFolder(&bInfo);

	/*
	 * This is a fix for Windows 2000, which seems to modify the folder
	 * name buffer even when the dialog is canceled (in this case the
	 * buffer contains garbage). See [Bug #3002230]
	 */
................................................................................
	Tcl_DString ds;

	Tcl_SetObjResult(interp, Tcl_NewStringObj(
		ConvertExternalFilename(path, &ds), -1));
	Tcl_DStringFree(&ds);
    }

    result = TCL_OK;

    if (utfTitle != NULL) {
	Tcl_DStringFree(&titleString);
    }

  cleanup:
    return result;
}
 
/*
 *----------------------------------------------------------------------
 *
 * ChooseDirectoryValidateProc --
 *






|
>









>
>
>
>
>
>
>
>







 







<
<
<
|
<
|
<
<
|
<
<
|
<
<
<
<
<
<
>
>
|
|
<
<
<
<
<
>

|
<
<
<
<
<
|
<
|
<
<
<
|
|
<
<

|
<
<
<
>
|
<
<
|




|
<







 







<
<
<
|
<
|
<
<
|
<
<
|
<
<
<
<
|
<
<
|
<
|
<
<
<
|
<
<
<
<
<
|
|
|
<
<
<
|
|
<
<
>

<
|
<

<
|
<
|
|
<
<
|
|
<
<

<
|
<

<
|
<

|
<
<

|
<
<
|
|
<
<

|
<
<

|
<
<
|
<
|
<

|
<
<
<

<
|
<
|
<
<
<
|
|
<
<
|
<
<
<
<
|
|
>
|







 







<
<
<
|
<
|
<
<
|
<
<
|
|
<
<
<
|
<
<
|
<
|
|
<
<

|
<
<
>

|
<
<
<
<
<
<
|
<
<
<

<
|
<

<
|
<

|
<
<
|
|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<
<

<
|
<
|
<
<
<
|
|
<
<
|
<
<
>

|
<
<
<
<

|
<
<

<
|
<

<
|
|
<

<
|
<

<
<
|
<
|







 







<
<
<
|
<
|
<
<
|
<
<
|
<
<
<
<
|
<
<
|
<

|
<
<

|
<
<
>

|
<
<
<
<
<
<
|
<
<
<

<
|
<

<
|
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<

|
<
<
<

<
|
<
|
<
<
<

|
<
<

|
<

|
|
<
|
<

<
|
<

<
|







 







|







 







|







 







|







>







 







>
>
>
>
>
>
>
>
>
|

>
>
>
>
>
>







 







>
>
>
>
>
>







 







|
>











>


<
|
>
>







 







|









<
<
<
<
<
<
<
<
<









>
>
>
>
>
>
>
>
>
>













>
>
>
>
>
>
>







 







>

>
>

|







 







|







 







|







 







|







 







|





<
|




|

|







 







|




<

<


<


>
|
>
>
|
<
>
|
<
<

<
>
>
>
|
<
>
>
>
>




>

<
|
<
<
<
<
<
>

<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|

<
|
<
<

|
|
<
<
<
<
<
<
<
|
<
<
<
<
<
|
<
<
<
<
>
|
<
<




|
|













|
|







 







>
>
>
>







 







|
<
<
<
<
<
<
|







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
...
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
...
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
...
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
...
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
...
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
....
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
....
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
....
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239

1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
....
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277









1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
....
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
....
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
....
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
....
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
....
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768

1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
....
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323

2324

2325
2326

2327
2328
2329
2330
2331
2332
2333

2334
2335


2336

2337
2338
2339
2340

2341
2342
2343
2344
2345
2346
2347
2348
2349
2350

2351





2352
2353

2354




















2355
2356
2357
2358

2359


2360
2361
2362







2363





2364




2365
2366


2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
....
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
....
2473
2474
2475
2476
2477
2478
2479
2480






2481
2482
2483
2484
2485
2486
2487
2488
    Tcl_Obj *extObj;            /* Default extension */
    Tcl_Obj *titleObj;          /* Title for dialog */
    Tcl_Obj *filterObj;         /* File type filter list */
    Tcl_Obj *typeVariableObj;   /* Variable in which to store type selected */
    Tcl_Obj *initialTypeObj;    /* Initial value of above, or NULL */
    Tcl_DString utfDirString;   /* Initial dir */
    int multi;                  /* Multiple selection enabled */
    int confirmOverwrite;       /* Confirm before overwriting */
    int mustExist;              /* Used only for  */
    int forceXPStyle;          /* XXX - Force XP style even on newer systems */
    TCHAR file[TK_MULTI_MAX_PATH]; /* File name
                                      XXX - fixed size because it was so
                                      historically. Why not malloc'ed ?
                                      XXX - also, TCHAR should really be WCHAR
                                      because TkWinGetUnicodeEncoding is always
                                      UCS2.
                                   */
} OFNOpts;

/* Define the operation for which option parsing is to be done. */
enum OFNOper {
    OFN_FILE_SAVE,              /* tk_getOpenFile */
    OFN_FILE_OPEN,              /* tk_getSaveFile */
    OFN_DIR_CHOOSE              /* tk_chooseDirectory */
};


/*
 * The following definitions are required when using older versions of
 * Visual C++ (like 6.0) and possibly MingW. Those headers do not contain
 * required definitions for interfaces new to Vista that we need for
 * the new file dialogs. Duplicating definitions is OK because they
 * should forever remain unchanged.
................................................................................
} SIATTRIBFLAGS;
typedef struct IShellItemArray IShellItemArray;
typedef struct IShellItemArrayVtbl
{
    BEGIN_INTERFACE
        
    HRESULT ( STDMETHODCALLTYPE *QueryInterface )( 



        IShellItemArray * this, REFIID riid,void **ppvObject);

    ULONG ( STDMETHODCALLTYPE *AddRef )(IShellItemArray * this);


    ULONG ( STDMETHODCALLTYPE *Release )(IShellItemArray * this);


    HRESULT ( STDMETHODCALLTYPE *BindToHandler )(IShellItemArray * this,






        IBindCtx *pbc, REFGUID bhid, REFIID riid, void **ppvOut);
    /* flags is actually is enum GETPROPERTYSTOREFLAGS */
    HRESULT ( STDMETHODCALLTYPE *GetPropertyStore )(
        IShellItemArray * this, int flags,  REFIID riid, void **ppv);





    /* keyType actually REFPROPERTYKEY */
    HRESULT ( STDMETHODCALLTYPE *GetPropertyDescriptionList )( 
         IShellItemArray * this, void* keyType, REFIID riid, void **ppv);





    HRESULT ( STDMETHODCALLTYPE *GetAttributes )(IShellItemArray * this,

        SIATTRIBFLAGS AttribFlags, SFGAOF sfgaoMask, SFGAOF *psfgaoAttribs);



    HRESULT ( STDMETHODCALLTYPE *GetCount )(
        IShellItemArray * this, DWORD *pdwNumItems);


    HRESULT ( STDMETHODCALLTYPE *GetItemAt )( 
        IShellItemArray * this, DWORD dwIndex, IShellItem **ppsi);



    /* ppenumShellItems actually (IEnumShellItems **) */
    HRESULT ( STDMETHODCALLTYPE *EnumItems )(


        IShellItemArray * this, void **ppenumShellItems);
        
    END_INTERFACE
} IShellItemArrayVtbl;

struct IShellItemArray {

    CONST_VTBL struct IShellItemArrayVtbl *lpVtbl;
};

#endif /* __IShellItemArray_INTERFACE_DEFINED__ */

#ifndef __IFileDialog_INTERFACE_DEFINED__

................................................................................

typedef struct IFileDialog IFileDialog;
typedef struct IFileDialogVtbl
{
    BEGIN_INTERFACE
        
    HRESULT ( STDMETHODCALLTYPE *QueryInterface )( 



         IFileDialog * this, REFIID riid, void **ppvObject);

    ULONG ( STDMETHODCALLTYPE *AddRef )( IFileDialog * this);


    ULONG ( STDMETHODCALLTYPE *Release )( IFileDialog * this);


    HRESULT ( STDMETHODCALLTYPE *Show )( IFileDialog * this, HWND hwndOwner);




    HRESULT ( STDMETHODCALLTYPE *SetFileTypes )( IFileDialog * this,


        UINT cFileTypes, const COMDLG_FILTERSPEC *rgFilterSpec);

    HRESULT ( STDMETHODCALLTYPE *SetFileTypeIndex )(IFileDialog * this, UINT);



    HRESULT ( STDMETHODCALLTYPE *GetFileTypeIndex )(IFileDialog * this, UINT *);





    /* XXX - Actually pfde is IFileDialogEvents* but we do not use
       this call and do not want to define IFileDialogEvents as that
       pulls in a whole bunch of other stuff. */



    HRESULT ( STDMETHODCALLTYPE *Advise )( 
        IFileDialog * this, void *pfde, DWORD *pdwCookie);


    HRESULT ( STDMETHODCALLTYPE *Unadvise )(IFileDialog * this, DWORD dwCookie);
    HRESULT ( STDMETHODCALLTYPE *SetOptions )( 

        IFileDialog * this, FILEOPENDIALOGOPTIONS fos);

    HRESULT ( STDMETHODCALLTYPE *GetOptions )( 

        IFileDialog * this, FILEOPENDIALOGOPTIONS *pfos);

    HRESULT ( STDMETHODCALLTYPE *SetDefaultFolder )(
        IFileDialog * this, IShellItem *psi);


    HRESULT ( STDMETHODCALLTYPE *SetFolder )(
        IFileDialog * this, IShellItem *psi);


    HRESULT ( STDMETHODCALLTYPE *GetFolder )( 

        IFileDialog * this, IShellItem **ppsi);

    HRESULT ( STDMETHODCALLTYPE *GetCurrentSelection )( 

        IFileDialog * this, IShellItem **ppsi);

    HRESULT ( STDMETHODCALLTYPE *SetFileName )( 
        IFileDialog * this,  LPCWSTR pszName);


    HRESULT ( STDMETHODCALLTYPE *GetFileName )( 
        IFileDialog * this,  LPWSTR *pszName);


    HRESULT ( STDMETHODCALLTYPE *SetTitle )(
        IFileDialog * this, LPCWSTR pszTitle);


    HRESULT ( STDMETHODCALLTYPE *SetOkButtonLabel )( 
        IFileDialog * this, LPCWSTR pszText);


    HRESULT ( STDMETHODCALLTYPE *SetFileNameLabel )( 
        IFileDialog * this,  LPCWSTR pszLabel);


    HRESULT ( STDMETHODCALLTYPE *GetResult )(

        IFileDialog * this, IShellItem **ppsi);

    HRESULT ( STDMETHODCALLTYPE *AddPlace )( 
        IFileDialog * this, IShellItem *psi, FDAP fdap);



    HRESULT ( STDMETHODCALLTYPE *SetDefaultExtension )( 

         IFileDialog * this, LPCWSTR pszDefaultExtension);

    HRESULT ( STDMETHODCALLTYPE *Close )( IFileDialog * this, HRESULT hr);



    HRESULT ( STDMETHODCALLTYPE *SetClientGuid )(
        IFileDialog * this, REFGUID guid);


    HRESULT ( STDMETHODCALLTYPE *ClearClientData )( IFileDialog * this);




    /* pFilter actually IShellItemFilter. But deprecated in Win7 AND we do
       not use it anyways. So define as void* */
    HRESULT ( STDMETHODCALLTYPE *SetFilter )( 
         IFileDialog * this, void *pFilter);
        
    END_INTERFACE
} IFileDialogVtbl;

struct IFileDialog {
    CONST_VTBL struct IFileDialogVtbl *lpVtbl;
};
................................................................................


typedef struct IFileSaveDialog IFileSaveDialog;
typedef struct IFileSaveDialogVtbl {
    BEGIN_INTERFACE
        
    HRESULT ( STDMETHODCALLTYPE *QueryInterface )( 



        IFileSaveDialog * this, REFIID riid, void **ppvObject);

    ULONG ( STDMETHODCALLTYPE *AddRef )( IFileSaveDialog * this);


    ULONG ( STDMETHODCALLTYPE *Release )( IFileSaveDialog * this);


    HRESULT ( STDMETHODCALLTYPE *Show )( 
        IFileSaveDialog * this, HWND hwndOwner);



    HRESULT ( STDMETHODCALLTYPE *SetFileTypes )( IFileSaveDialog * this,


        UINT cFileTypes, const COMDLG_FILTERSPEC *rgFilterSpec);

    HRESULT ( STDMETHODCALLTYPE *SetFileTypeIndex )(
        IFileSaveDialog * this, UINT iFileType);


    HRESULT ( STDMETHODCALLTYPE *GetFileTypeIndex )( 
         IFileSaveDialog * this, UINT *piFileType);


    /* Actually pfde is IFileSaveDialogEvents* */
    HRESULT ( STDMETHODCALLTYPE *Advise )( 
         IFileSaveDialog * this, void *pfde, DWORD *pdwCookie);






    HRESULT ( STDMETHODCALLTYPE *Unadvise )( IFileSaveDialog * this, DWORD);



    HRESULT ( STDMETHODCALLTYPE *SetOptions )( 

         IFileSaveDialog * this, FILEOPENDIALOGOPTIONS fos);

    HRESULT ( STDMETHODCALLTYPE *GetOptions )( 

         IFileSaveDialog * this, FILEOPENDIALOGOPTIONS *pfos);

    HRESULT ( STDMETHODCALLTYPE *SetDefaultFolder )( 
         IFileSaveDialog * this, IShellItem *psi);


    HRESULT ( STDMETHODCALLTYPE *SetFolder )(
        IFileSaveDialog * this, IShellItem *psi);


    HRESULT ( STDMETHODCALLTYPE *GetFolder )( 
         IFileSaveDialog * this, IShellItem **ppsi);


    HRESULT ( STDMETHODCALLTYPE *GetCurrentSelection )( 
         IFileSaveDialog * this, IShellItem **ppsi);


    HRESULT ( STDMETHODCALLTYPE *SetFileName )( 
         IFileSaveDialog * this,  LPCWSTR pszName);


    HRESULT ( STDMETHODCALLTYPE *GetFileName )( 
         IFileSaveDialog * this,  LPWSTR *pszName);


    HRESULT ( STDMETHODCALLTYPE *SetTitle )( 
         IFileSaveDialog * this, LPCWSTR pszTitle);


    HRESULT ( STDMETHODCALLTYPE *SetOkButtonLabel )( 
         IFileSaveDialog * this,  LPCWSTR pszText);


    HRESULT ( STDMETHODCALLTYPE *SetFileNameLabel )( 
         IFileSaveDialog * this,  LPCWSTR pszLabel);


    HRESULT ( STDMETHODCALLTYPE *GetResult )( 
         IFileSaveDialog * this, IShellItem **ppsi);


    HRESULT ( STDMETHODCALLTYPE *AddPlace )( 
         IFileSaveDialog * this, IShellItem *psi, FDAP fdap);



    HRESULT ( STDMETHODCALLTYPE *SetDefaultExtension )( 

         IFileSaveDialog * this, LPCWSTR pszDefaultExtension);

    HRESULT ( STDMETHODCALLTYPE *Close )( IFileSaveDialog * this, HRESULT hr);



    HRESULT ( STDMETHODCALLTYPE *SetClientGuid )(
        IFileSaveDialog * this, REFGUID guid);


    HRESULT ( STDMETHODCALLTYPE *ClearClientData )( IFileSaveDialog * this);


    /* pFilter Actually IShellItemFilter* */
    HRESULT ( STDMETHODCALLTYPE *SetFilter )( 
        IFileSaveDialog * this, void *pFilter);




    HRESULT ( STDMETHODCALLTYPE *SetSaveAsItem )( 
        IFileSaveDialog * this, IShellItem *psi);


    HRESULT ( STDMETHODCALLTYPE *SetProperties )( 

        IFileSaveDialog * this, IPropertyStore *pStore);

    HRESULT ( STDMETHODCALLTYPE *SetCollectedProperties )( 

        IFileSaveDialog * this, IPropertyDescriptionList *pList,
        BOOL fAppendDefault);

    HRESULT ( STDMETHODCALLTYPE *GetProperties )( 

        IFileSaveDialog * this, IPropertyStore **ppStore);

    HRESULT ( STDMETHODCALLTYPE *ApplyProperties )( 


        IFileSaveDialog * this, IShellItem *psi, IPropertyStore *pStore,

        HWND hwnd, IFileOperationProgressSink *pSink);

    END_INTERFACE

} IFileSaveDialogVtbl;

struct IFileSaveDialog {
    CONST_VTBL struct IFileSaveDialogVtbl *lpVtbl;
................................................................................
};

typedef struct IFileOpenDialog IFileOpenDialog;
typedef struct IFileOpenDialogVtbl {
    BEGIN_INTERFACE
        
    HRESULT ( STDMETHODCALLTYPE *QueryInterface )( 



        IFileOpenDialog * this, REFIID riid, void **ppvObject);

    ULONG ( STDMETHODCALLTYPE *AddRef )( IFileOpenDialog * this);


    ULONG ( STDMETHODCALLTYPE *Release )( IFileOpenDialog * this);


    HRESULT ( STDMETHODCALLTYPE *Show )( IFileOpenDialog * this, HWND);




    HRESULT ( STDMETHODCALLTYPE *SetFileTypes )( IFileOpenDialog * this,


        UINT cFileTypes, const COMDLG_FILTERSPEC *rgFilterSpec);

    HRESULT ( STDMETHODCALLTYPE *SetFileTypeIndex )( 
        IFileOpenDialog * this, UINT iFileType);


    HRESULT ( STDMETHODCALLTYPE *GetFileTypeIndex )( 
        IFileOpenDialog * this, UINT *piFileType);


    /* Actually pfde is IFileDialogEvents* */
    HRESULT ( STDMETHODCALLTYPE *Advise )( 
        IFileOpenDialog * this, void *pfde, DWORD *pdwCookie);






    HRESULT ( STDMETHODCALLTYPE *Unadvise )( IFileOpenDialog * this, DWORD);



    HRESULT ( STDMETHODCALLTYPE *SetOptions )( 

        IFileOpenDialog * this, FILEOPENDIALOGOPTIONS fos);

    HRESULT ( STDMETHODCALLTYPE *GetOptions )( 

        IFileOpenDialog * this, FILEOPENDIALOGOPTIONS *pfos);

    HRESULT ( STDMETHODCALLTYPE *SetDefaultFolder )( 
        IFileOpenDialog * this, IShellItem *psi);


    HRESULT ( STDMETHODCALLTYPE *SetFolder )( 
        IFileOpenDialog * this, IShellItem *psi);


    HRESULT ( STDMETHODCALLTYPE *GetFolder )( 
        IFileOpenDialog * this, IShellItem **ppsi);


    HRESULT ( STDMETHODCALLTYPE *GetCurrentSelection )( 
        IFileOpenDialog * this, IShellItem **ppsi);


    HRESULT ( STDMETHODCALLTYPE *SetFileName )( 
        IFileOpenDialog * this,  LPCWSTR pszName);


    HRESULT ( STDMETHODCALLTYPE *GetFileName )( 
        IFileOpenDialog * this, LPWSTR *pszName);


    HRESULT ( STDMETHODCALLTYPE *SetTitle )( 
        IFileOpenDialog * this, LPCWSTR pszTitle);


    HRESULT ( STDMETHODCALLTYPE *SetOkButtonLabel )( 
        IFileOpenDialog * this, LPCWSTR pszText);


    HRESULT ( STDMETHODCALLTYPE *SetFileNameLabel )( 
        IFileOpenDialog * this, LPCWSTR pszLabel);


    HRESULT ( STDMETHODCALLTYPE *GetResult )( 
        IFileOpenDialog * this, IShellItem **ppsi);


    HRESULT ( STDMETHODCALLTYPE *AddPlace )( 
        IFileOpenDialog * this, IShellItem *psi, FDAP fdap);



    HRESULT ( STDMETHODCALLTYPE *SetDefaultExtension )( 

        IFileOpenDialog * this, LPCWSTR pszDefaultExtension);

    HRESULT ( STDMETHODCALLTYPE *Close )( IFileOpenDialog * this, HRESULT hr);



    HRESULT ( STDMETHODCALLTYPE *SetClientGuid )( 
        IFileOpenDialog * this, REFGUID guid);


    HRESULT ( STDMETHODCALLTYPE *ClearClientData )( 
        IFileOpenDialog * this);

    HRESULT ( STDMETHODCALLTYPE *SetFilter )( 
        IFileOpenDialog * this,
        /* pFilter is actually IShellItemFilter */

        void *pFilter);

    HRESULT ( STDMETHODCALLTYPE *GetResults )( 

        IFileOpenDialog * this, IShellItemArray **ppenum);

    HRESULT ( STDMETHODCALLTYPE *GetSelectedItems )( 

        IFileOpenDialog * this, IShellItemArray **ppsai);

    END_INTERFACE
} IFileOpenDialogVtbl;

struct IFileOpenDialog
{
    CONST_VTBL struct IFileOpenDialogVtbl *lpVtbl;
................................................................................
int
Tk_GetOpenFileObjCmd(
    ClientData clientData,	/* Main window associated with interpreter. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    return GetFileName(clientData, interp, objc, objv, OFN_FILE_OPEN);
}
 
/*
 *----------------------------------------------------------------------
 *
 * Tk_GetSaveFileCmd --
 *
................................................................................
int
Tk_GetSaveFileObjCmd(
    ClientData clientData,	/* Main window associated with interpreter. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    return GetFileName(clientData, interp, objc, objv, OFN_FILE_SAVE);
}
 
/*
 *----------------------------------------------------------------------
 *
 * CleanupOFNOptions --
 *
................................................................................

static int
ParseOFNOptions(
    ClientData clientData,	/* Main window associated with interpreter. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[],	/* Argument objects. */
    enum OFNOper oper,			/* 1 for Open, 0 for Save */
    OFNOpts *optsPtr)           /* Output, uninitialized on entry */
{
    int i;
    Tcl_DString ds;
    enum options {
	FILE_DEFAULT, FILE_TYPES, FILE_INITDIR, FILE_INITFILE, FILE_PARENT,
	FILE_TITLE, FILE_TYPEVARIABLE, FILE_MULTIPLE, FILE_CONFIRMOW,
        FILE_MUSTEXIST,
        FILE_UNDOCUMENTED_XP_STYLE /* XXX - force XP - style dialogs */
    };
    struct Options {
	const char *name;
	enum options value;
    };
    static const struct Options saveOptions[] = {
................................................................................
	{"-multiple",		FILE_MULTIPLE},
	{"-parent",		FILE_PARENT},
	{"-title",		FILE_TITLE},
	{"-typevariable",	FILE_TYPEVARIABLE},
        {"-xpstyle",            FILE_UNDOCUMENTED_XP_STYLE}, /* XXX */
	{NULL,			FILE_DEFAULT/*ignored*/ }
    };
    static const struct Options dirOptions[] = {
	{"-initialdir", FILE_INITDIR},
        {"-mustexist",  FILE_MUSTEXIST},
	{"-parent",	FILE_PARENT},
	{"-title",	FILE_TITLE},
        {"-xpstyle",    FILE_UNDOCUMENTED_XP_STYLE}, /* XXX */
	{NULL,		FILE_DEFAULT/*ignored*/ }
    };

    const struct Options *options = NULL;

    switch (oper) {
    case OFN_FILE_SAVE: options = saveOptions; break;
    case OFN_DIR_CHOOSE: options = dirOptions; break;
    case OFN_FILE_OPEN: options = openOptions; break;
    }        
        
    ZeroMemory(optsPtr, sizeof(*optsPtr));
    optsPtr->tkwin = clientData;
    optsPtr->confirmOverwrite = 1; /* By default we ask for confirmation */
    Tcl_DStringInit(&optsPtr->utfDirString);
    optsPtr->file[0] = 0;

    for (i = 1; i < objc; i += 2) {
................................................................................
	    break;
	case FILE_CONFIRMOW:
	    if (Tcl_GetBooleanFromObj(interp, valuePtr,
                                      &optsPtr->confirmOverwrite) != TCL_OK) {
		return TCL_ERROR;
	    }
	    break;
        case FILE_MUSTEXIST:
	    if (Tcl_GetBooleanFromObj(interp, valuePtr,
                                      &optsPtr->mustExist) != TCL_OK) {
		return TCL_ERROR;
	    }
            break;
        case FILE_UNDOCUMENTED_XP_STYLE:
	    if (Tcl_GetBooleanFromObj(interp, valuePtr,
                                      &optsPtr->forceXPStyle) != TCL_OK) {
		return TCL_ERROR;
	    }
            break;
	}
................................................................................
 *	TCL_OK - dialog was successfully displayed, results returned in interp
 *      TCL_ERROR - error return
 *
 * Side effects:
 *      Dialogs is displayed
 *----------------------------------------------------------------------
 */
static int GetFileNameVista(Tcl_Interp *interp, OFNOpts *optsPtr,
                            enum OFNOper oper)
{
    HRESULT hr;
    HWND hWnd;
    DWORD flags, nfilters, defaultFilterIndex;
    COMDLG_FILTERSPEC *filterPtr = NULL;
    IFileDialog *fdlgIf = NULL;
    IShellItem *dirIf = NULL;
    LPWSTR wstr;
    Tcl_Obj *resultObj = NULL;
    ThreadSpecificData *tsdPtr =
	    Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
    int oldMode;

    if (tsdPtr->newFileDialogsState != FDLG_STATE_USE_NEW) {

        /* XXX - should be an assert but Tcl does not seem to have one? */
        Tcl_SetResult(interp, "Internal error: GetFileNameVista: IFileDialog API not available", TCL_STATIC);
        return TCL_ERROR;
    }

    /*
     * At this point new interfaces are supposed to be available.
     * fdlgIf is actually a IFileOpenDialog or IFileSaveDialog
     * both of which inherit from IFileDialog. We use the common
     * IFileDialog interface for the most part, casting only for
................................................................................
        return TCL_ERROR;

    /*
     * Beyond this point, do not just return on error as there will be
     * resources that need to be released/freed.
     */

    if (oper == OFN_FILE_OPEN || oper == OFN_DIR_CHOOSE)
        hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL,
                 CLSCTX_INPROC_SERVER, &IID_IFileOpenDialog, &fdlgIf);
    else
        hr = CoCreateInstance(&CLSID_FileSaveDialog, NULL,
                 CLSCTX_INPROC_SERVER, &IID_IFileSaveDialog, &fdlgIf);

    if (FAILED(hr))
        goto vamoose;










    /*
     * Get current settings first because we want to preserve existing
     * settings like whether to show hidden files etc. based on the
     * user's existing preference
     */
    hr = fdlgIf->lpVtbl->GetOptions(fdlgIf, &flags);
    if (FAILED(hr))
        goto vamoose;

    if (filterPtr) {
        flags |= FOS_STRICTFILETYPES; /* XXX - does this match old behaviour? */
        hr = fdlgIf->lpVtbl->SetFileTypes(fdlgIf, nfilters, filterPtr);
        if (FAILED(hr))
            goto vamoose;
        hr = fdlgIf->lpVtbl->SetFileTypeIndex(fdlgIf, defaultFilterIndex);
        if (FAILED(hr))
            goto vamoose;
    }        
        
    /* Flags are equivalent to those we used in the older API */

    /*
     * Following flags must be set irrespective of original setting
     * XXX - should FOS_NOVALIDATE be there ? Note FOS_NOVALIDATE has different
     * semantics than OFN_NOVALIDATE in the old API.
     */
    flags |=
        FOS_FORCEFILESYSTEM | /* Only want files, not other shell items */
        FOS_NOVALIDATE |           /* Don't check for access denied etc. */
        FOS_PATHMUSTEXIST;           /* The *directory* path must exist */


    if (oper == OFN_DIR_CHOOSE) {
        flags |= FOS_PICKFOLDERS;
        if (optsPtr->mustExist)
            flags |= FOS_FILEMUSTEXIST; /* XXX - check working */
    } else
        flags &= ~ FOS_PICKFOLDERS;

    if (optsPtr->multi)
        flags |= FOS_ALLOWMULTISELECT;
    else
        flags &= ~FOS_ALLOWMULTISELECT;

    if (optsPtr->confirmOverwrite)
        flags |= FOS_OVERWRITEPROMPT;
................................................................................
        if (SUCCEEDED(hr)) {
            /* Note we use SetFolder, not SetDefaultFolder - see MSDN docs */
            fdlgIf->lpVtbl->SetFolder(fdlgIf, dirIf); /* Ignore errors */
        }
        Tcl_DStringFree(&dirString);
    }    

    oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
    hr = fdlgIf->lpVtbl->Show(fdlgIf, hWnd);
    Tcl_SetServiceMode(oldMode);

    if (SUCCEEDED(hr)) {
        if ((oper == OFN_FILE_OPEN) && optsPtr->multi) {
            IShellItemArray *multiIf;
            DWORD dw, count;
            IFileOpenDialog *fodIf = (IFileOpenDialog *) fdlgIf;
            hr = fodIf->lpVtbl->GetResults(fodIf, &multiIf);
            if (SUCCEEDED(hr)) {
                Tcl_Obj *multiObj;
                hr = multiIf->lpVtbl->GetCount(multiIf, &count);
................................................................................
 *	TCL_OK - if dialog was successfully displayed
 *      TCL_ERROR - error return
 *
 * Side effects:
 *      See user documentation.
 *----------------------------------------------------------------------
 */
static int GetFileNameXP(Tcl_Interp *interp, OFNOpts *optsPtr, enum OFNOper oper)
{
    OPENFILENAME ofn;
    OFNData ofnData;
    int cdlgerr;
    int filterIndex = 0, result = TCL_ERROR, winCode, oldMode;
    HWND hWnd;
    Tcl_DString utfFilterString, ds;
................................................................................
    ofn.lpstrFile = optsPtr->file;
    ofn.nMaxFile = TK_MULTI_MAX_PATH;
    ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR
	    | OFN_EXPLORER| OFN_ENABLEHOOK| OFN_ENABLESIZING;
    ofn.lpfnHook = (LPOFNHOOKPROC) OFNHookProc;
    ofn.lCustData = (LPARAM) &ofnData;

    if (oper != OFN_FILE_SAVE) {
	ofn.Flags |= OFN_FILEMUSTEXIST;
    } else if (optsPtr->confirmOverwrite) {
	ofn.Flags |= OFN_OVERWRITEPROMPT;
    }
    if (tsdPtr->debugFlag != 0) {
	ofnData.interp = interp;
    }
................................................................................
    }

    /*
     * Popup the dialog.
     */

    oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
    if (oper != OFN_FILE_SAVE) {
	winCode = GetOpenFileName(&ofn);
    } else {
	winCode = GetSaveFileName(&ofn);
    }
    Tcl_SetServiceMode(oldMode);
    EatSpuriousMessageBugFix();

................................................................................

static int
GetFileName(
    ClientData clientData,	/* Main window associated with interpreter. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[],	/* Argument objects. */
    enum OFNOper oper)  	/* 1 to call GetOpenFileName(), 0 to call
				 * GetSaveFileName(). */
{
    OFNOpts ofnOpts;
    int result;


    result = ParseOFNOptions(clientData, interp, objc, objv, oper, &ofnOpts);
    if (result != TCL_OK)
        return result;

    if (VistaFileDialogsAvailable() && ! ofnOpts.forceXPStyle)
        result = GetFileNameVista(interp, &ofnOpts, oper);
    else
        result = GetFileNameXP(interp, &ofnOpts, oper);

    CleanupOFNOptions(&ofnOpts);
    return result;
}

 
/*
................................................................................
Tk_ChooseDirectoryObjCmd(
    ClientData clientData,	/* Main window associated with interpreter. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    TCHAR path[MAX_PATH];
    int oldMode, result;
    LPCITEMIDLIST pidl;		/* Returned by browser */
    BROWSEINFO bInfo;		/* Used by browser */
    ChooseDir cdCBData;	    /* Structure to pass back and forth */
    LPMALLOC pMalloc;		/* Used by shell */

    HWND hWnd;

    TCHAR saveDir[MAX_PATH];
    Tcl_DString titleString;	/* Title */

    Tcl_DString tempString;	/* temporary */
    Tcl_Obj *objPtr;
    OFNOpts ofnOpts;
    const char *utfDir;

    result = ParseOFNOptions(clientData, interp, objc, objv, 
                 OFN_DIR_CHOOSE, &ofnOpts);

    if (result != TCL_OK)
        return result;




    /* Use new dialogs if available */
    if (VistaFileDialogsAvailable() && ! ofnOpts.forceXPStyle) {
        result = GetFileNameVista(interp, &ofnOpts, OFN_DIR_CHOOSE);
        CleanupOFNOptions(&ofnOpts);

        return result;
    }

    /* Older dialogs */

    path[0] = '\0';
    ZeroMemory(&cdCBData, sizeof(ChooseDir));
    cdCBData.interp = interp;
    cdCBData.mustExist = ofnOpts.mustExist;


    utfDir = Tcl_DStringValue(&ofnOpts.utfDirString);





    if (utfDir[0] != '\0') {
	const TCHAR *uniStr;






















        Tcl_WinUtfToTChar(Tcl_DStringValue(&ofnOpts.utfDirString), -1,
                          &tempString);
        uniStr = (TCHAR *) Tcl_DStringValue(&tempString);


        /* Convert possible relative path to full path to keep dialog happy. */



        GetFullPathName(uniStr, MAX_PATH, saveDir, NULL);
        _tcsncpy(cdCBData.initDir, saveDir, MAX_PATH);







    }










    /* XXX - rest of this (original) code has no error checks at all. */



    /*
     * Get ready to call the browser
     */

    Tk_MakeWindowExist(ofnOpts.tkwin);
    hWnd = Tk_GetHWND(Tk_WindowId(ofnOpts.tkwin));

    /*
     * Setup the parameters used by SHBrowseForFolder
     */

    bInfo.hwndOwner = hWnd;
    bInfo.pszDisplayName = path;
    bInfo.pidlRoot = NULL;
    if (_tcslen(cdCBData.initDir) == 0) {
	GetCurrentDirectory(MAX_PATH, cdCBData.initDir);
    }
    bInfo.lParam = (LPARAM) &cdCBData;

    if (ofnOpts.titleObj != NULL) {
	Tcl_WinUtfToTChar(Tcl_GetString(ofnOpts.titleObj), -1, &titleString);
	bInfo.lpszTitle = (LPTSTR) Tcl_DStringValue(&titleString);
    } else {
	bInfo.lpszTitle = TEXT("Please choose a directory, then select OK.");
    }

    /*
     * Set flags to add edit box, status text line and use the new ui. Allow
................................................................................
     * user a chance to change their mind on an invalid folder if mustexist is
     * 0.
     */

    oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
    GetCurrentDirectory(MAX_PATH, saveDir);
    if (SHGetMalloc(&pMalloc) == NOERROR) {
        /*
         * XXX - MSDN says CoInitialize must have been called before
         * SHBrowseForFolder can be used but don't see that called anywhere.
         */
	pidl = SHBrowseForFolder(&bInfo);

	/*
	 * This is a fix for Windows 2000, which seems to modify the folder
	 * name buffer even when the dialog is canceled (in this case the
	 * buffer contains garbage). See [Bug #3002230]
	 */
................................................................................
	Tcl_DString ds;

	Tcl_SetObjResult(interp, Tcl_NewStringObj(
		ConvertExternalFilename(path, &ds), -1));
	Tcl_DStringFree(&ds);
    }

    CleanupOFNOptions(&ofnOpts);






    return TCL_OK;
}
 
/*
 *----------------------------------------------------------------------
 *
 * ChooseDirectoryValidateProc --
 *