]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dcprint.cpp
Fixed crash-on-exit bug due to status bar being deleted twice (MDI apps);
[wxWidgets.git] / src / msw / dcprint.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
4b7f2165 2// Name: src/msw/dcprint.cpp
2bda0e17
KB
3// Purpose: wxPrinterDC class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
4b7f2165 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
4b7f2165
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
4b7f2165 21 #pragma implementation "dcprint.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
4b7f2165 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
4b7f2165
VZ
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/window.h"
475f6e7a 35 #include "wx/dcmemory.h"
2bda0e17
KB
36#endif
37
42e69d6b 38#include "wx/msw/private.h"
0c589ad0
BM
39#include "wx/dcprint.h"
40#include "math.h"
2bda0e17 41
c455ab93 42#if wxUSE_COMMON_DIALOGS || defined(__WXWINE__)
4b7f2165 43 #include <commdlg.h>
2bda0e17
KB
44#endif
45
46#ifndef __WIN32__
4b7f2165 47 #include <print.h>
2bda0e17
KB
48#endif
49
3c1a88d8
VZ
50// mingw32 defines GDI_ERROR incorrectly
51#ifdef __GNUWIN32__
52 #undef GDI_ERROR
53 #define GDI_ERROR ((int)-1)
54#endif
55
4b7f2165
VZ
56// ----------------------------------------------------------------------------
57// wxWin macros
58// ----------------------------------------------------------------------------
3c1a88d8 59
2bda0e17 60IMPLEMENT_CLASS(wxPrinterDC, wxDC)
2bda0e17 61
4b7f2165
VZ
62// ============================================================================
63// implementation
64// ============================================================================
65
66// ----------------------------------------------------------------------------
67// wxPrinterDC construction
68// ----------------------------------------------------------------------------
69
7bcb11d3 70// This form is deprecated
debe6624 71wxPrinterDC::wxPrinterDC(const wxString& driver_name, const wxString& device_name, const wxString& file, bool interactive, int orientation)
2bda0e17 72{
7bcb11d3 73 m_isInteractive = interactive;
a17e237f 74
4b7f2165 75 if ( !!file )
7bcb11d3 76 m_printData.SetFilename(file);
a17e237f 77
47d67540 78#if wxUSE_COMMON_DIALOGS
7bcb11d3
JS
79 if (interactive)
80 {
81 PRINTDLG pd;
a17e237f 82
7bcb11d3
JS
83 pd.lStructSize = sizeof( PRINTDLG );
84 pd.hwndOwner=(HWND) NULL;
85 pd.hDevMode=(HANDLE)NULL;
86 pd.hDevNames=(HANDLE)NULL;
87 pd.Flags=PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS;
88 pd.nFromPage=0;
89 pd.nToPage=0;
90 pd.nMinPage=0;
91 pd.nMaxPage=0;
92 pd.nCopies=1;
93 pd.hInstance=(HINSTANCE)NULL;
a17e237f 94
7bcb11d3
JS
95 if ( PrintDlg( &pd ) != 0 )
96 {
97 m_hDC = (WXHDC) pd.hDC;
98 m_ok = TRUE;
99 }
100 else
101 {
102 m_ok = FALSE;
103 return;
104 }
a17e237f 105
7bcb11d3
JS
106 // m_dontDelete = TRUE;
107 }
108 else
4b7f2165 109#endif // wxUSE_COMMON_DIALOGS
223d09f6
KB
110 if ((!driver_name.IsNull() && driver_name != wxT("")) &&
111 (!device_name.IsNull() && device_name != wxT("")) &&
112 (!file.IsNull() && file != wxT("")))
7bcb11d3 113 {
837e5743 114 m_hDC = (WXHDC) CreateDC(WXSTRINGCAST driver_name, WXSTRINGCAST device_name, WXSTRINGCAST file, NULL);
7bcb11d3
JS
115 m_ok = m_hDC ? TRUE: FALSE;
116 }
117 else
118 {
119 wxPrintData printData;
120 printData.SetOrientation(orientation);
121 m_hDC = wxGetPrinterDC(printData);
122 m_ok = m_hDC ? TRUE: FALSE;
123 }
a17e237f 124
7bcb11d3
JS
125 if (m_hDC)
126 {
127 // int width = GetDeviceCaps(m_hDC, VERTRES);
128 // int height = GetDeviceCaps(m_hDC, HORZRES);
129 SetMapMode(wxMM_TEXT);
130 }
131 SetBrush(*wxBLACK_BRUSH);
132 SetPen(*wxBLACK_PEN);
133}
134
135wxPrinterDC::wxPrinterDC(const wxPrintData& printData)
136{
137 m_printData = printData;
138
139 m_isInteractive = FALSE;
140
141 m_hDC = wxGetPrinterDC(printData);
142 m_ok = (m_hDC != 0);
a17e237f 143
7bcb11d3
JS
144 if (m_hDC)
145 SetMapMode(wxMM_TEXT);
a17e237f 146
7bcb11d3
JS
147 SetBrush(*wxBLACK_BRUSH);
148 SetPen(*wxBLACK_PEN);
2bda0e17
KB
149}
150
7bcb11d3 151
2bda0e17
KB
152wxPrinterDC::wxPrinterDC(WXHDC theDC)
153{
7bcb11d3 154 m_isInteractive = FALSE;
a17e237f 155
7bcb11d3
JS
156 m_hDC = theDC;
157 m_ok = TRUE;
158 if (m_hDC)
159 {
160 // int width = GetDeviceCaps(m_hDC, VERTRES);
161 // int height = GetDeviceCaps(m_hDC, HORZRES);
162 SetMapMode(wxMM_TEXT);
163 }
164 SetBrush(*wxBLACK_BRUSH);
165 SetPen(*wxBLACK_PEN);
2bda0e17
KB
166}
167
4b7f2165 168wxPrinterDC::~wxPrinterDC()
2bda0e17
KB
169{
170}
171
4b7f2165
VZ
172// ----------------------------------------------------------------------------
173// wxPrinterDC {Start/End}{Page/Doc} methods
174// ----------------------------------------------------------------------------
175
7bcb11d3
JS
176bool wxPrinterDC::StartDoc(const wxString& message)
177{
178 DOCINFO docinfo;
179 docinfo.cbSize = sizeof(DOCINFO);
837e5743 180 docinfo.lpszDocName = (const wxChar*)message;
7bcb11d3
JS
181
182 wxString filename(m_printData.GetFilename());
183
184 if (filename.IsEmpty())
185 docinfo.lpszOutput = NULL;
186 else
837e5743 187 docinfo.lpszOutput = (const wxChar *) filename;
7bcb11d3
JS
188
189#if defined(__WIN95__)
190 docinfo.lpszDatatype = NULL;
191 docinfo.fwType = 0;
192#endif
a17e237f 193
7bcb11d3
JS
194 if (!m_hDC)
195 return FALSE;
a17e237f 196
4b7f2165 197 int ret = ::StartDoc(GetHdc(), &docinfo);
a17e237f 198
7bcb11d3
JS
199#ifndef __WIN16__
200 if (ret <= 0)
201 {
202 DWORD lastError = GetLastError();
223d09f6 203 wxLogDebug(wxT("wxDC::StartDoc failed with error: %d\n"), lastError);
7bcb11d3
JS
204 }
205#endif
a17e237f 206
7bcb11d3
JS
207 return (ret > 0);
208}
209
4b7f2165 210void wxPrinterDC::EndDoc()
7bcb11d3
JS
211{
212 if (m_hDC) ::EndDoc((HDC) m_hDC);
213}
214
4b7f2165 215void wxPrinterDC::StartPage()
7bcb11d3
JS
216{
217 if (m_hDC)
218 ::StartPage((HDC) m_hDC);
219}
220
4b7f2165 221void wxPrinterDC::EndPage()
7bcb11d3
JS
222{
223 if (m_hDC)
224 ::EndPage((HDC) m_hDC);
225}
226
227// Returns default device and port names
228static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName)
229{
230 deviceName = "";
231
232 LPDEVNAMES lpDevNames;
233 LPSTR lpszDriverName;
234 LPSTR lpszDeviceName;
235 LPSTR lpszPortName;
a17e237f 236
7bcb11d3
JS
237 PRINTDLG pd;
238
239 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
240#ifdef __GNUWIN32__
241 pd.lStructSize = 66; // sizeof(PRINTDLG);
242#else
243 pd.lStructSize = sizeof(PRINTDLG);
244#endif
245
246 pd.hwndOwner = (HWND)NULL;
247 pd.hDevMode = NULL; // Will be created by PrintDlg
248 pd.hDevNames = NULL; // Ditto
249 pd.Flags = PD_RETURNDEFAULT;
250 pd.nCopies = 1;
a17e237f 251
7bcb11d3
JS
252 if (!PrintDlg((LPPRINTDLG)&pd))
253 {
254 if ( pd.hDevMode )
255 GlobalFree(pd.hDevMode);
256 if (pd.hDevNames)
257 GlobalFree(pd.hDevNames);
a17e237f 258
7bcb11d3
JS
259 return FALSE;
260 }
a17e237f 261
7bcb11d3
JS
262 if (pd.hDevNames)
263 {
264 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
265 lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset;
266 lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset;
267 lpszPortName = (LPSTR)lpDevNames + lpDevNames->wOutputOffset;
7bcb11d3
JS
268
269 deviceName = lpszDeviceName;
270 portName = lpszPortName;
60fe7303
JS
271
272 GlobalUnlock(pd.hDevNames);
273 GlobalFree(pd.hDevNames);
274 pd.hDevNames=NULL;
7bcb11d3 275 }
a17e237f 276
7bcb11d3
JS
277 if (pd.hDevMode)
278 {
279 GlobalFree(pd.hDevMode);
280 pd.hDevMode=NULL;
281 }
223d09f6 282 return ( deviceName != wxT("") );
7bcb11d3
JS
283}
284
285#if 0
286// This uses defaults, except for orientation, so we should eliminate this function
287// and use the 2nd form (passing wxPrintData) instead.
2bda0e17
KB
288WXHDC wxGetPrinterDC(int orientation)
289{
290 HDC hDC;
291 LPDEVMODE lpDevMode = NULL;
292 LPDEVNAMES lpDevNames;
293 LPSTR lpszDriverName;
294 LPSTR lpszDeviceName;
295 LPSTR lpszPortName;
a17e237f 296
2bda0e17
KB
297 PRINTDLG pd;
298 // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68
7bcb11d3 299#ifdef __GNUWIN32__
2bda0e17 300 pd.lStructSize = 66; // sizeof(PRINTDLG);
7bcb11d3
JS
301#else
302 pd.lStructSize = sizeof(PRINTDLG);
303#endif
2bda0e17
KB
304 pd.hwndOwner = (HWND)NULL;
305 pd.hDevMode = NULL; // Will be created by PrintDlg
306 pd.hDevNames = NULL; // Ditto
307 pd.Flags = PD_RETURNDEFAULT;
308 pd.nCopies = 1;
a17e237f 309
2bda0e17
KB
310 if (!PrintDlg((LPPRINTDLG)&pd))
311 {
312 if ( pd.hDevMode )
313 GlobalFree(pd.hDevMode);
314 if (pd.hDevNames)
315 GlobalFree(pd.hDevNames);
a17e237f 316
2bda0e17
KB
317 return(0);
318 }
a17e237f 319
2bda0e17
KB
320 if (!pd.hDevNames)
321 {
322 if ( pd.hDevMode )
323 GlobalFree(pd.hDevMode);
324 }
a17e237f 325
2bda0e17
KB
326 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
327 lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset;
328 lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset;
329 lpszPortName = (LPSTR)lpDevNames + lpDevNames->wOutputOffset;
330 GlobalUnlock(pd.hDevNames);
a17e237f 331
2bda0e17
KB
332 if ( pd.hDevMode )
333 {
334 lpDevMode = (DEVMODE*) GlobalLock(pd.hDevMode);
335 lpDevMode->dmOrientation = orientation;
336 lpDevMode->dmFields |= DM_ORIENTATION;
337 }
a17e237f 338
2bda0e17
KB
339#ifdef __WIN32__
340 hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (DEVMODE *)lpDevMode);
341#else
342 hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (LPSTR)lpDevMode);
343#endif
a17e237f 344
2bda0e17
KB
345 if (pd.hDevMode && lpDevMode)
346 GlobalUnlock(pd.hDevMode);
a17e237f 347
2bda0e17
KB
348 if (pd.hDevNames)
349 {
7bcb11d3
JS
350 GlobalFree(pd.hDevNames);
351 pd.hDevNames=NULL;
2bda0e17
KB
352 }
353 if (pd.hDevMode)
354 {
355 GlobalFree(pd.hDevMode);
356 pd.hDevMode=NULL;
357 }
358 return (WXHDC) hDC;
359}
7bcb11d3
JS
360#endif
361
362// Gets an HDC for the specified printer configuration
363WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst)
364{
365 wxPrintData printData = printDataConst;
366 printData.ConvertToNative();
a17e237f 367
837e5743 368 wxChar* driverName = (wxChar*) NULL;
a17e237f 369
7bcb11d3 370 wxString devNameStr = printData.GetPrinterName();
837e5743 371 wxChar* portName = (wxChar*) NULL; // Obsolete in WIN32
a17e237f 372
4b7f2165
VZ
373 const wxChar* deviceName;
374 if ( !devNameStr )
837e5743 375 deviceName = (wxChar*) NULL;
7bcb11d3 376 else
4b7f2165 377 deviceName = devNameStr.c_str();
7bcb11d3
JS
378
379 LPDEVMODE lpDevMode = (LPDEVMODE) NULL;
380
48c12cb1 381 HGLOBAL hDevMode = (HGLOBAL)(DWORD) printData.GetNativeData();
7bcb11d3
JS
382
383 if ( hDevMode )
384 lpDevMode = (DEVMODE*) GlobalLock(hDevMode);
385
4b7f2165 386 if ( !devNameStr )
7bcb11d3
JS
387 {
388 // Retrieve the default device name
389 wxString portName;
a17e237f
VZ
390#ifdef __WXDEBUG__
391 bool ret =
392#else // !Debug
393 (void)
394#endif // Debug/Release
395 wxGetDefaultDeviceName(devNameStr, portName);
7bcb11d3 396
223d09f6 397 wxASSERT_MSG( ret, wxT("Could not get default device name.") );
2bda0e17 398
4b7f2165 399 deviceName = devNameStr.c_str();
7bcb11d3 400 }
a17e237f 401
7bcb11d3
JS
402#ifdef __WIN32__
403 HDC hDC = CreateDC(driverName, deviceName, portName, (DEVMODE *) lpDevMode);
404#else
405 HDC hDC = CreateDC(driverName, deviceName, portName, (LPSTR) lpDevMode);
406#endif
a17e237f 407
7bcb11d3
JS
408 if (hDevMode && lpDevMode)
409 GlobalUnlock(hDevMode);
a17e237f 410
7bcb11d3
JS
411 return (WXHDC) hDC;
412}
2bda0e17 413
4b7f2165
VZ
414// ----------------------------------------------------------------------------
415// wxPrinterDC bit blitting/bitmap drawing
416// ----------------------------------------------------------------------------
417
f6081a04
JS
418// Win16 doesn't define GDI_ERROR.
419#ifndef GDI_ERROR
420#define GDI_ERROR -1
421#endif
422
4b7f2165
VZ
423void wxPrinterDC::DoDrawBitmap(const wxBitmap &bmp,
424 wxCoord x, wxCoord y,
425 bool useMask)
426{
427 wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
428
429 int width = bmp.GetWidth(),
430 height = bmp.GetHeight();
431
432 if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB )
433 {
434 BITMAPINFO *info = (BITMAPINFO *) malloc( sizeof( BITMAPINFOHEADER ) + 256 * sizeof(RGBQUAD ) );
435 memset( info, 0, sizeof( BITMAPINFOHEADER ) );
436
437 int iBitsSize = ((width + 3 ) & ~3 ) * height;
438
439 void* bits = malloc( iBitsSize );
440
441 info->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
442 info->bmiHeader.biWidth = width;
443 info->bmiHeader.biHeight = height;
444 info->bmiHeader.biPlanes = 1;
445 info->bmiHeader.biBitCount = 8;
446 info->bmiHeader.biCompression = BI_RGB;
447
448 ScreenHDC display;
449 if ( GetDIBits(display, GetHbitmapOf(bmp), 0,
450 bmp.GetHeight(), bits, info,
451 DIB_RGB_COLORS) )
452 {
453 if ( ::StretchDIBits(GetHdc(), x, y,
454 width, height,
455 0 , 0, width, height,
456 bits, info,
457 DIB_RGB_COLORS, SRCCOPY) == GDI_ERROR )
458 {
459 wxLogLastError("StretchDIBits");
460 }
461 }
462
463 free(bits);
464 free(info);
465 }
466 else // no support for StretchDIBits()
467 {
468 wxMemoryDC memDC;
469 memDC.SelectObject(bmp);
470
471 Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask);
472
473 memDC.SelectObject(wxNullBitmap);
474 }
475}
476
477bool wxPrinterDC::DoBlit(wxCoord xdest, wxCoord ydest,
478 wxCoord width, wxCoord height,
479 wxDC *source,
480 wxCoord xsrc, wxCoord ysrc,
481 int rop, bool useMask)
482{
483 bool success = TRUE;
484
485 if ( useMask )
486 {
487 // If we are printing source colours are screen colours
488 // not printer colours and so we need copy the bitmap
489 // pixel by pixel.
490 RECT rect;
491 HDC dc_src = GetHdcOf(*source);
492 HDC dc_mask = ::CreateCompatibleDC(dc_src);
493
494 ::SelectObject(dc_mask, (HBITMAP) source->GetSelectedBitmap().GetMask()->GetMaskBitmap());
495 for (int x = 0; x < width; x++)
496 {
497 for (int y = 0; y < height; y++)
498 {
499 COLORREF cref = ::GetPixel(dc_mask, x, y);
500 if (cref)
501 {
502 HBRUSH brush = ::CreateSolidBrush(::GetPixel(dc_src, x, y));
503 rect.left = xdest + x;
504 rect.right = rect.left + 1;
505 rect.top = ydest + y;
506 rect.bottom = rect.top + 1;
507 ::FillRect(GetHdc(), &rect, brush);
508 ::DeleteObject(brush);
509 }
510 }
511 }
512 ::SelectObject(dc_mask, 0);
513 ::DeleteDC(dc_mask);
514 }
515 else // no mask
516 {
517 if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB )
518 {
519 wxBitmap& bmp = source->GetSelectedBitmap();
520 int width = bmp.GetWidth(),
521 height = bmp.GetHeight();
522
523 BITMAPINFO *info = (BITMAPINFO *) malloc( sizeof( BITMAPINFOHEADER ) + 256 * sizeof(RGBQUAD ) );
524 int iBitsSize = ((width + 3 ) & ~3 ) * height;
525
526 void* bits = malloc( iBitsSize );
527
528 memset( info , 0 , sizeof( BITMAPINFOHEADER ) );
529
530 info->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
531 info->bmiHeader.biWidth = width;
532 info->bmiHeader.biHeight = height;
533 info->bmiHeader.biPlanes = 1;
534 info->bmiHeader.biBitCount = 8;
535 info->bmiHeader.biCompression = BI_RGB;
536
537 ScreenHDC display;
538 if ( !::GetDIBits(display, GetHbitmapOf(bmp), 0,
539 height, bits, info, DIB_RGB_COLORS) )
540 {
541 wxLogLastError("GetDIBits");
542
543 success = FALSE;
544 }
545
546 if ( success )
547 {
548 success = ::StretchDIBits(GetHdc(), xdest, ydest,
549 width, height,
550 xsrc, ysrc,
551 width, height,
552 bits, info ,
553 DIB_RGB_COLORS,
554 SRCCOPY) != GDI_ERROR;
555 if ( !success )
556 {
557 wxLogLastError("StretchDIBits");
558 }
559 }
560
561 free(bits);
562 free(info);
563 }
564 else // no support for StretchDIBits
565 {
566 // as we are printing, source colours are screen colours not printer
567 // colours and so we need copy the bitmap pixel by pixel.
568 HDC dc_src = GetHdcOf(*source);
569 RECT rect;
570 for (int y = 0; y < height; y++)
571 {
572 // This is Stefan Csomor's optimisation, where identical adjacent
573 // pixels are drawn together.
574 for (int x = 0; x < width; x++)
575 {
576 COLORREF col = ::GetPixel(dc_src, x, y);
577 HBRUSH brush = ::CreateSolidBrush( col );
578
579 rect.left = xdest + x;
580 rect.top = ydest + y;
581 while( (x + 1 < width) && (::GetPixel(dc_src, x + 1, y) == col ) )
582 {
583 ++x;
584 }
585 rect.right = xdest + x + 1;
586 rect.bottom = rect.top + 1;
587 ::FillRect((HDC) m_hDC, &rect, brush);
588 ::DeleteObject(brush);
589 }
590 }
591 }
592 }
593
594 return success;
595}