1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
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>
23 #include <formatcontentcontrol.hxx>
24 #include <swabstdlg.hxx>
26 #include <viewopt.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,
36 u
"modules/swriter/ui/contentcontrolaliasbutton.ui"_ustr
,
37 u
"ContentControlAliasButton"_ustr
)
38 , m_xPushButton(m_xBuilder
->weld_button(u
"button"_ustr
))
39 , m_sLabel(pContentControl
->GetAlias())
41 m_xPushButton
->set_accessible_name(m_sLabel
);
42 m_xPushButton
->connect_clicked(LINK(this, SwContentControlAliasButton
, ClickHdl
));
43 m_xVirDev
= m_xPushButton
->create_virtual_device();
47 SwContentControlAliasButton::~SwContentControlAliasButton() { disposeOnce(); }
49 void SwContentControlAliasButton::dispose()
51 m_xPushButton
.reset();
52 m_xVirDev
.disposeAndClear();
53 SwFrameMenuButtonBase::dispose();
56 void SwContentControlAliasButton::SetOffset(Point aTopLeftPixel
)
58 // Compute the text size and get the box position & size from it.
59 tools::Rectangle aTextRect
;
60 m_xVirDev
->GetTextBoundRect(aTextRect
, m_sLabel
);
61 tools::Rectangle aTextPxRect
= m_xVirDev
->LogicToPixel(aTextRect
);
62 FontMetric aFontMetric
= m_xVirDev
->GetFontMetric(m_xVirDev
->GetFont());
63 Size
aBoxSize(aTextPxRect
.GetWidth() + BUTTON_WIDTH
+ TEXT_PADDING
* 2,
64 aFontMetric
.GetLineHeight() + TEXT_PADDING
* 2);
65 Point
aBoxPos(aTopLeftPixel
.X() + BOX_DISTANCE
, aTopLeftPixel
.Y() - aBoxSize
.Height());
67 // Set the position & size of the window.
68 SetPosSizePixel(aBoxPos
, aBoxSize
);
69 m_xVirDev
->SetOutputSizePixel(aBoxSize
);
74 IMPL_LINK_NOARG(SwContentControlAliasButton
, ClickHdl
, weld::Button
&, void)
81 SwView
& rView
= GetEditWin()->GetView();
82 SwWrtShell
& rWrtSh
= rView
.GetWrtShell();
83 SwAbstractDialogFactory
* pFact
= SwAbstractDialogFactory::Create();
84 ScopedVclPtr
<VclAbstractDialog
> pDlg(
85 pFact
->CreateSwContentControlDlg(GetEditWin()->GetFrameWeld(), rWrtSh
));
86 VclAbstractDialog::AsyncContext aContext
;
87 aContext
.maEndDialogFn
= [](sal_Int32
) {};
88 pDlg
->StartExecuteAsync(aContext
);
91 void SwContentControlAliasButton::PaintButton()
98 m_xVirDev
->SetMapMode(MapMode(MapUnit::MapPixel
));
99 drawinglayer::primitive2d::Primitive2DContainer aSeq
;
100 tools::Rectangle
aRect(Point(0, 0), m_xVirDev
->PixelToLogic(GetSizePixel()));
103 SwFrameButtonPainter::PaintButton(aSeq
, aRect
, /*bOnTop=*/false);
105 // Create the text primitive
106 const SwViewOption
* pVOpt
= GetEditWin()->GetView().GetWrtShell().GetViewOptions();
107 basegfx::BColor aLineColor
= pVOpt
->GetHeaderFooterMarkColor().getBColor();
108 basegfx::B2DVector aFontSize
;
109 drawinglayer::attribute::FontAttribute aFontAttr
110 = drawinglayer::primitive2d::getFontAttributeFromVclFont(aFontSize
, m_xVirDev
->GetFont(),
113 FontMetric aFontMetric
= m_xVirDev
->GetFontMetric(m_xVirDev
->GetFont());
114 double nTextOffsetY
= aFontMetric
.GetAscent() + TEXT_PADDING
;
115 double nTextOffsetX
= std::abs(aRect
.GetWidth() - m_xVirDev
->GetTextWidth(m_sLabel
)) / 2.0;
116 Point
aTextPos(nTextOffsetX
, nTextOffsetY
);
118 basegfx::B2DHomMatrix aTextMatrix
= basegfx::utils::createScaleTranslateB2DHomMatrix(
119 aFontSize
.getX(), aFontSize
.getY(), static_cast<double>(aTextPos
.X()),
120 static_cast<double>(aTextPos
.Y()));
122 aSeq
.push_back(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: */