code_production/cfg-tools/apps/scanner-check/CDlgMgr.cpp

632 lines
16 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// CDlgMgr.cpp: 实现文件
//
#include "pch.h"
#include "scanner-check.h"
#include "CDlgMgr.h"
#include "afxdialogex.h"
#include <vector>
#include <string>
#include <file/file_util.h>
#include <coding/coding.h>
// CDlgMgr 对话框
static std::wstring vid_pid_edit_org_proc = L"vid_pid_edit_org_proc";
static wchar_t* hex = L"";
IMPLEMENT_DYNAMIC(CDlgMgr, CDialogEx)
CDlgMgr::CDlgMgr(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MANAGER, pParent)
{
}
CDlgMgr::~CDlgMgr()
{
}
static bool is_checked(CWnd* ctrl)
{
return ((CButton*)ctrl)->GetCheck() == BST_CHECKED;
}
static bool is_checked(CDialogEx* dlg, UINT id)
{
return is_checked(dlg->GetDlgItem(id));
}
static void set_checked(CWnd* ctrl, bool checked)
{
((CButton*)ctrl)->SetCheck(checked ? BST_CHECKED : BST_UNCHECKED);
}
static void set_checked(CDialogEx* dlg, UINT id, bool checked)
{
return set_checked(dlg->GetDlgItem(id), checked);
}
static LRESULT vid_pid_edit_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
wchar_t now[40] = { 0 };
int len = 0, sel = 0, max_len = 4;
bool all_digit = true;
WNDPROC wndproc = (WNDPROC)GetPropW(hwnd, vid_pid_edit_org_proc.c_str());
std::wstring buf(L"");
if (!wndproc)
wndproc = DefWindowProcW;
switch (msg)
{
case WM_CHAR:
GetWindowTextW(hwnd, now, _countof(now));
len = lstrlenW(now);
sel = SendMessage(hwnd, EM_GETSEL, 0, 0);
sel = ((sel >> 16) & 0x0ff) - (sel & 0x0ffff);
if (wp >= ' ' && len - sel >= max_len)
return 0;
if (wp >= '0' && wp <= '9')
wp = hex[wp - '0'];
else if (wp >= 'A' && wp <= 'F')
wp = hex[wp - 'A' + 10];
else if (wp >= 'a' && wp <= 'f')
wp = hex[wp - 'a' + 10];
else if(wp >= ' ')
return 0;
break;
case WM_COPY:
return 0;
case WM_PASTE:
{
size_t l = 0;
if (file_util::get_clipboard(NULL, &l, CF_UNICODETEXT) == ERROR_INSUFFICIENT_BUFFER)
{
buf.resize(l + 4);
l++;
if (file_util::get_clipboard(&buf[0], &l, CF_UNICODETEXT))
l = 0;
}
if (l == 0)
return 0;
lp = (LPARAM)&buf[0];
msg = WM_SETTEXT;
}
case WM_SETTEXT:
for (int i = 0; len < max_len + 1 && i < lstrlenW((wchar_t*)lp); ++i)
{
wchar_t ch = ((wchar_t*)lp)[i];
if (ch >= '0' && ch <= '9')
now[len++] = hex[ch - '0'];
else if (ch >= 'A' && ch <= 'F')
{
now[len++] = hex[ch - 'A' + 10];
all_digit = false;
}
else if (ch >= 'a' && ch <= 'f')
{
now[len++] = hex[ch - 'a' + 10];
all_digit = false;
}
else
break;
}
if(len == 0)
return 0;
if (/*len == max_len + 1 &&*/ all_digit)
{
sel = _wtoi(now);
//if (sel <= 0x0ffff)
{
now[max_len] = 0;
for (int l = max_len - 1; l >= 0; --l, sel >>= 4)
now[l] = hex[sel % 16];
}
//else
// now[max_len] = 0;
}
lp = (LPARAM)now;
break;
}
return wndproc(hwnd, msg, wp, lp);
}
static void set_vid_pid_proc(HWND hwnd)
{
SetPropW(hwnd, vid_pid_edit_org_proc.c_str(), (HANDLE)(FARPROC)GetWindowLong(hwnd, GWL_WNDPROC));
SetWindowLong(hwnd, GWL_WNDPROC, (LONG)(FARPROC)vid_pid_edit_proc);
}
static WORD vid_pid_edit_value(HWND hwnd)
{
wchar_t text[40] = { 0 };
int off = 10;
WORD vpid = 0;
GetWindowTextW(hwnd, text + off, _countof(text) - off - 1);
for (int i = 0; text[off + i]; ++i)
{
const wchar_t* ptr = NULL;
text[0] = text[off + i];
ptr = wcsstr(hex, text);
if (!ptr)
break;
vpid <<= 4;
vpid += ptr - hex;
}
return vpid;
}
void CDlgMgr::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, list_);
DDX_Control(pDX, IDC_COMBO_ITEM, combo_);
DDX_Control(pDX, IDC_IPADDRESS1, ip_);
}
BEGIN_MESSAGE_MAP(CDlgMgr, CDialogEx)
ON_BN_CLICKED(IDC_CHECK2, &CDlgMgr::OnBnClickedCheckReport)
ON_MESSAGE(WM_USER + 1, &CDlgMgr::OnDisableIPCtrl)
ON_BN_CLICKED(IDC_BUTTON_MAINTAIN, &CDlgMgr::OnBnClickedButtonMaintain)
ON_BN_CLICKED(IDC_BUTTON_ADD, &CDlgMgr::OnBnClickedButtonAdd)
ON_CBN_SELCHANGE(IDC_COMBO_ITEM, &CDlgMgr::OnCbnSelchangeComboItem)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CDlgMgr::OnNMDblclkList1)
ON_BN_CLICKED(IDC_BUTTON_ADD_ALL, &CDlgMgr::OnBnClickedButtonAddAll)
ON_WM_DROPFILES()
END_MESSAGE_MAP()
static DWORD WINAPI disable_ip_ctrl(LPVOID lp)
{
Sleep(500);
PostMessage((HWND)lp, WM_USER + 1, 0, 0);
return 0;
}
LRESULT CDlgMgr::OnDisableIPCtrl(WPARAM, LPARAM)
{
OnBnClickedCheckReport();
return 0;
}
// CDlgMgr 消息处理程序
BOOL CDlgMgr::OnInitDialog()
{
CDialogEx::OnInitDialog();
int ind = list_.InsertColumn(list_.GetHeaderCtrl()->GetItemCount(), TEXT("\u5E8F\u53F7"), 0, 40);
list_.InsertColumn(list_.GetHeaderCtrl()->GetItemCount(), TEXT("\u6D4B\u8BD5\u9879\u76EE"), 0, 171);
list_.InsertColumn(list_.GetHeaderCtrl()->GetItemCount(), TEXT("\u5FC5\u987B\u8054\u673A\u6D4B\u8BD5"), 0, 86);
list_.InsertColumn(list_.GetHeaderCtrl()->GetItemCount(), TEXT("\u4E0D\u901A\u8FC7\u5219\u505C\u6B62"), 0, 86);
list_.SetExtendedStyle((list_.GetExStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES) & (~LVS_EX_CHECKBOXES));
list_.ModifyStyle(0, LVS_SINGLESEL);
set_checked(this, IDC_CHECK_ONLINE, true);
set_checked(this, IDC_CHECK_FATAL, true);
combo_.AddString(L"船形开关");
combo_.AddString(L"外观");
combo_.AddString(L"显示屏");
combo_.AddString(L"歪斜");
combo_.AddString(L"超声波");
set_vid_pid_proc(GetDlgItem(IDC_EDIT_VID_SRC)->m_hWnd);
set_vid_pid_proc(GetDlgItem(IDC_EDIT_PID_SRC)->m_hWnd);
set_vid_pid_proc(GetDlgItem(IDC_EDIT_VID_TO)->m_hWnd);
set_vid_pid_proc(GetDlgItem(IDC_EDIT_PID_TO)->m_hWnd);
SetDlgItemInt(IDC_EDIT_VID_SRC, 0x3072);
SetDlgItemInt(IDC_EDIT_PID_SRC, 0x200);
SetDlgItemInt(IDC_EDIT_VID_TO, 0x3072);
SetDlgItemInt(IDC_EDIT_PID_TO, 0x0239);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CDlgMgr::OnBnClickedCheckReport()
{
// TODO: 在此添加控件通知处理程序代码
bool enable = is_checked(this, IDC_CHECK2);
GetDlgItem(IDC_IPADDRESS1)->EnableWindow(enable);
GetDlgItem(IDC_IPADDRESS1)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_EDIT_IP)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_EDIT_PORT)->EnableWindow(enable);
GetDlgItem(IDC_EDIT_DB)->EnableWindow(enable);
if (enable)
GotoDlgCtrl(GetDlgItem(IDC_IPADDRESS1));
}
void CDlgMgr::OnBnClickedButtonMaintain()
{
// TODO: 在此添加控件通知处理程序代码
::PostMessage(GetParent()->m_hWnd, WM_TO_ITEM_MGR_TAB, 0, 0);
}
void CDlgMgr::OnBnClickedButtonAdd()
{
// TODO: 在此添加控件通知处理程序代码
wchar_t text[128] = { 0 };
bool online = is_checked(this, IDC_CHECK_ONLINE), fatal = is_checked(this, IDC_CHECK_FATAL);
int ind = -1;
if (::GetWindowTextW(GetDlgItem(IDC_COMBO_ITEM)->m_hWnd, text, _countof(text) - 1) == 0)
return;
for (int i = 0; i < list_.GetItemCount(); ++i)
{
wchar_t val[128] = { 0 };
list_.GetItemText(i, 1, val, _countof(val) - 1);
if (wcscmp(text, val) == 0)
{
ind = i;
break;
}
}
if (ind == -1)
{
wchar_t sn[20] = { 0 };
swprintf_s(sn, _countof(sn) - 1, L"%u", list_.GetItemCount() + 1);
ind = list_.InsertItem(list_.GetItemCount(), sn);
list_.SetItemText(ind, 1, text);
}
list_.SetItemText(ind, 2, online ? L"true" : L"false");
list_.SetItemText(ind, 3, fatal ? L"true" : L"false");
list_.SetItemState(ind, LVNI_FOCUSED | LVIS_SELECTED, LVNI_FOCUSED | LVIS_SELECTED);
list_.EnsureVisible(ind, FALSE);
}
void CDlgMgr::OnCbnSelchangeComboItem()
{
// TODO: 在此添加控件通知处理程序代码
wchar_t text[128] = { 0 }, val[128] = { 0 };
page_config::ITEM item;
::GetWindowTextW(GetDlgItem(IDC_COMBO_ITEM)->m_hWnd, text, _countof(text) - 1);
for (int i = 0; i < list_.GetItemCount(); ++i)
{
list_.GetItemText(i, 1, val, _countof(val) - 1);
if (wcscmp(text, val) == 0)
{
list_.SetItemState(i, LVNI_FOCUSED | LVIS_SELECTED, LVNI_FOCUSED | LVIS_SELECTED);
list_.GetItemText(i, 2, val, _countof(val) - 1);
set_checked(this, IDC_CHECK_ONLINE, wcscmp(val, L"false") == 0);
list_.GetItemText(i, 3, val, _countof(val) - 1);
set_checked(this, IDC_CHECK_FATAL, wcscmp(val, L"true") == 0);
return;
}
}
::SendMessageW(GetParent()->m_hWnd, WM_GET_TEST_ITEM_NAME, (WPARAM)text, (LPARAM)&item);
if (item.title == text)
{
set_checked(this, IDC_CHECK_ONLINE, !item.man);
set_checked(this, IDC_CHECK_FATAL, item.fatal);
}
}
void CDlgMgr::OnNMDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
POINT pos = { 0 };
RECT r = { 0 };
GetCursorPos(&pos);
list_.ScreenToClient(&pos);
r.left = list_.HitTest(pos);
if (r.left != -1)
{
std::vector<int> width;
int ind = r.left;
for (int i = 0; i < list_.GetHeaderCtrl()->GetItemCount(); ++i)
{
RECT r = { 0 };
list_.GetHeaderCtrl()->GetItemRect(i, &r);
width.push_back(r.right - r.left);
}
for (int i = 0; i < width.size(); ++i)
{
if (pos.x <= width[i])
{
wchar_t text[128] = { 0 };
if (i < 2)
{
std::wstring tips(L"\u5220\u9664\u6D4B\u8BD5\u9879\uFF1A");
list_.GetItemText(ind, 1, text, _countof(text) - 1);
tips += text;
tips += L"\uFF1F";
if (::MessageBoxW(m_hWnd,tips.c_str(), L"\u786E\u8BA4", MB_YESNO | MB_ICONQUESTION) == IDYES)
{
list_.DeleteItem(ind);
for (; ind < list_.GetItemCount(); ++ind)
{
swprintf_s(text, _countof(text) - 1, L"%u", ind + 1);
list_.SetItemText(ind, 0, text);
}
}
}
else
{
list_.GetItemText(ind, i, text, _countof(text) - 1);
if (wcscmp(text, L"true"))
list_.SetItemText(ind, i, L"true");
else
list_.SetItemText(ind, i, L"false");
}
break;
}
pos.x -= width[i];
}
}
*pResult = 0;
}
void CDlgMgr::OnBnClickedButtonAddAll()
{
// TODO: 在此添加控件通知处理程序代码
for (int i = 0; i < combo_.GetCount(); ++i)
{
combo_.SetCurSel(i);
OnCbnSelchangeComboItem();
OnBnClickedButtonAdd();
}
}
void CDlgMgr::init_test_items(std::vector<page_config::ITEM>& items)
{
combo_.ResetContent();
for (size_t i = 0; i < items.size(); ++i)
combo_.AddString(items[i].title.c_str());
if (items.size())
{
combo_.SetCurSel(0);
OnCbnSelchangeComboItem();
}
}
INTER_MODULE_CALLBACK(got_wstr)
{
*((std::wstring*)param) += std::wstring((const wchar_t*)data, len / 2);
return inter_module_data::SET_RESULT_CONTINUE;
}
std::wstring CDlgMgr::export_config(bool *ok, bool used_in_code, int code_ver)
{
known_file_util::IJsonW * jsn = known_file_util::create_jsonW(),
* child = known_file_util::create_jsonW();
wchar_t text[256] = { 0 };
if (!used_in_code)
{
WORD vid_f = vid_pid_edit_value(GetDlgItem(IDC_EDIT_VID_SRC)->m_hWnd),
pid_f = vid_pid_edit_value(GetDlgItem(IDC_EDIT_PID_SRC)->m_hWnd),
vid_t = vid_pid_edit_value(GetDlgItem(IDC_EDIT_VID_TO)->m_hWnd),
pid_t = vid_pid_edit_value(GetDlgItem(IDC_EDIT_PID_TO)->m_hWnd),
port = 0;
bool result = false, report = is_checked(GetDlgItem(IDC_CHECK2));
BYTE ip1 = 0, ip2 = 0, ip3 = 0, ip4 = 0;
if (!ok)
ok = &result;
*ok = false;
if (!vid_f)
{
GotoDlgCtrl(GetDlgItem(IDC_EDIT_VID_SRC));
return L"请重新输入 源VID!";
}
if (!pid_f)
{
GotoDlgCtrl(GetDlgItem(IDC_EDIT_PID_SRC));
return L"请重新输入 源PID!";
}
if (!vid_t)
{
GotoDlgCtrl(GetDlgItem(IDC_EDIT_VID_TO));
return L"请重新输入 目标VID!";
}
if (!pid_t)
{
GotoDlgCtrl(GetDlgItem(IDC_EDIT_PID_TO));
return L"请重新输入 目标PID!";
}
if (list_.GetItemCount() == 0)
return L"没有输入有效的测试项目!";
if (report)
{
ip_.GetAddress(ip1, ip2, ip3, ip4);
port = GetDlgItemInt(IDC_EDIT_PORT);
::GetDlgItemTextW(m_hWnd, IDC_EDIT_DB, text, _countof(text) - 1);
}
child->set_value(L"vid-org", vid_f);
child->set_value(L"pid-org", pid_f);
child->set_value(L"vid-to", vid_t);
child->set_value(L"pid-to", pid_t);
if (report)
{
::GetDlgItemTextW(m_hWnd, IDC_EDIT_DB, text, _countof(text) - 1);
std::wstring url(text);
swprintf_s(text, _countof(text) - 1, L"%u:", port);
url.insert(0, text);
swprintf_s(text, _countof(text) - 1, L"%u.%u.%u.%u:", ip1, ip2, ip3, ip4);
url.insert(0, text);
child->set_value(L"report-url", url.c_str());
}
jsn->set_value(L"global", child);
}
child->release();
for (size_t i = 0; i < list_.GetItemCount(); ++i)
{
page_config::ITEM item;
child = known_file_util::create_jsonW();
list_.GetItemText(i, 1, text, _countof(text) - 1);
::SendMessageW(GetParent()->m_hWnd, WM_GET_TEST_ITEM_NAME, (WPARAM)text, (LPARAM)&item);
child->set_value(L"name", item.name.c_str());
if (!used_in_code)
{
child->set_value(L"title", text);
list_.GetItemText(i, 2, text, _countof(text) - 1);
child->set_value(L"man", wcscmp(text, L"false") == 0);
list_.GetItemText(i, 3, text, _countof(text) - 1);
child->set_value(L"err-level", wcscmp(text, L"true") == 0 ? L"fatal" : L"warning");
}
else
{
child->set_value(L"ver", code_ver);
}
swprintf_s(text, _countof(text) - 1, L"%u", i + 1);
jsn->set_value(text, child);
child->release();
}
std::wstring ret(L"");
jsn->to_string(got_wstr, &ret);
jsn->release();
if (used_in_code)
{
size_t pos = ret.find(L"\"");
while (pos != std::string::npos)
{
ret.insert(pos, L"\\");
pos = ret.find(L"\"", pos + 2);
}
pos = ret.find(L"\\u");
while (pos != std::string::npos)
{
ret.insert(pos, L"\\");
pos = ret.find(L"\\u", pos + 3);
}
// divide into multi-lines with 16KB per line ...
int lines = 1;
wchar_t var[80] = { 0 };
pos = ret.length();
while (pos > 16 * 1024)
{
pos -= 12 * 1024;
while (ret[pos - 1] == L'\\')
pos++;
lines++;
swprintf_s(var, _countof(var) - 1, L"\");\r\nstatic std::string jsontext%d(\"", lines);
ret.insert(pos, var);
}
ret.insert(0, L"static std::string jsontext1(\"");
ret += L"\");\r\n";
}
*ok = true;
return ret;
}
void CDlgMgr::OnDropFiles(HDROP hDropInfo)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
wchar_t path[MAX_PATH] = { 0 };
std::wstring cont(L"");
std::string bom("");
DragQueryFileW(hDropInfo, 0, path, _countof(path) - 1);
file_util::load_file(path, got_str, &bom);
coding_util::bom::to_unicode(bom.c_str(), bom.length(), got_wstr, &cont);
known_file_util::IJsonW* jsn = known_file_util::create_jsonW();
if (jsn->attach(cont.c_str()))
{
known_file_util::IJsonW* child = NULL;
const wchar_t* val = NULL;
cont = L"";
jsn->get_value(L"global", &child);
if (child)
{
int n = 0;
child->get_value(L"vid-org", n);
SetDlgItemInt(IDC_EDIT_VID_SRC, n);
child->get_value(L"pid-org", n);
SetDlgItemInt(IDC_EDIT_PID_SRC, n);
child->get_value(L"vid-to", n);
SetDlgItemInt(IDC_EDIT_VID_TO, n);
child->get_value(L"pid-to", n);
SetDlgItemInt(IDC_EDIT_PID_TO, n);
if (child->get_value(L"report-url", &val) && val)
{
size_t pos = 0;
cont = val;
pos = cont.find(L":");
if (pos++ != std::wstring::npos)
{
ip_.SetWindowTextW(cont.substr(0, pos).c_str());
cont.erase(0, pos);
SetDlgItemInt(IDC_EDIT_PORT, _wtoi(cont.c_str()));
pos = cont.find(L":");
if (pos++ != std::wstring::npos)
cont.erase(0, pos);
}
::SetDlgItemTextW(m_hWnd, IDC_EDIT_DB, cont.c_str());
set_checked(this, IDC_CHECK2, true);
OnBnClickedCheckReport();
}
child->release();
}
list_.DeleteAllItems();
for (int i = 1; 1; ++i)
{
swprintf_s(path, _countof(path) - 1, L"%d", i);
if (!jsn->get_value(path, &child))
break;
if (child->get_value(L"title", &val))
{
int ind = list_.InsertItem(list_.GetItemCount(), path);
list_.SetItemText(ind, 1, val);
if (child->get_value(L"err-level", &val))
list_.SetItemText(ind, 3, wcscmp(val, L"fatal") == 0 ? L"true" : L"false");
bool man = false;
child->get_value(L"man", man);
list_.SetItemText(ind, 2, !man ? L"true" : L"false");
}
child->release();
}
}
jsn->release();
__super::OnDropFiles(hDropInfo);
}