xref: /trunk/main/toolkit/workben/unodialog.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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_toolkit.hxx"
30 #include <tools/svwin.h>
31 #include <sal/main.h>
32 #include <com/sun/star/awt/XToolkit.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/container/XNameContainer.hpp>
36 #include <com/sun/star/awt/XControlModel.hpp>
37 #include <com/sun/star/awt/XControlContainer.hpp>
38 #include <com/sun/star/awt/XControl.hpp>
39 #include <com/sun/star/awt/XDialog.hpp>
40 
41 
42 #include <tools/debug.hxx>
43 #include <vcl/svapp.hxx>
44 #include <vcl/wrkwin.hxx>
45 
46 #include <svtools/unoiface.hxx> // InitExtToolkit
47 #include <comphelper/processfactory.hxx>
48 
49 #include <cppuhelper/servicefactory.hxx>
50 #include <cppuhelper/bootstrap.hxx>
51 #include <comphelper/regpathhelper.hxx>
52 
53 #include <com/sun/star/lang/XInitialization.hpp>
54 #include <com/sun/star/registry/XSimpleRegistry.hpp>
55 
56 
57 using namespace ::com::sun::star;
58 using namespace ::com::sun::star::connection;
59 using namespace ::vos;
60 using namespace ::rtl;
61 using namespace ::com::sun::star::uno;
62 using namespace ::com::sun::star::registry;
63 using namespace ::com::sun::star::lang;
64 
65 
66 
67 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > createApplicationServiceManager()
68 {
69     try
70     {
71         ::rtl::OUString localRegistry = ::comphelper::getPathToUserRegistry();
72         ::rtl::OUString systemRegistry = ::comphelper::getPathToSystemRegistry();
73 
74         Reference< XSimpleRegistry > xLocalRegistry( ::cppu::createSimpleRegistry() );
75         Reference< XSimpleRegistry > xSystemRegistry( ::cppu::createSimpleRegistry() );
76         if ( xLocalRegistry.is() && (localRegistry.getLength() > 0) )
77         {
78             try
79             {
80                 xLocalRegistry->open( localRegistry, sal_False, sal_True);
81             }
82             catch ( InvalidRegistryException& )
83             {
84             }
85 
86             if ( !xLocalRegistry->isValid() )
87                 xLocalRegistry->open(localRegistry, sal_True, sal_True);
88         }
89 
90         if ( xSystemRegistry.is() && (systemRegistry.getLength() > 0) )
91             xSystemRegistry->open( systemRegistry, sal_True, sal_False);
92 
93         if ( (xLocalRegistry.is() && xLocalRegistry->isValid()) &&
94              (xSystemRegistry.is() && xSystemRegistry->isValid()) )
95         {
96             Reference < XSimpleRegistry > xReg( ::cppu::createNestedRegistry() );
97             Sequence< Any > seqAnys(2);
98             seqAnys[0] <<= xLocalRegistry ;
99             seqAnys[1] <<= xSystemRegistry ;
100             Reference< XInitialization > xInit( xReg, UNO_QUERY );
101             xInit->initialize( seqAnys );
102 
103             Reference< XComponentContext > xContext( ::cppu::bootstrap_InitialComponentContext( xReg ) );
104             return Reference< XMultiServiceFactory >( xContext->getServiceManager(), UNO_QUERY );
105         }
106     }
107     catch( ::com::sun::star::uno::Exception& )
108     {
109     }
110 
111     return ::cppu::createServiceFactory();
112 }
113 
114 
115 // -----------------------------------------------------------------------
116 void Main( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > & );
117 
118 SAL_IMPLEMENT_MAIN()
119 {
120     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >  xMSF = createApplicationServiceManager();
121     //SVInit( xMSF );
122     ::Main( xMSF );
123     //SVDeinit();
124     return NULL;
125 }
126 
127 /*
128 class MyApp : public Application
129 {
130 public:
131     void        Main()
132     {
133         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >  xMSF = createApplicationServiceManager();
134         ::Main( xMSF );
135     }
136 };
137 
138 MyApp aMyApp;
139 */
140 
141 
142 class MyWin : public WorkWindow
143 {
144 private:
145     uno::Reference< awt::XView > mxView;
146 
147 public:
148     MyWin() : WorkWindow( NULL, WB_APP|WB_STDWORK ) {;}
149     void        Paint( const Rectangle& r );
150     void        SetXView(  uno::Reference< awt::XView > xV  ) { mxView = xV; }
151 };
152 
153 
154 // -----------------------------------------------------------------------
155 
156 void Main( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > & xMSF )
157 {
158     ::comphelper::setProcessServiceFactory( xMSF );
159 
160     //uno::Reference< awt::XToolkit> xToolkit( xMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.ExtToolkit" ) ) ), uno::UNO_QUERY );
161     uno::Reference< awt::XToolkit> xToolkit( xMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ) ), uno::UNO_QUERY );
162 
163     // Create a DialogModel
164     uno::Reference< container::XNameContainer > xC( xMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ) ), uno::UNO_QUERY );
165     uno::Reference< lang::XMultiServiceFactory >  xModFact( xC, uno::UNO_QUERY );
166 
167     // Create a ButtonModel
168     uno::Reference< awt::XControlModel > xCtrl1( xModFact->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlButtonModel" ) ) ), uno::UNO_QUERY );
169     uno::Reference< beans::XPropertySet > xPSet( xCtrl1, uno::UNO_QUERY );
170     uno::Any aValue;
171     aValue <<= (sal_Int32) 10;
172     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "PositionX" ) ), aValue );
173     aValue <<= (sal_Int32) 10;
174     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "PositionY" ) ), aValue );
175     aValue <<= (sal_Int32) 40;
176     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Width" ) ), aValue );
177     aValue <<= (sal_Int32) 12;
178     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Height" ) ), aValue );
179     aValue <<= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Test!" ) );
180     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Label" ) ), aValue );
181     uno::Any aAny;
182     aAny <<= xCtrl1;
183     xC->insertByName( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Control1" ) ), aAny );
184 
185     uno::Reference< beans::XPropertySet > xDlgPSet( xC, uno::UNO_QUERY );
186     aValue <<= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Test-Dialog" ) );
187     xDlgPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Title" ) ), aValue );
188     aValue <<= (sal_Int32) 200;
189     xDlgPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Width" ) ), aValue );
190     aValue <<= (sal_Int32) 200;
191     xDlgPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Height" ) ), aValue );
192 
193     // Create a Dialog
194     uno::Reference< awt::XControl > xDlg( xMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialog" ) ) ), uno::UNO_QUERY );
195     uno::Reference< awt::XControlModel > xDlgMod( xC, uno::UNO_QUERY );
196     xDlg->setModel( xDlgMod );
197 
198     // Create a EditModel
199     uno::Reference< awt::XControlModel > xCtrl2( xModFact->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlEditModel" ) ) ), uno::UNO_QUERY );
200     xPSet = uno::Reference< beans::XPropertySet >( xCtrl2, uno::UNO_QUERY );
201     aValue <<= (sal_Int32) 10;
202     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "PositionX" ) ), aValue );
203     aValue <<= (sal_Int32) 40;
204     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "PositionY" ) ), aValue );
205     aValue <<= (sal_Int32) 80;
206     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Width" ) ), aValue );
207     aValue <<= (sal_Int32) 12;
208     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Height" ) ), aValue );
209     aValue <<= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Text..." ) );
210     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Text" ) ), aValue );
211     aAny <<= xCtrl2;
212     xC->insertByName( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Control2" ) ), aAny );
213 
214     // test if listener works...
215     aValue <<= (sal_Int32) 20;
216     xPSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "PositionX" ) ), aValue );
217 
218     MyWin * pWindow;
219     ::osl::Guard< vos::IMutex > aVclGuard( Application::GetSolarMutex() );
220     pWindow = new MyWin();
221     pWindow->Show();
222 
223     xDlg->setDesignMode( sal_True );
224 
225     uno::Reference< awt::XWindow > xWindow( xDlg, uno::UNO_QUERY );
226     xWindow->setVisible( sal_False );
227 
228     xDlg->createPeer( xToolkit, pWindow->GetComponentInterface() );
229 
230     uno::Reference< awt::XView > xView( xDlg, uno::UNO_QUERY );
231     pWindow->SetXView( xView );
232 
233     uno::Reference< awt::XDialog > xD( xDlg, uno::UNO_QUERY );
234 
235     //static BOOL bExecute = FALSE;
236     //if ( bExecute )
237         xD->execute();
238     //Execute();
239     Reference< XComponent > xDT( xD, uno::UNO_QUERY );
240     xDT->dispose();
241     delete pWindow;
242 
243     Reference< XComponent > xT( xToolkit, uno::UNO_QUERY );
244     xT->dispose();
245 
246     Reference< beans::XPropertySet > xProps( ::comphelper::getProcessServiceFactory(), UNO_QUERY );
247     if (xProps.is())
248     {
249         try
250         {
251             Reference< lang::XComponent > xComp;
252             if (xProps->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) ) >>= xComp)
253             {
254                 xComp->dispose();
255             }
256         }
257         catch (beans::UnknownPropertyException &)
258         {
259         }
260     }
261 }
262 
263 void MyWin::Paint( const Rectangle& r )
264 {
265     static BOOL bDraw = TRUE;
266     if ( bDraw && mxView.is() )
267         mxView->draw( 50, 50 );
268 }
269 
270