1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "csvtablebox.hxx"
21 #include <vcl/builderfactory.hxx>
22 #include <vcl/lstbox.hxx>
23 #include <vcl/settings.hxx>
24 #include "editutil.hxx"
25 #include "AccessibleCsvControl.hxx"
27 ScCsvTableBox::ScCsvTableBox( vcl::Window
* pParent
, WinBits nBits
) :
28 ScCsvControl( pParent
, maData
, nBits
),
29 maRuler( VclPtr
<ScCsvRuler
>::Create(*this) ),
30 maGrid( VclPtr
<ScCsvGrid
>::Create(*this) ),
31 maHScroll( VclPtr
<ScrollBar
>::Create( this, WB_HORZ
| WB_DRAG
) ),
32 maVScroll( VclPtr
<ScrollBar
>::Create( this, WB_VERT
| WB_DRAG
) ),
33 maScrollBox( VclPtr
<ScrollBarBox
>::Create(this) )
38 maHScroll
->EnableRTL( false ); // RTL
39 maHScroll
->SetLineSize( 1 );
40 maVScroll
->SetLineSize( 1 );
42 Link
<ScCsvControl
&,void> aLink
= LINK( this, ScCsvTableBox
, CsvCmdHdl
);
44 maRuler
->SetCmdHdl( aLink
);
45 maGrid
->SetCmdHdl( aLink
);
47 Link
<ScrollBar
*,void> aLink2
= LINK( this, ScCsvTableBox
, ScrollHdl
);
48 maHScroll
->SetScrollHdl( aLink2
);
49 maVScroll
->SetScrollHdl( aLink2
);
51 aLink2
= LINK( this, ScCsvTableBox
, ScrollEndHdl
);
52 maHScroll
->SetEndScrollHdl( aLink2
);
53 maVScroll
->SetEndScrollHdl( aLink2
);
58 ScCsvTableBox::~ScCsvTableBox()
63 void ScCsvTableBox::dispose()
65 maRuler
.disposeAndClear();
66 maGrid
.disposeAndClear();
67 maHScroll
.disposeAndClear();
68 maVScroll
.disposeAndClear();
69 maScrollBox
.disposeAndClear();
70 ScCsvControl::dispose();
73 VCL_BUILDER_FACTORY_ARGS(ScCsvTableBox
, WB_BORDER
)
75 Size
ScCsvTableBox::GetOptimalSize() const
77 Size
aDefault(LogicToPixel(Size(243, 82), MapMode(MAP_APPFONT
)));
81 // common table box handling --------------------------------------------------
83 void ScCsvTableBox::SetSeparatorsMode()
87 // rescue data for fixed width mode
88 mnFixedWidth
= GetPosCount();
89 maFixColStates
= maGrid
->GetColumnStates();
90 // switch to separators mode
92 // reset and reinitialize controls
94 Execute( CSVCMD_SETLINEOFFSET
, 0 );
95 Execute( CSVCMD_SETPOSCOUNT
, 1 );
96 Execute( CSVCMD_NEWCELLTEXTS
);
97 maGrid
->SetColumnStates( maSepColStates
);
103 void ScCsvTableBox::SetFixedWidthMode()
107 // rescue data for separators mode
108 maSepColStates
= maGrid
->GetColumnStates();
109 // switch to fixed width mode
111 // reset and reinitialize controls
113 Execute( CSVCMD_SETLINEOFFSET
, 0 );
114 Execute( CSVCMD_SETPOSCOUNT
, mnFixedWidth
);
115 maGrid
->SetSplits( maRuler
->GetSplits() );
116 maGrid
->SetColumnStates( maFixColStates
);
122 void ScCsvTableBox::Init()
127 void ScCsvTableBox::InitControls()
129 maGrid
->UpdateLayoutData();
131 long nScrollBarSize
= GetSettings().GetStyleSettings().GetScrollBarSize();
132 Size aWinSize
= CalcOutputSize( GetSizePixel() );
133 long nDataWidth
= aWinSize
.Width() - nScrollBarSize
;
134 long nDataHeight
= aWinSize
.Height() - nScrollBarSize
;
136 maData
.mnWinWidth
= nDataWidth
;
137 maData
.mnWinHeight
= nDataHeight
;
141 // ruler sets height internally
142 maRuler
->setPosSizePixel( 0, 0, nDataWidth
, 0 );
143 sal_Int32 nY
= maRuler
->GetSizePixel().Height();
144 maData
.mnWinHeight
-= nY
;
145 maGrid
->setPosSizePixel( 0, nY
, nDataWidth
, maData
.mnWinHeight
);
148 maGrid
->setPosSizePixel( 0, 0, nDataWidth
, nDataHeight
);
150 maRuler
->Show( mbFixedMode
);
152 // scrollbars always visible
153 maHScroll
->setPosSizePixel( 0, nDataHeight
, nDataWidth
, nScrollBarSize
);
157 // scrollbars always visible
158 maVScroll
->setPosSizePixel( nDataWidth
, 0, nScrollBarSize
, nDataHeight
);
162 bool bScrBox
= maHScroll
->IsVisible() && maVScroll
->IsVisible();
164 maScrollBox
->setPosSizePixel( nDataWidth
, nDataHeight
, nScrollBarSize
, nScrollBarSize
);
165 maScrollBox
->Show( bScrBox
);
167 // let the controls self-adjust to visible area
168 Execute( CSVCMD_SETPOSOFFSET
, GetFirstVisPos() );
169 Execute( CSVCMD_SETLINEOFFSET
, GetFirstVisLine() );
172 void ScCsvTableBox::InitHScrollBar()
174 maHScroll
->SetRange( Range( 0, GetPosCount() + 2 ) );
175 maHScroll
->SetVisibleSize( GetVisPosCount() );
176 maHScroll
->SetPageSize( GetVisPosCount() * 3 / 4 );
177 maHScroll
->SetThumbPos( GetFirstVisPos() );
180 void ScCsvTableBox::InitVScrollBar()
182 maVScroll
->SetRange( Range( 0, GetLineCount() + 1 ) );
183 maVScroll
->SetVisibleSize( GetVisLineCount() );
184 maVScroll
->SetPageSize( GetVisLineCount() - 2 );
185 maVScroll
->SetThumbPos( GetFirstVisLine() );
188 void ScCsvTableBox::MakePosVisible( sal_Int32 nPos
)
190 if( (0 <= nPos
) && (nPos
< GetPosCount()) )
192 if( nPos
- CSV_SCROLL_DIST
+ 1 <= GetFirstVisPos() )
193 Execute( CSVCMD_SETPOSOFFSET
, nPos
- CSV_SCROLL_DIST
);
194 else if( nPos
+ CSV_SCROLL_DIST
>= GetLastVisPos() )
195 Execute( CSVCMD_SETPOSOFFSET
, nPos
- GetVisPosCount() + CSV_SCROLL_DIST
);
199 // cell contents --------------------------------------------------------------
201 void ScCsvTableBox::SetUniStrings(
202 const OUString
* pTextLines
, const OUString
& rSepChars
,
203 sal_Unicode cTextSep
, bool bMergeSep
)
205 // assuming that pTextLines is a string array with size CSV_PREVIEW_LINES
206 // -> will be dynamic sometime
208 sal_Int32 nEndLine
= GetFirstVisLine() + CSV_PREVIEW_LINES
;
209 const OUString
* pString
= pTextLines
;
210 for( sal_Int32 nLine
= GetFirstVisLine(); nLine
< nEndLine
; ++nLine
, ++pString
)
213 maGrid
->ImplSetTextLineFix( nLine
, *pString
);
215 maGrid
->ImplSetTextLineSep( nLine
, *pString
, rSepChars
, cTextSep
, bMergeSep
);
220 // column settings ------------------------------------------------------------
222 void ScCsvTableBox::InitTypes( const ListBox
& rListBox
)
224 const sal_Int32 nTypeCount
= rListBox
.GetEntryCount();
225 std::vector
<OUString
> aTypeNames( nTypeCount
);
226 for( sal_Int32 nIndex
= 0; nIndex
< nTypeCount
; ++nIndex
)
227 aTypeNames
[ nIndex
] = rListBox
.GetEntry( nIndex
);
228 maGrid
->SetTypeNames( aTypeNames
);
231 void ScCsvTableBox::FillColumnData( ScAsciiOptions
& rOptions
) const
234 maGrid
->FillColumnDataFix( rOptions
);
236 maGrid
->FillColumnDataSep( rOptions
);
239 // event handling -------------------------------------------------------------
241 void ScCsvTableBox::Resize()
243 ScCsvControl::Resize();
247 void ScCsvTableBox::DataChanged( const DataChangedEvent
& rDCEvt
)
249 if( (rDCEvt
.GetType() == DataChangedEventType::SETTINGS
) && (rDCEvt
.GetFlags() & AllSettingsFlags::STYLE
) )
251 ScCsvControl::DataChanged( rDCEvt
);
254 IMPL_LINK_TYPED( ScCsvTableBox
, CsvCmdHdl
, ScCsvControl
&, rCtrl
, void )
256 const ScCsvCmd
& rCmd
= rCtrl
.GetCmd();
257 ScCsvCmdType eType
= rCmd
.GetType();
258 sal_Int32 nParam1
= rCmd
.GetParam1();
259 sal_Int32 nParam2
= rCmd
.GetParam2();
267 maGrid
->ImplRedraw();
268 maRuler
->ImplRedraw();
273 case CSVCMD_MAKEPOSVISIBLE
:
274 MakePosVisible( nParam1
);
277 case CSVCMD_NEWCELLTEXTS
:
279 Execute( CSVCMD_UPDATECELLTEXTS
);
283 ScCsvColStateVec
aStates( maGrid
->GetColumnStates() );
284 sal_Int32 nPos
= GetFirstVisPos();
285 Execute( CSVCMD_SETPOSCOUNT
, 1 );
286 Execute( CSVCMD_UPDATECELLTEXTS
);
287 Execute( CSVCMD_SETPOSOFFSET
, nPos
);
288 maGrid
->SetColumnStates( aStates
);
292 case CSVCMD_UPDATECELLTEXTS
:
293 maUpdateTextHdl
.Call( *this );
295 case CSVCMD_SETCOLUMNTYPE
:
296 maGrid
->SetSelColumnType( nParam1
);
298 case CSVCMD_EXPORTCOLUMNTYPE
:
299 maColTypeHdl
.Call( *this );
301 case CSVCMD_SETFIRSTIMPORTLINE
:
302 maGrid
->SetFirstImportedLine( nParam1
);
305 case CSVCMD_INSERTSPLIT
:
306 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::InsertSplit - invalid call" );
307 if( maRuler
->GetSplitCount() + 1 < sal::static_int_cast
<sal_uInt32
>(CSV_MAXCOLCOUNT
) )
309 maRuler
->InsertSplit( nParam1
);
310 maGrid
->InsertSplit( nParam1
);
313 case CSVCMD_REMOVESPLIT
:
314 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::RemoveSplit - invalid call" );
315 maRuler
->RemoveSplit( nParam1
);
316 maGrid
->RemoveSplit( nParam1
);
318 case CSVCMD_TOGGLESPLIT
:
319 Execute( maRuler
->HasSplit( nParam1
) ? CSVCMD_REMOVESPLIT
: CSVCMD_INSERTSPLIT
, nParam1
);
321 case CSVCMD_MOVESPLIT
:
322 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::MoveSplit - invalid call" );
323 maRuler
->MoveSplit( nParam1
, nParam2
);
324 maGrid
->MoveSplit( nParam1
, nParam2
);
326 case CSVCMD_REMOVEALLSPLITS
:
327 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::RemoveAllSplits - invalid call" );
328 maRuler
->RemoveAllSplits();
329 maGrid
->RemoveAllSplits();
337 const ScCsvLayoutData
aOldData( maData
);
340 case CSVCMD_SETPOSCOUNT
:
341 maData
.mnPosCount
= std::max( nParam1
, sal_Int32( 1 ) );
342 ImplSetPosOffset( GetFirstVisPos() );
344 case CSVCMD_SETPOSOFFSET
:
345 ImplSetPosOffset( nParam1
);
347 case CSVCMD_SETHDRWIDTH
:
348 maData
.mnHdrWidth
= std::max( nParam1
, sal_Int32( 0 ) );
349 ImplSetPosOffset( GetFirstVisPos() );
351 case CSVCMD_SETCHARWIDTH
:
352 maData
.mnCharWidth
= std::max( nParam1
, sal_Int32( 1 ) );
353 ImplSetPosOffset( GetFirstVisPos() );
355 case CSVCMD_SETLINECOUNT
:
356 maData
.mnLineCount
= std::max( nParam1
, sal_Int32( 1 ) );
357 ImplSetLineOffset( GetFirstVisLine() );
359 case CSVCMD_SETLINEOFFSET
:
360 ImplSetLineOffset( nParam1
);
362 case CSVCMD_SETHDRHEIGHT
:
363 maData
.mnHdrHeight
= std::max( nParam1
, sal_Int32( 0 ) );
364 ImplSetLineOffset( GetFirstVisLine() );
366 case CSVCMD_SETLINEHEIGHT
:
367 maData
.mnLineHeight
= std::max( nParam1
, sal_Int32( 1 ) );
368 ImplSetLineOffset( GetFirstVisLine() );
370 case CSVCMD_MOVERULERCURSOR
:
371 maData
.mnPosCursor
= IsVisibleSplitPos( nParam1
) ? nParam1
: CSV_POS_INVALID
;
373 case CSVCMD_MOVEGRIDCURSOR
:
374 maData
.mnColCursor
= ((0 <= nParam1
) && (nParam1
< GetPosCount())) ? nParam1
: CSV_POS_INVALID
;
378 // added to avoid warnings
382 if( maData
!= aOldData
)
385 maRuler
->ApplyLayout( aOldData
);
386 maGrid
->ApplyLayout( aOldData
);
391 IMPL_LINK_TYPED( ScCsvTableBox
, ScrollHdl
, ScrollBar
*, pScrollBar
, void )
393 OSL_ENSURE( pScrollBar
, "ScCsvTableBox::ScrollHdl - missing sender" );
395 if( pScrollBar
== maHScroll
.get() )
396 Execute( CSVCMD_SETPOSOFFSET
, pScrollBar
->GetThumbPos() );
397 else if( pScrollBar
== maVScroll
.get() )
398 Execute( CSVCMD_SETLINEOFFSET
, pScrollBar
->GetThumbPos() );
401 IMPL_LINK_TYPED( ScCsvTableBox
, ScrollEndHdl
, ScrollBar
*, pScrollBar
, void )
403 OSL_ENSURE( pScrollBar
, "ScCsvTableBox::ScrollEndHdl - missing sender" );
405 if( pScrollBar
== maHScroll
.get() )
407 if( GetRulerCursorPos() != CSV_POS_INVALID
)
408 Execute( CSVCMD_MOVERULERCURSOR
, maRuler
->GetNoScrollPos( GetRulerCursorPos() ) );
409 if( GetGridCursorPos() != CSV_POS_INVALID
)
410 Execute( CSVCMD_MOVEGRIDCURSOR
, maGrid
->GetNoScrollCol( GetGridCursorPos() ) );
414 // accessibility --------------------------------------------------------------
416 css::uno::Reference
< css::accessibility::XAccessible
> ScCsvTableBox::CreateAccessible()
418 // do not use the ScCsvControl mechanism, return default accessible object
419 return Control::CreateAccessible();
422 rtl::Reference
<ScAccessibleCsvControl
> ScCsvTableBox::ImplCreateAccessible()
424 return rtl::Reference
<ScAccessibleCsvControl
>(); // not used, see CreateAccessible()
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */