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