]>
Commit | Line | Data |
---|---|---|
82052aff GL |
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 | ||
123a7fdd GL |
38 | wxMutex text_mutex; |
39 | ||
82052aff GL |
40 | WX_DEFINE_ARRAY(wxThread *,wxArrayThread); |
41 | ||
42 | // Define a new frame type | |
43 | class MyFrame: public wxFrame | |
44 | { | |
45 | public: | |
46 | MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h); | |
47 | ||
48 | public: | |
49 | void OnQuit(wxCommandEvent& event); | |
50 | void OnAbout(wxCommandEvent& event); | |
51 | void OnStartThread(wxCommandEvent& event); | |
52 | void OnStopThread(wxCommandEvent& event); | |
53 | void OnPauseThread(wxCommandEvent& event); | |
54 | bool OnClose(void) { return TRUE; } | |
55 | ||
56 | public: | |
57 | wxArrayThread m_threads; | |
58 | wxTextCtrl *m_txtctrl; | |
59 | ||
60 | DECLARE_EVENT_TABLE() | |
61 | }; | |
62 | ||
63 | class MyThread: public wxThread | |
64 | { | |
65 | public: | |
66 | MyThread(MyFrame *frame); | |
67 | ||
68 | void *Entry(); | |
69 | public: | |
70 | MyFrame *m_frame; | |
71 | }; | |
72 | ||
73 | MyThread::MyThread(MyFrame *frame) | |
74 | : wxThread() | |
75 | { | |
76 | m_frame = frame; | |
77 | } | |
78 | ||
79 | void *MyThread::Entry() | |
80 | { | |
81 | wxString text; | |
82 | ||
83 | text.Printf("Thread 0x%x: Hello !\n", GetID()); | |
84 | DeferDestroy(TRUE); | |
85 | ||
86 | while (1) { | |
87 | TestDestroy(); | |
123a7fdd | 88 | text_mutex.Lock(); |
82052aff | 89 | m_frame->m_txtctrl->WriteText(text); |
123a7fdd | 90 | text_mutex.UnLock(); |
82052aff GL |
91 | wxSleep(1); |
92 | } | |
93 | ||
94 | return NULL; | |
95 | } | |
96 | ||
97 | // ID for the menu commands | |
98 | #define TEST_QUIT 1 | |
99 | #define TEST_TEXT 101 | |
100 | #define TEST_ABOUT 102 | |
101 | #define TEST_START_THREAD 103 | |
102 | #define TEST_STOP_THREAD 104 | |
103 | #define TEST_PAUSE_THREAD 105 | |
104 | ||
105 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
106 | EVT_MENU(TEST_QUIT, MyFrame::OnQuit) | |
107 | EVT_MENU(TEST_ABOUT, MyFrame::OnAbout) | |
108 | EVT_MENU(TEST_START_THREAD, MyFrame::OnStartThread) | |
109 | EVT_MENU(TEST_STOP_THREAD, MyFrame::OnStopThread) | |
110 | EVT_MENU(TEST_PAUSE_THREAD, MyFrame::OnPauseThread) | |
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(NULL, "Minimal wxWindows App", 50, 50, 450, 340); | |
121 | ||
122 | // Give it an icon | |
2049ba38 | 123 | #ifdef __WXMSW__ |
82052aff GL |
124 | frame->SetIcon(wxIcon("AIAI")); |
125 | #endif | |
126 | #ifdef __X__ | |
127 | frame->SetIcon(wxIcon("aiai.xbm")); | |
128 | #endif | |
129 | ||
130 | // Make a menubar | |
131 | wxMenu *file_menu = new wxMenu; | |
132 | ||
133 | file_menu->Append(TEST_ABOUT, "&About"); | |
134 | file_menu->Append(TEST_QUIT, "E&xit"); | |
135 | wxMenuBar *menu_bar = new wxMenuBar; | |
136 | menu_bar->Append(file_menu, "&File"); | |
137 | ||
138 | wxMenu *thread_menu = new wxMenu; | |
139 | thread_menu->Append(TEST_START_THREAD, "Start a new thread"); | |
140 | thread_menu->Append(TEST_STOP_THREAD, "Stop a running thread"); | |
141 | thread_menu->Append(TEST_PAUSE_THREAD, "Pause a running thread"); | |
142 | menu_bar->Append(thread_menu, "Thread"); | |
143 | frame->SetMenuBar(menu_bar); | |
144 | ||
145 | // Make a panel with a message | |
146 | wxPanel *panel = new wxPanel(frame, -1, wxPoint(0, 0), wxSize(400, 200), wxTAB_TRAVERSAL); | |
147 | ||
148 | frame->m_txtctrl = new wxTextCtrl(panel, -1, "", wxPoint(10, 10), wxSize(390, 190), | |
149 | wxTE_MULTILINE); | |
150 | ||
151 | // Show the frame | |
152 | frame->Show(TRUE); | |
153 | ||
154 | SetTopWindow(frame); | |
155 | ||
156 | return TRUE; | |
157 | } | |
158 | ||
159 | // My frame constructor | |
160 | MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h): | |
161 | wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)) | |
162 | {} | |
163 | ||
e3e65dac | 164 | void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) ) |
82052aff GL |
165 | { |
166 | MyThread *thread = new MyThread(this); | |
167 | ||
168 | thread->Create(); | |
169 | ||
170 | m_threads.Add(thread); | |
171 | } | |
172 | ||
e3e65dac | 173 | void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) ) |
82052aff | 174 | { |
e3e65dac | 175 | int no_thrd = m_threads.Count()-1; |
82052aff GL |
176 | |
177 | if (no_thrd < 0) | |
178 | return; | |
179 | ||
180 | delete (m_threads[no_thrd]); | |
181 | m_threads.Remove(no_thrd); | |
182 | } | |
183 | ||
e3e65dac | 184 | void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) ) |
82052aff GL |
185 | {} |
186 | ||
e3e65dac | 187 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) |
82052aff GL |
188 | { |
189 | uint i; | |
190 | for (i=0;i<m_threads.Count();i++) | |
191 | delete (m_threads[i]); | |
192 | Close(TRUE); | |
193 | } | |
194 | ||
e3e65dac | 195 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) |
82052aff GL |
196 | { |
197 | wxMessageDialog dialog(this, "wxThread sample (based on minimal)\nJulian Smart and Guilhem Lavaux", | |
198 | "About wxThread sample", wxYES_NO|wxCANCEL); | |
199 | ||
200 | dialog.ShowModal(); | |
201 | } | |
202 | ||
203 |