You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
9.5KB

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