LibreOffice Module sc (master)  1
datatableview.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <datatableview.hxx>
21 
22 #include <document.hxx>
23 #include <utility>
24 #include <viewdata.hxx>
25 #include <output.hxx>
26 #include <fillinfo.hxx>
27 #include <table.hxx>
28 
30 #include <vcl/scrbar.hxx>
31 #include <vcl/seleng.hxx>
32 #include <sal/log.hxx>
33 
34 constexpr double nPPTX = 0.06666;
35 constexpr double nPPTY = 0.06666;
36 
37 constexpr sal_uInt16 nRowHeaderWidth = 100;
38 constexpr sal_uInt16 nColHeaderHeight = 20;
39 constexpr sal_uInt16 nScrollBarSize = 10;
40 
42  ScHeaderControl(pParent, pSelectionEngine, 1024, false, nullptr),
43  mpDoc(nullptr),
44  mnCol(0)
45 {
46 }
47 
49 {
50  mpDoc = pDoc;
51 }
52 
54 {
55  mnCol = nCol;
56 }
57 
59 {
60  return mnCol;
61 }
62 
64 {
65  return ScViewData::ToPixel(mpDoc->GetColWidth(nPos, 0), nPPTX);
66 }
67 
69 {
70  return "Col: " + OUString::number(nPos + 1);
71 }
72 
74 {
75  return false;
76 }
77 
78 void ScDataTableColView::SetEntrySize(SCCOLROW nPos, sal_uInt16 nColWidth)
79 {
80  mpDoc->SetColWidthOnly(nPos, 0, nColWidth/nPPTX);
81 }
82 
84 {
85  for (SCCOLROW nCol = nPos; nCol <= nEndPos; ++nCol)
86  {
87  mpDoc->ShowCol(nCol, 0, false);
88  }
89 }
90 
91 
93  ScHeaderControl(pParent, pSelectionEngine, 1024, true, nullptr),
94  mpDoc(nullptr),
95  mnRow(0)
96 {
97 }
98 
100 {
101  mpDoc = pDoc;
102 }
103 
105 {
106  mnRow = nRow;
107 }
108 
110 {
111  return mnRow;
112 }
113 
115 {
116  return ScViewData::ToPixel(mpDoc->GetRowHeight(nPos, SCTAB(0), true), nPPTX);
117 }
118 
120 {
121  return OUString::number(nPos + 1);
122 }
123 
125 {
126  return false;
127 }
128 
129 void ScDataTableRowView::SetEntrySize(SCCOLROW nPos, sal_uInt16 nColWidth)
130 {
131  mpDoc->SetRowHeight(nPos, 0, nColWidth/nPPTX);
132 }
133 
135 {
136  for (SCCOLROW nCol = nPos; nCol <= nEndPos; ++nCol)
137  {
138  mpDoc->ShowRow(nCol, 0, false);
139  }
140 }
141 
142 ScDataTableView::ScDataTableView(const css::uno::Reference<css::awt::XWindow> &rParent) :
143  Control(VCLUnoHelper::GetWindow(rParent)),
144  mpSelectionEngine(new SelectionEngine(this)),
146  mpColView(VclPtr<ScDataTableColView>::Create(this, mpSelectionEngine.get())),
147  mpRowView(VclPtr<ScDataTableRowView>::Create(this, mpSelectionEngine.get())),
148  mpVScroll(VclPtr<ScrollBar>::Create(this, WinBits(WB_VSCROLL | WB_DRAG))),
149  mpHScroll(VclPtr<ScrollBar>::Create(this, WinBits(WB_HSCROLL | WB_DRAG))),
150  mnFirstVisibleRow(0),
151  mnFirstVisibleCol(0)
152 {
156 
158  mpVScroll->SetRangeMax(100);
159  mpVScroll->SetEndScrollHdl(LINK(this, ScDataTableView, ScrollHdl));
160 
162  mpHScroll->SetRangeMax(50);
163  mpHScroll->SetEndScrollHdl(LINK(this, ScDataTableView, ScrollHdl));
164 
165  mpTopLeft->Show();
166  mpColView->Show();
167  mpRowView->Show();
168  mpVScroll->Show();
169  mpHScroll->Show();
170 }
171 
172 void ScDataTableView::Init(std::shared_ptr<ScDocument> pDoc)
173 {
174  mpDoc = std::move(pDoc);
175  mpColView->Init(mpDoc.get());
176  mpRowView->Init(mpDoc.get());
177 }
178 
180 {
181  disposeOnce();
182 }
183 
185 {
192 }
193 
195 {
196  if (!rMEvt.IsLeft())
197  return;
198 
199  mpMouseEvent.reset(new MouseEvent(rMEvt));
200 }
201 
202 namespace {
203 
204 SCCOL findColFromPos(sal_uInt16 nPixelPos, const ScDocument* pDoc, SCCOL nStartCol = 0)
205 {
206  nPixelPos -= nRowHeaderWidth;
207  sal_uInt32 nPixelLength = 0;
208  for (SCCOL nCol : pDoc->GetColumnsRange(0, nStartCol, pDoc->MaxCol()))
209  {
210  sal_uInt16 nColWidth = pDoc->GetColWidth(nCol, 0, true);
211  sal_uInt32 nPixel = ScViewData::ToPixel(nColWidth, nPPTX);
212  nPixelLength += nPixel;
213 
214  if (nPixelLength >= nPixelPos)
215  {
216  return nCol;
217  }
218  }
219 
220  SAL_WARN("sc", "Could not find the corresponding column");
221  return -1;
222 }
223 
224 SCROW findRowFromPos(sal_uInt16 nPixelPos, const ScDocument* pDoc, SCROW nStartRow = 0)
225 {
226  nPixelPos -= nColHeaderHeight;
227  sal_uInt32 nPixelLength = 0;
228  for (SCROW nRow = nStartRow; nRow <= pDoc->MaxRow(); ++nRow)
229  {
230  sal_uInt16 nColWidth = pDoc->GetRowHeight(nRow, SCTAB(0), true);
231  sal_uInt32 nPixel = ScViewData::ToPixel(nColWidth, nPPTX);
232  nPixelLength += nPixel;
233 
234  if (nPixelLength >= nPixelPos)
235  {
236  return nRow;
237  }
238  }
239 
240  SAL_WARN("sc", "Could not find the corresponding row");
241  return -1;
242 }
243 
244 }
245 
247 {
248  if (!rMEvt.IsLeft())
249  return;
250  if (!mpMouseEvent) // tdf#120528 The event originated in another window, like context menu
251  return;
252 
253  SCCOL nStartCol = findColFromPos(mpMouseEvent->GetPosPixel().getX(), mpDoc.get());
254  SCCOL nEndCol = findColFromPos(rMEvt.GetPosPixel().getX(), mpDoc.get());
255  SCROW nStartRow = findRowFromPos(mpMouseEvent->GetPosPixel().getY(), mpDoc.get());
256  SCROW nEndRow = findRowFromPos(rMEvt.GetPosPixel().getY(), mpDoc.get());
257  PutInOrder(nStartCol, nEndCol);
258  PutInOrder(nStartRow, nEndRow);
259  mpColView->SetMark(true, nStartCol, nEndCol);
260  mpRowView->SetMark(true, nStartRow, nEndRow);
261 
262  mpMouseEvent.reset();
263 }
264 
266 {
267  Size aSize = GetSizePixel();
271 
274 }
275 
276 void ScDataTableView::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRectangle)
277 {
278  Size aSize = GetSizePixel();
279  SCCOL nMaxVisibleCol = findColFromPos(aSize.Width() - nScrollBarSize, mpDoc.get(), mnFirstVisibleCol);
280  SCROW nMaxVisibleRow = findRowFromPos(aSize.Height(), mpDoc.get(), mnFirstVisibleRow);
281 
282  ScTableInfo aTableInfo;
283  mpDoc->FillInfo(aTableInfo, mnFirstVisibleCol, mnFirstVisibleRow, nMaxVisibleCol, nMaxVisibleRow, 0, 0.06666, 0.06666, false, false);
284  ScOutputData aOutput(&rRenderContext, OUTTYPE_WINDOW, aTableInfo, mpDoc.get(), 0,
285  nRowHeaderWidth, nColHeaderHeight, mnFirstVisibleCol, mnFirstVisibleRow, nMaxVisibleCol, nMaxVisibleRow, nPPTX, nPPTY);
286 
287  aOutput.SetGridColor(COL_BLACK);
288  aOutput.SetSolidBackground(true);
289  aOutput.DrawClear();
290  aOutput.DrawDocumentBackground();
291  aOutput.DrawGrid(rRenderContext, true, false);
292  aOutput.DrawStrings();
293  Control::Paint(rRenderContext, rRectangle);
294 }
295 
297 {
298  return Size(600, 400);
299 }
300 
301 void ScDataTableView::getColRange(SCCOL& rStartCol, SCCOL& rEndCol) const
302 {
303  SCCOLROW aStart = 0;
304  SCCOLROW aEnd = 0;
305  mpColView->GetMarkRange(aStart, aEnd);
306  rStartCol = static_cast<SCCOL>(aStart);
307  rEndCol = static_cast<SCCOL>(aEnd);
308 }
309 
310 void ScDataTableView::getRowRange(SCROW& rStartCol, SCROW& rEndCol) const
311 {
312  SCCOLROW aStart = 0;
313  SCCOLROW aEnd = 0;
314  mpRowView->GetMarkRange(aStart, aEnd);
315  rStartCol = static_cast<SCROW>(aStart);
316  rEndCol = static_cast<SCROW>(aEnd);
317 }
318 
319 IMPL_LINK(ScDataTableView, ScrollHdl, ScrollBar*, pScrollBar, void)
320 {
321  if (pScrollBar == mpVScroll.get())
322  {
323  mnFirstVisibleRow = pScrollBar->GetThumbPos();
324  pScrollBar->SetRangeMax(mnFirstVisibleRow + 100);
325  mpRowView->SetPos(mnFirstVisibleRow);
326  }
327  else
328  {
329  mnFirstVisibleCol = pScrollBar->GetThumbPos();
330  pScrollBar->SetRangeMax(mnFirstVisibleCol + 50);
331  mpColView->SetPos(mnFirstVisibleCol);
332  }
333  Invalidate();
334 }
335 
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
long Width() const
SC_DLLPUBLIC void ShowCol(SCCOL nCol, SCTAB nTab, bool bShow)
Definition: document.cxx:4300
constexpr double nPPTY
virtual bool IsLayoutRTL() const override
void SetRangeMin(long nNewRange)
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
void Init(std::shared_ptr< ScDocument > pDoc)
void Init(ScDocument *pDoc)
virtual void HideEntries(SCCOLROW nPos, SCCOLROW nEndPos) override
VclPtr< ScrollBar > mpHScroll
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
VclPtr< ScDataTableRowView > mpRowView
void getColRange(SCCOL &rStartCol, SCCOL &rEndCol) const
virtual sal_uInt16 GetEntrySize(SCCOLROW nPos) const override
long Height() const
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
virtual Size GetSizePixel() const
sal_Int32 mnCol
void SetMark(bool bNewSet, SCCOLROW nNewStart, SCCOLROW nNewEnd)
Definition: hdrcont.cxx:131
void GetMarkRange(SCCOLROW &rStart, SCCOLROW &rEnd) const
Definition: hdrcont.cxx:1005
constexpr sal_uInt16 nColHeaderHeight
WinBits const WB_VSCROLL
virtual void dispose() override
SC_DLLPUBLIC sal_uInt16 GetRowHeight(SCROW nRow, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4163
virtual void SetEntrySize(SCCOLROW nPos, sal_uInt16 nWidth) override
IMPL_LINK(ScDataTableView, ScrollHdl, ScrollBar *, pScrollBar, void)
virtual void setPosSizePixel(long nX, long nY, long nWidth, long nHeight, PosSizeFlags nFlags=PosSizeFlags::All)
sal_Int64 WinBits
SharedBitmapDescriptor mpTopLeft
ScDataTableRowView(vcl::Window *pParent, SelectionEngine *pSelectionEngine)
SC_DLLPUBLIC SCROW MaxRow() const
Definition: document.hxx:876
sal_Int32 SCCOLROW
a type capable of holding either SCCOL or SCROW
Definition: types.hxx:24
VclPtr< ScrollBar > mpVScroll
WinBits const WB_HSCROLL
SC_DLLPUBLIC void SetColWidthOnly(SCCOL nCol, SCTAB nTab, sal_uInt16 nNewWidth)
Definition: document.cxx:4091
virtual void Resize() override
void PutInOrder(T &nStart, T &nEnd)
Definition: address.hxx:954
constexpr double nPPTX
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:875
#define nPixel
std::unique_ptr< MouseEvent > mpMouseEvent
constexpr sal_uInt16 nRowHeaderWidth
~ScDataTableView() override
sal_Int16 SCCOL
Definition: types.hxx:22
void SetRangeMax(long nNewRange)
std::shared_ptr< ScDocument > mpDoc
VclPtr< ScrollBarBox > mpTopLeft
void SetPos(SCCOLROW nRow)
SC_DLLPUBLIC void SetRowHeight(SCROW nRow, SCTAB nTab, sal_uInt16 nNewHeight)
Definition: document.cxx:4097
virtual sal_uInt16 GetEntrySize(SCCOLROW nPos) const override
virtual void dispose() override
void Init(ScDocument *pDoc)
virtual OUString GetEntryText(SCCOLROW nPos) const override
void SetPos(SCCOLROW nRow)
void SetGridColor(const Color &rColor)
Definition: output.cxx:235
void SetEndScrollHdl(const Link< ScrollBar *, void > &rLink)
constexpr sal_uInt16 nScrollBarSize
virtual SCCOLROW GetPos() const override
ScDocument * mpDoc
WinBits const WB_SIZEABLE
sal_Int32 SCROW
Definition: types.hxx:18
virtual bool IsLayoutRTL() const override
virtual void HideEntries(SCCOLROW nPos, SCCOLROW nEndPos) override
ScDataTableColView(vcl::Window *pParent, SelectionEngine *pSelectionEngine)
VclPtr< ScDataTableColView > mpColView
ScDocument * mpDoc
bool IsLeft() const
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect)
ScDataTableView(const css::uno::Reference< css::awt::XWindow > &rParent)
const Point & GetPosPixel() const
virtual OUString GetEntryText(SCCOLROW nPos) const override
virtual void SetEntrySize(SCCOLROW nPos, sal_uInt16 nWidth) override
SC_DLLPUBLIC ScColumnsRange GetColumnsRange(SCTAB nTab, SCCOL nColBegin, SCCOL nColEnd) const
Definition: document.cxx:2523
virtual SCCOLROW GetPos() const override
virtual void MouseButtonUp(const MouseEvent &rMEvt) override
virtual Size GetOptimalSize() const override
#define SAL_WARN(area, stream)
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
sal_Int32 mnRow
void getRowRange(SCROW &rStartRow, SCROW &rEndRow) const
SC_DLLPUBLIC sal_uInt16 GetColWidth(SCCOL nCol, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4122
static long ToPixel(sal_uInt16 nTwips, double nFactor)
Definition: viewdata.hxx:686
SC_DLLPUBLIC void ShowRow(SCROW nRow, SCTAB nTab, bool bShow)
Definition: document.cxx:4306
sal_Int16 SCTAB
Definition: types.hxx:23
WinBits const WB_DRAG
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)