bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / dialog / simptabl.cxx
blobec54618a0b4a9f03c5256906dd84edf1244136a0
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 <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)
33 , m_pTable(NULL)
35 SetBorderStyle(WINDOW_BORDER_NOBORDER);
38 SvxSimpleTableContainer::SvxSimpleTableContainer(Window* pParent, WinBits nBits)
39 : Control(pParent, nBits)
40 , m_pTable(NULL)
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)
52 m_pTable = 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();
62 if (nKey == KEY_TAB)
63 GetParent()->Notify( rNEvt );
64 else if (m_pTable && m_pTable->IsFocusOnCellEnabled() && ( nKey == KEY_LEFT || nKey == KEY_RIGHT))
65 return 0;
66 else
67 nResult = Control::PreNotify( rNEvt );
69 else
70 nResult = Control::PreNotify( rNEvt );
72 return nResult;
75 void SvxSimpleTableContainer::Resize()
77 Control::Resize();
78 if (m_pTable)
79 m_pTable->UpdateViewSize();
82 void SvxSimpleTableContainer::GetFocus()
84 Control::GetFocus();
85 if (m_pTable)
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),
95 nHeaderItemId(1),
96 bResizeFlag(sal_False),
97 bPaintFlag(sal_True)
99 m_rParentTableContainer.SetTable(this);
101 bSortDirection=sal_True;
102 nSortCol=0xFFFF;
103 nOldPos=0;
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));
111 EnableCellFocus();
112 DisableTransientChildren();
113 InitHeaderBar( &aHeaderBar );
115 UpdateViewSize();
117 aHeaderBar.Show();
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();
133 Point thePos(0,0);
135 aHeaderBar.SetPosPixel(thePos);
136 aHeaderBar.SetSizePixel(HbSize);
138 thePos.Y()+=HbSize.Height();
139 SvHeaderTabListBox::SetPosPixel(thePos);
140 SvHeaderTabListBox::SetSizePixel(theWinSize);
141 Invalidate();
144 void SvxSimpleTable::NotifyScrolled()
146 long nOffset=-GetXOffset();
147 if(nOldPos!=nOffset)
149 aHeaderBar.SetOffset(nOffset);
150 aHeaderBar.Invalidate();
151 aHeaderBar.Update();
152 nOldPos=nOffset;
154 SvHeaderTabListBox::NotifyScrolled();
157 void SvxSimpleTable::SetTabs()
159 SvHeaderTabListBox::SetTabs();
161 sal_uInt16 nPrivTabCount = TabCount();
162 if ( nPrivTabCount )
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();
192 nOldPos=nOffset;
194 aHeaderBar.SetOffset(nOffset);
195 aHeaderBar.Invalidate();
197 if(nPrivTabCount && bPaintFlag)
199 if(nPrivTabCount>aHeaderBar.GetItemCount())
200 nPrivTabCount=aHeaderBar.GetItemCount();
202 sal_uInt16 nPos = 0;
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) );
210 bPaintFlag=sal_True;
212 void SvxSimpleTable::InsertHeaderEntry(const OUString& rText,
213 sal_uInt16 nCol, HeaderBarItemBits nBits)
215 sal_Int32 nEnd = rText.indexOf( sal_Unicode( '\t' ) );
216 if( nEnd == -1 )
218 aHeaderBar.InsertItem(nHeaderItemId++, rText, 0, nBits, nCol);
220 else
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 );
230 SetTabs();
233 void SvxSimpleTable::ClearHeader()
235 aHeaderBar.Clear();
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)
275 bSortDirection=bDir;
276 if(nSortCol!=0xFFFF)
277 aHeaderBar.SetItemBits(nSortCol+1,HIB_STDSTYLE);
279 if (nCol != 0xFFFF)
281 if(bDir)
283 aHeaderBar.SetItemBits( nCol+1, HIB_STDSTYLE | HIB_DOWNARROW);
284 GetModel()->SetSortMode(SortAscending);
286 else
288 aHeaderBar.SetItemBits( nCol+1, HIB_STDSTYLE | HIB_UPARROW);
289 GetModel()->SetSortMode(SortDescending);
291 nSortCol=nCol;
292 GetModel()->SetCompareHdl( LINK( this, SvxSimpleTable, CompareHdl));
293 GetModel()->Resort();
295 else
296 GetModel()->SetSortMode(SortNone);
297 nSortCol=nCol;
300 void SvxSimpleTable::HBarClick()
302 sal_uInt16 nId=aHeaderBar.GetCurItemId();
304 if (aHeaderBar.GetItemBits(nId) & HIB_CLICKABLE)
306 if(nId==nSortCol+1)
308 SortByCol(nId-1,!bSortDirection);
310 else
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()
337 HideTracking();
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()
349 HideTracking();
350 sal_uInt16 nPrivTabCount=TabCount();
352 if(nPrivTabCount)
354 if(nPrivTabCount>aHeaderBar.GetItemCount())
355 nPrivTabCount=aHeaderBar.GetItemCount();
357 sal_uInt16 nPos=0;
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 );
363 nPos = nNewSize;
366 bPaintFlag=sal_False;
367 Invalidate();
368 Update();
371 CommandEvent SvxSimpleTable::GetCommandEvent() const
373 return aCEvt;
376 void SvxSimpleTable::Command( const CommandEvent& rCEvt )
378 aCEvt=rCEvt;
379 aCommandLink.Call(this);
380 SvHeaderTabListBox::Command(rCEvt);
383 IMPL_LINK( SvxSimpleTable, StartDragHdl, HeaderBar*, pCtr)
385 if(pCtr==&aHeaderBar)
387 HBarStartDrag();
389 return 0;
392 IMPL_LINK( SvxSimpleTable, DragHdl, HeaderBar*, pCtr)
394 if(pCtr==&aHeaderBar)
396 HBarDrag();
398 return 0;
401 IMPL_LINK( SvxSimpleTable, EndDragHdl, HeaderBar*, pCtr)
403 if(pCtr==&aHeaderBar)
405 HBarEndDrag();
407 return 0;
410 IMPL_LINK( SvxSimpleTable, HeaderBarClick, HeaderBar*, pCtr)
412 if(pCtr==&aHeaderBar)
414 HBarClick();
416 return 0;
419 IMPL_LINK( SvxSimpleTable, HeaderBarDblClick, HeaderBar*, pCtr)
421 if(pCtr==&aHeaderBar)
423 HBarDblClick();
425 return 0;
428 SvLBoxItem* SvxSimpleTable::GetEntryAtPos( SvTreeListEntry* pEntry, sal_uInt16 nPos ) const
430 DBG_ASSERT(pEntry,"GetEntryText:Invalid Entry");
431 SvLBoxItem* pItem = NULL;
433 if( pEntry )
435 sal_uInt16 nCount = pEntry->ItemCount();
437 nPos++;
439 if( nTreeFlags & TREEFLAG_CHKBTN ) nPos++;
441 if( nPos < nCount )
443 pItem = pEntry->GetItem( nPos);
446 return pItem;
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;
474 return eCompare;
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: */