]> git.saurik.com Git - wxWidgets.git/blob - samples/thread/test.cpp
Makefile tweaks
[wxWidgets.git] / samples / thread / test.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: test.cpp
3 // Purpose: wxWindows thread sample
4 // Author: Julian Smart(minimal)/Guilhem Lavaux(thread test)
5 // Modified by:
6 // Created: 06/16/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart, Markus Holzem, Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "test.cpp"
14 #pragma interface "test.cpp"
15 #endif
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/thread.h"
29 #include "wx/dynarray.h"
30
31 // Define a new application type
32 class MyApp: public wxApp
33 {
34 public:
35 bool OnInit(void);
36 };
37
38 WX_DEFINE_ARRAY(wxThread *,wxArrayThread);
39
40 // Define a new frame type
41 class MyFrame: public wxFrame
42 {
43 public:
44 MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
45
46 public:
47 void OnQuit(wxCommandEvent& event);
48 void OnAbout(wxCommandEvent& event);
49 void OnStartThread(wxCommandEvent& event);
50 void OnStopThread(wxCommandEvent& event);
51 void OnPauseThread(wxCommandEvent& event);
52 void OnSize(wxSizeEvent &event);
53 bool OnClose(void) { return TRUE; }
54
55 public:
56 wxArrayThread m_threads;
57 wxTextCtrl *m_txtctrl;
58
59 DECLARE_EVENT_TABLE()
60 };
61
62 class MyThread: public wxThread
63 {
64 public:
65 MyThread(MyFrame *frame);
66
67 void *Entry();
68 public:
69 MyFrame *m_frame;
70 };
71
72 MyThread::MyThread(MyFrame *frame)
73 : wxThread()
74 {
75 m_frame = frame;
76 }
77
78 void *MyThread::Entry()
79 {
80 wxString text;
81
82 text.Printf("Thread 0x%x: Hello !\n", GetID());
83 DeferDestroy(TRUE);
84
85 while (1) {
86 TestDestroy();
87 wxMutexGuiEnter();
88 m_frame->m_txtctrl->WriteText(text);
89 wxMutexGuiLeave();
90 wxSleep(1);
91 }
92
93 return NULL;
94 }
95
96 // ID for the menu commands
97 #define TEST_QUIT 1
98 #define TEST_TEXT 101
99 #define TEST_ABOUT 102
100 #define TEST_START_THREAD 103
101 #define TEST_STOP_THREAD 104
102 #define TEST_PAUSE_THREAD 105
103
104 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
105 EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
106 EVT_MENU(TEST_ABOUT, MyFrame::OnAbout)
107 EVT_MENU(TEST_START_THREAD, MyFrame::OnStartThread)
108 EVT_MENU(TEST_STOP_THREAD, MyFrame::OnStopThread)
109 EVT_MENU(TEST_PAUSE_THREAD, MyFrame::OnPauseThread)
110 EVT_SIZE(MyFrame::OnSize)
111 END_EVENT_TABLE()
112
113 // Create a new application object
114 IMPLEMENT_APP (MyApp)
115
116 // `Main program' equivalent, creating windows and returning main app frame
117 bool MyApp::OnInit(void)
118 {
119 // Create the main frame window
120 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "Minimal wxWindows App", 50, 50, 450, 340);
121
122 // Make a menubar
123 wxMenu *file_menu = new wxMenu;
124
125 file_menu->Append(TEST_ABOUT, "&About");
126 file_menu->Append(TEST_QUIT, "E&xit");
127 wxMenuBar *menu_bar = new wxMenuBar;
128 menu_bar->Append(file_menu, "&File");
129
130 wxMenu *thread_menu = new wxMenu;
131 thread_menu->Append(TEST_START_THREAD, "Start a new thread");
132 thread_menu->Append(TEST_STOP_THREAD, "Stop a running thread");
133 thread_menu->Append(TEST_PAUSE_THREAD, "Pause a running thread");
134 menu_bar->Append(thread_menu, "Thread");
135 frame->SetMenuBar(menu_bar);
136
137 // Make a panel with a message
138 wxPanel *panel = new wxPanel( frame, -1, wxPoint(0, 0), wxSize(400, 200), wxTAB_TRAVERSAL );
139
140 (void)new wxStaticText( panel, -1, "Log window", wxPoint(10,10) );
141
142
143 frame->m_txtctrl = new wxTextCtrl(panel, -1, "", wxPoint(10,30), wxSize(390, 190),
144 wxTE_MULTILINE);
145
146 // Show the frame
147 frame->Show(TRUE);
148
149 SetTopWindow(frame);
150
151 return TRUE;
152 }
153
154 // My frame constructor
155 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
156 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
157 {}
158
159 void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) )
160 {
161 MyThread *thread = new MyThread(this);
162
163 thread->Create();
164
165 m_threads.Add(thread);
166 }
167
168 void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) )
169 {
170 int no_thrd = m_threads.Count()-1;
171
172 if (no_thrd < 0)
173 return;
174
175 delete (m_threads[no_thrd]);
176 m_threads.Remove(no_thrd);
177 }
178
179 void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) )
180 {
181 }
182
183 void MyFrame::OnSize(wxSizeEvent& event )
184 {
185 wxFrame::OnSize(event);
186
187 wxSize size( GetClientSize() );
188
189 m_txtctrl->SetSize( 10, 30, size.x-20, size.y-40 );
190 }
191
192 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
193 {
194 unsigned int i;
195 for (i=0;i<m_threads.Count();i++)
196 delete (m_threads[i]);
197 Close(TRUE);
198 }
199
200 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
201 {
202 wxMessageDialog dialog(this, "wxThread sample (based on minimal)\nJulian Smart and Guilhem Lavaux",
203 "About wxThread sample", wxYES_NO|wxCANCEL);
204
205 dialog.ShowModal();
206 }
207
208