bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / ui / dbgui / csvtablebox.cxx
blobd02a32032bbf1fa05517bfcf606c03ae8b1105b3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
24 // ause
25 #include "editutil.hxx"
27 // ============================================================================
29 ScCsvTableBox::ScCsvTableBox( Window* pParent, WinBits nBits ) :
30 ScCsvControl( pParent, maData, nBits ),
31 maRuler( *this ),
32 maGrid( *this ),
33 maHScroll( this, WB_HORZ | WB_DRAG ),
34 maVScroll( this, WB_VERT | WB_DRAG ),
35 maScrollBox( this )
37 mbFixedMode = false;
38 mnFixedWidth = 1;
40 maHScroll.EnableRTL( false ); // RTL
41 maHScroll.SetLineSize( 1 );
42 maVScroll.SetLineSize( 1 );
44 Link aLink = LINK( this, ScCsvTableBox, CsvCmdHdl );
45 SetCmdHdl( aLink );
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 );
57 InitControls();
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)));
68 return aDefault;
71 // common table box handling --------------------------------------------------
73 void ScCsvTableBox::SetSeparatorsMode()
75 if( mbFixedMode )
77 // rescue data for fixed width mode
78 mnFixedWidth = GetPosCount();
79 maFixColStates = maGrid.GetColumnStates();
80 // switch to separators mode
81 mbFixedMode = false;
82 // reset and reinitialize controls
83 DisableRepaint();
84 Execute( CSVCMD_SETLINEOFFSET, 0 );
85 Execute( CSVCMD_SETPOSCOUNT, 1 );
86 Execute( CSVCMD_NEWCELLTEXTS );
87 maGrid.SetColumnStates( maSepColStates );
88 InitControls();
89 EnableRepaint();
93 void ScCsvTableBox::SetFixedWidthMode()
95 if( !mbFixedMode )
97 // rescue data for separators mode
98 maSepColStates = maGrid.GetColumnStates();
99 // switch to fixed width mode
100 mbFixedMode = true;
101 // reset and reinitialize controls
102 DisableRepaint();
103 Execute( CSVCMD_SETLINEOFFSET, 0 );
104 Execute( CSVCMD_SETPOSCOUNT, mnFixedWidth );
105 maGrid.SetSplits( maRuler.GetSplits() );
106 maGrid.SetColumnStates( maFixColStates );
107 InitControls();
108 EnableRepaint();
112 void ScCsvTableBox::Init()
114 maGrid.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;
129 if( mbFixedMode )
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 );
137 else
138 maGrid.setPosSizePixel( 0, 0, nDataWidth, nDataHeight );
139 maGrid.Show();
140 maRuler.Show( mbFixedMode );
142 // scrollbars always visible
143 maHScroll.setPosSizePixel( 0, nDataHeight, nDataWidth, nScrollBarSize );
144 InitHScrollBar();
145 maHScroll.Show();
147 // scrollbars always visible
148 maVScroll.setPosSizePixel( nDataWidth, 0, nScrollBarSize, nDataHeight );
149 InitVScrollBar();
150 maVScroll.Show();
152 bool bScrBox = maHScroll.IsVisible() && maVScroll.IsVisible();
153 if( bScrBox )
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
198 DisableRepaint();
199 sal_Int32 nEndLine = GetFirstVisLine() + CSV_PREVIEW_LINES;
200 const OUString* pString = pTextLines;
201 for( sal_Int32 nLine = GetFirstVisLine(); nLine < nEndLine; ++nLine, ++pString )
203 if( mbFixedMode )
204 maGrid.ImplSetTextLineFix( nLine, *pString );
205 else
206 maGrid.ImplSetTextLineSep( nLine, *pString, rSepChars, cTextSep, bMergeSep );
208 EnableRepaint();
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
225 if( mbFixedMode )
226 maGrid.FillColumnDataFix( rOptions );
227 else
228 maGrid.FillColumnDataSep( rOptions );
232 // event handling -------------------------------------------------------------
234 void ScCsvTableBox::Resize()
236 ScCsvControl::Resize();
237 InitControls();
240 void ScCsvTableBox::DataChanged( const DataChangedEvent& rDCEvt )
242 if( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_STYLE) )
243 InitControls();
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();
256 bool bFound = true;
257 switch( eType )
259 case CSVCMD_REPAINT:
260 if( !IsNoRepaint() )
262 maGrid.ImplRedraw();
263 maRuler.ImplRedraw();
264 InitHScrollBar();
265 InitVScrollBar();
267 break;
268 case CSVCMD_MAKEPOSVISIBLE:
269 MakePosVisible( nParam1 );
270 break;
272 case CSVCMD_NEWCELLTEXTS:
273 if( mbFixedMode )
274 Execute( CSVCMD_UPDATECELLTEXTS );
275 else
277 DisableRepaint();
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 );
284 EnableRepaint();
286 break;
287 case CSVCMD_UPDATECELLTEXTS:
288 maUpdateTextHdl.Call( this );
289 break;
290 case CSVCMD_SETCOLUMNTYPE:
291 maGrid.SetSelColumnType( nParam1 );
292 break;
293 case CSVCMD_EXPORTCOLUMNTYPE:
294 maColTypeHdl.Call( this );
295 break;
296 case CSVCMD_SETFIRSTIMPORTLINE:
297 maGrid.SetFirstImportedLine( nParam1 );
298 break;
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 );
307 break;
308 case CSVCMD_REMOVESPLIT:
309 OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::RemoveSplit - invalid call" );
310 maRuler.RemoveSplit( nParam1 );
311 maGrid.RemoveSplit( nParam1 );
312 break;
313 case CSVCMD_TOGGLESPLIT:
314 Execute( maRuler.HasSplit( nParam1 ) ? CSVCMD_REMOVESPLIT : CSVCMD_INSERTSPLIT, nParam1 );
315 break;
316 case CSVCMD_MOVESPLIT:
317 OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::MoveSplit - invalid call" );
318 maRuler.MoveSplit( nParam1, nParam2 );
319 maGrid.MoveSplit( nParam1, nParam2 );
320 break;
321 case CSVCMD_REMOVEALLSPLITS:
322 OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::RemoveAllSplits - invalid call" );
323 maRuler.RemoveAllSplits();
324 maGrid.RemoveAllSplits();
325 break;
326 default:
327 bFound = false;
329 if( bFound )
330 return 0;
332 const ScCsvLayoutData aOldData( maData );
333 switch( eType )
335 case CSVCMD_SETPOSCOUNT:
336 maData.mnPosCount = std::max( nParam1, sal_Int32( 1 ) );
337 ImplSetPosOffset( GetFirstVisPos() );
338 break;
339 case CSVCMD_SETPOSOFFSET:
340 ImplSetPosOffset( nParam1 );
341 break;
342 case CSVCMD_SETHDRWIDTH:
343 maData.mnHdrWidth = std::max( nParam1, sal_Int32( 0 ) );
344 ImplSetPosOffset( GetFirstVisPos() );
345 break;
346 case CSVCMD_SETCHARWIDTH:
347 maData.mnCharWidth = std::max( nParam1, sal_Int32( 1 ) );
348 ImplSetPosOffset( GetFirstVisPos() );
349 break;
350 case CSVCMD_SETLINECOUNT:
351 maData.mnLineCount = std::max( nParam1, sal_Int32( 1 ) );
352 ImplSetLineOffset( GetFirstVisLine() );
353 break;
354 case CSVCMD_SETLINEOFFSET:
355 ImplSetLineOffset( nParam1 );
356 break;
357 case CSVCMD_SETHDRHEIGHT:
358 maData.mnHdrHeight = std::max( nParam1, sal_Int32( 0 ) );
359 ImplSetLineOffset( GetFirstVisLine() );
360 break;
361 case CSVCMD_SETLINEHEIGHT:
362 maData.mnLineHeight = std::max( nParam1, sal_Int32( 1 ) );
363 ImplSetLineOffset( GetFirstVisLine() );
364 break;
365 case CSVCMD_MOVERULERCURSOR:
366 maData.mnPosCursor = IsVisibleSplitPos( nParam1 ) ? nParam1 : CSV_POS_INVALID;
367 break;
368 case CSVCMD_MOVEGRIDCURSOR:
369 maData.mnColCursor = ((0 <= nParam1) && (nParam1 < GetPosCount())) ? nParam1 : CSV_POS_INVALID;
370 break;
371 default:
373 // added to avoid warnings
377 if( maData != aOldData )
379 DisableRepaint();
380 maRuler.ApplyLayout( aOldData );
381 maGrid.ApplyLayout( aOldData );
382 EnableRepaint();
385 return 0;
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() );
397 return 0;
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() ) );
412 return 0;
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: */