Line data Source code
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/builderfactory.hxx>
22 : #include <vcl/lstbox.hxx>
23 : #include <vcl/settings.hxx>
24 : #include "editutil.hxx"
25 :
26 0 : 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 0 : maScrollBox( VclPtr<ScrollBarBox>::Create(this) )
33 : {
34 0 : mbFixedMode = false;
35 0 : mnFixedWidth = 1;
36 :
37 0 : maHScroll->EnableRTL( false ); // RTL
38 0 : maHScroll->SetLineSize( 1 );
39 0 : maVScroll->SetLineSize( 1 );
40 :
41 0 : Link<> aLink = LINK( this, ScCsvTableBox, CsvCmdHdl );
42 0 : SetCmdHdl( aLink );
43 0 : maRuler->SetCmdHdl( aLink );
44 0 : maGrid->SetCmdHdl( aLink );
45 :
46 0 : aLink = LINK( this, ScCsvTableBox, ScrollHdl );
47 0 : maHScroll->SetScrollHdl( aLink );
48 0 : maVScroll->SetScrollHdl( aLink );
49 :
50 0 : aLink = LINK( this, ScCsvTableBox, ScrollEndHdl );
51 0 : maHScroll->SetEndScrollHdl( aLink );
52 0 : maVScroll->SetEndScrollHdl( aLink );
53 :
54 0 : InitControls();
55 0 : }
56 :
57 0 : ScCsvTableBox::~ScCsvTableBox()
58 : {
59 0 : disposeOnce();
60 0 : }
61 :
62 0 : void ScCsvTableBox::dispose()
63 : {
64 0 : maRuler.disposeAndClear();
65 0 : maGrid.disposeAndClear();
66 0 : maHScroll.disposeAndClear();
67 0 : maVScroll.disposeAndClear();
68 0 : maScrollBox.disposeAndClear();
69 0 : ScCsvControl::dispose();
70 0 : }
71 :
72 0 : VCL_BUILDER_FACTORY_ARGS(ScCsvTableBox, WB_BORDER)
73 :
74 0 : Size ScCsvTableBox::GetOptimalSize() const
75 : {
76 0 : Size aDefault(LogicToPixel(Size(243, 82), MapMode(MAP_APPFONT)));
77 0 : return aDefault;
78 : }
79 :
80 : // common table box handling --------------------------------------------------
81 :
82 0 : void ScCsvTableBox::SetSeparatorsMode()
83 : {
84 0 : if( mbFixedMode )
85 : {
86 : // rescue data for fixed width mode
87 0 : mnFixedWidth = GetPosCount();
88 0 : maFixColStates = maGrid->GetColumnStates();
89 : // switch to separators mode
90 0 : mbFixedMode = false;
91 : // reset and reinitialize controls
92 0 : DisableRepaint();
93 0 : Execute( CSVCMD_SETLINEOFFSET, 0 );
94 0 : Execute( CSVCMD_SETPOSCOUNT, 1 );
95 0 : Execute( CSVCMD_NEWCELLTEXTS );
96 0 : maGrid->SetColumnStates( maSepColStates );
97 0 : InitControls();
98 0 : EnableRepaint();
99 : }
100 0 : }
101 :
102 0 : void ScCsvTableBox::SetFixedWidthMode()
103 : {
104 0 : if( !mbFixedMode )
105 : {
106 : // rescue data for separators mode
107 0 : maSepColStates = maGrid->GetColumnStates();
108 : // switch to fixed width mode
109 0 : mbFixedMode = true;
110 : // reset and reinitialize controls
111 0 : DisableRepaint();
112 0 : Execute( CSVCMD_SETLINEOFFSET, 0 );
113 0 : Execute( CSVCMD_SETPOSCOUNT, mnFixedWidth );
114 0 : maGrid->SetSplits( maRuler->GetSplits() );
115 0 : maGrid->SetColumnStates( maFixColStates );
116 0 : InitControls();
117 0 : EnableRepaint();
118 : }
119 0 : }
120 :
121 0 : void ScCsvTableBox::Init()
122 : {
123 0 : maGrid->Init();
124 0 : }
125 :
126 0 : void ScCsvTableBox::InitControls()
127 : {
128 0 : maGrid->UpdateLayoutData();
129 :
130 0 : long nScrollBarSize = GetSettings().GetStyleSettings().GetScrollBarSize();
131 0 : Size aWinSize = CalcOutputSize( GetSizePixel() );
132 0 : long nDataWidth = aWinSize.Width() - nScrollBarSize;
133 0 : long nDataHeight = aWinSize.Height() - nScrollBarSize;
134 :
135 0 : maData.mnWinWidth = nDataWidth;
136 0 : maData.mnWinHeight = nDataHeight;
137 :
138 0 : if( mbFixedMode )
139 : {
140 : // ruler sets height internally
141 0 : maRuler->setPosSizePixel( 0, 0, nDataWidth, 0 );
142 0 : sal_Int32 nY = maRuler->GetSizePixel().Height();
143 0 : maData.mnWinHeight -= nY;
144 0 : maGrid->setPosSizePixel( 0, nY, nDataWidth, maData.mnWinHeight );
145 : }
146 : else
147 0 : maGrid->setPosSizePixel( 0, 0, nDataWidth, nDataHeight );
148 0 : maGrid->Show();
149 0 : maRuler->Show( mbFixedMode );
150 :
151 : // scrollbars always visible
152 0 : maHScroll->setPosSizePixel( 0, nDataHeight, nDataWidth, nScrollBarSize );
153 0 : InitHScrollBar();
154 0 : maHScroll->Show();
155 :
156 : // scrollbars always visible
157 0 : maVScroll->setPosSizePixel( nDataWidth, 0, nScrollBarSize, nDataHeight );
158 0 : InitVScrollBar();
159 0 : maVScroll->Show();
160 :
161 0 : bool bScrBox = maHScroll->IsVisible() && maVScroll->IsVisible();
162 0 : if( bScrBox )
163 0 : maScrollBox->setPosSizePixel( nDataWidth, nDataHeight, nScrollBarSize, nScrollBarSize );
164 0 : maScrollBox->Show( bScrBox );
165 :
166 : // let the controls self-adjust to visible area
167 0 : Execute( CSVCMD_SETPOSOFFSET, GetFirstVisPos() );
168 0 : Execute( CSVCMD_SETLINEOFFSET, GetFirstVisLine() );
169 0 : }
170 :
171 0 : void ScCsvTableBox::InitHScrollBar()
172 : {
173 0 : maHScroll->SetRange( Range( 0, GetPosCount() + 2 ) );
174 0 : maHScroll->SetVisibleSize( GetVisPosCount() );
175 0 : maHScroll->SetPageSize( GetVisPosCount() * 3 / 4 );
176 0 : maHScroll->SetThumbPos( GetFirstVisPos() );
177 0 : }
178 :
179 0 : void ScCsvTableBox::InitVScrollBar()
180 : {
181 0 : maVScroll->SetRange( Range( 0, GetLineCount() + 1 ) );
182 0 : maVScroll->SetVisibleSize( GetVisLineCount() );
183 0 : maVScroll->SetPageSize( GetVisLineCount() - 2 );
184 0 : maVScroll->SetThumbPos( GetFirstVisLine() );
185 0 : }
186 :
187 0 : void ScCsvTableBox::MakePosVisible( sal_Int32 nPos )
188 : {
189 0 : if( (0 <= nPos) && (nPos < GetPosCount()) )
190 : {
191 0 : if( nPos - CSV_SCROLL_DIST + 1 <= GetFirstVisPos() )
192 0 : Execute( CSVCMD_SETPOSOFFSET, nPos - CSV_SCROLL_DIST );
193 0 : else if( nPos + CSV_SCROLL_DIST >= GetLastVisPos() )
194 0 : Execute( CSVCMD_SETPOSOFFSET, nPos - GetVisPosCount() + CSV_SCROLL_DIST );
195 : }
196 0 : }
197 :
198 : // cell contents --------------------------------------------------------------
199 :
200 0 : void ScCsvTableBox::SetUniStrings(
201 : const OUString* pTextLines, const OUString& rSepChars,
202 : sal_Unicode cTextSep, bool bMergeSep )
203 : {
204 : // assuming that pTextLines is a string array with size CSV_PREVIEW_LINES
205 : // -> will be dynamic sometime
206 0 : DisableRepaint();
207 0 : sal_Int32 nEndLine = GetFirstVisLine() + CSV_PREVIEW_LINES;
208 0 : const OUString* pString = pTextLines;
209 0 : for( sal_Int32 nLine = GetFirstVisLine(); nLine < nEndLine; ++nLine, ++pString )
210 : {
211 0 : if( mbFixedMode )
212 0 : maGrid->ImplSetTextLineFix( nLine, *pString );
213 : else
214 0 : maGrid->ImplSetTextLineSep( nLine, *pString, rSepChars, cTextSep, bMergeSep );
215 : }
216 0 : EnableRepaint();
217 0 : }
218 :
219 : // column settings ------------------------------------------------------------
220 :
221 0 : void ScCsvTableBox::InitTypes( const ListBox& rListBox )
222 : {
223 0 : sal_uInt16 nTypeCount = rListBox.GetEntryCount();
224 0 : StringVec aTypeNames( nTypeCount );
225 0 : for( sal_uInt16 nIndex = 0; nIndex < nTypeCount; ++nIndex )
226 0 : aTypeNames[ nIndex ] = rListBox.GetEntry( nIndex );
227 0 : maGrid->SetTypeNames( aTypeNames );
228 0 : }
229 :
230 0 : void ScCsvTableBox::FillColumnData( ScAsciiOptions& rOptions ) const
231 : {
232 0 : if( mbFixedMode )
233 0 : maGrid->FillColumnDataFix( rOptions );
234 : else
235 0 : maGrid->FillColumnDataSep( rOptions );
236 0 : }
237 :
238 : // event handling -------------------------------------------------------------
239 :
240 0 : void ScCsvTableBox::Resize()
241 : {
242 0 : ScCsvControl::Resize();
243 0 : InitControls();
244 0 : }
245 :
246 0 : void ScCsvTableBox::DataChanged( const DataChangedEvent& rDCEvt )
247 : {
248 0 : if( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) && (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
249 0 : InitControls();
250 0 : ScCsvControl::DataChanged( rDCEvt );
251 0 : }
252 :
253 0 : IMPL_LINK( ScCsvTableBox, CsvCmdHdl, ScCsvControl*, pCtrl )
254 : {
255 : OSL_ENSURE( pCtrl, "ScCsvTableBox::CsvCmdHdl - missing sender" );
256 :
257 0 : const ScCsvCmd& rCmd = pCtrl->GetCmd();
258 0 : ScCsvCmdType eType = rCmd.GetType();
259 0 : sal_Int32 nParam1 = rCmd.GetParam1();
260 0 : sal_Int32 nParam2 = rCmd.GetParam2();
261 :
262 0 : bool bFound = true;
263 0 : switch( eType )
264 : {
265 : case CSVCMD_REPAINT:
266 0 : if( !IsNoRepaint() )
267 : {
268 0 : maGrid->ImplRedraw();
269 0 : maRuler->ImplRedraw();
270 0 : InitHScrollBar();
271 0 : InitVScrollBar();
272 : }
273 0 : break;
274 : case CSVCMD_MAKEPOSVISIBLE:
275 0 : MakePosVisible( nParam1 );
276 0 : break;
277 :
278 : case CSVCMD_NEWCELLTEXTS:
279 0 : if( mbFixedMode )
280 0 : Execute( CSVCMD_UPDATECELLTEXTS );
281 : else
282 : {
283 0 : DisableRepaint();
284 0 : ScCsvColStateVec aStates( maGrid->GetColumnStates() );
285 0 : sal_Int32 nPos = GetFirstVisPos();
286 0 : Execute( CSVCMD_SETPOSCOUNT, 1 );
287 0 : Execute( CSVCMD_UPDATECELLTEXTS );
288 0 : Execute( CSVCMD_SETPOSOFFSET, nPos );
289 0 : maGrid->SetColumnStates( aStates );
290 0 : EnableRepaint();
291 : }
292 0 : break;
293 : case CSVCMD_UPDATECELLTEXTS:
294 0 : maUpdateTextHdl.Call( this );
295 0 : break;
296 : case CSVCMD_SETCOLUMNTYPE:
297 0 : maGrid->SetSelColumnType( nParam1 );
298 0 : break;
299 : case CSVCMD_EXPORTCOLUMNTYPE:
300 0 : maColTypeHdl.Call( this );
301 0 : break;
302 : case CSVCMD_SETFIRSTIMPORTLINE:
303 0 : maGrid->SetFirstImportedLine( nParam1 );
304 0 : break;
305 :
306 : case CSVCMD_INSERTSPLIT:
307 : OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::InsertSplit - invalid call" );
308 0 : if( maRuler->GetSplitCount() + 1 < sal::static_int_cast<sal_uInt32>(CSV_MAXCOLCOUNT) )
309 : {
310 0 : maRuler->InsertSplit( nParam1 );
311 0 : maGrid->InsertSplit( nParam1 );
312 : }
313 0 : break;
314 : case CSVCMD_REMOVESPLIT:
315 : OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::RemoveSplit - invalid call" );
316 0 : maRuler->RemoveSplit( nParam1 );
317 0 : maGrid->RemoveSplit( nParam1 );
318 0 : break;
319 : case CSVCMD_TOGGLESPLIT:
320 0 : Execute( maRuler->HasSplit( nParam1 ) ? CSVCMD_REMOVESPLIT : CSVCMD_INSERTSPLIT, nParam1 );
321 0 : break;
322 : case CSVCMD_MOVESPLIT:
323 : OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::MoveSplit - invalid call" );
324 0 : maRuler->MoveSplit( nParam1, nParam2 );
325 0 : maGrid->MoveSplit( nParam1, nParam2 );
326 0 : break;
327 : case CSVCMD_REMOVEALLSPLITS:
328 : OSL_ENSURE( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::RemoveAllSplits - invalid call" );
329 0 : maRuler->RemoveAllSplits();
330 0 : maGrid->RemoveAllSplits();
331 0 : break;
332 : default:
333 0 : bFound = false;
334 : }
335 0 : if( bFound )
336 0 : return 0;
337 :
338 0 : const ScCsvLayoutData aOldData( maData );
339 0 : switch( eType )
340 : {
341 : case CSVCMD_SETPOSCOUNT:
342 0 : maData.mnPosCount = std::max( nParam1, sal_Int32( 1 ) );
343 0 : ImplSetPosOffset( GetFirstVisPos() );
344 0 : break;
345 : case CSVCMD_SETPOSOFFSET:
346 0 : ImplSetPosOffset( nParam1 );
347 0 : break;
348 : case CSVCMD_SETHDRWIDTH:
349 0 : maData.mnHdrWidth = std::max( nParam1, sal_Int32( 0 ) );
350 0 : ImplSetPosOffset( GetFirstVisPos() );
351 0 : break;
352 : case CSVCMD_SETCHARWIDTH:
353 0 : maData.mnCharWidth = std::max( nParam1, sal_Int32( 1 ) );
354 0 : ImplSetPosOffset( GetFirstVisPos() );
355 0 : break;
356 : case CSVCMD_SETLINECOUNT:
357 0 : maData.mnLineCount = std::max( nParam1, sal_Int32( 1 ) );
358 0 : ImplSetLineOffset( GetFirstVisLine() );
359 0 : break;
360 : case CSVCMD_SETLINEOFFSET:
361 0 : ImplSetLineOffset( nParam1 );
362 0 : break;
363 : case CSVCMD_SETHDRHEIGHT:
364 0 : maData.mnHdrHeight = std::max( nParam1, sal_Int32( 0 ) );
365 0 : ImplSetLineOffset( GetFirstVisLine() );
366 0 : break;
367 : case CSVCMD_SETLINEHEIGHT:
368 0 : maData.mnLineHeight = std::max( nParam1, sal_Int32( 1 ) );
369 0 : ImplSetLineOffset( GetFirstVisLine() );
370 0 : break;
371 : case CSVCMD_MOVERULERCURSOR:
372 0 : maData.mnPosCursor = IsVisibleSplitPos( nParam1 ) ? nParam1 : CSV_POS_INVALID;
373 0 : break;
374 : case CSVCMD_MOVEGRIDCURSOR:
375 0 : maData.mnColCursor = ((0 <= nParam1) && (nParam1 < GetPosCount())) ? nParam1 : CSV_POS_INVALID;
376 0 : break;
377 : default:
378 : {
379 : // added to avoid warnings
380 : }
381 : }
382 :
383 0 : if( maData != aOldData )
384 : {
385 0 : DisableRepaint();
386 0 : maRuler->ApplyLayout( aOldData );
387 0 : maGrid->ApplyLayout( aOldData );
388 0 : EnableRepaint();
389 : }
390 :
391 0 : return 0;
392 : }
393 :
394 0 : IMPL_LINK( ScCsvTableBox, ScrollHdl, ScrollBar*, pScrollBar )
395 : {
396 : OSL_ENSURE( pScrollBar, "ScCsvTableBox::ScrollHdl - missing sender" );
397 :
398 0 : if( pScrollBar == maHScroll.get() )
399 0 : Execute( CSVCMD_SETPOSOFFSET, pScrollBar->GetThumbPos() );
400 0 : else if( pScrollBar == maVScroll.get() )
401 0 : Execute( CSVCMD_SETLINEOFFSET, pScrollBar->GetThumbPos() );
402 :
403 0 : return 0;
404 : }
405 :
406 0 : IMPL_LINK( ScCsvTableBox, ScrollEndHdl, ScrollBar*, pScrollBar )
407 : {
408 : OSL_ENSURE( pScrollBar, "ScCsvTableBox::ScrollEndHdl - missing sender" );
409 :
410 0 : if( pScrollBar == maHScroll.get() )
411 : {
412 0 : if( GetRulerCursorPos() != CSV_POS_INVALID )
413 0 : Execute( CSVCMD_MOVERULERCURSOR, maRuler->GetNoScrollPos( GetRulerCursorPos() ) );
414 0 : if( GetGridCursorPos() != CSV_POS_INVALID )
415 0 : Execute( CSVCMD_MOVEGRIDCURSOR, maGrid->GetNoScrollCol( GetGridCursorPos() ) );
416 : }
417 :
418 0 : return 0;
419 : }
420 :
421 : // accessibility --------------------------------------------------------------
422 :
423 0 : ScCsvTableBox::XAccessibleRef ScCsvTableBox::CreateAccessible()
424 : {
425 : // do not use the ScCsvControl mechanism, return default accessible object
426 0 : return Control::CreateAccessible();
427 : }
428 :
429 0 : ScAccessibleCsvControl* ScCsvTableBox::ImplCreateAccessible()
430 : {
431 0 : return NULL; // not used, see CreateAccessible()
432 156 : }
433 :
434 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|