Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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"
39 :
40 : using namespace com::sun::star;
41 :
42 : #define FRAME_OFFSET 4
43 :
44 : class AutoFmtPreview : public Window
45 : {
46 : public:
47 : AutoFmtPreview(Window* pParent);
48 : ~AutoFmtPreview();
49 :
50 : void NotifyChange( const SwTableAutoFmt& rNewData );
51 :
52 : void DetectRTL(SwWrtShell* pWrtShell);
53 :
54 : virtual void Resize();
55 : protected:
56 : virtual void Paint( const Rectangle& rRect );
57 :
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;
78 :
79 : uno::Reference< i18n::XBreakIterator > m_xBreak;
80 :
81 : void Init ();
82 : void DoPaint ( const Rectangle& rRect );
83 : void CalcCellArray ( sal_Bool bFitWidth );
84 : void CalcLineMap ();
85 : void PaintCells ();
86 :
87 : sal_uInt8 GetFormatIndex( size_t nCol, size_t nRow ) const;
88 : const SvxBoxItem& GetBoxItem( size_t nCol, size_t nRow ) const;
89 :
90 : void DrawString( size_t nCol, size_t nRow );
91 : void DrawStrings();
92 : void DrawBackground();
93 :
94 : void MakeFonts ( sal_uInt8 nIndex, Font& rFont, Font& rCJKFont, Font& rCTLFont );
95 : String MakeNumberString( String cellString, sal_Bool bAddDec );
96 : };
97 :
98 : class SwStringInputDlg : public ModalDialog
99 : {
100 : public:
101 : SwStringInputDlg( Window* pParent,
102 : const String& rTitle,
103 : const String& rEditTitle,
104 : const String& rDefault );
105 : ~SwStringInputDlg();
106 :
107 : void GetInputString( String& rString ) const;
108 :
109 : private:
110 : Edit* m_pEdInput; // Edit obtains the focus.
111 : };
112 :
113 :
114 0 : SwStringInputDlg::SwStringInputDlg(Window* pParent, const String& rTitle,
115 : const String& rEditTitle, const String& rDefault)
116 0 : : ModalDialog(pParent, "StringInputDialog", "modules/swriter/ui/stringinput.ui")
117 : {
118 0 : get<FixedText>("name")->SetText(rEditTitle);
119 0 : get(m_pEdInput, "edit");
120 :
121 0 : SetText(rTitle);
122 0 : m_pEdInput->SetText(rDefault);
123 0 : }
124 :
125 0 : void SwStringInputDlg::GetInputString( String& rString ) const
126 : {
127 0 : rString = m_pEdInput->GetText();
128 0 : }
129 :
130 :
131 0 : SwStringInputDlg::~SwStringInputDlg()
132 : {
133 0 : }
134 :
135 : // AutoFormat-Dialogue:
136 :
137 0 : 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 0 : , bSetAutoFmt(bSetAutoFormat)
152 : {
153 0 : get(m_pLbFormat, "formatlb");
154 0 : get(m_pFormatting, "formatting");
155 0 : get(m_pBtnNumFormat, "numformatcb");
156 0 : get(m_pBtnBorder, "bordercb");
157 0 : get(m_pBtnFont, "fontcb");
158 0 : get(m_pBtnPattern, "patterncb");
159 0 : get(m_pBtnAlignment, "alignmentcb");
160 0 : get(m_pBtnOk, "ok");
161 0 : get(m_pBtnCancel, "cancel");
162 0 : get(m_pBtnAdd, "add");
163 0 : get(m_pBtnRemove, "remove");
164 0 : get(m_pBtnRename, "rename");
165 0 : get(m_pWndPreview, "preview");
166 :
167 0 : m_pWndPreview->DetectRTL(pWrtShell);
168 :
169 0 : pTableTbl = new SwTableAutoFmtTbl;
170 0 : pTableTbl->Load();
171 :
172 0 : Init(pSelFmt);
173 0 : }
174 :
175 0 : SwAutoFormatDlg::~SwAutoFormatDlg()
176 : {
177 0 : if (bCoreDataChanged)
178 0 : pTableTbl->Save();
179 0 : delete pTableTbl;
180 0 : }
181 :
182 0 : void SwAutoFormatDlg::Init( const SwTableAutoFmt* pSelFmt )
183 : {
184 0 : Link aLk( LINK( this, SwAutoFormatDlg, CheckHdl ) );
185 0 : m_pBtnBorder->SetClickHdl( aLk );
186 0 : m_pBtnFont->SetClickHdl( aLk );
187 0 : m_pBtnPattern->SetClickHdl( aLk );
188 0 : m_pBtnAlignment->SetClickHdl( aLk );
189 0 : m_pBtnNumFormat->SetClickHdl( aLk );
190 :
191 0 : m_pBtnAdd->SetClickHdl ( LINK( this, SwAutoFormatDlg, AddHdl ) );
192 0 : m_pBtnRemove->SetClickHdl ( LINK( this, SwAutoFormatDlg, RemoveHdl ) );
193 0 : m_pBtnRename->SetClickHdl ( LINK( this, SwAutoFormatDlg, RenameHdl ) );
194 0 : m_pBtnOk->SetClickHdl ( LINK( this, SwAutoFormatDlg, OkHdl ) );
195 0 : m_pLbFormat->SetSelectHdl( LINK( this, SwAutoFormatDlg, SelFmtHdl ) );
196 :
197 0 : m_pBtnAdd->Enable( bSetAutoFmt );
198 :
199 0 : nIndex = 0;
200 0 : if( !bSetAutoFmt )
201 : {
202 : // Then the list to be expanded by the entry "- none -".
203 0 : m_pLbFormat->InsertEntry( ViewShell::GetShellRes()->aStrNone );
204 0 : nDfltStylePos = 1;
205 0 : nIndex = 255;
206 : }
207 :
208 0 : for (sal_uInt8 i = 0, nCount = static_cast<sal_uInt8>(pTableTbl->size());
209 : i < nCount; i++)
210 : {
211 0 : SwTableAutoFmt const& rFmt = (*pTableTbl)[ i ];
212 0 : m_pLbFormat->InsertEntry(rFmt.GetName());
213 0 : if (pSelFmt && rFmt.GetName() == pSelFmt->GetName())
214 0 : nIndex = i;
215 : }
216 :
217 0 : m_pLbFormat->SelectEntryPos( 255 != nIndex ? (nDfltStylePos + nIndex) : 0 );
218 0 : SelFmtHdl( 0 );
219 0 : }
220 :
221 0 : void SwAutoFormatDlg::UpdateChecks( const SwTableAutoFmt& rFmt, sal_Bool bEnable )
222 : {
223 0 : m_pBtnNumFormat->Enable( bEnable );
224 0 : m_pBtnNumFormat->Check( rFmt.IsValueFormat() );
225 :
226 0 : m_pBtnBorder->Enable( bEnable );
227 0 : m_pBtnBorder->Check( rFmt.IsFrame() );
228 :
229 0 : m_pBtnFont->Enable( bEnable );
230 0 : m_pBtnFont->Check( rFmt.IsFont() );
231 :
232 0 : m_pBtnPattern->Enable( bEnable );
233 0 : m_pBtnPattern->Check( rFmt.IsBackground() );
234 :
235 0 : m_pBtnAlignment->Enable( bEnable );
236 0 : m_pBtnAlignment->Check( rFmt.IsJustify() );
237 0 : }
238 :
239 0 : void SwAutoFormatDlg::FillAutoFmtOfIndex( SwTableAutoFmt*& rToFill ) const
240 : {
241 0 : if( 255 != nIndex )
242 : {
243 0 : if( rToFill )
244 0 : *rToFill = (*pTableTbl)[ nIndex ];
245 : else
246 0 : rToFill = new SwTableAutoFmt( (*pTableTbl)[ nIndex ] );
247 : }
248 : else
249 0 : delete rToFill, rToFill = 0;
250 0 : }
251 :
252 :
253 : // Handler:
254 :
255 0 : IMPL_LINK( SwAutoFormatDlg, CheckHdl, Button *, pBtn )
256 : {
257 0 : SwTableAutoFmt* pData = &(*pTableTbl)[nIndex];
258 0 : sal_Bool bCheck = ((CheckBox*)pBtn)->IsChecked(), bDataChgd = sal_True;
259 :
260 0 : if( pBtn == m_pBtnNumFormat )
261 0 : pData->SetValueFormat( bCheck );
262 0 : else if ( pBtn == m_pBtnBorder )
263 0 : pData->SetFrame( bCheck );
264 0 : else if ( pBtn == m_pBtnFont )
265 0 : pData->SetFont( bCheck );
266 0 : else if ( pBtn == m_pBtnPattern )
267 0 : pData->SetBackground( bCheck );
268 0 : else if ( pBtn == m_pBtnAlignment )
269 0 : pData->SetJustify( bCheck );
270 : else
271 0 : bDataChgd = sal_False;
272 :
273 0 : if( bDataChgd )
274 : {
275 0 : if( !bCoreDataChanged )
276 : {
277 0 : m_pBtnCancel->SetText( aStrClose );
278 0 : bCoreDataChanged = sal_True;
279 : }
280 :
281 0 : m_pWndPreview->NotifyChange( *pData );
282 : }
283 0 : return 0;
284 : }
285 :
286 0 : IMPL_LINK_NOARG(SwAutoFormatDlg, AddHdl)
287 : {
288 0 : bool bOk = false, bFmtInserted = false;
289 0 : while( !bOk )
290 : {
291 : SwStringInputDlg* pDlg = new SwStringInputDlg( this,
292 : aStrTitle,
293 : aStrLabel,
294 0 : aEmptyStr );
295 0 : if( RET_OK == pDlg->Execute() )
296 : {
297 0 : String aFormatName;
298 0 : pDlg->GetInputString( aFormatName );
299 :
300 0 : if( aFormatName.Len() > 0 )
301 : {
302 : sal_uInt16 n;
303 0 : for( n = 0; n < pTableTbl->size(); ++n )
304 0 : if( (*pTableTbl)[n].GetName() == aFormatName )
305 0 : break;
306 :
307 0 : if( n >= pTableTbl->size() )
308 : {
309 : // Format with the name does not already exist, so take up.
310 : SwTableAutoFmt* pNewData = new
311 0 : SwTableAutoFmt( aFormatName );
312 0 : pShell->GetTableAutoFmt( *pNewData );
313 :
314 : // Insert sorted!!
315 0 : for( n = 1; n < pTableTbl->size(); ++n )
316 0 : if( (*pTableTbl)[ n ].GetName() > aFormatName )
317 0 : break;
318 :
319 0 : pTableTbl->InsertAutoFmt(n, pNewData);
320 0 : m_pLbFormat->InsertEntry( aFormatName, nDfltStylePos + n );
321 0 : m_pLbFormat->SelectEntryPos( nDfltStylePos + n );
322 0 : bFmtInserted = true;
323 0 : m_pBtnAdd->Enable( sal_False );
324 0 : if ( !bCoreDataChanged )
325 : {
326 0 : m_pBtnCancel->SetText( aStrClose );
327 0 : bCoreDataChanged = sal_True;
328 : }
329 :
330 0 : SelFmtHdl( 0 );
331 0 : bOk = true;
332 : }
333 : }
334 :
335 0 : if( !bFmtInserted )
336 : {
337 : bOk = RET_CANCEL == ErrorBox( this,
338 : WinBits( WB_OK_CANCEL | WB_DEF_OK),
339 : aStrInvalidFmt
340 0 : ).Execute();
341 0 : }
342 : }
343 : else
344 0 : bOk = true;
345 0 : delete pDlg;
346 : }
347 0 : return 0;
348 : }
349 :
350 0 : IMPL_LINK_NOARG(SwAutoFormatDlg, RemoveHdl)
351 : {
352 0 : String aMessage = aStrDelMsg ;
353 0 : aMessage.AppendAscii("\n\n");
354 0 : aMessage += m_pLbFormat->GetSelectEntry() ;
355 0 : aMessage += '\n';
356 :
357 : MessBox* pBox = new MessBox( this, WinBits( WB_OK_CANCEL ),
358 0 : aStrDelTitle, aMessage);
359 :
360 0 : if ( pBox->Execute() == RET_OK )
361 : {
362 0 : m_pLbFormat->RemoveEntry( nDfltStylePos + nIndex );
363 0 : m_pLbFormat->SelectEntryPos( nDfltStylePos + nIndex-1 );
364 :
365 0 : pTableTbl->EraseAutoFmt(nIndex);
366 0 : nIndex--;
367 :
368 0 : if( !nIndex )
369 : {
370 0 : m_pBtnRemove->Enable(sal_False);
371 0 : m_pBtnRename->Enable(sal_False);
372 : }
373 :
374 0 : if( !bCoreDataChanged )
375 : {
376 0 : m_pBtnCancel->SetText( aStrClose );
377 0 : bCoreDataChanged = sal_True;
378 : }
379 : }
380 0 : delete pBox;
381 :
382 0 : SelFmtHdl( 0 );
383 :
384 0 : return 0;
385 : }
386 :
387 0 : IMPL_LINK_NOARG(SwAutoFormatDlg, RenameHdl)
388 : {
389 0 : bool bOk = false;
390 0 : while( !bOk )
391 : {
392 : SwStringInputDlg* pDlg = new SwStringInputDlg( this,
393 0 : aStrRenameTitle, m_pLbFormat->GetSelectEntry(),
394 0 : aEmptyStr );
395 0 : if( pDlg->Execute() == RET_OK )
396 : {
397 0 : bool bFmtRenamed = false;
398 0 : String aFormatName;
399 0 : pDlg->GetInputString( aFormatName );
400 :
401 0 : if ( aFormatName.Len() > 0 )
402 : {
403 : sal_uInt16 n;
404 0 : for( n = 0; n < pTableTbl->size(); ++n )
405 0 : if ((*pTableTbl)[n].GetName() == aFormatName)
406 0 : break;
407 :
408 0 : if( n >= pTableTbl->size() )
409 : {
410 : // no format with this name exists, so rename it
411 0 : m_pLbFormat->RemoveEntry( nDfltStylePos + nIndex );
412 0 : SwTableAutoFmt* p = &(*pTableTbl)[ nIndex ];
413 :
414 0 : p->SetName( aFormatName );
415 :
416 : // keep all arrays sorted!
417 0 : for( n = 1; n < pTableTbl->size(); ++n )
418 0 : if ((n != nIndex) &&
419 0 : ((*pTableTbl)[n].GetName() > aFormatName))
420 : {
421 0 : break;
422 : }
423 :
424 0 : pTableTbl->MoveAutoFmt(n, nIndex);
425 0 : m_pLbFormat->InsertEntry( aFormatName, nDfltStylePos + n );
426 0 : m_pLbFormat->SelectEntryPos( nDfltStylePos + n );
427 :
428 0 : if ( !bCoreDataChanged )
429 : {
430 0 : m_pBtnCancel->SetText( aStrClose );
431 0 : bCoreDataChanged = sal_True;
432 : }
433 :
434 0 : SelFmtHdl( 0 );
435 0 : bOk = true;
436 0 : bFmtRenamed = true;
437 : }
438 : }
439 :
440 0 : if( !bFmtRenamed )
441 : {
442 : bOk = RET_CANCEL == ErrorBox( this,
443 : WinBits( WB_OK_CANCEL | WB_DEF_OK),
444 : aStrInvalidFmt
445 0 : ).Execute();
446 0 : }
447 : }
448 : else
449 0 : bOk = true;
450 0 : delete pDlg;
451 : }
452 0 : return 0;
453 : }
454 :
455 0 : IMPL_LINK_NOARG(SwAutoFormatDlg, SelFmtHdl)
456 : {
457 0 : sal_Bool bBtnEnable = sal_False;
458 0 : sal_uInt8 nSelPos = (sal_uInt8) m_pLbFormat->GetSelectEntryPos(), nOldIdx = nIndex;
459 0 : if( nSelPos >= nDfltStylePos )
460 : {
461 0 : nIndex = nSelPos - nDfltStylePos;
462 0 : m_pWndPreview->NotifyChange( (*pTableTbl)[nIndex] );
463 0 : bBtnEnable = 0 != nIndex;
464 0 : UpdateChecks( (*pTableTbl)[nIndex], sal_True );
465 : }
466 : else
467 : {
468 0 : nIndex = 255;
469 :
470 0 : SwTableAutoFmt aTmp( ViewShell::GetShellRes()->aStrNone );
471 0 : aTmp.SetFont( sal_False );
472 0 : aTmp.SetJustify( sal_False );
473 0 : aTmp.SetFrame( sal_False );
474 0 : aTmp.SetBackground( sal_False );
475 0 : aTmp.SetValueFormat( sal_False );
476 0 : aTmp.SetWidthHeight( sal_False );
477 :
478 0 : if( nOldIdx != nIndex )
479 0 : m_pWndPreview->NotifyChange( aTmp );
480 0 : UpdateChecks( aTmp, sal_False );
481 : }
482 :
483 0 : m_pBtnRemove->Enable( bBtnEnable );
484 0 : m_pBtnRename->Enable( bBtnEnable );
485 :
486 0 : return 0;
487 : }
488 :
489 0 : IMPL_LINK_NOARG_INLINE_START(SwAutoFormatDlg, OkHdl)
490 : {
491 0 : if( bSetAutoFmt )
492 0 : pShell->SetTableAutoFmt( (*pTableTbl)[ nIndex ] );
493 0 : EndDialog( RET_OK );
494 0 : return sal_True;
495 : }
496 0 : IMPL_LINK_NOARG_INLINE_END(SwAutoFormatDlg, OkHdl)
497 :
498 : // AutoFmtPreview
499 :
500 0 : AutoFmtPreview::AutoFmtPreview(Window* pParent) :
501 : Window ( pParent ),
502 : aCurData ( aEmptyStr ),
503 : aVD ( *this ),
504 : aScriptedText ( aVD ),
505 : bFitWidth ( sal_False ),
506 : mbRTL ( false ),
507 : aStrJan ( SW_RES( STR_JAN ) ),
508 : aStrFeb ( SW_RES( STR_FEB ) ),
509 : aStrMar ( SW_RES( STR_MAR ) ),
510 : aStrNorth ( SW_RES( STR_NORTH ) ),
511 : aStrMid ( SW_RES( STR_MID ) ),
512 : aStrSouth ( SW_RES( STR_SOUTH ) ),
513 0 : aStrSum ( SW_RES( STR_SUM ) )
514 : {
515 0 : uno::Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
516 0 : m_xBreak = i18n::BreakIterator::create(xContext);
517 0 : pNumFmt = new SvNumberFormatter( xContext, LANGUAGE_SYSTEM );
518 :
519 0 : Init();
520 0 : }
521 :
522 0 : extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeAutoFmtPreview(Window *pParent, VclBuilder::stringmap &)
523 : {
524 0 : return new AutoFmtPreview(pParent);
525 : }
526 :
527 0 : void AutoFmtPreview::Resize()
528 : {
529 0 : aPrvSize = Size(GetSizePixel().Width() - 6, GetSizePixel().Height() - 30);
530 0 : nLabelColWidth = (aPrvSize.Width() - 4) / 4 - 12;
531 0 : nDataColWidth1 = (aPrvSize.Width() - 4 - 2 * nLabelColWidth) / 3;
532 0 : nDataColWidth2 = (aPrvSize.Width() - 4 - 2 * nLabelColWidth) / 4;
533 0 : nRowHeight = (aPrvSize.Height() - 4) / 5;
534 0 : NotifyChange(aCurData);
535 0 : }
536 :
537 0 : void AutoFmtPreview::DetectRTL(SwWrtShell* pWrtShell)
538 : {
539 0 : if (!pWrtShell->IsCrsrInTbl()) // We haven't created the table yet
540 0 : mbRTL = Application::GetSettings().GetLayoutRTL();
541 : else
542 0 : mbRTL = pWrtShell->IsTableRightToLeft();
543 0 : }
544 :
545 0 : AutoFmtPreview::~AutoFmtPreview()
546 : {
547 0 : delete pNumFmt;
548 0 : }
549 :
550 0 : static void lcl_SetFontProperties(
551 : Font& rFont,
552 : const SvxFontItem& rFontItem,
553 : const SvxWeightItem& rWeightItem,
554 : const SvxPostureItem& rPostureItem )
555 : {
556 0 : rFont.SetFamily ( rFontItem.GetFamily() );
557 0 : rFont.SetName ( rFontItem.GetFamilyName() );
558 0 : rFont.SetStyleName ( rFontItem.GetStyleName() );
559 0 : rFont.SetCharSet ( rFontItem.GetCharSet() );
560 0 : rFont.SetPitch ( rFontItem.GetPitch() );
561 0 : rFont.SetWeight ( (FontWeight)rWeightItem.GetValue() );
562 0 : rFont.SetItalic ( (FontItalic)rPostureItem.GetValue() );
563 0 : }
564 :
565 : #define SETONALLFONTS( MethodName, Value ) \
566 : rFont.MethodName( Value ); \
567 : rCJKFont.MethodName( Value ); \
568 : rCTLFont.MethodName( Value );
569 :
570 0 : void AutoFmtPreview::MakeFonts( sal_uInt8 nIndex, Font& rFont, Font& rCJKFont, Font& rCTLFont )
571 : {
572 0 : const SwBoxAutoFmt& rBoxFmt = aCurData.GetBoxFmt( nIndex );
573 :
574 0 : rFont = rCJKFont = rCTLFont = GetFont();
575 0 : Size aFontSize( rFont.GetSize().Width(), 10 );
576 :
577 0 : lcl_SetFontProperties( rFont, rBoxFmt.GetFont(), rBoxFmt.GetWeight(), rBoxFmt.GetPosture() );
578 0 : lcl_SetFontProperties( rCJKFont, rBoxFmt.GetCJKFont(), rBoxFmt.GetCJKWeight(), rBoxFmt.GetCJKPosture() );
579 0 : lcl_SetFontProperties( rCTLFont, rBoxFmt.GetCTLFont(), rBoxFmt.GetCTLWeight(), rBoxFmt.GetCTLPosture() );
580 :
581 0 : SETONALLFONTS( SetUnderline, (FontUnderline)rBoxFmt.GetUnderline().GetValue() );
582 0 : SETONALLFONTS( SetOverline, (FontUnderline)rBoxFmt.GetOverline().GetValue() );
583 0 : SETONALLFONTS( SetStrikeout, (FontStrikeout)rBoxFmt.GetCrossedOut().GetValue() );
584 0 : SETONALLFONTS( SetOutline, rBoxFmt.GetContour().GetValue() );
585 0 : SETONALLFONTS( SetShadow, rBoxFmt.GetShadowed().GetValue() );
586 0 : SETONALLFONTS( SetColor, rBoxFmt.GetColor().GetValue() );
587 0 : SETONALLFONTS( SetSize, aFontSize );
588 0 : SETONALLFONTS( SetTransparent, sal_True );
589 0 : }
590 :
591 0 : sal_uInt8 AutoFmtPreview::GetFormatIndex( size_t nCol, size_t nRow ) const
592 : {
593 : static const sal_uInt8 pnFmtMap[] =
594 : {
595 : 0, 1, 2, 1, 3,
596 : 4, 5, 6, 5, 7,
597 : 8, 9, 10, 9, 11,
598 : 4, 5, 6, 5, 7,
599 : 12, 13, 14, 13, 15
600 : };
601 0 : return pnFmtMap[ maArray.GetCellIndex( nCol, nRow, mbRTL ) ];
602 : }
603 :
604 0 : const SvxBoxItem& AutoFmtPreview::GetBoxItem( size_t nCol, size_t nRow ) const
605 : {
606 0 : return aCurData.GetBoxFmt( GetFormatIndex( nCol, nRow ) ).GetBox();
607 : }
608 :
609 0 : void AutoFmtPreview::DrawString( size_t nCol, size_t nRow )
610 : {
611 : // Output of the cell text:
612 : sal_uLong nNum;
613 : double nVal;
614 0 : OUString cellString;
615 0 : sal_uInt8 nIndex = static_cast< sal_uInt8 >( maArray.GetCellIndex( nCol, nRow, mbRTL ) );
616 :
617 0 : switch( nIndex )
618 : {
619 0 : case 1: cellString = aStrJan; break;
620 0 : case 2: cellString = aStrFeb; break;
621 0 : case 3: cellString = aStrMar; break;
622 0 : case 5: cellString = aStrNorth; break;
623 0 : case 10: cellString = aStrMid; break;
624 0 : case 15: cellString = aStrSouth; break;
625 : case 4:
626 0 : case 20: cellString = aStrSum; break;
627 :
628 : case 6:
629 : case 8:
630 : case 16:
631 0 : case 18: nVal = nIndex;
632 0 : nNum = 5;
633 0 : goto MAKENUMSTR;
634 : case 17:
635 0 : case 7: nVal = nIndex;
636 0 : nNum = 6;
637 0 : goto MAKENUMSTR;
638 : case 11:
639 : case 12:
640 0 : case 13: nVal = nIndex;
641 0 : nNum = 12 == nIndex ? 10 : 9;
642 0 : goto MAKENUMSTR;
643 :
644 0 : case 9: nVal = 21; nNum = 7; goto MAKENUMSTR;
645 0 : case 14: nVal = 36; nNum = 11; goto MAKENUMSTR;
646 0 : case 19: nVal = 51; nNum = 7; goto MAKENUMSTR;
647 0 : case 21: nVal = 33; nNum = 13; goto MAKENUMSTR;
648 0 : case 22: nVal = 36; nNum = 14; goto MAKENUMSTR;
649 0 : case 23: nVal = 39; nNum = 13; goto MAKENUMSTR;
650 0 : case 24: nVal = 108; nNum = 15; goto MAKENUMSTR;
651 : MAKENUMSTR:
652 0 : if( aCurData.IsValueFormat() )
653 : {
654 0 : OUString sFmt;
655 : LanguageType eLng, eSys;
656 0 : aCurData.GetBoxFmt( (sal_uInt8)nNum ).GetValueFormat( sFmt, eLng, eSys );
657 :
658 : short nType;
659 : bool bNew;
660 : sal_Int32 nCheckPos;
661 : sal_uInt32 nKey = pNumFmt->GetIndexPuttingAndConverting( sFmt, eLng,
662 0 : eSys, nType, bNew, nCheckPos);
663 : Color* pDummy;
664 0 : pNumFmt->GetOutputString( nVal, nKey, cellString, &pDummy );
665 : }
666 : else
667 0 : cellString = OUString::number((sal_Int32)nVal);
668 0 : break;
669 :
670 : }
671 :
672 0 : if( !cellString.isEmpty() )
673 : {
674 0 : Size aStrSize;
675 0 : sal_uInt8 nFmtIndex = GetFormatIndex( nCol, nRow );
676 0 : Rectangle cellRect = maArray.GetCellRect( nCol, nRow );
677 0 : Point aPos = cellRect.TopLeft();
678 0 : sal_uInt16 nRightX = 0;
679 :
680 0 : Size theMaxStrSize( cellRect.GetWidth() - FRAME_OFFSET,
681 0 : cellRect.GetHeight() - FRAME_OFFSET );
682 0 : if( aCurData.IsFont() )
683 : {
684 0 : Font aFont, aCJKFont, aCTLFont;
685 0 : MakeFonts( nFmtIndex, aFont, aCJKFont, aCTLFont );
686 0 : aScriptedText.SetFonts( &aFont, &aCJKFont, &aCTLFont );
687 : }
688 : else
689 0 : aScriptedText.SetDefaultFont();
690 :
691 0 : aScriptedText.SetText( cellString, m_xBreak );
692 0 : aStrSize = aScriptedText.GetTextSize();
693 :
694 0 : if( aCurData.IsFont() &&
695 0 : theMaxStrSize.Height() < aStrSize.Height() )
696 : {
697 : // If the string in this font does not
698 : // fit into the cell, the standard font
699 : // is taken again:
700 0 : aScriptedText.SetDefaultFont();
701 0 : aStrSize = aScriptedText.GetTextSize();
702 : }
703 :
704 0 : while( theMaxStrSize.Width() <= aStrSize.Width() &&
705 0 : cellString.getLength() > 1 )
706 : {
707 0 : cellString = cellString.copy(0, cellString.getLength() - 1 );
708 0 : aScriptedText.SetText( cellString, m_xBreak );
709 0 : aStrSize = aScriptedText.GetTextSize();
710 : }
711 :
712 0 : nRightX = (sal_uInt16)( cellRect.GetWidth()
713 0 : - aStrSize.Width()
714 0 : - FRAME_OFFSET );
715 :
716 : // vertical (always centering):
717 0 : aPos.Y() += (nRowHeight - (sal_uInt16)aStrSize.Height()) / 2;
718 :
719 : // horizontal
720 0 : if( mbRTL )
721 0 : aPos.X() += nRightX;
722 0 : else if (aCurData.IsJustify())
723 : {
724 : sal_uInt16 nHorPos = (sal_uInt16)
725 0 : ((cellRect.GetWidth()-aStrSize.Width())/2);
726 0 : const SvxAdjustItem& rAdj = aCurData.GetBoxFmt(nFmtIndex).GetAdjust();
727 0 : switch ( rAdj.GetAdjust() )
728 : {
729 : case SVX_ADJUST_LEFT:
730 0 : aPos.X() += FRAME_OFFSET;
731 0 : break;
732 : case SVX_ADJUST_RIGHT:
733 0 : aPos.X() += nRightX;
734 0 : break;
735 : default:
736 0 : aPos.X() += nHorPos;
737 0 : break;
738 : }
739 : }
740 : else
741 : {
742 : // Standard align:
743 0 : if ( (nCol == 0) || (nIndex == 4) )
744 : {
745 : // Text-Label left or sum left aligned
746 0 : aPos.X() += FRAME_OFFSET;
747 : }
748 : else
749 : {
750 : // numbers/dates right aligned
751 0 : aPos.X() += nRightX;
752 : }
753 : }
754 :
755 0 : aScriptedText.DrawText( aPos );
756 0 : }
757 0 : }
758 :
759 : #undef FRAME_OFFSET
760 :
761 0 : void AutoFmtPreview::DrawStrings()
762 : {
763 0 : for( size_t nRow = 0; nRow < 5; ++nRow )
764 0 : for( size_t nCol = 0; nCol < 5; ++nCol )
765 0 : DrawString( nCol, nRow );
766 0 : }
767 :
768 0 : void AutoFmtPreview::DrawBackground()
769 : {
770 0 : for( size_t nRow = 0; nRow < 5; ++nRow )
771 : {
772 0 : for( size_t nCol = 0; nCol < 5; ++nCol )
773 : {
774 0 : SvxBrushItem aBrushItem( aCurData.GetBoxFmt( GetFormatIndex( nCol, nRow ) ).GetBackground() );
775 :
776 0 : aVD.Push( PUSH_LINECOLOR | PUSH_FILLCOLOR );
777 0 : aVD.SetLineColor();
778 0 : aVD.SetFillColor( aBrushItem.GetColor() );
779 0 : aVD.DrawRect( maArray.GetCellRect( nCol, nRow ) );
780 0 : aVD.Pop();
781 0 : }
782 : }
783 0 : }
784 :
785 0 : void AutoFmtPreview::PaintCells()
786 : {
787 : // 1) background
788 0 : if ( aCurData.IsBackground() )
789 0 : DrawBackground();
790 :
791 : // 2) values
792 0 : DrawStrings();
793 :
794 : // 3) border
795 0 : if ( aCurData.IsFrame() )
796 0 : maArray.DrawArray( aVD );
797 0 : }
798 :
799 0 : void AutoFmtPreview::Init()
800 : {
801 0 : SetBorderStyle( GetBorderStyle() | WINDOW_BORDER_MONO );
802 0 : maArray.Initialize( 5, 5 );
803 0 : maArray.SetUseDiagDoubleClipping( false );
804 0 : CalcCellArray( sal_False );
805 0 : CalcLineMap();
806 0 : }
807 :
808 0 : void AutoFmtPreview::CalcCellArray( sal_Bool _bFitWidth )
809 : {
810 0 : maArray.SetXOffset( 2 );
811 0 : maArray.SetAllColWidths( _bFitWidth ? nDataColWidth2 : nDataColWidth1 );
812 0 : maArray.SetColWidth( 0, nLabelColWidth );
813 0 : maArray.SetColWidth( 4, nLabelColWidth );
814 :
815 0 : maArray.SetYOffset( 2 );
816 0 : maArray.SetAllRowHeights( nRowHeight );
817 :
818 0 : aPrvSize.Width() = maArray.GetWidth() + 4;
819 0 : aPrvSize.Height() = maArray.GetHeight() + 4;
820 0 : }
821 :
822 0 : inline void lclSetStyleFromBorder( svx::frame::Style& rStyle, const ::editeng::SvxBorderLine* pBorder )
823 : {
824 0 : rStyle.Set( pBorder, 0.05, 5 );
825 0 : }
826 :
827 0 : void AutoFmtPreview::CalcLineMap()
828 : {
829 0 : for( size_t nRow = 0; nRow < 5; ++nRow )
830 : {
831 0 : for( size_t nCol = 0; nCol < 5; ++nCol )
832 : {
833 0 : svx::frame::Style aStyle;
834 :
835 0 : const SvxBoxItem& rItem = GetBoxItem( nCol, nRow );
836 0 : lclSetStyleFromBorder( aStyle, rItem.GetLeft() );
837 0 : maArray.SetCellStyleLeft( nCol, nRow, aStyle );
838 0 : lclSetStyleFromBorder( aStyle, rItem.GetRight() );
839 0 : maArray.SetCellStyleRight( nCol, nRow, aStyle );
840 0 : lclSetStyleFromBorder( aStyle, rItem.GetTop() );
841 0 : maArray.SetCellStyleTop( nCol, nRow, aStyle );
842 0 : lclSetStyleFromBorder( aStyle, rItem.GetBottom() );
843 0 : maArray.SetCellStyleBottom( nCol, nRow, aStyle );
844 :
845 : // FIXME - uncomment to draw diagonal borders
846 : // lclSetStyleFromBorder( aStyle, GetDiagItem( nCol, nRow, true ).GetLine() );
847 : // maArray.SetCellStyleTLBR( nCol, nRow, aStyle );
848 : // lclSetStyleFromBorder( aStyle, GetDiagItem( nCol, nRow, false ).GetLine() );
849 : // maArray.SetCellStyleBLTR( nCol, nRow, aStyle );
850 : }
851 : }
852 0 : }
853 :
854 0 : void AutoFmtPreview::NotifyChange( const SwTableAutoFmt& rNewData )
855 : {
856 0 : aCurData = rNewData;
857 0 : bFitWidth = aCurData.IsJustify();//sal_True; //???
858 0 : CalcCellArray( bFitWidth );
859 0 : CalcLineMap();
860 0 : DoPaint( Rectangle( Point(0,0), GetSizePixel() ) );
861 0 : }
862 :
863 0 : void AutoFmtPreview::DoPaint( const Rectangle& /*rRect*/ )
864 : {
865 0 : sal_uInt32 nOldDrawMode = aVD.GetDrawMode();
866 0 : if( GetSettings().GetStyleSettings().GetHighContrastMode() )
867 0 : aVD.SetDrawMode( DRAWMODE_SETTINGSLINE | DRAWMODE_SETTINGSFILL | DRAWMODE_SETTINGSTEXT | DRAWMODE_SETTINGSGRADIENT );
868 :
869 0 : Bitmap thePreview;
870 0 : Point aCenterPos;
871 0 : Size theWndSize = GetSizePixel();
872 0 : Color oldColor;
873 0 : Font aFont;
874 :
875 0 : aFont = aVD.GetFont();
876 0 : aFont.SetTransparent( sal_True );
877 :
878 0 : aVD.SetFont ( aFont );
879 0 : aVD.SetLineColor ();
880 0 : const Color& rWinColor = GetSettings().GetStyleSettings().GetWindowColor();
881 0 : aVD.SetBackground ( Wallpaper(rWinColor) );
882 0 : aVD.SetFillColor ( rWinColor );
883 0 : aVD.SetOutputSizePixel ( aPrvSize );
884 :
885 : // Draw cells on virtual device
886 : // and save the result
887 0 : PaintCells();
888 0 : thePreview = aVD.GetBitmap( Point(0,0), aPrvSize );
889 :
890 : // Draw the Frame and center the preview:
891 : // (virtual Device for window output)
892 0 : aVD.SetOutputSizePixel( theWndSize );
893 0 : oldColor = aVD.GetLineColor();
894 0 : aVD.SetLineColor();
895 0 : aVD.DrawRect( Rectangle( Point(0,0), theWndSize ) );
896 0 : SetLineColor( oldColor );
897 0 : aCenterPos = Point( (theWndSize.Width() - aPrvSize.Width() ) / 2,
898 0 : (theWndSize.Height() - aPrvSize.Height()) / 2 );
899 0 : aVD.DrawBitmap( aCenterPos, thePreview );
900 :
901 : // Output in the preview window:
902 0 : DrawBitmap( Point(0,0), aVD.GetBitmap( Point(0,0), theWndSize ) );
903 :
904 0 : aVD.SetDrawMode( nOldDrawMode );
905 0 : }
906 :
907 0 : void AutoFmtPreview::Paint( const Rectangle& rRect )
908 : {
909 0 : DoPaint( rRect );
910 0 : }
911 :
912 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|