bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / table / tautofmt.cxx
blob0fcb9c143424c2e4d028b8b1558dc774947ff379
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 <vcl/edit.hxx>
21 #include <vcl/layout.hxx>
22 #include <vcl/msgbox.hxx>
23 #include <vcl/svapp.hxx>
24 #include <svl/zforlist.hxx>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/i18n/BreakIterator.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <svtools/scriptedtext.hxx>
29 #include <svtools/accessibilityoptions.hxx>
30 #include <svx/framelinkarray.hxx>
31 #include "app.hrc"
32 #include "swmodule.hxx"
33 #include "swtypes.hxx"
34 #include "view.hxx"
35 #include "wrtsh.hxx"
36 #include "tblafmt.hxx"
37 #include "tautofmt.hxx"
38 #include "shellres.hxx"
40 using namespace com::sun::star;
42 #define FRAME_OFFSET 4
44 class AutoFmtPreview : public Window
46 public:
47 AutoFmtPreview(Window* pParent);
48 ~AutoFmtPreview();
50 void NotifyChange( const SwTableAutoFmt& rNewData );
52 void DetectRTL(SwWrtShell* pWrtShell);
54 virtual void Resize();
55 protected:
56 virtual void Paint( const Rectangle& rRect );
58 private:
59 SwTableAutoFmt aCurData;
60 VirtualDevice aVD;
61 SvtScriptedTextHelper aScriptedText;
62 svx::frame::Array maArray; /// Implementation to draw the frame borders.
63 sal_Bool bFitWidth;
64 bool mbRTL;
65 Size aPrvSize;
66 long nLabelColWidth;
67 long nDataColWidth1;
68 long nDataColWidth2;
69 long nRowHeight;
70 const String aStrJan;
71 const String aStrFeb;
72 const String aStrMar;
73 const String aStrNorth;
74 const String aStrMid;
75 const String aStrSouth;
76 const String aStrSum;
77 SvNumberFormatter* pNumFmt;
79 uno::Reference< i18n::XBreakIterator > m_xBreak;
81 void Init ();
82 void DoPaint ( const Rectangle& rRect );
83 void CalcCellArray ( sal_Bool bFitWidth );
84 void CalcLineMap ();
85 void PaintCells ();
87 sal_uInt8 GetFormatIndex( size_t nCol, size_t nRow ) const;
88 const SvxBoxItem& GetBoxItem( size_t nCol, size_t nRow ) const;
90 void DrawString( size_t nCol, size_t nRow );
91 void DrawStrings();
92 void DrawBackground();
94 void MakeFonts ( sal_uInt8 nIndex, Font& rFont, Font& rCJKFont, Font& rCTLFont );
95 String MakeNumberString( String cellString, sal_Bool bAddDec );
98 class SwStringInputDlg : public ModalDialog
100 public:
101 SwStringInputDlg( Window* pParent,
102 const String& rTitle,
103 const String& rEditTitle,
104 const String& rDefault );
105 ~SwStringInputDlg();
107 void GetInputString( String& rString ) const;
109 private:
110 Edit* m_pEdInput; // Edit obtains the focus.
114 SwStringInputDlg::SwStringInputDlg(Window* pParent, const String& rTitle,
115 const String& rEditTitle, const String& rDefault)
116 : ModalDialog(pParent, "StringInputDialog", "modules/swriter/ui/stringinput.ui")
118 get<FixedText>("name")->SetText(rEditTitle);
119 get(m_pEdInput, "edit");
121 SetText(rTitle);
122 m_pEdInput->SetText(rDefault);
125 void SwStringInputDlg::GetInputString( String& rString ) const
127 rString = m_pEdInput->GetText();
131 SwStringInputDlg::~SwStringInputDlg()
135 // AutoFormat-Dialogue:
137 SwAutoFormatDlg::SwAutoFormatDlg( Window* pParent, SwWrtShell* pWrtShell,
138 sal_Bool bSetAutoFormat, const SwTableAutoFmt* pSelFmt )
139 : SfxModalDialog(pParent, "AutoFormatTableDialog", "modules/swriter/ui/autoformattable.ui")
140 , aStrTitle(SW_RES(STR_ADD_AUTOFORMAT_TITLE))
141 , aStrLabel(SW_RES(STR_ADD_AUTOFORMAT_LABEL))
142 , aStrClose(SW_RES(STR_BTN_AUTOFORMAT_CLOSE))
143 , aStrDelTitle(SW_RES(STR_DEL_AUTOFORMAT_TITLE))
144 , aStrDelMsg(SW_RES(STR_DEL_AUTOFORMAT_MSG))
145 , aStrRenameTitle(SW_RES(STR_RENAME_AUTOFORMAT_TITLE))
146 , aStrInvalidFmt(SW_RES(STR_INVALID_AUTOFORMAT_NAME))
147 , pShell(pWrtShell)
148 , nIndex(0)
149 , nDfltStylePos(0)
150 , bCoreDataChanged(sal_False)
151 , bSetAutoFmt(bSetAutoFormat)
153 get(m_pLbFormat, "formatlb");
154 get(m_pFormatting, "formatting");
155 get(m_pBtnNumFormat, "numformatcb");
156 get(m_pBtnBorder, "bordercb");
157 get(m_pBtnFont, "fontcb");
158 get(m_pBtnPattern, "patterncb");
159 get(m_pBtnAlignment, "alignmentcb");
160 get(m_pBtnOk, "ok");
161 get(m_pBtnCancel, "cancel");
162 get(m_pBtnAdd, "add");
163 get(m_pBtnRemove, "remove");
164 get(m_pBtnRename, "rename");
165 get(m_pWndPreview, "preview");
167 m_pWndPreview->DetectRTL(pWrtShell);
169 pTableTbl = new SwTableAutoFmtTbl;
170 pTableTbl->Load();
172 Init(pSelFmt);
175 SwAutoFormatDlg::~SwAutoFormatDlg()
177 if (bCoreDataChanged)
178 pTableTbl->Save();
179 delete pTableTbl;
182 void SwAutoFormatDlg::Init( const SwTableAutoFmt* pSelFmt )
184 Link aLk( LINK( this, SwAutoFormatDlg, CheckHdl ) );
185 m_pBtnBorder->SetClickHdl( aLk );
186 m_pBtnFont->SetClickHdl( aLk );
187 m_pBtnPattern->SetClickHdl( aLk );
188 m_pBtnAlignment->SetClickHdl( aLk );
189 m_pBtnNumFormat->SetClickHdl( aLk );
191 m_pBtnAdd->SetClickHdl ( LINK( this, SwAutoFormatDlg, AddHdl ) );
192 m_pBtnRemove->SetClickHdl ( LINK( this, SwAutoFormatDlg, RemoveHdl ) );
193 m_pBtnRename->SetClickHdl ( LINK( this, SwAutoFormatDlg, RenameHdl ) );
194 m_pBtnOk->SetClickHdl ( LINK( this, SwAutoFormatDlg, OkHdl ) );
195 m_pLbFormat->SetSelectHdl( LINK( this, SwAutoFormatDlg, SelFmtHdl ) );
197 m_pBtnAdd->Enable( bSetAutoFmt );
199 nIndex = 0;
200 if( !bSetAutoFmt )
202 // Then the list to be expanded by the entry "- none -".
203 m_pLbFormat->InsertEntry( ViewShell::GetShellRes()->aStrNone );
204 nDfltStylePos = 1;
205 nIndex = 255;
208 for (sal_uInt8 i = 0, nCount = static_cast<sal_uInt8>(pTableTbl->size());
209 i < nCount; i++)
211 SwTableAutoFmt const& rFmt = (*pTableTbl)[ i ];
212 m_pLbFormat->InsertEntry(rFmt.GetName());
213 if (pSelFmt && rFmt.GetName() == pSelFmt->GetName())
214 nIndex = i;
217 m_pLbFormat->SelectEntryPos( 255 != nIndex ? (nDfltStylePos + nIndex) : 0 );
218 SelFmtHdl( 0 );
221 void SwAutoFormatDlg::UpdateChecks( const SwTableAutoFmt& rFmt, sal_Bool bEnable )
223 m_pBtnNumFormat->Enable( bEnable );
224 m_pBtnNumFormat->Check( rFmt.IsValueFormat() );
226 m_pBtnBorder->Enable( bEnable );
227 m_pBtnBorder->Check( rFmt.IsFrame() );
229 m_pBtnFont->Enable( bEnable );
230 m_pBtnFont->Check( rFmt.IsFont() );
232 m_pBtnPattern->Enable( bEnable );
233 m_pBtnPattern->Check( rFmt.IsBackground() );
235 m_pBtnAlignment->Enable( bEnable );
236 m_pBtnAlignment->Check( rFmt.IsJustify() );
239 void SwAutoFormatDlg::FillAutoFmtOfIndex( SwTableAutoFmt*& rToFill ) const
241 if( 255 != nIndex )
243 if( rToFill )
244 *rToFill = (*pTableTbl)[ nIndex ];
245 else
246 rToFill = new SwTableAutoFmt( (*pTableTbl)[ nIndex ] );
248 else
249 delete rToFill, rToFill = 0;
253 // Handler:
255 IMPL_LINK( SwAutoFormatDlg, CheckHdl, Button *, pBtn )
257 SwTableAutoFmt* pData = &(*pTableTbl)[nIndex];
258 sal_Bool bCheck = ((CheckBox*)pBtn)->IsChecked(), bDataChgd = sal_True;
260 if( pBtn == m_pBtnNumFormat )
261 pData->SetValueFormat( bCheck );
262 else if ( pBtn == m_pBtnBorder )
263 pData->SetFrame( bCheck );
264 else if ( pBtn == m_pBtnFont )
265 pData->SetFont( bCheck );
266 else if ( pBtn == m_pBtnPattern )
267 pData->SetBackground( bCheck );
268 else if ( pBtn == m_pBtnAlignment )
269 pData->SetJustify( bCheck );
270 else
271 bDataChgd = sal_False;
273 if( bDataChgd )
275 if( !bCoreDataChanged )
277 m_pBtnCancel->SetText( aStrClose );
278 bCoreDataChanged = sal_True;
281 m_pWndPreview->NotifyChange( *pData );
283 return 0;
286 IMPL_LINK_NOARG(SwAutoFormatDlg, AddHdl)
288 bool bOk = false, bFmtInserted = false;
289 while( !bOk )
291 SwStringInputDlg* pDlg = new SwStringInputDlg( this,
292 aStrTitle,
293 aStrLabel,
294 aEmptyStr );
295 if( RET_OK == pDlg->Execute() )
297 String aFormatName;
298 pDlg->GetInputString( aFormatName );
300 if( aFormatName.Len() > 0 )
302 sal_uInt16 n;
303 for( n = 0; n < pTableTbl->size(); ++n )
304 if( (*pTableTbl)[n].GetName() == aFormatName )
305 break;
307 if( n >= pTableTbl->size() )
309 // Format with the name does not already exist, so take up.
310 SwTableAutoFmt* pNewData = new
311 SwTableAutoFmt( aFormatName );
312 pShell->GetTableAutoFmt( *pNewData );
314 // Insert sorted!!
315 for( n = 1; n < pTableTbl->size(); ++n )
316 if( (*pTableTbl)[ n ].GetName() > aFormatName )
317 break;
319 pTableTbl->InsertAutoFmt(n, pNewData);
320 m_pLbFormat->InsertEntry( aFormatName, nDfltStylePos + n );
321 m_pLbFormat->SelectEntryPos( nDfltStylePos + n );
322 bFmtInserted = true;
323 m_pBtnAdd->Enable( sal_False );
324 if ( !bCoreDataChanged )
326 m_pBtnCancel->SetText( aStrClose );
327 bCoreDataChanged = sal_True;
330 SelFmtHdl( 0 );
331 bOk = true;
335 if( !bFmtInserted )
337 bOk = RET_CANCEL == ErrorBox( this,
338 WinBits( WB_OK_CANCEL | WB_DEF_OK),
339 aStrInvalidFmt
340 ).Execute();
343 else
344 bOk = true;
345 delete pDlg;
347 return 0;
350 IMPL_LINK_NOARG(SwAutoFormatDlg, RemoveHdl)
352 String aMessage = aStrDelMsg ;
353 aMessage.AppendAscii("\n\n");
354 aMessage += m_pLbFormat->GetSelectEntry() ;
355 aMessage += '\n';
357 MessBox* pBox = new MessBox( this, WinBits( WB_OK_CANCEL ),
358 aStrDelTitle, aMessage);
360 if ( pBox->Execute() == RET_OK )
362 m_pLbFormat->RemoveEntry( nDfltStylePos + nIndex );
363 m_pLbFormat->SelectEntryPos( nDfltStylePos + nIndex-1 );
365 pTableTbl->EraseAutoFmt(nIndex);
366 nIndex--;
368 if( !nIndex )
370 m_pBtnRemove->Enable(sal_False);
371 m_pBtnRename->Enable(sal_False);
374 if( !bCoreDataChanged )
376 m_pBtnCancel->SetText( aStrClose );
377 bCoreDataChanged = sal_True;
380 delete pBox;
382 SelFmtHdl( 0 );
384 return 0;
387 IMPL_LINK_NOARG(SwAutoFormatDlg, RenameHdl)
389 bool bOk = false;
390 while( !bOk )
392 SwStringInputDlg* pDlg = new SwStringInputDlg( this,
393 aStrRenameTitle, m_pLbFormat->GetSelectEntry(),
394 aEmptyStr );
395 if( pDlg->Execute() == RET_OK )
397 bool bFmtRenamed = false;
398 String aFormatName;
399 pDlg->GetInputString( aFormatName );
401 if ( aFormatName.Len() > 0 )
403 sal_uInt16 n;
404 for( n = 0; n < pTableTbl->size(); ++n )
405 if ((*pTableTbl)[n].GetName() == aFormatName)
406 break;
408 if( n >= pTableTbl->size() )
410 // no format with this name exists, so rename it
411 m_pLbFormat->RemoveEntry( nDfltStylePos + nIndex );
412 SwTableAutoFmt* p = pTableTbl->ReleaseAutoFmt( nIndex );
414 p->SetName( aFormatName );
416 // keep all arrays sorted!
417 for( n = 1; n < pTableTbl->size(); ++n )
418 if ((*pTableTbl)[n].GetName() > aFormatName)
420 break;
423 pTableTbl->InsertAutoFmt( n, p );
424 m_pLbFormat->InsertEntry( aFormatName, nDfltStylePos + n );
425 m_pLbFormat->SelectEntryPos( nDfltStylePos + n );
427 if ( !bCoreDataChanged )
429 m_pBtnCancel->SetText( aStrClose );
430 bCoreDataChanged = sal_True;
433 SelFmtHdl( 0 );
434 bOk = true;
435 bFmtRenamed = true;
439 if( !bFmtRenamed )
441 bOk = RET_CANCEL == ErrorBox( this,
442 WinBits( WB_OK_CANCEL | WB_DEF_OK),
443 aStrInvalidFmt
444 ).Execute();
447 else
448 bOk = true;
449 delete pDlg;
451 return 0;
454 IMPL_LINK_NOARG(SwAutoFormatDlg, SelFmtHdl)
456 sal_Bool bBtnEnable = sal_False;
457 sal_uInt8 nSelPos = (sal_uInt8) m_pLbFormat->GetSelectEntryPos(), nOldIdx = nIndex;
458 if( nSelPos >= nDfltStylePos )
460 nIndex = nSelPos - nDfltStylePos;
461 m_pWndPreview->NotifyChange( (*pTableTbl)[nIndex] );
462 bBtnEnable = 0 != nIndex;
463 UpdateChecks( (*pTableTbl)[nIndex], sal_True );
465 else
467 nIndex = 255;
469 SwTableAutoFmt aTmp( ViewShell::GetShellRes()->aStrNone );
470 aTmp.SetFont( sal_False );
471 aTmp.SetJustify( sal_False );
472 aTmp.SetFrame( sal_False );
473 aTmp.SetBackground( sal_False );
474 aTmp.SetValueFormat( sal_False );
475 aTmp.SetWidthHeight( sal_False );
477 if( nOldIdx != nIndex )
478 m_pWndPreview->NotifyChange( aTmp );
479 UpdateChecks( aTmp, sal_False );
482 m_pBtnRemove->Enable( bBtnEnable );
483 m_pBtnRename->Enable( bBtnEnable );
485 return 0;
488 IMPL_LINK_NOARG_INLINE_START(SwAutoFormatDlg, OkHdl)
490 if( bSetAutoFmt )
491 pShell->SetTableAutoFmt( (*pTableTbl)[ nIndex ] );
492 EndDialog( RET_OK );
493 return sal_True;
495 IMPL_LINK_NOARG_INLINE_END(SwAutoFormatDlg, OkHdl)
497 // AutoFmtPreview
499 AutoFmtPreview::AutoFmtPreview(Window* pParent) :
500 Window ( pParent ),
501 aCurData ( aEmptyStr ),
502 aVD ( *this ),
503 aScriptedText ( aVD ),
504 bFitWidth ( sal_False ),
505 mbRTL ( false ),
506 aStrJan ( SW_RES( STR_JAN ) ),
507 aStrFeb ( SW_RES( STR_FEB ) ),
508 aStrMar ( SW_RES( STR_MAR ) ),
509 aStrNorth ( SW_RES( STR_NORTH ) ),
510 aStrMid ( SW_RES( STR_MID ) ),
511 aStrSouth ( SW_RES( STR_SOUTH ) ),
512 aStrSum ( SW_RES( STR_SUM ) )
514 uno::Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
515 m_xBreak = i18n::BreakIterator::create(xContext);
516 pNumFmt = new SvNumberFormatter( xContext, LANGUAGE_SYSTEM );
518 Init();
521 extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeAutoFmtPreview(Window *pParent, VclBuilder::stringmap &)
523 return new AutoFmtPreview(pParent);
526 void AutoFmtPreview::Resize()
528 aPrvSize = Size(GetSizePixel().Width() - 6, GetSizePixel().Height() - 30);
529 nLabelColWidth = (aPrvSize.Width() - 4) / 4 - 12;
530 nDataColWidth1 = (aPrvSize.Width() - 4 - 2 * nLabelColWidth) / 3;
531 nDataColWidth2 = (aPrvSize.Width() - 4 - 2 * nLabelColWidth) / 4;
532 nRowHeight = (aPrvSize.Height() - 4) / 5;
533 NotifyChange(aCurData);
536 void AutoFmtPreview::DetectRTL(SwWrtShell* pWrtShell)
538 if (!pWrtShell->IsCrsrInTbl()) // We haven't created the table yet
539 mbRTL = Application::GetSettings().GetLayoutRTL();
540 else
541 mbRTL = pWrtShell->IsTableRightToLeft();
544 AutoFmtPreview::~AutoFmtPreview()
546 delete pNumFmt;
549 static void lcl_SetFontProperties(
550 Font& rFont,
551 const SvxFontItem& rFontItem,
552 const SvxWeightItem& rWeightItem,
553 const SvxPostureItem& rPostureItem )
555 rFont.SetFamily ( rFontItem.GetFamily() );
556 rFont.SetName ( rFontItem.GetFamilyName() );
557 rFont.SetStyleName ( rFontItem.GetStyleName() );
558 rFont.SetCharSet ( rFontItem.GetCharSet() );
559 rFont.SetPitch ( rFontItem.GetPitch() );
560 rFont.SetWeight ( (FontWeight)rWeightItem.GetValue() );
561 rFont.SetItalic ( (FontItalic)rPostureItem.GetValue() );
564 #define SETONALLFONTS( MethodName, Value ) \
565 rFont.MethodName( Value ); \
566 rCJKFont.MethodName( Value ); \
567 rCTLFont.MethodName( Value );
569 void AutoFmtPreview::MakeFonts( sal_uInt8 nIndex, Font& rFont, Font& rCJKFont, Font& rCTLFont )
571 const SwBoxAutoFmt& rBoxFmt = aCurData.GetBoxFmt( nIndex );
573 rFont = rCJKFont = rCTLFont = GetFont();
574 Size aFontSize( rFont.GetSize().Width(), 10 );
576 lcl_SetFontProperties( rFont, rBoxFmt.GetFont(), rBoxFmt.GetWeight(), rBoxFmt.GetPosture() );
577 lcl_SetFontProperties( rCJKFont, rBoxFmt.GetCJKFont(), rBoxFmt.GetCJKWeight(), rBoxFmt.GetCJKPosture() );
578 lcl_SetFontProperties( rCTLFont, rBoxFmt.GetCTLFont(), rBoxFmt.GetCTLWeight(), rBoxFmt.GetCTLPosture() );
580 SETONALLFONTS( SetUnderline, (FontUnderline)rBoxFmt.GetUnderline().GetValue() );
581 SETONALLFONTS( SetOverline, (FontUnderline)rBoxFmt.GetOverline().GetValue() );
582 SETONALLFONTS( SetStrikeout, (FontStrikeout)rBoxFmt.GetCrossedOut().GetValue() );
583 SETONALLFONTS( SetOutline, rBoxFmt.GetContour().GetValue() );
584 SETONALLFONTS( SetShadow, rBoxFmt.GetShadowed().GetValue() );
585 SETONALLFONTS( SetColor, rBoxFmt.GetColor().GetValue() );
586 SETONALLFONTS( SetSize, aFontSize );
587 SETONALLFONTS( SetTransparent, sal_True );
590 sal_uInt8 AutoFmtPreview::GetFormatIndex( size_t nCol, size_t nRow ) const
592 static const sal_uInt8 pnFmtMap[] =
594 0, 1, 2, 1, 3,
595 4, 5, 6, 5, 7,
596 8, 9, 10, 9, 11,
597 4, 5, 6, 5, 7,
598 12, 13, 14, 13, 15
600 return pnFmtMap[ maArray.GetCellIndex( nCol, nRow, mbRTL ) ];
603 const SvxBoxItem& AutoFmtPreview::GetBoxItem( size_t nCol, size_t nRow ) const
605 return aCurData.GetBoxFmt( GetFormatIndex( nCol, nRow ) ).GetBox();
608 void AutoFmtPreview::DrawString( size_t nCol, size_t nRow )
610 // Output of the cell text:
611 sal_uLong nNum;
612 double nVal;
613 String cellString;
614 sal_uInt8 nIndex = static_cast< sal_uInt8 >( maArray.GetCellIndex( nCol, nRow, mbRTL ) );
616 switch( nIndex )
618 case 1: cellString = aStrJan; break;
619 case 2: cellString = aStrFeb; break;
620 case 3: cellString = aStrMar; break;
621 case 5: cellString = aStrNorth; break;
622 case 10: cellString = aStrMid; break;
623 case 15: cellString = aStrSouth; break;
624 case 4:
625 case 20: cellString = aStrSum; break;
627 case 6:
628 case 8:
629 case 16:
630 case 18: nVal = nIndex;
631 nNum = 5;
632 goto MAKENUMSTR;
633 case 17:
634 case 7: nVal = nIndex;
635 nNum = 6;
636 goto MAKENUMSTR;
637 case 11:
638 case 12:
639 case 13: nVal = nIndex;
640 nNum = 12 == nIndex ? 10 : 9;
641 goto MAKENUMSTR;
643 case 9: nVal = 21; nNum = 7; goto MAKENUMSTR;
644 case 14: nVal = 36; nNum = 11; goto MAKENUMSTR;
645 case 19: nVal = 51; nNum = 7; goto MAKENUMSTR;
646 case 21: nVal = 33; nNum = 13; goto MAKENUMSTR;
647 case 22: nVal = 36; nNum = 14; goto MAKENUMSTR;
648 case 23: nVal = 39; nNum = 13; goto MAKENUMSTR;
649 case 24: nVal = 108; nNum = 15; goto MAKENUMSTR;
650 MAKENUMSTR:
651 if( aCurData.IsValueFormat() )
653 String sFmt; LanguageType eLng, eSys;
654 aCurData.GetBoxFmt( (sal_uInt8)nNum ).GetValueFormat( sFmt, eLng, eSys );
656 short nType;
657 bool bNew;
658 xub_StrLen nCheckPos;
659 sal_uInt32 nKey = pNumFmt->GetIndexPuttingAndConverting( sFmt, eLng,
660 eSys, nType, bNew, nCheckPos);
661 Color* pDummy;
662 pNumFmt->GetOutputString( nVal, nKey, cellString, &pDummy );
664 else
665 cellString = OUString::number((sal_Int32)nVal);
666 break;
670 if( cellString.Len() )
672 Size aStrSize;
673 sal_uInt8 nFmtIndex = GetFormatIndex( nCol, nRow );
674 Rectangle cellRect = maArray.GetCellRect( nCol, nRow );
675 Point aPos = cellRect.TopLeft();
676 sal_uInt16 nRightX = 0;
678 Size theMaxStrSize( cellRect.GetWidth() - FRAME_OFFSET,
679 cellRect.GetHeight() - FRAME_OFFSET );
680 if( aCurData.IsFont() )
682 Font aFont, aCJKFont, aCTLFont;
683 MakeFonts( nFmtIndex, aFont, aCJKFont, aCTLFont );
684 aScriptedText.SetFonts( &aFont, &aCJKFont, &aCTLFont );
686 else
687 aScriptedText.SetDefaultFont();
689 aScriptedText.SetText( cellString, m_xBreak );
690 aStrSize = aScriptedText.GetTextSize();
692 if( aCurData.IsFont() &&
693 theMaxStrSize.Height() < aStrSize.Height() )
695 // If the string in this font does not
696 // fit into the cell, the standard font
697 // is taken again:
698 aScriptedText.SetDefaultFont();
699 aStrSize = aScriptedText.GetTextSize();
702 while( theMaxStrSize.Width() <= aStrSize.Width() &&
703 cellString.Len() > 1 )
705 cellString.Erase( cellString.Len() - 1 );
706 aScriptedText.SetText( cellString, m_xBreak );
707 aStrSize = aScriptedText.GetTextSize();
710 nRightX = (sal_uInt16)( cellRect.GetWidth()
711 - aStrSize.Width()
712 - FRAME_OFFSET );
714 // vertical (always centering):
715 aPos.Y() += (nRowHeight - (sal_uInt16)aStrSize.Height()) / 2;
717 // horizontal
718 if( mbRTL )
719 aPos.X() += nRightX;
720 else if (aCurData.IsJustify())
722 sal_uInt16 nHorPos = (sal_uInt16)
723 ((cellRect.GetWidth()-aStrSize.Width())/2);
724 const SvxAdjustItem& rAdj = aCurData.GetBoxFmt(nFmtIndex).GetAdjust();
725 switch ( rAdj.GetAdjust() )
727 case SVX_ADJUST_LEFT:
728 aPos.X() += FRAME_OFFSET;
729 break;
730 case SVX_ADJUST_RIGHT:
731 aPos.X() += nRightX;
732 break;
733 default:
734 aPos.X() += nHorPos;
735 break;
738 else
740 // Standard align:
741 if ( (nCol == 0) || (nIndex == 4) )
743 // Text-Label left or sum left aligned
744 aPos.X() += FRAME_OFFSET;
746 else
748 // numbers/dates right aligned
749 aPos.X() += nRightX;
753 aScriptedText.DrawText( aPos );
757 #undef FRAME_OFFSET
759 void AutoFmtPreview::DrawStrings()
761 for( size_t nRow = 0; nRow < 5; ++nRow )
762 for( size_t nCol = 0; nCol < 5; ++nCol )
763 DrawString( nCol, nRow );
766 void AutoFmtPreview::DrawBackground()
768 for( size_t nRow = 0; nRow < 5; ++nRow )
770 for( size_t nCol = 0; nCol < 5; ++nCol )
772 SvxBrushItem aBrushItem( aCurData.GetBoxFmt( GetFormatIndex( nCol, nRow ) ).GetBackground() );
774 aVD.Push( PUSH_LINECOLOR | PUSH_FILLCOLOR );
775 aVD.SetLineColor();
776 aVD.SetFillColor( aBrushItem.GetColor() );
777 aVD.DrawRect( maArray.GetCellRect( nCol, nRow ) );
778 aVD.Pop();
783 void AutoFmtPreview::PaintCells()
785 // 1) background
786 if ( aCurData.IsBackground() )
787 DrawBackground();
789 // 2) values
790 DrawStrings();
792 // 3) border
793 if ( aCurData.IsFrame() )
794 maArray.DrawArray( aVD );
797 void AutoFmtPreview::Init()
799 SetBorderStyle( GetBorderStyle() | WINDOW_BORDER_MONO );
800 maArray.Initialize( 5, 5 );
801 maArray.SetUseDiagDoubleClipping( false );
802 CalcCellArray( sal_False );
803 CalcLineMap();
806 void AutoFmtPreview::CalcCellArray( sal_Bool _bFitWidth )
808 maArray.SetXOffset( 2 );
809 maArray.SetAllColWidths( _bFitWidth ? nDataColWidth2 : nDataColWidth1 );
810 maArray.SetColWidth( 0, nLabelColWidth );
811 maArray.SetColWidth( 4, nLabelColWidth );
813 maArray.SetYOffset( 2 );
814 maArray.SetAllRowHeights( nRowHeight );
816 aPrvSize.Width() = maArray.GetWidth() + 4;
817 aPrvSize.Height() = maArray.GetHeight() + 4;
820 inline void lclSetStyleFromBorder( svx::frame::Style& rStyle, const ::editeng::SvxBorderLine* pBorder )
822 rStyle.Set( pBorder, 0.05, 5 );
825 void AutoFmtPreview::CalcLineMap()
827 for( size_t nRow = 0; nRow < 5; ++nRow )
829 for( size_t nCol = 0; nCol < 5; ++nCol )
831 svx::frame::Style aStyle;
833 const SvxBoxItem& rItem = GetBoxItem( nCol, nRow );
834 lclSetStyleFromBorder( aStyle, rItem.GetLeft() );
835 maArray.SetCellStyleLeft( nCol, nRow, aStyle );
836 lclSetStyleFromBorder( aStyle, rItem.GetRight() );
837 maArray.SetCellStyleRight( nCol, nRow, aStyle );
838 lclSetStyleFromBorder( aStyle, rItem.GetTop() );
839 maArray.SetCellStyleTop( nCol, nRow, aStyle );
840 lclSetStyleFromBorder( aStyle, rItem.GetBottom() );
841 maArray.SetCellStyleBottom( nCol, nRow, aStyle );
843 // FIXME - uncomment to draw diagonal borders
844 // lclSetStyleFromBorder( aStyle, GetDiagItem( nCol, nRow, true ).GetLine() );
845 // maArray.SetCellStyleTLBR( nCol, nRow, aStyle );
846 // lclSetStyleFromBorder( aStyle, GetDiagItem( nCol, nRow, false ).GetLine() );
847 // maArray.SetCellStyleBLTR( nCol, nRow, aStyle );
852 void AutoFmtPreview::NotifyChange( const SwTableAutoFmt& rNewData )
854 aCurData = rNewData;
855 bFitWidth = aCurData.IsJustify();//sal_True; //???
856 CalcCellArray( bFitWidth );
857 CalcLineMap();
858 DoPaint( Rectangle( Point(0,0), GetSizePixel() ) );
861 void AutoFmtPreview::DoPaint( const Rectangle& /*rRect*/ )
863 sal_uInt32 nOldDrawMode = aVD.GetDrawMode();
864 if( GetSettings().GetStyleSettings().GetHighContrastMode() )
865 aVD.SetDrawMode( DRAWMODE_SETTINGSLINE | DRAWMODE_SETTINGSFILL | DRAWMODE_SETTINGSTEXT | DRAWMODE_SETTINGSGRADIENT );
867 Bitmap thePreview;
868 Point aCenterPos;
869 Size theWndSize = GetSizePixel();
870 Color oldColor;
871 Font aFont;
873 aFont = aVD.GetFont();
874 aFont.SetTransparent( sal_True );
876 aVD.SetFont ( aFont );
877 aVD.SetLineColor ();
878 const Color& rWinColor = GetSettings().GetStyleSettings().GetWindowColor();
879 aVD.SetBackground ( Wallpaper(rWinColor) );
880 aVD.SetFillColor ( rWinColor );
881 aVD.SetOutputSizePixel ( aPrvSize );
883 // Draw cells on virtual device
884 // and save the result
885 PaintCells();
886 thePreview = aVD.GetBitmap( Point(0,0), aPrvSize );
888 // Draw the Frame and center the preview:
889 // (virtual Device for window output)
890 aVD.SetOutputSizePixel( theWndSize );
891 oldColor = aVD.GetLineColor();
892 aVD.SetLineColor();
893 aVD.DrawRect( Rectangle( Point(0,0), theWndSize ) );
894 SetLineColor( oldColor );
895 aCenterPos = Point( (theWndSize.Width() - aPrvSize.Width() ) / 2,
896 (theWndSize.Height() - aPrvSize.Height()) / 2 );
897 aVD.DrawBitmap( aCenterPos, thePreview );
899 // Output in the preview window:
900 DrawBitmap( Point(0,0), aVD.GetBitmap( Point(0,0), theWndSize ) );
902 aVD.SetDrawMode( nOldDrawMode );
905 void AutoFmtPreview::Paint( const Rectangle& rRect )
907 DoPaint( rRect );
910 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */