1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <comphelper/processfactory.hxx>
21 #include <svtools/simptabl.hxx>
22 #include <svtools/svlbitm.hxx>
23 #include <svtools/treelistentry.hxx>
24 #include <vcl/builderfactory.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/settings.hxx>
28 SvSimpleTableContainer::SvSimpleTableContainer(vcl::Window
* pParent
, WinBits nBits
)
29 : Control(pParent
, nBits
)
34 SvSimpleTableContainer::~SvSimpleTableContainer()
39 void SvSimpleTableContainer::dispose()
45 VCL_BUILDER_FACTORY_ARGS(SvSimpleTableContainer
,
46 WB_TABSTOP
| WB_DIALOGCONTROL
| WB_BORDER
)
48 void SvSimpleTableContainer::SetTable(SvSimpleTable
* pTable
)
53 bool SvSimpleTableContainer::PreNotify( NotifyEvent
& rNEvt
)
56 if ( rNEvt
.GetType() == MouseNotifyEvent::KEYINPUT
)
58 const vcl::KeyCode
& aKeyCode
= rNEvt
.GetKeyEvent()->GetKeyCode();
59 sal_uInt16 nKey
= aKeyCode
.GetCode();
61 GetParent()->Notify( rNEvt
);
62 else if (m_pTable
&& m_pTable
->IsFocusOnCellEnabled() && ( nKey
== KEY_LEFT
|| nKey
== KEY_RIGHT
))
65 nResult
= Control::PreNotify( rNEvt
);
68 nResult
= Control::PreNotify( rNEvt
);
73 void SvSimpleTableContainer::Resize()
77 m_pTable
->UpdateViewSize();
80 void SvSimpleTableContainer::GetFocus()
84 m_pTable
->GrabFocus();
87 // SvSimpleTable ------------------------------------------------------------
89 SvSimpleTable::SvSimpleTable(SvSimpleTableContainer
& rParent
, WinBits nBits
):
90 SvHeaderTabListBox(&rParent
, nBits
| WB_CLIPCHILDREN
| WB_HSCROLL
| WB_TABSTOP
),
91 m_rParentTableContainer(rParent
),
92 aHeaderBar(VclPtr
<HeaderBar
>::Create(&rParent
,WB_BUTTONSTYLE
| WB_BORDER
| WB_TABSTOP
)),
95 aCollator(*(IntlWrapper( Application::GetSettings().GetLanguageTag() ).getCaseCollator()))
97 m_rParentTableContainer
.SetTable(this);
99 bSortDirection
= true;
103 aHeaderBar
->SetStartDragHdl(LINK( this, SvSimpleTable
, StartDragHdl
));
104 aHeaderBar
->SetDragHdl(LINK( this, SvSimpleTable
, DragHdl
));
105 aHeaderBar
->SetEndDragHdl(LINK( this, SvSimpleTable
, EndDragHdl
));
106 aHeaderBar
->SetSelectHdl(LINK( this, SvSimpleTable
, HeaderBarClick
));
107 aHeaderBar
->SetDoubleClickHdl(LINK( this, SvSimpleTable
, HeaderBarDblClick
));
111 DisableTransientChildren();
112 InitHeaderBar( aHeaderBar
);
117 SvHeaderTabListBox::Show();
120 SvSimpleTable::~SvSimpleTable()
125 void SvSimpleTable::dispose()
127 m_rParentTableContainer
.SetTable(NULL
);
128 aHeaderBar
.disposeAndClear();
129 SvHeaderTabListBox::dispose();
132 void SvSimpleTable::UpdateViewSize()
134 Size theWinSize
=m_rParentTableContainer
.GetOutputSizePixel();
135 Size HbSize
=aHeaderBar
->GetSizePixel();
137 HbSize
.Width()=theWinSize
.Width();
138 theWinSize
.Height()-=HbSize
.Height();
141 aHeaderBar
->SetPosPixel(thePos
);
142 aHeaderBar
->SetSizePixel(HbSize
);
144 thePos
.Y()+=HbSize
.Height();
145 SvHeaderTabListBox::SetPosPixel(thePos
);
146 SvHeaderTabListBox::SetSizePixel(theWinSize
);
150 void SvSimpleTable::NotifyScrolled()
152 long nOffset
=-GetXOffset();
155 aHeaderBar
->SetOffset(nOffset
);
156 aHeaderBar
->Invalidate();
157 aHeaderBar
->Update();
160 SvHeaderTabListBox::NotifyScrolled();
163 void SvSimpleTable::SetTabs()
165 SvHeaderTabListBox::SetTabs();
167 sal_uInt16 nPrivTabCount
= TabCount();
170 if ( nPrivTabCount
> aHeaderBar
->GetItemCount() )
171 nPrivTabCount
= aHeaderBar
->GetItemCount();
173 sal_uInt16 i
, nPos
= 0;
174 for ( i
= 1; i
< nPrivTabCount
; ++i
)
176 sal_uInt16 nNewSize
= static_cast< sal_uInt16
>( GetTab(i
) ) - nPos
;
177 aHeaderBar
->SetItemSize( i
, nNewSize
);
178 nPos
= (sal_uInt16
)GetTab(i
);
181 aHeaderBar
->SetItemSize( i
, HEADERBAR_FULLSIZE
); // because no tab for last entry
185 void SvSimpleTable::SetTabs(const long* pTabs
, MapUnit eMapUnit
)
187 SvHeaderTabListBox::SetTabs(pTabs
,eMapUnit
);
190 void SvSimpleTable::Paint(vcl::RenderContext
& rRenderContext
, const Rectangle
& rRect
)
192 SvHeaderTabListBox::Paint(rRenderContext
, rRect
);
194 sal_uInt16 nPrivTabCount
= TabCount();
196 long nOffset
=- GetXOffset();
199 aHeaderBar
->SetOffset(nOffset
);
200 aHeaderBar
->Invalidate();
202 if (nPrivTabCount
&& bPaintFlag
)
204 if (nPrivTabCount
>aHeaderBar
->GetItemCount())
205 nPrivTabCount
=aHeaderBar
->GetItemCount();
208 for (sal_uInt16 i
= 1; i
< nPrivTabCount
; i
++)
210 sal_uInt16 nNewSize
= static_cast<sal_uInt16
>(GetTab(i
)) - nPos
;
211 aHeaderBar
->SetItemSize(i
, nNewSize
);
212 nPos
= static_cast<sal_uInt16
>(GetTab(i
));
218 void SvSimpleTable::InsertHeaderEntry(const OUString
& rText
,
219 sal_uInt16 nCol
, HeaderBarItemBits nBits
)
221 sal_Int32 nEnd
= rText
.indexOf( '\t' );
224 aHeaderBar
->InsertItem(nHeaderItemId
++, rText
, 0, nBits
, nCol
);
228 sal_Int32 nIndex
= 0;
231 OUString aString
= rText
.getToken(0, '\t', nIndex
);
232 aHeaderBar
->InsertItem(nHeaderItemId
++, aString
, 0, nBits
, nCol
);
234 while ( nIndex
>= 0 );
239 void SvSimpleTable::ClearHeader()
244 void SvSimpleTable::ShowTable()
246 m_rParentTableContainer
.Show();
249 void SvSimpleTable::HideTable()
251 m_rParentTableContainer
.Hide();
254 bool SvSimpleTable::IsVisible() const
256 return m_rParentTableContainer
.IsVisible();
259 void SvSimpleTable::EnableTable()
261 m_rParentTableContainer
.Enable();
264 void SvSimpleTable::DisableTable()
266 m_rParentTableContainer
.Disable();
269 bool SvSimpleTable::IsEnabled() const
271 return m_rParentTableContainer
.IsEnabled();
274 sal_uInt16
SvSimpleTable::GetSelectedCol()
276 return (aHeaderBar
->GetCurItemId()-1);
279 void SvSimpleTable::SortByCol(sal_uInt16 nCol
, bool bDir
)
282 aHeaderBar
->SetItemBits(nSortCol
+1,HeaderBarItemBits::STDSTYLE
);
286 if(bDir
|| nSortCol
!= nCol
)
288 aHeaderBar
->SetItemBits( nCol
+1, HeaderBarItemBits::STDSTYLE
| HeaderBarItemBits::DOWNARROW
);
289 GetModel()->SetSortMode(SortAscending
);
294 aHeaderBar
->SetItemBits( nCol
+1, HeaderBarItemBits::STDSTYLE
| HeaderBarItemBits::UPARROW
);
295 GetModel()->SetSortMode(SortDescending
);
298 GetModel()->SetCompareHdl( LINK( this, SvSimpleTable
, CompareHdl
));
302 GetModel()->Reverse();
303 Resize(); //update rows
308 GetModel()->Resort();
312 GetModel()->SetSortMode(SortNone
);
315 SetAlternatingRowColors( true );
318 void SvSimpleTable::HBarClick()
320 sal_uInt16 nId
=aHeaderBar
->GetCurItemId();
322 if (aHeaderBar
->GetItemBits(nId
) & HeaderBarItemBits::CLICKABLE
)
326 SortByCol(nId
-1,!bSortDirection
);
330 SortByCol(nId
-1,bSortDirection
);
333 aHeaderBarClickLink
.Call(this);
337 void SvSimpleTable::HBarDblClick()
339 aHeaderBarDblClickLink
.Call(this);
342 void SvSimpleTable::HBarStartDrag()
344 if(!aHeaderBar
->IsItemMode())
346 Rectangle
aSizeRect(Point(0,0),
347 SvHeaderTabListBox::GetOutputSizePixel());
348 aSizeRect
.Left()=-GetXOffset()+aHeaderBar
->GetDragPos();
349 aSizeRect
.Right()=-GetXOffset()+aHeaderBar
->GetDragPos();
350 ShowTracking( aSizeRect
, SHOWTRACK_SPLIT
);
353 void SvSimpleTable::HBarDrag()
356 if(!aHeaderBar
->IsItemMode())
358 Rectangle
aSizeRect(Point(0,0),
359 SvHeaderTabListBox::GetOutputSizePixel());
360 aSizeRect
.Left()=-GetXOffset()+aHeaderBar
->GetDragPos();
361 aSizeRect
.Right()=-GetXOffset()+aHeaderBar
->GetDragPos();
362 ShowTracking( aSizeRect
, SHOWTRACK_SPLIT
);
365 void SvSimpleTable::HBarEndDrag()
368 sal_uInt16 nPrivTabCount
=TabCount();
372 if(nPrivTabCount
>aHeaderBar
->GetItemCount())
373 nPrivTabCount
=aHeaderBar
->GetItemCount();
376 sal_uInt16 nNewSize
=0;
377 for(sal_uInt16 i
=1;i
<nPrivTabCount
;i
++)
379 nNewSize
= static_cast< sal_uInt16
>( aHeaderBar
->GetItemSize(i
) ) + nPos
;
380 SetTab( i
, nNewSize
, MAP_PIXEL
);
390 void SvSimpleTable::Command( const CommandEvent
& rCEvt
)
393 aCommandLink
.Call(this);
394 SvHeaderTabListBox::Command(rCEvt
);
397 IMPL_LINK( SvSimpleTable
, StartDragHdl
, HeaderBar
*, pCtr
)
399 if(pCtr
==aHeaderBar
.get())
406 IMPL_LINK( SvSimpleTable
, DragHdl
, HeaderBar
*, pCtr
)
408 if(pCtr
==aHeaderBar
.get())
415 IMPL_LINK( SvSimpleTable
, EndDragHdl
, HeaderBar
*, pCtr
)
417 if(pCtr
==aHeaderBar
.get())
424 IMPL_LINK( SvSimpleTable
, HeaderBarClick
, HeaderBar
*, pCtr
)
426 if(pCtr
==aHeaderBar
.get())
433 IMPL_LINK( SvSimpleTable
, HeaderBarDblClick
, HeaderBar
*, pCtr
)
435 if(pCtr
==aHeaderBar
.get())
442 SvLBoxItem
* SvSimpleTable::GetEntryAtPos( SvTreeListEntry
* pEntry
, sal_uInt16 nPos
) const
444 DBG_ASSERT(pEntry
,"GetEntryText:Invalid Entry");
445 SvLBoxItem
* pItem
= NULL
;
449 sal_uInt16 nCount
= pEntry
->ItemCount();
453 if( nTreeFlags
& SvTreeFlags::CHKBTN
) nPos
++;
457 pItem
= pEntry
->GetItem( nPos
);
463 sal_Int32
SvSimpleTable::ColCompare(SvTreeListEntry
* pLeft
,SvTreeListEntry
* pRight
)
465 sal_Int32 nCompare
= 0;
467 SvLBoxItem
* pLeftItem
= GetEntryAtPos( pLeft
, nSortCol
);
468 SvLBoxItem
* pRightItem
= GetEntryAtPos( pRight
, nSortCol
);
471 if(pLeftItem
!= NULL
&& pRightItem
!= NULL
)
473 sal_uInt16 nLeftKind
= pLeftItem
->GetType();
474 sal_uInt16 nRightKind
= pRightItem
->GetType();
476 if(nRightKind
== SV_ITEM_ID_LBOXSTRING
&&
477 nLeftKind
== SV_ITEM_ID_LBOXSTRING
)
478 nCompare
= aCollator
.compareString( static_cast<SvLBoxString
*>(pLeftItem
)->GetText(),
479 static_cast<SvLBoxString
*>(pRightItem
)->GetText());
484 IMPL_LINK( SvSimpleTable
, CompareHdl
, SvSortData
*, pData
)
486 SvTreeListEntry
* pLeft
= const_cast<SvTreeListEntry
*>(pData
->pLeft
);
487 SvTreeListEntry
* pRight
= const_cast<SvTreeListEntry
*>(pData
->pRight
);
488 return (long) ColCompare(pLeft
,pRight
);
491 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */