LibreOffice Module sw (master)  1
srcview.cxx
Go to the documentation of this file.
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 <rtl/tencinfo.h>
21 #include <osl/diagnose.h>
22 #include <hintids.hxx>
23 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
24 #include <unotools/tempfile.hxx>
25 #include <tools/urlobj.hxx>
26 #include <vcl/errinf.hxx>
27 #include <vcl/weld.hxx>
28 #include <vcl/print.hxx>
29 #include <vcl/metric.hxx>
30 #include <vcl/textview.hxx>
31 #include <vcl/svapp.hxx>
32 #include <svtools/ctrltool.hxx>
33 #include <svl/intitem.hxx>
34 #include <svl/stritem.hxx>
35 #include <unotools/pathoptions.hxx>
36 #include <svl/undo.hxx>
37 #include <unotools/textsearch.hxx>
38 #include <svl/eitem.hxx>
39 #include <svl/whiter.hxx>
40 #include <unotools/saveopt.hxx>
41 #include <vcl/transfer.hxx>
42 #include <svtools/strings.hrc>
43 #include <svtools/svtresid.hxx>
44 #include <svx/svxids.hrc>
45 #include <svtools/htmlcfg.hxx>
46 #include <sfx2/app.hxx>
47 #include <sfx2/objface.hxx>
48 #include <sfx2/viewfrm.hxx>
49 #include <sfx2/bindings.hxx>
50 #include <sfx2/docfilt.hxx>
51 #include <sfx2/fcontnr.hxx>
52 #include <sfx2/request.hxx>
53 #include <sfx2/prnmon.hxx>
54 #include <sfx2/docfile.hxx>
55 #include <editeng/fhgtitem.hxx>
56 #include <svx/srchdlg.hxx>
57 #include <svl/srchitem.hxx>
58 #include <editeng/fontitem.hxx>
59 #include <editeng/flstitem.hxx>
60 #include <editeng/unolingu.hxx>
61 #include <sfx2/sfxhtml.hxx>
62 #include <swtypes.hxx>
63 #include <swmodule.hxx>
64 #include <docsh.hxx>
65 #include <wdocsh.hxx>
66 #include <srcview.hxx>
67 #include "viewfunc.hxx"
68 #include <doc.hxx>
70 #include <IDocumentState.hxx>
71 #include <sfx2/msg.hxx>
72 #include <shellio.hxx>
73 
74 #include <cmdid.h>
75 #include <globals.hrc>
76 #include <strings.hrc>
77 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
78 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
79 #include <sfx2/filedlghelper.hxx>
80 #define ShellClass_SwSrcView
81 #include <swslots.hxx>
82 
83 #include <unomid.h>
84 
85 #include <com/sun/star/document/XDocumentProperties.hpp>
86 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
87 
88 using namespace ::com::sun::star;
89 using namespace ::com::sun::star::i18n;
90 using namespace ::com::sun::star::lang;
91 using namespace ::com::sun::star::uno;
92 using namespace ::com::sun::star::ui::dialogs;
93 using namespace ::sfx2;
94 
95 #define SWSRCVIEWFLAGS SfxViewShellFlags::NO_NEWWINDOW
96 
97 #define SRC_SEARCHOPTIONS (SearchOptionFlags::ALL & ~SearchOptionFlags(SearchOptionFlags::FORMAT|SearchOptionFlags::FAMILIES|SearchOptionFlags::SEARCHALL))
98 
99 // Printing margins -> like Basic - Ide
100 #define LMARGPRN 1700
101 #define RMARGPRN 900
102 #define TMARGPRN 2000
103 #define BMARGPRN 1000
104 #define BORDERPRN 300
105 
107 {
109 }
110 
112 
113 void SwSrcView::InitInterface_Impl()
114 {
115  GetStaticInterface()->RegisterPopupMenu("source");
116 
117  GetStaticInterface()->RegisterObjectBar(SFX_OBJECTBAR_TOOLS,
118  SfxVisibilityFlags::Standard|SfxVisibilityFlags::Server,
119  ToolbarId::Webtools_Toolbox);
120 
121  GetStaticInterface()->RegisterChildWindow(SvxSearchDialogWrapper::GetChildWindowId());
122 }
123 
124 
125 static void lcl_PrintHeader( vcl::RenderContext &rOutDev, sal_Int32 nPages, sal_Int32 nCurPage, const OUString& rTitle )
126 {
127  short nLeftMargin = LMARGPRN;
128  Size aSz = rOutDev.GetOutputSize();
129  short nBorder = BORDERPRN;
130 
131  Color aOldFillColor( rOutDev.GetFillColor() );
132  vcl::Font aOldFont( rOutDev.GetFont() );
133 
134  rOutDev.SetFillColor( COL_TRANSPARENT );
135 
136  vcl::Font aFont( aOldFont );
137  aFont.SetWeight( WEIGHT_BOLD );
138  aFont.SetAlignment( ALIGN_BOTTOM );
139  rOutDev.SetFont( aFont );
140 
141  long nFontHeight = rOutDev.GetTextHeight();
142 
143  // 1.Border => Line, 2+3 Border = Space.
144  long nYTop = TMARGPRN-3*nBorder-nFontHeight;
145 
146  long nXLeft = nLeftMargin-nBorder;
147  long nXRight = aSz.Width()-RMARGPRN+nBorder;
148 
149  rOutDev.DrawRect( tools::Rectangle(
150  Point( nXLeft, nYTop ),
151  Size( nXRight-nXLeft, aSz.Height() - nYTop - BMARGPRN + nBorder ) ) );
152 
153  long nY = TMARGPRN-2*nBorder;
154  Point aPos( nLeftMargin, nY );
155  rOutDev.DrawText( aPos, rTitle );
156  if ( nPages != 1 )
157  {
158  aFont.SetWeight( WEIGHT_NORMAL );
159  rOutDev.SetFont( aFont );
160  OUString aPageStr = " [" + SwResId( STR_PAGE ) + " " + OUString::number( nCurPage ) + "]";
161  aPos.AdjustX(rOutDev.GetTextWidth( rTitle ) );
162  rOutDev.DrawText( aPos, aPageStr );
163  }
164 
165  nY = TMARGPRN-nBorder;
166 
167  rOutDev.DrawLine( Point( nXLeft, nY ), Point( nXRight, nY ) );
168 
169  rOutDev.SetFont( aOldFont );
170  rOutDev.SetFillColor( aOldFillColor );
171 }
172 
173 static rtl_TextEncoding lcl_GetStreamCharSet(rtl_TextEncoding eLoadEncoding)
174 {
175  rtl_TextEncoding eRet = eLoadEncoding;
176  if(RTL_TEXTENCODING_DONTKNOW == eRet)
177  {
178  SvxHtmlOptions& rHtmlOptions = SvxHtmlOptions::Get();
179  const sal_Char *pCharSet =
180  rtl_getBestMimeCharsetFromTextEncoding( rHtmlOptions.GetTextEncoding() );
181  eRet = rtl_getTextEncodingFromMimeCharset( pCharSet );
182  }
183  return eRet;
184 }
185 
186 static OUString lcl_ConvertTabsToSpaces( const OUString& sLine )
187 {
188  if (sLine.isEmpty())
189  return sLine;
190 
191  OUString aRet = sLine;
192  const sal_Unicode aPadSpaces[4] = {' ', ' ', ' ', ' '};
193  sal_Int32 nPos = 0;
194  for (;;)
195  {
196  nPos = aRet.indexOf('\t', nPos);
197  if (nPos<0)
198  {
199  break;
200  }
201  // Not 4 blanks, but on 4th TabPos:
202  const sal_Int32 nPadLen = 4 - (nPos % 4);
203  aRet = aRet.replaceAt(nPos, 1, OUString(aPadSpaces, nPadLen));
204  nPos += nPadLen;
205  }
206  return aRet;
207 }
208 
210  SfxViewShell( pViewFrame, SWSRCVIEWFLAGS ),
211  aEditWin( VclPtr<SwSrcEditWindow>::Create( &pViewFrame->GetWindow(), this ) ),
212  bSourceSaved(false),
213  eLoadEncoding(RTL_TEXTENCODING_DONTKNOW)
214 {
215  Init();
216 }
217 
219 {
220  SwDocShell* pDocShell = GetDocShell();
221  OSL_ENSURE(dynamic_cast<SwWebDocShell*>( pDocShell), "Why no WebDocShell?" );
222  const TextSelection& rSel = aEditWin->GetTextView()->GetSelection();
223  static_cast<SwWebDocShell*>(pDocShell)->SetSourcePara( static_cast< sal_uInt16 >( rSel.GetStart().GetPara() ) );
224 
225  uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
226  pDocShell->GetModel(), uno::UNO_QUERY_THROW);
227  uno::Reference<document::XDocumentProperties> xDocProps
228  = xDPS->getDocumentProperties();
229  OUString url = xDocProps->getAutoloadURL();
230  sal_Int32 delay = xDocProps->getAutoloadSecs();
231  pDocShell->SetAutoLoad(INetURLObject(url), delay,
232  (delay != 0) || !url.isEmpty());
233  EndListening(*pDocShell);
234  pSearchItem.reset();
235 
237 }
238 
240 {
241  SvStream* pOutStream = rMed.GetOutStream();
243  aEditWin->Write( *pOutStream );
244 }
245 
247 {
248  SetName("Source");
249  SetWindow( aEditWin.get() );
250  SwDocShell* pDocShell = GetDocShell();
251  // If the doc is still loading, then the DocShell must fire up
252  // the Load if the loading is completed.
253  if(!pDocShell->IsLoading())
254  Load(pDocShell);
255  else
256  {
257  aEditWin->SetReadonly(true);
258  }
259 
260  SetNewWindowAllowed( false );
261  StartListening(*pDocShell, DuplicateHandling::Prevent);
262 }
263 
265 {
266  SfxObjectShell* pObjShell = GetViewFrame()->GetObjectShell();
267  return dynamic_cast<SwDocShell*>( pObjShell );
268 }
269 
270 void SwSrcView::SaveContent(const OUString& rTmpFile)
271 {
272  SfxMedium aMedium( rTmpFile, StreamMode::WRITE);
273  SvStream* pOutStream = aMedium.GetOutStream();
275  aEditWin->Write(*pOutStream);
276  aMedium.Commit();
277 }
278 
280 {
281  TextView* pTextView = aEditWin->GetTextView();
282  switch( rReq.GetSlot() )
283  {
284  case SID_SAVEACOPY:
285  case SID_SAVEASDOC:
286  {
287  SvtPathOptions aPathOpt;
288  // filesave dialog with autoextension
289  FileDialogHelper aDlgHelper(
290  TemplateDescription::FILESAVE_AUTOEXTENSION,
291  FileDialogFlags::NONE, aEditWin->GetFrameWeld());
292  uno::Reference < XFilePicker3 > xFP = aDlgHelper.GetFilePicker();
293 
294  // search for an html filter for export
296  std::shared_ptr<const SfxFilter> pFilter =
297  pFilterCont->GetFilter4Extension( "html", SfxFilterFlags::EXPORT );
298  if ( pFilter )
299  {
300  // filter found -> use its uiname and wildcard
301  const OUString& rUIName = pFilter->GetUIName();
302  const WildCard& rCard = pFilter->GetWildcard();
303  xFP->appendFilter( rUIName, rCard.getGlob() );
304  xFP->setCurrentFilter( rUIName ) ;
305  }
306  else
307  {
308  // filter not found
309  OUString sHtml("HTML");
310  xFP->appendFilter( sHtml, "*.html;*.htm" );
311  xFP->setCurrentFilter( sHtml ) ;
312  }
313 
314  xFP->setDisplayDirectory( aPathOpt.GetWorkPath() );
315  if( aDlgHelper.Execute() == ERRCODE_NONE)
316  {
317  SfxMedium aMedium( xFP->getSelectedFiles().getConstArray()[0],
318  StreamMode::WRITE | StreamMode::SHARE_DENYNONE );
319  SvStream* pOutStream = aMedium.GetOutStream();
321  aEditWin->Write( *pOutStream );
322  aMedium.Commit();
323  }
324  }
325  break;
326  case SID_SAVEDOC:
327  {
328  SwDocShell* pDocShell = GetDocShell();
329  assert(pDocShell);
330  SfxMedium* pMed = nullptr;
331  if(pDocShell->HasName())
332  pMed = pDocShell->GetMedium();
333  else
334  {
335  const SfxBoolItem* pItem = static_cast<const SfxBoolItem*>(pDocShell->ExecuteSlot(rReq, pDocShell->GetInterface()));
336  if(pItem && pItem->GetValue())
337  pMed = pDocShell->GetMedium();
338  }
339  if(pMed)
340  {
341  SvStream* pOutStream = pMed->GetOutStream();
342  pOutStream->Seek(0);
343  pOutStream->SetStreamSize(0);
345  aEditWin->Write( *pOutStream );
346  pMed->CloseOutStream();
347  pMed->Commit();
348  pDocShell->GetDoc()->getIDocumentState().ResetModified();
349  bSourceSaved = true;
351  }
352  }
353  break;
354  case FID_SEARCH_NOW:
355  {
356  const SfxItemSet* pTmpArgs = rReq.GetArgs();
357 
358  const sal_uInt16 nWhich = pTmpArgs->GetWhichByPos( 0 );
359  OSL_ENSURE( nWhich, "Which for SearchItem ?" );
360  const SfxPoolItem& rItem = pTmpArgs->Get( nWhich );
361  SetSearchItem( static_cast<const SvxSearchItem&>(rItem));
362  StartSearchAndReplace( static_cast<const SvxSearchItem&>(rItem), rReq.IsAPI() );
363  if(aEditWin->IsModified())
364  {
365  SwDocShell* pDocShell = GetDocShell();
366  assert(pDocShell);
367  pDocShell->GetDoc()->getIDocumentState().SetModified();
368  }
369  }
370  break;
371  case FN_REPEAT_SEARCH:
372  {
373  SvxSearchItem* pSrchItem = GetSearchItem();
374  if(pSrchItem)
375  {
376  StartSearchAndReplace( *pSrchItem, rReq.IsAPI() );
377  if(aEditWin->IsModified())
379  }
380  }
381  break;
382  case SID_PRINTDOC:
383  case SID_PRINTDOCDIRECT:
384  {
386  }
387  break;
388  case SID_UNDO:
389  pTextView->Undo();
391  break;
392  case SID_REDO:
393  pTextView->Redo();
395  break;
396  case SID_REPEAT:
397  break;
398  case SID_CUT:
399  if(pTextView->HasSelection())
400  pTextView->Cut();
401  break;
402  case SID_COPY:
403  if(pTextView->HasSelection())
404  pTextView->Copy();
405  break;
406  case SID_PASTE:
407  pTextView->Paste();
408  break;
409  case SID_SELECTALL:
411  break;
412  }
413  aEditWin->Invalidate();
414 }
415 
417 {
418  SfxWhichIter aIter(rSet);
419  sal_uInt16 nWhich = aIter.FirstWhich();
420  TextView* pTextView = aEditWin->GetTextView();
421 
422  while(nWhich)
423  {
424  switch(nWhich)
425  {
426  case SID_SAVEASDOC:
427  rSet.Put(SfxStringItem(nWhich, SwResId(STR_SAVEAS_SRC)));
428  break;
429  case SID_SAVEACOPY:
430  rSet.Put(SfxStringItem(nWhich, SwResId(STR_SAVEACOPY_SRC)));
431  break;
432  case SID_SAVEDOC:
433  {
434  SwDocShell* pDocShell = GetDocShell();
435  assert(pDocShell);
436  if(!pDocShell->IsModified())
437  rSet.DisableItem(nWhich);
438  }
439  break;
440  case SID_PRINTDOC:
441  case SID_PRINTDOCDIRECT:
442  break;
443  case SID_TABLE_CELL:
444  {
445  OUString aPos( SwResId(STR_SRCVIEW_ROW) );
446  TextSelection aSel = pTextView->GetSelection();
447  aPos += OUString::number( aSel.GetEnd().GetPara()+1 );
448  aPos += " : " +
449  SwResId(STR_SRCVIEW_COL);
450  aPos += OUString::number( aSel.GetEnd().GetIndex()+1 );
451  SfxStringItem aItem( nWhich, aPos );
452  rSet.Put( aItem );
453  }
454  break;
455  case SID_SEARCH_OPTIONS:
456  {
458  SwDocShell* pDocShell = GetDocShell();
459  assert(pDocShell);
460  if (pDocShell->IsReadOnly())
461  nOpt &= ~SearchOptionFlags(SearchOptionFlags::REPLACE|SearchOptionFlags::REPLACE_ALL);
462 
463  rSet.Put( SfxUInt16Item( SID_SEARCH_OPTIONS, static_cast<sal_uInt16>(nOpt) ) );
464  }
465  break;
466  case SID_SEARCH_ITEM:
467  {
468  OUString sSelected;
469  if ( !pTextView->HasSelection() )
470  {
471  const TextSelection& rSel = pTextView->GetSelection();
472  sSelected = aEditWin->GetTextEngine()->GetWord( rSel.GetStart());
473  }
474  else
475  {
476  sSelected = pTextView->GetSelected();
477  }
478  SvxSearchItem * pSrchItem = GetSearchItem();
479  pSrchItem->SetSearchString( sSelected );
480  rSet.Put( *pSrchItem );
481  }
482  break;
483  case FN_REPEAT_SEARCH:
484  {
485  if(!GetSearchItem())
486  rSet.DisableItem(nWhich);
487  }
488  break;
489  case SID_UNDO:
490  case SID_REDO:
491  {
492  SfxUndoManager& rMgr = pTextView->GetTextEngine()->GetUndoManager();
493  sal_uInt16 nCount = 0;
494  if(nWhich == SID_UNDO)
495  {
496  nCount = rMgr.GetUndoActionCount();
497  if(nCount)
498  {
499  OUString aStr(SvtResId( STR_UNDO));
500  aStr += rMgr.GetUndoActionComment(--nCount);
501  rSet.Put(SfxStringItem(nWhich, aStr));
502  }
503  else
504  rSet.DisableItem(nWhich);
505  }
506  else
507  {
508  nCount = rMgr.GetRedoActionCount();
509  if(nCount)
510  {
511  OUString aStr(SvtResId( STR_REDO));
512  aStr += rMgr.GetRedoActionComment(--nCount);
513  rSet.Put(SfxStringItem(nWhich,aStr));
514  }
515  else
516  rSet.DisableItem(nWhich);
517  }
518  }
519  break;
520  case SID_MAIL_SENDDOCASPDF:
521  case SID_MAIL_SENDDOC :
522  case SID_EXPORTDOCASPDF:
523  case SID_DIRECTEXPORTDOCASPDF:
524  case SID_EXPORTDOC:
525  case SID_REPEAT:
526  case SID_BROWSER_MODE:
527  case FN_PRINT_LAYOUT:
528  rSet.DisableItem(nWhich);
529  break;
530  case SID_CUT:
531  case SID_COPY:
532  if(!pTextView->HasSelection())
533  rSet.DisableItem(nWhich);
534  break;
535  case SID_PASTE:
536  {
537  TransferableDataHelper aDataHelper(
539  aEditWin.get()) );
540  bool bDisable = !aDataHelper.GetXTransferable().is() ||
541  0 == aDataHelper.GetFormatCount();
542  if( bDisable )
543  rSet.DisableItem(nWhich);
544  }
545  break;
546  }
547  nWhich = aIter.NextWhich();
548  }
549 }
550 
552 {
553  if(!pSearchItem)
554  {
555  pSearchItem.reset(new SvxSearchItem(SID_SEARCH_ITEM));
556  }
557  return pSearchItem.get();
558 }
559 
561 {
562  pSearchItem.reset(static_cast<SvxSearchItem*>(rItem.Clone()));
563 }
564 
566  bool bApi,
567  bool bRecursive)
568 {
569  TextView* pTextView = aEditWin->GetTextView();
570  TextPaM aPaM;
571 
572  bool bForward = !rSearchItem.GetBackward();
573  bool bAtStart = pTextView->GetSelection() == TextSelection( aPaM, aPaM );
574 
575  if( !bForward )
577 
578  i18nutil::SearchOptions2 aSearchOpt( rSearchItem.GetSearchOptions() );
579  aSearchOpt.Locale = GetAppLanguageTag().getLocale();
580 
581  sal_uInt16 nFound;
582  bool bAll = false;
583  switch( rSearchItem.GetCommand() )
584  {
585  case SvxSearchCmd::FIND:
586  case SvxSearchCmd::FIND_ALL:
587  nFound = pTextView->Search( aSearchOpt, bForward ) ? 1 : 0;
588  break;
589 
590  case SvxSearchCmd::REPLACE_ALL: bAll = true;
591  [[fallthrough]];
592  case SvxSearchCmd::REPLACE:
593  nFound = pTextView->Replace( aSearchOpt, bAll, bForward );
594  break;
595 
596  default:
597  nFound = 0;
598  }
599 
600  if( !nFound )
601  {
602  bool bNotFoundMessage = false;
603  if(!bRecursive)
604  {
605  bNotFoundMessage = bAtStart;
606  }
607  else if(bAtStart)
608  {
609  bNotFoundMessage = true;
610  }
611 
612  if(!bApi)
613  {
614  if(bNotFoundMessage)
615  {
616  std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(nullptr, "modules/swriter/ui/infonotfounddialog.ui"));
617  std::unique_ptr<weld::MessageDialog> xInfoBox(xBuilder->weld_message_dialog("InfoNotFoundDialog"));
618  xInfoBox->run();
619  }
620  else if(!bRecursive)
621  {
622  int nRet;
623 
624  if (!bForward)
625  {
626  std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(nullptr, "modules/swriter/ui/querycontinueenddialog.ui"));
627  std::unique_ptr<weld::MessageDialog> xQueryBox(xBuilder->weld_message_dialog("QueryContinueEndDialog"));
628  nRet = xQueryBox->run();
629  }
630  else
631  {
632  std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(nullptr, "modules/swriter/ui/querycontinuebegindialog.ui"));
633  std::unique_ptr<weld::MessageDialog> xQueryBox(xBuilder->weld_message_dialog("QueryContinueBeginDialog"));
634  nRet = xQueryBox->run();
635  }
636 
637  if (nRet == RET_YES)
638  {
639  pTextView->SetSelection( TextSelection( aPaM, aPaM ) );
640  StartSearchAndReplace( rSearchItem, false, true );
641  }
642  }
643  }
644  }
645 }
646 
648 {
649  SwDocShell* pDocSh = GetDocShell();
650  assert(pDocSh);
651  if ( (SfxPrinterChangeFlags::JOBSETUP | SfxPrinterChangeFlags::PRINTER) & nDiffFlags )
652  {
653  pDocSh->GetDoc()->getIDocumentDeviceAccess().setPrinter( pNew, true, true );
654  if ( nDiffFlags & SfxPrinterChangeFlags::PRINTER )
655  pDocSh->SetModified();
656  }
657  if ( nDiffFlags & SfxPrinterChangeFlags::OPTIONS )
658  ::SetPrinter( &pDocSh->getIDocumentDeviceAccess(), pNew, true );
659 
660  const bool bChgOri = bool(nDiffFlags & SfxPrinterChangeFlags::CHG_ORIENTATION);
661  const bool bChgSize = bool(nDiffFlags & SfxPrinterChangeFlags::CHG_SIZE);
662  if ( bChgOri || bChgSize )
663  {
664  pDocSh->SetModified();
665  }
666  return 0;
667 }
668 
670 {
671  SwDocShell* pDocSh = GetDocShell();
672  assert(pDocSh);
673  return pDocSh->GetDoc()->getIDocumentDeviceAccess().getPrinter(bCreate);
674 }
675 
677  OutputDevice *pOutDev,
678  sal_Int32 nPage,
679  bool bCalcNumPagesOnly )
680 {
681  if (!pOutDev || nPage <= 0)
682  return 0;
683 
688 
689  pOutDev->Push();
690 
691  TextEngine* pTextEngine = aEditWin->GetTextEngine();
692  pOutDev->SetMapMode(MapMode(MapUnit::Map100thMM));
693  vcl::Font aFont( aEditWin->GetOutWin()->GetFont() );
694  Size aSize( aFont.GetFontSize() );
695  aSize = aEditWin->GetOutWin()->PixelToLogic(aSize, MapMode(MapUnit::Map100thMM));
696  aFont.SetFontSize( aSize );
697  aFont.SetColor( COL_BLACK );
698  pOutDev->SetFont( aFont );
699 
700  OUString aTitle( GetViewFrame()->GetWindow().GetText() );
701 
702  const long nLineHeight = pOutDev->GetTextHeight(); // slightly more
703  const long nParaSpace = 10;
704 
705  Size aPaperSz = pOutDev->GetOutputSize();
706  aPaperSz.AdjustWidth( -(LMARGPRN + RMARGPRN) );
707  aPaperSz.AdjustHeight( -(TMARGPRN + BMARGPRN) );
708 
709  // nLinepPage is not true, if lines have to be wrapped...
710  const long nLinespPage = nLineHeight ? aPaperSz.Height() / nLineHeight : 1;
711  const long nCharWidth = pOutDev->GetTextWidth("X");
712  const sal_Int32 nCharspLine = nCharWidth ? static_cast<sal_Int32>(aPaperSz.Width() / nCharWidth) : 1;
713  const sal_uInt32 nParas = pTextEngine->GetParagraphCount();
714 
715  const sal_Int32 nPages = static_cast<sal_Int32>(nParas / nLinespPage + 1 );
716  sal_Int32 nCurPage = 1;
717 
718  // Print header...
719  if (!bCalcNumPagesOnly && nPage == nCurPage)
720  lcl_PrintHeader( *pOutDev, nPages, nCurPage, aTitle );
721  const Point aStartPos( LMARGPRN, TMARGPRN );
722  Point aPos( aStartPos );
723  for ( sal_uInt32 nPara = 0; nPara < nParas; ++nPara )
724  {
725  const OUString aLine( lcl_ConvertTabsToSpaces(pTextEngine->GetText( nPara )) );
726  const sal_Int32 nLineLen = aLine.getLength();
727  const sal_Int32 nLines = (nLineLen+nCharspLine-1) / nCharspLine;
728  for ( sal_Int32 nLine = 0; nLine < nLines; ++nLine )
729  {
730  aPos.AdjustY(nLineHeight );
731  if ( aPos.Y() > ( aPaperSz.Height() + TMARGPRN - nLineHeight/2 ) )
732  {
733  ++nCurPage;
734  if (!bCalcNumPagesOnly && nPage == nCurPage)
735  lcl_PrintHeader( *pOutDev, nPages, nCurPage, aTitle );
736  aPos = aStartPos;
737  }
738  if (!bCalcNumPagesOnly && nPage == nCurPage)
739  {
740  const sal_Int32 nStart = nLine * nCharspLine;
741  const sal_Int32 nLen = std::min(nLineLen-nStart, nCharspLine);
742  pOutDev->DrawText( aPos, aLine.copy(nStart, nLen) );
743  }
744  }
745  aPos.AdjustY(nParaSpace );
746  }
747 
748  pOutDev->Pop();
749 
750  OSL_ENSURE( bCalcNumPagesOnly || nPage <= nCurPage, "page number out of range" );
751  return nCurPage;
752 }
753 
754 void SwSrcView::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
755 {
756  if (rHint.GetId() == SfxHintId::ModeChanged || rHint.GetId() == SfxHintId::TitleChanged)
757  {
758  const SwDocShell* pDocSh = GetDocShell();
759  assert(pDocSh);
760  if (!(rHint.GetId() == SfxHintId::TitleChanged
761  && (pDocSh->IsReadOnly() || !aEditWin->IsReadonly())))
762  {
763  // Broadcast only comes once!
764  const bool bReadonly = pDocSh->IsReadOnly();
765  aEditWin->SetReadonly(bReadonly);
766  }
767  }
768  SfxViewShell::Notify(rBC, rHint);
769 }
770 
771 void SwSrcView::Load(SwDocShell* pDocShell)
772 {
773  SvxHtmlOptions& rHtmlOptions = SvxHtmlOptions::Get();
774  const sal_Char *pCharSet =
775  rtl_getBestMimeCharsetFromTextEncoding( rHtmlOptions.GetTextEncoding() );
776  rtl_TextEncoding eDestEnc = rtl_getTextEncodingFromMimeCharset( pCharSet );
777 
778  aEditWin->SetReadonly(pDocShell->IsReadOnly());
779  aEditWin->SetTextEncoding(eDestEnc);
780  SfxMedium* pMedium = pDocShell->GetMedium();
781 
782  std::shared_ptr<const SfxFilter> pFilter = pMedium->GetFilter();
783  bool bHtml = pFilter && pFilter->GetUserData() == "HTML";
784  bool bDocModified = pDocShell->IsModified();
785  if(bHtml && !bDocModified && pDocShell->HasName())
786  {
787  SvStream* pStream = pMedium->GetInStream();
788  if(pStream && ERRCODE_NONE == pStream->GetError() )
789  {
790  rtl_TextEncoding eHeaderEnc =
791  SfxHTMLParser::GetEncodingByHttpHeader(
792  pDocShell->GetHeaderAttributes() );
793  if( RTL_TEXTENCODING_DONTKNOW == eHeaderEnc )
794  {
795  const sal_Char *pTmpCharSet =
796  rtl_getBestMimeCharsetFromTextEncoding( RTL_TEXTENCODING_ISO_8859_1 );
797  eHeaderEnc = rtl_getTextEncodingFromMimeCharset( pTmpCharSet );
798  }
799  if( RTL_TEXTENCODING_DONTKNOW != eHeaderEnc &&
800  eDestEnc != eHeaderEnc )
801  {
802  eDestEnc = eHeaderEnc;
803  aEditWin->SetTextEncoding(eDestEnc);
804  }
805  pStream->SetStreamCharSet( eDestEnc );
806  pStream->Seek(0);
807  TextEngine* pTextEngine = aEditWin->GetTextEngine();
808  pTextEngine->EnableUndo(false);
809  aEditWin->Read(*pStream);
810  pTextEngine->EnableUndo(true);
811  }
812  else
813  {
814  vcl::Window& rTmpWindow = GetViewFrame()->GetWindow();
815  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(rTmpWindow.GetFrameWeld(),
816  VclMessageType::Info, VclButtonsType::Ok,
817  SwResId(STR_ERR_SRCSTREAM)));
818  xBox->run();
819  }
820  }
821  else
822  {
823  utl::TempFile aTempFile;
824  aTempFile.EnableKillingFile();
825  const OUString sFileURL( aTempFile.GetURL() );
826  SvtSaveOptions aOpt;
827 
828  {
829  SfxMedium aMedium( sFileURL,StreamMode::READWRITE );
830  SwWriter aWriter( aMedium, *pDocShell->GetDoc() );
831  WriterRef xWriter;
832  ::GetHTMLWriter(OUString(), aMedium.GetBaseURL( true ), xWriter);
833  const OUString sWriteName = pDocShell->HasName()
834  ? pMedium->GetName()
835  : sFileURL;
836  ErrCode nRes = aWriter.Write(xWriter, &sWriteName);
837  if(nRes)
838  {
840  aEditWin->SetReadonly(true);
841  }
842  aMedium.Commit();
843  SvStream* pInStream = aMedium.GetInStream();
844  pInStream->Seek(0);
845  pInStream->SetStreamCharSet( eDestEnc );
846 
847  aEditWin->Read(*pInStream);
848  }
849  }
851 
852  eLoadEncoding = eDestEnc;
853 
854  if(bDocModified)
855  pDocShell->SetModified();// The flag will be reset in between times.
856  // Disable AutoLoad
857  pDocShell->SetAutoLoad(INetURLObject(), 0, false);
858  OSL_ENSURE(dynamic_cast<SwWebDocShell*>( pDocShell), "Why no WebDocShell?" );
859  sal_uInt16 nLine = static_cast<SwWebDocShell*>(pDocShell)->GetSourcePara();
860  aEditWin->SetStartLine(nLine);
863 }
864 
865 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SfxPoolItem * ExecuteSlot(SfxRequest &rReq, const SfxInterface *pIF=nullptr)
SfxViewFrame * GetViewFrame() const
SFX_IMPL_SUPERCLASS_INTERFACE(DrawDocShell, SfxObjectShell)
bool GetValue() const
long Width() const
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, MetricVector *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
void Init()
Definition: srcview.cxx:246
bool SetStreamSize(sal_uInt64 nSize)
TextEngine * GetTextEngine()
Definition: srcedtw.hxx:125
virtual SfxObjectFactory & GetFactory() const =0
OUString SvtResId(const char *pId)
SvKeyValueIterator * GetHeaderAttributes()
virtual void SetModified(bool=true) override
Definition: docsh2.cxx:1397
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
void SaveContent(const OUString &rTmpFile)
Definition: srcview.cxx:270
long AdjustWidth(long n)
long AdjustX(long nHorzMove)
void SetReadonly(bool bSet)
Definition: srcedtw.hxx:138
IDocumentDeviceAccess const & getIDocumentDeviceAccess() const
Definition: doc.cxx:237
long Height() const
sal_uInt16 Replace(const i18nutil::SearchOptions &rSearchOptions, bool bAll, bool bForward)
#define RMARGPRN
Definition: srcview.cxx:101
OUString GetText(LineEnd aSeparator=LINEEND_LF) const
void SetSearchString(const OUString &rNewString)
const OUString & GetName() const
virtual void SetModified()=0
Must be called manually at changes of format.
void disposeAndClear()
virtual sal_uInt16 SetPrinter(SfxPrinter *pNew, SfxPrinterChangeFlags nDiff=SFX_PRINTER_ALL) override
Definition: srcview.cxx:647
void SetWeight(FontWeight)
bool Commit()
sal_uInt16 FirstWhich()
const TextPaM & GetEnd() const
sal_uInt64 Seek(sal_uInt64 nPos)
std::unique_ptr< SvxSearchItem > pSearchItem
Definition: srcview.hxx:37
css::uno::Reference< css::frame::XModel > GetModel() const
const SfxItemSet * GetArgs() const
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const override
virtual SfxObjectShell * GetObjectShell() override
TextViewOutWin * GetOutWin()
Definition: srcedtw.hxx:129
long AdjustHeight(long n)
void SetMapMode()
SfxHintId GetId() const
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
#define BMARGPRN
Definition: srcview.cxx:103
sal_uInt16 NextWhich()
SvxSearchItem * GetSearchItem()
Definition: srcview.cxx:551
const i18nutil::SearchOptions2 & GetSearchOptions() const
OUString GetWord(const TextPaM &rCursorPos, TextPaM *pStartOfWord=nullptr)
WEIGHT_BOLD
void Paste()
weld::Window * GetFrameWeld() const
#define BORDERPRN
Definition: srcview.cxx:104
rtl_TextEncoding eLoadEncoding
Definition: srcview.hxx:40
sal_uInt16 sal_Unicode
constexpr::Color COL_TRANSPARENT(0xFF, 0xFF, 0xFF, 0xFF)
vcl::Window & GetWindow() const
RET_YES
ErrCode GetError() const
sal_Int32 PrintSource(OutputDevice *pOutDev, sal_Int32 nPage, bool bCalcNumPagesOnly)
Definition: srcview.cxx:676
SfxPrinterChangeFlags
const vcl::Font & GetFont() const
char sal_Char
static TransferableDataHelper CreateFromSystemClipboard(vcl::Window *pWindow)
static void lcl_PrintHeader(vcl::RenderContext &rOutDev, sal_Int32 nPages, sal_Int32 nCurPage, const OUString &rTitle)
Definition: srcview.cxx:125
void SetTextEncoding(rtl_TextEncoding eEncoding)
Definition: srcedtw.cxx:990
SvStream * GetOutStream()
static rtl_TextEncoding lcl_GetStreamCharSet(rtl_TextEncoding eLoadEncoding)
Definition: srcview.cxx:173
void Cut()
sal_uInt32 GetParagraphCount() const
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE) override
Definition: srcedtw.cxx:757
bool IsModified() const
Definition: srcedtw.hxx:135
virtual size_t GetRedoActionCount(bool const i_currentLevel=CurrentLevel) const
std::shared_ptr< const SfxFilter > GetFilter4Extension(const OUString &rExt, SfxFilterFlags nMust=SfxFilterFlags::IMPORT, SfxFilterFlags nDont=SFX_FILTER_NOTINSTALLED) const
void DrawLine(const Point &rStartPt, const Point &rEndPt)
void DrawRect(const tools::Rectangle &rRect)
css::lang::Locale Locale
static SvxHtmlOptions & Get()
bool IsModified() const
TextEngine * GetTextEngine() const
SwDoc * GetDoc()
returns Doc. But be careful!
Definition: docsh.hxx:203
void SetAlignment(FontAlign)
void StartSearchAndReplace(const SvxSearchItem &rItem, bool bApi, bool bRecursive=false)
Definition: srcview.cxx:565
void CloseOutStream()
void SetWindow(vcl::Window *pViewPort)
SvxSearchCmd GetCommand() const
long AdjustY(long nVertMove)
OUString const & GetURL() const
css::uno::Reference< css::datatransfer::XTransferable > GetXTransferable() const
TextView * GetTextView()
Definition: srcedtw.hxx:123
static DialogMask HandleError(ErrCode nId, weld::Window *pParent=nullptr, DialogMask nMask=DialogMask::MAX)
SfxBindings & GetBindings()
void SetStartLine(sal_uInt16 nLine)
Definition: srcedtw.hxx:141
Size GetOutputSize() const
void SetFillColor()
WEIGHT_NORMAL
sal_uInt32 GetFormatCount() const
void Load(SwDocShell *pDocShell)
Definition: srcview.cxx:771
OUString GetBaseURL(bool bForSaving=false)
bool bSourceSaved
Definition: srcview.hxx:39
long const nBorder
const TextPaM & GetStart() const
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:393
bool GetBackward() const
ALIGN_BOTTOM
void GetHTMLWriter(const OUString &rFilterOptions, const OUString &rBaseURL, WriterRef &xRet)
Definition: wrthtml.cxx:1556
bool HasName() const
void ResetUndo()
OUString SwResId(const char *pId)
Definition: swmodule.cxx:190
void SetName(const OUString &rName)
sal_uInt16 GetWhichByPos(sal_uInt16 nPos) const
void SetSelection(const TextSelection &rNewSel)
void SetSearchItem(const SvxSearchItem &rItem)
Definition: srcview.cxx:560
#define TEXT_PARA_ALL
virtual size_t GetUndoActionCount(bool const i_currentLevel=CurrentLevel) const
SfxUndoManager & GetUndoManager()
bool Search(const i18nutil::SearchOptions &rSearchOptions, bool bForward)
void GrabFocus()
virtual SfxPrinter * getPrinter(bool bCreate) const =0
Return the printer set at the document.
long GetTextHeight() const
#define SFX_VIEW_REGISTRATION(DocClass)
const std::shared_ptr< const SfxFilter > & GetFilter() const
virtual void setPrinter(SfxPrinter *pP, bool bDeleteOld, bool bCallPrtDataChanged)=0
Set the printer at the document.
SwSrcView(SfxViewFrame *pFrame, SfxViewShell *)
Definition: srcview.cxx:209
bool IsReadOnly() const
#define LMARGPRN
Definition: srcview.cxx:100
Point PixelToLogic(const Point &rDevicePt) const
const TextSelection & GetSelection() const
const LanguageTag & GetAppLanguageTag()
Definition: init.cxx:736
IDocumentDeviceAccess & getIDocumentDeviceAccess()
Definition: docshini.cxx:708
void Read(SvStream &rInput)
Definition: srcedtw.hxx:120
virtual SfxPrinter * GetPrinter(bool bCreate=false) override
Definition: srcview.cxx:669
const OUString & GetWorkPath() const
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
#define SFX_OBJECTBAR_TOOLS
void GetState(SfxItemSet &)
Definition: srcview.cxx:416
OUString GetUndoActionComment(size_t nNo=0, bool const i_currentLevel=CurrentLevel) const
SvStream * GetInStream()
sal_uInt16 GetSlot() const
void SaveContentTo(SfxMedium &rMed)
Definition: srcview.cxx:239
void DisableItem(sal_uInt16 nWhich)
void Redo()
OUString GetRedoActionComment(size_t nNo=0, bool const i_currentLevel=CurrentLevel) const
long const nLeftMargin
void Write(SvStream &rOutput)
Definition: srcedtw.hxx:121
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
#define ERRCODE_NONE
void SetFont(const vcl::Font &rNewFont)
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
Definition: legacyitem.cxx:34
sal_Int32 GetIndex() const
#define FN_PRINT_LAYOUT
Definition: cmdid.h:167
void SetStreamCharSet(rtl_TextEncoding eCharSet)
VclPtr< SwSrcEditWindow > aEditWin
Definition: srcview.hxx:35
void Copy()
bool HasSelection() const
vcl::Window * GetWindow() const
OUString getGlob() const
long GetTextWidth(const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, vcl::TextLayoutCache const *=nullptr, SalLayoutGlyphs const *const pLayoutCache=nullptr) const
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage)
virtual SfxInterface * GetInterface() const
virtual SfxObjectShell * GetObjectShell() override
#define FN_REPEAT_SEARCH
Definition: cmdid.h:102
void SetAutoLoad(const INetURLObject &, sal_uInt32 nTime, bool bReload)
#define TMARGPRN
Definition: srcview.cxx:102
reference_type * get() const
OUString GetSelected()
#define SRC_SEARCHOPTIONS
Definition: srcview.cxx:97
static weld::Builder * CreateBuilder(weld::Widget *pParent, const OUString &rUIFile)
void InvalidateAll(bool bWithMsg)
void Undo()
rtl_TextEncoding GetTextEncoding() const
bool IsAPI() const
static OUString lcl_ConvertTabsToSpaces(const OUString &sLine)
Definition: srcview.cxx:186
virtual ~SwSrcView() override
Definition: srcview.cxx:218
void Execute(SfxRequest &)
Definition: srcview.cxx:279
SfxFilterContainer * GetFilterContainer() const
SFX_IMPL_NAMED_VIEWFACTORY(SwSrcView,"SourceView")
Definition: srcview.cxx:106
#define TEXT_INDEX_ALL
bool IsReadonly() const
Definition: srcedtw.hxx:139
void Push(PushFlags nFlags=PushFlags::ALL)
sal_Int32 nPos
SearchOptionFlags
#define SWSRCVIEWFLAGS
Definition: srcview.cxx:95
aStr
sal_uInt32 GetPara() const
void EnableKillingFile(bool bEnable=true)
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: srcview.cxx:754
long Y() const
void ClearModifyFlag()
Definition: srcedtw.hxx:133
void SetNewWindowAllowed(bool bSet)
const Color & GetFillColor() const
void EnableUndo(bool bEnable)
virtual void ResetModified()=0
SwDocShell * GetDocShell()
Definition: srcview.cxx:264
SfxMedium * GetMedium() const