示例#1
0
extern void
DtPrintFreeSetupData(DtPrintSetupData *target)
{
	DEBUGOUT(_LtDebug(__FILE__, NULL, "DtPrintFreeSetupData\n"));

	if (target->print_display && target->print_context)
		XpDestroyContext(target->print_display, target->print_context);

	target->print_context = (XPContext)0;
	if (target->print_display)
		XtCloseDisplay(target->print_display);
	target->print_display = (Display *)0;
}
int do_hello_world( int argc, char *argv[], const char *printername, const char *toFile, const char *sample_string )
{
    XtAppContext         app;                                                   
    Widget               toplevel,                                              
                         shell,                                                 
                         print_shell,                                           
                         hello;                                                 
    long                 dpi_x = 0L,
                         dpi_y = 0L;                                                 
    char                 fontname[256]; /* BUG: is this really big enougth ? */ 
    XFontStruct         *textFont;                                              
    XmFontList           textFontList;                                          
    Cardinal             n;                                                     
    Arg                  args[10];                                              
    MyPrintCallbackData  mpcd;
       
    if( doPrint )
    {
      /* Get printer, either by "name" (foobar) or "name@display" (foobar@gaja:5) */
      if( XpuGetPrinter(printername, &pdpy, &pcontext) != 1 )
        Error(("XpuGetPrinter failure.\n"));
     
      /* Configure the print context (paper size, title etc.)
       * We must do this before creating any Xt widgets - otherwise they will
       * make wrong assuptions about fonts, resultions etc. ...
       */
      XpuSetJobTitle(pdpy, pcontext, "Simple Xprint XawPrintShell widget demo");
      
      /* Configuration done, set the context */
      XpSetContext(pdpy, pcontext);

      /* Get default printer resolution */   
      if( XpuGetResolution(pdpy, pcontext, &dpi_x, &dpi_y) != 1 )
      {
        fprintf(stderr, "No default resolution for printer '%s'\n", printername);
        XpuClosePrinterDisplay(pdpy, pcontext);
        return(EXIT_FAILURE);
      }

      pscreen = XpGetScreenOfContext(pdpy, pcontext);
    }
    else
    {
      pdpy = XOpenDisplay(NULL);
      if( !pdpy )
        Error(("XOpenDisplay failure.\n"));

      dpi_x = dpi_y = 0L;
      
      pscreen = XDefaultScreenOfDisplay(pdpy);
    }  

    toplevel = XawOpenApplication(&app, 
                                  pdpy, pscreen,
                                  "xpawprintshelldemo", "XpXawPrintShellDemo",
                                  applicationShellWidgetClass,
                                  &argc, argv);

    if( !toplevel )
      Error(("XawOpenApplication failure.\n"));

    if( doPrint )
    {
      n = 0;
      /* Override any geometry resource settings as XawPrintShell adjusts it's size
       * to the current page siue when |XawPrintLAYOUTMODE_DRAWABLEAREA| or
       * |XawPrintLAYOUTMODE_PAGESIZE| are used. */
      XtSetArg(args[n], XtNgeometry,    "+0+0");                          n++;
      XtSetArg(args[n], XawNlayoutMode, XawPrintLAYOUTMODE_DRAWABLEAREA); n++;
      print_shell = XtCreatePopupShell("myprintshell", 
                                       xawPrintShellWidgetClass, 
                                       toplevel, args, n);

      /* we're mapping/unmapping at start/end page time */
      XtSetMappedWhenManaged(print_shell, False);
      
      shell = print_shell;
    }
    else
    {
      shell = toplevel;
    }

    sprintf(fontname, "-adobe-courier-medium-r-normal--40-*-%ld-%ld-*-*-iso8859-1", dpi_x, dpi_y);
    textFont = XLoadQueryFont(pdpy, fontname);
    if( !textFont )
    {          
      sprintf(fontname, "-*-*-*-*-*-*-*-160-%ld-%ld-*-*-iso8859-1", dpi_x, dpi_y);
      textFont = XLoadQueryFont(pdpy, fontname);
    }
    if( !textFont )
      Error(("XLoadQueryFont failure.\n"));
    textFontList = XmFontListCreate(textFont, XmSTRING_DEFAULT_CHARSET);     

    n = 0;
    /* Make sure the cursor is off, current Xprt servers do not seem to like
     * blinking cursors that much... ;-/ */
    XtSetArg(args[n], XmNcursorPositionVisible, False);              n++;
    XtSetArg(args[n], XmNvalue,                 sample_string);      n++;
    XtSetArg(args[n], XmNfontList,              textFontList);       n++;
    XtSetArg(args[n], XmNeditMode,              XmMULTI_LINE_EDIT);  n++;
    
    hello = XmCreateText(shell, "mytext", args, n);
    if( !hello )
      Error(("XmCreateText failure.\n"));
      
    XtManageChild(hello);
    XtRealizeWidget(toplevel);
    XtRealizeWidget(shell);
    
    if( doPrint )
    {
      int   num_total_rows;
      short num_visible_rows;
      int   num_pages;

      pdpy      = XtDisplay(toplevel);
      pdrawable = XtWindow(toplevel);
      if( !pdpy || !pdrawable )
        Error(("No display.\n"));
      
      /* Make sure that the Xt machinery is really using the right screen (assertion) */
      if( XpGetScreenOfContext(XtDisplay(toplevel), pcontext) != XtScreen(toplevel) )
        Error(("Widget's screen != print screen. BAD.\n"));
                  
      /* Get number of rows visible per page and the number of total rows 
       * in the whole text widget... */
      n = 0;
      XtSetArg(args[n], XmNrows,       &num_visible_rows); n++ ;
      XtSetArg(args[n], XmNtotalLines, &num_total_rows);   n++ ;
      XtGetValues(hello, args, n);

      /* Take away one row to match the one-line overlapping used by the
       * "next-page" action proc */      
      num_visible_rows -= 1;

      /* Calculate the number of pages */
      num_pages = (num_total_rows+num_visible_rows-1) / num_visible_rows;
      Log(("Printing %d pages (num_total_rows=%d, num_visible_rows=%d)...\n", 
           num_pages, num_total_rows, num_visible_rows));
      
      /* Prepare our own context data for the print shell callbacks */
      mpcd.num_pages          = num_pages;
      mpcd.printshell_content = hello;
      mpcd.num_visible_rows   = num_visible_rows;
      mpcd.appcontext         = app;
      
      /* Setup the print shell callbacks... */
      XtAddCallback(print_shell,  XawNpageSetupCallback, PrintOnePageCB,  (XtPointer)&mpcd);
      XtAddCallback(print_shell,  XawNstartJobCallback,  PrintStartJobCB, NULL);
      XtAddCallback(print_shell,  XawNendJobCallback,    PrintEndJobCB,   (XtPointer)&mpcd);  

      /* ... and finally start the print job. */
      if( toFile )
      {
        printtofile_handle = XpuStartJobToFile(pdpy, pcontext, toFile);
        if( !printtofile_handle )
        {
          perror("XpuStartJobToFile failure");
          Error(("XpuStartJobToFile failure."));
        }
      }
      else
      {
        XpuStartJobToSpooler(pdpy);
      }
    }
    
    XtAppMainLoop(app);
    
    if( doPrint )
    {
      if( toFile )
      {
        if( XpuWaitForPrintFileChild(printtofile_handle) != XPGetDocFinished )
        {
          fprintf(stderr, "%s: Error while printing to file.\n", ProgramName);
        }
      }

      PrintSpoolerCommandResults(pdpy, pcontext);    

      /* We have to use XpDestroyContext() and XtCloseDisplay() instead
       * of XpuClosePrinterDisplay() to make libXt happy... */
      if( pcontext != None )
        XpDestroyContext(pdpy, pcontext);
      XtCloseDisplay(pdpy);
    }
   
    return EXIT_SUCCESS;
}