-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWebDiffWindow.hpp
More file actions
1584 lines (1456 loc) · 43.9 KB
/
WebDiffWindow.hpp
File metadata and controls
1584 lines (1456 loc) · 43.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "WinWebDiffLib.h"
#include "WebWindow.hpp"
#include "DiffHighlighter.hpp"
#include "DiffLocation.hpp"
#include <shellapi.h>
#include <wil/win32_helpers.h>
class CWebDiffWindow : public IWebDiffWindow
{
public:
CWebDiffWindow()
{
}
~CWebDiffWindow()
{
}
bool Create(HINSTANCE hInstance, HWND hWndParent, int nID, const RECT& rc)
{
m_hInstance = hInstance;
MyRegisterClass(hInstance);
m_hWnd = CreateWindowExW(0, L"WinWebDiffWindowClass", nullptr, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN,
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, hWndParent, reinterpret_cast<HMENU>((intptr_t)nID), hInstance, this);
return m_hWnd ? true : false;
}
bool Destroy()
{
BOOL bSucceeded = true;
if (m_hWnd)
bSucceeded = DestroyWindow(m_hWnd);
m_hWnd = nullptr;
return !!bSucceeded;
}
bool IsWebView2Installed() const override
{
wil::unique_cotaskmem_string version_info;
HRESULT hr = GetAvailableCoreWebView2BrowserVersionString(nullptr, &version_info);
return (hr == S_OK && version_info != nullptr);
}
bool DownloadWebView2() const override
{
return ShellExecute(0, 0, L"https://go.microsoft.com/fwlink/p/?LinkId=2124703", 0, 0, SW_SHOW);
}
void AddEventListener(IWebDiffEventHandler* handler) override
{
m_listeners.push_back(handler);
}
void SetUserDataFolderType(UserdataFolderType userDataFolderType, bool perPane) override
{
m_userDataFolderType = userDataFolderType;
m_bUserDataFolderPerPane = perPane;
}
HRESULT New(int nUrls, IWebDiffCallback* callback) override
{
const wchar_t* urls[3] = { L"about:blank", L"about:blank", L"about:blank" };
return Open(nUrls, urls, callback);
}
HRESULT Open(const wchar_t* url1, const wchar_t* url2, IWebDiffCallback* callback) override
{
const wchar_t* urls[3] = { url1, url2, nullptr };
return Open(2, urls, callback);
}
HRESULT Open(const wchar_t* url1, const wchar_t* url2, const wchar_t* url3, IWebDiffCallback* callback) override
{
const wchar_t* urls[3] = { url1, url2, url3 };
return Open(3, urls, callback);
}
HRESULT Open(int nPanes, const wchar_t* urls[3], IWebDiffCallback* callback)
{
HRESULT hr = S_OK;
m_nPanes = nPanes;
if (m_hWnd)
{
Close();
for (int i = 0; i < nPanes; ++i)
{
std::wstring userDataFolder = GetUserDataFolderPath(i);
ComPtr<IWebDiffCallback> callback2(callback);
hr = m_webWindow[i].Create(this, m_hInstance, m_hWnd, urls[i], userDataFolder.c_str(),
m_size, m_fitToWindow, m_zoom, s_bDarkBackgroundEnabled, m_userAgent, nullptr,
[this, i, callback2](WebDiffEvent::EVENT_TYPE event, IUnknown* sender, IUnknown* args)
{
WebDiffEvent ev{};
ev.type = event;
ev.pane = i;
if (event == WebDiffEvent::SourceChanged)
{
m_webWindow[ev.pane].SetZoom(m_zoom);
}
else if (event == WebDiffEvent::ZoomFactorChanged)
{
m_zoom = m_webWindow[ev.pane].GetZoom();
for (int pane = 0; pane < m_nPanes; ++pane)
{
if (pane != ev.pane)
m_webWindow[pane].SetZoom(m_zoom);
}
}
else if (event == WebDiffEvent::HSCROLL)
{
for (int pane = 0; pane < m_nPanes; ++pane)
{
if (pane != ev.pane)
m_webWindow[pane].SetHScrollPos(m_webWindow[ev.pane].GetHScrollPos());
}
}
else if (event == WebDiffEvent::VSCROLL)
{
for (int pane = 0; pane < m_nPanes; ++pane)
{
if (pane != ev.pane)
m_webWindow[pane].SetVScrollPos(m_webWindow[ev.pane].GetVScrollPos());
}
}
else if (event == WebDiffEvent::NavigationStarting)
{
m_documentLoaded[ev.pane] = false;
m_urlChanged[ev.pane] = true;
SetCompareState(NOT_COMPARED);
}
else if (event == WebDiffEvent::FrameNavigationStarting)
{
}
else if (event == WebDiffEvent::NavigationCompleted)
{
addEventListener(sender, ev.pane, nullptr);
m_documentLoaded[ev.pane] = true;
if ((std::count(m_documentLoaded, m_documentLoaded + m_nPanes, true) == m_nPanes) &&
(std::count(m_urlChanged, m_urlChanged + m_nPanes, true) == m_nPanes))
{
std::fill_n(m_urlChanged, m_nPanes, false);
Recompare(callback2.Get());
}
}
else if (event == WebDiffEvent::FrameNavigationCompleted)
{
addEventListener(sender,ev.pane, nullptr);
}
else if (event == WebDiffEvent::GoBacked)
{
if (m_bSynchronizeEvents && GetSyncEventFlag(EVENT_GOBACKFORWARD))
{
for (int pane = 0; pane < m_nPanes; ++pane)
{
if (ev.pane == pane)
continue;
m_webWindow[pane].GoBack();
}
}
}
else if (event == WebDiffEvent::GoForwarded)
{
if (m_bSynchronizeEvents && GetSyncEventFlag(EVENT_GOBACKFORWARD))
{
for (int pane = 0; pane < m_nPanes; ++pane)
{
if (ev.pane == pane)
continue;
m_webWindow[pane].GoForward();
}
}
}
else if (event == WebDiffEvent::WebMessageReceived || event == WebDiffEvent::FrameWebMessageReceived)
{
std::wstring msg = m_webWindow[i].GetWebMessage();
#ifdef _DEBUG
wchar_t buf[4096];
wsprintfW(buf, L"WebMessageReceived(pane:%d): %s\n", ev.pane, msg.c_str());
OutputDebugString(buf);
#endif
WDocument doc;
doc.Parse(msg.c_str());
std::wstring event = doc.HasMember(L"event") ? doc[L"event"].GetString() : L"";
std::wstring window = doc.HasMember(L"window") ? doc[L"window"].GetString() : L"";
if (event == L"dblclick")
{
const int diffIndex = doc[L"wwdid"].GetInt();
if (diffIndex > -1)
SelectDiff(diffIndex);
}
else if (event == L"scroll")
{
if (m_bSynchronizeEvents && GetSyncEventFlag(EVENT_SCROLL))
syncEvent(ev.pane, msg);
for (int pane = 0; pane < m_nPanes; ++pane)
m_webWindow[pane].PostWebMessageAsJsonInAllFrames(L"{\"event\": \"diffRects\"}");
}
else if (event == L"click")
{
if (m_bSynchronizeEvents && GetSyncEventFlag(EVENT_CLICK))
syncEvent(ev.pane, msg);
}
else if (event == L"input")
{
if (m_bSynchronizeEvents && GetSyncEventFlag(EVENT_INPUT))
syncEvent(ev.pane, msg);
}
else if (event == L"diffRects")
{
m_diffLocation[ev.pane].read(doc);
}
}
RaiseEvent(ev);
});
}
std::vector<RECT> rects = CalcChildWebWindowRect(m_hWnd, m_nPanes, m_bHorizontalSplit);
for (int i = 0; i < m_nPanes; ++i)
m_webWindow[i].SetWindowRect(rects[i]);
}
return hr;
}
void Close() override
{
for (int i = 0; i < m_nPanes; ++i)
m_webWindow[i].Destroy();
}
void NewTab(int pane, const wchar_t *url, IWebDiffCallback* callback) override
{
if (pane < 0 || pane >= m_nPanes || !m_hWnd)
return;
m_webWindow[pane].NewTab(url, m_zoom, m_userAgent, callback);
}
void CloseActiveTab(int pane) override
{
if (pane < 0 || pane >= m_nPanes || !m_hWnd)
return;
m_webWindow[pane].CloseActiveTab();
}
HRESULT Reload(int pane) override
{
if (pane < 0 || pane >= m_nPanes || !m_hWnd)
return E_INVALIDARG;
return m_webWindow[pane].Reload();
}
HRESULT ReloadAll() override
{
HRESULT hr = S_OK;
for (int pane = 0; pane < m_nPanes; ++pane)
hr = m_webWindow[pane].Reload();
return hr;
}
HRESULT Recompare(IWebDiffCallback* callback) override
{
return compare(callback);
}
HRESULT SaveFile(int pane, FormatType kind, const wchar_t* filename, IWebDiffCallback* callback) override
{
if (pane < 0 || pane >= m_nPanes)
return false;
ComPtr<IWebDiffCallback> callback2(callback);
std::wstring sfilename(filename);
return unhighlightDifferencesLoop(
Callback<IWebDiffCallback>([this, kind, pane, sfilename, callback2](const WebDiffCallbackResult& result) -> HRESULT
{
HRESULT hr = result.errorCode;
if (SUCCEEDED(hr))
{
hr = m_webWindow[pane].SaveFile(sfilename, kind,
Callback<IWebDiffCallback>([this, callback2](const WebDiffCallbackResult& result) -> HRESULT
{
HRESULT hr = result.errorCode;
if (SUCCEEDED(hr))
hr = compare(callback2.Get());
if (FAILED(hr) && callback2)
return callback2->Invoke({ hr, nullptr });
return S_OK;
}).Get());
}
if (FAILED(hr) && callback2)
return callback2->Invoke({ hr, nullptr });
return S_OK;
}).Get());
}
HRESULT SaveFiles(FormatType kind, const wchar_t* filenames[], IWebDiffCallback* callback) override
{
auto sfilenames = std::make_shared<std::vector<std::wstring>>();
for (int pane = 0; pane < m_nPanes; ++pane)
sfilenames->push_back(filenames[pane]);
ComPtr<IWebDiffCallback> callback2(callback);
return unhighlightDifferencesLoop(
Callback<IWebDiffCallback>([this, kind, sfilenames, callback2](const WebDiffCallbackResult& result) -> HRESULT
{
HRESULT hr = result.errorCode;
if (SUCCEEDED(hr))
{
hr = saveFilesLoop(kind, sfilenames,
Callback<IWebDiffCallback>([this, sfilenames, callback2](const WebDiffCallbackResult& result) -> HRESULT
{
HRESULT hr = result.errorCode;
if (SUCCEEDED(hr))
hr = compare(callback2.Get());
if (FAILED(hr) && callback2)
return callback2->Invoke({ hr, nullptr });
return S_OK;
}).Get());
}
if (FAILED(hr) && callback2)
return callback2->Invoke({ hr, nullptr });
return S_OK;
}).Get());
}
HRESULT SaveDiffFiles(FormatType kind, const wchar_t* filenames[], IWebDiffCallback* callback) override
{
auto sfilenames = std::make_shared<std::vector<std::wstring>>();
for (int pane = 0; pane < m_nPanes; ++pane)
sfilenames->push_back(filenames[pane]);
ComPtr<IWebDiffCallback> callback2(callback);
HRESULT hr = saveFilesLoop(kind, sfilenames,
Callback<IWebDiffCallback>([this, sfilenames, callback2](const WebDiffCallbackResult& result) -> HRESULT
{
if (callback2)
return callback2->Invoke({ result.errorCode, nullptr });
return S_OK;
}).Get());
return hr;
}
HRESULT ClearBrowsingData(int pane, BrowsingDataType datakinds) override
{
int spane = pane, epane = pane;
if (pane < 0 || pane >= m_nPanes)
{
spane = 0;
epane = m_nPanes - 1;
}
HRESULT hr = S_OK;
for (int i = spane; i <= epane; ++i)
{
if (FAILED(hr = m_webWindow[i].ClearBrowsingData(datakinds)))
return hr;
}
return hr;
}
const wchar_t* GetCurrentUrl(int pane) override
{
if (pane < 0 || pane >= m_nPanes)
return L"";
return m_webWindow[pane].GetCurrentUrl();
}
int GetPaneCount() const override
{
return m_nPanes;
}
RECT GetPaneWindowRect(int pane) const override
{
if (pane < 0 || pane >= m_nPanes || !m_hWnd)
{
RECT rc = { -1, -1, -1, -1 };
return rc;
}
return m_webWindow[pane].GetWindowRect();
}
RECT GetWindowRect() const override
{
if (!m_hWnd)
return RECT{ 0 };
RECT rc, rcParent;
HWND hwndParent = GetParent(m_hWnd);
::GetWindowRect(hwndParent, &rcParent);
::GetWindowRect(m_hWnd, &rc);
rc.left -= rcParent.left;
rc.top -= rcParent.top;
rc.right -= rcParent.left;
rc.bottom -= rcParent.top;
return rc;
}
bool SetWindowRect(const RECT& rc) override
{
if (m_hWnd)
MoveWindow(m_hWnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE);
return true;
}
int GetActivePane() const override
{
if (!m_hWnd)
return -1;
for (int i = 0; i < m_nPanes; ++i)
if (m_webWindow[i].IsFocused())
return i;
return -1;
}
void SetActivePane(int pane) override
{
m_webWindow[pane].SetFocus();
}
bool GetHorizontalSplit() const override
{
return m_bHorizontalSplit;
}
void SetHorizontalSplit(bool horizontalSplit) override
{
if (!m_hWnd)
return;
m_bHorizontalSplit = horizontalSplit;
std::vector<RECT> rects = CalcChildWebWindowRect(m_hWnd, m_nPanes, m_bHorizontalSplit);
for (int i = 0; i < m_nPanes; ++i)
m_webWindow[i].SetWindowRect(rects[i]);
}
void GetDiffColorSettings(IWebDiffWindow::ColorSettings& settings) const override
{
settings = m_colorSettings;
}
void SetDiffColorSettings(const IWebDiffWindow::ColorSettings& settings) override
{
m_colorSettings = settings;
}
bool IsDarkBackgroundEnabled() const
{
return s_bDarkBackgroundEnabled;
}
void SetDarkBackgroundEnabled(bool enabled)
{
if (s_bDarkBackgroundEnabled == enabled)
return;
s_bDarkBackgroundEnabled = enabled;
DeleteObject(s_hbrBackground);
s_hbrBackground = CreateSolidBrush(s_bDarkBackgroundEnabled ? RGB(0, 0, 0) : GetSysColor(COLOR_3DFACE));
if (m_hWnd)
{
SetClassLongPtr(m_hWnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_hbrBackground);
InvalidateRect(m_hWnd, NULL, TRUE);
for (int pane = 0; pane < m_nPanes; ++pane)
m_webWindow[pane].SetDarkBackgroundEnabled(s_bDarkBackgroundEnabled);
}
}
double GetZoom() const override
{
return m_zoom;
}
void SetZoom(double zoom) override
{
if (zoom == m_zoom)
return;
m_zoom = std::clamp(zoom, 0.25, 5.0);
for (int pane = 0; pane < m_nPanes; ++pane)
m_webWindow[pane].SetZoom(m_zoom);
}
const wchar_t *GetUserAgent() const override
{
if (m_nPanes == 0)
return L"";
return m_userAgent.c_str();
}
void SetUserAgent(const wchar_t* userAgent) override
{
m_userAgent = userAgent;
for (int pane = 0; pane < m_nPanes; ++pane)
m_webWindow[pane].SetUserAgent(m_userAgent);
}
bool GetFitToWindow() const override
{
return m_fitToWindow;
}
void SetFitToWindow(bool fitToWindow) override
{
m_fitToWindow = fitToWindow;
for (int pane = 0; pane < m_nPanes; ++pane)
m_webWindow[pane].SetFitToWindow(fitToWindow);
}
SIZE GetSize() const override
{
return m_size;
}
void SetSize(const SIZE size) override
{
m_size = size;
for (int pane = 0; pane < m_nPanes; ++pane)
m_webWindow[pane].SetSize(size);
}
bool GetShowDifferences() const override
{
return m_bShowDifferences;
}
void SetShowDifferences(bool visible) override
{
if (visible == m_bShowDifferences)
return;
m_bShowDifferences = visible;
Recompare(nullptr);
}
bool GetShowWordDifferences() const override
{
return m_bShowWordDifferences;
}
void SetShowWordDifferences(bool visible) override
{
if (visible == m_bShowWordDifferences)
return;
m_bShowWordDifferences = visible;
Recompare(nullptr);
}
const DiffOptions& GetDiffOptions() const override
{
return m_diffOptions;
}
void SetDiffOptions(const DiffOptions& diffOptions) override
{
m_diffOptions = diffOptions;
}
bool GetSyncEvents() const
{
return m_bSynchronizeEvents;
}
void SetSyncEvents(bool syncEvents)
{
m_bSynchronizeEvents = syncEvents;
}
unsigned GetSyncEventFlags() const
{
return m_eventSyncFlags;
}
void SetSyncEventFlags(unsigned flags)
{
m_eventSyncFlags = flags;
}
bool GetSyncEventFlag(EventType event) const
{
return (m_eventSyncFlags & event) != 0;
}
void SetSyncEventFlag(EventType event, bool flag)
{
if (flag)
m_eventSyncFlags = m_eventSyncFlags | static_cast<unsigned>(event);
else
m_eventSyncFlags = m_eventSyncFlags & ~static_cast<unsigned>(event);
}
CompareState GetCompareState() const
{
return m_compareState;
}
void SetCompareState(CompareState compareState)
{
CompareState oldCompareState = m_compareState;
m_compareState = compareState;
if (m_compareState != oldCompareState)
{
WebDiffEvent ev{ WebDiffEvent::CompareStateChanged, -1 };
RaiseEvent(ev);
if (compareState == CompareState::COMPARED || compareState == CompareState::NOT_COMPARED)
{
if (ev.pane >= 0)
m_diffLocation[ev.pane].clear();
}
if (compareState == CompareState::COMPARED)
{
for (int pane = 0; pane < m_nPanes; ++pane)
m_webWindow[pane].PostWebMessageAsJsonInAllFrames(L"{\"event\": \"diffRects\"}");
}
}
}
LogCallback GetLogCallback() const
{
return m_logCallback;
}
void SetLogCallback(LogCallback logCallback)
{
m_logCallback = logCallback;
}
int GetDiffCount() const override
{
return static_cast<int>(m_diffInfos.size());
}
int GetConflictCount() const override
{
int conflictCount = 0;
for (size_t i = 0; i < m_diffInfos.size(); ++i)
if (m_diffInfos[i].op == OP_DIFF)
++conflictCount;
return conflictCount;
}
int GetCurrentDiffIndex() const override
{
return m_currentDiffIndex;
}
bool FirstDiff() override
{
int oldDiffIndex = m_currentDiffIndex;
if (m_diffInfos.size() == 0)
m_currentDiffIndex = -1;
else
m_currentDiffIndex = 0;
if (oldDiffIndex == m_currentDiffIndex)
return false;
return SUCCEEDED(selectDiff(m_currentDiffIndex, nullptr));
}
bool LastDiff() override
{
int oldDiffIndex = m_currentDiffIndex;
m_currentDiffIndex = static_cast<int>(m_diffInfos.size()) - 1;
if (oldDiffIndex == m_currentDiffIndex)
return false;
return SUCCEEDED(selectDiff(m_currentDiffIndex, nullptr));
}
bool NextDiff() override
{
int oldDiffIndex = m_currentDiffIndex;
++m_currentDiffIndex;
if (static_cast<size_t>(m_currentDiffIndex) >= m_diffInfos.size())
m_currentDiffIndex = static_cast<int>(m_diffInfos.size()) - 1;
if (oldDiffIndex == m_currentDiffIndex)
return false;
return SUCCEEDED(selectDiff(m_currentDiffIndex, nullptr));
}
bool PrevDiff() override
{
int oldDiffIndex = m_currentDiffIndex;
if (m_diffInfos.size() == 0)
m_currentDiffIndex = -1;
else
{
--m_currentDiffIndex;
if (m_currentDiffIndex < 0)
m_currentDiffIndex = 0;
}
if (oldDiffIndex == m_currentDiffIndex)
return false;
return SUCCEEDED(selectDiff(m_currentDiffIndex, nullptr));
}
bool FirstConflict() override
{
int oldDiffIndex = m_currentDiffIndex;
for (size_t i = 0; i < m_diffInfos.size(); ++i)
if (m_diffInfos[i].op == OP_DIFF)
m_currentDiffIndex = static_cast<int>(i);
if (oldDiffIndex == m_currentDiffIndex)
return false;
return SUCCEEDED(selectDiff(m_currentDiffIndex, nullptr));
}
bool LastConflict() override
{
int oldDiffIndex = m_currentDiffIndex;
for (int i = static_cast<int>(m_diffInfos.size() - 1); i >= 0; --i)
{
if (m_diffInfos[i].op == OP_DIFF)
{
m_currentDiffIndex = i;
break;
}
}
if (oldDiffIndex == m_currentDiffIndex)
return false;
return SUCCEEDED(selectDiff(m_currentDiffIndex, nullptr));
}
bool NextConflict() override
{
int oldDiffIndex = m_currentDiffIndex;
for (size_t i = m_currentDiffIndex + 1; i < m_diffInfos.size(); ++i)
{
if (m_diffInfos[i].op == OP_DIFF)
{
m_currentDiffIndex = static_cast<int>(i);
break;
}
}
if (oldDiffIndex == m_currentDiffIndex)
return false;
return SUCCEEDED(selectDiff(m_currentDiffIndex, nullptr));
}
bool PrevConflict() override
{
int oldDiffIndex = m_currentDiffIndex;
for (int i = m_currentDiffIndex - 1; i >= 0; --i)
{
if (m_diffInfos[i].op == OP_DIFF)
{
m_currentDiffIndex = i;
break;
}
}
if (oldDiffIndex == m_currentDiffIndex)
return false;
return SUCCEEDED(selectDiff(m_currentDiffIndex, nullptr));
}
bool SelectDiff(int diffIndex) override
{
if (diffIndex < 0 || static_cast<size_t>(diffIndex) >= m_diffInfos.size())
return false;
m_currentDiffIndex = diffIndex;
return SUCCEEDED(selectDiff(diffIndex, nullptr));
}
int GetNextDiffIndex() const override
{
if (m_diffInfos.size() == 0 || m_currentDiffIndex >= static_cast<int>(m_diffInfos.size() - 1))
return -1;
return m_currentDiffIndex + 1;
}
int GetPrevDiffIndex() const override
{
if (m_diffInfos.size() == 0 || m_currentDiffIndex <= 0)
return -1;
return m_currentDiffIndex - 1;
}
int GetNextConflictIndex() const override
{
for (size_t i = m_currentDiffIndex + 1; i < m_diffInfos.size(); ++i)
if (m_diffInfos[i].op == OP_DIFF)
return static_cast<int>(i);
return -1;
}
int GetPrevConflictIndex() const override
{
for (int i = static_cast<int>(m_currentDiffIndex - 1); i >= 0; --i)
if (m_diffInfos[i].op == OP_DIFF)
return i;
return -1;
}
HWND GetHWND() const override
{
return m_hWnd;
}
HWND GetPaneHWND(int pane) const override
{
if (pane < 0 || pane >= m_nPanes)
return nullptr;
return m_webWindow[pane].GetHWND();
}
bool Copy() override
{
return execCommand(L"copy");
}
bool Cut() override
{
return execCommand(L"cut");
}
bool Delete() override
{
return execCommand(L"delete");
}
bool Paste() override
{
return execCommand(L"paste");
}
bool SelectAll() override
{
return execCommand(L"selectall");
}
bool Undo() override
{
return execCommand(L"undo");
}
bool Redo() override
{
return execCommand(L"redo");
}
bool CanUndo() override
{
return true;
}
bool CanRedo() override
{
return true;
}
void RaiseEvent(const WebDiffEvent& e) override
{
for (const auto& listener : m_listeners)
listener->Invoke(e);
}
std::vector<DiffLocation::DiffRect> GetDiffRectArray(int pane)
{
return m_diffLocation[pane].getDiffRectArray();
}
std::vector<DiffLocation::ContainerRect> GetContainerRectArray(int pane)
{
return m_diffLocation[pane].getContainerRectArray();
}
DiffLocation::Rect GetVisibleAreaRect(int pane)
{
return m_diffLocation[pane].getVisibleAreaRect();
}
void ScrollTo(int pane, float scrollX, float scrollY)
{
std::wstring json = L"{\"event\": \"scrollTo\", \"window\": \"\", \"scrollX\": " + std::to_wstring(scrollX) + L", \"scrollY\": " + std::to_wstring(scrollY) + L"}";
m_webWindow[pane].PostWebMessageAsJsonInAllFrames(json.c_str());
}
private:
HRESULT getDocumentsLoop(std::shared_ptr<std::vector<std::wstring>> jsons, IWebDiffCallback* callback, int pane = 0)
{
static const wchar_t* method = L"DOM.getDocument";
static const wchar_t* params = L"{ \"depth\": -1, \"pierce\": true }";
ComPtr<IWebDiffCallback> callback2(callback);
HRESULT hr = m_webWindow[pane].CallDevToolsProtocolMethod(method, params,
Callback<IWebDiffCallback>([this, pane, jsons, callback2](const WebDiffCallbackResult& result) -> HRESULT
{
HRESULT hr = result.errorCode;
if (SUCCEEDED(hr))
{
jsons->push_back(result.returnObjectAsJson);
if (pane + 1 < m_nPanes)
hr = getDocumentsLoop(jsons, callback2.Get(), pane + 1);
else if (callback2)
return callback2->Invoke({ hr, nullptr });
}
if (FAILED(hr) && callback2)
return callback2->Invoke({ hr, nullptr });
return S_OK;
}).Get());
return hr;
}
HRESULT addEventListener(IUnknown* sender, int pane, IWebDiffCallback* callback)
{
return m_webWindow[pane].ExecuteScript(sender, GetScriptOnLoad(), callback);
}
HRESULT syncEvent(int srcPane, const std::wstring& json)
{
for (int pane = 0; pane < m_nPanes; ++pane)
{
if (pane == srcPane)
continue;
m_webWindow[pane].PostWebMessageAsJsonInAllFrames(json.c_str());
}
return S_OK;
}
HRESULT compare(IWebDiffCallback* callback)
{
SetCompareState(COMPARING);
ComPtr<IWebDiffCallback> callback2(callback);
std::shared_ptr<std::vector<std::wstring>> jsons(new std::vector<std::wstring>());
HRESULT hr = getDocumentsLoop(jsons,
Callback<IWebDiffCallback>([this, jsons, callback2](const WebDiffCallbackResult& result) -> HRESULT
{
HRESULT hr = result.errorCode;
if (SUCCEEDED(hr))
{
std::vector<TextSegments> textSegments(m_nPanes);
std::shared_ptr<std::vector<WDocument>> documents(new std::vector<WDocument>(m_nPanes));
for (int pane = 0; pane < m_nPanes; ++pane)
{
(*documents)[pane].Parse((*jsons)[pane].c_str());
#ifdef _DEBUG
WStringBuffer buffer;
WPrettyWriter writer(buffer);
(*documents)[pane].Accept(writer);
WriteToTextFile((L"c:\\tmp\\file" + std::to_wstring(pane) + L".json"),
buffer.GetString());
#endif
Highlighter::unhighlightNodes((*documents)[pane][L"root"], (*documents)[pane].GetAllocator());
textSegments[pane].Make((*documents)[pane][L"root"]);
}
m_diffInfos = Comparer::compare(m_diffOptions, textSegments);
Comparer::setNodeIdInDiffInfoList(m_diffInfos, textSegments);
if (m_currentDiffIndex != -1 && static_cast<size_t>(m_currentDiffIndex) >= m_diffInfos.size())
m_currentDiffIndex = static_cast<int>(m_diffInfos.size() - 1);
if (m_bShowDifferences)
{
Highlighter highlighter(*documents.get(), m_diffInfos, m_colorSettings, m_diffOptions, m_bShowWordDifferences, m_currentDiffIndex);
highlighter.highlightNodes();
#ifdef _DEBUG
for (int pane = 0; pane < m_nPanes; ++pane)
{
WStringBuffer buffer;
WPrettyWriter writer(buffer);
(*documents)[pane].Accept(writer);
WriteToTextFile((L"c:\\tmp\\file" + std::to_wstring(pane) + L"_1.json"),
buffer.GetString());
}
#endif
}
hr = highlightDocuments(documents,
Callback<IWebDiffCallback>([this, callback2](const WebDiffCallbackResult& result) -> HRESULT
{
HRESULT hr = result.errorCode;
if (SUCCEEDED(hr))
{
hr = setStyleSheetLoop(Highlighter::getStyleSheetText(m_currentDiffIndex, m_colorSettings).c_str(),
Callback<IWebDiffCallback>([this, callback2](const WebDiffCallbackResult& result) -> HRESULT
{
HRESULT hr = result.errorCode;
SetCompareState(FAILED(hr) ? NOT_COMPARED : COMPARED);
if (callback2)
return callback2->Invoke({ hr, nullptr });
return S_OK;
}).Get());
}
if (FAILED(hr))
SetCompareState(NOT_COMPARED);
if (FAILED(hr) && callback2)
return callback2->Invoke({ hr, nullptr });
return S_OK;
}).Get());
}
if (FAILED(hr))
SetCompareState(NOT_COMPARED);
if (FAILED(hr) && callback2)
return callback2->Invoke({ hr, nullptr });
return S_OK;
}).Get());
if (FAILED(hr))
SetCompareState(NOT_COMPARED);
return hr;
}
HRESULT saveFilesLoop(FormatType kind, std::shared_ptr<std::vector<std::wstring>> filenames, IWebDiffCallback* callback, int pane = 0)
{
ComPtr<IWebDiffCallback> callback2(callback);
HRESULT hr = m_webWindow[pane].SaveFile((*filenames)[pane].c_str(), kind,
Callback<IWebDiffCallback>([this, kind, filenames, callback2, pane](const WebDiffCallbackResult& result) -> HRESULT
{
HRESULT hr = result.errorCode;
if (SUCCEEDED(hr))
{
if (pane + 1 < m_nPanes)
hr = saveFilesLoop(kind, filenames, callback2.Get(), pane + 1);
else if (callback2)
return callback2->Invoke({ hr, nullptr });
}