|
| 1 | +// MainDlg.cpp : implementation of the CMainDlg class |
| 2 | +// |
| 3 | +///////////////////////////////////////////////////////////////////////////// |
| 4 | + |
| 5 | +#include "stdafx.h" |
| 6 | +#include "resource.h" |
| 7 | +#include "PParseHandlers.hpp" |
| 8 | +#include "XercesVal.h" |
| 9 | +#include "aboutdlg.h" |
| 10 | +#include "MainDlg.h" |
| 11 | + |
| 12 | +static bool isXsdFilePath(const wchar_t* path) { |
| 13 | + const wchar_t* ext = wcsrchr(path, '.'); |
| 14 | + return ext && wcscmp(ext + 1, L"xsd") == 0; |
| 15 | +} |
| 16 | + |
| 17 | +BOOL CMainDlg::PreTranslateMessage(MSG* pMsg) |
| 18 | +{ |
| 19 | + if (pMsg->message == WM_LBUTTONDBLCLK && |
| 20 | + pMsg->hwnd == ::GetDlgItem(m_hWnd, IDC_EDIT1)) |
| 21 | + { |
| 22 | + CEdit editBox(GetDlgItem(IDC_EDIT1)); |
| 23 | + CAtlString basestr; |
| 24 | + CSimpleStringW str(basestr.GetManager()); |
| 25 | + editBox.GetWindowTextW(str); |
| 26 | + if (str.GetLength() > 0 && OpenClipboard()) |
| 27 | + { |
| 28 | + int len = (str.GetLength() + 1) * sizeof(str[0]); |
| 29 | + HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, len); |
| 30 | + LPVOID lpText = NULL; |
| 31 | + if (hGlobal != NULL) |
| 32 | + lpText = GlobalLock(hGlobal); |
| 33 | + if (lpText != NULL) |
| 34 | + { |
| 35 | + memcpy(lpText, str.GetBuffer(), len); |
| 36 | + EmptyClipboard(); |
| 37 | + GlobalUnlock(hGlobal); |
| 38 | + SetClipboardData(CF_UNICODETEXT, hGlobal); |
| 39 | + } |
| 40 | + CloseClipboard(); |
| 41 | + ::MessageBoxW(m_hWnd, _T("Text copied."), L"Copy to clipboard", MB_OK); |
| 42 | + } |
| 43 | + |
| 44 | + return TRUE; //Important!!! Message is handled |
| 45 | + } |
| 46 | + return CWindow::IsDialogMessage(pMsg); |
| 47 | +} |
| 48 | + |
| 49 | +BOOL CMainDlg::OnIdle() |
| 50 | +{ |
| 51 | + UIUpdateChildWindows(); |
| 52 | + return FALSE; |
| 53 | +} |
| 54 | + |
| 55 | +LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) |
| 56 | +{ |
| 57 | + // center the dialog on the screen |
| 58 | + CenterWindow(); |
| 59 | + |
| 60 | + // set icons |
| 61 | + HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON)); |
| 62 | + SetIcon(hIcon, TRUE); |
| 63 | + HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON)); |
| 64 | + SetIcon(hIconSmall, FALSE); |
| 65 | + |
| 66 | + // register object for message filtering and idle updates |
| 67 | + CMessageLoop* pLoop = _Module.GetMessageLoop(); |
| 68 | + ATLASSERT(pLoop != NULL); |
| 69 | + pLoop->AddMessageFilter(this); |
| 70 | + pLoop->AddIdleHandler(this); |
| 71 | + |
| 72 | + UIAddChildWindowContainer(m_hWnd); |
| 73 | + InitDynamicLayout(false); |
| 74 | + |
| 75 | + validator.reset(new XercesValidator); |
| 76 | + xmlState = eLoadedState::NO; |
| 77 | + xsdState = eLoadedState::NO; |
| 78 | + |
| 79 | + if (const int argc = __argc; argc > 1) { |
| 80 | + CStringW xsdPath; |
| 81 | + CStringW xmlPath; |
| 82 | + for (int i = 1; i < argc; ++i) { |
| 83 | + const wchar_t* path = __wargv[i]; |
| 84 | + if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(path)) { |
| 85 | + ::MessageBoxW(m_hWnd, path, L"File not found!", MB_OK | MB_ICONERROR); |
| 86 | + break; |
| 87 | + } |
| 88 | + if (isXsdFilePath(path)) |
| 89 | + xsdPath = path; |
| 90 | + else |
| 91 | + xmlPath = path; |
| 92 | + } |
| 93 | + if (!xsdPath.IsEmpty()) |
| 94 | + validateFile(xsdPath); |
| 95 | + if (!xmlPath.IsEmpty()) |
| 96 | + validateFile(xmlPath); |
| 97 | + } |
| 98 | + |
| 99 | + return TRUE; |
| 100 | +} |
| 101 | + |
| 102 | +LRESULT CMainDlg::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) |
| 103 | +{ |
| 104 | + // unregister message filtering and idle updates |
| 105 | + CMessageLoop* pLoop = _Module.GetMessageLoop(); |
| 106 | + ATLASSERT(pLoop != NULL); |
| 107 | + pLoop->RemoveMessageFilter(this); |
| 108 | + pLoop->RemoveIdleHandler(this); |
| 109 | + |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | +LRESULT CMainDlg::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) |
| 114 | +{ |
| 115 | + CAboutDlg dlg; |
| 116 | + dlg.DoModal(); |
| 117 | + return 0; |
| 118 | +} |
| 119 | + |
| 120 | +LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) |
| 121 | +{ |
| 122 | + CSimpleFileDialog fileDialog(TRUE, |
| 123 | + NULL, |
| 124 | + NULL, |
| 125 | + OFN_HIDEREADONLY, |
| 126 | + L"XML Files (*.xsd;*.xml)\0*.xsd;*.xml\00"); |
| 127 | + |
| 128 | + if (fileDialog.DoModal() == IDOK) |
| 129 | + { |
| 130 | + validateFile(fileDialog.m_szFileName); |
| 131 | + } return 0; |
| 132 | +} |
| 133 | + |
| 134 | +LRESULT CMainDlg::OnDropFile(HDROP hDropInfo) |
| 135 | +{ |
| 136 | + TCHAR path[MAX_PATH] = { 0 }; |
| 137 | + CStringW xsdPath; |
| 138 | + CStringW xmlPath; |
| 139 | + |
| 140 | + UINT uFile = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, NULL); |
| 141 | + if (uFile != 0) { |
| 142 | + for (UINT i = 0; i < uFile; i++) { |
| 143 | + path[0] = '\0'; |
| 144 | + if (DragQueryFile(hDropInfo, i, path, MAX_PATH)) { |
| 145 | + if (isXsdFilePath(path)) |
| 146 | + xsdPath = path; |
| 147 | + else |
| 148 | + xmlPath = path; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + DragFinish(hDropInfo); |
| 153 | + if (!xsdPath.IsEmpty()) |
| 154 | + validateFile(xsdPath); |
| 155 | + if (!xmlPath.IsEmpty()) |
| 156 | + validateFile(xmlPath); |
| 157 | + return 0; |
| 158 | +} |
| 159 | + |
| 160 | +void CMainDlg::validateFile(const wchar_t* path) |
| 161 | +{ |
| 162 | + bool isXsdFile = isXsdFilePath(path); |
| 163 | + std::wstringstream errors; |
| 164 | + bool success = isXsdFile ? validator->setAndValidateXsd(path, errors) : validator->setAndValidateXml(path, errors); |
| 165 | + CStatic xsdBox(GetDlgItem(IDC_XSD)); |
| 166 | + CStatic xmlBox(GetDlgItem(IDC_XML)); |
| 167 | + |
| 168 | + if (isXsdFile) { |
| 169 | + xsdState = success ? eLoadedState::YES : eLoadedState::INVALID; |
| 170 | + xsdBox.SetWindowTextW(success ? L"VALID" : L"INVALID"); |
| 171 | + |
| 172 | + xmlState = eLoadedState::NO; |
| 173 | + xmlBox.SetWindowTextW(L""); |
| 174 | + } |
| 175 | + else { |
| 176 | + if (xsdState == eLoadedState::INVALID) { |
| 177 | + xsdState = eLoadedState::NO; |
| 178 | + xsdBox.SetWindowTextW(L""); |
| 179 | + } |
| 180 | + xmlState = success ? eLoadedState::YES : eLoadedState::INVALID; |
| 181 | + xmlBox.SetWindowTextW(success ? L"VALID" : L"INVALID"); |
| 182 | + } |
| 183 | + updateFileLables(path, isXsdFile); |
| 184 | + CEdit editBox(GetDlgItem(IDC_EDIT1)); |
| 185 | + editBox.SetWindowTextW(success ? L"" : errors.str().c_str()); |
| 186 | +} |
| 187 | + |
| 188 | +void CMainDlg::updateFileLables(const wchar_t* _path, bool isXsd) |
| 189 | +{ |
| 190 | + const wchar_t* sep = wcsrchr(_path, '\\'); |
| 191 | + const wchar_t* path = sep ? sep + 1 : _path; |
| 192 | + CStatic xsdBox(GetDlgItem(IDC_XSD_SELECTED)); |
| 193 | + CStatic xmlBox(GetDlgItem(IDC_XML_SELECTED)); |
| 194 | + |
| 195 | + std::wstring s; |
| 196 | + if (isXsd) { |
| 197 | + s = L"XSD file selected: "; |
| 198 | + s += path; |
| 199 | + xsdBox.SetWindowTextW(s.c_str()); |
| 200 | + xmlBox.SetWindowTextW(L"XML file selected: NONE"); |
| 201 | + } |
| 202 | + else { |
| 203 | + if (xsdState == eLoadedState::NO) { |
| 204 | + xsdBox.SetWindowTextW(L"XSD file selected: NONE"); |
| 205 | + } |
| 206 | + s = L"XML file selected: "; |
| 207 | + s += path; |
| 208 | + xmlBox.SetWindowTextW(s.c_str()); |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | + |
| 213 | +LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) |
| 214 | +{ |
| 215 | + CloseDialog(wID); |
| 216 | + return 0; |
| 217 | +} |
| 218 | + |
| 219 | +void CMainDlg::CloseDialog(int nVal) |
| 220 | +{ |
| 221 | + DestroyWindow(); |
| 222 | + ::PostQuitMessage(nVal); |
| 223 | +} |
| 224 | + |
| 225 | +LRESULT CMainDlg::OnStaticCtlColor(HDC hdcStatic, HWND hwnd) |
| 226 | +{ |
| 227 | + static HBRUSH greenBrush(CreateSolidBrush(RGB(0, 255, 0))); |
| 228 | + static HBRUSH redBrush(CreateSolidBrush(RGB(255, 0, 0))); |
| 229 | + |
| 230 | + if (GetDlgItem(IDC_XSD).m_hWnd == hwnd && xsdState != eLoadedState::NO) { |
| 231 | + //::SetTextColor(hdcStatic, RGB(0, 0, 0)); |
| 232 | + //::SetBkColor(hdcStatic, RGB(0, 255, 0)); |
| 233 | + ::SetBkMode(hdcStatic, TRANSPARENT); |
| 234 | + ::SelectObject(hdcStatic, xsdState == eLoadedState::YES ? greenBrush : redBrush); |
| 235 | + return (LRESULT)::GetCurrentObject(hdcStatic, OBJ_BRUSH); |
| 236 | + } |
| 237 | + else if (GetDlgItem(IDC_XML).m_hWnd == hwnd && xmlState != eLoadedState::NO) { |
| 238 | + ::SetBkMode(hdcStatic, TRANSPARENT); |
| 239 | + ::SelectObject(hdcStatic, xmlState == eLoadedState::YES ? greenBrush : redBrush); |
| 240 | + return (LRESULT)::GetCurrentObject(hdcStatic, OBJ_BRUSH); |
| 241 | + } |
| 242 | + else { |
| 243 | + this->SetMsgHandled(FALSE); |
| 244 | + return 0; |
| 245 | + } |
| 246 | +} |
0 commit comments