Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / app / docst.cxx
blobe260dd67dc6cf27df3c8f82dcf3aa2ee7c4f1c2d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_wasm_strip.h>
22 #include <memory>
24 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <com/sun/star/frame/XModel.hpp>
28 #include <comphelper/flagguard.hxx>
29 #include <o3tl/any.hxx>
30 #include <sal/log.hxx>
31 #include <osl/diagnose.h>
32 #include <hintids.hxx>
33 #include <sfx2/styledlg.hxx>
34 #include <svl/whiter.hxx>
35 #include <sfx2/tplpitem.hxx>
36 #include <sfx2/request.hxx>
37 #include <sfx2/dispatch.hxx>
38 #include <sfx2/newstyle.hxx>
39 #include <sfx2/printer.hxx>
40 #include <sfx2/viewfrm.hxx>
41 #include <svl/stritem.hxx>
42 #include <svl/ctloptions.hxx>
43 #include <sfx2/htmlmode.hxx>
44 #include <swmodule.hxx>
45 #include <fchrfmt.hxx>
46 #include <svtools/htmlcfg.hxx>
47 #include <svx/xdef.hxx>
48 #include <SwStyleNameMapper.hxx>
49 #include <SwRewriter.hxx>
50 #include <numrule.hxx>
51 #include <swundo.hxx>
52 #include <svx/drawitem.hxx>
53 #include <utility>
54 #include <view.hxx>
55 #include <wrtsh.hxx>
56 #include <docsh.hxx>
57 #include <uitool.hxx>
58 #include <cmdid.h>
59 #include <viewopt.hxx>
60 #include <doc.hxx>
61 #include <drawdoc.hxx>
62 #include <IDocumentDrawModelAccess.hxx>
63 #include <IDocumentUndoRedo.hxx>
64 #include <IDocumentSettingAccess.hxx>
65 #include <IDocumentDeviceAccess.hxx>
66 #include <IDocumentFieldsAccess.hxx>
67 #include <IDocumentState.hxx>
68 #include <frmfmt.hxx>
69 #include <charfmt.hxx>
70 #include <poolfmt.hxx>
71 #include <pagedesc.hxx>
72 #include <docstyle.hxx>
73 #include <uiitems.hxx>
74 #include <fmtcol.hxx>
75 #include <edtwin.hxx>
76 #include <unochart.hxx>
77 #include <swabstdlg.hxx>
78 #include <tblafmt.hxx>
79 #include <sfx2/watermarkitem.hxx>
80 #include <svl/grabbagitem.hxx>
81 #include <SwUndoFmt.hxx>
82 #include <strings.hrc>
83 #include <AccessibilityCheck.hxx>
84 #include <docmodel/theme/Theme.hxx>
85 #include <svx/svdpage.hxx>
87 using namespace ::com::sun::star;
89 void SwDocShell::StateStyleSheet(SfxItemSet& rSet, SwWrtShell* pSh)
91 SfxWhichIter aIter(rSet);
92 sal_uInt16 nWhich = aIter.FirstWhich();
93 SfxStyleFamily nActualFamily = SfxStyleFamily(USHRT_MAX);
95 SwWrtShell* pShell = pSh ? pSh : GetWrtShell();
96 if(!pShell)
98 while (nWhich)
100 rSet.DisableItem(nWhich);
101 nWhich = aIter.NextWhich();
103 return;
105 else
107 SfxViewFrame& rFrame = pShell->GetView().GetViewFrame();
108 std::unique_ptr<SfxUInt16Item> pFamilyItem;
109 rFrame.GetBindings().QueryState(SID_STYLE_FAMILY, pFamilyItem);
110 if (pFamilyItem)
112 nActualFamily = static_cast<SfxStyleFamily>(pFamilyItem->GetValue());
116 while (nWhich)
118 // determine current template to every family
119 OUString aName;
120 SwTableAutoFormat aTableAutoFormat("dummy"); // needed to check if can take a table auto format at current cursor position
121 switch (nWhich)
123 case SID_STYLE_APPLY:
124 {// here the template and its family are passed to the StyleBox
125 // so that this family is being showed
126 if(pShell->IsFrameSelected())
128 SwFrameFormat* pFormat = pShell->GetSelectedFrameFormat();
129 if( pFormat )
130 aName = pFormat->GetName();
132 else
134 SwTextFormatColl* pColl = pShell->GetCurTextFormatColl();
135 if(pColl)
136 aName = pColl->GetName();
138 rSet.Put(SfxTemplateItem(nWhich, aName));
140 break;
141 case SID_STYLE_FAMILY1:
142 if( !pShell->IsFrameSelected() )
144 SwCharFormat* pFormat = pShell->GetCurCharFormat();
145 if(pFormat)
146 aName = pFormat->GetName();
147 else
148 aName = SwResId(STR_POOLCHR_STANDARD);
149 rSet.Put(SfxTemplateItem(nWhich, aName));
151 break;
153 case SID_STYLE_FAMILY2:
154 if(!pShell->IsFrameSelected())
156 OUString aProgName;
157 SwTextFormatColl* pColl = pShell->GetCurTextFormatColl();
158 if(pColl)
160 aName = pColl->GetName();
161 sal_uInt16 nId = pColl->GetPoolFormatId();
162 SwStyleNameMapper::FillProgName(nId, aProgName);
165 SfxTemplateItem aItem(nWhich, aName, aProgName);
167 SfxStyleSearchBits nMask = SfxStyleSearchBits::Auto;
168 if (m_xDoc->getIDocumentSettingAccess().get(DocumentSettingId::HTML_MODE))
169 nMask = SfxStyleSearchBits::SwHtml;
170 else
172 const FrameTypeFlags nSelection = pShell->GetFrameType(nullptr,true);
173 if(pShell->GetCurTOX())
174 nMask = SfxStyleSearchBits::SwIndex ;
175 else if(nSelection & FrameTypeFlags::HEADER ||
176 nSelection & FrameTypeFlags::FOOTER ||
177 nSelection & FrameTypeFlags::TABLE ||
178 nSelection & FrameTypeFlags::FLY_ANY ||
179 nSelection & FrameTypeFlags::FOOTNOTE ||
180 nSelection & FrameTypeFlags::FTNPAGE)
181 nMask = SfxStyleSearchBits::SwExtra;
182 else
183 nMask = SfxStyleSearchBits::SwText;
186 aItem.SetValue(nMask);
187 rSet.Put(aItem);
190 break;
192 case SID_STYLE_FAMILY3:
194 if (m_xDoc->getIDocumentSettingAccess().get(DocumentSettingId::HTML_MODE))
195 rSet.DisableItem( nWhich );
196 else
198 SwFrameFormat* pFormat = pShell->GetSelectedFrameFormat();
199 if(pFormat && pShell->IsFrameSelected())
201 aName = pFormat->GetName();
202 rSet.Put(SfxTemplateItem(nWhich, aName));
205 break;
207 case SID_STYLE_FAMILY4:
209 if (m_xDoc->getIDocumentSettingAccess().get(DocumentSettingId::HTML_MODE) && !SvxHtmlOptions::IsPrintLayoutExtension())
210 rSet.DisableItem( nWhich );
211 else
213 size_t n = pShell->GetCurPageDesc( false );
214 if( n < pShell->GetPageDescCnt() )
215 aName = pShell->GetPageDesc( n ).GetName();
217 rSet.Put(SfxTemplateItem(nWhich, aName));
220 break;
221 case SID_STYLE_FAMILY5:
223 const SwNumRule* pRule = pShell->GetNumRuleAtCurrCursorPos();
224 if( pRule )
225 aName = pRule->GetName();
227 rSet.Put(SfxTemplateItem(nWhich, aName));
229 break;
230 case SID_STYLE_FAMILY6:
232 const SwTableNode *pTableNd = pShell->IsCursorInTable();
233 if( pTableNd )
234 aName = pTableNd->GetTable().GetTableStyleName();
236 rSet.Put(SfxTemplateItem(nWhich, aName));
238 break;
240 case SID_STYLE_WATERCAN:
242 SwEditWin& rEdtWin = pShell->GetView().GetEditWin();
243 SwApplyTemplate* pApply = rEdtWin.GetApplyTemplate();
244 rSet.Put(SfxBoolItem(nWhich, pApply && pApply->eType != SfxStyleFamily(0)));
246 break;
247 case SID_STYLE_UPDATE_BY_EXAMPLE:
248 if( pShell->IsFrameSelected()
249 ? SfxStyleFamily::Frame != nActualFamily
250 : ( SfxStyleFamily::Frame == nActualFamily ||
251 SfxStyleFamily::Page == nActualFamily ||
252 (SfxStyleFamily::Pseudo == nActualFamily && !pShell->GetNumRuleAtCurrCursorPos()) ||
253 (SfxStyleFamily::Table == nActualFamily && !pShell->GetTableAutoFormat(aTableAutoFormat))) )
255 rSet.DisableItem( nWhich );
257 break;
259 case SID_STYLE_NEW_BY_EXAMPLE:
260 if( (pShell->IsFrameSelected()
261 ? SfxStyleFamily::Frame != nActualFamily
262 : SfxStyleFamily::Frame == nActualFamily) ||
263 (SfxStyleFamily::Pseudo == nActualFamily && !pShell->GetNumRuleAtCurrCursorPos()) ||
264 (SfxStyleFamily::Table == nActualFamily && !pShell->GetTableAutoFormat(aTableAutoFormat)) )
266 rSet.DisableItem( nWhich );
268 break;
270 case SID_CLASSIFICATION_APPLY:
271 // Just trigger ClassificationCategoriesController::statusChanged().
272 rSet.InvalidateItem(nWhich);
273 break;
274 case SID_CLASSIFICATION_DIALOG:
275 rSet.InvalidateItem(nWhich);
276 break;
277 case SID_STYLE_EDIT:
278 break;
279 case SID_WATERMARK:
280 if (pSh)
282 SfxWatermarkItem aItem = pSh->GetWatermark();
283 rSet.Put(aItem);
285 break;
286 default:
287 OSL_FAIL("Invalid SlotId");
289 nWhich = aIter.NextWhich();
293 // evaluate StyleSheet-Requests
294 void SwDocShell::ExecStyleSheet( SfxRequest& rReq )
296 sal_uInt16 nSlot = rReq.GetSlot();
298 const SfxItemSet* pArgs = rReq.GetArgs();
299 const SfxPoolItem* pItem;
300 switch (nSlot)
302 case SID_STYLE_NEW:
303 if( pArgs && SfxItemState::SET == pArgs->GetItemState( SID_STYLE_FAMILY,
304 false, &pItem ))
306 const SfxStyleFamily nFamily = static_cast<SfxStyleFamily>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
308 OUString sName;
309 SfxStyleSearchBits nMask = SfxStyleSearchBits::Auto;
310 if( SfxItemState::SET == pArgs->GetItemState( SID_STYLE_NEW,
311 false, &pItem ))
312 sName = static_cast<const SfxStringItem*>(pItem)->GetValue();
313 if( SfxItemState::SET == pArgs->GetItemState( SID_STYLE_MASK,
314 false, &pItem ))
315 nMask = static_cast<SfxStyleSearchBits>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
316 OUString sParent;
317 if( SfxItemState::SET == pArgs->GetItemState( SID_STYLE_REFERENCE,
318 false, &pItem ))
319 sParent = static_cast<const SfxStringItem*>(pItem)->GetValue();
321 if (sName.isEmpty() && m_xBasePool)
322 sName = SfxStyleDialogController::GenerateUnusedName(*m_xBasePool, nFamily);
324 Edit(rReq.GetFrameWeld(), sName, sParent, nFamily, nMask, true, {}, nullptr, &rReq, nSlot);
326 break;
328 case SID_STYLE_APPLY:
329 if( !pArgs )
331 GetView()->GetViewFrame().GetDispatcher()->Execute(SID_STYLE_DESIGNER);
332 break;
334 else
336 // convert internal StyleName to DisplayName (slot implementation uses the latter)
337 const SfxStringItem* pNameItem = rReq.GetArg<SfxStringItem>(SID_APPLY_STYLE);
338 const SfxStringItem* pFamilyItem = rReq.GetArg<SfxStringItem>(SID_STYLE_FAMILYNAME);
339 if ( pFamilyItem && pNameItem )
341 uno::Reference< style::XStyleFamiliesSupplier > xModel(GetModel(), uno::UNO_QUERY);
344 uno::Reference< container::XNameAccess > xStyles;
345 uno::Reference< container::XNameAccess > xCont = xModel->getStyleFamilies();
346 xCont->getByName(pFamilyItem->GetValue()) >>= xStyles;
347 uno::Reference< beans::XPropertySet > xInfo;
348 xStyles->getByName( pNameItem->GetValue() ) >>= xInfo;
349 OUString aUIName;
350 xInfo->getPropertyValue("DisplayName") >>= aUIName;
351 if ( !aUIName.isEmpty() )
352 rReq.AppendItem( SfxStringItem( SID_STYLE_APPLY, aUIName ) );
354 catch (const uno::Exception&)
360 [[fallthrough]];
362 case SID_STYLE_EDIT:
363 case SID_STYLE_FONT:
364 case SID_STYLE_DELETE:
365 case SID_STYLE_HIDE:
366 case SID_STYLE_SHOW:
367 case SID_STYLE_WATERCAN:
368 case SID_STYLE_FAMILY:
369 case SID_STYLE_UPDATE_BY_EXAMPLE:
370 case SID_STYLE_NEW_BY_EXAMPLE:
372 OUString aParam;
373 SfxStyleFamily nFamily = SfxStyleFamily::Para;
374 SfxStyleSearchBits nMask = SfxStyleSearchBits::Auto;
375 SwWrtShell* pActShell = nullptr;
377 if( !pArgs )
379 switch (nSlot)
381 case SID_STYLE_NEW_BY_EXAMPLE:
383 SfxStyleSheetBasePool& rPool = *GetStyleSheetPool();
384 SfxNewStyleDlg aDlg(GetView()->GetFrameWeld(), rPool, nFamily);
385 if (aDlg.run() == RET_OK)
387 aParam = aDlg.GetName();
388 rReq.AppendItem(SfxStringItem(nSlot, aParam));
391 break;
393 case SID_STYLE_UPDATE_BY_EXAMPLE:
394 case SID_STYLE_EDIT:
396 SwTextFormatColl* pColl = GetWrtShell()->GetCurTextFormatColl();
397 if(pColl)
399 aParam = pColl->GetName();
400 rReq.AppendItem(SfxStringItem(nSlot, aParam));
403 break;
406 else
408 SAL_WARN_IF( !pArgs->Count(), "sw.ui", "SfxBug ItemSet is empty" );
410 SwWrtShell* pShell = GetWrtShell();
411 if( SfxItemState::SET == pArgs->GetItemState(nSlot, false, &pItem ))
412 aParam = static_cast<const SfxStringItem*>(pItem)->GetValue();
414 if( SfxItemState::SET == pArgs->GetItemState(SID_STYLE_FAMILY,
415 false, &pItem ))
416 nFamily = static_cast<SfxStyleFamily>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
418 if( SfxItemState::SET == pArgs->GetItemState(SID_STYLE_FAMILYNAME, false, &pItem ))
420 OUString aFamily = static_cast<const SfxStringItem*>(pItem)->GetValue();
421 if(aFamily == "CharacterStyles")
422 nFamily = SfxStyleFamily::Char;
423 else
424 if(aFamily == "ParagraphStyles")
425 nFamily = SfxStyleFamily::Para;
426 else
427 if(aFamily == "PageStyles")
428 nFamily = SfxStyleFamily::Page;
429 else
430 if(aFamily == "FrameStyles")
431 nFamily = SfxStyleFamily::Frame;
432 else
433 if(aFamily == "NumberingStyles")
434 nFamily = SfxStyleFamily::Pseudo;
435 else
436 if(aFamily == "TableStyles")
437 nFamily = SfxStyleFamily::Table;
440 if( SfxItemState::SET == pArgs->GetItemState(SID_STYLE_MASK,
441 false, &pItem ))
442 nMask = static_cast<SfxStyleSearchBits>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
443 if( const SwPtrItem* pShellItem = pArgs->GetItemIfSet(FN_PARAM_WRTSHELL, false ))
444 pActShell = pShell = static_cast<SwWrtShell*>(pShellItem->GetValue());
446 if( nSlot == SID_STYLE_UPDATE_BY_EXAMPLE && aParam.isEmpty() )
448 switch( nFamily )
450 case SfxStyleFamily::Para:
452 SwTextFormatColl* pColl = pShell->GetCurTextFormatColl();
453 if(pColl)
454 aParam = pColl->GetName();
456 break;
457 case SfxStyleFamily::Frame:
459 SwFrameFormat* pFrame = m_pWrtShell->GetSelectedFrameFormat();
460 if( pFrame )
461 aParam = pFrame->GetName();
463 break;
464 case SfxStyleFamily::Char:
466 SwCharFormat* pChar = m_pWrtShell->GetCurCharFormat();
467 if( pChar )
468 aParam = pChar->GetName();
470 break;
471 case SfxStyleFamily::Pseudo:
472 if(const SfxStringItem* pExName = pArgs->GetItemIfSet(SID_STYLE_UPD_BY_EX_NAME, false))
474 aParam = pExName->GetValue();
476 break;
477 case SfxStyleFamily::Table:
478 if(const SfxStringItem* pExName = pArgs->GetItemIfSet(SID_STYLE_UPD_BY_EX_NAME, false))
480 aParam = pExName->GetValue();
482 break;
483 default: break;
485 rReq.AppendItem(SfxStringItem(nSlot, aParam));
488 if (!aParam.isEmpty() || nSlot == SID_STYLE_WATERCAN )
490 sal_uInt16 nRet = 0xffff;
491 bool bReturns = false;
493 switch(nSlot)
495 case SID_STYLE_EDIT:
496 case SID_STYLE_FONT:
497 Edit(rReq.GetFrameWeld(), aParam, {}, nFamily, nMask, false, (nSlot == SID_STYLE_FONT) ? "font" : OUString(), pActShell);
498 break;
499 case SID_STYLE_DELETE:
500 Delete(aParam, nFamily);
501 break;
502 case SID_STYLE_HIDE:
503 case SID_STYLE_SHOW:
504 Hide(aParam, nFamily, nSlot == SID_STYLE_HIDE);
505 break;
506 case SID_STYLE_APPLY:
507 // Shell-switch in ApplyStyles
508 nRet = static_cast<sal_uInt16>(ApplyStyles(aParam, nFamily, pActShell, rReq.GetModifier() ));
509 bReturns = true;
510 break;
511 case SID_STYLE_WATERCAN:
512 nRet = static_cast<sal_uInt16>(DoWaterCan(aParam, nFamily));
513 bReturns = true;
514 break;
515 case SID_STYLE_UPDATE_BY_EXAMPLE:
516 UpdateStyle(aParam, nFamily, pActShell);
517 break;
518 case SID_STYLE_NEW_BY_EXAMPLE:
519 MakeByExample(aParam, nFamily, nMask, pActShell);
520 break;
522 default:
523 OSL_FAIL("Invalid SlotId");
526 if (bReturns)
528 if(rReq.IsAPI()) // Basic only gets TRUE or FALSE
529 rReq.SetReturnValue(SfxUInt16Item(nSlot, sal_uInt16(nRet !=0)));
530 else
531 rReq.SetReturnValue(SfxUInt16Item(nSlot, nRet));
534 rReq.Done();
537 break;
542 namespace {
544 class ApplyStyle
546 public:
547 ApplyStyle(SwDocShell &rDocSh, bool bNew,
548 rtl::Reference< SwDocStyleSheet > xTmp,
549 SfxStyleFamily nFamily, SfxAbstractApplyTabDialog *pDlg,
550 rtl::Reference< SfxStyleSheetBasePool > xBasePool,
551 bool bModified)
552 : m_pDlg(pDlg)
553 , m_rDocSh(rDocSh)
554 , m_bNew(bNew)
555 , m_xTmp(std::move(xTmp))
556 , m_nFamily(nFamily)
557 , m_xBasePool(std::move(xBasePool))
558 , m_bModified(bModified)
561 DECL_LINK( ApplyHdl, LinkParamNone*, void );
562 void apply()
564 ApplyHdl(nullptr);
566 VclPtr<SfxAbstractApplyTabDialog> m_pDlg;
567 // true if the document was initially modified before ApplyStyle was created
568 // or if ApplyStyle:::apply was called
569 bool DocIsModified() const
571 return m_bModified;
573 private:
574 SwDocShell &m_rDocSh;
575 bool m_bNew;
576 rtl::Reference< SwDocStyleSheet > m_xTmp;
577 SfxStyleFamily m_nFamily;
578 rtl::Reference< SfxStyleSheetBasePool > m_xBasePool;
579 bool m_bModified;
584 IMPL_LINK_NOARG(ApplyStyle, ApplyHdl, LinkParamNone*, void)
586 SwWrtShell* pWrtShell = m_rDocSh.GetWrtShell();
587 SwDoc* pDoc = m_rDocSh.GetDoc();
588 SwView* pView = m_rDocSh.GetView();
590 pWrtShell->StartAllAction();
592 if( SfxStyleFamily::Para == m_nFamily )
594 SfxItemSet aSet( *m_pDlg->GetOutputItemSet() );
595 ::ConvertAttrGenToChar(aSet, m_xTmp->GetItemSet(), /*bIsPara=*/true);
596 ::SfxToSwPageDescAttr( *pWrtShell, aSet );
597 // reset indent attributes at paragraph style, if a list style
598 // will be applied and no indent attributes will be applied.
599 m_xTmp->SetItemSet( aSet, true );
601 else
603 if(SfxStyleFamily::Page == m_nFamily || SfxStyleFamily::Frame == m_nFamily)
605 static const sal_uInt16 aInval[] = {
606 SID_IMAGE_ORIENTATION,
607 SID_ATTR_CHAR_FONT,
608 FN_INSERT_CTRL, FN_INSERT_OBJ_CTRL,
609 FN_TABLE_INSERT_COL_BEFORE,
610 FN_TABLE_INSERT_COL_AFTER, 0};
611 pView->GetViewFrame().GetBindings().Invalidate(aInval);
613 SfxItemSet aTmpSet( *m_pDlg->GetOutputItemSet() );
614 if( SfxStyleFamily::Char == m_nFamily )
616 ::ConvertAttrGenToChar(aTmpSet, m_xTmp->GetItemSet());
619 m_xTmp->SetItemSet( aTmpSet );
621 if( SfxStyleFamily::Page == m_nFamily && SvtCTLOptions::IsCTLFontEnabled() )
623 const SfxPoolItem *pItem = nullptr;
624 if( aTmpSet.GetItemState( m_rDocSh.GetPool().GetTrueWhich( SID_ATTR_FRAMEDIRECTION, false ) , true, &pItem ) == SfxItemState::SET )
625 SwChartHelper::DoUpdateAllCharts( pDoc );
628 if (m_nFamily == SfxStyleFamily::Page)
630 if (const SfxGrabBagItem* pGrabBagItem = aTmpSet.GetItemIfSet(SID_ATTR_CHAR_GRABBAG))
632 bool bGutterAtTop{};
633 auto it = pGrabBagItem->GetGrabBag().find("GutterAtTop");
634 if (it != pGrabBagItem->GetGrabBag().end())
636 it->second >>= bGutterAtTop;
638 bool bOldGutterAtTop
639 = pDoc->getIDocumentSettingAccess().get(DocumentSettingId::GUTTER_AT_TOP);
640 if (bOldGutterAtTop != bGutterAtTop)
642 pDoc->getIDocumentSettingAccess().set(DocumentSettingId::GUTTER_AT_TOP,
643 bGutterAtTop);
644 pWrtShell->InvalidateLayout(/*bSizeChanged=*/true);
649 if (m_nFamily == SfxStyleFamily::Frame)
651 if (const SfxBoolItem* pBoolItem = aTmpSet.GetItemIfSet(FN_KEEP_ASPECT_RATIO))
653 const SwViewOption* pVOpt = pWrtShell->GetViewOptions();
654 SwViewOption aUsrPref(*pVOpt);
655 aUsrPref.SetKeepRatio(pBoolItem->GetValue());
656 if (pBoolItem->GetValue() != pVOpt->IsKeepRatio())
658 SW_MOD()->ApplyUsrPref(aUsrPref, &pWrtShell->GetView());
664 if(m_bNew)
666 if(SfxStyleFamily::Frame == m_nFamily || SfxStyleFamily::Para == m_nFamily)
668 // clear FillStyle so that it works as a derived attribute
669 SfxItemSet aTmpSet(*m_pDlg->GetOutputItemSet());
671 aTmpSet.ClearItem(XATTR_FILLSTYLE);
672 m_xTmp->SetItemSet(aTmpSet);
676 if(SfxStyleFamily::Page == m_nFamily)
677 pView->InvalidateRulerPos();
679 if( m_bNew )
680 m_xBasePool->Broadcast(SfxStyleSheetHint(SfxHintId::StyleSheetCreated, *m_xTmp));
681 else
682 m_xBasePool->Broadcast(SfxStyleSheetHint(SfxHintId::StyleSheetModified, *m_xTmp));
684 pDoc->getIDocumentState().SetModified();
685 if( !m_bModified )
687 pDoc->GetIDocumentUndoRedo().SetUndoNoResetModified();
688 m_bModified = true;
691 pWrtShell->EndAllAction();
694 namespace
696 /// Checks if there is an Endnote page style in use, and makes sure it has the same orientation
697 /// with the Default (Standard) page style.
698 void syncEndnoteOrientation(const uno::Reference< style::XStyleFamiliesSupplier >& xStyleFamSupp)
700 if (!xStyleFamSupp.is())
702 SAL_WARN("sw.ui", "Ref to XStyleFamiliesSupplier is null.");
703 return;
705 uno::Reference<container::XNameAccess> xStyleFamilies = xStyleFamSupp->getStyleFamilies();
707 if (!xStyleFamilies.is())
708 return;
710 uno::Reference<container::XNameAccess> xPageStyles(xStyleFamilies->getByName("PageStyles"),
711 uno::UNO_QUERY);
713 if (!xPageStyles.is())
714 return;
716 uno::Reference<css::style::XStyle> xEndnotePageStyle(xPageStyles->getByName("Endnote"),
717 uno::UNO_QUERY);
719 if (!xEndnotePageStyle.is())
720 return;
722 // Language-independent name of the "Default Style" is "Standard"
723 uno::Reference<css::style::XStyle> xDefaultPageStyle(xPageStyles->getByName("Standard"),
724 uno::UNO_QUERY);
725 if (!xDefaultPageStyle.is())
726 return;
728 if (xEndnotePageStyle->isUserDefined() || !xEndnotePageStyle->isInUse())
729 return;
731 uno::Reference<beans::XPropertySet> xEndnotePagePropSet(xPageStyles->getByName("Endnote"), uno::UNO_QUERY);
732 uno::Reference<beans::XPropertySet> xDefaultPagePropSet(xPageStyles->getByName("Standard"), uno::UNO_QUERY);
734 if (!xEndnotePagePropSet.is() || !xDefaultPagePropSet.is())
736 SAL_WARN("sw.ui", "xEndnotePagePropSet or xDefaultPagePropSet is null.");
737 return;
740 auto const bIsDefLandScape = *o3tl::doAccess<bool>(
741 xDefaultPagePropSet->getPropertyValue("IsLandscape"));
742 auto const bIsEndLandScape = *o3tl::doAccess<bool>(
743 xEndnotePagePropSet->getPropertyValue("IsLandscape"));
745 if (bIsDefLandScape == bIsEndLandScape)
746 return;
748 auto const nWidth = xEndnotePagePropSet->getPropertyValue("Width");
749 auto const nHeight = xEndnotePagePropSet->getPropertyValue("Height");
751 xEndnotePagePropSet->setPropertyValue("IsLandscape", css::uno::toAny(bIsDefLandScape));
752 xEndnotePagePropSet->setPropertyValue("Width", nHeight);
753 xEndnotePagePropSet->setPropertyValue("Height", nWidth);
757 void SwDocShell::Edit(
758 weld::Window* pDialogParent,
759 const OUString &rName,
760 const OUString &rParent,
761 const SfxStyleFamily nFamily,
762 SfxStyleSearchBits nMask,
763 const bool bNew,
764 const OUString& sPage,
765 SwWrtShell* pActShell,
766 SfxRequest* pReq,
767 sal_uInt16 nSlot)
769 assert( GetWrtShell() );
770 const bool bBasic = pReq && pReq->IsAPI();
771 SfxStyleSheetBase *pStyle = nullptr;
773 bool bModified = m_xDoc->getIDocumentState().IsModified();
775 SwUndoId nNewStyleUndoId(SwUndoId::EMPTY);
777 if( bNew )
779 if (!bBasic)
781 // start undo action in order to get only one undo action for the
782 // UI new style + change style operations
783 m_pWrtShell->StartUndo();
786 if( SfxStyleSearchBits::All != nMask && SfxStyleSearchBits::AllVisible != nMask && SfxStyleSearchBits::Used != nMask )
787 nMask |= SfxStyleSearchBits::UserDefined;
788 else
789 nMask = SfxStyleSearchBits::UserDefined;
791 if ( nFamily == SfxStyleFamily::Para || nFamily == SfxStyleFamily::Char || nFamily == SfxStyleFamily::Frame )
793 // Prevent undo append from being done during paragraph, character, and frame style Make
794 // Do it after ok return from style dialog when derived from style is known
795 ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
796 pStyle = &m_xBasePool->Make( rName, nFamily, nMask );
798 else
800 pStyle = &m_xBasePool->Make( rName, nFamily, nMask );
803 // set the current one as Parent
804 SwDocStyleSheet* pDStyle = static_cast<SwDocStyleSheet*>(pStyle);
805 switch( nFamily )
807 case SfxStyleFamily::Para:
809 if(!rParent.isEmpty())
811 SwTextFormatColl* pColl = m_pWrtShell->FindTextFormatCollByName( rParent );
812 if(!pColl)
814 sal_uInt16 nId = SwStyleNameMapper::GetPoolIdFromUIName(rParent, SwGetPoolIdFromName::TxtColl);
815 if(USHRT_MAX != nId)
816 pColl = m_pWrtShell->GetTextCollFromPool( nId );
818 pDStyle->GetCollection()->SetDerivedFrom( pColl );
819 pDStyle->PresetParent( rParent );
821 else
823 SwTextFormatColl* pColl = m_pWrtShell->GetCurTextFormatColl();
824 pDStyle->GetCollection()->SetDerivedFrom( pColl );
825 if( pColl )
826 pDStyle->PresetParent( pColl->GetName() );
829 break;
830 case SfxStyleFamily::Char:
832 if(!rParent.isEmpty())
834 SwCharFormat* pCFormat = m_pWrtShell->FindCharFormatByName(rParent);
835 if(!pCFormat)
837 sal_uInt16 nId = SwStyleNameMapper::GetPoolIdFromUIName(rParent, SwGetPoolIdFromName::ChrFmt);
838 if(USHRT_MAX != nId)
839 pCFormat = m_pWrtShell->GetCharFormatFromPool( nId );
842 pDStyle->GetCharFormat()->SetDerivedFrom( pCFormat );
843 pDStyle->PresetParent( rParent );
845 else
847 SwCharFormat* pCFormat = m_pWrtShell->GetCurCharFormat();
848 pDStyle->GetCharFormat()->SetDerivedFrom( pCFormat );
849 if( pCFormat )
850 pDStyle->PresetParent( pCFormat->GetName() );
853 break;
854 case SfxStyleFamily::Frame :
856 if(!rParent.isEmpty())
858 SwFrameFormat* pFFormat = m_pWrtShell->GetDoc()->FindFrameFormatByName( rParent );
859 if(!pFFormat)
861 sal_uInt16 nId = SwStyleNameMapper::GetPoolIdFromUIName(rParent, SwGetPoolIdFromName::FrmFmt);
862 if(USHRT_MAX != nId)
863 pFFormat = m_pWrtShell->GetFrameFormatFromPool( nId );
865 pDStyle->GetFrameFormat()->SetDerivedFrom( pFFormat );
866 pDStyle->PresetParent( rParent );
869 break;
870 default: break;
873 if (!bBasic)
875 //Get the undo id for the type of style that was created in order to re-use that comment for the grouped
876 //create style + change style operations
877 m_pWrtShell->GetLastUndoInfo(nullptr, &nNewStyleUndoId);
880 else
882 pStyle = m_xBasePool->Find( rName, nFamily );
883 SAL_WARN_IF( !pStyle, "sw.ui", "Style not found" );
886 if(!pStyle)
887 return;
889 // put dialogues together
890 rtl::Reference< SwDocStyleSheet > xTmp( new SwDocStyleSheet( *static_cast<SwDocStyleSheet*>(pStyle) ) );
891 if( SfxStyleFamily::Para == nFamily )
893 SfxItemSet& rSet = xTmp->GetItemSet();
894 ::SwToSfxPageDescAttr( rSet );
895 // merge list level indent attributes into the item set if needed
896 xTmp->MergeIndentAttrsOfListStyle( rSet );
898 ::ConvertAttrCharToGen(xTmp->GetItemSet(), /*bIsPara=*/true);
900 else if( SfxStyleFamily::Char == nFamily )
902 ::ConvertAttrCharToGen(xTmp->GetItemSet());
905 if(SfxStyleFamily::Page == nFamily || SfxStyleFamily::Para == nFamily)
907 // create needed items for XPropertyList entries from the DrawModel so that
908 // the Area TabPage can access them
909 SfxItemSet& rSet = xTmp->GetItemSet();
910 const SwDrawModel* pDrawModel = GetDoc()->getIDocumentDrawModelAccess().GetDrawModel();
912 rSet.Put(SvxColorListItem(pDrawModel->GetColorList(), SID_COLOR_TABLE));
913 rSet.Put(SvxGradientListItem(pDrawModel->GetGradientList(), SID_GRADIENT_LIST));
914 rSet.Put(SvxHatchListItem(pDrawModel->GetHatchList(), SID_HATCH_LIST));
915 rSet.Put(SvxBitmapListItem(pDrawModel->GetBitmapList(), SID_BITMAP_LIST));
916 rSet.Put(SvxPatternListItem(pDrawModel->GetPatternList(), SID_PATTERN_LIST));
918 std::optional<SfxGrabBagItem> oGrabBag;
919 if (SfxGrabBagItem const* pItem = rSet.GetItemIfSet(SID_ATTR_CHAR_GRABBAG))
921 oGrabBag.emplace(*pItem);
923 else
925 oGrabBag.emplace(SID_ATTR_CHAR_GRABBAG);
927 bool bGutterAtTop
928 = GetDoc()->getIDocumentSettingAccess().get(DocumentSettingId::GUTTER_AT_TOP);
929 oGrabBag->GetGrabBag()["GutterAtTop"] <<= bGutterAtTop;
930 rSet.Put(*oGrabBag);
933 SwWrtShell* pCurrShell = pActShell ? pActShell : m_pWrtShell;
934 if (nFamily == SfxStyleFamily::Frame)
936 SfxItemSet& rSet = xTmp->GetItemSet();
937 const SwViewOption* pVOpt = pCurrShell->GetViewOptions();
938 rSet.Put(SfxBoolItem(FN_KEEP_ASPECT_RATIO, pVOpt->IsKeepRatio()));
941 if (!bBasic)
943 // prior to the dialog the HtmlMode at the DocShell is being sunk
944 sal_uInt16 nHtmlMode = ::GetHtmlMode(this);
946 // In HTML mode, we do not always have a printer. In order to show
947 // the correct page size in the Format - Page dialog, we have to
948 // get one here.
949 if( ( HTMLMODE_ON & nHtmlMode ) &&
950 !pCurrShell->getIDocumentDeviceAccess().getPrinter( false ) )
951 pCurrShell->InitPrt( pCurrShell->getIDocumentDeviceAccess().getPrinter( true ) );
953 PutItem(SfxUInt16Item(SID_HTML_MODE, nHtmlMode));
954 FieldUnit eMetric = ::GetDfltMetric(0 != (HTMLMODE_ON&nHtmlMode));
955 SW_MOD()->PutItem(SfxUInt16Item(SID_ATTR_METRIC, static_cast< sal_uInt16 >(eMetric)));
956 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
957 if (!pDialogParent)
959 SAL_WARN("sw.ui", "no parent for dialog supplied, assuming document frame is good enough");
960 pDialogParent = GetView()->GetFrameWeld();
962 VclPtr<SfxAbstractApplyTabDialog> pDlg(pFact->CreateTemplateDialog(pDialogParent,
963 *xTmp, nFamily, sPage, pCurrShell, bNew));
964 auto pApplyStyleHelper = std::make_shared<ApplyStyle>(*this, bNew, xTmp, nFamily, pDlg.get(), m_xBasePool, bModified);
965 pDlg->SetApplyHdl(LINK(pApplyStyleHelper.get(), ApplyStyle, ApplyHdl));
967 std::shared_ptr<SfxRequest> pRequest;
968 if (pReq)
970 pRequest = std::make_shared<SfxRequest>(*pReq);
971 pReq->Ignore(); // the 'old' request is not relevant any more
974 bool bIsDefaultPage = nFamily == SfxStyleFamily::Page
975 && rName == SwResId(STR_POOLPAGE_STANDARD)
976 && pStyle->IsUsed()
977 && !pStyle->IsUserDefined();
979 pDlg->StartExecuteAsync([bIsDefaultPage, bNew, nFamily, nSlot, nNewStyleUndoId, pApplyStyleHelper, pRequest, xTmp, this](sal_Int32 nResult){
980 if (RET_OK == nResult)
981 pApplyStyleHelper->apply();
983 if (bNew)
985 switch( nFamily )
987 case SfxStyleFamily::Para:
989 if(!xTmp->GetParent().isEmpty())
991 SwTextFormatColl* pColl = m_pWrtShell->FindTextFormatCollByName(xTmp->GetParent());
992 if (GetDoc()->GetIDocumentUndoRedo().DoesUndo())
994 GetDoc()->GetIDocumentUndoRedo().AppendUndo(
995 std::make_unique<SwUndoTextFormatCollCreate>(xTmp->GetCollection(), pColl, *GetDoc()));
999 break;
1000 case SfxStyleFamily::Char:
1002 if(!xTmp->GetParent().isEmpty())
1004 SwCharFormat* pCFormat = m_pWrtShell->FindCharFormatByName(xTmp->GetParent());
1005 if (GetDoc()->GetIDocumentUndoRedo().DoesUndo())
1007 GetDoc()->GetIDocumentUndoRedo().AppendUndo(
1008 std::make_unique<SwUndoCharFormatCreate>(xTmp->GetCharFormat(), pCFormat, *GetDoc()));
1012 break;
1013 case SfxStyleFamily::Frame:
1015 if(!xTmp->GetParent().isEmpty())
1017 SwFrameFormat* pFFormat = m_pWrtShell->GetDoc()->FindFrameFormatByName(xTmp->GetParent());
1018 if (GetDoc()->GetIDocumentUndoRedo().DoesUndo())
1020 GetDoc()->GetIDocumentUndoRedo().AppendUndo(
1021 std::make_unique<SwUndoFrameFormatCreate>(xTmp->GetFrameFormat(), pFFormat, *GetDoc()));
1025 break;
1026 default: break;
1029 SwRewriter aRewriter;
1030 aRewriter.AddRule(UndoArg1, xTmp->GetName());
1031 //Group the create style and change style operations together under the
1032 //one "create style" comment
1033 m_pWrtShell->EndUndo(nNewStyleUndoId, &aRewriter);
1036 bool bDocModified = pApplyStyleHelper->DocIsModified();
1038 if (RET_OK != nResult)
1040 if (bNew)
1042 GetWrtShell()->Undo();
1043 m_xDoc->GetIDocumentUndoRedo().ClearRedo();
1046 if (!bDocModified)
1047 m_xDoc->getIDocumentState().ResetModified();
1050 // Update Watermark if new page style was created
1051 if (nSlot == SID_STYLE_NEW && nFamily == SfxStyleFamily::Page)
1053 SwWrtShell* pShell = GetWrtShell();
1054 const SfxWatermarkItem aWatermark = pShell->GetWatermark();
1055 pShell->SetWatermark(aWatermark);
1058 pApplyStyleHelper->m_pDlg.disposeAndClear();
1059 if (pRequest)
1060 pRequest->Done();
1062 if (bIsDefaultPage && bDocModified)
1064 uno::Reference< style::XStyleFamiliesSupplier > xStyleFamSupp(GetModel(), uno::UNO_QUERY);
1066 if (!xStyleFamSupp.is())
1068 SAL_WARN("sw.ui", "Ref to XStyleFamiliesSupplier is null.");
1069 return;
1072 syncEndnoteOrientation(xStyleFamSupp);
1076 else
1078 // prior to the dialog the HtmlMode at the DocShell is being sunk
1079 PutItem(SfxUInt16Item(SID_HTML_MODE, ::GetHtmlMode(this)));
1081 GetWrtShell()->StartAllAction();
1083 if( SfxStyleFamily::Para == nFamily )
1085 ::SfxToSwPageDescAttr( *GetWrtShell(), xTmp->GetItemSet() );
1086 ::ConvertAttrGenToChar(xTmp->GetItemSet(), xTmp->GetItemSet(), /*bIsPara=*/true);
1088 else
1090 ::ConvertAttrGenToChar(xTmp->GetItemSet(), xTmp->GetItemSet());
1092 if(SfxStyleFamily::Page == nFamily)
1093 m_pView->InvalidateRulerPos();
1095 if( bNew )
1096 m_xBasePool->Broadcast(SfxStyleSheetHint(SfxHintId::StyleSheetCreated, *xTmp));
1098 m_xDoc->getIDocumentState().SetModified();
1099 if( !bModified ) // Bug 57028
1101 m_xDoc->GetIDocumentUndoRedo().SetUndoNoResetModified();
1103 GetWrtShell()->EndAllAction();
1107 void SwDocShell::Delete(const OUString &rName, SfxStyleFamily nFamily)
1109 SfxStyleSheetBase *pStyle = m_xBasePool->Find(rName, nFamily);
1111 if(pStyle)
1113 assert( GetWrtShell() );
1115 GetWrtShell()->StartAllAction();
1116 m_xBasePool->Remove(pStyle);
1117 GetWrtShell()->EndAllAction();
1121 void SwDocShell::Hide(const OUString &rName, SfxStyleFamily nFamily, bool bHidden)
1123 SfxStyleSheetBase *pStyle = m_xBasePool->Find(rName, nFamily);
1125 if(pStyle)
1127 assert( GetWrtShell() );
1129 GetWrtShell()->StartAllAction();
1130 rtl::Reference< SwDocStyleSheet > xTmp( new SwDocStyleSheet( *static_cast<SwDocStyleSheet*>(pStyle) ) );
1131 xTmp->SetHidden( bHidden );
1132 GetWrtShell()->EndAllAction();
1136 // apply template
1137 SfxStyleFamily SwDocShell::ApplyStyles(const OUString &rName, SfxStyleFamily nFamily,
1138 SwWrtShell* pShell, const sal_uInt16 nMode )
1140 SwDocStyleSheet* pStyle = static_cast<SwDocStyleSheet*>( m_xBasePool->Find( rName, nFamily ) );
1142 SAL_WARN_IF( !pStyle, "sw.ui", "Style not found" );
1144 if(!pStyle)
1145 return SfxStyleFamily::None;
1147 SwWrtShell *pSh = pShell ? pShell : GetWrtShell();
1149 assert( pSh );
1151 pSh->StartAllAction();
1153 switch (nFamily)
1155 case SfxStyleFamily::Char:
1157 SwFormatCharFormat aFormat(pStyle->GetCharFormat());
1158 pSh->SetAttrItem( aFormat, (nMode & KEY_SHIFT) ?
1159 SetAttrMode::DONTREPLACE : SetAttrMode::DEFAULT );
1160 break;
1162 case SfxStyleFamily::Para:
1164 // When outline-folding is enabled, MakeAllOutlineContentTemporarilyVisible makes
1165 // application of a paragraph style that has an outline-level greater than the previous
1166 // outline node become folded content of the previous outline node if the previous
1167 // outline node's content is folded.
1168 MakeAllOutlineContentTemporarilyVisible a(GetDoc());
1169 // #i62675#
1170 // clear also list attributes at affected text nodes, if paragraph
1171 // style has the list style attribute set.
1172 pSh->SetTextFormatColl( pStyle->GetCollection(), true );
1173 break;
1175 case SfxStyleFamily::Frame:
1177 if ( pSh->IsFrameSelected() )
1178 pSh->SetFrameFormat( pStyle->GetFrameFormat() );
1179 break;
1181 case SfxStyleFamily::Page:
1183 pSh->SetPageStyle(pStyle->GetPageDesc()->GetName());
1184 break;
1186 case SfxStyleFamily::Pseudo:
1188 // reset indent attribute on applying list style
1189 // continue list of list style
1190 const SwNumRule* pNumRule = pStyle->GetNumRule();
1191 if (pNumRule->GetName() == SwResId(STR_POOLNUMRULE_NOLIST))
1193 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
1194 pViewFrm->GetDispatcher()->Execute(FN_NUM_BULLET_OFF);
1195 break;
1197 const OUString sListIdForStyle =pNumRule->GetDefaultListId();
1198 pSh->SetCurNumRule( *pNumRule, false, sListIdForStyle, true );
1199 break;
1201 case SfxStyleFamily::Table:
1203 pSh->SetTableStyle(pStyle->GetName());
1204 break;
1206 default:
1207 OSL_FAIL("Unknown family");
1209 pSh->EndAllAction();
1211 return nFamily;
1214 // start watering-can
1215 SfxStyleFamily SwDocShell::DoWaterCan(const OUString &rName, SfxStyleFamily nFamily)
1217 assert( GetWrtShell() );
1219 SwEditWin& rEdtWin = m_pView->GetEditWin();
1220 SwApplyTemplate* pApply = rEdtWin.GetApplyTemplate();
1221 bool bWaterCan = !(pApply && pApply->eType != SfxStyleFamily(0));
1223 if( rName.isEmpty() )
1224 bWaterCan = false;
1226 SwApplyTemplate aTemplate;
1227 aTemplate.eType = nFamily;
1229 if(bWaterCan)
1231 SwDocStyleSheet* pStyle =
1232 static_cast<SwDocStyleSheet*>( m_xBasePool->Find(rName, nFamily) );
1234 SAL_WARN_IF( !pStyle, "sw.ui", "Where's the StyleSheet" );
1236 if(!pStyle) return nFamily;
1238 switch(nFamily)
1240 case SfxStyleFamily::Char:
1241 aTemplate.aColl.pCharFormat = pStyle->GetCharFormat();
1242 break;
1243 case SfxStyleFamily::Para:
1244 aTemplate.aColl.pTextColl = pStyle->GetCollection();
1245 break;
1246 case SfxStyleFamily::Frame:
1247 aTemplate.aColl.pFrameFormat = pStyle->GetFrameFormat();
1248 break;
1249 case SfxStyleFamily::Page:
1250 aTemplate.aColl.pPageDesc = const_cast<SwPageDesc*>(pStyle->GetPageDesc());
1251 break;
1252 case SfxStyleFamily::Pseudo:
1253 aTemplate.aColl.pNumRule = const_cast<SwNumRule*>(pStyle->GetNumRule());
1254 break;
1256 default:
1257 OSL_FAIL("Unknown family");
1260 else
1261 aTemplate.eType = SfxStyleFamily(0);
1263 m_pView->GetEditWin().SetApplyTemplate(aTemplate);
1265 return nFamily;
1268 // update template
1269 void SwDocShell::UpdateStyle(const OUString &rName, SfxStyleFamily nFamily, SwWrtShell* pShell)
1271 SwWrtShell* pCurrWrtShell = pShell ? pShell : GetWrtShell();
1272 assert( pCurrWrtShell );
1274 SwDocStyleSheet* pStyle =
1275 static_cast<SwDocStyleSheet*>( m_xBasePool->Find(rName, nFamily) );
1277 if (!pStyle)
1278 return;
1280 switch(nFamily)
1282 case SfxStyleFamily::Para:
1284 SwTextFormatColl* pColl = pStyle->GetCollection();
1285 if(pColl && !pColl->IsDefault())
1287 GetWrtShell()->StartAllAction();
1289 SwRewriter aRewriter;
1290 aRewriter.AddRule(UndoArg1, pColl->GetName());
1292 GetWrtShell()->StartUndo(SwUndoId::INSFMTATTR, &aRewriter);
1293 GetWrtShell()->FillByEx(pColl);
1294 // also apply template to remove hard set attributes
1295 GetWrtShell()->SetTextFormatColl( pColl );
1296 GetWrtShell()->EndUndo();
1297 GetWrtShell()->EndAllAction();
1299 break;
1301 case SfxStyleFamily::Frame:
1303 SwFrameFormat* pFrame = pStyle->GetFrameFormat();
1304 if( pCurrWrtShell->IsFrameSelected() && pFrame && !pFrame->IsDefault() )
1306 SfxItemSet aSet( GetPool(), aFrameFormatSetRange );
1307 pCurrWrtShell->StartAllAction();
1308 pCurrWrtShell->GetFlyFrameAttr( aSet );
1310 // #i105535#
1311 // no update of anchor attribute
1312 aSet.ClearItem( RES_ANCHOR );
1314 pFrame->SetFormatAttr( aSet );
1316 // also apply template to remove hard set attributes
1317 pCurrWrtShell->SetFrameFormat( pFrame, true );
1318 pCurrWrtShell->EndAllAction();
1321 break;
1322 case SfxStyleFamily::Char:
1324 SwCharFormat* pChar = pStyle->GetCharFormat();
1325 if( pChar && !pChar->IsDefault() )
1327 pCurrWrtShell->StartAllAction();
1328 pCurrWrtShell->FillByEx(pChar);
1329 // also apply template to remove hard set attributes
1330 pCurrWrtShell->EndAllAction();
1334 break;
1335 case SfxStyleFamily::Pseudo:
1337 const SwNumRule* pCurRule;
1338 if( pStyle->GetNumRule() &&
1339 nullptr != ( pCurRule = pCurrWrtShell->GetNumRuleAtCurrCursorPos() ))
1341 SwNumRule aRule( *pCurRule );
1342 // #i91400#
1343 aRule.SetName( pStyle->GetNumRule()->GetName(),
1344 pCurrWrtShell->GetDoc()->getIDocumentListsAccess() );
1345 pCurrWrtShell->ChgNumRuleFormats( aRule );
1348 break;
1349 case SfxStyleFamily::Table:
1351 if (SwFEShell* pFEShell = GetFEShell())
1353 if(pFEShell->IsTableMode())
1355 pFEShell->TableCursorToCursor();
1358 SwTableAutoFormat aFormat(rName);
1359 if (pCurrWrtShell->GetTableAutoFormat(aFormat))
1361 pCurrWrtShell->StartAllAction();
1362 pCurrWrtShell->GetDoc()->ChgTableStyle(rName, aFormat);
1363 pCurrWrtShell->EndAllAction();
1367 break;
1368 default: break;
1371 m_xDoc->BroadcastStyleOperation(rName, nFamily, SfxHintId::StyleSheetModified);
1374 // NewByExample
1375 void SwDocShell::MakeByExample( const OUString &rName, SfxStyleFamily nFamily,
1376 SfxStyleSearchBits nMask, SwWrtShell* pShell )
1378 SwWrtShell* pCurrWrtShell = pShell ? pShell : GetWrtShell();
1379 SwDocStyleSheet* pStyle = static_cast<SwDocStyleSheet*>( m_xBasePool->Find(
1380 rName, nFamily ) );
1381 if(!pStyle)
1383 // preserve the current mask of PI, then the new one is
1384 // immediately merged with the viewable area
1385 if( SfxStyleSearchBits::All == nMask || SfxStyleSearchBits::Used == nMask )
1386 nMask = SfxStyleSearchBits::UserDefined;
1387 else
1388 nMask |= SfxStyleSearchBits::UserDefined;
1390 if (nFamily == SfxStyleFamily::Para || nFamily == SfxStyleFamily::Char || nFamily == SfxStyleFamily::Frame)
1392 // Prevent undo append from being done during paragraph, character, and frame style Make. Do it later
1393 ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
1394 pStyle = static_cast<SwDocStyleSheet*>(&m_xBasePool->Make(rName, nFamily, nMask));
1396 else
1398 pStyle = static_cast<SwDocStyleSheet*>(&m_xBasePool->Make(rName, nFamily, nMask));
1402 switch(nFamily)
1404 case SfxStyleFamily::Para:
1406 SwTextFormatColl* pColl = pStyle->GetCollection();
1407 if(pColl && !pColl->IsDefault())
1409 pCurrWrtShell->StartAllAction();
1410 pCurrWrtShell->FillByEx(pColl);
1411 // also apply template to remove hard set attributes
1412 SwTextFormatColl * pDerivedFrom = pCurrWrtShell->GetCurTextFormatColl();
1413 pColl->SetDerivedFrom(pDerivedFrom);
1415 // set the mask at the Collection:
1416 sal_uInt16 nId = pColl->GetPoolFormatId() & 0x87ff;
1417 switch( nMask & static_cast<SfxStyleSearchBits>(0x0fff) )
1419 case SfxStyleSearchBits::SwText:
1420 nId |= COLL_TEXT_BITS;
1421 break;
1422 case SfxStyleSearchBits::SwChapter:
1423 nId |= COLL_DOC_BITS;
1424 break;
1425 case SfxStyleSearchBits::SwList:
1426 nId |= COLL_LISTS_BITS;
1427 break;
1428 case SfxStyleSearchBits::SwIndex:
1429 nId |= COLL_REGISTER_BITS;
1430 break;
1431 case SfxStyleSearchBits::SwExtra:
1432 nId |= COLL_EXTRA_BITS;
1433 break;
1434 case SfxStyleSearchBits::SwHtml:
1435 nId |= COLL_HTML_BITS;
1436 break;
1437 default: break;
1439 pColl->SetPoolFormatId(nId);
1441 if (GetDoc()->GetIDocumentUndoRedo().DoesUndo())
1443 GetDoc()->GetIDocumentUndoRedo().AppendUndo(
1444 std::make_unique<SwUndoTextFormatCollCreate>(pColl, pDerivedFrom, *GetDoc()));
1446 pCurrWrtShell->SetTextFormatColl(pColl);
1447 pCurrWrtShell->EndAllAction();
1450 break;
1451 case SfxStyleFamily::Frame:
1453 SwFrameFormat* pFrame = pStyle->GetFrameFormat();
1454 if(pCurrWrtShell->IsFrameSelected() && pFrame && !pFrame->IsDefault())
1456 pCurrWrtShell->StartAllAction();
1458 SfxItemSet aSet(GetPool(), aFrameFormatSetRange );
1459 pCurrWrtShell->GetFlyFrameAttr( aSet );
1460 aSet.ClearItem(RES_ANCHOR); // tdf#112574 no anchor in styles
1462 SwFrameFormat* pFFormat = pCurrWrtShell->GetSelectedFrameFormat();
1463 pFrame->SetDerivedFrom( pFFormat );
1464 pFrame->SetFormatAttr( aSet );
1465 if (GetDoc()->GetIDocumentUndoRedo().DoesUndo())
1467 GetDoc()->GetIDocumentUndoRedo().AppendUndo(
1468 std::make_unique<SwUndoFrameFormatCreate>(pFrame, pFFormat, *GetDoc()));
1470 // also apply template to remove hard set attributes
1471 pCurrWrtShell->SetFrameFormat(pFrame);
1472 pCurrWrtShell->EndAllAction();
1475 break;
1476 case SfxStyleFamily::Char:
1478 SwCharFormat* pChar = pStyle->GetCharFormat();
1479 if(pChar && !pChar->IsDefault())
1481 pCurrWrtShell->StartAllAction();
1482 pCurrWrtShell->FillByEx( pChar );
1483 SwCharFormat * pDerivedFrom = pCurrWrtShell->GetCurCharFormat();
1484 pChar->SetDerivedFrom( pDerivedFrom );
1485 SwFormatCharFormat aFormat( pChar );
1487 if (GetDoc()->GetIDocumentUndoRedo().DoesUndo())
1489 // Looks like sometimes pDerivedFrom can be null and this is not supported by redo code
1490 // So use default format as a derived from in such situations
1491 GetDoc()->GetIDocumentUndoRedo().AppendUndo(
1492 std::make_unique<SwUndoCharFormatCreate>(
1493 pChar, pDerivedFrom ? pDerivedFrom : GetDoc()->GetDfltCharFormat(),
1494 *GetDoc()));
1496 pCurrWrtShell->SetAttrItem(aFormat);
1497 pCurrWrtShell->EndAllAction();
1500 break;
1502 case SfxStyleFamily::Page:
1504 pCurrWrtShell->StartAllAction();
1505 size_t nPgDsc = pCurrWrtShell->GetCurPageDesc();
1506 SwPageDesc& rSrc = const_cast<SwPageDesc&>(pCurrWrtShell->GetPageDesc( nPgDsc ));
1507 SwPageDesc& rDest = *const_cast<SwPageDesc*>(pStyle->GetPageDesc());
1509 sal_uInt16 nPoolId = rDest.GetPoolFormatId();
1510 sal_uInt16 nHId = rDest.GetPoolHelpId();
1511 sal_uInt8 nHFId = rDest.GetPoolHlpFileId();
1513 pCurrWrtShell->GetDoc()->CopyPageDesc( rSrc, rDest );
1515 // PoolId must NEVER be copied!
1516 rDest.SetPoolFormatId( nPoolId );
1517 rDest.SetPoolHelpId( nHId );
1518 rDest.SetPoolHlpFileId( nHFId );
1520 // when Headers/Footers are created, there is no Undo anymore!
1521 pCurrWrtShell->GetDoc()->GetIDocumentUndoRedo().DelAllUndoObj();
1523 pCurrWrtShell->EndAllAction();
1525 break;
1527 case SfxStyleFamily::Pseudo:
1529 const SwNumRule* pCurRule = pCurrWrtShell->GetNumRuleAtCurrCursorPos();
1531 if (pCurRule)
1533 pCurrWrtShell->StartAllAction();
1535 SwNumRule aRule( *pCurRule );
1536 OUString sOrigRule( aRule.GetName() );
1537 // #i91400#
1538 aRule.SetName( pStyle->GetNumRule()->GetName(),
1539 pCurrWrtShell->GetDoc()->getIDocumentListsAccess() );
1540 pCurrWrtShell->ChgNumRuleFormats( aRule );
1542 pCurrWrtShell->ReplaceNumRule( sOrigRule, aRule.GetName() );
1544 pCurrWrtShell->EndAllAction();
1547 break;
1549 case SfxStyleFamily::Table:
1551 SwTableAutoFormat* pFormat = pStyle->GetTableFormat();
1552 if (pCurrWrtShell->GetTableAutoFormat(*pFormat))
1554 pCurrWrtShell->StartAllAction();
1556 pCurrWrtShell->SetTableStyle(rName);
1558 pCurrWrtShell->EndAllAction();
1561 break;
1563 default: break;
1566 m_xDoc->BroadcastStyleOperation(rName, nFamily, SfxHintId::StyleSheetCreated);
1569 sfx::AccessibilityIssueCollection SwDocShell::runAccessibilityCheck()
1571 #if !ENABLE_WASM_STRIP_ACCESSIBILITY
1572 sw::AccessibilityCheck aCheck(m_xDoc.get());
1573 aCheck.check();
1574 return aCheck.getIssueCollection();
1575 #else
1576 return sfx::AccessibilityIssueCollection();
1577 #endif
1580 std::set<Color> SwDocShell::GetDocColors()
1582 return m_xDoc->GetDocColors();
1585 std::shared_ptr<model::ColorSet> SwDocShell::GetThemeColors()
1587 SdrPage* pPage = m_xDoc->getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
1588 if (!pPage)
1589 return {};
1590 auto const& pTheme = pPage->getSdrPageProperties().GetTheme();
1591 if (!pTheme)
1592 return {};
1593 return pTheme->getColorSet();
1596 void SwDocShell::LoadStyles( SfxObjectShell& rSource )
1598 LoadStyles_(rSource, false);
1601 // bPreserveCurrentDocument determines whether SetFixFields() is called
1602 // This call modifies the source document. This mustn't happen when the source
1603 // is a document the user is working on.
1604 // Calls of ::LoadStyles() normally use files especially loaded for the purpose
1605 // of importing styles.
1606 void SwDocShell::LoadStyles_( SfxObjectShell& rSource, bool bPreserveCurrentDocument )
1608 /* [Description]
1610 This method is called by SFx if Styles have to be reloaded from a
1611 document-template. Existing Styles should be overwritten by that.
1612 That's why the document has to be reformatted. Therefore applications
1613 will usually override this method and call the baseclass' implementation
1614 in their implementation.
1616 // When the source is our document, we do the checking ourselves
1617 // (much quicker and doesn't use the crutch StxStylePool).
1618 if( dynamic_cast<const SwDocShell*>( &rSource) != nullptr)
1620 // in order for the Headers/Footers not to get the fixed content
1621 // of the template, update all the Source's
1622 // FixFields once.
1623 if(!bPreserveCurrentDocument)
1624 static_cast<SwDocShell&>(rSource).m_xDoc->getIDocumentFieldsAccess().SetFixFields(nullptr);
1625 if (m_pWrtShell)
1627 // rhbz#818557, fdo#58893: EndAllAction will call SelectShell(),
1628 // which pushes a bunch of SfxShells that are not cleared
1629 // (for unknown reasons) when closing the document, causing crash;
1630 // setting g_bNoInterrupt appears to avoid the problem.
1631 ::comphelper::FlagRestorationGuard g(g_bNoInterrupt, true);
1632 m_pWrtShell->StartAllAction();
1633 m_xDoc->ReplaceStyles( *static_cast<SwDocShell&>(rSource).m_xDoc );
1634 m_pWrtShell->EndAllAction();
1636 else
1638 bool bModified = m_xDoc->getIDocumentState().IsModified();
1639 m_xDoc->ReplaceStyles( *static_cast<SwDocShell&>(rSource).m_xDoc );
1640 if (!bModified && m_xDoc->getIDocumentState().IsModified() && !m_pView)
1642 // the View is created later, but overwrites the Modify-Flag.
1643 // Undo doesn't work anymore anyways.
1644 m_xDoc->GetIDocumentUndoRedo().SetUndoNoResetModified();
1648 else
1649 SfxObjectShell::LoadStyles( rSource );
1652 void SwDocShell::FormatPage(
1653 weld::Window* pDialogParent,
1654 const OUString& rPage,
1655 const OUString& rPageId,
1656 SwWrtShell& rActShell,
1657 SfxRequest* pRequest)
1659 Edit(pDialogParent, rPage, OUString(), SfxStyleFamily::Page, SfxStyleSearchBits::Auto, false, rPageId, &rActShell, pRequest);
1662 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */