android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / uibase / docvw / contentcontrolaliasbutton.cxx
blob0d11b5c60641f809f8f8fcb9663da66715fac210
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
8 */
10 #include <contentcontrolaliasbutton.hxx>
12 #include <basegfx/matrix/b2dhommatrixtools.hxx>
13 #include <basegfx/vector/b2dvector.hxx>
14 #include <drawinglayer/attribute/fontattribute.hxx>
15 #include <drawinglayer/primitive2d/textlayoutdevice.hxx>
16 #include <drawinglayer/primitive2d/textprimitive2d.hxx>
17 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
18 #include <drawinglayer/processor2d/processor2dtools.hxx>
19 #include <vcl/metric.hxx>
21 #include <HeaderFooterWin.hxx>
22 #include <edtwin.hxx>
23 #include <formatcontentcontrol.hxx>
24 #include <swabstdlg.hxx>
25 #include <view.hxx>
26 #include <viewopt.hxx>
27 #include <wrtsh.hxx>
29 #define TEXT_PADDING 3
30 #define BOX_DISTANCE 3
31 #define BUTTON_WIDTH 12
33 SwContentControlAliasButton::SwContentControlAliasButton(SwEditWin* pEditWin,
34 SwContentControl* pContentControl)
35 : SwFrameMenuButtonBase(pEditWin, nullptr, "modules/swriter/ui/contentcontrolaliasbutton.ui",
36 "ContentControlAliasButton")
37 , m_xPushButton(m_xBuilder->weld_button("button"))
38 , m_sLabel(pContentControl->GetAlias())
40 m_xPushButton->set_accessible_name(m_sLabel);
41 m_xPushButton->connect_clicked(LINK(this, SwContentControlAliasButton, ClickHdl));
42 m_xVirDev = m_xPushButton->create_virtual_device();
43 SetVirDevFont();
46 SwContentControlAliasButton::~SwContentControlAliasButton() { disposeOnce(); }
48 void SwContentControlAliasButton::dispose()
50 m_xPushButton.reset();
51 m_xVirDev.disposeAndClear();
52 SwFrameMenuButtonBase::dispose();
55 void SwContentControlAliasButton::SetOffset(Point aTopLeftPixel)
57 // Compute the text size and get the box position & size from it.
58 tools::Rectangle aTextRect;
59 m_xVirDev->GetTextBoundRect(aTextRect, m_sLabel);
60 tools::Rectangle aTextPxRect = m_xVirDev->LogicToPixel(aTextRect);
61 FontMetric aFontMetric = m_xVirDev->GetFontMetric(m_xVirDev->GetFont());
62 Size aBoxSize(aTextPxRect.GetWidth() + BUTTON_WIDTH + TEXT_PADDING * 2,
63 aFontMetric.GetLineHeight() + TEXT_PADDING * 2);
64 Point aBoxPos(aTopLeftPixel.X() + BOX_DISTANCE, aTopLeftPixel.Y() - aBoxSize.Height());
66 // Set the position & size of the window.
67 SetPosSizePixel(aBoxPos, aBoxSize);
68 m_xVirDev->SetOutputSizePixel(aBoxSize);
70 PaintButton();
73 IMPL_LINK_NOARG(SwContentControlAliasButton, ClickHdl, weld::Button&, void)
75 if (m_bReadOnly)
77 return;
80 SwView& rView = GetEditWin()->GetView();
81 SwWrtShell& rWrtSh = rView.GetWrtShell();
82 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
83 ScopedVclPtr<VclAbstractDialog> pDlg(
84 pFact->CreateSwContentControlDlg(GetEditWin()->GetFrameWeld(), rWrtSh));
85 VclAbstractDialog::AsyncContext aContext;
86 aContext.maEndDialogFn = [](sal_Int32) {};
87 pDlg->StartExecuteAsync(aContext);
90 void SwContentControlAliasButton::PaintButton()
92 if (!m_xVirDev)
94 return;
97 m_xVirDev->SetMapMode(MapMode(MapUnit::MapPixel));
98 drawinglayer::primitive2d::Primitive2DContainer aSeq;
99 tools::Rectangle aRect(Point(0, 0), m_xVirDev->PixelToLogic(GetSizePixel()));
101 // Create button
102 SwFrameButtonPainter::PaintButton(aSeq, aRect, /*bOnTop=*/false);
104 // Create the text primitive
105 const SwViewOption* pVOpt = GetEditWin()->GetView().GetWrtShell().GetViewOptions();
106 basegfx::BColor aLineColor = pVOpt->GetHeaderFooterMarkColor().getBColor();
107 basegfx::B2DVector aFontSize;
108 drawinglayer::attribute::FontAttribute aFontAttr
109 = drawinglayer::primitive2d::getFontAttributeFromVclFont(aFontSize, m_xVirDev->GetFont(),
110 false, false);
112 FontMetric aFontMetric = m_xVirDev->GetFontMetric(m_xVirDev->GetFont());
113 double nTextOffsetY = aFontMetric.GetAscent() + TEXT_PADDING;
114 double nTextOffsetX = std::abs(aRect.GetWidth() - m_xVirDev->GetTextWidth(m_sLabel)) / 2.0;
115 Point aTextPos(nTextOffsetX, nTextOffsetY);
117 basegfx::B2DHomMatrix aTextMatrix = basegfx::utils::createScaleTranslateB2DHomMatrix(
118 aFontSize.getX(), aFontSize.getY(), static_cast<double>(aTextPos.X()),
119 static_cast<double>(aTextPos.Y()));
121 aSeq.push_back(drawinglayer::primitive2d::Primitive2DReference(
122 new drawinglayer::primitive2d::TextSimplePortionPrimitive2D(
123 aTextMatrix, m_sLabel, 0, m_sLabel.getLength(), std::vector<double>(), {},
124 std::move(aFontAttr), css::lang::Locale(), aLineColor)));
126 // Create the processor and process the primitives
127 drawinglayer::geometry::ViewInformation2D aViewInfo;
128 std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor
129 = drawinglayer::processor2d::createProcessor2DFromOutputDevice(*m_xVirDev, aViewInfo);
131 pProcessor->process(aSeq);
133 m_xPushButton->set_custom_button(m_xVirDev.get());
136 void SwContentControlAliasButton::ShowAll(bool bShow) { Show(bShow); }
138 bool SwContentControlAliasButton::Contains(const Point& rDocPt) const
140 tools::Rectangle aRect(GetPosPixel(), GetSizePixel());
141 return aRect.Contains(rDocPt);
144 void SwContentControlAliasButton::SetReadonly(bool bReadonly) { m_bReadOnly = bReadonly; }
146 void SwContentControlAliasButton::SetContentControl(SwContentControl* pContentControl)
148 m_sLabel = pContentControl->GetAlias();
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */