1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_sc.hxx" 30 31 32 // ============================================================================ 33 #include "csvtablebox.hxx" 34 #include <tools/debug.hxx> 35 #include <vcl/lstbox.hxx> 36 37 // ause 38 #include "editutil.hxx" 39 40 // ============================================================================ 41 42 //UNUSED2009-05 ScCsvTableBox::ScCsvTableBox( Window* pParent ) : 43 //UNUSED2009-05 ScCsvControl( pParent, maData, WB_BORDER | WB_TABSTOP | WB_DIALOGCONTROL ), 44 //UNUSED2009-05 maRuler( *this ), 45 //UNUSED2009-05 maGrid( *this ), 46 //UNUSED2009-05 maHScroll( this, WB_HORZ | WB_DRAG ), 47 //UNUSED2009-05 maVScroll( this, WB_VERT | WB_DRAG ), 48 //UNUSED2009-05 maScrollBox( this ) 49 //UNUSED2009-05 { 50 //UNUSED2009-05 Init(); 51 //UNUSED2009-05 } 52 53 ScCsvTableBox::ScCsvTableBox( Window* pParent, const ResId& rResId ) : 54 ScCsvControl( pParent, maData, rResId ), 55 maRuler( *this ), 56 maGrid( *this ), 57 maHScroll( this, WB_HORZ | WB_DRAG ), 58 maVScroll( this, WB_VERT | WB_DRAG ), 59 maScrollBox( this ) 60 { 61 Init(); 62 } 63 64 65 // common table box handling -------------------------------------------------- 66 67 void ScCsvTableBox::SetSeparatorsMode() 68 { 69 if( mbFixedMode ) 70 { 71 // rescue data for fixed width mode 72 mnFixedWidth = GetPosCount(); 73 maFixColStates = maGrid.GetColumnStates(); 74 // switch to separators mode 75 mbFixedMode = false; 76 // reset and reinitialize controls 77 DisableRepaint(); 78 Execute( CSVCMD_SETLINEOFFSET, 0 ); 79 Execute( CSVCMD_SETPOSCOUNT, 1 ); 80 Execute( CSVCMD_NEWCELLTEXTS ); 81 maGrid.SetColumnStates( maSepColStates ); 82 InitControls(); 83 EnableRepaint(); 84 } 85 } 86 87 void ScCsvTableBox::SetFixedWidthMode() 88 { 89 if( !mbFixedMode ) 90 { 91 // rescue data for separators mode 92 maSepColStates = maGrid.GetColumnStates(); 93 // switch to fixed width mode 94 mbFixedMode = true; 95 // reset and reinitialize controls 96 DisableRepaint(); 97 Execute( CSVCMD_SETLINEOFFSET, 0 ); 98 Execute( CSVCMD_SETPOSCOUNT, mnFixedWidth ); 99 maGrid.SetSplits( maRuler.GetSplits() ); 100 maGrid.SetColumnStates( maFixColStates ); 101 InitControls(); 102 EnableRepaint(); 103 } 104 } 105 106 void ScCsvTableBox::Init() 107 { 108 mbFixedMode = false; 109 mnFixedWidth = 1; 110 111 maHScroll.EnableRTL( false ); // #107812# RTL 112 maHScroll.SetLineSize( 1 ); 113 maVScroll.SetLineSize( 1 ); 114 115 Link aLink = LINK( this, ScCsvTableBox, CsvCmdHdl ); 116 SetCmdHdl( aLink ); 117 maRuler.SetCmdHdl( aLink ); 118 maGrid.SetCmdHdl( aLink ); 119 120 aLink = LINK( this, ScCsvTableBox, ScrollHdl ); 121 maHScroll.SetScrollHdl( aLink ); 122 maVScroll.SetScrollHdl( aLink ); 123 124 aLink = LINK( this, ScCsvTableBox, ScrollEndHdl ); 125 maHScroll.SetEndScrollHdl( aLink ); 126 maVScroll.SetEndScrollHdl( aLink ); 127 128 InitControls(); 129 } 130 131 void ScCsvTableBox::InitControls() 132 { 133 maGrid.UpdateLayoutData(); 134 135 long nScrollBarSize = GetSettings().GetStyleSettings().GetScrollBarSize(); 136 Size aWinSize = CalcOutputSize( GetSizePixel() ); 137 long nDataWidth = aWinSize.Width() - nScrollBarSize; 138 long nDataHeight = aWinSize.Height() - nScrollBarSize; 139 140 maData.mnWinWidth = nDataWidth; 141 maData.mnWinHeight = nDataHeight; 142 143 if( mbFixedMode ) 144 { 145 // ruler sets height internally 146 maRuler.SetPosSizePixel( 0, 0, nDataWidth, 0 ); 147 sal_Int32 nY = maRuler.GetSizePixel().Height(); 148 maData.mnWinHeight -= nY; 149 maGrid.SetPosSizePixel( 0, nY, nDataWidth, maData.mnWinHeight ); 150 } 151 else 152 maGrid.SetPosSizePixel( 0, 0, nDataWidth, nDataHeight ); 153 maGrid.Show(); 154 maRuler.Show( mbFixedMode ); 155 156 // scrollbars always visible 157 maHScroll.SetPosSizePixel( 0, nDataHeight, nDataWidth, nScrollBarSize ); 158 InitHScrollBar(); 159 maHScroll.Show(); 160 161 // scrollbars always visible 162 maVScroll.SetPosSizePixel( nDataWidth, 0, nScrollBarSize, nDataHeight ); 163 InitVScrollBar(); 164 maVScroll.Show(); 165 166 bool bScrBox = maHScroll.IsVisible() && maVScroll.IsVisible(); 167 if( bScrBox ) 168 maScrollBox.SetPosSizePixel( nDataWidth, nDataHeight, nScrollBarSize, nScrollBarSize ); 169 maScrollBox.Show( bScrBox ); 170 171 // let the controls self-adjust to visible area 172 Execute( CSVCMD_SETPOSOFFSET, GetFirstVisPos() ); 173 Execute( CSVCMD_SETLINEOFFSET, GetFirstVisLine() ); 174 } 175 176 void ScCsvTableBox::InitHScrollBar() 177 { 178 maHScroll.SetRange( Range( 0, GetPosCount() + 2 ) ); 179 maHScroll.SetVisibleSize( GetVisPosCount() ); 180 maHScroll.SetPageSize( GetVisPosCount() * 3 / 4 ); 181 maHScroll.SetThumbPos( GetFirstVisPos() ); 182 } 183 184 void ScCsvTableBox::InitVScrollBar() 185 { 186 maVScroll.SetRange( Range( 0, GetLineCount() + 1 ) ); 187 maVScroll.SetVisibleSize( GetVisLineCount() ); 188 maVScroll.SetPageSize( GetVisLineCount() - 2 ); 189 maVScroll.SetThumbPos( GetFirstVisLine() ); 190 } 191 192 void ScCsvTableBox::MakePosVisible( sal_Int32 nPos ) 193 { 194 if( (0 <= nPos) && (nPos < GetPosCount()) ) 195 { 196 if( nPos - CSV_SCROLL_DIST + 1 <= GetFirstVisPos() ) 197 Execute( CSVCMD_SETPOSOFFSET, nPos - CSV_SCROLL_DIST ); 198 else if( nPos + CSV_SCROLL_DIST >= GetLastVisPos() ) 199 Execute( CSVCMD_SETPOSOFFSET, nPos - GetVisPosCount() + CSV_SCROLL_DIST ); 200 } 201 } 202 203 204 // cell contents -------------------------------------------------------------- 205 206 void ScCsvTableBox::SetUniStrings( 207 const String* pTextLines, const String& rSepChars, 208 sal_Unicode cTextSep, bool bMergeSep ) 209 { 210 // assuming that pTextLines is a string array with size CSV_PREVIEW_LINES 211 // -> will be dynamic sometime 212 DisableRepaint(); 213 sal_Int32 nEndLine = GetFirstVisLine() + CSV_PREVIEW_LINES; 214 const String* pString = pTextLines; 215 for( sal_Int32 nLine = GetFirstVisLine(); nLine < nEndLine; ++nLine, ++pString ) 216 { 217 if( mbFixedMode ) 218 maGrid.ImplSetTextLineFix( nLine, *pString ); 219 else 220 maGrid.ImplSetTextLineSep( nLine, *pString, rSepChars, cTextSep, bMergeSep ); 221 } 222 EnableRepaint(); 223 } 224 225 //UNUSED2009-05 void ScCsvTableBox::SetByteStrings( 226 //UNUSED2009-05 const ByteString* pTextLines, CharSet eCharSet, 227 //UNUSED2009-05 const String& rSepChars, sal_Unicode cTextSep, bool bMergeSep ) 228 //UNUSED2009-05 { 229 //UNUSED2009-05 // assuming that pTextLines is a string array with size CSV_PREVIEW_LINES 230 //UNUSED2009-05 // -> will be dynamic sometime 231 //UNUSED2009-05 DisableRepaint(); 232 //UNUSED2009-05 sal_Int32 nEndLine = GetFirstVisLine() + CSV_PREVIEW_LINES; 233 //UNUSED2009-05 const ByteString* pString = pTextLines; 234 //UNUSED2009-05 for( sal_Int32 nLine = GetFirstVisLine(); nLine < nEndLine; ++nLine, ++pString ) 235 //UNUSED2009-05 { 236 //UNUSED2009-05 if( mbFixedMode ) 237 //UNUSED2009-05 maGrid.ImplSetTextLineFix( nLine, String( *pString, eCharSet ) ); 238 //UNUSED2009-05 else 239 //UNUSED2009-05 maGrid.ImplSetTextLineSep( nLine, String( *pString, eCharSet ), rSepChars, cTextSep, bMergeSep ); 240 //UNUSED2009-05 } 241 //UNUSED2009-05 EnableRepaint(); 242 //UNUSED2009-05 } 243 244 245 // column settings ------------------------------------------------------------ 246 247 void ScCsvTableBox::InitTypes( const ListBox& rListBox ) 248 { 249 sal_uInt16 nTypeCount = rListBox.GetEntryCount(); 250 StringVec aTypeNames( nTypeCount ); 251 for( sal_uInt16 nIndex = 0; nIndex < nTypeCount; ++nIndex ) 252 aTypeNames[ nIndex ] = rListBox.GetEntry( nIndex ); 253 maGrid.SetTypeNames( aTypeNames ); 254 } 255 256 void ScCsvTableBox::FillColumnData( ScAsciiOptions& rOptions ) const 257 { 258 if( mbFixedMode ) 259 maGrid.FillColumnDataFix( rOptions ); 260 else 261 maGrid.FillColumnDataSep( rOptions ); 262 } 263 264 265 // event handling ------------------------------------------------------------- 266 267 void ScCsvTableBox::Resize() 268 { 269 ScCsvControl::Resize(); 270 InitControls(); 271 } 272 273 void ScCsvTableBox::DataChanged( const DataChangedEvent& rDCEvt ) 274 { 275 if( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_STYLE) ) 276 InitControls(); 277 ScCsvControl::DataChanged( rDCEvt ); 278 } 279 280 IMPL_LINK( ScCsvTableBox, CsvCmdHdl, ScCsvControl*, pCtrl ) 281 { 282 DBG_ASSERT( pCtrl, "ScCsvTableBox::CsvCmdHdl - missing sender" ); 283 284 const ScCsvCmd& rCmd = pCtrl->GetCmd(); 285 ScCsvCmdType eType = rCmd.GetType(); 286 sal_Int32 nParam1 = rCmd.GetParam1(); 287 sal_Int32 nParam2 = rCmd.GetParam2(); 288 289 bool bFound = true; 290 switch( eType ) 291 { 292 case CSVCMD_REPAINT: 293 if( !IsNoRepaint() ) 294 { 295 maGrid.ImplRedraw(); 296 maRuler.ImplRedraw(); 297 InitHScrollBar(); 298 InitVScrollBar(); 299 } 300 break; 301 case CSVCMD_MAKEPOSVISIBLE: 302 MakePosVisible( nParam1 ); 303 break; 304 305 case CSVCMD_NEWCELLTEXTS: 306 if( mbFixedMode ) 307 Execute( CSVCMD_UPDATECELLTEXTS ); 308 else 309 { 310 DisableRepaint(); 311 ScCsvColStateVec aStates( maGrid.GetColumnStates() ); 312 sal_Int32 nPos = GetFirstVisPos(); 313 Execute( CSVCMD_SETPOSCOUNT, 1 ); 314 Execute( CSVCMD_UPDATECELLTEXTS ); 315 Execute( CSVCMD_SETPOSOFFSET, nPos ); 316 maGrid.SetColumnStates( aStates ); 317 EnableRepaint(); 318 } 319 break; 320 case CSVCMD_UPDATECELLTEXTS: 321 maUpdateTextHdl.Call( this ); 322 break; 323 case CSVCMD_SETCOLUMNTYPE: 324 maGrid.SetSelColumnType( nParam1 ); 325 break; 326 case CSVCMD_EXPORTCOLUMNTYPE: 327 maColTypeHdl.Call( this ); 328 break; 329 case CSVCMD_SETFIRSTIMPORTLINE: 330 maGrid.SetFirstImportedLine( nParam1 ); 331 break; 332 333 case CSVCMD_INSERTSPLIT: 334 DBG_ASSERT( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::InsertSplit - invalid call" ); 335 if( maRuler.GetSplitCount() + 1 < sal::static_int_cast<sal_uInt32>(CSV_MAXCOLCOUNT) ) 336 { 337 maRuler.InsertSplit( nParam1 ); 338 maGrid.InsertSplit( nParam1 ); 339 } 340 break; 341 case CSVCMD_REMOVESPLIT: 342 DBG_ASSERT( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::RemoveSplit - invalid call" ); 343 maRuler.RemoveSplit( nParam1 ); 344 maGrid.RemoveSplit( nParam1 ); 345 break; 346 case CSVCMD_TOGGLESPLIT: 347 Execute( maRuler.HasSplit( nParam1 ) ? CSVCMD_REMOVESPLIT : CSVCMD_INSERTSPLIT, nParam1 ); 348 break; 349 case CSVCMD_MOVESPLIT: 350 DBG_ASSERT( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::MoveSplit - invalid call" ); 351 maRuler.MoveSplit( nParam1, nParam2 ); 352 maGrid.MoveSplit( nParam1, nParam2 ); 353 break; 354 case CSVCMD_REMOVEALLSPLITS: 355 DBG_ASSERT( mbFixedMode, "ScCsvTableBox::CsvCmdHdl::RemoveAllSplits - invalid call" ); 356 maRuler.RemoveAllSplits(); 357 maGrid.RemoveAllSplits(); 358 break; 359 default: 360 bFound = false; 361 } 362 if( bFound ) 363 return 0; 364 365 const ScCsvLayoutData aOldData( maData ); 366 switch( eType ) 367 { 368 case CSVCMD_SETPOSCOUNT: 369 maData.mnPosCount = Max( nParam1, sal_Int32( 1 ) ); 370 ImplSetPosOffset( GetFirstVisPos() ); 371 break; 372 case CSVCMD_SETPOSOFFSET: 373 ImplSetPosOffset( nParam1 ); 374 break; 375 case CSVCMD_SETHDRWIDTH: 376 maData.mnHdrWidth = Max( nParam1, sal_Int32( 0 ) ); 377 ImplSetPosOffset( GetFirstVisPos() ); 378 break; 379 case CSVCMD_SETCHARWIDTH: 380 maData.mnCharWidth = Max( nParam1, sal_Int32( 1 ) ); 381 ImplSetPosOffset( GetFirstVisPos() ); 382 break; 383 case CSVCMD_SETLINECOUNT: 384 maData.mnLineCount = Max( nParam1, sal_Int32( 1 ) ); 385 ImplSetLineOffset( GetFirstVisLine() ); 386 break; 387 case CSVCMD_SETLINEOFFSET: 388 ImplSetLineOffset( nParam1 ); 389 break; 390 case CSVCMD_SETHDRHEIGHT: 391 maData.mnHdrHeight = Max( nParam1, sal_Int32( 0 ) ); 392 ImplSetLineOffset( GetFirstVisLine() ); 393 break; 394 case CSVCMD_SETLINEHEIGHT: 395 maData.mnLineHeight = Max( nParam1, sal_Int32( 1 ) ); 396 ImplSetLineOffset( GetFirstVisLine() ); 397 break; 398 case CSVCMD_MOVERULERCURSOR: 399 maData.mnPosCursor = IsVisibleSplitPos( nParam1 ) ? nParam1 : CSV_POS_INVALID; 400 break; 401 case CSVCMD_MOVEGRIDCURSOR: 402 maData.mnColCursor = ((0 <= nParam1) && (nParam1 < GetPosCount())) ? nParam1 : CSV_POS_INVALID; 403 break; 404 default: 405 { 406 // added to avoid warnings 407 } 408 } 409 410 if( maData != aOldData ) 411 { 412 DisableRepaint(); 413 maRuler.ApplyLayout( aOldData ); 414 maGrid.ApplyLayout( aOldData ); 415 EnableRepaint(); 416 } 417 418 return 0; 419 } 420 421 IMPL_LINK( ScCsvTableBox, ScrollHdl, ScrollBar*, pScrollBar ) 422 { 423 DBG_ASSERT( pScrollBar, "ScCsvTableBox::ScrollHdl - missing sender" ); 424 425 if( pScrollBar == &maHScroll ) 426 Execute( CSVCMD_SETPOSOFFSET, pScrollBar->GetThumbPos() ); 427 else if( pScrollBar == &maVScroll ) 428 Execute( CSVCMD_SETLINEOFFSET, pScrollBar->GetThumbPos() ); 429 430 return 0; 431 } 432 433 IMPL_LINK( ScCsvTableBox, ScrollEndHdl, ScrollBar*, pScrollBar ) 434 { 435 DBG_ASSERT( pScrollBar, "ScCsvTableBox::ScrollEndHdl - missing sender" ); 436 437 if( pScrollBar == &maHScroll ) 438 { 439 if( GetRulerCursorPos() != CSV_POS_INVALID ) 440 Execute( CSVCMD_MOVERULERCURSOR, maRuler.GetNoScrollPos( GetRulerCursorPos() ) ); 441 if( GetGridCursorPos() != CSV_POS_INVALID ) 442 Execute( CSVCMD_MOVEGRIDCURSOR, maGrid.GetNoScrollCol( GetGridCursorPos() ) ); 443 } 444 445 return 0; 446 } 447 448 449 // accessibility -------------------------------------------------------------- 450 451 ScCsvTableBox::XAccessibleRef ScCsvTableBox::CreateAccessible() 452 { 453 // do not use the ScCsvControl mechanism, return default accessible object 454 return Control::CreateAccessible(); 455 } 456 457 ScAccessibleCsvControl* ScCsvTableBox::ImplCreateAccessible() 458 { 459 return NULL; // not used, see CreateAccessible() 460 } 461 462 463 // ============================================================================ 464 465