Debian Codegraph @

src/af/xap/win/xap_Win32Dlg_PrintPreview.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (C) 1998 AbiSource, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <glib/gstdio.h>
24 #include <io.h>
25
26 #include "ut_assert.h"
27 #include "ut_string.h"
28 #include "ut_path.h"
29 #include "ut_debugmsg.h"
30 #include "xap_Dialog_Id.h"
31 #include "xap_Win32Dlg_PrintPreview.h"
32 #include "xap_Frame.h"
33 #include "xap_App.h"
34 #include "xap_DialogFactory.h"
35 #include "gr_Win32Graphics.h"
36 #include "ut_rand.h"
37 #include "ut_Win32OS.h"
38
39 #include "fp_PageSize.h"
40 #include "fv_View.h"
41
42 #define INITGUID
43 #include <ole2.h>
44
45 #define GDIPCONST const
46 #define WINGDIPAPI __stdcall
47
48 typedef int GpStatus;
49 typedef int PixelFormat;
50
51 typedef void GpImage;
52 typedef void GpBitmap;
53 typedef void GpMetafile;
54 typedef void GpGraphics;
55
56 struct _GdiplusStartupInput
57 {
58 guint32 GdiplusVersion;
59 gpointer DebugEventCallback;
60 gboolean SuppressBackgroundThread;
61 gboolean SuppressExternalCodecs;
62 };
63 typedef struct _GdiplusStartupInput GdiplusStartupInput;
64
65 struct EncoderParameter
66 {
67 GUID Guid;
68 ULONG NumberOfValues;
69 ULONG Type;
70 VOID* Value;
71 };
72
73 struct EncoderParameters
74 {
75 UINT Count; /* Number of parameters in this structure */
76 EncoderParameter Parameter[1]; /* Parameter values */
77 };
78
79 typedef enum {
80 EncoderParameterValueTypeByte = 1,
81 EncoderParameterValueTypeASCII = 2,
82 EncoderParameterValueTypeShort = 3,
83 EncoderParameterValueTypeLong = 4,
84 EncoderParameterValueTypeRational = 5,
85 EncoderParameterValueTypeLongRange = 6,
86 EncoderParameterValueTypeUndefined = 7,
87 EncoderParameterValueTypeRationalRange = 8,
88 EncoderParameterValueTypePointer = 9
89 } EncoderParameterValueType;
90
91 enum EncoderValue
92 {
93 EncoderValueColorTypeCMYK,
94 EncoderValueColorTypeYCCK,
95 EncoderValueCompressionLZW,
96 EncoderValueCompressionCCITT3,
97 EncoderValueCompressionCCITT4,
98 EncoderValueCompressionRle,
99 EncoderValueCompressionNone,
100 EncoderValueScanMethodInterlaced,
101 EncoderValueScanMethodNonInterlaced,
102 EncoderValueVersionGif87,
103 EncoderValueVersionGif89,
104 EncoderValueRenderProgressive,
105 EncoderValueRenderNonProgressive,
106 EncoderValueTransformRotate90,
107 EncoderValueTransformRotate180,
108 EncoderValueTransformRotate270,
109 EncoderValueTransformFlipHorizontal,
110 EncoderValueTransformFlipVertical,
111 EncoderValueMultiFrame,
112 EncoderValueLastFrame,
113 EncoderValueFlush,
114 EncoderValueFrameDimensionTime,
115 EncoderValueFrameDimensionResolution,
116 EncoderValueFrameDimensionPage
117 };
118
119 #define PixelFormatIndexed 0x00010000 // Indexes into a palette
120 #define PixelFormatGDI 0x00020000 // Is a GDI-supported format
121 #define PixelFormatAlpha 0x00040000 // Has an alpha component
122 #define PixelFormatPAlpha 0x00080000 // Pre-multiplied alpha
123 #define PixelFormatExtended 0x00100000 // Extended color 16 bits/channel
124 #define PixelFormatCanonical 0x00200000
125
126 #define PixelFormatUndefined 0
127 #define PixelFormatDontCare 0
128
129 #define PixelFormat1bppIndexed (1 | ( 1 << 8) | PixelFormatIndexed | PixelFormatGDI)
130 #define PixelFormat4bppIndexed (2 | ( 4 << 8) | PixelFormatIndexed | PixelFormatGDI)
131 #define PixelFormat8bppIndexed (3 | ( 8 << 8) | PixelFormatIndexed | PixelFormatGDI)
132 #define PixelFormat16bppGrayScale (4 | (16 << 8) | PixelFormatExtended)
133 #define PixelFormat16bppRGB555 (5 | (16 << 8) | PixelFormatGDI)
134 #define PixelFormat16bppRGB565 (6 | (16 << 8) | PixelFormatGDI)
135 #define PixelFormat16bppARGB1555 (7 | (16 << 8) | PixelFormatAlpha | PixelFormatGDI)
136 #define PixelFormat24bppRGB (8 | (24 << 8) | PixelFormatGDI)
137 #define PixelFormat32bppRGB (9 | (32 << 8) | PixelFormatGDI)
138 #define PixelFormat32bppARGB (10 | (32 << 8) | PixelFormatAlpha | PixelFormatGDI | PixelFormatCanonical)
139 #define PixelFormat32bppPARGB (11 | (32 << 8) | PixelFormatAlpha | PixelFormatPAlpha | PixelFormatGDI)
140 #define PixelFormat48bppRGB (12 | (48 << 8) | PixelFormatExtended)
141 #define PixelFormat64bppARGB (13 | (64 << 8) | PixelFormatAlpha | PixelFormatCanonical | PixelFormatExtended)
142 #define PixelFormat64bppPARGB (14 | (64 << 8) | PixelFormatAlpha | PixelFormatPAlpha | PixelFormatExtended)
143
144 typedef DWORD ARGB;
145
146 typedef GpStatus (WINGDIPAPI* GdiplusStartupFunc) (gpointer, const gpointer, gpointer);
147 typedef GpStatus (WINGDIPAPI* GdipDisposeImageFunc) (GpImage*);
148 typedef GpStatus (WINGDIPAPI* GdipSaveImageToFileFunc) (GpImage *image, GDIPCONST WCHAR* filename,
149 GDIPCONST CLSID* clsidEncoder,
150 GDIPCONST EncoderParameters* encoderParams);
151 typedef GpStatus (WINGDIPAPI* GdipLoadImageFromFileFunc) (GDIPCONST WCHAR* filename, GpImage **image);
152 typedef GpStatus (WINGDIPAPI* GdipSaveAddFunc) (GpImage *image, GDIPCONST EncoderParameters* encoderParams);
153 typedef GpStatus (WINGDIPAPI* GdipSaveAddImageFunc) (GpImage *orig, GpImage *image,
154 GDIPCONST EncoderParameters* encoderParams);
155 typedef GpStatus (WINGDIPAPI* GdipCreateMetafileFromEmfFunc) (HENHMETAFILE hEmf, BOOL deleteEmf, GpMetafile **image);
156 typedef GpStatus (WINGDIPAPI* GdipCreateMetafileFromFileFunc) (GDIPCONST WCHAR* file, GpMetafile **metafile);
157 typedef GpStatus (WINGDIPAPI* GdipCreateBitmapFromFileFunc) (GDIPCONST WCHAR* filename, GpBitmap **bitmap);
158 typedef GpStatus (WINGDIPAPI* GdipDeleteGraphicsFunc) (GpGraphics *graphics);
159 typedef GpStatus (WINGDIPAPI* GdipCreateBitmapFromScan0Func) (INT width, INT height, INT stride, PixelFormat format, BYTE* scan0,
160 GpBitmap** bitmap);
161 typedef GpStatus (WINGDIPAPI* GdipGetImageWidthFunc) (GpImage*, guint*);
162 typedef GpStatus (WINGDIPAPI* GdipGetImageHeightFunc) (GpImage*, guint*);
163 typedef GpStatus (WINGDIPAPI* GdipDrawImageIFunc) (GpGraphics *graphics, GpImage *image, INT x, INT y);
164 typedef GpStatus (WINGDIPAPI* GdipGetImageGraphicsContextFunc) (GpImage *image, GpGraphics **graphics);
165 typedef GpStatus (WINGDIPAPI* GdipFlushFunc) (GpGraphics *graphics, INT intention);
166 typedef GpStatus (WINGDIPAPI* GdipGraphicsClearFunc) (GpGraphics *graphics, ARGB color);
167 typedef GpStatus (WINGDIPAPI* GdipBitmapSetResolutionFunc) (GpBitmap* bitmap, float xdpi, float ydpi);
168 typedef GpStatus (WINGDIPAPI* GdipGetImageHorizontalResolutionFunc) (GpImage *image, float *resolution);
169 typedef GpStatus (WINGDIPAPI* GdipGetImageVerticalResolutionFunc) (GpImage *image, float *resolution);
170
171 static GdiplusStartupFunc GdiplusStartup = 0;
172 static GdipDisposeImageFunc GdipDisposeImage = 0;
173 static GdipSaveImageToFileFunc GdipSaveImageToFile = 0;
174 static GdipLoadImageFromFileFunc GdipLoadImageFromFile = 0;
175 static GdipSaveAddImageFunc GdipSaveAddImage = 0;
176 static GdipSaveAddFunc GdipSaveAdd = 0;
177 static GdipCreateMetafileFromEmfFunc GdipCreateMetafileFromEmf = 0;
178 static GdipCreateMetafileFromFileFunc GdipCreateMetafileFromFile = 0;
179 static GdipCreateBitmapFromFileFunc GdipCreateBitmapFromFile = 0;
180 static GdipDeleteGraphicsFunc GdipDeleteGraphics = 0;
181 static GdipCreateBitmapFromScan0Func GdipCreateBitmapFromScan0 = 0;
182 static GdipGetImageWidthFunc GdipGetImageWidth = 0;
183 static GdipGetImageHeightFunc GdipGetImageHeight = 0;
184 static GdipDrawImageIFunc GdipDrawImageI = 0;
185 static GdipGetImageGraphicsContextFunc GdipGetImageGraphicsContext = 0;
186 static GdipFlushFunc GdipFlush = 0;
187 static GdipGraphicsClearFunc GdipGraphicsClear = 0;
188 static GdipBitmapSetResolutionFunc GdipBitmapSetResolution = 0;
189 static GdipGetImageHorizontalResolutionFunc GdipGetImageHorizontalResolution = 0;
190 static GdipGetImageVerticalResolutionFunc GdipGetImageVerticalResolution = 0;
191
192 static GDIPCONST CLSID tiff_clsid = { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } };
193 static GDIPCONST GUID EncoderSaveFlag = { 0x292266FC, 0xac40, 0x47bf, { 0x8c, 0xfc, 0xa8, 0x5b, 0x89, 0xa6,0x55, 0xde } };
194
195 #if 0
196 #define d(x) G_STMT_START { (x); } G_STMT_END
197 #else
198 #define d(x) G_STMT_START { } G_STMT_END
199 #endif
200
201 static GpStatus
202 gdip_init (void)
203 {
204 GdiplusStartupInput input;
205 ULONG_PTR gdiplusToken = 0;
206 static HINSTANCE gdipluslib = NULL;
207
208 if (!gdipluslib)
209 gdipluslib = LoadLibraryW (L"gdiplus.dll");
210 else
211 return 0; /* gdip_init() is idempotent */
212
213 if (!gdipluslib)
214 return 18; // GdiplusNotInitialized
215
216 #define LOOKUP(func) \
217 G_STMT_START { \
218 func = (func##Func) GetProcAddress (gdipluslib, #func); \
219 if (!func) {\
220 g_warning ("Couldn't load function: %s\n", #func); \
221 return 18; \
222 } \
223 } G_STMT_END
224
225 LOOKUP (GdiplusStartup);
226 LOOKUP (GdipDisposeImage);
227 LOOKUP (GdipSaveImageToFile);
228 LOOKUP (GdipLoadImageFromFile);
229 LOOKUP (GdipSaveAddImage);
230 LOOKUP (GdipSaveAdd);
231 LOOKUP (GdipCreateMetafileFromEmf);
232 LOOKUP (GdipCreateMetafileFromFile);
233 LOOKUP (GdipCreateBitmapFromFile);
234 LOOKUP (GdipCreateBitmapFromScan0);
235 LOOKUP (GdipDeleteGraphics);
236 LOOKUP (GdipGetImageWidth);
237 LOOKUP (GdipGetImageHeight);
238 LOOKUP (GdipDrawImageI);
239 LOOKUP (GdipGetImageGraphicsContext);
240 LOOKUP (GdipFlush);
241 LOOKUP (GdipGraphicsClear);
242 LOOKUP (GdipBitmapSetResolution);
243 LOOKUP (GdipGetImageHorizontalResolution);
244 LOOKUP (GdipGetImageVerticalResolution);
245
246 #undef LOOKUP
247
248 input.GdiplusVersion = 1;
249 input.DebugEventCallback = NULL;
250 input.SuppressBackgroundThread = input.SuppressExternalCodecs = FALSE;
251
252 return GdiplusStartup (&gdiplusToken, &input, NULL);
253 }
254
255 #define ASSERT_GDIP_SUCCESS(status, func) \
256 G_STMT_START { \
257 GpStatus _gdip_status = (status); \
258 if (_gdip_status != 0) { \
259 g_warning ("%s failed with reason %d\n", #func, _gdip_status); \
260 } \
261 } G_STMT_END
262
263 class ABI_EXPORT GR_Win32PrintPreviewGraphics : public GR_Win32Graphics
264 {
265 public:
266
267 GR_Win32PrintPreviewGraphics(const RECT &rect)
268 : GR_Win32Graphics(createbestmetafilehdc(), getDocInfo()),
269 m_pGraphics(0),
270 page_rect(rect),
271 metafile_dc(0),
272 m_multiPageTiff(0),
273 m_tiffFilename(0)
274 {
275 GpStatus status = gdip_init ();
276 ASSERT_GDIP_SUCCESS (status, gdip_init);
277 if (status != 0)
278 throw "GDI+ initialization failed";
279
280 d (g_print ("gdip_init() was successful. preview graphics created\n"));
281
282 metafile_xres = GetDeviceCaps(getPrimaryDC(), LOGPIXELSX);
283 metafile_yres = GetDeviceCaps(getPrimaryDC(), LOGPIXELSY);
284
285 d (g_print ("xres: %d | yres: %d\n", metafile_xres, metafile_yres));
286 }
287
288 virtual ~GR_Win32PrintPreviewGraphics()
289 {
290 d (g_print ("~GR_Win32PrintPreviewGraphics()\n"));
291 if (m_multiPageTiff)
292 GdipDisposeImage (m_multiPageTiff);
293 g_free(m_tiffFilename);
294
295 // todo: delete DC and docInfo
296 }
297
298 virtual UT_uint32 getClassId() {return s_getClassId();}
299
300 virtual GR_Capability getCapability() {return GRCAP_PRINTER_ONLY;}
301
302 static const char * graphicsDescriptor(){return "Win32 Print Preview";}
303
304 virtual void drawGlyph(UT_uint32 glyph_idx, UT_sint32 xoff, UT_sint32 yoff)
305 {
306 UT_return_if_fail(m_pGraphics != 0);
307 d (g_print ("drawGlyph()\n"));
308 m_pGraphics->drawGlyph(glyph_idx, xoff, yoff);
309 }
310
311 virtual void drawChar(UT_UCSChar Char, UT_sint32 xoff, UT_sint32 yoff)
312 {
313 UT_return_if_fail(m_pGraphics != 0);
314 d (g_print ("drawChar()\n"));
315 m_pGraphics->drawChar(Char, xoff, yoff);
316 }
317
318 virtual void drawChars(const UT_UCSChar* pChars,
319 int iCharOffset, int iLength,
320 UT_sint32 xoff, UT_sint32 yoff,
321 int * pCharWidth)
322 {
323 UT_return_if_fail(m_pGraphics != 0);
324 d (g_print ("drawChars()\n"));
325 m_pGraphics->drawChars(pChars, iCharOffset, iLength, xoff, yoff, pCharWidth);
326 }
327
328 virtual void setFont(const GR_Font* pFont)
329 {
330 if (m_pGraphics) {
331 m_pGraphics->setFont(pFont);
332 d (g_print ("setFont()\n"));
333 }
334 else
335 GR_Win32Graphics::setFont(pFont);
336 }
337
338 virtual void clearFont(void)
339 {
340 if (m_pGraphics) {
341 m_pGraphics->clearFont();
342 d (g_print ("clearFont()\n"));
343 }
344 else
345 GR_Win32Graphics::clearFont();
346 }
347
348 virtual void setColor(const UT_RGBColor& clr)
349 {
350 UT_return_if_fail(m_pGraphics != 0);
351 d (g_print ("setColor()\n"));
352 m_pGraphics->setColor(clr);
353 }
354
355 virtual void getColor(UT_RGBColor& clr)
356 {
357 UT_return_if_fail(m_pGraphics != 0);
358 d (g_print ("getColor()\n"));
359 m_pGraphics->getColor(clr);
360 }
361
362 virtual void drawLine(UT_sint32 a, UT_sint32 b, UT_sint32 c, UT_sint32 d)
363 {
364 UT_return_if_fail(m_pGraphics != 0);
365 d (g_print ("drawLine()\n"));
366 m_pGraphics->drawLine(a, b, c, d);
367 }
368
369 virtual void xorLine(UT_sint32 a, UT_sint32 b, UT_sint32 c, UT_sint32 d)
370 {
371 UT_return_if_fail(m_pGraphics != 0);
372 d (g_print ("xorLine()\n"));
373 m_pGraphics->xorLine(a, b, c, d);
374 }
375
376 virtual void setLineWidth(UT_sint32 w)
377 {
378 UT_return_if_fail(m_pGraphics != 0);
379 d (g_print ("setLineWidth()\n"));
380 m_pGraphics->setLineWidth(w);
381 }
382
383 virtual void setLineProperties ( double inWidthPixels,
384 JoinStyle inJoinStyle = JOIN_MITER,
385 CapStyle inCapStyle = CAP_BUTT,
386 LineStyle inLineStyle = LINE_SOLID )
387 {
388 UT_return_if_fail(m_pGraphics != 0);
389 d (g_print ("setLineProperties()\n"));
390 m_pGraphics->setLineProperties(inWidthPixels, inJoinStyle, inCapStyle, inLineStyle);
391 }
392
393 virtual void polyLine(UT_Point * pts, UT_uint32 nPoints)
394 {
395 UT_return_if_fail(m_pGraphics != 0);
396 d (g_print ("polyLine()\n"));
397 m_pGraphics->polyLine(pts, nPoints);
398 }
399
400 virtual void fillRect(const UT_RGBColor& c,
401 UT_sint32 x, UT_sint32 y,
402 UT_sint32 w, UT_sint32 h)
403 {
404 UT_return_if_fail(m_pGraphics != 0);
405 d (g_print ("fillRect()\n"));
406 m_pGraphics->fillRect(c, x, y, w, h);
407 }
408
409 virtual void invertRect(const UT_Rect* pRect)
410 {
411 UT_return_if_fail(m_pGraphics != 0);
412 d (g_print ("invertRect()\n"));
413 m_pGraphics->invertRect(pRect);
414 }
415
416 virtual void setClipRect(const UT_Rect* pRect)
417 {
418 UT_return_if_fail(m_pGraphics != 0);
419 d (g_print ("setClipRect()\n"));
420 m_pGraphics->setClipRect(pRect);
421 }
422
423 virtual void clearArea(UT_sint32 a, UT_sint32 b, UT_sint32 c, UT_sint32 d)
424 {
425 UT_return_if_fail(m_pGraphics != 0);
426 d (g_print ("clearArea()\n"));
427 m_pGraphics->clearArea(a, b, c, d);
428 }
429
430 virtual GR_Image* createNewImage(const char* pszName, const UT_ByteBuf* pBB, const std::string& mimetype,
431 UT_sint32 iDisplayWidth, UT_sint32 iDisplayHeight,
432 GR_Image::GRType iType)
433 {
434 d (g_print ("createNewImage()\n"));
435 if (m_pGraphics)
436 return m_pGraphics->createNewImage(pszName, pBB, mimetype, iDisplayWidth, iDisplayHeight, iType);
437 else
438 return GR_Win32Graphics::createNewImage(pszName, pBB, mimetype, iDisplayWidth, iDisplayHeight, iType);
439 }
440
441 virtual void drawImage(GR_Image* pImg, UT_sint32 xDest, UT_sint32 yDest)
442 {
443 UT_return_if_fail(m_pGraphics != 0);
444 d (g_print ("drawImage()\n"));
445 m_pGraphics->drawImage(pImg, xDest, yDest);
446 }
447
448 virtual bool queryProperties(GR_Graphics::Properties gp) const
449 {
450 d (g_print ("queryProperties()\n"));
451 return (gp == DGP_PAPER);
452 }
453
454 virtual void polygon(UT_RGBColor& c,UT_Point *pts,UT_uint32 nPoints)
455 {
456 UT_return_if_fail(m_pGraphics != 0);
457 d (g_print ("polygon()\n"));
458 m_pGraphics->polygon(c, pts, nPoints);
459 }
460
461 virtual GR_Image * genImageFromRectangle(const UT_Rect & r)
462 {
463 UT_return_val_if_fail(m_pGraphics != 0, 0);
464 d (g_print ("genImageFromRectangle()\n"));
465 return m_pGraphics->genImageFromRectangle(r);
466 }
467
468 virtual void saveRectangle(UT_Rect & r, UT_uint32 iIndx)
469 {
470 UT_return_if_fail(m_pGraphics != 0);
471 d (g_print ("saveRectangle()\n"));
472 m_pGraphics->saveRectangle(r, iIndx);
473 }
474
475 virtual void restoreRectangle(UT_uint32 iIndx)
476 {
477 UT_return_if_fail(m_pGraphics != 0);
478 d (g_print ("restoreRectangle()\n"));
479 m_pGraphics->restoreRectangle(iIndx);
480 }
481
482 virtual void flush(void)
483 {
484 UT_return_if_fail(m_pGraphics != 0);
485 d (g_print ("flush()\n"));
486 m_pGraphics->flush();
487 }
488
489 virtual bool startPrint(void)
490 {
491 std::string sName = UT_createTmpFile("pr", ".tif");
492
493 d (g_print ("saving to %s\n", sName.c_str()));
494 m_tiffFilename = g_utf8_to_utf16 (sName.c_str(), -1, NULL, NULL, NULL);
495
496 return true;
497 }
498
499 virtual bool startPage(const char * /*szPageLabel*/, UT_uint32 /*pageNumber*/,
500 bool /*bPortrait*/, UT_uint32 /*iWidth*/, UT_uint32 /*iHeight*/)
501 {
502 endPage(); // prints any previous pages and clears the font cache
503
504 d (g_print ("startPage(%d)\n", pageNumber));
505
506 metafile_dc = CreateEnhMetaFileW (getPrimaryDC(), NULL, &page_rect, L"AbiWord\0Print Preview\0\0");
507
508 GR_Win32AllocInfo ai(metafile_dc, m_pDocInfo, NULL);
509 m_pGraphics = (GR_Win32Graphics *)XAP_App::getApp()->newGraphics(ai);
510
511 return true;
512 }
513
514 virtual bool endPrint(void)
515 {
516 endPage();
517
518 EncoderParameters encoder_params;
519 ULONG parameterValue = EncoderValueFlush;
520
521 encoder_params.Count = 1;
522 encoder_params.Parameter[0].Guid = EncoderSaveFlag;
523 encoder_params.Parameter[0].Type = EncoderParameterValueTypeLong;
524 encoder_params.Parameter[0].NumberOfValues = 1;
525 encoder_params.Parameter[0].Value = &parameterValue;
526
527 GpStatus status = GdipSaveAdd (m_multiPageTiff, &encoder_params);
528 ASSERT_GDIP_SUCCESS (status, GdipSaveAdd);
529
530 status = GdipDisposeImage (m_multiPageTiff);
531 m_multiPageTiff = 0;
532 ASSERT_GDIP_SUCCESS (status, GdipDisposeImage);
533
534 d (g_print("endPrint()\n"));
535
536 ShellExecuteW (NULL, L"open", (WCHAR *)m_tiffFilename, NULL, NULL, SW_SHOW);
537
538 return true;
539 }
540
541 void endPage()
542 {
543 invalidateCache(); // clear the font cache
544
545 d (g_print ("endPage()\n"));
546 if (metafile_dc)
547 {
548 GpStatus status;
549 HENHMETAFILE metafile;
550
551 metafile = CloseEnhMetaFile (metafile_dc);
552 d (g_print ("metafile = %p\n", metafile));
553
554 // must be a Bitmap for the multi-page stuff to work
555 GpImage *meta;
556
557 // jump through hoops to convert a metafile to a bitmap
558 // saveaddimage() won't work if the base image or the
559 // new image is a metafile.
560 GpImage *m;
561 status = GdipCreateMetafileFromEmf(metafile, TRUE, &m);
562 ASSERT_GDIP_SUCCESS (status, GdipCreateMetafileFromEmf);
563
564 guint width, height;
565 GdipGetImageWidth(m, &width);
566 GdipGetImageHeight(m, &height);
567
568 d (g_print ("w: %d | h: %d\n", width, height));
569
570 status = GdipCreateBitmapFromScan0 (width, height, 0, PixelFormat32bppARGB, NULL, &meta);
571 ASSERT_GDIP_SUCCESS (status, GdipCreateBitmapFromScan0);
572
573 status = GdipBitmapSetResolution (meta, metafile_xres, metafile_yres);
574 ASSERT_GDIP_SUCCESS (status, GdipBitmapSetResolution);
575
576 GpGraphics *g;
577 status = GdipGetImageGraphicsContext (meta, &g);
578 ASSERT_GDIP_SUCCESS (status, GdipGetImageGraphicsContext);
579
580 // gotta clear the bitmap
581 status = GdipGraphicsClear(g, 0xffffffff);
582 ASSERT_GDIP_SUCCESS (status, GdipGraphicsClear);
583
584 status = GdipDrawImageI(g, m, 0, 0);
585 ASSERT_GDIP_SUCCESS (status, GdipDrawImage);
586
587 status = GdipFlush(g, 1);
588 ASSERT_GDIP_SUCCESS (status, GdipFlush);
589
590 GdipDeleteGraphics(g);
591 GdipDisposeImage(m);
592
593 if (!m_multiPageTiff)
594 {
595 EncoderParameters encoder_params;
596 ULONG parameterValue = EncoderValueMultiFrame;
597
598 encoder_params.Count = 1;
599 encoder_params.Parameter[0].Guid = EncoderSaveFlag;
600 encoder_params.Parameter[0].Type = EncoderParameterValueTypeLong;
601 encoder_params.Parameter[0].NumberOfValues = 1;
602 encoder_params.Parameter[0].Value = &parameterValue;
603
604 status = GdipSaveImageToFile (meta, (WCHAR *)m_tiffFilename, &tiff_clsid, &encoder_params);
605 ASSERT_GDIP_SUCCESS (status, GdipSaveImageToFile);
606
607 d (g_print ("Saved EMF to file\n"));
608
609 m_multiPageTiff = meta;
610 }
611 else
612 {
613 EncoderParameters encoder_params;
614 ULONG parameterValue = EncoderValueFrameDimensionPage;
615
616 encoder_params.Count = 1;
617 encoder_params.Parameter[0].Guid = EncoderSaveFlag;
618 encoder_params.Parameter[0].Type = EncoderParameterValueTypeLong;
619 encoder_params.Parameter[0].NumberOfValues = 1;
620 encoder_params.Parameter[0].Value = &parameterValue;
621
622 status = GdipSaveAddImage (m_multiPageTiff, meta, &encoder_params);
623 ASSERT_GDIP_SUCCESS (status, GdipSaveAddImage);
624
625 d (g_print ("SaveAddImage\n"));
626
627 GdipDisposeImage (meta);
628 }
629
630 metafile_dc = 0;
631 DELETEP(m_pGraphics);
632 }
633 }
634
635 protected:
636
637 virtual GR_Font* _findFont(const char* pszFontFamily,
638 const char* pszFontStyle,
639 const char* pszFontVariant,
640 const char* pszFontWeight,
641 const char* pszFontStretch,
642 const char* pszFontSize,
643 const char* pszLang)
644 {
645 if (!m_pGraphics)
646 return GR_Win32Graphics::_findFont(pszFontFamily, pszFontStyle, pszFontVariant, pszFontWeight, pszFontStretch, pszFontSize, pszLang);
647 else
648 return m_pGraphics->findFont(pszFontFamily, pszFontStyle, pszFontVariant, pszFontWeight, pszFontStretch, pszFontSize, pszLang);
649 }
650
651 GR_Win32Graphics *m_pGraphics;
652 RECT page_rect;
653 HDC metafile_dc;
654 GpBitmap *m_multiPageTiff;
655 gunichar2 * m_tiffFilename;
656 int metafile_xres;
657 int metafile_yres;
658 };
659
660 XAP_Win32Dialog_PrintPreview::XAP_Win32Dialog_PrintPreview(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id)
661 : XAP_Dialog_PrintPreview(pDlgFactory,id), m_pPrintGraphics(0), m_emfFilename(0)
662 {
663 }
664
665 XAP_Win32Dialog_PrintPreview::~XAP_Win32Dialog_PrintPreview(void)
666 {
667 DELETEP(m_pPrintGraphics);
668 g_free(m_emfFilename);
669 }
670
671 XAP_Dialog * XAP_Win32Dialog_PrintPreview::static_constructor(XAP_DialogFactory * pFactory,
672 XAP_Dialog_Id id)
673 {
674 return new XAP_Win32Dialog_PrintPreview (pFactory,id);
675 }
676
677 GR_Graphics * XAP_Win32Dialog_PrintPreview::getPrinterGraphicsContext(void)
678 {
679 return m_pPrintGraphics;
680 }
681
682 void XAP_Win32Dialog_PrintPreview::releasePrinterGraphicsContext(GR_Graphics * /*pGraphics*/)
683 {
684 DELETEP(m_pPrintGraphics);
685 }
686
687 void XAP_Win32Dialog_PrintPreview::runModal(XAP_Frame * pFrame)
688 {
689 RECT rect;
690
691 FV_View * pView = static_cast<FV_View*>(pFrame->getCurrentView());
692
693 rect.left = 0;
694 rect.right = (LONG)(pView->getPageSize().Width (DIM_MM) * 100);
695 rect.top = 0;
696 rect.bottom = (LONG)(pView->getPageSize().Height (DIM_MM) * 100);
697
698 try
699 {
700 m_pPrintGraphics = new GR_Win32PrintPreviewGraphics(rect);
701 }
702 catch (...)
703 {
704 m_pPrintGraphics = 0;
705 }
706 }