/[pcsx2_0.9.7]/trunk/common/include/Utilities/pxEvents.h
ViewVC logotype

Contents of /trunk/common/include/Utilities/pxEvents.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (show annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (9 years, 10 months ago) by william
File MIME type: text/plain
File size: 12834 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug in ./trunk
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #pragma once
17
18 BEGIN_DECLARE_EVENT_TYPES()
19 DECLARE_EVENT_TYPE( pxEvt_StartIdleEventTimer, -1 )
20 DECLARE_EVENT_TYPE( pxEvt_DeleteObject, -1 )
21 DECLARE_EVENT_TYPE( pxEvt_DeleteThread, -1 )
22 DECLARE_EVENT_TYPE( pxEvt_InvokeAction, -1 )
23 DECLARE_EVENT_TYPE( pxEvt_SynchronousCommand, -1 )
24 END_DECLARE_EVENT_TYPES()
25
26 typedef void FnType_Void();
27
28 // --------------------------------------------------------------------------------------
29 // SynchronousActionState
30 // --------------------------------------------------------------------------------------
31 class SynchronousActionState
32 {
33 DeclareNoncopyableObject( SynchronousActionState );
34
35 protected:
36 bool m_posted;
37 Threading::Semaphore m_sema;
38 ScopedPtr<BaseException> m_exception;
39
40 public:
41 sptr return_value;
42
43 SynchronousActionState()
44 {
45 m_posted = false;
46 return_value = 0;
47 }
48
49 virtual ~SynchronousActionState() throw() {}
50
51 void SetException( const BaseException& ex );
52 void SetException( BaseException* ex );
53
54 Threading::Semaphore& GetSemaphore() { return m_sema; }
55 const Threading::Semaphore& GetSemaphore() const { return m_sema; }
56
57 void RethrowException() const;
58 int WaitForResult();
59 int WaitForResult_NoExceptions();
60 void PostResult( int res );
61 void ClearResult();
62 void PostResult();
63 };
64
65
66 // --------------------------------------------------------------------------------------
67 // pxSimpleEvent - non-abstract implementation of wxEvent
68 // --------------------------------------------------------------------------------------
69 // Why? I mean, why is wxEvent abstract? Over-designed OO is such a bad habit. And while
70 // I'm on my high horse (and it's so very high!), why use 'private'? Ever? Seriously, it
71 // sucks. Stop using it, people.
72 //
73 class pxSimpleEvent : public wxEvent
74 {
75 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(pxSimpleEvent)
76
77 public:
78 explicit pxSimpleEvent( int evtid=0 )
79 : wxEvent(0, evtid)
80 { }
81
82 pxSimpleEvent( wxWindowID winId, int evtid )
83 : wxEvent(winId, evtid)
84 { }
85
86 virtual wxEvent *Clone() const { return new pxSimpleEvent(*this); }
87 };
88
89 // --------------------------------------------------------------------------------------
90 // pxActionEvent
91 // --------------------------------------------------------------------------------------
92 class pxActionEvent : public wxEvent
93 {
94 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(pxActionEvent)
95
96 protected:
97 SynchronousActionState* m_state;
98
99 public:
100 virtual ~pxActionEvent() throw() { }
101 virtual pxActionEvent *Clone() const { return new pxActionEvent(*this); }
102
103 explicit pxActionEvent( SynchronousActionState* sema=NULL, int msgtype=pxEvt_InvokeAction );
104 explicit pxActionEvent( SynchronousActionState& sema, int msgtype=pxEvt_InvokeAction );
105 pxActionEvent( const pxActionEvent& src );
106
107 Threading::Semaphore* GetSemaphore() const { return m_state ? &m_state->GetSemaphore() : NULL; }
108
109 const SynchronousActionState* GetSyncState() const { return m_state; }
110 SynchronousActionState* GetSyncState() { return m_state; }
111
112 void SetSyncState( SynchronousActionState* obj ) { m_state = obj; }
113 void SetSyncState( SynchronousActionState& obj ) { m_state = &obj; }
114
115 virtual void SetException( BaseException* ex );
116 void SetException( const BaseException& ex );
117
118 virtual void _DoInvokeEvent();
119
120 protected:
121 // Extending classes should implement this method to perfoem whatever action it is
122 // the event is supposed to do. :) Thread affinity is garaunteed to be the Main/UI
123 // thread, and exceptions will be handled automatically.
124 //
125 // Exception note: exceptions are passed back to the thread that posted the event
126 // to the queue, when possible. If the calling thread is not blocking for a result
127 // from this event, then the exception will be posted to the Main/UI thread instead.
128 virtual void InvokeEvent() {}
129 };
130
131
132 // --------------------------------------------------------------------------------------
133 // pxExceptionEvent
134 // --------------------------------------------------------------------------------------
135 class pxExceptionEvent : public pxActionEvent
136 {
137 typedef pxActionEvent _parent;
138
139 protected:
140 BaseException* m_except;
141
142 public:
143 pxExceptionEvent( BaseException* ex=NULL )
144 {
145 m_except = ex;
146 }
147
148 pxExceptionEvent( const BaseException& ex );
149
150 virtual ~pxExceptionEvent() throw()
151 {
152 }
153
154 virtual pxExceptionEvent *Clone() const { return new pxExceptionEvent(*this); }
155
156 protected:
157 void InvokeEvent();
158 };
159
160 // --------------------------------------------------------------------------------------
161 // pxSynchronousCommandEvent
162 // --------------------------------------------------------------------------------------
163
164 class pxSynchronousCommandEvent : public wxCommandEvent
165 {
166 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(pxSynchronousCommandEvent)
167
168 protected:
169 SynchronousActionState* m_sync;
170 wxEventType m_realEvent;
171
172 public:
173 virtual ~pxSynchronousCommandEvent() throw() { }
174 virtual pxSynchronousCommandEvent *Clone() const { return new pxSynchronousCommandEvent(*this); }
175
176 pxSynchronousCommandEvent(SynchronousActionState* sema=NULL, wxEventType commandType = wxEVT_NULL, int winid = 0);
177 pxSynchronousCommandEvent(SynchronousActionState& sema, wxEventType commandType = wxEVT_NULL, int winid = 0);
178
179 pxSynchronousCommandEvent(SynchronousActionState* sema, const wxCommandEvent& evt);
180 pxSynchronousCommandEvent(SynchronousActionState& sema, const wxCommandEvent& evt);
181
182 pxSynchronousCommandEvent(const pxSynchronousCommandEvent& src);
183
184 Threading::Semaphore* GetSemaphore() { return m_sync ? &m_sync->GetSemaphore() : NULL; }
185 wxEventType GetRealEventType() const { return m_realEvent; }
186
187 void SetException( BaseException* ex );
188 void SetException( const BaseException& ex );
189 };
190
191 // --------------------------------------------------------------------------------------
192 // BaseMessageBoxEvent
193 // --------------------------------------------------------------------------------------
194 class BaseMessageBoxEvent : public pxActionEvent
195 {
196 typedef pxActionEvent _parent;
197 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(BaseMessageBoxEvent)
198
199 protected:
200 wxString m_Content;
201
202 public:
203 virtual ~BaseMessageBoxEvent() throw() { }
204 virtual BaseMessageBoxEvent *Clone() const { return new BaseMessageBoxEvent(*this); }
205
206 explicit BaseMessageBoxEvent( const wxString& content=wxEmptyString, SynchronousActionState* instdata=NULL );
207 BaseMessageBoxEvent( const wxString& content, SynchronousActionState& instdata );
208 BaseMessageBoxEvent( const BaseMessageBoxEvent& event );
209
210 protected:
211 virtual void InvokeEvent();
212 virtual int _DoDialog() const;
213 };
214
215 // --------------------------------------------------------------------------------------
216 // MsgButtons
217 // --------------------------------------------------------------------------------------
218 class MsgButtons
219 {
220 protected:
221 BITFIELD32()
222 bool
223 m_OK :1,
224 m_Cancel :1,
225 m_Yes :1,
226 m_No :1,
227 m_AllowToAll:1,
228 m_Apply :1,
229 m_Abort :1,
230 m_Retry :1,
231 m_Ignore :1,
232 m_Reset :1,
233 m_Close :1;
234 BITFIELD_END
235
236 wxString m_CustomLabel;
237
238 public:
239 MsgButtons() { bitset = 0; }
240
241 MsgButtons& OK() { m_OK = true; return *this; }
242 MsgButtons& Cancel() { m_Cancel = true; return *this; }
243 MsgButtons& Apply() { m_Apply = true; return *this; }
244 MsgButtons& Yes() { m_Yes = true; return *this; }
245 MsgButtons& No() { m_No = true; return *this; }
246 MsgButtons& ToAll() { m_AllowToAll = true; return *this; }
247
248 MsgButtons& Abort() { m_Abort = true; return *this; }
249 MsgButtons& Retry() { m_Retry = true; return *this; }
250 MsgButtons& Ignore() { m_Ignore = true; return *this; }
251 MsgButtons& Reset() { m_Reset = true; return *this; }
252 MsgButtons& Close() { m_Close = true; return *this; }
253
254 MsgButtons& Custom( const wxString& label)
255 {
256 m_CustomLabel = label;
257 return *this;
258 }
259
260 MsgButtons& OKCancel() { m_OK = m_Cancel = true; return *this; }
261 MsgButtons& YesNo() { m_Yes = m_No = true; return *this; }
262
263 bool HasOK() const { return m_OK; }
264 bool HasCancel() const { return m_Cancel; }
265 bool HasApply() const { return m_Apply; }
266 bool HasYes() const { return m_Yes; }
267 bool HasNo() const { return m_No; }
268 bool AllowsToAll() const{ return m_AllowToAll; }
269
270 bool HasAbort() const { return m_Abort; }
271 bool HasRetry() const { return m_Retry; }
272 bool HasIgnore() const { return m_Ignore; }
273 bool HasReset() const { return m_Reset; }
274 bool HasClose() const { return m_Close; }
275
276 bool HasCustom() const { return !m_CustomLabel.IsEmpty(); }
277 const wxString& GetCustomLabel() const { return m_CustomLabel; }
278
279 bool Allows( wxWindowID id ) const;
280 void SetBestFocus( wxWindow* dialog ) const;
281 void SetBestFocus( wxWindow& dialog ) const;
282
283 bool operator ==( const MsgButtons& right ) const
284 {
285 return OpEqu( bitset );
286 }
287
288 bool operator !=( const MsgButtons& right ) const
289 {
290 return !OpEqu( bitset );
291 }
292 };
293
294 // --------------------------------------------------------------------------------------
295 // pxMessageBoxEvent
296 // --------------------------------------------------------------------------------------
297 // This event type is used to transfer message boxes to the main UI thread, and return the
298 // result of the box. It's the only way a message box can be issued from non-main threads
299 // with complete safety in wx2.8.
300 //
301 // For simplicity sake this message box only supports two basic designs. The main design
302 // is a generic message box with confirmation buttons of your choosing. Additionally you
303 // can specify a "scrollableContent" text string, which is added into a read-only richtext
304 // control similar to the console logs and such.
305 //
306 // Future consideration: If wxWidgets 3.0 has improved thread safety, then it should probably
307 // be reasonable for it to work with a more flexable model where the dialog can be created
308 // on a child thread, passed to the main thread, where ShowModal() is run (keeping the nested
309 // message pumps on the main thread where they belong). But so far this is not possible,
310 // because of various subtle issues in wx2.8 design.
311 //
312 class pxMessageBoxEvent : public BaseMessageBoxEvent
313 {
314 typedef BaseMessageBoxEvent _parent;
315 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(pxMessageBoxEvent)
316
317 protected:
318 wxString m_Title;
319 MsgButtons m_Buttons;
320
321 public:
322 virtual ~pxMessageBoxEvent() throw() { }
323 virtual pxMessageBoxEvent *Clone() const { return new pxMessageBoxEvent(*this); }
324
325 pxMessageBoxEvent() {}
326 pxMessageBoxEvent( const wxString& title, const wxString& content, const MsgButtons& buttons, SynchronousActionState& instdata );
327 pxMessageBoxEvent( const wxString& title, const wxString& content, const MsgButtons& buttons, SynchronousActionState* instdata=NULL );
328 pxMessageBoxEvent( const pxMessageBoxEvent& event );
329
330 protected:
331 int _DoDialog() const;
332 };
333
334 // --------------------------------------------------------------------------------------
335 // pxAssertionEvent
336 // --------------------------------------------------------------------------------------
337 class pxAssertionEvent : public BaseMessageBoxEvent
338 {
339 typedef BaseMessageBoxEvent _parent;
340 DECLARE_DYNAMIC_CLASS_NO_ASSIGN( pxAssertionEvent )
341
342 protected:
343 wxString m_Stacktrace;
344
345 public:
346 virtual ~pxAssertionEvent() throw() { }
347 virtual pxAssertionEvent *Clone() const { return new pxAssertionEvent(*this); }
348
349 pxAssertionEvent( const wxString& content=wxEmptyString, const wxString& trace=wxEmptyString, SynchronousActionState* instdata=NULL );
350 pxAssertionEvent( const wxString& content, const wxString& trace, SynchronousActionState& instdata );
351 pxAssertionEvent( const pxAssertionEvent& event );
352
353 pxAssertionEvent& SetStacktrace( const wxString& trace );
354
355 protected:
356 int _DoDialog() const;
357 };
358
359
360 typedef void (wxEvtHandler::*pxSyncronousEventFunction)(pxSynchronousCommandEvent&);
361
362 #define pxSynchronousEventHandler(func) \
363 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(pxSyncronousEventFunction, &func )

  ViewVC Help
Powered by ViewVC 1.1.22