xref: /trunk/main/sw/source/ui/table/tautofmt.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 
27 #ifdef SW_DLLIMPLEMENTATION
28 #undef SW_DLLIMPLEMENTATION
29 #endif
30 
31 
32 
33 
34 #ifndef _EDIT_HXX //autogen
35 #include <vcl/edit.hxx>
36 #endif
37 #ifndef _MSGBOX_HXX //autogen
38 #include <vcl/msgbox.hxx>
39 #endif
40 #include <vcl/svapp.hxx>
41 #include <svl/zforlist.hxx>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/i18n/XBreakIterator.hpp>
44 #include <comphelper/processfactory.hxx>
45 #include <svtools/scriptedtext.hxx>
46 #include <svtools/accessibilityoptions.hxx>
47 #include <svx/framelinkarray.hxx>
48 #include "swmodule.hxx"
49 #include "swtypes.hxx"
50 #ifndef _VIEW_HXX
51 #include "view.hxx"
52 #endif
53 #include "wrtsh.hxx"
54 #include "tblafmt.hxx"
55 #ifndef _TAUTOFMT_HXX
56 #include "tautofmt.hxx"
57 #endif
58 #include "shellres.hxx"
59 #ifndef _TAUTOFMT_HRC
60 #include "tautofmt.hrc"
61 #endif
62 
63 using namespace com::sun::star;
64 
65 #define FRAME_OFFSET 4
66 
67 //========================================================================
68 
69 class AutoFmtPreview : public Window
70 {
71 public:
72             AutoFmtPreview( Window* pParent, const ResId& rRes, SwWrtShell* pWrtShell );
73 			~AutoFmtPreview();
74 
75 	void NotifyChange( const SwTableAutoFmt& rNewData );
76 
77 protected:
78 	virtual void Paint( const Rectangle& rRect );
79 
80 private:
81     SwTableAutoFmt          aCurData;
82     VirtualDevice           aVD;
83     SvtScriptedTextHelper   aScriptedText;
84     svx::frame::Array       maArray;            /// Implementation to draw the frame borders.
85     sal_Bool                    bFitWidth;
86     bool                    mbRTL;
87     Size                    aPrvSize;
88     long                    nLabelColWidth;
89     long                    nDataColWidth1;
90     long                    nDataColWidth2;
91     long                    nRowHeight;
92     const String            aStrJan;
93     const String            aStrFeb;
94     const String            aStrMar;
95     const String            aStrNorth;
96     const String            aStrMid;
97     const String            aStrSouth;
98     const String            aStrSum;
99     SvNumberFormatter*      pNumFmt;
100 
101     uno::Reference< lang::XMultiServiceFactory > m_xMSF;
102     uno::Reference< i18n::XBreakIterator >       m_xBreak;
103 
104 	//-------------------------------------------
105 	void	Init			();
106 	void	DoPaint			( const Rectangle& rRect );
107 	void	CalcCellArray	( sal_Bool bFitWidth );
108 	void	CalcLineMap		();
109 	void	PaintCells		();
110 
111     sal_uInt8                GetFormatIndex( size_t nCol, size_t nRow ) const;
112     const SvxBoxItem&   GetBoxItem( size_t nCol, size_t nRow ) const;
113 
114     void                DrawString( size_t nCol, size_t nRow );
115     void                DrawStrings();
116     void                DrawBackground();
117 
118     void    MakeFonts       ( sal_uInt8 nIndex, Font& rFont, Font& rCJKFont, Font& rCTLFont );
119 	String	MakeNumberString( String cellString, sal_Bool bAddDec );
120 };
121 
122 //========================================================================
123 
124 class SwStringInputDlg : public ModalDialog
125 {
126 public:
127 			SwStringInputDlg( 	  Window* pParent,
128 							const String& rTitle,
129 							const String& rEditTitle,
130 							const String& rDefault );
131 			~SwStringInputDlg();
132 
133 	void GetInputString( String& rString ) const;
134 
135 private:
136 	Edit			aEdInput;	// Edit erhaelt so den Focus
137 	FixedText		aFtEditTitle;
138 	OKButton		aBtnOk;
139 	CancelButton	aBtnCancel;
140 };
141 
142 
SwStringInputDlg(Window * pParent,const String & rTitle,const String & rEditTitle,const String & rDefault)143 SwStringInputDlg::SwStringInputDlg( Window* 		pParent,
144 									const String&	rTitle,
145 									const String&	rEditTitle,
146 									const String&	rDefault	) :
147 	ModalDialog 	( pParent, SW_RES( DLG_SWDLG_STRINPUT ) ),
148 	//
149 	aEdInput		( this, SW_RES( ED_INPUT ) ),
150     aFtEditTitle    ( this, SW_RES( FT_LABEL ) ),
151 	aBtnOk			( this, SW_RES( BTN_OK ) ),
152     aBtnCancel      ( this, SW_RES( BTN_CANCEL ) )
153 {
154 	SetText( rTitle );
155 	aFtEditTitle.SetText( rEditTitle );
156 	aEdInput.SetText( rDefault );
157 	//-------------
158 	FreeResource();
159 }
160 
161 //------------------------------------------------------------------------
162 
GetInputString(String & rString) const163 void SwStringInputDlg::GetInputString( String& rString ) const
164 {
165 	rString = aEdInput.GetText();
166 }
167 
168 
~SwStringInputDlg()169 __EXPORT SwStringInputDlg::~SwStringInputDlg()
170 {
171 }
172 
173 //========================================================================
174 // AutoFormat-Dialog:
175 
176 
SwAutoFormatDlg(Window * pParent,SwWrtShell * pWrtShell,sal_Bool bSetAutoFormat,const SwTableAutoFmt * pSelFmt)177 SwAutoFormatDlg::SwAutoFormatDlg( Window* pParent, SwWrtShell* pWrtShell,
178 					sal_Bool bSetAutoFormat, const SwTableAutoFmt* pSelFmt )
179 	: SfxModalDialog( pParent, SW_RES( DLG_AUTOFMT_TABLE ) ),
180 	//
181     aFlFormat       ( this, SW_RES( FL_FORMAT ) ),
182 	aLbFormat		( this, SW_RES( LB_FORMAT ) ),
183     aFlFormats       ( this, SW_RES( FL_FORMATS ) ),
184 
185 	aBtnNumFormat	( this, SW_RES( BTN_NUMFORMAT ) ),
186 	aBtnBorder		( this, SW_RES( BTN_BORDER ) ),
187 	aBtnFont		( this, SW_RES( BTN_FONT ) ),
188 	aBtnPattern 	( this, SW_RES( BTN_PATTERN ) ),
189 	aBtnAlignment	( this, SW_RES( BTN_ALIGNMENT ) ),
190 	aBtnOk			( this, SW_RES( BTN_OK ) ),
191 	aBtnCancel		( this, SW_RES( BTN_CANCEL ) ),
192 	aBtnHelp		( this, SW_RES( BTN_HELP ) ),
193 	aBtnAdd 		( this, SW_RES( BTN_ADD ) ),
194 	aBtnRemove		( this, SW_RES( BTN_REMOVE ) ),
195     aBtnRename      ( this, SW_RES( BTN_RENAME ) ),
196     aBtnMore        ( this, SW_RES( BTN_MORE ) ),
197 	aStrTitle		( SW_RES( STR_ADD_TITLE ) ),
198 	aStrLabel		( SW_RES( STR_ADD_LABEL ) ),
199 	aStrClose		( SW_RES( STR_BTN_CLOSE ) ),
200 	aStrDelTitle	( SW_RES( STR_DEL_TITLE ) ),
201 	aStrDelMsg		( SW_RES( STR_DEL_MSG ) ),
202 	aStrRenameTitle	( SW_RES( STR_RENAME_TITLE ) ),
203 	aStrInvalidFmt	( SW_RES( STR_INVALID_AFNAME )),
204     pWndPreview     ( new AutoFmtPreview( this, SW_RES( WND_PREVIEW ), pWrtShell )),
205 	//
206     pShell          ( pWrtShell ),
207 	nIndex			( 0 ),
208 	nDfltStylePos	( 0 ),
209 	bCoreDataChanged( sal_False ),
210     bSetAutoFmt     ( bSetAutoFormat )
211 {
212 	pTableTbl = new SwTableAutoFmtTbl;
213 	pTableTbl->Load();
214 
215 	Init( pSelFmt );
216 	//------------- >
217 	FreeResource();
218 }
219 
220 //------------------------------------------------------------------------
221 
222 
~SwAutoFormatDlg()223 __EXPORT SwAutoFormatDlg::~SwAutoFormatDlg()
224 {
225 	delete pWndPreview;
226 
227 	if( bCoreDataChanged )
228 		pTableTbl->Save();
229 	delete pTableTbl;
230 }
231 
232 //------------------------------------------------------------------------
233 
234 
Init(const SwTableAutoFmt * pSelFmt)235 void SwAutoFormatDlg::Init( const SwTableAutoFmt* pSelFmt )
236 {
237 	Link aLk( LINK( this, SwAutoFormatDlg, CheckHdl ) );
238 	aBtnBorder.SetClickHdl( aLk );
239 	aBtnFont.SetClickHdl( aLk );
240 	aBtnPattern.SetClickHdl( aLk );
241 	aBtnAlignment.SetClickHdl( aLk );
242 	aBtnNumFormat.SetClickHdl( aLk );
243 
244 	aBtnAdd.SetClickHdl ( LINK( this, SwAutoFormatDlg, AddHdl ) );
245 	aBtnRemove.SetClickHdl ( LINK( this, SwAutoFormatDlg, RemoveHdl ) );
246 	aBtnRename.SetClickHdl ( LINK( this, SwAutoFormatDlg, RenameHdl ) );
247 	aBtnOk.SetClickHdl ( LINK( this, SwAutoFormatDlg, OkHdl ) );
248 	aLbFormat.SetSelectHdl( LINK( this, SwAutoFormatDlg, SelFmtHdl ) );
249 
250 	aBtnMore.AddWindow( &aBtnNumFormat );
251 	aBtnMore.AddWindow( &aBtnBorder );
252 	aBtnMore.AddWindow( &aBtnFont );
253 	aBtnMore.AddWindow( &aBtnPattern );
254 	aBtnMore.AddWindow( &aBtnAlignment );
255     aBtnMore.AddWindow( &aFlFormats );
256 	aBtnMore.AddWindow( &aBtnRename );
257 
258 	aBtnAdd.Enable( bSetAutoFmt );
259 
260 	nIndex = 0;
261 	if( !bSetAutoFmt )
262 	{
263 		// dann muss die Liste um den Eintrag <Keins> erweitert werden.
264 		aLbFormat.InsertEntry( ViewShell::GetShellRes()->aStrNone );
265 		nDfltStylePos = 1;
266 		nIndex = 255;
267 	}
268 
269 	for( sal_uInt8 i = 0, nCount = (sal_uInt8)pTableTbl->Count(); i < nCount; i++ )
270 	{
271 		SwTableAutoFmt* pFmt = (*pTableTbl)[ i ];
272 		aLbFormat.InsertEntry( pFmt->GetName() );
273 		if( pSelFmt && pFmt->GetName() == pSelFmt->GetName() )
274 			nIndex = i;
275 	}
276 
277 	aLbFormat.SelectEntryPos( 255 != nIndex ? (nDfltStylePos + nIndex) : 0 );
278 	SelFmtHdl( 0 );
279 }
280 
281 //------------------------------------------------------------------------
282 
283 
UpdateChecks(const SwTableAutoFmt & rFmt,sal_Bool bEnable)284 void SwAutoFormatDlg::UpdateChecks( const SwTableAutoFmt& rFmt, sal_Bool bEnable )
285 {
286 	aBtnNumFormat.Enable( bEnable );
287 	aBtnNumFormat.Check( rFmt.IsValueFormat() );
288 
289 	aBtnBorder.Enable( bEnable );
290 	aBtnBorder.Check( rFmt.IsFrame() );
291 
292 	aBtnFont.Enable( bEnable );
293 	aBtnFont.Check( rFmt.IsFont() );
294 
295 	aBtnPattern.Enable( bEnable );
296 	aBtnPattern.Check( rFmt.IsBackground() );
297 
298 	aBtnAlignment.Enable( bEnable );
299 	aBtnAlignment.Check( rFmt.IsJustify() );
300 }
301 
FillAutoFmtOfIndex(SwTableAutoFmt * & rToFill) const302 void SwAutoFormatDlg::FillAutoFmtOfIndex( SwTableAutoFmt*& rToFill ) const
303 {
304 	if( 255 != nIndex )
305 	{
306 		if( rToFill )
307 			*rToFill = *(*pTableTbl)[ nIndex ];
308 		else
309 			rToFill = new SwTableAutoFmt( *(*pTableTbl)[ nIndex ] );
310 	}
311 	else if( rToFill )
312 		delete rToFill, rToFill = 0;
313 }
314 
315 
316 /*------------------------------------------------------------------------
317   Handler:
318   ---------*/
319 
320 
IMPL_LINK(SwAutoFormatDlg,CheckHdl,Button *,pBtn)321 IMPL_LINK( SwAutoFormatDlg, CheckHdl, Button *, pBtn )
322 {
323 	SwTableAutoFmtPtr pData  = (*pTableTbl)[nIndex];
324 	sal_Bool bCheck = ((CheckBox*)pBtn)->IsChecked(), bDataChgd = sal_True;
325 
326 	if( pBtn == &aBtnNumFormat )
327 		pData->SetValueFormat( bCheck );
328 	else if ( pBtn == &aBtnBorder )
329 		pData->SetFrame( bCheck );
330 	else if ( pBtn == &aBtnFont )
331 		pData->SetFont( bCheck );
332 	else if ( pBtn == &aBtnPattern )
333 		pData->SetBackground( bCheck );
334 	else if ( pBtn == &aBtnAlignment )
335 		pData->SetJustify( bCheck );
336 //	  else if ( pBtn == &aBtnAdjust )
337 //		  pData->SetIncludeWidthHeight( bCheck );
338 	else
339 		bDataChgd = sal_False;
340 
341 	if( bDataChgd )
342 	{
343 		if( !bCoreDataChanged )
344 		{
345 			aBtnCancel.SetText( aStrClose );
346 			bCoreDataChanged = sal_True;
347 		}
348 
349 		pWndPreview->NotifyChange( *pData );
350 	}
351 	return 0;
352 }
353 
354 /*------------------------------------------------------------------------*/
355 
356 
IMPL_LINK(SwAutoFormatDlg,AddHdl,void *,EMPTYARG)357 IMPL_LINK( SwAutoFormatDlg, AddHdl, void *, EMPTYARG )
358 {
359 	sal_Bool bOk = sal_False, bFmtInserted = sal_False;
360 	while( !bOk )
361 	{
362 		SwStringInputDlg*	pDlg = new SwStringInputDlg( this,
363 															aStrTitle,
364 															aStrLabel,
365 															aEmptyStr );
366 		if( RET_OK == pDlg->Execute() )
367 		{
368 			String aFormatName;
369 			pDlg->GetInputString( aFormatName );
370 
371 			if( aFormatName.Len() > 0 )
372 			{
373 				sal_uInt16 n;
374 				for( n = 0; n < pTableTbl->Count(); ++n )
375 					if( (*pTableTbl)[n]->GetName() == aFormatName )
376 						break;
377 
378 				if( n >= pTableTbl->Count() )
379 				{
380 					// Format mit dem Namen noch nicht vorhanden, also
381 					// aufnehmen
382 					SwTableAutoFmtPtr pNewData = new
383 										SwTableAutoFmt( aFormatName );
384 					pShell->GetTableAutoFmt( *pNewData );
385 
386 					// Sortiert einfuegen!!
387 					for( n = 1; n < pTableTbl->Count(); ++n )
388 						if( (*pTableTbl)[ n ]->GetName() > aFormatName )
389 							break;
390 
391 					pTableTbl->Insert( pNewData, n );
392 					aLbFormat.InsertEntry( aFormatName, nDfltStylePos + n );
393 					aLbFormat.SelectEntryPos( nDfltStylePos + n );
394 					bFmtInserted = sal_True;
395 					aBtnAdd.Enable( sal_False );
396 					if ( !bCoreDataChanged )
397 					{
398 						aBtnCancel.SetText( aStrClose );
399 						bCoreDataChanged = sal_True;
400 					}
401 
402 					SelFmtHdl( 0 );
403 					bOk = sal_True;
404 				}
405 			}
406 
407 			if( !bFmtInserted )
408 			{
409 				bOk = RET_CANCEL == ErrorBox( this,
410 									WinBits( WB_OK_CANCEL | WB_DEF_OK),
411 									aStrInvalidFmt
412 									).Execute();
413 			}
414 		}
415 		else
416 			bOk = sal_True;
417 		delete pDlg;
418 	}
419 	return 0;
420 }
421 
422 //------------------------------------------------------------------------
423 
IMPL_LINK(SwAutoFormatDlg,RemoveHdl,void *,EMPTYARG)424 IMPL_LINK( SwAutoFormatDlg, RemoveHdl, void *, EMPTYARG )
425 {
426 	String aMessage	= aStrDelMsg ;
427 	aMessage.AppendAscii("\n\n");
428 	aMessage += aLbFormat.GetSelectEntry() ;
429 	aMessage += '\n';
430 
431 	MessBox* pBox = new MessBox( this, WinBits( WB_OK_CANCEL ),
432 									aStrDelTitle, aMessage);
433 
434 	if ( pBox->Execute() == RET_OK )
435 	{
436 		aLbFormat.RemoveEntry( nDfltStylePos + nIndex );
437 		aLbFormat.SelectEntryPos( nDfltStylePos + nIndex-1 );
438 
439 		pTableTbl->DeleteAndDestroy( nIndex );
440 		nIndex--;
441 
442 		if( !nIndex )
443 		{
444 			aBtnRemove.Enable(sal_False);
445 			aBtnRename.Enable(sal_False);
446 		}
447 
448 		if( !bCoreDataChanged )
449 		{
450 			aBtnCancel.SetText( aStrClose );
451 			bCoreDataChanged = sal_True;
452 		}
453 	}
454 	delete pBox;
455 
456 	SelFmtHdl( 0 );
457 
458 	return 0;
459 }
460 
IMPL_LINK(SwAutoFormatDlg,RenameHdl,void *,EMPTYARG)461 IMPL_LINK( SwAutoFormatDlg, RenameHdl, void *, EMPTYARG )
462 {
463 	sal_Bool bOk = sal_False;
464 	while( !bOk )
465 	{
466 		SwStringInputDlg* pDlg = new SwStringInputDlg( this,
467 						aStrRenameTitle, aLbFormat.GetSelectEntry(),
468 														aEmptyStr );
469 		if( pDlg->Execute() == RET_OK )
470 		{
471 			sal_Bool bFmtRenamed = sal_False;
472 			String aFormatName;
473 			pDlg->GetInputString( aFormatName );
474 
475 			if ( aFormatName.Len() > 0 )
476 			{
477 				sal_uInt16 n;
478 				for( n = 0; n < pTableTbl->Count(); ++n )
479 					if ((*pTableTbl)[n]->GetName() == aFormatName)
480 						break;
481 
482 				if( n >= pTableTbl->Count() )
483 				{
484 					// Format mit dem Namen noch nicht vorhanden, also
485 					// umbenennen
486 
487 					aLbFormat.RemoveEntry( nDfltStylePos + nIndex );
488 					SwTableAutoFmtPtr p = (*pTableTbl)[ nIndex ];
489 					pTableTbl->Remove( nIndex );
490 
491 					p->SetName( aFormatName );
492 
493 					// Sortiert einfuegen!!
494 					for( n = 1; n < pTableTbl->Count(); ++n )
495 						if( (*pTableTbl)[ n ]->GetName() > aFormatName )
496 							break;
497 
498 					pTableTbl->Insert( p, n );
499 					aLbFormat.InsertEntry( aFormatName, nDfltStylePos + n );
500 					aLbFormat.SelectEntryPos( nDfltStylePos + n );
501 
502 					if ( !bCoreDataChanged )
503 					{
504 						aBtnCancel.SetText( aStrClose );
505 						bCoreDataChanged = sal_True;
506 					}
507 
508 					SelFmtHdl( 0 );
509 					bOk = sal_True;
510 					bFmtRenamed = sal_True;
511 				}
512 			}
513 
514 			if( !bFmtRenamed )
515 			{
516 				bOk = RET_CANCEL == ErrorBox( this,
517 									WinBits( WB_OK_CANCEL | WB_DEF_OK),
518 									aStrInvalidFmt
519 									).Execute();
520 			}
521 		}
522 		else
523 			bOk = sal_True;
524 		delete pDlg;
525 	}
526 	return 0;
527 }
528 
529 //------------------------------------------------------------------------
530 
IMPL_LINK(SwAutoFormatDlg,SelFmtHdl,void *,EMPTYARG)531 IMPL_LINK( SwAutoFormatDlg, SelFmtHdl, void *, EMPTYARG )
532 {
533 	sal_Bool bBtnEnable = sal_False;
534 	sal_uInt8 nSelPos = (sal_uInt8) aLbFormat.GetSelectEntryPos(), nOldIdx = nIndex;
535 	if( nSelPos >= nDfltStylePos )
536 	{
537 		nIndex = nSelPos - nDfltStylePos;
538 		pWndPreview->NotifyChange( *(*pTableTbl)[nIndex] );
539 		bBtnEnable = 0 != nIndex;
540 		UpdateChecks( *(*pTableTbl)[nIndex], sal_True );
541 	}
542 	else
543 	{
544 		nIndex = 255;
545 
546 		SwTableAutoFmt aTmp( ViewShell::GetShellRes()->aStrNone );
547 		aTmp.SetFont( sal_False );
548 		aTmp.SetJustify( sal_False );
549 		aTmp.SetFrame( sal_False );
550 		aTmp.SetBackground( sal_False );
551 		aTmp.SetValueFormat( sal_False );
552 		aTmp.SetWidthHeight( sal_False );
553 
554 		if( nOldIdx != nIndex )
555 			pWndPreview->NotifyChange( aTmp );
556 		UpdateChecks( aTmp, sal_False );
557 	}
558 
559 	aBtnRemove.Enable( bBtnEnable );
560 	aBtnRename.Enable( bBtnEnable );
561 
562 	return 0;
563 }
564 //------------------------------------------------------------------------
565 
IMPL_LINK_INLINE_START(SwAutoFormatDlg,OkHdl,Button *,EMPTYARG)566 IMPL_LINK_INLINE_START( SwAutoFormatDlg, OkHdl, Button *, EMPTYARG )
567 {
568 	if( bSetAutoFmt )
569 		pShell->SetTableAutoFmt( *(*pTableTbl)[ nIndex ] );
570 	EndDialog( RET_OK );
571 	return sal_True;
572 }
IMPL_LINK_INLINE_END(SwAutoFormatDlg,OkHdl,Button *,EMPTYARG)573 IMPL_LINK_INLINE_END( SwAutoFormatDlg, OkHdl, Button *, EMPTYARG )
574 
575 //========================================================================
576 // AutoFmtPreview
577 
578 //------------------------------------------------------------------------
579 
580 AutoFmtPreview::AutoFmtPreview( Window* pParent, const ResId& rRes, SwWrtShell* pWrtShell ) :
581 		Window			( pParent, rRes ),
582 
583         aCurData        ( aEmptyStr ),
584         aVD             ( *this ),
585         aScriptedText   ( aVD ),
586         bFitWidth       ( sal_False ),
587         mbRTL           ( false ),
588         aPrvSize        ( GetSizePixel().Width() - 6, GetSizePixel().Height() - 30 ),
589         nLabelColWidth  ( (aPrvSize.Width() - 4) / 4 - 12 ),
590         nDataColWidth1  ( (aPrvSize.Width() - 4 - 2 * nLabelColWidth) / 3 ),
591         nDataColWidth2  ( (aPrvSize.Width() - 4 - 2 * nLabelColWidth) / 4 ),
592         nRowHeight      ( (aPrvSize.Height() - 4) / 5 ),
593         aStrJan         ( SW_RES( STR_JAN ) ),
594 		aStrFeb 		( SW_RES( STR_FEB ) ),
595 		aStrMar 		( SW_RES( STR_MAR ) ),
596 		aStrNorth		( SW_RES( STR_NORTH ) ),
597 		aStrMid 		( SW_RES( STR_MID ) ),
598 		aStrSouth		( SW_RES( STR_SOUTH ) ),
599 		aStrSum 		( SW_RES( STR_SUM ) ),
600 		m_xMSF          ( comphelper::getProcessServiceFactory() )
601 {
602     if (!pWrtShell->IsCrsrInTbl()) // We haven't created the table yet
603         mbRTL = Application::GetSettings().GetLayoutRTL();
604     else
605         mbRTL = pWrtShell->IsTableRightToLeft();
606 
607     DBG_ASSERT( m_xMSF.is(), "AutoFmtPreview: no MultiServiceFactory");
608 	if ( m_xMSF.is() )
609 	{
610         m_xBreak = uno::Reference< i18n::XBreakIterator >(
611 			m_xMSF->createInstance (
612 				rtl::OUString::createFromAscii( "com.sun.star.i18n.BreakIterator" ) ),
613             uno::UNO_QUERY);
614 	}
615 	pNumFmt	= new SvNumberFormatter( m_xMSF, LANGUAGE_SYSTEM );
616 
617 	Init();
618 }
619 
620 //------------------------------------------------------------------------
621 
~AutoFmtPreview()622 __EXPORT AutoFmtPreview::~AutoFmtPreview()
623 {
624 	delete pNumFmt;
625 }
626 
627 //------------------------------------------------------------------------
628 
lcl_SetFontProperties(Font & rFont,const SvxFontItem & rFontItem,const SvxWeightItem & rWeightItem,const SvxPostureItem & rPostureItem)629 static void lcl_SetFontProperties(
630         Font& rFont,
631         const SvxFontItem& rFontItem,
632         const SvxWeightItem& rWeightItem,
633         const SvxPostureItem& rPostureItem )
634 {
635     rFont.SetFamily     ( rFontItem.GetFamily() );
636     rFont.SetName       ( rFontItem.GetFamilyName() );
637     rFont.SetStyleName  ( rFontItem.GetStyleName() );
638     rFont.SetCharSet    ( rFontItem.GetCharSet() );
639     rFont.SetPitch      ( rFontItem.GetPitch() );
640     rFont.SetWeight     ( (FontWeight)rWeightItem.GetValue() );
641     rFont.SetItalic     ( (FontItalic)rPostureItem.GetValue() );
642 }
643 
644 #define SETONALLFONTS( MethodName, Value )                  \
645 rFont.MethodName( Value );                                  \
646 rCJKFont.MethodName( Value );                               \
647 rCTLFont.MethodName( Value );
648 
MakeFonts(sal_uInt8 nIndex,Font & rFont,Font & rCJKFont,Font & rCTLFont)649 void AutoFmtPreview::MakeFonts( sal_uInt8 nIndex, Font& rFont, Font& rCJKFont, Font& rCTLFont )
650 {
651 	const SwBoxAutoFmt& rBoxFmt = aCurData.GetBoxFmt( nIndex );
652 
653     rFont = rCJKFont = rCTLFont = GetFont();
654     Size aFontSize( rFont.GetSize().Width(), 10 );
655 
656     lcl_SetFontProperties( rFont, rBoxFmt.GetFont(), rBoxFmt.GetWeight(), rBoxFmt.GetPosture() );
657     lcl_SetFontProperties( rCJKFont, rBoxFmt.GetCJKFont(), rBoxFmt.GetCJKWeight(), rBoxFmt.GetCJKPosture() );
658     lcl_SetFontProperties( rCTLFont, rBoxFmt.GetCTLFont(), rBoxFmt.GetCTLWeight(), rBoxFmt.GetCTLPosture() );
659 
660     SETONALLFONTS( SetUnderline,    (FontUnderline)rBoxFmt.GetUnderline().GetValue() );
661     SETONALLFONTS( SetOverline,     (FontUnderline)rBoxFmt.GetOverline().GetValue() );
662     SETONALLFONTS( SetStrikeout,    (FontStrikeout)rBoxFmt.GetCrossedOut().GetValue() );
663     SETONALLFONTS( SetOutline,      rBoxFmt.GetContour().GetValue() );
664     SETONALLFONTS( SetShadow,       rBoxFmt.GetShadowed().GetValue() );
665     SETONALLFONTS( SetColor,        rBoxFmt.GetColor().GetValue() );
666     SETONALLFONTS( SetSize,         aFontSize );
667     SETONALLFONTS( SetTransparent,  sal_True );
668 }
669 
670 //------------------------------------------------------------------------
671 
GetFormatIndex(size_t nCol,size_t nRow) const672 sal_uInt8 AutoFmtPreview::GetFormatIndex( size_t nCol, size_t nRow ) const
673 {
674     static const sal_uInt8 pnFmtMap[] =
675     {
676         0,  1,  2,  1,  3,
677         4,  5,  6,  5,  7,
678         8,  9,  10, 9,  11,
679         4,  5,  6,  5,  7,
680         12, 13, 14, 13, 15
681     };
682     return pnFmtMap[ maArray.GetCellIndex( nCol, nRow, mbRTL ) ];
683 }
684 
GetBoxItem(size_t nCol,size_t nRow) const685 const SvxBoxItem& AutoFmtPreview::GetBoxItem( size_t nCol, size_t nRow ) const
686 {
687     return aCurData.GetBoxFmt( GetFormatIndex( nCol, nRow ) ).GetBox();
688 }
689 
690 //------------------------------------------------------------------------
691 
DrawString(size_t nCol,size_t nRow)692 void AutoFmtPreview::DrawString( size_t nCol, size_t nRow )
693 {
694 	//------------------------
695 	// Ausgabe des Zelltextes:
696 	//------------------------
697 	sal_uLong   nNum;
698 	double  nVal;
699 	String cellString;
700     sal_uInt8    nIndex = static_cast< sal_uInt8 >( maArray.GetCellIndex( nCol, nRow, mbRTL ) );
701 
702 	switch( nIndex )
703 	{
704 		case  1: cellString = aStrJan;			break;
705 		case  2: cellString = aStrFeb;			break;
706 		case  3: cellString = aStrMar;			break;
707 		case  5: cellString = aStrNorth;		break;
708 		case 10: cellString = aStrMid;			break;
709 		case 15: cellString = aStrSouth;		break;
710 		case  4:
711 		case 20: cellString = aStrSum;			break;
712 
713 		case  6:
714 		case  8:
715 		case 16:
716 		case 18:    nVal = nIndex;
717 					nNum = 5;
718 					goto MAKENUMSTR;
719 		case 17:
720 		case  7: 	nVal = nIndex;
721 					nNum = 6;
722 					goto MAKENUMSTR;
723 		case 11:
724 		case 12:
725 		case 13: 	nVal = nIndex;
726 					nNum = 12 == nIndex ? 10 : 9;
727 					goto MAKENUMSTR;
728 
729 		case  9:	nVal = 21; nNum = 7; 	goto MAKENUMSTR;
730 		case 14:	nVal = 36; nNum = 11; 	goto MAKENUMSTR;
731 		case 19:	nVal = 51; nNum = 7;	goto MAKENUMSTR;
732 		case 21:	nVal = 33; nNum = 13;	goto MAKENUMSTR;
733 		case 22:	nVal = 36; nNum = 14;	goto MAKENUMSTR;
734 		case 23:	nVal = 39; nNum = 13;	goto MAKENUMSTR;
735 		case 24:	nVal = 108; nNum = 15;	goto MAKENUMSTR;
736 MAKENUMSTR:
737 			if( aCurData.IsValueFormat() )
738 			{
739 				String sFmt; LanguageType eLng, eSys;
740 				aCurData.GetBoxFmt( (sal_uInt8)nNum ).GetValueFormat( sFmt, eLng, eSys );
741 
742                 short nType;
743                 sal_Bool bNew;
744                 xub_StrLen nCheckPos;
745                 sal_uInt32 nKey = pNumFmt->GetIndexPuttingAndConverting( sFmt, eLng,
746                         eSys, nType, bNew, nCheckPos);
747 				Color* pDummy;
748 				pNumFmt->GetOutputString( nVal, nKey, cellString, &pDummy );
749 			}
750 			else
751                 cellString = String::CreateFromInt32((sal_Int32)nVal);
752 			break;
753 
754 	}
755 
756 	if( cellString.Len() )
757 	{
758 		Size				aStrSize;
759         sal_uInt8                nFmtIndex       = GetFormatIndex( nCol, nRow );
760         Rectangle           cellRect        = maArray.GetCellRect( nCol, nRow );
761         Point               aPos            = cellRect.TopLeft();
762 		sal_uInt16				nRightX 		= 0;
763 //			  sal_Bool				  bJustify		  = aCurData.IsJustify();
764 //			  ScHorJustifyAttr	  aHorJustifyItem;
765 //			CellHorJustify	  eJustification;
766 
767 		Size theMaxStrSize( cellRect.GetWidth() - FRAME_OFFSET,
768 							cellRect.GetHeight() - FRAME_OFFSET );
769 		if( aCurData.IsFont() )
770 		{
771             Font aFont, aCJKFont, aCTLFont;
772             MakeFonts( nFmtIndex, aFont, aCJKFont, aCTLFont );
773             aScriptedText.SetFonts( &aFont, &aCJKFont, &aCTLFont );
774 		}
775         else
776             aScriptedText.SetDefaultFont();
777 
778 		aScriptedText.SetText( cellString, m_xBreak );
779         aStrSize = aScriptedText.GetTextSize();
780 
781 		if( aCurData.IsFont() &&
782 			theMaxStrSize.Height() < aStrSize.Height() )
783 		{
784 				// wenn der String in diesem Font nicht
785 				// in die Zelle passt, wird wieder der
786 				// Standard-Font genommen:
787                 aScriptedText.SetDefaultFont();
788                 aStrSize = aScriptedText.GetTextSize();
789 		}
790 
791 		while( theMaxStrSize.Width() <= aStrSize.Width() &&
792 				cellString.Len() > 1 )
793 		{
794 //					if( eJustification == SVX_HOR_JUSTIFY_RIGHT )
795 //  						cellString.Erase( 0, 1 );
796 //					else
797 			cellString.Erase( cellString.Len() - 1 );
798 			aScriptedText.SetText( cellString, m_xBreak );
799             aStrSize = aScriptedText.GetTextSize();
800 		}
801 
802 		nRightX  = (sal_uInt16)(  cellRect.GetWidth()
803 								- aStrSize.Width()
804 								- FRAME_OFFSET );
805 		//-------------
806 		// Ausrichtung:
807 		//-------------
808 		/*   if ( bJustify )
809 		{
810 			aCurData.GetHorJustify( nFmtIndex, aHorJustifyItem );
811 			eJustification = (CellHorJustify)aHorJustifyItem.GetValue();
812 		}
813 		else
814 		{
815 			eJustification = SC_HOR_JUSTIFY_STANDARD;
816 		}*/
817 
818 		//-----------------------------
819 		// vertikal (immer zentrieren):
820 		//-----------------------------
821 		aPos.Y() += (nRowHeight - (sal_uInt16)aStrSize.Height()) / 2;
822 
823 		//-----------
824 		// horizontal
825 		//-----------
826 /* 		  if ( eJustification != SC_HOR_JUSTIFY_STANDARD )*/
827         if( mbRTL )
828             aPos.X() += nRightX;
829         else if (aCurData.IsJustify())
830 		{
831 			sal_uInt16 nHorPos = (sal_uInt16)
832 					((cellRect.GetWidth()-aStrSize.Width())/2);
833 			const SvxAdjustItem& rAdj = aCurData.GetBoxFmt(nFmtIndex).GetAdjust();
834 			switch ( rAdj.GetAdjust() )
835 			{
836 				case SVX_ADJUST_LEFT:
837 					aPos.X() += FRAME_OFFSET;
838 					break;
839 				case SVX_ADJUST_RIGHT:
840 					aPos.X() += nRightX;
841 					break;
842 				default:
843 					aPos.X() += nHorPos;
844 					break;
845 			}
846 		}
847 		else
848 		{
849 			//---------------------
850 			// Standardausrichtung:
851 			//---------------------
852             if ( (nCol == 0) || (nIndex == 4) )
853 			{
854 				// Text-Label links oder Summe linksbuendig
855 				aPos.X() += FRAME_OFFSET;
856 			}
857 			else
858 			{
859 					// Zahlen/Datum rechtsbuendig
860 				aPos.X() += nRightX;
861 			}
862 		}
863 
864 		//-------------------------------
865         aScriptedText.DrawText( aPos );
866 		//-------------------------------
867 	}
868 }
869 
870 #undef FRAME_OFFSET
871 
872 //------------------------------------------------------------------------
873 
DrawStrings()874 void AutoFmtPreview::DrawStrings()
875 {
876     for( size_t nRow = 0; nRow < 5; ++nRow )
877         for( size_t nCol = 0; nCol < 5; ++nCol )
878             DrawString( nCol, nRow );
879 }
880 
881 //------------------------------------------------------------------------
882 
883 
DrawBackground()884 void AutoFmtPreview::DrawBackground()
885 {
886     for( size_t nRow = 0; nRow < 5; ++nRow )
887     {
888         for( size_t nCol = 0; nCol < 5; ++nCol )
889         {
890             SvxBrushItem aBrushItem( aCurData.GetBoxFmt( GetFormatIndex( nCol, nRow ) ).GetBackground() );
891 
892             aVD.Push( PUSH_LINECOLOR | PUSH_FILLCOLOR );
893             aVD.SetLineColor();
894             aVD.SetFillColor( aBrushItem.GetColor() );
895             aVD.DrawRect( maArray.GetCellRect( nCol, nRow ) );
896             aVD.Pop();
897         }
898     }
899 }
900 
901 //------------------------------------------------------------------------
902 
903 
PaintCells()904 void AutoFmtPreview::PaintCells()
905 {
906     // 1) background
907     if ( aCurData.IsBackground() )
908         DrawBackground();
909 
910     // 2) values
911     DrawStrings();
912 
913     // 3) border
914     if ( aCurData.IsFrame() )
915         maArray.DrawArray( aVD );
916 }
917 
918 //------------------------------------------------------------------------
919 
920 
Init()921 void __EXPORT AutoFmtPreview::Init()
922 {
923     SetBorderStyle( GetBorderStyle() | WINDOW_BORDER_MONO );
924     maArray.Initialize( 5, 5 );
925     maArray.SetUseDiagDoubleClipping( false );
926 	CalcCellArray( sal_False );
927 	CalcLineMap();
928 }
929 
930 //------------------------------------------------------------------------
931 
932 
CalcCellArray(sal_Bool _bFitWidth)933 void AutoFmtPreview::CalcCellArray( sal_Bool _bFitWidth )
934 {
935     maArray.SetXOffset( 2 );
936     maArray.SetAllColWidths( _bFitWidth ? nDataColWidth2 : nDataColWidth1 );
937     maArray.SetColWidth( 0, nLabelColWidth );
938     maArray.SetColWidth( 4, nLabelColWidth );
939 
940     maArray.SetYOffset( 2 );
941     maArray.SetAllRowHeights( nRowHeight );
942 
943     aPrvSize.Width() = maArray.GetWidth() + 4;
944     aPrvSize.Height() = maArray.GetHeight() + 4;
945 }
946 
947 //------------------------------------------------------------------------
948 
lclSetStyleFromBorder(svx::frame::Style & rStyle,const SvxBorderLine * pBorder)949 inline void lclSetStyleFromBorder( svx::frame::Style& rStyle, const SvxBorderLine* pBorder )
950 {
951     rStyle.Set( pBorder, 0.05, 5 );
952 }
953 
CalcLineMap()954 void AutoFmtPreview::CalcLineMap()
955 {
956     for( size_t nRow = 0; nRow < 5; ++nRow )
957     {
958         for( size_t nCol = 0; nCol < 5; ++nCol )
959         {
960             svx::frame::Style aStyle;
961 
962             const SvxBoxItem& rItem = GetBoxItem( nCol, nRow );
963             lclSetStyleFromBorder( aStyle, rItem.GetLeft() );
964             maArray.SetCellStyleLeft( nCol, nRow, aStyle );
965             lclSetStyleFromBorder( aStyle, rItem.GetRight() );
966             maArray.SetCellStyleRight( nCol, nRow, aStyle );
967             lclSetStyleFromBorder( aStyle, rItem.GetTop() );
968             maArray.SetCellStyleTop( nCol, nRow, aStyle );
969             lclSetStyleFromBorder( aStyle, rItem.GetBottom() );
970             maArray.SetCellStyleBottom( nCol, nRow, aStyle );
971 
972 // FIXME - uncomment to draw diagonal borders
973 //            lclSetStyleFromBorder( aStyle, GetDiagItem( nCol, nRow, true ).GetLine() );
974 //            maArray.SetCellStyleTLBR( nCol, nRow, aStyle );
975 //            lclSetStyleFromBorder( aStyle, GetDiagItem( nCol, nRow, false ).GetLine() );
976 //            maArray.SetCellStyleBLTR( nCol, nRow, aStyle );
977         }
978     }
979 }
980 
981 //------------------------------------------------------------------------
982 
983 
NotifyChange(const SwTableAutoFmt & rNewData)984 void AutoFmtPreview::NotifyChange( const SwTableAutoFmt& rNewData )
985 {
986 	aCurData  = rNewData;
987 	bFitWidth = aCurData.IsJustify();//sal_True;  //???
988 	CalcCellArray( bFitWidth );
989 	CalcLineMap();
990 	DoPaint( Rectangle( Point(0,0), GetSizePixel() ) );
991 }
992 
993 //------------------------------------------------------------------------
994 
995 
DoPaint(const Rectangle &)996 void AutoFmtPreview::DoPaint( const Rectangle& /*rRect*/ )
997 {
998     sal_uInt32 nOldDrawMode = aVD.GetDrawMode();
999     if( GetSettings().GetStyleSettings().GetHighContrastMode() &&
1000             SW_MOD()->GetAccessibilityOptions().GetIsForBorders() )
1001         aVD.SetDrawMode( DRAWMODE_SETTINGSLINE | DRAWMODE_SETTINGSFILL | DRAWMODE_SETTINGSTEXT | DRAWMODE_SETTINGSGRADIENT );
1002 
1003 	Bitmap	thePreview;
1004 	Point	aCenterPos;
1005 	Size	theWndSize = GetSizePixel();
1006 	Size	thePrevSize;
1007 	Color 	oldColor;
1008 	Font	aFont;
1009 
1010 	aFont = aVD.GetFont();
1011 	aFont.SetTransparent( sal_True );
1012 
1013 	aVD.SetFont 		 ( aFont );
1014     aVD.SetLineColor     ();
1015     const Color& rWinColor = GetSettings().GetStyleSettings().GetWindowColor();
1016     aVD.SetBackground    ( Wallpaper(rWinColor) );
1017     aVD.SetFillColor     ( rWinColor );
1018 	aVD.SetOutputSizePixel	( aPrvSize );
1019 
1020 	//--------------------------------
1021 	// Zellen auf virtual Device malen
1022 	// und Ergebnis sichern
1023 	//--------------------------------
1024 	PaintCells();
1025 	thePreview = aVD.GetBitmap( Point(0,0), aPrvSize );
1026 
1027 	//--------------------------------------
1028 	// Rahmen malen und Vorschau zentrieren:
1029 	// (virtual Device fuer Fensterausgabe)
1030 	//--------------------------------------
1031 	aVD.SetOutputSizePixel( theWndSize );
1032 	oldColor = aVD.GetLineColor();
1033     aVD.SetLineColor();
1034 	aVD.DrawRect( Rectangle( Point(0,0), theWndSize ) );
1035 	SetLineColor( oldColor );
1036 	aCenterPos	= Point( (theWndSize.Width()  - aPrvSize.Width() ) / 2,
1037 						 (theWndSize.Height() - aPrvSize.Height()) / 2 );
1038 	aVD.DrawBitmap( aCenterPos, thePreview );
1039 
1040 	//----------------------------
1041 	// Ausgabe im Vorschaufenster:
1042 	//----------------------------
1043 	DrawBitmap( Point(0,0), aVD.GetBitmap( Point(0,0), theWndSize ) );
1044 
1045     aVD.SetDrawMode( nOldDrawMode );
1046 }
1047 
1048 //------------------------------------------------------------------------
1049 
Paint(const Rectangle & rRect)1050 void __EXPORT AutoFmtPreview::Paint( const Rectangle& rRect )
1051 {
1052 	DoPaint( rRect );
1053 }
1054