merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / doc / docxforms.cxx
blobcc2807ce4337609e800ed17a5aaeb3b95fc60ddf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: docxforms.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
37 #include <doc.hxx>
38 #include <docsh.hxx>
39 #include <com/sun/star/uno/Reference.hxx>
40 #include <com/sun/star/container/XNameContainer.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/frame/XModule.hpp>
43 #include <com/sun/star/xforms/XModel.hpp>
44 #include <com/sun/star/xforms/XFormsUIHelper1.hpp>
45 #include <unotools/processfactory.hxx>
46 #include <tools/diagnose_ex.h>
49 using namespace ::com::sun::star;
51 using uno::Reference;
52 using uno::XInterface;
53 using uno::UNO_QUERY;
54 using uno::makeAny;
55 using uno::Exception;
56 using container::XNameContainer;
57 using xforms::XModel;
58 using frame::XModule;
59 using xforms::XFormsUIHelper1;
60 using rtl::OUString;
63 Reference<XNameContainer> SwDoc::getXForms() const
65 return xXForms;
68 bool SwDoc::isXForms() const
70 return xXForms.is();
73 Reference<XInterface> lcl_createInstance( const sal_Char* pServiceName )
75 DBG_ASSERT( pServiceName != NULL, "no service name" );
76 return utl::getProcessServiceFactory()->createInstance(
77 OUString::createFromAscii( pServiceName ) );
80 void SwDoc::initXForms( bool bCreateDefaultModel )
82 DBG_ASSERT( ! isXForms(), "please initialize only once" );
84 try
86 // create XForms components
87 xXForms.set( lcl_createInstance( "com.sun.star.xforms.XForms" ),
88 UNO_QUERY );
89 DBG_ASSERT( xXForms.is(), "can't create XForms container" );
91 // change our module identifier, to be able to have a dedicated UI
92 Reference< XModule > xModule;
93 SwDocShell* pShell( GetDocShell() );
94 if ( pShell )
95 xModule = xModule.query( pShell->GetModel() );
96 DBG_ASSERT( xModule.is(), "SwDoc::initXForms: no XModule at the document!" );
97 if ( xModule.is() )
98 xModule->setIdentifier( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xforms.XMLFormDocument" ) ) );
100 // create default model
101 if( bCreateDefaultModel && xXForms.is() )
103 OUString sName(RTL_CONSTASCII_USTRINGPARAM("Model 1"));
104 Reference<XModel> xModel(
105 lcl_createInstance( "com.sun.star.xforms.Model" ),
106 UNO_QUERY );
107 DBG_ASSERT( xModel.is(), "no model?" );
108 if( xModel.is() )
110 xModel->setID( sName );
111 Reference<XFormsUIHelper1>( xModel, UNO_QUERY )->newInstance(
112 OUString(RTL_CONSTASCII_USTRINGPARAM("Instance 1")),
113 OUString(), sal_True );
114 xModel->initialize();
115 xXForms->insertByName( sName, makeAny( xModel ) );
117 DBG_ASSERT( xXForms->hasElements(), "can't create XForms model" );
120 DBG_ASSERT( isXForms(), "initialization failed" );
122 catch( const Exception& )
124 DBG_UNHANDLED_EXCEPTION();