nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / uibase / sidebar / StylePresetsPanel.cxx
blob12e400b939b9efbc845eb60c3bee3a3ffca97b5e
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 */
11 #include <sal/config.h>
13 #include "StylePresetsPanel.hxx"
15 #include <vcl/image.hxx>
16 #include <vcl/virdev.hxx>
17 #include <vcl/svapp.hxx>
19 #include <sfx2/objsh.hxx>
20 #include <sfx2/StylePreviewRenderer.hxx>
22 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <sfx2/doctempl.hxx>
26 #include <shellio.hxx>
27 #include <docsh.hxx>
29 #include <sfx2/docfile.hxx>
31 namespace sw::sidebar {
33 namespace {
35 void renderPreview(sfx2::StyleManager* pStyleManager, OutputDevice& aOutputDevice,
36 OUString const & sName, sal_Int32 nHeight, tools::Rectangle const & aRect)
38 SfxStyleSheetBase* pStyleSheet = pStyleManager->Search(sName, SfxStyleFamily::Para);
40 if (pStyleSheet)
42 std::unique_ptr<sfx2::StylePreviewRenderer> pStylePreviewRenderer
43 = pStyleManager->CreateStylePreviewRenderer(aOutputDevice, pStyleSheet, nHeight);
44 pStylePreviewRenderer->recalculate();
45 pStylePreviewRenderer->render(aRect, sfx2::StylePreviewRenderer::RenderAlign::TOP);
49 BitmapEx GenerateStylePreview(SfxObjectShell& rSource, OUString const & aName)
51 sfx2::StyleManager* pStyleManager = rSource.GetStyleManager();
53 ScopedVclPtrInstance<VirtualDevice> pVirtualDev(*Application::GetDefaultDevice());
55 float fScalingFactor = pVirtualDev->GetDPIScaleFactor();
57 sal_Int32 nMargin = 6 * fScalingFactor;
59 sal_Int32 nPreviewWidth = 144 * fScalingFactor;
61 sal_Int32 nNameHeight = 16 * fScalingFactor;
62 sal_Int32 nTitleHeight = 32 * fScalingFactor;
63 sal_Int32 nHeadingHeight = 24 * fScalingFactor;
64 sal_Int32 nTextBodyHeight = 16 * fScalingFactor;
65 sal_Int32 nBottomMargin = 2 * fScalingFactor;
67 sal_Int32 nNameFontSize = 12 * fScalingFactor;
69 sal_Int32 nPreviewHeight = nNameHeight + nTitleHeight + nHeadingHeight + nTextBodyHeight + nBottomMargin;
71 Size aSize(nPreviewWidth, nPreviewHeight);
73 pVirtualDev->SetOutputSizePixel(aSize);
75 pVirtualDev->SetLineColor(COL_LIGHTGRAY);
76 pVirtualDev->SetFillColor();
78 tools::Long y = 0;
80 pVirtualDev->SetFillColor(COL_LIGHTGRAY);
81 tools::Rectangle aNameRect(0, y, nPreviewWidth, nNameHeight);
82 pVirtualDev->DrawRect(aNameRect);
84 vcl::Font aFont;
85 aFont.SetFontSize(Size(0, nNameFontSize));
87 pVirtualDev->SetFont(aFont);
89 Size aTextSize(pVirtualDev->GetTextWidth(aName), pVirtualDev->GetTextHeight());
91 Point aPoint((aNameRect.GetWidth() / 2.0) - (aTextSize.Width() / 2.0),
92 y + (aNameRect.GetHeight() / 2.0) - (aTextSize.Height() / 2.0));
94 pVirtualDev->DrawText(aPoint, aName);
96 y += nNameHeight;
100 tools::Rectangle aRenderRect(Point(nMargin, y), aSize);
101 renderPreview(pStyleManager, *pVirtualDev, "Title", nTitleHeight, aRenderRect);
102 y += nTitleHeight;
106 tools::Rectangle aRenderRect(Point(nMargin, y), aSize);
107 renderPreview(pStyleManager, *pVirtualDev, "Heading 1", nHeadingHeight, aRenderRect);
108 y += nHeadingHeight;
111 tools::Rectangle aRenderRect(Point(nMargin, y), aSize);
112 renderPreview(pStyleManager, *pVirtualDev, "Text Body", nTextBodyHeight, aRenderRect);
115 return pVirtualDev->GetBitmapEx(Point(), aSize);
118 BitmapEx CreatePreview(OUString const & aUrl, OUString const & aName)
120 SfxMedium aMedium(aUrl, StreamMode::STD_READWRITE);
121 SfxObjectShell* pObjectShell = SfxObjectShell::Current();
122 SfxObjectShellLock xTemplDoc = SfxObjectShell::CreateObjectByFactoryName(pObjectShell->GetFactory().GetFactoryName(), SfxObjectCreateMode::ORGANIZER);
123 xTemplDoc->DoInitNew();
124 if (xTemplDoc->LoadFrom(aMedium))
126 return GenerateStylePreview(*xTemplDoc, aName);
128 return BitmapEx();
133 VclPtr<vcl::Window> StylePresetsPanel::Create (vcl::Window* pParent,
134 const css::uno::Reference<css::frame::XFrame>& rxFrame)
136 if (pParent == nullptr)
137 throw css::lang::IllegalArgumentException("no parent Window given to StylePresetsPanel::Create", nullptr, 0);
138 if (!rxFrame.is())
139 throw css::lang::IllegalArgumentException("no XFrame given to StylePresetsPanel::Create", nullptr, 1);
141 return VclPtr<StylePresetsPanel>::Create(pParent, rxFrame);
144 StylePresetsPanel::StylePresetsPanel(vcl::Window* pParent,
145 const css::uno::Reference<css::frame::XFrame>& rxFrame)
146 : PanelLayout(pParent, "StylePresetsPanel", "modules/swriter/ui/sidebarstylepresets.ui", rxFrame)
147 , mxValueSet(new ValueSet(nullptr))
148 , mxValueSetWin(new weld::CustomWeld(*m_xBuilder, "valueset", *mxValueSet))
150 mxValueSet->SetColCount(2);
152 mxValueSet->SetDoubleClickHdl(LINK(this, StylePresetsPanel, DoubleClickHdl));
154 RefreshList();
156 m_pInitialFocusWidget = mxValueSet->GetDrawingArea();
159 void StylePresetsPanel::RefreshList()
161 SfxDocumentTemplates aTemplates;
162 sal_uInt16 nCount = aTemplates.GetRegionCount();
163 for (sal_uInt16 i = 0; i < nCount; ++i)
165 OUString aRegionName(aTemplates.GetFullRegionName(i));
166 if (aRegionName == "Styles")
168 for (sal_uInt16 j = 0; j < aTemplates.GetCount(i); ++j)
170 OUString aName = aTemplates.GetName(i,j);
171 OUString aURL = aTemplates.GetPath(i,j);
172 BitmapEx aPreview = CreatePreview(aURL, aName);
173 sal_uInt16 nId = j + 1;
174 mxValueSet->InsertItem(nId, Image(aPreview), aName);
175 maTemplateEntries.push_back(std::make_unique<TemplateEntry>(aURL));
176 mxValueSet->SetItemData(nId, maTemplateEntries.back().get());
178 mxValueSet->SetOptimalSize();
183 StylePresetsPanel::~StylePresetsPanel()
185 disposeOnce();
188 void StylePresetsPanel::dispose()
190 mxValueSetWin.reset();
191 mxValueSet.reset();
193 PanelLayout::dispose();
196 IMPL_LINK_NOARG(StylePresetsPanel, DoubleClickHdl, ValueSet*, void)
198 sal_Int32 nItemId = mxValueSet->GetSelectedItemId();
199 TemplateEntry* pEntry = static_cast<TemplateEntry*>(mxValueSet->GetItemData(nItemId));
201 SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current());
202 if (pDocSh)
204 SwgReaderOption aOption;
205 aOption.SetTextFormats(true);
206 aOption.SetNumRules(true);
207 pDocSh->LoadStylesFromFile(pEntry->maURL, aOption, false);
211 void StylePresetsPanel::NotifyItemUpdate(const sal_uInt16 /*nSId*/,
212 const SfxItemState /*eState*/,
213 const SfxPoolItem* /*pState*/)
217 } // end of namespace ::sw::sidebar
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */