1 //
2 // "$Id$"
3 //
4 // implementation of Fl_Paged_Device class for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 2010-2016 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file.  If this
10 // file is missing or damaged, see the license at:
11 //
12 //     http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems to:
15 //
16 //     http://www.fltk.org/str.php
17 //
18 /** \file Fl_Paged_Device.cxx
19  \brief implementation of class Fl_Paged_Device.
20  */
21 
22 #include <FL/Fl_Paged_Device.H>
23 #include <FL/Fl.H>
24 #include <FL/fl_draw.H>
25 
26 const char *Fl_Paged_Device::class_id = "Fl_Paged_Device";
27 
28 
29 /**
30  @brief Draws the widget on the printed page.
31  *
32  The widget's position on the printed page is determined by the last call to origin()
33  and by the optional delta_x and delta_y arguments.
34  Its dimensions are in points unless there was a previous call to scale().
35  @param[in] widget Any FLTK widget (e.g., standard, custom, window).
36  @param[in] delta_x Optional horizontal offset for positioning the widget relatively
37  to the current origin of graphics functions.
38  @param[in] delta_y Same as above, vertically.
39  */
print_widget(Fl_Widget * widget,int delta_x,int delta_y)40 void Fl_Paged_Device::print_widget(Fl_Widget* widget, int delta_x, int delta_y)
41 {
42   int old_x, old_y, new_x, new_y, is_window;
43   if ( ! widget->visible() ) return;
44   is_window = (widget->as_window() != NULL);
45   uchar old_damage = widget->damage();
46   widget->damage(FL_DAMAGE_ALL);
47   // set origin to the desired top-left position of the widget
48   origin(&old_x, &old_y);
49   new_x = old_x + delta_x;
50   new_y = old_y + delta_y;
51   if (!is_window) {
52     new_x -= widget->x();
53     new_y -= widget->y();
54   }
55   if (new_x != old_x || new_y != old_y) {
56     translate(new_x - old_x, new_y - old_y );
57   }
58   // if widget is a main window, clip all drawings to the window area
59   if (is_window && !widget->window()) {
60     fl_push_clip(0, 0, widget->w(), widget->h() );
61 #ifdef __APPLE__ // for Mac OS X 10.6 and above, make window with rounded bottom corners
62     if ( fl_mac_os_version >= 100600 && driver()->class_name() == Fl_Quartz_Graphics_Driver::class_id ) {
63       Fl_X::clip_to_rounded_corners(fl_gc, widget->w(), widget->h());
64     }
65 #endif
66   }
67   // we do some trickery to recognize OpenGL windows and draw them via a plugin
68   int drawn_by_plugin = 0;
69   if (widget->as_gl_window()) {
70     Fl_Plugin_Manager pm("fltk:device");
71     Fl_Device_Plugin *pi = (Fl_Device_Plugin*)pm.plugin("opengl.device.fltk.org");
72     if (pi) {
73       drawn_by_plugin = pi->print(widget, 0, 0, 0);
74     }
75   }
76   if (!drawn_by_plugin) {
77     widget->draw();
78   }
79   if (is_window && !widget->window()) fl_pop_clip();
80   // find subwindows of widget and print them
81   traverse(widget);
82   // reset origin to where it was
83   if (new_x != old_x || new_y != old_y) {
84     untranslate();
85   }
86   if ((old_damage & FL_DAMAGE_CHILD) == 0) widget->clear_damage(old_damage);
87   else widget->damage(FL_DAMAGE_ALL);
88 }
89 
90 
traverse(Fl_Widget * widget)91 void Fl_Paged_Device::traverse(Fl_Widget *widget)
92 {
93   Fl_Group *g = widget->as_group();
94   if (!g) return;
95   int n = g->children();
96   for (int i = 0; i < n; i++) {
97     Fl_Widget *c = g->child(i);
98     if ( !c->visible() ) continue;
99     if ( c->as_window() ) {
100       print_widget(c, c->x(), c->y());
101     }
102     else traverse(c);
103   }
104 }
105 
106 /**
107  @brief Computes the page coordinates of the current origin of graphics functions.
108  *
109  @param[out] x If non-null, *x is set to the horizontal page offset of graphics origin.
110  @param[out] y Same as above, vertically.
111  */
origin(int * x,int * y)112 void Fl_Paged_Device::origin(int *x, int *y)
113 {
114   if (x) *x = x_offset;
115   if (y) *y = y_offset;
116 }
117 
118 /**
119  @brief Prints a rectangular part of an on-screen window.
120 
121  @param win The window from where to capture.
122  @param x The rectangle left
123  @param y The rectangle top
124  @param w The rectangle width
125  @param h The rectangle height
126  @param delta_x Optional horizontal offset from current graphics origin where to print the captured rectangle.
127  @param delta_y As above, vertically.
128  */
print_window_part(Fl_Window * win,int x,int y,int w,int h,int delta_x,int delta_y)129 void Fl_Paged_Device::print_window_part(Fl_Window *win, int x, int y, int w, int h, int delta_x, int delta_y)
130 {
131   Fl_Surface_Device *current = Fl_Surface_Device::surface();
132   Fl_Display_Device::display_device()->set_current();
133   Fl_Window *save_front = Fl::first_window();
134   win->show();
135   fl_gc = NULL;
136   Fl::check();
137   win->make_current();
138   uchar *image_data;
139   image_data = fl_read_image(NULL, x, y, w, h);
140 #ifdef __APPLE__
141   Fl_X::q_release_context(); // matches make_current() call above
142 #endif
143   if (save_front != win) save_front->show();
144   current->set_current();
145   fl_draw_image(image_data, delta_x, delta_y, w, h, 3);
146   delete[] image_data;
147 #ifdef WIN32
148   fl_gc = GetDC(fl_xid(win));
149   ReleaseDC(fl_xid(win), fl_gc);
150 #endif
151 }
152 
153 /**
154  @brief Starts a print job.
155 
156  @param[in] pagecount the total number of pages of the job (or 0 if you don't know the number of pages)
157  @param[out] frompage if non-null, *frompage is set to the first page the user wants printed
158  @param[out] topage if non-null, *topage is set to the last page the user wants printed
159  @return 0 if OK, non-zero if any error
160  */
start_job(int pagecount,int * frompage,int * topage)161 int Fl_Paged_Device::start_job(int pagecount, int *frompage, int *topage) {return 1;}
162 
163 /**
164  @brief Starts a new printed page
165 
166  The page coordinates are initially in points, i.e., 1/72 inch,
167  and with origin at the top left of the printable page area.
168  @return 0 if OK, non-zero if any error
169  */
start_page(void)170 int Fl_Paged_Device::start_page (void) {return 1;}
171 
172 /**
173  @brief Computes the width and height of the printable area of the page.
174 
175  Values are in the same unit as that used by FLTK drawing functions,
176  are unchanged by calls to origin(), but are changed by scale() calls.
177  Values account for the user-selected paper type and print orientation.
178  @return 0 if OK, non-zero if any error
179  */
printable_rect(int * w,int * h)180 int Fl_Paged_Device::printable_rect(int *w, int *h) {return 1;}
181 
182 /**
183  @brief Computes the dimensions of margins that lie between the printable page area and
184  the full page.
185 
186  Values are in the same unit as that used by FLTK drawing functions. They are changed
187  by scale() calls.
188  @param[out] left If non-null, *left is set to the left margin size.
189  @param[out] top If non-null, *top is set to the top margin size.
190  @param[out] right If non-null, *right is set to the right margin size.
191  @param[out] bottom If non-null, *bottom is set to the bottom margin size.
192  */
margins(int * left,int * top,int * right,int * bottom)193 void Fl_Paged_Device::margins(int *left, int *top, int *right, int *bottom) {}
194 
195 /**
196  @brief Sets the position in page coordinates of the origin of graphics functions.
197 
198  Arguments should be expressed relatively to the result of a previous printable_rect() call.
199  That is, <tt>printable_rect(&w, &h); origin(w/2, 0);</tt> sets the graphics origin at the
200  top center of the page printable area.
201  Origin() calls are not affected by rotate() calls.
202  Successive origin() calls don't combine their effects.
203  @param[in] x Horizontal position in page coordinates of the desired origin of graphics functions.
204  @param[in] y Same as above, vertically.
205  */
origin(int x,int y)206 void Fl_Paged_Device::origin(int x, int y) {}
207 
208 /**
209  @brief Changes the scaling of page coordinates.
210 
211  This function also resets the origin of graphics functions at top left of printable page area.
212  After a scale() call, do a printable_rect() call to get the new dimensions of the printable page area.
213  Successive scale() calls don't combine their effects.
214  @param scale_x Horizontal dimensions of plot are multiplied by this quantity.
215  @param scale_y Same as above, vertically.
216   The value 0. is equivalent to setting \p scale_y = \p scale_x. Thus, scale(factor);
217   is equivalent to scale(factor, factor);
218  */
scale(float scale_x,float scale_y)219 void Fl_Paged_Device::scale (float scale_x, float scale_y) {}
220 
221 /**
222  @brief Rotates the graphics operations relatively to paper.
223 
224  The rotation is centered on the current graphics origin.
225  Successive rotate() calls don't combine their effects.
226  @param angle Rotation angle in counter-clockwise degrees.
227  */
rotate(float angle)228 void Fl_Paged_Device::rotate(float angle) {}
229 
230 /**
231  @brief To be called at the end of each page.
232 
233  @return 0 if OK, non-zero if any error.
234  */
end_page(void)235 int Fl_Paged_Device::end_page (void) {return 1;}
236 
237 /**
238  @brief To be called at the end of a print job.
239  */
end_job(void)240 void Fl_Paged_Device::end_job (void) {}
241 
242 /**
243  @brief Translates the current graphics origin accounting for the current rotation.
244 
245  This function is only useful after a rotate() call.
246  Each translate() call must be matched by an untranslate() call.
247  Successive translate() calls add up their effects.
248  */
translate(int x,int y)249 void Fl_Paged_Device::translate(int x, int y) {}
250 
251 /**
252  @brief Undoes the effect of a previous translate() call.
253  */
untranslate(void)254 void Fl_Paged_Device::untranslate(void) {}
255 
256 const Fl_Paged_Device::page_format Fl_Paged_Device::page_formats[NO_PAGE_FORMATS] = {
257   // order of enum Page_Format
258   // comes from appendix B of 5003.PPD_Spec_v4.3.pdf
259 
260   // A* // index(Ai) = i
261   {2384, 3370, "A0"},
262   {1684, 2384, "A1"},
263   {1191, 1684, "A2"},
264   { 842, 1191, "A3"},
265   { 595,  842, "A4"},
266   { 420,  595, "A5"},
267   { 297,  420, "A6"},
268   { 210,  297, "A7"},
269   { 148,  210, "A8"},
270   { 105,  148, "A9"},
271 
272   // B* // index(Bi) = i+10
273   {2920, 4127, "B0"},
274   {2064, 2920, "B1"},
275   {1460, 2064, "B2"},
276   {1032, 1460, "B3"},
277   { 729, 1032, "B4"},
278   { 516,  729, "B5"},
279   { 363,  516, "B6"},
280   { 258,  363, "B7"},
281   { 181,  258, "B8"},
282   { 127,  181, "B9"},
283   {  91,  127, "B10"},
284 
285   // others
286   { 459,  649, "EnvC5"}, // envelope
287   { 312,  624, "EnvDL"}, // envelope
288   { 522,  756, "Executive"},
289   { 595,  935, "Folio"},
290   {1224,  792, "Ledger"}, // landscape
291   { 612, 1008, "Legal"},
292   { 612,  792, "Letter"},
293   { 792, 1224, "Tabloid"},
294   { 297,  684, "Env10"} // envelope
295 };
296 
297 //
298 // End of "$Id$".
299 //
300