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 <UnfloatTableButton.hxx>
11 #include <HeaderFooterWin.hxx>
16 #include <strings.hrc>
17 #include <fmtpdsc.hxx>
18 #include <vcl/metric.hxx>
19 #include <vcl/settings.hxx>
20 #include <viewopt.hxx>
25 #include <pagefrm.hxx>
26 #include <ndindex.hxx>
28 #include <swtable.hxx>
29 #include <unoprnms.hxx>
31 #include <IDocumentState.hxx>
32 #include <IDocumentUndoRedo.hxx>
33 #include <IDocumentLayoutAccess.hxx>
34 #include <drawinglayer/primitive2d/textprimitive2d.hxx>
35 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
36 #include <drawinglayer/attribute/fontattribute.hxx>
37 #include <drawinglayer/primitive2d/textlayoutdevice.hxx>
38 #include <basegfx/matrix/b2dhommatrixtools.hxx>
39 #include <drawinglayer/processor2d/processor2dtools.hxx>
40 #include <basegfx/vector/b2dvector.hxx>
41 #include <svl/grabbagitem.hxx>
44 #define TEXT_PADDING 3
45 #define BOX_DISTANCE 3
46 #define BUTTON_WIDTH 12
48 UnfloatTableButton::UnfloatTableButton(SwEditWin
* pEditWin
, const SwFrame
* pFrame
)
49 : SwFrameMenuButtonBase(pEditWin
, pFrame
, u
"modules/swriter/ui/unfloatbutton.ui"_ustr
,
50 u
"UnfloatButton"_ustr
)
51 , m_xPushButton(m_xBuilder
->weld_button(u
"button"_ustr
))
52 , m_sLabel(SwResId(STR_UNFLOAT_TABLE
))
54 m_xPushButton
->set_accessible_name(m_sLabel
);
55 m_xVirDev
= m_xPushButton
->create_virtual_device();
56 m_xPushButton
->connect_clicked(LINK(this, UnfloatTableButton
, ClickHdl
));
60 weld::Button
* UnfloatTableButton::GetButton() { return m_xPushButton
.get(); }
62 UnfloatTableButton::~UnfloatTableButton() { disposeOnce(); }
64 void UnfloatTableButton::dispose()
66 m_xPushButton
.reset();
67 m_xVirDev
.disposeAndClear();
68 SwFrameMenuButtonBase::dispose();
71 void UnfloatTableButton::SetOffset(Point aTopRightPixel
)
73 // Compute the text size and get the box position & size from it
74 tools::Rectangle aTextRect
;
75 m_xVirDev
->GetTextBoundRect(aTextRect
, m_sLabel
);
76 tools::Rectangle aTextPxRect
= m_xVirDev
->LogicToPixel(aTextRect
);
77 FontMetric aFontMetric
= m_xVirDev
->GetFontMetric(m_xVirDev
->GetFont());
78 Size
aBoxSize(aTextPxRect
.GetWidth() + BUTTON_WIDTH
+ TEXT_PADDING
* 2,
79 aFontMetric
.GetLineHeight() + TEXT_PADDING
* 2);
81 Point
aBoxPos(aTopRightPixel
.X() - aBoxSize
.Width() - BOX_DISTANCE
, aTopRightPixel
.Y());
83 if (AllSettings::GetLayoutRTL())
85 aBoxPos
.setX(aTopRightPixel
.X() + BOX_DISTANCE
);
88 // Set the position & Size of the window
89 SetPosSizePixel(aBoxPos
, aBoxSize
);
90 m_xVirDev
->SetOutputSizePixel(aBoxSize
);
95 IMPL_LINK_NOARG(UnfloatTableButton
, ClickHdl
, weld::Button
&, void)
97 assert(GetFrame()->IsFlyFrame());
98 // const_cast is needed because of bad design of ISwFrameControl and derived classes
99 SwFlyFrame
* pFlyFrame
= const_cast<SwFlyFrame
*>(static_cast<const SwFlyFrame
*>(GetFrame()));
101 // Find the table inside the text frame
102 SwTabFrame
* pTableFrame
= nullptr;
103 SwFrame
* pLower
= pFlyFrame
->GetLower();
106 if (pLower
->IsTabFrame())
108 pTableFrame
= static_cast<SwTabFrame
*>(pLower
);
111 pLower
= pLower
->GetNext();
114 if (pTableFrame
== nullptr)
117 // Insert the table at the position of the text node which has the frame anchored to
118 SwFrame
* pAnchoreFrame
= pFlyFrame
->AnchorFrame();
119 if (pAnchoreFrame
== nullptr || !pAnchoreFrame
->IsTextFrame())
122 SwTextFrame
* pTextFrame
= static_cast<SwTextFrame
*>(pAnchoreFrame
);
123 if (pTextFrame
->GetTextNodeFirst() == nullptr)
126 SwTableNode
* pTableNode
= pTableFrame
->GetTable()->GetTableNode();
127 if (pTableNode
== nullptr)
130 SwDoc
& rDoc
= pTextFrame
->GetDoc();
132 // tdf#129176: clear "TablePosition" grab bag, since we explicitly change the position here
133 // See DomainMapperTableHandler::endTableGetTableStyle, where the grab bag is filled, and
134 // DocxAttributeOutput::TableDefinition that uses it on export
135 SwFrameFormat
* pTableFormat
= pTableFrame
->GetTable()->GetFrameFormat();
136 assert(pTableFormat
);
137 if (const SfxGrabBagItem
* pGrabBagItem
= pTableFormat
->GetAttrSet().GetItem(RES_FRMATR_GRABBAG
))
139 std::map
<OUString
, css::uno::Any
> aGrabBagMap
= pGrabBagItem
->GetGrabBag(); // Editable copy
140 if (aGrabBagMap
.erase(u
"TablePosition"_ustr
))
142 SfxGrabBagItem
aNewGrabBagItem(RES_FRMATR_GRABBAG
, std::move(aGrabBagMap
));
144 aNewGrabBagItem
.QueryValue(aVal
);
145 pTableFormat
->SetFormatAttr(aNewGrabBagItem
);
146 const rtl::Reference
<SwXTextTable
> xTable
147 = SwXTextTable::CreateXTextTable(pTableFormat
);
148 xTable
->setPropertyValue(UNO_NAME_TABLE_INTEROP_GRAB_BAG
, aVal
);
152 // When we move the table before the first text node, we need to clear RES_PAGEDESC attribute
153 // of the text node otherwise LO will create a page break after the table
154 if (pTextFrame
->GetTextNodeFirst())
156 const SwPageDesc
* pPageDesc
157 = pTextFrame
->GetPageDescItem().GetPageDesc(); // First text node of the page has this
160 // First set the existing page desc for the table node
161 SfxItemSetFixed
<RES_PAGEDESC
, RES_PAGEDESC
> aSet(
162 GetEditWin()->GetView().GetWrtShell().GetAttrPool());
163 aSet
.Put(SwFormatPageDesc(pPageDesc
));
164 SwPaM
aPaMTable(*pTableNode
);
165 rDoc
.getIDocumentContentOperations().InsertItemSet(
166 aPaMTable
, aSet
, SetAttrMode::DEFAULT
, GetPageFrame()->getRootFrame());
168 // Then remove pagedesc from the attributes of the text node
169 aSet
.Put(SwFormatPageDesc(nullptr));
170 SwPaM
aPaMTextNode(*pTextFrame
->GetTextNodeFirst());
171 rDoc
.getIDocumentContentOperations().InsertItemSet(
172 aPaMTextNode
, aSet
, SetAttrMode::DEFAULT
, GetPageFrame()->getRootFrame());
176 SwWrtShell
& rWrtShell
= GetEditWin()->GetView().GetWrtShell();
177 rWrtShell
.UnfloatFlyFrame();
179 rDoc
.getIDocumentState().SetModified();
181 // Undoing MoveNodeRange() is not working correctly in case of tables, it crashes sometimes
182 // So don't allow to undo after unfloating (similar to MakeFlyAndMove() method)
183 if (rDoc
.GetIDocumentUndoRedo().DoesUndo())
185 rDoc
.GetIDocumentUndoRedo().DelAllUndoObj();
189 void UnfloatTableButton::PaintButton()
194 m_xVirDev
->SetMapMode(MapMode(MapUnit::MapPixel
));
195 drawinglayer::primitive2d::Primitive2DContainer aSeq
;
196 const ::tools::Rectangle
aRect(
197 ::tools::Rectangle(Point(0, 0), m_xVirDev
->PixelToLogic(GetSizePixel())));
200 SwFrameButtonPainter::PaintButton(aSeq
, aRect
, true);
202 // Create the text primitive
203 basegfx::BColor aLineColor
204 = SwViewOption::GetCurrentViewOptions().GetHeaderFooterMarkColor().getBColor();
205 basegfx::B2DVector aFontSize
;
206 drawinglayer::attribute::FontAttribute aFontAttr
207 = drawinglayer::primitive2d::getFontAttributeFromVclFont(aFontSize
, m_xVirDev
->GetFont(),
210 FontMetric aFontMetric
= m_xVirDev
->GetFontMetric(m_xVirDev
->GetFont());
211 double nTextOffsetY
= aFontMetric
.GetAscent() + TEXT_PADDING
;
212 double nTextOffsetX
= std::abs(aRect
.GetWidth() - m_xVirDev
->GetTextWidth(m_sLabel
)) / 2.0;
213 Point
aTextPos(nTextOffsetX
, nTextOffsetY
);
215 basegfx::B2DHomMatrix
aTextMatrix(basegfx::utils::createScaleTranslateB2DHomMatrix(
216 aFontSize
.getX(), aFontSize
.getY(), static_cast<double>(aTextPos
.X()),
217 static_cast<double>(aTextPos
.Y())));
219 aSeq
.push_back(new drawinglayer::primitive2d::TextSimplePortionPrimitive2D(
220 aTextMatrix
, m_sLabel
, 0, m_sLabel
.getLength(), std::vector
<double>(), {},
221 std::move(aFontAttr
), css::lang::Locale(), aLineColor
));
223 // Create the processor and process the primitives
224 const drawinglayer::geometry::ViewInformation2D aNewViewInfos
;
225 std::unique_ptr
<drawinglayer::processor2d::BaseProcessor2D
> pProcessor(
226 drawinglayer::processor2d::createProcessor2DFromOutputDevice(*m_xVirDev
, aNewViewInfos
));
228 pProcessor
->process(aSeq
);
230 m_xPushButton
->set_custom_button(m_xVirDev
.get());
233 void UnfloatTableButton::ShowAll(bool bShow
) { Show(bShow
); }
235 bool UnfloatTableButton::Contains(const Point
& rDocPt
) const
237 ::tools::Rectangle
aRect(GetPosPixel(), GetSizePixel());
238 if (aRect
.Contains(rDocPt
))
244 void UnfloatTableButton::SetReadonly(bool bReadonly
) { ShowAll(!bReadonly
); }
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */