]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dcprint.cpp
fixed bug in mouse handling
[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
f6bcfd97
BP
38#if wxUSE_PRINTING_ARCHITECTURE
39
42e69d6b 40#include "wx/msw/private.h"
0c589ad0
BM
41#include "wx/dcprint.h"
42#include "math.h"
2bda0e17 43
c455ab93 44#if wxUSE_COMMON_DIALOGS || defined(__WXWINE__)
4b7f2165 45 #include <commdlg.h>
2bda0e17
KB
46#endif
47
48#ifndef __WIN32__
4b7f2165 49 #include <print.h>
2bda0e17
KB
50#endif
51
3c1a88d8
VZ
52// mingw32 defines GDI_ERROR incorrectly
53#ifdef __GNUWIN32__
54 #undef GDI_ERROR
55 #define GDI_ERROR ((int)-1)
56#endif
57
4b7f2165
VZ
58// ----------------------------------------------------------------------------
59// wxWin macros
60// ----------------------------------------------------------------------------
3c1a88d8 61
2bda0e17 62IMPLEMENT_CLASS(wxPrinterDC, wxDC)
2bda0e17 63
4b7f2165
VZ
64// ============================================================================
65// implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// wxPrinterDC construction
70// ----------------------------------------------------------------------------
71
7bcb11d3 72// This form is deprecated
debe6624 73wxPrinterDC::wxPrinterDC(const wxString& driver_name, const wxString& device_name, const wxString& file, bool interactive, int orientation)
2bda0e17 74{
7bcb11d3 75 m_isInteractive = interactive;
a17e237f 76
4b7f2165 77 if ( !!file )
7bcb11d3 78 m_printData.SetFilename(file);
a17e237f 79
47d67540 80#if wxUSE_COMMON_DIALOGS
7bcb11d3
JS
81 if (interactive)
82 {
83 PRINTDLG pd;
a17e237f 84
7bcb11d3
JS
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;
a17e237f 96
7bcb11d3
JS
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 }
a17e237f 107
7bcb11d3
JS
108 // m_dontDelete = TRUE;
109 }
110 else
4b7f2165 111#endif // wxUSE_COMMON_DIALOGS
223d09f6
KB
112 if ((!driver_name.IsNull() && driver_name != wxT("")) &&
113 (!device_name.IsNull() && device_name != wxT("")) &&
114 (!file.IsNull() && file != wxT("")))
7bcb11d3 115 {
837e5743 116 m_hDC = (WXHDC) CreateDC(WXSTRINGCAST driver_name, WXSTRINGCAST device_name, WXSTRINGCAST file, NULL);
7bcb11d3
JS
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 }
a17e237f 126
7bcb11d3
JS
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
137wxPrinterDC::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);
a17e237f 145
7bcb11d3
JS
146 if (m_hDC)
147 SetMapMode(wxMM_TEXT);
a17e237f 148
7bcb11d3
JS
149 SetBrush(*wxBLACK_BRUSH);
150 SetPen(*wxBLACK_PEN);
2bda0e17
KB
151}
152
7bcb11d3 153
2bda0e17
KB
154wxPrinterDC::wxPrinterDC(WXHDC theDC)
155{
7bcb11d3 156 m_isInteractive = FALSE;
a17e237f 157
7bcb11d3
JS
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);
2bda0e17
KB
168}
169
4b7f2165 170wxPrinterDC::~wxPrinterDC()
2bda0e17
KB
171{
172}
173
4b7f2165
VZ
174// ----------------------------------------------------------------------------
175// wxPrinterDC {Start/End}{Page/Doc} methods
176// ----------------------------------------------------------------------------
177
7bcb11d3
JS
178bool wxPrinterDC::StartDoc(const wxString& message)
179{
180 DOCINFO docinfo;
181 docinfo.cbSize = sizeof(DOCINFO);
837e5743 182 docinfo.lpszDocName = (const wxChar*)message;
7bcb11d3
JS
183
184 wxString filename(m_printData.GetFilename());
185
186 if (filename.IsEmpty())
187 docinfo.lpszOutput = NULL;
188 else
837e5743 189 docinfo.lpszOutput = (const wxChar *) filename;
7bcb11d3
JS
190
191#if defined(__WIN95__)
192 docinfo.lpszDatatype = NULL;
193 docinfo.fwType = 0;
194#endif
a17e237f 195
7bcb11d3
JS
196 if (!m_hDC)
197 return FALSE;
a17e237f 198
4b7f2165 199 int ret = ::StartDoc(GetHdc(), &docinfo);
a17e237f 200
7bcb11d3
JS
201#ifndef __WIN16__
202 if (ret <= 0)
203 {
204 DWORD lastError = GetLastError();
223d09f6 205 wxLogDebug(wxT("wxDC::StartDoc failed with error: %d\n"), lastError);
7bcb11d3
JS
206 }
207#endif
a17e237f 208
7bcb11d3
JS
209 return (ret > 0);
210}
211
4b7f2165 212void wxPrinterDC::EndDoc()
7bcb11d3
JS
213{
214 if (m_hDC) ::EndDoc((HDC) m_hDC);
215}
216
4b7f2165 217void wxPrinterDC::StartPage()
7bcb11d3
JS
218{
219 if (m_hDC)
220 ::StartPage((HDC) m_hDC);
221}
222
4b7f2165 223void wxPrinterDC::EndPage()
7bcb11d3
JS
224{
225 if (m_hDC)
226 ::EndPage((HDC) m_hDC);
227}
228
229// Returns default device and port names
230static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName)
231{
232 deviceName = "";
233
234 LPDEVNAMES lpDevNames;
235 LPSTR lpszDriverName;
236 LPSTR lpszDeviceName;
237 LPSTR lpszPortName;
a17e237f 238
7bcb11d3
JS
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;
a17e237f 253
7bcb11d3
JS
254 if (!PrintDlg((LPPRINTDLG)&pd))
255 {
256 if ( pd.hDevMode )
257 GlobalFree(pd.hDevMode);
258 if (pd.hDevNames)
259 GlobalFree(pd.hDevNames);
a17e237f 260
7bcb11d3
JS
261 return FALSE;
262 }
a17e237f 263
7bcb11d3
JS
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;
7bcb11d3
JS
270
271 deviceName = lpszDeviceName;
272 portName = lpszPortName;
60fe7303
JS
273
274 GlobalUnlock(pd.hDevNames);
275 GlobalFree(pd.hDevNames);
276 pd.hDevNames=NULL;
7bcb11d3 277 }
a17e237f 278
7bcb11d3
JS
279 if (pd.hDevMode)
280 {
281 GlobalFree(pd.hDevMode);
282 pd.hDevMode=NULL;
283 }
223d09f6 284 return ( deviceName != wxT("") );
7bcb11d3
JS
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.
2bda0e17
KB
290WXHDC wxGetPrinterDC(int orientation)
291{
292 HDC hDC;
293 LPDEVMODE lpDevMode = NULL;
294 LPDEVNAMES lpDevNames;
295 LPSTR lpszDriverName;
296 LPSTR lpszDeviceName;
297 LPSTR lpszPortName;
a17e237f 298
2bda0e17
KB
299 PRINTDLG pd;
300 // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68
7bcb11d3 301#ifdef __GNUWIN32__
2bda0e17 302 pd.lStructSize = 66; // sizeof(PRINTDLG);
7bcb11d3
JS
303#else
304 pd.lStructSize = sizeof(PRINTDLG);
305#endif
2bda0e17
KB
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;
a17e237f 311
2bda0e17
KB
312 if (!PrintDlg((LPPRINTDLG)&pd))
313 {
314 if ( pd.hDevMode )
315 GlobalFree(pd.hDevMode);
316 if (pd.hDevNames)
317 GlobalFree(pd.hDevNames);
a17e237f 318
2bda0e17
KB
319 return(0);
320 }
a17e237f 321
2bda0e17
KB
322 if (!pd.hDevNames)
323 {
324 if ( pd.hDevMode )
325 GlobalFree(pd.hDevMode);
326 }
a17e237f 327
2bda0e17
KB
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);
a17e237f 333
2bda0e17
KB
334 if ( pd.hDevMode )
335 {
336 lpDevMode = (DEVMODE*) GlobalLock(pd.hDevMode);
337 lpDevMode->dmOrientation = orientation;
338 lpDevMode->dmFields |= DM_ORIENTATION;
339 }
a17e237f 340
2bda0e17
KB
341#ifdef __WIN32__
342 hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (DEVMODE *)lpDevMode);
343#else
344 hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (LPSTR)lpDevMode);
345#endif
a17e237f 346
2bda0e17
KB
347 if (pd.hDevMode && lpDevMode)
348 GlobalUnlock(pd.hDevMode);
a17e237f 349
2bda0e17
KB
350 if (pd.hDevNames)
351 {
7bcb11d3
JS
352 GlobalFree(pd.hDevNames);
353 pd.hDevNames=NULL;
2bda0e17
KB
354 }
355 if (pd.hDevMode)
356 {
357 GlobalFree(pd.hDevMode);
358 pd.hDevMode=NULL;
359 }
360 return (WXHDC) hDC;
361}
7bcb11d3
JS
362#endif
363
364// Gets an HDC for the specified printer configuration
365WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst)
366{
367 wxPrintData printData = printDataConst;
368 printData.ConvertToNative();
a17e237f 369
837e5743 370 wxChar* driverName = (wxChar*) NULL;
a17e237f 371
7bcb11d3 372 wxString devNameStr = printData.GetPrinterName();
837e5743 373 wxChar* portName = (wxChar*) NULL; // Obsolete in WIN32
a17e237f 374
4b7f2165
VZ
375 const wxChar* deviceName;
376 if ( !devNameStr )
837e5743 377 deviceName = (wxChar*) NULL;
7bcb11d3 378 else
4b7f2165 379 deviceName = devNameStr.c_str();
7bcb11d3
JS
380
381 LPDEVMODE lpDevMode = (LPDEVMODE) NULL;
382
48c12cb1 383 HGLOBAL hDevMode = (HGLOBAL)(DWORD) printData.GetNativeData();
7bcb11d3
JS
384
385 if ( hDevMode )
386 lpDevMode = (DEVMODE*) GlobalLock(hDevMode);
387
4b7f2165 388 if ( !devNameStr )
7bcb11d3
JS
389 {
390 // Retrieve the default device name
391 wxString portName;
a17e237f
VZ
392#ifdef __WXDEBUG__
393 bool ret =
394#else // !Debug
395 (void)
396#endif // Debug/Release
397 wxGetDefaultDeviceName(devNameStr, portName);
7bcb11d3 398
223d09f6 399 wxASSERT_MSG( ret, wxT("Could not get default device name.") );
2bda0e17 400
4b7f2165 401 deviceName = devNameStr.c_str();
7bcb11d3 402 }
a17e237f 403
7bcb11d3
JS
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
a17e237f 409
7bcb11d3
JS
410 if (hDevMode && lpDevMode)
411 GlobalUnlock(hDevMode);
a17e237f 412
7bcb11d3
JS
413 return (WXHDC) hDC;
414}
2bda0e17 415
4b7f2165
VZ
416// ----------------------------------------------------------------------------
417// wxPrinterDC bit blitting/bitmap drawing
418// ----------------------------------------------------------------------------
419
f6081a04
JS
420// Win16 doesn't define GDI_ERROR.
421#ifndef GDI_ERROR
422#define GDI_ERROR -1
423#endif
424
4b7f2165
VZ
425void 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 {
f6bcfd97 461 wxLogLastError(wxT("StretchDIBits"));
4b7f2165
VZ
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
479bool 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 {
f6bcfd97 543 wxLogLastError(wxT("GetDIBits"));
4b7f2165
VZ
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 {
f6bcfd97 559 wxLogLastError(wxT("StretchDIBits"));
4b7f2165
VZ
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}
f6bcfd97
BP
598
599#endif
600 // wxUSE_PRINTING_ARCHITECTURE