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