Debian Codegraph @

src/af/xap/gtk/xap_UnixDlg_Print.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 /* AbiSource Application Framework
3 * Copyright (C) 2003 Dom Lachowicz
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 "xap_UnixDlg_Print.h"
22
23 #include <gtk/gtk.h>
24
25 #include "ut_assert.h"
26 #include "xap_UnixDialogHelper.h"
27 #include "xap_Dialog_Id.h"
28 #include "xap_App.h"
29 #include "xap_Strings.h"
30 #include "xap_Frame.h"
31 #include "ut_misc.h"
32 #include "xad_Document.h"
33 #include "pd_Document.h"
34
35 #include "gr_CairoPrintGraphics.h"
36 #include "fl_DocLayout.h"
37 #include "fv_View.h"
38 #include "xap_UnixFrameImpl.h"
39 #include "ap_FrameData.h"
40 #include "gr_DrawArgs.h"
41 #include "ap_Strings.h"
42 #include "ap_EditMethods.h"
43
44 #define GTKPRINTRES 72.
45
46 static void s_Begin_Print(GtkPrintOperation * ,
47 GtkPrintContext *context,
48 gpointer p)
49 {
50 XAP_UnixDialog_Print * pDlg = (XAP_UnixDialog_Print *) p;
51 //
52 // Use context to extract the cairo_t surface for the print.
53 //
54 pDlg->BeginPrint(context);
55 }
56
57 static void s_Print_Page(GtkPrintOperation * , GtkPrintContext *, gint page_nr,gpointer p)
58 {
59 XAP_UnixDialog_Print * pDlg = (XAP_UnixDialog_Print *) p;
60 pDlg->PrintPage(page_nr);
61 }
62
63 XAP_Dialog * XAP_UnixDialog_Print::static_constructor(XAP_DialogFactory * pFactory,
64 XAP_Dialog_Id id)
65 {
66 return new XAP_UnixDialog_Print(pFactory,id);
67 }
68
69 XAP_UnixDialog_Print::XAP_UnixDialog_Print(XAP_DialogFactory * pDlgFactory,
70 XAP_Dialog_Id id)
71 : XAP_Dialog_Print(pDlgFactory,id),
72 m_pPrintGraphics (NULL),
73 m_bIsPreview(false),
74 m_pPageSetup(NULL),
75 m_pGtkPageSize(NULL),
76 m_pPO(NULL),
77 m_pView(NULL),
78 m_iNumberPages(0),
79 m_iCurrentPage(0),
80 m_pDL(NULL),
81 m_pPrintView(NULL),
82 m_pPrintLayout(NULL),
83 m_bDidQuickPrint(false),
84 m_bShowParagraphs(false),
85 m_pFrame(NULL)
86 {
87 }
88
89 XAP_UnixDialog_Print::~XAP_UnixDialog_Print(void)
90 {
91 }
92
93 GR_Graphics * XAP_UnixDialog_Print::getPrinterGraphicsContext(void)
94 {
95 return m_pPrintGraphics;
96 }
97
98 void XAP_UnixDialog_Print::releasePrinterGraphicsContext(GR_Graphics * pGraphics)
99 {
100 UT_UNUSED(pGraphics);
101 UT_ASSERT(pGraphics == m_pPrintGraphics);
102 DELETEP(m_pPrintGraphics);
103 if(m_pPageSetup)
104 g_object_unref(m_pPageSetup);
105 m_pPageSetup = NULL;
106 if(m_pGtkPageSize)
107 gtk_paper_size_free (m_pGtkPageSize);
108 m_pGtkPageSize= NULL;
109 if(m_pPO)
110 g_object_unref(m_pPO);
111 m_pPO= NULL;
112 }
113
114 /*****************************************************************/
115 /*****************************************************************/
116
117 void XAP_UnixDialog_Print::BeginPrint(GtkPrintContext *context)
118 {
119 // Note: Help landscape printing survive, don't do anything on the cairo
120 // context in this function. Any transformations etc. shall take place in
121 // PrintPage, because GtkPrint may do some transformations itself (which
122 // will come out differently if we scale here).
123 cairo_t* cr = gtk_print_context_get_cairo_context (context);
124 //
125 // The cairo context is automatically unref'd at the end of the print
126 // so we need to reference it to allow it to be deleted by the PrintGraphics
127 // class
128 cairo_reference(cr);
129 gtk_print_operation_set_n_pages (m_pPO,m_iNumberPages);
130 AP_FrameData *pFrameData = static_cast<AP_FrameData *>(m_pFrame->getFrameData());
131
132 xxx_UT_DEBUGMSG(("Initial Cairo Context %x \n",cr));
133 m_pPrintGraphics = (GR_Graphics *) new GR_CairoPrintGraphics(cr, gr_PRINTRES);
134
135 double ScreenRes = m_pView->getGraphics()->getDeviceResolution();
136 static_cast<GR_CairoPrintGraphics *>(m_pPrintGraphics)
137 ->setResolutionRatio(gr_PRINTRES/ScreenRes);
138
139 if(m_pView->getViewMode() == VIEW_PRINT )
140 {
141 m_pPrintLayout = m_pDL;
142 m_pPrintView = m_pView;
143 m_pPrintLayout->setQuickPrint(m_pPrintGraphics);
144 m_bDidQuickPrint = true;
145 if(pFrameData->m_bShowPara)
146 {
147 m_pPrintView->setShowPara(false);
148 m_bShowParagraphs = true;
149 }
150 else
151 m_bShowParagraphs = false;
152 }
153 else
154 {
155 m_pPrintLayout = new FL_DocLayout(m_pView->getDocument(),m_pPrintGraphics);
156 m_pPrintView = new FV_View(XAP_App::getApp(),0,m_pPrintLayout);
157 m_pPrintView->getLayout()->fillLayouts();
158 m_pPrintView->getLayout()->formatAll();
159 m_pPrintView->getLayout()->recalculateTOCFields();
160 m_bDidQuickPrint = false;
161 }
162 m_pPrintGraphics->startPrint();
163 }
164 void XAP_UnixDialog_Print::PrintPage(gint page_nr)
165 {
166 xxx_UT_DEBUGMSG(("Print Page %d \n",page_nr));
167
168 m_pPrintGraphics->beginPaint();
169 cairo_t *cr = static_cast<GR_CairoPrintGraphics *>(m_pPrintGraphics)->getCairo();
170
171 //
172 // We set the resolution of the printer context to higher than screen
173 // so we don't loose resolution when printing images.
174 //
175 // We then correct for this with thise scale. GtkPrint contexts always
176 // assume 72 DPI
177 //
178 // In the future we can use this to do 2,4,6,8 etc pages per page
179 //
180 double srat = static_cast<double>(GTKPRINTRES)/static_cast<double>(gr_PRINTRES);
181 cairo_scale(cr, srat, srat);
182 xxx_UT_DEBUGMSG(("Resolution Ratio set to %f \n",gr_PrintRes/ScreenRes));
183 UT_DEBUGMSG(("Resolution Ratio of Direct call is %f Cast call is %f \n",m_pPrintGraphics->getResolutionRatio(), static_cast<GR_CairoPrintGraphics *>(m_pPrintGraphics)->getResolutionRatio()));
184
185 dg_DrawArgs da;
186 da.pG = m_pPrintGraphics;
187 da.xoff = 0;
188 da.yoff = 0;
189 const XAP_StringSet *pSS = XAP_App::getApp()->getStringSet ();
190 const gchar * msgTmpl = pSS->getValue (AP_STRING_ID_MSG_PrintStatus);
191 gchar msgBuf [1024];
192 sprintf (msgBuf, msgTmpl, page_nr+1, m_iNumberPages);
193 if(m_pFrame)
194 {
195 m_pFrame->setStatusMessage ( msgBuf );
196 m_pFrame->nullUpdate();
197 }
198 m_pPrintView->draw(page_nr, &da);
199 m_pPrintGraphics->endPaint();
200 }
201
202 void XAP_UnixDialog_Print::setPreview(bool b)
203 {
204 m_bIsPreview = b;
205 }
206
207 void XAP_UnixDialog_Print::setupPrint()
208 {
209 double blockMrgnLeft, blockMrgnRight, mrgnTop, mrgnBottom, mrgnLeft, mrgnRight = 0.;
210 double width, height;
211 bool portrait;
212
213 m_pView = static_cast<FV_View*>(m_pFrame->getCurrentView());
214 m_pPO = gtk_print_operation_new();
215 //
216 // Set filename if it's not present already
217 //
218 std::string sURI = m_pView->getDocument()->getPrintFilename();
219
220 if(sURI.empty())
221 {
222 const char * filename = m_pView->getDocument()->getFilename();
223 if(filename) {
224 sURI = filename;
225 UT_addOrReplacePathSuffix(sURI, ".pdf");
226 }
227 }
228 if(!sURI.empty()) {
229 GtkPrintSettings * pSettings = gtk_print_settings_new();
230 gtk_print_settings_set(pSettings,
231 GTK_PRINT_SETTINGS_OUTPUT_URI,
232 sURI.c_str() );
233 gtk_print_operation_set_print_settings(m_pPO,pSettings);
234 g_object_unref(pSettings);
235 }
236
237 s_getPageMargins(m_pView, blockMrgnLeft, blockMrgnRight, mrgnLeft, mrgnRight, mrgnTop, mrgnBottom);
238
239 portrait = m_pView->getPageSize().isPortrait();
240
241 width = m_pView->getPageSize().Width (DIM_MM);
242 height = m_pView->getPageSize().Height (DIM_MM);
243
244 m_pPageSetup = gtk_page_setup_new();
245
246 char * pszName = m_pView->getPageSize().getPredefinedName();
247 bool isPredefined = false;
248 const char * pszGtkName = NULL;
249 if(pszName == NULL)
250 {
251 }
252 else if(g_ascii_strcasecmp(pszName,"Custom") == 0)
253 {
254 }
255 else if(g_ascii_strcasecmp(pszName,"A0") == 0)
256 {
257 isPredefined = true;
258 pszGtkName = "iso_a0";
259 }
260 else if(g_ascii_strcasecmp(pszName,"A1") == 0)
261 {
262 isPredefined = true;
263 pszGtkName = "iso_a1";
264 }
265 else if(g_ascii_strcasecmp(pszName,"A2") == 0)
266 {
267 isPredefined = true;
268 pszGtkName = "iso_a2";
269 }
270 else if(g_ascii_strcasecmp(pszName,"A3") == 0)
271 {
272 isPredefined = true;
273 pszGtkName = "iso_a3";
274 }
275 else if(g_ascii_strcasecmp(pszName,"A4") == 0)
276 {
277 isPredefined = true;
278 pszGtkName = "iso_a4";
279 }
280 else if(g_ascii_strcasecmp(pszName,"A5") == 0)
281 {
282 isPredefined = true;
283 pszGtkName = "iso_a5";
284 }
285 else if(g_ascii_strcasecmp(pszName,"A6") == 0)
286 {
287 isPredefined = true;
288 pszGtkName = "iso_a6";
289 }
290 else if(g_ascii_strcasecmp(pszName,"A7") == 0)
291 {
292 isPredefined = true;
293 pszGtkName = "iso_a7";
294 }
295 else if(g_ascii_strcasecmp(pszName,"A8") == 0)
296 {
297 isPredefined = true;
298 pszGtkName = "iso_a8";
299 }
300 else if(g_ascii_strcasecmp(pszName,"A9") == 0)
301 {
302 isPredefined = true;
303 pszGtkName = "iso_a9";
304 }
305 else if(g_ascii_strcasecmp(pszName,"B0") == 0)
306 {
307 isPredefined = true;
308 pszGtkName = "iso_b0";
309 }
310 else if(g_ascii_strcasecmp(pszName,"B1") == 0)
311 {
312 isPredefined = true;
313 pszGtkName = "iso_b1";
314 }
315 else if(g_ascii_strcasecmp(pszName,"B2") == 0)
316 {
317 isPredefined = true;
318 pszGtkName = "iso_b2";
319 }
320 else if(g_ascii_strcasecmp(pszName,"B3") == 0)
321 {
322 isPredefined = true;
323 pszGtkName = "iso_b3";
324 }
325 else if(g_ascii_strcasecmp(pszName,"B4") == 0)
326 {
327 isPredefined = true;
328 pszGtkName = "iso_b4";
329 }
330 else if(g_ascii_strcasecmp(pszName,"B4") == 0)
331 {
332 isPredefined = true;
333 pszGtkName = "iso_b4";
334 }
335 else if(g_ascii_strcasecmp(pszName,"B5") == 0)
336 {
337 isPredefined = true;
338 pszGtkName = "iso_b5";
339 }
340 else if(g_ascii_strcasecmp(pszName,"B6") == 0)
341 {
342 isPredefined = true;
343 pszGtkName = "iso_b6";
344 }
345 else if(g_ascii_strcasecmp(pszName,"B7") == 0)
346 {
347 isPredefined = true;
348 pszGtkName = "iso_b7";
349 }
350 else if(g_ascii_strcasecmp(pszName,"Legal") == 0)
351 {
352 isPredefined = true;
353 pszGtkName = "na_legal";
354 }
355 else if(g_ascii_strcasecmp(pszName,"Letter") == 0)
356 {
357 isPredefined = true;
358 pszGtkName = "na_letter";
359 }
360 if(isPredefined)
361 {
362 m_pGtkPageSize = gtk_paper_size_new(static_cast<const gchar *>(pszGtkName));
363 }
364 else
365 {
366 /*
367 * Width() and Height() will return the paper size as shown in the UI.
368 * Gtk wants the real paper size, however, and will swap the two
369 * itself when we specify the orientation.
370 */
371 m_pGtkPageSize = gtk_paper_size_new_custom("custom",
372 "custom",
373 portrait ? width : height,
374 portrait ? height : width,
375 GTK_UNIT_MM);
376 }
377 //
378 // Set the Page Size
379 //
380 gtk_page_setup_set_paper_size(m_pPageSetup,m_pGtkPageSize);
381 //
382 // Set the margins
383 //
384 gtk_page_setup_set_top_margin(m_pPageSetup,mrgnTop,GTK_UNIT_INCH);
385 gtk_page_setup_set_bottom_margin(m_pPageSetup,mrgnBottom,GTK_UNIT_INCH);
386 gtk_page_setup_set_left_margin(m_pPageSetup,mrgnLeft,GTK_UNIT_INCH);
387 gtk_page_setup_set_right_margin(m_pPageSetup,mrgnRight,GTK_UNIT_INCH);
388 //
389 // Set orientation
390 //
391 if( portrait)
392 gtk_page_setup_set_orientation(m_pPageSetup,GTK_PAGE_ORIENTATION_PORTRAIT);
393 else
394 gtk_page_setup_set_orientation(m_pPageSetup,GTK_PAGE_ORIENTATION_LANDSCAPE);
395 gtk_print_operation_set_default_page_setup(m_pPO,m_pPageSetup);
396 gtk_print_operation_set_use_full_page (m_pPO, true);
397 m_pDL = m_pView->getLayout();
398 m_iCurrentPage = m_pDL->findPage(m_pView->getCurrentPage());
399 m_iNumberPages = (gint) m_pDL->countPages();
400 gtk_print_operation_set_current_page(m_pPO,m_iCurrentPage);
401
402 g_signal_connect (m_pPO, "begin_print", G_CALLBACK (s_Begin_Print), this);
403 g_signal_connect (m_pPO, "draw_page", G_CALLBACK (s_Print_Page), this);
404 }
405
406 void XAP_UnixDialog_Print::runModal(XAP_Frame * pFrame)
407 {
408 m_pFrame = pFrame;
409 setupPrint();
410 gtk_print_operation_set_show_progress(m_pPO, TRUE);
411
412 XAP_UnixFrameImpl * pUnixFrameImpl = static_cast<XAP_UnixFrameImpl *>(m_pFrame->getFrameImpl());
413
414 // Get the GtkWindow of the parent frame
415 GtkWidget * parent = pUnixFrameImpl->getTopLevelWindow();
416 GtkWindow * pPWindow = GTK_WINDOW(parent);
417 // const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
418 // const gchar * szDialogName = einterpret_cast<const gchar *>(pSS->getValue(XAP_STRING_ID_DLG_UP_PrintTitle);
419
420 gtk_print_operation_run (m_pPO,
421 (m_bIsPreview)? GTK_PRINT_OPERATION_ACTION_PREVIEW:
422 GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
423 pPWindow, NULL);
424
425 cleanup();
426 }
427
428 void XAP_UnixDialog_Print::cleanup(void)
429 {
430 //
431 // Get the filename we printed to
432 //
433 GtkPrintSettings * pSettings = gtk_print_operation_get_print_settings(m_pPO);
434 const gchar * szFname = gtk_print_settings_get(pSettings,GTK_PRINT_SETTINGS_OUTPUT_URI);
435 if((szFname != NULL) && (strcmp(szFname,"output.pdf") != 0))
436 {
437 m_pView->getDocument()->setPrintFilename(szFname);
438 }
439 g_object_unref(m_pPO);
440 m_pPO= NULL;
441 if(!m_bDidQuickPrint)
442 {
443 UT_DEBUGMSG(("Deleting PrintView %p \n",m_pPrintView));
444 DELETEP(m_pPrintLayout);
445 DELETEP(m_pPrintView);
446 }
447 else
448 {
449 if(m_pPrintLayout)
450 m_pPrintLayout->setQuickPrint(NULL);
451 m_pPrintLayout = NULL;
452 m_pPrintView = NULL;
453
454 if(m_bShowParagraphs)
455 m_pView->setShowPara(true);
456 m_pDL->incrementGraphicTick();
457 }
458 UT_DEBUGMSG(("Reset fontmap m_pView %p Graphics %p \n",m_pView,m_pView->getGraphics()));
459 static_cast<GR_CairoGraphics *>(m_pView->getGraphics())->resetFontMapResolution();
460 DELETEP(m_pPrintGraphics);
461 //
462 // Finish pending expose events.
463 //
464 m_pFrame->nullUpdate();
465 }
466
467 void XAP_UnixDialog_Print::PrintDirectly(XAP_Frame * pFrame, const char * szFilename, const char * szPrinter)
468 {
469 m_pFrame = pFrame;
470 setupPrint();
471 if(szFilename)
472 {
473 gtk_print_operation_set_export_filename(m_pPO, szFilename);
474 gtk_print_operation_run (m_pPO,GTK_PRINT_OPERATION_ACTION_EXPORT,
475 NULL,NULL);
476 }
477 else
478 {
479 GtkPrintSettings * pSettings = gtk_print_operation_get_print_settings(m_pPO);
480 if(szPrinter)
481 {
482 gtk_print_settings_set_printer(pSettings, szPrinter);
483 }
484 else
485 {
486 gtk_print_settings_set_printer(pSettings, GTK_PRINT_SETTINGS_PRINTER);
487 }
488 gtk_print_operation_set_print_settings(m_pPO,pSettings);
489 gtk_print_operation_run (m_pPO,GTK_PRINT_OPERATION_ACTION_PRINT,
490 NULL,NULL);
491 }
492 cleanup();
493 }