Bump version to 5.0-14
[LibreOffice.git] / svtools / source / contnr / simptabl.cxx
blobbdbac957da3e6a6d80638d22dda93d220b50a7c5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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)
30 , m_pTable(NULL)
34 SvSimpleTableContainer::~SvSimpleTableContainer()
36 disposeOnce();
39 void SvSimpleTableContainer::dispose()
41 m_pTable.clear();
42 Control::dispose();
45 VCL_BUILDER_FACTORY_ARGS(SvSimpleTableContainer,
46 WB_TABSTOP | WB_DIALOGCONTROL | WB_BORDER)
48 void SvSimpleTableContainer::SetTable(SvSimpleTable* pTable)
50 m_pTable = pTable;
53 bool SvSimpleTableContainer::PreNotify( NotifyEvent& rNEvt )
55 bool nResult = true;
56 if ( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
58 const vcl::KeyCode& aKeyCode = rNEvt.GetKeyEvent()->GetKeyCode();
59 sal_uInt16 nKey = aKeyCode.GetCode();
60 if (nKey == KEY_TAB)
61 GetParent()->Notify( rNEvt );
62 else if (m_pTable && m_pTable->IsFocusOnCellEnabled() && ( nKey == KEY_LEFT || nKey == KEY_RIGHT))
63 return false;
64 else
65 nResult = Control::PreNotify( rNEvt );
67 else
68 nResult = Control::PreNotify( rNEvt );
70 return nResult;
73 void SvSimpleTableContainer::Resize()
75 Control::Resize();
76 if (m_pTable)
77 m_pTable->UpdateViewSize();
80 void SvSimpleTableContainer::GetFocus()
82 Control::GetFocus();
83 if (m_pTable)
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)),
93 nHeaderItemId(1),
94 bPaintFlag(true),
95 aCollator(*(IntlWrapper( Application::GetSettings().GetLanguageTag() ).getCaseCollator()))
97 m_rParentTableContainer.SetTable(this);
99 bSortDirection = true;
100 nSortCol = 0xFFFF;
101 nOldPos = 0;
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));
110 EnableCellFocus();
111 DisableTransientChildren();
112 InitHeaderBar( aHeaderBar );
114 UpdateViewSize();
116 aHeaderBar->Show();
117 SvHeaderTabListBox::Show();
120 SvSimpleTable::~SvSimpleTable()
122 disposeOnce();
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();
139 Point thePos(0,0);
141 aHeaderBar->SetPosPixel(thePos);
142 aHeaderBar->SetSizePixel(HbSize);
144 thePos.Y()+=HbSize.Height();
145 SvHeaderTabListBox::SetPosPixel(thePos);
146 SvHeaderTabListBox::SetSizePixel(theWinSize);
147 Invalidate();
150 void SvSimpleTable::NotifyScrolled()
152 long nOffset=-GetXOffset();
153 if(nOldPos!=nOffset)
155 aHeaderBar->SetOffset(nOffset);
156 aHeaderBar->Invalidate();
157 aHeaderBar->Update();
158 nOldPos=nOffset;
160 SvHeaderTabListBox::NotifyScrolled();
163 void SvSimpleTable::SetTabs()
165 SvHeaderTabListBox::SetTabs();
167 sal_uInt16 nPrivTabCount = TabCount();
168 if ( nPrivTabCount )
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();
197 nOldPos = nOffset;
199 aHeaderBar->SetOffset(nOffset);
200 aHeaderBar->Invalidate();
202 if (nPrivTabCount && bPaintFlag)
204 if (nPrivTabCount>aHeaderBar->GetItemCount())
205 nPrivTabCount=aHeaderBar->GetItemCount();
207 sal_uInt16 nPos = 0;
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));
215 bPaintFlag = true;
218 void SvSimpleTable::InsertHeaderEntry(const OUString& rText,
219 sal_uInt16 nCol, HeaderBarItemBits nBits)
221 sal_Int32 nEnd = rText.indexOf( '\t' );
222 if( nEnd == -1 )
224 aHeaderBar->InsertItem(nHeaderItemId++, rText, 0, nBits, nCol);
226 else
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 );
236 SetTabs();
239 void SvSimpleTable::ClearHeader()
241 aHeaderBar->Clear();
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)
281 if(nSortCol!=0xFFFF)
282 aHeaderBar->SetItemBits(nSortCol+1,HeaderBarItemBits::STDSTYLE);
284 if (nCol != 0xFFFF)
286 if(bDir || nSortCol != nCol)
288 aHeaderBar->SetItemBits( nCol+1, HeaderBarItemBits::STDSTYLE | HeaderBarItemBits::DOWNARROW);
289 GetModel()->SetSortMode(SortAscending);
290 bDir = true;
292 else
294 aHeaderBar->SetItemBits( nCol+1, HeaderBarItemBits::STDSTYLE | HeaderBarItemBits::UPARROW);
295 GetModel()->SetSortMode(SortDescending);
298 GetModel()->SetCompareHdl( LINK( this, SvSimpleTable, CompareHdl));
300 if(nSortCol == nCol)
302 GetModel()->Reverse();
303 Resize(); //update rows
305 else
307 nSortCol=nCol;
308 GetModel()->Resort();
311 else
312 GetModel()->SetSortMode(SortNone);
313 nSortCol=nCol;
314 bSortDirection=bDir;
315 SetAlternatingRowColors( true );
318 void SvSimpleTable::HBarClick()
320 sal_uInt16 nId=aHeaderBar->GetCurItemId();
322 if (aHeaderBar->GetItemBits(nId) & HeaderBarItemBits::CLICKABLE)
324 if(nId==nSortCol+1)
326 SortByCol(nId-1,!bSortDirection);
328 else
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()
355 HideTracking();
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()
367 HideTracking();
368 sal_uInt16 nPrivTabCount=TabCount();
370 if(nPrivTabCount)
372 if(nPrivTabCount>aHeaderBar->GetItemCount())
373 nPrivTabCount=aHeaderBar->GetItemCount();
375 sal_uInt16 nPos=0;
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 );
381 nPos = nNewSize;
384 bPaintFlag = false;
385 Invalidate();
386 Update();
390 void SvSimpleTable::Command( const CommandEvent& rCEvt )
392 aCEvt=rCEvt;
393 aCommandLink.Call(this);
394 SvHeaderTabListBox::Command(rCEvt);
397 IMPL_LINK( SvSimpleTable, StartDragHdl, HeaderBar*, pCtr)
399 if(pCtr==aHeaderBar.get())
401 HBarStartDrag();
403 return 0;
406 IMPL_LINK( SvSimpleTable, DragHdl, HeaderBar*, pCtr)
408 if(pCtr==aHeaderBar.get())
410 HBarDrag();
412 return 0;
415 IMPL_LINK( SvSimpleTable, EndDragHdl, HeaderBar*, pCtr)
417 if(pCtr==aHeaderBar.get())
419 HBarEndDrag();
421 return 0;
424 IMPL_LINK( SvSimpleTable, HeaderBarClick, HeaderBar*, pCtr)
426 if(pCtr==aHeaderBar.get())
428 HBarClick();
430 return 0;
433 IMPL_LINK( SvSimpleTable, HeaderBarDblClick, HeaderBar*, pCtr)
435 if(pCtr==aHeaderBar.get())
437 HBarDblClick();
439 return 0;
442 SvLBoxItem* SvSimpleTable::GetEntryAtPos( SvTreeListEntry* pEntry, sal_uInt16 nPos ) const
444 DBG_ASSERT(pEntry,"GetEntryText:Invalid Entry");
445 SvLBoxItem* pItem = NULL;
447 if( pEntry )
449 sal_uInt16 nCount = pEntry->ItemCount();
451 nPos++;
453 if( nTreeFlags & SvTreeFlags::CHKBTN ) nPos++;
455 if( nPos < nCount )
457 pItem = pEntry->GetItem( nPos);
460 return pItem;
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());
481 return nCompare;
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: */