LCOV - code coverage report
Current view: top level - sw/source/uibase/uiview - srcview.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 11 441 2.5 %
Date: 2015-06-13 12:38:46 Functions: 8 34 23.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <hintids.hxx>
      21             : #include <com/sun/star/util/SearchOptions.hpp>
      22             : #include <com/sun/star/util/SearchFlags.hpp>
      23             : #include <com/sun/star/i18n/TransliterationModules.hpp>
      24             : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
      25             : #include <comphelper/string.hxx>
      26             : #include <unotools/tempfile.hxx>
      27             : #include <tools/urlobj.hxx>
      28             : #include <vcl/layout.hxx>
      29             : #include <vcl/print.hxx>
      30             : #include <vcl/msgbox.hxx>
      31             : #include <vcl/wrkwin.hxx>
      32             : #include <vcl/metric.hxx>
      33             : #include <svtools/ctrltool.hxx>
      34             : #include <svl/intitem.hxx>
      35             : #include <svl/stritem.hxx>
      36             : #include <unotools/pathoptions.hxx>
      37             : #include <svl/undo.hxx>
      38             : #include <unotools/textsearch.hxx>
      39             : #include <svl/eitem.hxx>
      40             : #include <svl/whiter.hxx>
      41             : #include <unotools/saveopt.hxx>
      42             : #include <svtools/transfer.hxx>
      43             : #include <svtools/svtools.hrc>
      44             : #include <svtools/svtresid.hxx>
      45             : #include <svx/svxids.hrc>
      46             : #include <svtools/htmlcfg.hxx>
      47             : #include <sfx2/app.hxx>
      48             : #include <sfx2/objface.hxx>
      49             : #include <sfx2/viewfrm.hxx>
      50             : #include <sfx2/bindings.hxx>
      51             : #include <sfx2/docfilt.hxx>
      52             : #include <sfx2/fcontnr.hxx>
      53             : #include <sfx2/request.hxx>
      54             : #include <sfx2/prnmon.hxx>
      55             : #include <sfx2/docfile.hxx>
      56             : #include <editeng/fhgtitem.hxx>
      57             : #include <svx/srchdlg.hxx>
      58             : #include <svl/srchitem.hxx>
      59             : #include <editeng/fontitem.hxx>
      60             : #include <editeng/flstitem.hxx>
      61             : #include <editeng/unolingu.hxx>
      62             : #include <sfx2/sfxhtml.hxx>
      63             : #include <swtypes.hxx>
      64             : #include <swmodule.hxx>
      65             : #include <docsh.hxx>
      66             : #include <wdocsh.hxx>
      67             : #include <srcview.hxx>
      68             : #include <viewfunc.hxx>
      69             : #include <doc.hxx>
      70             : #include <IDocumentDeviceAccess.hxx>
      71             : #include <IDocumentState.hxx>
      72             : #include <sfx2/msg.hxx>
      73             : #include <shellio.hxx>
      74             : 
      75             : #include <cmdid.h>
      76             : #include <helpid.h>
      77             : #include <globals.hrc>
      78             : #include <shells.hrc>
      79             : #include <popup.hrc>
      80             : #include <web.hrc>
      81             : #include <view.hrc>
      82             : #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
      83             : #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
      84             : #include <sfx2/filedlghelper.hxx>
      85             : #define SwSrcView
      86             : #include "swslots.hxx"
      87             : 
      88             : #include <unomid.h>
      89             : 
      90             : #include <com/sun/star/document/XDocumentProperties.hpp>
      91             : #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
      92             : 
      93             : using namespace ::com::sun::star;
      94             : using namespace ::com::sun::star::i18n;
      95             : using namespace ::com::sun::star::lang;
      96             : using namespace ::com::sun::star::uno;
      97             : using namespace ::com::sun::star::ui::dialogs;
      98             : using namespace ::sfx2;
      99             : using ::com::sun::star::util::SearchOptions;
     100             : 
     101             : #define SWSRCVIEWFLAGS ( SfxViewShellFlags::CAN_PRINT | SfxViewShellFlags::NO_NEWWINDOW )
     102             : 
     103             : #define SRC_SEARCHOPTIONS (SearchOptionFlags::ALL & ~SearchOptionFlags(SearchOptionFlags::FORMAT|SearchOptionFlags::FAMILIES|SearchOptionFlags::SEARCHALL))
     104             : 
     105             : // Printing margins -> like Basic - Ide
     106             : #define LMARGPRN        1700
     107             : #define RMARGPRN         900
     108             : #define TMARGPRN        2000
     109             : #define BMARGPRN        1000
     110             : #define BORDERPRN       300
     111             : 
     112         118 : SFX_IMPL_NAMED_VIEWFACTORY(SwSrcView, "SourceView")
     113             : {
     114          59 :     SFX_VIEW_REGISTRATION(SwWebDocShell);
     115          59 : }
     116             : 
     117         295 : SFX_IMPL_SUPERCLASS_INTERFACE(SwSrcView, SfxViewShell)
     118             : 
     119          59 : void SwSrcView::InitInterface_Impl()
     120             : {
     121          59 :     GetStaticInterface()->RegisterPopupMenu(SW_RES(MN_SRCVIEW_POPUPMENU));
     122             : 
     123             :     GetStaticInterface()->RegisterObjectBar(SFX_OBJECTBAR_TOOLS|SFX_VISIBILITY_STANDARD|SFX_VISIBILITY_SERVER,
     124          59 :                                             RID_WEBTOOLS_TOOLBOX);
     125             : 
     126          59 :     GetStaticInterface()->RegisterChildWindow(SvxSearchDialogWrapper::GetChildWindowId());
     127          59 : }
     128             : 
     129         102 : TYPEINIT1(SwSrcView, SfxViewShell)
     130             : 
     131           0 : static void lcl_PrintHeader( vcl::RenderContext &rOutDev, sal_Int32 nPages, sal_Int32 nCurPage, const OUString& rTitle )
     132             : {
     133           0 :     short nLeftMargin   = LMARGPRN;
     134           0 :     Size aSz = rOutDev.GetOutputSize();
     135           0 :     short nBorder = BORDERPRN;
     136             : 
     137           0 :     Color aOldFillColor( rOutDev.GetFillColor() );
     138           0 :     vcl::Font aOldFont( rOutDev.GetFont() );
     139             : 
     140           0 :     rOutDev.SetFillColor( Color(COL_TRANSPARENT) );
     141             : 
     142           0 :     vcl::Font aFont( aOldFont );
     143           0 :     aFont.SetWeight( WEIGHT_BOLD );
     144           0 :     aFont.SetAlign( ALIGN_BOTTOM );
     145           0 :     rOutDev.SetFont( aFont );
     146             : 
     147           0 :     long nFontHeight = rOutDev.GetTextHeight();
     148             : 
     149             :     // 1.Border => Line, 2+3 Border = Space.
     150           0 :     long nYTop = TMARGPRN-3*nBorder-nFontHeight;
     151             : 
     152           0 :     long nXLeft = nLeftMargin-nBorder;
     153           0 :     long nXRight = aSz.Width()-RMARGPRN+nBorder;
     154             : 
     155             :     rOutDev.DrawRect( Rectangle(
     156             :         Point( nXLeft, nYTop ),
     157           0 :         Size( nXRight-nXLeft, aSz.Height() - nYTop - BMARGPRN + nBorder ) ) );
     158             : 
     159           0 :     long nY = TMARGPRN-2*nBorder;
     160           0 :     Point aPos( nLeftMargin, nY );
     161           0 :     rOutDev.DrawText( aPos, rTitle );
     162           0 :     if ( nPages != 1 )
     163             :     {
     164           0 :         aFont.SetWeight( WEIGHT_NORMAL );
     165           0 :         rOutDev.SetFont( aFont );
     166           0 :         OUString aPageStr( " [" );
     167           0 :         aPageStr += SW_RES( STR_PAGE );
     168           0 :         aPageStr += " ";
     169           0 :         aPageStr += OUString::number( nCurPage );
     170           0 :         aPageStr += "]";
     171           0 :         aPos.X() += rOutDev.GetTextWidth( rTitle );
     172           0 :         rOutDev.DrawText( aPos, aPageStr );
     173             :     }
     174             : 
     175           0 :     nY = TMARGPRN-nBorder;
     176             : 
     177           0 :     rOutDev.DrawLine( Point( nXLeft, nY ), Point( nXRight, nY ) );
     178             : 
     179           0 :     rOutDev.SetFont( aOldFont );
     180           0 :     rOutDev.SetFillColor( aOldFillColor );
     181           0 : }
     182             : 
     183           0 : static rtl_TextEncoding lcl_GetStreamCharSet(rtl_TextEncoding eLoadEncoding)
     184             : {
     185           0 :     rtl_TextEncoding eRet = eLoadEncoding;
     186           0 :     if(RTL_TEXTENCODING_DONTKNOW == eRet)
     187             :     {
     188           0 :         SvxHtmlOptions& rHtmlOptions = SvxHtmlOptions::Get();
     189             :         const sal_Char *pCharSet =
     190           0 :             rtl_getBestMimeCharsetFromTextEncoding( rHtmlOptions.GetTextEncoding() );
     191           0 :         eRet = rtl_getTextEncodingFromMimeCharset( pCharSet );
     192             :     }
     193           0 :     return eRet;
     194             : }
     195             : 
     196           0 : static OUString lcl_ConvertTabsToSpaces( const OUString& sLine )
     197             : {
     198           0 :     if (sLine.isEmpty())
     199           0 :         return sLine;
     200             : 
     201           0 :     OUString aRet = sLine;
     202           0 :     const sal_Unicode aPadSpaces[4] = {' ', ' ', ' ', ' '};
     203           0 :     sal_Int32 nPos = 0;
     204             :     for (;;)
     205             :     {
     206           0 :         nPos = aRet.indexOf('\t', nPos);
     207           0 :         if (nPos<0)
     208             :         {
     209           0 :             break;
     210             :         }
     211             :         // Not 4 blanks, but on 4th TabPos:
     212           0 :         const sal_Int32 nPadLen = 4 - (nPos % 4);
     213           0 :         aRet = aRet.replaceAt(nPos, 1, OUString(aPadSpaces, nPadLen));
     214           0 :         nPos += nPadLen;
     215           0 :     }
     216           0 :     return aRet;
     217             : }
     218             : 
     219           0 : SwSrcView::SwSrcView(SfxViewFrame* pViewFrame, SfxViewShell*) :
     220           0 :     SfxViewShell( pViewFrame, SWSRCVIEWFLAGS ),
     221           0 :     aEditWin( VclPtr<SwSrcEditWindow>::Create( &pViewFrame->GetWindow(), this ) ),
     222             :     pSearchItem(0),
     223             :     bSourceSaved(false),
     224           0 :     eLoadEncoding(RTL_TEXTENCODING_DONTKNOW)
     225             : {
     226           0 :     Init();
     227           0 : }
     228             : 
     229           0 : SwSrcView::~SwSrcView()
     230             : {
     231           0 :     SwDocShell* pDocShell = GetDocShell();
     232             :     OSL_ENSURE(PTR_CAST(SwWebDocShell, pDocShell), "Why no WebDocShell?");
     233           0 :     const TextSelection&  rSel = aEditWin->GetTextView()->GetSelection();
     234           0 :     static_cast<SwWebDocShell*>(pDocShell)->SetSourcePara( static_cast< sal_uInt16 >( rSel.GetStart().GetPara() ) );
     235             : 
     236             :     uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
     237           0 :         pDocShell->GetModel(), uno::UNO_QUERY_THROW);
     238             :     uno::Reference<document::XDocumentProperties> xDocProps
     239           0 :         = xDPS->getDocumentProperties();
     240           0 :     OUString url = xDocProps->getAutoloadURL();
     241           0 :     sal_Int32 delay = xDocProps->getAutoloadSecs();
     242             :     pDocShell->SetAutoLoad(INetURLObject(url), delay,
     243           0 :                             (delay != 0) || !url.isEmpty());
     244           0 :     EndListening(*pDocShell);
     245           0 :     delete pSearchItem;
     246           0 : }
     247             : 
     248           0 : void SwSrcView::SaveContentTo(SfxMedium& rMed)
     249             : {
     250           0 :     SvStream* pOutStream = rMed.GetOutStream();
     251           0 :     pOutStream->SetStreamCharSet(lcl_GetStreamCharSet(eLoadEncoding));
     252           0 :     aEditWin->Write( *pOutStream );
     253           0 : }
     254             : 
     255           0 : void SwSrcView::Init()
     256             : {
     257           0 :     SetHelpId(SW_SRC_VIEWSHELL);
     258           0 :     SetName(OUString("Source"));
     259           0 :     SetWindow( aEditWin.get() );
     260           0 :     SwDocShell* pDocShell = GetDocShell();
     261             :     // If the doc is still loading, then the DocShell must fire up
     262             :     // the Load if the loading is completed.
     263           0 :     if(!pDocShell->IsLoading())
     264           0 :         Load(pDocShell);
     265             :     else
     266             :     {
     267           0 :         aEditWin->SetReadonly(true);
     268             :     }
     269             : 
     270           0 :     SetNewWindowAllowed( false );
     271           0 :     StartListening(*pDocShell,true);
     272           0 : }
     273             : 
     274           0 : SwDocShell*     SwSrcView::GetDocShell()
     275             : {
     276           0 :     SfxObjectShell* pObjShell = GetViewFrame()->GetObjectShell();
     277           0 :     return PTR_CAST(SwDocShell, pObjShell);
     278             : }
     279             : 
     280           0 : void SwSrcView::SaveContent(const OUString& rTmpFile)
     281             : {
     282           0 :     SfxMedium aMedium( rTmpFile, StreamMode::WRITE);
     283           0 :     SvStream* pOutStream = aMedium.GetOutStream();
     284           0 :     pOutStream->SetStreamCharSet( lcl_GetStreamCharSet(eLoadEncoding) );
     285           0 :     aEditWin->Write(*pOutStream);
     286           0 :     aMedium.Commit();
     287           0 : }
     288             : 
     289           0 : void SwSrcView::Execute(SfxRequest& rReq)
     290             : {
     291           0 :     TextView* pTextView = aEditWin->GetTextView();
     292           0 :     switch( rReq.GetSlot() )
     293             :     {
     294             :         case SID_SAVEACOPY:
     295             :         case SID_SAVEASDOC:
     296             :         {
     297           0 :             SvtPathOptions aPathOpt;
     298             :             // filesave dialog with autoextension
     299             :             FileDialogHelper aDlgHelper(
     300           0 :                 TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
     301           0 :             uno::Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
     302           0 :             uno::Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
     303             : 
     304             :             // search for an html filter for export
     305           0 :             SfxFilterContainer* pFilterCont = GetObjectShell()->GetFactory().GetFilterContainer();
     306             :             const SfxFilter* pFilter =
     307           0 :                 pFilterCont->GetFilter4Extension( OUString("html"), SfxFilterFlags::EXPORT );
     308           0 :             if ( pFilter )
     309             :             {
     310             :                 // filter found -> use its uiname and wildcard
     311           0 :                 const OUString& rUIName = pFilter->GetUIName();
     312           0 :                 const WildCard& rCard = pFilter->GetWildcard();
     313           0 :                 xFltMgr->appendFilter( rUIName, rCard.getGlob() );
     314           0 :                 xFltMgr->setCurrentFilter( rUIName ) ;
     315             :             }
     316             :             else
     317             :             {
     318             :                 // filter not found
     319           0 :                 OUString sHtml("HTML");
     320           0 :                 xFltMgr->appendFilter( sHtml, OUString("*.html;*.htm") );
     321           0 :                 xFltMgr->setCurrentFilter( sHtml ) ;
     322             :             }
     323             : 
     324           0 :             xFP->setDisplayDirectory( aPathOpt.GetWorkPath() );
     325           0 :             if( aDlgHelper.Execute() == ERRCODE_NONE)
     326             :             {
     327           0 :                 SfxMedium aMedium( xFP->getFiles().getConstArray()[0],
     328           0 :                                     StreamMode::WRITE | StreamMode::SHARE_DENYNONE );
     329           0 :                 SvStream* pOutStream = aMedium.GetOutStream();
     330           0 :                 pOutStream->SetStreamCharSet(lcl_GetStreamCharSet(eLoadEncoding));
     331           0 :                 aEditWin->Write( *pOutStream );
     332           0 :                 aMedium.Commit();
     333           0 :             }
     334             :         }
     335           0 :         break;
     336             :         case SID_SAVEDOC:
     337             :         {
     338           0 :             SwDocShell* pDocShell = GetDocShell();
     339           0 :             SfxMedium* pMed = 0;
     340           0 :             if(pDocShell->HasName())
     341           0 :                 pMed = pDocShell->GetMedium();
     342             :             else
     343             :             {
     344           0 :                 const SfxBoolItem* pItem = static_cast<const SfxBoolItem*>(pDocShell->ExecuteSlot(rReq, pDocShell->GetInterface()));
     345           0 :                 if(pItem && pItem->GetValue())
     346           0 :                     pMed = pDocShell->GetMedium();
     347             :             }
     348           0 :             if(pMed)
     349             :             {
     350           0 :                 SvStream* pOutStream = pMed->GetOutStream();
     351           0 :                 pOutStream->Seek(0);
     352           0 :                 pOutStream->SetStreamSize(0);
     353           0 :                 pOutStream->SetStreamCharSet(lcl_GetStreamCharSet(eLoadEncoding));
     354           0 :                 aEditWin->Write( *pOutStream );
     355           0 :                 pMed->CloseOutStream();
     356           0 :                 pMed->Commit();
     357           0 :                 pDocShell->GetDoc()->getIDocumentState().ResetModified();
     358           0 :                 SourceSaved();
     359           0 :                 aEditWin->ClearModifyFlag();
     360             :             }
     361             :         }
     362           0 :         break;
     363             :         case FID_SEARCH_NOW:
     364             :         {
     365           0 :             const SfxItemSet* pTmpArgs = rReq.GetArgs();
     366             : 
     367           0 :             const sal_uInt16 nWhich = pTmpArgs->GetWhichByPos( 0 );
     368             :             OSL_ENSURE( nWhich, "Which for SearchItem ?" );
     369           0 :             const SfxPoolItem& rItem = pTmpArgs->Get( nWhich );
     370           0 :             SetSearchItem( static_cast<const SvxSearchItem&>(rItem));
     371           0 :             StartSearchAndReplace( static_cast<const SvxSearchItem&>(rItem), false, rReq.IsAPI() );
     372           0 :             if(aEditWin->IsModified())
     373           0 :                 GetDocShell()->GetDoc()->getIDocumentState().SetModified();
     374             :         }
     375           0 :         break;
     376             :         case FN_REPEAT_SEARCH:
     377             :         {
     378           0 :             SvxSearchItem* pSrchItem = GetSearchItem();
     379           0 :             if(pSrchItem)
     380             :             {
     381           0 :                 StartSearchAndReplace( *pSrchItem, false, rReq.IsAPI() );
     382           0 :                 if(aEditWin->IsModified())
     383           0 :                     GetDocShell()->GetDoc()->getIDocumentState().SetModified();
     384             :             }
     385             :         }
     386           0 :         break;
     387             :         case SID_PRINTDOC:
     388             :         case SID_PRINTDOCDIRECT:
     389             :         {
     390           0 :             SfxViewShell::ExecuteSlot( rReq, SfxViewShell::GetInterface() );
     391             :         }
     392           0 :         break;
     393             :         case SID_UNDO:
     394           0 :             pTextView->Undo();
     395           0 :             GetViewFrame()->GetBindings().InvalidateAll(false);
     396           0 :         break;
     397             :         case SID_REDO:
     398           0 :             pTextView->Redo();
     399           0 :             GetViewFrame()->GetBindings().InvalidateAll(false);
     400           0 :         break;
     401             :         case SID_REPEAT:
     402           0 :         break;
     403             :         case SID_CUT:
     404           0 :             if(pTextView->HasSelection())
     405           0 :                 pTextView->Cut();
     406           0 :         break;
     407             :         case SID_COPY:
     408           0 :             if(pTextView->HasSelection())
     409           0 :                 pTextView->Copy();
     410           0 :         break;
     411             :         case SID_PASTE:
     412           0 :             pTextView->Paste();
     413           0 :         break;
     414             :         case SID_SELECTALL:
     415           0 :             pTextView->SetSelection( TextSelection( TextPaM( 0, 0 ), TextPaM( 0xFFFFFFFF, 0xFFFF ) ) );
     416           0 :         break;
     417             :     }
     418           0 :     aEditWin->Invalidate();
     419           0 : }
     420             : 
     421           0 : void SwSrcView::GetState(SfxItemSet& rSet)
     422             : {
     423           0 :     SfxWhichIter aIter(rSet);
     424           0 :     sal_uInt16 nWhich = aIter.FirstWhich();
     425           0 :     TextView* pTextView = aEditWin->GetTextView();
     426             : 
     427           0 :     while(nWhich)
     428             :     {
     429           0 :         switch(nWhich)
     430             :         {
     431             :             case SID_SAVEASDOC:
     432           0 :                 rSet.Put(SfxStringItem(nWhich, OUString(SW_RES(STR_SAVEAS_SRC))));
     433           0 :             break;
     434             :             case SID_SAVEACOPY:
     435           0 :                 rSet.Put(SfxStringItem(nWhich, OUString(SW_RES(STR_SAVEACOPY_SRC))));
     436           0 :             break;
     437             :             case SID_SAVEDOC:
     438             :             {
     439           0 :                 SwDocShell* pDocShell = GetDocShell();
     440           0 :                 if(!pDocShell->IsModified())
     441           0 :                     rSet.DisableItem(nWhich);
     442             :             }
     443           0 :             break;
     444             :             case SID_PRINTDOC:
     445             :             case SID_PRINTDOCDIRECT:
     446           0 :             break;
     447             :             case SID_TABLE_CELL:
     448             :             {
     449           0 :                 OUString aPos( SW_RES(STR_SRCVIEW_ROW) );
     450           0 :                 TextSelection aSel = pTextView->GetSelection();
     451           0 :                 aPos += OUString::number( aSel.GetEnd().GetPara()+1 );
     452           0 :                 aPos += " : ";
     453           0 :                 aPos += SW_RES(STR_SRCVIEW_COL);
     454           0 :                 aPos += OUString::number( aSel.GetEnd().GetIndex()+1 );
     455           0 :                 SfxStringItem aItem( nWhich, aPos );
     456           0 :                 rSet.Put( aItem );
     457             :             }
     458           0 :             break;
     459             :             case SID_SEARCH_OPTIONS:
     460             :             {
     461           0 :                 SearchOptionFlags nOpt = SRC_SEARCHOPTIONS;
     462           0 :                 if(GetDocShell()->IsReadOnly())
     463           0 :                     nOpt &= ~SearchOptionFlags(SearchOptionFlags::REPLACE|SearchOptionFlags::REPLACE_ALL);
     464             : 
     465           0 :                 rSet.Put( SfxUInt16Item( SID_SEARCH_OPTIONS,  static_cast<sal_uInt16>(nOpt) ) );
     466             :             }
     467           0 :             break;
     468             :             case SID_SEARCH_ITEM:
     469             :             {
     470           0 :                 OUString sSelected;
     471           0 :                 if ( !pTextView->HasSelection() )
     472             :                 {
     473           0 :                     const TextSelection& rSel = pTextView->GetSelection();
     474           0 :                     sSelected = aEditWin->GetTextEngine()->GetWord( rSel.GetStart());
     475             :                 }
     476             :                 else
     477             :                 {
     478           0 :                     sSelected = pTextView->GetSelected();
     479             :                 }
     480           0 :                 SvxSearchItem * pSrchItem = GetSearchItem();
     481           0 :                 pSrchItem->SetSearchString( sSelected );
     482           0 :                 rSet.Put( *pSrchItem );
     483             :             }
     484           0 :             break;
     485             :             case FN_REPEAT_SEARCH:
     486             :             {
     487           0 :                 if(!GetSearchItem())
     488           0 :                     rSet.DisableItem(nWhich);
     489             :             }
     490           0 :             break;
     491             :             case SID_UNDO:
     492             :             case SID_REDO:
     493             :             {
     494           0 :                 ::svl::IUndoManager& rMgr = pTextView->GetTextEngine()->GetUndoManager();
     495           0 :                 sal_uInt16 nCount = 0;
     496           0 :                 if(nWhich == SID_UNDO)
     497             :                 {
     498           0 :                     nCount = rMgr.GetUndoActionCount();
     499           0 :                     if(nCount)
     500             :                     {
     501           0 :                         OUString aStr(SvtResId( STR_UNDO));
     502           0 :                         aStr += rMgr.GetUndoActionComment(--nCount);
     503           0 :                         rSet.Put(SfxStringItem(nWhich, aStr));
     504             :                     }
     505             :                     else
     506           0 :                         rSet.DisableItem(nWhich);
     507             :                 }
     508             :                 else
     509             :                 {
     510           0 :                     nCount = rMgr.GetRedoActionCount();
     511           0 :                     if(nCount)
     512             :                     {
     513           0 :                         OUString aStr(SvtResId( STR_REDO));
     514           0 :                         aStr += rMgr.GetRedoActionComment(--nCount);
     515           0 :                         rSet.Put(SfxStringItem(nWhich,aStr));
     516             :                     }
     517             :                     else
     518           0 :                         rSet.DisableItem(nWhich);
     519             :                 }
     520             :             }
     521           0 :             break;
     522             :             case SID_MAIL_SENDDOCASPDF:
     523             :             case SID_MAIL_SENDDOC :
     524             :             case SID_EXPORTDOCASPDF:
     525             :             case SID_DIRECTEXPORTDOCASPDF:
     526             :             case SID_EXPORTDOC:
     527             :             case SID_REPEAT:
     528             :             case SID_BROWSER_MODE:
     529             :             case FN_PRINT_LAYOUT:
     530           0 :                 rSet.DisableItem(nWhich);
     531           0 :             break;
     532             :             case SID_CUT:
     533             :             case SID_COPY:
     534           0 :                 if(!pTextView->HasSelection())
     535           0 :                     rSet.DisableItem(nWhich);
     536           0 :             break;
     537             :             case SID_PASTE:
     538             :             {
     539             :                 TransferableDataHelper aDataHelper(
     540             :                     TransferableDataHelper::CreateFromSystemClipboard(
     541           0 :                                                         aEditWin.get()) );
     542           0 :                 bool bDisable = !aDataHelper.GetXTransferable().is() ||
     543           0 :                             0 == aDataHelper.GetFormatCount();
     544           0 :                 if( bDisable )
     545           0 :                     rSet.DisableItem(nWhich);
     546             :             }
     547           0 :             break;
     548             :         }
     549           0 :         nWhich = aIter.NextWhich();
     550           0 :     }
     551           0 : }
     552             : 
     553           0 : SvxSearchItem* SwSrcView::GetSearchItem()
     554             : {
     555           0 :     if(!pSearchItem)
     556             :     {
     557           0 :         pSearchItem = new SvxSearchItem(SID_SEARCH_ITEM);
     558             :     }
     559           0 :     return pSearchItem;
     560             : }
     561             : 
     562           0 : void SwSrcView::SetSearchItem( const SvxSearchItem& rItem )
     563             : {
     564           0 :     delete pSearchItem;
     565           0 :     pSearchItem = static_cast<SvxSearchItem*>(rItem.Clone());
     566           0 : }
     567             : 
     568           0 : sal_uInt16 SwSrcView::StartSearchAndReplace(const SvxSearchItem& rSearchItem,
     569             :                                                   bool bFromStart,
     570             :                                                   bool bApi,
     571             :                                                   bool bRecursive)
     572             : {
     573           0 :     ExtTextView* pTextView = aEditWin->GetTextView();
     574           0 :     TextSelection aSel;
     575           0 :     TextPaM aPaM;
     576             : 
     577           0 :     bool bForward = !rSearchItem.GetBackward();
     578           0 :     bool bAtStart = pTextView->GetSelection() == TextSelection( aPaM, aPaM );
     579             : 
     580           0 :     if( !bForward )
     581           0 :         aPaM = TextPaM( (sal_uLong)-1, USHRT_MAX );
     582             : 
     583           0 :     if( bFromStart )
     584             :     {
     585           0 :         aSel = pTextView->GetSelection();
     586           0 :         pTextView->SetSelection( TextSelection( aPaM, aPaM ));
     587             :     }
     588             : 
     589           0 :     util::SearchOptions aSearchOpt( rSearchItem.GetSearchOptions() );
     590           0 :     aSearchOpt.Locale = GetAppLanguageTag().getLocale();
     591             : 
     592             :     sal_uInt16 nFound;
     593           0 :     bool bAll = false;
     594           0 :     switch( rSearchItem.GetCommand() )
     595             :     {
     596             :     case SvxSearchCmd::FIND:
     597             :     case SvxSearchCmd::FIND_ALL:
     598           0 :         nFound = pTextView->Search( aSearchOpt, bForward ) ? 1 : 0;
     599           0 :         break;
     600             : 
     601           0 :     case SvxSearchCmd::REPLACE_ALL: bAll = true;
     602             :     case SvxSearchCmd::REPLACE:
     603           0 :         nFound = pTextView->Replace( aSearchOpt, bAll, bForward );
     604           0 :         break;
     605             : 
     606             :     default:
     607           0 :         nFound = 0;
     608             :     }
     609             : 
     610           0 :     if( !nFound )
     611             :     {
     612           0 :         bool bNotFoundMessage = false;
     613           0 :         if(!bRecursive)
     614             :         {
     615           0 :             if(!bFromStart)
     616             :             {
     617           0 :                 bNotFoundMessage = bAtStart;
     618             :             }
     619             :             else
     620             :             {
     621           0 :                 bNotFoundMessage = true;
     622           0 :                 pTextView->SetSelection( aSel );
     623             :             }
     624             :         }
     625           0 :         else if(bAtStart)
     626             :         {
     627           0 :             bNotFoundMessage = true;
     628             :         }
     629             : 
     630           0 :         if(!bApi)
     631             :         {
     632           0 :             if(bNotFoundMessage)
     633             :             {
     634             :                 ScopedVclPtrInstance<MessageDialog>::Create(nullptr, "InfoNotFoundDialog",
     635           0 :                     "modules/swriter/ui/infonotfounddialog.ui")->Execute();
     636             :             }
     637           0 :             else if(!bRecursive)
     638             :             {
     639             :                 int nRet;
     640             : 
     641           0 :                 if (!bForward)
     642             :                 {
     643             :                     nRet = ScopedVclPtrInstance<MessageDialog>::Create(nullptr, "QueryContinueEndDialog",
     644           0 :                         "modules/swriter/ui/querycontinueenddialog.ui")->Execute();
     645             :                 }
     646             :                 else
     647             :                 {
     648             :                     nRet = ScopedVclPtrInstance<MessageDialog>::Create(nullptr, "QueryContinueBeginDialog",
     649           0 :                         "modules/swriter/ui/querycontinuebegindialog.ui")->Execute();
     650             :                 }
     651             : 
     652           0 :                 if (nRet == RET_YES)
     653             :                 {
     654           0 :                     pTextView->SetSelection( TextSelection( aPaM, aPaM ) );
     655           0 :                     StartSearchAndReplace( rSearchItem, false, false, true );
     656             :                 }
     657             :             }
     658             :         }
     659             :     }
     660           0 :     return nFound;
     661             : }
     662             : 
     663           0 : sal_uInt16 SwSrcView::SetPrinter(SfxPrinter* pNew, SfxPrinterChangeFlags nDiffFlags, bool )
     664             : {
     665           0 :     SwDocShell* pDocSh = GetDocShell();
     666           0 :     if ( (SfxPrinterChangeFlags::JOBSETUP | SfxPrinterChangeFlags::PRINTER) & nDiffFlags )
     667             :     {
     668           0 :         pDocSh->GetDoc()->getIDocumentDeviceAccess().setPrinter( pNew, true, true );
     669           0 :         if ( nDiffFlags & SfxPrinterChangeFlags::PRINTER )
     670           0 :             pDocSh->SetModified();
     671             :     }
     672           0 :     if ( nDiffFlags & SfxPrinterChangeFlags::OPTIONS )
     673           0 :         ::SetPrinter( pDocSh->getIDocumentDeviceAccess(), pNew, true );
     674             : 
     675           0 :     const bool bChgOri  = bool(nDiffFlags & SfxPrinterChangeFlags::CHG_ORIENTATION);
     676           0 :     const bool bChgSize = bool(nDiffFlags & SfxPrinterChangeFlags::CHG_SIZE);
     677           0 :     if ( bChgOri || bChgSize )
     678             :     {
     679           0 :         pDocSh->SetModified();
     680             :     }
     681           0 :     return 0;
     682             : }
     683             : 
     684           0 : SfxPrinter* SwSrcView::GetPrinter( bool bCreate )
     685             : {
     686           0 :     return  GetDocShell()->GetDoc()->getIDocumentDeviceAccess().getPrinter( bCreate );
     687             : }
     688             : 
     689           0 : sal_Int32 SwSrcView::PrintSource(
     690             :     OutputDevice *pOutDev,
     691             :     sal_Int32 nPage,
     692             :     bool bCalcNumPagesOnly )
     693             : {
     694           0 :     if (!pOutDev || nPage <= 0)
     695           0 :         return 0;
     696             : 
     697             :     //! This algorithm for printing the n-th page is very poor since it
     698             :     //! needs to go over the text of all previous pages to get to the correct one.
     699             :     //! But since HTML source code is expected to be just a small number of pages
     700             :     //! even this poor algorithm should be enough...
     701             : 
     702           0 :     pOutDev->Push();
     703             : 
     704           0 :     TextEngine* pTextEngine = aEditWin->GetTextEngine();
     705           0 :     pOutDev->SetMapMode( MAP_100TH_MM );
     706           0 :     vcl::Font aFont( aEditWin->GetOutWin()->GetFont() );
     707           0 :     Size aSize( aFont.GetSize() );
     708           0 :     aSize = aEditWin->GetOutWin()->PixelToLogic( aSize, MAP_100TH_MM );
     709           0 :     aFont.SetSize( aSize );
     710           0 :     aFont.SetColor( COL_BLACK );
     711           0 :     pOutDev->SetFont( aFont );
     712             : 
     713           0 :     OUString aTitle( GetViewFrame()->GetWindow().GetText() );
     714             : 
     715           0 :     const long nLineHeight = pOutDev->GetTextHeight(); // slightly more
     716           0 :     const long nParaSpace = 10;
     717             : 
     718           0 :     Size aPaperSz = pOutDev->GetOutputSize();
     719           0 :     aPaperSz.Width() -= (LMARGPRN + RMARGPRN);
     720           0 :     aPaperSz.Height() -= (TMARGPRN + BMARGPRN);
     721             : 
     722             :     // nLinepPage is not true, if lines have to be wrapped...
     723           0 :     const long nLinespPage = nLineHeight ? aPaperSz.Height() / nLineHeight : 1;
     724           0 :     const long nCharWidth = pOutDev->GetTextWidth("X");
     725           0 :     const sal_Int32 nCharspLine = nCharWidth ? static_cast<sal_Int32>(aPaperSz.Width() / nCharWidth) : 1;
     726           0 :     const sal_uLong nParas = pTextEngine->GetParagraphCount();
     727             : 
     728           0 :     const sal_Int32 nPages = static_cast<sal_Int32>(nParas / nLinespPage + 1 );
     729           0 :     sal_Int32 nCurPage = 1;
     730             : 
     731             :     // Print header...
     732           0 :     if (!bCalcNumPagesOnly && nPage == nCurPage)
     733           0 :         lcl_PrintHeader( *pOutDev, nPages, nCurPage, aTitle );
     734           0 :     const Point aStartPos( LMARGPRN, TMARGPRN );
     735           0 :     Point aPos( aStartPos );
     736           0 :     for ( sal_uLong nPara = 0; nPara < nParas; ++nPara )
     737             :     {
     738           0 :         const OUString aLine( lcl_ConvertTabsToSpaces(pTextEngine->GetText( nPara )) );
     739           0 :         const sal_Int32 nLineLen = aLine.getLength();
     740           0 :         const sal_Int32 nLines = (nLineLen+nCharspLine-1) / nCharspLine;
     741           0 :         for ( sal_Int32 nLine = 0; nLine < nLines; ++nLine )
     742             :         {
     743           0 :             aPos.Y() += nLineHeight;
     744           0 :             if ( aPos.Y() > ( aPaperSz.Height() + TMARGPRN - nLineHeight/2 ) )
     745             :             {
     746           0 :                 ++nCurPage;
     747           0 :                 if (!bCalcNumPagesOnly && nPage == nCurPage)
     748           0 :                     lcl_PrintHeader( *pOutDev, nPages, nCurPage, aTitle );
     749           0 :                 aPos = aStartPos;
     750             :             }
     751           0 :             if (!bCalcNumPagesOnly && nPage == nCurPage)
     752             :             {
     753           0 :                 const sal_Int32 nStart = nLine * nCharspLine;
     754           0 :                 const sal_Int32 nLen = std::min(nLineLen-nStart, nCharspLine);
     755           0 :                 pOutDev->DrawText( aPos, aLine.copy(nStart, nLen) );
     756             :             }
     757             :         }
     758           0 :         aPos.Y() += nParaSpace;
     759           0 :     }
     760             : 
     761           0 :     pOutDev->Pop();
     762             : 
     763             :     OSL_ENSURE( bCalcNumPagesOnly || nPage <= nCurPage, "page number out of range" );
     764           0 :     return nCurPage;
     765             : }
     766             : 
     767           0 : void SwSrcView::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
     768             : {
     769           0 :     const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>(&rHint);
     770           0 :     if ( pSimpleHint &&
     771             :             (
     772           0 :                 pSimpleHint->GetId() == SFX_HINT_MODECHANGED ||
     773             :                 (
     774           0 :                     pSimpleHint->GetId() == SFX_HINT_TITLECHANGED &&
     775           0 :                     !GetDocShell()->IsReadOnly() && aEditWin->IsReadonly()
     776             :                 )
     777             :             )
     778             :        )
     779             :     {
     780             :         // Broadcast only comes once!
     781           0 :         const SwDocShell* pDocSh = GetDocShell();
     782           0 :         const bool bReadonly = pDocSh->IsReadOnly();
     783           0 :         aEditWin->SetReadonly(bReadonly);
     784             :     }
     785           0 :     SfxViewShell::Notify(rBC, rHint);
     786           0 : }
     787             : 
     788           0 : void SwSrcView::Load(SwDocShell* pDocShell)
     789             : {
     790           0 :     SvxHtmlOptions& rHtmlOptions = SvxHtmlOptions::Get();
     791             :     const sal_Char *pCharSet =
     792           0 :         rtl_getBestMimeCharsetFromTextEncoding( rHtmlOptions.GetTextEncoding() );
     793           0 :     rtl_TextEncoding eDestEnc = rtl_getTextEncodingFromMimeCharset( pCharSet );
     794             : 
     795           0 :     aEditWin->SetReadonly(pDocShell->IsReadOnly());
     796           0 :     aEditWin->SetTextEncoding(eDestEnc);
     797           0 :     SfxMedium* pMedium = pDocShell->GetMedium();
     798             : 
     799           0 :     const SfxFilter* pFilter = pMedium->GetFilter();
     800           0 :     bool bHtml = pFilter && pFilter->GetUserData() == "HTML";
     801           0 :     bool bDocModified = pDocShell->IsModified();
     802           0 :     if(bHtml && !bDocModified && pDocShell->HasName())
     803             :     {
     804           0 :         SvStream* pStream = pMedium->GetInStream();
     805           0 :         if(pStream && 0 == pStream->GetError() )
     806             :         {
     807             :             rtl_TextEncoding eHeaderEnc =
     808             :                 SfxHTMLParser::GetEncodingByHttpHeader(
     809           0 :                                             pDocShell->GetHeaderAttributes() );
     810           0 :             if( RTL_TEXTENCODING_DONTKNOW == eHeaderEnc )
     811             :             {
     812             :                 const sal_Char *pTmpCharSet =
     813           0 :                     rtl_getBestMimeCharsetFromTextEncoding( RTL_TEXTENCODING_ISO_8859_1 );
     814           0 :                 eHeaderEnc = rtl_getTextEncodingFromMimeCharset( pTmpCharSet );
     815             :             }
     816           0 :             if( RTL_TEXTENCODING_DONTKNOW != eHeaderEnc &&
     817             :                  eDestEnc != eHeaderEnc )
     818             :             {
     819           0 :                 eDestEnc = eHeaderEnc;
     820           0 :                 aEditWin->SetTextEncoding(eDestEnc);
     821             :             }
     822           0 :             pStream->SetStreamCharSet( eDestEnc );
     823           0 :             pStream->Seek(0);
     824           0 :             TextEngine* pTextEngine = aEditWin->GetTextEngine();
     825           0 :             pTextEngine->EnableUndo(false);
     826           0 :             aEditWin->Read(*pStream);
     827           0 :             pTextEngine->EnableUndo(true);
     828             :         }
     829             :         else
     830             :         {
     831           0 :             vcl::Window *pTmpWindow = &GetViewFrame()->GetWindow();
     832           0 :             ScopedVclPtrInstance<MessageDialog>::Create(pTmpWindow, SW_RES(STR_ERR_SRCSTREAM), VCL_MESSAGE_INFO)->Execute();
     833             :         }
     834             :     }
     835             :     else
     836             :     {
     837           0 :         utl::TempFile aTempFile;
     838           0 :         aTempFile.EnableKillingFile();
     839           0 :         const OUString sFileURL( aTempFile.GetURL() );
     840           0 :         SvtSaveOptions aOpt;
     841             : 
     842             :         {
     843           0 :             SfxMedium aMedium( sFileURL,STREAM_READWRITE );
     844           0 :             SwWriter aWriter( aMedium, *pDocShell->GetDoc() );
     845           0 :             WriterRef xWriter;
     846           0 :             ::GetHTMLWriter(OUString(), aMedium.GetBaseURL( true ), xWriter);
     847           0 :             const OUString sWriteName = pDocShell->HasName()
     848             :                 ? pMedium->GetName()
     849           0 :                 : sFileURL;
     850           0 :             sal_uLong nRes = aWriter.Write(xWriter, &sWriteName);
     851           0 :             if(nRes)
     852             :             {
     853           0 :                 ErrorHandler::HandleError(ErrCode(nRes));
     854           0 :                 aEditWin->SetReadonly(true);
     855             :             }
     856           0 :             aMedium.Commit();
     857           0 :             SvStream* pInStream = aMedium.GetInStream();
     858           0 :             pInStream->Seek(0);
     859           0 :             pInStream->SetStreamCharSet( eDestEnc );
     860             : 
     861           0 :             aEditWin->Read(*pInStream);
     862           0 :         }
     863             :     }
     864           0 :     aEditWin->ClearModifyFlag();
     865             : 
     866           0 :     eLoadEncoding = eDestEnc;
     867             : 
     868           0 :     if(bDocModified)
     869           0 :         pDocShell->SetModified();// The flag will be reset in between times.
     870             :     // Disable AutoLoad
     871           0 :     pDocShell->SetAutoLoad(INetURLObject(), 0, false);
     872             :     OSL_ENSURE(PTR_CAST(SwWebDocShell, pDocShell), "Why no WebDocShell?");
     873           0 :     sal_uInt16 nLine = static_cast<SwWebDocShell*>(pDocShell)->GetSourcePara();
     874           0 :     aEditWin->SetStartLine(nLine);
     875           0 :     aEditWin->GetTextEngine()->ResetUndo();
     876           0 :     aEditWin->GetOutWin()->GrabFocus();
     877         177 : }
     878             : 
     879             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11