]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dcprint.cpp
Use InvalidateRect for Smartphone.
[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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
42 #if wxUSE_WXDIB
43 #include "wx/msw/dib.h"
44 #endif
45
46 #include "wx/dcprint.h"
47 #include "wx/printdlg.h"
48 #include "wx/msw/printdlg.h"
49 #include "wx/math.h"
50
51 #include "wx/msw/wrapcdlg.h"
52 #ifndef __WIN32__
53 #include <print.h>
54 #endif
55
56 // mingw32 defines GDI_ERROR incorrectly
57 #if defined(__GNUWIN32__) || !defined(GDI_ERROR)
58 #undef GDI_ERROR
59 #define GDI_ERROR ((int)-1)
60 #endif
61
62 // ----------------------------------------------------------------------------
63 // wxWin macros
64 // ----------------------------------------------------------------------------
65
66 IMPLEMENT_CLASS(wxPrinterDC, wxDC)
67
68 // ============================================================================
69 // implementation
70 // ============================================================================
71
72 // ----------------------------------------------------------------------------
73 // wxPrinterDC construction
74 // ----------------------------------------------------------------------------
75
76 // This form is deprecated
77 wxPrinterDC::wxPrinterDC(const wxString& driver_name,
78 const wxString& device_name,
79 const wxString& file,
80 bool interactive,
81 int orientation)
82 {
83 m_isInteractive = interactive;
84
85 if ( !file.empty() )
86 m_printData.SetFilename(file);
87
88 #if wxUSE_COMMON_DIALOGS
89 if ( interactive )
90 {
91 PRINTDLG pd;
92
93 pd.lStructSize = sizeof( PRINTDLG );
94 pd.hwndOwner = (HWND) NULL;
95 pd.hDevMode = (HANDLE)NULL;
96 pd.hDevNames = (HANDLE)NULL;
97 pd.Flags = PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS;
98 pd.nFromPage = 0;
99 pd.nToPage = 0;
100 pd.nMinPage = 0;
101 pd.nMaxPage = 0;
102 pd.nCopies = 1;
103 pd.hInstance = (HINSTANCE)NULL;
104
105 m_ok = PrintDlg( &pd ) != 0;
106 if ( m_ok )
107 {
108 m_hDC = (WXHDC) pd.hDC;
109 }
110 }
111 else
112 #endif // wxUSE_COMMON_DIALOGS
113 {
114 if ( !driver_name.empty() && !device_name.empty() && !file.empty() )
115 {
116 m_hDC = (WXHDC) CreateDC(driver_name, device_name, file, NULL);
117 }
118 else // we don't have all parameters, ask the user
119 {
120 wxPrintData printData;
121 printData.SetOrientation(orientation);
122 m_hDC = wxGetPrinterDC(printData);
123 }
124
125 m_ok = m_hDC ? true: false;
126
127 // as we created it, we must delete it as well
128 m_bOwnsDC = true;
129 }
130
131 Init();
132 }
133
134 wxPrinterDC::wxPrinterDC(const wxPrintData& printData)
135 {
136 m_printData = printData;
137
138 m_isInteractive = false;
139
140 m_hDC = wxGetPrinterDC(printData);
141 m_ok = m_hDC != 0;
142 m_bOwnsDC = true;
143
144 Init();
145 }
146
147
148 wxPrinterDC::wxPrinterDC(WXHDC dc)
149 {
150 m_isInteractive = false;
151
152 m_hDC = dc;
153 m_bOwnsDC = true;
154 m_ok = true;
155 }
156
157 void wxPrinterDC::Init()
158 {
159 if ( m_hDC )
160 {
161 // int width = GetDeviceCaps(m_hDC, VERTRES);
162 // int height = GetDeviceCaps(m_hDC, HORZRES);
163 SetMapMode(wxMM_TEXT);
164
165 SetBrush(*wxBLACK_BRUSH);
166 SetPen(*wxBLACK_PEN);
167 }
168 }
169
170 // ----------------------------------------------------------------------------
171 // wxPrinterDC {Start/End}{Page/Doc} methods
172 // ----------------------------------------------------------------------------
173
174 bool wxPrinterDC::StartDoc(const wxString& message)
175 {
176 DOCINFO docinfo;
177 docinfo.cbSize = sizeof(DOCINFO);
178 docinfo.lpszDocName = (const wxChar*)message;
179
180 wxString filename(m_printData.GetFilename());
181
182 if (filename.empty())
183 docinfo.lpszOutput = NULL;
184 else
185 docinfo.lpszOutput = (const wxChar *) filename;
186
187 #if defined(__WIN95__)
188 docinfo.lpszDatatype = NULL;
189 docinfo.fwType = 0;
190 #endif
191
192 if (!m_hDC)
193 return false;
194
195 int ret = ::StartDoc(GetHdc(), &docinfo);
196
197 if (ret <= 0)
198 {
199 DWORD lastError = GetLastError();
200 wxLogDebug(wxT("wxDC::StartDoc failed with error: %ld\n"), lastError);
201 }
202
203 return (ret > 0);
204 }
205
206 void wxPrinterDC::EndDoc()
207 {
208 if (m_hDC) ::EndDoc((HDC) m_hDC);
209 }
210
211 void wxPrinterDC::StartPage()
212 {
213 if (m_hDC)
214 ::StartPage((HDC) m_hDC);
215 }
216
217 void wxPrinterDC::EndPage()
218 {
219 if (m_hDC)
220 ::EndPage((HDC) m_hDC);
221 }
222
223 // Returns default device and port names
224 static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName)
225 {
226 deviceName.clear();
227
228 LPDEVNAMES lpDevNames;
229 LPTSTR lpszDeviceName;
230 LPTSTR lpszPortName;
231
232 PRINTDLG pd;
233
234 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
235 #ifdef __GNUWIN32__
236 memset(&pd, 0, 66);
237 pd.lStructSize = 66; // sizeof(PRINTDLG);
238 #else
239 memset(&pd, 0, sizeof(PRINTDLG));
240 pd.lStructSize = sizeof(PRINTDLG);
241 #endif
242
243 pd.hwndOwner = (HWND)NULL;
244 pd.hDevMode = NULL; // Will be created by PrintDlg
245 pd.hDevNames = NULL; // Ditto
246 pd.Flags = PD_RETURNDEFAULT;
247 pd.nCopies = 1;
248
249 if (!PrintDlg((LPPRINTDLG)&pd))
250 {
251 if ( pd.hDevMode )
252 GlobalFree(pd.hDevMode);
253 if (pd.hDevNames)
254 GlobalFree(pd.hDevNames);
255
256 return false;
257 }
258
259 if (pd.hDevNames)
260 {
261 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
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.empty() );
279 }
280
281 // Gets an HDC for the specified printer configuration
282 WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst)
283 {
284 #if defined(__WXUNIVERSAL__) && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
285
286 #if 0
287 wxPostScriptPrintNativeData *data =
288 (wxPostScriptPrintNativeData *) printDataConst.GetNativeData();
289 // FIXME: how further ???
290 #else
291 return 0;
292 #endif
293
294 #else // Postscript vs. native Windows
295
296 wxWindowsPrintNativeData *data =
297 (wxWindowsPrintNativeData *) printDataConst.GetNativeData();
298
299 data->TransferFrom( printDataConst );
300
301 wxChar* driverName = (wxChar*) NULL;
302
303 wxString devNameStr = printDataConst.GetPrinterName();
304 wxChar* portName = (wxChar*) NULL; // Obsolete in WIN32
305
306 const wxChar* deviceName;
307 if ( !devNameStr )
308 deviceName = (wxChar*) NULL;
309 else
310 deviceName = devNameStr.c_str();
311
312 LPDEVMODE lpDevMode = (LPDEVMODE) NULL;
313
314 HGLOBAL hDevMode = (HGLOBAL)(DWORD) data->GetDevMode();
315
316 if ( hDevMode )
317 lpDevMode = (DEVMODE*) GlobalLock(hDevMode);
318
319 if ( !devNameStr )
320 {
321 // Retrieve the default device name
322 wxString portName;
323 if ( !wxGetDefaultDeviceName(devNameStr, portName) )
324 {
325 return 0; // Could not get default device name
326 }
327 deviceName = devNameStr.c_str();
328 }
329
330 #ifdef __WIN32__
331 HDC hDC = CreateDC(driverName, deviceName, portName, (DEVMODE *) lpDevMode);
332 #else
333 HDC hDC = CreateDC(driverName, deviceName, portName, (LPSTR) lpDevMode);
334 #endif
335
336 if (hDevMode && lpDevMode)
337 GlobalUnlock(hDevMode);
338
339 return (WXHDC) hDC;
340 #endif
341 }
342
343 // ----------------------------------------------------------------------------
344 // wxPrinterDC bit blitting/bitmap drawing
345 // ----------------------------------------------------------------------------
346
347 // helper of DoDrawBitmap() and DoBlit()
348 static
349 bool DrawBitmapUsingStretchDIBits(HDC hdc,
350 const wxBitmap& bmp,
351 wxCoord x, wxCoord y)
352 {
353 #if wxUSE_WXDIB
354 wxDIB dib(bmp);
355 bool ok = dib.IsOk();
356 if ( !ok )
357 return false;
358
359 DIBSECTION ds;
360 if ( !::GetObject(dib.GetHandle(), sizeof(ds), &ds) )
361 {
362 wxLogLastError(_T("GetObject(DIBSECTION)"));
363
364 return false;
365 }
366
367 // ok, we've got all data we need, do blit it
368 if ( ::StretchDIBits
369 (
370 hdc,
371 x, y,
372 ds.dsBmih.biWidth, ds.dsBmih.biHeight,
373 0, 0,
374 ds.dsBmih.biWidth, ds.dsBmih.biHeight,
375 ds.dsBm.bmBits,
376 (LPBITMAPINFO)&ds.dsBmih,
377 DIB_RGB_COLORS,
378 SRCCOPY
379 ) == GDI_ERROR )
380 {
381 wxLogLastError(wxT("StretchDIBits"));
382
383 return false;
384 }
385
386 return true;
387 #else
388 return false;
389 #endif
390 }
391
392 void wxPrinterDC::DoDrawBitmap(const wxBitmap& bmp,
393 wxCoord x, wxCoord y,
394 bool useMask)
395 {
396 wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
397
398 int width = bmp.GetWidth(),
399 height = bmp.GetHeight();
400
401 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
402 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, x, y) )
403 {
404 // no support for StretchDIBits() or an error occured if we got here
405 wxMemoryDC memDC;
406 memDC.SelectObject(bmp);
407
408 Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask);
409
410 memDC.SelectObject(wxNullBitmap);
411 }
412 }
413
414 bool wxPrinterDC::DoBlit(wxCoord xdest, wxCoord ydest,
415 wxCoord width, wxCoord height,
416 wxDC *source,
417 wxCoord WXUNUSED(xsrc), wxCoord WXUNUSED(ysrc),
418 int WXUNUSED(rop), bool useMask,
419 wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask))
420 {
421 wxBitmap& bmp = source->GetSelectedBitmap();
422 wxMask *mask = useMask ? bmp.GetMask() : NULL;
423 if ( mask )
424 {
425 // If we are printing source colours are screen colours not printer
426 // colours and so we need copy the bitmap pixel by pixel.
427 RECT rect;
428 HDC dcSrc = GetHdcOf(*source);
429 MemoryHDC dcMask(dcSrc);
430 SelectInHDC selectMask(dcMask, (HBITMAP)mask->GetMaskBitmap());
431
432 for (int x = 0; x < width; x++)
433 {
434 for (int y = 0; y < height; y++)
435 {
436 COLORREF cref = ::GetPixel(dcMask, x, y);
437 if (cref)
438 {
439 HBRUSH brush = ::CreateSolidBrush(::GetPixel(dcSrc, x, y));
440 rect.left = xdest + x;
441 rect.right = rect.left + 1;
442 rect.top = ydest + y;
443 rect.bottom = rect.top + 1;
444 ::FillRect(GetHdc(), &rect, brush);
445 ::DeleteObject(brush);
446 }
447 }
448 }
449 }
450 else // no mask
451 {
452 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
453 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, xdest, ydest) )
454 {
455 // no support for StretchDIBits
456
457 // as we are printing, source colours are screen colours not
458 // printer colours and so we need copy the bitmap pixel by pixel.
459 HDC dcSrc = GetHdcOf(*source);
460 RECT rect;
461 for (int y = 0; y < height; y++)
462 {
463 // optimization: draw identical adjacent pixels together.
464 for (int x = 0; x < width; x++)
465 {
466 COLORREF col = ::GetPixel(dcSrc, x, y);
467 HBRUSH brush = ::CreateSolidBrush( col );
468
469 rect.left = xdest + x;
470 rect.top = ydest + y;
471 while( (x + 1 < width) &&
472 (::GetPixel(dcSrc, x + 1, y) == col ) )
473 {
474 ++x;
475 }
476 rect.right = xdest + x + 1;
477 rect.bottom = rect.top + 1;
478 ::FillRect((HDC) m_hDC, &rect, brush);
479 ::DeleteObject(brush);
480 }
481 }
482 }
483 }
484
485 return true;
486 }
487
488 #endif
489 // wxUSE_PRINTING_ARCHITECTURE