Update ooo320-m1
[ooovba.git] / basic / source / app / printer.cxx
blob424089c27eefaae6ee62aa9875b31c2dae5170f9
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.10 $
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_basic.hxx"
34 #ifndef _PRINT_HXX //autogen
35 #include <vcl/print.hxx>
36 #endif
37 #ifndef _DIALOG_HXX //autogen
38 #include <vcl/dialog.hxx>
39 #endif
40 #ifndef _FIXED_HXX //autogen
41 #include <vcl/fixed.hxx>
42 #endif
43 #ifndef _BUTTON_HXX //autogen
44 #include <vcl/button.hxx>
45 #endif
46 #ifndef _BASIC_TTRESHLP_HXX
47 #include <basic/ttstrhlp.hxx>
48 #endif
50 #include <algorithm>
52 #include "app.hxx"
53 #include "printer.hxx"
54 #include "basic.hrc"
55 #include "resids.hrc"
56 #include "basrid.hxx"
58 class PrintingDialog : public ModelessDialog {
59 String aName;
60 FixedText aText;
61 CancelButton aCancel;
62 public:
63 PrintingDialog( Window*, BasicPrinter*, ResId&, String& );
64 void ChangeMessage( short );
67 BasicPrinter::BasicPrinter() : Printer()
69 nPage = 0; nLine = 9999;
70 SetMapMode( MapMode( MAP_POINT ) );
71 Size s( GetOutputSize() );
72 // Use 10 point font
73 Font aFont( FAMILY_MODERN, Size( 0, 10 ) );
74 aFont.SetPitch( PITCH_FIXED );
75 SetFont( aFont );
76 // Output: 6 Lines/Inch = 12 Point
77 nLines = (short) s.Height() / 12;
78 nYoff = 12;
79 SetStartPrintHdl( LINK( this, BasicPrinter, StartPrintHdl ) );
80 SetEndPrintHdl( LINK( this, BasicPrinter, EndPrintHdl ) );
81 SetPrintPageHdl( LINK( this, BasicPrinter, PrintPageHdl ) );
84 void BasicPrinter::Header()
86 if( nPage ) EndPage();
87 nPage++;
88 StartPage();
89 String aHdr;
90 String aPage( SttResId( IDS_PAGE ) );
91 aPage.Append( String::CreateFromInt32(nPage) );
92 aHdr = aFile.Copy( 0, 80 - aPage.Len() );
93 aHdr.Expand( 80 - aPage.Len(), ' ' );
94 aHdr += aPage;
95 DrawText( Point( 0, 0 ), aHdr );
96 nLine = 2;
99 void BasicPrinter::Print( const String& rFile, const String& rText, BasicFrame *pFrame )
101 nPage = 0; nLine = 9999;
102 aFile = rFile;
103 // Setup dialog
104 SttResId aResId( IDD_PRINT_DIALOG );
105 pDlg = new PrintingDialog
106 ( aBasicApp.pFrame, this, aResId, aFile );
107 // Set position of dialog
108 Size s1 = aBasicApp.pFrame->GetSizePixel();
109 Size s2 = pDlg->GetSizePixel();
110 pDlg->SetPosPixel( Point( (s1.Width() - s2.Width() ) / 2,
111 (s1.Height()- s2.Height() ) / 2 ) );
112 // Disable PRINT-Menu
113 MenuBar* pBar = pFrame->GetMenuBar();
114 Menu* pFileMenu = pBar->GetPopupMenu( RID_APPFILE );
115 pFileMenu->EnableItem( RID_FILEPRINT, FALSE );
117 pDlg->ChangeMessage( 1 );
118 pDlg->Show();
119 StartJob( rFile );
120 StartPage();
121 xub_StrLen nDone=0;
122 while( nDone < rText.Len() )
124 if( nLine >= nLines ) Header();
125 xub_StrLen nLineEnd = std::min( rText.Search( '\n', nDone ), rText.Search( '\r', nDone ) );
126 DrawText( Point( 0, nLine * nYoff ), rText, nDone, nLineEnd-nDone-1 );
127 nDone = nLineEnd;
128 if( nDone <= rText.Len() && rText.GetChar(nDone) == '\r' ) nDone++;
129 if( nDone <= rText.Len() && rText.GetChar(nDone) == '\n' ) nDone++;
130 nLine++;
131 Application::Reschedule();
133 EndPage();
134 EndJob();
135 nPage = 1;
136 while( IsPrinting() ) Application::Reschedule();
137 delete pDlg; pDlg = NULL;
138 pFileMenu->EnableItem( RID_FILEPRINT, TRUE );
141 IMPL_LINK_INLINE_START( BasicPrinter, StartPrintHdl, Printer *, pPrinter )
143 (void) pPrinter; /* avoid warning about unused parameter */
144 if( pDlg != NULL )
145 pDlg->Show();
146 return 0;
148 IMPL_LINK_INLINE_END( BasicPrinter, StartPrintHdl, Printer *, pPrinter )
150 IMPL_LINK_INLINE_START( BasicPrinter, EndPrintHdl, Printer *, pPrinter )
152 (void) pPrinter; /* avoid warning about unused parameter */
153 if( pDlg != NULL)
154 pDlg->Hide();
155 return 0;
157 IMPL_LINK_INLINE_END( BasicPrinter, EndPrintHdl, Printer *, pPrinter )
159 IMPL_LINK_INLINE_START( BasicPrinter, PrintPageHdl, Printer *, pPrinter )
161 (void) pPrinter; /* avoid warning about unused parameter */
162 if( pDlg != NULL)
163 pDlg->ChangeMessage( nPage );
164 return 0;
166 IMPL_LINK_INLINE_END( BasicPrinter, PrintPageHdl, Printer *, pPrinter )
168 IMPL_LINK_INLINE( BasicPrinter, Abort , void *, EMPTYARG,
170 AbortJob();
171 return 0L;
175 /////////////////////////////////////////////////////////////////////////
177 PrintingDialog::PrintingDialog
178 ( Window* pParent, BasicPrinter* pPrn, ResId& rId, String& rName )
179 : ModelessDialog( pParent, rId )
180 , aName ( rName )
181 , aText ( this, ResId( RID_TEXT, *rId.GetResMgr() ) )
182 , aCancel( this, ResId( RID_CANCEL, *rId.GetResMgr() ) )
184 FreeResource();
185 aCancel.SetClickHdl( LINK( pPrn, BasicPrinter, Abort ) );
188 void PrintingDialog::ChangeMessage( short nPage )
190 String aMsg( SttResId( IDS_PRINTMSG ) );
191 aMsg += aName;
192 aMsg += CUniString("\n");
193 aMsg += String( SttResId( IDS_PAGE ) );
194 aMsg += String::CreateFromInt32( nPage );
195 aText.SetText( aMsg );