fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / dbgui / tpsort.cxx
blob85661d1067a23d7b01661d8e89369c51063fdf3b
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 #undef SC_DLLIMPLEMENTATION
22 #include <vcl/msgbox.hxx>
23 #include <i18nlangtag/languagetag.hxx>
24 #include <svtools/collatorres.hxx>
25 #include <unotools/collatorwrapper.hxx>
26 #include <unotools/localedatawrapper.hxx>
27 #include <comphelper/processfactory.hxx>
29 #include "scitems.hxx"
30 #include "uiitems.hxx"
31 #include "viewdata.hxx"
32 #include "document.hxx"
33 #include "global.hxx"
34 #include "globalnames.hxx"
35 #include "dbdata.hxx"
36 #include "userlist.hxx"
37 #include "rangeutl.hxx"
38 #include "scresid.hxx"
39 #include "sc.hrc"
40 #include "globstr.hrc"
42 #include "sortkeydlg.hxx"
44 #include "sortdlg.hxx"
46 #include "tpsort.hxx"
48 using namespace com::sun::star;
51 * Since the settings on the second Tab Page (Options) effects
52 * the first Tab Page, there must be a way for it to communicate with the
53 * other Page.
55 * At the moment this problem is solved through using two data members of the
56 * Tab Pages. If a page is enabled / disabled, it compares this data member
57 * with its own state (-> Activate() / Deactivate()).
59 * In the meantime the class SfxTabPage offers the following method:
61 * virtual sal_Bool HasExchangeSupport() const; -> return sal_True;
62 * virtual void ActivatePage(const SfxItemSet &);
63 * virtual int DeactivatePage(SfxItemSet * = 0);
65 * This still needs to be changed!
68 // Sort Criteria Tab page
70 ScTabPageSortFields::ScTabPageSortFields(vcl::Window* pParent,
71 const SfxItemSet& rArgSet)
72 : SfxTabPage(pParent, "SortCriteriaPage",
73 "modules/scalc/ui/sortcriteriapage.ui", &rArgSet)
76 aStrUndefined ( SC_RESSTR( SCSTR_UNDEFINED ) ),
77 aStrColumn ( SC_RESSTR( SCSTR_COLUMN ) ),
78 aStrRow ( SC_RESSTR( SCSTR_ROW ) ),
80 nWhichSort ( rArgSet.GetPool()->GetWhich( SID_SORT ) ),
81 pDlg ( static_cast<ScSortDlg*>(GetParentDialog()) ),
82 pViewData ( NULL ),
83 aSortData ( static_cast<const ScSortItem&>(
84 rArgSet.Get( nWhichSort )).
85 GetSortData() ),
86 nFieldCount ( 0 ),
87 nSortKeyCount ( DEFSORT ),
88 bHasHeader ( false ),
89 bSortByRows ( false ),
90 maSortKeyCtrl ( this, maSortKeyItems )
92 Init();
93 SetExchangeSupport();
96 ScTabPageSortFields::~ScTabPageSortFields()
98 disposeOnce();
101 void ScTabPageSortFields::dispose()
103 pDlg.clear();
104 maSortKeyItems.clear();
105 maSortKeyCtrl.dispose();
106 SfxTabPage::dispose();
109 void ScTabPageSortFields::SetPosSizePixel(const Point& rAllocPos, const Size& rAllocation)
111 SfxTabPage::SetPosSizePixel(rAllocPos, rAllocation);
112 maSortKeyCtrl.setScrollRange();
115 void ScTabPageSortFields::SetSizePixel(const Size& rAllocation)
117 SfxTabPage::SetSizePixel(rAllocation);
118 maSortKeyCtrl.setScrollRange();
121 void ScTabPageSortFields::SetPosPixel(const Point& rAllocPos)
123 SfxTabPage::SetPosPixel(rAllocPos);
124 maSortKeyCtrl.setScrollRange();
127 void ScTabPageSortFields::Init()
129 const ScSortItem& rSortItem = static_cast<const ScSortItem&>(
130 GetItemSet().Get( nWhichSort ));
132 pViewData = rSortItem.GetViewData();
133 OSL_ENSURE( pViewData, "ViewData not found!" );
135 nFieldArr.push_back( 0 );
136 nFirstCol = 0;
137 nFirstRow = 0;
139 // Create three sort key dialogs by default
140 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
142 maSortKeyCtrl.AddSortKey(i+1);
143 maSortKeyItems[i].m_pLbSort->SetSelectHdl( LINK( this, ScTabPageSortFields, SelectHdl ) );
147 VclPtr<SfxTabPage> ScTabPageSortFields::Create( vcl::Window* pParent,
148 const SfxItemSet* rArgSet )
150 return VclPtr<ScTabPageSortFields>::Create( pParent, *rArgSet );
153 void ScTabPageSortFields::Reset( const SfxItemSet* /* rArgSet */ )
155 bSortByRows = aSortData.bByRow;
156 bHasHeader = aSortData.bHasHeader;
158 if ( maSortKeyItems[0].m_pLbSort->GetEntryCount() == 0 )
159 FillFieldLists(0);
161 // ListBox selection:
162 if (!aSortData.maKeyState.empty() && aSortData.maKeyState[0].bDoSort)
164 // Make sure that the all sort keys are reset
165 for ( sal_uInt16 i=nSortKeyCount; i<aSortData.GetSortKeyCount(); i++ )
167 maSortKeyCtrl.AddSortKey(i+1);
168 maSortKeyItems[i].m_pLbSort->SetSelectHdl( LINK( this,
169 ScTabPageSortFields, SelectHdl ) );
171 nSortKeyCount = aSortData.GetSortKeyCount();
172 FillFieldLists(0);
174 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
176 if (aSortData.maKeyState[i].bDoSort )
178 maSortKeyItems[i].m_pLbSort->SelectEntryPos( GetFieldSelPos(
179 aSortData.maKeyState[i].nField ) );
180 (aSortData.maKeyState[i].bAscending)
181 ? maSortKeyItems[i].m_pBtnUp->Check()
182 : maSortKeyItems[i].m_pBtnDown->Check();
184 else
186 maSortKeyItems[i].m_pLbSort->SelectEntryPos( 0 ); // Select none
187 maSortKeyItems[i].m_pBtnUp->Check();
191 // Enable or disable field depending on preceding Listbox selection
192 maSortKeyItems[0].EnableField();
193 for ( sal_uInt16 i=1; i<nSortKeyCount; i++ )
194 if ( maSortKeyItems[i - 1].m_pLbSort->GetSelectEntryPos() == 0 )
195 maSortKeyItems[i].DisableField();
196 else
197 maSortKeyItems[i].EnableField();
199 else
201 SCCOL nCol = pViewData->GetCurX();
203 if( nCol < aSortData.nCol1 )
204 nCol = aSortData.nCol1;
205 else if( nCol > aSortData.nCol2 )
206 nCol = aSortData.nCol2;
208 sal_uInt16 nSort1Pos = nCol - aSortData.nCol1+1;
210 maSortKeyItems[0].m_pLbSort->SelectEntryPos( nSort1Pos );
211 for ( sal_uInt16 i=1; i<nSortKeyCount; i++ )
212 maSortKeyItems[i].m_pLbSort->SelectEntryPos( 0 );
214 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
215 maSortKeyItems[i].m_pBtnUp->Check();
217 maSortKeyItems[0].EnableField();
218 maSortKeyItems[1].EnableField();
219 for ( sal_uInt16 i=2; i<nSortKeyCount; i++ )
220 maSortKeyItems[i].DisableField();
223 if ( pDlg )
225 pDlg->SetByRows ( bSortByRows );
226 pDlg->SetHeaders( bHasHeader );
229 // Make sure that there is always a last undefined sort key
230 if ( maSortKeyItems[nSortKeyCount - 1].m_pLbSort->GetSelectEntryPos() > 0 )
231 SetLastSortKey( nSortKeyCount );
234 bool ScTabPageSortFields::FillItemSet( SfxItemSet* rArgSet )
236 ScSortParam aNewSortData = aSortData;
238 if (pDlg)
240 const SfxItemSet* pExample = pDlg->GetExampleSet();
241 const SfxPoolItem* pItem;
242 if ( pExample && pExample->GetItemState( nWhichSort, true, &pItem ) == SfxItemState::SET )
244 ScSortParam aTempData = static_cast<const ScSortItem*>(pItem)->GetSortData();
245 aTempData.maKeyState = aNewSortData.maKeyState;
246 aNewSortData = aTempData;
249 std::vector<sal_Int32> nSortPos;
251 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
253 nSortPos.push_back( maSortKeyItems[i].m_pLbSort->GetSelectEntryPos() );
255 if ( nSortPos[i] == LISTBOX_ENTRY_NOTFOUND ) nSortPos[i] = 0;
258 if( nSortKeyCount >= aNewSortData.GetSortKeyCount() )
259 aNewSortData.maKeyState.resize(nSortKeyCount);
261 if ( nSortPos[0] > 0 )
263 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
264 aNewSortData.maKeyState[i].bDoSort = (nSortPos[i] > 0);
266 // If the "OK" was selected on the Options page while the sort
267 // direction was changed, then the first field (i.e. nFieldArr[0])
268 // of the respective direction is chosen as the sorting criterion:
269 if ( pDlg && bSortByRows != pDlg->GetByRows() )
271 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
272 aNewSortData.maKeyState[i].nField = ( bSortByRows ?
273 static_cast<SCCOLROW>(nFirstRow) :
274 static_cast<SCCOLROW>(nFirstCol) );
276 else
278 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
279 aNewSortData.maKeyState[i].nField = nFieldArr[nSortPos[i]];
282 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
283 aNewSortData.maKeyState[i].bAscending = maSortKeyItems[i].m_pBtnUp->IsChecked();
285 // bHasHeader is in ScTabPageSortOptions::FillItemSet, where it belongs
287 else
289 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
290 aNewSortData.maKeyState[i].bDoSort = false;
293 rArgSet->Put( ScSortItem( SCITEM_SORTDATA, NULL, &aNewSortData ) );
295 return true;
298 // for data exchange without dialogue detour:
299 void ScTabPageSortFields::ActivatePage( const SfxItemSet& rSet )
301 // Refresh local copy with shared data
302 aSortData = static_cast<const ScSortItem&>(rSet.Get( SCITEM_SORTDATA )).GetSortData();
303 if ( pDlg )
305 if ( bHasHeader != pDlg->GetHeaders()
306 || bSortByRows != pDlg->GetByRows() )
308 std::vector<sal_uInt16> nCurSel;
309 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
310 nCurSel.push_back( maSortKeyItems[i].m_pLbSort->GetSelectEntryPos() );
312 bHasHeader = pDlg->GetHeaders();
313 bSortByRows = pDlg->GetByRows();
314 FillFieldLists(0);
316 for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
317 maSortKeyItems[i].m_pLbSort->SelectEntryPos( nCurSel[i] );
322 SfxTabPage::sfxpg ScTabPageSortFields::DeactivatePage( SfxItemSet* pSetP )
324 if ( pDlg )
326 if ( bHasHeader != pDlg->GetHeaders() )
327 pDlg->SetHeaders( bHasHeader );
329 if ( bSortByRows != pDlg->GetByRows() )
330 pDlg->SetByRows( bSortByRows );
333 if ( pSetP )
334 FillItemSet( pSetP );
336 return SfxTabPage::LEAVE_PAGE;
339 void ScTabPageSortFields::FillFieldLists( sal_uInt16 nStartField )
341 if ( pViewData )
343 ScDocument* pDoc = pViewData->GetDocument();
345 if ( pDoc )
347 for ( sal_uInt16 i=nStartField; i<nSortKeyCount; i++ )
349 maSortKeyItems[i].m_pLbSort->Clear();
350 maSortKeyItems[i].m_pLbSort->InsertEntry( aStrUndefined, 0 );
353 SCCOL nFirstSortCol = aSortData.nCol1;
354 SCROW nFirstSortRow = aSortData.nRow1;
355 SCTAB nTab = pViewData->GetTabNo();
356 sal_uInt16 i = 1;
357 nFieldArr.clear();
358 nFieldArr.push_back(0);
360 if ( bSortByRows )
362 OUString aFieldName;
363 SCCOL nMaxCol = aSortData.nCol2;
364 SCCOL col;
366 for ( col=nFirstSortCol; col<=nMaxCol && i<SC_MAXFIELDS; col++ )
368 aFieldName = pDoc->GetString(col, nFirstSortRow, nTab);
369 if ( !bHasHeader || aFieldName.isEmpty() )
371 aFieldName = ScGlobal::ReplaceOrAppend( aStrColumn, "%1", ScColToAlpha( col ));
373 nFieldArr.push_back( col );
375 for ( sal_uInt16 j=nStartField; j<nSortKeyCount; j++ )
376 maSortKeyItems[j].m_pLbSort->InsertEntry( aFieldName, i );
378 i++;
381 else
383 OUString aFieldName;
384 SCROW nMaxRow = aSortData.nRow2;
385 SCROW row;
387 for ( row=nFirstSortRow; row<=nMaxRow && i<SC_MAXFIELDS; row++ )
389 aFieldName = pDoc->GetString(nFirstSortCol, row, nTab);
390 if ( !bHasHeader || aFieldName.isEmpty() )
392 aFieldName = ScGlobal::ReplaceOrAppend( aStrRow, "%1", OUString::number( row+1));
394 nFieldArr.push_back( row );
396 for ( sal_uInt16 j=nStartField; j<nSortKeyCount; j++ )
397 maSortKeyItems[j].m_pLbSort->InsertEntry( aFieldName, i );
399 i++;
402 nFieldCount = i;
407 sal_uInt16 ScTabPageSortFields::GetFieldSelPos( SCCOLROW nField )
409 sal_uInt16 nFieldPos = 0;
410 bool bFound = false;
412 for ( sal_uInt16 n=1; n<nFieldCount && !bFound; n++ )
414 if ( nFieldArr[n] == nField )
416 nFieldPos = n;
417 bFound = true;
421 return nFieldPos;
424 void ScTabPageSortFields::SetLastSortKey( sal_uInt16 nItem )
426 // Extend local SortParam copy
427 const ScSortKeyState atempKeyState = { false, 0, true };
428 aSortData.maKeyState.push_back( atempKeyState );
430 // Add Sort Key Item
431 ++nSortKeyCount;
432 maSortKeyCtrl.AddSortKey( nSortKeyCount );
433 maSortKeyItems[nItem].m_pLbSort->SetSelectHdl(
434 LINK( this, ScTabPageSortFields, SelectHdl ) );
436 FillFieldLists( nItem );
438 // Set Status
439 maSortKeyItems[nItem].m_pBtnUp->Check();
440 maSortKeyItems[nItem].m_pLbSort->SelectEntryPos( 0 );
443 // Handler:
445 IMPL_LINK( ScTabPageSortFields, SelectHdl, ListBox *, pLb )
447 OUString aSelEntry = pLb->GetSelectEntry();
448 ScSortKeyItems::iterator pIter;
450 // If last listbox is enabled add one item
451 if ( maSortKeyItems.back().m_pLbSort == pLb )
452 if ( aSelEntry != aStrUndefined )
454 SetLastSortKey( nSortKeyCount );
455 return 0;
458 // Find selected listbox
459 for ( pIter = maSortKeyItems.begin(); pIter != maSortKeyItems.end(); ++pIter )
461 if ( pIter->m_pLbSort == pLb ) break;
464 // If not selecting the last Listbox, modify the succeeding ones
465 ++pIter;
466 if ( std::distance(maSortKeyItems.begin(), pIter) < nSortKeyCount )
468 if ( aSelEntry == aStrUndefined )
470 for ( ; pIter != maSortKeyItems.end(); ++pIter )
472 pIter->m_pLbSort->SelectEntryPos( 0 );
474 if ( pIter->m_pFlSort->IsEnabled() )
475 pIter->DisableField();
478 else
480 if ( !pIter->m_pFlSort->IsEnabled() )
481 pIter->EnableField();
484 return 0;
487 // Sort option Tab Page:
489 ScTabPageSortOptions::ScTabPageSortOptions( vcl::Window* pParent,
490 const SfxItemSet& rArgSet )
491 : SfxTabPage(pParent, "SortOptionsPage",
492 "modules/scalc/ui/sortoptionspage.ui", &rArgSet)
493 , aStrRowLabel(SC_RESSTR(SCSTR_ROW_LABEL))
494 , aStrColLabel(SC_RESSTR(SCSTR_COL_LABEL))
495 , aStrUndefined(SC_RESSTR(SCSTR_UNDEFINED))
496 , nWhichSort(rArgSet.GetPool()->GetWhich(SID_SORT))
497 , aSortData(static_cast<const ScSortItem&>(rArgSet.Get(nWhichSort)).GetSortData())
498 , pViewData(NULL)
499 , pDoc(NULL)
500 , pDlg(static_cast<ScSortDlg*>(GetParentDialog()))
501 , pColRes( NULL )
502 , pColWrap( NULL )
504 get(m_pBtnCase, "case");
505 get(m_pBtnHeader, "header");
506 get(m_pBtnFormats, "formats");
507 get(m_pBtnNaturalSort, "naturalsort");
508 get(m_pBtnCopyResult, "copyresult");
509 get(m_pLbOutPos, "outarealb");
510 get(m_pEdOutPos, "outareaed");
511 get(m_pBtnSortUser, "sortuser");
512 get(m_pLbSortUser, "sortuserlb");
513 get(m_pFtAlgorithm, "algorithmft");
514 get(m_pLbAlgorithm, "algorithmlb");
515 get(m_pBtnTopDown, "topdown");
516 get(m_pBtnLeftRight, "leftright");
517 get(m_pLbLanguage, "language");
518 Init();
519 SetExchangeSupport();
521 m_pLbOutPos->SetAccessibleName(m_pBtnCopyResult->GetText());
522 m_pEdOutPos->SetAccessibleName(m_pBtnCopyResult->GetText());
523 m_pLbSortUser->SetAccessibleName(m_pBtnSortUser->GetText());
526 ScTabPageSortOptions::~ScTabPageSortOptions()
528 disposeOnce();
531 void ScTabPageSortOptions::dispose()
533 sal_uInt16 nEntries = m_pLbOutPos->GetEntryCount();
535 for ( sal_uInt16 i=1; i<nEntries; i++ )
536 delete static_cast<OUString*>(m_pLbOutPos->GetEntryData( i ));
538 delete pColRes;
539 delete pColWrap; //! not if from document
540 m_pBtnCase.clear();
541 m_pBtnHeader.clear();
542 m_pBtnFormats.clear();
543 m_pBtnNaturalSort.clear();
544 m_pBtnCopyResult.clear();
545 m_pLbOutPos.clear();
546 m_pEdOutPos.clear();
547 m_pBtnSortUser.clear();
548 m_pLbSortUser.clear();
549 m_pLbLanguage.clear();
550 m_pFtAlgorithm.clear();
551 m_pLbAlgorithm.clear();
552 m_pBtnTopDown.clear();
553 m_pBtnLeftRight.clear();
554 pDlg.clear();
555 SfxTabPage::dispose();
558 void ScTabPageSortOptions::Init()
560 // CollatorResource has user-visible names for sort algorithms
561 pColRes = new CollatorResource();
563 //! use CollatorWrapper from document?
564 pColWrap = new CollatorWrapper( comphelper::getProcessComponentContext() );
566 const ScSortItem& rSortItem = static_cast<const ScSortItem&>(
567 GetItemSet().Get( nWhichSort ));
569 m_pLbOutPos->SetSelectHdl ( LINK( this, ScTabPageSortOptions, SelOutPosHdl ) );
570 m_pBtnCopyResult->SetClickHdl( LINK( this, ScTabPageSortOptions, EnableHdl ) );
571 m_pBtnSortUser->SetClickHdl ( LINK( this, ScTabPageSortOptions, EnableHdl ) );
572 m_pBtnTopDown->SetClickHdl ( LINK( this, ScTabPageSortOptions, SortDirHdl ) );
573 m_pBtnLeftRight->SetClickHdl ( LINK( this, ScTabPageSortOptions, SortDirHdl ) );
574 m_pLbLanguage->SetSelectHdl ( LINK( this, ScTabPageSortOptions, FillAlgorHdl ) );
576 pViewData = rSortItem.GetViewData();
577 pDoc = pViewData ? pViewData->GetDocument() : NULL;
579 OSL_ENSURE( pViewData, "ViewData not found! :-/" );
581 if ( pViewData && pDoc )
583 ScDBCollection* pDBColl = pDoc->GetDBCollection();
584 const SCTAB nCurTab = pViewData->GetTabNo();
585 OUString theDbName = OUString(STR_DB_LOCAL_NONAME);
586 const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
588 m_pLbOutPos->Clear();
589 m_pLbOutPos->InsertEntry( aStrUndefined, 0 );
590 m_pLbOutPos->Disable();
592 ScAreaNameIterator aIter( pDoc );
593 OUString aName;
594 ScRange aRange;
595 while ( aIter.Next( aName, aRange ) )
597 sal_uInt16 nInsert = m_pLbOutPos->InsertEntry( aName );
599 OUString aRefStr(aRange.aStart.Format(SCA_ABS_3D, pDoc, eConv));
600 m_pLbOutPos->SetEntryData( nInsert, new OUString( aRefStr ) );
603 m_pLbOutPos->SelectEntryPos( 0 );
604 m_pEdOutPos->SetText( EMPTY_OUSTRING );
606 // Check whether the field that is passed on is a database field:
608 ScAddress aScAddress( aSortData.nCol1, aSortData.nRow1, nCurTab );
609 OUString theArea =
610 ScRange( aScAddress,
611 ScAddress( aSortData.nCol2, aSortData.nRow2, nCurTab )
612 ).Format(SCR_ABS, pDoc, eConv);
614 if ( pDBColl )
616 ScDBData* pDBData
617 = pDBColl->GetDBAtArea( nCurTab,
618 aSortData.nCol1, aSortData.nRow1,
619 aSortData.nCol2, aSortData.nRow2 );
620 if ( pDBData )
622 theDbName = pDBData->GetName();
623 m_pBtnHeader->Check( pDBData->HasHeader() );
627 theArea += " (" + theDbName + ")";
629 m_pBtnHeader->SetText( aStrColLabel );
632 FillUserSortListBox();
634 // get available languages
636 m_pLbLanguage->SetLanguageList( SvxLanguageListFlags::ALL | SvxLanguageListFlags::ONLY_KNOWN, false );
637 m_pLbLanguage->InsertLanguage( LANGUAGE_SYSTEM );
640 VclPtr<SfxTabPage> ScTabPageSortOptions::Create( vcl::Window* pParent,
641 const SfxItemSet* rArgSet )
643 return VclPtr<ScTabPageSortOptions>::Create( pParent, *rArgSet );
646 void ScTabPageSortOptions::Reset( const SfxItemSet* /* rArgSet */ )
648 if ( aSortData.bUserDef )
650 m_pBtnSortUser->Check( true );
651 m_pLbSortUser->Enable();
652 m_pLbSortUser->SelectEntryPos( aSortData.nUserIndex );
654 else
656 m_pBtnSortUser->Check( false );
657 m_pLbSortUser->Disable();
658 m_pLbSortUser->SelectEntryPos( 0 );
661 m_pBtnCase->Check ( aSortData.bCaseSens );
662 m_pBtnFormats->Check ( aSortData.bIncludePattern );
663 m_pBtnHeader->Check ( aSortData.bHasHeader );
664 m_pBtnNaturalSort->Check ( aSortData.bNaturalSort );
666 if ( aSortData.bByRow )
668 m_pBtnTopDown->Check();
669 m_pBtnHeader->SetText( aStrColLabel );
671 else
673 m_pBtnLeftRight->Check();
674 m_pBtnHeader->SetText( aStrRowLabel );
677 LanguageType eLang = LanguageTag::convertToLanguageType( aSortData.aCollatorLocale, false);
678 if ( eLang == LANGUAGE_DONTKNOW )
679 eLang = LANGUAGE_SYSTEM;
680 m_pLbLanguage->SelectLanguage( eLang );
681 FillAlgorHdl(m_pLbLanguage); // get algorithms, select default
682 if ( !aSortData.aCollatorAlgorithm.isEmpty() )
683 m_pLbAlgorithm->SelectEntry( pColRes->GetTranslation( aSortData.aCollatorAlgorithm ) );
685 if ( pDoc && !aSortData.bInplace )
687 sal_uInt16 nFormat = (aSortData.nDestTab != pViewData->GetTabNo())
688 ? SCR_ABS_3D
689 : SCR_ABS;
691 theOutPos.Set( aSortData.nDestCol,
692 aSortData.nDestRow,
693 aSortData.nDestTab );
695 OUString aStr(theOutPos.Format(nFormat, pDoc, pDoc->GetAddressConvention()));
696 m_pBtnCopyResult->Check();
697 m_pLbOutPos->Enable();
698 m_pEdOutPos->Enable();
699 m_pEdOutPos->SetText( aStr );
700 EdOutPosModHdl(m_pEdOutPos);
701 m_pEdOutPos->GrabFocus();
702 m_pEdOutPos->SetSelection( Selection( 0, SELECTION_MAX ) );
704 else
706 m_pBtnCopyResult->Check( false );
707 m_pLbOutPos->Disable();
708 m_pEdOutPos->Disable();
709 m_pEdOutPos->SetText( EMPTY_OUSTRING );
713 bool ScTabPageSortOptions::FillItemSet( SfxItemSet* rArgSet )
715 // Create local copy of ScParam
716 ScSortParam aNewSortData = aSortData;
718 if (pDlg)
720 const SfxItemSet* pExample = pDlg->GetExampleSet();
721 const SfxPoolItem* pItem;
722 if ( pExample && pExample->GetItemState( nWhichSort, true, &pItem ) == SfxItemState::SET )
723 aNewSortData = static_cast<const ScSortItem*>(pItem)->GetSortData();
725 aNewSortData.bByRow = m_pBtnTopDown->IsChecked();
726 aNewSortData.bHasHeader = m_pBtnHeader->IsChecked();
727 aNewSortData.bCaseSens = m_pBtnCase->IsChecked();
728 aNewSortData.bNaturalSort = m_pBtnNaturalSort->IsChecked();
729 aNewSortData.bIncludePattern = m_pBtnFormats->IsChecked();
730 aNewSortData.bInplace = !m_pBtnCopyResult->IsChecked();
731 aNewSortData.nDestCol = theOutPos.Col();
732 aNewSortData.nDestRow = theOutPos.Row();
733 aNewSortData.nDestTab = theOutPos.Tab();
734 aNewSortData.bUserDef = m_pBtnSortUser->IsChecked();
735 aNewSortData.nUserIndex = (m_pBtnSortUser->IsChecked())
736 ? m_pLbSortUser->GetSelectEntryPos()
737 : 0;
739 // get locale
740 LanguageType eLang = m_pLbLanguage->GetSelectLanguage();
741 aNewSortData.aCollatorLocale = LanguageTag::convertToLocale( eLang, false);
743 // get algorithm
744 OUString sAlg;
745 if ( eLang != LANGUAGE_SYSTEM )
747 uno::Sequence<OUString> aAlgos = pColWrap->listCollatorAlgorithms(
748 aNewSortData.aCollatorLocale );
749 sal_uInt16 nSel = m_pLbAlgorithm->GetSelectEntryPos();
750 if ( nSel < aAlgos.getLength() )
751 sAlg = aAlgos[nSel];
753 aNewSortData.aCollatorAlgorithm = sAlg;
755 rArgSet->Put( ScSortItem( SCITEM_SORTDATA, &aNewSortData ) );
757 return true;
760 // for data exchange without dialogue detour:
761 void ScTabPageSortOptions::ActivatePage( const SfxItemSet& rSet )
763 // Refresh local copy with shared data
764 aSortData = static_cast<const ScSortItem&>(rSet.Get( SCITEM_SORTDATA )).GetSortData();
765 if ( pDlg )
767 if ( m_pBtnHeader->IsChecked() != pDlg->GetHeaders() )
769 m_pBtnHeader->Check( pDlg->GetHeaders() );
772 if ( m_pBtnTopDown->IsChecked() != pDlg->GetByRows() )
774 m_pBtnTopDown->Check( pDlg->GetByRows() );
775 m_pBtnLeftRight->Check( !pDlg->GetByRows() );
778 m_pBtnHeader->SetText( (pDlg->GetByRows())
779 ? aStrColLabel
780 : aStrRowLabel );
784 SfxTabPage::sfxpg ScTabPageSortOptions::DeactivatePage( SfxItemSet* pSetP )
786 bool bPosInputOk = true;
788 if ( m_pBtnCopyResult->IsChecked() )
790 OUString thePosStr = m_pEdOutPos->GetText();
791 ScAddress thePos;
792 sal_Int32 nColonPos = thePosStr.indexOf( ':' );
794 if ( -1 != nColonPos )
795 thePosStr = thePosStr.copy( 0, nColonPos );
797 if ( pViewData )
799 // visible table is default for input without table
800 // must be changed to GetRefTabNo when sorting has RefInput!
801 thePos.SetTab( pViewData->GetTabNo() );
804 sal_uInt16 nResult = thePos.Parse( thePosStr, pDoc, pDoc->GetAddressConvention() );
806 bPosInputOk = ( SCA_VALID == (nResult & SCA_VALID) );
808 if ( !bPosInputOk )
810 ScopedVclPtrInstance<MessageDialog>::Create(this, ScGlobal::GetRscString( STR_INVALID_TABREF))->Execute();
811 m_pEdOutPos->GrabFocus();
812 m_pEdOutPos->SetSelection( Selection( 0, SELECTION_MAX ) );
813 theOutPos.Set(0,0,0);
815 else
817 m_pEdOutPos->SetText( thePosStr );
818 theOutPos = thePos;
822 if ( pDlg && bPosInputOk )
824 pDlg->SetHeaders( m_pBtnHeader->IsChecked() );
825 pDlg->SetByRows ( m_pBtnTopDown->IsChecked() );
828 if ( pSetP && bPosInputOk )
829 FillItemSet( pSetP );
831 return bPosInputOk ? SfxTabPage::LEAVE_PAGE : SfxTabPage::KEEP_PAGE;
834 void ScTabPageSortOptions::FillUserSortListBox()
836 ScUserList* pUserLists = ScGlobal::GetUserList();
838 m_pLbSortUser->Clear();
839 if ( pUserLists )
841 size_t nCount = pUserLists->size();
842 if ( nCount > 0 )
843 for ( size_t i=0; i<nCount; ++i )
844 m_pLbSortUser->InsertEntry( (*pUserLists)[i]->GetString() );
848 // Handler:
850 IMPL_LINK( ScTabPageSortOptions, EnableHdl, CheckBox *, pBox )
852 if (pBox == m_pBtnCopyResult)
854 if ( pBox->IsChecked() )
856 m_pLbOutPos->Enable();
857 m_pEdOutPos->Enable();
858 m_pEdOutPos->GrabFocus();
860 else
862 m_pLbOutPos->Disable();
863 m_pEdOutPos->Disable();
866 else if (pBox == m_pBtnSortUser)
868 if ( pBox->IsChecked() )
870 m_pLbSortUser->Enable();
871 m_pLbSortUser->GrabFocus();
873 else
874 m_pLbSortUser->Disable();
876 return 0;
879 IMPL_LINK( ScTabPageSortOptions, SelOutPosHdl, ListBox *, pLb )
881 if (pLb == m_pLbOutPos)
883 OUString aString;
884 sal_uInt16 nSelPos = m_pLbOutPos->GetSelectEntryPos();
886 if ( nSelPos > 0 )
887 aString = *static_cast<OUString*>(m_pLbOutPos->GetEntryData( nSelPos ));
889 m_pEdOutPos->SetText( aString );
891 return 0;
894 IMPL_LINK( ScTabPageSortOptions, SortDirHdl, RadioButton *, pBtn )
896 if (pBtn == m_pBtnTopDown)
898 m_pBtnHeader->SetText( aStrColLabel );
900 else if (pBtn == m_pBtnLeftRight)
902 m_pBtnHeader->SetText( aStrRowLabel );
904 return 0;
907 void ScTabPageSortOptions::EdOutPosModHdl( Edit* pEd )
909 if (pEd == m_pEdOutPos)
911 OUString theCurPosStr = m_pEdOutPos->GetText();
912 sal_uInt16 nResult = ScAddress().Parse( theCurPosStr, pDoc, pDoc->GetAddressConvention() );
914 if ( SCA_VALID == (nResult & SCA_VALID) )
916 bool bFound = false;
917 sal_uInt16 i = 0;
918 sal_uInt16 nCount = m_pLbOutPos->GetEntryCount();
920 for ( i=2; i<nCount && !bFound; i++ )
922 OUString* pStr = static_cast<OUString*>(m_pLbOutPos->GetEntryData( i ));
923 bFound = (theCurPosStr == *pStr);
926 if ( bFound )
927 m_pLbOutPos->SelectEntryPos( --i );
928 else
929 m_pLbOutPos->SelectEntryPos( 0 );
934 IMPL_LINK_NOARG(ScTabPageSortOptions, FillAlgorHdl)
936 m_pLbAlgorithm->SetUpdateMode( false );
937 m_pLbAlgorithm->Clear();
939 LanguageType eLang = m_pLbLanguage->GetSelectLanguage();
940 if ( eLang == LANGUAGE_SYSTEM )
942 // for LANGUAGE_SYSTEM no algorithm can be selected because
943 // it wouldn't necessarily exist for other languages
944 // -> leave list box empty if LANGUAGE_SYSTEM is selected
945 m_pFtAlgorithm->Enable( false ); // nothing to select
946 m_pLbAlgorithm->Enable( false ); // nothing to select
948 else
950 lang::Locale aLocale( LanguageTag::convertToLocale( eLang ));
951 uno::Sequence<OUString> aAlgos = pColWrap->listCollatorAlgorithms( aLocale );
953 long nCount = aAlgos.getLength();
954 const OUString* pArray = aAlgos.getConstArray();
955 for (long i=0; i<nCount; i++)
957 OUString sAlg = pArray[i];
958 OUString sUser = pColRes->GetTranslation( sAlg );
959 m_pLbAlgorithm->InsertEntry( sUser, LISTBOX_APPEND );
961 m_pLbAlgorithm->SelectEntryPos( 0 ); // first entry is default
962 m_pFtAlgorithm->Enable( nCount > 1 ); // enable only if there is a choice
963 m_pLbAlgorithm->Enable( nCount > 1 ); // enable only if there is a choice
966 m_pLbAlgorithm->SetUpdateMode( true );
967 return 0;
970 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */