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