/
th-gui.cpp
278 lines (228 loc) · 7.4 KB
/
th-gui.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// File: th-gui.cpp
// Description: Using wxThread
// WebSite: http://cool-emerald.blogspot.com
// MIT License (https://opensource.org/licenses/MIT)
// Copyright (c) 2018 Yan Naing Aye
// References (Original example)
// https://github.com/wxWidgets/wxWidgets/blob/master/samples/thread/thread.cpp
#include "wx/wx.h"
#include "./sample.xpm"
// --------------------------------------------------------
// a thread which executes GUI calls using wxMutexGuiEnter/Leave
#define GUITHREAD_BMP_SIZE 300
#define GUITHREAD_NUM_UPDATES 50
class MyImageDialog;
class MyGUIThread : public wxThread
{
public:
MyGUIThread(MyImageDialog *dlg) : wxThread(wxTHREAD_JOINABLE) {
m_dlg = dlg;
}
virtual ExitCode Entry();
private:
MyImageDialog *m_dlg;
};
// --------------------------------------------------------
// an helper dialog used by MyFrame::OnStartGUIThread
class MyImageDialog: public wxDialog
{
public:
// ctor
MyImageDialog(wxFrame *frame);
~MyImageDialog();
// stuff used by MyGUIThread:
wxBitmap m_bmp; // the bitmap drawn by MyGUIThread
wxCriticalSection m_csBmp; // protects m_bmp
private:
void OnGUIThreadEvent(wxThreadEvent& event);
void OnPaint(wxPaintEvent&);
MyGUIThread m_thread;
int m_nCurrentProgress;
wxDECLARE_EVENT_TABLE();
};
// --------------------------------------------------------
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
// --------------------------------------------------------
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title);
~MyFrame();
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnStart(wxCommandEvent& event);
private:
wxButton *btnStart;
wxStaticText *lblText;
wxDECLARE_EVENT_TABLE();
};
// --------------------------------------------------------
// IDs for the controls and the menu commands
enum
{
ID_MNU_QUIT = wxID_EXIT,
ID_MNU_ABOUT = wxID_ABOUT,
ID_BTN_START,
ID_LBL_TEXT,
GUITHREAD_EVENT
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_MNU_QUIT, MyFrame::OnQuit)
EVT_MENU(ID_MNU_ABOUT, MyFrame::OnAbout)
wxEND_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
if ( !wxApp::OnInit() )
return false;
MyFrame *frame = new MyFrame("wxWidgets App");
frame->Show(true);
return true;
}
// --------------------------------------------------------
// MyFrame
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600, 450),
wxDEFAULT_FRAME_STYLE ^ wxRESIZE_BORDER)
{
SetIcon(wxICON(sample));
wxMenu *fileMenu = new wxMenu;
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(ID_MNU_ABOUT, "&About\tF1", "Show about dialog");
fileMenu->Append(ID_MNU_QUIT, "E&xit\tAlt-X", "Quit this program");
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, "&File");
menuBar->Append(helpMenu, "&Help");
SetMenuBar(menuBar);
CreateStatusBar(1);
SetStatusText("wxWidgets App");
btnStart = new wxButton(this, ID_BTN_START, wxT("Start"),
wxPoint(20,20), wxSize(100, 25));
Connect(ID_BTN_START, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(MyFrame::OnStart));
lblText = new wxStaticText(this,ID_LBL_TEXT,wxT("Click the button to start the gui thread"),
wxPoint(20,50));
}
MyFrame::~MyFrame()
{
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox(wxString::Format
(
"wxWidgets thread\n"
"\n"
"Author: Yan Naing Aye \n"
"Web: http://cool-emerald.blogspot.com"
),
"About wxWidgets thread",
wxOK | wxICON_INFORMATION,
this);
}
void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
{
MyImageDialog dlg(this);
dlg.ShowModal();
}
// --------------------------------------------------------
// MyImageDialog
wxBEGIN_EVENT_TABLE(MyImageDialog, wxDialog)
EVT_THREAD(GUITHREAD_EVENT, MyImageDialog::OnGUIThreadEvent)
EVT_PAINT(MyImageDialog::OnPaint)
wxEND_EVENT_TABLE()
MyImageDialog::MyImageDialog(wxFrame *parent)
: wxDialog(parent, wxID_ANY, "Image created by a secondary thread",
wxDefaultPosition, wxSize(GUITHREAD_BMP_SIZE,GUITHREAD_BMP_SIZE)*1.5, wxDEFAULT_DIALOG_STYLE),
m_thread(this)
{
m_nCurrentProgress = 0;
CentreOnScreen();
// NOTE: no need to lock m_csBmp until the thread isn't started:
// create the bitmap
if (!m_bmp.Create(GUITHREAD_BMP_SIZE,GUITHREAD_BMP_SIZE) || !m_bmp.IsOk())
{
wxLogError("Couldn't create the bitmap!");
return;
}
// clean it
wxMemoryDC dc(m_bmp);
dc.SetBackground( *wxBLACK_BRUSH);
dc.Clear();
// draw the bitmap from a secondary thread
if ( m_thread.Create() != wxTHREAD_NO_ERROR ||
m_thread.Run() != wxTHREAD_NO_ERROR )
{
wxLogError(wxT("Can't create/run thread!"));
return;
}
}
MyImageDialog::~MyImageDialog()
{
// in case our thread is still running and for some reason we are destroyed,
// do wait for the thread to complete as it assumes that its MyImageDialog
// pointer is always valid
m_thread.Delete();
}
void MyImageDialog::OnGUIThreadEvent(wxThreadEvent& event)
{
m_nCurrentProgress = int(((float)event.GetInt()*100)/GUITHREAD_NUM_UPDATES);
Refresh();
}
void MyImageDialog::OnPaint(wxPaintEvent& WXUNUSED(evt))
{
wxPaintDC dc(this);
const wxSize& sz = dc.GetSize();
{
// paint the bitmap
wxCriticalSectionLocker locker(m_csBmp);
dc.DrawBitmap(m_bmp, (sz.GetWidth()-GUITHREAD_BMP_SIZE)/2,
(sz.GetHeight()-GUITHREAD_BMP_SIZE)/2);
}
// paint a sort of progress bar with a 10px border:
dc.SetBrush( *wxRED_BRUSH);
dc.DrawRectangle(10,10, m_nCurrentProgress*(sz.GetWidth()-20)/100,30);
dc.SetTextForeground( *wxBLUE);
dc.DrawText(wxString::Format("%d%%", m_nCurrentProgress),
(sz.GetWidth()-dc.GetCharWidth()*2)/2,
25-dc.GetCharHeight()/2);
}
// --------------------------------------------------------
// MyGUIThread
wxThread::ExitCode MyGUIThread::Entry()
{
for (int i=0; i<GUITHREAD_NUM_UPDATES && !TestDestroy(); i++)
{
// inform the GUI toolkit that we're going to use GUI functions
// from a secondary thread:
wxMutexGuiEnter();
{
wxCriticalSectionLocker lock(m_dlg->m_csBmp);
// draw some more stuff on the bitmap
wxMemoryDC dc(m_dlg->m_bmp);
dc.SetBrush((i%2)==0 ? *wxBLUE_BRUSH : *wxGREEN_BRUSH);
dc.DrawRectangle(rand()%GUITHREAD_BMP_SIZE, rand()%GUITHREAD_BMP_SIZE, 30, 30);
// simulate long drawing time:
wxMilliSleep(200);
}
// if we don't release the GUI mutex the MyImageDialog won't be able to refresh
wxMutexGuiLeave();
// notify the dialog that another piece of our masterpiece is complete:
wxThreadEvent event( wxEVT_THREAD, GUITHREAD_EVENT );
event.SetInt(i+1);
wxQueueEvent( m_dlg, event.Clone() );
// give the main thread the time to refresh before we lock the GUI mutex again
// FIXME: find a better way to do this!
wxMilliSleep(100);
}
return (ExitCode)0;
}
// --------------------------------------------------------