]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dcprint.cpp
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 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
43 // This form is deprecated
44 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
, const wxString
& device_name
, const wxString
& file
, bool interactive
, int orientation
)
46 m_isInteractive
= interactive
;
48 if (!file
.IsNull() && file
!= wxT(""))
49 m_printData
.SetFilename(file
);
51 #if wxUSE_COMMON_DIALOGS
56 pd
.lStructSize
= sizeof( PRINTDLG
);
57 pd
.hwndOwner
=(HWND
) NULL
;
58 pd
.hDevMode
=(HANDLE
)NULL
;
59 pd
.hDevNames
=(HANDLE
)NULL
;
60 pd
.Flags
=PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
66 pd
.hInstance
=(HINSTANCE
)NULL
;
68 if ( PrintDlg( &pd
) != 0 )
70 m_hDC
= (WXHDC
) pd
.hDC
;
79 // m_dontDelete = TRUE;
83 if ((!driver_name
.IsNull() && driver_name
!= wxT("")) &&
84 (!device_name
.IsNull() && device_name
!= wxT("")) &&
85 (!file
.IsNull() && file
!= wxT("")))
87 m_hDC
= (WXHDC
) CreateDC(WXSTRINGCAST driver_name
, WXSTRINGCAST device_name
, WXSTRINGCAST file
, NULL
);
88 m_ok
= m_hDC
? TRUE
: FALSE
;
92 wxPrintData printData
;
93 printData
.SetOrientation(orientation
);
94 m_hDC
= wxGetPrinterDC(printData
);
95 m_ok
= m_hDC
? TRUE
: FALSE
;
100 // int width = GetDeviceCaps(m_hDC, VERTRES);
101 // int height = GetDeviceCaps(m_hDC, HORZRES);
102 SetMapMode(wxMM_TEXT
);
104 SetBrush(*wxBLACK_BRUSH
);
105 SetPen(*wxBLACK_PEN
);
108 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
110 m_printData
= printData
;
112 m_isInteractive
= FALSE
;
114 m_hDC
= wxGetPrinterDC(printData
);
118 SetMapMode(wxMM_TEXT
);
120 SetBrush(*wxBLACK_BRUSH
);
121 SetPen(*wxBLACK_PEN
);
125 wxPrinterDC::wxPrinterDC(WXHDC theDC
)
127 m_isInteractive
= FALSE
;
133 // int width = GetDeviceCaps(m_hDC, VERTRES);
134 // int height = GetDeviceCaps(m_hDC, HORZRES);
135 SetMapMode(wxMM_TEXT
);
137 SetBrush(*wxBLACK_BRUSH
);
138 SetPen(*wxBLACK_PEN
);
141 wxPrinterDC::~wxPrinterDC(void)
145 bool wxPrinterDC::StartDoc(const wxString
& message
)
148 docinfo
.cbSize
= sizeof(DOCINFO
);
149 docinfo
.lpszDocName
= (const wxChar
*)message
;
151 wxString
filename(m_printData
.GetFilename());
153 if (filename
.IsEmpty())
154 docinfo
.lpszOutput
= NULL
;
156 docinfo
.lpszOutput
= (const wxChar
*) filename
;
158 #if defined(__WIN95__)
159 docinfo
.lpszDatatype
= NULL
;
168 ::StartDoc((HDC
) m_hDC
, &docinfo
);
171 ::StartDocW((HDC
) m_hDC
, &docinfo
);
174 ::StartDoc((HDC
) m_hDC
, &docinfo
);
176 ::StartDocA((HDC
) m_hDC
, &docinfo
);
184 DWORD lastError
= GetLastError();
185 wxLogDebug(wxT("wxDC::StartDoc failed with error: %d\n"), lastError
);
192 void wxPrinterDC::EndDoc(void)
194 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
197 void wxPrinterDC::StartPage(void)
200 ::StartPage((HDC
) m_hDC
);
203 void wxPrinterDC::EndPage(void)
206 ::EndPage((HDC
) m_hDC
);
209 // Returns default device and port names
210 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
214 LPDEVNAMES lpDevNames
;
215 LPSTR lpszDriverName
;
216 LPSTR lpszDeviceName
;
221 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
223 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
225 pd
.lStructSize
= sizeof(PRINTDLG
);
228 pd
.hwndOwner
= (HWND
)NULL
;
229 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
230 pd
.hDevNames
= NULL
; // Ditto
231 pd
.Flags
= PD_RETURNDEFAULT
;
234 if (!PrintDlg((LPPRINTDLG
)&pd
))
237 GlobalFree(pd
.hDevMode
);
239 GlobalFree(pd
.hDevNames
);
246 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
247 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
248 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
249 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
251 deviceName
= lpszDeviceName
;
252 portName
= lpszPortName
;
254 GlobalUnlock(pd
.hDevNames
);
255 GlobalFree(pd
.hDevNames
);
261 GlobalFree(pd
.hDevMode
);
264 return ( deviceName
!= wxT("") );
268 // This uses defaults, except for orientation, so we should eliminate this function
269 // and use the 2nd form (passing wxPrintData) instead.
270 WXHDC
wxGetPrinterDC(int orientation
)
273 LPDEVMODE lpDevMode
= NULL
;
274 LPDEVNAMES lpDevNames
;
275 LPSTR lpszDriverName
;
276 LPSTR lpszDeviceName
;
280 // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68
282 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
284 pd
.lStructSize
= sizeof(PRINTDLG
);
286 pd
.hwndOwner
= (HWND
)NULL
;
287 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
288 pd
.hDevNames
= NULL
; // Ditto
289 pd
.Flags
= PD_RETURNDEFAULT
;
292 if (!PrintDlg((LPPRINTDLG
)&pd
))
295 GlobalFree(pd
.hDevMode
);
297 GlobalFree(pd
.hDevNames
);
305 GlobalFree(pd
.hDevMode
);
308 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
309 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
310 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
311 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
312 GlobalUnlock(pd
.hDevNames
);
316 lpDevMode
= (DEVMODE
*) GlobalLock(pd
.hDevMode
);
317 lpDevMode
->dmOrientation
= orientation
;
318 lpDevMode
->dmFields
|= DM_ORIENTATION
;
322 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (DEVMODE
*)lpDevMode
);
324 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (LPSTR
)lpDevMode
);
327 if (pd
.hDevMode
&& lpDevMode
)
328 GlobalUnlock(pd
.hDevMode
);
332 GlobalFree(pd
.hDevNames
);
337 GlobalFree(pd
.hDevMode
);
344 // Gets an HDC for the specified printer configuration
345 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
347 wxPrintData printData
= printDataConst
;
348 printData
.ConvertToNative();
350 wxChar
* driverName
= (wxChar
*) NULL
;
352 wxString devNameStr
= printData
.GetPrinterName();
354 wxChar
* portName
= (wxChar
*) NULL
; // Obsolete in WIN32
356 if (devNameStr
== wxT(""))
357 deviceName
= (wxChar
*) NULL
;
359 deviceName
= WXSTRINGCAST devNameStr
;
361 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
363 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) printData
.GetNativeData();
366 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
368 if (devNameStr
== wxT(""))
370 // Retrieve the default device name
376 #endif // Debug/Release
377 wxGetDefaultDeviceName(devNameStr
, portName
);
379 wxASSERT_MSG( ret
, wxT("Could not get default device name.") );
381 deviceName
= WXSTRINGCAST devNameStr
;
385 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
387 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
390 if (hDevMode
&& lpDevMode
)
391 GlobalUnlock(hDevMode
);