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