GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / svtools / source / contnr / simptabl.cxx
blob6f25107d621dc35c959d36367f49e6a9f56ed61d
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 <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)
32 , m_pTable(NULL)
34 SetBorderStyle(WINDOW_BORDER_NOBORDER);
37 SvSimpleTableContainer::SvSimpleTableContainer(Window* pParent, WinBits nBits)
38 : Control(pParent, nBits)
39 , m_pTable(NULL)
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)
51 m_pTable = 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();
61 if (nKey == KEY_TAB)
62 GetParent()->Notify( rNEvt );
63 else if (m_pTable && m_pTable->IsFocusOnCellEnabled() && ( nKey == KEY_LEFT || nKey == KEY_RIGHT))
64 return 0;
65 else
66 nResult = Control::PreNotify( rNEvt );
68 else
69 nResult = Control::PreNotify( rNEvt );
71 return nResult;
74 void SvSimpleTableContainer::Resize()
76 Control::Resize();
77 if (m_pTable)
78 m_pTable->UpdateViewSize();
81 void SvSimpleTableContainer::GetFocus()
83 Control::GetFocus();
84 if (m_pTable)
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),
94 nHeaderItemId(1),
95 bResizeFlag(sal_False),
96 bPaintFlag(sal_True)
98 m_rParentTableContainer.SetTable(this);
100 bSortDirection=sal_True;
101 nSortCol=0xFFFF;
102 nOldPos=0;
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));
110 EnableCellFocus();
111 DisableTransientChildren();
112 InitHeaderBar( &aHeaderBar );
114 UpdateViewSize();
116 aHeaderBar.Show();
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();
132 Point thePos(0,0);
134 aHeaderBar.SetPosPixel(thePos);
135 aHeaderBar.SetSizePixel(HbSize);
137 thePos.Y()+=HbSize.Height();
138 SvHeaderTabListBox::SetPosPixel(thePos);
139 SvHeaderTabListBox::SetSizePixel(theWinSize);
140 Invalidate();
143 void SvSimpleTable::NotifyScrolled()
145 long nOffset=-GetXOffset();
146 if(nOldPos!=nOffset)
148 aHeaderBar.SetOffset(nOffset);
149 aHeaderBar.Invalidate();
150 aHeaderBar.Update();
151 nOldPos=nOffset;
153 SvHeaderTabListBox::NotifyScrolled();
156 void SvSimpleTable::SetTabs()
158 SvHeaderTabListBox::SetTabs();
160 sal_uInt16 nPrivTabCount = TabCount();
161 if ( nPrivTabCount )
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();
191 nOldPos=nOffset;
193 aHeaderBar.SetOffset(nOffset);
194 aHeaderBar.Invalidate();
196 if(nPrivTabCount && bPaintFlag)
198 if(nPrivTabCount>aHeaderBar.GetItemCount())
199 nPrivTabCount=aHeaderBar.GetItemCount();
201 sal_uInt16 nPos = 0;
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) );
209 bPaintFlag=sal_True;
211 void SvSimpleTable::InsertHeaderEntry(const OUString& rText,
212 sal_uInt16 nCol, HeaderBarItemBits nBits)
214 sal_Int32 nEnd = rText.indexOf( '\t' );
215 if( nEnd == -1 )
217 aHeaderBar.InsertItem(nHeaderItemId++, rText, 0, nBits, nCol);
219 else
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 );
229 SetTabs();
232 void SvSimpleTable::ClearHeader()
234 aHeaderBar.Clear();
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)
274 bSortDirection=bDir;
275 if(nSortCol!=0xFFFF)
276 aHeaderBar.SetItemBits(nSortCol+1,HIB_STDSTYLE);
278 if (nCol != 0xFFFF)
280 if(bDir)
282 aHeaderBar.SetItemBits( nCol+1, HIB_STDSTYLE | HIB_DOWNARROW);
283 GetModel()->SetSortMode(SortAscending);
285 else
287 aHeaderBar.SetItemBits( nCol+1, HIB_STDSTYLE | HIB_UPARROW);
288 GetModel()->SetSortMode(SortDescending);
290 nSortCol=nCol;
291 GetModel()->SetCompareHdl( LINK( this, SvSimpleTable, CompareHdl));
292 GetModel()->Resort();
294 else
295 GetModel()->SetSortMode(SortNone);
296 nSortCol=nCol;
299 void SvSimpleTable::HBarClick()
301 sal_uInt16 nId=aHeaderBar.GetCurItemId();
303 if (aHeaderBar.GetItemBits(nId) & HIB_CLICKABLE)
305 if(nId==nSortCol+1)
307 SortByCol(nId-1,!bSortDirection);
309 else
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()
336 HideTracking();
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()
348 HideTracking();
349 sal_uInt16 nPrivTabCount=TabCount();
351 if(nPrivTabCount)
353 if(nPrivTabCount>aHeaderBar.GetItemCount())
354 nPrivTabCount=aHeaderBar.GetItemCount();
356 sal_uInt16 nPos=0;
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 );
362 nPos = nNewSize;
365 bPaintFlag=sal_False;
366 Invalidate();
367 Update();
370 CommandEvent SvSimpleTable::GetCommandEvent() const
372 return aCEvt;
375 void SvSimpleTable::Command( const CommandEvent& rCEvt )
377 aCEvt=rCEvt;
378 aCommandLink.Call(this);
379 SvHeaderTabListBox::Command(rCEvt);
382 IMPL_LINK( SvSimpleTable, StartDragHdl, HeaderBar*, pCtr)
384 if(pCtr==&aHeaderBar)
386 HBarStartDrag();
388 return 0;
391 IMPL_LINK( SvSimpleTable, DragHdl, HeaderBar*, pCtr)
393 if(pCtr==&aHeaderBar)
395 HBarDrag();
397 return 0;
400 IMPL_LINK( SvSimpleTable, EndDragHdl, HeaderBar*, pCtr)
402 if(pCtr==&aHeaderBar)
404 HBarEndDrag();
406 return 0;
409 IMPL_LINK( SvSimpleTable, HeaderBarClick, HeaderBar*, pCtr)
411 if(pCtr==&aHeaderBar)
413 HBarClick();
415 return 0;
418 IMPL_LINK( SvSimpleTable, HeaderBarDblClick, HeaderBar*, pCtr)
420 if(pCtr==&aHeaderBar)
422 HBarDblClick();
424 return 0;
427 SvLBoxItem* SvSimpleTable::GetEntryAtPos( SvTreeListEntry* pEntry, sal_uInt16 nPos ) const
429 DBG_ASSERT(pEntry,"GetEntryText:Invalid Entry");
430 SvLBoxItem* pItem = NULL;
432 if( pEntry )
434 sal_uInt16 nCount = pEntry->ItemCount();
436 nPos++;
438 if( nTreeFlags & TREEFLAG_CHKBTN ) nPos++;
440 if( nPos < nCount )
442 pItem = pEntry->GetItem( nPos);
445 return pItem;
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());
470 if (nCompare == 0)
471 nCompare = -1;
474 return nCompare;
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: */