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 
299  // search for an html filter for export
301  std::shared_ptr<const SfxFilter> pFilter =
302  pFilterCont->GetFilter4Extension( "html", SfxFilterFlags::EXPORT );
303  if ( pFilter )
304  {
305  // filter found -> use its uiname and wildcard
306  const OUString& rUIName = pFilter->GetUIName();
307  const WildCard& rCard = pFilter->GetWildcard();
308  xFP->appendFilter( rUIName, rCard.getGlob() );
309  xFP->setCurrentFilter( rUIName ) ;
310  }
311  else
312  {
313  // filter not found
314  OUString sHtml("HTML");
315  xFP->appendFilter( sHtml, "*.html;*.htm" );
316  xFP->setCurrentFilter( sHtml ) ;
317  }
318 
319  xFP->setDisplayDirectory( aPathOpt.GetWorkPath() );
320  if( aDlgHelper.Execute() == ERRCODE_NONE)
321  {
322  SfxMedium aMedium( xFP->getSelectedFiles().getConstArray()[0],
323  StreamMode::WRITE | StreamMode::SHARE_DENYNONE );
324  SvStream* pOutStream = aMedium.GetOutStream();
326  aEditWin->Write( *pOutStream );
327  aMedium.Commit();
328  }
329  }
330  break;
331  case SID_SAVEDOC:
332  {
333  SwDocShell* pDocShell = GetDocShell();
334  assert(pDocShell);
335  SfxMedium* pMed = nullptr;
336  if(pDocShell->HasName())
337  pMed = pDocShell->GetMedium();
338  else
339  {
340  const SfxBoolItem* pItem = static_cast<const SfxBoolItem*>(pDocShell->ExecuteSlot(rReq, pDocShell->GetInterface()));
341  if(pItem && pItem->GetValue())
342  pMed = pDocShell->GetMedium();
343  }
344  if(pMed)
345  {
346  SvStream* pOutStream = pMed->GetOutStream();
347  pOutStream->Seek(0);
348  pOutStream->SetStreamSize(0);
350  aEditWin->Write( *pOutStream );
351  pMed->CloseOutStream();
352  pMed->Commit();
353  pDocShell->GetDoc()->getIDocumentState().ResetModified();
354  bSourceSaved = true;
356  }
357  }
358  break;
359  case FID_SEARCH_NOW:
360  {
361  const SfxItemSet* pTmpArgs = rReq.GetArgs();
362 
363  const sal_uInt16 nWhich = pTmpArgs->GetWhichByPos( 0 );
364  OSL_ENSURE( nWhich, "Which for SearchItem ?" );
365  const SfxPoolItem& rItem = pTmpArgs->Get( nWhich );
366  SetSearchItem( static_cast<const SvxSearchItem&>(rItem));
367  StartSearchAndReplace( static_cast<const SvxSearchItem&>(rItem), rReq.IsAPI() );
368  if(aEditWin->IsModified())
369  {
370  SwDocShell* pDocShell = GetDocShell();
371  assert(pDocShell);
372  pDocShell->GetDoc()->getIDocumentState().SetModified();
373  }
374  }
375  break;
376  case FN_REPEAT_SEARCH:
377  {
378  SvxSearchItem* pSrchItem = GetSearchItem();
379  if(pSrchItem)
380  {
381  StartSearchAndReplace( *pSrchItem, rReq.IsAPI() );
382  if(aEditWin->IsModified())
384  }
385  }
386  break;
387  case SID_PRINTDOC:
388  case SID_PRINTDOCDIRECT:
389  {
391  }
392  break;
393  case SID_UNDO:
394  pTextView->Undo();
396  break;
397  case SID_REDO:
398  pTextView->Redo();
400  break;
401  case SID_REPEAT:
402  break;
403  case SID_CUT:
404  if(pTextView->HasSelection())
405  pTextView->Cut();
406  break;
407  case SID_COPY:
408  if(pTextView->HasSelection())
409  pTextView->Copy();
410  break;
411  case SID_PASTE:
412  pTextView->Paste();
413  break;
414  case SID_SELECTALL:
416  break;
417  }
418  aEditWin->Invalidate();
419 }
420 
422 {
423  SfxWhichIter aIter(rSet);
424  sal_uInt16 nWhich = aIter.FirstWhich();
425  TextView* pTextView = aEditWin->GetTextView();
426 
427  while(nWhich)
428  {
429  switch(nWhich)
430  {
431  case SID_SAVEASDOC:
432  rSet.Put(SfxStringItem(nWhich, SwResId(STR_SAVEAS_SRC)));
433  break;
434  case SID_SAVEACOPY:
435  rSet.Put(SfxStringItem(nWhich, SwResId(STR_SAVEACOPY_SRC)));
436  break;
437  case SID_SAVEDOC:
438  {
439  SwDocShell* pDocShell = GetDocShell();
440  assert(pDocShell);
441  if(!pDocShell->IsModified())
442  rSet.DisableItem(nWhich);
443  }
444  break;
445  case SID_PRINTDOC:
446  case SID_PRINTDOCDIRECT:
447  break;
448  case SID_TABLE_CELL:
449  {
450  OUString aPos( SwResId(STR_SRCVIEW_ROW) );
451  TextSelection aSel = pTextView->GetSelection();
452  aPos += OUString::number( aSel.GetEnd().GetPara()+1 );
453  aPos += " : ";
454  aPos += SwResId(STR_SRCVIEW_COL);
455  aPos += OUString::number( aSel.GetEnd().GetIndex()+1 );
456  SfxStringItem aItem( nWhich, aPos );
457  rSet.Put( aItem );
458  }
459  break;
460  case SID_SEARCH_OPTIONS:
461  {
463  SwDocShell* pDocShell = GetDocShell();
464  assert(pDocShell);
465  if (pDocShell->IsReadOnly())
466  nOpt &= ~SearchOptionFlags(SearchOptionFlags::REPLACE|SearchOptionFlags::REPLACE_ALL);
467 
468  rSet.Put( SfxUInt16Item( SID_SEARCH_OPTIONS, static_cast<sal_uInt16>(nOpt) ) );
469  }
470  break;
471  case SID_SEARCH_ITEM:
472  {
473  OUString sSelected;
474  if ( !pTextView->HasSelection() )
475  {
476  const TextSelection& rSel = pTextView->GetSelection();
477  sSelected = aEditWin->GetTextEngine()->GetWord( rSel.GetStart());
478  }
479  else
480  {
481  sSelected = pTextView->GetSelected();
482  }
483  SvxSearchItem * pSrchItem = GetSearchItem();
484  pSrchItem->SetSearchString( sSelected );
485  rSet.Put( *pSrchItem );
486  }
487  break;
488  case FN_REPEAT_SEARCH:
489  {
490  if(!GetSearchItem())
491  rSet.DisableItem(nWhich);
492  }
493  break;
494  case SID_UNDO:
495  case SID_REDO:
496  {
497  SfxUndoManager& rMgr = pTextView->GetTextEngine()->GetUndoManager();
498  sal_uInt16 nCount = 0;
499  if(nWhich == SID_UNDO)
500  {
501  nCount = rMgr.GetUndoActionCount();
502  if(nCount)
503  {
504  OUString aStr(SvtResId( STR_UNDO));
505  aStr += rMgr.GetUndoActionComment(--nCount);
506  rSet.Put(SfxStringItem(nWhich, aStr));
507  }
508  else
509  rSet.DisableItem(nWhich);
510  }
511  else
512  {
513  nCount = rMgr.GetRedoActionCount();
514  if(nCount)
515  {
516  OUString aStr(SvtResId( STR_REDO));
517  aStr += rMgr.GetRedoActionComment(--nCount);
518  rSet.Put(SfxStringItem(nWhich,aStr));
519  }
520  else
521  rSet.DisableItem(nWhich);
522  }
523  }
524  break;
525  case SID_MAIL_SENDDOCASPDF:
526  case SID_MAIL_SENDDOC :
527  case SID_EXPORTDOCASPDF:
528  case SID_DIRECTEXPORTDOCASPDF:
529  case SID_EXPORTDOC:
530  case SID_REPEAT:
531  case SID_BROWSER_MODE:
532  case FN_PRINT_LAYOUT:
533  rSet.DisableItem(nWhich);
534  break;
535  case SID_CUT:
536  case SID_COPY:
537  if(!pTextView->HasSelection())
538  rSet.DisableItem(nWhich);
539  break;
540  case SID_PASTE:
541  {
542  TransferableDataHelper aDataHelper(
544  aEditWin.get()) );
545  bool bDisable = !aDataHelper.GetXTransferable().is() ||
546  0 == aDataHelper.GetFormatCount();
547  if( bDisable )
548  rSet.DisableItem(nWhich);
549  }
550  break;
551  }
552  nWhich = aIter.NextWhich();
553  }
554 }
555 
557 {
558  if(!pSearchItem)
559  {
560  pSearchItem.reset(new SvxSearchItem(SID_SEARCH_ITEM));
561  }
562  return pSearchItem.get();
563 }
564 
566 {
567  pSearchItem.reset(static_cast<SvxSearchItem*>(rItem.Clone()));
568 }
569 
571  bool bApi,
572  bool bRecursive)
573 {
574  TextView* pTextView = aEditWin->GetTextView();
575  TextSelection aSel;
576  TextPaM aPaM;
577 
578  bool bForward = !rSearchItem.GetBackward();
579  bool bAtStart = pTextView->GetSelection() == TextSelection( aPaM, aPaM );
580 
581  if( !bForward )
583 
584  i18nutil::SearchOptions2 aSearchOpt( rSearchItem.GetSearchOptions() );
585  aSearchOpt.Locale = GetAppLanguageTag().getLocale();
586 
587  sal_uInt16 nFound;
588  bool bAll = false;
589  switch( rSearchItem.GetCommand() )
590  {
591  case SvxSearchCmd::FIND:
592  case SvxSearchCmd::FIND_ALL:
593  nFound = pTextView->Search( aSearchOpt, bForward ) ? 1 : 0;
594  break;
595 
596  case SvxSearchCmd::REPLACE_ALL: bAll = true;
597  [[fallthrough]];
598  case SvxSearchCmd::REPLACE:
599  nFound = pTextView->Replace( aSearchOpt, bAll, bForward );
600  break;
601 
602  default:
603  nFound = 0;
604  }
605 
606  if( !nFound )
607  {
608  bool bNotFoundMessage = false;
609  if(!bRecursive)
610  {
611  bNotFoundMessage = bAtStart;
612  }
613  else if(bAtStart)
614  {
615  bNotFoundMessage = true;
616  }
617 
618  if(!bApi)
619  {
620  if(bNotFoundMessage)
621  {
622  std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(nullptr, "modules/swriter/ui/infonotfounddialog.ui"));
623  std::unique_ptr<weld::MessageDialog> xInfoBox(xBuilder->weld_message_dialog("InfoNotFoundDialog"));
624  xInfoBox->run();
625  }
626  else if(!bRecursive)
627  {
628  int nRet;
629 
630  if (!bForward)
631  {
632  std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(nullptr, "modules/swriter/ui/querycontinueenddialog.ui"));
633  std::unique_ptr<weld::MessageDialog> xQueryBox(xBuilder->weld_message_dialog("QueryContinueEndDialog"));
634  nRet = xQueryBox->run();
635  }
636  else
637  {
638  std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(nullptr, "modules/swriter/ui/querycontinuebegindialog.ui"));
639  std::unique_ptr<weld::MessageDialog> xQueryBox(xBuilder->weld_message_dialog("QueryContinueBeginDialog"));
640  nRet = xQueryBox->run();
641  }
642 
643  if (nRet == RET_YES)
644  {
645  pTextView->SetSelection( TextSelection( aPaM, aPaM ) );
646  StartSearchAndReplace( rSearchItem, false, true );
647  }
648  }
649  }
650  }
651 }
652 
654 {
655  SwDocShell* pDocSh = GetDocShell();
656  assert(pDocSh);
657  if ( (SfxPrinterChangeFlags::JOBSETUP | SfxPrinterChangeFlags::PRINTER) & nDiffFlags )
658  {
659  pDocSh->GetDoc()->getIDocumentDeviceAccess().setPrinter( pNew, true, true );
660  if ( nDiffFlags & SfxPrinterChangeFlags::PRINTER )
661  pDocSh->SetModified();
662  }
663  if ( nDiffFlags & SfxPrinterChangeFlags::OPTIONS )
664  ::SetPrinter( &pDocSh->getIDocumentDeviceAccess(), pNew, true );
665 
666  const bool bChgOri = bool(nDiffFlags & SfxPrinterChangeFlags::CHG_ORIENTATION);
667  const bool bChgSize = bool(nDiffFlags & SfxPrinterChangeFlags::CHG_SIZE);
668  if ( bChgOri || bChgSize )
669  {
670  pDocSh->SetModified();
671  }
672  return 0;
673 }
674 
676 {
677  SwDocShell* pDocSh = GetDocShell();
678  assert(pDocSh);
679  return pDocSh->GetDoc()->getIDocumentDeviceAccess().getPrinter(bCreate);
680 }
681 
683  OutputDevice *pOutDev,
684  sal_Int32 nPage,
685  bool bCalcNumPagesOnly )
686 {
687  if (!pOutDev || nPage <= 0)
688  return 0;
689 
694 
695  pOutDev->Push();
696 
697  TextEngine* pTextEngine = aEditWin->GetTextEngine();
698  pOutDev->SetMapMode(MapMode(MapUnit::Map100thMM));
699  vcl::Font aFont( aEditWin->GetOutWin()->GetFont() );
700  Size aSize( aFont.GetFontSize() );
701  aSize = aEditWin->GetOutWin()->PixelToLogic(aSize, MapMode(MapUnit::Map100thMM));
702  aFont.SetFontSize( aSize );
703  aFont.SetColor( COL_BLACK );
704  pOutDev->SetFont( aFont );
705 
706  OUString aTitle( GetViewFrame()->GetWindow().GetText() );
707 
708  const long nLineHeight = pOutDev->GetTextHeight(); // slightly more
709  const long nParaSpace = 10;
710 
711  Size aPaperSz = pOutDev->GetOutputSize();
712  aPaperSz.AdjustWidth( -(LMARGPRN + RMARGPRN) );
713  aPaperSz.AdjustHeight( -(TMARGPRN + BMARGPRN) );
714 
715  // nLinepPage is not true, if lines have to be wrapped...
716  const long nLinespPage = nLineHeight ? aPaperSz.Height() / nLineHeight : 1;
717  const long nCharWidth = pOutDev->GetTextWidth("X");
718  const sal_Int32 nCharspLine = nCharWidth ? static_cast<sal_Int32>(aPaperSz.Width() / nCharWidth) : 1;
719  const sal_uInt32 nParas = pTextEngine->GetParagraphCount();
720 
721  const sal_Int32 nPages = static_cast<sal_Int32>(nParas / nLinespPage + 1 );
722  sal_Int32 nCurPage = 1;
723 
724  // Print header...
725  if (!bCalcNumPagesOnly && nPage == nCurPage)
726  lcl_PrintHeader( *pOutDev, nPages, nCurPage, aTitle );
727  const Point aStartPos( LMARGPRN, TMARGPRN );
728  Point aPos( aStartPos );
729  for ( sal_uInt32 nPara = 0; nPara < nParas; ++nPara )
730  {
731  const OUString aLine( lcl_ConvertTabsToSpaces(pTextEngine->GetText( nPara )) );
732  const sal_Int32 nLineLen = aLine.getLength();
733  const sal_Int32 nLines = (nLineLen+nCharspLine-1) / nCharspLine;
734  for ( sal_Int32 nLine = 0; nLine < nLines; ++nLine )
735  {
736  aPos.AdjustY(nLineHeight );
737  if ( aPos.Y() > ( aPaperSz.Height() + TMARGPRN - nLineHeight/2 ) )
738  {
739  ++nCurPage;
740  if (!bCalcNumPagesOnly && nPage == nCurPage)
741  lcl_PrintHeader( *pOutDev, nPages, nCurPage, aTitle );
742  aPos = aStartPos;
743  }
744  if (!bCalcNumPagesOnly && nPage == nCurPage)
745  {
746  const sal_Int32 nStart = nLine * nCharspLine;
747  const sal_Int32 nLen = std::min(nLineLen-nStart, nCharspLine);
748  pOutDev->DrawText( aPos, aLine.copy(nStart, nLen) );
749  }
750  }
751  aPos.AdjustY(nParaSpace );
752  }
753 
754  pOutDev->Pop();
755 
756  OSL_ENSURE( bCalcNumPagesOnly || nPage <= nCurPage, "page number out of range" );
757  return nCurPage;
758 }
759 
760 void SwSrcView::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
761 {
762  if (rHint.GetId() == SfxHintId::ModeChanged || rHint.GetId() == SfxHintId::TitleChanged)
763  {
764  const SwDocShell* pDocSh = GetDocShell();
765  assert(pDocSh);
766  if (!(rHint.GetId() == SfxHintId::TitleChanged
767  && (pDocSh->IsReadOnly() || !aEditWin->IsReadonly())))
768  {
769  // Broadcast only comes once!
770  const bool bReadonly = pDocSh->IsReadOnly();
771  aEditWin->SetReadonly(bReadonly);
772  }
773  }
774  SfxViewShell::Notify(rBC, rHint);
775 }
776 
777 void SwSrcView::Load(SwDocShell* pDocShell)
778 {
779  SvxHtmlOptions& rHtmlOptions = SvxHtmlOptions::Get();
780  const sal_Char *pCharSet =
781  rtl_getBestMimeCharsetFromTextEncoding( rHtmlOptions.GetTextEncoding() );
782  rtl_TextEncoding eDestEnc = rtl_getTextEncodingFromMimeCharset( pCharSet );
783 
784  aEditWin->SetReadonly(pDocShell->IsReadOnly());
785  aEditWin->SetTextEncoding(eDestEnc);
786  SfxMedium* pMedium = pDocShell->GetMedium();
787 
788  std::shared_ptr<const SfxFilter> pFilter = pMedium->GetFilter();
789  bool bHtml = pFilter && pFilter->GetUserData() == "HTML";
790  bool bDocModified = pDocShell->IsModified();
791  if(bHtml && !bDocModified && pDocShell->HasName())
792  {
793  SvStream* pStream = pMedium->GetInStream();
794  if(pStream && ERRCODE_NONE == pStream->GetError() )
795  {
796  rtl_TextEncoding eHeaderEnc =
797  SfxHTMLParser::GetEncodingByHttpHeader(
798  pDocShell->GetHeaderAttributes() );
799  if( RTL_TEXTENCODING_DONTKNOW == eHeaderEnc )
800  {
801  const sal_Char *pTmpCharSet =
802  rtl_getBestMimeCharsetFromTextEncoding( RTL_TEXTENCODING_ISO_8859_1 );
803  eHeaderEnc = rtl_getTextEncodingFromMimeCharset( pTmpCharSet );
804  }
805  if( RTL_TEXTENCODING_DONTKNOW != eHeaderEnc &&
806  eDestEnc != eHeaderEnc )
807  {
808  eDestEnc = eHeaderEnc;
809  aEditWin->SetTextEncoding(eDestEnc);
810  }
811  pStream->SetStreamCharSet( eDestEnc );
812  pStream->Seek(0);
813  TextEngine* pTextEngine = aEditWin->GetTextEngine();
814  pTextEngine->EnableUndo(false);
815  aEditWin->Read(*pStream);
816  pTextEngine->EnableUndo(true);
817  }
818  else
819  {
820  vcl::Window& rTmpWindow = GetViewFrame()->GetWindow();
821  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(rTmpWindow.GetFrameWeld(),
822  VclMessageType::Info, VclButtonsType::Ok,
823  SwResId(STR_ERR_SRCSTREAM)));
824  xBox->run();
825  }
826  }
827  else
828  {
829  utl::TempFile aTempFile;
830  aTempFile.EnableKillingFile();
831  const OUString sFileURL( aTempFile.GetURL() );
832  SvtSaveOptions aOpt;
833 
834  {
835  SfxMedium aMedium( sFileURL,StreamMode::READWRITE );
836  SwWriter aWriter( aMedium, *pDocShell->GetDoc() );
837  WriterRef xWriter;
838  ::GetHTMLWriter(OUString(), aMedium.GetBaseURL( true ), xWriter);
839  const OUString sWriteName = pDocShell->HasName()
840  ? pMedium->GetName()
841  : sFileURL;
842  ErrCode nRes = aWriter.Write(xWriter, &sWriteName);
843  if(nRes)
844  {
846  aEditWin->SetReadonly(true);
847  }
848  aMedium.Commit();
849  SvStream* pInStream = aMedium.GetInStream();
850  pInStream->Seek(0);
851  pInStream->SetStreamCharSet( eDestEnc );
852 
853  aEditWin->Read(*pInStream);
854  }
855  }
857 
858  eLoadEncoding = eDestEnc;
859 
860  if(bDocModified)
861  pDocShell->SetModified();// The flag will be reset in between times.
862  // Disable AutoLoad
863  pDocShell->SetAutoLoad(INetURLObject(), 0, false);
864  OSL_ENSURE(dynamic_cast<SwWebDocShell*>( pDocShell), "Why no WebDocShell?" );
865  sal_uInt16 nLine = static_cast<SwWebDocShell*>(pDocShell)->GetSourcePara();
866  aEditWin->SetStartLine(nLine);
869 }
870 
871 /* 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:1402
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:653
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:556
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:682
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)
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:570
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:777
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
void GetHTMLWriter(const OUString &rFilterOptions, const OUString &rBaseURL, WriterRef &xRet)
Definition: wrthtml.cxx:1558
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:565
#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:778
IDocumentDeviceAccess & getIDocumentDeviceAccess()
Definition: docshini.cxx:708
void Read(SvStream &rInput)
Definition: srcedtw.hxx:120
virtual SfxPrinter * GetPrinter(bool bCreate=false) override
Definition: srcview.cxx:675
const OUString & GetWorkPath() const
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
#define SFX_OBJECTBAR_TOOLS
void GetState(SfxItemSet &)
Definition: srcview.cxx:421
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:165
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:760
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