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 <unotools/intlwrapper.hxx>
25 #include <vcl/builder.hxx>
26 #include <vcl/svapp.hxx>
28 // SvSimpleTableContainer ------------------------------------------------------
30 SvSimpleTableContainer::SvSimpleTableContainer(Window
* pParent
, const ResId
& rResId
)
31 : Control(pParent
, rResId
)
34 SetBorderStyle(WINDOW_BORDER_NOBORDER
);
37 SvSimpleTableContainer::SvSimpleTableContainer(Window
* pParent
, WinBits nBits
)
38 : Control(pParent
, nBits
)
43 extern "C" SAL_DLLPUBLIC_EXPORT Window
* SAL_CALL
makeSvSimpleTableContainer(Window
*pParent
,
44 VclBuilder::stringmap
&)
46 return new SvSimpleTableContainer(pParent
, WB_TABSTOP
| WB_DIALOGCONTROL
| WB_BORDER
);
49 void SvSimpleTableContainer::SetTable(SvSimpleTable
* pTable
)
54 long SvSimpleTableContainer::PreNotify( NotifyEvent
& rNEvt
)
56 long nResult
= sal_True
;
57 if ( rNEvt
.GetType() == EVENT_KEYINPUT
)
59 const KeyCode
& aKeyCode
= rNEvt
.GetKeyEvent()->GetKeyCode();
60 sal_uInt16 nKey
= aKeyCode
.GetCode();
62 GetParent()->Notify( rNEvt
);
63 else if (m_pTable
&& m_pTable
->IsFocusOnCellEnabled() && ( nKey
== KEY_LEFT
|| nKey
== KEY_RIGHT
))
66 nResult
= Control::PreNotify( rNEvt
);
69 nResult
= Control::PreNotify( rNEvt
);
74 void SvSimpleTableContainer::Resize()
78 m_pTable
->UpdateViewSize();
81 void SvSimpleTableContainer::GetFocus()
85 m_pTable
->GrabFocus();
88 // SvSimpleTable ------------------------------------------------------------
90 SvSimpleTable::SvSimpleTable(SvSimpleTableContainer
& rParent
, WinBits nBits
):
91 SvHeaderTabListBox(&rParent
, nBits
| WB_CLIPCHILDREN
| WB_HSCROLL
| WB_TABSTOP
),
92 m_rParentTableContainer(rParent
),
93 aHeaderBar(&rParent
,WB_BUTTONSTYLE
| WB_BORDER
| WB_TABSTOP
),
95 bResizeFlag(sal_False
),
98 m_rParentTableContainer
.SetTable(this);
100 bSortDirection
=sal_True
;
104 aHeaderBar
.SetStartDragHdl(LINK( this, SvSimpleTable
, StartDragHdl
));
105 aHeaderBar
.SetDragHdl(LINK( this, SvSimpleTable
, DragHdl
));
106 aHeaderBar
.SetEndDragHdl(LINK( this, SvSimpleTable
, EndDragHdl
));
107 aHeaderBar
.SetSelectHdl(LINK( this, SvSimpleTable
, HeaderBarClick
));
108 aHeaderBar
.SetDoubleClickHdl(LINK( this, SvSimpleTable
, HeaderBarDblClick
));
111 DisableTransientChildren();
112 InitHeaderBar( &aHeaderBar
);
117 SvHeaderTabListBox::Show();
120 SvSimpleTable::~SvSimpleTable()
122 m_rParentTableContainer
.SetTable(NULL
);
125 void SvSimpleTable::UpdateViewSize()
127 Size theWinSize
=m_rParentTableContainer
.GetOutputSizePixel();
128 Size HbSize
=aHeaderBar
.GetSizePixel();
130 HbSize
.Width()=theWinSize
.Width();
131 theWinSize
.Height()-=HbSize
.Height();
134 aHeaderBar
.SetPosPixel(thePos
);
135 aHeaderBar
.SetSizePixel(HbSize
);
137 thePos
.Y()+=HbSize
.Height();
138 SvHeaderTabListBox::SetPosPixel(thePos
);
139 SvHeaderTabListBox::SetSizePixel(theWinSize
);
143 void SvSimpleTable::NotifyScrolled()
145 long nOffset
=-GetXOffset();
148 aHeaderBar
.SetOffset(nOffset
);
149 aHeaderBar
.Invalidate();
153 SvHeaderTabListBox::NotifyScrolled();
156 void SvSimpleTable::SetTabs()
158 SvHeaderTabListBox::SetTabs();
160 sal_uInt16 nPrivTabCount
= TabCount();
163 if ( nPrivTabCount
> aHeaderBar
.GetItemCount() )
164 nPrivTabCount
= aHeaderBar
.GetItemCount();
166 sal_uInt16 i
, nNewSize
= static_cast< sal_uInt16
>( GetTab(0) ), nPos
= 0;
167 for ( i
= 1; i
< nPrivTabCount
; ++i
)
169 nNewSize
= static_cast< sal_uInt16
>( GetTab(i
) ) - nPos
;
170 aHeaderBar
.SetItemSize( i
, nNewSize
);
171 nPos
= (sal_uInt16
)GetTab(i
);
174 aHeaderBar
.SetItemSize( i
, HEADERBAR_FULLSIZE
); // because no tab for last entry
178 void SvSimpleTable::SetTabs( long* pTabs
, MapUnit eMapUnit
)
180 SvHeaderTabListBox::SetTabs(pTabs
,eMapUnit
);
183 void SvSimpleTable::Paint( const Rectangle
& rRect
)
185 SvHeaderTabListBox::Paint(rRect
);
187 sal_uInt16 nPrivTabCount
= TabCount();
188 sal_uInt16 nNewSize
= ( nPrivTabCount
> 0 ) ? (sal_uInt16
)GetTab(0) : 0;
190 long nOffset
=-GetXOffset();
193 aHeaderBar
.SetOffset(nOffset
);
194 aHeaderBar
.Invalidate();
196 if(nPrivTabCount
&& bPaintFlag
)
198 if(nPrivTabCount
>aHeaderBar
.GetItemCount())
199 nPrivTabCount
=aHeaderBar
.GetItemCount();
202 for(sal_uInt16 i
=1;i
<nPrivTabCount
;i
++)
204 nNewSize
= static_cast< sal_uInt16
>( GetTab(i
) ) - nPos
;
205 aHeaderBar
.SetItemSize( i
, nNewSize
);
206 nPos
= static_cast< sal_uInt16
>( GetTab(i
) );
211 void SvSimpleTable::InsertHeaderEntry(const OUString
& rText
,
212 sal_uInt16 nCol
, HeaderBarItemBits nBits
)
214 sal_Int32 nEnd
= rText
.indexOf( '\t' );
217 aHeaderBar
.InsertItem(nHeaderItemId
++, rText
, 0, nBits
, nCol
);
221 sal_Int32 nIndex
= 0;
224 OUString aString
= rText
.getToken(0, '\t', nIndex
);
225 aHeaderBar
.InsertItem(nHeaderItemId
++, aString
, 0, nBits
, nCol
);
227 while ( nIndex
>= 0 );
232 void SvSimpleTable::ClearHeader()
237 void SvSimpleTable::ShowTable()
239 m_rParentTableContainer
.Show();
242 void SvSimpleTable::HideTable()
244 m_rParentTableContainer
.Hide();
247 sal_Bool
SvSimpleTable::IsVisible() const
249 return m_rParentTableContainer
.IsVisible();
252 void SvSimpleTable::EnableTable()
254 m_rParentTableContainer
.Enable();
257 void SvSimpleTable::DisableTable()
259 m_rParentTableContainer
.Disable();
262 sal_Bool
SvSimpleTable::IsEnabled() const
264 return m_rParentTableContainer
.IsEnabled();
267 sal_uInt16
SvSimpleTable::GetSelectedCol()
269 return (aHeaderBar
.GetCurItemId()-1);
272 void SvSimpleTable::SortByCol(sal_uInt16 nCol
,sal_Bool bDir
)
276 aHeaderBar
.SetItemBits(nSortCol
+1,HIB_STDSTYLE
);
282 aHeaderBar
.SetItemBits( nCol
+1, HIB_STDSTYLE
| HIB_DOWNARROW
);
283 GetModel()->SetSortMode(SortAscending
);
287 aHeaderBar
.SetItemBits( nCol
+1, HIB_STDSTYLE
| HIB_UPARROW
);
288 GetModel()->SetSortMode(SortDescending
);
291 GetModel()->SetCompareHdl( LINK( this, SvSimpleTable
, CompareHdl
));
292 GetModel()->Resort();
295 GetModel()->SetSortMode(SortNone
);
299 void SvSimpleTable::HBarClick()
301 sal_uInt16 nId
=aHeaderBar
.GetCurItemId();
303 if (aHeaderBar
.GetItemBits(nId
) & HIB_CLICKABLE
)
307 SortByCol(nId
-1,!bSortDirection
);
311 SortByCol(nId
-1,bSortDirection
);
314 aHeaderBarClickLink
.Call(this);
318 void SvSimpleTable::HBarDblClick()
320 aHeaderBarDblClickLink
.Call(this);
323 void SvSimpleTable::HBarStartDrag()
325 if(!aHeaderBar
.IsItemMode())
327 Rectangle
aSizeRect(Point(0,0),
328 SvHeaderTabListBox::GetOutputSizePixel());
329 aSizeRect
.Left()=-GetXOffset()+aHeaderBar
.GetDragPos();
330 aSizeRect
.Right()=-GetXOffset()+aHeaderBar
.GetDragPos();
331 ShowTracking( aSizeRect
, SHOWTRACK_SPLIT
);
334 void SvSimpleTable::HBarDrag()
337 if(!aHeaderBar
.IsItemMode())
339 Rectangle
aSizeRect(Point(0,0),
340 SvHeaderTabListBox::GetOutputSizePixel());
341 aSizeRect
.Left()=-GetXOffset()+aHeaderBar
.GetDragPos();
342 aSizeRect
.Right()=-GetXOffset()+aHeaderBar
.GetDragPos();
343 ShowTracking( aSizeRect
, SHOWTRACK_SPLIT
);
346 void SvSimpleTable::HBarEndDrag()
349 sal_uInt16 nPrivTabCount
=TabCount();
353 if(nPrivTabCount
>aHeaderBar
.GetItemCount())
354 nPrivTabCount
=aHeaderBar
.GetItemCount();
357 sal_uInt16 nNewSize
=0;
358 for(sal_uInt16 i
=1;i
<nPrivTabCount
;i
++)
360 nNewSize
= static_cast< sal_uInt16
>( aHeaderBar
.GetItemSize(i
) ) + nPos
;
361 SetTab( i
, nNewSize
, MAP_PIXEL
);
365 bPaintFlag
=sal_False
;
370 CommandEvent
SvSimpleTable::GetCommandEvent() const
375 void SvSimpleTable::Command( const CommandEvent
& rCEvt
)
378 aCommandLink
.Call(this);
379 SvHeaderTabListBox::Command(rCEvt
);
382 IMPL_LINK( SvSimpleTable
, StartDragHdl
, HeaderBar
*, pCtr
)
384 if(pCtr
==&aHeaderBar
)
391 IMPL_LINK( SvSimpleTable
, DragHdl
, HeaderBar
*, pCtr
)
393 if(pCtr
==&aHeaderBar
)
400 IMPL_LINK( SvSimpleTable
, EndDragHdl
, HeaderBar
*, pCtr
)
402 if(pCtr
==&aHeaderBar
)
409 IMPL_LINK( SvSimpleTable
, HeaderBarClick
, HeaderBar
*, pCtr
)
411 if(pCtr
==&aHeaderBar
)
418 IMPL_LINK( SvSimpleTable
, HeaderBarDblClick
, HeaderBar
*, pCtr
)
420 if(pCtr
==&aHeaderBar
)
427 SvLBoxItem
* SvSimpleTable::GetEntryAtPos( SvTreeListEntry
* pEntry
, sal_uInt16 nPos
) const
429 DBG_ASSERT(pEntry
,"GetEntryText:Invalid Entry");
430 SvLBoxItem
* pItem
= NULL
;
434 sal_uInt16 nCount
= pEntry
->ItemCount();
438 if( nTreeFlags
& TREEFLAG_CHKBTN
) nPos
++;
442 pItem
= pEntry
->GetItem( nPos
);
448 sal_Int32
SvSimpleTable::ColCompare(SvTreeListEntry
* pLeft
,SvTreeListEntry
* pRight
)
450 sal_Int32 nCompare
= 0;
452 SvLBoxItem
* pLeftItem
= GetEntryAtPos( pLeft
, nSortCol
);
453 SvLBoxItem
* pRightItem
= GetEntryAtPos( pRight
, nSortCol
);
456 if(pLeftItem
!= NULL
&& pRightItem
!= NULL
)
458 sal_uInt16 nLeftKind
= pLeftItem
->GetType();
459 sal_uInt16 nRightKind
= pRightItem
->GetType();
461 if(nRightKind
== SV_ITEM_ID_LBOXSTRING
&&
462 nLeftKind
== SV_ITEM_ID_LBOXSTRING
)
464 IntlWrapper
aIntlWrapper( Application::GetSettings().GetLanguageTag() );
465 const CollatorWrapper
* pCollator
= aIntlWrapper
.getCaseCollator();
467 nCompare
= pCollator
->compareString( ((SvLBoxString
*)pLeftItem
)->GetText(),
468 ((SvLBoxString
*)pRightItem
)->GetText());
477 IMPL_LINK( SvSimpleTable
, CompareHdl
, SvSortData
*, pData
)
479 SvTreeListEntry
* pLeft
= (SvTreeListEntry
*)(pData
->pLeft
);
480 SvTreeListEntry
* pRight
= (SvTreeListEntry
*)(pData
->pRight
);
481 return (long) ColCompare(pLeft
,pRight
);
484 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */