xref: /trunk/main/sw/source/ui/uiview/srcview.cxx (revision c2eaa082)
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_sw.hxx"
26 
27 #include <hintids.hxx>
28 #include <com/sun/star/util/SearchOptions.hpp>
29 #include <com/sun/star/util/SearchFlags.hpp>
30 #include <com/sun/star/i18n/TransliterationModules.hpp>
31 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
32 #include <unotools/tempfile.hxx>
33 #include <tools/urlobj.hxx>
34 #include <vcl/print.hxx>
35 #include <vcl/msgbox.hxx>
36 #include <vcl/wrkwin.hxx>
37 #include <vcl/metric.hxx>
38 #include <svtools/ctrltool.hxx>
39 #include <svl/intitem.hxx>
40 #include <svl/stritem.hxx>
41 #include <unotools/pathoptions.hxx>
42 #include <svl/undo.hxx>
43 #include <svtools/txtcmp.hxx>
44 #include <svl/eitem.hxx>
45 #include <svl/whiter.hxx>
46 #include <unotools/saveopt.hxx>
47 #include <svtools/transfer.hxx>
48 #include <svtools/svtools.hrc>
49 #include <svtools/svtdata.hxx>
50 #include <svx/svxids.hrc>
51 #include <svtools/htmlcfg.hxx>
52 #include <sfx2/app.hxx>
53 #include <sfx2/objface.hxx>
54 #include <sfx2/viewfrm.hxx>
55 #include <sfx2/bindings.hxx>
56 #include <sfx2/docfilt.hxx>
57 #include <sfx2/fcontnr.hxx>
58 #include <sfx2/request.hxx>
59 #include <sfx2/prnmon.hxx>
60 #include <sfx2/docfile.hxx>
61 #include <editeng/fhgtitem.hxx>
62 #include <svx/srchdlg.hxx>
63 #include <svl/srchitem.hxx>
64 #include <editeng/fontitem.hxx>
65 #include <editeng/flstitem.hxx>
66 #include "editeng/unolingu.hxx"
67 #include <sfx2/sfxhtml.hxx>
68 #include <swtypes.hxx>
69 #include <swmodule.hxx>
70 #include <docsh.hxx>
71 #include <wdocsh.hxx>
72 #include <srcview.hxx>
73 #include <viewfunc.hxx>
74 #include <doc.hxx>
75 #include <sfx2/msg.hxx>
76 #include <shellio.hxx>
77 
78 #include <cmdid.h>          // FN_       ...
79 #include <helpid.h>
80 #include <globals.hrc>
81 #include <shells.hrc>
82 #include <popup.hrc>
83 #include <web.hrc>
84 #include <view.hrc>
85 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
86 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
87 #include <sfx2/filedlghelper.hxx>
88 #define SwSrcView
89 #include "swslots.hxx"
90 
91 #include <unomid.h>
92 
93 #include <com/sun/star/document/XDocumentProperties.hpp>
94 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
95 
96 using namespace ::com::sun::star;
97 using namespace ::com::sun::star::i18n;
98 using namespace ::com::sun::star::lang;
99 using namespace ::com::sun::star::uno;
100 using namespace ::com::sun::star::ui::dialogs;
101 using namespace ::com::sun::star::i18n;
102 using namespace ::com::sun::star::lang;
103 using namespace ::sfx2;
104 using ::rtl::OUString;
105 using ::com::sun::star::util::SearchOptions;
106 
107 
108 #define SWSRCVIEWFLAGS ( SFX_VIEW_CAN_PRINT|\
109 					  SFX_VIEW_NO_NEWWINDOW )
110 
111 #define SRC_SEARCHOPTIONS (0xFFFF & ~(SEARCH_OPTIONS_FORMAT|SEARCH_OPTIONS_FAMILIES|SEARCH_OPTIONS_SEARCH_ALL))
112 
113 // Druckraender -> wie Basic - Ide
114 #define LMARGPRN		1700
115 #define RMARGPRN		 900
116 #define TMARGPRN    	2000
117 #define BMARGPRN    	1000
118 #define BORDERPRN		300
119 
120 SFX_IMPL_NAMED_VIEWFACTORY(SwSrcView, "SourceView")
121 {
122 	SFX_VIEW_REGISTRATION(SwWebDocShell);
123 }
124 
125 SFX_IMPL_INTERFACE( SwSrcView, SfxViewShell, SW_RES(0) )
126 {
127 	SFX_POPUPMENU_REGISTRATION(SW_RES(MN_SRCVIEW_POPUPMENU));
128 	SFX_OBJECTBAR_REGISTRATION(	SFX_OBJECTBAR_TOOLS|
129 								SFX_VISIBILITY_STANDARD|SFX_VISIBILITY_SERVER,
130 								SW_RES(RID_WEBTOOLS_TOOLBOX) );
131 	SFX_CHILDWINDOW_REGISTRATION(SvxSearchDialogWrapper::GetChildWindowId());
132 }
133 
TYPEINIT1(SwSrcView,SfxViewShell)134 TYPEINIT1(SwSrcView, SfxViewShell)
135 
136 /*-----------------18.11.96 08.05-------------------
137 
138 --------------------------------------------------*/
139 
140 
141 void lcl_PrintHeader( OutputDevice &rOutDev, sal_uInt16 nPages, sal_uInt16 nCurPage, const String& rTitle )
142 {
143 	short nLeftMargin 	= LMARGPRN;
144     Size aSz = rOutDev.GetOutputSize();
145 	short nBorder = BORDERPRN;
146 
147     Color aOldFillColor( rOutDev.GetFillColor() );
148     Font aOldFont( rOutDev.GetFont() );
149 
150     rOutDev.SetFillColor( Color(COL_TRANSPARENT) );
151 
152 	Font aFont( aOldFont );
153 	aFont.SetWeight( WEIGHT_BOLD );
154 	aFont.SetAlign( ALIGN_BOTTOM );
155     rOutDev.SetFont( aFont );
156 
157     long nFontHeight = rOutDev.GetTextHeight();
158 
159 	// 1.Border => Strich, 2+3 Border = Freiraum.
160 	long nYTop = TMARGPRN-3*nBorder-nFontHeight;
161 
162 	long nXLeft = nLeftMargin-nBorder;
163 	long nXRight = aSz.Width()-RMARGPRN+nBorder;
164 
165     rOutDev.DrawRect( Rectangle(
166 		Point( nXLeft, nYTop ),
167         Size( nXRight-nXLeft, aSz.Height() - nYTop - BMARGPRN + nBorder ) ) );
168 
169 
170 	long nY = TMARGPRN-2*nBorder;
171 	Point aPos( nLeftMargin, nY );
172     rOutDev.DrawText( aPos, rTitle );
173 	if ( nPages != 1 )
174 	{
175 		aFont.SetWeight( WEIGHT_NORMAL );
176         rOutDev.SetFont( aFont );
177 		String aPageStr( C2S(" [") );
178 		aPageStr += String( SW_RES( STR_PAGE ) );
179 		aPageStr += ' ';
180 		aPageStr += String::CreateFromInt32( nCurPage );
181 		aPageStr += ']';
182         aPos.X() += rOutDev.GetTextWidth( rTitle );
183         rOutDev.DrawText( aPos, aPageStr );
184 	}
185 
186 
187 	nY = TMARGPRN-nBorder;
188 
189     rOutDev.DrawLine( Point( nXLeft, nY ), Point( nXRight, nY ) );
190 
191     rOutDev.SetFont( aOldFont );
192     rOutDev.SetFillColor( aOldFillColor );
193 }
194 /* -----------------13.11.2003 16:24-----------------
195 
196  --------------------------------------------------*/
lcl_GetStreamCharSet(rtl_TextEncoding eLoadEncoding)197 rtl_TextEncoding lcl_GetStreamCharSet(rtl_TextEncoding eLoadEncoding)
198 {
199     rtl_TextEncoding eRet = eLoadEncoding;
200     if(RTL_TEXTENCODING_DONTKNOW == eRet)
201     {
202         SvxHtmlOptions* pHtmlOptions = SvxHtmlOptions::Get();
203         const sal_Char *pCharSet =
204             rtl_getBestMimeCharsetFromTextEncoding( pHtmlOptions->GetTextEncoding() );
205         eRet = rtl_getTextEncodingFromMimeCharset( pCharSet );
206     }
207     return eRet;
208 }
209 /*-----------------18.11.96 08.21-------------------
210 
211 --------------------------------------------------*/
212 
lcl_ConvertTabsToSpaces(String & rLine)213 void lcl_ConvertTabsToSpaces( String& rLine )
214 {
215 	if ( rLine.Len() )
216 	{
217 		sal_uInt16 nPos = 0;
218 		sal_uInt16 nMax = rLine.Len();
219 		while ( nPos < nMax )
220 		{
221 			if ( rLine.GetChar(nPos) == '\t' )
222 			{
223 				// Nicht 4 Blanks, sondern an 4er TabPos:
224 				String aBlanker;
225 				aBlanker.Fill( ( 4 - ( nPos % 4 ) ), ' ' );
226 				rLine.Erase( nPos, 1 );
227 				rLine.Insert( aBlanker, nPos );
228 				nMax = rLine.Len();
229 			}
230 			nPos++;	// Nicht optimal, falls Tab, aber auch nicht verkehrt...
231 		}
232 	}
233 }
234 
235 /*--------------------------------------------------------------------
236 	Beschreibung:
237  --------------------------------------------------------------------*/
238 
239 
SwSrcView(SfxViewFrame * pViewFrame,SfxViewShell *)240 SwSrcView::SwSrcView(SfxViewFrame* pViewFrame, SfxViewShell*) :
241 	SfxViewShell( pViewFrame, SWSRCVIEWFLAGS ),
242 	aEditWin( &pViewFrame->GetWindow(), this ),
243 	pSearchItem(0),
244     bSourceSaved(sal_False),
245     eLoadEncoding(RTL_TEXTENCODING_DONTKNOW)
246 {
247 	Init();
248 }
249 
250 /*--------------------------------------------------------------------
251 	Beschreibung:
252  --------------------------------------------------------------------*/
253 
254 
~SwSrcView()255 SwSrcView::~SwSrcView()
256 {
257 	SwDocShell* pDocShell = GetDocShell();
258 	DBG_ASSERT(PTR_CAST(SwWebDocShell, pDocShell), "Wieso keine WebDocShell?");
259 	const TextSelection&  rSel = aEditWin.GetTextView()->GetSelection();
260 	((SwWebDocShell*)pDocShell)->SetSourcePara( static_cast< sal_uInt16 >( rSel.GetStart().GetPara() ) );
261 
262     uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
263         pDocShell->GetModel(), uno::UNO_QUERY_THROW);
264     uno::Reference<document::XDocumentProperties> xDocProps
265         = xDPS->getDocumentProperties();
266     ::rtl::OUString url = xDocProps->getAutoloadURL();
267     sal_Int32 delay = xDocProps->getAutoloadSecs();
268 	pDocShell->SetAutoLoad(INetURLObject(url), delay,
269                             (delay != 0) || !url.equalsAscii(""));
270 //	EndListening(*GetViewFrame());
271 	EndListening(*pDocShell);
272 	delete pSearchItem;
273 }
274 
275 /*-----------------24.04.97 10:37-------------------
276 
277 --------------------------------------------------*/
SaveContentTo(SfxMedium & rMed)278 void SwSrcView::SaveContentTo(SfxMedium& rMed)
279 {
280     SvStream* pOutStream = rMed.GetOutStream();
281     pOutStream->SetStreamCharSet(lcl_GetStreamCharSet(eLoadEncoding));
282     aEditWin.Write( *pOutStream );//, EE_FORMAT_TEXT);
283 }
284 
285 /*--------------------------------------------------------------------
286 	Beschreibung:
287  --------------------------------------------------------------------*/
288 
289 
Init()290 void SwSrcView::Init()
291 {
292 	SetHelpId(SW_SRC_VIEWSHELL);
293 	SetName(C2S("Source"));
294 	SetWindow( &aEditWin );
295     SwDocShell* pDocShell = GetDocShell();
296 	// wird das Doc noch geladen, dann muss die DocShell das Load
297 	// anwerfen, wenn das Laden abgeschlossen ist
298 	if(!pDocShell->IsLoading())
299 		Load(pDocShell);
300 	else
301 	{
302 		aEditWin.SetReadonly(sal_True);
303 	}
304 
305     SetNewWindowAllowed( sal_False );
306     StartListening(*pDocShell,sal_True);
307 }
308 
309 /*--------------------------------------------------------------------
310 	Beschreibung:
311  --------------------------------------------------------------------*/
312 
GetDocShell()313 SwDocShell*		SwSrcView::GetDocShell()
314 {
315 	SfxObjectShell* pObjShell = GetViewFrame()->GetObjectShell();
316 	return PTR_CAST(SwDocShell, pObjShell);
317 }
318 
319 /*--------------------------------------------------------------------
320 	Beschreibung:
321  --------------------------------------------------------------------*/
322 
323 
SaveContent(const String & rTmpFile)324 void SwSrcView::SaveContent(const String& rTmpFile)
325 {
326 	SfxMedium aMedium( rTmpFile,	STREAM_WRITE, sal_True);
327     SvStream* pOutStream = aMedium.GetOutStream();
328     pOutStream->SetStreamCharSet( lcl_GetStreamCharSet(eLoadEncoding) );
329 	aEditWin.Write(*pOutStream);//, EE_FORMAT_TEXT);
330 	aMedium.Commit();
331 }
332 
333 /*--------------------------------------------------------------------
334 	Beschreibung:
335  --------------------------------------------------------------------*/
336 
337 
Execute(SfxRequest & rReq)338 void SwSrcView::Execute(SfxRequest& rReq)
339 {
340 	sal_uInt16 nSlot = rReq.GetSlot();
341 	TextView* pTextView = aEditWin.GetTextView();
342 	switch( nSlot )
343 	{
344         case SID_SAVEASDOC:
345 		{
346 			SvtPathOptions aPathOpt;
347 			// filesave dialog with autoextension
348 			FileDialogHelper aDlgHelper(
349                 TemplateDescription::FILESAVE_AUTOEXTENSION,
350                 TemplateDescription::FILESAVE_SIMPLE );
351             uno::Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
352 //			pFileDlg->SetHelpId(HID_FILEDLG_SRCVIEW);
353             uno::Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
354 
355 			// search for an html filter for export
356 			SfxFilterContainer* pFilterCont = GetObjectShell()->GetFactory().GetFilterContainer();
357 			const SfxFilter* pFilter =
358 				pFilterCont->GetFilter4Extension( C2S("html"), SFX_FILTER_EXPORT );
359 			if ( pFilter )
360 			{
361 				// filter found -> use its uiname and wildcard
362 				const String& rUIName = pFilter->GetUIName();
363 				const WildCard& rCard = pFilter->GetWildcard();
364 	            xFltMgr->appendFilter( rUIName, rCard() );
365 	            xFltMgr->setCurrentFilter( rUIName ) ;
366 			}
367 			else
368 			{
369 				// filter not found
370 	            String sHtml(C2S("HTML"));
371 	            xFltMgr->appendFilter( sHtml, C2S("*.html;*.htm") );
372 	            xFltMgr->setCurrentFilter( sHtml ) ;
373 			}
374 
375             xFP->setDisplayDirectory( aPathOpt.GetWorkPath() );
376             if( aDlgHelper.Execute() == ERRCODE_NONE)
377 			{
378                 SfxMedium aMedium( xFP->getFiles().getConstArray()[0],
379 									STREAM_WRITE | STREAM_SHARE_DENYNONE,
380 									sal_False );
381                 SvStream* pOutStream = aMedium.GetOutStream();
382                 pOutStream->SetStreamCharSet(lcl_GetStreamCharSet(eLoadEncoding));
383 				aEditWin.Write( *pOutStream );
384 				aMedium.Commit();
385 			}
386 		}
387 		break;
388 		case SID_SAVEDOC:
389 		{
390 			SwDocShell* pDocShell = GetDocShell();
391 			SfxMedium* pMed = 0;
392 			if(pDocShell->HasName())
393 				pMed = pDocShell->GetMedium();
394 			else
395 			{
396 				SfxBoolItem* pItem = (SfxBoolItem*)pDocShell->ExecuteSlot(rReq, pDocShell->GetInterface());
397 				if(pItem && pItem->GetValue())
398 					pMed = pDocShell->GetMedium();
399 			}
400 			if(pMed)
401 			{
402                 SvStream* pOutStream = pMed->GetOutStream();
403                 pOutStream->Seek(0);
404                 pOutStream->SetStreamSize(0);
405                 pOutStream->SetStreamCharSet(lcl_GetStreamCharSet(eLoadEncoding));
406                 aEditWin.Write( *pOutStream );
407                 pMed->CloseOutStream();
408                 pMed->Commit();
409 				pDocShell->GetDoc()->ResetModified();
410 				SourceSaved();
411 				aEditWin.ClearModifyFlag();
412 			}
413 		}
414 		break;
415 		case FID_SEARCH_NOW:
416 		{
417 			const SfxItemSet* pTmpArgs = rReq.GetArgs();
418 
419 			sal_uInt16 nWhich = pTmpArgs->GetWhichByPos( 0 );
420 			DBG_ASSERT( nWhich, "Wich fuer SearchItem ?" );
421 			const SfxPoolItem& rItem = pTmpArgs->Get( nWhich );
422 			SetSearchItem( (const SvxSearchItem&)rItem);
423 			StartSearchAndReplace( (const SvxSearchItem&)rItem, sal_False, rReq.IsAPI() );
424 			if(aEditWin.IsModified())
425 				GetDocShell()->GetDoc()->SetModified();
426 		}
427 		break;
428         case FN_REPEAT_SEARCH:
429         {
430             SvxSearchItem* pSrchItem = GetSearchItem();
431             if(pSrchItem)
432             {
433                 StartSearchAndReplace( *pSrchItem, sal_False, rReq.IsAPI() );
434                 if(aEditWin.IsModified())
435                     GetDocShell()->GetDoc()->SetModified();
436             }
437         }
438         break;
439 		case SID_PRINTDOC:
440 		case SID_PRINTDOCDIRECT:
441 		{
442 			SfxViewShell::ExecuteSlot( rReq, SfxViewShell::GetInterface() );
443 		}
444 		break;
445 		case SID_UNDO:
446 			pTextView->Undo();
447 			GetViewFrame()->GetBindings().InvalidateAll(sal_False);
448 		break;
449 		case SID_REDO:
450 			pTextView->Redo();
451 			GetViewFrame()->GetBindings().InvalidateAll(sal_False);
452 		break;
453 		case SID_REPEAT:
454 		break;
455 		case SID_CUT:
456 			if(pTextView->HasSelection())
457 				pTextView->Cut();
458 		break;
459 		case SID_COPY:
460 			if(pTextView->HasSelection())
461 				pTextView->Copy();
462 		break;
463 		case SID_PASTE:
464 			pTextView->Paste();
465 		break;
466 		case SID_SELECTALL:
467 			pTextView->SetSelection( TextSelection( TextPaM( 0, 0 ), TextPaM( 0xFFFFFFFF, 0xFFFF ) ) );
468 		break;
469 	}
470 	aEditWin.Invalidate();
471 }
472 
473 /*--------------------------------------------------------------------
474 	Beschreibung:
475  --------------------------------------------------------------------*/
476 
477 
GetState(SfxItemSet & rSet)478 void SwSrcView::GetState(SfxItemSet& rSet)
479 {
480 	SfxWhichIter aIter(rSet);
481 	sal_uInt16 nWhich = aIter.FirstWhich();
482 	TextView* pTextView = aEditWin.GetTextView();
483 
484 	while(nWhich)
485 	{
486 		switch(nWhich)
487 		{
488 			case SID_SAVEASDOC:
489 				rSet.Put(SfxStringItem(nWhich, String(SW_RES(STR_SAVEAS_SRC))));
490 			break;
491 			case SID_SAVEDOC:
492 			{
493 				SwDocShell* pDocShell = GetDocShell();
494 				if(!pDocShell->IsModified())
495 					rSet.DisableItem(nWhich);
496 			}
497 			break;
498 			case SID_PRINTDOC:
499 			case SID_PRINTDOCDIRECT:
500 			break;
501 			case SID_TABLE_CELL:
502 			{
503 				String aPos( SW_RES(STR_SRCVIEW_ROW) );
504 				TextSelection aSel = pTextView->GetSelection();
505 				aPos += String::CreateFromInt32( aSel.GetEnd().GetPara()+1 );
506 				aPos +=C2S(" : ");
507 				aPos += String(SW_RES(STR_SRCVIEW_COL));
508 				aPos += String::CreateFromInt32( aSel.GetEnd().GetIndex()+1 );
509 				SfxStringItem aItem( nWhich, aPos );
510 				rSet.Put( aItem );
511 			}
512 			break;
513             case SID_SEARCH_OPTIONS:
514 			{
515 				sal_uInt16 nOpt = SRC_SEARCHOPTIONS;
516 				if(GetDocShell()->IsReadOnly())
517 					nOpt &= ~(SEARCH_OPTIONS_REPLACE|SEARCH_OPTIONS_REPLACE_ALL);
518 
519 				rSet.Put( SfxUInt16Item( SID_SEARCH_OPTIONS,  nOpt) );
520 			}
521 			break;
522 			case SID_SEARCH_ITEM:
523 			{
524 				String sSelected;
525 				if ( !pTextView->HasSelection() )
526 				{
527 					const TextSelection& rSel = pTextView->GetSelection();
528 					sSelected = aEditWin.GetTextEngine()->GetWord( rSel.GetStart());
529 				}
530 				else
531 				{
532 					sSelected = pTextView->GetSelected();
533 				}
534 				SvxSearchItem * pSrchItem = GetSearchItem();
535 				pSrchItem->SetSearchString( sSelected );
536 				rSet.Put( *pSrchItem );
537 			}
538 			break;
539             case FN_REPEAT_SEARCH:
540             {
541                 if(!GetSearchItem())
542                     rSet.DisableItem(nWhich);
543             };
544             break;
545             case SID_UNDO:
546 			case SID_REDO:
547 			{
548                 ::svl::IUndoManager& rMgr = pTextView->GetTextEngine()->GetUndoManager();
549 				sal_uInt16 nCount = 0;
550 				if(nWhich == SID_UNDO)
551 				{
552 					nCount = rMgr.GetUndoActionCount();
553 					if(nCount)
554 					{
555 						String aStr(SvtResId( STR_UNDO));;
556 						aStr += rMgr.GetUndoActionComment(--nCount);
557 						rSet.Put(SfxStringItem(nWhich, aStr));
558 					}
559 					else
560 						rSet.DisableItem(nWhich);
561 				}
562 				else
563 				{
564 					nCount = rMgr.GetRedoActionCount();
565 					if(nCount)
566 					{
567 						String aStr(SvtResId( STR_REDO));;
568 						aStr += rMgr.GetRedoActionComment(--nCount);
569 						rSet.Put(SfxStringItem(nWhich,aStr));
570 					}
571 					else
572 						rSet.DisableItem(nWhich);
573 				}
574 			}
575 			break;
576             case SID_MAIL_SENDDOCASPDF:
577             case SID_MAIL_SENDDOC :
578             case SID_EXPORTDOCASPDF:
579             case SID_DIRECTEXPORTDOCASPDF:
580             case SID_EXPORTDOC:
581             case SID_REPEAT:
582 			case SID_BROWSER_MODE:
583 			case FN_PRINT_LAYOUT:
584 				rSet.DisableItem(nWhich);
585 			break;
586 			case SID_CUT:
587 			case SID_COPY:
588 				if(!pTextView->HasSelection())
589 					rSet.DisableItem(nWhich);
590 			break;
591 			case SID_PASTE:
592 			{
593 				TransferableDataHelper aDataHelper(
594 					TransferableDataHelper::CreateFromSystemClipboard(
595 														&aEditWin) );
596 				sal_Bool bDisable = !aDataHelper.GetXTransferable().is() ||
597 							0 == aDataHelper.GetFormatCount();
598 				if( bDisable )
599 					rSet.DisableItem(nWhich);
600 			}
601 			break;
602         }
603 		nWhich = aIter.NextWhich();
604 	}
605 }
606 
607 /*--------------------------------------------------------------------
608 	Beschreibung:
609  --------------------------------------------------------------------*/
610 
611 
GetSearchItem()612 SvxSearchItem* SwSrcView::GetSearchItem()
613 {
614 	if(!pSearchItem)
615 	{
616 		pSearchItem = new SvxSearchItem(SID_SEARCH_ITEM);
617 	}
618 	return pSearchItem;
619 }
620 
621 /*--------------------------------------------------------------------
622 	Beschreibung:
623  --------------------------------------------------------------------*/
624 
625 
SetSearchItem(const SvxSearchItem & rItem)626 void SwSrcView::SetSearchItem( const SvxSearchItem& rItem )
627 {
628 	delete pSearchItem;
629 	pSearchItem = (SvxSearchItem*)rItem.Clone();
630 }
631 
632 /*--------------------------------------------------------------------
633 	Beschreibung:
634  --------------------------------------------------------------------*/
635 
636 
StartSearchAndReplace(const SvxSearchItem & rSearchItem,sal_Bool bFromStart,sal_Bool bApi,sal_Bool bRecursive)637 sal_uInt16 SwSrcView::StartSearchAndReplace(const SvxSearchItem& rSearchItem,
638 													sal_Bool bFromStart,
639 													sal_Bool bApi,
640 													sal_Bool bRecursive)
641 {
642 	ExtTextView* pTextView = aEditWin.GetTextView();
643 	TextSelection aSel;
644 	TextPaM aPaM;
645 
646 	sal_Bool bForward = !rSearchItem.GetBackward();
647 	sal_Bool bAtStart = pTextView->GetSelection() == TextSelection( aPaM, aPaM );
648 
649 	if( !bForward )
650 		aPaM = TextPaM( (sal_uLong)-1, (sal_uInt16)-1 );
651 
652 	if( bFromStart )
653 	{
654 		aSel = pTextView->GetSelection();
655 		pTextView->SetSelection( TextSelection( aPaM, aPaM ));
656 	}
657 
658 	util::SearchOptions aSearchOpt( rSearchItem.GetSearchOptions() );
659 	aSearchOpt.Locale = SvxCreateLocale(
660 		static_cast< LanguageType >( GetAppLanguage() ) );
661 
662 	sal_uInt16 nFound;
663 	sal_Bool bAll = sal_False;
664 	switch( rSearchItem.GetCommand() )
665 	{
666 	case SVX_SEARCHCMD_FIND:
667 	case SVX_SEARCHCMD_FIND_ALL:
668 		nFound = pTextView->Search( aSearchOpt, bForward );
669 		break;
670 
671 	case SVX_SEARCHCMD_REPLACE_ALL: bAll = sal_True;
672 	case SVX_SEARCHCMD_REPLACE:
673 		nFound = pTextView->Replace( aSearchOpt, bAll, bForward );
674 		break;
675 
676 	default:
677 		nFound = 0;
678 	}
679 
680 	if( !nFound )
681 	{
682 		sal_Bool bNotFoundMessage = sal_False;
683 		if(!bRecursive)
684 		{
685 			if(!bFromStart)
686 			{
687 				bNotFoundMessage = bAtStart;
688 			}
689 			else
690 			{
691 				bNotFoundMessage = sal_True;
692 				pTextView->SetSelection( aSel );
693 			}
694 		}
695 		else if(bAtStart)
696 		{
697 			bNotFoundMessage = sal_True;
698 		}
699 
700 
701 		if(!bApi)
702 		{
703 			if(bNotFoundMessage)
704 			{
705 				InfoBox( 0, SW_RES(MSG_NOT_FOUND)).Execute();
706 			}
707 			else if(!bRecursive && RET_YES ==
708 				QueryBox(0, SW_RES( bForward ? MSG_SEARCH_END
709 								             : MSG_SEARCH_START)).Execute())
710 			{
711 				pTextView->SetSelection( TextSelection( aPaM, aPaM ) );
712 				StartSearchAndReplace( rSearchItem, sal_False, sal_False, sal_True );
713 			}
714 		}
715 	}
716 	return nFound;
717 }
718 
719 /*--------------------------------------------------------------------
720 	Beschreibung:
721  --------------------------------------------------------------------*/
722 
SetPrinter(SfxPrinter * pNew,sal_uInt16 nDiffFlags,bool)723 sal_uInt16 SwSrcView::SetPrinter(SfxPrinter* pNew, sal_uInt16 nDiffFlags, bool )
724 {
725     SwDocShell* pDocSh = GetDocShell();
726     if ( (SFX_PRINTER_JOBSETUP | SFX_PRINTER_PRINTER) & nDiffFlags )
727     {
728         pDocSh->GetDoc()->setPrinter( pNew, true, true );
729         if ( nDiffFlags & SFX_PRINTER_PRINTER )
730             pDocSh->SetModified();
731     }
732     if ( nDiffFlags & SFX_PRINTER_OPTIONS )
733         ::SetPrinter( pDocSh->getIDocumentDeviceAccess(), pNew, sal_True );
734 
735     const sal_Bool bChgOri = nDiffFlags & SFX_PRINTER_CHG_ORIENTATION ? sal_True : sal_False;
736     const sal_Bool bChgSize= nDiffFlags & SFX_PRINTER_CHG_SIZE ? sal_True : sal_False;
737     if ( bChgOri || bChgSize )
738     {
739         pDocSh->SetModified();
740     }
741     return 0;
742 }
743 
744 /*--------------------------------------------------------------------
745     Beschreibung:
746  --------------------------------------------------------------------*/
747 
GetPrinter(sal_Bool bCreate)748 SfxPrinter* SwSrcView::GetPrinter( sal_Bool bCreate )
749 {
750     return  GetDocShell()->GetDoc()->getPrinter( bCreate );
751 }
752 
753 /*--------------------------------------------------------------------
754     Beschreibung:
755  --------------------------------------------------------------------*/
PrintSource(OutputDevice * pOutDev,sal_Int32 nPage,bool bCalcNumPagesOnly)756 sal_Int32 SwSrcView::PrintSource(
757     OutputDevice *pOutDev,
758     sal_Int32 nPage,
759     bool bCalcNumPagesOnly )
760 {
761     if (!pOutDev || nPage <= 0)
762         return 0;
763 
764     //! this a lgorithm for printing the n-th page is very poor since it
765     //! needs to go over the text of all previous pages to get to the correct one.
766     //! But since HTML source code is expected to be just a small number of pages
767     //! even this poor algorithm should be enough...
768 
769     pOutDev->Push();
770 
771     TextEngine* pTextEngine = aEditWin.GetTextEngine();
772     pOutDev->SetMapMode( MAP_100TH_MM );
773     Font aFont( aEditWin.GetOutWin()->GetFont() );
774     Size aSize( aFont.GetSize() );
775     aSize = aEditWin.GetOutWin()->PixelToLogic( aSize, MAP_100TH_MM );
776     aFont.SetSize( aSize );
777     aFont.SetColor( COL_BLACK );
778     pOutDev->SetFont( aFont );
779 
780     String aTitle( GetViewFrame()->GetWindow().GetText() );
781 
782     sal_uInt16 nLineHeight = (sal_uInt16) pOutDev->GetTextHeight(); // etwas mehr.
783     sal_uInt16 nParaSpace = 10;
784 
785     Size aPaperSz = pOutDev->GetOutputSize();
786     aPaperSz.Width() -= (LMARGPRN + RMARGPRN);
787     aPaperSz.Height() -= (TMARGPRN + BMARGPRN);
788 
789     // nLinepPage stimmt nicht, wenn Zeilen umgebrochen werden muessen...
790     sal_uInt16 nLinespPage = (sal_uInt16) (aPaperSz.Height() / nLineHeight);
791     sal_uInt16 nCharspLine = (sal_uInt16) (aPaperSz.Width()  / pOutDev->GetTextWidth( 'X' ));
792     sal_uInt32 nParas = pTextEngine->GetParagraphCount();
793 
794     sal_uInt16 nPages = (sal_uInt16) (nParas / nLinespPage + 1 );
795     sal_uInt16 nCurPage = 1;
796 
797     // Header drucken...
798     if (!bCalcNumPagesOnly && nPage == nCurPage)
799         lcl_PrintHeader( *pOutDev, nPages, nCurPage, aTitle );
800     const Point aStartPos( LMARGPRN, TMARGPRN );
801     Point aPos( aStartPos );
802     for ( sal_uInt32 nPara = 0; nPara < nParas; ++nPara )
803     {
804         String aLine( pTextEngine->GetText( nPara ) );
805         lcl_ConvertTabsToSpaces( aLine );
806         sal_uInt16 nLines = aLine.Len() / nCharspLine + 1;
807         for ( sal_uInt16 nLine = 0; nLine < nLines; ++nLine )
808         {
809             String aTmpLine( aLine, nLine * nCharspLine, nCharspLine );
810             aPos.Y() += nLineHeight;
811             if ( aPos.Y() > ( aPaperSz.Height() + TMARGPRN - nLineHeight/2 ) )
812             {
813                 ++nCurPage;
814                 if (!bCalcNumPagesOnly && nPage == nCurPage)
815                     lcl_PrintHeader( *pOutDev, nPages, nCurPage, aTitle );
816                 aPos = aStartPos;
817             }
818             if (!bCalcNumPagesOnly && nPage == nCurPage)
819                 pOutDev->DrawText( aPos, aTmpLine );
820         }
821         aPos.Y() += nParaSpace;
822     }
823 
824     pOutDev->Pop();
825 
826     DBG_ASSERT( bCalcNumPagesOnly || nPage <= nCurPage, "page number out of range" );
827     return nCurPage;
828 }
829 
830 
831 /*--------------------------------------------------------------------
832 	Beschreibung:
833  --------------------------------------------------------------------*/
834 
835 
Notify(SfxBroadcaster & rBC,const SfxHint & rHint)836 void SwSrcView::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
837 {
838 	if ( rHint.ISA(SfxSimpleHint) &&
839             (
840                 ((SfxSimpleHint&) rHint).GetId() == SFX_HINT_MODECHANGED ||
841                 (
842                     ((SfxSimpleHint&) rHint).GetId() == SFX_HINT_TITLECHANGED &&
843                     !GetDocShell()->IsReadOnly() && aEditWin.IsReadonly()
844                 )
845             )
846        )
847 	{
848 		// Broadcast kommt nur einmal!
849 		const SwDocShell* pDocSh = GetDocShell();
850 		const sal_Bool bReadonly = pDocSh->IsReadOnly();
851 		aEditWin.SetReadonly(bReadonly);
852 	}
853 	SfxViewShell::Notify(rBC, rHint);
854 }
855 
856 /*-----------------19.04.97 10:19-------------------
857 
858 --------------------------------------------------*/
Load(SwDocShell * pDocShell)859 void SwSrcView::Load(SwDocShell* pDocShell)
860 {
861     SvxHtmlOptions* pHtmlOptions = SvxHtmlOptions::Get();
862     const sal_Char *pCharSet =
863         rtl_getBestMimeCharsetFromTextEncoding( pHtmlOptions->GetTextEncoding() );
864 	rtl_TextEncoding eDestEnc = rtl_getTextEncodingFromMimeCharset( pCharSet );
865 
866     aEditWin.SetReadonly(pDocShell->IsReadOnly());
867     aEditWin.SetTextEncoding(eDestEnc);
868 	SfxMedium* pMedium = pDocShell->GetMedium();
869 
870 	const SfxFilter* pFilter = pMedium->GetFilter();
871 	sal_Bool bHtml = pFilter && pFilter->GetUserData().EqualsAscii("HTML");
872 	sal_Bool bDocModified = pDocShell->IsModified();
873 	if(bHtml && !bDocModified && pDocShell->HasName())
874 	{
875 		SvStream* pStream = pMedium->GetInStream();
876 		if(pStream && 0 == pStream->GetError() )
877 		{
878 			rtl_TextEncoding eHeaderEnc =
879 				SfxHTMLParser::GetEncodingByHttpHeader(
880                                             pDocShell->GetHeaderAttributes() );
881 			if( RTL_TEXTENCODING_DONTKNOW == eHeaderEnc )
882 			{
883     			const sal_Char *pTmpCharSet =
884 					rtl_getBestMimeCharsetFromTextEncoding( RTL_TEXTENCODING_ISO_8859_1 );
885 				eHeaderEnc = rtl_getTextEncodingFromMimeCharset( pTmpCharSet );
886 			}
887 			if( RTL_TEXTENCODING_DONTKNOW != eHeaderEnc &&
888 			 	eDestEnc != eHeaderEnc )
889 			{
890 				eDestEnc = eHeaderEnc;
891 				aEditWin.SetTextEncoding(eDestEnc);
892 			}
893 			pStream->SetStreamCharSet( eDestEnc );
894 			pStream->Seek(0);
895             TextEngine* pTextEngine = aEditWin.GetTextEngine();
896             pTextEngine->EnableUndo(sal_False);
897 			aEditWin.Read(*pStream);//, EE_FORMAT_TEXT);
898             pTextEngine->EnableUndo(sal_True);
899         }
900 		else
901 		{
902 			Window *pTmpWindow = &GetViewFrame()->GetWindow();
903 			InfoBox(pTmpWindow, SW_RES(MSG_ERR_SRCSTREAM)).Execute();
904 		}
905 	}
906 	else
907 	{
908 		utl::TempFile aTempFile;
909 		aTempFile.EnableKillingFile();
910         String sFileURL( aTempFile.GetURL() );
911 		SvtSaveOptions aOpt;
912 
913 		{
914 			SfxMedium aMedium( sFileURL,STREAM_READWRITE, sal_True );
915 			SwWriter aWriter( aMedium, *pDocShell->GetDoc() );
916 			WriterRef xWriter;
917             ::GetHTMLWriter(aEmptyStr, aMedium.GetBaseURL( true ), xWriter);
918 			String sWriteName = pDocShell->HasName() ?
919 									pMedium->GetName() :
920 										(const String&) sFileURL;
921 			sal_uLong nRes = aWriter.Write(xWriter, &sWriteName);
922 			if(nRes)
923 			{
924 				ErrorHandler::HandleError(ErrCode(nRes));
925 				aEditWin.SetReadonly(sal_True);
926 			}
927 			aMedium.Commit();
928 			SvStream* pInStream = aMedium.GetInStream();
929 			pInStream->Seek(0);
930 			pInStream->SetStreamCharSet( eDestEnc );
931 
932 
933 			aEditWin.Read(*pInStream);//, EE_FORMAT_TEXT);
934 		}
935 	}
936 	aEditWin.ClearModifyFlag();
937 
938     eLoadEncoding = eDestEnc;
939 
940 	if(bDocModified)
941 		pDocShell->SetModified();// das Flag wird zwischendurch zurueckgesetzt
942 	// AutoLoad abschalten
943 	pDocShell->SetAutoLoad(INetURLObject(), 0, sal_False);
944 	DBG_ASSERT(PTR_CAST(SwWebDocShell, pDocShell), "Wieso keine WebDocShell?");
945 	sal_uInt16 nLine = ((SwWebDocShell*)pDocShell)->GetSourcePara();
946 	aEditWin.SetStartLine(nLine);
947 	aEditWin.GetTextEngine()->ResetUndo();
948 	aEditWin.GetOutWin()->GrabFocus();
949 }
950 
951 
952