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();
65 }
66 
67 void UnfloatTableButton::SetOffset(Point aTopRightPixel)
68 {
69  // Compute the text size and get the box position & size from it
70  tools::Rectangle aTextRect;
71  m_xVirDev->GetTextBoundRect(aTextRect, m_sLabel);
72  tools::Rectangle aTextPxRect = m_xVirDev->LogicToPixel(aTextRect);
74  Size aBoxSize(aTextPxRect.GetWidth() + BUTTON_WIDTH + TEXT_PADDING * 2,
75  aFontMetric.GetLineHeight() + TEXT_PADDING * 2);
76 
77  Point aBoxPos(aTopRightPixel.X() - aBoxSize.Width() - BOX_DISTANCE, aTopRightPixel.Y());
78 
80  {
81  aBoxPos.setX(aTopRightPixel.X() + BOX_DISTANCE);
82  }
83 
84  // Set the position & Size of the window
85  SetPosSizePixel(aBoxPos, aBoxSize);
86  m_xVirDev->SetOutputSizePixel(aBoxSize);
87 
88  PaintButton();
89 }
90 
92 {
93  assert(GetFrame()->IsFlyFrame());
94  // const_cast is needed because of bad design of ISwFrameControl and derived classes
95  SwFlyFrame* pFlyFrame = const_cast<SwFlyFrame*>(static_cast<const SwFlyFrame*>(GetFrame()));
96 
97  // Find the table inside the text frame
98  SwTabFrame* pTableFrame = nullptr;
99  SwFrame* pLower = pFlyFrame->GetLower();
100  while (pLower)
101  {
102  if (pLower->IsTabFrame())
103  {
104  pTableFrame = static_cast<SwTabFrame*>(pLower);
105  break;
106  }
107  pLower = pLower->GetNext();
108  }
109 
110  if (pTableFrame == nullptr)
111  return;
112 
113  // Insert the table at the position of the text node which has the frame anchored to
114  SwFrame* pAnchoreFrame = pFlyFrame->AnchorFrame();
115  if (pAnchoreFrame == nullptr || !pAnchoreFrame->IsTextFrame())
116  return;
117 
118  SwTextFrame* pTextFrame = static_cast<SwTextFrame*>(pAnchoreFrame);
119  if (pTextFrame->GetTextNodeFirst() == nullptr)
120  return;
121 
122  SwNodeIndex aInsertPos((*pTextFrame->GetTextNodeFirst()));
123 
124  SwTableNode* pTableNode = pTableFrame->GetTable()->GetTableNode();
125  if (pTableNode == nullptr)
126  return;
127 
128  SwDoc& rDoc = pTextFrame->GetDoc();
129 
130  // tdf#129176: clear "TablePosition" grab bag, since we explicitly change the position here
131  // See DomainMapperTableHandler::endTableGetTableStyle, where the grab bag is filled, and
132  // DocxAttributeOutput::TableDefinition that uses it on export
133  SwFrameFormat* pTableFormat = pTableFrame->GetTable()->GetFrameFormat();
134  assert(pTableFormat);
135  if (const SfxGrabBagItem* pGrabBagItem = pTableFormat->GetAttrSet().GetItem(RES_FRMATR_GRABBAG))
136  {
137  SfxGrabBagItem aGrabBagItem(*pGrabBagItem); // Editable copy
138  if (aGrabBagItem.GetGrabBag().erase("TablePosition"))
139  {
140  css::uno::Any aVal;
141  aGrabBagItem.QueryValue(aVal);
142  const auto xTable = SwXTextTable::CreateXTextTable(pTableFormat);
143  const css::uno::Reference<css::beans::XPropertySet> xSet(xTable, css::uno::UNO_QUERY);
144  assert(xSet);
145  xSet->setPropertyValue(UNO_NAME_TABLE_INTEROP_GRAB_BAG, aVal);
146  }
147  }
148 
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())
152  {
153  const SwPageDesc* pPageDesc
154  = pTextFrame->GetPageDescItem().GetPageDesc(); // First text node of the page has this
155  if (pPageDesc)
156  {
157  // First set the existing page desc for the table node
158  SfxItemSet aSet(GetEditWin()->GetView().GetWrtShell().GetAttrPool(),
160  aSet.Put(SwFormatPageDesc(pPageDesc));
161  SwPaM aPaMTable(*pTableNode);
163  aPaMTable, aSet, SetAttrMode::DEFAULT, GetPageFrame()->getRootFrame());
164 
165  // Then remove pagedesc from the attributes of the text node
166  aSet.Put(SwFormatPageDesc(nullptr));
167  SwPaM aPaMTextNode(*pTextFrame->GetTextNodeFirst());
169  aPaMTextNode, aSet, SetAttrMode::DEFAULT, GetPageFrame()->getRootFrame());
170  }
171  }
172 
173  // Move the table outside of the text frame
174  SwNodeRange aRange(*pTableNode, 0, *pTableNode->EndOfSectionNode(), 1);
176 
177  // Remove the floating table's frame
178  SwFlyFrameFormat* pFrameFormat = pFlyFrame->GetFormat();
179  if (pFrameFormat)
180  {
181  rDoc.getIDocumentLayoutAccess().DelLayoutFormat(pFrameFormat);
182  }
183 
185 
186  // Undoing MoveNodeRange() is not working correctly in case of tables, it crashes sometimes
187  // So don't allow to undo after unfloating (similar to MakeFlyAndMove() method)
188  if (rDoc.GetIDocumentUndoRedo().DoesUndo())
189  {
191  }
192 }
193 
195 {
196  if (!m_xVirDev)
197  return;
198 
199  m_xVirDev->SetMapMode(MapMode(MapUnit::MapPixel));
201  const ::tools::Rectangle aRect(
203 
204  // Create button
205  SwFrameButtonPainter::PaintButton(aSeq, aRect, true);
206 
207  // Create the text primitive
209  basegfx::B2DVector aFontSize;
212  false, false);
213 
215  double nTextOffsetY = aFontMetric.GetAscent() + TEXT_PADDING;
216  double nTextOffsetX = std::abs(aRect.GetWidth() - m_xVirDev->GetTextWidth(m_sLabel)) / 2.0;
217  Point aTextPos(nTextOffsetX, nTextOffsetY);
218 
220  aFontSize.getX(), aFontSize.getY(), static_cast<double>(aTextPos.X()),
221  static_cast<double>(aTextPos.Y())));
222 
225  aTextMatrix, m_sLabel, 0, m_sLabel.getLength(), std::vector<double>(), aFontAttr,
226  css::lang::Locale(), aLineColor)));
227 
228  // Create the processor and process the primitives
229  const drawinglayer::geometry::ViewInformation2D aNewViewInfos;
230  std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor(
232  aNewViewInfos));
233 
234  pProcessor->process(aSeq);
235 
236  m_xPushButton->set_custom_button(m_xVirDev.get());
237 }
238 
239 void UnfloatTableButton::ShowAll(bool bShow) { Show(bShow); }
240 
241 bool UnfloatTableButton::Contains(const Point& rDocPt) const
242 {
244  if (aRect.IsInside(rDocPt))
245  return true;
246 
247  return false;
248 }
249 
250 void UnfloatTableButton::SetReadonly(bool bReadonly) { ShowAll(!bReadonly); }
251 
252 /* 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:686
void SetOffset(Point aTopRightPixel)
Base class of the Writer layout elements.
Definition: frame.hxx:298
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:157
static css::uno::Reference< css::text::XTextTable > CreateXTextTable(SwFrameFormat *pFrameFormat)
Definition: unotbl.cxx:2010
virtual const SwFlyFrameFormat * GetFormat() const override
Definition: fly.cxx:2859
#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:143
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:30
double getX() const
Definition: doc.hxx:187
virtual Size GetSizePixel() const
#define BUTTON_WIDTH
double getY() const
tools::Long GetWidth() const
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:858
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:1215
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:454
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.
OUString SwResId(const char *pId)
Definition: swmodule.cxx:166
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:1199
const SwPageFrame * GetPageFrame() const
general base class for all free-flowing frames
Definition: flyfrm.hxx:60
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
SwDoc & GetDoc()
Definition: txtfrm.hxx:457
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:120
virtual SwEditWin * GetEditWin() override
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
#define TEXT_PADDING
tools::Long GetLineHeight() const
SwTableNode * GetTableNode() const
Definition: swtable.cxx:1913
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
SwFrame * GetNext()
Definition: frame.hxx:659