]>
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"
32 #if wxUSE_COMMON_DIALOGS
52 #if !USE_SHARED_LIBRARY
53 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
56 // This form is deprecated
57 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
, const wxString
& device_name
, const wxString
& file
, bool interactive
, int orientation
)
59 m_isInteractive
= interactive
;
61 if (!file
.IsNull() && file
!= "")
62 m_printData
.SetFilename(file
);
64 #if wxUSE_COMMON_DIALOGS
69 pd
.lStructSize
= sizeof( PRINTDLG
);
70 pd
.hwndOwner
=(HWND
) NULL
;
71 pd
.hDevMode
=(HANDLE
)NULL
;
72 pd
.hDevNames
=(HANDLE
)NULL
;
73 pd
.Flags
=PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
79 pd
.hInstance
=(HINSTANCE
)NULL
;
81 if ( PrintDlg( &pd
) != 0 )
83 m_hDC
= (WXHDC
) pd
.hDC
;
92 // m_dontDelete = TRUE;
96 if ((!driver_name
.IsNull() && driver_name
!= "") &&
97 (!device_name
.IsNull() && device_name
!= "") &&
98 (!file
.IsNull() && file
!= ""))
100 m_hDC
= (WXHDC
) CreateDC((char *) (const char *) driver_name
, (char *) (const char *) device_name
, (char *) (const char *) file
, NULL
);
101 m_ok
= m_hDC
? TRUE
: FALSE
;
105 wxPrintData printData
;
106 printData
.SetOrientation(orientation
);
107 m_hDC
= wxGetPrinterDC(printData
);
108 m_ok
= m_hDC
? TRUE
: FALSE
;
113 // int width = GetDeviceCaps(m_hDC, VERTRES);
114 // int height = GetDeviceCaps(m_hDC, HORZRES);
115 SetMapMode(wxMM_TEXT
);
117 SetBrush(*wxBLACK_BRUSH
);
118 SetPen(*wxBLACK_PEN
);
121 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
123 m_printData
= printData
;
125 m_isInteractive
= FALSE
;
127 m_hDC
= wxGetPrinterDC(printData
);
131 SetMapMode(wxMM_TEXT
);
133 SetBrush(*wxBLACK_BRUSH
);
134 SetPen(*wxBLACK_PEN
);
138 wxPrinterDC::wxPrinterDC(WXHDC theDC
)
140 m_isInteractive
= FALSE
;
146 // int width = GetDeviceCaps(m_hDC, VERTRES);
147 // int height = GetDeviceCaps(m_hDC, HORZRES);
148 SetMapMode(wxMM_TEXT
);
150 SetBrush(*wxBLACK_BRUSH
);
151 SetPen(*wxBLACK_PEN
);
154 wxPrinterDC::~wxPrinterDC(void)
158 bool wxPrinterDC::StartDoc(const wxString
& message
)
161 docinfo
.cbSize
= sizeof(DOCINFO
);
162 docinfo
.lpszDocName
= (const char *)message
;
164 wxString
filename(m_printData
.GetFilename());
166 if (filename
.IsEmpty())
167 docinfo
.lpszOutput
= NULL
;
169 docinfo
.lpszOutput
= (const char *) filename
;
171 #if defined(__WIN95__)
172 docinfo
.lpszDatatype
= NULL
;
181 ::StartDoc((HDC
) m_hDC
, &docinfo
);
184 ::StartDocW((HDC
) m_hDC
, &docinfo
);
187 ::StartDoc((HDC
) m_hDC
, &docinfo
);
189 ::StartDocA((HDC
) m_hDC
, &docinfo
);
197 DWORD lastError
= GetLastError();
198 wxLogDebug("wxDC::StartDoc failed with error: %d\n", lastError
);
205 void wxPrinterDC::EndDoc(void)
207 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
210 void wxPrinterDC::StartPage(void)
213 ::StartPage((HDC
) m_hDC
);
216 void wxPrinterDC::EndPage(void)
219 ::EndPage((HDC
) m_hDC
);
222 // Returns default device and port names
223 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
227 LPDEVNAMES lpDevNames
;
228 LPSTR lpszDriverName
;
229 LPSTR lpszDeviceName
;
234 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
236 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
238 pd
.lStructSize
= sizeof(PRINTDLG
);
241 pd
.hwndOwner
= (HWND
)NULL
;
242 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
243 pd
.hDevNames
= NULL
; // Ditto
244 pd
.Flags
= PD_RETURNDEFAULT
;
247 if (!PrintDlg((LPPRINTDLG
)&pd
))
250 GlobalFree(pd
.hDevMode
);
252 GlobalFree(pd
.hDevNames
);
259 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
260 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
261 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
262 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
263 GlobalUnlock(pd
.hDevNames
);
264 GlobalFree(pd
.hDevNames
);
267 deviceName
= lpszDeviceName
;
268 portName
= lpszPortName
;
273 GlobalFree(pd
.hDevMode
);
276 return ( deviceName
!= "" );
280 // This uses defaults, except for orientation, so we should eliminate this function
281 // and use the 2nd form (passing wxPrintData) instead.
282 WXHDC
wxGetPrinterDC(int orientation
)
285 LPDEVMODE lpDevMode
= NULL
;
286 LPDEVNAMES lpDevNames
;
287 LPSTR lpszDriverName
;
288 LPSTR lpszDeviceName
;
292 // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68
294 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
296 pd
.lStructSize
= sizeof(PRINTDLG
);
298 pd
.hwndOwner
= (HWND
)NULL
;
299 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
300 pd
.hDevNames
= NULL
; // Ditto
301 pd
.Flags
= PD_RETURNDEFAULT
;
304 if (!PrintDlg((LPPRINTDLG
)&pd
))
307 GlobalFree(pd
.hDevMode
);
309 GlobalFree(pd
.hDevNames
);
317 GlobalFree(pd
.hDevMode
);
320 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
321 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
322 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
323 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
324 GlobalUnlock(pd
.hDevNames
);
328 lpDevMode
= (DEVMODE
*) GlobalLock(pd
.hDevMode
);
329 lpDevMode
->dmOrientation
= orientation
;
330 lpDevMode
->dmFields
|= DM_ORIENTATION
;
334 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (DEVMODE
*)lpDevMode
);
336 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (LPSTR
)lpDevMode
);
339 if (pd
.hDevMode
&& lpDevMode
)
340 GlobalUnlock(pd
.hDevMode
);
344 GlobalFree(pd
.hDevNames
);
349 GlobalFree(pd
.hDevMode
);
356 // Gets an HDC for the specified printer configuration
357 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
359 wxPrintData printData
= printDataConst
;
360 printData
.ConvertToNative();
362 char* driverName
= (char*) NULL
;
364 wxString devNameStr
= printData
.GetPrinterName();
366 char* portName
= (char*) NULL
; // Obsolete in WIN32
368 if (devNameStr
== "")
369 deviceName
= (char*) NULL
;
371 deviceName
= (char*) (const char*) devNameStr
;
373 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
375 HGLOBAL hDevMode
= (HGLOBAL
) printData
.GetNativeData();
378 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
380 if (devNameStr
== "")
382 // Retrieve the default device name
384 bool ret
= wxGetDefaultDeviceName(devNameStr
, portName
);
386 wxASSERT_MSG( ret
, "Could not get default device name." );
388 deviceName
= (char*) (const char*) devNameStr
;
392 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
394 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
397 if (hDevMode
&& lpDevMode
)
398 GlobalUnlock(hDevMode
);