bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / misc / srtdlg.cxx
blobaba0a8e62d3ae4b5648b67e7b1144ec59ff2def7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "srtdlg.hxx"
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>
43 static sal_Bool bCheck1 = sal_True;
44 static sal_Bool bCheck2 = sal_False;
45 static sal_Bool bCheck3 = sal_False;
47 static sal_uInt16 nCol1 = 1;
48 static sal_uInt16 nCol2 = 1;
49 static sal_uInt16 nCol3 = 1;
51 static sal_uInt16 nType1 = 0;
52 static sal_uInt16 nType2 = 0;
53 static sal_uInt16 nType3 = 0;
55 static sal_uInt16 nLang = LANGUAGE_NONE;
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;
63 static sal_Unicode cDeli = '\t';
65 using namespace ::com::sun::star::lang;
66 using namespace ::com::sun::star::uno;
67 using namespace ::com::sun::star;
69 static void lcl_ClearLstBoxAndDelUserData( ListBox& rLstBox )
71 void* pDel;
72 for( sal_uInt16 n = 0, nEnd = rLstBox.GetEntryCount(); n < nEnd; ++n )
73 if( 0 != ( pDel = rLstBox.GetEntryData( n )) )
74 delete (String*)pDel;
75 rLstBox.Clear();
78 /*--------------------------------------------------------------------
79 Description: determine lines and columns for table selection
80 --------------------------------------------------------------------*/
81 static bool lcl_GetSelTbl( SwWrtShell &rSh, sal_uInt16& rX, sal_uInt16& rY )
83 const SwTableNode* pTblNd = rSh.IsCrsrInTbl();
84 if( !pTblNd )
85 return false;
87 _FndBox aFndBox( 0, 0 );
89 // look for all boxes / lines
91 SwSelBoxes aSelBoxes;
92 ::GetTblSel( rSh, aSelBoxes );
93 _FndPara aPara( aSelBoxes, &aFndBox );
94 const SwTable& rTbl = pTblNd->GetTable();
95 ForEach_FndLineCopyCol( (SwTableLines&)rTbl.GetTabLines(), &aPara );
97 rX = aFndBox.GetLines().size();
98 if( !rX )
99 return false;
101 rY = aFndBox.GetLines().front().GetBoxes().size();
102 return true;
105 /*--------------------------------------------------------------------
106 Description: init list
107 --------------------------------------------------------------------*/
108 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 , nY(99)
118 get(m_pColLbl, "column");
119 get(m_pTypLbl, "keytype");
121 get(m_pKeyCB1, "key1");
122 get(m_pColEdt1, "colsb1");
123 get(m_pTypDLB1, "typelb1");
124 get(m_pSortUp1RB, "up1");
125 get(m_pSortDn1RB, "down1");
127 get(m_pKeyCB2, "key2");
128 get(m_pColEdt2, "colsb2");
129 get(m_pTypDLB2, "typelb2");
130 get(m_pSortUp2RB, "up2");
131 get(m_pSortDn2RB, "down2");
133 get(m_pKeyCB3, "key3");
134 get(m_pColEdt3, "colsb3");
135 get(m_pTypDLB3, "typelb3");
136 get(m_pSortUp3RB, "up3");
137 get(m_pSortDn3RB, "down3");
139 get(m_pColumnRB, "columns");
140 get(m_pRowRB, "rows");
142 get(m_pDelimTabRB, "tabs");
143 get(m_pDelimFreeRB, "character");
144 get(m_pDelimEdt, "separator");
145 get(m_pDelimPB, "delimpb");
147 get(m_pLangLB, "langlb");
148 get(m_pCaseCB, "matchcase");
150 m_pColEdt1->SetAccessibleName(m_pColLbl->GetText());
151 m_pColEdt2->SetAccessibleName(m_pColLbl->GetText());
152 m_pColEdt3->SetAccessibleName(m_pColLbl->GetText());
153 m_pTypDLB1->SetAccessibleName(m_pTypLbl->GetText());
154 m_pTypDLB2->SetAccessibleName(m_pTypLbl->GetText());
155 m_pTypDLB3->SetAccessibleName(m_pTypLbl->GetText());
156 m_pSortUp1RB->SetAccessibleRelationMemberOf(m_pKeyCB1);
157 m_pSortDn1RB->SetAccessibleRelationMemberOf(m_pKeyCB1);
158 m_pSortUp2RB->SetAccessibleRelationMemberOf(m_pKeyCB2);
159 m_pSortDn2RB->SetAccessibleRelationMemberOf(m_pKeyCB2);
160 m_pSortUp3RB->SetAccessibleRelationMemberOf(m_pKeyCB3);
161 m_pSortDn3RB->SetAccessibleRelationMemberOf(m_pKeyCB3);
163 m_pDelimEdt->SetMaxTextLen( 1 );
164 if(rSh.GetSelectionType() &
165 (nsSelectionType::SEL_TBL|nsSelectionType::SEL_TBL_CELLS) )
167 m_pColumnRB->Check(bCol);
168 m_pColLbl->SetText(bCol ? aRowTxt : aColTxt);
169 m_pRowRB->Check(!bCol);
170 m_pDelimTabRB->Enable(sal_False);
171 m_pDelimFreeRB->Enable(sal_False);
172 m_pDelimEdt->Enable(sal_False);
174 else
176 m_pColumnRB->Enable(sal_False);
177 m_pRowRB->Check(sal_True);
178 m_pColLbl->SetText(aColTxt);
181 // initialise
182 Link aLk = LINK(this,SwSortDlg, CheckHdl);
183 m_pKeyCB1->SetClickHdl( aLk );
184 m_pKeyCB2->SetClickHdl( aLk );
185 m_pKeyCB3->SetClickHdl( aLk );
186 m_pColumnRB->SetClickHdl( aLk );
187 m_pRowRB->SetClickHdl( aLk );
189 aLk = LINK(this,SwSortDlg, DelimHdl);
190 m_pDelimFreeRB->SetClickHdl(aLk);
191 m_pDelimTabRB->SetClickHdl(aLk);
193 m_pDelimPB->SetClickHdl( LINK( this, SwSortDlg, DelimCharHdl ));
195 m_pKeyCB1->Check(bCheck1);
196 m_pKeyCB2->Check(bCheck2);
197 m_pKeyCB3->Check(bCheck3);
199 m_pColEdt1->SetValue(nCol1);
200 m_pColEdt2->SetValue(nCol2);
201 m_pColEdt3->SetValue(nCol3);
203 // first initialise the language, then select the
204 if( LANGUAGE_NONE == nLang || LANGUAGE_DONTKNOW == nLang )
205 nLang = (sal_uInt16)GetAppLanguage();
207 m_pLangLB->SetLanguageList( LANG_LIST_ALL | LANG_LIST_ONLY_KNOWN, sal_True, sal_False);
208 m_pLangLB->SelectLanguage( nLang );
210 LanguageHdl( 0 );
211 m_pLangLB->SetSelectHdl( LINK( this, SwSortDlg, LanguageHdl ));
213 m_pSortUp1RB->Check(bAsc1);
214 m_pSortDn1RB->Check(!bAsc1);
215 m_pSortUp2RB->Check(bAsc2);
216 m_pSortDn2RB->Check(!bAsc2);
217 m_pSortUp3RB->Check(bAsc3);
218 m_pSortDn3RB->Check(!bAsc3);
220 m_pCaseCB->Check( bCsSens );
222 m_pDelimTabRB->Check(cDeli == '\t');
223 if(!m_pDelimTabRB->IsChecked())
225 m_pDelimEdt->SetText(OUString(cDeli));
226 m_pDelimFreeRB->Check(sal_True);
227 DelimHdl(m_pDelimFreeRB);
229 else
230 DelimHdl(m_pDelimTabRB);
232 if( ::lcl_GetSelTbl( rSh, nX, nY) )
234 sal_uInt16 nMax = m_pRowRB->IsChecked()? nY : nX;
235 m_pColEdt1->SetMax(nMax);
236 m_pColEdt2->SetMax(nMax);
237 m_pColEdt3->SetMax(nMax);
240 m_pDelimPB->SetAccessibleRelationMemberOf(m_pDelimFreeRB);
242 m_pColEdt1->SetAccessibleRelationMemberOf(m_pKeyCB1);
243 m_pTypDLB1->SetAccessibleRelationMemberOf(m_pKeyCB1);
245 m_pColEdt2->SetAccessibleRelationMemberOf(m_pKeyCB2);
246 m_pTypDLB2->SetAccessibleRelationMemberOf(m_pKeyCB2);
248 m_pColEdt3->SetAccessibleRelationMemberOf(m_pKeyCB3);
249 m_pTypDLB3->SetAccessibleRelationMemberOf(m_pKeyCB3);
252 SwSortDlg::~SwSortDlg()
254 ::lcl_ClearLstBoxAndDelUserData(*m_pTypDLB1);
255 ::lcl_ClearLstBoxAndDelUserData(*m_pTypDLB2);
256 ::lcl_ClearLstBoxAndDelUserData(*m_pTypDLB3);
257 delete pColRes;
260 sal_Unicode SwSortDlg::GetDelimChar() const
262 sal_Unicode cRet = '\t';
263 if( !m_pDelimTabRB->IsChecked() )
265 String aTmp( m_pDelimEdt->GetText() );
266 if( aTmp.Len() )
267 cRet = aTmp.GetChar( 0 );
269 return cRet;
272 /*--------------------------------------------------------------------
273 Description: pass on to the Core
274 --------------------------------------------------------------------*/
275 void SwSortDlg::Apply()
277 // save all settings
278 bCheck1 = m_pKeyCB1->IsChecked();
279 bCheck2 = m_pKeyCB2->IsChecked();
280 bCheck3 = m_pKeyCB3->IsChecked();
282 nCol1 = (sal_uInt16)m_pColEdt1->GetValue();
283 nCol2 = (sal_uInt16)m_pColEdt2->GetValue();
284 nCol3 = (sal_uInt16)m_pColEdt3->GetValue();
286 nType1 = m_pTypDLB1->GetSelectEntryPos();
287 nType2 = m_pTypDLB2->GetSelectEntryPos();
288 nType3 = m_pTypDLB3->GetSelectEntryPos();
290 bAsc1 = m_pSortUp1RB->IsChecked();
291 bAsc2 = m_pSortUp2RB->IsChecked();
292 bAsc3 = m_pSortUp3RB->IsChecked();
293 bCol = m_pColumnRB->IsChecked();
294 nLang = m_pLangLB->GetSelectLanguage();
295 cDeli = GetDelimChar();
296 bCsSens = m_pCaseCB->IsChecked();
298 void* pUserData;
299 SwSortOptions aOptions;
300 if( bCheck1 )
302 String sEntry( m_pTypDLB1->GetSelectEntry() );
303 if( sEntry == aNumericTxt )
304 sEntry.Erase();
305 else if( 0 != (pUserData = m_pTypDLB1->GetEntryData(
306 m_pTypDLB1->GetSelectEntryPos())) )
307 sEntry = *(String*)pUserData;
309 SwSortKey *pKey = new SwSortKey( nCol1, sEntry,
310 bAsc1 ? SRT_ASCENDING : SRT_DESCENDING );
311 aOptions.aKeys.push_back( pKey );
314 if( bCheck2 )
316 String sEntry( m_pTypDLB2->GetSelectEntry() );
317 if( sEntry == aNumericTxt )
318 sEntry.Erase();
319 else if( 0 != (pUserData = m_pTypDLB2->GetEntryData(
320 m_pTypDLB2->GetSelectEntryPos())) )
321 sEntry = *(String*)pUserData;
323 SwSortKey *pKey = new SwSortKey( nCol2, sEntry,
324 bAsc2 ? SRT_ASCENDING : SRT_DESCENDING );
325 aOptions.aKeys.push_back( pKey );
328 if( bCheck3 )
330 String sEntry( m_pTypDLB3->GetSelectEntry() );
331 if( sEntry == aNumericTxt )
332 sEntry.Erase();
333 else if( 0 != (pUserData = m_pTypDLB3->GetEntryData(
334 m_pTypDLB3->GetSelectEntryPos())) )
335 sEntry = *(String*)pUserData;
337 SwSortKey *pKey = new SwSortKey( nCol3, sEntry,
338 bAsc3 ? SRT_ASCENDING : SRT_DESCENDING );
339 aOptions.aKeys.push_back( pKey );
342 aOptions.eDirection = bCol ? SRT_COLUMNS : SRT_ROWS;
343 aOptions.cDeli = cDeli;
344 aOptions.nLanguage = nLang;
345 aOptions.bTable = rSh.IsTableMode();
346 aOptions.bIgnoreCase = !bCsSens;
348 sal_Bool bRet;
350 SwWait aWait( *rSh.GetView().GetDocShell(), sal_True );
351 rSh.StartAllAction();
352 if( 0 != (bRet = rSh.Sort( aOptions )))
353 rSh.SetModified();
354 rSh.EndAllAction();
357 if( !bRet )
358 InfoBox( this->GetParent(), SW_RES(MSG_SRTERR)).Execute();
361 IMPL_LINK( SwSortDlg, DelimHdl, RadioButton*, pButton )
363 sal_Bool bEnable = pButton == m_pDelimFreeRB && m_pDelimFreeRB->IsEnabled();
364 m_pDelimEdt->Enable( bEnable );
365 m_pDelimPB->Enable( bEnable );
366 return 0;
369 IMPL_LINK_NOARG(SwSortDlg, DelimCharHdl)
371 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
372 if(pFact)
374 SfxAllItemSet aSet( rSh.GetAttrPool() );
375 aSet.Put( SfxInt32Item( SID_ATTR_CHAR, GetDelimChar() ) );
376 SfxAbstractDialog* pMap = pFact->CreateSfxDialog( m_pDelimPB, aSet,
377 rSh.GetView().GetViewFrame()->GetFrame().GetFrameInterface(), RID_SVXDLG_CHARMAP );
378 if( RET_OK == pMap->Execute() )
380 SFX_ITEMSET_ARG( pMap->GetOutputItemSet(), pItem, SfxInt32Item, SID_ATTR_CHAR, sal_False );
381 if ( pItem )
382 m_pDelimEdt->SetText( OUString(pItem->GetValue()) );
385 delete pMap;
387 return 0;
390 IMPL_LINK( SwSortDlg, CheckHdl, CheckBox *, pCheck )
392 if( pCheck == ( CheckBox* ) m_pRowRB)
394 m_pColLbl->SetText(aColTxt);
395 m_pColEdt1->SetMax(nY);
396 m_pColEdt2->SetMax(nY);
397 m_pColEdt3->SetMax(nY);
399 m_pColEdt1->SetAccessibleName(aColTxt);
400 m_pColEdt2->SetAccessibleName(aColTxt);
401 m_pColEdt3->SetAccessibleName(aColTxt);
403 else if( pCheck == ( CheckBox* ) m_pColumnRB)
405 m_pColLbl->SetText(aRowTxt);
406 m_pColEdt1->SetMax(nX);
407 m_pColEdt2->SetMax(nX);
408 m_pColEdt3->SetMax(nX);
410 m_pColEdt1->SetAccessibleName(aRowTxt);
411 m_pColEdt2->SetAccessibleName(aRowTxt);
412 m_pColEdt3->SetAccessibleName(aRowTxt);
414 else if(!m_pKeyCB1->IsChecked() &&
415 !m_pKeyCB2->IsChecked() &&
416 !m_pKeyCB3->IsChecked())
417 pCheck->Check(sal_True);
418 return 0;
421 IMPL_LINK( SwSortDlg, LanguageHdl, ListBox*, pLBox )
423 Sequence < OUString > aSeq( GetAppCollator().listCollatorAlgorithms(
424 LanguageTag( m_pLangLB->GetSelectLanguage()).getLocale() ));
426 if( !pColRes )
427 pColRes = new CollatorResource();
429 const sal_uInt16 nLstBoxCnt = 3;
430 ListBox* aLstArr[ nLstBoxCnt ] = { m_pTypDLB1, m_pTypDLB2, m_pTypDLB3 };
431 sal_uInt16* aTypeArr[ nLstBoxCnt ] = { &nType1, &nType2, &nType3 };
432 String aOldStrArr[ nLstBoxCnt ];
433 sal_uInt16 n;
435 void* pUserData;
436 for( n = 0; n < nLstBoxCnt; ++n )
438 ListBox* pL = aLstArr[ n ];
439 if( 0 != (pUserData = pL->GetEntryData( pL->GetSelectEntryPos())) )
440 aOldStrArr[ n ] = *(String*)pUserData;
441 ::lcl_ClearLstBoxAndDelUserData( *pL );
444 sal_uInt16 nInsPos;
445 String sAlg, sUINm;
446 for( long nCnt = 0, nEnd = aSeq.getLength(); nCnt <= nEnd; ++nCnt )
448 if( nCnt < nEnd )
449 sUINm = pColRes->GetTranslation( sAlg = aSeq[ nCnt ] );
450 else
451 sUINm = sAlg = aNumericTxt;
453 for( n = 0; n < nLstBoxCnt; ++n )
455 ListBox* pL = aLstArr[ n ];
456 nInsPos = pL->InsertEntry( sUINm );
457 pL->SetEntryData( nInsPos, new String( sAlg ));
458 if( pLBox && sAlg == aOldStrArr[ n ] )
459 pL->SelectEntryPos( nInsPos );
463 for( n = 0; n < nLstBoxCnt; ++n )
465 ListBox* pL = aLstArr[ n ];
466 if( !pLBox )
467 pL->SelectEntryPos( *aTypeArr[n] );
468 else if( LISTBOX_ENTRY_NOTFOUND == pL->GetSelectEntryPos() )
469 pL->SelectEntryPos( 0 );
471 return 0;
474 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */