home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / CKPM5X_S.ZIP / CKOPM6.C < prev    next >
C/C++ Source or Header  |  1990-05-27  |  13KB  |  444 lines

  1. /******************************************************************************
  2. File name:  ckopm6.c    Rev: 01  Date: 27-May-90 Programmer: C.P.Armstrong
  3.  
  4. File title: More kermit PM interface.  Mainly printing and metafile stuff.
  5.  
  6. Contents:   
  7.  
  8. Modification History:
  9.     01  27-May-90   C.P.Armstrong   created
  10.  
  11. ******************************************************************************/
  12. #define INCL_GPI    
  13. #define INCL_WIN
  14. #define INCL_AVIO
  15. #define INCL_PIC            /* For the PicPrint function */
  16. #define INCL_DEV
  17. #include <OS2.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "ckopm.h"
  21. #include "ckorc.h"
  22.  
  23. #define DEFAULT_META "kerdef.met"
  24.  
  25. /******************************************************************************
  26. Function:       do_print()
  27.  
  28. Description:    Prints out the graphics display to the default printer. 
  29.                 This is done by creating a metafile and using the OS/2 1.2
  30.                 function PicPrint to print the metafile.
  31.  
  32.                 Uses a default file name of DEFAULT_META.  This file is
  33.                 deleted before calling the metafile name.
  34.  
  35. Syntax:         int do_print(HAB hab,HWND hwnd)      
  36.                 hab  - threads anchor block
  37.                 hwnd - display window handle
  38.  
  39. Returns:        0 if successful
  40.  
  41. Mods:           27-May-90 C.P.Armstrong created
  42.  
  43. ******************************************************************************/
  44. int do_print(HAB hab,HWND hwnd)
  45.     {
  46.     unlink(DEFAULT_META);
  47.  
  48.     if(MakeMetaFile(DEFAULT_META,hab,hwnd)!=0)
  49.         {
  50.         dbprintf("do_print - MakeMetaFile failed\n");
  51.         return(-1);
  52.         }
  53.  
  54.     /* Try using the new 1.2 PicPrint */
  55.     if(!PicPrint(hab,DEFAULT_META,PIP_MF,NULL))
  56.         {
  57.         dbprintf("FALSE return from PicPrint\n");
  58.         return(-1);
  59.         }
  60.     else
  61.         dbprintf("PicPrint returned TRUE\n");
  62.  
  63.     return(0);
  64.     }
  65.  
  66. /******************************************************************************
  67. Function:       MakeMetaFile()
  68.  
  69. Description:    Writes out the current graphics window display to a metafile.
  70.  
  71. Syntax:         int MakeMetaFile(char * metname,HAB hab,HWND hwnd)
  72.                     metname - name for the metafile, must not exist already.
  73.                     hwnd - Handle of the window containing the graphics
  74.                     hab  - threads anchor block
  75.  
  76. Returns:        0 if successful
  77.  
  78. Mods:           27-May-90 C.P.Armstrong created
  79.  
  80. ******************************************************************************/
  81. int MakeMetaFile(char * metname,HAB hab,HWND hwnd)
  82.     {
  83.     extern struct plot_command pcroot;
  84.   
  85.     struct plot_command * pcn;
  86.  
  87.     SHORT len;
  88.     DEVOPENSTRUC dop;
  89.     HDC hdcm;
  90.     LONG lrc;
  91.     HPS hpsm;
  92.     HMF hm;
  93.     SIZEL szl;
  94.     SWP swp;
  95.  
  96. /* The MS.Prog.Ref says only the necessary params must be passed and to pass */
  97. /* 4 for OD_METAFILE.  Initializing only the first 4 causes DevOpenDC to mpf.*/
  98. /* Passing 2 for 4 initialized params alos causes DevOpenDC to mpf.  So far  */
  99. /* it seems that initializing all params and sending 2 as the number required*/
  100. /* works - OS/2 1.2 "November" with no CSDs.                                 */
  101.  
  102.     /* Open the device context */
  103.     dop.pszLogAddress = NULL;
  104.     dop.pszDriverName = "DISPLAY";
  105.     dop.pdriv = NULL;
  106.     dop.pszDataType = NULL;
  107.     dop.pszComment = NULL;
  108.     dop.pszQueueProcName=NULL;
  109.     dop.pszQueueProcParams=NULL;
  110.     dop.pszSpoolerParams=NULL;
  111.     dop.pszNetworkParams=NULL;
  112.  
  113.  
  114.     dbprintf("MakeMetaFile - about to open DC\n");
  115.  
  116.     if( (hdcm = DevOpenDC(hab, (LONG) OD_METAFILE, "*",
  117.             2L, (PDEVOPENDATA) &dop, NULL)) == DEV_ERROR)
  118.         {
  119.         dbprintf("MakeMetaFile - Failed to open metafile\n");
  120.         return(-1);
  121.         }
  122.  
  123.     /* Lets associate */
  124.     szl.cx = 0L;
  125.     szl.cy = 0L;
  126.     
  127.     if( (hpsm = GpiCreatePS(hab,hdcm,&szl, 
  128.             PU_PELS | GPIA_ASSOC)) == GPI_ERROR)
  129.         {
  130.         dbprintf("MakeMetaFile - Failed to create metafile PS\n");
  131.         return(-1);
  132.         }
  133.             
  134.     /* Determine page size */
  135.     if(!DevQueryCaps(hdcm,CAPS_WIDTH,2L, (PLONG) &szl))
  136.         {
  137.         dbprintf("MakeMetaFile - Failed to determine page size\n");
  138.         return(-1);
  139.         }
  140.     else
  141.         dbprintf("MakeMetaFile - page size is %ld,%ld\n",szl.cx,szl.cy);
  142.     
  143.     /* Convert to PS units */
  144.     if( GpiConvert(hpsm, CVTC_DEVICE,CVTC_WORLD,1L,(PPOINTL) &szl)!=GPI_OK)
  145.         {
  146.         dbprintf("MakeMetaFile - Failed to convert units\n");
  147.         return(-1);
  148.         }
  149.     else
  150.         dbprintf("MakeMetaFile - converted page size %ld,%ld\n",szl.cx,szl.cy);
  151.  
  152.     swp.cx = (SHORT) szl.cx;
  153.     swp.cy = (SHORT) szl.cy;
  154.  
  155.     /* Select a vector font */    
  156.     if( SelectFont(hpsm, 4L,"Helv",1,0,0)!=GPI_OK)
  157.         dbprintf("MakeMetaFile - Error selecting Helv vector font\n");
  158.     else    
  159.         {                                   /* Size the font */
  160.         SetCharBox(hpsm, 
  161.           MAXCHARWIDTH * swp.cx/MAXXRES, 
  162.           MAXCHARHEIGHT * swp.cy/MAXYRES);
  163.         }
  164.  
  165.     /* Plot loop */
  166.     pcn = &pcroot;
  167.     do
  168.         {
  169.         pc_interp(*pcn,&swp,0,hpsm);
  170.         pcn = pcn->next;
  171.         }
  172.     while(pcn != NULL);
  173.     
  174.     /* Delete PS, close device etc. */
  175.     GpiAssociate(hpsm,NULL);
  176.     hm = DevCloseDC(hdcm);
  177.  
  178.     if(GpiSaveMetaFile(hm,metname)!=GPI_OK)
  179.         {
  180.         dbprintf("MakeMetaFile - Problem saving the meta file\n");
  181.         }
  182.     GpiDestroyPS(hpsm);
  183.  
  184.     return(0);
  185.     }
  186.     
  187. /******************************************************************************
  188. Function:       do_meta()
  189.  
  190. Description:    Saves the current graphics display to a metafile.  The filename
  191.                 is chosen by means of the fileopen dialog.
  192.                 
  193.                 If the a file with the chosen name exists it is deleted before
  194.                 the metafile is written.
  195.  
  196. Syntax:         int do_meta(hab,hwnd)
  197.                     HAB hab;    Threads anchor block
  198.                     HWND hwnd;  Graphics display handle
  199.  
  200. Returns:        0 if successful
  201.  
  202. Mods:           27-May-90 C.P.Armstrong created
  203.  
  204. ******************************************************************************/
  205. int do_meta(hab,hwnd)
  206.     HAB hab;
  207.     HWND hwnd;
  208.     {
  209.     struct dlgopn dlo;
  210.     char metname[81];
  211.  
  212.     dlo.title = "Metafile Name";
  213.     dlo.name = metname;
  214.     strcpy(metname,"*.MET");
  215.  
  216.     /* Call the filename find dialog */
  217.     WinDlgBox(HWND_DESKTOP,hwnd,FileOpnDlgProc,(HMODULE)0,IDD_LG2,&dlo);
  218.     if(metname[0]=='\0')
  219.        {
  220.        return(0);
  221.        }
  222.  
  223.     unlink(metname);
  224.     if(MakeMetaFile(metname,hab,hwnd)!=0)
  225.         {
  226.         dbprintf("do_meta - MakeMetaFile failed\n");
  227.         return(-1);
  228.         }
  229.     
  230.     return(0);
  231.     }
  232.     
  233.  
  234. // The code below can be used to print out using OS/2 1.1, which does not have
  235. // the PicPrint routine.  It was tested using an HP Laserjet II and seemd to
  236. // work okay except for the vector graphics which would cause the program to
  237. // protection fault when the Gpi function to set the font was called.
  238. //    extern struct plot_command pcroot;
  239. //    char defprn[40];
  240. //    char details[256];
  241. //
  242. //    char * driver;
  243. //    char * devnme;    
  244. //    char * logport;
  245. //    struct plot_command * pcn;
  246. //
  247. //    SHORT len;
  248. //    DEVOPENSTRUC dop;
  249. //    HDC hdcp;
  250. //    LONG lrc;
  251. //    HPS hpsp;
  252. //    SIZEL szl;
  253. //    SWP swp;
  254. //    USHORT jobid;
  255. //    LONG jlen;
  256. //    
  257. //    PDRIVDATA pd;
  258.  
  259. //    /* Get the default printer name */
  260. //    len = WinQueryProfileString(hab,
  261. //            "PM_SPOOLER",
  262. //            "PRINTER",
  263. //            "",
  264. //          defprn,
  265. //            40);
  266.             
  267. //    defprn[len-2]=0;  /* Null teriminate and remove final ";" */
  268.     
  269.                       /* This is the default printer name "PRINTER1" */
  270.  
  271. //    len = WinQueryProfileString(hab,
  272. //            "PM_SPOOLER_PRINTER",
  273. //            defprn,
  274. //            "",
  275. //            details,
  276. //            256);
  277.  
  278. //    /* Format of details is ;
  279. //        LPT1;LASERJET;LPT1Q;netinfo;
  280. //       We want the driver and the logical port */
  281. //
  282. //    if( (driver = strchr(details,';'))==NULL)
  283. //        {
  284. //        dbprintf("do_print - Bad details format\n\n");
  285. //        return(-1);
  286. //        }
  287. //
  288. //    driver++;
  289. //    if( (logport = strchr(driver,';'))==NULL)
  290. //        {
  291. //        dbprintf("do_print - Bad driver format\n\n");
  292. //        return(-1);
  293. //        }
  294. //
  295. //    *logport=0;     /* Null terminate driver(s) */
  296. //    logport++;
  297. //    
  298. //    if( strchr(logport,';')==NULL)      /* Strcspn does not give an error */
  299. //        {
  300. //        dbprintf("do_print - Bad port format\n\n");
  301. //        return(-1);                     /* if the character is not found */
  302. //        }
  303. //    else
  304. //        logport[strcspn(logport,";")]=0;
  305. //
  306. //    if( strchr(driver,',')!=NULL)
  307. //        driver[strcspn(driver,",")]=0;
  308. //        
  309. //
  310. //    /* With the Laserjet driver the "details" look like
  311. //        LPT1;LASERJET.Laserjet II;LPT1Q;;
  312. //       The driver is LASERJET.DRV.  The driver needs to know the model, but it
  313. //       doesn't say where this info should be put.  By trial and error I've 
  314. //       found that putting it in a DRIVDATA struct gets printout on my laserjet
  315. //       II.
  316. //    */
  317. //    if( strchr(driver,'.')!=NULL)       /* Get rid of "." if it's there */
  318. //        {
  319. //        devnme = &driver[1+strcspn(driver,".")];
  320. //        /* Nul terminate the driver name */
  321. //        driver[strcspn(driver,".")]=0;
  322. //        }
  323. //    else
  324. //        {
  325. //        devnme=NULL;
  326. //        }
  327. //
  328. //    /* Get driver details */
  329. //    len = DevPostDeviceModes(hab,(LONG) NULL,driver,devnme,
  330. //      logport,DPDM_QUERYJOBPROP);
  331. //    pd = malloc(len*sizeof(LONG));
  332. //    DevPostDeviceModes(hab,pd,driver,devnme,
  333. //      logport,DPDM_QUERYJOBPROP);
  334. //
  335. //
  336. //    dbprintf("Printer device is %s\nPrinter driver is %s\nLogical port is %s\n",
  337. //      devnme,driver,logport);
  338. //    
  339. //    /* Open the device context */
  340. //    dop.pszLogAddress = logport;
  341. //    dop.pszDriverName = driver;
  342. //    dop.pdriv = pd;
  343. //    dop.pszDataType = "PM_Q_STD";
  344. //
  345. // 
  346. //
  347. //    if( (hdcp = DevOpenDC(hab, OD_QUEUED, "*",
  348. //            4L, (PDEVOPENDATA) &dop, NULL)) == DEV_ERROR)
  349. //        { 
  350. //        pm_err("Failed to open printer device");
  351. //        dbprintf("do_print - Failed to open device\n\n");
  352. //        return(-1);
  353. //        }
  354. //
  355. //
  356. //
  357. //    /* Tell it we're starting a job */
  358. //    if( (lrc = DevEscape(hdcp,DEVESC_STARTDOC,
  359. //               (LONG)   strlen("Kermit graph"),
  360. //                        "Kermit graph",
  361. //               (PLONG) NULL, (PBYTE) NULL))!=DEV_OK)
  362. //        {
  363. //        dbprintf("do_print - job start failed\n\n");
  364. //        return(-1);
  365. //        }
  366. //
  367. //    /* Lets associate */
  368. //    szl.cx = 0L;
  369. //    szl.cy = 0L;
  370. //    
  371. //    if( (hpsp = GpiCreatePS(hab,hdcp,&szl, 
  372. //            PU_PELS | GPIF_DEFAULT | GPIT_NORMAL | GPIA_ASSOC)) == GPI_ERROR)
  373. //        {
  374. //        dbprintf("do_print - Failed to create PS\n\n");
  375. //        return(-1);
  376. //        }
  377. //            
  378. //    /* Determine page size */
  379. //    if(!DevQueryCaps(hdcp,CAPS_WIDTH,2L, (PLONG) &szl))
  380. //        {
  381. //        dbprintf("do_print - Failed to determine page size\n\n");
  382. //        return(-1);
  383. //        }
  384. //    else
  385. //        dbprintf("do_print - page size is %ld,%ld\n",szl.cx,szl.cy);
  386. //    
  387. //    /* Convert to PS units */
  388. //    if( GpiConvert(hpsp, CVTC_DEVICE,CVTC_WORLD,1L,(PPOINTL) &szl)!=GPI_OK)
  389. //        {
  390. //        dbprintf("do_print - Failed to convert units\n\n");
  391. //        return(-1);
  392. //        }
  393. //    else
  394. //        dbprintf("do_print - converted page size %ld,%ld\n",szl.cx,szl.cy);
  395. //
  396. //    
  397. //    swp.cx = (SHORT) szl.cx;
  398. //    swp.cy = (SHORT) szl.cy;
  399. //    
  400. ////    dbprintf("do_print - Trying to select the Helv vector font\n");
  401. //
  402. //    /* Select a vector font */    
  403. ////    if( SelectFont(hpsp, 10L,"Helv",1,0,0)!=GPI_OK)
  404. ////        dbprintf("********** do_print - Error selecting Helv vector font\n");
  405. ////    else
  406. ////        {
  407. ////        dbprintf("do_print - Font selected\n");
  408. ////        SetCharBox(hpsp,                    /* Size the font */
  409. ////          MAXCHARWIDTH * swp.cx/MAXXRES, 
  410. ////          MAXCHARHEIGHT * swp.cy/MAXYRES);
  411. ////        dbprintf("do_print - Char box set\n");
  412. ////        }
  413. //
  414. //    /* Plot loop */
  415. //    pcn = &pcroot;
  416. //    do
  417. //        {
  418. //        pc_interp(*pcn,&swp,0,hpsp);
  419. //        pcn = pcn->next;
  420. //        }
  421. //    while(pcn != NULL);
  422. //
  423. //    dbprintf("do_print - Now let's try to print it\n");
  424. //
  425. //    jlen = sizeof(USHORT);
  426. //    /* Tell queue we've finished document */
  427. //    if( (lrc = DevEscape(hdcp,DEVESC_ENDDOC,
  428. //                         0L, (PBYTE) NULL,
  429. //                 (PLONG) &jlen, (PBYTE) &jobid))!=DEV_OK)
  430. //        {
  431. //        dbprintf("do_print - Failed end print job\n");
  432. //        }
  433. //
  434. //    /* Delete PS, close device etc. */
  435. //    GpiAssociate(hpsp,NULL);
  436. //    DevCloseDC(hdcp);
  437. //    GpiDestroyPS(hpsp);
  438. //
  439. //    if(pd!=NULL)
  440. //        free(pd);
  441. //
  442. //    dbprintf("do_print - Normal exit\n\n");
  443. //    return(0);
  444.