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