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
, "modules/swriter/ui/unfloatbutton.ui",
51 , m_xPushButton(m_xBuilder
->weld_button("button"))
52 , m_sLabel(SwResId(STR_UNFLOAT_TABLE
))
54 m_xPushButton
->set_accessible_name(m_sLabel
);
55 m_xVirDev
= m_xPushButton
->create_virtual_device();
59 UnfloatTableButton::~UnfloatTableButton() { disposeOnce(); }
61 void UnfloatTableButton::dispose()
63 m_xPushButton
.reset();
64 m_xVirDev
.disposeAndClear();
65 SwFrameMenuButtonBase::dispose();
68 void UnfloatTableButton::SetOffset(Point aTopRightPixel
)
70 // Compute the text size and get the box position & size from it
71 tools::Rectangle aTextRect
;
72 m_xVirDev
->GetTextBoundRect(aTextRect
, m_sLabel
);
73 tools::Rectangle aTextPxRect
= m_xVirDev
->LogicToPixel(aTextRect
);
74 FontMetric aFontMetric
= m_xVirDev
->GetFontMetric(m_xVirDev
->GetFont());
75 Size
aBoxSize(aTextPxRect
.GetWidth() + BUTTON_WIDTH
+ TEXT_PADDING
* 2,
76 aFontMetric
.GetLineHeight() + TEXT_PADDING
* 2);
78 Point
aBoxPos(aTopRightPixel
.X() - aBoxSize
.Width() - BOX_DISTANCE
, aTopRightPixel
.Y());
80 if (AllSettings::GetLayoutRTL())
82 aBoxPos
.setX(aTopRightPixel
.X() + BOX_DISTANCE
);
85 // Set the position & Size of the window
86 SetPosSizePixel(aBoxPos
, aBoxSize
);
87 m_xVirDev
->SetOutputSizePixel(aBoxSize
);
92 void UnfloatTableButton::MouseButtonDown(const MouseEvent
& /*rMEvt*/)
94 assert(GetFrame()->IsFlyFrame());
95 // const_cast is needed because of bad design of ISwFrameControl and derived classes
96 SwFlyFrame
* pFlyFrame
= const_cast<SwFlyFrame
*>(static_cast<const SwFlyFrame
*>(GetFrame()));
98 // Find the table inside the text frame
99 SwTabFrame
* pTableFrame
= nullptr;
100 SwFrame
* pLower
= pFlyFrame
->GetLower();
103 if (pLower
->IsTabFrame())
105 pTableFrame
= static_cast<SwTabFrame
*>(pLower
);
108 pLower
= pLower
->GetNext();
111 if (pTableFrame
== nullptr)
114 // Insert the table at the position of the text node which has the frame anchored to
115 SwFrame
* pAnchoreFrame
= pFlyFrame
->AnchorFrame();
116 if (pAnchoreFrame
== nullptr || !pAnchoreFrame
->IsTextFrame())
119 SwTextFrame
* pTextFrame
= static_cast<SwTextFrame
*>(pAnchoreFrame
);
120 if (pTextFrame
->GetTextNodeFirst() == nullptr)
123 SwNodeIndex
aInsertPos(*pTextFrame
->GetTextNodeFirst());
125 SwTableNode
* pTableNode
= pTableFrame
->GetTable()->GetTableNode();
126 if (pTableNode
== nullptr)
129 SwDoc
& rDoc
= pTextFrame
->GetDoc();
131 // tdf#129176: clear "TablePosition" grab bag, since we explicitly change the position here
132 // See DomainMapperTableHandler::endTableGetTableStyle, where the grab bag is filled, and
133 // DocxAttributeOutput::TableDefinition that uses it on export
134 SwFrameFormat
* pTableFormat
= pTableFrame
->GetTable()->GetFrameFormat();
135 assert(pTableFormat
);
136 if (const SfxGrabBagItem
* pGrabBagItem
= pTableFormat
->GetAttrSet().GetItem(RES_FRMATR_GRABBAG
))
138 SfxGrabBagItem
aGrabBagItem(*pGrabBagItem
); // Editable copy
139 if (aGrabBagItem
.GetGrabBag().erase("TablePosition"))
142 aGrabBagItem
.QueryValue(aVal
);
143 const rtl::Reference
<SwXTextTable
> xTable
144 = SwXTextTable::CreateXTextTable(pTableFormat
);
145 xTable
->setPropertyValue(UNO_NAME_TABLE_INTEROP_GRAB_BAG
, aVal
);
149 // When we move the table before the first text node, we need to clear RES_PAGEDESC attribute
150 // of the text node otherwise LO will create a page break after the table
151 if (pTextFrame
->GetTextNodeFirst())
153 const SwPageDesc
* pPageDesc
154 = pTextFrame
->GetPageDescItem().GetPageDesc(); // First text node of the page has this
157 // First set the existing page desc for the table node
158 SfxItemSetFixed
<RES_PAGEDESC
, RES_PAGEDESC
> aSet(
159 GetEditWin()->GetView().GetWrtShell().GetAttrPool());
160 aSet
.Put(SwFormatPageDesc(pPageDesc
));
161 SwPaM
aPaMTable(*pTableNode
);
162 rDoc
.getIDocumentContentOperations().InsertItemSet(
163 aPaMTable
, aSet
, SetAttrMode::DEFAULT
, GetPageFrame()->getRootFrame());
165 // Then remove pagedesc from the attributes of the text node
166 aSet
.Put(SwFormatPageDesc(nullptr));
167 SwPaM
aPaMTextNode(*pTextFrame
->GetTextNodeFirst());
168 rDoc
.getIDocumentContentOperations().InsertItemSet(
169 aPaMTextNode
, aSet
, SetAttrMode::DEFAULT
, GetPageFrame()->getRootFrame());
173 // Move the table outside of the text frame
174 SwNodeRange
aRange(*pTableNode
, SwNodeOffset(0), *pTableNode
->EndOfSectionNode(),
176 rDoc
.getIDocumentContentOperations().MoveNodeRange(aRange
, aInsertPos
.GetNode(),
177 SwMoveFlags::DEFAULT
);
179 // Remove the floating table's frame
180 SwFlyFrameFormat
* pFrameFormat
= pFlyFrame
->GetFormat();
183 rDoc
.getIDocumentLayoutAccess().DelLayoutFormat(pFrameFormat
);
186 rDoc
.getIDocumentState().SetModified();
188 // Undoing MoveNodeRange() is not working correctly in case of tables, it crashes sometimes
189 // So don't allow to undo after unfloating (similar to MakeFlyAndMove() method)
190 if (rDoc
.GetIDocumentUndoRedo().DoesUndo())
192 rDoc
.GetIDocumentUndoRedo().DelAllUndoObj();
196 void UnfloatTableButton::PaintButton()
201 m_xVirDev
->SetMapMode(MapMode(MapUnit::MapPixel
));
202 drawinglayer::primitive2d::Primitive2DContainer aSeq
;
203 const ::tools::Rectangle
aRect(
204 ::tools::Rectangle(Point(0, 0), m_xVirDev
->PixelToLogic(GetSizePixel())));
207 SwFrameButtonPainter::PaintButton(aSeq
, aRect
, true);
209 // Create the text primitive
210 basegfx::BColor aLineColor
211 = SwViewOption::GetCurrentViewOptions().GetHeaderFooterMarkColor().getBColor();
212 basegfx::B2DVector aFontSize
;
213 drawinglayer::attribute::FontAttribute aFontAttr
214 = drawinglayer::primitive2d::getFontAttributeFromVclFont(aFontSize
, m_xVirDev
->GetFont(),
217 FontMetric aFontMetric
= m_xVirDev
->GetFontMetric(m_xVirDev
->GetFont());
218 double nTextOffsetY
= aFontMetric
.GetAscent() + TEXT_PADDING
;
219 double nTextOffsetX
= std::abs(aRect
.GetWidth() - m_xVirDev
->GetTextWidth(m_sLabel
)) / 2.0;
220 Point
aTextPos(nTextOffsetX
, nTextOffsetY
);
222 basegfx::B2DHomMatrix
aTextMatrix(basegfx::utils::createScaleTranslateB2DHomMatrix(
223 aFontSize
.getX(), aFontSize
.getY(), static_cast<double>(aTextPos
.X()),
224 static_cast<double>(aTextPos
.Y())));
226 aSeq
.push_back(drawinglayer::primitive2d::Primitive2DReference(
227 new drawinglayer::primitive2d::TextSimplePortionPrimitive2D(
228 aTextMatrix
, m_sLabel
, 0, m_sLabel
.getLength(), std::vector
<double>(), {},
229 std::move(aFontAttr
), css::lang::Locale(), aLineColor
)));
231 // Create the processor and process the primitives
232 const drawinglayer::geometry::ViewInformation2D aNewViewInfos
;
233 std::unique_ptr
<drawinglayer::processor2d::BaseProcessor2D
> pProcessor(
234 drawinglayer::processor2d::createProcessor2DFromOutputDevice(*m_xVirDev
, aNewViewInfos
));
236 pProcessor
->process(aSeq
);
238 m_xPushButton
->set_custom_button(m_xVirDev
.get());
241 void UnfloatTableButton::ShowAll(bool bShow
) { Show(bShow
); }
243 bool UnfloatTableButton::Contains(const Point
& rDocPt
) const
245 ::tools::Rectangle
aRect(GetPosPixel(), GetSizePixel());
246 if (aRect
.Contains(rDocPt
))
252 void UnfloatTableButton::SetReadonly(bool bReadonly
) { ShowAll(!bReadonly
); }
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */