nim_duilib/examples/proto_debuger/base_form.cpp
2022-02-20 11:55:16 +08:00

520 lines
15 KiB
C++

#include "base_form.h"
#include "serial_port.h"
#include "udp_form.h"
#include "utils.h"
#include "global.h"
const std::wstring BasicForm::kClassName = L"Basic";
BasicForm::BasicForm():
mMonitorNewSelect(nullptr),
mRightShow(nullptr)
{
this->SetInitSize(1024, 768, true, false);
}
BasicForm::~BasicForm()
{
}
std::wstring BasicForm::GetSkinFolder()
{
return L"basic";
}
std::wstring BasicForm::GetSkinFile()
{
return L"basic.xml";
}
std::wstring BasicForm::GetWindowClassName() const
{
return kClassName;
}
int gTcpServerCnt = 0;
int gTcpClientCnt = 0;
LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_ADD_UART_CLOSE) {
wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str());
mRightShow->SetVisible(false);
mRightShow = nullptr;
mRightSide->SetAutoDestroy(true);
mRightSide->RemoveAll();
mUartForm.erase(*(wstring*)(wParam));
int cnt = mMonitor->GetRootNode()->GetChildNode(0)->GetChildNodeCount();
ui::TreeNode* p = nullptr;
for (int i = 0; i < cnt; i++) {
if (mMonitor->GetRootNode()->GetChildNode(0)->GetChildNode(i)->GetText()
== *(wstring*)(wParam)) {
p = mMonitor->GetRootNode()->GetChildNode(0)->GetChildNode(i);
break;
}
}
auto comname = *(wstring*)(wParam);
mMonitor->GetRootNode()->GetChildNode(0)->RemoveChildNode(p);
UINT PortNum = 0;
for (int i = 3; comname[i] != L'\0'; i++) //ת»»ÎªÊý×Ö
{
PortNum = PortNum * 10 + (comname[i] - L'0');
}
SerialPort::ClosePort(PortNum);
}
if (uMsg == WM_USER_UDP_GROUP_CLOSE) {
if (mRightShow == nullptr)
return true;
wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str());
mRightShow->SetVisible(false);
mRightShow = nullptr;
mRightSide->SetAutoDestroy(true);
mRightSide->RemoveAll();
mUdpForm.erase(*(wstring*)(wParam));
int cnt = mMonitor->GetRootNode()->GetChildNode(4)->GetChildNodeCount();
ui::TreeNode* p = nullptr;
for (int i = 0; i < cnt; i++) {
if (mMonitor->GetRootNode()->GetChildNode(4)->GetChildNode(i)->GetText()
== *(wstring*)(wParam)) {
p = mMonitor->GetRootNode()->GetChildNode(4)->GetChildNode(i);
break;
}
}
mMonitor->GetRootNode()->GetChildNode(4)->RemoveChildNode(p);
delete mRightShow;
mRightShow = nullptr;
}
if (uMsg == WM_USER_UDP_CLOSE) {
if (mRightShow == nullptr)
return true;
wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str());
mRightShow->SetVisible(false);
mRightShow = nullptr;
mRightSide->SetAutoDestroy(true);
mRightSide->RemoveAll();
mUdpForm.erase(*(wstring*)(wParam));
int cnt = mMonitor->GetRootNode()->GetChildNode(3)->GetChildNodeCount();
ui::TreeNode* p = nullptr;
for (int i = 0; i < cnt; i++) {
if (mMonitor->GetRootNode()->GetChildNode(3)->GetChildNode(i)->GetText()
== *(wstring*)(wParam)) {
p = mMonitor->GetRootNode()->GetChildNode(3)->GetChildNode(i);
break;
}
}
mMonitor->GetRootNode()->GetChildNode(3)->RemoveChildNode(p);
delete mRightShow;
mRightShow = nullptr;
}
if (uMsg == WM_USER_TCP_CLIENT_CLOSE) {
wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str());
mRightShow->SetVisible(false);
mRightSide->SetAutoDestroy(true);
mRightSide->RemoveAll();
mTcpClientForm.erase(*(wstring*)(wParam));
int cnt = mMonitor->GetRootNode()->GetChildNode(1)->GetChildNodeCount();
ui::TreeNode* p = nullptr;
for (int i = 0; i < cnt; i++) {
if (mMonitor->GetRootNode()->GetChildNode(1)->GetChildNode(i)->GetText()
== *(wstring*)(wParam)) {
p = mMonitor->GetRootNode()->GetChildNode(1)->GetChildNode(i);
break;
}
}
mMonitor->GetRootNode()->GetChildNode(1)->RemoveChildNode(p);
// delete mRightShow;
mRightShow = nullptr;
}
if (uMsg == WM_USER_TCP_SERVER_CLOSE) {
wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str());
mRightShow->SetVisible(false);
mRightSide->RemoveAll();
mTcpServerForm.erase(*(wstring*)(wParam));
int cnt = mMonitor->GetRootNode()->GetChildNode(2)->GetChildNodeCount();
ui::TreeNode* p = nullptr;
for (int i = 0; i < cnt; i++) {
if (mMonitor->GetRootNode()->GetChildNode(2)->GetChildNode(i)->GetText()
== *(wstring*)(wParam)) {
p = mMonitor->GetRootNode()->GetChildNode(2)->GetChildNode(i);
break;
}
}
if(nullptr != p)
mMonitor->GetRootNode()->GetChildNode(2)->RemoveChildNode(p);
//delete mRightShow;
mRightShow = nullptr;
}
if (uMsg == WM_USER_POS_CHANGED) {
}
if (uMsg == WM_ADD_TCPCLIENT_MONITOR) {
TcpClientInfo* info = (TcpClientInfo*)wParam;
TcpClientLibevent* cli = (TcpClientLibevent*)lParam;
ui::TreeNode* node = new ui::TreeNode;
auto key = info->ip + L":" + std::to_wstring(gTcpClientCnt);
node->SetText(key);
node->SetClass(L"listitem");
mMonitor->GetRootNode()->GetChildNode(1)->AddChildNode(node);
gTcpClientCnt++;
if (mTcpClientForm.find(key) == mTcpClientForm.end())
{
auto form = new TcpClientForm(this,wstring2string(info->ip),info->port, cli);
form->SetChildLayoutXML(L"basic/tcp_form.xml");
form->SetName(key);
form->SetVisible(false);
mTcpClientForm[key] = form;
if (!mRightSide->Add(form))
printf("error 1");
}
node->AttachAllEvents([this](ui::EventArgs* ev) {
if (ui::EventType::kEventSelect == ev->Type) {
wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
TcpClientForm* p = mTcpClientForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
if (p != nullptr) {
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
p->SetAutoDestroy(true);
if (mRightShow != nullptr) {
mRightShow->SetVisible(false);
p->SetVisible(true);
mRightShow = p;
mRightSide->SelectItem(p->GetName());
}
else {
p->SetVisible(true);
mRightSide->SelectItem(p->GetName());
mRightShow = p;
}
}
}
return true;
});
mMonitorNewSelect->Close();
}
if (uMsg == WM_ADD_TCPSERVER_MONITOR) {
TcpServerInfo* info = (TcpServerInfo*)wParam;
TcpServerLibevent* server = (TcpServerLibevent*)lParam;
ui::TreeNode* node = new ui::TreeNode;
mMonitor->GetRootNode()->GetChildNode(2)->AddChildNode(node);
auto key = info->ip + L":" + std::to_wstring(info->port);
node->SetText(key);
node->SetClass(L"listitem");
gTcpServerCnt++;
printf("WM_ADD_TCPSERVER_MONITOR\r\n");
if (mTcpServerForm.find(info->ip + L":"
+ std::to_wstring(gTcpServerCnt)) == mTcpServerForm.end())
{
auto form = new TcpServerFrom(this, wstring2string(info->ip), info->port, server);
form->SetChildLayoutXML(L"basic/tcp_server_form.xml");
form->SetName(key);
form->SetVisible(false);
mTcpServerForm[key] = form;
if (!mRightSide->Add(form))
printf("error 1");
}
node->AttachAllEvents(
[this](ui::EventArgs* ev) {
if (ui::EventType::kEventSelect == ev->Type) {
wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
TcpServerFrom* p = mTcpServerForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
if (p != nullptr) {
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
p->SetAutoDestroy(true);
if (mRightShow != nullptr) {
mRightShow->SetVisible(false);
p->SetVisible(true);
mRightShow = p;
wprintf(L"%s", p->GetName());
mRightSide->SelectItem(p->GetName());
}
else {
wprintf(L"%s", p->GetName());
p->SetVisible(true);
mRightSide->SelectItem(p->GetName());
mRightShow = p;
}
}
}
return true;
}
);
mMonitorNewSelect->Close();
}
if (uMsg == WM_ADD_UDP_MONITOR) {
printf("add udp monitor");
wchar_t pdata[100] = L"\0";
TcpServerInfo* p = (TcpServerInfo*)wParam;
UdpDataGramLibevent* udp = (UdpDataGramLibevent*)lParam;
wsprintf(pdata, L"%s:%d", p->ip.c_str(), p->port);
ui::TreeNode* node = new ui::TreeNode;
node->SetText(pdata);
node->SetClass(L"listitem");
mMonitor->GetRootNode()->GetChildNode(3)->AddChildNode(node);
if (mUdpForm.find(std::wstring(pdata)) == mUdpForm.end())
{
auto form = new UdpForm(this, wstring2string(p->ip), p->port, udp);
form->SetChildLayoutXML(L"basic/udp_form.xml");
form->SetName(pdata);
form->SetVisible(false);
mUdpForm[pdata] = form;
if (!mRightSide->Add(form))
printf("error 1");
}
node->AttachAllEvents(
[this](ui::EventArgs* ev) {
if (ui::EventType::kEventSelect == ev->Type) {
wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
UdpForm* p = mUdpForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
if (p != nullptr) {
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
p->SetAutoDestroy(true);
if (mRightShow != nullptr) {
mRightShow->SetVisible(false);
p->SetVisible(true);
mRightShow = p;
wprintf(L"%s", p->GetName());
mRightSide->SelectItem(p->GetName());
}
else {
wprintf(L"%s", p->GetName());
p->SetVisible(true);
mRightSide->SelectItem(p->GetName());
mRightShow = p;
}
}
return true;
}
});
}
if (uMsg == WM_ADD_UDP_GROUP_MONITOR) {
printf("add udp monitor");
wchar_t pdata[100] = L"\0";
TcpServerInfo* p = (TcpServerInfo*)wParam;
UdpDataGramLibevent* udp = (UdpDataGramLibevent*)lParam;
wsprintf(pdata, L"%s:%d", p->ip.c_str(), p->port);
ui::TreeNode* node = new ui::TreeNode;
node->SetText(pdata);
node->SetClass(L"listitem");
mMonitor->GetRootNode()->GetChildNode(4)->AddChildNode(node);
if (mUdpGroupForm.find(std::wstring(pdata)) == mUdpGroupForm.end())
{
auto form = new UdpGroupForm(this, wstring2string(p->ip), p->port, udp);
form->SetChildLayoutXML(L"basic/udp_form.xml");
form->SetName(pdata);
form->SetVisible(false);
mUdpGroupForm[pdata] = form;
if (!mRightSide->Add(form))
printf("error 1");
}
node->AttachAllEvents(
[this](ui::EventArgs* ev) {
if (ui::EventType::kEventSelect == ev->Type) {
wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
UdpGroupForm* p = mUdpGroupForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
if (p != nullptr) {
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
p->SetAutoDestroy(true);
if (mRightShow != nullptr) {
mRightShow->SetVisible(false);
p->SetVisible(true);
mRightShow = p;
wprintf(L"%s", p->GetName());
mRightSide->SelectItem(p->GetName());
}
else {
wprintf(L"%s", p->GetName());
p->SetVisible(true);
mRightSide->SelectItem(p->GetName());
mRightShow = p;
}
}
return true;
}
});
}
if (uMsg == WM_ADD_UART_MONITOR) {
printf("add uart monitor");
UartInfo* p = (UartInfo*)wParam;
wprintf(L"%s", p->name.c_str());
ui::TreeNode* node = new ui::TreeNode;
node->SetText(p->name);
node->SetClass(L"listitem");
mMonitor->GetRootNode()->GetChildNode(0)->AddChildNode(node);
if (mUartForm.find(p->name) == mUartForm.end()) {
auto form = new UartForm(this,p->name, p->baurate,p->port_num,
p->data_bits, p->stop_bits, p->verify, p->flow_control);
form->SetChildLayoutXML(L"basic/uart_form.xml");
form->SetName(p->name);
form->SetVisible(false);
mUartForm[p->name] = form;
if (!mRightSide->Add(form))
printf("erroer 1");
}
node->AttachAllEvents([this](ui::EventArgs* ev){
if (ui::EventType::kEventSelect == ev->Type) {
wprintf(L"%s\r\n",dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
UartForm* p = mUartForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
if (p != nullptr) {
p->SetAutoDestroy(true);
if (mRightShow != nullptr) {
mRightShow->SetVisible(false);
p->SetVisible(true);
mRightShow = p;
mRightSide->SelectItem(p->GetName());
}
else {
p->SetVisible(true);
mRightSide->SelectItem(p->GetName());
mRightShow = p;
}
}
}
return true;
});
}
return WindowImplBase::HandleMessage(uMsg, wParam, lParam);
}
void BasicForm::InitWindow(){
/* Show settings menu */
ui::Button* btn_add_new = dynamic_cast<ui::Button*>(FindControl(L"add_new"));
btn_add_new->SetText(L"н¨¼àÌýÏî");
if (nullptr != btn_add_new) {
btn_add_new->AttachClick([this](ui::EventArgs* args) {
if (mMonitorNewSelect == nullptr) {
mMonitorNewSelect = new NewMonitorForm(this);
mMonitorNewSelect->Create(this->GetHWND(), NewMonitorForm::kClassName.c_str(),
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0);
mMonitorNewSelect->CenterWindow();
mMonitorNewSelect->ShowModalFake(this->GetHWND());
}
else {
mMonitorNewSelect = new NewMonitorForm(this);
mMonitorNewSelect->Create(this->GetHWND(), NewMonitorForm::kClassName.c_str(),
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0);
mMonitorNewSelect->CenterWindow();
mMonitorNewSelect->ShowModalFake(this->GetHWND());
}
return true;
});
}
mMonitor = dynamic_cast<ui::TreeView*>(FindControl(L"tree"));
mMonitor->SetClass(L"list");
mMonitor->SetIndent(5);
if (nullptr != mMonitor) {
ui::TreeNode* node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"uart"));
node->SetClass(L"list_topitem");
mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"tcp client"));
node->SetClass(L"list_topitem");
node->SetFixedHeight(30);
mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"tcp server"));
node->SetClass(L"list_topitem");
node->SetFixedHeight(30);
mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"udp"));
node->SetClass(L"list_topitem");
node->SetFixedHeight(30);
mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"udp group"));
node->SetClass(L"list_topitem");
node->SetFixedHeight(30);
mMonitor->GetRootNode()->AddChildNode(node);
}
mRightSide = dynamic_cast<ui::TabBox*>(FindControl(L"tab_side"));
mRightSide->RemoveAll();
this->SetMinInfo(1280, 768, true);
this->SetInitSize(1280, 768, false, false);
ui::UiRect maxRect = GetMaximizeInfo();
this->Invalidate(maxRect);
RECT rc;
//È¡µ±Ç°´°¿ÚÇøÓò
GetClientRect(m_hWnd, &rc);
//¸Ä±ä´°¿Ú´óС
//Òƶ¯´°¿Úµ½Ö¸¶¨ÇøÓò
::MoveWindow(m_hWnd, rc.left, rc.top, rc.right, rc.bottom, true);
}
LuaBindInterface* BasicForm::FindCurrentFormByLuaPointer(lua_State* pointer)
{
for (auto itr = mUartForm.begin(); itr != mUartForm.end(); itr++) {
if (itr->second->LuaVM()->VM() == pointer) {
return (LuaBindInterface*)itr->second;
}
}
for (auto itr = mTcpClientForm.begin(); itr != mTcpClientForm.end(); itr++) {
if (itr->second->LuaVM()->VM() == pointer) {
return (LuaBindInterface*)itr->second;
}
}
for (auto itr = mTcpServerForm.begin(); itr != mTcpServerForm.end(); itr++) {
if (itr->second->LuaVM()->VM() == pointer) {
return (LuaBindInterface*)itr->second;
}
}
for (auto itr = mUdpForm.begin(); itr != mUdpForm.end(); itr++) {
if (itr->second->LuaVM()->VM() == pointer) {
return (LuaBindInterface*)itr->second;
}
}
return nullptr;
}
LRESULT BasicForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PostQuitMessage(0L);
return __super::OnClose(uMsg, wParam, lParam, bHandled);
}