home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / printq14.zip / HELLOQ3 / HELLO.C next >
Text File  |  1994-04-25  |  15KB  |  438 lines

  1. /* hello.c: source code file for Hello queue/2 */
  2. /* Version 1.3, (C) Peter Wansch, 1993         */
  3. /* created: 93-8-16                            */
  4.  
  5. #define INCL_PM    
  6. #define INCL_WINSTDFONT
  7.  
  8. #include <printq.h>
  9. #include <stdio.h>
  10. #include <stdlib.h> 
  11. #include <string.h> 
  12.  
  13. #include "hello.h"
  14.  
  15. /* global variables */
  16. HAB hab;                                     /* anchor-block handle */
  17. HWND hwndFrame;                              /* frame window handle */
  18. CHAR szOut[NUM_OF_LINES][LENGTH_STRING+1];   /* message strings     */
  19. BOOL fIni = TRUE;                            /* logo display flag   */
  20. LONG lMaxAscender;
  21. LONG lMaxBaselineExt;          
  22. HPOINTER hptrWait, hptrSystem;
  23. POINTL ptlStart = {0, 0};
  24.  
  25. /* variables for the printer font dialog */
  26. FONTDLG  fontDlg;
  27. FONTMETRICS fontMetrics;
  28. BOOL fFirst = TRUE;
  29.     
  30. int main(VOID)
  31. {
  32.   QMSG  qmsg;                                 /* message queue handle               */
  33.   FONTMETRICS fm;                             /* default screen font metrics        */
  34.   HPS hps;                                    /* screen ps handle for retrieving fm */
  35.   ULONG flCreate = FCF_STANDARD;              /* frame creation flag                */
  36.   HWND hwndClient;                            /* client window handle               */
  37.   HMQ hmq;                                    /* queue handle (1st thread)          */
  38.   BOOL fInitialize;
  39.   ULONG ulBufSize;
  40.   LONG cb;
  41.   PDRIVDATA pDriverData = NULL;
  42.   HPS hpsPrn;
  43.  
  44.   /* register application to Presentation Manager */
  45.   hab = WinInitialize(0);
  46.   if(!hab)
  47.   {
  48.     WinAlarm(HWND_DESKTOP, WA_ERROR); 
  49.     exit(RETURN_ERROR);
  50.   }
  51.  
  52.   /* get driver data from user ini file */
  53.   ulBufSize = sizeof(cb);
  54.   fInitialize = FALSE;
  55.   if (PrfQueryProfileData(HINI_PROFILE, "Hello", "cb", &cb, &ulBufSize))
  56.   {
  57.     /* there is an entry for cb */
  58.     /* have to allocate memory for the driverdata structure first */
  59.     if (!DosAllocMem((PPVOID)&pDriverData, cb, PAG_READ | PAG_WRITE | PAG_COMMIT))
  60.     {
  61.       /* read the driver data structure */
  62.       ulBufSize = cb;
  63.       if (PrfQueryProfileData(HINI_PROFILE, "Hello", "DriverData", (PVOID)pDriverData, &ulBufSize) && ulBufSize == cb)
  64.       {
  65.         fInitialize = TRUE;
  66.       }
  67.     }
  68.   }
  69.  
  70.   /* initialize PRINTQ */
  71.   if (!SpoolInitialize(hab, pDriverData, &fInitialize))
  72.   {
  73.     WinAlarm(HWND_DESKTOP, WA_ERROR); 
  74.     DosFreeMem(pDriverData);
  75.     WinTerminate(hab);
  76.     exit(RETURN_ERROR);
  77.   }
  78.  
  79.   DosFreeMem(pDriverData);
  80.  
  81.   /* create message queue */
  82.   hmq = WinCreateMsgQueue( hab, 0 );
  83.   if(!hmq)
  84.   {
  85.     WinAlarm(HWND_DESKTOP, WA_ERROR); 
  86.     SpoolTerminate();
  87.     WinTerminate(hab);
  88.     exit(RETURN_ERROR);
  89.   }
  90.  
  91.   /* display timed logo */
  92.   if (!WinDlgBox(HWND_DESKTOP, HWND_DESKTOP, (PFNWP)dpProdInfo, NULLHANDLE, DB_PRODINFO, (PBOOL)&fIni))
  93.   {
  94.     WinDestroyMsgQueue(hmq);
  95.     SpoolTerminate();
  96.     WinTerminate(hab);
  97.     exit(RETURN_ERROR);
  98.   }
  99.   fIni = FALSE;
  100.  
  101.   /* register private window class for application window */
  102.   if (!WinRegisterClass(hab,(PSZ)"Hello", wpMain, CS_SIZEREDRAW, 0))
  103.   {
  104.     WinDestroyMsgQueue(hmq);
  105.     SpoolTerminate();
  106.     WinTerminate(hab);
  107.     exit(RETURN_ERROR);
  108.   }
  109.  
  110.   /* create application window */
  111.   hwndFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE, &flCreate, "Hello", "Hello queue/2", 
  112.                                  WS_VISIBLE, NULLHANDLE, WD_MAIN, &hwndClient);
  113.   if (!hwndFrame)
  114.   {
  115.     WinDestroyMsgQueue(hmq);
  116.     SpoolTerminate();
  117.     WinTerminate(hab);
  118.     exit(RETURN_ERROR);
  119.   }
  120.  
  121.   /* query default screen font metrics to position strings in the application window */
  122.   hps = WinGetPS (HWND_DESKTOP);
  123.   GpiQueryFontMetrics (hps, sizeof(FONTMETRICS), &fm);
  124.   WinReleasePS(hps); 
  125.   lMaxAscender=fm.lMaxAscender;
  126.   lMaxBaselineExt=fm.lMaxBaselineExt;
  127.  
  128.   /* query required pointers */
  129.   hptrWait = WinQuerySysPointer(HWND_DESKTOP, SPTR_WAIT, FALSE);
  130.   hptrSystem = WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE);        
  131.  
  132.   /* main message loop */
  133.   while(WinGetMsg(hab, &qmsg, (HWND)NULL, 0, 0))
  134.     WinDispatchMsg(hab, &qmsg);
  135.  
  136.   /* terminate application */
  137.   WinDestroyWindow(hwndFrame);        
  138.   WinDestroyMsgQueue(hmq);   
  139.          
  140.   /* de-register from PRINTQ and Presentation Manager */
  141.   SpoolTerminate();
  142.   WinTerminate(hab);                  
  143.   return(0);
  144. }
  145.  
  146. MRESULT EXPENTRY wpMain (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  147. {
  148.   HPS hpsPrn;
  149.   POINTL pt;
  150.   FONTMETRICS fmPrn;
  151.   LONG lprnMaxAscender, lprnMaxBaselineExt;
  152.   SHORT i;
  153.   USHORT usJobId;
  154.   BOOL fExceed;
  155.   RECTL rclUpdate, rclClient;
  156.   POINTL  aptl[LENGTH_STRING+1];
  157.   SIZEF sizef;
  158.   FATTRS fat;
  159.  
  160.  
  161.   switch( msg )
  162.   {
  163.     case WM_CREATE:
  164.       InitOut();
  165.       return (MRESULT)FALSE;
  166.  
  167.     case WM_ERASEBACKGROUND:
  168.       return (MRESULT)TRUE;
  169.  
  170.     case WM_COMMAND:
  171.       switch (SHORT1FROMMP(mp1))
  172.       {
  173.         case MI_FONT:
  174.           SetFont();
  175.           break;
  176.  
  177.         case MI_SETUP:
  178.           /* display Printer setup dialog box */
  179.           WinDlgBox(HWND_DESKTOP, hwnd, dpQueryQInfo, (HMODULE)0, DB_QUERYPRINT, NULL);
  180.           InitOut();
  181.           WinInvalidateRegion(hwnd, NULLHANDLE, FALSE);
  182.           SpoolEndSetup(); 
  183.           break;
  184.  
  185.         case MI_PRINT:
  186.           /* print text displayed in the application window */
  187.           WinSetPointer(HWND_DESKTOP, hptrWait);
  188.           fExceed = FALSE;
  189.  
  190.           /* change the application title bar text to inform user of spooling action */
  191.           WinSetWindowText(hwndFrame, (PSZ)"Spooling ...");
  192.  
  193.           /* begin a print job and obtain a handle to the printer presentation space */
  194.           SpoolBeginSpool("Printer Capabilities", "Printer Caps", "COP=1", 0UL, &hpsPrn, FALSE);
  195.  
  196.           if (!fFirst)
  197.           {
  198.             /* create and select font selected in the font dialog */
  199.             fat = fontDlg.fAttrs;
  200.             sizef.cx = (FIXED)(((fontDlg.fxPointSize) / 72) * alCaps[CAPS_HORIZONTAL_FONT_RES]);
  201.             sizef.cy = (FIXED)(((fontDlg.fxPointSize) / 72) * alCaps[CAPS_VERTICAL_FONT_RES]);
  202.             GpiCreateLogFont(hpsPrn, (PSTR8)fat.szFacename, LCID_NORMAL, &fat);
  203.             GpiSetCharSet(hpsPrn, LCID_NORMAL);
  204.             GpiSetCharBox(hpsPrn, &sizef);
  205.           }
  206.  
  207.           GpiQueryFontMetrics (hpsPrn, sizeof(FONTMETRICS), &fmPrn);
  208.           lprnMaxAscender=fmPrn.lMaxAscender;
  209.           lprnMaxBaselineExt=fmPrn.lMaxBaselineExt;
  210.  
  211.           /* print lines */
  212.           pt.x = 0; pt.y = hcInfoDef.yPels-lprnMaxAscender;
  213.           for (i=0; i < NUM_OF_LINES; i++)
  214.           {
  215.             /* form feed */
  216.             if (i%alCaps[CAPS_HEIGHT_IN_CHARS]==0 && i>0)
  217.             {
  218.               SpoolNewFrame();
  219.               pt.y = hcInfoDef.yPels-lprnMaxAscender;
  220.             }
  221.             GpiCharStringAt(hpsPrn, &pt, (LONG)strlen(szOut[i]), (PSZ)szOut[i]);
  222.             pt.y-=lprnMaxBaselineExt;
  223.           }
  224.  
  225.           /* end print job */
  226.           SpoolEndSpool(&usJobId);
  227.  
  228.           /* restore arrow pointer an set old application title bar text */
  229.           WinSetPointer(HWND_DESKTOP, hptrSystem);
  230.           WinSetWindowText(hwndFrame, (PSZ)"Hello queue/2");
  231.           break;
  232.  
  233.         case MI_INFO:
  234.           /* display product information dialog box */
  235.           WinDlgBox( HWND_DESKTOP, hwndFrame, (PFNWP)dpProdInfo, (HMODULE)0, DB_PRODINFO, (PBOOL)&fIni);           /* initialization data          */
  236.           break;
  237.  
  238.         default:
  239.           return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  240.       }
  241.       break;
  242.  
  243.       case WM_PAINT:
  244.       {
  245.         HPS hps;
  246.         /* paint client window */
  247.         /* begin drawing */
  248.         hps = WinBeginPaint( hwnd, (HPS)NULL, &rclUpdate);
  249.  
  250.         /* paint invalid rectangle first */
  251.         WinFillRect (hps, &rclUpdate, SYSCLR_WINDOW);
  252.  
  253.         /* draw lines */
  254.         WinQueryWindowRect(hwnd, &rclClient);
  255.         pt.x = 0; pt.y = rclClient.yTop-lMaxAscender;
  256.         for (i=0; i < NUM_OF_LINES; i++)
  257.         {
  258.           GpiCharStringAt(hps, &pt, (LONG)strlen(szOut[i]), (PSZ)szOut[i]);
  259.           pt.y-=lMaxBaselineExt;
  260.         }
  261.  
  262.       WinEndPaint(hps);               /* drawing is complete */
  263.       }
  264.       break;
  265.       
  266.     case WM_CLOSE:
  267.       /* write info about the currently selected printer into the user ini file */
  268.       /* write the size of the structure first */
  269.       if (PrfWriteProfileData(HINI_PROFILE, "Hello", "cb", &(prqInfoDef.pDriverData->cb), sizeof(prqInfoDef.pDriverData->cb)))
  270.         PrfWriteProfileData(HINI_PROFILE, "Hello", "DriverData", prqInfoDef.pDriverData, prqInfoDef.pDriverData->cb);
  271.  
  272.       WinPostMsg( hwnd, WM_QUIT, NULL, NULL);     /* cause termination */
  273.       break;
  274.  
  275.     default:
  276.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  277.   }
  278.   return (MRESULT) FALSE;
  279. }
  280.  
  281. MRESULT EXPENTRY dpQueryQInfo( HWND hwndDlg, ULONG msg, MPARAM mp1, MPARAM mp2 )
  282. {
  283.   static BOOL fVisitedJobProp;
  284.   HWND hwndListBox;
  285.  
  286.   switch (msg)
  287.   {
  288.     case WM_INITDLG:
  289.       fVisitedJobProp = FALSE;
  290.       hwndListBox = WinWindowFromID(hwndDlg, LB_QUEUES);
  291.       /* fill list box and select default printer */
  292.       SpoolBeginSetup(hwndListBox);
  293.       break;
  294.  
  295.     case WM_CONTROL:
  296.       /* list box item is double-clicked */
  297.       if (SHORT2FROMMP(mp1) == LN_ENTER && SHORT1FROMMP(mp1) == LB_QUEUES)
  298.         WinPostMsg(hwndDlg, WM_COMMAND, MPFROM2SHORT(DID_OK, 0), NULL);
  299.       break;
  300.      
  301.     case WM_COMMAND:                    /* posted by push button or key */
  302.       switch(SHORT1FROMMP(mp1))       /* extract the command value */
  303.       {
  304.         case PB_JOBPROP:
  305.           /* Job properties button has been chosen, display Jop properties dialog box */
  306.           if (!SpoolJobProp())
  307.             WinAlarm(HWND_DESKTOP, WA_ERROR);
  308.           fVisitedJobProp = TRUE;
  309.           break;
  310.         case DID_OK:            /* the OK push button or key */
  311.           /* set the currently hilited printer the default */
  312.           SpoolSetDef();
  313.           /* reset font dialog */
  314.           fFirst = TRUE;
  315.           /* close dialog */
  316.           WinDismissDlg(hwndDlg, TRUE);
  317.           break;
  318.         case DID_CANCEL:        /* the Cancel pushbutton or Escape key */
  319.           WinDismissDlg(hwndDlg, fVisitedJobProp ? TRUE : FALSE);   /* removes the dialog window */
  320.           break;
  321.         default:
  322.           break;
  323.       }
  324.       break;
  325.     default:
  326.       return WinDefDlgProc( hwndDlg, msg, mp1, mp2 );
  327.   }
  328.   return (MRESULT) FALSE;
  329. }
  330.  
  331. MRESULT EXPENTRY dpProdInfo(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  332. {
  333.   LONG lLogoDisplayTime;
  334.   ULONG ulTimerId;
  335.  
  336.   switch(msg)
  337.   {
  338.     case WM_INITDLG:
  339.       if (*((PBOOL)mp2))  
  340.       {
  341.         lLogoDisplayTime = PrfQueryProfileInt(HINI_USERPROFILE, "PM_ControlPanel", "LogoDisplayTime", -1L);
  342.         switch(lLogoDisplayTime)
  343.         {  
  344.           case 0: WinDismissDlg(hwnd, TRUE); break; /* logo is not displayed */
  345.           case -1: break; /* indefinite logo display */
  346.           default: ulTimerId = WinStartTimer(hab, hwnd, TI_LOGO, lLogoDisplayTime); break;
  347.         }
  348.       }
  349.       break;
  350.  
  351.     case WM_TIMER:
  352.       if (SHORT1FROMMP(mp1) == TI_LOGO)
  353.         WinPostMsg(hwnd, WM_COMMAND, NULL, NULL);
  354.       break;
  355.  
  356.     case WM_COMMAND:
  357.       /* OK has been pressed or timer messages has arrived */
  358.       WinStopTimer(hab, hwnd, ulTimerId);
  359.       WinDismissDlg(hwnd, TRUE);
  360.       break;
  361.  
  362.     default:
  363.       return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  364.   }
  365.   return (MRESULT)0;
  366. }
  367.  
  368. void InitOut(void)
  369. {
  370.   sprintf(szOut[0], "Queue information:");
  371.   sprintf(szOut[1], "  Queue name: %s", prqInfoDef.pszName);
  372.   sprintf(szOut[2], "  Default queue processor: %s", prqInfoDef.pszPrProc);
  373.   sprintf(szOut[3], "  Queue description: %s", prqInfoDef.pszComment);
  374.   sprintf(szOut[4], "  Default device driver: %s", prqInfoDef.pszDriverName);
  375.   sprintf(szOut[5], "  Print devices connected to queue: %s", prqInfoDef.pszPrinters);
  376.   sprintf(szOut[6], "");
  377.   sprintf(szOut[7], "Default form description: ");
  378.   sprintf(szOut[8], "  Form name: %s", hcInfoDef.szFormname);
  379.   sprintf(szOut[9], "  Width (left-to-right): %ld milimeters", hcInfoDef.cx);
  380.   sprintf(szOut[10], "  Height (top-to-bottom): %ld milimeters", hcInfoDef.cy);
  381.   sprintf(szOut[11], "  Number of pels between left and right clip limits: %ld", hcInfoDef.xPels);
  382.   sprintf(szOut[12], "  Number of pels between bottom and top clip limits: %ld", hcInfoDef.yPels);
  383.   sprintf(szOut[13], "");  
  384.   sprintf(szOut[14], "Device capabilities: ");
  385.   sprintf(szOut[15], "  Media width: %ld pels", alCaps[CAPS_WIDTH]);
  386.   sprintf(szOut[16], "  Media height: %ld pels", alCaps[CAPS_HEIGHT]);
  387.   sprintf(szOut[17], "  Media width: %ld chars", alCaps[CAPS_WIDTH_IN_CHARS]);
  388.   sprintf(szOut[18], "  Media height: %ld chars", alCaps[CAPS_HEIGHT_IN_CHARS]);
  389.   sprintf(szOut[19], "  Char height: %ld pels", alCaps[CAPS_CHAR_HEIGHT]);
  390.   sprintf(szOut[20], "  Number of device specific fonts: %ld", alCaps[CAPS_DEVICE_FONTS]);
  391.   sprintf(szOut[21], "  Number of distinct colors available on the device: %ld", alCaps[CAPS_PHYS_COLORS]);
  392.   sprintf(szOut[22], "  Horizontal resolution: %ld dpi", alCaps[CAPS_HORIZONTAL_FONT_RES]);
  393.   sprintf(szOut[23], "  Vertical resolution: %ld dpi", alCaps[CAPS_VERTICAL_FONT_RES]);
  394. }
  395.  
  396. VOID SetFont(void)
  397. {
  398.    HPS hpsPrn;
  399.    CHAR szFamily[FACESIZE];
  400.  
  401.    if (fFirst)
  402.    {
  403.      memset(&fontDlg, 0, sizeof(fontDlg));            /* initialize all fields */
  404.  
  405.      SpoolBeginInfo(&hpsPrn, 0UL, FALSE);
  406.      GpiQueryFontMetrics (hpsPrn, sizeof(FONTMETRICS), &fontMetrics);
  407.      SpoolEndInfo();
  408.  
  409.      /* create printer default font */
  410.      fontDlg.fAttrs.usCodePage = fontMetrics.usCodePage;
  411.      fontDlg.cbSize     = sizeof(FONTDLG);               
  412.      fontDlg.hpsScreen  = NULLHANDLE;
  413.      fontDlg.pszTitle      = "Printer fonts"; /* application supplied title        */
  414.      fontDlg.pszPreview    = "Hello queue/2"; /* string to print in preview window */
  415.      szFamily[0] = 0;
  416.      fontDlg.pszFamilyname = szFamily;      /* point to Family name of font    */
  417.      fontDlg.usFamilyBufLen = sizeof(szFamily); /*Length of family name buffer */
  418.      fontDlg.fl           = FNTS_CENTER;
  419.      fontDlg.clrFore      = CLR_BLACK; /* Selected foreground color       */
  420.      fontDlg.clrBack      = CLR_WHITE; /* Selected background color       */
  421.      fontDlg.pfnDlgProc = NULL;
  422.   }
  423.  
  424.    if (!fFirst)
  425.      fontDlg.fl = FNTS_CENTER | FNTS_INITFROMFATTRS;
  426.  
  427.    /* display font dlg */
  428.    SpoolBeginInfo(&hpsPrn, 0UL, FALSE);
  429.    fontDlg.hpsPrinter = hpsPrn;            /* Printer presentation space */
  430.    WinFontDlg(HWND_DESKTOP, hwndFrame, &fontDlg) == NULLHANDLE;
  431.    SpoolEndInfo();
  432.  
  433.    if (fontDlg.lReturn == DID_CANCEL && fFirst)
  434.      fFirst = TRUE;
  435.    else
  436.      fFirst = FALSE;
  437. }
  438.