1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
17 #include "wx/statusbr.h"
18 #include "wx/toolbar.h"
19 #include "wx/stream.h"
21 #include "wx/gtk/win_gtk.h"
28 #include <libgnorba/gnorba.h>
29 #include <bonobo/bonobo.h>
30 #include <bonobo/gnome-main.h>
31 #include <bonobo/gnome-component.h>
32 #include <bonobo/gnome-component-factory.h>
33 #include <bonobo/gnome-persist-stream.h>
34 #include <bonobo/gtk-interfaces.h>
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 const wxChar
*wxOleNameStr
= _T("olecontrol");
44 //---------------------------------------------------------------------------
45 // wxOleServerEnvPrivate
46 //---------------------------------------------------------------------------
48 class wxOleServerEnvPrivate
52 wxOleServerEnvPrivate() {}
53 ~wxOleServerEnvPrivate() {}
55 CORBA_Environment m_ev
;
59 //---------------------------------------------------------------------------
61 //---------------------------------------------------------------------------
63 class wxOleInputStream
: public wxInputStream
67 wxOleInputStream( GNOME_Stream stream
);
70 bool Ok() const { return m_error
; }
75 GNOME_Stream m_gstream
;
77 size_t OnSysRead(void *buffer
, size_t size
);
78 off_t
OnSysSeek(off_t pos
, wxSeekMode mode
);
79 off_t
OnSysTell() const;
82 //---------------------------------------------------------------------------
84 //---------------------------------------------------------------------------
86 IMPLEMENT_CLASS(wxOleServerEnv
,wxObject
)
88 wxOleServerEnv::wxOleServerEnv( const wxString
&name
, const wxString
&version
)
91 m_serverVersion
= version
;
93 m_priv
= new wxOleServerEnvPrivate();
95 CORBA_exception_init( &(m_priv
->m_ev
) );
98 m_serverName
.mb_str(),
99 m_serverVersion
.mb_str(),
102 GNORBA_INIT_SERVER_FUNC
,
105 if (m_priv
->m_ev
._major
!= CORBA_NO_EXCEPTION
)
110 m_priv
->m_orb
= gnome_CORBA_ORB();
112 if (bonobo_init( m_priv
->m_orb
, NULL
, NULL
) == FALSE
)
119 wxOleServerEnv::~wxOleServerEnv()
121 CORBA_exception_free( &(m_priv
->m_ev
) );
125 //---------------------------------------------------------------------------
127 //---------------------------------------------------------------------------
129 wxOleInputStream::wxOleInputStream( GNOME_Stream stream
)
132 m_error
= (m_gstream
);
135 wxOleInputStream::~wxOleInputStream()
137 /* we don't create the stream so we
138 don't destroy it either. */
141 size_t wxOleInputStream::OnSysRead( void *buffer
, size_t size
)
143 GNOME_Stream_iobuf
*gbuffer
= GNOME_Stream_iobuf__alloc();
145 CORBA_Environment ev
;
146 CORBA_exception_init( &ev
);
148 GNOME_Stream_read( m_gstream
, size
, &gbuffer
, &ev
);
150 CORBA_exception_free( &ev
);
152 memcpy( buffer
, gbuffer
->_buffer
, gbuffer
->_length
);
154 m_error
= (gbuffer
->_length
!= size
);
156 CORBA_free( gbuffer
);
159 off_t
wxOleInputStream::OnSysSeek( off_t pos
, wxSeekMode mode
)
161 CORBA_Environment ev
;
162 CORBA_exception_init( &ev
);
164 GNOME_Stream_seek( m_gstream
, pos
/* offset */, 0 /* whence */, &ev
);
166 CORBA_exception_free( &ev
);
169 off_t
wxOleInputStream::OnSysTell() const
171 return 0; /* oh well */
175 //---------------------------------------------------------------------------
176 // wxOleServerPrivate
177 //---------------------------------------------------------------------------
179 class wxOleServerPrivate
183 wxOleServerPrivate() {}
184 ~wxOleServerPrivate() {}
186 GnomeComponentFactory
*m_factory
;
189 //---------------------------------------------------------------------------
191 //---------------------------------------------------------------------------
194 gnome_view_factory_callback( GnomeComponent
*component
, wxOleServer
*server
)
197 printf( "Create OLE control.\n" );
200 wxOleControl
*ctx
= server
->CreateOleControl();
202 if (!ctx
) return (GnomeView
*) NULL
;
205 printf( "Creating OLE control succeeded. Returning as GnomeView\n" );
208 return gnome_view_new( ctx
->m_widget
);
212 gnome_load_from_stream_callback( GnomePersistStream
*ps
, GNOME_Stream stream
, GnomeComponent
* component
)
214 wxOleInputStream
wxstream( stream
);
219 static GnomeComponent
*
220 gnome_component_factory_callback( GnomeComponentFactory
*factory
, const char *path
, wxOleServer
*server
)
223 printf( "new component.\n" );
224 if (path) printf( "path is %s.\n", path );
227 GnomeComponent
*component
=
228 gnome_component_new( gnome_view_factory_callback
, (void*) server
);
232 printf( "component creation failed.\n" );
234 printf( "component creation succeded.\n" );
237 GnomePersistStream
*stream
=
238 gnome_persist_stream_new( gnome_load_from_stream_callback
, NULL
/*save*/, (void*) component
);
242 printf( "stream creation failed.\n" );
244 printf( "stream creation succeded.\n" );
247 gtk_object_add_interface( GTK_OBJECT(component
), GTK_OBJECT(stream
) );
252 IMPLEMENT_CLASS(wxOleServer
,wxObject
)
254 wxOleServer::wxOleServer( const wxString
&id
)
259 m_priv
= new wxOleServerPrivate();
262 printf( "new component factory.\n" );
266 gnome_component_factory_new( m_ID
.mb_str(), gnome_component_factory_callback
, (void*) this );
269 wxOleServer::~wxOleServer()
274 wxOleControl
*wxOleServer::CreateOleControl()
276 return new wxOleControl( -1 );
279 //-----------------------------------------------------------------------------
281 //-----------------------------------------------------------------------------
283 static void gtk_olectx_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxOleControl
*win
)
285 if (!win
->m_hasVMT
) return;
288 printf( "OnFrameResize from " );
289 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
290 printf( win->GetClassInfo()->GetClassName() );
294 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
296 win
->m_sizeSet
= FALSE
;
297 win
->m_width
= alloc
->width
;
298 win
->m_height
= alloc
->height
;
302 //-----------------------------------------------------------------------------
304 //-----------------------------------------------------------------------------
306 static gint
gtk_olectx_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxOleControl
*win
)
309 printf( "OnDelete from " );
310 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
311 printf( win->GetClassInfo()->GetClassName() );
320 //-----------------------------------------------------------------------------
322 //-----------------------------------------------------------------------------
324 static gint
gtk_olectx_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxOleControl
*win
)
326 if (!win
->m_hasVMT
) return FALSE
;
331 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
332 mevent
.SetEventObject( win
);
333 win
->GetEventHandler()->ProcessEvent( mevent
);
338 //---------------------------------------------------------------------------
340 //---------------------------------------------------------------------------
342 IMPLEMENT_CLASS(wxOleControl
,wxFrame
)
344 wxOleControl::wxOleControl( wxWindowID id
, long style
, const wxString
&name
)
346 Create( id
, style
, name
);
349 bool wxOleControl::Create( wxWindowID id
, long style
, const wxString
&name
)
351 wxTopLevelWindows
.Append( this );
353 m_needParent
= FALSE
;
355 PreCreation( (wxWindow
*) NULL
, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
357 m_title
= _T("wxWidgets OLE Server");
359 /* any widget that can contain another widget and resizes it
361 m_widget
= gtk_hbox_new(0,0);
363 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
365 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
366 GTK_SIGNAL_FUNC(gtk_olectx_delete_callback
), (gpointer
)this );
368 /* m_mainWidget holds the toolbar, the menubar and the client area */
369 m_mainWidget
= gtk_myfixed_new();
370 gtk_widget_show( m_mainWidget
);
371 GTK_WIDGET_UNSET_FLAGS( m_mainWidget
, GTK_CAN_FOCUS
);
372 gtk_container_add( GTK_CONTAINER(m_widget
), m_mainWidget
);
374 /* m_wxwindow only represents the client area without toolbar and menubar */
375 m_wxwindow
= gtk_myfixed_new();
376 gtk_widget_show( m_wxwindow
);
377 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
378 gtk_container_add( GTK_CONTAINER(m_mainWidget
), m_wxwindow
);
382 /* the user resized the frame by dragging etc. */
383 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
384 GTK_SIGNAL_FUNC(gtk_olectx_size_callback
), (gpointer
)this );
386 /* the only way to get the window size is to connect to this event */
387 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
388 GTK_SIGNAL_FUNC(gtk_olectx_configure_callback
), (gpointer
)this );
390 gtk_widget_show_all( m_widget
);
395 wxOleControl::~wxOleControl()
397 if (m_frameMenuBar
) delete m_frameMenuBar
;
398 m_frameMenuBar
= (wxMenuBar
*) NULL
;
400 if (m_frameStatusBar
) delete m_frameStatusBar
;
401 m_frameStatusBar
= (wxStatusBar
*) NULL
;
403 if (m_frameToolBar
) delete m_frameToolBar
;
404 m_frameToolBar
= (wxToolBar
*) NULL
;
406 wxTopLevelWindows
.DeleteObject( this );
408 if (wxTheApp
->GetTopWindow() == this)
409 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
411 if (wxTopLevelWindows
.Number() == 0)
412 wxTheApp
->ExitMainLoop();
416 void wxOleControl::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
421 void wxOleControl::DoSetClientSize(int width
, int height
)