xref: /aoo41x/main/svx/source/dialog/simptabl.cxx (revision f6e50924)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 // INCLUDE -------------------------------------------------------------------
28 
29 #include <svx/simptabl.hxx>
30 #include <vcl/svapp.hxx>
31 
32 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
33 #include <comphelper/processfactory.hxx>
34 #endif
35 #include <unotools/intlwrapper.hxx>
36 
37 // SvxSimpTblContainer  ------------------------------------------------------
38 
SvxSimpTblContainer(Window * pParent,WinBits nWinStyle)39 SvxSimpTblContainer::SvxSimpTblContainer( Window* pParent, WinBits nWinStyle):
40 		Control(pParent,nWinStyle)
41 {
42 	//Do Nothing;
43 }
44 
SvxSimpTblContainer(Window * pParent,const ResId & rResId)45 SvxSimpTblContainer::SvxSimpTblContainer( Window* pParent, const ResId& rResId):
46 		Control(pParent,rResId)
47 {
48 	//Do Nothing;
49 }
50 
PreNotify(NotifyEvent & rNEvt)51 long SvxSimpTblContainer::PreNotify( NotifyEvent& rNEvt )
52 {
53     long nResult = sal_True;
54     if ( rNEvt.GetType() == EVENT_KEYINPUT )
55 	{
56         const KeyCode& aKeyCode = rNEvt.GetKeyEvent()->GetKeyCode();
57         sal_uInt16 nKey = aKeyCode.GetCode();
58         if ( nKey == KEY_TAB )
59             GetParent()->Notify( rNEvt );
60         else if ( m_pTable->IsFocusOnCellEnabled() && ( nKey == KEY_LEFT || nKey == KEY_RIGHT ) )
61             return 0;
62 		else
63             nResult = Control::PreNotify( rNEvt );
64 	}
65 	else
66         nResult = Control::PreNotify( rNEvt );
67 
68 	return nResult;
69 }
70 
71 
72 // SvxSimpleTable ------------------------------------------------------------
73 
SvxSimpleTable(Window * pParent,WinBits nBits)74 SvxSimpleTable::SvxSimpleTable( Window* pParent,WinBits nBits ):
75         SvHeaderTabListBox(pParent,WB_CLIPCHILDREN | WB_HSCROLL | WB_TABSTOP),
76 		aPrivContainer(pParent,nBits|WB_DIALOGCONTROL),
77 		aHeaderBar(pParent,WB_BUTTONSTYLE | WB_BORDER | WB_TABSTOP),
78         nHeaderItemId(1),
79         bResizeFlag(sal_False),
80         bPaintFlag(sal_True)
81 {
82 	bSortDirection=sal_True;
83 	nSortCol=0xFFFF;
84 	nOldPos=0;
85 
86 	SetParent(&aPrivContainer);
87 	aHeaderBar.SetParent(&aPrivContainer);
88     aPrivContainer.SetTable( this );
89 
90 	aHeaderBar.SetStartDragHdl(LINK( this, SvxSimpleTable, StartDragHdl));
91 	aHeaderBar.SetDragHdl(LINK( this, SvxSimpleTable, DragHdl));
92 	aHeaderBar.SetEndDragHdl(LINK( this, SvxSimpleTable, EndDragHdl));
93 	aHeaderBar.SetSelectHdl(LINK( this, SvxSimpleTable, HeaderBarClick));
94 	aHeaderBar.SetDoubleClickHdl(LINK( this, SvxSimpleTable, HeaderBarDblClick));
95 
96     EnableCellFocus();
97     DisableTransientChildren();
98     InitHeaderBar( &aHeaderBar );
99 
100     aHeaderBar.Show();
101     SvHeaderTabListBox::Show();
102 }
103 
104 
SvxSimpleTable(Window * pParent,const ResId & rResId)105 SvxSimpleTable::SvxSimpleTable( Window* pParent,const ResId& rResId):
106         SvHeaderTabListBox(pParent,WB_CLIPCHILDREN | WB_HSCROLL | WB_TABSTOP),
107 		aPrivContainer(pParent,rResId),
108 		aHeaderBar(pParent,WB_BUTTONSTYLE | WB_BORDER  | WB_TABSTOP),
109         nHeaderItemId(1),
110         bResizeFlag(sal_True),
111         bPaintFlag(sal_True)
112 {
113 
114 	bSortDirection=sal_True;
115 	nOldPos=0;
116 	nSortCol=0xFFFF;
117 
118 	pMyParentWin=pParent;
119 	SetParent(&aPrivContainer);
120 	aHeaderBar.SetParent(&aPrivContainer);
121     aPrivContainer.SetTable( this );
122 
123 	WinBits nBits=aPrivContainer.GetStyle()|WB_DIALOGCONTROL;
124 	aPrivContainer.SetStyle(nBits);
125 
126 	aHeaderBar.SetStartDragHdl(LINK( this, SvxSimpleTable, StartDragHdl));
127 	aHeaderBar.SetDragHdl(LINK( this, SvxSimpleTable, DragHdl));
128 	aHeaderBar.SetEndDragHdl(LINK( this, SvxSimpleTable, EndDragHdl));
129 	aHeaderBar.SetSelectHdl(LINK( this, SvxSimpleTable, HeaderBarClick));
130 	aHeaderBar.SetDoubleClickHdl(LINK( this, SvxSimpleTable, HeaderBarDblClick));
131 
132 	Size theWinSize=aPrivContainer.GetOutputSizePixel();
133 	Size HbSize=aHeaderBar.GetSizePixel();
134 
135 	HbSize.Width()=theWinSize.Width();
136 	theWinSize.Height()-=HbSize.Height();
137 	Point	thePos(0,0);
138 
139 	aHeaderBar.SetPosPixel(thePos);
140 	aHeaderBar.SetSizePixel(HbSize);
141 
142 	thePos.Y()+=HbSize.Height();
143     SvHeaderTabListBox::SetPosPixel(thePos);
144     SvHeaderTabListBox::SetSizePixel(theWinSize);
145 
146     EnableCellFocus();
147     DisableTransientChildren();
148     InitHeaderBar( &aHeaderBar );
149 
150     aHeaderBar.Show();
151     SvHeaderTabListBox::Show();
152 }
153 
~SvxSimpleTable()154 SvxSimpleTable::~SvxSimpleTable()
155 {
156 	SetParent(pMyParentWin);
157 	aPrivContainer.SetParent(this);
158 	aHeaderBar.SetParent(this);
159 }
160 
161 
UpdateViewSize()162 void SvxSimpleTable::UpdateViewSize()
163 {
164 	Size theWinSize=aPrivContainer.GetOutputSizePixel();
165 	Size HbSize=aHeaderBar.GetSizePixel();
166 
167 	HbSize.Width()=theWinSize.Width();
168 	theWinSize.Height()-=HbSize.Height();
169 	Point	thePos(0,0);
170 
171 	aHeaderBar.SetPosPixel(thePos);
172 	aHeaderBar.SetSizePixel(HbSize);
173 
174 	thePos.Y()+=HbSize.Height();
175     SvHeaderTabListBox::SetPosPixel(thePos);
176     SvHeaderTabListBox::SetSizePixel(theWinSize);
177 	Invalidate();
178 }
179 
NotifyScrolled()180 void SvxSimpleTable::NotifyScrolled()
181 {
182 	long nOffset=-GetXOffset();
183 	if(nOldPos!=nOffset)
184 	{
185 		aHeaderBar.SetOffset(nOffset);
186 		aHeaderBar.Invalidate();
187 		aHeaderBar.Update();
188 		nOldPos=nOffset;
189 	}
190     SvHeaderTabListBox::NotifyScrolled();
191 }
192 
SetTabs()193 void SvxSimpleTable::SetTabs()
194 {
195     SvHeaderTabListBox::SetTabs();
196 
197 	sal_uInt16 nPrivTabCount = TabCount();
198 	if ( nPrivTabCount )
199 	{
200 		if ( nPrivTabCount > aHeaderBar.GetItemCount() )
201 			nPrivTabCount = aHeaderBar.GetItemCount();
202 
203         sal_uInt16 i, nNewSize = static_cast< sal_uInt16 >( GetTab(0) ), nPos = 0;
204 		for ( i = 1; i < nPrivTabCount; ++i )
205 		{
206             nNewSize = static_cast< sal_uInt16 >( GetTab(i) ) - nPos;
207 			aHeaderBar.SetItemSize( i, nNewSize );
208 			nPos = (sal_uInt16)GetTab(i);
209 		}
210 
211 		aHeaderBar.SetItemSize( i, HEADERBAR_FULLSIZE ); // because no tab for last entry
212 	}
213 }
214 
SetTabs(long * pTabs,MapUnit eMapUnit)215 void SvxSimpleTable::SetTabs( long* pTabs, MapUnit eMapUnit)
216 {
217     SvHeaderTabListBox::SetTabs(pTabs,eMapUnit);
218 }
219 
Paint(const Rectangle & rRect)220 void SvxSimpleTable::Paint( const Rectangle& rRect )
221 {
222     SvHeaderTabListBox::Paint(rRect );
223 
224 	sal_uInt16 nPrivTabCount = TabCount();
225 	sal_uInt16 nPos = 0;
226 	sal_uInt16 nNewSize = ( nPrivTabCount > 0 ) ? (sal_uInt16)GetTab(0) : 0;
227 
228 	long nOffset=-GetXOffset();
229 	nOldPos=nOffset;
230 
231 	aHeaderBar.SetOffset(nOffset);
232 	aHeaderBar.Invalidate();
233 
234 	if(nPrivTabCount && bPaintFlag)
235 	{
236 		if(nPrivTabCount>aHeaderBar.GetItemCount())
237 				nPrivTabCount=aHeaderBar.GetItemCount();
238 
239 		for(sal_uInt16 i=1;i<nPrivTabCount;i++)
240 		{
241             nNewSize = static_cast< sal_uInt16 >( GetTab(i) ) - nPos;
242             aHeaderBar.SetItemSize( i, nNewSize );
243             nPos= static_cast< sal_uInt16 >( GetTab(i) );
244 		}
245 	}
246 	bPaintFlag=sal_True;
247 }
InsertHeaderEntry(const XubString & rText,sal_uInt16 nCol,HeaderBarItemBits nBits)248 void SvxSimpleTable::InsertHeaderEntry(const XubString& rText,sal_uInt16 nCol,
249 									   HeaderBarItemBits nBits)
250 {
251 	xub_StrLen nEnd = rText.Search( sal_Unicode( '\t' ) );
252 	if( nEnd == STRING_NOTFOUND )
253 	{
254 		aHeaderBar.InsertItem(nHeaderItemId++, rText, 0, nBits, nCol);
255 	}
256 	else
257 	{
258 		xub_StrLen nCount = rText.GetTokenCount( sal_Unicode( '\t' ) );
259 
260 		for( xub_StrLen i=0; i<nCount; i++ )
261 		{
262 			String	aString=rText.GetToken(i, sal_Unicode( '\t' ) );
263 			aHeaderBar.InsertItem(nHeaderItemId++, aString, 0, nBits, nCol);
264 		}
265 	}
266 	SetTabs();
267 }
268 
ClearAll()269 void SvxSimpleTable::ClearAll()
270 {
271 	aHeaderBar.Clear();
272 	Clear();
273 }
ClearHeader()274 void SvxSimpleTable::ClearHeader()
275 {
276 	aHeaderBar.Clear();
277 }
278 
ShowTable()279 void SvxSimpleTable::ShowTable()
280 {
281 	aPrivContainer.Show();
282 }
283 
HideTable()284 void SvxSimpleTable::HideTable()
285 {
286 	aPrivContainer.Hide();
287 }
288 
IsVisible() const289 sal_Bool SvxSimpleTable::IsVisible() const
290 {
291 	return aPrivContainer.IsVisible();
292 }
293 
EnableTable()294 void SvxSimpleTable::EnableTable()
295 {
296 	aPrivContainer.Enable();
297 }
298 
DisableTable()299 void SvxSimpleTable::DisableTable()
300 {
301 	aPrivContainer.Disable();
302 }
303 
IsEnabled() const304 sal_Bool SvxSimpleTable::IsEnabled() const
305 {
306 	return aPrivContainer.IsEnabled();
307 }
308 
TableToTop()309 void SvxSimpleTable::TableToTop()
310 {
311 	aPrivContainer.ToTop();
312 }
313 
SetPosPixel(const Point & rNewPos)314 void SvxSimpleTable::SetPosPixel( const Point& rNewPos )
315 {
316 	aPrivContainer.SetPosPixel(rNewPos);
317 }
318 
GetPosPixel() const319 Point SvxSimpleTable::GetPosPixel() const
320 {
321 	return aPrivContainer.GetPosPixel();
322 }
323 
SetPosSizePixel(const Point & rNewPos,Size & rNewSize)324 void SvxSimpleTable::SetPosSizePixel( const Point& rNewPos, Size& rNewSize )
325 {
326 	aPrivContainer.SetPosPixel(rNewPos);
327 	aPrivContainer.SetSizePixel(rNewSize);
328 }
329 
SetPosSize(const Point & rNewPos,const Size & rNewSize)330 void SvxSimpleTable::SetPosSize( const Point& rNewPos, const Size& rNewSize )
331 {
332 	aPrivContainer.SetPosPixel(rNewPos);
333     SvHeaderTabListBox::SetPosSizePixel(rNewPos,rNewSize);
334 }
335 
GetSizePixel() const336 Size SvxSimpleTable::GetSizePixel() const
337 {
338 	return aPrivContainer.GetSizePixel();
339 }
340 
GetOutputSizePixel() const341 Size SvxSimpleTable::GetOutputSizePixel() const
342 {
343 	return aPrivContainer.GetOutputSizePixel();
344 }
345 
SetSizePixel(const Size & rNewSize)346 void SvxSimpleTable::SetSizePixel(const Size& rNewSize )
347 {
348 	aPrivContainer.SetSizePixel(rNewSize);
349 	UpdateViewSize();
350 }
351 
SetOutputSizePixel(const Size & rNewSize)352 void SvxSimpleTable::SetOutputSizePixel(const Size& rNewSize )
353 {
354 	aPrivContainer.SetOutputSizePixel(rNewSize);
355 	UpdateViewSize();
356 }
357 
GetSelectedCol()358 sal_uInt16 SvxSimpleTable::GetSelectedCol()
359 {
360 	return (aHeaderBar.GetCurItemId()-1);
361 }
362 
SortByCol(sal_uInt16 nCol,sal_Bool bDir)363 void SvxSimpleTable::SortByCol(sal_uInt16 nCol,sal_Bool bDir)
364 {
365 	bSortDirection=bDir;
366 	if(nSortCol!=0xFFFF)
367 		aHeaderBar.SetItemBits(nSortCol+1,HIB_STDSTYLE);
368 
369 	if (nCol != 0xFFFF)
370 	{
371 		if(bDir)
372 		{
373 			aHeaderBar.SetItemBits( nCol+1, HIB_STDSTYLE | HIB_DOWNARROW);
374 			GetModel()->SetSortMode(SortAscending);
375 		}
376 		else
377 		{
378 			aHeaderBar.SetItemBits( nCol+1, HIB_STDSTYLE | HIB_UPARROW);
379 			GetModel()->SetSortMode(SortDescending);
380 		}
381 		nSortCol=nCol;
382 		GetModel()->SetCompareHdl( LINK( this, SvxSimpleTable, CompareHdl));
383 		GetModel()->Resort();
384 	}
385 	else
386 		GetModel()->SetSortMode(SortNone);
387 	nSortCol=nCol;
388 }
389 
HBarClick()390 void SvxSimpleTable::HBarClick()
391 {
392 	sal_uInt16 nId=aHeaderBar.GetCurItemId();
393 
394 	if (aHeaderBar.GetItemBits(nId) & HIB_CLICKABLE)
395 	{
396 		if(nId==nSortCol+1)
397 		{
398 			SortByCol(nId-1,!bSortDirection);
399 		}
400 		else
401 		{
402 			SortByCol(nId-1,bSortDirection);
403 		}
404 
405 		aHeaderBarClickLink.Call(this);
406 	}
407 }
408 
HBarDblClick()409 void SvxSimpleTable::HBarDblClick()
410 {
411 	aHeaderBarDblClickLink.Call(this);
412 }
413 
HBarStartDrag()414 void SvxSimpleTable::HBarStartDrag()
415 {
416 	if(!aHeaderBar.IsItemMode())
417 	{
418 		Rectangle aSizeRect(Point(0,0),
419             SvHeaderTabListBox::GetOutputSizePixel());
420 		aSizeRect.Left()=-GetXOffset()+aHeaderBar.GetDragPos();
421 		aSizeRect.Right()=-GetXOffset()+aHeaderBar.GetDragPos();
422 		ShowTracking( aSizeRect, SHOWTRACK_SPLIT );
423 	}
424 }
HBarDrag()425 void SvxSimpleTable::HBarDrag()
426 {
427 	HideTracking();
428 	if(!aHeaderBar.IsItemMode())
429 	{
430 		Rectangle aSizeRect(Point(0,0),
431             SvHeaderTabListBox::GetOutputSizePixel());
432 		aSizeRect.Left()=-GetXOffset()+aHeaderBar.GetDragPos();
433 		aSizeRect.Right()=-GetXOffset()+aHeaderBar.GetDragPos();
434 		ShowTracking( aSizeRect, SHOWTRACK_SPLIT );
435 	}
436 }
HBarEndDrag()437 void SvxSimpleTable::HBarEndDrag()
438 {
439 	HideTracking();
440 	sal_uInt16 nPrivTabCount=TabCount();
441 	sal_uInt16 nPos=0;
442 	sal_uInt16 nNewSize=0;
443 
444 	if(nPrivTabCount)
445 	{
446 		if(nPrivTabCount>aHeaderBar.GetItemCount())
447 				nPrivTabCount=aHeaderBar.GetItemCount();
448 
449 		//for(sal_uInt16 i=1;i<=nPrivTabCount;i++)
450 		for(sal_uInt16 i=1;i<nPrivTabCount;i++)
451 		{
452             nNewSize = static_cast< sal_uInt16 >( aHeaderBar.GetItemSize(i) ) + nPos;
453             SetTab( i, nNewSize, MAP_PIXEL );
454             nPos = nNewSize;
455 		}
456 	}
457 	bPaintFlag=sal_False;
458 	Invalidate();
459 	Update();
460 }
461 
GetCommandEvent() const462 CommandEvent SvxSimpleTable::GetCommandEvent() const
463 {
464 	return aCEvt;
465 }
466 
Command(const CommandEvent & rCEvt)467 void SvxSimpleTable::Command( const CommandEvent& rCEvt )
468 {
469 	aCEvt=rCEvt;
470 	aCommandLink.Call(this);
471     SvHeaderTabListBox::Command(rCEvt);
472 }
473 
IMPL_LINK(SvxSimpleTable,StartDragHdl,HeaderBar *,pCtr)474 IMPL_LINK( SvxSimpleTable, StartDragHdl, HeaderBar*, pCtr)
475 {
476 	if(pCtr==&aHeaderBar)
477 	{
478 		HBarStartDrag();
479 	}
480 	return 0;
481 }
482 
IMPL_LINK(SvxSimpleTable,DragHdl,HeaderBar *,pCtr)483 IMPL_LINK( SvxSimpleTable, DragHdl, HeaderBar*, pCtr)
484 {
485 	if(pCtr==&aHeaderBar)
486 	{
487 		HBarDrag();
488 	}
489 	return 0;
490 }
491 
IMPL_LINK(SvxSimpleTable,EndDragHdl,HeaderBar *,pCtr)492 IMPL_LINK( SvxSimpleTable, EndDragHdl, HeaderBar*, pCtr)
493 {
494 	if(pCtr==&aHeaderBar)
495 	{
496 		HBarEndDrag();
497 	}
498 	return 0;
499 }
500 
IMPL_LINK(SvxSimpleTable,HeaderBarClick,HeaderBar *,pCtr)501 IMPL_LINK( SvxSimpleTable, HeaderBarClick, HeaderBar*, pCtr)
502 {
503 	if(pCtr==&aHeaderBar)
504 	{
505 		HBarClick();
506 	}
507 	return 0;
508 }
509 
IMPL_LINK(SvxSimpleTable,HeaderBarDblClick,HeaderBar *,pCtr)510 IMPL_LINK( SvxSimpleTable, HeaderBarDblClick, HeaderBar*, pCtr)
511 {
512 	if(pCtr==&aHeaderBar)
513 	{
514 		HBarDblClick();
515 	}
516 	return 0;
517 }
518 
GetEntryAtPos(SvLBoxEntry * pEntry,sal_uInt16 nPos) const519 SvLBoxItem* SvxSimpleTable::GetEntryAtPos( SvLBoxEntry* pEntry, sal_uInt16 nPos ) const
520 {
521 	DBG_ASSERT(pEntry,"GetEntryText:Invalid Entry");
522 	SvLBoxItem* pItem = NULL;
523 
524 	if( pEntry )
525 	{
526 		sal_uInt16 nCount = pEntry->ItemCount();
527 
528 		nPos++;
529 
530 		if( nTreeFlags & TREEFLAG_CHKBTN ) nPos++;
531 
532 		if( nPos < nCount )
533 		{
534 			pItem = pEntry->GetItem( nPos);
535 		}
536 	}
537 	return pItem;
538 }
539 
ColCompare(SvLBoxEntry * pLeft,SvLBoxEntry * pRight)540 StringCompare SvxSimpleTable::ColCompare(SvLBoxEntry* pLeft,SvLBoxEntry* pRight)
541 {
542 	StringCompare eCompare=COMPARE_EQUAL;
543 
544 	SvLBoxItem* pLeftItem = GetEntryAtPos( pLeft, nSortCol);
545 	SvLBoxItem* pRightItem = GetEntryAtPos( pRight, nSortCol);
546 
547 
548 	if(pLeftItem != NULL && pRightItem != NULL)
549 	{
550 		sal_uInt16 nLeftKind=pLeftItem->IsA();
551 		sal_uInt16 nRightKind=pRightItem->IsA();
552 
553 		if(nRightKind == SV_ITEM_ID_LBOXSTRING &&
554 			nLeftKind == SV_ITEM_ID_LBOXSTRING )
555 		{
556 			IntlWrapper aIntlWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
557 			const CollatorWrapper* pCollator = aIntlWrapper.getCaseCollator();
558 
559 			eCompare=(StringCompare)pCollator->compareString( ((SvLBoxString*)pLeftItem)->GetText(),
560 									((SvLBoxString*)pRightItem)->GetText());
561 
562 			if(eCompare==COMPARE_EQUAL) eCompare=COMPARE_LESS;
563 		}
564 	}
565 	return eCompare;
566 }
567 
IMPL_LINK(SvxSimpleTable,CompareHdl,SvSortData *,pData)568 IMPL_LINK( SvxSimpleTable, CompareHdl, SvSortData*, pData)
569 {
570 	SvLBoxEntry* pLeft = (SvLBoxEntry*)(pData->pLeft );
571 	SvLBoxEntry* pRight = (SvLBoxEntry*)(pData->pRight );
572 	return (long) ColCompare(pLeft,pRight);
573 }
574 
575 
576