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"
26 ScCsvTableBox::ScCsvTableBox( vcl::Window
* pParent
, WinBits nBits
) :
27 ScCsvControl( pParent
, maData
, nBits
),
28 maRuler( VclPtr
<ScCsvRuler
>::Create(*this) ),
29 maGrid( VclPtr
<ScCsvGrid
>::Create(*this) ),
30 maHScroll( VclPtr
<ScrollBar
>::Create( this, WB_HORZ
| WB_DRAG
) ),
31 maVScroll( VclPtr
<ScrollBar
>::Create( this, WB_VERT
| WB_DRAG
) ),
32 maScrollBox( VclPtr
<ScrollBarBox
>::Create(this) )
37 maHScroll
->EnableRTL( false ); // RTL
38 maHScroll
->SetLineSize( 1 );
39 maVScroll
->SetLineSize( 1 );
41 Link
<> aLink
= LINK( this, ScCsvTableBox
, CsvCmdHdl
);
43 maRuler
->SetCmdHdl( aLink
);
44 maGrid
->SetCmdHdl( aLink
);
46 aLink
= LINK( this, ScCsvTableBox
, ScrollHdl
);
47 maHScroll
->SetScrollHdl( aLink
);
48 maVScroll
->SetScrollHdl( aLink
);
50 aLink
= LINK( this, ScCsvTableBox
, ScrollEndHdl
);
51 maHScroll
->SetEndScrollHdl( aLink
);
52 maVScroll
->SetEndScrollHdl( aLink
);
57 ScCsvTableBox::~ScCsvTableBox()
62 void ScCsvTableBox::dispose()
64 maRuler
.disposeAndClear();
65 maGrid
.disposeAndClear();
66 maHScroll
.disposeAndClear();
67 maVScroll
.disposeAndClear();
68 maScrollBox
.disposeAndClear();
69 ScCsvControl::dispose();
72 VCL_BUILDER_FACTORY_ARGS(ScCsvTableBox
, WB_BORDER
)
74 Size
ScCsvTableBox::GetOptimalSize() const
76 Size
aDefault(LogicToPixel(Size(243, 82), MapMode(MAP_APPFONT
)));
80 // common table box handling --------------------------------------------------
82 void ScCsvTableBox::SetSeparatorsMode()
86 // rescue data for fixed width mode
87 mnFixedWidth
= GetPosCount();
88 maFixColStates
= maGrid
->GetColumnStates();
89 // switch to separators mode
91 // reset and reinitialize controls
93 Execute( CSVCMD_SETLINEOFFSET
, 0 );
94 Execute( CSVCMD_SETPOSCOUNT
, 1 );
95 Execute( CSVCMD_NEWCELLTEXTS
);
96 maGrid
->SetColumnStates( maSepColStates
);
102 void ScCsvTableBox::SetFixedWidthMode()
106 // rescue data for separators mode
107 maSepColStates
= maGrid
->GetColumnStates();
108 // switch to fixed width mode
110 // reset and reinitialize controls
112 Execute( CSVCMD_SETLINEOFFSET
, 0 );
113 Execute( CSVCMD_SETPOSCOUNT
, mnFixedWidth
);
114 maGrid
->SetSplits( maRuler
->GetSplits() );
115 maGrid
->SetColumnStates( maFixColStates
);
121 void ScCsvTableBox::Init()
126 void ScCsvTableBox::InitControls()
128 maGrid
->UpdateLayoutData();
130 long nScrollBarSize
= GetSettings().GetStyleSettings().GetScrollBarSize();
131 Size aWinSize
= CalcOutputSize( GetSizePixel() );
132 long nDataWidth
= aWinSize
.Width() - nScrollBarSize
;
133 long nDataHeight
= aWinSize
.Height() - nScrollBarSize
;
135 maData
.mnWinWidth
= nDataWidth
;
136 maData
.mnWinHeight
= nDataHeight
;
140 // ruler sets height internally
141 maRuler
->setPosSizePixel( 0, 0, nDataWidth
, 0 );
142 sal_Int32 nY
= maRuler
->GetSizePixel().Height();
143 maData
.mnWinHeight
-= nY
;
144 maGrid
->setPosSizePixel( 0, nY
, nDataWidth
, maData
.mnWinHeight
);
147 maGrid
->setPosSizePixel( 0, 0, nDataWidth
, nDataHeight
);
149 maRuler
->Show( mbFixedMode
);
151 // scrollbars always visible
152 maHScroll
->setPosSizePixel( 0, nDataHeight
, nDataWidth
, nScrollBarSize
);
156 // scrollbars always visible
157 maVScroll
->setPosSizePixel( nDataWidth
, 0, nScrollBarSize
, nDataHeight
);
161 bool bScrBox
= maHScroll
->IsVisible() && maVScroll
->IsVisible();
163 maScrollBox
->setPosSizePixel( nDataWidth
, nDataHeight
, nScrollBarSize
, nScrollBarSize
);
164 maScrollBox
->Show( bScrBox
);
166 // let the controls self-adjust to visible area
167 Execute( CSVCMD_SETPOSOFFSET
, GetFirstVisPos() );
168 Execute( CSVCMD_SETLINEOFFSET
, GetFirstVisLine() );
171 void ScCsvTableBox::InitHScrollBar()
173 maHScroll
->SetRange( Range( 0, GetPosCount() + 2 ) );
174 maHScroll
->SetVisibleSize( GetVisPosCount() );
175 maHScroll
->SetPageSize( GetVisPosCount() * 3 / 4 );
176 maHScroll
->SetThumbPos( GetFirstVisPos() );
179 void ScCsvTableBox::InitVScrollBar()
181 maVScroll
->SetRange( Range( 0, GetLineCount() + 1 ) );
182 maVScroll
->SetVisibleSize( GetVisLineCount() );
183 maVScroll
->SetPageSize( GetVisLineCount() - 2 );
184 maVScroll
->SetThumbPos( GetFirstVisLine() );
187 void ScCsvTableBox::MakePosVisible( sal_Int32 nPos
)
189 if( (0 <= nPos
) && (nPos
< GetPosCount()) )
191 if( nPos
- CSV_SCROLL_DIST
+ 1 <= GetFirstVisPos() )
192 Execute( CSVCMD_SETPOSOFFSET
, nPos
- CSV_SCROLL_DIST
);
193 else if( nPos
+ CSV_SCROLL_DIST
>= GetLastVisPos() )
194 Execute( CSVCMD_SETPOSOFFSET
, nPos
- GetVisPosCount() + CSV_SCROLL_DIST
);
198 // cell contents --------------------------------------------------------------
200 void ScCsvTableBox::SetUniStrings(
201 const OUString
* pTextLines
, const OUString
& rSepChars
,
202 sal_Unicode cTextSep
, bool bMergeSep
)
204 // assuming that pTextLines is a string array with size CSV_PREVIEW_LINES
205 // -> will be dynamic sometime
207 sal_Int32 nEndLine
= GetFirstVisLine() + CSV_PREVIEW_LINES
;
208 const OUString
* pString
= pTextLines
;
209 for( sal_Int32 nLine
= GetFirstVisLine(); nLine
< nEndLine
; ++nLine
, ++pString
)
212 maGrid
->ImplSetTextLineFix( nLine
, *pString
);
214 maGrid
->ImplSetTextLineSep( nLine
, *pString
, rSepChars
, cTextSep
, bMergeSep
);
219 // column settings ------------------------------------------------------------
221 void ScCsvTableBox::InitTypes( const ListBox
& rListBox
)
223 sal_uInt16 nTypeCount
= rListBox
.GetEntryCount();
224 StringVec
aTypeNames( nTypeCount
);
225 for( sal_uInt16 nIndex
= 0; nIndex
< nTypeCount
; ++nIndex
)
226 aTypeNames
[ nIndex
] = rListBox
.GetEntry( nIndex
);
227 maGrid
->SetTypeNames( aTypeNames
);
230 void ScCsvTableBox::FillColumnData( ScAsciiOptions
& rOptions
) const
233 maGrid
->FillColumnDataFix( rOptions
);
235 maGrid
->FillColumnDataSep( rOptions
);
238 // event handling -------------------------------------------------------------
240 void ScCsvTableBox::Resize()
242 ScCsvControl::Resize();
246 void ScCsvTableBox::DataChanged( const DataChangedEvent
& rDCEvt
)
248 if( (rDCEvt
.GetType() == DataChangedEventType::SETTINGS
) && (rDCEvt
.GetFlags() & AllSettingsFlags::STYLE
) )
250 ScCsvControl::DataChanged( rDCEvt
);
253 IMPL_LINK( ScCsvTableBox
, CsvCmdHdl
, ScCsvControl
*, pCtrl
)
255 OSL_ENSURE( pCtrl
, "ScCsvTableBox::CsvCmdHdl - missing sender" );
257 const ScCsvCmd
& rCmd
= pCtrl
->GetCmd();
258 ScCsvCmdType eType
= rCmd
.GetType();
259 sal_Int32 nParam1
= rCmd
.GetParam1();
260 sal_Int32 nParam2
= rCmd
.GetParam2();
268 maGrid
->ImplRedraw();
269 maRuler
->ImplRedraw();
274 case CSVCMD_MAKEPOSVISIBLE
:
275 MakePosVisible( nParam1
);
278 case CSVCMD_NEWCELLTEXTS
:
280 Execute( CSVCMD_UPDATECELLTEXTS
);
284 ScCsvColStateVec
aStates( maGrid
->GetColumnStates() );
285 sal_Int32 nPos
= GetFirstVisPos();
286 Execute( CSVCMD_SETPOSCOUNT
, 1 );
287 Execute( CSVCMD_UPDATECELLTEXTS
);
288 Execute( CSVCMD_SETPOSOFFSET
, nPos
);
289 maGrid
->SetColumnStates( aStates
);
293 case CSVCMD_UPDATECELLTEXTS
:
294 maUpdateTextHdl
.Call( this );
296 case CSVCMD_SETCOLUMNTYPE
:
297 maGrid
->SetSelColumnType( nParam1
);
299 case CSVCMD_EXPORTCOLUMNTYPE
:
300 maColTypeHdl
.Call( this );
302 case CSVCMD_SETFIRSTIMPORTLINE
:
303 maGrid
->SetFirstImportedLine( nParam1
);
306 case CSVCMD_INSERTSPLIT
:
307 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::InsertSplit - invalid call" );
308 if( maRuler
->GetSplitCount() + 1 < sal::static_int_cast
<sal_uInt32
>(CSV_MAXCOLCOUNT
) )
310 maRuler
->InsertSplit( nParam1
);
311 maGrid
->InsertSplit( nParam1
);
314 case CSVCMD_REMOVESPLIT
:
315 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::RemoveSplit - invalid call" );
316 maRuler
->RemoveSplit( nParam1
);
317 maGrid
->RemoveSplit( nParam1
);
319 case CSVCMD_TOGGLESPLIT
:
320 Execute( maRuler
->HasSplit( nParam1
) ? CSVCMD_REMOVESPLIT
: CSVCMD_INSERTSPLIT
, nParam1
);
322 case CSVCMD_MOVESPLIT
:
323 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::MoveSplit - invalid call" );
324 maRuler
->MoveSplit( nParam1
, nParam2
);
325 maGrid
->MoveSplit( nParam1
, nParam2
);
327 case CSVCMD_REMOVEALLSPLITS
:
328 OSL_ENSURE( mbFixedMode
, "ScCsvTableBox::CsvCmdHdl::RemoveAllSplits - invalid call" );
329 maRuler
->RemoveAllSplits();
330 maGrid
->RemoveAllSplits();
338 const ScCsvLayoutData
aOldData( maData
);
341 case CSVCMD_SETPOSCOUNT
:
342 maData
.mnPosCount
= std::max( nParam1
, sal_Int32( 1 ) );
343 ImplSetPosOffset( GetFirstVisPos() );
345 case CSVCMD_SETPOSOFFSET
:
346 ImplSetPosOffset( nParam1
);
348 case CSVCMD_SETHDRWIDTH
:
349 maData
.mnHdrWidth
= std::max( nParam1
, sal_Int32( 0 ) );
350 ImplSetPosOffset( GetFirstVisPos() );
352 case CSVCMD_SETCHARWIDTH
:
353 maData
.mnCharWidth
= std::max( nParam1
, sal_Int32( 1 ) );
354 ImplSetPosOffset( GetFirstVisPos() );
356 case CSVCMD_SETLINECOUNT
:
357 maData
.mnLineCount
= std::max( nParam1
, sal_Int32( 1 ) );
358 ImplSetLineOffset( GetFirstVisLine() );
360 case CSVCMD_SETLINEOFFSET
:
361 ImplSetLineOffset( nParam1
);
363 case CSVCMD_SETHDRHEIGHT
:
364 maData
.mnHdrHeight
= std::max( nParam1
, sal_Int32( 0 ) );
365 ImplSetLineOffset( GetFirstVisLine() );
367 case CSVCMD_SETLINEHEIGHT
:
368 maData
.mnLineHeight
= std::max( nParam1
, sal_Int32( 1 ) );
369 ImplSetLineOffset( GetFirstVisLine() );
371 case CSVCMD_MOVERULERCURSOR
:
372 maData
.mnPosCursor
= IsVisibleSplitPos( nParam1
) ? nParam1
: CSV_POS_INVALID
;
374 case CSVCMD_MOVEGRIDCURSOR
:
375 maData
.mnColCursor
= ((0 <= nParam1
) && (nParam1
< GetPosCount())) ? nParam1
: CSV_POS_INVALID
;
379 // added to avoid warnings
383 if( maData
!= aOldData
)
386 maRuler
->ApplyLayout( aOldData
);
387 maGrid
->ApplyLayout( aOldData
);
394 IMPL_LINK( ScCsvTableBox
, ScrollHdl
, ScrollBar
*, pScrollBar
)
396 OSL_ENSURE( pScrollBar
, "ScCsvTableBox::ScrollHdl - missing sender" );
398 if( pScrollBar
== maHScroll
.get() )
399 Execute( CSVCMD_SETPOSOFFSET
, pScrollBar
->GetThumbPos() );
400 else if( pScrollBar
== maVScroll
.get() )
401 Execute( CSVCMD_SETLINEOFFSET
, pScrollBar
->GetThumbPos() );
406 IMPL_LINK( ScCsvTableBox
, ScrollEndHdl
, ScrollBar
*, pScrollBar
)
408 OSL_ENSURE( pScrollBar
, "ScCsvTableBox::ScrollEndHdl - missing sender" );
410 if( pScrollBar
== maHScroll
.get() )
412 if( GetRulerCursorPos() != CSV_POS_INVALID
)
413 Execute( CSVCMD_MOVERULERCURSOR
, maRuler
->GetNoScrollPos( GetRulerCursorPos() ) );
414 if( GetGridCursorPos() != CSV_POS_INVALID
)
415 Execute( CSVCMD_MOVEGRIDCURSOR
, maGrid
->GetNoScrollCol( GetGridCursorPos() ) );
421 // accessibility --------------------------------------------------------------
423 ScCsvTableBox::XAccessibleRef
ScCsvTableBox::CreateAccessible()
425 // do not use the ScCsvControl mechanism, return default accessible object
426 return Control::CreateAccessible();
429 ScAccessibleCsvControl
* ScCsvTableBox::ImplCreateAccessible()
431 return NULL
; // not used, see CreateAccessible()
434 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */