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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (hide annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 7 months ago) by william
File MIME type: text/plain
File size: 13114 byte(s)
re-commit (had local access denied errors when committing)
1 william 31 /* 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 william 273 ScopedExcept m_exception;
39 william 31
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 william 62 explicit pxSimpleEvent( int evtid=0 )
79 william 31 : 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 william 62 // pxActionEvent
91 william 31 // --------------------------------------------------------------------------------------
92 william 62 class pxActionEvent : public wxEvent
93 william 31 {
94 william 62 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(pxActionEvent)
95 william 31
96     protected:
97     SynchronousActionState* m_state;
98    
99     public:
100 william 62 virtual ~pxActionEvent() throw() { }
101     virtual pxActionEvent *Clone() const { return new pxActionEvent(*this); }
102 william 31
103 william 62 explicit pxActionEvent( SynchronousActionState* sema=NULL, int msgtype=pxEvt_InvokeAction );
104     explicit pxActionEvent( SynchronousActionState& sema, int msgtype=pxEvt_InvokeAction );
105     pxActionEvent( const pxActionEvent& src );
106 william 31
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 william 62 // 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 william 31 virtual void InvokeEvent() {}
129     };
130    
131    
132     // --------------------------------------------------------------------------------------
133     // pxExceptionEvent
134     // --------------------------------------------------------------------------------------
135 william 62 class pxExceptionEvent : public pxActionEvent
136 william 31 {
137 william 62 typedef pxActionEvent _parent;
138 william 31
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 william 62 class BaseMessageBoxEvent : public pxActionEvent
195 william 31 {
196 william 62 typedef pxActionEvent _parent;
197 william 31 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 william 280 wxString m_CustomLabel;
237     wxString m_CustomLabelId;
238 william 31
239     public:
240     MsgButtons() { bitset = 0; }
241    
242     MsgButtons& OK() { m_OK = true; return *this; }
243     MsgButtons& Cancel() { m_Cancel = true; return *this; }
244     MsgButtons& Apply() { m_Apply = true; return *this; }
245     MsgButtons& Yes() { m_Yes = true; return *this; }
246     MsgButtons& No() { m_No = true; return *this; }
247     MsgButtons& ToAll() { m_AllowToAll = true; return *this; }
248    
249     MsgButtons& Abort() { m_Abort = true; return *this; }
250     MsgButtons& Retry() { m_Retry = true; return *this; }
251     MsgButtons& Ignore() { m_Ignore = true; return *this; }
252     MsgButtons& Reset() { m_Reset = true; return *this; }
253     MsgButtons& Close() { m_Close = true; return *this; }
254    
255 william 280 // label - native language label displayed to user
256     // id - raw ASCII identifier used in the config file (do not translate, hence char*)
257     MsgButtons& Custom( const wxString& label, const char* id )
258 william 31 {
259     m_CustomLabel = label;
260 william 280 m_CustomLabelId = fromUTF8(id);
261 william 31 return *this;
262     }
263    
264     MsgButtons& OKCancel() { m_OK = m_Cancel = true; return *this; }
265     MsgButtons& YesNo() { m_Yes = m_No = true; return *this; }
266    
267     bool HasOK() const { return m_OK; }
268     bool HasCancel() const { return m_Cancel; }
269     bool HasApply() const { return m_Apply; }
270     bool HasYes() const { return m_Yes; }
271     bool HasNo() const { return m_No; }
272     bool AllowsToAll() const{ return m_AllowToAll; }
273    
274     bool HasAbort() const { return m_Abort; }
275     bool HasRetry() const { return m_Retry; }
276     bool HasIgnore() const { return m_Ignore; }
277     bool HasReset() const { return m_Reset; }
278     bool HasClose() const { return m_Close; }
279    
280     bool HasCustom() const { return !m_CustomLabel.IsEmpty(); }
281 william 280 const wxString& GetCustomLabel() const { return m_CustomLabel; }
282     const wxString& GetCustomLabelId() const { return m_CustomLabelId; }
283 william 31
284     bool Allows( wxWindowID id ) const;
285     void SetBestFocus( wxWindow* dialog ) const;
286     void SetBestFocus( wxWindow& dialog ) const;
287    
288     bool operator ==( const MsgButtons& right ) const
289     {
290     return OpEqu( bitset );
291     }
292    
293     bool operator !=( const MsgButtons& right ) const
294     {
295     return !OpEqu( bitset );
296     }
297     };
298    
299     // --------------------------------------------------------------------------------------
300     // pxMessageBoxEvent
301     // --------------------------------------------------------------------------------------
302     // This event type is used to transfer message boxes to the main UI thread, and return the
303     // result of the box. It's the only way a message box can be issued from non-main threads
304     // with complete safety in wx2.8.
305     //
306     // For simplicity sake this message box only supports two basic designs. The main design
307     // is a generic message box with confirmation buttons of your choosing. Additionally you
308     // can specify a "scrollableContent" text string, which is added into a read-only richtext
309     // control similar to the console logs and such.
310     //
311     // Future consideration: If wxWidgets 3.0 has improved thread safety, then it should probably
312     // be reasonable for it to work with a more flexable model where the dialog can be created
313     // on a child thread, passed to the main thread, where ShowModal() is run (keeping the nested
314     // message pumps on the main thread where they belong). But so far this is not possible,
315     // because of various subtle issues in wx2.8 design.
316     //
317     class pxMessageBoxEvent : public BaseMessageBoxEvent
318     {
319     typedef BaseMessageBoxEvent _parent;
320     DECLARE_DYNAMIC_CLASS_NO_ASSIGN(pxMessageBoxEvent)
321    
322     protected:
323     wxString m_Title;
324     MsgButtons m_Buttons;
325    
326     public:
327     virtual ~pxMessageBoxEvent() throw() { }
328     virtual pxMessageBoxEvent *Clone() const { return new pxMessageBoxEvent(*this); }
329    
330     pxMessageBoxEvent() {}
331     pxMessageBoxEvent( const wxString& title, const wxString& content, const MsgButtons& buttons, SynchronousActionState& instdata );
332     pxMessageBoxEvent( const wxString& title, const wxString& content, const MsgButtons& buttons, SynchronousActionState* instdata=NULL );
333     pxMessageBoxEvent( const pxMessageBoxEvent& event );
334    
335     protected:
336     int _DoDialog() const;
337     };
338    
339     // --------------------------------------------------------------------------------------
340     // pxAssertionEvent
341     // --------------------------------------------------------------------------------------
342     class pxAssertionEvent : public BaseMessageBoxEvent
343     {
344     typedef BaseMessageBoxEvent _parent;
345     DECLARE_DYNAMIC_CLASS_NO_ASSIGN( pxAssertionEvent )
346    
347     protected:
348     wxString m_Stacktrace;
349    
350     public:
351     virtual ~pxAssertionEvent() throw() { }
352     virtual pxAssertionEvent *Clone() const { return new pxAssertionEvent(*this); }
353    
354     pxAssertionEvent( const wxString& content=wxEmptyString, const wxString& trace=wxEmptyString, SynchronousActionState* instdata=NULL );
355     pxAssertionEvent( const wxString& content, const wxString& trace, SynchronousActionState& instdata );
356     pxAssertionEvent( const pxAssertionEvent& event );
357    
358     pxAssertionEvent& SetStacktrace( const wxString& trace );
359    
360     protected:
361     int _DoDialog() const;
362     };
363    
364    
365     typedef void (wxEvtHandler::*pxSyncronousEventFunction)(pxSynchronousCommandEvent&);
366    
367     #define pxSynchronousEventHandler(func) \
368     (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(pxSyncronousEventFunction, &func )

  ViewVC Help
Powered by ViewVC 1.1.22