Update ooo320-m1
[ooovba.git] / svx / source / dialog / simptabl.cxx
blob4a3ad5e661a14ac3825961b637a89669973fa367
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: simptabl.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 // INCLUDE -------------------------------------------------------------------
36 #include <svx/simptabl.hxx>
37 #include <vcl/svapp.hxx>
39 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
40 #include <comphelper/processfactory.hxx>
41 #endif
42 #include <unotools/intlwrapper.hxx>
44 // SvxSimpTblContainer ------------------------------------------------------
46 SvxSimpTblContainer::SvxSimpTblContainer( Window* pParent, WinBits nWinStyle):
47 Control(pParent,nWinStyle)
49 //Do Nothing;
52 SvxSimpTblContainer::SvxSimpTblContainer( Window* pParent, const ResId& rResId):
53 Control(pParent,rResId)
55 //Do Nothing;
58 long SvxSimpTblContainer::PreNotify( NotifyEvent& rNEvt )
60 long nResult = TRUE;
61 if ( rNEvt.GetType() == EVENT_KEYINPUT )
63 const KeyCode& aKeyCode = rNEvt.GetKeyEvent()->GetKeyCode();
64 USHORT nKey = aKeyCode.GetCode();
65 if ( nKey == KEY_TAB )
66 GetParent()->Notify( rNEvt );
67 else if ( m_pTable->IsFocusOnCellEnabled() && ( nKey == KEY_LEFT || nKey == KEY_RIGHT ) )
68 return 0;
69 else
70 nResult = Control::PreNotify( rNEvt );
72 else
73 nResult = Control::PreNotify( rNEvt );
75 return nResult;
79 // SvxSimpleTable ------------------------------------------------------------
81 SvxSimpleTable::SvxSimpleTable( Window* pParent,WinBits nBits ):
82 SvHeaderTabListBox(pParent,WB_CLIPCHILDREN | WB_HSCROLL | WB_TABSTOP),
83 aPrivContainer(pParent,nBits|WB_DIALOGCONTROL),
84 aHeaderBar(pParent,WB_BUTTONSTYLE | WB_BORDER | WB_TABSTOP),
85 nHeaderItemId(1),
86 bResizeFlag(FALSE),
87 bPaintFlag(TRUE)
89 bSortDirection=TRUE;
90 nSortCol=0xFFFF;
91 nOldPos=0;
93 SetParent(&aPrivContainer);
94 aHeaderBar.SetParent(&aPrivContainer);
95 aPrivContainer.SetTable( this );
97 aHeaderBar.SetStartDragHdl(LINK( this, SvxSimpleTable, StartDragHdl));
98 aHeaderBar.SetDragHdl(LINK( this, SvxSimpleTable, DragHdl));
99 aHeaderBar.SetEndDragHdl(LINK( this, SvxSimpleTable, EndDragHdl));
100 aHeaderBar.SetSelectHdl(LINK( this, SvxSimpleTable, HeaderBarClick));
101 aHeaderBar.SetDoubleClickHdl(LINK( this, SvxSimpleTable, HeaderBarDblClick));
103 EnableCellFocus();
104 DisableTransientChildren();
105 InitHeaderBar( &aHeaderBar );
107 aHeaderBar.Show();
108 SvHeaderTabListBox::Show();
112 SvxSimpleTable::SvxSimpleTable( Window* pParent,const ResId& rResId):
113 SvHeaderTabListBox(pParent,WB_CLIPCHILDREN | WB_HSCROLL | WB_TABSTOP),
114 aPrivContainer(pParent,rResId),
115 aHeaderBar(pParent,WB_BUTTONSTYLE | WB_BORDER | WB_TABSTOP),
116 nHeaderItemId(1),
117 bResizeFlag(TRUE),
118 bPaintFlag(TRUE)
121 bSortDirection=TRUE;
122 nOldPos=0;
123 nSortCol=0xFFFF;
125 pMyParentWin=pParent;
126 SetParent(&aPrivContainer);
127 aHeaderBar.SetParent(&aPrivContainer);
128 aPrivContainer.SetTable( this );
130 WinBits nBits=aPrivContainer.GetStyle()|WB_DIALOGCONTROL;
131 aPrivContainer.SetStyle(nBits);
133 aHeaderBar.SetStartDragHdl(LINK( this, SvxSimpleTable, StartDragHdl));
134 aHeaderBar.SetDragHdl(LINK( this, SvxSimpleTable, DragHdl));
135 aHeaderBar.SetEndDragHdl(LINK( this, SvxSimpleTable, EndDragHdl));
136 aHeaderBar.SetSelectHdl(LINK( this, SvxSimpleTable, HeaderBarClick));
137 aHeaderBar.SetDoubleClickHdl(LINK( this, SvxSimpleTable, HeaderBarDblClick));
139 Size theWinSize=aPrivContainer.GetOutputSizePixel();
140 Size HbSize=aHeaderBar.GetSizePixel();
142 HbSize.Width()=theWinSize.Width();
143 theWinSize.Height()-=HbSize.Height();
144 Point thePos(0,0);
146 aHeaderBar.SetPosPixel(thePos);
147 aHeaderBar.SetSizePixel(HbSize);
149 thePos.Y()+=HbSize.Height();
150 SvHeaderTabListBox::SetPosPixel(thePos);
151 SvHeaderTabListBox::SetSizePixel(theWinSize);
153 EnableCellFocus();
154 DisableTransientChildren();
155 InitHeaderBar( &aHeaderBar );
157 aHeaderBar.Show();
158 SetWindowBits(WB_CLIPCHILDREN | WB_HSCROLL);
159 SvHeaderTabListBox::Show();
162 SvxSimpleTable::~SvxSimpleTable()
164 SetParent(pMyParentWin);
165 aPrivContainer.SetParent(this);
166 aHeaderBar.SetParent(this);
170 void SvxSimpleTable::UpdateViewSize()
172 Size theWinSize=aPrivContainer.GetOutputSizePixel();
173 Size HbSize=aHeaderBar.GetSizePixel();
175 HbSize.Width()=theWinSize.Width();
176 theWinSize.Height()-=HbSize.Height();
177 Point thePos(0,0);
179 aHeaderBar.SetPosPixel(thePos);
180 aHeaderBar.SetSizePixel(HbSize);
182 thePos.Y()+=HbSize.Height();
183 SvHeaderTabListBox::SetPosPixel(thePos);
184 SvHeaderTabListBox::SetSizePixel(theWinSize);
185 Invalidate();
188 void SvxSimpleTable::NotifyScrolled()
190 long nOffset=-GetXOffset();
191 if(nOldPos!=nOffset)
193 aHeaderBar.SetOffset(nOffset);
194 aHeaderBar.Invalidate();
195 aHeaderBar.Update();
196 nOldPos=nOffset;
198 SvHeaderTabListBox::NotifyScrolled();
201 void SvxSimpleTable::SetTabs()
203 SvHeaderTabListBox::SetTabs();
205 USHORT nPrivTabCount = TabCount();
206 if ( nPrivTabCount )
208 if ( nPrivTabCount > aHeaderBar.GetItemCount() )
209 nPrivTabCount = aHeaderBar.GetItemCount();
211 USHORT i, nNewSize = static_cast< USHORT >( GetTab(0) ), nPos = 0;
212 for ( i = 1; i < nPrivTabCount; ++i )
214 nNewSize = static_cast< USHORT >( GetTab(i) ) - nPos;
215 aHeaderBar.SetItemSize( i, nNewSize );
216 nPos = (USHORT)GetTab(i);
219 aHeaderBar.SetItemSize( i, HEADERBAR_FULLSIZE ); // because no tab for last entry
223 void SvxSimpleTable::SetTabs( long* pTabs, MapUnit eMapUnit)
225 SvHeaderTabListBox::SetTabs(pTabs,eMapUnit);
228 void SvxSimpleTable::Paint( const Rectangle& rRect )
230 SvHeaderTabListBox::Paint(rRect );
232 USHORT nPrivTabCount = TabCount();
233 USHORT nPos = 0;
234 USHORT nNewSize = ( nPrivTabCount > 0 ) ? (USHORT)GetTab(0) : 0;
236 long nOffset=-GetXOffset();
237 nOldPos=nOffset;
239 aHeaderBar.SetOffset(nOffset);
240 aHeaderBar.Invalidate();
242 if(nPrivTabCount && bPaintFlag)
244 if(nPrivTabCount>aHeaderBar.GetItemCount())
245 nPrivTabCount=aHeaderBar.GetItemCount();
247 for(USHORT i=1;i<nPrivTabCount;i++)
249 nNewSize = static_cast< USHORT >( GetTab(i) ) - nPos;
250 aHeaderBar.SetItemSize( i, nNewSize );
251 nPos= static_cast< USHORT >( GetTab(i) );
254 bPaintFlag=TRUE;
256 void SvxSimpleTable::InsertHeaderEntry(const XubString& rText,USHORT nCol,
257 HeaderBarItemBits nBits)
259 xub_StrLen nEnd = rText.Search( sal_Unicode( '\t' ) );
260 if( nEnd == STRING_NOTFOUND )
262 aHeaderBar.InsertItem(nHeaderItemId++, rText, 0, nBits, nCol);
264 else
266 xub_StrLen nCount = rText.GetTokenCount( sal_Unicode( '\t' ) );
268 for( xub_StrLen i=0; i<nCount; i++ )
270 String aString=rText.GetToken(i, sal_Unicode( '\t' ) );
271 aHeaderBar.InsertItem(nHeaderItemId++, aString, 0, nBits, nCol);
274 SetTabs();
277 void SvxSimpleTable::ClearAll()
279 aHeaderBar.Clear();
280 Clear();
282 void SvxSimpleTable::ClearHeader()
284 aHeaderBar.Clear();
287 void SvxSimpleTable::ShowTable()
289 aPrivContainer.Show();
292 void SvxSimpleTable::HideTable()
294 aPrivContainer.Show();
297 BOOL SvxSimpleTable::IsVisible() const
299 return aPrivContainer.IsVisible();
302 void SvxSimpleTable::EnableTable()
304 aPrivContainer.Enable();
307 void SvxSimpleTable::DisableTable()
309 aPrivContainer.Disable();
312 BOOL SvxSimpleTable::IsEnabled() const
314 return aPrivContainer.IsEnabled();
317 void SvxSimpleTable::TableToTop()
319 aPrivContainer.ToTop();
322 void SvxSimpleTable::SetPosPixel( const Point& rNewPos )
324 aPrivContainer.SetPosPixel(rNewPos);
327 Point SvxSimpleTable::GetPosPixel() const
329 return aPrivContainer.GetPosPixel();
332 void SvxSimpleTable::SetPosSizePixel( const Point& rNewPos, Size& rNewSize )
334 aPrivContainer.SetPosPixel(rNewPos);
335 aPrivContainer.SetSizePixel(rNewSize);
338 void SvxSimpleTable::SetPosSize( const Point& rNewPos, const Size& rNewSize )
340 aPrivContainer.SetPosPixel(rNewPos);
341 SvHeaderTabListBox::SetPosSizePixel(rNewPos,rNewSize);
344 Size SvxSimpleTable::GetSizePixel() const
346 return aPrivContainer.GetSizePixel();
349 Size SvxSimpleTable::GetOutputSizePixel() const
351 return aPrivContainer.GetOutputSizePixel();
354 void SvxSimpleTable::SetSizePixel(const Size& rNewSize )
356 aPrivContainer.SetSizePixel(rNewSize);
357 UpdateViewSize();
360 void SvxSimpleTable::SetOutputSizePixel(const Size& rNewSize )
362 aPrivContainer.SetOutputSizePixel(rNewSize);
363 UpdateViewSize();
366 USHORT SvxSimpleTable::GetSelectedCol()
368 return (aHeaderBar.GetCurItemId()-1);
371 void SvxSimpleTable::SortByCol(USHORT nCol,BOOL bDir)
373 bSortDirection=bDir;
374 if(nSortCol!=0xFFFF)
375 aHeaderBar.SetItemBits(nSortCol+1,HIB_STDSTYLE);
377 if (nCol != 0xFFFF)
379 if(bDir)
381 aHeaderBar.SetItemBits( nCol+1, HIB_STDSTYLE | HIB_DOWNARROW);
382 GetModel()->SetSortMode(SortAscending);
384 else
386 aHeaderBar.SetItemBits( nCol+1, HIB_STDSTYLE | HIB_UPARROW);
387 GetModel()->SetSortMode(SortDescending);
389 nSortCol=nCol;
390 GetModel()->SetCompareHdl( LINK( this, SvxSimpleTable, CompareHdl));
391 GetModel()->Resort();
393 else
394 GetModel()->SetSortMode(SortNone);
395 nSortCol=nCol;
398 void SvxSimpleTable::HBarClick()
400 USHORT nId=aHeaderBar.GetCurItemId();
402 if (aHeaderBar.GetItemBits(nId) & HIB_CLICKABLE)
404 if(nId==nSortCol+1)
406 SortByCol(nId-1,!bSortDirection);
408 else
410 SortByCol(nId-1,bSortDirection);
413 aHeaderBarClickLink.Call(this);
417 void SvxSimpleTable::HBarDblClick()
419 aHeaderBarDblClickLink.Call(this);
422 void SvxSimpleTable::HBarStartDrag()
424 if(!aHeaderBar.IsItemMode())
426 Rectangle aSizeRect(Point(0,0),
427 SvHeaderTabListBox::GetOutputSizePixel());
428 aSizeRect.Left()=-GetXOffset()+aHeaderBar.GetDragPos();
429 aSizeRect.Right()=-GetXOffset()+aHeaderBar.GetDragPos();
430 ShowTracking( aSizeRect, SHOWTRACK_SPLIT );
433 void SvxSimpleTable::HBarDrag()
435 HideTracking();
436 if(!aHeaderBar.IsItemMode())
438 Rectangle aSizeRect(Point(0,0),
439 SvHeaderTabListBox::GetOutputSizePixel());
440 aSizeRect.Left()=-GetXOffset()+aHeaderBar.GetDragPos();
441 aSizeRect.Right()=-GetXOffset()+aHeaderBar.GetDragPos();
442 ShowTracking( aSizeRect, SHOWTRACK_SPLIT );
445 void SvxSimpleTable::HBarEndDrag()
447 HideTracking();
448 USHORT nPrivTabCount=TabCount();
449 USHORT nPos=0;
450 USHORT nNewSize=0;
452 if(nPrivTabCount)
454 if(nPrivTabCount>aHeaderBar.GetItemCount())
455 nPrivTabCount=aHeaderBar.GetItemCount();
457 //for(USHORT i=1;i<=nPrivTabCount;i++)
458 for(USHORT i=1;i<nPrivTabCount;i++)
460 nNewSize = static_cast< USHORT >( aHeaderBar.GetItemSize(i) ) + nPos;
461 SetTab( i, nNewSize, MAP_PIXEL );
462 nPos = nNewSize;
465 bPaintFlag=FALSE;
466 Invalidate();
467 Update();
470 CommandEvent SvxSimpleTable::GetCommandEvent() const
472 return aCEvt;
475 void SvxSimpleTable::Command( const CommandEvent& rCEvt )
477 aCEvt=rCEvt;
478 aCommandLink.Call(this);
479 SvHeaderTabListBox::Command(rCEvt);
482 IMPL_LINK( SvxSimpleTable, StartDragHdl, HeaderBar*, pCtr)
484 if(pCtr==&aHeaderBar)
486 HBarStartDrag();
488 return 0;
491 IMPL_LINK( SvxSimpleTable, DragHdl, HeaderBar*, pCtr)
493 if(pCtr==&aHeaderBar)
495 HBarDrag();
497 return 0;
500 IMPL_LINK( SvxSimpleTable, EndDragHdl, HeaderBar*, pCtr)
502 if(pCtr==&aHeaderBar)
504 HBarEndDrag();
506 return 0;
509 IMPL_LINK( SvxSimpleTable, HeaderBarClick, HeaderBar*, pCtr)
511 if(pCtr==&aHeaderBar)
513 HBarClick();
515 return 0;
518 IMPL_LINK( SvxSimpleTable, HeaderBarDblClick, HeaderBar*, pCtr)
520 if(pCtr==&aHeaderBar)
522 HBarDblClick();
524 return 0;
527 SvLBoxItem* SvxSimpleTable::GetEntryAtPos( SvLBoxEntry* pEntry, USHORT nPos ) const
529 DBG_ASSERT(pEntry,"GetEntryText:Invalid Entry");
530 SvLBoxItem* pItem = NULL;
532 if( pEntry )
534 USHORT nCount = pEntry->ItemCount();
536 nPos++;
538 if( nTreeFlags & TREEFLAG_CHKBTN ) nPos++;
540 if( nPos < nCount )
542 pItem = pEntry->GetItem( nPos);
545 return pItem;
548 StringCompare SvxSimpleTable::ColCompare(SvLBoxEntry* pLeft,SvLBoxEntry* pRight)
550 StringCompare eCompare=COMPARE_EQUAL;
552 SvLBoxItem* pLeftItem = GetEntryAtPos( pLeft, nSortCol);
553 SvLBoxItem* pRightItem = GetEntryAtPos( pRight, nSortCol);
556 if(pLeftItem != NULL && pRightItem != NULL)
558 USHORT nLeftKind=pLeftItem->IsA();
559 USHORT nRightKind=pRightItem->IsA();
561 if(nRightKind == SV_ITEM_ID_LBOXSTRING &&
562 nLeftKind == SV_ITEM_ID_LBOXSTRING )
564 IntlWrapper aIntlWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
565 const CollatorWrapper* pCollator = aIntlWrapper.getCaseCollator();
567 eCompare=(StringCompare)pCollator->compareString( ((SvLBoxString*)pLeftItem)->GetText(),
568 ((SvLBoxString*)pRightItem)->GetText());
570 if(eCompare==COMPARE_EQUAL) eCompare=COMPARE_LESS;
573 return eCompare;
576 IMPL_LINK( SvxSimpleTable, CompareHdl, SvSortData*, pData)
578 SvLBoxEntry* pLeft = (SvLBoxEntry*)(pData->pLeft );
579 SvLBoxEntry* pRight = (SvLBoxEntry*)(pData->pRight );
580 return (long) ColCompare(pLeft,pRight);