Debian Codegraph @

src/af/xap/cocoa/xap_CocoaDlg_Print.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (C) 1998 AbiSource, Inc.
3 * Copyright (C) 2003-2009 Hubert Figuiere
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301 USA.
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <string>
26
27 #import <Cocoa/Cocoa.h>
28
29 #include "ut_debugmsg.h"
30 #include "ut_string.h"
31 #include "ut_assert.h"
32 #include "pd_Document.h"
33 #include "gr_CocoaCairoGraphics.h"
34 #include "xap_Dialog_Id.h"
35 #include "xap_CocoaDlg_Print.h"
36 #include "xap_CocoaApp.h"
37 #include "xap_CocoaFrame.h"
38 #import "xap_PrintingNSView.h"
39 #include "fv_View.h"
40 #include "fl_DocLayout.h"
41 #include "ap_Strings.h"
42 #include "ap_CocoaFrame.h"
43 #include "ap_PrintingDelegate.h"
44
45 /*****************************************************************/
46
47 XAP_Dialog * XAP_CocoaDialog_Print::static_constructor(XAP_DialogFactory * pFactory,
48 XAP_Dialog_Id dlgid)
49 {
50 XAP_CocoaDialog_Print * p = new XAP_CocoaDialog_Print(pFactory,dlgid);
51 return p;
52 }
53
54 XAP_CocoaDialog_Print::XAP_CocoaDialog_Print(XAP_DialogFactory * pDlgFactory,
55 XAP_Dialog_Id dlgid)
56 : XAP_Dialog_Print(pDlgFactory,dlgid),
57 m_pPrintGraphics(NULL)
58 {
59 }
60
61 XAP_CocoaDialog_Print::~XAP_CocoaDialog_Print(void)
62 {
63 // DELETEP(m_pPrintGraphics);
64 }
65
66 void XAP_CocoaDialog_Print::useStart(void)
67 {
68 XAP_Dialog_Print::useStart();
69
70 }
71
72 void XAP_CocoaDialog_Print::useEnd(void)
73 {
74 XAP_Dialog_Print::useEnd();
75 }
76
77 GR_Graphics * XAP_CocoaDialog_Print::getPrinterGraphicsContext(void)
78 {
79 if (m_pPrintGraphics == NULL) {
80 NSSize size = [[NSPrintInfo sharedPrintInfo] paperSize]; // TODO get the size from a real data
81 XAP_PrintingNSView* printingView = [[XAP_PrintingNSView alloc] initWithFrame:NSMakeRect(0,0,size.width,size.height)];
82 GR_CocoaCairoAllocInfo ai(printingView);
83 m_pPrintGraphics = (GR_CocoaCairoGraphics*)XAP_App::getApp()->newGraphics(ai);
84 m_pPrintGraphics->setIsPrinting(true);
85 }
86 return m_pPrintGraphics;
87 }
88
89
90 void XAP_CocoaDialog_Print::releasePrinterGraphicsContext(GR_Graphics * pGraphics)
91 {
92 UT_ASSERT(pGraphics == m_pPrintGraphics);
93 DELETEP(pGraphics);
94 }
95
96 /*****************************************************************/
97
98 void XAP_CocoaDialog_Print::runModal(XAP_Frame * /*pFrame*/)
99 {
100 UT_ASSERT_NOT_REACHED();
101 }
102
103
104 void XAP_CocoaDialog_Print::runPrint(XAP_Frame * /*pFrame*/, FV_View * pPrintView,
105 UT_sint32 /*iWidth*/, UT_sint32 /*iHeight*/)
106 {
107 UT_ASSERT(m_pPrintGraphics);
108 XAP_PrintingNSView* printingView = (XAP_PrintingNSView*)m_pPrintGraphics->getView();
109 [printingView setPrintingDelegate:(new AP_PrintingDelegate(pPrintView))];
110 fp_PageSize ps = pPrintView->getPageSize();
111 bool orient = ps.isPortrait ();
112 m_pPrintGraphics->setPortrait (orient);
113
114
115 NSPrintOperation * op = [NSPrintOperation printOperationWithView:printingView];
116 if (m_bBypassActualDialog) {
117 [op setShowsPrintPanel:NO];
118 }
119 [op runOperation];
120 }
121
122
123
124 /*****************************************************************/
125
126
127 bool s_doPrint(FV_View * pView, bool bTryToSuppressDialog,bool bPrintDirectly)
128 {
129 XAP_Frame * pFrame = static_cast<XAP_Frame *> ( pView->getParentData());
130 UT_ASSERT(pFrame);
131
132 pFrame->raise();
133
134 XAP_DialogFactory * pDialogFactory
135 = static_cast<XAP_DialogFactory *>(pFrame->getDialogFactory());
136
137 XAP_Dialog_Print * pDialog
138 = static_cast<XAP_Dialog_Print *>(pDialogFactory->requestDialog(bPrintDirectly? XAP_DIALOG_ID_PRINT_DIRECTLY: XAP_DIALOG_ID_PRINT));
139 UT_ASSERT(pDialog);
140 if (!pDialog)
141 return false;
142
143 FL_DocLayout* pLayout = pView->getLayout();
144 PD_Document * doc = pLayout->getDocument();
145
146 pDialog->setPaperSize (pView->getPageSize().getPredefinedName());
147 pDialog->setDocumentTitle(pFrame->getNonDecoratedTitle());
148 pDialog->setDocumentPathname((doc->getFilename())
149 ? doc->getFilename()
150 : pFrame->getNonDecoratedTitle());
151 pDialog->setEnablePageRangeButton(true,1,pLayout->countPages());
152 pDialog->setEnablePrintSelection(false); // TODO change this when we know how to do it.
153 pDialog->setEnablePrintToFile(true);
154 pDialog->setTryToBypassActualDialog(bTryToSuppressDialog);
155
156 //
157 // Turn on Wait cursor
158 //
159 pView->setCursorWait();
160 // s_pLoadingFrame = pFrame;
161 // s_pLoadingDoc = static_cast<AD_Document *>(doc);
162
163 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
164 std::string msg;
165 pSS->getValueUTF8(AP_STRING_ID_MSG_PrintingDoc, msg);
166
167 pFrame->setStatusMessage (msg);
168
169 // TODO these are here temporarily to make printing work. We'll fix the hack later.
170 // BUGBUG assumes all pages are same size and orientation
171 UT_sint32 iWidth = pLayout->getWidth();
172 UT_sint32 iHeight = pLayout->getHeight() / pLayout->countPages();
173
174 GR_Graphics * pGraphics = dynamic_cast<XAP_CocoaDialog_Print*>(pDialog)->getPrinterGraphicsContext();
175 UT_ASSERT(pGraphics->queryProperties(GR_Graphics::DGP_PAPER));
176
177 FL_DocLayout * pDocLayout = new FL_DocLayout(doc,pGraphics);
178 FV_View * pPrintView = new FV_View(XAP_App::getApp(),0,pDocLayout);
179 pPrintView->getLayout()->fillLayouts();
180 pPrintView->getLayout()->formatAll();
181
182
183 // const char *pDocName = ((doc->getFilename()) ? doc->getFilename() : pFrame->getNonDecoratedTitle());
184
185 dynamic_cast<XAP_CocoaDialog_Print*>(pDialog)->runPrint(pFrame, pPrintView, iWidth, iHeight);
186
187 // s_actuallyPrint(doc, pGraphics, pPrintView, pDocName, nCopies, bCollate,
188 // iWidth, iHeight, nToPage, nFromPage);
189
190 delete pDocLayout;
191 delete pPrintView;
192
193 pDialog->releasePrinterGraphicsContext(pGraphics);
194
195 //
196 // Turn off wait cursor
197 //
198 pView->clearCursorWait();
199 // s_pLoadingFrame = NULL;
200
201
202 XAP_Dialog_Print::tAnswer ans = pDialog->getAnswer();
203 bool bOK = (ans == XAP_Dialog_Print::a_OK);
204
205
206 pDialogFactory->releaseDialog(pDialog);
207
208 return bOK;
209 }