LibreOffice Module sc (master) 1
csvtablebox.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 <csvtablebox.hxx>
21#include <vcl/settings.hxx>
22
24 : mxRuler(new ScCsvRuler(maData, this))
25 , mxGrid(new ScCsvGrid(maData, rBuilder.weld_menu("popup"), this))
26 , mxScroll(rBuilder.weld_scrolled_window("scrolledwindow", true))
27 , mxRulerWeld(new weld::CustomWeld(rBuilder, "csvruler", *mxRuler))
28 , mxGridWeld(new weld::CustomWeld(rBuilder, "csvgrid", *mxGrid))
29 , maEndScrollIdle("ScCsvTableBox maEndScrollIdle")
30{
31 Size aSize(mxScroll->get_approximate_digit_width() * 67,
32 mxScroll->get_text_height() * 10);
33 // this needs to be larger than the ScCsvGrid initial size to get it
34 // to stretch to fit, see ScCsvGrid::SetDrawingArea
35 mxScroll->set_size_request(aSize.Width(), aSize.Height());
36
37 mbFixedMode = false;
38 mnFixedWidth = 1;
39
40 Link<ScCsvControl&,void> aLink = LINK( this, ScCsvTableBox, CsvCmdHdl );
41 mxRuler->SetCmdHdl( aLink );
42 mxGrid->SetCmdHdl( aLink );
43
44 mxScroll->connect_hadjustment_changed(LINK(this, ScCsvTableBox, HScrollHdl));
45 mxScroll->connect_vadjustment_changed(LINK(this, ScCsvTableBox, VScrollHdl));
46
47 maEndScrollIdle.SetPriority(TaskPriority::LOWEST);
49
51}
52
54{
55}
56
57// common table box handling --------------------------------------------------
58
60{
61 if( !mbFixedMode )
62 return;
63
64 // rescue data for fixed width mode
65 mnFixedWidth = mxGrid->GetPosCount();
66 maFixColStates = mxGrid->GetColumnStates();
67 // switch to separators mode
68 mbFixedMode = false;
69 // reset and reinitialize controls
70 mxGrid->DisableRepaint();
71 mxGrid->Execute( CSVCMD_SETLINEOFFSET, 0 );
72 mxGrid->Execute( CSVCMD_SETPOSCOUNT, 1 );
73 mxGrid->Execute( CSVCMD_NEWCELLTEXTS );
74 mxGrid->SetColumnStates( std::vector(maSepColStates) );
76 mxGrid->EnableRepaint();
77}
78
80{
81 if( mbFixedMode )
82 return;
83
84 // rescue data for separators mode
85 maSepColStates = mxGrid->GetColumnStates();
86 // switch to fixed width mode
87 mbFixedMode = true;
88 // reset and reinitialize controls
89 mxGrid->DisableRepaint();
90 mxGrid->Execute( CSVCMD_SETLINEOFFSET, 0 );
92 mxGrid->SetSplits( mxRuler->GetSplits() );
93 mxGrid->SetColumnStates( std::vector(maFixColStates) );
95 mxGrid->EnableRepaint();
96}
97
99{
100 mxGrid->Init();
101}
102
104{
105 mxGrid->UpdateLayoutData();
106
107 mxGrid->Show();
108 if (mbFixedMode)
109 mxRuler->Show();
110 else
111 mxRuler->Hide();
112
113 Size aWinSize = mxGrid->GetOutputSizePixel();
114 maData.mnWinWidth = aWinSize.Width();
115 maData.mnWinHeight = aWinSize.Height();
116
117 // scrollbars always visible
119
120 // scrollbars always visible
122
123 // let the controls self-adjust to visible area
124 mxGrid->Execute( CSVCMD_SETPOSOFFSET, mxGrid->GetFirstVisPos() );
125 mxGrid->Execute( CSVCMD_SETLINEOFFSET, mxGrid->GetFirstVisLine() );
126}
127
129{
130 int nLower = 0;
131 int nValue = mxGrid->GetFirstVisPos();
132 int nUpper = mxGrid->GetPosCount() + 2;
133 int nPageSize = mxGrid->GetVisPosCount();
134
135 // Undo scrollbar RTL
137 nValue = nUpper - (nValue - nLower + nPageSize);
138
139 mxScroll->hadjustment_configure(nValue, nLower, nUpper,
140 1, mxGrid->GetVisPosCount() * 3 / 4,
141 nPageSize);
142}
143
145{
146 mxScroll->vadjustment_configure(mxGrid->GetFirstVisLine(), 0, mxGrid->GetLineCount() + 1,
147 1, mxGrid->GetVisLineCount() - 2,
148 mxGrid->GetVisLineCount());
149}
150
151void ScCsvTableBox::MakePosVisible( sal_Int32 nPos )
152{
153 if( (0 <= nPos) && (nPos < mxGrid->GetPosCount()) )
154 {
155 if( nPos - CSV_SCROLL_DIST + 1 <= mxGrid->GetFirstVisPos() )
157 else if( nPos + CSV_SCROLL_DIST >= mxGrid->GetLastVisPos() )
158 mxGrid->Execute( CSVCMD_SETPOSOFFSET, nPos - mxGrid->GetVisPosCount() + CSV_SCROLL_DIST );
159 }
160}
161
162// cell contents --------------------------------------------------------------
163
165 const OUString* pTextLines, const OUString& rSepChars,
166 sal_Unicode cTextSep, bool bMergeSep, bool bRemoveSpace )
167{
168 // assuming that pTextLines is a string array with size CSV_PREVIEW_LINES
169 // -> will be dynamic sometime
170 mxGrid->DisableRepaint();
171 sal_Int32 nEndLine = mxGrid->GetFirstVisLine() + CSV_PREVIEW_LINES;
172 const OUString* pString = pTextLines;
173 for( sal_Int32 nLine = mxGrid->GetFirstVisLine(); nLine < nEndLine; ++nLine, ++pString )
174 {
175 if( mbFixedMode )
176 mxGrid->ImplSetTextLineFix( nLine, *pString );
177 else
178 mxGrid->ImplSetTextLineSep( nLine, *pString, rSepChars, cTextSep, bMergeSep, bRemoveSpace );
179 }
180 mxGrid->EnableRepaint();
181}
182
183// column settings ------------------------------------------------------------
184
186{
187 const sal_Int32 nTypeCount = rListBox.get_count();
188 std::vector<OUString> aTypeNames( nTypeCount );
189 for( sal_Int32 nIndex = 0; nIndex < nTypeCount; ++nIndex )
190 aTypeNames[ nIndex ] = rListBox.get_text( nIndex );
191 mxGrid->SetTypeNames( std::move(aTypeNames) );
192}
193
195{
196 if( mbFixedMode )
197 mxGrid->FillColumnDataFix( rOptions );
198 else
199 mxGrid->FillColumnDataSep( rOptions );
200}
201
202// event handling -------------------------------------------------------------
203
204IMPL_LINK( ScCsvTableBox, CsvCmdHdl, ScCsvControl&, rCtrl, void )
205{
206 const ScCsvCmd& rCmd = rCtrl.GetCmd();
207 ScCsvCmdType eType = rCmd.GetType();
208 sal_Int32 nParam1 = rCmd.GetParam1();
209 sal_Int32 nParam2 = rCmd.GetParam2();
210
211 bool bFound = true;
212 switch( eType )
213 {
214 case CSVCMD_REPAINT:
215 if( !mxGrid->IsNoRepaint() )
216 {
217 mxGrid->Invalidate();
218 mxRuler->Invalidate();
219 InitHScrollBar();
220 InitVScrollBar();
221 }
222 break;
224 MakePosVisible( nParam1 );
225 break;
226
228 if( mbFixedMode )
229 mxGrid->Execute( CSVCMD_UPDATECELLTEXTS );
230 else
231 {
232 mxGrid->DisableRepaint();
233 ScCsvColStateVec aStates( mxGrid->GetColumnStates() );
234 sal_Int32 nPos = mxGrid->GetFirstVisPos();
235 mxGrid->Execute( CSVCMD_SETPOSCOUNT, 1 );
236 mxGrid->Execute( CSVCMD_UPDATECELLTEXTS );
237 mxGrid->Execute( CSVCMD_SETPOSOFFSET, nPos );
238 mxGrid->SetColumnStates( std::move(aStates) );
239 mxGrid->EnableRepaint();
240 }
241 break;
243 maUpdateTextHdl.Call( *this );
244 break;
246 mxGrid->SetSelColumnType( nParam1 );
247 break;
249 maColTypeHdl.Call( *this );
250 break;
252 mxGrid->SetFirstImportedLine( nParam1 );
253 break;
254
256 OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::InsertSplit - invalid call" );
257 if( mxRuler->GetSplitCount() + 1 < sal::static_int_cast<sal_uInt32>(CSV_MAXCOLCOUNT) )
258 {
259 mxRuler->InsertSplit( nParam1 );
260 mxGrid->InsertSplit( nParam1 );
261 }
262 break;
264 OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::RemoveSplit - invalid call" );
265 mxRuler->RemoveSplit( nParam1 );
266 mxGrid->RemoveSplit( nParam1 );
267 break;
269 mxGrid->Execute( mxRuler->HasSplit( nParam1 ) ? CSVCMD_REMOVESPLIT : CSVCMD_INSERTSPLIT, nParam1 );
270 break;
271 case CSVCMD_MOVESPLIT:
272 OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::MoveSplit - invalid call" );
273 mxRuler->MoveSplit( nParam1, nParam2 );
274 mxGrid->MoveSplit( nParam1, nParam2 );
275 break;
277 OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::RemoveAllSplits - invalid call" );
278 mxRuler->RemoveAllSplits();
279 mxGrid->RemoveAllSplits();
280 break;
281 default:
282 bFound = false;
283 }
284 if( bFound )
285 return;
286
287 const ScCsvLayoutData aOldData( maData );
288 switch( eType )
289 {
291 maData.mnPosCount = std::max( nParam1, sal_Int32( 1 ) );
292 ImplSetPosOffset( mxGrid->GetFirstVisPos() );
293 break;
295 ImplSetPosOffset( nParam1 );
296 break;
298 maData.mnHdrWidth = std::max( nParam1, sal_Int32( 0 ) );
299 ImplSetPosOffset( mxGrid->GetFirstVisPos() );
300 break;
302 maData.mnCharWidth = std::max( nParam1, sal_Int32( 1 ) );
303 ImplSetPosOffset( mxGrid->GetFirstVisPos() );
304 break;
306 maData.mnLineCount = std::max( nParam1, sal_Int32( 1 ) );
307 ImplSetLineOffset( mxGrid->GetFirstVisLine() );
308 break;
310 ImplSetLineOffset( nParam1 );
311 break;
313 maData.mnHdrHeight = std::max( nParam1, sal_Int32( 0 ) );
314 ImplSetLineOffset( mxGrid->GetFirstVisLine() );
315 break;
317 maData.mnLineHeight = std::max( nParam1, sal_Int32( 1 ) );
318 ImplSetLineOffset( mxGrid->GetFirstVisLine() );
319 break;
321 maData.mnPosCursor = mxGrid->IsVisibleSplitPos( nParam1 ) ? nParam1 : CSV_POS_INVALID;
322 break;
324 maData.mnColCursor = ((0 <= nParam1) && (nParam1 < mxGrid->GetPosCount())) ? nParam1 : CSV_POS_INVALID;
325 break;
326 default:
327 {
328 // added to avoid warnings
329 }
330 }
331
332 if( maData != aOldData )
333 {
334 mxGrid->DisableRepaint();
335 mxRuler->ApplyLayout( aOldData );
336 mxGrid->ApplyLayout( aOldData );
337 mxGrid->EnableRepaint();
338 }
339}
340
341IMPL_LINK(ScCsvTableBox, HScrollHdl, weld::ScrolledWindow&, rScroll, void)
342{
343 int nLower = 0;
344 int nValue = rScroll.hadjustment_get_value();
345 int nUpper = mxGrid->GetPosCount() + 2;
346 int nPageSize = mxGrid->GetVisPosCount();
347
348 // Undo scrollbar RTL
350 nValue = nUpper - (nValue - nLower + nPageSize);
351
352 mxGrid->Execute(CSVCMD_SETPOSOFFSET, nValue);
353 maEndScrollIdle.Start();
354}
355
356IMPL_LINK(ScCsvTableBox, VScrollHdl, weld::ScrolledWindow&, rScroll, void)
357{
358 mxGrid->Execute(CSVCMD_SETLINEOFFSET, rScroll.vadjustment_get_value());
359}
360
361IMPL_LINK_NOARG(ScCsvTableBox, ScrollEndHdl, Timer*, void)
362{
363 if( mxGrid->GetRulerCursorPos() != CSV_POS_INVALID )
364 mxGrid->Execute( CSVCMD_MOVERULERCURSOR, mxRuler->GetNoScrollPos( mxGrid->GetRulerCursorPos() ) );
365 if( mxGrid->GetGridCursorPos() != CSV_POS_INVALID )
366 mxGrid->Execute( CSVCMD_MOVEGRIDCURSOR, mxGrid->GetNoScrollCol( mxGrid->GetGridCursorPos() ) );
367}
368
369/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool GetLayoutRTL()
Data for a CSV control command.
Definition: csvcontrol.hxx:199
sal_Int32 GetParam1() const
Definition: csvcontrol.hxx:212
ScCsvCmdType GetType() const
Definition: csvcontrol.hxx:211
sal_Int32 GetParam2() const
Definition: csvcontrol.hxx:213
Base class for the CSV ruler and the data grid control.
Definition: csvcontrol.hxx:223
A data grid control for the CSV import dialog.
Definition: csvgrid.hxx:58
A ruler control for the CSV import dialog.
Definition: csvruler.hxx:33
The control in the CSV import dialog that contains a ruler and a data grid to visualize and modify th...
Definition: csvtablebox.hxx:44
void SetUniStrings(const OUString *pTextLines, const OUString &rSepChars, sal_Unicode cTextSep, bool bMergeSep, bool bRemoveSpace)
Fills all cells of all lines with the passed texts (Unicode strings).
Idle maEndScrollIdle
Handler for exporting the column type.
Definition: csvtablebox.hxx:57
SAL_DLLPRIVATE void InitHScrollBar()
Initializes size and position data of horizontal scrollbar.
void InitTypes(const weld::ComboBox &rListBox)
Reads UI strings for data types from the list box.
void FillColumnData(ScAsciiOptions &rOptions) const
Fills the options object with current column data.
ScCsvTableBox(weld::Builder &rBuilder)
false = Separators, true = Fixed width.
Definition: csvtablebox.cxx:23
void SetFixedWidthMode()
Sets the control to fixed width mode.
Definition: csvtablebox.cxx:79
bool mbFixedMode
Cached total width for fixed width mode.
Definition: csvtablebox.hxx:64
sal_Int32 mnFixedWidth
Column states in separators mode.
Definition: csvtablebox.hxx:62
SAL_DLLPRIVATE void InitVScrollBar()
Initializes size and position data of vertical scrollbar.
ScCsvColStateVec maFixColStates
Called when horizontal scrolling has ended.
Definition: csvtablebox.hxx:59
std::unique_ptr< ScCsvRuler > mxRuler
Current layout data of the controls.
Definition: csvtablebox.hxx:48
ScCsvColStateVec maSepColStates
Column states in fixed width mode.
Definition: csvtablebox.hxx:60
ScCsvLayoutData maData
Definition: csvtablebox.hxx:46
std::unique_ptr< ScCsvGrid > mxGrid
The ruler for fixed width mode.
Definition: csvtablebox.hxx:49
SAL_DLLPRIVATE void InitControls()
Initializes the children controls (pos/size, scroll bars, ...).
void Init()
Finishes initialization.
Definition: csvtablebox.cxx:98
SAL_DLLPRIVATE void MakePosVisible(sal_Int32 nPos)
Moves controls (not cursors!) so that nPos becomes visible.
std::unique_ptr< weld::ScrolledWindow > mxScroll
Calc-like data table for fixed width mode.
Definition: csvtablebox.hxx:50
void SetSeparatorsMode()
Sets the control to separators mode.
Definition: csvtablebox.cxx:59
constexpr tools::Long Height() const
constexpr tools::Long Width() const
void SetPriority(TaskPriority ePriority)
void SetInvokeHandler(const Link< Timer *, void > &rLink)
virtual OUString get_text(int pos) const=0
virtual int get_count() const=0
const sal_Int32 CSV_SCROLL_DIST
Minimum distance to border for auto scroll.
Definition: csvcontrol.hxx:39
const sal_Int32 CSV_PREVIEW_LINES
TODO make string array dynamic.
Definition: csvcontrol.hxx:42
const sal_Int32 CSV_MAXCOLCOUNT
Maximum count of columns.
Definition: csvcontrol.hxx:44
ScCsvCmdType
Enumeration of possible commands to change any settings of the CSV controls.
Definition: csvcontrol.hxx:159
@ CSVCMD_SETCOLUMNTYPE
Update cell texts with current split settings. [-].
Definition: csvcontrol.hxx:184
@ CSVCMD_SETLINECOUNT
Change character pixel width. [width in pixel].
Definition: csvcontrol.hxx:171
@ CSVCMD_SETFIRSTIMPORTLINE
Send selected column type to external controls. [-].
Definition: csvcontrol.hxx:186
@ CSVCMD_SETHDRWIDTH
Change position offset (scroll pos). [position].
Definition: csvcontrol.hxx:167
@ CSVCMD_SETLINEHEIGHT
Change height of top header line. [height in pixel].
Definition: csvcontrol.hxx:174
@ CSVCMD_MOVEGRIDCURSOR
Move ruler cursor to new position. [position].
Definition: csvcontrol.hxx:178
@ CSVCMD_SETPOSCOUNT
Repaint all controls. [-].
Definition: csvcontrol.hxx:165
@ CSVCMD_REPAINT
No command. [-].
Definition: csvcontrol.hxx:162
@ CSVCMD_EXPORTCOLUMNTYPE
Change data type of selected columns. [column type].
Definition: csvcontrol.hxx:185
@ CSVCMD_MOVESPLIT
Inserts or removes a split. [position].
Definition: csvcontrol.hxx:192
@ CSVCMD_UPDATECELLTEXTS
Recalculate splits and cell texts. [-].
Definition: csvcontrol.hxx:183
@ CSVCMD_NEWCELLTEXTS
Move to make passed position visible (for mouse tracking). [position].
Definition: csvcontrol.hxx:182
@ CSVCMD_SETPOSOFFSET
Change position/column count. [character count].
Definition: csvcontrol.hxx:166
@ CSVCMD_REMOVESPLIT
Insert a split. [position].
Definition: csvcontrol.hxx:190
@ CSVCMD_SETCHARWIDTH
Change width of the header column. [width in pixel].
Definition: csvcontrol.hxx:168
@ CSVCMD_MAKEPOSVISIBLE
Move data grid cursor to new column. [position].
Definition: csvcontrol.hxx:179
@ CSVCMD_INSERTSPLIT
Set number of first imported line. [line index].
Definition: csvcontrol.hxx:189
@ CSVCMD_MOVERULERCURSOR
Change data line pixel height. [height in pixel}.
Definition: csvcontrol.hxx:177
@ CSVCMD_SETHDRHEIGHT
Change first visible line. [line index].
Definition: csvcontrol.hxx:173
@ CSVCMD_SETLINEOFFSET
Change number of data lines. [line count].
Definition: csvcontrol.hxx:172
@ CSVCMD_TOGGLESPLIT
Remove a split. [position].
Definition: csvcontrol.hxx:191
@ CSVCMD_REMOVEALLSPLITS
Move a split. [old position, new position].
Definition: csvcontrol.hxx:193
::std::vector< ScCsvColState > ScCsvColStateVec
Definition: csvgrid.hxx:53
const sal_Int32 CSV_POS_INVALID
Constant for an invalid ruler position.
Definition: csvsplits.hxx:29
IMPL_LINK(ScCsvTableBox, CsvCmdHdl, ScCsvControl &, rCtrl, void)
IMPL_LINK_NOARG(ScCsvTableBox, ScrollEndHdl, Timer *, void)
DocumentType eType
sal_Int16 nValue
sal_Int32 nIndex
sal_uInt16 nPos
std::vector< sal_Int8, boost::noinit_adaptor< std::allocator< sal_Int8 > > > maData
A structure containing all layout data valid for both ruler and data grid (i.e.
Definition: csvcontrol.hxx:112
sal_Int32 mnWinHeight
Index of first visible line (0-based).
Definition: csvcontrol.hxx:125
sal_Int32 mnWinWidth
Horizontal scroll offset.
Definition: csvcontrol.hxx:117
sal_uInt16 sal_Unicode