1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dcprint.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/string.h"
28 #include "wx/window.h"
29 #include "wx/msw/private.h"
30 #include "wx/dcprint.h"
33 #if wxUSE_COMMON_DIALOGS || defined(__WXWINE__)
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_CLASS(wxPrinterDC, wxDC)
45 // This form is deprecated
46 wxPrinterDC::wxPrinterDC(const wxString& driver_name, const wxString& device_name, const wxString& file, bool interactive, int orientation)
48 m_isInteractive = interactive;
50 if (!file.IsNull() && file != _T(""))
51 m_printData.SetFilename(file);
53 #if wxUSE_COMMON_DIALOGS
58 pd.lStructSize = sizeof( PRINTDLG );
59 pd.hwndOwner=(HWND) NULL;
60 pd.hDevMode=(HANDLE)NULL;
61 pd.hDevNames=(HANDLE)NULL;
62 pd.Flags=PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS;
68 pd.hInstance=(HINSTANCE)NULL;
70 if ( PrintDlg( &pd ) != 0 )
72 m_hDC = (WXHDC) pd.hDC;
81 // m_dontDelete = TRUE;
85 if ((!driver_name.IsNull() && driver_name != _T("")) &&
86 (!device_name.IsNull() && device_name != _T("")) &&
87 (!file.IsNull() && file != _T("")))
89 m_hDC = (WXHDC) CreateDC(WXSTRINGCAST driver_name, WXSTRINGCAST device_name, WXSTRINGCAST file, NULL);
90 m_ok = m_hDC ? TRUE: FALSE;
94 wxPrintData printData;
95 printData.SetOrientation(orientation);
96 m_hDC = wxGetPrinterDC(printData);
97 m_ok = m_hDC ? TRUE: FALSE;
102 // int width = GetDeviceCaps(m_hDC, VERTRES);
103 // int height = GetDeviceCaps(m_hDC, HORZRES);
104 SetMapMode(wxMM_TEXT);
106 SetBrush(*wxBLACK_BRUSH);
107 SetPen(*wxBLACK_PEN);
110 wxPrinterDC::wxPrinterDC(const wxPrintData& printData)
112 m_printData = printData;
114 m_isInteractive = FALSE;
116 m_hDC = wxGetPrinterDC(printData);
120 SetMapMode(wxMM_TEXT);
122 SetBrush(*wxBLACK_BRUSH);
123 SetPen(*wxBLACK_PEN);
127 wxPrinterDC::wxPrinterDC(WXHDC theDC)
129 m_isInteractive = FALSE;
135 // int width = GetDeviceCaps(m_hDC, VERTRES);
136 // int height = GetDeviceCaps(m_hDC, HORZRES);
137 SetMapMode(wxMM_TEXT);
139 SetBrush(*wxBLACK_BRUSH);
140 SetPen(*wxBLACK_PEN);
143 wxPrinterDC::~wxPrinterDC(void)
147 bool wxPrinterDC::StartDoc(const wxString& message)
150 docinfo.cbSize = sizeof(DOCINFO);
151 docinfo.lpszDocName = (const wxChar*)message;
153 wxString filename(m_printData.GetFilename());
155 if (filename.IsEmpty())
156 docinfo.lpszOutput = NULL;
158 docinfo.lpszOutput = (const wxChar *) filename;
160 #if defined(__WIN95__)
161 docinfo.lpszDatatype = NULL;
170 ::StartDoc((HDC) m_hDC, &docinfo);
173 ::StartDocW((HDC) m_hDC, &docinfo);
176 ::StartDoc((HDC) m_hDC, &docinfo);
178 ::StartDocA((HDC) m_hDC, &docinfo);
186 DWORD lastError = GetLastError();
187 wxLogDebug(_T("wxDC::StartDoc failed with error: %d\n"), lastError);
194 void wxPrinterDC::EndDoc(void)
196 if (m_hDC) ::EndDoc((HDC) m_hDC);
199 void wxPrinterDC::StartPage(void)
202 ::StartPage((HDC) m_hDC);
205 void wxPrinterDC::EndPage(void)
208 ::EndPage((HDC) m_hDC);
211 // Returns default device and port names
212 static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName)
216 LPDEVNAMES lpDevNames;
217 LPSTR lpszDriverName;
218 LPSTR lpszDeviceName;
223 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
225 pd.lStructSize = 66; // sizeof(PRINTDLG);
227 pd.lStructSize = sizeof(PRINTDLG);
230 pd.hwndOwner = (HWND)NULL;
231 pd.hDevMode = NULL; // Will be created by PrintDlg
232 pd.hDevNames = NULL; // Ditto
233 pd.Flags = PD_RETURNDEFAULT;
236 if (!PrintDlg((LPPRINTDLG)&pd))
239 GlobalFree(pd.hDevMode);
241 GlobalFree(pd.hDevNames);
248 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
249 lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset;
250 lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset;
251 lpszPortName = (LPSTR)lpDevNames + lpDevNames->wOutputOffset;
252 GlobalUnlock(pd.hDevNames);
253 GlobalFree(pd.hDevNames);
256 deviceName = lpszDeviceName;
257 portName = lpszPortName;
262 GlobalFree(pd.hDevMode);
265 return ( deviceName != _T("") );
269 // This uses defaults, except for orientation, so we should eliminate this function
270 // and use the 2nd form (passing wxPrintData) instead.
271 WXHDC wxGetPrinterDC(int orientation)
274 LPDEVMODE lpDevMode = NULL;
275 LPDEVNAMES lpDevNames;
276 LPSTR lpszDriverName;
277 LPSTR lpszDeviceName;
281 // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68
283 pd.lStructSize = 66; // sizeof(PRINTDLG);
285 pd.lStructSize = sizeof(PRINTDLG);
287 pd.hwndOwner = (HWND)NULL;
288 pd.hDevMode = NULL; // Will be created by PrintDlg
289 pd.hDevNames = NULL; // Ditto
290 pd.Flags = PD_RETURNDEFAULT;
293 if (!PrintDlg((LPPRINTDLG)&pd))
296 GlobalFree(pd.hDevMode);
298 GlobalFree(pd.hDevNames);
306 GlobalFree(pd.hDevMode);
309 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
310 lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset;
311 lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset;
312 lpszPortName = (LPSTR)lpDevNames + lpDevNames->wOutputOffset;
313 GlobalUnlock(pd.hDevNames);
317 lpDevMode = (DEVMODE*) GlobalLock(pd.hDevMode);
318 lpDevMode->dmOrientation = orientation;
319 lpDevMode->dmFields |= DM_ORIENTATION;
323 hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (DEVMODE *)lpDevMode);
325 hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (LPSTR)lpDevMode);
328 if (pd.hDevMode && lpDevMode)
329 GlobalUnlock(pd.hDevMode);
333 GlobalFree(pd.hDevNames);
338 GlobalFree(pd.hDevMode);
345 // Gets an HDC for the specified printer configuration
346 WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst)
348 wxPrintData printData = printDataConst;
349 printData.ConvertToNative();
351 wxChar* driverName = (wxChar*) NULL;
353 wxString devNameStr = printData.GetPrinterName();
355 wxChar* portName = (wxChar*) NULL; // Obsolete in WIN32
357 if (devNameStr == _T(""))
358 deviceName = (wxChar*) NULL;
360 deviceName = WXSTRINGCAST devNameStr;
362 LPDEVMODE lpDevMode = (LPDEVMODE) NULL;
364 HGLOBAL hDevMode = (HGLOBAL)(DWORD) printData.GetNativeData();
367 lpDevMode = (DEVMODE*) GlobalLock(hDevMode);
369 if (devNameStr == _T(""))
371 // Retrieve the default device name
373 bool ret = wxGetDefaultDeviceName(devNameStr, portName);
375 wxASSERT_MSG( ret, _T("Could not get default device name.") );
377 deviceName = WXSTRINGCAST devNameStr;
381 HDC hDC = CreateDC(driverName, deviceName, portName, (DEVMODE *) lpDevMode);
383 HDC hDC = CreateDC(driverName, deviceName, portName, (LPSTR) lpDevMode);
386 if (hDevMode && lpDevMode)
387 GlobalUnlock(hDevMode);