xref: /aoo4110/main/toolkit/workben/unodialog.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_toolkit.hxx"
26 #include <tools/svwin.h>
27 #include <sal/main.h>
28 #include <com/sun/star/awt/XToolkit.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/container/XNameContainer.hpp>
32 #include <com/sun/star/awt/XControlModel.hpp>
33 #include <com/sun/star/awt/XControlContainer.hpp>
34 #include <com/sun/star/awt/XControl.hpp>
35 #include <com/sun/star/awt/XDialog.hpp>
36 
37 
38 #include <tools/debug.hxx>
39 #include <vcl/svapp.hxx>
40 #include <vcl/wrkwin.hxx>
41 
42 #include <svtools/unoiface.hxx>	// InitExtToolkit
43 #include <comphelper/processfactory.hxx>
44 
45 #include <cppuhelper/servicefactory.hxx>
46 #include <cppuhelper/bootstrap.hxx>
47 #include <comphelper/regpathhelper.hxx>
48 
49 #include <com/sun/star/lang/XInitialization.hpp>
50 #include <com/sun/star/registry/XSimpleRegistry.hpp>
51 
52 
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::connection;
55 using namespace ::vos;
56 using namespace ::rtl;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::registry;
59 using namespace ::com::sun::star::lang;
60 
61 
62 
createApplicationServiceManager()63 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > createApplicationServiceManager()
64 {
65 	try
66 	{
67         ::rtl::OUString localRegistry = ::comphelper::getPathToUserRegistry();
68         ::rtl::OUString systemRegistry = ::comphelper::getPathToSystemRegistry();
69 
70         Reference< XSimpleRegistry > xLocalRegistry( ::cppu::createSimpleRegistry() );
71         Reference< XSimpleRegistry > xSystemRegistry( ::cppu::createSimpleRegistry() );
72         if ( xLocalRegistry.is() && (localRegistry.getLength() > 0) )
73         {
74             try
75             {
76                 xLocalRegistry->open( localRegistry, sal_False, sal_True);
77             }
78             catch ( InvalidRegistryException& )
79             {
80             }
81 
82             if ( !xLocalRegistry->isValid() )
83                 xLocalRegistry->open(localRegistry, sal_True, sal_True);
84         }
85 
86         if ( xSystemRegistry.is() && (systemRegistry.getLength() > 0) )
87             xSystemRegistry->open( systemRegistry, sal_True, sal_False);
88 
89         if ( (xLocalRegistry.is() && xLocalRegistry->isValid()) &&
90              (xSystemRegistry.is() && xSystemRegistry->isValid()) )
91         {
92             Reference < XSimpleRegistry > xReg( ::cppu::createNestedRegistry() );
93             Sequence< Any > seqAnys(2);
94             seqAnys[0] <<= xLocalRegistry ;
95             seqAnys[1] <<= xSystemRegistry ;
96             Reference< XInitialization > xInit( xReg, UNO_QUERY );
97             xInit->initialize( seqAnys );
98 
99             Reference< XComponentContext > xContext( ::cppu::bootstrap_InitialComponentContext( xReg ) );
100             return Reference< XMultiServiceFactory >( xContext->getServiceManager(), UNO_QUERY );
101         }
102 	}
103 	catch( ::com::sun::star::uno::Exception& )
104 	{
105 	}
106 
107 	return ::cppu::createServiceFactory();
108 }
109 
110 
111 // -----------------------------------------------------------------------
112 void Main( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > & );
113 
SAL_IMPLEMENT_MAIN()114 SAL_IMPLEMENT_MAIN()
115 {
116     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >  xMSF = createApplicationServiceManager();
117 	//SVInit( xMSF );
118 	::Main( xMSF );
119 	//SVDeinit();
120 	return NULL;
121 }
122 
123 /*
124 class MyApp : public Application
125 {
126 public:
127     void        Main()
128 	{
129 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >  xMSF = createApplicationServiceManager();
130 		::Main( xMSF );
131 	}
132 };
133 
134 MyApp aMyApp;
135 */
136 
137 
138 class MyWin : public WorkWindow
139 {
140 private:
141 	uno::Reference< awt::XView > mxView;
142 
143 public:
MyWin()144 	MyWin() : WorkWindow( NULL, WB_APP|WB_STDWORK ) {;}
145     void        Paint( const Rectangle& r );
SetXView(uno::Reference<awt::XView> xV)146 	void		SetXView(  uno::Reference< awt::XView > xV  ) { mxView = xV; }
147 };
148 
149 
150 // -----------------------------------------------------------------------
151 
Main(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & xMSF)152 void Main( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > & xMSF )
153 {
154 	::comphelper::setProcessServiceFactory( xMSF );
155 
156 	//uno::Reference< awt::XToolkit> xToolkit( xMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.ExtToolkit" ) ) ), uno::UNO_QUERY );
157 	uno::Reference< awt::XToolkit> xToolkit( xMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ) ), uno::UNO_QUERY );
158 
159 	// Create a DialogModel
160 	uno::Reference< container::XNameContainer > xC( xMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ) ), uno::UNO_QUERY );
161     uno::Reference< lang::XMultiServiceFactory >  xModFact( xC, uno::UNO_QUERY );
162 
163 	// Create a ButtonModel
164 	uno::Reference< awt::XControlModel > xCtrl1( xModFact->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlButtonModel" ) ) ), uno::UNO_QUERY );
165 	uno::Reference< beans::XPropertySet > xPSet( xCtrl1, uno::UNO_QUERY );
166 	uno::Any aValue;
167 	aValue <<= (sal_Int32) 10;
168 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "PositionX" ) ), aValue );
169 	aValue <<= (sal_Int32) 10;
170 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "PositionY" ) ), aValue );
171 	aValue <<= (sal_Int32) 40;
172 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Width" ) ), aValue );
173 	aValue <<= (sal_Int32) 12;
174 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Height" ) ), aValue );
175 	aValue <<= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Test!" ) );
176 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Label" ) ), aValue );
177 	uno::Any aAny;
178 	aAny <<= xCtrl1;
179 	xC->insertByName( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Control1" ) ), aAny );
180 
181 	uno::Reference< beans::XPropertySet > xDlgPSet( xC, uno::UNO_QUERY );
182 	aValue <<= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Test-Dialog" ) );
183 	xDlgPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Title" ) ), aValue );
184 	aValue <<= (sal_Int32) 200;
185 	xDlgPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Width" ) ), aValue );
186 	aValue <<= (sal_Int32) 200;
187 	xDlgPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Height" ) ), aValue );
188 
189 	// Create a Dialog
190 	uno::Reference< awt::XControl > xDlg( xMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialog" ) ) ), uno::UNO_QUERY );
191 	uno::Reference< awt::XControlModel > xDlgMod( xC, uno::UNO_QUERY );
192 	xDlg->setModel( xDlgMod );
193 
194  	// Create a EditModel
195 	uno::Reference< awt::XControlModel > xCtrl2( xModFact->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlEditModel" ) ) ), uno::UNO_QUERY );
196 	xPSet = uno::Reference< beans::XPropertySet >( xCtrl2, uno::UNO_QUERY );
197 	aValue <<= (sal_Int32) 10;
198 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "PositionX" ) ), aValue );
199 	aValue <<= (sal_Int32) 40;
200 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "PositionY" ) ), aValue );
201 	aValue <<= (sal_Int32) 80;
202 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Width" ) ), aValue );
203 	aValue <<= (sal_Int32) 12;
204 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Height" ) ), aValue );
205 	aValue <<= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Text..." ) );
206 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Text" ) ), aValue );
207 	aAny <<= xCtrl2;
208 	xC->insertByName( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Control2" ) ), aAny );
209 
210 	// test if listener works...
211 	aValue <<= (sal_Int32) 20;
212 	xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "PositionX" ) ), aValue );
213 
214 	MyWin * pWindow;
215 	::osl::Guard< vos::IMutex > aVclGuard( Application::GetSolarMutex() );
216 	pWindow = new MyWin();
217 	pWindow->Show();
218 
219 	xDlg->setDesignMode( sal_True );
220 
221 	uno::Reference< awt::XWindow > xWindow( xDlg, uno::UNO_QUERY );
222 	xWindow->setVisible( sal_False );
223 
224 	xDlg->createPeer( xToolkit, pWindow->GetComponentInterface() );
225 
226 	uno::Reference< awt::XView > xView( xDlg, uno::UNO_QUERY );
227 	pWindow->SetXView( xView );
228 
229 	uno::Reference< awt::XDialog > xD( xDlg, uno::UNO_QUERY );
230 
231 	//static BOOL bExecute = FALSE;
232 	//if ( bExecute )
233 		xD->execute();
234 	//Execute();
235 	Reference< XComponent > xDT( xD, uno::UNO_QUERY );
236 	xDT->dispose();
237 	delete pWindow;
238 
239 	Reference< XComponent > xT( xToolkit, uno::UNO_QUERY );
240 	xT->dispose();
241 
242     Reference< beans::XPropertySet > xProps( ::comphelper::getProcessServiceFactory(), UNO_QUERY );
243     if (xProps.is())
244     {
245         try
246         {
247             Reference< lang::XComponent > xComp;
248             if (xProps->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) ) >>= xComp)
249             {
250                 xComp->dispose();
251             }
252         }
253         catch (beans::UnknownPropertyException &)
254         {
255         }
256     }
257 }
258 
Paint(const Rectangle & r)259 void MyWin::Paint( const Rectangle& r )
260 {
261 	static BOOL bDraw = TRUE;
262 	if ( bDraw && mxView.is() )
263 		mxView->draw( 50, 50 );
264 }
265 
266