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