]>
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/dcprint.h"
31 #if wxUSE_COMMON_DIALOGS
51 #if !USE_SHARED_LIBRARY
52 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
55 // This form is deprecated
56 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
, const wxString
& device_name
, const wxString
& file
, bool interactive
, int orientation
)
58 m_isInteractive
= interactive
;
60 if (!file
.IsNull() && file
!= "")
61 m_printData
.SetFilename(file
);
63 #if wxUSE_COMMON_DIALOGS
68 pd
.lStructSize
= sizeof( PRINTDLG
);
69 pd
.hwndOwner
=(HWND
) NULL
;
70 pd
.hDevMode
=(HANDLE
)NULL
;
71 pd
.hDevNames
=(HANDLE
)NULL
;
72 pd
.Flags
=PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
78 pd
.hInstance
=(HINSTANCE
)NULL
;
80 if ( PrintDlg( &pd
) != 0 )
82 m_hDC
= (WXHDC
) pd
.hDC
;
91 // m_dontDelete = TRUE;
95 if ((!driver_name
.IsNull() && driver_name
!= "") &&
96 (!device_name
.IsNull() && device_name
!= "") &&
97 (!file
.IsNull() && file
!= ""))
99 m_hDC
= (WXHDC
) CreateDC((char *) (const char *) driver_name
, (char *) (const char *) device_name
, (char *) (const char *) file
, NULL
);
100 m_ok
= m_hDC
? TRUE
: FALSE
;
104 wxPrintData printData
;
105 printData
.SetOrientation(orientation
);
106 m_hDC
= wxGetPrinterDC(printData
);
107 m_ok
= m_hDC
? TRUE
: FALSE
;
112 // int width = GetDeviceCaps(m_hDC, VERTRES);
113 // int height = GetDeviceCaps(m_hDC, HORZRES);
114 SetMapMode(wxMM_TEXT
);
116 SetBrush(*wxBLACK_BRUSH
);
117 SetPen(*wxBLACK_PEN
);
120 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
122 m_printData
= printData
;
124 m_isInteractive
= FALSE
;
126 m_hDC
= wxGetPrinterDC(printData
);
130 SetMapMode(wxMM_TEXT
);
132 SetBrush(*wxBLACK_BRUSH
);
133 SetPen(*wxBLACK_PEN
);
137 wxPrinterDC::wxPrinterDC(WXHDC theDC
)
139 m_isInteractive
= FALSE
;
145 // int width = GetDeviceCaps(m_hDC, VERTRES);
146 // int height = GetDeviceCaps(m_hDC, HORZRES);
147 SetMapMode(wxMM_TEXT
);
149 SetBrush(*wxBLACK_BRUSH
);
150 SetPen(*wxBLACK_PEN
);
153 wxPrinterDC::~wxPrinterDC(void)
157 bool wxPrinterDC::StartDoc(const wxString
& message
)
160 docinfo
.cbSize
= sizeof(DOCINFO
);
161 docinfo
.lpszDocName
= (const char *)message
;
163 wxString
filename(m_printData
.GetFilename());
165 if (filename
.IsEmpty())
166 docinfo
.lpszOutput
= NULL
;
168 docinfo
.lpszOutput
= (const char *) filename
;
170 #if defined(__WIN95__)
171 docinfo
.lpszDatatype
= NULL
;
180 ::StartDoc((HDC
) m_hDC
, &docinfo
);
183 ::StartDocW((HDC
) m_hDC
, &docinfo
);
186 ::StartDoc((HDC
) m_hDC
, &docinfo
);
188 ::StartDocA((HDC
) m_hDC
, &docinfo
);
196 DWORD lastError
= GetLastError();
197 wxDebugMsg("wxDC::StartDoc failed with error: %d\n", lastError
);
204 void wxPrinterDC::EndDoc(void)
206 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
209 void wxPrinterDC::StartPage(void)
212 ::StartPage((HDC
) m_hDC
);
215 void wxPrinterDC::EndPage(void)
218 ::EndPage((HDC
) m_hDC
);
221 // Returns default device and port names
222 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
226 LPDEVNAMES lpDevNames
;
227 LPSTR lpszDriverName
;
228 LPSTR lpszDeviceName
;
233 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
235 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
237 pd
.lStructSize
= sizeof(PRINTDLG
);
240 pd
.hwndOwner
= (HWND
)NULL
;
241 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
242 pd
.hDevNames
= NULL
; // Ditto
243 pd
.Flags
= PD_RETURNDEFAULT
;
246 if (!PrintDlg((LPPRINTDLG
)&pd
))
249 GlobalFree(pd
.hDevMode
);
251 GlobalFree(pd
.hDevNames
);
258 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
259 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
260 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
261 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
262 GlobalUnlock(pd
.hDevNames
);
263 GlobalFree(pd
.hDevNames
);
266 deviceName
= lpszDeviceName
;
267 portName
= lpszPortName
;
272 GlobalFree(pd
.hDevMode
);
275 return ( deviceName
!= "" );
279 // This uses defaults, except for orientation, so we should eliminate this function
280 // and use the 2nd form (passing wxPrintData) instead.
281 WXHDC
wxGetPrinterDC(int orientation
)
284 LPDEVMODE lpDevMode
= NULL
;
285 LPDEVNAMES lpDevNames
;
286 LPSTR lpszDriverName
;
287 LPSTR lpszDeviceName
;
291 // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68
293 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
295 pd
.lStructSize
= sizeof(PRINTDLG
);
297 pd
.hwndOwner
= (HWND
)NULL
;
298 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
299 pd
.hDevNames
= NULL
; // Ditto
300 pd
.Flags
= PD_RETURNDEFAULT
;
303 if (!PrintDlg((LPPRINTDLG
)&pd
))
306 GlobalFree(pd
.hDevMode
);
308 GlobalFree(pd
.hDevNames
);
316 GlobalFree(pd
.hDevMode
);
319 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
320 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
321 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
322 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
323 GlobalUnlock(pd
.hDevNames
);
327 lpDevMode
= (DEVMODE
*) GlobalLock(pd
.hDevMode
);
328 lpDevMode
->dmOrientation
= orientation
;
329 lpDevMode
->dmFields
|= DM_ORIENTATION
;
333 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (DEVMODE
*)lpDevMode
);
335 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (LPSTR
)lpDevMode
);
338 if (pd
.hDevMode
&& lpDevMode
)
339 GlobalUnlock(pd
.hDevMode
);
343 GlobalFree(pd
.hDevNames
);
348 GlobalFree(pd
.hDevMode
);
355 // Gets an HDC for the specified printer configuration
356 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
358 wxPrintData printData
= printDataConst
;
359 printData
.ConvertToNative();
361 char* driverName
= (char*) NULL
;
363 wxString devNameStr
= printData
.GetPrinterName();
365 char* portName
= (char*) NULL
; // Obsolete in WIN32
367 if (devNameStr
== "")
368 deviceName
= (char*) NULL
;
370 deviceName
= (char*) (const char*) devNameStr
;
372 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
374 HGLOBAL hDevMode
= (HGLOBAL
) printData
.GetNativeData();
377 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
379 if (devNameStr
== "")
381 // Retrieve the default device name
383 bool ret
= wxGetDefaultDeviceName(devNameStr
, portName
);
385 wxASSERT_MSG( ret
, "Could not get default device name." );
387 deviceName
= (char*) (const char*) devNameStr
;
391 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
393 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
396 if (hDevMode
&& lpDevMode
)
397 GlobalUnlock(hDevMode
);