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 <IDocumentState.hxx>
30 #include <IDocumentUndoRedo.hxx>
40 
41 #define TEXT_PADDING 3
42 #define BOX_DISTANCE 3
43 #define BUTTON_WIDTH 12
44 
46  : SwFrameMenuButtonBase(pEditWin, pFrame)
47  , m_sLabel(SwResId(STR_UNFLOAT_TABLE))
48 {
49 }
50 
52 
54 {
55  // Compute the text size and get the box position & size from it
56  tools::Rectangle aTextRect;
57  GetTextBoundRect(aTextRect, m_sLabel);
58  tools::Rectangle aTextPxRect = LogicToPixel(aTextRect);
59  FontMetric aFontMetric = GetFontMetric(GetFont());
60  Size aBoxSize(aTextPxRect.GetWidth() + BUTTON_WIDTH + TEXT_PADDING * 2,
61  aFontMetric.GetLineHeight() + TEXT_PADDING * 2);
62 
63  Point aBoxPos(aTopRightPixel.X() - aBoxSize.Width() - BOX_DISTANCE, aTopRightPixel.Y());
64 
66  {
67  aBoxPos.setX(aTopRightPixel.X() + BOX_DISTANCE);
68  }
69 
70  // Set the position & Size of the window
71  SetPosSizePixel(aBoxPos, aBoxSize);
72 }
73 
75 {
76  assert(GetFrame()->IsFlyFrame());
77  // const_cast is needed because of bad design of ISwFrameControl and derived classes
78  SwFlyFrame* pFlyFrame = const_cast<SwFlyFrame*>(static_cast<const SwFlyFrame*>(GetFrame()));
79 
80  // Find the table inside the text frame
81  SwTabFrame* pTableFrame = nullptr;
82  SwFrame* pLower = pFlyFrame->GetLower();
83  while (pLower)
84  {
85  if (pLower->IsTabFrame())
86  {
87  pTableFrame = static_cast<SwTabFrame*>(pLower);
88  break;
89  }
90  pLower = pLower->GetNext();
91  }
92 
93  if (pTableFrame == nullptr)
94  return;
95 
96  // Insert the table at the position of the text node which has the frame anchored to
97  SwFrame* pAnchoreFrame = pFlyFrame->AnchorFrame();
98  if (pAnchoreFrame == nullptr || !pAnchoreFrame->IsTextFrame())
99  return;
100 
101  SwTextFrame* pTextFrame = static_cast<SwTextFrame*>(pAnchoreFrame);
102  if (pTextFrame->GetTextNodeFirst() == nullptr)
103  return;
104 
105  SwNodeIndex aInsertPos((*pTextFrame->GetTextNodeFirst()));
106 
107  SwTableNode* pTableNode = pTableFrame->GetTable()->GetTableNode();
108  if (pTableNode == nullptr)
109  return;
110 
111  SwDoc& rDoc = pTextFrame->GetDoc();
112 
113  // When we move the table before the first text node, we need to clear RES_PAGEDESC attribute
114  // of the text node otherwise LO will create a page break after the table
115  if (pTextFrame->GetTextNodeFirst())
116  {
117  const SwPageDesc* pPageDesc
118  = pTextFrame->GetPageDescItem().GetPageDesc(); // First text node of the page has this
119  if (pPageDesc)
120  {
121  // First set the existing page desc for the table node
122  SfxItemSet aSet(GetEditWin()->GetView().GetWrtShell().GetAttrPool(),
124  aSet.Put(SwFormatPageDesc(pPageDesc));
125  SwPaM aPaMTable(*pTableNode);
127  aPaMTable, aSet, SetAttrMode::DEFAULT, GetPageFrame()->getRootFrame());
128 
129  // Then remove pagedesc from the attributes of the text node
130  aSet.Put(SwFormatPageDesc(nullptr));
131  SwPaM aPaMTextNode(*pTextFrame->GetTextNodeFirst());
133  aPaMTextNode, aSet, SetAttrMode::DEFAULT, GetPageFrame()->getRootFrame());
134  }
135  }
136 
137  // Move the table outside of the text frame
138  SwNodeRange aRange(*pTableNode, 0, *pTableNode->EndOfSectionNode(), 1);
140 
141  // Remove the floating table's frame
142  SwFlyFrameFormat* pFrameFormat = pFlyFrame->GetFormat();
143  if (pFrameFormat)
144  {
145  rDoc.getIDocumentLayoutAccess().DelLayoutFormat(pFrameFormat);
146  }
147 
149 
150  // Undoing MoveNodeRange() is not working correctly in case of tables, it crashes sometimes
151  // So don't allow to undo after unfloating (similar to MakeFlyAndMove() method)
152  if (rDoc.GetIDocumentUndoRedo().DoesUndo())
153  {
155  }
156 }
157 
159 {
160  SetMapMode(MapMode(MapUnit::MapPixel));
162  const ::tools::Rectangle aRect(
163  ::tools::Rectangle(Point(0, 0), rRenderContext.PixelToLogic(GetSizePixel())));
164 
165  // Create button
166  SwFrameButtonPainter::PaintButton(aSeq, aRect, true);
167 
168  // Create the text primitive
170  basegfx::B2DVector aFontSize;
173  aFontSize, rRenderContext.GetFont(), false, false);
174 
175  FontMetric aFontMetric = rRenderContext.GetFontMetric(rRenderContext.GetFont());
176  double nTextOffsetY = aFontMetric.GetAscent() + TEXT_PADDING;
177  double nTextOffsetX = std::abs(aRect.GetWidth() - rRenderContext.GetTextWidth(m_sLabel)) / 2.0;
178  Point aTextPos(nTextOffsetX, nTextOffsetY);
179 
181  aFontSize.getX(), aFontSize.getY(), static_cast<double>(aTextPos.X()),
182  static_cast<double>(aTextPos.Y())));
183 
186  aTextMatrix, m_sLabel, 0, m_sLabel.getLength(), std::vector<double>(), aFontAttr,
187  css::lang::Locale(), aLineColor)));
188 
189  // Create the processor and process the primitives
190  const drawinglayer::geometry::ViewInformation2D aNewViewInfos;
191  std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor(
193  aNewViewInfos));
194 
195  pProcessor->process(aSeq);
196 }
197 
198 void UnfloatTableButton::ShowAll(bool bShow) { Show(bShow); }
199 
200 bool UnfloatTableButton::Contains(const Point& rDocPt) const
201 {
203  if (aRect.IsInside(rDocPt))
204  return true;
205 
206  return false;
207 }
208 
209 void UnfloatTableButton::SetReadonly(bool bReadonly) { ShowAll(!bReadonly); }
210 
211 /* 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
virtual const SwFlyFrameFormat * GetFormat() const override
Definition: fly.cxx:2814
#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:447
const SwTable * GetTable() const
Definition: tabfrm.hxx:142
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:176
virtual const SwFrame * GetFrame() override
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:347
void SetMapMode()
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
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
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:445
Window class for the Writer edit area, this is the one handling mouse and keyboard events and doing t...
Definition: edtwin.hxx:59
static void PaintButton(drawinglayer::primitive2d::Primitive2DContainer &rSeq, const tools::Rectangle &rRect, bool bOnTop)
UnfloatTableButton(SwEditWin *pEditWin, const SwFrame *pFrame)
static bool GetLayoutRTL()
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:426
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:191
long GetAscent() const
virtual bool MoveNodeRange(SwNodeRange &, SwNodeIndex &, SwMoveFlags)=0
long X() const
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:437
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)
bool IsTabFrame() const
Definition: frame.hxx:1194
general base class for all free-flowing frames
Definition: flyfrm.hxx:60
SwFrame * GetLower()
Definition: findfrm.cxx:169
css::uno::Reference< css::graphic::XPrimitive2D > Primitive2DReference
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
const SwPageFrame * GetPageFrame()
virtual void ShowAll(bool bShow) override
SwDoc & GetDoc()
Definition: txtfrm.hxx:448
virtual SwEditWin * GetEditWin() override
#define TEXT_PADDING
long Y() const
long GetLineHeight() const
SwTableNode * GetTableNode() const
Definition: swtable.cxx:1927
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
SwFrame * GetNext()
Definition: frame.hxx:654