1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "wxole.h"
21 #include "wx/statusbr.h"
22 #include "wx/toolbar.h"
23 #include "wx/stream.h"
25 #include "wx/gtk/win_gtk.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>
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 const wxChar
*wxOleNameStr
= _T("olecontrol");
48 //---------------------------------------------------------------------------
49 // wxOleServerEnvPrivate
50 //---------------------------------------------------------------------------
52 class wxOleServerEnvPrivate
56 wxOleServerEnvPrivate() {}
57 ~wxOleServerEnvPrivate() {}
59 CORBA_Environment m_ev
;
63 //---------------------------------------------------------------------------
65 //---------------------------------------------------------------------------
67 class wxOleInputStream
: public wxInputStream
71 wxOleInputStream( GNOME_Stream stream
);
74 bool Ok() const { return m_error
; }
79 GNOME_Stream m_gstream
;
81 size_t OnSysRead(void *buffer
, size_t size
);
82 off_t
OnSysSeek(off_t pos
, wxSeekMode mode
);
83 off_t
OnSysTell() const;
86 //---------------------------------------------------------------------------
88 //---------------------------------------------------------------------------
90 IMPLEMENT_CLASS(wxOleServerEnv
,wxObject
)
92 wxOleServerEnv::wxOleServerEnv( const wxString
&name
, const wxString
&version
)
95 m_serverVersion
= version
;
97 m_priv
= new wxOleServerEnvPrivate();
99 CORBA_exception_init( &(m_priv
->m_ev
) );
102 m_serverName
.mb_str(),
103 m_serverVersion
.mb_str(),
106 GNORBA_INIT_SERVER_FUNC
,
109 if (m_priv
->m_ev
._major
!= CORBA_NO_EXCEPTION
)
114 m_priv
->m_orb
= gnome_CORBA_ORB();
116 if (bonobo_init( m_priv
->m_orb
, NULL
, NULL
) == FALSE
)
123 wxOleServerEnv::~wxOleServerEnv()
125 CORBA_exception_free( &(m_priv
->m_ev
) );
129 //---------------------------------------------------------------------------
131 //---------------------------------------------------------------------------
133 wxOleInputStream::wxOleInputStream( GNOME_Stream stream
)
136 m_error
= (m_gstream
);
139 wxOleInputStream::~wxOleInputStream()
141 /* we don't create the stream so we
142 don't destroy it either. */
145 size_t wxOleInputStream::OnSysRead( void *buffer
, size_t size
)
147 GNOME_Stream_iobuf
*gbuffer
= GNOME_Stream_iobuf__alloc();
149 CORBA_Environment ev
;
150 CORBA_exception_init( &ev
);
152 GNOME_Stream_read( m_gstream
, size
, &gbuffer
, &ev
);
154 CORBA_exception_free( &ev
);
156 memcpy( buffer
, gbuffer
->_buffer
, gbuffer
->_length
);
158 m_error
= (gbuffer
->_length
!= size
);
160 CORBA_free( gbuffer
);
163 off_t
wxOleInputStream::OnSysSeek( off_t pos
, wxSeekMode mode
)
165 CORBA_Environment ev
;
166 CORBA_exception_init( &ev
);
168 GNOME_Stream_seek( m_gstream
, pos
/* offset */, 0 /* whence */, &ev
);
170 CORBA_exception_free( &ev
);
173 off_t
wxOleInputStream::OnSysTell() const
175 return 0; /* oh well */
179 //---------------------------------------------------------------------------
180 // wxOleServerPrivate
181 //---------------------------------------------------------------------------
183 class wxOleServerPrivate
187 wxOleServerPrivate() {}
188 ~wxOleServerPrivate() {}
190 GnomeComponentFactory
*m_factory
;
193 //---------------------------------------------------------------------------
195 //---------------------------------------------------------------------------
198 gnome_view_factory_callback( GnomeComponent
*component
, wxOleServer
*server
)
201 printf( "Create OLE control.\n" );
204 wxOleControl
*ctx
= server
->CreateOleControl();
206 if (!ctx
) return (GnomeView
*) NULL
;
209 printf( "Creating OLE control succeeded. Returning as GnomeView\n" );
212 return gnome_view_new( ctx
->m_widget
);
216 gnome_load_from_stream_callback( GnomePersistStream
*ps
, GNOME_Stream stream
, GnomeComponent
* component
)
218 wxOleInputStream
wxstream( stream
);
223 static GnomeComponent
*
224 gnome_component_factory_callback( GnomeComponentFactory
*factory
, const char *path
, wxOleServer
*server
)
227 printf( "new component.\n" );
228 if (path) printf( "path is %s.\n", path );
231 GnomeComponent
*component
=
232 gnome_component_new( gnome_view_factory_callback
, (void*) server
);
236 printf( "component creation failed.\n" );
238 printf( "component creation succeded.\n" );
241 GnomePersistStream
*stream
=
242 gnome_persist_stream_new( gnome_load_from_stream_callback
, NULL
/*save*/, (void*) component
);
246 printf( "stream creation failed.\n" );
248 printf( "stream creation succeded.\n" );
251 gtk_object_add_interface( GTK_OBJECT(component
), GTK_OBJECT(stream
) );
256 IMPLEMENT_CLASS(wxOleServer
,wxObject
)
258 wxOleServer::wxOleServer( const wxString
&id
)
263 m_priv
= new wxOleServerPrivate();
266 printf( "new component factory.\n" );
270 gnome_component_factory_new( m_ID
.mb_str(), gnome_component_factory_callback
, (void*) this );
273 wxOleServer::~wxOleServer()
278 wxOleControl
*wxOleServer::CreateOleControl()
280 return new wxOleControl( -1 );
283 //-----------------------------------------------------------------------------
285 //-----------------------------------------------------------------------------
287 static void gtk_olectx_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxOleControl
*win
)
289 if (!win
->m_hasVMT
) return;
292 printf( "OnFrameResize from " );
293 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
294 printf( win->GetClassInfo()->GetClassName() );
298 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
300 win
->m_sizeSet
= FALSE
;
301 win
->m_width
= alloc
->width
;
302 win
->m_height
= alloc
->height
;
306 //-----------------------------------------------------------------------------
308 //-----------------------------------------------------------------------------
310 static gint
gtk_olectx_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxOleControl
*win
)
313 printf( "OnDelete from " );
314 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
315 printf( win->GetClassInfo()->GetClassName() );
324 //-----------------------------------------------------------------------------
326 //-----------------------------------------------------------------------------
328 static gint
gtk_olectx_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxOleControl
*win
)
330 if (!win
->m_hasVMT
) return FALSE
;
335 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
336 mevent
.SetEventObject( win
);
337 win
->GetEventHandler()->ProcessEvent( mevent
);
342 //---------------------------------------------------------------------------
344 //---------------------------------------------------------------------------
346 IMPLEMENT_CLASS(wxOleControl
,wxFrame
)
348 wxOleControl::wxOleControl( wxWindowID id
, long style
, const wxString
&name
)
350 Create( id
, style
, name
);
353 bool wxOleControl::Create( wxWindowID id
, long style
, const wxString
&name
)
355 wxTopLevelWindows
.Append( this );
357 m_needParent
= FALSE
;
359 PreCreation( (wxWindow
*) NULL
, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
361 m_title
= _T("wxWindows OLE Server");
363 /* any widget that can contain another widget and resizes it
365 m_widget
= gtk_hbox_new(0,0);
367 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
369 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
370 GTK_SIGNAL_FUNC(gtk_olectx_delete_callback
), (gpointer
)this );
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
);
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
);
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 );
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 );
394 gtk_widget_show_all( m_widget
);
399 wxOleControl::~wxOleControl()
401 if (m_frameMenuBar
) delete m_frameMenuBar
;
402 m_frameMenuBar
= (wxMenuBar
*) NULL
;
404 if (m_frameStatusBar
) delete m_frameStatusBar
;
405 m_frameStatusBar
= (wxStatusBar
*) NULL
;
407 if (m_frameToolBar
) delete m_frameToolBar
;
408 m_frameToolBar
= (wxToolBar
*) NULL
;
410 wxTopLevelWindows
.DeleteObject( this );
412 if (wxTheApp
->GetTopWindow() == this)
413 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
415 if (wxTopLevelWindows
.Number() == 0)
416 wxTheApp
->ExitMainLoop();
420 void wxOleControl::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
425 void wxOleControl::DoSetClientSize(int width
, int height
)