dsrc isn't necessary for this repo
[client-tools.git] / src / external / 3rd / application / UiBuilder / DiffDialogBox.cpp
blob39cdd52d41dbac58032f8890ac6e1f0dce126b97
1 //////////////////////////////////////////////////////////////////////////
3 #include "FirstUiBuilder.h"
4 #include "DiffDialogBox.h"
5 #include "UIBaseObject.h"
6 #include "UILowerString.h"
8 //////////////////////////////////////////////////////////////////////////
10 #include "resource.h"
11 #include <vector>
12 #include <algorithm>
13 #include <memory>
15 namespace DiffDialogBoxNamespace
17 std::string s_diffStringBuffer;
20 using namespace DiffDialogBoxNamespace;
22 //////////////////////////////////////////////////////////////////////////
24 bool DiffDialogBox::DiffObjects(HWND hwndParent, UIBaseObject const & lhs, UIBaseObject const & rhs)
26 // Do the Diff inline.
27 s_diffStringBuffer = lhs.GetFullPath();
28 s_diffStringBuffer += " and ";
29 s_diffStringBuffer += rhs.GetFullPath();
31 // Get properties on the left.
32 UIBaseObject::UIPropertyNameVector allProperties;
33 lhs.GetPropertyNames(allProperties, false);
35 // Get properties on the right.
36 rhs.GetPropertyNames(allProperties, false);
38 // Merge.
39 std::sort(allProperties.begin(), allProperties.end());
40 allProperties.erase(std::unique(allProperties.begin(), allProperties.end()), allProperties.end());
43 // Diff if both objects have it and add to diff text.
44 bool hasDifferences = false;
45 for (UIBaseObject::UIPropertyNameVector::const_iterator itPropertyName = allProperties.begin(); itPropertyName != allProperties.end(); ++itPropertyName)
47 UIString lhsValue;
48 UIString rhsValue;
49 UILowerString const & property = *itPropertyName;
51 if (property == UIBaseObject::PropertyName::Name)
53 continue;
56 lhs.GetProperty(property, lhsValue);
57 rhs.GetProperty(property, rhsValue);
59 UILowerString lhsLowerValue(UIUnicode::wideToNarrow(lhsValue));
60 UILowerString rhsLowerValue(UIUnicode::wideToNarrow(rhsValue));
62 // Add Diff!
63 if (lhsLowerValue != rhsLowerValue)
65 if (!hasDifferences)
67 s_diffStringBuffer += allProperties.size() == 1 ? " has the following difference:\r\n\r\n" :
68 " have the following differences:\r\n\r\n";
69 hasDifferences = true;
72 s_diffStringBuffer += "[";
73 s_diffStringBuffer += property.c_str();
74 s_diffStringBuffer += "]\t";
75 s_diffStringBuffer += lhsLowerValue.empty() ? "[EMPTY]" : lhsLowerValue.c_str();
76 s_diffStringBuffer += " <=> ";
77 s_diffStringBuffer += rhsLowerValue.empty() ? "[EMPTY]" : rhsLowerValue.c_str();
78 s_diffStringBuffer += "\r\n";
82 if (!hasDifferences)
84 s_diffStringBuffer += " are not different.";
88 if (DialogBox(GetModuleHandle(0), MAKEINTRESOURCE(IDD_DIFF_WINDOW), hwndParent, DialogProc))
90 return true;
92 else
94 return false;
98 //////////////////////////////////////////////////////////////////////////
100 BOOL CALLBACK DiffDialogBox::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
102 UI_UNREF(lParam);
104 switch(uMsg)
106 case WM_INITDIALOG:
108 const int height = HIWORD(GetDialogBaseUnits());
109 HWND hControl = GetDlgItem(hwndDlg, IDC_VALUE);
111 HFONT fixedFont = CreateFont(height * 3 / 4, 0,
112 0, 0,
113 0, 0, 0, 0,
114 ANSI_CHARSET,
115 OUT_DEFAULT_PRECIS,
116 CLIP_DEFAULT_PRECIS,
117 DEFAULT_QUALITY,
118 FF_DONTCARE,
119 "lucida console");
121 SendMessage(hControl, WM_SETFONT, reinterpret_cast<WPARAM>(fixedFont), 1);
123 SetDlgItemText(hwndDlg, IDC_DIFF_EDIT, s_diffStringBuffer.c_str());
124 return TRUE;
127 case WM_CLOSE:
128 // Equivalent to pressing cancel
129 EndDialog(hwndDlg, 0);
130 return 0;
132 case WM_COMMAND:
133 if(LOWORD(wParam) == IDOK)
135 EndDialog(hwndDlg, 1);
137 else if(LOWORD(wParam) == IDCANCEL)
139 EndDialog(hwndDlg, 0);
141 return 0;
143 default:
144 return 0;