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
34constexpr double nPPTX = 0.06666;
35constexpr double nPPTY = 0.06666;
36
37constexpr sal_uInt16 nRowHeaderWidth = 100;
38constexpr sal_uInt16 nColHeaderHeight = 20;
39constexpr sal_uInt16 nScrollBarSize = 10;
40
42 ScHeaderControl(pParent, pSelectionEngine, pDoc->MaxCol()+1, false, nullptr),
43 mpDoc(pDoc),
44 mnCol(0)
45{
46}
47
49{
50 mnCol = nCol;
51}
52
54{
55 return mnCol;
56}
57
59{
61}
62
64{
65 return "Col: " + OUString::number(nPos + 1);
66}
67
69{
70 return false;
71}
72
73void ScDataTableColView::SetEntrySize(SCCOLROW nPos, sal_uInt16 nColWidth)
74{
75 mpDoc->SetColWidthOnly(nPos, 0, nColWidth/nPPTX);
76}
77
79{
80 for (SCCOLROW nCol = nPos; nCol <= nEndPos; ++nCol)
81 {
82 mpDoc->ShowCol(nCol, 0, false);
83 }
84}
85
86
88 ScHeaderControl(pParent, pSelectionEngine, pDoc->MaxRow()+1, true, nullptr),
89 mpDoc(pDoc),
90 mnRow(0)
91{
92}
93
95{
96 mnRow = nRow;
97}
98
100{
101 return mnRow;
102}
103
105{
107}
108
110{
111 return OUString::number(nPos + 1);
112}
113
115{
116 return false;
117}
118
119void ScDataTableRowView::SetEntrySize(SCCOLROW nPos, sal_uInt16 nColWidth)
120{
121 mpDoc->SetRowHeight(nPos, 0, nColWidth/nPPTX);
122}
123
125{
126 for (SCCOLROW nCol = nPos; nCol <= nEndPos; ++nCol)
127 {
128 mpDoc->ShowRow(nCol, 0, false);
129 }
130}
131
132ScDataTableView::ScDataTableView(const css::uno::Reference<css::awt::XWindow> &rParent, std::shared_ptr<ScDocument> pDoc) :
133 Control(VCLUnoHelper::GetWindow(rParent)),
134 mpDoc(std::move(pDoc)),
135 mpSelectionEngine(new SelectionEngine(this)),
137 mpColView(VclPtr<ScDataTableColView>::Create(this, mpDoc.get(), mpSelectionEngine.get())),
138 mpRowView(VclPtr<ScDataTableRowView>::Create(this, mpDoc.get(), mpSelectionEngine.get())),
139 mpVScroll(VclPtr<ScrollBar>::Create(this, WinBits(WB_VSCROLL | WB_DRAG))),
140 mpHScroll(VclPtr<ScrollBar>::Create(this, WinBits(WB_HSCROLL | WB_DRAG))),
141 mnFirstVisibleRow(0),
142 mnFirstVisibleCol(0)
143{
144 mpTopLeft->setPosSizePixel(0, 0, nRowHeaderWidth, nColHeaderHeight);
147
148 mpVScroll->SetRangeMin(0);
149 mpVScroll->SetRangeMax(100);
150 mpVScroll->SetEndScrollHdl(LINK(this, ScDataTableView, ScrollHdl));
151
152 mpHScroll->SetRangeMin(0);
153 mpHScroll->SetRangeMax(50);
154 mpHScroll->SetEndScrollHdl(LINK(this, ScDataTableView, ScrollHdl));
155
156 mpTopLeft->Show();
157 mpColView->Show();
158 mpRowView->Show();
159 mpVScroll->Show();
160 mpHScroll->Show();
161}
162
164{
165 disposeOnce();
166}
167
169{
176}
177
179{
180 if (!rMEvt.IsLeft())
181 return;
182
183 mpMouseEvent.reset(new MouseEvent(rMEvt));
184}
185
186namespace {
187
188SCCOL findColFromPos(sal_uInt16 nPixelPos, const ScDocument* pDoc, SCCOL nStartCol = 0)
189{
190 nPixelPos -= nRowHeaderWidth;
191 sal_uInt32 nPixelLength = 0;
192 for (SCCOL nCol : pDoc->GetColumnsRange(0, nStartCol, pDoc->MaxCol()))
193 {
194 sal_uInt16 nColWidth = pDoc->GetColWidth(nCol, 0, true);
195 sal_uInt32 nPixel = ScViewData::ToPixel(nColWidth, nPPTX);
196 nPixelLength += nPixel;
197
198 if (nPixelLength >= nPixelPos)
199 {
200 return nCol;
201 }
202 }
203
204 SAL_WARN("sc", "Could not find the corresponding column");
205 return pDoc->MaxCol();
206}
207
208SCROW findRowFromPos(sal_uInt16 nPixelPos, const ScDocument* pDoc, SCROW nStartRow = 0)
209{
210 nPixelPos -= nColHeaderHeight;
211 sal_uInt32 nPixelLength = 0;
212 for (SCROW nRow = nStartRow; nRow <= pDoc->MaxRow(); ++nRow)
213 {
214 sal_uInt16 nColWidth = pDoc->GetRowHeight(nRow, SCTAB(0), true);
215 sal_uInt32 nPixel = ScViewData::ToPixel(nColWidth, nPPTX);
216 nPixelLength += nPixel;
217
218 if (nPixelLength >= nPixelPos)
219 {
220 return nRow;
221 }
222 }
223
224 SAL_WARN("sc", "Could not find the corresponding row");
225 return pDoc->MaxRow();
226}
227
228}
229
231{
232 if (!rMEvt.IsLeft())
233 return;
234 if (!mpMouseEvent) // tdf#120528 The event originated in another window, like context menu
235 return;
236
237 SCCOL nStartCol = findColFromPos(mpMouseEvent->GetPosPixel().getX(), mpDoc.get());
238 SCCOL nEndCol = findColFromPos(rMEvt.GetPosPixel().getX(), mpDoc.get());
239 SCROW nStartRow = findRowFromPos(mpMouseEvent->GetPosPixel().getY(), mpDoc.get());
240 SCROW nEndRow = findRowFromPos(rMEvt.GetPosPixel().getY(), mpDoc.get());
241 PutInOrder(nStartCol, nEndCol);
242 PutInOrder(nStartRow, nEndRow);
243 mpColView->SetMark(true, nStartCol, nEndCol);
244 mpRowView->SetMark(true, nStartRow, nEndRow);
245
246 mpMouseEvent.reset();
247}
248
250{
251 Size aSize = GetSizePixel();
252 mpTopLeft->setPosSizePixel(0, 0, nRowHeaderWidth, nColHeaderHeight);
253 mpColView->setPosSizePixel(nRowHeaderWidth, 0, aSize.Width() - nScrollBarSize, nColHeaderHeight);
254 mpRowView->setPosSizePixel(0, nColHeaderHeight, nRowHeaderWidth, aSize.Height());
255
258}
259
260void ScDataTableView::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRectangle)
261{
262 Size aSize = GetSizePixel();
263 SCCOL nMaxVisibleCol = findColFromPos(aSize.Width() - nScrollBarSize, mpDoc.get(), mnFirstVisibleCol);
264 SCROW nMaxVisibleRow = findRowFromPos(aSize.Height(), mpDoc.get(), mnFirstVisibleRow);
265
266 ScTableInfo aTableInfo;
267 mpDoc->FillInfo(aTableInfo, mnFirstVisibleCol, mnFirstVisibleRow, nMaxVisibleCol, nMaxVisibleRow, 0, 0.06666, 0.06666, false, false);
268 ScOutputData aOutput(&rRenderContext, OUTTYPE_WINDOW, aTableInfo, mpDoc.get(), 0,
270
271 aOutput.SetGridColor(COL_BLACK);
272 aOutput.SetSolidBackground(true);
273 aOutput.DrawClear();
274 aOutput.DrawDocumentBackground();
275 aOutput.DrawGrid(rRenderContext, true, false);
276 aOutput.DrawStrings();
277 Control::Paint(rRenderContext, rRectangle);
278}
279
281{
282 return Size(450, 400);
283}
284
285void ScDataTableView::getColRange(SCCOL& rStartCol, SCCOL& rEndCol) const
286{
287 SCCOLROW aStart = 0;
288 SCCOLROW aEnd = 0;
289 mpColView->GetMarkRange(aStart, aEnd);
290 rStartCol = static_cast<SCCOL>(aStart);
291 rEndCol = static_cast<SCCOL>(aEnd);
292}
293
294void ScDataTableView::getRowRange(SCROW& rStartCol, SCROW& rEndCol) const
295{
296 SCCOLROW aStart = 0;
297 SCCOLROW aEnd = 0;
298 mpRowView->GetMarkRange(aStart, aEnd);
299 rStartCol = static_cast<SCROW>(aStart);
300 rEndCol = static_cast<SCROW>(aEnd);
301}
302
303IMPL_LINK(ScDataTableView, ScrollHdl, ScrollBar*, pScrollBar, void)
304{
305 if (pScrollBar == mpVScroll.get())
306 {
307 mnFirstVisibleRow = pScrollBar->GetThumbPos();
308 pScrollBar->SetRangeMax(std::min(mpDoc->MaxRow(), static_cast<SCROW>(mnFirstVisibleRow + 100)));
309 mpRowView->SetPos(mnFirstVisibleRow);
310 }
311 else
312 {
313 mnFirstVisibleCol = pScrollBar->GetThumbPos();
314 pScrollBar->SetRangeMax(std::min(mpDoc->MaxCol(), static_cast<SCCOL>(mnFirstVisibleCol + 50)));
315 mpColView->SetPos(mnFirstVisibleCol);
316 }
317 Invalidate();
318}
319
320/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SharedBitmapDescriptor mpTopLeft
void PutInOrder(T &nStart, T &nEnd)
Definition: address.hxx:150
virtual void dispose() override
const Point & GetPosPixel() const
bool IsLeft() const
void SetPos(SCCOLROW nRow)
virtual OUString GetEntryText(SCCOLROW nPos) const override
virtual SCCOLROW GetPos() const override
ScDocument * mpDoc
ScDataTableColView(vcl::Window *pParent, ScDocument *pDoc, SelectionEngine *pSelectionEngine)
virtual void HideEntries(SCCOLROW nPos, SCCOLROW nEndPos) override
virtual sal_uInt16 GetEntrySize(SCCOLROW nPos) const override
virtual bool IsLayoutRTL() const override
virtual void SetEntrySize(SCCOLROW nPos, sal_uInt16 nWidth) override
void SetPos(SCCOLROW nRow)
virtual void SetEntrySize(SCCOLROW nPos, sal_uInt16 nWidth) override
virtual SCCOLROW GetPos() const override
ScDocument * mpDoc
virtual sal_uInt16 GetEntrySize(SCCOLROW nPos) const override
virtual void HideEntries(SCCOLROW nPos, SCCOLROW nEndPos) override
ScDataTableRowView(vcl::Window *pParent, ScDocument *pDoc, SelectionEngine *pSelectionEngine)
virtual OUString GetEntryText(SCCOLROW nPos) const override
virtual bool IsLayoutRTL() const override
VclPtr< ScrollBar > mpVScroll
void getRowRange(SCROW &rStartRow, SCROW &rEndRow) const
virtual void dispose() override
VclPtr< ScDataTableRowView > mpRowView
~ScDataTableView() override
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
void getColRange(SCCOL &rStartCol, SCCOL &rEndCol) const
std::shared_ptr< ScDocument > mpDoc
virtual Size GetOptimalSize() const override
VclPtr< ScrollBarBox > mpTopLeft
virtual void MouseButtonUp(const MouseEvent &rMEvt) override
std::unique_ptr< MouseEvent > mpMouseEvent
VclPtr< ScDataTableColView > mpColView
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
ScDataTableView(const css::uno::Reference< css::awt::XWindow > &rParent, std::shared_ptr< ScDocument > pDoc)
VclPtr< ScrollBar > mpHScroll
virtual void Resize() override
SC_DLLPUBLIC sal_uInt16 GetRowHeight(SCROW nRow, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4218
SC_DLLPUBLIC sal_uInt16 GetColWidth(SCCOL nCol, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4177
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:891
SC_DLLPUBLIC SCROW MaxRow() const
Definition: document.hxx:892
SC_DLLPUBLIC void ShowRow(SCROW nRow, SCTAB nTab, bool bShow)
Definition: document.cxx:4361
SC_DLLPUBLIC void ShowCol(SCCOL nCol, SCTAB nTab, bool bShow)
Definition: document.cxx:4355
SC_DLLPUBLIC void SetColWidthOnly(SCCOL nCol, SCTAB nTab, sal_uInt16 nNewWidth)
Definition: document.cxx:4146
SC_DLLPUBLIC void SetRowHeight(SCROW nRow, SCTAB nTab, sal_uInt16 nNewHeight)
Definition: document.cxx:4152
SC_DLLPUBLIC ScColumnsRange GetColumnsRange(SCTAB nTab, SCCOL nColBegin, SCCOL nColEnd) const
Definition: document.cxx:2576
void DrawClear()
Definition: output.cxx:1332
void DrawStrings(bool bPixelToLogic=false)
Definition: output2.cxx:1469
void SetSolidBackground(bool bSet)
Definition: output.hxx:322
void SetGridColor(const Color &rColor)
Definition: output.cxx:242
void DrawGrid(vcl::RenderContext &rRenderContext, bool bGrid, bool bPage, bool bMergeCover=false)
Definition: output.cxx:296
void DrawDocumentBackground()
Definition: output.cxx:816
static tools::Long ToPixel(sal_uInt16 nTwips, double nFactor)
Definition: viewdata.hxx:700
constexpr tools::Long Height() const
constexpr tools::Long Width() const
void disposeAndClear()
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect)
virtual Size GetSizePixel() const
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
constexpr sal_uInt16 nRowHeaderWidth
constexpr double nPPTX
IMPL_LINK(ScDataTableView, ScrollHdl, ScrollBar *, pScrollBar, void)
constexpr double nPPTY
constexpr sal_uInt16 nColHeaderHeight
constexpr sal_uInt16 nScrollBarSize
sal_Int32 mnRow
sal_Int32 mnCol
sal_uInt16 nPos
#define SAL_WARN(area, stream)
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
@ OUTTYPE_WINDOW
Definition: output.hxx:57
#define nPixel
sal_Int32 SCCOLROW
a type capable of holding either SCCOL or SCROW
Definition: types.hxx:23
sal_Int16 SCTAB
Definition: types.hxx:22
sal_Int16 SCCOL
Definition: types.hxx:21
sal_Int32 SCROW
Definition: types.hxx:17
sal_Int64 WinBits
WinBits const WB_DRAG
WinBits const WB_SIZEABLE
WinBits const WB_VSCROLL
WinBits const WB_HSCROLL