1 /*---------------------------------------------------------------------------- 2 -- 3 -- Module: xitInfoPrint 4 -- 5 -- Project: xit - X Internal Toolkit 6 -- System: <> 7 -- Subsystem: <> 8 -- Function block: <> 9 -- 10 -- Description: 11 -- Print a section from the info file. 12 -- 13 -- Filename: xitInfoPrint.c 14 -- 15 -- Authors: Roger Larsson, Ulrika Bornetun 16 -- Creation date: 1992-10-17 17 -- 18 -- 19 -- (C) Copyright Ulrika Bornetun, Roger Larsson (1995) 20 -- All rights reserved 21 -- 22 -- Permission to use, copy, modify, and distribute this software and its 23 -- documentation for any purpose and without fee is hereby granted, 24 -- provided that the above copyright notice appear in all copies. Ulrika 25 -- Bornetun and Roger Larsson make no representations about the usability 26 -- of this software for any purpose. It is provided "as is" without express 27 -- or implied warranty. 28 ----------------------------------------------------------------------------*/ 29 30 /* SCCS module identifier. */ 31 static char SCCSID[] = "@(#) Module: xitInfoPrint.c, Version: 1.1, Date: 95/02/18 15:10:37"; 32 33 34 /*---------------------------------------------------------------------------- 35 -- Include files 36 ----------------------------------------------------------------------------*/ 37 38 #include <limits.h> 39 #include <string.h> 40 #include <stdlib.h> 41 #include <stdio.h> 42 #include <ctype.h> 43 #include <sys/types.h> 44 #include <sys/stat.h> 45 #include <sys/wait.h> 46 47 #include <X11/Intrinsic.h> 48 #include <X11/Shell.h> 49 #include <X11/cursorfont.h> 50 51 #include <Xm/Protocols.h> 52 53 #include <Xm/Xm.h> 54 #include <Xm/List.h> 55 #include <Xm/RowColumn.h> 56 #include <Xm/Text.h> 57 #include <Xm/ToggleB.h> 58 59 #include "System.h" 60 #include "Message.h" 61 62 #include "msgTopic.h" 63 #include "xitError.h" 64 #include "xitInfoFile.h" 65 #include "xitTools.h" 66 #include "xitInfoPrint.h" 67 68 69 /*---------------------------------------------------------------------------- 70 -- Macro definitions 71 ----------------------------------------------------------------------------*/ 72 73 /* Local widgets in the print window. */ 74 #define outputLa dataLocalW[ 0 ] 75 #define outputRb dataLocalW[ 1 ] 76 #define outputRc dataLocalW[ 2 ] 77 #define prNameLa dataLocalW[ 3 ] 78 #define prNameTx dataLocalW[ 4 ] 79 #define prSelectRc dataLocalW[ 5 ] 80 #define printerLi dataLocalW[ 6 ] 81 #define whatLa dataLocalW[ 7 ] 82 #define whatRb dataLocalW[ 8 ] 83 #define whatRc dataLocalW[ 9 ] 84 85 86 /*---------------------------------------------------------------------------- 87 -- Type declarations 88 ----------------------------------------------------------------------------*/ 89 90 /* Print info about. */ 91 typedef enum { 92 INFO_SECTION, 93 INFO_CHAPTER, 94 INFO_BOOK 95 } INFO_TYPE; 96 97 /* Output to what? */ 98 typedef enum { 99 TO_PRINTER, 100 TO_FILE 101 } OUTPUT_TYPE; 102 103 104 /* Record while printing entries. */ 105 typedef struct { 106 107 /* Section to use. */ 108 int section_no; 109 110 /* Book to use. */ 111 char book[ 50 ]; 112 113 /* Chapter to use. */ 114 char chapter_id[ 50 ]; 115 116 /* Selected printer. */ 117 char pr_name[ 50 ]; 118 119 /* File where the book is. */ 120 char *book_file; 121 122 /* The printer command to use. */ 123 char *print_script; 124 125 /* The system printers. */ 126 char *system_pr_file; 127 128 /* The user printers. */ 129 char *user_pr_file; 130 131 /* The printer window. */ 132 Widget printerW; 133 134 /* Callback to inform our creator of specific actions. */ 135 void *user_data; 136 XIT_IP_ACTION_CB actionCB; 137 138 } PRINTER_REC, *PRINTER_REC_REF; 139 140 141 /*---------------------------------------------------------------------------- 142 -- Global definitions 143 ----------------------------------------------------------------------------*/ 144 145 /* Name of module. */ 146 static char *module_name = "xitInfoPrint"; 147 148 /* Name of text domain. */ 149 static char *td = "Topic"; 150 151 152 /*---------------------------------------------------------------------------- 153 -- Function prototypes 154 ----------------------------------------------------------------------------*/ 155 156 static void 157 closeCB( Widget widget, 158 PRINTER_REC_REF printer_ref, 159 XtPointer call_data ); 160 161 static Boolean 162 createPrintFile( PRINTER_REC_REF printer_ref, 163 INFO_TYPE print_info, 164 char *file_name ); 165 166 static Widget 167 createPrintWindow( PRINTER_REC_REF printer_ref, 168 Widget parent ); 169 170 static void 171 destroyCB( Widget widget, 172 PRINTER_REC_REF printer_ref, 173 XtPointer call_data ); 174 175 static void 176 loadPrinterList( PRINTER_REC_REF printer_ref ); 177 178 static void 179 printCB( Widget widget, 180 PRINTER_REC_REF printer_ref, 181 XtPointer call_data ); 182 183 static void 184 printerSelectCB( Widget widget, 185 PRINTER_REC_REF printer_ref, 186 XmListCallbackStruct *call_data ); 187 188 static void 189 startPrintJob( PRINTER_REC_REF printer_ref ); 190 191 static char 192 *stripSpaces( char *buffer_ref ); 193 194 195 196 /*---------------------------------------------------------------------------- 197 -- Functions 198 ----------------------------------------------------------------------------*/ 199 200 XIT_IP_HANDLE xitIpInitialize(Widget parent,char * system_pr_file,char * user_pr_file,char * print_script,XIT_IP_ACTION_CB actionCB,void * user_data)201 xitIpInitialize( Widget parent, 202 char *system_pr_file, 203 char *user_pr_file, 204 char *print_script, 205 XIT_IP_ACTION_CB actionCB, 206 void *user_data ) 207 { 208 209 /* Variables. */ 210 PRINTER_REC_REF printer_ref; 211 212 213 /* Code. */ 214 215 /* Create and initialize our private data. */ 216 printer_ref = SysNew( PRINTER_REC ); 217 if( printer_ref == NULL ) 218 return( NULL ); 219 220 printer_ref -> system_pr_file = SysNewString( system_pr_file ); 221 printer_ref -> user_pr_file = SysNewString( user_pr_file ); 222 printer_ref -> print_script = SysNewString( print_script ); 223 printer_ref -> book_file = NULL; 224 printer_ref -> actionCB = actionCB; 225 printer_ref -> user_data = user_data; 226 227 228 229 /* Create the printer window. */ 230 printer_ref -> printerW = createPrintWindow( printer_ref, parent ); 231 232 if( printer_ref -> printerW == NULL ) { 233 SysFree( printer_ref ); 234 235 return( NULL ); 236 } 237 238 239 /* Take the printers and write them into the list. */ 240 loadPrinterList( printer_ref ); 241 242 243 return( (XIT_IP_HANDLE) printer_ref ); 244 245 } /* xitIpInitialize */ 246 247 248 /*----------------------------------------------------------------------*/ 249 250 void xitIpDestroy(XIT_IP_HANDLE printer_handle)251 xitIpDestroy( XIT_IP_HANDLE printer_handle ) 252 { 253 254 /* Variables. */ 255 PRINTER_REC_REF printer_ref; 256 257 258 /* Code. */ 259 260 if( printer_handle == NULL ) 261 return; 262 263 /* Our private data. */ 264 printer_ref = (PRINTER_REC_REF) printer_handle; 265 266 267 /* Destroy the window. */ 268 XtDestroyWidget( printer_ref -> printerW ); 269 270 271 return; 272 273 } /* xitIpDestroy */ 274 275 276 /*----------------------------------------------------------------------*/ 277 278 void xitIpDisplayPrinter(XIT_IP_HANDLE printer_handle,char * book,char * chapter_id,int section_no,char * book_file)279 xitIpDisplayPrinter( XIT_IP_HANDLE printer_handle, 280 char *book, 281 char *chapter_id, 282 int section_no, 283 char *book_file ) 284 { 285 286 /* Variables. */ 287 Widget mainW; 288 PRINTER_REC_REF printer_ref; 289 290 291 /* Code. */ 292 293 printer_ref = (PRINTER_REC_REF) printer_handle; 294 if( printer_ref == NULL ) 295 return; 296 297 mainW = XtNameToWidget( printer_ref -> printerW, "PrintFdFo" ); 298 299 300 /* Save the 'what to print' data. */ 301 strcpy( printer_ref -> book, book ); 302 strcpy( printer_ref -> chapter_id, chapter_id ); 303 304 printer_ref -> section_no = section_no; 305 306 if( printer_ref -> book_file != NULL ) 307 SysFree( printer_ref -> book_file ); 308 printer_ref -> book_file = SysNewString( book_file ); 309 310 311 /* Make sure the window is visible. */ 312 XtManageChild( printer_ref -> printerW ); 313 314 315 return; 316 317 } /* xitIpDisplayPrinter */ 318 319 320 /*----------------------------------------------------------------------*/ 321 322 static Boolean createPrintFile(PRINTER_REC_REF printer_ref,INFO_TYPE print_info,char * file_name)323 createPrintFile( PRINTER_REC_REF printer_ref, 324 INFO_TYPE print_info, 325 char *file_name ) 326 { 327 328 /* Variables. */ 329 int index; 330 char buffer[ 500 ]; 331 char *char_ref; 332 FILE *file_ref; 333 XIT_IF_HANDLE info_handle; 334 XIT_IF_SECTION section_rec; 335 XIT_IF_STATUS status; 336 337 338 /* Code. */ 339 340 /* Initalize the info file. */ 341 info_handle = xitIfInitialize( printer_ref -> book_file ); 342 if( info_handle == NULL ) 343 return( False ); 344 345 file_ref = fopen( file_name, "w" ); 346 if( file_ref == NULL ) 347 return( False ); 348 349 350 /* Print the current section? */ 351 if( print_info == INFO_SECTION ) { 352 353 status = xitIfGetChapter( info_handle, 354 printer_ref -> chapter_id, 355 printer_ref -> section_no, 356 §ion_rec ); 357 358 if( status == XIT_IF_OK && section_rec.link[ 0 ] == '\0' ) { 359 360 /* Print and underline the chapter title. */ 361 char_ref = stripSpaces( section_rec.title ); 362 363 fprintf( file_ref, "\n%s\n", char_ref ); 364 365 for( index = 0; index < strlen( char_ref ); index++ ) 366 fprintf( file_ref, "-" ); 367 fprintf( file_ref, "\n" ); 368 369 /* Read the text lines. */ 370 status = xitIfGetNextTextLine( info_handle, 371 buffer, sizeof( buffer ) ); 372 while( status == XIT_IF_OK ) { 373 fprintf( file_ref, "%s\n", buffer ); 374 375 status = xitIfGetNextTextLine( info_handle, 376 buffer, sizeof( buffer ) ); 377 } 378 379 } /* if */ 380 381 } /* if */ 382 383 384 /* Print the current chapter? */ 385 if( print_info == INFO_CHAPTER ) { 386 387 status = xitIfGetChapter( info_handle, 388 printer_ref -> chapter_id, 1, 389 §ion_rec ); 390 391 while( status == XIT_IF_OK ) { 392 393 if( section_rec.link[ 0 ] == '\0' ) { 394 395 /* Print and underline the chapter title. */ 396 char_ref = stripSpaces( section_rec.title ); 397 398 fprintf( file_ref, "\n%s\n", char_ref ); 399 400 for( index = 0; index < strlen( char_ref ); index++ ) 401 fprintf( file_ref, "-" ); 402 fprintf( file_ref, "\n" ); 403 404 /* Read the text lines. */ 405 status = xitIfGetNextTextLine( info_handle, 406 buffer, sizeof( buffer ) ); 407 while( status == XIT_IF_OK ) { 408 fprintf( file_ref, "%s\n", buffer ); 409 410 status = xitIfGetNextTextLine( info_handle, 411 buffer, sizeof( buffer ) ); 412 } 413 414 } /* if */ 415 416 status = xitIfGetNextSection( info_handle, 417 printer_ref -> chapter_id, §ion_rec ); 418 } /* while */ 419 420 } /* if */ 421 422 423 /* Print the current book? */ 424 if( print_info == INFO_BOOK ) { 425 426 status = xitIfGetChapter( info_handle, "*", 1, 427 §ion_rec ); 428 429 while( status == XIT_IF_OK ) { 430 431 if( section_rec.link[ 0 ] == '\0' ) { 432 433 /* Print and underline the chapter title. */ 434 char_ref = stripSpaces( section_rec.title ); 435 436 fprintf( file_ref, "\n%s\n", char_ref ); 437 438 for( index = 0; index < strlen( char_ref ); index++ ) 439 fprintf( file_ref, "-" ); 440 fprintf( file_ref, "\n" ); 441 442 /* Read the text lines. */ 443 status = xitIfGetNextTextLine( info_handle, 444 buffer, sizeof( buffer ) ); 445 while( status == XIT_IF_OK ) { 446 fprintf( file_ref, "%s\n", buffer ); 447 448 status = xitIfGetNextTextLine( info_handle, 449 buffer, sizeof( buffer ) ); 450 } 451 452 } /* if */ 453 454 status = xitIfGetNextSection( info_handle, "*", §ion_rec ); 455 456 } /* while */ 457 458 } /* if */ 459 460 461 /* Clean up. */ 462 xitIfDestroy( info_handle ); 463 fclose( file_ref ); 464 465 466 return( True ); 467 468 } /* createPrintFile */ 469 470 471 /*----------------------------------------------------------------------*/ 472 473 static Widget createPrintWindow(PRINTER_REC_REF printer_ref,Widget parent)474 createPrintWindow( PRINTER_REC_REF printer_ref, 475 Widget parent ) 476 { 477 478 /* Variables. */ 479 Arg args[ 10 ]; 480 Cardinal n; 481 char *char_ref; 482 Widget dataLocalW[ 10 ]; 483 Widget outputTb[ 2 ]; 484 Widget whatTb[ 3 ]; 485 Widget printFd; 486 Widget workFo; 487 488 static XIT_TEXT_STRUCT text_field_def[] = { 489 { "PrNameTx", NULL, 1, True }, 490 }; 491 492 static XIT_ACTION_AREA_ITEM action_buttons[] = { 493 { "", printCB, NULL }, 494 { NULL, NULL, NULL }, 495 { NULL, NULL, NULL }, 496 { NULL, NULL, NULL }, 497 { "", closeCB, NULL }, 498 }; 499 500 501 /* Code. */ 502 503 /* Get text for menues and buttons. */ 504 action_buttons[ 0 ].label = msgDGetText( td, MINF_DO_PRINT_BUTTON ); 505 action_buttons[ 0 ].data = printer_ref; 506 action_buttons[ 4 ].label = msgDGetText( td, MINF_CLOSE_BUTTON ); 507 action_buttons[ 4 ].data = printer_ref; 508 509 /* Create a form dialog with buttons. */ 510 printFd = xitCreateFormDialog( parent, "PrintFd", 511 1, 0, 512 action_buttons, 513 XtNumber( action_buttons ) ); 514 515 XtAddCallback( printFd, XmNdestroyCallback, 516 (XtCallbackProc) destroyCB, (XtPointer) printer_ref ); 517 518 n = 0; 519 XtSetArg( args[ n ], XmNtitle, msgDGetText( td, MINF_PRINT_TITLE ) ); n++; 520 XtSetValues( XtParent( printFd ), args, n ); 521 522 /* Close the window if this window is deleted. */ 523 { 524 Atom wm_delete_window; 525 526 wm_delete_window = XmInternAtom( XtDisplay( printFd ), 527 "WM_DELETE_WINDOW", False ); 528 529 XmAddWMProtocols( XtParent( printFd ), &wm_delete_window, 1 ); 530 XmAddWMProtocolCallback( XtParent( printFd ), wm_delete_window, 531 (XtCallbackProc) closeCB, 532 (XtPointer) printer_ref ); 533 } /* block */ 534 535 536 /* Container for the contents of the window. */ 537 workFo = XtNameToWidget( printFd, "PrintFdFo" ); 538 539 540 /* Create the list of printers. */ 541 n = 0; 542 XtSetArg( args[ n ], XmNlistSizePolicy, XmCONSTANT ); n++; 543 XtSetArg( args[ n ], XmNscrollBarDisplayPolicy, XmSTATIC ); n++; 544 XtSetArg( args[ n ], XmNselectionPolicy, XmSINGLE_SELECT ); n++; 545 XtSetArg( args[ n ], XmNlistMarginHeight, 5 ); n++; 546 XtSetArg( args[ n ], XmNlistMarginWidth, 5 ); n++; 547 printerLi = XmCreateScrolledList( workFo, "PrinterLi", args, n ); 548 549 XtAddCallback( printerLi, XmNsingleSelectionCallback, 550 (XtCallbackProc) printerSelectCB, (XtPointer) printer_ref ); 551 552 553 /* Create a print select form. */ 554 n = 0; 555 XtSetArg( args[ n ], XmNorientation, XmHORIZONTAL ); n++; 556 XtSetArg( args[ n ], XmNpacking, XmPACK_TIGHT ); n++; 557 prSelectRc = XmCreateRowColumn( workFo, "PrSelectRc", args, n ); 558 559 /* Name of printer. */ 560 prNameLa = xitCreateLabel( prSelectRc, "PrNameLa", 561 msgDGetText( td, MINF_PRINTER_NAME ), -1 ); 562 563 prNameTx = xitCreateTextCols( prSelectRc, &text_field_def[ 0 ], 10 ); 564 565 n = 0; 566 XtSetArg( args[ n ], XmNmaxLength, 50 ); n++; 567 XtSetValues( prNameTx, args, n ); 568 569 570 /* Create the paper format form. */ 571 n = 0; 572 XtSetArg( args[ n ], XmNorientation, XmHORIZONTAL ); n++; 573 whatRc = XmCreateRowColumn( workFo, "WhatRc", args, n ); 574 575 whatLa = xitCreateLabel( whatRc, "WhatLa", 576 msgDGetText( td, MINF_PRINT_WHAT_LABEL ), -1 ); 577 578 n = 0; 579 XtSetArg( args[ n ], XmNorientation, XmHORIZONTAL ); n++; 580 XtSetArg( args[ n ], XmNpacking, XmPACK_COLUMN ); n++; 581 XtSetArg( args[ n ], XmNradioBehavior, True ); n++; 582 whatRb = XmCreateRowColumn( whatRc, "WhatRb", args, n ); 583 584 whatTb[ 0 ] = xitCreateToggleButton( 585 whatRb, "What1Tb", 586 msgDGetText( td, MINF_PRINT_SECTION ), True ); 587 588 whatTb[ 1 ] = xitCreateToggleButton( 589 whatRb, "What2Tb", 590 msgDGetText( td, MINF_PRINT_CHAPTER ), False ); 591 592 whatTb[ 2 ] = xitCreateToggleButton( 593 whatRb, "What3Tb", 594 msgDGetText( td, MINF_PRINT_BOOK ), False ); 595 596 597 /* Create the 'output to' form. */ 598 n = 0; 599 XtSetArg( args[ n ], XmNorientation, XmHORIZONTAL ); n++; 600 outputRc = XmCreateRowColumn( workFo, "OutputRc", args, n ); 601 602 outputLa = xitCreateLabel( outputRc, "OutputLa", 603 msgDGetText( td, MINF_OUTPUT_TO_LABEL ), -1 ); 604 605 n = 0; 606 XtSetArg( args[ n ], XmNorientation, XmHORIZONTAL ); n++; 607 XtSetArg( args[ n ], XmNpacking, XmPACK_COLUMN ); n++; 608 XtSetArg( args[ n ], XmNradioBehavior, True ); n++; 609 outputRb = XmCreateRowColumn( outputRc, "OutputRb", args, n ); 610 611 outputTb[ 0 ] = xitCreateToggleButton( 612 outputRb, "Output1Tb", 613 msgDGetText( td, MINF_OUTPUT_TO_PRINTER ), True ); 614 615 outputTb[ 1 ] = xitCreateToggleButton( 616 outputRb, "Output2Tb", 617 msgDGetText( td, MINF_OUTPUT_TO_FILE ), False ); 618 619 620 /* Put the Parts together. */ 621 xitAttachWidget( XtParent( printerLi ), 622 XmATTACH_FORM, NULL, XmATTACH_FORM, NULL, 623 XmATTACH_NONE, NULL, XmATTACH_NONE, NULL ); 624 xitAttachWidget( prSelectRc, 625 XmATTACH_WIDGET, XtParent( printerLi ), 626 XmATTACH_FORM, NULL, 627 XmATTACH_NONE, NULL, 628 XmATTACH_NONE, NULL ); 629 xitAttachWidget( whatRc, 630 XmATTACH_WIDGET, prSelectRc, XmATTACH_FORM, NULL, 631 XmATTACH_NONE, NULL, XmATTACH_NONE, NULL ); 632 xitAttachWidget( outputRc, 633 XmATTACH_WIDGET, whatRc, XmATTACH_FORM, NULL, 634 XmATTACH_NONE, NULL, XmATTACH_NONE, NULL ); 635 636 637 /* Make sure there is enough space between the children. */ 638 n = 0; 639 XtSetArg( args[ n ], XmNtopOffset, 5 ); n++; 640 XtSetArg( args[ n ], XmNleftOffset, 5 ); n++; 641 XtSetArg( args[ n ], XmNrightOffset, 5 ); n++; 642 XtSetArg( args[ n ], XmNbottomOffset, 5 ); n++; 643 XtSetValues( XtParent( printerLi ), args, n ); 644 XtSetValues( prSelectRc, args, n ); 645 XtSetValues( whatRc, args, n ); 646 XtSetValues( outputRc, args, n ); 647 648 649 /* Manage all the children. */ 650 XtManageChildren( outputTb, XtNumber( outputTb ) ); 651 XtManageChildren( whatTb, XtNumber( whatTb ) ); 652 653 xitManageChildren( dataLocalW, XtNumber( dataLocalW ) ); 654 655 656 /* Set the size of the window. */ 657 xitSetSizeFormDialog( printFd, True ); 658 659 660 /* Make the final attachments. */ 661 xitAttachWidget( outputRc, 662 XmATTACH_NONE, NULL, XmATTACH_FORM, NULL, 663 XmATTACH_NONE, NULL, XmATTACH_FORM, NULL ); 664 xitAttachWidget( whatRc, 665 XmATTACH_NONE, NULL, XmATTACH_FORM, NULL, 666 XmATTACH_NONE, NULL, XmATTACH_WIDGET, outputRc ); 667 xitAttachWidget( prSelectRc, 668 XmATTACH_NONE, NULL, XmATTACH_FORM, NULL, 669 XmATTACH_NONE, NULL, XmATTACH_WIDGET, whatRc ); 670 xitAttachWidget( XtParent( printerLi ), 671 XmATTACH_FORM, NULL, XmATTACH_FORM, NULL, 672 XmATTACH_FORM, NULL, XmATTACH_WIDGET, prSelectRc ); 673 674 /* Default printer and printer type? */ 675 char_ref = getenv( "PRINTER" ); 676 677 if( char_ref != NULL ) 678 XmTextSetString( prNameTx, char_ref ); 679 680 681 return( printFd ); 682 683 } /* createPrintWindow */ 684 685 686 /*----------------------------------------------------------------------*/ 687 688 static void loadPrinterList(PRINTER_REC_REF printer_ref)689 loadPrinterList( PRINTER_REC_REF printer_ref ) 690 { 691 692 /* Variables. */ 693 int index; 694 int items = 0; 695 int parsed_items; 696 int source_index = 0; 697 char buffer[ 100 ]; 698 char delimiter[ 10 ]; 699 char printer_desc[ 100 ]; 700 char printer_flags[ 50 ]; 701 char printer_name[ 50 ]; 702 char printer_type[ 50 ]; 703 char *char_ref; 704 char *printer_source[ 10 ]; 705 Arg args[ 10 ]; 706 Cardinal n; 707 FILE *file_ref; 708 XmString list_items[ 100 ]; 709 Widget mainW; 710 Widget prListW; 711 712 713 /* Code. */ 714 715 mainW = XtNameToWidget( printer_ref -> printerW, "PrintFdFo" ); 716 prListW = XtNameToWidget( mainW, "PrinterLiSW.PrinterLi" ); 717 718 /* Whre do we find our printer definitions? */ 719 printer_source[ 0 ] = printer_ref -> user_pr_file; 720 printer_source[ 1 ] = printer_ref -> system_pr_file; 721 printer_source[ 2 ] = NULL; 722 723 724 /* Process all the printer sources. */ 725 while( printer_source[ source_index ] != NULL ) { 726 727 /* Try to open the file, if we fail, try the next. */ 728 file_ref = fopen( printer_source[ source_index ], "r" ); 729 if( file_ref == NULL ) { 730 source_index++; 731 continue; 732 } 733 734 /* Read all the printer definitions and build the printer list. */ 735 while( fgets( buffer, sizeof( buffer ), file_ref ) != NULL ) { 736 737 /* Skip all leading blanks. */ 738 char_ref = buffer; 739 while( isspace( *char_ref ) && *char_ref != '\0' ) 740 char_ref++; 741 742 /* Is this a comment? */ 743 if( *char_ref == '#' || *char_ref == '!' ) 744 continue; 745 746 /* Parse the printer definition. */ 747 printer_desc[ 0 ] = '\0'; 748 printer_flags[ 0 ] = '\0'; 749 printer_name[ 0 ] = '\0'; 750 printer_type[ 0 ] = '\0'; 751 752 parsed_items = sscanf( char_ref, "%[^$]%c%[^$]%c%[^$]%c%[^\n]", 753 printer_name, delimiter, 754 printer_type, delimiter, 755 printer_desc, delimiter, 756 printer_flags ); 757 if( parsed_items < 5 ) 758 continue; 759 760 if( printer_desc[ strlen( printer_desc ) -1 ] == '\n' ) 761 printer_desc[ strlen( printer_desc ) - 1 ] = '\0'; 762 763 /* Create the entry in the printer list. */ 764 sprintf( buffer, "%-15.15s %-5.5s %-40.40s", 765 printer_name, printer_type, printer_desc ); 766 767 /* Add the printer definition to the list. */ 768 list_items[ items ] = XmStringCreate( buffer, CS ); 769 items++; 770 771 } /* while */ 772 773 fclose( file_ref ); 774 source_index++; 775 776 } /* while */ 777 778 if( items < 1 ) 779 return; 780 781 782 /* Assign the printers to the list. */ 783 n = 0; 784 XtSetArg( args[ n ], XmNitems, list_items ); n++; 785 XtSetArg( args[ n ], XmNitemCount, items ); n++; 786 XtSetValues( prListW, args, n ); 787 788 /* Free the allocated strings. */ 789 for( index = 0; index < items; index++ ) 790 XmStringFree( list_items[ index ] ); 791 792 793 return; 794 795 } /* loadPrinterList */ 796 797 798 /*----------------------------------------------------------------------*/ 799 800 static void startPrintJob(PRINTER_REC_REF printer_ref)801 startPrintJob( PRINTER_REC_REF printer_ref ) 802 { 803 804 /* Variables. */ 805 Boolean ok; 806 int status; 807 char file_name[ PATH_MAX ]; 808 char *char_ref; 809 Widget mainW; 810 Widget tempW; 811 INFO_TYPE print_info = INFO_SECTION; 812 OUTPUT_TYPE output_to = TO_PRINTER; 813 814 815 /* Code. */ 816 817 mainW = XtNameToWidget( printer_ref -> printerW, "PrintFdFo" ); 818 819 820 /* The file where the data to print is written. */ 821 char_ref = getenv( "HOME" ); 822 if( char_ref != NULL ) 823 sprintf( file_name, "%s/XInfo.out", char_ref ); 824 else 825 sprintf( file_name, "./XInfo.out" ); 826 827 828 /* What do we do? */ 829 tempW = XtNameToWidget( mainW, "WhatRc.WhatRb.What1Tb" ); 830 if( XmToggleButtonGetState( tempW ) ) 831 print_info = INFO_SECTION; 832 833 tempW = XtNameToWidget( mainW, "WhatRc.WhatRb.What2Tb" ); 834 if( XmToggleButtonGetState( tempW ) ) 835 print_info = INFO_CHAPTER; 836 837 tempW = XtNameToWidget( mainW, "WhatRc.WhatRb.What3Tb" ); 838 if( XmToggleButtonGetState( tempW ) ) 839 print_info = INFO_BOOK; 840 841 842 /* Print to file/printer? */ 843 tempW = XtNameToWidget( mainW, "OutputRc.OutputRb.Output1Tb" ); 844 if( XmToggleButtonGetState( tempW ) ) 845 output_to = TO_PRINTER; 846 847 tempW = XtNameToWidget( mainW, "OutputRc.OutputRb.Output2Tb" ); 848 if( XmToggleButtonGetState( tempW ) ) 849 output_to = TO_FILE; 850 851 852 /* Create the file to print. */ 853 ok = createPrintFile( printer_ref, print_info, file_name ); 854 855 if( ! ok ) { 856 xitErMessage( printer_ref -> printerW, XIT_ER_ERROR, 857 module_name, "okCB", 858 msgDGetText( td, MINF_CANNOT_CREATE_PRINT_FILE ) ); 859 return; 860 } 861 862 863 /* Send to printer? */ 864 if( output_to == TO_PRINTER ) { 865 866 Cursor wait_cursor; 867 Display *display = XtDisplay( printer_ref -> printerW ); 868 Window window = XtWindow( printer_ref -> printerW ); 869 870 /* This might take time. */ 871 wait_cursor = XCreateFontCursor( display, XC_watch ); 872 XDefineCursor( display, window, wait_cursor ); 873 874 XFlush( display ); 875 876 char_ref = (char *) SysMalloc( strlen( printer_ref -> print_script ) + 877 strlen( printer_ref -> pr_name ) + 878 strlen( file_name ) + 879 10 ); 880 881 sprintf( char_ref, "%s %s %s", 882 printer_ref -> print_script, printer_ref -> pr_name, 883 file_name ); 884 885 status = system( char_ref ); 886 887 SysFree( char_ref ); 888 889 XUndefineCursor( display, window ); 890 } 891 892 893 /* Notify the user about the result. */ 894 char_ref = (char *) SysMalloc( strlen( file_name ) + 100 ); 895 896 if( output_to == TO_PRINTER ) 897 sprintf( char_ref, msgDGetText( td, MINF_RESULT_TO_PRINTER ), 898 printer_ref -> pr_name ); 899 else 900 sprintf( char_ref, msgDGetText( td, MINF_RESULT_TO_FILE ), 901 file_name ); 902 903 tempW = xitCreateInformationDialog( 904 printer_ref -> printerW, "InformationDialog", 905 msgDGetText( td, MINF_INFORMATION_LABEL ), 906 char_ref, 907 NULL, NULL ); 908 909 SysFree( char_ref ); 910 911 912 return; 913 914 } /* startPrintJob */ 915 916 917 /*----------------------------------------------------------------------*/ 918 919 static char stripSpaces(char * buffer_ref)920 *stripSpaces( char *buffer_ref ) 921 { 922 923 /* Variables. */ 924 char *char_ref; 925 char *start_ref; 926 927 928 /* Code. */ 929 930 start_ref = buffer_ref; 931 while( isspace( *start_ref ) ) 932 start_ref++; 933 934 if( *start_ref == '\0' ) 935 return( start_ref ); 936 937 char_ref = buffer_ref + strlen( buffer_ref ) - 1; 938 while( isspace( *char_ref ) && char_ref > start_ref ) 939 char_ref--; 940 *(char_ref + 1) = '\0'; 941 942 return( start_ref ); 943 944 } /* stripSpaces */ 945 946 947 /*----------------------------------------------------------------------*/ 948 949 static void closeCB(Widget widget,PRINTER_REC_REF printer_ref,XtPointer call_data)950 closeCB( Widget widget, 951 PRINTER_REC_REF printer_ref, 952 XtPointer call_data ) 953 { 954 955 /* Code. */ 956 957 /* Do we have a user action callback registered? */ 958 if( printer_ref -> actionCB != NULL ) 959 (* printer_ref -> actionCB)( XIT_IP_REASON_POPDOWN, 960 printer_ref -> user_data ); 961 962 XtUnmanageChild( printer_ref -> printerW ); 963 964 965 return; 966 967 } /* closeCB */ 968 969 970 /*----------------------------------------------------------------------*/ 971 972 static void destroyCB(Widget widget,PRINTER_REC_REF printer_ref,XtPointer call_data)973 destroyCB( Widget widget, 974 PRINTER_REC_REF printer_ref, 975 XtPointer call_data ) 976 { 977 978 /* Code. */ 979 980 /* Do we have a user action callback registered? */ 981 if( printer_ref -> actionCB != NULL ) 982 (* printer_ref -> actionCB)( XIT_IP_REASON_DESTROY, 983 printer_ref -> user_data ); 984 985 /* Free allocated data. */ 986 if( printer_ref -> system_pr_file != NULL ) 987 SysFree( printer_ref -> system_pr_file ); 988 989 if( printer_ref -> user_pr_file != NULL ) 990 SysFree( printer_ref -> user_pr_file ); 991 992 if( printer_ref -> print_script != NULL ) 993 SysFree( printer_ref -> print_script ); 994 995 SysFree( printer_ref ); 996 997 998 return; 999 1000 } /* destroyCB */ 1001 1002 1003 /*----------------------------------------------------------------------*/ 1004 1005 static void printCB(Widget widget,PRINTER_REC_REF printer_ref,XtPointer call_data)1006 printCB( Widget widget, 1007 PRINTER_REC_REF printer_ref, 1008 XtPointer call_data ) 1009 { 1010 1011 /* Variables. */ 1012 Boolean need_printer = True; 1013 char *char_ref; 1014 Widget mainW; 1015 Widget tempW; 1016 1017 1018 /* Code. */ 1019 1020 mainW = XtNameToWidget( printer_ref -> printerW, "PrintFdFo" ); 1021 1022 1023 /* Do we need a printer name? */ 1024 tempW = XtNameToWidget( mainW, "OutputRc.OutputRb.Output2Tb" ); 1025 if( XmToggleButtonGetState( tempW ) ) 1026 need_printer = False; 1027 1028 1029 /* Printer name. */ 1030 tempW = XtNameToWidget( mainW, "PrSelectRc.PrNameTx" ); 1031 1032 char_ref = xitStringGetText( tempW ); 1033 if( char_ref == NULL ) 1034 return; 1035 1036 strcpy( printer_ref -> pr_name, char_ref ); 1037 SysFree( char_ref ); 1038 1039 char_ref = printer_ref -> pr_name; 1040 while( isspace( *char_ref ) ) 1041 char_ref++; 1042 1043 if( *char_ref == '\0' && need_printer ) { 1044 xitErMessage( printer_ref -> printerW, XIT_ER_ERROR, 1045 module_name, "okCB", 1046 msgDGetText( td, MINF_NO_PRINTER_GIVEN ) ); 1047 return; 1048 } else if( *char_ref == '\0' ) { 1049 strcpy( printer_ref -> pr_name, "x" ); 1050 } 1051 1052 /* Start the print job. */ 1053 startPrintJob( printer_ref ); 1054 1055 1056 return; 1057 1058 } /* printCB */ 1059 1060 1061 /*----------------------------------------------------------------------*/ 1062 1063 static void printerSelectCB(Widget widget,PRINTER_REC_REF printer_ref,XmListCallbackStruct * call_data)1064 printerSelectCB( Widget widget, 1065 PRINTER_REC_REF printer_ref, 1066 XmListCallbackStruct *call_data ) 1067 { 1068 1069 /* Variables. */ 1070 char printer_name[ 20 ]; 1071 char *char_ref; 1072 Widget mainW; 1073 Widget tempW; 1074 1075 1076 /* Code. */ 1077 1078 mainW = XtNameToWidget( printer_ref -> printerW, "PrintFdFo" ); 1079 1080 1081 /* Fetch the selected printer. */ 1082 char_ref = xitStringGetString( call_data -> item, CS ); 1083 1084 sscanf( char_ref, "%[^ ]", printer_name ); 1085 SysFree( char_ref ); 1086 1087 1088 /* Set the printer name. */ 1089 tempW = XtNameToWidget( mainW, "PrSelectRc.PrNameTx" ); 1090 XmTextSetString( tempW, printer_name ); 1091 1092 1093 return; 1094 1095 } /* printerSelectCB */ 1096