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