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 
43 #define TEXT_PADDING 3
44 #define BOX_DISTANCE 3
45 #define BUTTON_WIDTH 12
46 
48  : SwFrameMenuButtonBase(pEditWin, pFrame)
49  , m_sLabel(SwResId(STR_UNFLOAT_TABLE))
50 {
51 }
52 
54 
56 {
57  // Compute the text size and get the box position & size from it
58  tools::Rectangle aTextRect;
59  GetTextBoundRect(aTextRect, m_sLabel);
60  tools::Rectangle aTextPxRect = LogicToPixel(aTextRect);
61  FontMetric aFontMetric = GetFontMetric(GetFont());
62  Size aBoxSize(aTextPxRect.GetWidth() + BUTTON_WIDTH + TEXT_PADDING * 2,
63  aFontMetric.GetLineHeight() + TEXT_PADDING * 2);
64 
65  Point aBoxPos(aTopRightPixel.X() - aBoxSize.Width() - BOX_DISTANCE, aTopRightPixel.Y());
66 
68  {
69  aBoxPos.setX(aTopRightPixel.X() + BOX_DISTANCE);
70  }
71 
72  // Set the position & Size of the window
73  SetPosSizePixel(aBoxPos, aBoxSize);
74 }
75 
77 {
78  assert(GetFrame()->IsFlyFrame());
79  // const_cast is needed because of bad design of ISwFrameControl and derived classes
80  SwFlyFrame* pFlyFrame = const_cast<SwFlyFrame*>(static_cast<const SwFlyFrame*>(GetFrame()));
81 
82  // Find the table inside the text frame
83  SwTabFrame* pTableFrame = nullptr;
84  SwFrame* pLower = pFlyFrame->GetLower();
85  while (pLower)
86  {
87  if (pLower->IsTabFrame())
88  {
89  pTableFrame = static_cast<SwTabFrame*>(pLower);
90  break;
91  }
92  pLower = pLower->GetNext();
93  }
94 
95  if (pTableFrame == nullptr)
96  return;
97 
98  // Insert the table at the position of the text node which has the frame anchored to
99  SwFrame* pAnchoreFrame = pFlyFrame->AnchorFrame();
100  if (pAnchoreFrame == nullptr || !pAnchoreFrame->IsTextFrame())
101  return;
102 
103  SwTextFrame* pTextFrame = static_cast<SwTextFrame*>(pAnchoreFrame);
104  if (pTextFrame->GetTextNodeFirst() == nullptr)
105  return;
106 
107  SwNodeIndex aInsertPos((*pTextFrame->GetTextNodeFirst()));
108 
109  SwTableNode* pTableNode = pTableFrame->GetTable()->GetTableNode();
110  if (pTableNode == nullptr)
111  return;
112 
113  SwDoc& rDoc = pTextFrame->GetDoc();
114 
115  // tdf#129176: clear "TablePosition" grab bag, since we explicitly change the position here
116  // See DomainMapperTableHandler::endTableGetTableStyle, where the grab bag is filled, and
117  // DocxAttributeOutput::TableDefinition that uses it on export
118  SwFrameFormat* pTableFormat = pTableFrame->GetTable()->GetFrameFormat();
119  assert(pTableFormat);
120  if (const SfxGrabBagItem* pGrabBagItem = pTableFormat->GetAttrSet().GetItem(RES_FRMATR_GRABBAG))
121  {
122  SfxGrabBagItem aGrabBagItem(*pGrabBagItem); // Editable copy
123  if (aGrabBagItem.GetGrabBag().erase("TablePosition"))
124  {
125  css::uno::Any aVal;
126  aGrabBagItem.QueryValue(aVal);
127  const auto xTable = SwXTextTable::CreateXTextTable(pTableFormat);
128  const css::uno::Reference<css::beans::XPropertySet> xSet(xTable, css::uno::UNO_QUERY);
129  assert(xSet);
130  xSet->setPropertyValue(UNO_NAME_TABLE_INTEROP_GRAB_BAG, aVal);
131  }
132  }
133 
134  // When we move the table before the first text node, we need to clear RES_PAGEDESC attribute
135  // of the text node otherwise LO will create a page break after the table
136  if (pTextFrame->GetTextNodeFirst())
137  {
138  const SwPageDesc* pPageDesc
139  = pTextFrame->GetPageDescItem().GetPageDesc(); // First text node of the page has this
140  if (pPageDesc)
141  {
142  // First set the existing page desc for the table node
143  SfxItemSet aSet(GetEditWin()->GetView().GetWrtShell().GetAttrPool(),
145  aSet.Put(SwFormatPageDesc(pPageDesc));
146  SwPaM aPaMTable(*pTableNode);
148  aPaMTable, aSet, SetAttrMode::DEFAULT, GetPageFrame()->getRootFrame());
149 
150  // Then remove pagedesc from the attributes of the text node
151  aSet.Put(SwFormatPageDesc(nullptr));
152  SwPaM aPaMTextNode(*pTextFrame->GetTextNodeFirst());
154  aPaMTextNode, aSet, SetAttrMode::DEFAULT, GetPageFrame()->getRootFrame());
155  }
156  }
157 
158  // Move the table outside of the text frame
159  SwNodeRange aRange(*pTableNode, 0, *pTableNode->EndOfSectionNode(), 1);
161 
162  // Remove the floating table's frame
163  SwFlyFrameFormat* pFrameFormat = pFlyFrame->GetFormat();
164  if (pFrameFormat)
165  {
166  rDoc.getIDocumentLayoutAccess().DelLayoutFormat(pFrameFormat);
167  }
168 
170 
171  // Undoing MoveNodeRange() is not working correctly in case of tables, it crashes sometimes
172  // So don't allow to undo after unfloating (similar to MakeFlyAndMove() method)
173  if (rDoc.GetIDocumentUndoRedo().DoesUndo())
174  {
176  }
177 }
178 
180 {
181  SetMapMode(MapMode(MapUnit::MapPixel));
183  const ::tools::Rectangle aRect(
184  ::tools::Rectangle(Point(0, 0), rRenderContext.PixelToLogic(GetSizePixel())));
185 
186  // Create button
187  SwFrameButtonPainter::PaintButton(aSeq, aRect, true);
188 
189  // Create the text primitive
191  basegfx::B2DVector aFontSize;
194  aFontSize, rRenderContext.GetFont(), false, false);
195 
196  FontMetric aFontMetric = rRenderContext.GetFontMetric(rRenderContext.GetFont());
197  double nTextOffsetY = aFontMetric.GetAscent() + TEXT_PADDING;
198  double nTextOffsetX = std::abs(aRect.GetWidth() - rRenderContext.GetTextWidth(m_sLabel)) / 2.0;
199  Point aTextPos(nTextOffsetX, nTextOffsetY);
200 
202  aFontSize.getX(), aFontSize.getY(), static_cast<double>(aTextPos.X()),
203  static_cast<double>(aTextPos.Y())));
204 
207  aTextMatrix, m_sLabel, 0, m_sLabel.getLength(), std::vector<double>(), aFontAttr,
208  css::lang::Locale(), aLineColor)));
209 
210  // Create the processor and process the primitives
211  const drawinglayer::geometry::ViewInformation2D aNewViewInfos;
212  std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor(
214  aNewViewInfos));
215 
216  pProcessor->process(aSeq);
217 }
218 
219 void UnfloatTableButton::ShowAll(bool bShow) { Show(bShow); }
220 
221 bool UnfloatTableButton::Contains(const Point& rDocPt) const
222 {
224  if (aRect.IsInside(rDocPt))
225  return true;
226 
227  return false;
228 }
229 
230 void UnfloatTableButton::SetReadonly(bool bReadonly) { ShowAll(!bReadonly); }
231 
232 /* 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:295
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:149
long GetWidth() const
static css::uno::Reference< css::text::XTextTable > CreateXTextTable(SwFrameFormat *pFrameFormat)
Definition: unotbl.cxx:2138
virtual const SwFlyFrameFormat * GetFormat() const override
Definition: fly.cxx:2810
#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
#define RES_FRMATR_GRABBAG
Definition: hintids.hxx:329
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:185
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:854
virtual bool Contains(const Point &rDocPt) const override
Returns true if the point is inside the control.
void setX(long nX)
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:1210
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:447
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
long X() const
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)
css::uno::Reference< css::graphic::XPrimitive2D > Primitive2DReference
bool IsTabFrame() const
Definition: frame.hxx:1194
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:450
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 Y() const
long GetLineHeight() const
SwTableNode * GetTableNode() const
Definition: swtable.cxx:1926
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
SwFrame * GetNext()
Definition: frame.hxx:654