forked from coddec/Classic-Shell
- Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathClassicCopy.cpp
776 lines (702 loc) · 23 KB
/
ClassicCopy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
// Classic Shell (c) 2009-2017, Ivo Beltchev
// Open-Shell (c) 2017-2018, The Open-Shell Team
// Confidential information of Ivo Beltchev. Not for disclosure or distribution without prior written consent from the author
#include"stdafx.h"
#include<commctrl.h>
#include<oleacc.h>
#include<atlcomcli.h>
#include<dwmapi.h>
#include<utility>
#include"Translations.h"
#include"Settings.h"
#include"ResourceHelper.h"
#include"ComHelper.h"
#include"dllmain.h"
staticwchar_t g_TitleMove[256];
staticwchar_t g_TitleCopy[256];
staticwchar_t g_TitleFolder[256];
staticwchar_t g_ButtonMove[256];
staticwchar_t g_ButtonDontMove[256];
staticwchar_t g_ButtonCopy[256];
staticwchar_t g_ButtonDontCopy[256];
staticwchar_t g_ButtonCancel[256];
staticwchar_t g_ButtonMore[256];
static HMODULE g_hShell32;
staticvoidLogPrint( CString *pLog, constwchar_t *format, ... )
{
wchar_t buf[256];
va_list args;
va_start(args,format);
int len=Vsprintf(buf,_countof(buf),format,args);
va_end(args);
*pLog+=buf;
}
// CClassicCopyFile - this is the implementation of the Copy UI dialog box for files
classCClassicCopyFile
{
public:
CClassicCopyFile( void );
~CClassicCopyFile( void );
boolRun( HWND hWnd, IAccessible *pAcc, CString *pLog );
private:
voidEnumAccChildren( IAccessible *pAcc, CString *pLog );
voidAddAccChild( IAccessible *pAcc, const VARIANT &id, CString *pLog );
voidGetFileInfo( IAccessible *pAcc, bool bSrc );
CString m_FileName;
bool m_bSystem;
bool m_bReadOnly;
HICON m_Icon;
CString m_SrcSize;
CString m_SrcTime;
HICON m_SrcIcon;
CString m_DstSize;
CString m_DstTime;
HICON m_DstIcon;
typedef std::pair<CComPtr<IAccessible>,int> CControl;
CControl m_YesButton;
CControl m_NoButton;
CControl m_CheckBox;
CControl m_Cancel;
bool m_bCopyMultiLast; // the last of a series of multi-file boxes
static INT_PTR CALLBACK DialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );
staticvoidPumpMessages( void );
};
CClassicCopyFile::CClassicCopyFile( void )
{
m_Icon=m_SrcIcon=m_DstIcon=NULL;
m_bCopyMultiLast=false;
m_bSystem=m_bReadOnly=false;
}
CClassicCopyFile::~CClassicCopyFile( void )
{
if (m_Icon) DestroyIcon(m_Icon);
if (m_SrcIcon) DestroyIcon(m_SrcIcon);
if (m_DstIcon) DestroyIcon(m_DstIcon);
}
// Show the dialog box. Returns true to suppress the original task dialog box
boolCClassicCopyFile::Run( HWND hWnd, IAccessible *pAcc, CString *pLog )
{
// find all interesting controls
EnumAccChildren(pAcc,pLog);
if (!m_YesButton.first || m_YesButton.second!=CHILDID_SELF || !m_NoButton.first || m_NoButton.second!=CHILDID_SELF || !m_Cancel.first)
returnfalse; // something is wrong, do nothing
if (pLog) pLog->Empty(); // success - no need to log anything
// get the info for the source and the destination file (file name, icon, properties)
m_bReadOnly=false;
m_bSystem=false;
GetFileInfo(m_YesButton.first,true);
GetFileInfo(m_NoButton.first,false);
TlsData *pTlsData=GetTlsData();
if (m_CheckBox.first)
pTlsData->bCopyMultiFile=true;
elseif (pTlsData->bCopyMultiFile)
m_bCopyMultiLast=true;
// pick the correct dialog template (for single and multiple files, for LTR and RTL)
int dlg=pTlsData->bCopyMultiFile?(IsLanguageRTL()?IDD_FILEMULTIR:IDD_FILEMULTI):(IsLanguageRTL()?IDD_FILER:IDD_FILE);
HWND parent=GetWindow(GetAncestor(hWnd,GA_ROOT),GW_OWNER);
int res=(int)DialogBoxParam(g_Instance,MAKEINTRESOURCE(dlg),parent,DialogProc,(LPARAM)this);
if (res==IDOK || (res==IDYES && m_bCopyMultiLast))
{
// Yes was pressed, proceed with the operation
m_YesButton.first->accDoDefaultAction(CComVariant(CHILDID_SELF));
}
elseif (res==IDNO)
{
// No
if (m_CheckBox.first && GetKeyState(VK_SHIFT)<0)
m_CheckBox.first->accDoDefaultAction(CComVariant(CHILDID_SELF)); // Shift+No = No to All
PumpMessages(); // messages need to be pumped after every accessibility action. otherwise the next action doesn't work
m_NoButton.first->accDoDefaultAction(CComVariant(CHILDID_SELF));
}
elseif (res==IDYES)
{
// Yes to All
m_CheckBox.first->accDoDefaultAction(CComVariant(CHILDID_SELF));
PumpMessages(); // messages need to be pumped after every accessibility action. otherwise the next action doesn't work
m_YesButton.first->accDoDefaultAction(CComVariant(CHILDID_SELF));
}
if (res==IDCANCEL)
{
// Cancel
if (GetKeyState(VK_SHIFT)<0 || GetKeyState(VK_CONTROL)<0)
returnfalse; // // Shift+Cancel or Ctrl+Cancel was clicked - show the original dialog box
m_Cancel.first->accDoDefaultAction(CComVariant(CHILDID_SELF));
}
if (res==IDC_LINKMORE)
{
// More... was clicked - show the original dialog box
returnfalse;
}
returntrue;
}
voidCClassicCopyFile::PumpMessages( void )
{
MSG msg;
while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
voidCClassicCopyFile::AddAccChild( IAccessible *pAcc, const VARIANT &id, CString *pLog )
{
CComVariant state;
pAcc->get_accState(id,&state);
if (state.intVal&(STATE_SYSTEM_UNAVAILABLE|STATE_SYSTEM_INVISIBLE)) return;
CComVariant role;
pAcc->get_accRole(id,&role);
if (role.intVal==ROLE_SYSTEM_PUSHBUTTON)
{
CComBSTR name;
if (SUCCEEDED(pAcc->get_accName(id,&name)) && name)
{
if (pLog)
LogPrint(pLog,L"button found: '%s', %d\r\n",name,id.intVal);
if (_wcsicmp(name,g_ButtonCopy)==0 || _wcsicmp(name,g_ButtonMove)==0)
{
m_YesButton.first=pAcc;
m_YesButton.second=id.intVal;
if (pLog)
LogPrint(pLog,L"YES button: %p, %d\r\n",pAcc,id.intVal);
}
elseif (_wcsicmp(name,g_ButtonDontCopy)==0 || _wcsicmp(name,g_ButtonDontMove)==0)
{
m_NoButton.first=pAcc;
m_NoButton.second=id.intVal;
if (pLog)
LogPrint(pLog,L"NO button: %p, %d\r\n",pAcc,id.intVal);
}
elseif (_wcsicmp(name,g_ButtonCancel)==0)
{
m_Cancel.first=pAcc;
m_Cancel.second=id.intVal;
if (pLog)
LogPrint(pLog,L"CANCEL button: %p, %d\r\n",pAcc,id.intVal);
}
}
}
if (role.intVal==ROLE_SYSTEM_CHECKBUTTON)
{
// hopefully there is only one checkbox
m_CheckBox.first=pAcc;
m_CheckBox.second=id.intVal;
}
}
voidCClassicCopyFile::EnumAccChildren( IAccessible *pAcc, CString *pLog )
{
AddAccChild(pAcc,CComVariant(CHILDID_SELF),pLog);
long count;
pAcc->get_accChildCount(&count);
CComVariant children[20];
AccessibleChildren(pAcc,0,count,children,&count);
for (int i=0;i<count;i++)
{
if (children[i].vt==VT_DISPATCH)
{
CComQIPtr<IAccessible> pChild=children[i].pdispVal;
if (pChild)
EnumAccChildren(pChild,pLog);
}
else
AddAccChild(pAcc,children[i],pLog);
}
}
voidCClassicCopyFile::GetFileInfo( IAccessible *pAcc, bool bSrc )
{
long count;
pAcc->get_accChildCount(&count);
CComVariant children[20];
AccessibleChildren(pAcc,0,count,children,&count);
wchar_t fname[_MAX_PATH]=L"";
wchar_t dir[_MAX_PATH]=L"";
CString size;
CString date;
// get the file name, directory, size and date
for (int i=0;i<count;i++)
{
CComBSTR name;
if (children[i].vt==VT_DISPATCH)
{
CComQIPtr<IAccessible> pChild=children[i].pdispVal;
if (pChild)
pChild->get_accName(CComVariant(CHILDID_SELF),&name);
}
else
{
pAcc->get_accName(children[i],&name);
}
switch (i)
{
case2: if (wcslen(name)<_countof(fname)) wcscpy_s(fname,name); break;
case3: if (wcslen(name)<_countof(dir)) wcscpy_s(dir,name); break;
case4: size=name; break;
case5: date=name; break;
}
}
if (bSrc)
{
m_FileName=fname;
m_SrcSize=size;
m_SrcTime=date;
}
else
{
m_DstSize=size;
m_DstTime=date;
}
if (!fname[0] || !dir[0]) return;
wchar_t fname2[_MAX_PATH];
memcpy(fname2,fname,sizeof(fname2));
*PathFindExtension(fname2)=0;
int len1=Strlen(fname2);
// the directory text is something like "filename (directory)". we need to parse out the real directory name
int len2=Strlen(dir);
if (dir[0]==0x202A) len1++; // for Arabic the first character is some RTL marker. needs to be skipped
if (dir[len1]==0x1E) len1++; // for Hebrew there is an extra control character after the file name
if (len1+1>=len2 || dir[len1]!=L'' || dir[len1+1]!=L'(' || dir[len2-1]!=L')') return;
if (dir[len1+2]==0x202A) len1++; // for Hebrew the first character is some RTL marker. needs to be skipped
if (dir[len2-2]==0x202C) len2--; // for Hebrew the last character is some RTL marker. needs to be skipped
dir[len2-1]=0;
// construct the full file name
wchar_t path[_MAX_PATH];
_wmakepath_s(path,NULL,dir+len1+2,fname,NULL);
if (!bSrc)
{
DWORD attrib=GetFileAttributes(path);
if (attrib!=INVALID_FILE_ATTRIBUTES)
{
if (attrib&FILE_ATTRIBUTE_READONLY) m_bReadOnly=true;
if (attrib&FILE_ATTRIBUTE_SYSTEM) m_bSystem=true;
}
}
// get file icon
HICON hIcon=NULL;
CAbsolutePidl pidl;
if (SUCCEEDED(SHParseDisplayName(path,NULL,&pidl,0,NULL)) && pidl)
{
int iconSize=GetSystemMetrics(SM_CXICON);
HBITMAP hBitmap=NULL;
CComPtr<IShellItemImageFactory> pFactory;
if (SUCCEEDED(SHCreateItemFromIDList(pidl,IID_IShellItemImageFactory,(void**)&pFactory)) && pFactory)
{
SIZE size={iconSize,iconSize};
if (FAILED(pFactory->GetImage(size,SIIGBF_ICONONLY,&hBitmap)))
hBitmap=NULL;
}
if (hBitmap)
{
HBITMAP hMonoBitmap=CreateBitmap(iconSize,iconSize,1,1,NULL);
ICONINFO info={TRUE,0,0,hMonoBitmap,hBitmap};
hIcon=CreateIconIndirect(&info);
DeleteObject(hMonoBitmap);
DeleteObject(hBitmap);
}
}
if (!hIcon) return;
if (bSrc)
m_SrcIcon=hIcon;
else
m_DstIcon=hIcon;
}
constint WM_BRINGFOREGROUND=WM_USER+11;
INT_PTR CALLBACK CClassicCopyFile::DialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if (uMsg==WM_INITDIALOG)
{
SetWindowText(hwndDlg,FindTranslation(L"Copy.Title",L"Confirm File Replace"));
CClassicCopyFile *pThis=(CClassicCopyFile*)lParam;
wchar_t text[_MAX_PATH*2];
if (pThis->m_bSystem)
{
Sprintf(text,_countof(text),FindTranslation(L"Copy.SubtitleSys",L"This folder already contains a system file named '%s'."),pThis->m_FileName);
if (GetSettingInt(L"OverwriteAlertLevel")>=1)
PlaySound(L".Default",NULL,SND_APPLICATION|SND_ALIAS|SND_ASYNC|SND_NODEFAULT|SND_SYSTEM);
}
elseif (pThis->m_bReadOnly)
{
Sprintf(text,_countof(text),FindTranslation(L"Copy.SubtitleRO",L"This folder already contains a read-only file named '%s'."),pThis->m_FileName);
if (GetSettingInt(L"OverwriteAlertLevel")>=2)
PlaySound(L".Default",NULL,SND_APPLICATION|SND_ALIAS|SND_ASYNC|SND_NODEFAULT|SND_SYSTEM);
}
else
Sprintf(text,_countof(text),FindTranslation(L"Copy.Subtitle",L"This folder already contains a file named '%s'."),pThis->m_FileName);
SetDlgItemText(hwndDlg,IDC_STATICFNAME,text);
// load icon for file conflict (146) from Shell32.dll
HMODULE hShell32=GetModuleHandle(L"Shell32.dll");
pThis->m_Icon=LoadIcon(hShell32,MAKEINTRESOURCE(146));
if (pThis->m_Icon)
SendDlgItemMessage(hwndDlg,IDC_STATICICON1,STM_SETICON,(LPARAM)pThis->m_Icon,0);
// set the localized text
SetDlgItemText(hwndDlg,IDC_STATICPROMPT1,FindTranslation(L"Copy.Prompt1",L"Do you want to replace the existing file:"));
SetDlgItemText(hwndDlg,IDC_STATICDSTSIZE,pThis->m_DstSize);
SetDlgItemText(hwndDlg,IDC_STATICDSTTIME,pThis->m_DstTime);
SetDlgItemText(hwndDlg,IDC_STATICPROMPT2,FindTranslation(L"Copy.Prompt2",L"with this one?"));
SendDlgItemMessage(hwndDlg,IDC_STATICDSTICON,STM_SETICON,(LPARAM)pThis->m_DstIcon,0);
SetDlgItemText(hwndDlg,IDC_STATICSRCSIZE,pThis->m_SrcSize);
SetDlgItemText(hwndDlg,IDC_STATICSRCTIME,pThis->m_SrcTime);
SendDlgItemMessage(hwndDlg,IDC_STATICSRCICON,STM_SETICON,(LPARAM)pThis->m_SrcIcon,0);
SetDlgItemText(hwndDlg,IDOK,FindTranslation(L"Copy.Yes",L"&Yes"));
SetDlgItemText(hwndDlg,IDNO,FindTranslation(L"Copy.No",L"&No"));
if (GetDlgItem(hwndDlg,IDYES))
SetDlgItemText(hwndDlg,IDYES,FindTranslation(L"Copy.YesAll",L"Yes to &All"));
if (GetDlgItem(hwndDlg,IDCANCEL))
SetDlgItemText(hwndDlg,IDCANCEL,FindTranslation(L"Copy.Cancel",L"Cancel"));
Sprintf(text,_countof(text),L"<a>%s</a>",FindTranslation(L"Copy.More",L"&More..."));
SetDlgItemText(hwndDlg,IDC_LINKMORE,text);
PostMessage(hwndDlg,WM_BRINGFOREGROUND,0,0);
returnTRUE;
}
if (uMsg==WM_BRINGFOREGROUND)
{
// bring window to front (sometimes on Windows7 it shows up behind Explorer)
SetForegroundWindow(hwndDlg);
returnTRUE;
}
if (uMsg==WM_COMMAND && (wParam==IDOK || wParam==IDYES || wParam==IDNO || wParam==IDCANCEL))
{
EndDialog(hwndDlg,wParam);
returnTRUE;
}
if (uMsg==WM_NOTIFY)
{
NMHDR *pHdr=(NMHDR*)lParam;
if (pHdr->idFrom==IDC_LINKMORE && (pHdr->code==NM_CLICK || pHdr->code==NM_RETURN))
{
EndDialog(hwndDlg,IDC_LINKMORE);
returnTRUE;
}
}
returnFALSE;
}
///////////////////////////////////////////////////////////////////////////////
// CClassicCopyFolder - this is the implementation of the Copy UI dialog box for folders
classCClassicCopyFolder
{
public:
CClassicCopyFolder( void ) { m_Icon=NULL; m_Original=NULL; }
~CClassicCopyFolder( void );
boolRun( HWND hWnd );
private:
HICON m_Icon;
HWND m_Original;
static INT_PTR CALLBACK DialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );
};
CClassicCopyFolder::~CClassicCopyFolder( void )
{
if (m_Icon) DestroyIcon(m_Icon);
}
// Show the dialog box. Returns true to suppress the original task dialog box
boolCClassicCopyFolder::Run( HWND hWnd )
{
m_Original=hWnd;
constint ID_ALLCHECK=16663;
HWND check=GetDlgItem(hWnd,ID_ALLCHECK);
bool bMulti=(check && (GetWindowLong(check,GWL_STYLE)&WS_VISIBLE));
// pick the correct dialog template (for single and multiple files, for LTR and RTL)
int dlg=bMulti?(IsLanguageRTL()?IDD_FOLDERMULTIR:IDD_FOLDERMULTI):(IsLanguageRTL()?IDD_FOLDERR:IDD_FOLDER);
HWND parent=GetWindow(GetAncestor(hWnd,GA_ROOT),GW_OWNER);
int res=(int)DialogBoxParam(g_Instance,MAKEINTRESOURCE(dlg),parent,DialogProc,(LPARAM)this);
if (res==IDOK) // Yes button for single folder
{
// Yes was pressed, proceed with the operation
PostMessage(hWnd,WM_COMMAND,IDYES,(LPARAM)GetDlgItem(hWnd,IDYES));
}
elseif (res==IDNO)
{
// No
if (bMulti)
{
if (GetKeyState(VK_SHIFT)<0)
{
CheckDlgButton(hWnd,ID_ALLCHECK,BST_CHECKED);
SendMessage(hWnd,WM_COMMAND,ID_ALLCHECK,(LPARAM)check);
}
PostMessage(hWnd,WM_COMMAND,IDNO,(LPARAM)GetDlgItem(hWnd,IDNO)); // Skip
}
else
PostMessage(hWnd,WM_COMMAND,IDCANCEL,(LPARAM)GetDlgItem(hWnd,IDCANCEL)); // No
}
elseif (res==IDYES)
{
// Yes to All
CheckDlgButton(hWnd,ID_ALLCHECK,BST_CHECKED);
SendMessage(hWnd,WM_COMMAND,ID_ALLCHECK,(LPARAM)check);
PostMessage(hWnd,WM_COMMAND,IDYES,(LPARAM)GetDlgItem(hWnd,IDYES));
}
if (res==IDCANCEL)
{
// Cancel
if (GetKeyState(VK_SHIFT)<0 || GetKeyState(VK_CONTROL)<0)
returnfalse; // // Shift+Cancel or Ctrl+Cancel was clicked - show the original dialog box
PostMessage(hWnd,WM_COMMAND,IDCANCEL,(LPARAM)GetDlgItem(hWnd,IDCANCEL)); // No
}
if (res==IDC_LINKMORE)
{
// More... was clicked - show the original dialog box
returnfalse;
}
returntrue;
}
INT_PTR CALLBACK CClassicCopyFolder::DialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if (uMsg==WM_INITDIALOG)
{
SetWindowText(hwndDlg,FindTranslation(L"Folder.Title",L"Confirm Folder Replace"));
CClassicCopyFolder *pThis=(CClassicCopyFolder*)lParam;
wchar_t text[2048];
// find the link control and get its text
HWND link=FindWindowEx(pThis->m_Original,NULL,WC_LINK,NULL);
if (link)
GetWindowText(link,text,_countof(text));
else
text[0]=0;
Strcat(text,_countof(text),L"\r\n\r\n");
Strcat(text,_countof(text),FindTranslation(L"Folder.Prompt",L"Do you still want to move or copy the folder?"));
SetDlgItemText(hwndDlg,IDC_STATICFNAME,text);
// load icon for file conflict (146) from Shell32.dll
HMODULE hShell32=GetModuleHandle(L"Shell32.dll");
pThis->m_Icon=LoadIcon(hShell32,MAKEINTRESOURCE(146));
if (pThis->m_Icon)
SendDlgItemMessage(hwndDlg,IDC_STATICICON1,STM_SETICON,(LPARAM)pThis->m_Icon,0);
// set the localized text
SetDlgItemText(hwndDlg,IDOK,FindTranslation(L"Copy.Yes",L"&Yes"));
SetDlgItemText(hwndDlg,IDNO,FindTranslation(L"Copy.No",L"&No"));
if (GetDlgItem(hwndDlg,IDYES))
SetDlgItemText(hwndDlg,IDYES,FindTranslation(L"Copy.YesAll",L"Yes to &All"));
if (GetDlgItem(hwndDlg,IDCANCEL))
SetDlgItemText(hwndDlg,IDCANCEL,FindTranslation(L"Copy.Cancel",L"Cancel"));
Sprintf(text,_countof(text),L"<a>%s</a>",FindTranslation(L"Copy.More",L"&More..."));
SetDlgItemText(hwndDlg,IDC_LINKMORE,text);
PostMessage(hwndDlg,WM_BRINGFOREGROUND,0,0);
returnTRUE;
}
if (uMsg==WM_BRINGFOREGROUND)
{
// bring window to front (sometimes on Windows7 it shows up behind Explorer)
SetForegroundWindow(hwndDlg);
returnTRUE;
}
if (uMsg==WM_COMMAND && (wParam==IDOK || wParam==IDYES || wParam==IDNO || wParam==IDCANCEL))
{
EndDialog(hwndDlg,wParam);
returnTRUE;
}
if (uMsg==WM_NOTIFY)
{
NMHDR *pHdr=(NMHDR*)lParam;
if (pHdr->idFrom==IDC_LINKMORE && (pHdr->code==NM_CLICK || pHdr->code==NM_RETURN))
{
EndDialog(hwndDlg,IDC_LINKMORE);
returnTRUE;
}
}
returnFALSE;
}
///////////////////////////////////////////////////////////////////////////////
static CComPtr<IAccessible> FindMoreButton( IAccessible *pAcc )
{
CComVariant state;
CComVariant self(CHILDID_SELF);
pAcc->get_accState(self,&state);
if (!(state.intVal&(STATE_SYSTEM_UNAVAILABLE|STATE_SYSTEM_INVISIBLE)))
{
CComVariant role;
pAcc->get_accRole(self,&role);
if (role.intVal==ROLE_SYSTEM_PUSHBUTTON)
{
CComBSTR name;
if (SUCCEEDED(pAcc->get_accName(self,&name)) && name && _wcsicmp(name,g_ButtonMore)==0)
return pAcc;
}
}
CComVariant children[30];
long count;
AccessibleChildren(pAcc,0,_countof(children),children,&count);
for (int i=0;i<count;i++)
{
if (children[i].vt==VT_DISPATCH)
{
CComQIPtr<IAccessible> pChild=children[i].pdispVal;
if (pChild)
{
CComPtr<IAccessible> pRes=FindMoreButton(pChild);
if (pRes) return pRes;
}
}
}
returnNULL;
}
///////////////////////////////////////////////////////////////////////////////
static LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData )
{
if (uMsg==WM_WINDOWPOSCHANGING)
{
WINDOWPOS *pos=(WINDOWPOS*)lParam;
if (pos->flags&SWP_SHOWWINDOW)
{
wchar_t title[256];
GetWindowText(hWnd,title,_countof(title));
if (_wcsicmp(title,g_TitleMove)==0 || _wcsicmp(title,g_TitleCopy)==0)
{
// file UI
if (GetSettingBool(L"ReplaceFileUI"))
{
CString log;
CComPtr<IAccessible> pAcc;
HRESULT h=AccessibleObjectFromWindow(hWnd,OBJID_WINDOW,IID_IAccessible,(void**)&pAcc);
if (SUCCEEDED(h) && pAcc)
{
CClassicCopyFile copy;
if (copy.Run(hWnd,pAcc,g_bLogLevel?&log:NULL))
{
pos->x=pos->y=-20000;
pos->flags&=~(SWP_SHOWWINDOW|SWP_NOMOVE);
}
}
elseif (g_bLogLevel)
LogPrint(&log,L"AccessibleObjectFromWindow: error=0x%X, hWnd=0x%p",h,hWnd);
if (g_bLogLevel)
{
wchar_t fname[_MAX_PATH]=L"%LOCALAPPDATA%\\OpenShell";
DoEnvironmentSubst(fname,_MAX_PATH);
SHCreateDirectory(NULL,fname);
Strcat(fname,_countof(fname),L"\\ExplorerLog.txt");
if (!log.IsEmpty())
{
FILE *f;
if (_wfopen_s(&f,fname,L"wb")==0)
{
fwprintf(f,L"\xFEFF");
OSVERSIONINFOEX ver={sizeof(ver)};
#pragma warning(suppress:4996)
GetVersionEx((OSVERSIONINFO*)&ver);
fwprintf(f,L"version = %d.%d.%d - %d.%d\r\n\r\n",ver.dwMajorVersion,ver.dwMinorVersion,ver.dwBuildNumber,ver.wServicePackMajor,ver.wServicePackMinor);
wchar_t languages[100]={0};
ULONG size=4; // up to 4 languages
ULONG len=_countof(languages);
GetUserPreferredUILanguages(MUI_LANGUAGE_NAME,&size,languages,&len);
for (constwchar_t *lang=languages;*lang;lang+=wcslen(lang)+1)
fwprintf(f,L"language = %s\r\n",lang);
fwprintf(f,L"\r\n");
fwprintf(f,L"g_ButtonMove = '%s'\r\n",g_ButtonMove);
fwprintf(f,L"g_ButtonDontMove = '%s'\r\n",g_ButtonDontMove);
fwprintf(f,L"g_ButtonCopy = '%s'\r\n",g_ButtonCopy);
fwprintf(f,L"g_ButtonDontCopy = '%s'\r\n",g_ButtonDontCopy);
fwprintf(f,L"g_ButtonCancel = '%s'\r\n",g_ButtonCancel);
fwprintf(f,L"\r\n");
fwrite((constwchar_t*)log,log.GetLength(),2,f);
fclose(f);
}
}
else
DeleteFile(fname);
}
}
}
elseif (_wcsicmp(title,g_TitleFolder)==0)
{
// folder UI
if (GetSettingBool(L"ReplaceFolderUI"))
{
CClassicCopyFolder copy;
if (copy.Run(hWnd))
{
pos->x=pos->y=-20000;
pos->flags&=~(SWP_SHOWWINDOW|SWP_NOMOVE);
}
}
}
else
{
// look for progress bar
if (GetSettingBool(L"EnableMore"))
{
HWND progress=FindChildWindow(hWnd,PROGRESS_CLASS);
if (progress)
{
bool bDef;
int delay=GetSettingInt(L"MoreProgressDelay",bDef);
if (bDef)
{
delay=0;
if (GetWinVersion()>=WIN_VER_WIN7)
{
BOOL comp;
if (SUCCEEDED(DwmIsCompositionEnabled(&comp)) && comp)
delay=500;
}
}
SetTimer(hWnd,'CLEX',delay,NULL);
returnDefSubclassProc(hWnd,uMsg,wParam,lParam);
}
}
}
LRESULT res=DefSubclassProc(hWnd,uMsg,wParam,lParam);
RemoveWindowSubclass(hWnd,WindowProc,uIdSubclass);
return res;
}
}
if (uMsg==WM_TIMER && wParam=='CLEX')
{
KillTimer(hWnd,wParam);
CComPtr<IAccessible> pAcc;
HRESULT h=AccessibleObjectFromWindow(hWnd,OBJID_WINDOW,IID_IAccessible,(void**)&pAcc);
if (SUCCEEDED(h) && pAcc)
{
CComPtr<IAccessible> pMore=FindMoreButton(pAcc);
if (pMore) pMore->accDoDefaultAction(CComVariant(CHILDID_SELF));
}
LRESULT res=DefSubclassProc(hWnd,uMsg,wParam,lParam);
RemoveWindowSubclass(hWnd,WindowProc,uIdSubclass);
return res;
}
returnDefSubclassProc(hWnd,uMsg,wParam,lParam);
}
LRESULT CALLBACK ClassicCopyHook( int nCode, WPARAM wParam, LPARAM lParam )
{
if (nCode==HCBT_CREATEWND)
{
HWND hWnd=(HWND)wParam;
CBT_CREATEWND *create=(CBT_CREATEWND*)lParam;
HINSTANCE hInst=(HINSTANCE)GetWindowLongPtr(hWnd,GWLP_HINSTANCE);
if (create->lpcs->lpszName && (intptr_t)create->lpcs->lpszClass==32770 && hInst==g_hShell32)
{
WaitDllInitThread();
static LONG id;
int i=InterlockedIncrement(&id);
SetWindowSubclass(hWnd,WindowProc,i,0);
}
}
TlsData *pTlsData=GetTlsData();
returnCallNextHookEx(pTlsData->copyHook,nCode,wParam,lParam);
}
voidInitClassicCopyProcess( void )
{
// load UI text from shell32.dll
// the text is used to locate controls in the copy dialog by name
g_hShell32=GetModuleHandle(L"shell32.dll");
LoadString(g_hShell32,17027,g_TitleMove,256);
LoadString(g_hShell32,17024,g_TitleCopy,256);
LoadString(g_hShell32,16705,g_TitleFolder,256);
LoadString(g_hShell32,13610,g_ButtonMove,256);
LoadString(g_hShell32,13623,g_ButtonDontMove,256);
LoadString(g_hShell32,13604,g_ButtonCopy,256);
LoadString(g_hShell32,13606,g_ButtonDontCopy,256);
LoadString(g_hShell32,13588,g_ButtonCancel,256);
LoadString(g_hShell32,32992,g_ButtonMore,256);
}
voidInitClassicCopyThread( void )
{
TlsData *pTlsData=GetTlsData();
if (!pTlsData->copyHook)
pTlsData->copyHook=SetWindowsHookEx(WH_CBT,ClassicCopyHook,g_Instance,GetCurrentThreadId());
}
voidFreeClassicCopyThread( void )
{
TlsData *pTlsData=GetTlsData();
if (pTlsData->copyHook)
{
UnhookWindowsHookEx(pTlsData->copyHook);
pTlsData->copyHook=NULL;
}
}