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/builder.hxx>
22 #include <vcl/lstbox.hxx>
25 #include "editutil.hxx"
27 // ============================================================================
29 ScCsvTableBox::ScCsvTableBox( Window
* pParent
, WinBits nBits
) :
30 ScCsvControl( pParent
, maData
, nBits
),
33 maHScroll( this, WB_HORZ
| WB_DRAG
),
34 maVScroll( this, WB_VERT
| WB_DRAG
),
40 maHScroll
.EnableRTL( false ); // RTL
41 maHScroll
.SetLineSize( 1 );
42 maVScroll
.SetLineSize( 1 );
44 Link aLink
= LINK( this, ScCsvTableBox
, CsvCmdHdl
);
46 maRuler
.SetCmdHdl( aLink
);
47 maGrid
.SetCmdHdl( aLink
);
49 aLink
= LINK( this, ScCsvTableBox
, ScrollHdl
);
50 maHScroll
.SetScrollHdl( aLink
);
51 maVScroll
.SetScrollHdl( aLink
);
53 aLink
= LINK( this, ScCsvTableBox
, ScrollEndHdl
);
54 maHScroll
.SetEndScrollHdl( aLink
);
55 maVScroll
.SetEndScrollHdl( aLink
);
60 extern "C" SAL_DLLPUBLIC_EXPORT Window
* SAL_CALL
makeScCsvTableBox(Window
*pParent
, VclBuilder::stringmap
&)
62 return new ScCsvTableBox(pParent
, WB_BORDER
);
65 Size
ScCsvTableBox::GetOptimalSize() const
67 Size
aDefault(LogicToPixel(Size(243, 82), MapMode(MAP_APPFONT
)));
71 // common table box handling --------------------------------------------------
73 void ScCsvTableBox::SetSeparatorsMode()
77 // rescue data for fixed width mode
78 mnFixedWidth
= GetPosCount();
79 maFixColStates
= maGrid
.GetColumnStates();
80 // switch to separators mode
82 // reset and reinitialize controls
84 Execute( CSVCMD_SETLINEOFFSET
, 0 );
85 Execute( CSVCMD_SETPOSCOUNT
, 1 );
86 Execute( CSVCMD_NEWCELLTEXTS
);
87 maGrid
.SetColumnStates( maSepColStates
);
93 void ScCsvTableBox::SetFixedWidthMode()
97 // rescue data for separators mode
98 maSepColStates
= maGrid
.GetColumnStates();
99 // switch to fixed width mode
101 // reset and reinitialize controls
103 Execute( CSVCMD_SETLINEOFFSET
, 0 );
104 Execute( CSVCMD_SETPOSCOUNT
, mnFixedWidth
);
105 maGrid
.SetSplits( maRuler
.GetSplits() );
106 maGrid
.SetColumnStates( maFixColStates
);
112 void ScCsvTableBox::Init()
117 void ScCsvTableBox::InitControls()
119 maGrid
.UpdateLayoutData();
121 long nScrollBarSize
= GetSettings().GetStyleSettings().GetScrollBarSize();
122 Size aWinSize
= CalcOutputSize( GetSizePixel() );
123 long nDataWidth
= aWinSize
.Width() - nScrollBarSize
;
124 long nDataHeight
= aWinSize
.Height() - nScrollBarSize
;
126 maData
.mnWinWidth
= nDataWidth
;
127 maData
.mnWinHeight
= nDataHeight
;
131 // ruler sets height internally
132 maRuler
.setPosSizePixel( 0, 0, nDataWidth
, 0 );
133 sal_Int32 nY
= maRuler
.GetSizePixel().Height();
134 maData
.mnWinHeight
-= nY
;
135 maGrid
.setPosSizePixel( 0, nY
, nDataWidth
, maData
.mnWinHeight
);
138 maGrid
.setPosSizePixel( 0, 0, nDataWidth
, nDataHeight
);
140 maRuler
.Show( mbFixedMode
);
142 // scrollbars always visible
143 maHScroll
.setPosSizePixel( 0, nDataHeight
, nDataWidth
, nScrollBarSize
);
147 // scrollbars always visible
148 maVScroll
.setPosSizePixel( nDataWidth
, 0, nScrollBarSize
, nDataHeight
);
152 bool bScrBox
= maHScroll
.IsVisible() && maVScroll
.IsVisible();
154 maScrollBox
.setPosSizePixel( nDataWidth
, nDataHeight
, nScrollBarSize
, nScrollBarSize
);
155 maScrollBox
.Show( bScrBox
);
157 // let the controls self-adjust to visible area
158 Execute( CSVCMD_SETPOSOFFSET
, GetFirstVisPos() );
159 Execute( CSVCMD_SETLINEOFFSET
, GetFirstVisLine() );
162 void ScCsvTableBox::InitHScrollBar()
164 maHScroll
.SetRange( Range( 0, GetPosCount() + 2 ) );
165 maHScroll
.SetVisibleSize( GetVisPosCount() );
166 maHScroll
.SetPageSize( GetVisPosCount() * 3 / 4 );
167 maHScroll
.SetThumbPos( GetFirstVisPos() );
170 void ScCsvTableBox::InitVScrollBar()
172 maVScroll
.SetRange( Range( 0, GetLineCount() + 1 ) );
173 maVScroll
.SetVisibleSize( GetVisLineCount() );
174 maVScroll
.SetPageSize( GetVisLineCount() - 2 );
175 maVScroll
.SetThumbPos( GetFirstVisLine() );
178 void ScCsvTableBox::MakePosVisible( sal_Int32 nPos
)
180 if( (0 <= nPos
) && (nPos
< GetPosCount()) )
182 if( nPos
- CSV_SCROLL_DIST
+ 1 <= GetFirstVisPos() )
183 Execute( CSVCMD_SETPOSOFFSET
, nPos
- CSV_SCROLL_DIST
);
184 else if( nPos
+ CSV_SCROLL_DIST
>= GetLastVisPos() )
185 Execute( CSVCMD_SETPOSOFFSET
, nPos
- GetVisPosCount() + CSV_SCROLL_DIST
);
190 // cell contents --------------------------------------------------------------
192 void ScCsvTableBox::SetUniStrings(
193 const OUString
* pTextLines
, const String
& rSepChars
,
194 sal_Unicode cTextSep
, bool bMergeSep
)
196 // assuming that pTextLines is a string array with size CSV_PREVIEW_LINES
197 // -> will be dynamic sometime
199 sal_Int32 nEndLine
= GetFirstVisLine() + CSV_PREVIEW_LINES
;
200 const OUString
* pString
= pTextLines
;
201 for( sal_Int32 nLine
= GetFirstVisLine(); nLine
< nEndLine
; ++nLine
, ++pString
)
204 maGrid
.ImplSetTextLineFix( nLine
, *pString
);
206 maGrid
.ImplSetTextLineSep( nLine
, *pString
, rSepChars
, cTextSep
, bMergeSep
);
212 // column settings ------------------------------------------------------------
214 void ScCsvTableBox::InitTypes( const ListBox
& rListBox
)
216 sal_uInt16 nTypeCount
= rListBox
.GetEntryCount();
217 StringVec
aTypeNames( nTypeCount
);
218 for( sal_uInt16 nIndex
= 0; nIndex
< nTypeCount
; ++nIndex
)
219 aTypeNames
[ nIndex
] = rListBox
.GetEntry( nIndex
);
220 maGrid
.SetTypeNames( aTypeNames
);
223 void ScCsvTableBox::FillColumnData( ScAsciiOptions
& rOptions
) const
226 maGrid
.FillColumnDataFix( rOptions
);
228 maGrid
.FillColumnDataSep( rOptions
);
232 // event handling -------------------------------------------------------------
234 void ScCsvTableBox::Resize()
236 ScCsvControl::Resize();
240 void ScCsvTableBox::DataChanged( const DataChangedEvent
& rDCEvt
)
242 if( (rDCEvt
.GetType() == DATACHANGED_SETTINGS
) && (rDCEvt
.GetFlags() & SETTINGS_STYLE
) )
244 ScCsvControl::DataChanged( rDCEvt
);
247 IMPL_LINK( ScCsvTableBox
, CsvCmdHdl
, ScCsvControl
*, pCtrl
)
249 OSL_ENSURE( pCtrl
, "ScCsvTableBox::CsvCmdHdl - missing sender" );
251 const ScCsvCmd
& rCmd
= pCtrl
->GetCmd();
252 ScCsvCmdType eType
= rCmd
.GetType();
253 sal_Int32 nParam1
= rCmd
.GetParam1();
254 sal_Int32 nParam2
= rCmd
.GetParam2();
263 maRuler
.ImplRedraw();
268 case CSVCMD_MAKEPOSVISIBLE
:
269 MakePosVisible( nParam1
);
272 case CSVCMD_NEWCELLTEXTS
:
274 Execute( CSVCMD_UPDATECELLTEXTS
);
278 ScCsvColStateVec
aStates( maGrid
.GetColumnStates() );
279 sal_Int32 nPos
= GetFirstVisPos();
280 Execute( CSVCMD_SETPOSCOUNT
, 1 );
281 Execute( CSVCMD_UPDATECELLTEXTS
);
282 Execute( CSVCMD_SETPOSOFFSET
, nPos
);
283 maGrid
.SetColumnStates( aStates
);
287 case CSVCMD_UPDATECELLTEXTS
:
288 maUpdateTextHdl
.Call( this );
290 case CSVCMD_SETCOLUMNTYPE
:
291 maGrid
.SetSelColumnType( nParam1
);
293 case CSVCMD_EXPORTCOLUMNTYPE
:
294 maColTypeHdl
.Call( this );
296 case CSVCMD_SETFIRSTIMPORTLINE
:
297 maGrid
.SetFirstImportedLine( nParam1
);
300 case CSVCMD_INSERTSPLIT
:
301 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::InsertSplit - invalid call" );
302 if( maRuler
.GetSplitCount() + 1 < sal::static_int_cast
<sal_uInt32
>(CSV_MAXCOLCOUNT
) )
304 maRuler
.InsertSplit( nParam1
);
305 maGrid
.InsertSplit( nParam1
);
308 case CSVCMD_REMOVESPLIT
:
309 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::RemoveSplit - invalid call" );
310 maRuler
.RemoveSplit( nParam1
);
311 maGrid
.RemoveSplit( nParam1
);
313 case CSVCMD_TOGGLESPLIT
:
314 Execute( maRuler
.HasSplit( nParam1
) ? CSVCMD_REMOVESPLIT
: CSVCMD_INSERTSPLIT
, nParam1
);
316 case CSVCMD_MOVESPLIT
:
317 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::MoveSplit - invalid call" );
318 maRuler
.MoveSplit( nParam1
, nParam2
);
319 maGrid
.MoveSplit( nParam1
, nParam2
);
321 case CSVCMD_REMOVEALLSPLITS
:
322 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::RemoveAllSplits - invalid call" );
323 maRuler
.RemoveAllSplits();
324 maGrid
.RemoveAllSplits();
332 const ScCsvLayoutData
aOldData( maData
);
335 case CSVCMD_SETPOSCOUNT
:
336 maData
.mnPosCount
= std::max( nParam1
, sal_Int32( 1 ) );
337 ImplSetPosOffset( GetFirstVisPos() );
339 case CSVCMD_SETPOSOFFSET
:
340 ImplSetPosOffset( nParam1
);
342 case CSVCMD_SETHDRWIDTH
:
343 maData
.mnHdrWidth
= std::max( nParam1
, sal_Int32( 0 ) );
344 ImplSetPosOffset( GetFirstVisPos() );
346 case CSVCMD_SETCHARWIDTH
:
347 maData
.mnCharWidth
= std::max( nParam1
, sal_Int32( 1 ) );
348 ImplSetPosOffset( GetFirstVisPos() );
350 case CSVCMD_SETLINECOUNT
:
351 maData
.mnLineCount
= std::max( nParam1
, sal_Int32( 1 ) );
352 ImplSetLineOffset( GetFirstVisLine() );
354 case CSVCMD_SETLINEOFFSET
:
355 ImplSetLineOffset( nParam1
);
357 case CSVCMD_SETHDRHEIGHT
:
358 maData
.mnHdrHeight
= std::max( nParam1
, sal_Int32( 0 ) );
359 ImplSetLineOffset( GetFirstVisLine() );
361 case CSVCMD_SETLINEHEIGHT
:
362 maData
.mnLineHeight
= std::max( nParam1
, sal_Int32( 1 ) );
363 ImplSetLineOffset( GetFirstVisLine() );
365 case CSVCMD_MOVERULERCURSOR
:
366 maData
.mnPosCursor
= IsVisibleSplitPos( nParam1
) ? nParam1
: CSV_POS_INVALID
;
368 case CSVCMD_MOVEGRIDCURSOR
:
369 maData
.mnColCursor
= ((0 <= nParam1
) && (nParam1
< GetPosCount())) ? nParam1
: CSV_POS_INVALID
;
373 // added to avoid warnings
377 if( maData
!= aOldData
)
380 maRuler
.ApplyLayout( aOldData
);
381 maGrid
.ApplyLayout( aOldData
);
388 IMPL_LINK( ScCsvTableBox
, ScrollHdl
, ScrollBar
*, pScrollBar
)
390 OSL_ENSURE( pScrollBar
, "ScCsvTableBox::ScrollHdl - missing sender" );
392 if( pScrollBar
== &maHScroll
)
393 Execute( CSVCMD_SETPOSOFFSET
, pScrollBar
->GetThumbPos() );
394 else if( pScrollBar
== &maVScroll
)
395 Execute( CSVCMD_SETLINEOFFSET
, pScrollBar
->GetThumbPos() );
400 IMPL_LINK( ScCsvTableBox
, ScrollEndHdl
, ScrollBar
*, pScrollBar
)
402 OSL_ENSURE( pScrollBar
, "ScCsvTableBox::ScrollEndHdl - missing sender" );
404 if( pScrollBar
== &maHScroll
)
406 if( GetRulerCursorPos() != CSV_POS_INVALID
)
407 Execute( CSVCMD_MOVERULERCURSOR
, maRuler
.GetNoScrollPos( GetRulerCursorPos() ) );
408 if( GetGridCursorPos() != CSV_POS_INVALID
)
409 Execute( CSVCMD_MOVEGRIDCURSOR
, maGrid
.GetNoScrollCol( GetGridCursorPos() ) );
416 // accessibility --------------------------------------------------------------
418 ScCsvTableBox::XAccessibleRef
ScCsvTableBox::CreateAccessible()
420 // do not use the ScCsvControl mechanism, return default accessible object
421 return Control::CreateAccessible();
424 ScAccessibleCsvControl
* ScCsvTableBox::ImplCreateAccessible()
426 return NULL
; // not used, see CreateAccessible()
430 // ============================================================================
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */