Update ooo320-m1
[ooovba.git] / sfx2 / source / view / printer.cxx
blobec782c1750ea427311f74195d5c9c5d9c16e9968
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: printer.cxx,v $
10 * $Revision: 1.19 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
33 #include <vcl/virdev.hxx>
34 #include <vcl/metric.hxx>
35 #include <vcl/msgbox.hxx>
36 #include <svtools/printdlg.hxx>
37 #include <svtools/printwarningoptions.hxx>
38 #include <svtools/printoptions.hxx>
39 #include <vector>
41 #ifndef GCC
42 #endif
44 #include <sfx2/printer.hxx>
45 #include <sfx2/printopt.hxx>
46 #include "sfxtypes.hxx"
47 #include <sfx2/prnmon.hxx>
48 #include <sfx2/viewsh.hxx>
49 #include <sfx2/tabdlg.hxx>
50 #include "sfxresid.hxx"
51 #include "view.hrc"
53 #ifdef MSC
54 // der ist buggy
55 #define NEW_OBJECTS(Class, nCount) ((Class*) new char[ sizeof(Class) * (nCount) ])
56 #else
57 #define NEW_OBJECTS(Class, nCount) (new Class[nCount])
58 #endif
61 USHORT SfxFontSizeInfo::pStaticSizes[] =
63 60,
64 80,
65 100,
66 120,
67 140,
68 180,
69 240,
70 360,
71 480,
72 600,
73 720
76 //--------------------------------------------------------------------
78 SV_DECL_PTRARR_DEL(SfxFontArr_Impl,SfxFont*,10,5)
80 // struct SfxPrinter_Impl ------------------------------------------------
82 struct SfxPrinter_Impl
84 SfxFontArr_Impl* mpFonts;
85 BOOL mbAll;
86 BOOL mbSelection;
87 BOOL mbFromTo;
88 BOOL mbRange;
90 SfxPrinter_Impl() :
91 mpFonts ( NULL ),
92 mbAll ( TRUE ),
93 mbSelection ( TRUE ),
94 mbFromTo ( TRUE ),
95 mbRange ( TRUE ) {}
96 ~SfxPrinter_Impl() { delete mpFonts; }
99 #define FONTS() pImpl->mpFonts
101 struct SfxPrintOptDlg_Impl
103 sal_Bool mbHelpDisabled;
105 SfxPrintOptDlg_Impl() :
106 mbHelpDisabled ( sal_False ) {}
109 //--------------------------------------------------------------------
111 SfxFontSizeInfo::SfxFontSizeInfo( const SfxFont &rFont,
112 const OutputDevice &rDevice ) :
114 pSizes(0),
115 nSizes(0),
116 bScalable(TRUE)
119 if ( 0 == rDevice.GetDevFontCount() )
120 bScalable = FALSE;
121 else
123 OutputDevice &rDev = (OutputDevice&) rDevice;
124 Font aFont(rFont.GetName(), Size(0,12));
125 aFont.SetFamily(rFont.GetFamily());
126 aFont.SetPitch(rFont.GetPitch());
127 aFont.SetCharSet(rFont.GetCharSet());
129 // verfuegbare Groessen in die Liste eintragen, Groesse in 10tel Punkt
130 int nSizeCount = rDev.GetDevFontSizeCount(aFont);
131 pSizes = NEW_OBJECTS(Size, nSizeCount);
132 const MapMode aOldMapMode = rDev.GetMapMode();
133 MapMode aMap(aOldMapMode);
134 aMap.SetMapUnit(MAP_POINT);
135 const Fraction aTen(1, 10);
136 aMap.SetScaleX(aTen);
137 aMap.SetScaleY(aTen);
138 rDev.SetMapMode(aMap);
140 // Es gibt Fonts mit Bitmaps und skalierbaren Groessen
141 // In diesem Fall wird der Fonts als skalierbar behandelt.
142 BOOL bFoundScalable = FALSE;
143 for ( int i = 0; i < nSizeCount; ++i )
145 const Size aSize( rDev.GetDevFontSize(aFont, i) );
146 if ( aSize.Height() != 0 )
147 pSizes[nSizes++] = aSize;
148 else
149 bFoundScalable |= TRUE;
151 if( !bFoundScalable )
152 bScalable = FALSE;
153 else
155 // statische Font-Sizes verwenden
156 delete [] pSizes;
157 nSizes = 0;
159 rDev.SetMapMode(aOldMapMode);
162 if ( 0 == nSizes )
164 nSizes = sizeof(pStaticSizes) / sizeof(USHORT);
165 pSizes = NEW_OBJECTS(Size, nSizes);
166 for ( USHORT nPos = 0; nPos <nSizes; ++nPos )
167 pSizes[nPos] = Size( 0, pStaticSizes[nPos] );
171 //--------------------------------------------------------------------
173 SfxFontSizeInfo::~SfxFontSizeInfo()
175 delete [] pSizes;
178 //--------------------------------------------------------------------
180 BOOL SfxFontSizeInfo::HasSize(const Size &rSize) const
182 if ( bScalable )
183 return TRUE;
184 for ( USHORT i = 0; i < nSizes; ++i)
185 if ( pSizes[i] == rSize )
186 return TRUE;
187 return FALSE;
190 //--------------------------------------------------------------------
192 SfxFont::SfxFont( const FontFamily eFontFamily, const String& aFontName,
193 const FontPitch eFontPitch, const CharSet eFontCharSet ):
194 aName( aFontName ),
195 eFamily( eFontFamily ),
196 ePitch( eFontPitch ),
197 eCharSet( eFontCharSet )
201 // class SfxPrinter ------------------------------------------------------
203 SfxPrinter* SfxPrinter::Create( SvStream& rStream, SfxItemSet* pOptions )
205 /* [Beschreibung]
207 Erzeugt einen <SfxPrinter> aus dem Stream. Geladen wird genaugenommen
208 nur ein JobSetup. Falls ein solcher Drucker auf dem System nicht
209 verf"augbar ist, wird das Original als Orig-JobSetup gemerkt und
210 ein "anhlicher exisitierender Drucker genommen.
212 Die 'pOptions' werden in den erzeugten SfxPrinter "ubernommen,
213 der Returnwert geh"ort dem Caller.
217 // JobSetup laden
218 JobSetup aFileJobSetup;
219 rStream >> aFileJobSetup;
221 // Drucker erzeugen
222 SfxPrinter *pPrinter = new SfxPrinter( pOptions, aFileJobSetup );
223 return pPrinter;
226 //--------------------------------------------------------------------
228 SvStream& SfxPrinter::Store( SvStream& rStream ) const
230 /* [Beschreibung]
232 Speichert das verwendete JobSetup des <SfxPrinter>s.
236 return ( rStream << GetJobSetup() );
239 //--------------------------------------------------------------------
241 SfxPrinter::SfxPrinter( SfxItemSet* pTheOptions ) :
243 /* [Beschreibung]
245 Dieser Ctor erzeugt einen Standard-Drucker.
248 pOptions( pTheOptions ),
249 bKnown(sal_True)
252 pImpl = new SfxPrinter_Impl;
255 //--------------------------------------------------------------------
257 SfxPrinter::SfxPrinter( SfxItemSet* pTheOptions,
258 const JobSetup& rTheOrigJobSetup ) :
260 Printer ( rTheOrigJobSetup.GetPrinterName() ),
261 pOptions ( pTheOptions )
264 pImpl = new SfxPrinter_Impl;
265 bKnown = GetName() == rTheOrigJobSetup.GetPrinterName();
267 if ( bKnown )
268 SetJobSetup( rTheOrigJobSetup );
271 //--------------------------------------------------------------------
273 SfxPrinter::SfxPrinter( SfxItemSet* pTheOptions,
274 const String& rPrinterName ) :
276 Printer ( rPrinterName ),
277 pOptions ( pTheOptions ),
278 bKnown ( GetName() == rPrinterName )
281 pImpl = new SfxPrinter_Impl;
284 //--------------------------------------------------------------------
286 SfxPrinter::SfxPrinter( const SfxPrinter& rPrinter ) :
288 Printer ( rPrinter.GetName() ),
289 pOptions( rPrinter.GetOptions().Clone() ),
290 bKnown ( rPrinter.IsKnown() )
292 SetJobSetup( rPrinter.GetJobSetup() );
293 SetPrinterProps( &rPrinter );
294 SetMapMode( rPrinter.GetMapMode() );
296 pImpl = new SfxPrinter_Impl;
297 pImpl->mbAll = rPrinter.pImpl->mbAll;
298 pImpl->mbSelection = rPrinter.pImpl->mbSelection;
299 pImpl->mbFromTo = rPrinter.pImpl->mbFromTo;
300 pImpl->mbRange = rPrinter.pImpl->mbRange;
303 //--------------------------------------------------------------------
305 SfxPrinter* SfxPrinter::Clone() const
307 if ( IsDefPrinter() )
309 SfxPrinter *pNewPrinter;
310 pNewPrinter = new SfxPrinter( GetOptions().Clone() );
311 pNewPrinter->SetJobSetup( GetJobSetup() );
312 pNewPrinter->SetPrinterProps( this );
313 pNewPrinter->SetMapMode( GetMapMode() );
314 pNewPrinter->pImpl->mbAll = pImpl->mbAll;
315 pNewPrinter->pImpl->mbSelection =pImpl->mbSelection;
316 pNewPrinter->pImpl->mbFromTo = pImpl->mbFromTo;
317 pNewPrinter->pImpl->mbRange =pImpl->mbRange;
318 return pNewPrinter;
320 else
321 return new SfxPrinter( *this );
324 //--------------------------------------------------------------------
326 SfxPrinter::~SfxPrinter()
328 delete pOptions;
329 delete pImpl;
332 //--------------------------------------------------------------------
334 void SfxPrinter::SetOptions( const SfxItemSet &rNewOptions )
336 pOptions->Set(rNewOptions);
339 //--------------------------------------------------------------------
341 void SfxPrinter::EnableRange( USHORT nRange )
343 PrintDialogRange eRange = (PrintDialogRange)nRange;
345 if ( eRange == PRINTDIALOG_ALL )
346 pImpl->mbAll = TRUE;
347 else if ( eRange == PRINTDIALOG_SELECTION )
348 pImpl->mbSelection = TRUE;
349 else if ( eRange == PRINTDIALOG_FROMTO )
350 pImpl->mbFromTo = TRUE;
351 else if ( eRange == PRINTDIALOG_RANGE )
352 pImpl->mbRange = TRUE;
355 //--------------------------------------------------------------------
357 void SfxPrinter::DisableRange( USHORT nRange )
359 PrintDialogRange eRange = (PrintDialogRange)nRange;
361 if ( eRange == PRINTDIALOG_ALL )
362 pImpl->mbAll = FALSE;
363 else if ( eRange == PRINTDIALOG_SELECTION )
364 pImpl->mbSelection = FALSE;
365 else if ( eRange == PRINTDIALOG_FROMTO )
366 pImpl->mbFromTo = FALSE;
367 else if ( eRange == PRINTDIALOG_RANGE )
368 pImpl->mbRange = FALSE;
371 //--------------------------------------------------------------------
373 BOOL SfxPrinter::IsRangeEnabled( USHORT nRange ) const
375 PrintDialogRange eRange = (PrintDialogRange)nRange;
376 BOOL bRet = FALSE;
378 if ( eRange == PRINTDIALOG_ALL )
379 bRet = pImpl->mbAll;
380 else if ( eRange == PRINTDIALOG_SELECTION )
381 bRet = pImpl->mbSelection;
382 else if ( eRange == PRINTDIALOG_FROMTO )
383 bRet = pImpl->mbFromTo;
384 else if ( eRange == PRINTDIALOG_RANGE )
385 bRet = pImpl->mbRange;
387 return bRet;
390 //--------------------------------------------------------------------
392 SV_IMPL_PTRARR(SfxFontArr_Impl,SfxFont*)
394 //--------------------------------------------------------------------
396 const SfxFont* SfxFindFont_Impl( const SfxFontArr_Impl& rArr,
397 const String& rName )
399 const USHORT nCount = rArr.Count();
400 for ( USHORT i = 0; i < nCount; ++i )
402 const SfxFont *pFont = rArr[i];
403 if ( pFont->GetName() == rName )
404 return pFont;
406 return NULL;
409 //--------------------------------------------------------------------
411 void SfxPrinter::UpdateFonts_Impl()
413 VirtualDevice *pVirDev = 0;
414 const OutputDevice *pOut = this;
416 // falls kein Drucker gefunden werden konnte, ein
417 // temp. Device erzeugen fuer das Erfragen der Fonts
418 if( !IsValid() )
419 pOut = pVirDev = new VirtualDevice;
421 int nCount = pOut->GetDevFontCount();
422 FONTS() = new SfxFontArr_Impl((BYTE)nCount);
424 std::vector< Font > aNonRegularFonts;
425 for(int i = 0;i < nCount;++i)
427 Font aFont(pOut->GetDevFont(i));
428 if ( (aFont.GetItalic() != ITALIC_NONE) ||
429 (aFont.GetWeight() != WEIGHT_MEDIUM) )
431 // First: Don't add non-regular fonts. The font name is not unique so we have
432 // to filter the device font list.
433 aNonRegularFonts.push_back( aFont );
435 else if ( FONTS()->Count() == 0 ||
436 (*FONTS())[FONTS()->Count()-1]->GetName() != aFont.GetName() )
438 DBG_ASSERT(0 == SfxFindFont_Impl(*FONTS(), aFont.GetName()), "Doppelte Fonts vom SV-Device!");
439 SfxFont* pTmp = new SfxFont( aFont.GetFamily(), aFont.GetName(),
440 aFont.GetPitch(), aFont.GetCharSet() );
441 FONTS()->C40_INSERT(SfxFont, pTmp, FONTS()->Count());
444 delete pVirDev;
446 // Try to add all non-regular fonts. It could be that there was no regular font
447 // with the same name added.
448 std::vector< Font >::const_iterator pIter;
449 for ( pIter = aNonRegularFonts.begin(); pIter != aNonRegularFonts.end(); pIter++ )
451 if ( SfxFindFont_Impl( *FONTS(), pIter->GetName() ) == 0 )
453 SfxFont* pTmp = new SfxFont( pIter->GetFamily(), pIter->GetName(),
454 pIter->GetPitch(), pIter->GetCharSet() );
455 FONTS()->C40_INSERT( SfxFont, pTmp, FONTS()->Count() );
460 //--------------------------------------------------------------------
462 USHORT SfxPrinter::GetFontCount()
464 if ( !FONTS() )
465 UpdateFonts_Impl();
466 return FONTS()->Count();
469 //--------------------------------------------------------------------
471 const SfxFont* SfxPrinter::GetFont( USHORT nNo ) const
473 DBG_ASSERT( FONTS(), "bitte erst GetFontCount() abfragen!" );
474 return (*FONTS())[ nNo ];
477 //--------------------------------------------------------------------
479 const SfxFont* SfxPrinter::GetFontByName( const String &rFontName )
481 if ( !FONTS() )
482 UpdateFonts_Impl();
483 return SfxFindFont_Impl(*FONTS(), rFontName);
486 //--------------------------------------------------------------------
488 BOOL SfxPrinter::InitJob( Window* pUIParent, BOOL bAskAboutTransparentObjects )
490 const SvtPrinterOptions aPrinterOpt;
491 const SvtPrintFileOptions aPrintFileOpt;
492 const SvtBasePrintOptions* pPrinterOpt = &aPrinterOpt;
493 const SvtBasePrintOptions* pPrintFileOpt = &aPrintFileOpt;
494 PrinterOptions aNewPrinterOptions;
495 BOOL bRet = TRUE;
497 ( ( IsPrintFileEnabled() && GetPrintFile().Len() ) ? pPrintFileOpt : pPrinterOpt )->GetPrinterOptions( aNewPrinterOptions );
499 if( bAskAboutTransparentObjects && !aNewPrinterOptions.IsReduceTransparency() )
501 if ( !Application::IsHeadlessModeEnabled() )
503 SvtPrintWarningOptions aWarnOpt;
505 if( aWarnOpt.IsTransparency() )
507 TransparencyPrintWarningBox aWarnBox( pUIParent );
508 const USHORT nRet = aWarnBox.Execute();
510 if( nRet == RET_CANCEL )
511 bRet = FALSE;
512 else
514 aNewPrinterOptions.SetReduceTransparency( nRet != RET_NO );
515 aWarnOpt.SetTransparency( !aWarnBox.IsNoWarningChecked() );
521 if( bRet )
522 SetPrinterOptions( aNewPrinterOptions );
524 return bRet;
527 //--------------------------------------------------------------------
529 SfxPrintOptionsDialog::SfxPrintOptionsDialog( Window *pParent,
530 SfxViewShell *pViewShell,
531 const SfxItemSet *pSet ) :
533 ModalDialog( pParent, WinBits( WB_STDMODAL | WB_3DLOOK ) ),
535 aOkBtn ( this ),
536 aCancelBtn ( this ),
537 aHelpBtn ( this ),
538 pDlgImpl ( new SfxPrintOptDlg_Impl ),
539 pViewSh ( pViewShell ),
540 pOptions ( pSet->Clone() ),
541 pPage ( NULL )
544 SetText( SfxResId( STR_PRINT_OPTIONS_TITLE ) );
546 // TabPage einh"angen
547 pPage = pViewSh->CreatePrintOptionsPage( this, *pOptions );
548 DBG_ASSERT( pPage, "CreatePrintOptions != SFX_VIEW_HAS_PRINTOPTIONS" );
549 pPage->Reset( *pOptions );
550 SetHelpId( pPage->GetHelpId() );
551 pPage->Show();
553 // Dialoggr"o\se bestimmen
554 Size a6Sz = LogicToPixel( Size( 6, 6 ), MAP_APPFONT );
555 Size aBtnSz = LogicToPixel( Size( 50, 14 ), MAP_APPFONT );
556 Size aOutSz( pPage->GetSizePixel() );
557 aOutSz.Height() += 6;
558 long nWidth = aBtnSz.Width();
559 nWidth += a6Sz.Width();
560 aOutSz.Width() += nWidth;
561 if ( aOutSz.Height() < 90 )
562 // mindestens die H"ohe der 3 Buttons
563 aOutSz.Height() = 90;
564 SetOutputSizePixel( aOutSz );
566 // set position and size of the buttons
567 Point aBtnPos( aOutSz.Width() - aBtnSz.Width() - a6Sz.Width(), a6Sz.Height() );
568 aOkBtn.SetPosSizePixel( aBtnPos, aBtnSz );
569 aBtnPos.Y() += aBtnSz.Height() + ( a6Sz.Height() / 2 );
570 aCancelBtn.SetPosSizePixel( aBtnPos, aBtnSz );
571 aBtnPos.Y() += aBtnSz.Height() + a6Sz.Height();
572 aHelpBtn.SetPosSizePixel( aBtnPos, aBtnSz );
574 aCancelBtn.Show();
575 aOkBtn.Show();
576 aHelpBtn.Show();
579 //--------------------------------------------------------------------
581 SfxPrintOptionsDialog::~SfxPrintOptionsDialog()
583 delete pDlgImpl;
584 delete pPage;
585 delete pOptions;
588 //--------------------------------------------------------------------
590 short SfxPrintOptionsDialog::Execute()
592 short nRet = ModalDialog::Execute();
593 if ( nRet == RET_OK )
594 pPage->FillItemSet( *pOptions );
595 else
596 pPage->Reset( *pOptions );
597 return nRet;
600 //--------------------------------------------------------------------
602 long SfxPrintOptionsDialog::Notify( NotifyEvent& rNEvt )
604 if ( rNEvt.GetType() == EVENT_KEYINPUT )
606 if ( rNEvt.GetKeyEvent()->GetKeyCode().GetCode() == KEY_F1 && pDlgImpl->mbHelpDisabled )
607 return 1; // help disabled -> <F1> does nothing
610 return ModalDialog::Notify( rNEvt );
613 //--------------------------------------------------------------------
615 void SfxPrintOptionsDialog::DisableHelp()
617 pDlgImpl->mbHelpDisabled = sal_True;
619 aHelpBtn.Disable();