1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <comphelper/string.hxx>
21 #include <sfx2/new.hxx>
22 #include <vcl/msgbox.hxx>
23 #include <vcl/morebtn.hxx>
24 #include <svtools/svmedit.hxx>
25 #include <svl/itemset.hxx>
26 #include <svl/eitem.hxx>
27 #include <svtools/sfxecode.hxx>
28 #include <svtools/ehdl.hxx>
29 #include <tools/urlobj.hxx>
30 #include <unotools/localfilehelper.hxx>
34 #include <sfx2/app.hxx>
35 #include <sfx2/objsh.hxx>
36 #include <sfx2/sfxresid.hxx>
37 #include <sfx2/docfile.hxx>
38 #include "preview.hxx"
39 #include <sfx2/printer.hxx>
40 #include <vcl/waitobj.hxx>
42 //========================================================================
44 #define MORE_BTN(x) pMoreBt->x
46 //========================================================================
48 void SfxPreviewBase_Impl::SetObjectShell( SfxObjectShell
* pObj
)
50 ::boost::shared_ptr
<GDIMetaFile
> pFile
= pObj
51 ? pObj
->GetPreviewMetaFile()
52 : ::boost::shared_ptr
<GDIMetaFile
>();
57 SfxPreviewBase_Impl::SfxPreviewBase_Impl(
58 Window
* pParent
, const ResId
& rResId
)
59 : Window(pParent
, rResId
), pMetaFile()
63 SfxPreviewBase_Impl::~SfxPreviewBase_Impl()
67 void SfxPreviewBase_Impl::Resize()
72 void SfxPreviewWin_Impl::ImpPaint(
73 const Rectangle
&, GDIMetaFile
* pFile
, Window
* pWindow
)
75 Size aTmpSize
= pFile
? pFile
->GetPrefSize() : Size(1,1 );
76 DBG_ASSERT( aTmpSize
.Height()*aTmpSize
.Width(),
77 "size of first page is 0, overload GetFirstPageSize or set vis-area!" );
79 long nWidth
= pWindow
->GetOutputSize().Width() - 2*FRAME
;
80 long nHeight
= pWindow
->GetOutputSize().Height() - 2*FRAME
;
81 if( nWidth
< 0 ) nWidth
= 0;
82 if( nHeight
< 0 ) nHeight
= 0;
84 double dRatio
=((double)aTmpSize
.Width())/aTmpSize
.Height();
85 double dRatioPreV
=((double) nWidth
) / nHeight
;
88 if (dRatio
>dRatioPreV
)
90 aSize
=Size(nWidth
, (sal_uInt16
)(nWidth
/dRatio
));
91 aPoint
=Point( 0, (sal_uInt16
)((nHeight
-aSize
.Height())/2));
95 aSize
=Size((sal_uInt16
)(nHeight
*dRatio
), nHeight
);
96 aPoint
=Point((sal_uInt16
)((nWidth
-aSize
.Width())/2),0);
98 Point bPoint
=Point(nWidth
,nHeight
)-aPoint
;
101 pWindow
->SetLineColor();
102 Color
aLightGrayCol( COL_LIGHTGRAY
);
103 pWindow
->SetFillColor( aLightGrayCol
);
104 pWindow
->DrawRect( Rectangle( Point( 0,0 ), pWindow
->GetOutputSize() ) );
107 Color
aBlackCol( COL_BLACK
);
108 Color
aWhiteCol( COL_WHITE
);
109 pWindow
->SetLineColor( aBlackCol
);
110 pWindow
->SetFillColor( aWhiteCol
);
111 pWindow
->DrawRect( Rectangle( aPoint
+ Point( FRAME
, FRAME
), bPoint
+ Point( FRAME
, FRAME
) ) );
113 pFile
->Play( pWindow
, aPoint
+ Point( FRAME
, FRAME
), aSize
);
117 void SfxPreviewWin_Impl::Paint( const Rectangle
& rRect
)
119 ImpPaint( rRect
, pMetaFile
.get(), this );
122 class SfxNewFileDialog_Impl
126 FixedText aTemplateFt
;
129 CheckBox aPreviewBtn
;
130 SfxPreviewWin_Impl aPreviewWin
;
136 FixedText aKeywordsFt
;
139 MultiLineEdit aDescEd
;
140 FixedLine aDocinfoGb
;
142 CheckBox aTextStyleCB
;
143 CheckBox aFrameStyleCB
;
144 CheckBox aPageStyleCB
;
145 CheckBox aNumStyleCB
;
146 CheckBox aMergeStyleCB
;
147 PushButton aLoadFilePB
;
150 CancelButton aCancelBt
;
155 OUString sLoadTemplate
;
158 SfxDocumentTemplates aTemplates
;
159 SfxObjectShellLock xDocShell
;
160 SfxNewFileDialog
* pAntiImpl
;
163 DECL_LINK( Update
, void * );
165 DECL_LINK( RegionSelect
, ListBox
* );
166 DECL_LINK(TemplateSelect
, void *);
167 DECL_LINK( DoubleClick
, ListBox
* );
168 void TogglePreview(CheckBox
*);
169 DECL_LINK( Expand
, void * );
170 DECL_LINK( PreviewClick
, CheckBox
* );
171 DECL_LINK(LoadFile
, void *);
172 sal_uInt16
GetSelectedTemplatePos() const;
176 SfxNewFileDialog_Impl( SfxNewFileDialog
* pAntiImplP
, sal_uInt16 nFlags
);
177 ~SfxNewFileDialog_Impl();
179 // Returns sal_False if '- No -' is set as a template
180 // Template name can only be obtained if IsTemplate() is TRUE
182 sal_Bool
IsTemplate() const;
183 OUString
GetTemplateFileName() const;
185 sal_uInt16
GetTemplateFlags()const;
186 void SetTemplateFlags(sal_uInt16 nSet
);
190 //-------------------------------------------------------------------------
192 void SfxNewFileDialog_Impl::ClearInfo()
195 aTitleEd
.SetText(aNo
);
196 aThemaEd
.SetText(aNo
);
197 aKeywordsEd
.SetText(aNo
);
198 aDescEd
.SetText(aNo
);
201 //-------------------------------------------------------------------------
203 IMPL_LINK_NOARG(SfxNewFileDialog_Impl
, Update
)
205 if ( xDocShell
.Is() )
207 if ( xDocShell
->GetProgress() )
212 const sal_uInt16 nEntry
= GetSelectedTemplatePos();
216 aPreviewWin
.Invalidate();
217 aPreviewWin
.SetObjectShell( 0);
221 if ( aPreviewBtn
.IsChecked() && (nFlags
& SFXWB_PREVIEW
) == SFXWB_PREVIEW
)
224 OUString aFileName
= aTemplates
.GetPath( aRegionLb
.GetSelectEntryPos(), nEntry
-1);
225 INetURLObject
aTestObj( aFileName
);
226 if( aTestObj
.GetProtocol() == INET_PROT_NOT_VALID
)
228 // temp. fix until Templates are managed by UCB compatible service
229 // does NOT work with locally cached components !
231 utl::LocalFileHelper::ConvertPhysicalNameToURL( aFileName
, aTemp
);
235 INetURLObject
aObj( aFileName
);
236 for ( SfxObjectShell
* pTmp
= SfxObjectShell::GetFirst();
238 pTmp
= SfxObjectShell::GetNext(*pTmp
) )
241 if ( pTmp
->GetMedium())
243 if( INetURLObject( pTmp
->GetMedium()->GetName() ) == aObj
)
250 if ( !xDocShell
.Is() )
252 Window
*pParent
= Application::GetDefDialogParent();
253 Application::SetDefDialogParent( pAntiImpl
);
254 SfxErrorContext
eEC(ERRCTX_SFX_LOADTEMPLATE
,pAntiImpl
);
255 SfxApplication
*pSfxApp
= SFX_APP();
257 SfxItemSet
* pSet
= new SfxAllItemSet( pSfxApp
->GetPool() );
258 pSet
->Put( SfxBoolItem( SID_TEMPLATE
, sal_True
) );
259 pSet
->Put( SfxBoolItem( SID_PREVIEW
, sal_True
) );
260 lErr
= pSfxApp
->LoadTemplate( xDocShell
, aFileName
, sal_True
, pSet
);
262 ErrorHandler::HandleError(lErr
);
263 Application::SetDefDialogParent( pParent
);
264 if ( !xDocShell
.Is() )
266 aPreviewWin
.SetObjectShell( 0 );
271 aPreviewWin
.SetObjectShell( xDocShell
);
276 //-------------------------------------------------------------------------
278 IMPL_LINK( SfxNewFileDialog_Impl
, RegionSelect
, ListBox
*, pBox
)
280 if ( xDocShell
.Is() && xDocShell
->GetProgress() )
283 const sal_uInt16 nRegion
= pBox
->GetSelectEntryPos();
284 const sal_uInt16 nCount
= aTemplates
.GetRegionCount()? aTemplates
.GetCount(nRegion
): 0;
285 aTemplateLb
.SetUpdateMode(sal_False
);
287 OUString aSel
= aRegionLb
.GetSelectEntry();
288 sal_Int32 nc
= aSel
.indexOf('(');
289 if (nc
!= -1 && nc
!= 0)
290 aSel
= aSel
.replaceAt(nc
-1, 1, "");
291 if ( aSel
.compareToIgnoreAsciiCase( SfxResId(STR_STANDARD
).toString() ) == 0 )
292 aTemplateLb
.InsertEntry(aNone
);
293 for (sal_uInt16 i
= 0; i
< nCount
; ++i
)
294 aTemplateLb
.InsertEntry(aTemplates
.GetName(nRegion
, i
));
295 aTemplateLb
.SelectEntryPos(0);
296 aTemplateLb
.SetUpdateMode(sal_True
);
297 aTemplateLb
.Invalidate();
298 aTemplateLb
.Update();
299 TemplateSelect(&aTemplateLb
);
303 //-------------------------------------------------------------------------
305 IMPL_LINK_NOARG_INLINE_START(SfxNewFileDialog_Impl
, Expand
)
307 TemplateSelect(&aTemplateLb
);
310 IMPL_LINK_NOARG_INLINE_END(SfxNewFileDialog_Impl
, Expand
)
312 //-------------------------------------------------------------------------
314 IMPL_LINK( SfxNewFileDialog_Impl
, PreviewClick
, CheckBox
*, pBox
)
316 if ( xDocShell
.Is() && xDocShell
->GetProgress() )
319 sal_uInt16 nEntry
= GetSelectedTemplatePos();
320 if ( nEntry
&& pBox
->IsChecked() )
323 aPreviewWin
.Invalidate();
329 aPreviewWin
.SetObjectShell( 0 );
334 //-------------------------------------------------------------------------
336 IMPL_LINK_NOARG(SfxNewFileDialog_Impl
, TemplateSelect
)
339 if ( xDocShell
&& xDocShell
->GetProgress() )
342 if ( !MORE_BTN(GetState()) )
343 // Dialog is not opened
350 //-------------------------------------------------------------------------
352 IMPL_LINK_INLINE_START( SfxNewFileDialog_Impl
, DoubleClick
, ListBox
*, pListBox
)
356 if ( !xDocShell
.Is() || !xDocShell
->GetProgress() )
357 pAntiImpl
->EndDialog(RET_OK
);
360 IMPL_LINK_INLINE_END( SfxNewFileDialog_Impl
, DoubleClick
, ListBox
*, pListBox
)
362 //-------------------------------------------------------------------------
364 IMPL_LINK_NOARG_INLINE_START(SfxNewFileDialog_Impl
, LoadFile
)
366 pAntiImpl
->EndDialog(RET_TEMPLATE_LOAD
);
369 IMPL_LINK_NOARG_INLINE_END(SfxNewFileDialog_Impl
, LoadFile
)
370 //-------------------------------------------------------------------------
372 sal_uInt16
SfxNewFileDialog_Impl::GetSelectedTemplatePos() const
374 sal_uInt16 nEntry
= aTemplateLb
.GetSelectEntryPos();
375 OUString aSel
= aRegionLb
.GetSelectEntry();
376 sal_Int32 nc
= aSel
.indexOf('(');
377 if (nc
!= -1 && nc
!= 0)
378 aSel
= aSel
.replaceAt(nc
-1, 1, "");
379 if ( aSel
.compareToIgnoreAsciiCase(SfxResId(STR_STANDARD
).toString()) != 0 )
381 if (!aTemplateLb
.GetSelectEntryCount())
386 //-------------------------------------------------------------------------
388 sal_Bool
SfxNewFileDialog_Impl::IsTemplate() const
390 return GetSelectedTemplatePos()!=0;
394 //-------------------------------------------------------------------------
396 OUString
SfxNewFileDialog_Impl::GetTemplateFileName() const
398 if(!IsTemplate() || !aTemplates
.GetRegionCount())
400 return aTemplates
.GetPath(aRegionLb
.GetSelectEntryPos(),
401 GetSelectedTemplatePos()-1);
404 //-------------------------------------------------------------------------
406 void AdjustPosSize_Impl(Window
*pWin
, short nMoveOffset
, short nSizeOffset
)
408 Point
aPos(pWin
->GetPosPixel());
409 Size
aSize(pWin
->GetSizePixel());
410 aPos
.X() -= nMoveOffset
;
411 aSize
.Width() += nSizeOffset
;
412 pWin
->SetPosSizePixel(aPos
, aSize
);
414 //-------------------------------------------------------------------------
415 sal_uInt16
SfxNewFileDialog_Impl::GetTemplateFlags()const
417 sal_uInt16 nRet
= aTextStyleCB
.IsChecked() ? SFX_LOAD_TEXT_STYLES
: 0;
418 if(aFrameStyleCB
.IsChecked())
419 nRet
|= SFX_LOAD_FRAME_STYLES
;
420 if(aPageStyleCB
.IsChecked())
421 nRet
|= SFX_LOAD_PAGE_STYLES
;
422 if(aNumStyleCB
.IsChecked())
423 nRet
|= SFX_LOAD_NUM_STYLES
;
424 if(aMergeStyleCB
.IsChecked())
425 nRet
|= SFX_MERGE_STYLES
;
428 //-------------------------------------------------------------------------
429 void SfxNewFileDialog_Impl::SetTemplateFlags(sal_uInt16 nSet
)
431 aTextStyleCB
.Check( 0 != (nSet
&SFX_LOAD_TEXT_STYLES
));
432 aFrameStyleCB
.Check( 0 != (nSet
&SFX_LOAD_FRAME_STYLES
));
433 aPageStyleCB
.Check( 0 != (nSet
&SFX_LOAD_PAGE_STYLES
));
434 aNumStyleCB
.Check( 0 != (nSet
&SFX_LOAD_NUM_STYLES
));
435 aMergeStyleCB
.Check( 0 != (nSet
&SFX_MERGE_STYLES
));
438 //-------------------------------------------------------------------------
440 SfxNewFileDialog_Impl::SfxNewFileDialog_Impl(
441 SfxNewFileDialog
* pAntiImplP
, sal_uInt16 nFl
)
442 : aRegionFt( pAntiImplP
, SfxResId( FT_REGION
) ),
443 aRegionLb( pAntiImplP
, SfxResId( LB_REGION
) ),
444 aTemplateFt( pAntiImplP
, SfxResId( FT_TEMPLATE
) ),
445 aTemplateLb( pAntiImplP
, SfxResId( LB_TEMPLATE
) ),
446 aPreviewBtn( pAntiImplP
, SfxResId( BTN_PREVIEW
) ),
447 aPreviewWin( pAntiImplP
, SfxResId( WIN_PREVIEW
) ),
448 aTitleFt( pAntiImplP
, SfxResId( FT_TITLE
) ),
449 aTitleEd( pAntiImplP
, SfxResId( ED_TITLE
) ),
450 aThemaFt( pAntiImplP
, SfxResId( FT_THEMA
) ),
451 aThemaEd( pAntiImplP
, SfxResId( ED_THEMA
) ),
452 aKeywordsFt( pAntiImplP
, SfxResId( FT_KEYWORDS
) ),
453 aKeywordsEd( pAntiImplP
, SfxResId( ED_KEYWORDS
) ),
454 aDescFt( pAntiImplP
, SfxResId( FT_DESC
) ),
455 aDescEd( pAntiImplP
, SfxResId( ED_DESC
) ),
456 aDocinfoGb( pAntiImplP
, SfxResId( GB_DOCINFO
) ),
457 aTextStyleCB( pAntiImplP
, SfxResId( CB_TEXT_STYLE
)),
458 aFrameStyleCB( pAntiImplP
, SfxResId( CB_FRAME_STYLE
)),
459 aPageStyleCB( pAntiImplP
, SfxResId( CB_PAGE_STYLE
)),
460 aNumStyleCB( pAntiImplP
, SfxResId( CB_NUM_STYLE
)),
461 aMergeStyleCB( pAntiImplP
, SfxResId( CB_MERGE_STYLE
)),
462 aLoadFilePB( pAntiImplP
, SfxResId( PB_LOAD_FILE
)),
463 aOkBt( pAntiImplP
, SfxResId( BT_OK
) ),
464 aCancelBt( pAntiImplP
, SfxResId( BT_CANCEL
) ),
465 aHelpBt( pAntiImplP
, SfxResId( BT_HELP
) ),
466 pMoreBt( new MoreButton( pAntiImplP
, SfxResId( BT_MORE
) ) ),
467 aNone(SfxResId(STR_NONE
).toString()),
468 sLoadTemplate(SfxResId(STR_LOAD_TEMPLATE
).toString()),
470 pAntiImpl( pAntiImplP
)
472 short nMoveOffset
= *(short *)pAntiImplP
->GetClassRes();
473 pAntiImplP
->IncrementRes(sizeof(short));
474 short nExpandSize
= *(short *)pAntiImplP
->GetClassRes();
475 pAntiImplP
->IncrementRes(sizeof(short));
476 pAntiImplP
->FreeResource();
480 else if(SFXWB_LOAD_TEMPLATE
== nFlags
)
482 aLoadFilePB
.SetClickHdl(LINK(this, SfxNewFileDialog_Impl
, LoadFile
));
485 aFrameStyleCB
.Show();
488 aMergeStyleCB
.Show();
489 Size
aSize(pAntiImplP
->GetOutputSizePixel());
490 Size
aTmp(pAntiImplP
->LogicToPixel(Size(16, 16), MAP_APPFONT
));
491 aSize
.Height() += aTmp
.Height();
492 pAntiImplP
->SetOutputSizePixel(aSize
);
494 aTextStyleCB
.Check();
495 pAntiImplP
->SetText(sLoadTemplate
);
499 MORE_BTN(SetClickHdl(LINK(this, SfxNewFileDialog_Impl
, Expand
)));
500 if((nFlags
& SFXWB_PREVIEW
) == SFXWB_PREVIEW
)
502 MORE_BTN(AddWindow(&aPreviewBtn
));
503 MORE_BTN(AddWindow(&aPreviewWin
));
504 aPreviewBtn
.SetClickHdl(LINK(this, SfxNewFileDialog_Impl
, PreviewClick
));
510 nMoveOffset
= (short)pAntiImplP
->LogicToPixel(
511 Size(nMoveOffset
, nMoveOffset
), MAP_APPFONT
).Width();
512 nExpandSize
= (short)pAntiImplP
->LogicToPixel(
513 Size(nExpandSize
, nExpandSize
), MAP_APPFONT
).Width();
514 AdjustPosSize_Impl(&aTitleFt
, nMoveOffset
, 0);
515 AdjustPosSize_Impl(&aTitleEd
, nMoveOffset
, nExpandSize
);
516 AdjustPosSize_Impl(&aThemaFt
, nMoveOffset
, 0);
517 AdjustPosSize_Impl(&aThemaEd
, nMoveOffset
, nExpandSize
);
518 AdjustPosSize_Impl(&aKeywordsFt
, nMoveOffset
, 0);
519 AdjustPosSize_Impl(&aKeywordsEd
, nMoveOffset
, nExpandSize
);
520 AdjustPosSize_Impl(&aDescFt
, nMoveOffset
, 0);
521 AdjustPosSize_Impl(&aDescEd
, nMoveOffset
, nExpandSize
);
522 AdjustPosSize_Impl(&aDocinfoGb
, nMoveOffset
, nExpandSize
);
526 OUString
&rExtra
= pAntiImplP
->GetExtraData();
527 sal_Int32 nTokCount
= comphelper::string::getTokenCount(rExtra
, '|');
528 if( nTokCount
> 0 && nFlags
)
529 MORE_BTN(SetState(comphelper::string::equals(rExtra
.getToken( 0, '|'), 'Y')));
530 if( nTokCount
> 1 && nFlags
)
531 aPreviewBtn
.Check(comphelper::string::equals(rExtra
.getToken( 1 ,'|'), 'Y'));
533 aTemplateLb
.SetSelectHdl(LINK(this, SfxNewFileDialog_Impl
, TemplateSelect
));
534 aTemplateLb
.SetDoubleClickHdl(LINK(this, SfxNewFileDialog_Impl
, DoubleClick
));
536 // update the template configuration if necessary
538 WaitObject
aWaitCursor( pAntiImplP
->GetParent() );
539 aTemplates
.Update( sal_True
/* be smart */ );
541 // fill the list boxes
542 const sal_uInt16 nCount
= aTemplates
.GetRegionCount();
545 for(sal_uInt16 i
= 0; i
< nCount
; ++i
)
546 aRegionLb
.InsertEntry(aTemplates
.GetFullRegionName(i
));
547 aRegionLb
.SetSelectHdl(LINK(this, SfxNewFileDialog_Impl
, RegionSelect
));
550 aPrevTimer
.SetTimeout( 500 );
551 aPrevTimer
.SetTimeoutHdl( LINK( this, SfxNewFileDialog_Impl
, Update
));
553 aRegionLb
.SelectEntryPos(0);
554 RegionSelect(&aRegionLb
);
557 //-------------------------------------------------------------------------
559 SfxNewFileDialog_Impl::~SfxNewFileDialog_Impl()
561 OUString
&rExtra
= pAntiImpl
->GetExtraData();
562 rExtra
= MORE_BTN(GetState()) ? OUString("Y") : OUString("N");
564 rExtra
+= aPreviewBtn
.IsChecked() ? OUString("Y") : OUString("N");
568 //-------------------------------------------------------------------------
569 SfxNewFileDialog::SfxNewFileDialog(Window
*pParent
, sal_uInt16 nFlags
)
570 : SfxModalDialog( pParent
, SfxResId( DLG_NEW_FILE
) )
572 pImpl
= new SfxNewFileDialog_Impl( this, nFlags
);
574 //-------------------------------------------------------------------------
575 SfxNewFileDialog::~SfxNewFileDialog()
579 //-------------------------------------------------------------------------
580 sal_Bool
SfxNewFileDialog::IsTemplate() const
582 return pImpl
->IsTemplate();
584 //-------------------------------------------------------------------------
585 OUString
SfxNewFileDialog::GetTemplateFileName() const
587 return pImpl
->GetTemplateFileName();
589 //-------------------------------------------------------------------------
590 sal_uInt16
SfxNewFileDialog::GetTemplateFlags()const
592 return pImpl
->GetTemplateFlags();
595 //-------------------------------------------------------------------------
596 void SfxNewFileDialog::SetTemplateFlags(sal_uInt16 nSet
)
598 pImpl
->SetTemplateFlags(nSet
);
601 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */