Add libwx-perl
[pkg-perl] / deb-src / libwx-perl / libwx-perl-0.96 / debian / libwx-perl / usr / lib / perl5 / Wx / cpp / event.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        cpp/event.h
3 // Purpose:     C++ helpers for user-defined events
4 // Author:      Mattia Barbon
5 // Modified by:
6 // Created:     30/03/2002
7 // RCS-ID:      $Id: event.h 2526 2009-02-07 14:35:21Z mbarbon $
8 // Copyright:   (c) 2002-2004, 2006-2007, 2009 Mattia Barbon
9 // Licence:     This program is free software; you can redistribute it and/or
10 //              modify it under the same terms as Perl itself
11 /////////////////////////////////////////////////////////////////////////////
12
13 #include <wx/event.h>
14
15 #include "cpp/v_cback.h"
16
17 class wxPlEvent : public wxEvent
18 {
19     WXPLI_DECLARE_DYNAMIC_CLASS_CTOR( wxPlEvent );
20     WXPLI_DECLARE_V_CBACK();
21
22     // only to appease wxWidgets' RTTI
23     wxPlEvent() : m_callback( NULL ) {}
24 public:
25     wxPlEvent( const char* package, int id, wxEventType eventType )
26         : wxEvent( id, eventType ),
27           m_callback( "Wx::PlEvent" )
28     {
29         m_callback.SetSelf( wxPli_make_object( this, package ), true );
30     }
31
32     virtual ~wxPlEvent()
33     {
34         m_callback.DeleteSelf( false );
35     }
36
37     virtual wxEvent* Clone() const;
38 };
39
40 wxEvent* wxPlEvent::Clone() const
41 {
42     dTHX;
43     wxPlEvent* self = (wxPlEvent*)this;
44
45     // only to appease wxWidgets' RTTI
46     if( !self->m_callback.IsOk() )
47         return new wxPlEvent();
48
49     if( wxPliVirtualCallback_FindCallback( aTHX_ &self->m_callback, "Clone" ) )
50     {
51         SV* ret = wxPliVirtualCallback_CallCallback
52             ( aTHX_ &self->m_callback, G_SCALAR, NULL );
53         wxPlEvent* clone =
54             (wxPlEvent*)wxPli_sv_2_object( aTHX_ ret, "Wx::PlEvent" );
55         
56         return clone;
57     }
58
59     return 0;
60 }
61
62 WXPLI_IMPLEMENT_DYNAMIC_CLASS_CTOR( wxPlEvent, wxEvent );
63
64 class wxPlCommandEvent : public wxCommandEvent
65 {
66     WXPLI_DECLARE_DYNAMIC_CLASS_CTOR( wxPlCommandEvent );
67     WXPLI_DECLARE_V_CBACK();
68
69     // only to appease wxWidgets' RTTI
70     wxPlCommandEvent() : m_callback( NULL ) {}
71 public:
72     wxPlCommandEvent( const char* package, int id, wxEventType eventType )
73         : wxCommandEvent( id, eventType ),
74           m_callback( "Wx::PlCommandEvent" )
75     {
76         m_callback.SetSelf( wxPli_make_object( this, package ), true );
77     }
78
79     virtual ~wxPlCommandEvent()
80     {
81         m_callback.DeleteSelf( false );
82     }
83
84     virtual wxEvent* Clone() const;
85 };
86
87 wxEvent* wxPlCommandEvent::Clone() const
88 {
89     dTHX;
90     wxPlCommandEvent* self = (wxPlCommandEvent*)this;
91
92     // only to appease wxWidgets' RTTI
93     if( !self->m_callback.IsOk() )
94         return new wxPlCommandEvent();
95
96     if( wxPliVirtualCallback_FindCallback( aTHX_ &self->m_callback, "Clone" ) )
97     {
98         SV* ret = wxPliVirtualCallback_CallCallback
99             ( aTHX_ &self->m_callback, G_SCALAR, NULL );
100         wxPlCommandEvent* clone = (wxPlCommandEvent*)
101             wxPli_sv_2_object( aTHX_ ret, "Wx::PlCommandEvent" );
102
103         return clone;
104     }
105
106     return 0;
107 }
108
109 WXPLI_IMPLEMENT_DYNAMIC_CLASS_CTOR( wxPlCommandEvent, wxCommandEvent );
110
111 class wxPlThreadEvent : public wxEvent
112 {
113     WXPLI_DECLARE_DYNAMIC_CLASS_CTOR( wxPlThreadEvent );
114 public:
115     static void SetStash( SV* hv_ref )
116     {
117         m_hv = (HV*)SvRV( hv_ref );
118     }
119     static HV* GetStash() { return m_hv; }
120
121     wxPlThreadEvent() : m_data( 0 ) {}
122     wxPlThreadEvent( pTHX_ const char* package, int id, wxEventType eventType,
123                      SV* data )
124         : wxEvent( id, eventType )
125     {
126         PL_lockhook( aTHX_ (SV*)GetStash() );
127         PL_sharehook( aTHX_ data );
128         int data_id;
129         char buffer[30];
130         size_t length;
131         for(;;)
132         {
133             data_id = rand();
134             length = sprintf( buffer, "%d", data_id );
135             if( !hv_exists( GetStash(), buffer, length ) )
136                 break;
137         }
138         SV** dst = hv_fetch( GetStash(), buffer, length, 1 );
139         sv_setsv( *dst, data );
140         mg_set( *dst );
141         m_data = data_id;    
142     }
143
144     wxPlThreadEvent( const wxPlThreadEvent& e )
145         : wxEvent( e ),
146           m_data( e.m_data )
147     { }
148
149     ~wxPlThreadEvent()
150     { 
151         if( !m_data )
152             return;
153
154         dTHX;
155
156         ENTER;
157         SAVETMPS;
158
159         PL_lockhook( aTHX_ (SV*)m_hv );
160
161         char buffer[30];
162         size_t length = sprintf( buffer, "%d", m_data );
163
164         hv_delete( m_hv, buffer, length, G_DISCARD );
165
166         FREETMPS;
167         LEAVE;
168     }
169
170     int _GetData() const { return m_data; }
171
172     SV* GetData() const
173     {
174         dTHX;
175
176         if( !m_data )
177             return &PL_sv_undef;
178
179         PL_lockhook( aTHX_ (SV*)m_hv );
180
181         char buffer[30];
182         size_t length = sprintf( buffer, "%d", m_data );
183
184         SV** value = hv_fetch( m_hv, buffer, length, 0 );
185         if( !value )
186             return NULL;
187         mg_get( *value );
188         SvREFCNT_inc( *value );
189
190         return *value;
191     }
192
193     virtual wxEvent* Clone() const;
194 private:
195     int m_data;
196     static HV* m_hv;
197 };
198
199 wxEvent* wxPlThreadEvent::Clone() const
200 {
201     wxEvent* clone = new wxPlThreadEvent( *this );
202     ((wxPlThreadEvent*)this)->m_data = 0;
203
204     return clone;
205 }
206
207 HV* wxPlThreadEvent::m_hv = NULL;
208
209 wxPliSelfRef* wxPliGetSelfForwxPlThreadEvent( wxObject* object ) { return 0; }
210 // XXX HACK!
211 #if wxUSE_EXTENDED_RTTI
212 const wxClassInfo* wxPlThreadEvent::ms_classParents[] =
213     { &wxEvent::ms_classInfo , NULL };
214 wxPliClassInfo wxPlThreadEvent::ms_classInfo(
215     ms_classParents, (wxChar*)wxT( "wxPlPlThreadEvent"),
216     (int)sizeof(wxPlThreadEvent), wxPlThreadEvent::wxCreateObject,
217     (wxPliGetCallbackObjectFn) wxPliGetSelfForwxPlThreadEvent );
218 #else
219 wxPliClassInfo wxPlThreadEvent::ms_classInfo(
220     (wxChar*)wxT( "wxPlPlThreadEvent"), &wxEvent::ms_classInfo,
221     NULL, (int)sizeof(wxPlThreadEvent), wxPlThreadEvent::wxCreateObject,
222     (wxPliGetCallbackObjectFn) wxPliGetSelfForwxPlThreadEvent );
223 #endif
224 wxObject* wxPlThreadEvent::wxCreateObject() { return new wxPlThreadEvent(); }
225
226 // local variables: //
227 // mode: c++ //
228 // end: //