-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwxuimain.cpp
More file actions
161 lines (137 loc) · 3.88 KB
/
wxuimain.cpp
File metadata and controls
161 lines (137 loc) · 3.88 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
#include "stdafx.h"
namespace local {
class WxFrame : public wxFrame {
public:
WxFrame(wxWindow* parent, const wxString& label);
void OnAbout(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
static const int CMD_SHOW_WINDOW = wxNewId();
static const int CMD_TERMINATE = wxNewId();
class WxApp : public shared::wx::IwxApp {
public:
WxApp();
virtual ~WxApp();
private:
void OnShowWindow(wxThreadEvent& event);
void OnTerminate(wxThreadEvent& event);
};
wxBEGIN_EVENT_TABLE(WxFrame, wxFrame)
EVT_BUTTON(wxID_ABOUT, WxFrame::OnAbout)
wxEND_EVENT_TABLE()
WxFrame::WxFrame(wxWindow* parent, const wxString& label)
: wxFrame(parent, wxID_ANY, label)
{
wxPanel* p = new wxPanel(this, wxID_ANY);
wxSizer* sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add
(
new wxStaticText
(
p, wxID_ANY,
wxString::Format
(
"Running using %s\n"
"wxApp instance is %p, thread ID %ld",
wxVERSION_STRING,
wxApp::GetInstance(),
wxThread::GetCurrentId()
)
),
wxSizerFlags(1).Expand().Border(wxALL, 10)
);
sizer->Add
(
new wxButton(p, wxID_ABOUT, "Show info"),
wxSizerFlags(0).Right().Border(wxALL, 10)
);
p->SetSizerAndFit(sizer);
wxSizer* fsizer = new wxBoxSizer(wxVERTICAL);
fsizer->Add(p, wxSizerFlags(1).Expand());
SetSizerAndFit(fsizer);
}
void WxFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) {
wxMessageBox("This window is running in its own thread,\n"
"using private wxWidgets instance compiled into the DLL.",
"About",
wxOK | wxICON_INFORMATION);
}
WxApp::WxApp() {
SetExitOnFrameDelete(false);
Bind(wxEVT_THREAD, &WxApp::OnShowWindow, this, CMD_SHOW_WINDOW);
Bind(wxEVT_THREAD, &WxApp::OnTerminate, this, CMD_TERMINATE);
}
WxApp::~WxApp() {
}
void WxApp::OnShowWindow(wxThreadEvent& event) {
/*wxFrame* f = new WxFrame(NULL, event.GetString());
f->Show(true);*/
auto mdiframe = new shared::wx::IMDIParentFrame(nullptr);
mdiframe->Show(true);
}
void WxApp::OnTerminate(wxThreadEvent& WXUNUSED(event)) {
ExitMainLoop();
}
wxIMPLEMENT_APP_NO_MAIN(WxApp);
WxMain::WxMain(const UIType& ui_type, const bool& init_show) : UIBase(ui_type, init_show) {
}
WxMain::~WxMain() {
}
void WxMain::Create() {
std::lock_guard<std::mutex> lock{ *m_Mutex };
do {
if (m_IsOpen.load())
break;
HANDLE hEvent = ::CreateEventW(NULL, FALSE, FALSE, NULL);
if (!hEvent)
break;
m_UIMainThread = reinterpret_cast<HANDLE>(::_beginthreadex(NULL, 0,
[](void* pRouteEvent)->unsigned int {
unsigned int result = 0;
do {
if (!__gpHinstance || !pRouteEvent)
break;
HANDLE* phEvent = reinterpret_cast<HANDLE*>(pRouteEvent);
if (!phEvent)
break;
/*const HINSTANCE
hInstance = wxDynamicLibrary::MSWGetModuleHandle(shared::Win::GetModuleNameA(__gpHinstance), phUIMainThread);
if (!hInstance)
break;*/
wxDISABLE_DEBUG_SUPPORT();
wxInitializer wxinit;
if (!wxinit.IsOk())
break;
if (!::SetEvent(*phEvent))
break;
wxEntry(__gpHinstance);
result = 1;
} while (0);
return result;
}, &hEvent, 0, NULL));
if (!m_UIMainThread)
break;
::WaitForSingleObject(hEvent, INFINITE);
SK_CLOSE_HANDLE(hEvent);
// Send a message to wx thread to show a new frame:
wxThreadEvent* event = new wxThreadEvent(wxEVT_THREAD, CMD_SHOW_WINDOW);
event->SetString("Hello wxWidgets!");
wxQueueEvent(wxApp::GetInstance(), event);
m_IsOpen.store(true);
} while (0);
}
void WxMain::Destory() {
std::lock_guard<std::mutex> lock{ *m_Mutex };
do {
if (!m_IsOpen.load())
break;
if (!m_UIMainThread)
break;
wxThreadEvent* event = new wxThreadEvent(wxEVT_THREAD, CMD_TERMINATE);
wxQueueEvent(wxApp::GetInstance(), event);
::WaitForSingleObject(m_UIMainThread, INFINITE);
SK_CLOSE_HANDLE(m_UIMainThread);
m_IsOpen.store(false);
} while (0);
}
}///namespace local