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_svx.hxx" 30 31 // INCLUDE ------------------------------------------------------------------- 32 33 #include <svx/simptabl.hxx> 34 #include <vcl/svapp.hxx> 35 36 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX 37 #include <comphelper/processfactory.hxx> 38 #endif 39 #include <unotools/intlwrapper.hxx> 40 41 // SvxSimpTblContainer ------------------------------------------------------ 42 43 SvxSimpTblContainer::SvxSimpTblContainer( Window* pParent, WinBits nWinStyle): 44 Control(pParent,nWinStyle) 45 { 46 //Do Nothing; 47 } 48 49 SvxSimpTblContainer::SvxSimpTblContainer( Window* pParent, const ResId& rResId): 50 Control(pParent,rResId) 51 { 52 //Do Nothing; 53 } 54 55 long SvxSimpTblContainer::PreNotify( NotifyEvent& rNEvt ) 56 { 57 long nResult = sal_True; 58 if ( rNEvt.GetType() == EVENT_KEYINPUT ) 59 { 60 const KeyCode& aKeyCode = rNEvt.GetKeyEvent()->GetKeyCode(); 61 sal_uInt16 nKey = aKeyCode.GetCode(); 62 if ( nKey == KEY_TAB ) 63 GetParent()->Notify( rNEvt ); 64 else if ( m_pTable->IsFocusOnCellEnabled() && ( nKey == KEY_LEFT || nKey == KEY_RIGHT ) ) 65 return 0; 66 else 67 nResult = Control::PreNotify( rNEvt ); 68 } 69 else 70 nResult = Control::PreNotify( rNEvt ); 71 72 return nResult; 73 } 74 75 76 // SvxSimpleTable ------------------------------------------------------------ 77 78 SvxSimpleTable::SvxSimpleTable( Window* pParent,WinBits nBits ): 79 SvHeaderTabListBox(pParent,WB_CLIPCHILDREN | WB_HSCROLL | WB_TABSTOP), 80 aPrivContainer(pParent,nBits|WB_DIALOGCONTROL), 81 aHeaderBar(pParent,WB_BUTTONSTYLE | WB_BORDER | WB_TABSTOP), 82 nHeaderItemId(1), 83 bResizeFlag(sal_False), 84 bPaintFlag(sal_True) 85 { 86 bSortDirection=sal_True; 87 nSortCol=0xFFFF; 88 nOldPos=0; 89 90 SetParent(&aPrivContainer); 91 aHeaderBar.SetParent(&aPrivContainer); 92 aPrivContainer.SetTable( this ); 93 94 aHeaderBar.SetStartDragHdl(LINK( this, SvxSimpleTable, StartDragHdl)); 95 aHeaderBar.SetDragHdl(LINK( this, SvxSimpleTable, DragHdl)); 96 aHeaderBar.SetEndDragHdl(LINK( this, SvxSimpleTable, EndDragHdl)); 97 aHeaderBar.SetSelectHdl(LINK( this, SvxSimpleTable, HeaderBarClick)); 98 aHeaderBar.SetDoubleClickHdl(LINK( this, SvxSimpleTable, HeaderBarDblClick)); 99 100 EnableCellFocus(); 101 DisableTransientChildren(); 102 InitHeaderBar( &aHeaderBar ); 103 104 aHeaderBar.Show(); 105 SvHeaderTabListBox::Show(); 106 } 107 108 109 SvxSimpleTable::SvxSimpleTable( Window* pParent,const ResId& rResId): 110 SvHeaderTabListBox(pParent,WB_CLIPCHILDREN | WB_HSCROLL | WB_TABSTOP), 111 aPrivContainer(pParent,rResId), 112 aHeaderBar(pParent,WB_BUTTONSTYLE | WB_BORDER | WB_TABSTOP), 113 nHeaderItemId(1), 114 bResizeFlag(sal_True), 115 bPaintFlag(sal_True) 116 { 117 118 bSortDirection=sal_True; 119 nOldPos=0; 120 nSortCol=0xFFFF; 121 122 pMyParentWin=pParent; 123 SetParent(&aPrivContainer); 124 aHeaderBar.SetParent(&aPrivContainer); 125 aPrivContainer.SetTable( this ); 126 127 WinBits nBits=aPrivContainer.GetStyle()|WB_DIALOGCONTROL; 128 aPrivContainer.SetStyle(nBits); 129 130 aHeaderBar.SetStartDragHdl(LINK( this, SvxSimpleTable, StartDragHdl)); 131 aHeaderBar.SetDragHdl(LINK( this, SvxSimpleTable, DragHdl)); 132 aHeaderBar.SetEndDragHdl(LINK( this, SvxSimpleTable, EndDragHdl)); 133 aHeaderBar.SetSelectHdl(LINK( this, SvxSimpleTable, HeaderBarClick)); 134 aHeaderBar.SetDoubleClickHdl(LINK( this, SvxSimpleTable, HeaderBarDblClick)); 135 136 Size theWinSize=aPrivContainer.GetOutputSizePixel(); 137 Size HbSize=aHeaderBar.GetSizePixel(); 138 139 HbSize.Width()=theWinSize.Width(); 140 theWinSize.Height()-=HbSize.Height(); 141 Point thePos(0,0); 142 143 aHeaderBar.SetPosPixel(thePos); 144 aHeaderBar.SetSizePixel(HbSize); 145 146 thePos.Y()+=HbSize.Height(); 147 SvHeaderTabListBox::SetPosPixel(thePos); 148 SvHeaderTabListBox::SetSizePixel(theWinSize); 149 150 EnableCellFocus(); 151 DisableTransientChildren(); 152 InitHeaderBar( &aHeaderBar ); 153 154 aHeaderBar.Show(); 155 SvHeaderTabListBox::Show(); 156 } 157 158 SvxSimpleTable::~SvxSimpleTable() 159 { 160 SetParent(pMyParentWin); 161 aPrivContainer.SetParent(this); 162 aHeaderBar.SetParent(this); 163 } 164 165 166 void SvxSimpleTable::UpdateViewSize() 167 { 168 Size theWinSize=aPrivContainer.GetOutputSizePixel(); 169 Size HbSize=aHeaderBar.GetSizePixel(); 170 171 HbSize.Width()=theWinSize.Width(); 172 theWinSize.Height()-=HbSize.Height(); 173 Point thePos(0,0); 174 175 aHeaderBar.SetPosPixel(thePos); 176 aHeaderBar.SetSizePixel(HbSize); 177 178 thePos.Y()+=HbSize.Height(); 179 SvHeaderTabListBox::SetPosPixel(thePos); 180 SvHeaderTabListBox::SetSizePixel(theWinSize); 181 Invalidate(); 182 } 183 184 void SvxSimpleTable::NotifyScrolled() 185 { 186 long nOffset=-GetXOffset(); 187 if(nOldPos!=nOffset) 188 { 189 aHeaderBar.SetOffset(nOffset); 190 aHeaderBar.Invalidate(); 191 aHeaderBar.Update(); 192 nOldPos=nOffset; 193 } 194 SvHeaderTabListBox::NotifyScrolled(); 195 } 196 197 void SvxSimpleTable::SetTabs() 198 { 199 SvHeaderTabListBox::SetTabs(); 200 201 sal_uInt16 nPrivTabCount = TabCount(); 202 if ( nPrivTabCount ) 203 { 204 if ( nPrivTabCount > aHeaderBar.GetItemCount() ) 205 nPrivTabCount = aHeaderBar.GetItemCount(); 206 207 sal_uInt16 i, nNewSize = static_cast< sal_uInt16 >( GetTab(0) ), nPos = 0; 208 for ( i = 1; i < nPrivTabCount; ++i ) 209 { 210 nNewSize = static_cast< sal_uInt16 >( GetTab(i) ) - nPos; 211 aHeaderBar.SetItemSize( i, nNewSize ); 212 nPos = (sal_uInt16)GetTab(i); 213 } 214 215 aHeaderBar.SetItemSize( i, HEADERBAR_FULLSIZE ); // because no tab for last entry 216 } 217 } 218 219 void SvxSimpleTable::SetTabs( long* pTabs, MapUnit eMapUnit) 220 { 221 SvHeaderTabListBox::SetTabs(pTabs,eMapUnit); 222 } 223 224 void SvxSimpleTable::Paint( const Rectangle& rRect ) 225 { 226 SvHeaderTabListBox::Paint(rRect ); 227 228 sal_uInt16 nPrivTabCount = TabCount(); 229 sal_uInt16 nPos = 0; 230 sal_uInt16 nNewSize = ( nPrivTabCount > 0 ) ? (sal_uInt16)GetTab(0) : 0; 231 232 long nOffset=-GetXOffset(); 233 nOldPos=nOffset; 234 235 aHeaderBar.SetOffset(nOffset); 236 aHeaderBar.Invalidate(); 237 238 if(nPrivTabCount && bPaintFlag) 239 { 240 if(nPrivTabCount>aHeaderBar.GetItemCount()) 241 nPrivTabCount=aHeaderBar.GetItemCount(); 242 243 for(sal_uInt16 i=1;i<nPrivTabCount;i++) 244 { 245 nNewSize = static_cast< sal_uInt16 >( GetTab(i) ) - nPos; 246 aHeaderBar.SetItemSize( i, nNewSize ); 247 nPos= static_cast< sal_uInt16 >( GetTab(i) ); 248 } 249 } 250 bPaintFlag=sal_True; 251 } 252 void SvxSimpleTable::InsertHeaderEntry(const XubString& rText,sal_uInt16 nCol, 253 HeaderBarItemBits nBits) 254 { 255 xub_StrLen nEnd = rText.Search( sal_Unicode( '\t' ) ); 256 if( nEnd == STRING_NOTFOUND ) 257 { 258 aHeaderBar.InsertItem(nHeaderItemId++, rText, 0, nBits, nCol); 259 } 260 else 261 { 262 xub_StrLen nCount = rText.GetTokenCount( sal_Unicode( '\t' ) ); 263 264 for( xub_StrLen i=0; i<nCount; i++ ) 265 { 266 String aString=rText.GetToken(i, sal_Unicode( '\t' ) ); 267 aHeaderBar.InsertItem(nHeaderItemId++, aString, 0, nBits, nCol); 268 } 269 } 270 SetTabs(); 271 } 272 273 void SvxSimpleTable::ClearAll() 274 { 275 aHeaderBar.Clear(); 276 Clear(); 277 } 278 void SvxSimpleTable::ClearHeader() 279 { 280 aHeaderBar.Clear(); 281 } 282 283 void SvxSimpleTable::ShowTable() 284 { 285 aPrivContainer.Show(); 286 } 287 288 void SvxSimpleTable::HideTable() 289 { 290 aPrivContainer.Hide(); 291 } 292 293 sal_Bool SvxSimpleTable::IsVisible() const 294 { 295 return aPrivContainer.IsVisible(); 296 } 297 298 void SvxSimpleTable::EnableTable() 299 { 300 aPrivContainer.Enable(); 301 } 302 303 void SvxSimpleTable::DisableTable() 304 { 305 aPrivContainer.Disable(); 306 } 307 308 sal_Bool SvxSimpleTable::IsEnabled() const 309 { 310 return aPrivContainer.IsEnabled(); 311 } 312 313 void SvxSimpleTable::TableToTop() 314 { 315 aPrivContainer.ToTop(); 316 } 317 318 void SvxSimpleTable::SetPosPixel( const Point& rNewPos ) 319 { 320 aPrivContainer.SetPosPixel(rNewPos); 321 } 322 323 Point SvxSimpleTable::GetPosPixel() const 324 { 325 return aPrivContainer.GetPosPixel(); 326 } 327 328 void SvxSimpleTable::SetPosSizePixel( const Point& rNewPos, Size& rNewSize ) 329 { 330 aPrivContainer.SetPosPixel(rNewPos); 331 aPrivContainer.SetSizePixel(rNewSize); 332 } 333 334 void SvxSimpleTable::SetPosSize( const Point& rNewPos, const Size& rNewSize ) 335 { 336 aPrivContainer.SetPosPixel(rNewPos); 337 SvHeaderTabListBox::SetPosSizePixel(rNewPos,rNewSize); 338 } 339 340 Size SvxSimpleTable::GetSizePixel() const 341 { 342 return aPrivContainer.GetSizePixel(); 343 } 344 345 Size SvxSimpleTable::GetOutputSizePixel() const 346 { 347 return aPrivContainer.GetOutputSizePixel(); 348 } 349 350 void SvxSimpleTable::SetSizePixel(const Size& rNewSize ) 351 { 352 aPrivContainer.SetSizePixel(rNewSize); 353 UpdateViewSize(); 354 } 355 356 void SvxSimpleTable::SetOutputSizePixel(const Size& rNewSize ) 357 { 358 aPrivContainer.SetOutputSizePixel(rNewSize); 359 UpdateViewSize(); 360 } 361 362 sal_uInt16 SvxSimpleTable::GetSelectedCol() 363 { 364 return (aHeaderBar.GetCurItemId()-1); 365 } 366 367 void SvxSimpleTable::SortByCol(sal_uInt16 nCol,sal_Bool bDir) 368 { 369 bSortDirection=bDir; 370 if(nSortCol!=0xFFFF) 371 aHeaderBar.SetItemBits(nSortCol+1,HIB_STDSTYLE); 372 373 if (nCol != 0xFFFF) 374 { 375 if(bDir) 376 { 377 aHeaderBar.SetItemBits( nCol+1, HIB_STDSTYLE | HIB_DOWNARROW); 378 GetModel()->SetSortMode(SortAscending); 379 } 380 else 381 { 382 aHeaderBar.SetItemBits( nCol+1, HIB_STDSTYLE | HIB_UPARROW); 383 GetModel()->SetSortMode(SortDescending); 384 } 385 nSortCol=nCol; 386 GetModel()->SetCompareHdl( LINK( this, SvxSimpleTable, CompareHdl)); 387 GetModel()->Resort(); 388 } 389 else 390 GetModel()->SetSortMode(SortNone); 391 nSortCol=nCol; 392 } 393 394 void SvxSimpleTable::HBarClick() 395 { 396 sal_uInt16 nId=aHeaderBar.GetCurItemId(); 397 398 if (aHeaderBar.GetItemBits(nId) & HIB_CLICKABLE) 399 { 400 if(nId==nSortCol+1) 401 { 402 SortByCol(nId-1,!bSortDirection); 403 } 404 else 405 { 406 SortByCol(nId-1,bSortDirection); 407 } 408 409 aHeaderBarClickLink.Call(this); 410 } 411 } 412 413 void SvxSimpleTable::HBarDblClick() 414 { 415 aHeaderBarDblClickLink.Call(this); 416 } 417 418 void SvxSimpleTable::HBarStartDrag() 419 { 420 if(!aHeaderBar.IsItemMode()) 421 { 422 Rectangle aSizeRect(Point(0,0), 423 SvHeaderTabListBox::GetOutputSizePixel()); 424 aSizeRect.Left()=-GetXOffset()+aHeaderBar.GetDragPos(); 425 aSizeRect.Right()=-GetXOffset()+aHeaderBar.GetDragPos(); 426 ShowTracking( aSizeRect, SHOWTRACK_SPLIT ); 427 } 428 } 429 void SvxSimpleTable::HBarDrag() 430 { 431 HideTracking(); 432 if(!aHeaderBar.IsItemMode()) 433 { 434 Rectangle aSizeRect(Point(0,0), 435 SvHeaderTabListBox::GetOutputSizePixel()); 436 aSizeRect.Left()=-GetXOffset()+aHeaderBar.GetDragPos(); 437 aSizeRect.Right()=-GetXOffset()+aHeaderBar.GetDragPos(); 438 ShowTracking( aSizeRect, SHOWTRACK_SPLIT ); 439 } 440 } 441 void SvxSimpleTable::HBarEndDrag() 442 { 443 HideTracking(); 444 sal_uInt16 nPrivTabCount=TabCount(); 445 sal_uInt16 nPos=0; 446 sal_uInt16 nNewSize=0; 447 448 if(nPrivTabCount) 449 { 450 if(nPrivTabCount>aHeaderBar.GetItemCount()) 451 nPrivTabCount=aHeaderBar.GetItemCount(); 452 453 //for(sal_uInt16 i=1;i<=nPrivTabCount;i++) 454 for(sal_uInt16 i=1;i<nPrivTabCount;i++) 455 { 456 nNewSize = static_cast< sal_uInt16 >( aHeaderBar.GetItemSize(i) ) + nPos; 457 SetTab( i, nNewSize, MAP_PIXEL ); 458 nPos = nNewSize; 459 } 460 } 461 bPaintFlag=sal_False; 462 Invalidate(); 463 Update(); 464 } 465 466 CommandEvent SvxSimpleTable::GetCommandEvent() const 467 { 468 return aCEvt; 469 } 470 471 void SvxSimpleTable::Command( const CommandEvent& rCEvt ) 472 { 473 aCEvt=rCEvt; 474 aCommandLink.Call(this); 475 SvHeaderTabListBox::Command(rCEvt); 476 } 477 478 IMPL_LINK( SvxSimpleTable, StartDragHdl, HeaderBar*, pCtr) 479 { 480 if(pCtr==&aHeaderBar) 481 { 482 HBarStartDrag(); 483 } 484 return 0; 485 } 486 487 IMPL_LINK( SvxSimpleTable, DragHdl, HeaderBar*, pCtr) 488 { 489 if(pCtr==&aHeaderBar) 490 { 491 HBarDrag(); 492 } 493 return 0; 494 } 495 496 IMPL_LINK( SvxSimpleTable, EndDragHdl, HeaderBar*, pCtr) 497 { 498 if(pCtr==&aHeaderBar) 499 { 500 HBarEndDrag(); 501 } 502 return 0; 503 } 504 505 IMPL_LINK( SvxSimpleTable, HeaderBarClick, HeaderBar*, pCtr) 506 { 507 if(pCtr==&aHeaderBar) 508 { 509 HBarClick(); 510 } 511 return 0; 512 } 513 514 IMPL_LINK( SvxSimpleTable, HeaderBarDblClick, HeaderBar*, pCtr) 515 { 516 if(pCtr==&aHeaderBar) 517 { 518 HBarDblClick(); 519 } 520 return 0; 521 } 522 523 SvLBoxItem* SvxSimpleTable::GetEntryAtPos( SvLBoxEntry* pEntry, sal_uInt16 nPos ) const 524 { 525 DBG_ASSERT(pEntry,"GetEntryText:Invalid Entry"); 526 SvLBoxItem* pItem = NULL; 527 528 if( pEntry ) 529 { 530 sal_uInt16 nCount = pEntry->ItemCount(); 531 532 nPos++; 533 534 if( nTreeFlags & TREEFLAG_CHKBTN ) nPos++; 535 536 if( nPos < nCount ) 537 { 538 pItem = pEntry->GetItem( nPos); 539 } 540 } 541 return pItem; 542 } 543 544 StringCompare SvxSimpleTable::ColCompare(SvLBoxEntry* pLeft,SvLBoxEntry* pRight) 545 { 546 StringCompare eCompare=COMPARE_EQUAL; 547 548 SvLBoxItem* pLeftItem = GetEntryAtPos( pLeft, nSortCol); 549 SvLBoxItem* pRightItem = GetEntryAtPos( pRight, nSortCol); 550 551 552 if(pLeftItem != NULL && pRightItem != NULL) 553 { 554 sal_uInt16 nLeftKind=pLeftItem->IsA(); 555 sal_uInt16 nRightKind=pRightItem->IsA(); 556 557 if(nRightKind == SV_ITEM_ID_LBOXSTRING && 558 nLeftKind == SV_ITEM_ID_LBOXSTRING ) 559 { 560 IntlWrapper aIntlWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() ); 561 const CollatorWrapper* pCollator = aIntlWrapper.getCaseCollator(); 562 563 eCompare=(StringCompare)pCollator->compareString( ((SvLBoxString*)pLeftItem)->GetText(), 564 ((SvLBoxString*)pRightItem)->GetText()); 565 566 if(eCompare==COMPARE_EQUAL) eCompare=COMPARE_LESS; 567 } 568 } 569 return eCompare; 570 } 571 572 IMPL_LINK( SvxSimpleTable, CompareHdl, SvSortData*, pData) 573 { 574 SvLBoxEntry* pLeft = (SvLBoxEntry*)(pData->pLeft ); 575 SvLBoxEntry* pRight = (SvLBoxEntry*)(pData->pRight ); 576 return (long) ColCompare(pLeft,pRight); 577 } 578 579 580