]> git.saurik.com Git - wxWidgets.git/blob - utils/wxOLE/gtk/wxole.cpp
fix memory leak as Dimitri suggested
[wxWidgets.git] / utils / wxOLE / gtk / wxole.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxole.cpp
3 // Purpose: wxOLE
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 20/04/99
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "wxole.h"
14 #endif
15
16 #include "wx/defs.h"
17 #include "wxole.h"
18
19 #include "wx/app.h"
20 #include "wx/menu.h"
21 #include "wx/statusbr.h"
22 #include "wx/toolbar.h"
23 #include "wx/stream.h"
24
25 #include "wx/gtk/win_gtk.h"
26
27 extern "C" {
28 #include "gtk/gtk.h"
29 #include "gdk/gdk.h"
30
31 #include <gnome.h>
32 #include <libgnorba/gnorba.h>
33 #include <bonobo/bonobo.h>
34 #include <bonobo/gnome-main.h>
35 #include <bonobo/gnome-component.h>
36 #include <bonobo/gnome-component-factory.h>
37 #include <bonobo/gnome-persist-stream.h>
38 #include <bonobo/gtk-interfaces.h>
39 }
40
41
42 //-----------------------------------------------------------------------------
43 // global data
44 //-----------------------------------------------------------------------------
45
46 const wxChar *wxOleNameStr = _T("olecontrol");
47
48 //---------------------------------------------------------------------------
49 // wxOleServerEnvPrivate
50 //---------------------------------------------------------------------------
51
52 class wxOleServerEnvPrivate
53 {
54 public:
55
56 wxOleServerEnvPrivate() {}
57 ~wxOleServerEnvPrivate() {}
58
59 CORBA_Environment m_ev;
60 CORBA_ORB m_orb;
61 };
62
63 //---------------------------------------------------------------------------
64 // wxOleInputStream
65 //---------------------------------------------------------------------------
66
67 class wxOleInputStream : public wxInputStream
68 {
69 public:
70
71 wxOleInputStream( GNOME_Stream stream );
72 ~wxOleInputStream();
73
74 bool Ok() const { return m_error; }
75
76 protected:
77
78 bool m_error;
79 GNOME_Stream m_gstream;
80
81 size_t OnSysRead(void *buffer, size_t size);
82 off_t OnSysSeek(off_t pos, wxSeekMode mode);
83 off_t OnSysTell() const;
84 };
85
86 //---------------------------------------------------------------------------
87 // wxOleServerEnv
88 //---------------------------------------------------------------------------
89
90 IMPLEMENT_CLASS(wxOleServerEnv,wxObject)
91
92 wxOleServerEnv::wxOleServerEnv( const wxString &name, const wxString &version )
93 {
94 m_serverName = name;
95 m_serverVersion = version;
96
97 m_priv = new wxOleServerEnvPrivate();
98
99 CORBA_exception_init( &(m_priv->m_ev) );
100
101 gnome_CORBA_init(
102 m_serverName.mb_str(),
103 m_serverVersion.mb_str(),
104 &wxTheApp->argc,
105 wxTheApp->argv,
106 GNORBA_INIT_SERVER_FUNC,
107 &(m_priv->m_ev) );
108
109 if (m_priv->m_ev._major != CORBA_NO_EXCEPTION)
110 {
111 return;
112 }
113
114 m_priv->m_orb = gnome_CORBA_ORB();
115
116 if (bonobo_init( m_priv->m_orb, NULL, NULL ) == FALSE)
117 {
118 return;
119 }
120
121 }
122
123 wxOleServerEnv::~wxOleServerEnv()
124 {
125 CORBA_exception_free( &(m_priv->m_ev) );
126 delete m_priv;
127 }
128
129 //---------------------------------------------------------------------------
130 // wxOleInputStream
131 //---------------------------------------------------------------------------
132
133 wxOleInputStream::wxOleInputStream( GNOME_Stream stream )
134 {
135 m_gstream = stream;
136 m_error = (m_gstream);
137 }
138
139 wxOleInputStream::~wxOleInputStream()
140 {
141 /* we don't create the stream so we
142 don't destroy it either. */
143 }
144
145 size_t wxOleInputStream::OnSysRead( void *buffer, size_t size )
146 {
147 GNOME_Stream_iobuf *gbuffer = GNOME_Stream_iobuf__alloc();
148
149 CORBA_Environment ev;
150 CORBA_exception_init( &ev );
151
152 GNOME_Stream_read( m_gstream, size, &gbuffer, &ev );
153
154 CORBA_exception_free( &ev );
155
156 memcpy( buffer, gbuffer->_buffer, gbuffer->_length );
157
158 m_error = (gbuffer->_length != size);
159
160 CORBA_free( gbuffer );
161 }
162
163 off_t wxOleInputStream::OnSysSeek( off_t pos, wxSeekMode mode )
164 {
165 CORBA_Environment ev;
166 CORBA_exception_init( &ev );
167
168 GNOME_Stream_seek( m_gstream, pos /* offset */, 0 /* whence */, &ev );
169
170 CORBA_exception_free( &ev );
171 }
172
173 off_t wxOleInputStream::OnSysTell() const
174 {
175 return 0; /* oh well */
176 }
177
178
179 //---------------------------------------------------------------------------
180 // wxOleServerPrivate
181 //---------------------------------------------------------------------------
182
183 class wxOleServerPrivate
184 {
185 public:
186
187 wxOleServerPrivate() {}
188 ~wxOleServerPrivate() {}
189
190 GnomeComponentFactory *m_factory;
191 };
192
193 //---------------------------------------------------------------------------
194 // wxOleServer
195 //---------------------------------------------------------------------------
196
197 static GnomeView*
198 gnome_view_factory_callback( GnomeComponent *component, wxOleServer *server )
199 {
200 /*
201 printf( "Create OLE control.\n" );
202 */
203
204 wxOleControl *ctx = server->CreateOleControl();
205
206 if (!ctx) return (GnomeView*) NULL;
207
208 /*
209 printf( "Creating OLE control succeeded. Returning as GnomeView\n" );
210 */
211
212 return gnome_view_new( ctx->m_widget );
213 }
214
215 static int
216 gnome_load_from_stream_callback( GnomePersistStream *ps, GNOME_Stream stream, GnomeComponent* component )
217 {
218 wxOleInputStream wxstream( stream );
219
220
221 }
222
223 static GnomeComponent*
224 gnome_component_factory_callback( GnomeComponentFactory *factory, const char *path, wxOleServer *server )
225 {
226 /*
227 printf( "new component.\n" );
228 if (path) printf( "path is %s.\n", path );
229 */
230
231 GnomeComponent *component =
232 gnome_component_new( gnome_view_factory_callback, (void*) server );
233
234 /*
235 if (!component)
236 printf( "component creation failed.\n" );
237 else
238 printf( "component creation succeded.\n" );
239 */
240
241 GnomePersistStream *stream =
242 gnome_persist_stream_new( gnome_load_from_stream_callback, NULL /*save*/, (void*) component );
243
244 /*
245 if (!stream)
246 printf( "stream creation failed.\n" );
247 else
248 printf( "stream creation succeded.\n" );
249 */
250
251 gtk_object_add_interface( GTK_OBJECT(component), GTK_OBJECT(stream) );
252
253 return component;
254 }
255
256 IMPLEMENT_CLASS(wxOleServer,wxObject)
257
258 wxOleServer::wxOleServer( const wxString &id )
259 {
260 m_ID = "component:";
261 m_ID += id;
262
263 m_priv = new wxOleServerPrivate();
264
265 /*
266 printf( "new component factory.\n" );
267 */
268
269 m_priv->m_factory =
270 gnome_component_factory_new( m_ID.mb_str(), gnome_component_factory_callback, (void*) this );
271 }
272
273 wxOleServer::~wxOleServer()
274 {
275 delete m_priv;
276 }
277
278 wxOleControl *wxOleServer::CreateOleControl()
279 {
280 return new wxOleControl( -1 );
281 }
282
283 //-----------------------------------------------------------------------------
284 // "size_allocate"
285 //-----------------------------------------------------------------------------
286
287 static void gtk_olectx_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxOleControl *win )
288 {
289 if (!win->m_hasVMT) return;
290
291 /*
292 printf( "OnFrameResize from " );
293 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
294 printf( win->GetClassInfo()->GetClassName() );
295 printf( ".\n" );
296 */
297
298 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
299 {
300 win->m_sizeSet = FALSE;
301 win->m_width = alloc->width;
302 win->m_height = alloc->height;
303 }
304 }
305
306 //-----------------------------------------------------------------------------
307 // "delete_event"
308 //-----------------------------------------------------------------------------
309
310 static gint gtk_olectx_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxOleControl *win )
311 {
312 /*
313 printf( "OnDelete from " );
314 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
315 printf( win->GetClassInfo()->GetClassName() );
316 printf( ".\n" );
317 */
318
319 win->Close();
320
321 return TRUE;
322 }
323
324 //-----------------------------------------------------------------------------
325 // "configure_event"
326 //-----------------------------------------------------------------------------
327
328 static gint gtk_olectx_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxOleControl *win )
329 {
330 if (!win->m_hasVMT) return FALSE;
331
332 win->m_x = event->x;
333 win->m_y = event->y;
334
335 wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() );
336 mevent.SetEventObject( win );
337 win->GetEventHandler()->ProcessEvent( mevent );
338
339 return FALSE;
340 }
341
342 //---------------------------------------------------------------------------
343 // wxOleControl
344 //---------------------------------------------------------------------------
345
346 IMPLEMENT_CLASS(wxOleControl,wxFrame)
347
348 wxOleControl::wxOleControl( wxWindowID id, long style, const wxString &name )
349 {
350 Create( id, style, name );
351 }
352
353 bool wxOleControl::Create( wxWindowID id, long style, const wxString &name )
354 {
355 wxTopLevelWindows.Append( this );
356
357 m_needParent = FALSE;
358
359 PreCreation( (wxWindow*) NULL, id, wxDefaultPosition, wxDefaultSize, style, name );
360
361 m_title = _T("wxWindows OLE Server");
362
363 /* any widget that can contain another widget and resizes it
364 to its full size */
365 m_widget = gtk_hbox_new(0,0);
366
367 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
368
369 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
370 GTK_SIGNAL_FUNC(gtk_olectx_delete_callback), (gpointer)this );
371
372 /* m_mainWidget holds the toolbar, the menubar and the client area */
373 m_mainWidget = gtk_myfixed_new();
374 gtk_widget_show( m_mainWidget );
375 GTK_WIDGET_UNSET_FLAGS( m_mainWidget, GTK_CAN_FOCUS );
376 gtk_container_add( GTK_CONTAINER(m_widget), m_mainWidget );
377
378 /* m_wxwindow only represents the client area without toolbar and menubar */
379 m_wxwindow = gtk_myfixed_new();
380 gtk_widget_show( m_wxwindow );
381 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
382 gtk_container_add( GTK_CONTAINER(m_mainWidget), m_wxwindow );
383
384 PostCreation();
385
386 /* the user resized the frame by dragging etc. */
387 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
388 GTK_SIGNAL_FUNC(gtk_olectx_size_callback), (gpointer)this );
389
390 /* the only way to get the window size is to connect to this event */
391 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
392 GTK_SIGNAL_FUNC(gtk_olectx_configure_callback), (gpointer)this );
393
394 gtk_widget_show_all( m_widget );
395
396 return TRUE;
397 }
398
399 wxOleControl::~wxOleControl()
400 {
401 if (m_frameMenuBar) delete m_frameMenuBar;
402 m_frameMenuBar = (wxMenuBar *) NULL;
403
404 if (m_frameStatusBar) delete m_frameStatusBar;
405 m_frameStatusBar = (wxStatusBar *) NULL;
406
407 if (m_frameToolBar) delete m_frameToolBar;
408 m_frameToolBar = (wxToolBar *) NULL;
409
410 wxTopLevelWindows.DeleteObject( this );
411
412 if (wxTheApp->GetTopWindow() == this)
413 wxTheApp->SetTopWindow( (wxWindow*) NULL );
414
415 if (wxTopLevelWindows.Number() == 0)
416 wxTheApp->ExitMainLoop();
417 }
418
419
420 void wxOleControl::DoSetSize( int x, int y, int width, int height, int sizeFlags )
421 {
422 // ignore
423 }
424
425 void wxOleControl::DoSetClientSize(int width, int height)
426 {
427 // ignore
428 }