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 <svx/simptabl.hxx>
21 #include <vcl/builder.hxx>
22 #include <vcl/svapp.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <unotools/intlwrapper.hxx>
26 #include <svtools/svlbitm.hxx>
27 #include "svtools/treelistentry.hxx"
29 // SvxSimpleTableContainer ------------------------------------------------------
31 SvxSimpleTableContainer::SvxSimpleTableContainer(Window
* pParent
, const ResId
& rResId
)
32 : Control(pParent
, rResId
)
35 SetBorderStyle(WINDOW_BORDER_NOBORDER
);
38 SvxSimpleTableContainer::SvxSimpleTableContainer(Window
* pParent
, WinBits nBits
)
39 : Control(pParent
, nBits
)
44 extern "C" SAL_DLLPUBLIC_EXPORT Window
* SAL_CALL
makeSvxSimpleTableContainer(Window
*pParent
,
45 VclBuilder::stringmap
&)
47 return new SvxSimpleTableContainer(pParent
, WB_TABSTOP
| WB_DIALOGCONTROL
| WB_BORDER
);
50 void SvxSimpleTableContainer::SetTable(SvxSimpleTable
* pTable
)
55 long SvxSimpleTableContainer::PreNotify( NotifyEvent
& rNEvt
)
57 long nResult
= sal_True
;
58 if ( rNEvt
.GetType() == EVENT_KEYINPUT
)
60 const KeyCode
& aKeyCode
= rNEvt
.GetKeyEvent()->GetKeyCode();
61 sal_uInt16 nKey
= aKeyCode
.GetCode();
63 GetParent()->Notify( rNEvt
);
64 else if (m_pTable
&& m_pTable
->IsFocusOnCellEnabled() && ( nKey
== KEY_LEFT
|| nKey
== KEY_RIGHT
))
67 nResult
= Control::PreNotify( rNEvt
);
70 nResult
= Control::PreNotify( rNEvt
);
75 void SvxSimpleTableContainer::Resize()
79 m_pTable
->UpdateViewSize();
82 void SvxSimpleTableContainer::GetFocus()
86 m_pTable
->GrabFocus();
89 // SvxSimpleTable ------------------------------------------------------------
91 SvxSimpleTable::SvxSimpleTable(SvxSimpleTableContainer
& rParent
, WinBits nBits
):
92 SvHeaderTabListBox(&rParent
, nBits
| WB_CLIPCHILDREN
| WB_HSCROLL
| WB_TABSTOP
),
93 m_rParentTableContainer(rParent
),
94 aHeaderBar(&rParent
,WB_BUTTONSTYLE
| WB_BORDER
| WB_TABSTOP
),
96 bResizeFlag(sal_False
),
99 m_rParentTableContainer
.SetTable(this);
101 bSortDirection
=sal_True
;
105 aHeaderBar
.SetStartDragHdl(LINK( this, SvxSimpleTable
, StartDragHdl
));
106 aHeaderBar
.SetDragHdl(LINK( this, SvxSimpleTable
, DragHdl
));
107 aHeaderBar
.SetEndDragHdl(LINK( this, SvxSimpleTable
, EndDragHdl
));
108 aHeaderBar
.SetSelectHdl(LINK( this, SvxSimpleTable
, HeaderBarClick
));
109 aHeaderBar
.SetDoubleClickHdl(LINK( this, SvxSimpleTable
, HeaderBarDblClick
));
112 DisableTransientChildren();
113 InitHeaderBar( &aHeaderBar
);
118 SvHeaderTabListBox::Show();
121 SvxSimpleTable::~SvxSimpleTable()
123 m_rParentTableContainer
.SetTable(NULL
);
126 void SvxSimpleTable::UpdateViewSize()
128 Size theWinSize
=m_rParentTableContainer
.GetOutputSizePixel();
129 Size HbSize
=aHeaderBar
.GetSizePixel();
131 HbSize
.Width()=theWinSize
.Width();
132 theWinSize
.Height()-=HbSize
.Height();
135 aHeaderBar
.SetPosPixel(thePos
);
136 aHeaderBar
.SetSizePixel(HbSize
);
138 thePos
.Y()+=HbSize
.Height();
139 SvHeaderTabListBox::SetPosPixel(thePos
);
140 SvHeaderTabListBox::SetSizePixel(theWinSize
);
144 void SvxSimpleTable::NotifyScrolled()
146 long nOffset
=-GetXOffset();
149 aHeaderBar
.SetOffset(nOffset
);
150 aHeaderBar
.Invalidate();
154 SvHeaderTabListBox::NotifyScrolled();
157 void SvxSimpleTable::SetTabs()
159 SvHeaderTabListBox::SetTabs();
161 sal_uInt16 nPrivTabCount
= TabCount();
164 if ( nPrivTabCount
> aHeaderBar
.GetItemCount() )
165 nPrivTabCount
= aHeaderBar
.GetItemCount();
167 sal_uInt16 i
, nNewSize
= static_cast< sal_uInt16
>( GetTab(0) ), nPos
= 0;
168 for ( i
= 1; i
< nPrivTabCount
; ++i
)
170 nNewSize
= static_cast< sal_uInt16
>( GetTab(i
) ) - nPos
;
171 aHeaderBar
.SetItemSize( i
, nNewSize
);
172 nPos
= (sal_uInt16
)GetTab(i
);
175 aHeaderBar
.SetItemSize( i
, HEADERBAR_FULLSIZE
); // because no tab for last entry
179 void SvxSimpleTable::SetTabs( long* pTabs
, MapUnit eMapUnit
)
181 SvHeaderTabListBox::SetTabs(pTabs
,eMapUnit
);
184 void SvxSimpleTable::Paint( const Rectangle
& rRect
)
186 SvHeaderTabListBox::Paint(rRect
);
188 sal_uInt16 nPrivTabCount
= TabCount();
189 sal_uInt16 nNewSize
= ( nPrivTabCount
> 0 ) ? (sal_uInt16
)GetTab(0) : 0;
191 long nOffset
=-GetXOffset();
194 aHeaderBar
.SetOffset(nOffset
);
195 aHeaderBar
.Invalidate();
197 if(nPrivTabCount
&& bPaintFlag
)
199 if(nPrivTabCount
>aHeaderBar
.GetItemCount())
200 nPrivTabCount
=aHeaderBar
.GetItemCount();
203 for(sal_uInt16 i
=1;i
<nPrivTabCount
;i
++)
205 nNewSize
= static_cast< sal_uInt16
>( GetTab(i
) ) - nPos
;
206 aHeaderBar
.SetItemSize( i
, nNewSize
);
207 nPos
= static_cast< sal_uInt16
>( GetTab(i
) );
212 void SvxSimpleTable::InsertHeaderEntry(const OUString
& rText
,
213 sal_uInt16 nCol
, HeaderBarItemBits nBits
)
215 sal_Int32 nEnd
= rText
.indexOf( sal_Unicode( '\t' ) );
218 aHeaderBar
.InsertItem(nHeaderItemId
++, rText
, 0, nBits
, nCol
);
222 sal_Int32 nIndex
= 0;
225 OUString aString
= rText
.getToken(0, '\t', nIndex
);
226 aHeaderBar
.InsertItem(nHeaderItemId
++, aString
, 0, nBits
, nCol
);
228 while ( nIndex
>= 0 );
233 void SvxSimpleTable::ClearHeader()
238 void SvxSimpleTable::ShowTable()
240 m_rParentTableContainer
.Show();
243 void SvxSimpleTable::HideTable()
245 m_rParentTableContainer
.Hide();
248 sal_Bool
SvxSimpleTable::IsVisible() const
250 return m_rParentTableContainer
.IsVisible();
253 void SvxSimpleTable::EnableTable()
255 m_rParentTableContainer
.Enable();
258 void SvxSimpleTable::DisableTable()
260 m_rParentTableContainer
.Disable();
263 sal_Bool
SvxSimpleTable::IsEnabled() const
265 return m_rParentTableContainer
.IsEnabled();
268 sal_uInt16
SvxSimpleTable::GetSelectedCol()
270 return (aHeaderBar
.GetCurItemId()-1);
273 void SvxSimpleTable::SortByCol(sal_uInt16 nCol
,sal_Bool bDir
)
277 aHeaderBar
.SetItemBits(nSortCol
+1,HIB_STDSTYLE
);
283 aHeaderBar
.SetItemBits( nCol
+1, HIB_STDSTYLE
| HIB_DOWNARROW
);
284 GetModel()->SetSortMode(SortAscending
);
288 aHeaderBar
.SetItemBits( nCol
+1, HIB_STDSTYLE
| HIB_UPARROW
);
289 GetModel()->SetSortMode(SortDescending
);
292 GetModel()->SetCompareHdl( LINK( this, SvxSimpleTable
, CompareHdl
));
293 GetModel()->Resort();
296 GetModel()->SetSortMode(SortNone
);
300 void SvxSimpleTable::HBarClick()
302 sal_uInt16 nId
=aHeaderBar
.GetCurItemId();
304 if (aHeaderBar
.GetItemBits(nId
) & HIB_CLICKABLE
)
308 SortByCol(nId
-1,!bSortDirection
);
312 SortByCol(nId
-1,bSortDirection
);
315 aHeaderBarClickLink
.Call(this);
319 void SvxSimpleTable::HBarDblClick()
321 aHeaderBarDblClickLink
.Call(this);
324 void SvxSimpleTable::HBarStartDrag()
326 if(!aHeaderBar
.IsItemMode())
328 Rectangle
aSizeRect(Point(0,0),
329 SvHeaderTabListBox::GetOutputSizePixel());
330 aSizeRect
.Left()=-GetXOffset()+aHeaderBar
.GetDragPos();
331 aSizeRect
.Right()=-GetXOffset()+aHeaderBar
.GetDragPos();
332 ShowTracking( aSizeRect
, SHOWTRACK_SPLIT
);
335 void SvxSimpleTable::HBarDrag()
338 if(!aHeaderBar
.IsItemMode())
340 Rectangle
aSizeRect(Point(0,0),
341 SvHeaderTabListBox::GetOutputSizePixel());
342 aSizeRect
.Left()=-GetXOffset()+aHeaderBar
.GetDragPos();
343 aSizeRect
.Right()=-GetXOffset()+aHeaderBar
.GetDragPos();
344 ShowTracking( aSizeRect
, SHOWTRACK_SPLIT
);
347 void SvxSimpleTable::HBarEndDrag()
350 sal_uInt16 nPrivTabCount
=TabCount();
354 if(nPrivTabCount
>aHeaderBar
.GetItemCount())
355 nPrivTabCount
=aHeaderBar
.GetItemCount();
358 sal_uInt16 nNewSize
=0;
359 for(sal_uInt16 i
=1;i
<nPrivTabCount
;i
++)
361 nNewSize
= static_cast< sal_uInt16
>( aHeaderBar
.GetItemSize(i
) ) + nPos
;
362 SetTab( i
, nNewSize
, MAP_PIXEL
);
366 bPaintFlag
=sal_False
;
371 CommandEvent
SvxSimpleTable::GetCommandEvent() const
376 void SvxSimpleTable::Command( const CommandEvent
& rCEvt
)
379 aCommandLink
.Call(this);
380 SvHeaderTabListBox::Command(rCEvt
);
383 IMPL_LINK( SvxSimpleTable
, StartDragHdl
, HeaderBar
*, pCtr
)
385 if(pCtr
==&aHeaderBar
)
392 IMPL_LINK( SvxSimpleTable
, DragHdl
, HeaderBar
*, pCtr
)
394 if(pCtr
==&aHeaderBar
)
401 IMPL_LINK( SvxSimpleTable
, EndDragHdl
, HeaderBar
*, pCtr
)
403 if(pCtr
==&aHeaderBar
)
410 IMPL_LINK( SvxSimpleTable
, HeaderBarClick
, HeaderBar
*, pCtr
)
412 if(pCtr
==&aHeaderBar
)
419 IMPL_LINK( SvxSimpleTable
, HeaderBarDblClick
, HeaderBar
*, pCtr
)
421 if(pCtr
==&aHeaderBar
)
428 SvLBoxItem
* SvxSimpleTable::GetEntryAtPos( SvTreeListEntry
* pEntry
, sal_uInt16 nPos
) const
430 DBG_ASSERT(pEntry
,"GetEntryText:Invalid Entry");
431 SvLBoxItem
* pItem
= NULL
;
435 sal_uInt16 nCount
= pEntry
->ItemCount();
439 if( nTreeFlags
& TREEFLAG_CHKBTN
) nPos
++;
443 pItem
= pEntry
->GetItem( nPos
);
449 StringCompare
SvxSimpleTable::ColCompare(SvTreeListEntry
* pLeft
,SvTreeListEntry
* pRight
)
451 StringCompare eCompare
=COMPARE_EQUAL
;
453 SvLBoxItem
* pLeftItem
= GetEntryAtPos( pLeft
, nSortCol
);
454 SvLBoxItem
* pRightItem
= GetEntryAtPos( pRight
, nSortCol
);
457 if(pLeftItem
!= NULL
&& pRightItem
!= NULL
)
459 sal_uInt16 nLeftKind
= pLeftItem
->GetType();
460 sal_uInt16 nRightKind
= pRightItem
->GetType();
462 if(nRightKind
== SV_ITEM_ID_LBOXSTRING
&&
463 nLeftKind
== SV_ITEM_ID_LBOXSTRING
)
465 IntlWrapper
aIntlWrapper( Application::GetSettings().GetLanguageTag() );
466 const CollatorWrapper
* pCollator
= aIntlWrapper
.getCaseCollator();
468 eCompare
=(StringCompare
)pCollator
->compareString( ((SvLBoxString
*)pLeftItem
)->GetText(),
469 ((SvLBoxString
*)pRightItem
)->GetText());
471 if(eCompare
==COMPARE_EQUAL
) eCompare
=COMPARE_LESS
;
477 IMPL_LINK( SvxSimpleTable
, CompareHdl
, SvSortData
*, pData
)
479 SvTreeListEntry
* pLeft
= (SvTreeListEntry
*)(pData
->pLeft
);
480 SvTreeListEntry
* pRight
= (SvTreeListEntry
*)(pData
->pRight
);
481 return (long) ColCompare(pLeft
,pRight
);
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */