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)
50  , m_sLabel(SwResId(STR_UNFLOAT_TABLE))
51 {
52 }
53 
55 
56 void UnfloatTableButton::SetOffset(Point aTopRightPixel)
57 {
58  // Compute the text size and get the box position & size from it
59  tools::Rectangle aTextRect;
60  GetTextBoundRect(aTextRect, m_sLabel);
61  tools::Rectangle aTextPxRect = LogicToPixel(aTextRect);
62  FontMetric aFontMetric = GetFontMetric(GetFont());
63  Size aBoxSize(aTextPxRect.GetWidth() + BUTTON_WIDTH + TEXT_PADDING * 2,
64  aFontMetric.GetLineHeight() + TEXT_PADDING * 2);
65 
66  Point aBoxPos(aTopRightPixel.X() - aBoxSize.Width() - BOX_DISTANCE, aTopRightPixel.Y());
67 
69  {
70  aBoxPos.setX(aTopRightPixel.X() + BOX_DISTANCE);
71  }
72 
73  // Set the position & Size of the window
74  SetPosSizePixel(aBoxPos, aBoxSize);
75 }
76 
78 {
79  assert(GetFrame()->IsFlyFrame());
80  // const_cast is needed because of bad design of ISwFrameControl and derived classes
81  SwFlyFrame* pFlyFrame = const_cast<SwFlyFrame*>(static_cast<const SwFlyFrame*>(GetFrame()));
82 
83  // Find the table inside the text frame
84  SwTabFrame* pTableFrame = nullptr;
85  SwFrame* pLower = pFlyFrame->GetLower();
86  while (pLower)
87  {
88  if (pLower->IsTabFrame())
89  {
90  pTableFrame = static_cast<SwTabFrame*>(pLower);
91  break;
92  }
93  pLower = pLower->GetNext();
94  }
95 
96  if (pTableFrame == nullptr)
97  return;
98 
99  // Insert the table at the position of the text node which has the frame anchored to
100  SwFrame* pAnchoreFrame = pFlyFrame->AnchorFrame();
101  if (pAnchoreFrame == nullptr || !pAnchoreFrame->IsTextFrame())
102  return;
103 
104  SwTextFrame* pTextFrame = static_cast<SwTextFrame*>(pAnchoreFrame);
105  if (pTextFrame->GetTextNodeFirst() == nullptr)
106  return;
107 
108  SwNodeIndex aInsertPos((*pTextFrame->GetTextNodeFirst()));
109 
110  SwTableNode* pTableNode = pTableFrame->GetTable()->GetTableNode();
111  if (pTableNode == nullptr)
112  return;
113 
114  SwDoc& rDoc = pTextFrame->GetDoc();
115 
116  // tdf#129176: clear "TablePosition" grab bag, since we explicitly change the position here
117  // See DomainMapperTableHandler::endTableGetTableStyle, where the grab bag is filled, and
118  // DocxAttributeOutput::TableDefinition that uses it on export
119  SwFrameFormat* pTableFormat = pTableFrame->GetTable()->GetFrameFormat();
120  assert(pTableFormat);
121  if (const SfxGrabBagItem* pGrabBagItem = pTableFormat->GetAttrSet().GetItem(RES_FRMATR_GRABBAG))
122  {
123  SfxGrabBagItem aGrabBagItem(*pGrabBagItem); // Editable copy
124  if (aGrabBagItem.GetGrabBag().erase("TablePosition"))
125  {
126  css::uno::Any aVal;
127  aGrabBagItem.QueryValue(aVal);
128  const auto xTable = SwXTextTable::CreateXTextTable(pTableFormat);
129  const css::uno::Reference<css::beans::XPropertySet> xSet(xTable, css::uno::UNO_QUERY);
130  assert(xSet);
131  xSet->setPropertyValue(UNO_NAME_TABLE_INTEROP_GRAB_BAG, aVal);
132  }
133  }
134 
135  // When we move the table before the first text node, we need to clear RES_PAGEDESC attribute
136  // of the text node otherwise LO will create a page break after the table
137  if (pTextFrame->GetTextNodeFirst())
138  {
139  const SwPageDesc* pPageDesc
140  = pTextFrame->GetPageDescItem().GetPageDesc(); // First text node of the page has this
141  if (pPageDesc)
142  {
143  // First set the existing page desc for the table node
144  SfxItemSet aSet(GetEditWin()->GetView().GetWrtShell().GetAttrPool(),
146  aSet.Put(SwFormatPageDesc(pPageDesc));
147  SwPaM aPaMTable(*pTableNode);
149  aPaMTable, aSet, SetAttrMode::DEFAULT, GetPageFrame()->getRootFrame());
150 
151  // Then remove pagedesc from the attributes of the text node
152  aSet.Put(SwFormatPageDesc(nullptr));
153  SwPaM aPaMTextNode(*pTextFrame->GetTextNodeFirst());
155  aPaMTextNode, aSet, SetAttrMode::DEFAULT, GetPageFrame()->getRootFrame());
156  }
157  }
158 
159  // Move the table outside of the text frame
160  SwNodeRange aRange(*pTableNode, 0, *pTableNode->EndOfSectionNode(), 1);
162 
163  // Remove the floating table's frame
164  SwFlyFrameFormat* pFrameFormat = pFlyFrame->GetFormat();
165  if (pFrameFormat)
166  {
167  rDoc.getIDocumentLayoutAccess().DelLayoutFormat(pFrameFormat);
168  }
169 
171 
172  // Undoing MoveNodeRange() is not working correctly in case of tables, it crashes sometimes
173  // So don't allow to undo after unfloating (similar to MakeFlyAndMove() method)
174  if (rDoc.GetIDocumentUndoRedo().DoesUndo())
175  {
177  }
178 }
179 
181 {
182  SetMapMode(MapMode(MapUnit::MapPixel));
184  const ::tools::Rectangle aRect(
185  ::tools::Rectangle(Point(0, 0), rRenderContext.PixelToLogic(GetSizePixel())));
186 
187  // Create button
188  SwFrameButtonPainter::PaintButton(aSeq, aRect, true);
189 
190  // Create the text primitive
192  basegfx::B2DVector aFontSize;
195  aFontSize, rRenderContext.GetFont(), false, false);
196 
197  FontMetric aFontMetric = rRenderContext.GetFontMetric(rRenderContext.GetFont());
198  double nTextOffsetY = aFontMetric.GetAscent() + TEXT_PADDING;
199  double nTextOffsetX = std::abs(aRect.GetWidth() - rRenderContext.GetTextWidth(m_sLabel)) / 2.0;
200  Point aTextPos(nTextOffsetX, nTextOffsetY);
201 
203  aFontSize.getX(), aFontSize.getY(), static_cast<double>(aTextPos.X()),
204  static_cast<double>(aTextPos.Y())));
205 
208  aTextMatrix, m_sLabel, 0, m_sLabel.getLength(), std::vector<double>(), aFontAttr,
209  css::lang::Locale(), aLineColor)));
210 
211  // Create the processor and process the primitives
212  const drawinglayer::geometry::ViewInformation2D aNewViewInfos;
213  std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor(
215  aNewViewInfos));
216 
217  pProcessor->process(aSeq);
218 }
219 
220 void UnfloatTableButton::ShowAll(bool bShow) { Show(bShow); }
221 
222 bool UnfloatTableButton::Contains(const Point& rDocPt) const
223 {
225  if (aRect.IsInside(rDocPt))
226  return true;
227 
228  return false;
229 }
230 
231 void UnfloatTableButton::SetReadonly(bool bReadonly) { ShowAll(!bReadonly); }
232 
233 /* 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:682
void SetOffset(Point aTopRightPixel)
Base class of the Writer layout elements.
Definition: frame.hxx:297
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:151
long GetWidth() const
static css::uno::Reference< css::text::XTextTable > CreateXTextTable(SwFrameFormat *pFrameFormat)
Definition: unotbl.cxx:2017
virtual const SwFlyFrameFormat * GetFormat() const override
Definition: fly.cxx:2800
#define BOX_DISTANCE
Pagedescriptor Client of SwPageDesc that is "described" by the attribute.
Definition: fmtpdsc.hxx:35
SwPageDesc * GetPageDesc()
Definition: fmtpdsc.hxx:62
virtual void SetReadonly(bool bReadonly) override
static Color & GetHeaderFooterMarkColor()
Definition: viewopt.cxx:445
const SwTable * GetTable() const
Definition: tabfrm.hxx:144
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:186
virtual Size GetSizePixel() const
#define BUTTON_WIDTH
double getY() const
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:143
virtual const SwFrame * GetFrame() override
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:314
void SetMapMode()
SwTableFormat * GetFrameFormat()
Definition: swtable.hxx:201
attribute::FontAttribute getFontAttributeFromVclFont(basegfx::B2DVector &o_rSize, const vcl::Font &rFont, bool bRTL, bool bBiDiStrong)
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 long *pDXArray=nullptr, const SalLayoutGlyphs *pGlyphs=nullptr) const
#define UNO_NAME_TABLE_INTEROP_GRAB_BAG
Definition: unoprnms.hxx:855
virtual bool Contains(const Point &rDocPt) const override
Returns true if the point is inside the control.
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:1212
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:449
Style of a layout element.
Definition: frmfmt.hxx:57
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)
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:393
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
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:178
long GetAscent() const
virtual bool MoveNodeRange(SwNodeRange &, SwNodeIndex &, SwMoveFlags)=0
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:404
virtual const SwFormatPageDesc & GetPageDescItem() const override
Definition: findfrm.cxx:669
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)
constexpr TypedWhichId< SfxGrabBagItem > RES_FRMATR_GRABBAG(129)
css::uno::Reference< css::graphic::XPrimitive2D > Primitive2DReference
bool IsTabFrame() const
Definition: frame.hxx:1196
const SwPageFrame * GetPageFrame() const
general base class for all free-flowing frames
Definition: flyfrm.hxx:60
SwFrame * GetLower()
Definition: findfrm.cxx:169
Sequence< sal_Int8 > aSeq
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
long GetTextWidth(const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, vcl::TextLayoutCache const *=nullptr, SalLayoutGlyphs const *const pLayoutCache=nullptr) const
basegfx::BColor getBColor() const
virtual void ShowAll(bool bShow) override
SwDoc & GetDoc()
Definition: txtfrm.hxx:452
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:116
virtual SwEditWin * GetEditWin() override
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
#define TEXT_PADDING
long GetLineHeight() const
SwTableNode * GetTableNode() const
Definition: swtable.cxx:1926
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
SwFrame * GetNext()
Definition: frame.hxx:656