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