1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 #undef SC_DLLIMPLEMENTATION
32 
33 
34 
35 //------------------------------------------------------------------
36 
37 #include "scitems.hxx"
38 #include <svx/algitem.hxx>
39 #include <editeng/boxitem.hxx>
40 #include <editeng/brshitem.hxx>
41 #include <editeng/cntritem.hxx>
42 #include <editeng/colritem.hxx>
43 #include <editeng/crsditem.hxx>
44 #include <editeng/fontitem.hxx>
45 #include <editeng/postitem.hxx>
46 #include <editeng/shdditem.hxx>
47 #include <editeng/udlnitem.hxx>
48 #include <editeng/wghtitem.hxx>
49 #include <svl/zforlist.hxx>
50 #include <vcl/msgbox.hxx>
51 #include <comphelper/processfactory.hxx>
52 #include <sfx2/sfxresid.hxx>
53 #include "sc.hrc"
54 #include "scmod.hxx"
55 #include "attrib.hxx"
56 #include "zforauto.hxx"
57 #include "scitems.hxx"
58 #include "global.hxx"
59 #include "globstr.hrc"
60 #include "autoform.hxx"
61 #include "strindlg.hxx"
62 #include "miscdlgs.hrc"
63 #include "scuiautofmt.hxx"
64 #include "scresid.hxx"
65 #include "document.hxx"
66 
67 //========================================================================
68 // AutoFormat-Dialog:
69 
70 ScAutoFormatDlg::ScAutoFormatDlg( Window*					pParent,
71 								  ScAutoFormat* 			pAutoFormat,
72                                   const ScAutoFormatData*   pSelFormatData,
73                                   ScDocument*               pDoc ) :
74 
75 	ModalDialog 	( pParent, ScResId( RID_SCDLG_AUTOFORMAT ) ),
76 	//
77     aFlFormat       ( this, ScResId( FL_FORMAT ) ),
78 	aLbFormat		( this, ScResId( LB_FORMAT ) ),
79     pWndPreview     ( new ScAutoFmtPreview( this, ScResId( WND_PREVIEW ), pDoc ) ),
80 	aBtnOk			( this, ScResId( BTN_OK ) ),
81 	aBtnCancel		( this, ScResId( BTN_CANCEL ) ),
82 	aBtnHelp		( this, ScResId( BTN_HELP ) ),
83 	aBtnAdd 		( this, ScResId( BTN_ADD ) ),
84 	aBtnRemove		( this, ScResId( BTN_REMOVE ) ),
85 	aBtnMore		( this, ScResId( BTN_MORE ) ),
86     aFlFormatting   ( this, ScResId( FL_FORMATTING ) ),
87 	aBtnNumFormat	( this, ScResId( BTN_NUMFORMAT ) ),
88 	aBtnBorder		( this, ScResId( BTN_BORDER ) ),
89 	aBtnFont		( this, ScResId( BTN_FONT ) ),
90 	aBtnPattern 	( this, ScResId( BTN_PATTERN ) ),
91 	aBtnAlignment	( this, ScResId( BTN_ALIGNMENT ) ),
92 	aBtnAdjust		( this, ScResId( BTN_ADJUST ) ),
93 	aBtnRename		( this, ScResId( BTN_RENAME ) ),
94     aStrTitle       ( ScResId( STR_ADD_TITLE ) ),
95     aStrLabel       ( ScResId( STR_ADD_LABEL ) ),
96     aStrClose       ( ScResId( STR_BTN_CLOSE ) ),
97     aStrDelTitle    ( ScResId( STR_DEL_TITLE ) ),
98     aStrDelMsg      ( ScResId( STR_DEL_MSG ) ) ,
99     aStrRename      ( ScResId( STR_RENAME_TITLE ) ),
100 	//
101 	pFormat 		( pAutoFormat ),
102 	pSelFmtData 	( pSelFormatData ),
103 	nIndex			( 0 ),
104 	bCoreDataChanged( sal_False ),
105 	bFmtInserted	( sal_False )
106 {
107 	Init();
108 	pWndPreview->NotifyChange( (*pFormat)[0] );
109 	FreeResource();
110 }
111 
112 //------------------------------------------------------------------------
113 
114 __EXPORT ScAutoFormatDlg::~ScAutoFormatDlg()
115 {
116 	delete pWndPreview;
117 }
118 
119 //------------------------------------------------------------------------
120 
121 void ScAutoFormatDlg::Init()
122 {
123 	sal_uInt16 nCount;
124 	String aEntry;
125 
126 	aLbFormat	 .SetSelectHdl( LINK( this, ScAutoFormatDlg, SelFmtHdl ) );
127 	aBtnNumFormat.SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
128 	aBtnBorder	 .SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
129 	aBtnFont	 .SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
130 	aBtnPattern  .SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
131 	aBtnAlignment.SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
132 	aBtnAdjust	 .SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
133 	aBtnAdd 	 .SetClickHdl ( LINK( this, ScAutoFormatDlg, AddHdl ) );
134 	aBtnRemove	 .SetClickHdl ( LINK( this, ScAutoFormatDlg, RemoveHdl ) );
135 	aBtnOk		 .SetClickHdl ( LINK( this, ScAutoFormatDlg, CloseHdl ) );
136 	aBtnCancel	 .SetClickHdl ( LINK( this, ScAutoFormatDlg, CloseHdl ) );
137 	aBtnRename	 .SetClickHdl ( LINK( this, ScAutoFormatDlg, RenameHdl ) );
138 	aLbFormat	 .SetDoubleClickHdl( LINK( this, ScAutoFormatDlg, DblClkHdl ) );
139 
140 	aBtnMore.AddWindow( &aBtnRename );
141 	aBtnMore.AddWindow( &aBtnNumFormat );
142 	aBtnMore.AddWindow( &aBtnBorder );
143 	aBtnMore.AddWindow( &aBtnFont );
144 	aBtnMore.AddWindow( &aBtnPattern );
145 	aBtnMore.AddWindow( &aBtnAlignment );
146 	aBtnMore.AddWindow( &aBtnAdjust );
147     aBtnMore.AddWindow( &aFlFormatting );
148 
149 	nCount = pFormat->GetCount();
150 
151 	for ( sal_uInt16 i = 0; i < nCount; i++ )
152 	{
153 		((*pFormat)[i])->GetName( aEntry );
154 		aLbFormat.InsertEntry( aEntry );
155 	}
156 
157 	if ( nCount == 1 )
158 		aBtnRemove.Disable();
159 
160 	aLbFormat.SelectEntryPos( 0 );
161 	aBtnRename.Disable();
162 	aBtnRemove.Disable();
163 
164 	nIndex = 0;
165 	UpdateChecks();
166 
167 	if ( !pSelFmtData )
168 	{
169 		aBtnAdd.Disable();
170 		aBtnRemove.Disable();
171 		bFmtInserted = sal_True;
172 	}
173 }
174 
175 //------------------------------------------------------------------------
176 
177 void ScAutoFormatDlg::UpdateChecks()
178 {
179 	ScAutoFormatData* pData = (*pFormat)[nIndex];
180 
181 	aBtnNumFormat.Check( pData->GetIncludeValueFormat() );
182 	aBtnBorder	 .Check( pData->GetIncludeFrame() );
183 	aBtnFont	 .Check( pData->GetIncludeFont() );
184 	aBtnPattern  .Check( pData->GetIncludeBackground() );
185 	aBtnAlignment.Check( pData->GetIncludeJustify() );
186 	aBtnAdjust	 .Check( pData->GetIncludeWidthHeight() );
187 }
188 
189 //------------------------------------------------------------------------
190 // Handler:
191 //---------
192 
193 IMPL_LINK( ScAutoFormatDlg, CloseHdl, PushButton *, pBtn )
194 {
195 	if ( pBtn == &aBtnOk || pBtn == &aBtnCancel )
196 	{
197 		if ( bCoreDataChanged )
198 			ScGlobal::GetAutoFormat()->Save();
199 
200 		EndDialog( (pBtn == &aBtnOk) ? RET_OK : RET_CANCEL );
201 	}
202 	return 0;
203 }
204 
205 //------------------------------------------------------------------------
206 
207 IMPL_LINK_INLINE_START( ScAutoFormatDlg, DblClkHdl, void *, EMPTYARG )
208 {
209 	if ( bCoreDataChanged )
210 		ScGlobal::GetAutoFormat()->Save();
211 
212 	EndDialog( RET_OK );
213 	return 0;
214 }
215 IMPL_LINK_INLINE_END( ScAutoFormatDlg, DblClkHdl, void *, EMPTYARG )
216 
217 //------------------------------------------------------------------------
218 
219 IMPL_LINK( ScAutoFormatDlg, CheckHdl, Button *, pBtn )
220 {
221 	ScAutoFormatData* pData  = (*pFormat)[nIndex];
222 	sal_Bool			  bCheck = ((CheckBox*)pBtn)->IsChecked();
223 
224 	if ( pBtn == &aBtnNumFormat )
225 		pData->SetIncludeValueFormat( bCheck );
226 	else if ( pBtn == &aBtnBorder )
227 		pData->SetIncludeFrame( bCheck );
228 	else if ( pBtn == &aBtnFont )
229 		pData->SetIncludeFont( bCheck );
230 	else if ( pBtn == &aBtnPattern )
231 		pData->SetIncludeBackground( bCheck );
232 	else if ( pBtn == &aBtnAlignment )
233 		pData->SetIncludeJustify( bCheck );
234 	else if ( pBtn == &aBtnAdjust )
235 		pData->SetIncludeWidthHeight( bCheck );
236 
237 	if ( !bCoreDataChanged )
238 	{
239 		aBtnCancel.SetText( aStrClose );
240 		bCoreDataChanged = sal_True;
241 	}
242 
243 	pWndPreview->NotifyChange( pData );
244 
245 	return 0;
246 }
247 
248 //------------------------------------------------------------------------
249 
250 IMPL_LINK( ScAutoFormatDlg, AddHdl, void *, EMPTYARG )
251 {
252 	if ( !bFmtInserted && pSelFmtData )
253 	{
254 		String				aStrStandard( SfxResId(STR_STANDARD) );
255 		String				aFormatName;
256 		ScStringInputDlg*	pDlg;
257 		sal_Bool				bOk = sal_False;
258 
259 		while ( !bOk )
260 		{
261 			pDlg = new ScStringInputDlg( this,
262 										 aStrTitle,
263 										 aStrLabel,
264 										 aFormatName,
265 										 HID_SC_ADD_AUTOFMT, HID_SC_AUTOFMT_NAME );
266 
267 			if ( pDlg->Execute() == RET_OK )
268 			{
269 				pDlg->GetInputString( aFormatName );
270 
271 				if ( (aFormatName.Len() > 0) && (aFormatName != aStrStandard) )
272 				{
273 					ScAutoFormatData* pNewData
274 						= new ScAutoFormatData( *pSelFmtData );
275 
276 					pNewData->SetName( aFormatName );
277 					bFmtInserted = pFormat->Insert( pNewData );
278 
279 					if ( bFmtInserted )
280 					{
281 						sal_uInt16 nAt = pFormat->IndexOf( pNewData );
282 
283 						aLbFormat.InsertEntry( aFormatName, nAt );
284 						aLbFormat.SelectEntry( aFormatName );
285 						aBtnAdd.Disable();
286 
287 						if ( !bCoreDataChanged )
288 						{
289 							aBtnCancel.SetText( aStrClose );
290 							bCoreDataChanged = sal_True;
291 						}
292 
293 						SelFmtHdl( 0 );
294 						bOk = sal_True;
295 					}
296 					else
297 						delete pNewData;
298 
299 				}
300 
301 				if ( !bFmtInserted )
302 				{
303 					sal_uInt16 nRet = ErrorBox( this,
304 											WinBits( WB_OK_CANCEL | WB_DEF_OK),
305 											ScGlobal::GetRscString(STR_INVALID_AFNAME)
306 										  ).Execute();
307 
308 					bOk = ( nRet == RET_CANCEL );
309 				}
310 			}
311 			else
312 				bOk = sal_True;
313 
314 			delete pDlg;
315 		}
316 	}
317 
318 	return 0;
319 }
320 
321 //------------------------------------------------------------------------
322 
323 IMPL_LINK( ScAutoFormatDlg, RemoveHdl, void *, EMPTYARG )
324 {
325 	if ( (nIndex > 0) && (aLbFormat.GetEntryCount() > 0) )
326 	{
327 		String aMsg( aStrDelMsg.GetToken( 0, '#' ) );
328 
329 		aMsg += aLbFormat.GetSelectEntry();
330 		aMsg += aStrDelMsg.GetToken( 1, '#' );
331 
332 		if ( RET_YES ==
333 			 QueryBox( this, WinBits( WB_YES_NO | WB_DEF_YES ), aMsg ).Execute() )
334 		{
335 			aLbFormat.RemoveEntry( nIndex );
336 			aLbFormat.SelectEntryPos( nIndex-1 );
337 
338 			if ( nIndex-1 == 0 )
339 				aBtnRemove.Disable();
340 
341 			if ( !bCoreDataChanged )
342 			{
343 				aBtnCancel.SetText( aStrClose );
344 				bCoreDataChanged = sal_True;
345 			}
346 
347 			pFormat->AtFree( nIndex ); // in der Core loeschen
348 			nIndex--;
349 
350 			SelFmtHdl( 0 );
351 		}
352 	}
353 
354 	SelFmtHdl( 0 );
355 
356 	return 0;
357 }
358 
359 IMPL_LINK( ScAutoFormatDlg, RenameHdl, void *, EMPTYARG )
360 {
361 	sal_Bool bOk = sal_False;
362 	while( !bOk )
363 	{
364 
365 		String aFormatName=aLbFormat.GetSelectEntry();
366 		String aEntry;
367 
368 		ScStringInputDlg* pDlg = new ScStringInputDlg( this,
369 										 aStrRename,
370 										 aStrLabel,
371 										 aFormatName,
372                                          HID_SC_REN_AFMT_DLG, HID_SC_REN_AFMT_NAME );
373 		if( pDlg->Execute() == RET_OK )
374 		{
375 			sal_Bool bFmtRenamed = sal_False;
376 			pDlg->GetInputString( aFormatName );
377 			sal_uInt16 n;
378 
379 			if ( aFormatName.Len() > 0 )
380 			{
381 				for( n = 0; n < pFormat->GetCount(); ++n )
382 				{
383 					(*pFormat)[n]->GetName(aEntry);
384 					if ( aEntry== aFormatName)
385 						break;
386 				}
387 				if( n >= pFormat->GetCount() )
388 				{
389 					// Format mit dem Namen noch nicht vorhanden, also
390 					// umbenennen
391 
392 					aLbFormat.RemoveEntry(nIndex );
393 					ScAutoFormatData* p=(*pFormat)[ nIndex ];
394 					ScAutoFormatData* pNewData
395 						= new ScAutoFormatData(*p);
396 
397 					pFormat->AtFree( nIndex );
398 
399 					pNewData->SetName( aFormatName );
400 
401 					pFormat->Insert( pNewData);
402 
403 					sal_uInt16 nCount = pFormat->GetCount();
404 
405 					aLbFormat.SetUpdateMode(sal_False);
406 					aLbFormat.Clear();
407 					for ( sal_uInt16 i = 0; i < nCount; i++ )
408 					{
409 						((*pFormat)[i])->GetName( aEntry );
410 						aLbFormat.InsertEntry( aEntry );
411 					}
412 
413 					aLbFormat.SetUpdateMode( sal_True);
414 					aLbFormat.SelectEntry( aFormatName);
415 
416 					if ( !bCoreDataChanged )
417 					{
418 						aBtnCancel.SetText( aStrClose );
419 						bCoreDataChanged = sal_True;
420 					}
421 
422 
423 					SelFmtHdl( 0 );
424 					bOk = sal_True;
425 					bFmtRenamed = sal_True;
426 				}
427 			}
428 			if( !bFmtRenamed )
429 			{
430 				bOk = RET_CANCEL == ErrorBox( this,
431 									WinBits( WB_OK_CANCEL | WB_DEF_OK),
432 									ScGlobal::GetRscString(STR_INVALID_AFNAME)
433 									).Execute();
434 			}
435 		}
436 		else
437 			bOk = sal_True;
438 		delete pDlg;
439 	}
440 
441 	return 0;
442 }
443 
444 //------------------------------------------------------------------------
445 
446 IMPL_LINK( ScAutoFormatDlg, SelFmtHdl, void *, EMPTYARG )
447 {
448 	nIndex = aLbFormat.GetSelectEntryPos();
449 	UpdateChecks();
450 
451 	if ( nIndex == 0 )
452 	{
453 		aBtnRename.Disable();
454 		aBtnRemove.Disable();
455 	}
456 	else
457 	{
458 		aBtnRename.Enable();
459 		aBtnRemove.Enable();
460 	}
461 
462 	pWndPreview->NotifyChange( (*pFormat)[nIndex] );
463 
464 	return 0;
465 }
466 
467 //------------------------------------------------------------------------
468 
469 String __EXPORT ScAutoFormatDlg::GetCurrFormatName()
470 {
471 	String	aResult;
472 
473 	((*pFormat)[nIndex])->GetName( aResult );
474 
475 	return aResult;
476 }
477