LibreOffice Module sw (master)  1
UnfloatTableButton.cxx
Go to the documentation of this file.
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.
4  *
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  */
9 
10 #include <UnfloatTableButton.hxx>
11 #include <HeaderFooterWin.hxx>
12 
13 #include <edtwin.hxx>
14 #include <view.hxx>
15 #include <wrtsh.hxx>
16 #include <strings.hrc>
17 #include <fmtpdsc.hxx>
18 #include <vcl/metric.hxx>
19 #include <vcl/settings.hxx>
20 #include <viewopt.hxx>
21 #include <frame.hxx>
22 #include <flyfrm.hxx>
23 #include <tabfrm.hxx>
24 #include <txtfrm.hxx>
25 #include <pagefrm.hxx>
26 #include <ndindex.hxx>
27 #include <ndtxt.hxx>
28 #include <swtable.hxx>
29 #include <unoprnms.hxx>
30 #include <unotbl.hxx>
31 #include <IDocumentState.hxx>
32 #include <IDocumentUndoRedo.hxx>
41 #include <svl/grabbagitem.hxx>
42 #include <doc.hxx>
43 
44 #define TEXT_PADDING 3
45 #define BOX_DISTANCE 3
46 #define BUTTON_WIDTH 12
47 
49  : SwFrameMenuButtonBase(pEditWin, pFrame, "modules/swriter/ui/unfloatbutton.ui",
50  "UnfloatButton")
51  , m_xPushButton(m_xBuilder->weld_button("button"))
52  , m_sLabel(SwResId(STR_UNFLOAT_TABLE))
53 {
54  m_xPushButton->set_accessible_name(m_sLabel);
55  m_xVirDev = m_xPushButton->create_virtual_device();
56  SetVirDevFont();
57 }
58 
60 
62 {
63  m_xPushButton.reset();
66 }
67 
68 void UnfloatTableButton::SetOffset(Point aTopRightPixel)
69 {
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);
75  Size aBoxSize(aTextPxRect.GetWidth() + BUTTON_WIDTH + TEXT_PADDING * 2,
76  aFontMetric.GetLineHeight() + TEXT_PADDING * 2);
77 
78  Point aBoxPos(aTopRightPixel.X() - aBoxSize.Width() - BOX_DISTANCE, aTopRightPixel.Y());
79 
81  {
82  aBoxPos.setX(aTopRightPixel.X() + BOX_DISTANCE);
83  }
84 
85  // Set the position & Size of the window
86  SetPosSizePixel(aBoxPos, aBoxSize);
87  m_xVirDev->SetOutputSizePixel(aBoxSize);
88 
89  PaintButton();
90 }
91 
93 {
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()));
97 
98  // Find the table inside the text frame
99  SwTabFrame* pTableFrame = nullptr;
100  SwFrame* pLower = pFlyFrame->GetLower();
101  while (pLower)
102  {
103  if (pLower->IsTabFrame())
104  {
105  pTableFrame = static_cast<SwTabFrame*>(pLower);
106  break;
107  }
108  pLower = pLower->GetNext();
109  }
110 
111  if (pTableFrame == nullptr)
112  return;
113 
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())
117  return;
118 
119  SwTextFrame* pTextFrame = static_cast<SwTextFrame*>(pAnchoreFrame);
120  if (pTextFrame->GetTextNodeFirst() == nullptr)
121  return;
122 
123  SwNodeIndex aInsertPos((*pTextFrame->GetTextNodeFirst()));
124 
125  SwTableNode* pTableNode = pTableFrame->GetTable()->GetTableNode();
126  if (pTableNode == nullptr)
127  return;
128 
129  SwDoc& rDoc = pTextFrame->GetDoc();
130 
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))
137  {
138  SfxGrabBagItem aGrabBagItem(*pGrabBagItem); // Editable copy
139  if (aGrabBagItem.GetGrabBag().erase("TablePosition"))
140  {
141  css::uno::Any aVal;
142  aGrabBagItem.QueryValue(aVal);
143  const auto xTable = SwXTextTable::CreateXTextTable(pTableFormat);
144  const css::uno::Reference<css::beans::XPropertySet> xSet(xTable, css::uno::UNO_QUERY);
145  assert(xSet);
146  xSet->setPropertyValue(UNO_NAME_TABLE_INTEROP_GRAB_BAG, aVal);
147  }
148  }
149 
150  // When we move the table before the first text node, we need to clear RES_PAGEDESC attribute
151  // of the text node otherwise LO will create a page break after the table
152  if (pTextFrame->GetTextNodeFirst())
153  {
154  const SwPageDesc* pPageDesc
155  = pTextFrame->GetPageDescItem().GetPageDesc(); // First text node of the page has this
156  if (pPageDesc)
157  {
158  // First set the existing page desc for the table node
159  SfxItemSet aSet(GetEditWin()->GetView().GetWrtShell().GetAttrPool(),
161  aSet.Put(SwFormatPageDesc(pPageDesc));
162  SwPaM aPaMTable(*pTableNode);
164  aPaMTable, aSet, SetAttrMode::DEFAULT, GetPageFrame()->getRootFrame());
165 
166  // Then remove pagedesc from the attributes of the text node
167  aSet.Put(SwFormatPageDesc(nullptr));
168  SwPaM aPaMTextNode(*pTextFrame->GetTextNodeFirst());
170  aPaMTextNode, aSet, SetAttrMode::DEFAULT, GetPageFrame()->getRootFrame());
171  }
172  }
173 
174  // Move the table outside of the text frame
175  SwNodeRange aRange(*pTableNode, 0, *pTableNode->EndOfSectionNode(), 1);
177 
178  // Remove the floating table's frame
179  SwFlyFrameFormat* pFrameFormat = pFlyFrame->GetFormat();
180  if (pFrameFormat)
181  {
182  rDoc.getIDocumentLayoutAccess().DelLayoutFormat(pFrameFormat);
183  }
184 
186 
187  // Undoing MoveNodeRange() is not working correctly in case of tables, it crashes sometimes
188  // So don't allow to undo after unfloating (similar to MakeFlyAndMove() method)
189  if (rDoc.GetIDocumentUndoRedo().DoesUndo())
190  {
192  }
193 }
194 
196 {
197  if (!m_xVirDev)
198  return;
199 
200  m_xVirDev->SetMapMode(MapMode(MapUnit::MapPixel));
202  const ::tools::Rectangle aRect(
204 
205  // Create button
206  SwFrameButtonPainter::PaintButton(aSeq, aRect, true);
207 
208  // Create the text primitive
210  basegfx::B2DVector aFontSize;
213  false, false);
214 
216  double nTextOffsetY = aFontMetric.GetAscent() + TEXT_PADDING;
217  double nTextOffsetX = std::abs(aRect.GetWidth() - m_xVirDev->GetTextWidth(m_sLabel)) / 2.0;
218  Point aTextPos(nTextOffsetX, nTextOffsetY);
219 
221  aFontSize.getX(), aFontSize.getY(), static_cast<double>(aTextPos.X()),
222  static_cast<double>(aTextPos.Y())));
223 
226  aTextMatrix, m_sLabel, 0, m_sLabel.getLength(), std::vector<double>(), aFontAttr,
227  css::lang::Locale(), aLineColor)));
228 
229  // Create the processor and process the primitives
230  const drawinglayer::geometry::ViewInformation2D aNewViewInfos;
231  std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor(
233  aNewViewInfos));
234 
235  pProcessor->process(aSeq);
236 
237  m_xPushButton->set_custom_button(m_xVirDev.get());
238 }
239 
240 void UnfloatTableButton::ShowAll(bool bShow) { Show(bShow); }
241 
242 bool UnfloatTableButton::Contains(const Point& rDocPt) const
243 {
245  if (aRect.IsInside(rDocPt))
246  return true;
247 
248  return false;
249 }
250 
251 void UnfloatTableButton::SetReadonly(bool bReadonly) { ShowAll(!bReadonly); }
252 
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
virtual Point GetPosPixel() const
const SwEndNode * EndOfSectionNode() const
Definition: node.hxx:683
void SetOffset(Point aTopRightPixel)
double getY() const
Base class of the Writer layout elements.
Definition: frame.hxx:313
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:158
static css::uno::Reference< css::text::XTextTable > CreateXTextTable(SwFrameFormat *pFrameFormat)
Definition: unotbl.cxx:2009
virtual const SwFlyFrameFormat * GetFormat() const override
Definition: fly.cxx:2877
#define BOX_DISTANCE
virtual void dispose() override
Pagedescriptor Client of SwPageDesc that is "described" by the attribute.
Definition: fmtpdsc.hxx:35
SwPageDesc * GetPageDesc()
Definition: fmtpdsc.hxx:61
virtual void SetReadonly(bool bReadonly) override
static Color & GetHeaderFooterMarkColor()
Definition: viewopt.cxx:477
const SwTable * GetTable() const
Definition: tabfrm.hxx:159
virtual void InsertItemSet(const SwPaM &rRg, const SfxItemSet &, const SetAttrMode nFlags=SetAttrMode::DEFAULT, SwRootFrame const *pLayout=nullptr)=0
virtual void SetModified()=0
Must be called manually at changes of format.
SwTabFrame is one table in the document layout, containing rows (which contain cells).
Definition: tabfrm.hxx:46
Definition: doc.hxx:187
virtual Size GetSizePixel() const
#define BUTTON_WIDTH
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:144
virtual const SwFrame * GetFrame() override
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:315
void SetMapMode()
SwTableFormat * GetFrameFormat()
Definition: swtable.hxx:203
attribute::FontAttribute getFontAttributeFromVclFont(basegfx::B2DVector &o_rSize, const vcl::Font &rFont, bool bRTL, bool bBiDiStrong)
#define UNO_NAME_TABLE_INTEROP_GRAB_BAG
Definition: unoprnms.hxx:859
virtual bool Contains(const Point &rDocPt) const override
Returns true if the point is inside the control.
tools::Long GetAscent() const
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
const vcl::Font & GetFont() const
virtual void DelLayoutFormat(SwFrameFormat *pFormat)=0
virtual void DelAllUndoObj()=0
Delete all Undo actions.
bool IsTextFrame() const
Definition: frame.hxx:1230
constexpr tools::Long GetWidth() const
B2DHomMatrix createScaleTranslateB2DHomMatrix(double fScaleX, double fScaleY, double fTranslateX, double fTranslateY)
FontMetric GetFontMetric() const
bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
virtual bool DoesUndo() const =0
Is Undo enabled?
SwFrame * AnchorFrame()
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
SwTextNode * GetTextNodeFirst()
Definition: txtfrm.hxx:457
Style of a layout element.
Definition: frmfmt.hxx:58
Window class for the Writer edit area, this is the one handling mouse and keyboard events and doing t...
Definition: edtwin.hxx:58
static void PaintButton(drawinglayer::primitive2d::Primitive2DContainer &rSeq, const tools::Rectangle &rRect, bool bOnTop)
bool SetOutputSizePixel(const Size &rNewSize, bool bErase=true)
UnfloatTableButton(SwEditWin *pEditWin, const SwFrame *pFrame)
static bool GetLayoutRTL()
const std::map< OUString, css::uno::Any > & GetGrabBag() const
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:394
bool IsInside(const Point &rPOINT) const
Marks a node in the document model.
Definition: ndindex.hxx:31
Class sharing some MenuButton code.
virtual bool MoveNodeRange(SwNodeRange &, SwNodeIndex &, SwMoveFlags)=0
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:405
virtual const SwFormatPageDesc & GetPageDescItem() const override
Definition: findfrm.cxx:670
virtual void SetPosSizePixel(const Point &rNewPos, const Size &rNewSize)
virtual ~UnfloatTableButton() override
Point PixelToLogic(const Point &rDevicePt) const
Point LogicToPixel(const Point &rLogicPt) const
std::unique_ptr< drawinglayer::processor2d::BaseProcessor2D > createBaseProcessor2DFromOutputDevice(OutputDevice &rTargetOutDev, const drawinglayer::geometry::ViewInformation2D &rViewInformation2D)
std::unique_ptr< weld::Button > m_xPushButton
constexpr TypedWhichId< SfxGrabBagItem > RES_FRMATR_GRABBAG(129)
css::uno::Reference< css::graphic::XPrimitive2D > Primitive2DReference
bool IsTabFrame() const
Definition: frame.hxx:1214
const SwPageFrame * GetPageFrame() const
general base class for all free-flowing frames
Definition: flyfrm.hxx:78
virtual void dispose() override
tools::Long GetTextWidth(const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, vcl::TextLayoutCache const *=nullptr, SalLayoutGlyphs const *const pLayoutCache=nullptr) const
SwFrame * GetLower()
Definition: findfrm.cxx:170
Sequence< sal_Int8 > aSeq
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
basegfx::BColor getBColor() const
bool GetTextBoundRect(tools::Rectangle &rRect, const OUString &rStr, sal_Int32 nBase=0, sal_Int32 nIndex=0, sal_Int32 nLen=-1, sal_uLong nLayoutWidth=0, const tools::Long *pDXArray=nullptr, const SalLayoutGlyphs *pGlyphs=nullptr) const
VclPtr< VirtualDevice > m_xVirDev
virtual void ShowAll(bool bShow) override
VirtualDevice * get() const
double getX() const
SwDoc & GetDoc()
Definition: txtfrm.hxx:460
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:120
virtual SwEditWin * GetEditWin() override
OUString SwResId(std::string_view aId)
Definition: swmodule.cxx:165
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
#define TEXT_PADDING
tools::Long GetLineHeight() const
SwTableNode * GetTableNode() const
Definition: swtable.cxx:1928
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
SwFrame * GetNext()
Definition: frame.hxx:674