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