]>
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"
30 #include "wx/msw/private.h"
32 #if wxUSE_COMMON_DIALOGS
40 #if !USE_SHARED_LIBRARY
41 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
44 // This form is deprecated
45 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
, const wxString
& device_name
, const wxString
& file
, bool interactive
, int orientation
)
47 m_isInteractive
= interactive
;
49 if (!file
.IsNull() && file
!= "")
50 m_printData
.SetFilename(file
);
52 #if wxUSE_COMMON_DIALOGS
57 pd
.lStructSize
= sizeof( PRINTDLG
);
58 pd
.hwndOwner
=(HWND
) NULL
;
59 pd
.hDevMode
=(HANDLE
)NULL
;
60 pd
.hDevNames
=(HANDLE
)NULL
;
61 pd
.Flags
=PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
67 pd
.hInstance
=(HINSTANCE
)NULL
;
69 if ( PrintDlg( &pd
) != 0 )
71 m_hDC
= (WXHDC
) pd
.hDC
;
80 // m_dontDelete = TRUE;
84 if ((!driver_name
.IsNull() && driver_name
!= "") &&
85 (!device_name
.IsNull() && device_name
!= "") &&
86 (!file
.IsNull() && file
!= ""))
88 m_hDC
= (WXHDC
) CreateDC((char *) (const char *) driver_name
, (char *) (const char *) device_name
, (char *) (const char *) file
, NULL
);
89 m_ok
= m_hDC
? TRUE
: FALSE
;
93 wxPrintData printData
;
94 printData
.SetOrientation(orientation
);
95 m_hDC
= wxGetPrinterDC(printData
);
96 m_ok
= m_hDC
? TRUE
: FALSE
;
101 // int width = GetDeviceCaps(m_hDC, VERTRES);
102 // int height = GetDeviceCaps(m_hDC, HORZRES);
103 SetMapMode(wxMM_TEXT
);
105 SetBrush(*wxBLACK_BRUSH
);
106 SetPen(*wxBLACK_PEN
);
109 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
111 m_printData
= printData
;
113 m_isInteractive
= FALSE
;
115 m_hDC
= wxGetPrinterDC(printData
);
119 SetMapMode(wxMM_TEXT
);
121 SetBrush(*wxBLACK_BRUSH
);
122 SetPen(*wxBLACK_PEN
);
126 wxPrinterDC::wxPrinterDC(WXHDC theDC
)
128 m_isInteractive
= FALSE
;
134 // int width = GetDeviceCaps(m_hDC, VERTRES);
135 // int height = GetDeviceCaps(m_hDC, HORZRES);
136 SetMapMode(wxMM_TEXT
);
138 SetBrush(*wxBLACK_BRUSH
);
139 SetPen(*wxBLACK_PEN
);
142 wxPrinterDC::~wxPrinterDC(void)
146 bool wxPrinterDC::StartDoc(const wxString
& message
)
149 docinfo
.cbSize
= sizeof(DOCINFO
);
150 docinfo
.lpszDocName
= (const char *)message
;
152 wxString
filename(m_printData
.GetFilename());
154 if (filename
.IsEmpty())
155 docinfo
.lpszOutput
= NULL
;
157 docinfo
.lpszOutput
= (const char *) filename
;
159 #if defined(__WIN95__)
160 docinfo
.lpszDatatype
= NULL
;
169 ::StartDoc((HDC
) m_hDC
, &docinfo
);
172 ::StartDocW((HDC
) m_hDC
, &docinfo
);
175 ::StartDoc((HDC
) m_hDC
, &docinfo
);
177 ::StartDocA((HDC
) m_hDC
, &docinfo
);
185 DWORD lastError
= GetLastError();
186 wxLogDebug("wxDC::StartDoc failed with error: %d\n", lastError
);
193 void wxPrinterDC::EndDoc(void)
195 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
198 void wxPrinterDC::StartPage(void)
201 ::StartPage((HDC
) m_hDC
);
204 void wxPrinterDC::EndPage(void)
207 ::EndPage((HDC
) m_hDC
);
210 // Returns default device and port names
211 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
215 LPDEVNAMES lpDevNames
;
216 LPSTR lpszDriverName
;
217 LPSTR lpszDeviceName
;
222 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
224 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
226 pd
.lStructSize
= sizeof(PRINTDLG
);
229 pd
.hwndOwner
= (HWND
)NULL
;
230 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
231 pd
.hDevNames
= NULL
; // Ditto
232 pd
.Flags
= PD_RETURNDEFAULT
;
235 if (!PrintDlg((LPPRINTDLG
)&pd
))
238 GlobalFree(pd
.hDevMode
);
240 GlobalFree(pd
.hDevNames
);
247 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
248 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
249 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
250 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
251 GlobalUnlock(pd
.hDevNames
);
252 GlobalFree(pd
.hDevNames
);
255 deviceName
= lpszDeviceName
;
256 portName
= lpszPortName
;
261 GlobalFree(pd
.hDevMode
);
264 return ( deviceName
!= "" );
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 char* driverName
= (char*) NULL
;
352 wxString devNameStr
= printData
.GetPrinterName();
354 char* portName
= (char*) NULL
; // Obsolete in WIN32
356 if (devNameStr
== "")
357 deviceName
= (char*) NULL
;
359 deviceName
= (char*) (const char*) devNameStr
;
361 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
363 HGLOBAL hDevMode
= (HGLOBAL
) printData
.GetNativeData();
366 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
368 if (devNameStr
== "")
370 // Retrieve the default device name
372 bool ret
= wxGetDefaultDeviceName(devNameStr
, portName
);
374 wxASSERT_MSG( ret
, "Could not get default device name." );
376 deviceName
= (char*) (const char*) devNameStr
;
380 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
382 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
385 if (hDevMode
&& lpDevMode
)
386 GlobalUnlock(hDevMode
);