1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 
31 //#include <vos/mutex.hxx>
32 #include <vos/dynload.hxx>
33 
34 #include <vcl/wrkwin.hxx>
35 #include <vcl/svapp.hxx>
36 
37 #include <stardiv/uno/repos/implementationregistration.hxx>
38 #include <stardiv/uno/repos/serinfo.hxx>
39 #include <stardiv/uno/awt/vcllstnr.hxx>
40 #include <stardiv/uno/awt/device.hxx>
41 #include <stardiv/uno/awt/graphics.hxx>
42 #include <stardiv/uno/awt/vclwin.hxx>
43 
44 #include <usr/services.hxx>
45 
46 #include <svtools/unoiface.hxx>
47 
48 using namespace vos;
49 using namespace usr;
50 
51 //==================================================================================================
52 class Listener_Impl
53 	: public UsrObject
54 	, public XMouseListener
55 	, public XMouseMotionListener
56 	, public XKeyListener
57 	, public XWindowListener
58 	, public XFocusListener
59 	, public XPaintListener
60 {
61 public:
62 	SMART_UNO_DECLARATION( Listener_Impl, UsrObject );
63 
64 	virtual BOOL		queryInterface( Uik aUik, XInterfaceRef& rOut );
65 
66 	// XMouseListener
67 	virtual void		mousePressed( const VclMouseEvent& evt );
68 	virtual void		mouseReleased( const VclMouseEvent& evt );
69 	virtual void		mouseEntered( const VclMouseEvent& evt );
70 	virtual void		mouseExited( const VclMouseEvent& evt );
71 
72 	// XMouseMotionListener
73 	virtual void		mouseDragged( const VclMouseEvent& evt );
74 	virtual void		mouseMoved( const VclMouseEvent& evt );
75 
76 	// XKeyListener
77 	virtual void		keyPressed( const VclKeyEvent& evt );
78 	virtual void		keyReleased( const VclKeyEvent& evt );
79 
80 	// XFocusListener
81 	virtual void		focusGained( const FocusEvent& evt );
82 	virtual void		focusLost( const FocusEvent& evt );
83 
84 	// XWindowListener
85 	virtual void		windowResized( const WindowEvent& evt );
86 	virtual void		windowMoved( const WindowEvent& evt );
87 	virtual void		windowShown( const EventObject& evt );
88 	virtual void		windowHidden( const EventObject& evt );
89 
90 	// XPaintListener
91 	virtual void		windowPaint( const PaintEvent& evt );
92 
93 	// XEventListener
94 	virtual void		disposing( const EventObject& evt );
95 
96 public:
97 	void				addAllListeners( const XControlRef& xControl );
98 	void				removeAllListeners( const XControlRef& xControl );
99 };
100 
101 //--------------------------------------------------------------------------------------------------
102 void Listener_Impl::addAllListeners( const XControlRef& xControl )
103 {
104 	XWindowRef xWindow( xControl, USR_QUERY );
105 
106 	xWindow->addMouseListener( (XMouseListener*)this );
107 	xWindow->addMouseMotionListener( (XMouseMotionListener*)this );
108 	xWindow->addKeyListener( (XKeyListener*)this );
109 	xWindow->addFocusListener( (XFocusListener*)this );
110 	xWindow->addWindowListener( (XWindowListener*)this );
111 	xWindow->addPaintListener( (XPaintListener*)this );
112 	// cast due to ambiguities
113 	xControl->addEventListener( (XEventListener*)(XPaintListener*)this );
114 }
115 
116 //--------------------------------------------------------------------------------------------------
117 void Listener_Impl::removeAllListeners( const XControlRef& xControl )
118 {
119 	XWindowRef xWindow( xControl, USR_QUERY );
120 
121 	xWindow->removeMouseListener( (XMouseListener*)this );
122 	xWindow->removeMouseMotionListener( (XMouseMotionListener*)this );
123 	xWindow->removeKeyListener( (XKeyListener*)this );
124 	xWindow->removeFocusListener( (XFocusListener*)this );
125 	xWindow->removeWindowListener( (XWindowListener*)this );
126 	xWindow->removePaintListener( (XPaintListener*)this );
127 	// cast due to ambiguities
128 	xControl->removeEventListener( (XEventListener*)(XPaintListener*)this );
129 }
130 
131 //--------------------------------------------------------------------------------------------------
132 SMART_UNO_IMPLEMENTATION( Listener_Impl, UsrObject );
133 
134 //--------------------------------------------------------------------------------------------------
135 BOOL Listener_Impl::queryInterface( Uik aUik, XInterfaceRef& rOut )
136 {
137 	if (aUik == XMouseListener::getSmartUik())
138 		rOut = (XMouseListener*)this;
139 	else if (aUik == XMouseMotionListener::getSmartUik())
140 		rOut = (XMouseMotionListener*)this;
141 	else if (aUik == XWindowListener::getSmartUik())
142 		rOut = (XWindowListener*)this;
143 	else if (aUik == XFocusListener::getSmartUik())
144 		rOut = (XFocusListener*)this;
145 	else if (aUik == XKeyListener::getSmartUik())
146 		rOut = (XKeyListener*)this;
147 	else if (aUik == XPaintListener::getSmartUik())
148 		rOut = (XPaintListener*)this;
149 	else if (aUik == ((XEventListener*)NULL)->getSmartUik())
150 		rOut = (XEventListener*)(XMouseListener*)this;
151 	else
152 		return UsrObject::queryInterface( aUik, rOut );
153 
154 	return TRUE;
155 }
156 
157 //--------------------------------------------------------------------------------------------------
158 // XMouseListener
159 void Listener_Impl::mousePressed( const VclMouseEvent& evt )	{}
160 void Listener_Impl::mouseReleased( const VclMouseEvent& evt )	{}
161 void Listener_Impl::mouseEntered( const VclMouseEvent& evt )	{}
162 void Listener_Impl::mouseExited( const VclMouseEvent& evt )		{}
163 
164 // XMouseMotionListener
165 void Listener_Impl::mouseDragged( const VclMouseEvent& evt )	{}
166 void Listener_Impl::mouseMoved( const VclMouseEvent& evt )		{}
167 
168 // XKeyListener
169 void Listener_Impl::keyPressed( const VclKeyEvent& evt )		{}
170 void Listener_Impl::keyReleased( const VclKeyEvent& evt )		{}
171 
172 // XFocusListener
173 void Listener_Impl::focusGained( const FocusEvent& evt )		{}
174 void Listener_Impl::focusLost( const FocusEvent& evt )			{}
175 
176 // XWindowListener
177 void Listener_Impl::windowResized( const WindowEvent& evt )		{}
178 void Listener_Impl::windowMoved( const WindowEvent& evt )		{}
179 void Listener_Impl::windowShown( const EventObject& evt )		{}
180 void Listener_Impl::windowHidden( const EventObject& evt )		{}
181 
182 // XPaintListener
183 void Listener_Impl::windowPaint( const PaintEvent& evt )
184 {
185 	if (evt.Source.is())
186 	{
187 		XControlRef xControl( evt.Source, USR_QUERY );
188 		if (xControl.is())
189 		{
190 			XDeviceRef xDev( xControl->getPeer(), USR_QUERY );
191 			XGraphicsRef xGraphics = xDev->createGraphics();
192 			xGraphics->drawLine( 0, 0, 200, 200 );
193 			xGraphics->drawLine( 200, 0, 0, 200 );
194 		}
195 	}
196 }
197 
198 // XEventListener
199 void Listener_Impl::disposing( const EventObject& evt )		{}
200 
201 
202 //==================================================================================================
203 class FrameControlApplication
204 	: public Application
205 {
206 public:
207     virtual void        Main();
208     virtual void        ShowStatusText( const XubString& rText );
209 
210 public:
211 						FrameControlApplication()		{}
212 
213 private:
214 	void				init();
215 	void				deinit();
216 
217 private:
218 	Listener_Impl*		_pListener;
219 	XControlRef			_xControl;
220 
221 	WorkWindow*			_pWorkWin;
222 };
223 
224 FrameControlApplication g_App;
225 
226 #ifdef __MWERKS__
227 Application* pApp = &g_App;
228 #endif
229 
230 
231 //--------------------------------------------------------------------------------------------------
232 void FrameControlApplication::init()
233 {
234 	XMultiServiceFactoryRef xMgr = createRegistryServiceManager( L"test.rdb" );
235 	registerUsrServices( xMgr );
236 	setProcessServiceManager( xMgr );
237 	InitExtVclToolkit();
238 	Application::RegisterUnoServices();
239 
240 	XServiceRegistryRef xRegMgr(xMgr, USR_QUERY);
241 
242 	XImplementationRegistrationRef xIR( xMgr->createInstance(L"stardiv.uno.repos.ImplementationRegistration"), USR_QUERY );
243 	try
244 	{
245 		char szDllName[_MAX_PATH]="";
246 
247 		ORealDynamicLoader::computeModuleName("fc", szDllName, _MAX_PATH);
248 		UString aFCDllName = StringToOUString(szDllName, CHARSET_SYSTEM);
249 		xIR->registerImplementation(L"stardiv.loader.SharedLibrary", aFCDllName, XSimpleRegistryRef() );
250 	}
251 	catch( CannotRegisterImplementationException& e )
252 	{
253 	}
254 
255 
256 
257 	// ...
258 
259 	XInterfaceRef xInst = xMgr->createInstance( L"stardiv.one.frame.FrameControl" );
260 	if (xInst->queryInterface( XControl::getSmartUik(), _xControl ))
261 	{
262 		_pWorkWin = new WorkWindow( NULL, WB_APP | WB_STDWORK );
263 		_pWorkWin->Show();
264 		XWindowPeerRef xParent( _pWorkWin->GetComponentInterface() );
265 
266 		XToolkitRef xToolkit( xMgr->createInstance( L"stardiv.vcl.VclToolkit" ), USR_QUERY );
267 		//xToolkit = XToolkitRef( xMgr->createInstance( L"stardiv.uno.awt.Toolkit" ), USR_QUERY );
268 		_xControl->createPeer( xToolkit, xParent );
269 		XWindowRef xWin( _xControl, USR_QUERY );
270 		xWin->setPosSize( 50, 50, 400, 400, PosSize_POSSIZE );
271 		xWin->setVisible( TRUE );
272 
273 		_pListener = new Listener_Impl();
274 		_pListener->acquire();
275 		_pListener->addAllListeners( _xControl );
276 		// ... on paint a cross should be drawn
277 	}
278 }
279 
280 //--------------------------------------------------------------------------------------------------
281 void FrameControlApplication::deinit()
282 {
283 	if (_pListener)
284 	{
285 
286 		_pListener->removeAllListeners( _xControl );
287 		_xControl->dispose();	// disposing event should occur
288 		_pListener->release();
289 		_pListener = NULL;
290 
291 		_xControl = XControlRef();
292 	}
293 
294 	_pWorkWin->Hide();
295 	delete _pWorkWin;
296 }
297 
298 
299 //--------------------------------------------------------------------------------------------------
300 void FrameControlApplication::Main()
301 {
302 //  	void TestErrcodes();
303 //  	TestErrcodes();
304 
305 	EnterMultiThread();
306 	SetAppName( "RadioActiveControl-Demo" );
307 	EnableSVLook();
308 
309 	init();
310 
311 	Execute();
312 
313 	deinit();
314 }
315 
316 //--------------------------------------------------------------------------------------------------
317 void FrameControlApplication::ShowStatusText( const XubString& rStatus )
318 {
319 	Application::GetAppWindow()->SetText( rStatus );
320 }
321