Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "srtdlg.hxx"
21 :
22 : #include <vcl/msgbox.hxx>
23 : #include <svl/intitem.hxx>
24 : #include <svl/eitem.hxx>
25 : #include <sfx2/dispatch.hxx>
26 : #include <svx/svxids.hrc>
27 : #include <editeng/unolingu.hxx>
28 : #include <svx/svxdlg.hxx>
29 : #include <svx/dialogs.hrc>
30 : #include <unotools/collatorwrapper.hxx>
31 : #include <svtools/collatorres.hxx>
32 : #include <swwait.hxx>
33 : #include <view.hxx>
34 : #include <cmdid.h>
35 : #include <wrtsh.hxx>
36 : #include <misc.hrc>
37 : #include <app.hrc>
38 : #include <swtable.hxx>
39 : #include <node.hxx>
40 : #include <tblsel.hxx>
41 : #include <sfx2/request.hxx>
42 :
43 : static sal_Bool bCheck1 = sal_True;
44 : static sal_Bool bCheck2 = sal_False;
45 : static sal_Bool bCheck3 = sal_False;
46 :
47 : static sal_uInt16 nCol1 = 1;
48 : static sal_uInt16 nCol2 = 1;
49 : static sal_uInt16 nCol3 = 1;
50 :
51 : static sal_uInt16 nType1 = 0;
52 : static sal_uInt16 nType2 = 0;
53 : static sal_uInt16 nType3 = 0;
54 :
55 : static sal_uInt16 nLang = LANGUAGE_NONE;
56 :
57 : static sal_Bool bAsc1 = sal_True;
58 : static sal_Bool bAsc2 = sal_True;
59 : static sal_Bool bAsc3 = sal_True;
60 : static sal_Bool bCol = sal_False;
61 : static sal_Bool bCsSens= sal_False;
62 :
63 : static sal_Unicode cDeli = '\t';
64 :
65 : using namespace ::com::sun::star::lang;
66 : using namespace ::com::sun::star::uno;
67 : using namespace ::com::sun::star;
68 :
69 0 : static void lcl_ClearLstBoxAndDelUserData( ListBox& rLstBox )
70 : {
71 : void* pDel;
72 0 : for( sal_uInt16 n = 0, nEnd = rLstBox.GetEntryCount(); n < nEnd; ++n )
73 0 : if( 0 != ( pDel = rLstBox.GetEntryData( n )) )
74 0 : delete (String*)pDel;
75 0 : rLstBox.Clear();
76 0 : }
77 :
78 : /*--------------------------------------------------------------------
79 : Description: determine lines and columns for table selection
80 : --------------------------------------------------------------------*/
81 0 : static bool lcl_GetSelTbl( SwWrtShell &rSh, sal_uInt16& rX, sal_uInt16& rY )
82 : {
83 0 : const SwTableNode* pTblNd = rSh.IsCrsrInTbl();
84 0 : if( !pTblNd )
85 0 : return false;
86 :
87 0 : _FndBox aFndBox( 0, 0 );
88 :
89 : // look for all boxes / lines
90 : {
91 0 : SwSelBoxes aSelBoxes;
92 0 : ::GetTblSel( rSh, aSelBoxes );
93 0 : _FndPara aPara( aSelBoxes, &aFndBox );
94 0 : const SwTable& rTbl = pTblNd->GetTable();
95 0 : ForEach_FndLineCopyCol( (SwTableLines&)rTbl.GetTabLines(), &aPara );
96 : }
97 0 : rX = aFndBox.GetLines().size();
98 0 : if( !rX )
99 0 : return false;
100 :
101 0 : rY = aFndBox.GetLines().front().GetBoxes().size();
102 0 : return true;
103 : }
104 :
105 : /*--------------------------------------------------------------------
106 : Description: init list
107 : --------------------------------------------------------------------*/
108 0 : SwSortDlg::SwSortDlg(Window* pParent, SwWrtShell &rShell)
109 : : SvxStandardDialog(pParent, "SortDialog", "modules/swriter/ui/sortdialog.ui")
110 : , aColTxt(SW_RES(STR_COL))
111 : , aRowTxt(SW_RES(STR_ROW))
112 : , aNumericTxt(SW_RES(STR_NUMERIC))
113 : , rSh(rShell)
114 : , pColRes(0)
115 : , nX(99)
116 0 : , nY(99)
117 : {
118 0 : get(m_pColLbl, "column");
119 0 : get(m_pTypLbl, "keytype");
120 :
121 0 : get(m_pKeyCB1, "key1");
122 0 : get(m_pColEdt1, "colsb1");
123 0 : get(m_pTypDLB1, "typelb1");
124 0 : get(m_pSortUp1RB, "up1");
125 0 : get(m_pSortDn1RB, "down1");
126 :
127 0 : get(m_pKeyCB2, "key2");
128 0 : get(m_pColEdt2, "colsb2");
129 0 : get(m_pTypDLB2, "typelb2");
130 0 : get(m_pSortUp2RB, "up2");
131 0 : get(m_pSortDn2RB, "down2");
132 :
133 0 : get(m_pKeyCB3, "key3");
134 0 : get(m_pColEdt3, "colsb3");
135 0 : get(m_pTypDLB3, "typelb3");
136 0 : get(m_pSortUp3RB, "up3");
137 0 : get(m_pSortDn3RB, "down3");
138 :
139 0 : get(m_pColumnRB, "columns");
140 0 : get(m_pRowRB, "rows");
141 :
142 0 : get(m_pDelimTabRB, "tabs");
143 0 : get(m_pDelimFreeRB, "character");
144 0 : get(m_pDelimEdt, "separator");
145 0 : get(m_pDelimPB, "delimpb");
146 :
147 0 : get(m_pLangLB, "langlb");
148 0 : get(m_pCaseCB, "matchcase");
149 :
150 0 : m_pColEdt1->SetAccessibleName(m_pColLbl->GetText());
151 0 : m_pColEdt2->SetAccessibleName(m_pColLbl->GetText());
152 0 : m_pColEdt3->SetAccessibleName(m_pColLbl->GetText());
153 0 : m_pTypDLB1->SetAccessibleName(m_pTypLbl->GetText());
154 0 : m_pTypDLB2->SetAccessibleName(m_pTypLbl->GetText());
155 0 : m_pTypDLB3->SetAccessibleName(m_pTypLbl->GetText());
156 0 : m_pSortUp1RB->SetAccessibleRelationMemberOf(m_pKeyCB1);
157 0 : m_pSortDn1RB->SetAccessibleRelationMemberOf(m_pKeyCB1);
158 0 : m_pSortUp2RB->SetAccessibleRelationMemberOf(m_pKeyCB2);
159 0 : m_pSortDn2RB->SetAccessibleRelationMemberOf(m_pKeyCB2);
160 0 : m_pSortUp3RB->SetAccessibleRelationMemberOf(m_pKeyCB3);
161 0 : m_pSortDn3RB->SetAccessibleRelationMemberOf(m_pKeyCB3);
162 :
163 0 : m_pDelimEdt->SetMaxTextLen( 1 );
164 0 : if(rSh.GetSelectionType() &
165 : (nsSelectionType::SEL_TBL|nsSelectionType::SEL_TBL_CELLS) )
166 : {
167 0 : m_pColumnRB->Check(bCol);
168 0 : m_pColLbl->SetText(bCol ? aRowTxt : aColTxt);
169 0 : m_pRowRB->Check(!bCol);
170 0 : m_pDelimTabRB->Enable(sal_False);
171 0 : m_pDelimFreeRB->Enable(sal_False);
172 0 : m_pDelimEdt->Enable(sal_False);
173 : }
174 : else
175 : {
176 0 : m_pColumnRB->Enable(sal_False);
177 0 : m_pRowRB->Check(sal_True);
178 0 : m_pColLbl->SetText(aColTxt);
179 : }
180 :
181 : // initialise
182 0 : Link aLk = LINK(this,SwSortDlg, CheckHdl);
183 0 : m_pKeyCB1->SetClickHdl( aLk );
184 0 : m_pKeyCB2->SetClickHdl( aLk );
185 0 : m_pKeyCB3->SetClickHdl( aLk );
186 0 : m_pColumnRB->SetClickHdl( aLk );
187 0 : m_pRowRB->SetClickHdl( aLk );
188 :
189 0 : aLk = LINK(this,SwSortDlg, DelimHdl);
190 0 : m_pDelimFreeRB->SetClickHdl(aLk);
191 0 : m_pDelimTabRB->SetClickHdl(aLk);
192 :
193 0 : m_pDelimPB->SetClickHdl( LINK( this, SwSortDlg, DelimCharHdl ));
194 :
195 0 : m_pKeyCB1->Check(bCheck1);
196 0 : m_pKeyCB2->Check(bCheck2);
197 0 : m_pKeyCB3->Check(bCheck3);
198 :
199 0 : m_pColEdt1->SetValue(nCol1);
200 0 : m_pColEdt2->SetValue(nCol2);
201 0 : m_pColEdt3->SetValue(nCol3);
202 :
203 : // first initialise the language, then select the
204 0 : if( LANGUAGE_NONE == nLang || LANGUAGE_DONTKNOW == nLang )
205 0 : nLang = (sal_uInt16)GetAppLanguage();
206 :
207 0 : m_pLangLB->SetLanguageList( LANG_LIST_ALL | LANG_LIST_ONLY_KNOWN, sal_True, sal_False);
208 0 : m_pLangLB->SelectLanguage( nLang );
209 :
210 0 : LanguageHdl( 0 );
211 0 : m_pLangLB->SetSelectHdl( LINK( this, SwSortDlg, LanguageHdl ));
212 :
213 0 : m_pSortUp1RB->Check(bAsc1);
214 0 : m_pSortDn1RB->Check(!bAsc1);
215 0 : m_pSortUp2RB->Check(bAsc2);
216 0 : m_pSortDn2RB->Check(!bAsc2);
217 0 : m_pSortUp3RB->Check(bAsc3);
218 0 : m_pSortDn3RB->Check(!bAsc3);
219 :
220 0 : m_pCaseCB->Check( bCsSens );
221 :
222 0 : m_pDelimTabRB->Check(cDeli == '\t');
223 0 : if(!m_pDelimTabRB->IsChecked())
224 : {
225 0 : m_pDelimEdt->SetText(OUString(cDeli));
226 0 : m_pDelimFreeRB->Check(sal_True);
227 0 : DelimHdl(m_pDelimFreeRB);
228 : }
229 : else
230 0 : DelimHdl(m_pDelimTabRB);
231 :
232 0 : if( ::lcl_GetSelTbl( rSh, nX, nY) )
233 : {
234 0 : sal_uInt16 nMax = m_pRowRB->IsChecked()? nY : nX;
235 0 : m_pColEdt1->SetMax(nMax);
236 0 : m_pColEdt2->SetMax(nMax);
237 0 : m_pColEdt3->SetMax(nMax);
238 : }
239 :
240 0 : m_pDelimPB->SetAccessibleRelationMemberOf(m_pDelimFreeRB);
241 :
242 0 : m_pColEdt1->SetAccessibleRelationMemberOf(m_pKeyCB1);
243 0 : m_pTypDLB1->SetAccessibleRelationMemberOf(m_pKeyCB1);
244 :
245 0 : m_pColEdt2->SetAccessibleRelationMemberOf(m_pKeyCB2);
246 0 : m_pTypDLB2->SetAccessibleRelationMemberOf(m_pKeyCB2);
247 :
248 0 : m_pColEdt3->SetAccessibleRelationMemberOf(m_pKeyCB3);
249 0 : m_pTypDLB3->SetAccessibleRelationMemberOf(m_pKeyCB3);
250 0 : }
251 :
252 0 : SwSortDlg::~SwSortDlg()
253 : {
254 0 : ::lcl_ClearLstBoxAndDelUserData(*m_pTypDLB1);
255 0 : ::lcl_ClearLstBoxAndDelUserData(*m_pTypDLB2);
256 0 : ::lcl_ClearLstBoxAndDelUserData(*m_pTypDLB3);
257 0 : delete pColRes;
258 0 : }
259 :
260 0 : sal_Unicode SwSortDlg::GetDelimChar() const
261 : {
262 0 : sal_Unicode cRet = '\t';
263 0 : if( !m_pDelimTabRB->IsChecked() )
264 : {
265 0 : String aTmp( m_pDelimEdt->GetText() );
266 0 : if( aTmp.Len() )
267 0 : cRet = aTmp.GetChar( 0 );
268 : }
269 0 : return cRet;
270 : }
271 :
272 : /*--------------------------------------------------------------------
273 : Description: pass on to the Core
274 : --------------------------------------------------------------------*/
275 0 : void SwSortDlg::Apply()
276 : {
277 : // save all settings
278 0 : bCheck1 = m_pKeyCB1->IsChecked();
279 0 : bCheck2 = m_pKeyCB2->IsChecked();
280 0 : bCheck3 = m_pKeyCB3->IsChecked();
281 :
282 0 : nCol1 = (sal_uInt16)m_pColEdt1->GetValue();
283 0 : nCol2 = (sal_uInt16)m_pColEdt2->GetValue();
284 0 : nCol3 = (sal_uInt16)m_pColEdt3->GetValue();
285 :
286 0 : nType1 = m_pTypDLB1->GetSelectEntryPos();
287 0 : nType2 = m_pTypDLB2->GetSelectEntryPos();
288 0 : nType3 = m_pTypDLB3->GetSelectEntryPos();
289 :
290 0 : bAsc1 = m_pSortUp1RB->IsChecked();
291 0 : bAsc2 = m_pSortUp2RB->IsChecked();
292 0 : bAsc3 = m_pSortUp3RB->IsChecked();
293 0 : bCol = m_pColumnRB->IsChecked();
294 0 : nLang = m_pLangLB->GetSelectLanguage();
295 0 : cDeli = GetDelimChar();
296 0 : bCsSens = m_pCaseCB->IsChecked();
297 :
298 : void* pUserData;
299 0 : SwSortOptions aOptions;
300 0 : if( bCheck1 )
301 : {
302 0 : String sEntry( m_pTypDLB1->GetSelectEntry() );
303 0 : if( sEntry == aNumericTxt )
304 0 : sEntry.Erase();
305 0 : else if( 0 != (pUserData = m_pTypDLB1->GetEntryData(
306 0 : m_pTypDLB1->GetSelectEntryPos())) )
307 0 : sEntry = *(String*)pUserData;
308 :
309 : SwSortKey *pKey = new SwSortKey( nCol1, sEntry,
310 0 : bAsc1 ? SRT_ASCENDING : SRT_DESCENDING );
311 0 : aOptions.aKeys.push_back( pKey );
312 : }
313 :
314 0 : if( bCheck2 )
315 : {
316 0 : String sEntry( m_pTypDLB2->GetSelectEntry() );
317 0 : if( sEntry == aNumericTxt )
318 0 : sEntry.Erase();
319 0 : else if( 0 != (pUserData = m_pTypDLB2->GetEntryData(
320 0 : m_pTypDLB2->GetSelectEntryPos())) )
321 0 : sEntry = *(String*)pUserData;
322 :
323 : SwSortKey *pKey = new SwSortKey( nCol2, sEntry,
324 0 : bAsc2 ? SRT_ASCENDING : SRT_DESCENDING );
325 0 : aOptions.aKeys.push_back( pKey );
326 : }
327 :
328 0 : if( bCheck3 )
329 : {
330 0 : String sEntry( m_pTypDLB3->GetSelectEntry() );
331 0 : if( sEntry == aNumericTxt )
332 0 : sEntry.Erase();
333 0 : else if( 0 != (pUserData = m_pTypDLB3->GetEntryData(
334 0 : m_pTypDLB3->GetSelectEntryPos())) )
335 0 : sEntry = *(String*)pUserData;
336 :
337 : SwSortKey *pKey = new SwSortKey( nCol3, sEntry,
338 0 : bAsc3 ? SRT_ASCENDING : SRT_DESCENDING );
339 0 : aOptions.aKeys.push_back( pKey );
340 : }
341 :
342 0 : aOptions.eDirection = bCol ? SRT_COLUMNS : SRT_ROWS;
343 0 : aOptions.cDeli = cDeli;
344 0 : aOptions.nLanguage = nLang;
345 0 : aOptions.bTable = rSh.IsTableMode();
346 0 : aOptions.bIgnoreCase = !bCsSens;
347 :
348 : sal_Bool bRet;
349 : {
350 0 : SwWait aWait( *rSh.GetView().GetDocShell(), sal_True );
351 0 : rSh.StartAllAction();
352 0 : if( 0 != (bRet = rSh.Sort( aOptions )))
353 0 : rSh.SetModified();
354 0 : rSh.EndAllAction();
355 : }
356 :
357 0 : if( !bRet )
358 0 : InfoBox( this->GetParent(), SW_RES(MSG_SRTERR)).Execute();
359 0 : }
360 :
361 0 : IMPL_LINK( SwSortDlg, DelimHdl, RadioButton*, pButton )
362 : {
363 0 : sal_Bool bEnable = pButton == m_pDelimFreeRB && m_pDelimFreeRB->IsEnabled();
364 0 : m_pDelimEdt->Enable( bEnable );
365 0 : m_pDelimPB->Enable( bEnable );
366 0 : return 0;
367 : }
368 :
369 0 : IMPL_LINK_NOARG(SwSortDlg, DelimCharHdl)
370 : {
371 0 : SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
372 0 : if(pFact)
373 : {
374 0 : SfxAllItemSet aSet( rSh.GetAttrPool() );
375 0 : aSet.Put( SfxInt32Item( SID_ATTR_CHAR, GetDelimChar() ) );
376 : SfxAbstractDialog* pMap = pFact->CreateSfxDialog( m_pDelimPB, aSet,
377 0 : rSh.GetView().GetViewFrame()->GetFrame().GetFrameInterface(), RID_SVXDLG_CHARMAP );
378 0 : if( RET_OK == pMap->Execute() )
379 : {
380 0 : SFX_ITEMSET_ARG( pMap->GetOutputItemSet(), pItem, SfxInt32Item, SID_ATTR_CHAR, sal_False );
381 0 : if ( pItem )
382 0 : m_pDelimEdt->SetText( OUString(pItem->GetValue()) );
383 : }
384 :
385 0 : delete pMap;
386 : }
387 0 : return 0;
388 : }
389 :
390 0 : IMPL_LINK( SwSortDlg, CheckHdl, CheckBox *, pCheck )
391 : {
392 0 : if( pCheck == ( CheckBox* ) m_pRowRB)
393 : {
394 0 : m_pColLbl->SetText(aColTxt);
395 0 : m_pColEdt1->SetMax(nY);
396 0 : m_pColEdt2->SetMax(nY);
397 0 : m_pColEdt3->SetMax(nY);
398 :
399 0 : m_pColEdt1->SetAccessibleName(aColTxt);
400 0 : m_pColEdt2->SetAccessibleName(aColTxt);
401 0 : m_pColEdt3->SetAccessibleName(aColTxt);
402 : }
403 0 : else if( pCheck == ( CheckBox* ) m_pColumnRB)
404 : {
405 0 : m_pColLbl->SetText(aRowTxt);
406 0 : m_pColEdt1->SetMax(nX);
407 0 : m_pColEdt2->SetMax(nX);
408 0 : m_pColEdt3->SetMax(nX);
409 :
410 0 : m_pColEdt1->SetAccessibleName(aRowTxt);
411 0 : m_pColEdt2->SetAccessibleName(aRowTxt);
412 0 : m_pColEdt3->SetAccessibleName(aRowTxt);
413 : }
414 0 : else if(!m_pKeyCB1->IsChecked() &&
415 0 : !m_pKeyCB2->IsChecked() &&
416 0 : !m_pKeyCB3->IsChecked())
417 0 : pCheck->Check(sal_True);
418 0 : return 0;
419 : }
420 :
421 0 : IMPL_LINK( SwSortDlg, LanguageHdl, ListBox*, pLBox )
422 : {
423 0 : Sequence < OUString > aSeq( GetAppCollator().listCollatorAlgorithms(
424 0 : LanguageTag( m_pLangLB->GetSelectLanguage()).getLocale() ));
425 :
426 0 : if( !pColRes )
427 0 : pColRes = new CollatorResource();
428 :
429 0 : const sal_uInt16 nLstBoxCnt = 3;
430 0 : ListBox* aLstArr[ nLstBoxCnt ] = { m_pTypDLB1, m_pTypDLB2, m_pTypDLB3 };
431 0 : sal_uInt16* aTypeArr[ nLstBoxCnt ] = { &nType1, &nType2, &nType3 };
432 0 : String aOldStrArr[ nLstBoxCnt ];
433 : sal_uInt16 n;
434 :
435 : void* pUserData;
436 0 : for( n = 0; n < nLstBoxCnt; ++n )
437 : {
438 0 : ListBox* pL = aLstArr[ n ];
439 0 : if( 0 != (pUserData = pL->GetEntryData( pL->GetSelectEntryPos())) )
440 0 : aOldStrArr[ n ] = *(String*)pUserData;
441 0 : ::lcl_ClearLstBoxAndDelUserData( *pL );
442 : }
443 :
444 : sal_uInt16 nInsPos;
445 0 : String sAlg, sUINm;
446 0 : for( long nCnt = 0, nEnd = aSeq.getLength(); nCnt <= nEnd; ++nCnt )
447 : {
448 0 : if( nCnt < nEnd )
449 0 : sUINm = pColRes->GetTranslation( sAlg = aSeq[ nCnt ] );
450 : else
451 0 : sUINm = sAlg = aNumericTxt;
452 :
453 0 : for( n = 0; n < nLstBoxCnt; ++n )
454 : {
455 0 : ListBox* pL = aLstArr[ n ];
456 0 : nInsPos = pL->InsertEntry( sUINm );
457 0 : pL->SetEntryData( nInsPos, new String( sAlg ));
458 0 : if( pLBox && sAlg == aOldStrArr[ n ] )
459 0 : pL->SelectEntryPos( nInsPos );
460 : }
461 : }
462 :
463 0 : for( n = 0; n < nLstBoxCnt; ++n )
464 : {
465 0 : ListBox* pL = aLstArr[ n ];
466 0 : if( !pLBox )
467 0 : pL->SelectEntryPos( *aTypeArr[n] );
468 0 : else if( LISTBOX_ENTRY_NOTFOUND == pL->GetSelectEntryPos() )
469 0 : pL->SelectEntryPos( 0 );
470 : }
471 0 : return 0;
472 0 : }
473 :
474 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|