2 // "$Id: Fl_GDI_Printer.cxx 8467 2011-02-23 14:36:18Z manolo $"
4 // Support for WIN32 printing for the Fast Light Tool Kit (FLTK).
6 // Copyright 2010 by Bill Spitzak and others.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
29 #include <FL/Fl_Printer.H>
32 #include <FL/fl_ask.H>
35 extern HWND fl_window
;
37 Fl_System_Printer::Fl_System_Printer(void) : Fl_Paged_Device() {
39 driver(Fl_Display_Device::display_device()->driver());
42 Fl_System_Printer::~Fl_System_Printer(void) {
46 static void WIN_SetupPrinterDeviceContext(HDC prHDC
)
52 SetGraphicsMode(prHDC
, GM_ADVANCED
); // to allow for rotations
53 SetMapMode(prHDC
, MM_ANISOTROPIC
);
54 SetTextAlign(prHDC
, TA_BASELINE
|TA_LEFT
);
55 SetBkMode(prHDC
, TRANSPARENT
);
56 // this matches 720 logical units to the number of device units in 10 inches of paper
57 // thus the logical unit is the point (= 1/72 inch)
58 SetWindowExtEx(prHDC
, 720, 720, NULL
);
59 SetViewportExtEx(prHDC
, 10*GetDeviceCaps(prHDC
, LOGPIXELSX
), 10*GetDeviceCaps(prHDC
, LOGPIXELSY
), NULL
);
63 int Fl_System_Printer::start_job (int pagecount
, int *frompage
, int *topage
)
72 memset (&pd
, 0, sizeof (PRINTDLG
));
73 pd
.lStructSize
= sizeof (PRINTDLG
);
74 pd
.hwndOwner
= GetForegroundWindow();
75 pd
.Flags
= PD_RETURNDC
| PD_USEDEVMODECOPIESANDCOLLATE
| PD_NOSELECTION
;
77 pd
.nMaxPage
= pagecount
;
78 if (PrintDlg (&pd
) != 0) {
81 strcpy (docName
, "FLTK");
82 memset(&di
, 0, sizeof(DOCINFO
));
83 di
.cbSize
= sizeof (DOCINFO
);
84 di
.lpszDocName
= (LPCSTR
) docName
;
85 prerr
= StartDoc (hPr
, &di
);
88 //fl_alert ("StartDoc error %d", prerr);
92 commdlgerr
= CommDlgExtendedError ();
93 fl_alert ("Unable to create print context, error %lu",
94 (unsigned long) commdlgerr
);
101 if((pd
.Flags
& PD_PAGENUMS
) != 0 ) {
102 if (frompage
) *frompage
= pd
.nFromPage
;
103 if (topage
) *topage
= pd
.nToPage
;
106 if (frompage
) *frompage
= 1;
107 if (topage
) *topage
= pagecount
;
111 WIN_SetupPrinterDeviceContext (hPr
);
118 void Fl_System_Printer::end_job (void)
120 Fl_Display_Device::display_device()->set_current();
123 prerr
= EndDoc (hPr
);
125 fl_alert ("EndDoc error %d", prerr
);
129 if (pd
.hDevMode
!= NULL
) {
130 GlobalFree (pd
.hDevMode
);
132 if (pd
.hDevNames
!= NULL
) {
133 GlobalFree (pd
.hDevNames
);
139 void Fl_System_Printer::absolute_printable_rect(int *x
, int *y
, int *w
, int *h
)
144 if (hPr
== NULL
) return;
145 SetWindowOrgEx(fl_gc
, 0, 0, NULL
);
147 physPageSize
.x
= GetDeviceCaps(hPr
, HORZRES
);
148 physPageSize
.y
= GetDeviceCaps(hPr
, VERTRES
);
149 DPtoLP(hPr
, &physPageSize
, 1);
150 *w
= physPageSize
.x
+ 1;
151 *h
= physPageSize
.y
+ 1;
152 pixelsPerInch
.x
= GetDeviceCaps(hPr
, LOGPIXELSX
);
153 pixelsPerInch
.y
= GetDeviceCaps(hPr
, LOGPIXELSY
);
154 DPtoLP(hPr
, &pixelsPerInch
, 1);
155 left_margin
= (pixelsPerInch
.x
/ 4);
156 *w
-= (pixelsPerInch
.x
/ 2);
157 top_margin
= (pixelsPerInch
.y
/ 4);
158 *h
-= (pixelsPerInch
.y
/ 2);
162 origin(x_offset
, y_offset
);
165 void Fl_System_Printer::margins(int *left
, int *top
, int *right
, int *bottom
)
168 absolute_printable_rect(&x
, &y
, &w
, &h
);
171 if (right
) *right
= x
;
172 if (bottom
) *bottom
= y
;
175 int Fl_System_Printer::printable_rect(int *w
, int *h
)
178 absolute_printable_rect(&x
, &y
, w
, h
);
182 int Fl_System_Printer::start_page (void)
188 WIN_SetupPrinterDeviceContext (hPr
);
189 prerr
= StartPage (hPr
);
191 fl_alert ("StartPage error %d", prerr
);
194 printable_rect(&w
, &h
);
202 void Fl_System_Printer::origin (int deltax
, int deltay
)
204 SetWindowOrgEx(fl_gc
, - left_margin
- deltax
, - top_margin
- deltay
, NULL
);
209 void Fl_System_Printer::scale (float scalex
, float scaley
)
211 if (scaley
== 0.) scaley
= scalex
;
213 SetWindowExtEx(fl_gc
, (int)(720 / scalex
+ 0.5), (int)(720 / scaley
+ 0.5), NULL
);
214 printable_rect(&w
, &h
);
218 void Fl_System_Printer::rotate (float rot_angle
)
222 angle
= - rot_angle
* M_PI
/ 180.;
223 mat
.eM11
= cos(angle
);
224 mat
.eM12
= sin(angle
);
225 mat
.eM21
= - mat
.eM12
;
227 mat
.eDx
= mat
.eDy
= 0;
228 SetWorldTransform(fl_gc
, &mat
);
231 int Fl_System_Printer::end_page (void)
237 prerr
= EndPage (hPr
);
240 fl_alert ("EndPage error %d", prerr
);
248 static int translate_stack_depth
= 0;
249 const int translate_stack_max
= 5;
250 static int translate_stack_x
[translate_stack_max
];
251 static int translate_stack_y
[translate_stack_max
];
253 static void do_translate(int x
, int y
)
256 tr
.eM11
= tr
.eM22
= 1;
257 tr
.eM12
= tr
.eM21
= 0;
260 ModifyWorldTransform(fl_gc
, &tr
, MWT_LEFTMULTIPLY
);
263 void Fl_System_Printer::translate (int x
, int y
)
266 if (translate_stack_depth
< translate_stack_max
) {
267 translate_stack_x
[translate_stack_depth
] = x
;
268 translate_stack_y
[translate_stack_depth
] = y
;
269 translate_stack_depth
++;
273 void Fl_System_Printer::untranslate (void)
275 if (translate_stack_depth
> 0) {
276 translate_stack_depth
--;
277 do_translate( - translate_stack_x
[translate_stack_depth
], - translate_stack_y
[translate_stack_depth
] );
284 // End of "$Id: Fl_GDI_Printer.cxx 8467 2011-02-23 14:36:18Z manolo $".