merge the formfield patch from ooo-build
[ooovba.git] / sw / source / filter / html / htmlbas.cxx
blob1eab2c7fbc45b6dee9db4a84ca69c44f64e1a58f
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: htmlbas.cxx,v $
10 * $Revision: 1.16 $
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"
35 #include <hintids.hxx>
37 #include <sfx2/sfx.hrc>
39 #define _SVSTDARR_STRINGSSORTDTOR
40 #include <svtools/svstdarr.hxx>
41 #include <basic/sbx.hxx>
42 #include <basic/basmgr.hxx>
43 #include <basic/sbmod.hxx>
44 #include <sfx2/evntconf.hxx>
45 #include <sfx2/app.hxx>
46 #include <svtools/htmlout.hxx>
47 #include <svtools/htmltokn.h>
48 #include <svtools/htmlkywd.hxx>
50 #include <com/sun/star/document/XEventsSupplier.hpp>
51 #include <com/sun/star/uno/Reference.hxx>
53 #include <fmtornt.hxx>
54 #include <fmtfld.hxx>
56 #include "doc.hxx"
57 #include "docsh.hxx"
58 #include "docufld.hxx"
59 #include "wrthtml.hxx"
60 #include "swhtml.hxx"
63 using namespace ::com::sun::star;
64 using namespace ::com::sun::star::uno;
65 using namespace ::com::sun::star::container;
68 static HTMLOutEvent __FAR_DATA aBodyEventTable[] =
70 { OOO_STRING_SVTOOLS_HTML_O_SDonload, OOO_STRING_SVTOOLS_HTML_O_onload, SFX_EVENT_OPENDOC },
71 { OOO_STRING_SVTOOLS_HTML_O_SDonunload, OOO_STRING_SVTOOLS_HTML_O_onunload, SFX_EVENT_PREPARECLOSEDOC },
72 { OOO_STRING_SVTOOLS_HTML_O_SDonfocus, OOO_STRING_SVTOOLS_HTML_O_onfocus, SFX_EVENT_ACTIVATEDOC },
73 { OOO_STRING_SVTOOLS_HTML_O_SDonblur, OOO_STRING_SVTOOLS_HTML_O_onblur, SFX_EVENT_DEACTIVATEDOC },
74 { 0, 0, 0 }
78 void SwHTMLParser::NewScript()
80 ParseScriptOptions( aScriptType, sBaseURL, eScriptLang, aScriptURL,
81 aBasicLib, aBasicModule );
83 if( aScriptURL.Len() )
85 // Den Inhalt des Script-Tags ignorieren
86 bIgnoreRawData = TRUE;
90 void SwHTMLParser::EndScript()
92 BOOL bInsIntoBasic = FALSE,
93 bInsSrcIntoFld = FALSE;
95 switch( eScriptLang )
97 case HTML_SL_STARBASIC:
98 bInsIntoBasic = TRUE;
99 break;
100 default:
101 bInsSrcIntoFld = TRUE;
102 break;
105 bIgnoreRawData = FALSE;
106 aScriptSource.ConvertLineEnd();
108 // MIB 23.5.97: SGML-Kommentare brauchen nicht mehr entfernt zu werden,
109 // weil JS das jetzt selber kann.
110 // RemoveSGMLComment( aScriptSource, TRUE );
112 // Ausser StarBasic und unbenutzem JavaScript jedes Script oder den
113 // Modulnamen in einem Feld merken merken
114 if( bInsSrcIntoFld && !bIgnoreHTMLComments )
116 SwScriptFieldType *pType =
117 (SwScriptFieldType*)pDoc->GetSysFldType( RES_SCRIPTFLD );
119 SwScriptField aFld( pType, aScriptType,
120 aScriptURL.Len() ? aScriptURL : aScriptSource,
121 aScriptURL.Len()!=0 );
122 InsertAttr( SwFmtFld( aFld ) );
125 SwDocShell *pDocSh = pDoc->GetDocShell();
126 if( aScriptSource.Len() && pDocSh &&
127 bInsIntoBasic && IsNewDoc() )
129 // Fuer JavaScript und StarBasic noch ein Basic-Modul anlegen
130 // Das Basic entfernt natuerlich weiterhin keine SGML-Kommentare
131 RemoveSGMLComment( aScriptSource, TRUE );
133 // get library name
134 ::rtl::OUString aLibName;
135 if( aBasicLib.Len() )
136 aLibName = aBasicLib;
137 else
138 aLibName = ::rtl::OUString::createFromAscii( "Standard" );
140 // get module library container
141 Reference< script::XLibraryContainer > xModLibContainer( pDocSh->GetBasicContainer(), UNO_QUERY );
143 if ( xModLibContainer.is() )
145 Reference< container::XNameContainer > xModLib;
146 if ( xModLibContainer->hasByName( aLibName ) )
148 // get module library
149 Any aElement = xModLibContainer->getByName( aLibName );
150 aElement >>= xModLib;
152 else
154 // create module library
155 xModLib = xModLibContainer->createLibrary( aLibName );
158 if ( xModLib.is() )
160 if( !aBasicModule.Len() )
162 // create module name
163 BOOL bFound = TRUE;
164 while( bFound )
166 aBasicModule.AssignAscii( "Modul" );
167 aBasicModule += String::CreateFromInt32( (sal_Int32)(++nSBModuleCnt) );
168 bFound = xModLib->hasByName( ::rtl::OUString( aBasicModule ) );
172 // create module
173 ::rtl::OUString aModName( aBasicModule );
174 if ( !xModLib->hasByName( aModName ) )
176 Any aElement;
177 aElement <<= ::rtl::OUString( aScriptSource );
178 xModLib->insertByName( aModName , aElement );
183 // get dialog library container
184 Reference< script::XLibraryContainer > xDlgLibContainer( pDocSh->GetDialogContainer(), UNO_QUERY );
186 if ( xDlgLibContainer.is() )
188 if ( !xDlgLibContainer->hasByName( aLibName ) )
190 // create dialog library
191 xDlgLibContainer->createLibrary( aLibName );
196 aScriptSource.Erase();
197 aScriptType.Erase();
198 aScriptURL.Erase();
200 aBasicLib.Erase();
201 aBasicModule.Erase();
204 void SwHTMLParser::AddScriptSource()
206 // Hier merken wir und nur ein par Strings
207 if( aToken.Len() > 2 &&
208 (HTML_SL_STARBASIC==eScriptLang && aToken.GetChar( 0 ) == '\'') )
210 xub_StrLen nPos = STRING_NOTFOUND;
211 if( !aBasicLib.Len() )
213 nPos = aToken.SearchAscii( OOO_STRING_SVTOOLS_HTML_SB_library );
214 if( nPos != STRING_NOTFOUND )
216 aBasicLib =
217 aToken.Copy( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_library) - 1 );
218 aBasicLib.EraseLeadingChars().EraseTrailingChars();
222 if( !aBasicModule.Len() && nPos==STRING_NOTFOUND )
224 nPos = aToken.SearchAscii( OOO_STRING_SVTOOLS_HTML_SB_module );
225 if( nPos != STRING_NOTFOUND )
227 aBasicModule =
228 aToken.Copy( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_module) - 1 );
229 aBasicModule.EraseLeadingChars().EraseTrailingChars();
233 if( nPos==STRING_NOTFOUND )
235 if( aScriptSource.Len() )
236 aScriptSource += '\n';
237 (aScriptSource += aToken);
240 else if( aScriptSource.Len() || aToken.Len() )
242 // Leerzeilen am Anfang werden ignoriert
243 if( aScriptSource.Len() )
245 aScriptSource += '\n';
247 else
249 // Wir stehen hinter dem CR/LF der Zeile davor
250 nScriptStartLineNr = GetLineNr() - 1;
252 aScriptSource += aToken;
256 void SwHTMLParser::InsertBasicDocEvent( rtl::OUString aEvent, const String& rName,
257 ScriptType eScrType,
258 const String& rScrType )
260 ASSERT( rName.Len(), "InsertBasicDocEvent() ohne Macro gerufen" );
261 if( !rName.Len() )
262 return;
264 SwDocShell *pDocSh = pDoc->GetDocShell();
265 ASSERT( pDocSh, "Wo ist die DocShell?" );
266 if( !pDocSh )
267 return;
269 String sEvent( rName );
270 sEvent.ConvertLineEnd();
271 String sScriptType;
272 if( EXTENDED_STYPE == eScrType )
273 sScriptType = rScrType;
275 rtl::OUString aEventName;
277 SfxEventConfiguration* pECfg = SFX_APP()->GetEventConfig();
278 pECfg->ConfigureEvent( aEvent, SvxMacro( sEvent, sScriptType, eScrType ),
279 pDocSh );
282 void SwHTMLWriter::OutBasic()
284 if( !bCfgStarBasic )
285 return;
287 SFX_APP()->EnterBasicCall();
289 BasicManager *pBasicMan = pDoc->GetDocShell()->GetBasicManager();
290 ASSERT( pBasicMan, "Wo ist der Basic-Manager?" );
291 //JP 17.07.96: Bug 29538 - nur das DocumentBasic schreiben
292 if( !pBasicMan || pBasicMan == SFX_APP()->GetBasicManager() )
294 SFX_APP()->LeaveBasicCall();
295 return;
298 // und jetzt alle StarBasic-Module und alle unbenutzen JavaSrript-Module
299 // ausgeben
300 for( USHORT i=0; i<pBasicMan->GetLibCount(); i++ )
302 StarBASIC *pBasic = pBasicMan->GetLib( i );
303 const String& rLibName = pBasic->GetName();
305 SbxArray *pModules = pBasic->GetModules();
306 for( USHORT j=0; j<pModules->Count(); j++ )
308 const SbModule *pModule = PTR_CAST( SbModule, pModules->Get(j) );
309 ASSERT( pModule, "Wo ist das Modul?" );
311 String sLang(
312 String::CreateFromAscii( SVX_MACRO_LANGUAGE_STARBASIC ) );
313 ScriptType eType = STARBASIC;
315 if( 0==i && 0==j )
317 OutNewLine();
318 ByteString sOut( '<' );
319 sOut.Append( OOO_STRING_SVTOOLS_HTML_meta );
320 sOut.Append( ' ' );
321 sOut.Append( OOO_STRING_SVTOOLS_HTML_O_httpequiv );
322 sOut.Append( "=\"" );
323 sOut.Append( OOO_STRING_SVTOOLS_HTML_META_content_script_type );
324 sOut.Append( "\" " );
325 sOut.Append( OOO_STRING_SVTOOLS_HTML_O_content );
326 sOut.Append( "=\"text/x-" );
327 Strm() << sOut.GetBuffer();
328 // Entities aren't welcome here
329 ByteString sLang8( sLang, eDestEnc );
330 Strm() << sLang8.GetBuffer() << "\">";
333 const String& rModName = pModule->GetName();
334 Strm() << SwHTMLWriter::sNewLine; // nicht einruecken!
335 HTMLOutFuncs::OutScript( Strm(), GetBaseURL(), pModule->GetSource(),
336 sLang, eType, aEmptyStr,
337 &rLibName, &rModName,
338 eDestEnc, &aNonConvertableCharacters );
342 SFX_APP()->LeaveBasicCall();
345 static const char* aEventNames[] =
347 "OnLoad", "OnPrepareUnload", "OnFocus", "OnUnfocus"
350 void SwHTMLWriter::OutBasicBodyEvents()
352 SwDocShell *pDocSh = pDoc->GetDocShell();
353 if( !pDocSh )
354 return;
356 SvxMacroTableDtor *pDocTable = new SvxMacroTableDtor;
358 uno::Reference< document::XEventsSupplier > xSup( pDocSh->GetModel(), uno::UNO_QUERY );
359 uno::Reference < container::XNameReplace > xEvents = xSup->getEvents();
360 for ( sal_Int32 i=0; i<4; i++ )
362 SvxMacro* pMacro = SfxEventConfiguration::ConvertToMacro( xEvents->getByName( ::rtl::OUString::createFromAscii(aEventNames[i]) ), pDocSh, TRUE );
363 if ( pMacro )
364 pDocTable->Insert( aBodyEventTable[i].nEvent, pMacro );
367 if( pDocTable && pDocTable->Count() )
368 HTMLOutFuncs::Out_Events( Strm(), *pDocTable, aBodyEventTable,
369 bCfgStarBasic, eDestEnc, &aNonConvertableCharacters );