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