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

Generated by: LCOV version 1.10