TDE graphics utilities
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.

kpswidget.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /**
  2. * Copyright (C) 2000-2003 the KGhostView authors. See file AUTHORS.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #ifndef __KPSWIDGET_H__
  19. #define __KPSWIDGET_H__
  20. #include <queue>
  21. #include <tqstring.h>
  22. #include <tqstringlist.h>
  23. #include <tqwidget.h>
  24. #include <tqpixmap.h>
  25. #include "dscparse_adapter.h"
  26. #include "configuration.h"
  27. #include <X11/X.h>
  28. class TDEProcess;
  29. class KGVConfigDialog;
  30. class MessagesDialog;
  31. /**
  32. * @class KPSWidget
  33. *
  34. * @brief KPSWidget is a widget on which Ghostscript can render.
  35. *
  36. * @ref ghostscript_interface
  37. */
  38. class KPSWidget : public TQWidget
  39. {
  40. Q_OBJECT
  41. public:
  42. KPSWidget( TQWidget* parent = 0, const char* name = 0 );
  43. ~KPSWidget();
  44. /**
  45. * Start up the Ghostscript interpreter. Returns true if Ghostscript
  46. * could be started; otherwise false.
  47. */
  48. bool startInterpreter();
  49. /**
  50. * Stop the interpreter process.
  51. */
  52. void stopInterpreter();
  53. /**
  54. * Returns true if the interpreter is ready for new input.
  55. */
  56. bool isInterpreterReady() const;
  57. bool isInterpreterBusy() const;
  58. /**
  59. * Returns true if the interpreter is running; otherwise returns false.
  60. */
  61. bool isInterpreterRunning() const;
  62. /**
  63. * Tell ghostscript to start the next page.
  64. * Returns false if ghostscript is not running, or not ready to start
  65. * another page. If another page is started, sets the _interpreterReady
  66. * flag and cursor.
  67. */
  68. bool nextPage();
  69. /**
  70. * Queue a portion of a PostScript file for output to ghostscript and
  71. * start processing the queue.
  72. *
  73. * fp: FILE* of the file in question. Should be seek()able.
  74. * begin: position in file to start.
  75. * len: number of bytes to write.
  76. *
  77. * If an interpreter is not running, nothing is queued and false is
  78. * returned.
  79. */
  80. bool sendPS( FILE*, unsigned int begin, unsigned int end );
  81. /**
  82. * Sets the filename of the ghostscript input.
  83. * @p usePipe indicates whether we use a pipe for
  84. * communication or let ghoscript read the file itself.
  85. */
  86. void setFileName( const TQString&, bool usePipe );
  87. /**
  88. * Set the bounding box of the drawable. See my comment in the source
  89. * file.
  90. */
  91. void setBoundingBox( const KDSCBBOX& );
  92. /**
  93. * Set the orientation of the page.
  94. */
  95. void setOrientation( CDSC_ORIENTATION_ENUM );
  96. /**
  97. * Sets the resolution according to the physical resolution of the screen
  98. * and the magnification value.
  99. */
  100. void setMagnification( double magnification );
  101. /**
  102. * @return the boundingbox of the drawable.
  103. */
  104. const KDSCBBOX& boundingBox() const;
  105. /**
  106. * @return the current orientation.
  107. */
  108. CDSC_ORIENTATION_ENUM orientation() const;
  109. /**
  110. * Double buffering means that all the drawing is done outside the
  111. * screen and the finished picture is then flashed to the screen.
  112. * This reduces flicker ( to almost none ) at the price of speed.
  113. *
  114. * By default, KPSWidget is *not* double buffered.
  115. */
  116. bool isDoubleBuffered() const { return _doubleBuffer; }
  117. void setDoubleBuffering( bool n );
  118. void clear();
  119. public slots:
  120. /**
  121. * Call this when the settings have changed.
  122. */
  123. void readSettings();
  124. signals:
  125. /**
  126. * This signal gets emited whenever a page is finished, but contains a reference to the pixmap
  127. * used to hold the image.
  128. *
  129. * Don't change the pixmap or bad things will happen. This is the backing pixmap of the display.
  130. */
  131. void newPageImage( TQPixmap image );
  132. /**
  133. * This signal is emitted whenever the ghostscript process has
  134. * written data to stdout or stderr.
  135. */
  136. void output( char* data, int len );
  137. /**
  138. * This means that gs exited uncleanly
  139. *
  140. * @param msg a <strong>translated</strong> error message to display the user which may be null if we cannot tell anything important
  141. */
  142. void ghostscriptError( const TQString& mgs );
  143. protected:
  144. struct Record
  145. {
  146. Record( FILE* fp_, long begin_, unsigned len_ )
  147. :fp( fp_ ), begin( begin_ ), len( len_ ) { }
  148. FILE* fp;
  149. long begin;
  150. unsigned int len;
  151. };
  152. // void resizeEvent( TQResizeEvent* );
  153. bool x11Event( XEvent* );
  154. /**
  155. * Setup the widget according to the current settings for the
  156. * boundingBox, the resolution and the orientation. This involves
  157. * the following things:
  158. * - Resize the widget
  159. * - Resize and clear the background pixmap.
  160. * - Setup the GHOSTVIEW and GHOSTVIEW_COLORS properties.
  161. *
  162. * Make sure ghostscript isn't running when calling this method.
  163. */
  164. void setupWidget();
  165. void setGhostscriptPath( const TQString& );
  166. void setGhostscriptArguments( const TQStringList& );
  167. void setPalette( Configuration::EnumPalette::type );
  168. protected slots:
  169. void gs_input( TDEProcess* );
  170. void gs_output( TDEProcess*, char* buffer, int len );
  171. void interpreterFailed();
  172. void slotProcessExited( TDEProcess* );
  173. private:
  174. Window _gsWindow; // Destination of ghostscript messages.
  175. enum AtomName { GHOSTVIEW = 0, GHOSTVIEW_COLORS, NEXT, PAGE, DONE };
  176. Atom _atoms[5];
  177. TQPixmap _backgroundPixmap;
  178. /**
  179. * The following properties determine how Ghostscript is started.
  180. * If any of these is changed, Ghostscript needs to be restarted.
  181. */
  182. TQString _ghostscriptPath;
  183. TQStringList _ghostscriptArguments;
  184. TQString _fileName;
  185. bool _usePipe;
  186. bool _doubleBuffer;
  187. /**
  188. * Flag set when one of the properties _ghostscriptPath,
  189. * _ghostscriptArguments or _fileName has been changed.
  190. */
  191. bool _ghostscriptDirty;
  192. /**
  193. * The following properties determine how Ghostscript renders its
  194. * pages. If any of these is changed, the widget needs to be setup,
  195. * and Ghostscript needs to be restarted.
  196. */
  197. CDSC_ORIENTATION_ENUM _orientation;
  198. KDSCBBOX _boundingBox;
  199. float _magnification;
  200. Configuration::EnumPalette::type _palette;
  201. /**
  202. * Flag set when one of the properties _orientation, _boundingBox,
  203. * _dpi[X|Y] or _palette has been changed.
  204. */
  205. bool _widgetDirty;
  206. TDEProcess* _process;
  207. char* _buffer;
  208. std::queue<Record> _inputQueue;
  209. bool _stdinReady;
  210. bool _interpreterBusy;
  211. bool _interpreterReady;
  212. };
  213. inline const KDSCBBOX& KPSWidget::boundingBox() const
  214. {
  215. return _boundingBox;
  216. }
  217. inline CDSC_ORIENTATION_ENUM KPSWidget::orientation() const
  218. {
  219. return _orientation;
  220. }
  221. /**
  222. * @page ghostview_interface Ghostview interface to Ghostscript
  223. *
  224. * When the GHOSTVIEW environment variable is set, Ghostscript draws on
  225. * an existing drawable rather than creating its own window. Ghostscript
  226. * can be directed to draw on either a window or a pixmap.
  227. *
  228. *
  229. * @section window Drawing on a Window
  230. *
  231. * The GHOSTVIEW environment variable contains the window id of the target
  232. * window. The window id is an integer. Ghostscript will use the attributes
  233. * of the window to obtain the width, height, colormap, screen, and visual of
  234. * the window. The remainder of the information is gotten from the GHOSTVIEW
  235. * property on that window.
  236. *
  237. *
  238. * @section pixmap Drawing on a Pixmap
  239. *
  240. * The GHOSTVIEW environment variable contains a window id and a pixmap id.
  241. * They are integers separated by white space. Ghostscript will use the
  242. * attributes of the window to obtain the colormap, screen, and visual to use.
  243. * The width and height will be obtained from the pixmap. The remainder of the
  244. * information, is gotten from the GHOSTVIEW property on the window. In this
  245. * case, the property is deleted when read.
  246. *
  247. *
  248. * @section gv_variable The GHOSTVIEW environment variable
  249. *
  250. * @par parameters:
  251. * <tt> window-id [pixmap-id] </tt>
  252. *
  253. * @par scanf format:
  254. * @code "%d %d" @endcode
  255. *
  256. * @par Explanation of the parameters
  257. * @li @e window-id:
  258. * tells Ghostscript where to:
  259. * - read the GHOSTVIEW property
  260. * - send events
  261. * If pixmap-id is not present, Ghostscript will draw on this window.
  262. *
  263. * @li @e pixmap-id:
  264. * If present, tells Ghostscript that a pixmap will be used as the
  265. * final destination for drawing. The window will not be touched for
  266. * drawing purposes.
  267. *
  268. *
  269. * @section gv_property The GHOSTVIEW property
  270. *
  271. * @par type:
  272. * STRING
  273. *
  274. * @par parameters:
  275. * <tt> bpixmap orient llx lly urx ury xdpi ydpi [left bottom top right]
  276. * </tt>
  277. *
  278. * @par scanf format:
  279. * @code "%d %d %d %d %d %d %f %f %d %d %d %d" @endcode
  280. *
  281. * @par Explanation of the parameters
  282. * @li @e bpixmap:
  283. * pixmap id of the backing pixmap for the window. If no pixmap is to
  284. * be used, this parameter should be zero. This parameter must be zero
  285. * when drawing on a pixmap.
  286. *
  287. * @li <em>orient:</em>
  288. * orientation of the page. The number represents clockwise rotation
  289. * of the paper in degrees. Permitted values are 0, 90, 180, 270.
  290. *
  291. * @li <em>llx, lly, urx, ury:</em>
  292. * Bounding box of the drawable. The bounding box is specified in
  293. * PostScript points in default user coordinates. (Note the word
  294. * @e drawable. This means that this bounding box is generally not
  295. * the same as the BoundingBox specified in the DSC. In case
  296. * DocumentMedia is specified, it is equal to the Media's bounding
  297. * box.)
  298. *
  299. * @li <em>xdpi, ydpi:</em>
  300. * Resolution of window. (This can be derived from the other
  301. * parameters, but not without roundoff error. These values are
  302. * included to avoid this error.)
  303. *
  304. * @li <em>left, bottom, top, right (optional):</em>
  305. * Margins around the window. The margins extend the imageable area
  306. * beyond the boundaries of the window. This is primarily used for
  307. * popup zoom windows. I have encountered several instances of
  308. * PostScript programs that position themselves with respect to the
  309. * imageable area. The margins are specified in PostScript points.
  310. * If omitted, the margins are assumed to be 0.
  311. *
  312. *
  313. * @section events Events from Ghostscript
  314. *
  315. * If the final destination is a pixmap, the client will get a property
  316. * notify event when Ghostscript reads the GHOSTVIEW property causing it to
  317. * be deleted.
  318. *
  319. * Ghostscript sends events to the window where it read the GHOSTVIEW
  320. * property. These events are of type ClientMessage. The message_type is set
  321. * to either PAGE or DONE. The first long data value gives the window to be
  322. * used to send replies to Ghostscript. The second long data value gives the
  323. * primary drawable. If rendering to a pixmap, it is the primary drawable.
  324. * If rendering to a window, the backing pixmap is the primary drawable. If
  325. * no backing pixmap is employed, then the window is the primary drawable.
  326. * This field is necessary to distinguish multiple Ghostscripts rendering to
  327. * separate pixmaps where the GHOSTVIEW property was placed on the same
  328. * window.
  329. *
  330. * The PAGE message indicates that a "page" has completed. Ghostscript will
  331. * wait until it receives a ClientMessage whose message_type is NEXT before
  332. * continuing.
  333. *
  334. * The DONE message indicates that Ghostscript has finished processing.
  335. */
  336. #endif // __KPSWIDGET_H__
  337. // vim:sw=4:sts=4:ts=8:noet