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