Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #undef SC_DLLIMPLEMENTATION
31 : :
32 : : #include <vcl/msgbox.hxx>
33 : : #include <i18npool/mslangid.hxx>
34 : : #include <svtools/collatorres.hxx>
35 : : #include <unotools/collatorwrapper.hxx>
36 : : #include <unotools/localedatawrapper.hxx>
37 : : #include <comphelper/processfactory.hxx>
38 : :
39 : : #include "scitems.hxx"
40 : : #include "uiitems.hxx"
41 : : #include "viewdata.hxx"
42 : : #include "document.hxx"
43 : : #include "global.hxx"
44 : : #include "globalnames.hxx"
45 : : #include "dbdata.hxx"
46 : : #include "userlist.hxx"
47 : : #include "rangeutl.hxx"
48 : : #include "scresid.hxx"
49 : : #include "sc.hrc" // -> Slot IDs
50 : : #include "globstr.hrc"
51 : :
52 : : #include "sortkeydlg.hxx"
53 : :
54 : : #include "sortdlg.hxx"
55 : : #include "sortdlg.hrc"
56 : :
57 : : #define _TPSORT_CXX
58 : : #include "tpsort.hxx"
59 : : #undef _TPSORT_CXX
60 : :
61 : : using namespace com::sun::star;
62 : :
63 : : // -----------------------------------------------------------------------
64 : :
65 : : /*
66 : : * Since the settings on the second Tab Page (Options) effects
67 : : * the first Tab Page, there must be a way for it to communicate with the
68 : : * other Page.
69 : : *
70 : : * At the moment this problem is solved through using two data members of the
71 : : * Tab Pages. If a page is enabled / disabled, it compares this data member
72 : : * with its own state (-> Activate() / Deactivate()).
73 : : *
74 : : * In the meantime the class SfxTabPage offers the following method:
75 : : *
76 : : * virtual sal_Bool HasExchangeSupport() const; -> return sal_True;
77 : : * virtual void ActivatePage(const SfxItemSet &);
78 : : * virtual int DeactivatePage(SfxItemSet * = 0);
79 : : *
80 : : * This still needs to be changed!
81 : : */
82 : :
83 : : //========================================================================
84 : : //========================================================================
85 : : // Sort Criteria Tab page
86 : :
87 : 0 : ScTabPageSortFields::ScTabPageSortFields( Window* pParent,
88 : : const SfxItemSet& rArgSet )
89 : :
90 : : : SfxTabPage ( pParent,
91 : : ScResId( RID_SCPAGE_SORT_FIELDS ),
92 : : rArgSet ),
93 : : //
94 : : aStrUndefined ( SC_RESSTR( SCSTR_UNDEFINED ) ),
95 : : aStrColumn ( SC_RESSTR( SCSTR_COLUMN ) ),
96 : : aStrRow ( SC_RESSTR( SCSTR_ROW ) ),
97 : : //
98 : 0 : nWhichSort ( rArgSet.GetPool()->GetWhich( SID_SORT ) ),
99 : 0 : pDlg ( (ScSortDlg*)(GetParent()->GetParent()) ),
100 : : pViewData ( NULL ),
101 : : aSortData ( ((const ScSortItem&)
102 : 0 : rArgSet.Get( nWhichSort )).
103 : 0 : GetSortData() ),
104 : : nFieldCount ( 0 ),
105 : : nSortKeyCount ( DEFSORT ),
106 : : nCurrentOffset ( 0 ),
107 : : bHasHeader ( false ),
108 : : bSortByRows ( false ),
109 : 0 : maSortKeyCtrl ( this, ScResId( CTRL_MANAGESORTKEY ), maSortKeyItems )
110 : : {
111 : 0 : Init();
112 : 0 : FreeResource();
113 : 0 : SetExchangeSupport();
114 : 0 : }
115 : :
116 : : // -----------------------------------------------------------------------
117 : :
118 : 0 : ScTabPageSortFields::~ScTabPageSortFields()
119 : : {
120 : 0 : }
121 : :
122 : : // -----------------------------------------------------------------------
123 : :
124 : 0 : void ScTabPageSortFields::Init()
125 : : {
126 : : const ScSortItem& rSortItem = (const ScSortItem&)
127 : 0 : GetItemSet().Get( nWhichSort );
128 : :
129 : 0 : pViewData = rSortItem.GetViewData();
130 : : OSL_ENSURE( pViewData, "ViewData not found!" );
131 : :
132 : 0 : nFieldArr.push_back( 0 );
133 : 0 : nFirstCol = 0;
134 : 0 : nFirstRow = 0;
135 : :
136 : : // Create three sort key dialogs by default
137 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
138 : : {
139 : 0 : maSortKeyCtrl.AddSortKey(i+1);
140 : 0 : maSortKeyItems[i].aLbSort.SetSelectHdl( LINK( this, ScTabPageSortFields, SelectHdl ) );
141 : : }
142 : 0 : }
143 : :
144 : : // -----------------------------------------------------------------------
145 : :
146 : 0 : SfxTabPage* ScTabPageSortFields::Create( Window* pParent,
147 : : const SfxItemSet& rArgSet )
148 : : {
149 : 0 : return ( new ScTabPageSortFields( pParent, rArgSet ) );
150 : : }
151 : :
152 : : // -----------------------------------------------------------------------
153 : :
154 : 0 : void ScTabPageSortFields::Reset( const SfxItemSet& /* rArgSet */ )
155 : : {
156 : 0 : bSortByRows = aSortData.bByRow;
157 : 0 : bHasHeader = aSortData.bHasHeader;
158 : :
159 : 0 : if ( maSortKeyItems[0].aLbSort.GetEntryCount() == 0 )
160 : 0 : FillFieldLists(0);
161 : :
162 : : // ListBox selection:
163 : 0 : if ( aSortData.maKeyState[0].bDoSort )
164 : : {
165 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
166 : : {
167 : 0 : if ( aSortData.maKeyState[i].bDoSort )
168 : : {
169 : 0 : maSortKeyItems[i].aLbSort.SelectEntryPos( GetFieldSelPos(
170 : 0 : aSortData.maKeyState[i].nField ) );
171 : :
172 : 0 : (aSortData.maKeyState[i].bAscending)
173 : 0 : ? maSortKeyItems[i].aBtnUp.Check()
174 : 0 : : maSortKeyItems[i].aBtnDown.Check();
175 : : }
176 : : else
177 : : {
178 : 0 : maSortKeyItems[i].aLbSort.SelectEntryPos( 0 ); // Select none
179 : 0 : maSortKeyItems[i].aBtnUp.Check();
180 : : }
181 : : }
182 : :
183 : : // Enable or disable field depending on preceding Listbox selection
184 : 0 : maSortKeyItems[0].EnableField();
185 : 0 : for ( sal_uInt16 i=1; i<nSortKeyCount; i++ )
186 : 0 : if ( maSortKeyItems[i - 1].aLbSort.GetSelectEntryPos() == 0 )
187 : 0 : maSortKeyItems[i].DisableField();
188 : : else
189 : 0 : maSortKeyItems[i].EnableField();
190 : :
191 : : }
192 : : else
193 : : {
194 : 0 : SCCOL nCol = pViewData->GetCurX();
195 : :
196 : 0 : if( nCol < aSortData.nCol1 )
197 : 0 : nCol = aSortData.nCol1;
198 : 0 : else if( nCol > aSortData.nCol2 )
199 : 0 : nCol = aSortData.nCol2;
200 : :
201 : 0 : sal_uInt16 nSort1Pos = nCol - aSortData.nCol1+1;
202 : :
203 : 0 : maSortKeyItems[0].aLbSort.SelectEntryPos( nSort1Pos );
204 : 0 : for ( sal_uInt16 i=1; i<nSortKeyCount; i++ )
205 : 0 : maSortKeyItems[i].aLbSort.SelectEntryPos( 0 );
206 : :
207 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
208 : 0 : maSortKeyItems[i].aBtnUp.Check();
209 : :
210 : :
211 : 0 : maSortKeyItems[0].EnableField();
212 : 0 : maSortKeyItems[1].EnableField();
213 : 0 : for ( sal_uInt16 i=2; i<nSortKeyCount; i++ )
214 : 0 : maSortKeyItems[i].DisableField();
215 : : }
216 : :
217 : 0 : if ( pDlg )
218 : : {
219 : 0 : pDlg->SetByRows ( bSortByRows );
220 : 0 : pDlg->SetHeaders( bHasHeader );
221 : : }
222 : 0 : }
223 : :
224 : : // -----------------------------------------------------------------------
225 : :
226 : 0 : sal_Bool ScTabPageSortFields::FillItemSet( SfxItemSet& rArgSet )
227 : : {
228 : 0 : ScSortParam aNewSortData = aSortData;
229 : :
230 : 0 : if (pDlg)
231 : : {
232 : 0 : const SfxItemSet* pExample = pDlg->GetExampleSet();
233 : : const SfxPoolItem* pItem;
234 : 0 : if ( pExample && pExample->GetItemState( nWhichSort, sal_True, &pItem ) == SFX_ITEM_SET )
235 : : {
236 : 0 : ScSortParam aTempData = static_cast<const ScSortItem*>(pItem)->GetSortData();
237 : 0 : aTempData.maKeyState = aNewSortData.maKeyState;
238 : 0 : aNewSortData = aTempData;
239 : : }
240 : : }
241 : 0 : std::vector<sal_uInt16> nSortPos;
242 : :
243 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
244 : : {
245 : 0 : nSortPos.push_back( maSortKeyItems[i].aLbSort.GetSelectEntryPos() );
246 : :
247 : 0 : if ( nSortPos[i] == LISTBOX_ENTRY_NOTFOUND ) nSortPos[i] = 0;
248 : : }
249 : :
250 : 0 : if ( nSortPos[0] > 0 )
251 : : {
252 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
253 : 0 : aNewSortData.maKeyState[i].bDoSort = (nSortPos[i] > 0);
254 : :
255 : : // If the "OK" was selected on the Options page while the sort
256 : : // direction was changed, then the first field (i.e. nFieldArr[0])
257 : : // of the respective direction is chosen as the sorting criterion:
258 : 0 : if ( bSortByRows != pDlg->GetByRows() )
259 : : {
260 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
261 : 0 : aNewSortData.maKeyState[i].nField = ( bSortByRows ?
262 : : static_cast<SCCOLROW>(nFirstRow) :
263 : 0 : static_cast<SCCOLROW>(nFirstCol) );
264 : : }
265 : : else
266 : : {
267 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
268 : 0 : aNewSortData.maKeyState[i].nField = nFieldArr[nSortPos[i]];
269 : : }
270 : :
271 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
272 : 0 : aNewSortData.maKeyState[i].bAscending = maSortKeyItems[i].aBtnUp.IsChecked();
273 : :
274 : : // bHasHeader is in ScTabPageSortOptions::FillItemSet, where it belongs
275 : : }
276 : : else
277 : : {
278 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
279 : 0 : aNewSortData.maKeyState[i].bDoSort = false;
280 : : }
281 : :
282 : 0 : rArgSet.Put( ScSortItem( SCITEM_SORTDATA, NULL, &aNewSortData ) );
283 : :
284 : 0 : return sal_True;
285 : : }
286 : :
287 : : // -----------------------------------------------------------------------
288 : :
289 : : // for data exchange without dialogue detour: (still TODO!)
290 : 0 : void ScTabPageSortFields::ActivatePage()
291 : : {
292 : 0 : if ( pDlg )
293 : : {
294 : 0 : if ( bHasHeader != pDlg->GetHeaders()
295 : 0 : || bSortByRows != pDlg->GetByRows() )
296 : : {
297 : 0 : std::vector<sal_uInt16> nCurSel;
298 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
299 : 0 : nCurSel.push_back( maSortKeyItems[i].aLbSort.GetSelectEntryPos() );
300 : :
301 : 0 : bHasHeader = pDlg->GetHeaders();
302 : 0 : bSortByRows = pDlg->GetByRows();
303 : 0 : FillFieldLists(0);
304 : :
305 : 0 : for ( sal_uInt16 i=0; i<nSortKeyCount; i++ )
306 : 0 : maSortKeyItems[i].aLbSort.SelectEntryPos( nCurSel[i] );
307 : : }
308 : : }
309 : 0 : }
310 : :
311 : : // -----------------------------------------------------------------------
312 : :
313 : 0 : int ScTabPageSortFields::DeactivatePage( SfxItemSet* pSetP )
314 : : {
315 : 0 : if ( pDlg )
316 : : {
317 : 0 : if ( bHasHeader != pDlg->GetHeaders() )
318 : 0 : pDlg->SetHeaders( bHasHeader );
319 : :
320 : 0 : if ( bSortByRows != pDlg->GetByRows() )
321 : 0 : pDlg->SetByRows( bSortByRows );
322 : : }
323 : :
324 : 0 : if ( pSetP )
325 : 0 : FillItemSet( *pSetP );
326 : :
327 : 0 : return SfxTabPage::LEAVE_PAGE;
328 : : }
329 : :
330 : : // -----------------------------------------------------------------------
331 : :
332 : 0 : void ScTabPageSortFields::FillFieldLists( sal_uInt16 nStartField )
333 : : {
334 : 0 : if ( pViewData )
335 : : {
336 : 0 : ScDocument* pDoc = pViewData->GetDocument();
337 : :
338 : 0 : if ( pDoc )
339 : : {
340 : 0 : for ( sal_uInt16 i=nStartField; i<nSortKeyCount; i++ )
341 : : {
342 : 0 : maSortKeyItems[i].aLbSort.Clear();
343 : 0 : maSortKeyItems[i].aLbSort.InsertEntry( aStrUndefined, 0 );
344 : : }
345 : :
346 : 0 : SCCOL nFirstSortCol = aSortData.nCol1;
347 : 0 : SCROW nFirstSortRow = aSortData.nRow1;
348 : 0 : SCTAB nTab = pViewData->GetTabNo();
349 : 0 : sal_uInt16 i = 1;
350 : :
351 : 0 : if ( bSortByRows )
352 : : {
353 : 0 : rtl::OUString aFieldName;
354 : 0 : SCCOL nMaxCol = aSortData.nCol2;
355 : : SCCOL col;
356 : :
357 : 0 : for ( col=nFirstSortCol; col<=nMaxCol && i<SC_MAXFIELDS; col++ )
358 : : {
359 : 0 : pDoc->GetString( col, nFirstSortRow, nTab, aFieldName );
360 : 0 : if ( !bHasHeader || aFieldName.isEmpty() )
361 : : {
362 : 0 : rtl::OUStringBuffer aBuf;
363 : 0 : aBuf.append(aStrColumn);
364 : 0 : aFieldName = aBuf.makeStringAndClear().replaceAll("%1", ScColToAlpha( col ));
365 : : }
366 : 0 : nFieldArr.push_back( col );
367 : :
368 : 0 : for ( sal_uInt16 j=nStartField; j<nSortKeyCount; j++ )
369 : 0 : maSortKeyItems[j].aLbSort.InsertEntry( aFieldName, i );
370 : :
371 : 0 : i++;
372 : 0 : }
373 : : }
374 : : else
375 : : {
376 : 0 : rtl::OUString aFieldName;
377 : 0 : SCROW nMaxRow = aSortData.nRow2;
378 : : SCROW row;
379 : :
380 : 0 : for ( row=nFirstSortRow; row<=nMaxRow && i<SC_MAXFIELDS; row++ )
381 : : {
382 : 0 : pDoc->GetString( nFirstSortCol, row, nTab, aFieldName );
383 : 0 : if ( !bHasHeader || aFieldName.isEmpty() )
384 : : {
385 : 0 : rtl::OUStringBuffer aBuf;
386 : 0 : rtl::OUStringBuffer tempBuf;
387 : :
388 : 0 : aBuf.append(aStrRow);
389 : 0 : tempBuf.append(row+1 );
390 : 0 : aFieldName = aBuf.makeStringAndClear().replaceAll("%1", tempBuf.makeStringAndClear());
391 : : }
392 : 0 : nFieldArr.push_back( row );
393 : :
394 : 0 : for ( sal_uInt16 j=nStartField; j<nSortKeyCount; j++ )
395 : 0 : maSortKeyItems[j].aLbSort.InsertEntry( aFieldName, i );
396 : :
397 : 0 : i++;
398 : 0 : }
399 : : }
400 : 0 : nFieldCount = i;
401 : : }
402 : : }
403 : 0 : }
404 : :
405 : : //------------------------------------------------------------------------
406 : :
407 : 0 : sal_uInt16 ScTabPageSortFields::GetFieldSelPos( SCCOLROW nField )
408 : : {
409 : 0 : sal_uInt16 nFieldPos = 0;
410 : 0 : sal_Bool bFound = false;
411 : :
412 : 0 : for ( sal_uInt16 n=1; n<nFieldCount && !bFound; n++ )
413 : : {
414 : 0 : if ( nFieldArr[n] == nField )
415 : : {
416 : 0 : nFieldPos = n;
417 : 0 : bFound = sal_True;
418 : : }
419 : : }
420 : :
421 : 0 : return nFieldPos;
422 : : }
423 : :
424 : : // -----------------------------------------------------------------------
425 : : // Handler:
426 : : //---------
427 : :
428 : 0 : IMPL_LINK( ScTabPageSortFields, SelectHdl, ListBox *, pLb )
429 : : {
430 : 0 : rtl::OUString aSelEntry = pLb->GetSelectEntry();
431 : 0 : ScSortKeyItems::iterator pIter;
432 : 0 : sal_uInt16 nSortKeyIndex = nSortKeyCount;
433 : :
434 : : // If last listbox is enabled add one item
435 : 0 : if ( &maSortKeyItems.back().aLbSort == pLb )
436 : 0 : if ( aSelEntry != aStrUndefined )
437 : : {
438 : : // Extend local SortParam copy
439 : 0 : const ScSortKeyState atempKeyState = { false, 0, true };
440 : 0 : aSortData.maKeyState.push_back( atempKeyState );
441 : :
442 : : // Add Sort Key Item
443 : 0 : ++nSortKeyCount;
444 : 0 : maSortKeyCtrl.AddSortKey( nSortKeyCount );
445 : 0 : maSortKeyItems[nSortKeyIndex].aLbSort.SetSelectHdl( LINK( this, ScTabPageSortFields, SelectHdl ) );
446 : :
447 : 0 : FillFieldLists( nSortKeyIndex );
448 : :
449 : : // Set Status
450 : 0 : maSortKeyItems[nSortKeyIndex].aBtnUp.Check();
451 : 0 : maSortKeyItems[nSortKeyIndex].aLbSort.SelectEntryPos( 0 );
452 : 0 : return 0;
453 : : }
454 : :
455 : : // Find selected listbox
456 : 0 : for ( pIter = maSortKeyItems.begin(); pIter != maSortKeyItems.end(); ++pIter )
457 : : {
458 : 0 : if ( &pIter->aLbSort == pLb ) break;
459 : : }
460 : :
461 : : // If not selecting the last Listbox, modify the succeeding ones
462 : 0 : ++pIter;
463 : 0 : if ( std::distance(maSortKeyItems.begin(), pIter) < nSortKeyCount )
464 : : {
465 : 0 : if ( aSelEntry == aStrUndefined )
466 : : {
467 : 0 : for ( ; pIter != maSortKeyItems.end(); ++pIter )
468 : : {
469 : 0 : pIter->aLbSort.SelectEntryPos( 0 );
470 : :
471 : 0 : if ( pIter->aFlSort.IsEnabled() )
472 : 0 : pIter->DisableField();
473 : : }
474 : : }
475 : : else
476 : : {
477 : 0 : if ( !pIter->aFlSort.IsEnabled() )
478 : 0 : pIter->EnableField();
479 : : }
480 : : }
481 : 0 : return 0;
482 : : }
483 : :
484 : : //========================================================================
485 : : // Sort option Tab Page:
486 : : //========================================================================
487 : :
488 : 0 : ScTabPageSortOptions::ScTabPageSortOptions( Window* pParent,
489 : : const SfxItemSet& rArgSet )
490 : :
491 : : : SfxTabPage ( pParent,
492 : : ScResId( RID_SCPAGE_SORT_OPTIONS ),
493 : : rArgSet ),
494 : : //
495 : : aBtnCase ( this, ScResId( BTN_CASESENSITIVE ) ),
496 : : aBtnHeader ( this, ScResId( BTN_LABEL ) ),
497 : : aBtnFormats ( this, ScResId( BTN_FORMATS ) ),
498 : : aBtnNaturalSort ( this, ScResId( BTN_NATURALSORT ) ),
499 : : aBtnCopyResult ( this, ScResId( BTN_COPYRESULT ) ),
500 : : aLbOutPos ( this, ScResId( LB_OUTAREA ) ),
501 : : aEdOutPos ( this, ScResId( ED_OUTAREA ) ),
502 : : aBtnSortUser ( this, ScResId( BTN_SORT_USER ) ),
503 : : aLbSortUser ( this, ScResId( LB_SORT_USER ) ),
504 : : aFtLanguage ( this, ScResId( FT_LANGUAGE ) ),
505 : : aLbLanguage ( this, ScResId( LB_LANGUAGE ) ),
506 : : aFtAlgorithm ( this, ScResId( FT_ALGORITHM ) ),
507 : : aLbAlgorithm ( this, ScResId( LB_ALGORITHM ) ),
508 : : aLineDirection ( this, ScResId( FL_DIRECTION ) ),
509 : : aBtnTopDown ( this, ScResId( BTN_TOP_DOWN ) ),
510 : : aBtnLeftRight ( this, ScResId( BTN_LEFT_RIGHT ) ),
511 : : //
512 : : aStrRowLabel ( SC_RESSTR( STR_ROW_LABEL ) ),
513 : : aStrColLabel ( SC_RESSTR( STR_COL_LABEL ) ),
514 : : aStrUndefined ( SC_RESSTR( SCSTR_UNDEFINED ) ),
515 : : //
516 : 0 : nWhichSort ( rArgSet.GetPool()->GetWhich( SID_SORT ) ),
517 : : aSortData ( ((const ScSortItem&)
518 : 0 : rArgSet.Get( nWhichSort )).GetSortData() ),
519 : : pViewData ( NULL ),
520 : : pDoc ( NULL ),
521 : 0 : pDlg ( (ScSortDlg*)(GetParent() ? GetParent()->GetParent() : 0 ) ),
522 : : pColRes ( NULL ),
523 : 0 : pColWrap ( NULL )
524 : : {
525 : 0 : Init();
526 : 0 : FreeResource();
527 : 0 : SetExchangeSupport();
528 : :
529 : 0 : aLbOutPos.SetAccessibleRelationLabeledBy(&aBtnCopyResult);
530 : 0 : aLbOutPos.SetAccessibleName(aBtnCopyResult.GetText());
531 : 0 : aEdOutPos.SetAccessibleRelationLabeledBy(&aBtnCopyResult);
532 : 0 : aEdOutPos.SetAccessibleName(aBtnCopyResult.GetText());
533 : 0 : aLbSortUser.SetAccessibleRelationLabeledBy(&aBtnSortUser);
534 : 0 : aLbSortUser.SetAccessibleName(aBtnSortUser.GetText());
535 : 0 : }
536 : :
537 : : // -----------------------------------------------------------------------
538 : :
539 : 0 : ScTabPageSortOptions::~ScTabPageSortOptions()
540 : : {
541 : 0 : sal_uInt16 nEntries = aLbOutPos.GetEntryCount();
542 : :
543 : 0 : for ( sal_uInt16 i=1; i<nEntries; i++ )
544 : 0 : delete (rtl::OUString*)aLbOutPos.GetEntryData( i );
545 : :
546 : 0 : delete pColRes;
547 : 0 : delete pColWrap; //! not if from document
548 : 0 : }
549 : :
550 : : // -----------------------------------------------------------------------
551 : :
552 : 0 : void ScTabPageSortOptions::Init()
553 : : {
554 : : // CollatorResource has user-visible names for sort algorithms
555 : 0 : pColRes = new CollatorResource();
556 : :
557 : : //! use CollatorWrapper from document?
558 : 0 : pColWrap = new CollatorWrapper( comphelper::getProcessServiceFactory() );
559 : :
560 : : const ScSortItem& rSortItem = (const ScSortItem&)
561 : 0 : GetItemSet().Get( nWhichSort );
562 : :
563 : 0 : aLbOutPos.SetSelectHdl ( LINK( this, ScTabPageSortOptions, SelOutPosHdl ) );
564 : 0 : aBtnCopyResult.SetClickHdl( LINK( this, ScTabPageSortOptions, EnableHdl ) );
565 : 0 : aBtnSortUser.SetClickHdl ( LINK( this, ScTabPageSortOptions, EnableHdl ) );
566 : 0 : aBtnTopDown.SetClickHdl ( LINK( this, ScTabPageSortOptions, SortDirHdl ) );
567 : 0 : aBtnLeftRight.SetClickHdl ( LINK( this, ScTabPageSortOptions, SortDirHdl ) );
568 : 0 : aLbLanguage.SetSelectHdl ( LINK( this, ScTabPageSortOptions, FillAlgorHdl ) );
569 : :
570 : 0 : pViewData = rSortItem.GetViewData();
571 : 0 : pDoc = pViewData ? pViewData->GetDocument() : NULL;
572 : :
573 : : OSL_ENSURE( pViewData, "ViewData not found! :-/" );
574 : :
575 : :
576 : 0 : if ( pViewData && pDoc )
577 : : {
578 : 0 : String theArea;
579 : 0 : ScDBCollection* pDBColl = pDoc->GetDBCollection();
580 : 0 : rtl::OUString theDbArea;
581 : 0 : const SCTAB nCurTab = pViewData->GetTabNo();
582 : 0 : rtl::OUString theDbName = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(STR_DB_LOCAL_NONAME));
583 : 0 : const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
584 : :
585 : 0 : aLbOutPos.Clear();
586 : 0 : aLbOutPos.InsertEntry( aStrUndefined, 0 );
587 : 0 : aLbOutPos.Disable();
588 : :
589 : 0 : ScAreaNameIterator aIter( pDoc );
590 : 0 : rtl::OUString aName;
591 : 0 : ScRange aRange;
592 : 0 : rtl::OUString aRefStr;
593 : 0 : while ( aIter.Next( aName, aRange ) )
594 : : {
595 : 0 : sal_uInt16 nInsert = aLbOutPos.InsertEntry( aName );
596 : :
597 : 0 : aRange.aStart.Format( aRefStr, SCA_ABS_3D, pDoc, eConv );
598 : 0 : aLbOutPos.SetEntryData( nInsert, new rtl::OUString( aRefStr ) );
599 : : }
600 : :
601 : 0 : aLbOutPos.SelectEntryPos( 0 );
602 : 0 : aEdOutPos.SetText( EMPTY_STRING );
603 : :
604 : : // Check whether the field that is passed on is a database field:
605 : :
606 : 0 : ScAddress aScAddress( aSortData.nCol1, aSortData.nRow1, nCurTab );
607 : : ScRange( aScAddress,
608 : : ScAddress( aSortData.nCol2, aSortData.nRow2, nCurTab )
609 : 0 : ).Format( theArea, SCR_ABS, pDoc, eConv );
610 : :
611 : 0 : if ( pDBColl )
612 : : {
613 : : ScDBData* pDBData
614 : : = pDBColl->GetDBAtArea( nCurTab,
615 : : aSortData.nCol1, aSortData.nRow1,
616 : 0 : aSortData.nCol2, aSortData.nRow2 );
617 : 0 : if ( pDBData )
618 : : {
619 : 0 : theDbName = pDBData->GetName();
620 : 0 : aBtnHeader.Check( pDBData->HasHeader() );
621 : : }
622 : : }
623 : :
624 : 0 : theArea.AppendAscii(RTL_CONSTASCII_STRINGPARAM(" ("));
625 : 0 : theArea += theDbName;
626 : 0 : theArea += ')';
627 : :
628 : 0 : aBtnHeader.SetText( aStrColLabel );
629 : : }
630 : :
631 : 0 : FillUserSortListBox();
632 : :
633 : : // get available languages
634 : :
635 : 0 : aLbLanguage.SetLanguageList( LANG_LIST_ALL | LANG_LIST_ONLY_KNOWN, false );
636 : 0 : aLbLanguage.InsertLanguage( LANGUAGE_SYSTEM );
637 : 0 : }
638 : :
639 : : // -----------------------------------------------------------------------
640 : :
641 : 0 : SfxTabPage* ScTabPageSortOptions::Create(
642 : : Window* pParent,
643 : : const SfxItemSet& rArgSet )
644 : : {
645 : 0 : return ( new ScTabPageSortOptions( pParent, rArgSet ) );
646 : : }
647 : :
648 : : // -----------------------------------------------------------------------
649 : :
650 : 0 : void ScTabPageSortOptions::Reset( const SfxItemSet& /* rArgSet */ )
651 : : {
652 : 0 : if ( aSortData.bUserDef )
653 : : {
654 : 0 : aBtnSortUser.Check( sal_True );
655 : 0 : aLbSortUser.Enable();
656 : 0 : aLbSortUser.SelectEntryPos( aSortData.nUserIndex );
657 : : }
658 : : else
659 : : {
660 : 0 : aBtnSortUser.Check( false );
661 : 0 : aLbSortUser.Disable();
662 : 0 : aLbSortUser.SelectEntryPos( 0 );
663 : : }
664 : :
665 : 0 : aBtnCase.Check ( aSortData.bCaseSens );
666 : 0 : aBtnFormats.Check ( aSortData.bIncludePattern );
667 : 0 : aBtnHeader.Check ( aSortData.bHasHeader );
668 : 0 : aBtnNaturalSort.Check ( aSortData.bNaturalSort );
669 : :
670 : 0 : if ( aSortData.bByRow )
671 : : {
672 : 0 : aBtnTopDown.Check();
673 : 0 : aBtnHeader.SetText( aStrColLabel );
674 : : }
675 : : else
676 : : {
677 : 0 : aBtnLeftRight.Check();
678 : 0 : aBtnHeader.SetText( aStrRowLabel );
679 : : }
680 : :
681 : 0 : LanguageType eLang = MsLangId::convertLocaleToLanguage( aSortData.aCollatorLocale );
682 : 0 : if ( eLang == LANGUAGE_DONTKNOW )
683 : 0 : eLang = LANGUAGE_SYSTEM;
684 : 0 : aLbLanguage.SelectLanguage( eLang );
685 : 0 : FillAlgorHdl( &aLbLanguage ); // get algorithms, select default
686 : 0 : if ( !aSortData.aCollatorAlgorithm.isEmpty() )
687 : 0 : aLbAlgorithm.SelectEntry( pColRes->GetTranslation( aSortData.aCollatorAlgorithm ) );
688 : :
689 : 0 : if ( pDoc && !aSortData.bInplace )
690 : : {
691 : 0 : rtl::OUString aStr;
692 : 0 : sal_uInt16 nFormat = (aSortData.nDestTab != pViewData->GetTabNo())
693 : : ? SCR_ABS_3D
694 : 0 : : SCR_ABS;
695 : :
696 : : theOutPos.Set( aSortData.nDestCol,
697 : : aSortData.nDestRow,
698 : 0 : aSortData.nDestTab );
699 : :
700 : 0 : theOutPos.Format( aStr, nFormat, pDoc, pDoc->GetAddressConvention() );
701 : 0 : aBtnCopyResult.Check();
702 : 0 : aLbOutPos.Enable();
703 : 0 : aEdOutPos.Enable();
704 : 0 : aEdOutPos.SetText( aStr );
705 : 0 : EdOutPosModHdl( &aEdOutPos );
706 : 0 : aEdOutPos.GrabFocus();
707 : 0 : aEdOutPos.SetSelection( Selection( 0, SELECTION_MAX ) );
708 : : }
709 : : else
710 : : {
711 : 0 : aBtnCopyResult.Check( false );
712 : 0 : aLbOutPos.Disable();
713 : 0 : aEdOutPos.Disable();
714 : 0 : aEdOutPos.SetText( EMPTY_STRING );
715 : : }
716 : 0 : }
717 : :
718 : : // -----------------------------------------------------------------------
719 : :
720 : 0 : sal_Bool ScTabPageSortOptions::FillItemSet( SfxItemSet& rArgSet )
721 : : {
722 : : // Create local copy of ScParam
723 : 0 : ScSortParam aNewSortData = aSortData;
724 : :
725 : 0 : if (pDlg)
726 : : {
727 : 0 : const SfxItemSet* pExample = pDlg->GetExampleSet();
728 : : const SfxPoolItem* pItem;
729 : 0 : if ( pExample && pExample->GetItemState( nWhichSort, sal_True, &pItem ) == SFX_ITEM_SET )
730 : 0 : aNewSortData = static_cast<const ScSortItem*>(pItem)->GetSortData();
731 : : }
732 : 0 : aNewSortData.bByRow = aBtnTopDown.IsChecked();
733 : 0 : aNewSortData.bHasHeader = aBtnHeader.IsChecked();
734 : 0 : aNewSortData.bCaseSens = aBtnCase.IsChecked();
735 : 0 : aNewSortData.bNaturalSort = aBtnNaturalSort.IsChecked();
736 : 0 : aNewSortData.bIncludePattern = aBtnFormats.IsChecked();
737 : 0 : aNewSortData.bInplace = !aBtnCopyResult.IsChecked();
738 : 0 : aNewSortData.nDestCol = theOutPos.Col();
739 : 0 : aNewSortData.nDestRow = theOutPos.Row();
740 : 0 : aNewSortData.nDestTab = theOutPos.Tab();
741 : 0 : aNewSortData.bUserDef = aBtnSortUser.IsChecked();
742 : 0 : aNewSortData.nUserIndex = (aBtnSortUser.IsChecked())
743 : 0 : ? aLbSortUser.GetSelectEntryPos()
744 : 0 : : 0;
745 : :
746 : : // get locale
747 : 0 : LanguageType eLang = aLbLanguage.GetSelectLanguage();
748 : 0 : aNewSortData.aCollatorLocale = MsLangId::convertLanguageToLocale( eLang, false );
749 : :
750 : : // get algorithm
751 : 0 : rtl::OUString sAlg;
752 : 0 : if ( eLang != LANGUAGE_SYSTEM )
753 : : {
754 : : uno::Sequence<rtl::OUString> aAlgos = pColWrap->listCollatorAlgorithms(
755 : 0 : aNewSortData.aCollatorLocale );
756 : 0 : sal_uInt16 nSel = aLbAlgorithm.GetSelectEntryPos();
757 : 0 : if ( nSel < aAlgos.getLength() )
758 : 0 : sAlg = aAlgos[nSel];
759 : : }
760 : 0 : aNewSortData.aCollatorAlgorithm = sAlg;
761 : :
762 : 0 : rArgSet.Put( ScSortItem( SCITEM_SORTDATA, &aNewSortData ) );
763 : :
764 : 0 : return sal_True;
765 : : }
766 : :
767 : : // -----------------------------------------------------------------------
768 : :
769 : : // for data exchange without dialogue detour: (still TODO!)
770 : 0 : void ScTabPageSortOptions::ActivatePage()
771 : : {
772 : 0 : if ( pDlg )
773 : : {
774 : 0 : if ( aBtnHeader.IsChecked() != pDlg->GetHeaders() )
775 : : {
776 : 0 : aBtnHeader.Check( pDlg->GetHeaders() );
777 : : }
778 : :
779 : 0 : if ( aBtnTopDown.IsChecked() != pDlg->GetByRows() )
780 : : {
781 : 0 : aBtnTopDown.Check( pDlg->GetByRows() );
782 : 0 : aBtnLeftRight.Check( !pDlg->GetByRows() );
783 : : }
784 : :
785 : 0 : aBtnHeader.SetText( (pDlg->GetByRows())
786 : : ? aStrColLabel
787 : 0 : : aStrRowLabel );
788 : : }
789 : 0 : }
790 : :
791 : : // -----------------------------------------------------------------------
792 : :
793 : 0 : int ScTabPageSortOptions::DeactivatePage( SfxItemSet* pSetP )
794 : : {
795 : 0 : sal_Bool bPosInputOk = sal_True;
796 : :
797 : 0 : if ( aBtnCopyResult.IsChecked() )
798 : : {
799 : 0 : String thePosStr = aEdOutPos.GetText();
800 : 0 : ScAddress thePos;
801 : 0 : xub_StrLen nColonPos = thePosStr.Search( ':' );
802 : :
803 : 0 : if ( STRING_NOTFOUND != nColonPos )
804 : 0 : thePosStr.Erase( nColonPos );
805 : :
806 : 0 : if ( pViewData )
807 : : {
808 : : // visible table is default for input without table
809 : : // must be changed to GetRefTabNo when sorting has RefInput!
810 : 0 : thePos.SetTab( pViewData->GetTabNo() );
811 : : }
812 : :
813 : 0 : sal_uInt16 nResult = thePos.Parse( thePosStr, pDoc, pDoc->GetAddressConvention() );
814 : :
815 : 0 : bPosInputOk = ( SCA_VALID == (nResult & SCA_VALID) );
816 : :
817 : 0 : if ( !bPosInputOk )
818 : : {
819 : : ErrorBox( this, WinBits( WB_OK | WB_DEF_OK ),
820 : 0 : ScGlobal::GetRscString( STR_INVALID_TABREF )
821 : 0 : ).Execute();
822 : 0 : aEdOutPos.GrabFocus();
823 : 0 : aEdOutPos.SetSelection( Selection( 0, SELECTION_MAX ) );
824 : 0 : theOutPos.Set(0,0,0);
825 : : }
826 : : else
827 : : {
828 : 0 : aEdOutPos.SetText( thePosStr );
829 : 0 : theOutPos = thePos;
830 : 0 : }
831 : : }
832 : :
833 : 0 : if ( pDlg && bPosInputOk )
834 : : {
835 : 0 : pDlg->SetHeaders( aBtnHeader.IsChecked() );
836 : 0 : pDlg->SetByRows ( aBtnTopDown.IsChecked() );
837 : : }
838 : :
839 : 0 : if ( pSetP && bPosInputOk )
840 : 0 : FillItemSet( *pSetP );
841 : :
842 : 0 : return bPosInputOk ? SfxTabPage::LEAVE_PAGE : SfxTabPage::KEEP_PAGE;
843 : : }
844 : :
845 : : // -----------------------------------------------------------------------
846 : :
847 : 0 : void ScTabPageSortOptions::FillUserSortListBox()
848 : : {
849 : 0 : ScUserList* pUserLists = ScGlobal::GetUserList();
850 : :
851 : 0 : aLbSortUser.Clear();
852 : 0 : if ( pUserLists )
853 : : {
854 : 0 : size_t nCount = pUserLists->size();
855 : 0 : if ( nCount > 0 )
856 : 0 : for ( size_t i=0; i<nCount; ++i )
857 : 0 : aLbSortUser.InsertEntry( (*pUserLists)[i]->GetString() );
858 : : }
859 : 0 : }
860 : :
861 : : // -----------------------------------------------------------------------
862 : : // Handler:
863 : :
864 : 0 : IMPL_LINK( ScTabPageSortOptions, EnableHdl, CheckBox *, pBox )
865 : : {
866 : 0 : if ( pBox == &aBtnCopyResult )
867 : : {
868 : 0 : if ( pBox->IsChecked() )
869 : : {
870 : 0 : aLbOutPos.Enable();
871 : 0 : aEdOutPos.Enable();
872 : 0 : aEdOutPos.GrabFocus();
873 : : }
874 : : else
875 : : {
876 : 0 : aLbOutPos.Disable();
877 : 0 : aEdOutPos.Disable();
878 : : }
879 : : }
880 : 0 : else if ( pBox == &aBtnSortUser )
881 : : {
882 : 0 : if ( pBox->IsChecked() )
883 : : {
884 : 0 : aLbSortUser.Enable();
885 : 0 : aLbSortUser.GrabFocus();
886 : : }
887 : : else
888 : 0 : aLbSortUser.Disable();
889 : : }
890 : 0 : return 0;
891 : : }
892 : :
893 : : // -----------------------------------------------------------------------
894 : :
895 : 0 : IMPL_LINK( ScTabPageSortOptions, SelOutPosHdl, ListBox *, pLb )
896 : : {
897 : 0 : if ( pLb == &aLbOutPos )
898 : : {
899 : 0 : rtl::OUString aString;
900 : 0 : sal_uInt16 nSelPos = aLbOutPos.GetSelectEntryPos();
901 : :
902 : 0 : if ( nSelPos > 0 )
903 : 0 : aString = *(String*)aLbOutPos.GetEntryData( nSelPos );
904 : :
905 : 0 : aEdOutPos.SetText( aString );
906 : : }
907 : 0 : return 0;
908 : : }
909 : :
910 : : // -----------------------------------------------------------------------
911 : :
912 : 0 : IMPL_LINK( ScTabPageSortOptions, SortDirHdl, RadioButton *, pBtn )
913 : : {
914 : 0 : if ( pBtn == &aBtnTopDown )
915 : : {
916 : 0 : aBtnHeader.SetText( aStrColLabel );
917 : : }
918 : 0 : else if ( pBtn == &aBtnLeftRight )
919 : : {
920 : 0 : aBtnHeader.SetText( aStrRowLabel );
921 : : }
922 : 0 : return 0;
923 : : }
924 : :
925 : : // -----------------------------------------------------------------------
926 : :
927 : 0 : void ScTabPageSortOptions::EdOutPosModHdl( Edit* pEd )
928 : : {
929 : 0 : if ( pEd == &aEdOutPos )
930 : : {
931 : 0 : String theCurPosStr = aEdOutPos.GetText();
932 : 0 : sal_uInt16 nResult = ScAddress().Parse( theCurPosStr, pDoc, pDoc->GetAddressConvention() );
933 : :
934 : 0 : if ( SCA_VALID == (nResult & SCA_VALID) )
935 : : {
936 : 0 : String* pStr = NULL;
937 : 0 : sal_Bool bFound = false;
938 : 0 : sal_uInt16 i = 0;
939 : 0 : sal_uInt16 nCount = aLbOutPos.GetEntryCount();
940 : :
941 : 0 : for ( i=2; i<nCount && !bFound; i++ )
942 : : {
943 : 0 : pStr = (String*)aLbOutPos.GetEntryData( i );
944 : 0 : bFound = (theCurPosStr == *pStr);
945 : : }
946 : :
947 : 0 : if ( bFound )
948 : 0 : aLbOutPos.SelectEntryPos( --i );
949 : : else
950 : 0 : aLbOutPos.SelectEntryPos( 0 );
951 : 0 : }
952 : : }
953 : 0 : }
954 : :
955 : : // -----------------------------------------------------------------------
956 : :
957 : 0 : IMPL_LINK_NOARG(ScTabPageSortOptions, FillAlgorHdl)
958 : : {
959 : 0 : aLbAlgorithm.SetUpdateMode( false );
960 : 0 : aLbAlgorithm.Clear();
961 : :
962 : 0 : LanguageType eLang = aLbLanguage.GetSelectLanguage();
963 : 0 : if ( eLang == LANGUAGE_SYSTEM )
964 : : {
965 : : // for LANGUAGE_SYSTEM no algorithm can be selected because
966 : : // it wouldn't necessarily exist for other languages
967 : : // -> leave list box empty if LANGUAGE_SYSTEM is selected
968 : 0 : aFtAlgorithm.Enable( false ); // nothing to select
969 : 0 : aLbAlgorithm.Enable( false ); // nothing to select
970 : : }
971 : : else
972 : : {
973 : 0 : lang::Locale aLocale( MsLangId::convertLanguageToLocale( eLang ));
974 : 0 : uno::Sequence<rtl::OUString> aAlgos = pColWrap->listCollatorAlgorithms( aLocale );
975 : :
976 : 0 : long nCount = aAlgos.getLength();
977 : 0 : const rtl::OUString* pArray = aAlgos.getConstArray();
978 : 0 : for (long i=0; i<nCount; i++)
979 : : {
980 : 0 : String sAlg = pArray[i];
981 : 0 : String sUser = pColRes->GetTranslation( sAlg );
982 : 0 : aLbAlgorithm.InsertEntry( sUser, LISTBOX_APPEND );
983 : 0 : }
984 : 0 : aLbAlgorithm.SelectEntryPos( 0 ); // first entry is default
985 : 0 : aFtAlgorithm.Enable( nCount > 1 ); // enable only if there is a choice
986 : 0 : aLbAlgorithm.Enable( nCount > 1 ); // enable only if there is a choice
987 : : }
988 : :
989 : 0 : aLbAlgorithm.SetUpdateMode( sal_True );
990 : 0 : return 0;
991 : : }
992 : :
993 : :
994 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|