printJob.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: printJob.c /main/29 1997/08/01 14:32:04 samborn $ */
  24. /**********************************<+>*************************************
  25. ***************************************************************************
  26. **
  27. ** File: printJob.c
  28. **
  29. ** Project: HP DT dtpad, a memo maker type editor based on the
  30. ** Dt Editor widget.
  31. **
  32. ** Description: Routines which manipulate the print setup dialog
  33. **
  34. **************************************************************************
  35. **********************************<+>*************************************/
  36. /*
  37. * Common Desktop Environment
  38. *
  39. * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
  40. * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
  41. * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
  42. * (c) Copyright 1993, 1994, 1995 Novell, Inc.
  43. * (c) Copyright 1995 Digital Equipment Corp.
  44. * (c) Copyright 1995 Fujitsu Limited
  45. * (c) Copyright 1995 Hitachi, Ltd.
  46. *
  47. *
  48. * RESTRICTED RIGHTS LEGEND
  49. *
  50. *Use, duplication, or disclosure by the U.S. Government is subject to
  51. *restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  52. *Technical Data and Computer Software clause in DFARS 252.227-7013. Rights
  53. *for non-DOD U.S. Government Departments and Agencies are as set forth in
  54. *FAR 52.227-19(c)(1,2).
  55. *Hewlett-Packard Company, 3000 Hanover Street, Palo Alto, CA 94304 U.S.A.
  56. *International Business Machines Corp., Route 100, Somers, NY 10589 U.S.A.
  57. *Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, CA 94043 U.S.A.
  58. *Novell, Inc., 190 River Road, Summit, NJ 07901 U.S.A.
  59. *Digital Equipment Corp., 111 Powdermill Road, Maynard, MA 01754, U.S.A.
  60. *Fujitsu Limited, 1015, Kamikodanaka Nakahara-Ku, Kawasaki 211, Japan
  61. *Hitachi, Ltd., 6, Kanda Surugadai 4-Chome, Chiyoda-ku, Tokyo 101, Japan
  62. */
  63. #include <errno.h>
  64. #include <pwd.h>
  65. #include <stdio.h>
  66. #include <string.h>
  67. #include <sys/types.h>
  68. #include <time.h>
  69. #include <X11/Intrinsic.h>
  70. #include <X11/Shell.h>
  71. #include <Xm/Xm.h>
  72. #include <Xm/DialogS.h>
  73. #include <Xm/Form.h>
  74. #include <Xm/Label.h>
  75. #if 0 && defined(PRINTING_SUPPORTED)
  76. #include <Xm/Print.h>
  77. #endif /* PRINTING_SUPPORTED */
  78. #include <Dt/Editor.h>
  79. #include <Dt/Print.h>
  80. #include "dtpad.h"
  81. static void _pjCreatePrintShell(PrintJob *pJob);
  82. static void _pjCreateOutputWidgets(PrintJob *pJob);
  83. static void _pjDoPrint(PrintJob*);
  84. static void _pjUpdatePageHeaders(
  85. PrintJob*,
  86. PrintStringTypeEnum,
  87. PrintStringTypeEnum,
  88. PrintStringTypeEnum,
  89. PrintStringTypeEnum);
  90. #if 0 && defined(PRINTING_SUPPORTED)
  91. static void _pjFinishedPrintToFile(
  92. Display*,
  93. XPContext,
  94. XPGetDocStatus,
  95. XPointer);
  96. #endif /* PRINTING_SUPPORTED */
  97. static char * _pjGetPageHeaderString(PrintJob*, PrintStringTypeEnum);
  98. static void _pjCancelCB (Widget, XtPointer client_data, XtPointer);
  99. static void _pjCloseDisplayCB (Widget, XtPointer client_data, XtPointer);
  100. static void _pjPdmSetupCB (Widget, XtPointer client_data, XtPointer);
  101. static void _pjPdmNotificationCB (Widget, XtPointer client_data, XtPointer);
  102. static void _pjPrintCB (Widget, XtPointer client_data, XtPointer);
  103. static void _pjPrintOnePageCB(Widget, XtPointer, XtPointer);
  104. static void _pjRegisterActivePrintDisplay(Display*);
  105. static void _pjUnregisterActivePrintDisplay(Display*);
  106. static Display *_pjErrorPrintDisplay = NULL;
  107. static Display **_pjActivePrintDisplay = NULL;
  108. static int _pjMaxActivePrintDisplay = 0;
  109. /************************************************************************
  110. * PrintJobCreate
  111. * Creates a new print job for the specified document file.
  112. ************************************************************************/
  113. PrintJob *
  114. PrintJobCreate(
  115. char *documentName,
  116. char *tempFileName,
  117. Boolean silent,
  118. Editor *pPad
  119. )
  120. {
  121. PrintJob *pJob;
  122. pJob = (PrintJob *) XtMalloc( sizeof(PrintJob) );
  123. memset(pJob, 0, sizeof(PrintJob));
  124. pJob->pPad = pPad;
  125. pJob->parentShell = pPad->app_shell;
  126. pJob->documentName = strdup(documentName);
  127. pJob->tempFileName = strdup(tempFileName);
  128. pJob->silent = silent;
  129. pJob->pOutput = NULL;
  130. pJob->pSetup = NULL;
  131. pJob->pShell = NULL;
  132. pJob->printData = (DtPrintSetupData*) XtMalloc(sizeof(DtPrintSetupData));
  133. memset(pJob->printData, 0, sizeof(DtPrintSetupData));
  134. pJob->npagesDone = 0;
  135. pJob->npagesTotal = 0;
  136. pJob->nextpageShell = NULL;
  137. pJob->nextpageButton = NULL;
  138. return pJob;
  139. }
  140. /************************************************************************
  141. * PrintJobDestroy
  142. * Destroys the specified PrintJob.
  143. ************************************************************************/
  144. void
  145. PrintJobDestroy(PrintJob *pJob)
  146. {
  147. if (pJob == NULL)
  148. return;
  149. if (pJob->pPad)
  150. {
  151. pJob->pPad->numPendingTasks--;
  152. ClearStatusMessage(pJob->pPad);
  153. XtSetSensitive(pJob->pPad->fileStuff.fileWidgets.printBtn, True);
  154. /*
  155. XtSetSensitive(pJob->pPad->fileStuff.fileWidgets.silentPrintBtn, True);
  156. */
  157. }
  158. if (pJob->documentName != NULL)
  159. {
  160. free((char*) pJob->documentName);
  161. pJob->documentName = NULL;
  162. }
  163. if (pJob->tempFileName != NULL)
  164. {
  165. free((char*) pJob->tempFileName);
  166. pJob->tempFileName = NULL;
  167. }
  168. if (pJob->pOutput != NULL)
  169. {
  170. PrintOutputDestroy(pJob->pOutput);
  171. pJob->pOutput = NULL;
  172. }
  173. if (pJob->pShell != NULL)
  174. {
  175. _pjRegisterActivePrintDisplay(XtDisplay(pJob->pShell));
  176. if (XtDisplay(pJob->pShell) == PrintJobGetErrorPrintDisplay())
  177. {
  178. char *errMsg;
  179. /*
  180. * Need to display an error dialog;
  181. */
  182. errMsg =
  183. GETMESSAGE(
  184. 14, 24,
  185. "The X Print Server is temporarily out of resources.");
  186. Warning(pJob->pPad, (char *) errMsg, XmDIALOG_ERROR);
  187. PrintJobSetErrorPrintDisplay(NULL);
  188. }
  189. XtDestroyWidget(pJob->pShell);
  190. pJob->pShell = NULL;
  191. }
  192. if (pJob->pSetup != NULL)
  193. {
  194. PrintSetupDestroy(pJob->pSetup);
  195. pJob->pSetup = NULL;
  196. }
  197. if (pJob->printData)
  198. {
  199. DtPrintFreeSetupData(pJob->printData);
  200. XtFree((XtPointer) pJob->printData);
  201. pJob->printData = NULL;
  202. }
  203. if (pJob->nextpageShell)
  204. {
  205. XtDestroyWidget(pJob->nextpageShell);
  206. pJob->nextpageShell = NULL;
  207. }
  208. XtFree((char *) pJob);
  209. }
  210. /************************************************************************
  211. * PrintJobExecute
  212. * Executes the specified PrintJob
  213. ************************************************************************/
  214. void
  215. PrintJobExecute(PrintJob *pJob)
  216. {
  217. if (pJob == NULL) return;
  218. pJob->pSetup = PrintSetupCreate(
  219. pJob->parentShell,
  220. pJob->documentName,
  221. pJob->pPad->xrdb.wordWrap,
  222. pJob->pPad,
  223. (XtCallbackProc) _pjCancelCB, pJob,
  224. (XtCallbackProc) _pjCloseDisplayCB, pJob,
  225. (XtCallbackProc) _pjPrintCB, pJob,
  226. (XtCallbackProc) _pjPdmSetupCB, pJob);
  227. if (pJob->silent)
  228. {
  229. /*
  230. * The DtPrintSetupDialog will display itself automatically
  231. * along with an error message in the event of an error.
  232. */
  233. if (FALSE == PrintSetupGetDefaultPrintData(
  234. pJob->pSetup,
  235. pJob->printData))
  236. return;
  237. _pjCreatePrintShell(pJob);
  238. _pjDoPrint(pJob);
  239. }
  240. else
  241. PrintSetupDisplay(pJob->pSetup);
  242. }
  243. /************************************************************************
  244. * PrintJobCancel
  245. * Cancels and deletes the specified print job.
  246. ************************************************************************/
  247. void
  248. PrintJobCancel(PrintJob *pJob)
  249. {
  250. PrintJobDestroy(pJob);
  251. }
  252. /************************************************************************
  253. * PrintJobGetErrorPrintDisplay
  254. * Returns the last print display on which an error occurred.
  255. ************************************************************************/
  256. Display *
  257. PrintJobGetErrorPrintDisplay(void)
  258. {
  259. return _pjErrorPrintDisplay;
  260. }
  261. /************************************************************************
  262. * PrintJobSetErrorPrintDisplay
  263. * Save a pointer to the print display on which an error occurred.
  264. ************************************************************************/
  265. void
  266. PrintJobSetErrorPrintDisplay(Display *display)
  267. {
  268. _pjErrorPrintDisplay = display;
  269. }
  270. /************************************************************************
  271. * PrintJobIsActivePrintDisplay
  272. * Save a pointer to the print display on which an error occurred.
  273. ************************************************************************/
  274. Boolean
  275. PrintJobIsActivePrintDisplay(Display *display)
  276. {
  277. int i;
  278. for (i = 0; i < _pjMaxActivePrintDisplay; i++)
  279. if (display == _pjActivePrintDisplay[i])
  280. return True;
  281. return False;
  282. }
  283. /************************************************************************
  284. * _pjRegisterActivePrintDisplay
  285. * Save the Display pointer for an active print display connection
  286. ************************************************************************/
  287. static void
  288. _pjRegisterActivePrintDisplay(Display *display)
  289. {
  290. size_t size;
  291. int i;
  292. if (0 == _pjMaxActivePrintDisplay)
  293. {
  294. _pjMaxActivePrintDisplay = 10;
  295. size = _pjMaxActivePrintDisplay * sizeof(Display*);
  296. _pjActivePrintDisplay = (Display**) malloc(size);
  297. memset((char*) _pjActivePrintDisplay, 0, size);
  298. }
  299. for (i = 0; i < _pjMaxActivePrintDisplay; i++)
  300. {
  301. if (NULL == _pjActivePrintDisplay[i])
  302. {
  303. _pjActivePrintDisplay[i] = display;
  304. return;
  305. }
  306. }
  307. size = _pjMaxActivePrintDisplay * sizeof(Display*);
  308. _pjActivePrintDisplay =
  309. (Display**) realloc((void*) _pjActivePrintDisplay, 2*size);
  310. memset((char*) (_pjActivePrintDisplay + size), 0, size);
  311. _pjActivePrintDisplay[_pjMaxActivePrintDisplay] = display;
  312. _pjMaxActivePrintDisplay *= 2;
  313. }
  314. /************************************************************************
  315. * _pjUnregisterActivePrintDisplay
  316. * Delete the Display pointer for an active print display connection
  317. ************************************************************************/
  318. static void
  319. _pjUnregisterActivePrintDisplay(Display *display)
  320. {
  321. int i;
  322. for (i = 0; i < _pjMaxActivePrintDisplay; i++)
  323. if (display == _pjActivePrintDisplay[i])
  324. _pjActivePrintDisplay[i] = NULL;
  325. }
  326. /************************************************************************
  327. * _pjCreatePrintShell
  328. * Creates the print shell (XmPrintShell or XmDialogShell) to control
  329. * printing.
  330. ************************************************************************/
  331. static void
  332. _pjCreatePrintShell(PrintJob *pJob)
  333. {
  334. DtPrintSetupData *psd = NULL;
  335. if (pJob == NULL ||
  336. pJob->pShell != NULL ||
  337. pJob->parentShell == NULL ||
  338. pJob->pSetup == NULL) return;
  339. #if 0 && defined(PRINTING_SUPPORTED)
  340. /*
  341. * Create the print shell and
  342. * the print output widgets
  343. */
  344. #ifdef PRINT_TO_VIDEO
  345. /*
  346. * Create a dialog shell widget on the video display.
  347. */
  348. pJob->pShell = XmCreateDialogShell(
  349. pJob->parentShell,
  350. "PrintVideo",
  351. NULL, 0);
  352. {
  353. XmString xms;
  354. pJob->nextpageShell = XmCreateDialogShell(
  355. pJob->parentShell,
  356. "PrintNextPage",
  357. NULL, 0);
  358. xms = XmStringCreateLocalized("Push for Next Page");
  359. pJob->nextpageButton = XtVaCreateManagedWidget(
  360. "NextPageButton",
  361. xmPushButtonWidgetClass,
  362. pJob->nextpageShell,
  363. XmNlabelString, xms,
  364. XmNwidth, 200,
  365. XmNheight, 200,
  366. NULL);
  367. XmStringFree(xms);
  368. XtAddCallback(
  369. pJob->nextpageButton,
  370. XmNactivateCallback,
  371. _pjPrintOnePageCB,
  372. (XtPointer) pJob);
  373. XtManageChild(pJob->nextpageShell);
  374. XtRealizeWidget(pJob->nextpageShell);
  375. }
  376. #else
  377. /*
  378. * Create an XmPrintShell widget on the print display.
  379. */
  380. psd = pJob->printData;
  381. if (psd != NULL)
  382. pJob->pShell = XmPrintSetup(
  383. pJob->parentShell,
  384. XpGetScreenOfContext(psd->print_display, psd->print_context),
  385. "Print",
  386. NULL, 0);
  387. XtAddCallback(
  388. pJob->pShell,
  389. XmNpageSetupCallback,
  390. _pjPrintOnePageCB,
  391. (XtPointer) pJob);
  392. XtAddCallback(
  393. pJob->pShell,
  394. XmNpdmNotificationCallback,
  395. _pjPdmNotificationCB,
  396. (XtPointer) pJob);
  397. #endif
  398. if (pJob->pShell == NULL) return;
  399. #endif /* PRINTING_SUPPORTED */
  400. }
  401. /************************************************************************
  402. * _pjCreateOutputWidgets
  403. * Actually run the specified PrintJob.
  404. * Creates the PrintOutput object to be used for printing.
  405. * Initializes the mailbox to iterate through the messages.
  406. * Calls spoolOne to send a print job to the Xp server.
  407. ************************************************************************/
  408. static void
  409. _pjCreateOutputWidgets(PrintJob *pJob)
  410. {
  411. DtEditorErrorCode errorCode;
  412. Boolean parseError;
  413. DtPrintSetupData *psd = NULL;
  414. int save_data;
  415. /*
  416. * Notify the user that we're printing
  417. */
  418. /* TBD: Is there a status bar on pPad? */
  419. if (pJob->pShell == NULL)
  420. {
  421. {/* TBD error dialog */}
  422. PrintJobDestroy(pJob);
  423. return;
  424. }
  425. pJob->pOutput = PrintOutputCreate(pJob->pShell);
  426. PrintOutputSetWordWrap(pJob->pOutput, PrintSetupUseWordWrap(pJob->pSetup));
  427. PrintOutputSetPageMargins(
  428. pJob->pOutput,
  429. PrintSetupGetMarginSpec(pJob->pSetup, DTPRINT_OPTION_MARGIN_TOP),
  430. PrintSetupGetMarginSpec(pJob->pSetup, DTPRINT_OPTION_MARGIN_RIGHT),
  431. PrintSetupGetMarginSpec(pJob->pSetup, DTPRINT_OPTION_MARGIN_BOTTOM),
  432. PrintSetupGetMarginSpec(pJob->pSetup, DTPRINT_OPTION_MARGIN_LEFT),
  433. &parseError
  434. );
  435. if (parseError)
  436. {
  437. /*
  438. * Display an error dialog.
  439. DtMailGenDialog *genDialog = new DtMailGenDialog(
  440. "Dialog",
  441. _parent->baseWidget());
  442. */
  443. char *i18nMsg;
  444. char *errMsg;
  445. i18nMsg = GETMESSAGE(
  446. 14, 1,
  447. "One of the following margin specifiers \n has incorrect syntax: \n %s \n %s \n %s \n %s \nContinue using default margins?"
  448. );
  449. errMsg = (char *) XtMalloc(1024);
  450. sprintf(
  451. errMsg,
  452. i18nMsg,
  453. PrintSetupGetMarginSpec(pJob->pSetup, DTPRINT_OPTION_MARGIN_TOP),
  454. PrintSetupGetMarginSpec(pJob->pSetup, DTPRINT_OPTION_MARGIN_RIGHT),
  455. PrintSetupGetMarginSpec(pJob->pSetup, DTPRINT_OPTION_MARGIN_BOTTOM),
  456. PrintSetupGetMarginSpec(pJob->pSetup, DTPRINT_OPTION_MARGIN_LEFT)
  457. );
  458. Warning(pJob->pPad, (char *) errMsg, XmDIALOG_WARNING);
  459. XtFree(errMsg);
  460. }
  461. /*
  462. * Load the file
  463. */
  464. errorCode = PrintOutputLoadFile(pJob->pOutput, pJob->tempFileName);
  465. switch (errorCode)
  466. {
  467. case DtEDITOR_NO_ERRORS:
  468. case DtEDITOR_READ_ONLY_FILE:
  469. break;
  470. case DtEDITOR_NONEXISTENT_FILE:
  471. Warning(
  472. pJob->pPad,
  473. (char *) GETMESSAGE(14, 2, "File does not exist."),
  474. XmDIALOG_WARNING);
  475. break;
  476. case DtEDITOR_DIRECTORY:
  477. Warning(
  478. pJob->pPad,
  479. (char *) GETMESSAGE(14, 3, "Specified file is a directory."),
  480. XmDIALOG_WARNING);
  481. break;
  482. case DtEDITOR_CHAR_SPECIAL_FILE:
  483. case DtEDITOR_BLOCK_MODE_FILE:
  484. Warning(
  485. pJob->pPad,
  486. (char *) GETMESSAGE(14, 4, "File type error."),
  487. XmDIALOG_WARNING);
  488. break;
  489. case DtEDITOR_NULLS_REMOVED:
  490. Warning(
  491. pJob->pPad,
  492. (char *) GETMESSAGE(14, 5, "File contains NULL characters."),
  493. XmDIALOG_WARNING);
  494. break;
  495. case DtEDITOR_INSUFFICIENT_MEMORY:
  496. Warning(
  497. pJob->pPad,
  498. (char*)
  499. GETMESSAGE(14, 6, "Unable to load file (insufficient memory)."),
  500. XmDIALOG_ERROR);
  501. break;
  502. case DtEDITOR_NO_FILE_ACCESS:
  503. case DtEDITOR_UNREADABLE_FILE:
  504. default:
  505. Warning(
  506. pJob->pPad,
  507. (char *)
  508. GETMESSAGE(14, 7, "File does not have read permissions"),
  509. XmDIALOG_WARNING);
  510. break;
  511. }
  512. XtRealizeWidget( pJob->pShell );
  513. PrintOutputFirstPage(pJob->pOutput);
  514. pJob->npagesTotal = PrintOutputGetNumLines(pJob->pOutput) +
  515. PrintOutputGetLinesPerPage(pJob->pOutput) - 1;
  516. pJob->npagesTotal /= PrintOutputGetLinesPerPage(pJob->pOutput);
  517. pJob->npagesDone = 0;
  518. }
  519. /************************************************************************
  520. * _pjDoPrint
  521. * Actually run the specified PrintJob.
  522. * Creates the PrintOutput object to be used for printing.
  523. * Initializes the mailbox to iterate through the messages.
  524. * Calls spoolOne to send a print job to the Xp server.
  525. ************************************************************************/
  526. static void
  527. _pjDoPrint(PrintJob *pJob)
  528. {
  529. #if 0 && defined(PRINTING_SUPPORTED)
  530. static char buf[1024];
  531. static char *format;
  532. DtPrintSetupData *psd = NULL;
  533. int dest = XPSpool;
  534. /*
  535. * Notify the user that we're printing
  536. */
  537. if (pJob->pPad != NULL) {
  538. pJob->pPad->numPendingTasks++;
  539. format = GETMESSAGE(14, 21, "Printing %s ...");
  540. sprintf(buf, format, pJob->documentName);
  541. SetStatusMessage(pJob->pPad, buf);
  542. XtSetSensitive(pJob->pPad->fileStuff.fileWidgets.printBtn, False);
  543. }
  544. #ifndef PRINT_TO_VIDEO
  545. psd = pJob->printData;
  546. if (psd->destination == DtPRINT_TO_FILE)
  547. dest = XPGetData;
  548. _pjRegisterActivePrintDisplay(XtDisplay(pJob->pShell));
  549. XpStartJob(XtDisplay(pJob->pShell), dest);
  550. XFlush(XtDisplay(pJob->pShell));
  551. if (psd->destination == DtPRINT_TO_FILE)
  552. {
  553. if (FALSE == XmPrintToFile(
  554. XtDisplay(pJob->pShell),
  555. psd->dest_info,
  556. _pjFinishedPrintToFile,
  557. (XPointer) pJob))
  558. {
  559. char *fmt = "%s\n%s: %s";
  560. char *message;
  561. char *appmessage = NULL;
  562. char *sysmessage = strerror(errno);
  563. appmessage = (char *) GETMESSAGE(
  564. 14, 14,
  565. "'Print to File' was unsuccessful.");
  566. if (NULL == sysmessage)
  567. {
  568. message = XtMalloc(strlen(appmessage) + 1);
  569. sprintf(message, "%s", appmessage);
  570. }
  571. else
  572. {
  573. message = XtMalloc(
  574. strlen(appmessage) +
  575. strlen(sysmessage) +
  576. strlen(psd->dest_info) +
  577. strlen(fmt) + 1);
  578. sprintf(message, fmt, appmessage, psd->dest_info, sysmessage);
  579. }
  580. Warning(pJob->pPad, message, XmDIALOG_WARNING);
  581. XtFree(message);
  582. XpCancelJob(XtDisplay(pJob->pShell), False);
  583. PrintJobDestroy(pJob);
  584. }
  585. }
  586. #endif
  587. #endif /* PRINTING_SUPPORTED */
  588. }
  589. /************************************************************************
  590. * _pjUpdatePageHeaders
  591. * Configures the header and footer string in the PrintOutput.
  592. ************************************************************************/
  593. static void
  594. _pjUpdatePageHeaders(
  595. PrintJob *pJob,
  596. PrintStringTypeEnum hl_type,
  597. PrintStringTypeEnum hr_type,
  598. PrintStringTypeEnum fl_type,
  599. PrintStringTypeEnum fr_type
  600. )
  601. {
  602. char *hl_string,
  603. *hr_string,
  604. *fl_string,
  605. *fr_string;
  606. if (pJob == (PrintJob *) NULL) return;
  607. hl_string = _pjGetPageHeaderString(pJob, hl_type);
  608. hr_string = _pjGetPageHeaderString(pJob, hr_type);
  609. fl_string = _pjGetPageHeaderString(pJob, fl_type);
  610. fr_string = _pjGetPageHeaderString(pJob, fr_type);
  611. if (PRINT_NONE_STRING_TYPE_ENUM == hl_type &&
  612. PRINT_NONE_STRING_TYPE_ENUM == hr_type)
  613. PrintOutputHideHeaders(pJob->pOutput);
  614. else
  615. PrintOutputShowHeaders(pJob->pOutput);
  616. if (PRINT_NONE_STRING_TYPE_ENUM == fl_type &&
  617. PRINT_NONE_STRING_TYPE_ENUM == fr_type)
  618. PrintOutputHideFooters(pJob->pOutput);
  619. else
  620. PrintOutputShowFooters(pJob->pOutput);
  621. PrintOutputSetHdrFtrStrings(
  622. pJob->pOutput,
  623. hl_string,
  624. hr_string,
  625. fl_string,
  626. fr_string
  627. );
  628. free(hl_string);
  629. free(hr_string);
  630. free(fl_string);
  631. free(fr_string);
  632. }
  633. /************************************************************************
  634. * _pjGetPageHeaderString
  635. * Returns a header and footer string of the specified type.
  636. ************************************************************************/
  637. static char *
  638. _pjGetPageHeaderString(PrintJob *pJob, PrintStringTypeEnum type)
  639. {
  640. char *format,
  641. *buf = (char *) NULL;
  642. switch (type)
  643. {
  644. case PRINT_NONE_STRING_TYPE_ENUM:
  645. buf = strdup(" ");
  646. break;
  647. case PRINT_DATE_STRING_TYPE_ENUM:
  648. {
  649. time_t clock;
  650. char *date;
  651. clock = time((time_t*) NULL);
  652. date = ctime(&clock);
  653. /* Strip off the trailing newline. */
  654. date[strlen(date)-1] = '\0';
  655. format = GETMESSAGE(14, 8, "Date: %s");
  656. buf = (char *) malloc(strlen(format) + strlen(date) + 1);
  657. if (buf != (char *) NULL)
  658. sprintf(buf, format, date);
  659. }
  660. break;
  661. case PRINT_DOCNAME_STRING_TYPE_ENUM:
  662. {
  663. unsigned buflen;
  664. format = GETMESSAGE(14, 9, "Document: %s");
  665. buflen = strlen(format) + strlen(pJob->documentName) + 1;
  666. buf = (char *) malloc(buflen);
  667. if (buf != (char *) NULL)
  668. sprintf(buf, format, pJob->documentName);
  669. }
  670. break;
  671. case PRINT_PAGE_NUMBER_STRING_TYPE_ENUM:
  672. /*
  673. * Allocate space for the format and the translated page number.
  674. */
  675. {
  676. format = GETMESSAGE(14, 10, "Page %d of %d");
  677. buf = (char *) malloc(strlen(format) + 16);
  678. if (buf != (char *) NULL)
  679. sprintf(buf, format, pJob->npagesDone, pJob->npagesTotal);
  680. }
  681. break;
  682. case PRINT_USER_NAME_STRING_TYPE_ENUM:
  683. /*
  684. * Allocate space for the format and the username.
  685. */
  686. {
  687. struct passwd *pw;
  688. format = GETMESSAGE(14, 11, "Document For: %s");
  689. pw = getpwuid(getuid());
  690. buf = (char *) malloc(strlen(format) + strlen(pw->pw_name) + 1);
  691. if (buf != (char *) NULL)
  692. sprintf(buf, format, pw->pw_name);
  693. }
  694. break;
  695. default:
  696. buf = strdup("DEFAULT not impld");
  697. break;
  698. }
  699. return buf;
  700. }
  701. /*
  702. *
  703. * Name: _pjFinishedPrintToFile
  704. *
  705. * Description:
  706. *
  707. * App-specific print data holder allocate function.
  708. *
  709. */
  710. #if 0 && defined(PRINTING_SUPPORTED)
  711. static void _pjFinishedPrintToFile(
  712. Display *display,
  713. XPContext context,
  714. XPGetDocStatus status,
  715. XPointer client_data)
  716. {
  717. char *message = NULL;
  718. PrintJob *pJob = (PrintJob *) client_data;
  719. Editor *pPad = pJob->pPad;
  720. if (status != XPGetDocFinished)
  721. {
  722. message = (char *) GETMESSAGE(
  723. 14, 14,
  724. "'Print to File' was unsuccessful.");
  725. Warning(pPad, message, XmDIALOG_WARNING);
  726. }
  727. else if (display != PrintJobGetErrorPrintDisplay())
  728. {
  729. message = (char *) GETMESSAGE(
  730. 14, 15,
  731. "'Print to File' completed successfully.");
  732. Warning(pPad, message, XmDIALOG_INFORMATION);
  733. }
  734. PrintJobDestroy(pJob);
  735. }
  736. #endif /* PRINTING_SUPPORTED */
  737. /*
  738. * Name: _pjCancelCB
  739. * Description:
  740. * An XtCallbackProc which can be added to the callback list of
  741. * a widget to cancel the print job passed back as client_data.
  742. */
  743. static void
  744. _pjCancelCB (Widget widget, XtPointer client_data, XtPointer call_data)
  745. {
  746. PrintJob *pJob = (PrintJob *) client_data;
  747. PrintJobDestroy(pJob);
  748. }
  749. /*
  750. * Name: _pjCloseDisplayCB
  751. * Description:
  752. * An XtCallbackProc which can be added to the callback list of
  753. * a widget to cancel the print job passed back as client_data.
  754. */
  755. static void
  756. _pjCloseDisplayCB (Widget widget, XtPointer client_data, XtPointer call_data)
  757. {
  758. PrintJob *pJob = (PrintJob *) client_data;
  759. DtPrintSetupCallbackStruct *pbs = (DtPrintSetupCallbackStruct *) call_data;
  760. if (pJob->pShell != NULL)
  761. {
  762. XtDestroyWidget(pJob->pShell);
  763. pJob->pShell = NULL;
  764. }
  765. DtPrintFreeSetupData(pJob->printData);
  766. }
  767. /*
  768. * Name: _pjPrintCB
  769. * Description:
  770. * An XtCallbackProc which can be added to the callback list of
  771. * a widget to execute the print job passed back as client_data.
  772. */
  773. static void
  774. _pjPrintCB (Widget widget, XtPointer client_data, XtPointer call_data)
  775. {
  776. PrintJob *pJob = (PrintJob *) client_data;
  777. DtPrintSetupCallbackStruct *pbs = (DtPrintSetupCallbackStruct *) call_data;
  778. DtPrintCopySetupData(pJob->printData, pbs->print_data);
  779. _pjCreatePrintShell(pJob);
  780. _pjDoPrint(pJob);
  781. }
  782. /*
  783. * Name: _pjPdmSetupCB
  784. * Description:
  785. * An XtCallbackProc which can be added to the callback list of
  786. * a widget to popup the PDM for the print job.
  787. */
  788. static void
  789. _pjPdmSetupCB(Widget print_setup, XtPointer client_data, XtPointer call_data)
  790. {
  791. #if 0 && defined(PRINTING_SUPPORTED)
  792. char *pname = "_pjPdmSetupCB";
  793. PrintJob *pJob = (PrintJob *) client_data;
  794. DtPrintSetupCallbackStruct
  795. *pbs = (DtPrintSetupCallbackStruct *) call_data;
  796. DtPrintCopySetupData(pJob->printData, pbs->print_data);
  797. _pjCreatePrintShell(pJob);
  798. /* Pop up the PDM */
  799. if (pJob->pShell)
  800. {
  801. Widget shell = print_setup;
  802. while (! (shell == NULL || XtIsShell(shell)) )
  803. shell = XtParent(shell);
  804. if (shell)
  805. XmPrintPopupPDM(pJob->pShell, shell);
  806. else
  807. fprintf(stderr, "Internal Error %s: Missing XmPrintShell.", pname);
  808. }
  809. #endif /* PRINTING_SUPPORTED */
  810. }
  811. /************************************************************************
  812. * _pjPdmNotificationCB
  813. * XmNpdmNotificationCallback for the XmPrintShell
  814. ************************************************************************/
  815. static void
  816. _pjPdmNotificationCB (Widget widget, XtPointer client_data, XtPointer call_data)
  817. {
  818. #if 0 && defined(PRINTING_SUPPORTED)
  819. PrintJob *pJob = (PrintJob*) client_data;
  820. XmPrintShellCallbackStruct *pscbs = (XmPrintShellCallbackStruct*)call_data;
  821. char *message = NULL;
  822. switch (pscbs->reason)
  823. {
  824. case XmCR_PDM_NONE:
  825. case XmCR_PDM_START_ERROR:
  826. case XmCR_PDM_START_VXAUTH:
  827. case XmCR_PDM_START_PXAUTH:
  828. message = (char *) GETMESSAGE(
  829. 14, 25,
  830. "Print Dialog Manager error - setup failed.");
  831. break;
  832. default:
  833. message = NULL;
  834. break;
  835. }
  836. if (message != NULL)
  837. Warning( pJob->pPad, message, XmDIALOG_WARNING);
  838. #endif /* PRINTING_SUPPORTED */
  839. }
  840. /************************************************************************
  841. * _pjPrintOnePageCB
  842. * XmNpageSetupCallback for the XmPrintShell
  843. ************************************************************************/
  844. static void
  845. _pjPrintOnePageCB(
  846. Widget widget,
  847. XtPointer client_data,
  848. XtPointer call_data
  849. )
  850. {
  851. #if 0 && defined(PRINTING_SUPPORTED)
  852. PrintJob *pJob = (PrintJob *) client_data;
  853. XmPrintShellCallbackStruct *pscbs = (XmPrintShellCallbackStruct*)call_data;
  854. int top = 0;
  855. if (pJob->pOutput == NULL)
  856. {
  857. XtArgVal width0, height0;
  858. Dimension width, height;
  859. width = 0; height=0;
  860. XtVaGetValues(
  861. pJob->pShell,
  862. XmNwidth, &width0,
  863. XmNheight, &height0,
  864. NULL);
  865. width = (Dimension)width0;
  866. height = (Dimension)height0;
  867. #if defined(PRINT_TO_VIDEO)
  868. printf("PrintShell in _pjPrintOnePageCB: <W %d - H %d>\n",width,height);
  869. #endif
  870. if (width < 100 || height < 100)
  871. {
  872. width = 2550; height=3250;
  873. XtVaSetValues(
  874. pJob->pShell,
  875. XmNwidth, width,
  876. XmNheight, height,
  877. NULL);
  878. width = 0; height=0;
  879. XtVaGetValues(
  880. pJob->pShell,
  881. XmNwidth, &width,
  882. XmNheight, &height,
  883. NULL);
  884. #if defined(PRINT_TO_VIDEO)
  885. printf(
  886. "PrintShell in _pjPrintOnePageCB: <W %d - H %d>\n",width,height);
  887. #endif
  888. }
  889. _pjCreateOutputWidgets(pJob);
  890. }
  891. #ifdef PRINT_TO_VIDEO
  892. if (pJob->npagesDone > 0 && pJob->npagesDone == pJob->npagesTotal)
  893. #else
  894. if (pscbs->last_page)
  895. #endif
  896. {
  897. DtPrintSetupData *psd = pJob->printData;
  898. /*
  899. * This spool job is done. Clean up.
  900. * If this is a print to file job,
  901. * clean up in the job finished callback.
  902. */
  903. if (psd->destination != DtPRINT_TO_FILE)
  904. PrintJobDestroy(pJob);
  905. return;
  906. }
  907. if (pJob->npagesDone > 0 && PrintOutputPageDown(pJob->pOutput) == FALSE)
  908. {
  909. #ifndef PRINT_TO_VIDEO
  910. pscbs->last_page = TRUE;
  911. #endif
  912. return;
  913. }
  914. /*
  915. * Update header and footer strings.
  916. */
  917. pJob->npagesDone++;
  918. _pjUpdatePageHeaders(
  919. pJob,
  920. PrintSetupGetHdrFtrSpec(pJob->pSetup, DTPRINT_OPTION_HEADER_LEFT),
  921. PrintSetupGetHdrFtrSpec(pJob->pSetup, DTPRINT_OPTION_HEADER_RIGHT),
  922. PrintSetupGetHdrFtrSpec(pJob->pSetup, DTPRINT_OPTION_FOOTER_LEFT),
  923. PrintSetupGetHdrFtrSpec(pJob->pSetup, DTPRINT_OPTION_FOOTER_RIGHT)
  924. );
  925. /*
  926. * Notify the user that we're printing
  927. */
  928. if (pJob->pPad != NULL)
  929. {
  930. static char buf[1024];
  931. char *format;
  932. format = GETMESSAGE(14, 22, "Printing %s: page %d of %d ...");
  933. sprintf(buf, format,
  934. pJob->documentName,
  935. pJob->npagesDone,
  936. pJob->npagesTotal);
  937. SetStatusMessage(pJob->pPad, buf);
  938. }
  939. #ifndef PRINT_TO_VIDEO
  940. if (pJob->npagesDone >= pJob->npagesTotal)
  941. pscbs->last_page = TRUE;
  942. #endif
  943. #endif /* PRINTING_SUPPORTED */
  944. }