xref: /aoo42x/main/sw/source/ui/misc/srtdlg.cxx (revision efeef26f)
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_sw.hxx"
26 #ifdef SW_DLLIMPLEMENTATION
27 #undef SW_DLLIMPLEMENTATION
28 #endif
29 
30 #include "srtdlg.hxx"
31 
32 #ifndef _MSGBOX_HXX //autogen
33 #include <vcl/msgbox.hxx>
34 #endif
35 #include <svl/intitem.hxx>
36 #include <svl/eitem.hxx>
37 #include <sfx2/dispatch.hxx>
38 #include <svx/svxids.hrc>
39 #include <editeng/unolingu.hxx>
40 #include <svx/svxdlg.hxx>
41 #include <svx/dialogs.hrc>
42 #include <unotools/collatorwrapper.hxx>
43 #include <svtools/collatorres.hxx>
44 #include <swwait.hxx>
45 #include <view.hxx>
46 #include <cmdid.h>
47 #include <wrtsh.hxx>
48 #include <misc.hrc>
49 #include <srtdlg.hrc>
50 #include <swtable.hxx>
51 #include <node.hxx>
52 #include <tblsel.hxx>
53 #include <sfx2/request.hxx>
54 
55 // sw/inc/tblsel.hxx
56 SV_IMPL_PTRARR( _FndBoxes, _FndBox* )
57 SV_IMPL_PTRARR( _FndLines, _FndLine* )
58 
59 static sal_Bool bCheck1 = sal_True;
60 static sal_Bool bCheck2 = sal_False;
61 static sal_Bool bCheck3 = sal_False;
62 
63 static sal_uInt16 nCol1 = 1;
64 static sal_uInt16 nCol2 = 1;
65 static sal_uInt16 nCol3 = 1;
66 
67 static sal_uInt16 nType1 = 0;
68 static sal_uInt16 nType2 = 0;
69 static sal_uInt16 nType3 = 0;
70 
71 static sal_uInt16 nLang = LANGUAGE_NONE;
72 
73 static sal_Bool	  bAsc1	 = sal_True;
74 static sal_Bool	  bAsc2	 = sal_True;
75 static sal_Bool	  bAsc3	 = sal_True;
76 static sal_Bool	  bCol	 = sal_False;
77 static sal_Bool	  bCsSens= sal_False;
78 
79 static sal_Unicode	  cDeli	 = '\t';
80 
81 using namespace ::com::sun::star::lang;
82 using namespace ::com::sun::star::uno;
83 using ::rtl::OUString;
84 
85 
86 void lcl_ClearLstBoxAndDelUserData( ListBox& rLstBox )
87 {
88 	void* pDel;
89 	for( sal_uInt16 n = 0, nEnd = rLstBox.GetEntryCount(); n < nEnd; ++n )
90 		if( 0 != ( pDel = rLstBox.GetEntryData( n )) )
91 			delete (String*)pDel;
92 	rLstBox.Clear();
93 }
94 
95 /*--------------------------------------------------------------------
96 	 Beschreibung: 	Fuer Tabellenselektion sel. Zeilen und Spalten
97 					feststellen
98  --------------------------------------------------------------------*/
99 
100 
101 sal_Bool lcl_GetSelTbl( SwWrtShell &rSh, sal_uInt16& rX, sal_uInt16& rY )
102 {
103 	const SwTableNode* pTblNd = rSh.IsCrsrInTbl();
104 	if( !pTblNd )
105 		return sal_False;
106 
107 	_FndBox aFndBox( 0, 0 );
108 
109 	// suche alle Boxen / Lines
110 	{
111 		SwSelBoxes aSelBoxes;
112 		::GetTblSel( rSh, aSelBoxes );
113 		_FndPara aPara( aSelBoxes, &aFndBox );
114 		const SwTable& rTbl = pTblNd->GetTable();
115 		((SwTableLines&)rTbl.GetTabLines()).ForEach( &_FndLineCopyCol, &aPara );
116 	}
117 	rX = aFndBox.GetLines().Count();
118 	if( !rX )
119 		return sal_False;
120 
121 	rY = aFndBox.GetLines()[0]->GetBoxes().Count();
122 	return sal_True;
123 }
124 
125 /*--------------------------------------------------------------------
126 	 Beschreibung: Init-Liste
127  --------------------------------------------------------------------*/
128 
129 SwSortDlg::SwSortDlg(Window* pParent, SwWrtShell &rShell) :
130 
131 	SvxStandardDialog(pParent, SW_RES(DLG_SORTING)),
132 
133     aColLbl(this, 		SW_RES(FT_COL   )),
134 	aTypLbl(this, 		SW_RES(FT_KEYTYP)),
135 	aDirLbl(this, 		SW_RES(FT_DIR	)),
136     aSortFL(this,      SW_RES(FL_SORT_2  )),
137 
138 
139     aKeyCB1(this,       SW_RES(CB_KEY1  )),
140 	aColEdt1(this, 		SW_RES(ED_KEY1  )),
141 	aTypDLB1(this, 		SW_RES(DLB_KEY1 )),
142 	aSortUpRB(this, 	SW_RES(RB_UP    )),
143 	aSortDnRB(this, 	SW_RES(RB_DN    )),
144 
145     aKeyCB2(this,       SW_RES(CB_KEY2  )),
146 	aColEdt2(this, 		SW_RES(ED_KEY2  )),
147 	aTypDLB2(this, 		SW_RES(DLB_KEY2 )),
148 	aSortUp2RB(this, 	SW_RES(RB_UP2    )),
149 	aSortDn2RB(this, 	SW_RES(RB_DN2    )),
150 
151     aKeyCB3(this,       SW_RES(CB_KEY3  )),
152 	aColEdt3(this, 		SW_RES(ED_KEY3  )),
153 	aTypDLB3(this, 		SW_RES(DLB_KEY3 )),
154 	aSortUp3RB(this, 	SW_RES(RB_UP3    )),
155 	aSortDn3RB(this, 	SW_RES(RB_DN3    )),
156 	aDirFL(this,       SW_RES(FL_DIR   )),
157 
158 	aColumnRB(this, 	SW_RES(RB_COL   )),
159 	aRowRB(this, 		SW_RES(RB_ROW   )),
160 
161     aDelimFL(this,     SW_RES(FL_DELIM )),
162     aDelimTabRB(this,   SW_RES(RB_TAB   )),
163 	aDelimFreeRB(this, 	SW_RES(RB_TABCH )),
164 	aDelimEdt(this, 	SW_RES(ED_TABCH )),
165     aDelimPB(this,    	SW_RES( PB_DELIM)),
166 
167     aLangFL(this,       SW_RES( FL_LANG )),
168     aLangLB(this,     	SW_RES( LB_LANG )),
169 
170     aSortOptFL(this,    SW_RES( FL_SORT )),
171     aCaseCB(this,     	SW_RES( CB_CASE )),
172 
173     aOkBtn(this,        SW_RES(BT_OK    )),
174     aCancelBtn(this,    SW_RES(BT_CANCEL)),
175     aHelpBtn(this,      SW_RES(BT_HELP  )),
176 
177     aColTxt(            SW_RES(STR_COL)),
178     aRowTxt(            SW_RES(STR_ROW)),
179 	aNumericTxt(		SW_RES(STR_NUMERIC)),
180     rSh(rShell),
181     pColRes( 0 ),
182 	nX( 99 ),
183     nY( 99 )
184 {
185 	aColEdt1.SetAccessibleName(aColLbl.GetText());
186 	aColEdt2.SetAccessibleName(aColLbl.GetText());
187 	aColEdt3.SetAccessibleName(aColLbl.GetText());
188 	aTypDLB1.SetAccessibleName(aTypLbl.GetText());
189 	aTypDLB2.SetAccessibleName(aTypLbl.GetText());
190 	aTypDLB3.SetAccessibleName(aTypLbl.GetText());
191 	aSortUpRB.SetAccessibleRelationMemberOf( &aKeyCB1 );
192 	aSortDnRB.SetAccessibleRelationMemberOf( &aKeyCB1 );
193 	aSortUp2RB.SetAccessibleRelationMemberOf( &aKeyCB2 );
194 	aSortDn2RB.SetAccessibleRelationMemberOf( &aKeyCB2 );
195 	aSortUp3RB.SetAccessibleRelationMemberOf( &aKeyCB3 );
196 	aSortDn3RB.SetAccessibleRelationMemberOf( &aKeyCB3 );
197 
198 	aDelimEdt.SetMaxTextLen( 1 );
199 	if(rSh.GetSelectionType() &
200 			(nsSelectionType::SEL_TBL|nsSelectionType::SEL_TBL_CELLS) )
201 	{
202 		aColumnRB.Check(bCol);
203 		aColLbl.SetText(bCol ? aRowTxt : aColTxt);
204 		aRowRB.Check(!bCol);
205 		aDelimTabRB.Enable(sal_False);
206 		aDelimFreeRB.Enable(sal_False);
207 		aDelimEdt.Enable(sal_False);
208 	}
209 	else
210 	{
211 		aColumnRB.Enable(sal_False);
212 		aRowRB.Check(sal_True);
213 		aColLbl.SetText(aColTxt);
214 	}
215 
216 	// Initialisieren
217 	Link aLk = LINK(this,SwSortDlg, CheckHdl);
218 	aKeyCB1.SetClickHdl( aLk );
219 	aKeyCB2.SetClickHdl( aLk );
220 	aKeyCB3.SetClickHdl( aLk );
221 	aColumnRB.SetClickHdl( aLk );
222 	aRowRB.SetClickHdl( aLk );
223 
224 	aLk = LINK(this,SwSortDlg, DelimHdl);
225 	aDelimFreeRB.SetClickHdl(aLk);
226 	aDelimTabRB.SetClickHdl(aLk);
227 
228 	aDelimPB.SetClickHdl( LINK( this, SwSortDlg, DelimCharHdl ));
229 
230 	aKeyCB1.Check(bCheck1);
231 	aKeyCB2.Check(bCheck2);
232 	aKeyCB3.Check(bCheck3);
233 
234 	aColEdt1.SetValue(nCol1);
235 	aColEdt2.SetValue(nCol2);
236 	aColEdt3.SetValue(nCol3);
237 
238 	// first initialise the language, then select the
239 	if( LANGUAGE_NONE == nLang || LANGUAGE_DONTKNOW == nLang )
240 		nLang = (sal_uInt16)GetAppLanguage();
241 
242     aLangLB.SetLanguageList( LANG_LIST_ALL | LANG_LIST_ONLY_KNOWN, sal_True, sal_False);
243 	aLangLB.SelectLanguage( nLang );
244 
245 	LanguageHdl( 0 );
246 	aLangLB.SetSelectHdl( LINK( this, SwSortDlg, LanguageHdl ));
247 
248 	aSortUpRB.Check(bAsc1);
249 	aSortDnRB.Check(!bAsc1);
250 	aSortUp2RB.Check(bAsc2);
251 	aSortDn2RB.Check(!bAsc2);
252 	aSortUp3RB.Check(bAsc3);
253 	aSortDn3RB.Check(!bAsc3);
254 
255 	aCaseCB.Check( bCsSens );
256 
257 	aDelimTabRB.Check(cDeli == '\t');
258 	if(!aDelimTabRB.IsChecked())
259 	{
260 		aDelimEdt.SetText(cDeli);
261 		aDelimFreeRB.Check(sal_True);
262 		DelimHdl(&aDelimFreeRB);
263 	}
264 	else
265 		DelimHdl(&aDelimTabRB);
266 
267 	FreeResource();
268 	if( ::lcl_GetSelTbl( rSh, nX, nY) )
269 	{
270 		sal_uInt16 nMax = aRowRB.IsChecked()? nY : nX;
271 		aColEdt1.SetMax(nMax);
272 		aColEdt2.SetMax(nMax);
273 		aColEdt3.SetMax(nMax);
274 	}
275 
276 	aDelimEdt.SetAccessibleRelationLabeledBy(&aDelimFreeRB);
277 	aDelimPB.SetAccessibleRelationLabeledBy(&aDelimFreeRB);
278 	aDelimPB.SetAccessibleRelationMemberOf(&aDelimFL);
279 
280 	aColEdt1.SetAccessibleRelationMemberOf(&aKeyCB1);
281 	aColEdt1.SetAccessibleRelationLabeledBy(&aColLbl);
282 	aTypDLB1.SetAccessibleRelationMemberOf(&aKeyCB1);
283 	aTypDLB1.SetAccessibleRelationLabeledBy(&aTypLbl);
284 
285 	aColEdt2.SetAccessibleRelationMemberOf(&aKeyCB2);
286 	aColEdt2.SetAccessibleRelationLabeledBy(&aColLbl);
287 	aTypDLB2.SetAccessibleRelationMemberOf(&aKeyCB2);
288 	aTypDLB2.SetAccessibleRelationLabeledBy(&aTypLbl);
289 
290 	aColEdt3.SetAccessibleRelationMemberOf(&aKeyCB3);
291 	aColEdt3.SetAccessibleRelationLabeledBy(&aColLbl);
292 	aTypDLB3.SetAccessibleRelationMemberOf(&aKeyCB3);
293 	aTypDLB3.SetAccessibleRelationLabeledBy(&aTypLbl);
294 }
295 
296 SwSortDlg::~SwSortDlg()
297 {
298 	::lcl_ClearLstBoxAndDelUserData( aTypDLB1 );
299 	::lcl_ClearLstBoxAndDelUserData( aTypDLB2 );
300 	::lcl_ClearLstBoxAndDelUserData( aTypDLB3 );
301 	delete pColRes;
302 }
303 
304 sal_Unicode	SwSortDlg::GetDelimChar() const
305 {
306     sal_Unicode cRet = '\t';
307 	if( !aDelimTabRB.IsChecked() )
308 	{
309 		String aTmp( aDelimEdt.GetText() );
310 		if( aTmp.Len() )
311             cRet = aTmp.GetChar( 0 );
312 	}
313     return cRet;
314 }
315 
316 /*--------------------------------------------------------------------
317 	Beschreibung: An die Core weiterreichen
318  --------------------------------------------------------------------*/
319 void SwSortDlg::Apply()
320 {
321 	// Alte Einstellung speichern
322 	//
323 	bCheck1 = aKeyCB1.IsChecked();
324 	bCheck2 = aKeyCB2.IsChecked();
325 	bCheck3 = aKeyCB3.IsChecked();
326 
327 	nCol1 = (sal_uInt16)aColEdt1.GetValue();
328 	nCol2 = (sal_uInt16)aColEdt2.GetValue();
329 	nCol3 = (sal_uInt16)aColEdt3.GetValue();
330 
331 	nType1 = aTypDLB1.GetSelectEntryPos();
332 	nType2 = aTypDLB2.GetSelectEntryPos();
333 	nType3 = aTypDLB3.GetSelectEntryPos();
334 
335 	bAsc1 = aSortUpRB.IsChecked();
336 	bAsc2 = aSortUp2RB.IsChecked();
337 	bAsc3 = aSortUp3RB.IsChecked();
338 	bCol = aColumnRB.IsChecked();
339 	nLang = aLangLB.GetSelectLanguage();
340 	cDeli = GetDelimChar();
341 	bCsSens = aCaseCB.IsChecked();
342 
343 	void* pUserData;
344 	SwSortOptions aOptions;
345 	if( bCheck1 )
346 	{
347 		String sEntry( aTypDLB1.GetSelectEntry() );
348 		if( sEntry == aNumericTxt )
349 			sEntry.Erase();
350 		else if( 0 != (pUserData = aTypDLB1.GetEntryData(
351 											aTypDLB1.GetSelectEntryPos())) )
352 			sEntry = *(String*)pUserData;
353 
354 		SwSortKey *pKey = new SwSortKey( nCol1, sEntry,
355 									bAsc1 ? SRT_ASCENDING : SRT_DESCENDING );
356 		aOptions.aKeys.C40_INSERT(SwSortKey, pKey, aOptions.aKeys.Count());
357 	}
358 
359 	if( bCheck2 )
360 	{
361 		String sEntry( aTypDLB2.GetSelectEntry() );
362 		if( sEntry == aNumericTxt )
363 			sEntry.Erase();
364 		else if( 0 != (pUserData = aTypDLB2.GetEntryData(
365 											aTypDLB2.GetSelectEntryPos())) )
366 			sEntry = *(String*)pUserData;
367 
368 		SwSortKey *pKey = new SwSortKey( nCol2, sEntry,
369 									bAsc2 ? SRT_ASCENDING : SRT_DESCENDING );
370 		aOptions.aKeys.C40_INSERT( SwSortKey, pKey, aOptions.aKeys.Count() );
371 	}
372 
373 	if( bCheck3 )
374 	{
375 		String sEntry( aTypDLB3.GetSelectEntry() );
376 		if( sEntry == aNumericTxt )
377 			sEntry.Erase();
378 		else if( 0 != (pUserData = aTypDLB3.GetEntryData(
379 											aTypDLB3.GetSelectEntryPos())) )
380 			sEntry = *(String*)pUserData;
381 
382 		SwSortKey *pKey = new SwSortKey( nCol3, sEntry,
383 									bAsc3 ? SRT_ASCENDING : SRT_DESCENDING );
384 		aOptions.aKeys.C40_INSERT( SwSortKey, pKey, aOptions.aKeys.Count() );
385 	}
386 
387 	aOptions.eDirection =  bCol ? SRT_COLUMNS : SRT_ROWS;
388 	aOptions.cDeli = cDeli;
389 	aOptions.nLanguage = nLang;
390 	aOptions.bTable = rSh.IsTableMode();
391 	aOptions.bIgnoreCase = !bCsSens;
392 
393 	sal_Bool bRet;
394 	{
395 		SwWait aWait( *rSh.GetView().GetDocShell(), sal_True );
396 		rSh.StartAllAction();
397 		if( 0 != (bRet = rSh.Sort( aOptions )))
398 			rSh.SetModified();
399 		rSh.EndAllAction();
400 	}
401 
402 	if( !bRet )
403 		InfoBox( this->GetParent(), SW_RES(MSG_SRTERR)).Execute();
404 }
405 
406 /* -----------------30.09.98 10:03-------------------
407  *
408  * --------------------------------------------------*/
409 IMPL_LINK( SwSortDlg, DelimHdl, RadioButton*, pButton )
410 {
411 	sal_Bool bEnable = pButton == &aDelimFreeRB && aDelimFreeRB.IsEnabled();
412 	aDelimEdt.Enable( bEnable );
413 	aDelimPB.Enable( bEnable );
414 	return 0;
415 }
416 
417 IMPL_LINK( SwSortDlg, DelimCharHdl, PushButton*, EMPTYARG )
418 {
419 	SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
420 	if(pFact)
421 	{
422 		SfxAllItemSet aSet( rSh.GetAttrPool() );
423 		aSet.Put( SfxInt32Item( SID_ATTR_CHAR, GetDelimChar() ) );
424         SfxAbstractDialog* pMap = pFact->CreateSfxDialog( &aDelimPB, aSet,
425 			rSh.GetView().GetViewFrame()->GetFrame().GetFrameInterface(), RID_SVXDLG_CHARMAP );
426 		if( RET_OK == pMap->Execute() )
427 		{
428 			SFX_ITEMSET_ARG( pMap->GetOutputItemSet(), pItem, SfxInt32Item, SID_ATTR_CHAR, sal_False );
429 			if ( pItem )
430 				aDelimEdt.SetText( sal_Unicode ( pItem->GetValue() ) );
431 		}
432 
433 		delete pMap;
434 	}
435 	return 0;
436 }
437 
438 
439 IMPL_LINK( SwSortDlg, CheckHdl, CheckBox *, pCheck )
440 {
441 	if( pCheck == ( CheckBox* ) &aRowRB)
442 	{
443 		aColLbl.SetText(aColTxt);
444 		aColEdt1.SetMax(nY);
445 		aColEdt2.SetMax(nY);
446 		aColEdt3.SetMax(nY);
447 
448 		aColEdt1.SetAccessibleName(aColTxt);
449 		aColEdt2.SetAccessibleName(aColTxt);
450 		aColEdt3.SetAccessibleName(aColTxt);
451 	}
452 	else if( pCheck == ( CheckBox* ) &aColumnRB)
453 	{
454 		aColLbl.SetText(aRowTxt);
455 		aColEdt1.SetMax(nX);
456 		aColEdt2.SetMax(nX);
457 		aColEdt3.SetMax(nX);
458 
459 		aColEdt1.SetAccessibleName(aRowTxt);
460 		aColEdt2.SetAccessibleName(aRowTxt);
461 		aColEdt3.SetAccessibleName(aRowTxt);
462 	}
463 	else if(!aKeyCB1.IsChecked() &&
464 				!aKeyCB2.IsChecked() &&
465 					!aKeyCB3.IsChecked())
466 		pCheck->Check(sal_True);
467 	return 0;
468 }
469 
470 IMPL_LINK( SwSortDlg, LanguageHdl, ListBox*, pLBox )
471 {
472 	Locale aLcl( SvxCreateLocale( aLangLB.GetSelectLanguage() ) );
473 	Sequence < OUString > aSeq(
474 							GetAppCollator().listCollatorAlgorithms( aLcl ));
475 
476 	if( !pColRes )
477 		pColRes = new CollatorRessource();
478 
479 	const sal_uInt16 nLstBoxCnt = 3;
480 	ListBox* aLstArr[ nLstBoxCnt ] = { &aTypDLB1, &aTypDLB2, &aTypDLB3 };
481 	sal_uInt16* aTypeArr[ nLstBoxCnt ] = { &nType1, &nType2, &nType3 };
482 	String aOldStrArr[ nLstBoxCnt ];
483 	sal_uInt16 n;
484 
485 	void* pUserData;
486 	for( n = 0; n < nLstBoxCnt; ++n )
487 	{
488 		ListBox* pL = aLstArr[ n ];
489 		if( 0 != (pUserData = pL->GetEntryData( pL->GetSelectEntryPos())) )
490 			aOldStrArr[ n ] = *(String*)pUserData;
491 		::lcl_ClearLstBoxAndDelUserData( *pL );
492 	}
493 
494 	sal_uInt16 nInsPos;
495 	String sAlg, sUINm;
496 	for( long nCnt = 0, nEnd = aSeq.getLength(); nCnt <= nEnd; ++nCnt )
497 	{
498 		if( nCnt < nEnd )
499 			sUINm = pColRes->GetTranslation( sAlg = aSeq[ nCnt ] );
500 		else
501 			sUINm = sAlg = aNumericTxt;
502 
503 		for( n = 0; n < nLstBoxCnt; ++n )
504 		{
505 			ListBox* pL = aLstArr[ n ];
506 			nInsPos = pL->InsertEntry( sUINm );
507 			pL->SetEntryData( nInsPos, new String( sAlg ));
508 			if( pLBox && sAlg == aOldStrArr[ n ] )
509 				pL->SelectEntryPos( nInsPos );
510 		}
511 	}
512 
513 	for( n = 0; n < nLstBoxCnt; ++n )
514 	{
515 		ListBox* pL = aLstArr[ n ];
516 		if( !pLBox )
517 			pL->SelectEntryPos( *aTypeArr[n] );
518 		else if( LISTBOX_ENTRY_NOTFOUND == pL->GetSelectEntryPos() )
519 			pL->SelectEntryPos( 0 );
520 	}
521 	return 0;
522 }
523 
524 
525 
526 
527 
528 
529