+ // Fill out the DEVMODE structure
+ // so we can use it as input in the 'real' PrintDlg
+ if (!PrintDlg(&pd))
+ {
+ if ( pd.hDevMode )
+ GlobalFree(pd.hDevMode);
+ if ( pd.hDevNames )
+ GlobalFree(pd.hDevNames);
+ pd.hDevMode = NULL;
+ pd.hDevNames = NULL;
+
+#if wxDEBUG_LEVEL
+ wxLogDebug(wxT("Printing error: ") + wxGetPrintDlgError());
+#endif // wxDEBUG_LEVEL
+ }
+ else
+ {
+ hDevMode = pd.hDevMode;
+ m_devMode = hDevMode;
+ pd.hDevMode = NULL;
+
+ // We'll create a new DEVNAMEs structure below.
+ if ( pd.hDevNames )
+ GlobalFree(pd.hDevNames);
+ pd.hDevNames = NULL;
+
+ // hDevNames = pd->hDevNames;
+ // m_devNames = (void*)(long) hDevNames;
+ // pd->hDevnames = NULL;
+
+ }
+ }
+
+ if ( hDevMode )
+ {
+ GlobalPtrLock lockDevMode(hDevMode);
+ DEVMODE * const devMode = static_cast<DEVMODE *>(lockDevMode.Get());
+
+ //// Orientation
+ devMode->dmOrientation = (short)data.GetOrientation();
+
+ //// Collation
+ devMode->dmCollate = (data.GetCollate() ? DMCOLLATE_TRUE : DMCOLLATE_FALSE);
+ devMode->dmFields |= DM_COLLATE;
+
+ //// Number of copies
+ devMode->dmCopies = (short)data.GetNoCopies();
+ devMode->dmFields |= DM_COPIES;
+
+ //// Printer name
+ wxString name = data.GetPrinterName();
+ if (!name.empty())
+ {
+ // NB: the cast is needed in the ANSI build, strangely enough
+ // dmDeviceName is BYTE[] and not char[] there
+ wxStrlcpy(reinterpret_cast<wxChar *>(devMode->dmDeviceName),
+ name.wx_str(),
+ WXSIZEOF(devMode->dmDeviceName));
+ }
+
+ //// Colour
+ if (data.GetColour())
+ devMode->dmColor = DMCOLOR_COLOR;
+ else
+ devMode->dmColor = DMCOLOR_MONOCHROME;
+ devMode->dmFields |= DM_COLOR;
+
+ //// Paper size
+
+ // Paper id has priority over paper size. If id is specified, then size
+ // is ignored (as it can be filled in even for standard paper sizes)
+
+ wxPrintPaperType *paperType = NULL;
+
+ const wxPaperSize paperId = data.GetPaperId();
+ if ( paperId != wxPAPER_NONE && wxThePrintPaperDatabase )
+ {
+ paperType = wxThePrintPaperDatabase->FindPaperType(paperId);
+ }
+
+ if ( paperType )
+ {
+ devMode->dmPaperSize = (short)paperType->GetPlatformId();
+ devMode->dmFields |= DM_PAPERSIZE;
+ }
+ else // custom (or no) paper size
+ {
+ const wxSize paperSize = data.GetPaperSize();
+ if ( paperSize != wxDefaultSize )
+ {
+ // Fall back on specifying the paper size explicitly
+ if(m_customWindowsPaperId != 0)
+ devMode->dmPaperSize = m_customWindowsPaperId;
+ else
+ devMode->dmPaperSize = DMPAPER_USER;
+ devMode->dmPaperWidth = (short)(paperSize.x * 10);
+ devMode->dmPaperLength = (short)(paperSize.y * 10);
+ devMode->dmFields |= DM_PAPERWIDTH;
+ devMode->dmFields |= DM_PAPERLENGTH;
+
+ // A printer driver may or may not also want DM_PAPERSIZE to
+ // be specified. Also, if the printer driver doesn't implement the DMPAPER_USER
+ // size, then this won't work, and even if you found the correct id by
+ // enumerating the driver's paper sizes, it probably won't change the actual size,
+ // it'll just select that custom paper type with its own current setting.
+ // For a discussion on this, see http://www.codeguru.com/forum/showthread.php?threadid=458617
+ // Although m_customWindowsPaperId is intended to work around this, it's
+ // unclear how it can help you set the custom paper size programmatically.
+ }
+ //else: neither paper type nor size specified, don't fill DEVMODE
+ // at all so that the system defaults are used
+ }
+
+ //// Duplex
+ short duplex;
+ switch (data.GetDuplex())
+ {
+ case wxDUPLEX_HORIZONTAL:
+ duplex = DMDUP_HORIZONTAL;
+ break;
+ case wxDUPLEX_VERTICAL:
+ duplex = DMDUP_VERTICAL;
+ break;
+ default:
+ // in fact case wxDUPLEX_SIMPLEX:
+ duplex = DMDUP_SIMPLEX;
+ break;
+ }
+ devMode->dmDuplex = duplex;
+ devMode->dmFields |= DM_DUPLEX;
+
+ //// Quality
+
+ short quality;
+ switch (data.GetQuality())
+ {
+ case wxPRINT_QUALITY_MEDIUM:
+ quality = DMRES_MEDIUM;
+ break;
+ case wxPRINT_QUALITY_LOW:
+ quality = DMRES_LOW;
+ break;
+ case wxPRINT_QUALITY_DRAFT:
+ quality = DMRES_DRAFT;
+ break;
+ case wxPRINT_QUALITY_HIGH:
+ quality = DMRES_HIGH;
+ break;
+ default:
+ quality = (short)data.GetQuality();
+ devMode->dmYResolution = quality;
+ devMode->dmFields |= DM_YRESOLUTION;
+ break;
+ }
+ devMode->dmPrintQuality = quality;
+ devMode->dmFields |= DM_PRINTQUALITY;
+
+ if (data.GetPrivDataLen() > 0)
+ {
+ memcpy( (char *)devMode+devMode->dmSize, data.GetPrivData(), data.GetPrivDataLen() );
+ devMode->dmDriverExtra = (WXWORD)data.GetPrivDataLen();
+ }
+
+ if (data.GetBin() != wxPRINTBIN_DEFAULT)
+ {
+ switch (data.GetBin())
+ {
+ case wxPRINTBIN_ONLYONE: devMode->dmDefaultSource = DMBIN_ONLYONE; break;
+ case wxPRINTBIN_LOWER: devMode->dmDefaultSource = DMBIN_LOWER; break;
+ case wxPRINTBIN_MIDDLE: devMode->dmDefaultSource = DMBIN_MIDDLE; break;
+ case wxPRINTBIN_MANUAL: devMode->dmDefaultSource = DMBIN_MANUAL; break;
+ case wxPRINTBIN_ENVELOPE: devMode->dmDefaultSource = DMBIN_ENVELOPE; break;
+ case wxPRINTBIN_ENVMANUAL: devMode->dmDefaultSource = DMBIN_ENVMANUAL; break;
+ case wxPRINTBIN_AUTO: devMode->dmDefaultSource = DMBIN_AUTO; break;
+ case wxPRINTBIN_TRACTOR: devMode->dmDefaultSource = DMBIN_TRACTOR; break;
+ case wxPRINTBIN_SMALLFMT: devMode->dmDefaultSource = DMBIN_SMALLFMT; break;
+ case wxPRINTBIN_LARGEFMT: devMode->dmDefaultSource = DMBIN_LARGEFMT; break;
+ case wxPRINTBIN_LARGECAPACITY: devMode->dmDefaultSource = DMBIN_LARGECAPACITY; break;
+ case wxPRINTBIN_CASSETTE: devMode->dmDefaultSource = DMBIN_CASSETTE; break;
+ case wxPRINTBIN_FORMSOURCE: devMode->dmDefaultSource = DMBIN_FORMSOURCE; break;
+
+ default:
+ devMode->dmDefaultSource = (short)(DMBIN_USER + data.GetBin() - wxPRINTBIN_USER); // 256 + data.GetBin() - 14 = 242 + data.GetBin()
+ break;
+ }
+
+ devMode->dmFields |= DM_DEFAULTSOURCE;
+ }
+ if (data.GetMedia() != wxPRINTMEDIA_DEFAULT)
+ {
+ devMode->dmMediaType = data.GetMedia();
+ devMode->dmFields |= DM_MEDIATYPE;
+ }
+
+ if( printer )
+ {
+ // Step 3:
+ // Merge the new settings with the old.
+ // This gives the driver an opportunity to update any private
+ // portions of the DevMode structure.
+ DocumentProperties( NULL,
+ printer,
+ szPrinterName,
+ (LPDEVMODE)hDevMode, // Reuse our buffer for output.
+ (LPDEVMODE)hDevMode, // Pass the driver our changes
+ DM_IN_BUFFER | // Commands to Merge our changes and
+ DM_OUT_BUFFER ); // write the result.
+ }
+ }
+
+ if ( m_devNames )
+ {
+ ::GlobalFree(static_cast<HGLOBAL>(m_devNames));
+ }
+
+ // TODO: I hope it's OK to pass some empty strings to DEVNAMES.
+ m_devNames = wxCreateDevNames(wxEmptyString, data.GetPrinterName(), wxEmptyString);
+
+ return true;
+}