home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / printq / helloq1 / hello.c next >
Text File  |  1993-08-19  |  13KB  |  364 lines

  1. /* hello.c: source code file for Hello queue/2 */
  2. /* Version 1.0, (C) Peter Wansch, 1993         */
  3. /* created: 93-5-7                             */
  4. /* modified: 93-8-16                           */
  5.  
  6. #define INCL_PM    
  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. int main(VOID)
  26. {
  27.   QMSG  qmsg;                                 /* message queue handle               */
  28.   FONTMETRICS fm;                             /* default screen font metrics        */
  29.   HPS hps;                                    /* screen ps handle for retrieving fm */
  30.   ULONG flCreate = FCF_STANDARD;              /* frame creation flag                */
  31.   HWND hwndClient;                            /* client window handle               */
  32.   HMQ hmq;                                    /* queue handle (1st thread)          */
  33.   BOOL fIniQ = FALSE;
  34.  
  35.   /* register application to Presentation Manager */
  36.   hab = WinInitialize(0);
  37.   if(!hab)
  38.   {
  39.     WinAlarm(HWND_DESKTOP, WA_ERROR); 
  40.     exit(RETURN_ERROR);
  41.   }
  42.  
  43.   /* initialize PRINTQ */
  44.   if (!SpoolInitialize(hab, NULL, &fIniQ))
  45.   {
  46.     WinAlarm(HWND_DESKTOP, WA_ERROR); 
  47.     WinTerminate(hab);
  48.     exit(RETURN_ERROR);
  49.   }
  50.  
  51.   /* create message queue */
  52.   hmq = WinCreateMsgQueue( hab, 0 );
  53.   if(!hmq)
  54.   {
  55.     WinAlarm(HWND_DESKTOP, WA_ERROR); 
  56.     SpoolTerminate();
  57.     WinTerminate(hab);
  58.     exit(RETURN_ERROR);
  59.   }
  60.  
  61.   /* display timed logo */
  62.   if (!WinDlgBox(HWND_DESKTOP, HWND_DESKTOP, (PFNWP)dpProdInfo, NULLHANDLE, DB_PRODINFO, (PBOOL)&fIni))
  63.   {
  64.     WinDestroyMsgQueue(hmq);
  65.     SpoolTerminate();
  66.     WinTerminate(hab);
  67.     exit(RETURN_ERROR);
  68.   }
  69.   fIni = FALSE;
  70.  
  71.   /* register private window class for application window */
  72.   if (!WinRegisterClass(hab,(PSZ)"Hello", wpMain, CS_SIZEREDRAW, 0))
  73.   {
  74.     WinDestroyMsgQueue(hmq);
  75.     SpoolTerminate();
  76.     WinTerminate(hab);
  77.     exit(RETURN_ERROR);
  78.   }
  79.  
  80.   /* create application window */
  81.   hwndFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE, &flCreate, "Hello", "Hello queue/2", 
  82.                                  WS_VISIBLE, NULLHANDLE, WD_MAIN, &hwndClient);
  83.   if (!hwndFrame)
  84.   {
  85.     WinDestroyMsgQueue(hmq);
  86.     SpoolTerminate();
  87.     WinTerminate(hab);
  88.     exit(RETURN_ERROR);
  89.   }
  90.  
  91.   /* query default screen font metrics to position strings in the application window */
  92.   hps = WinGetPS (HWND_DESKTOP);
  93.   GpiQueryFontMetrics (hps, sizeof(FONTMETRICS), &fm);
  94.   WinReleasePS(hps); 
  95.   lMaxAscender=fm.lMaxAscender;
  96.   lMaxBaselineExt=fm.lMaxBaselineExt;
  97.  
  98.   /* query required pointers */
  99.   hptrWait = WinQuerySysPointer(HWND_DESKTOP, SPTR_WAIT, FALSE);
  100.   hptrSystem = WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE);        
  101.  
  102.   /* main message loop */
  103.   while(WinGetMsg(hab, &qmsg, (HWND)NULL, 0, 0))
  104.     WinDispatchMsg(hab, &qmsg);
  105.  
  106.   /* terminate application */
  107.   WinDestroyWindow(hwndFrame);        
  108.   WinDestroyMsgQueue(hmq);   
  109.          
  110.   /* de-register from PRINTQ and Presentation Manager */
  111.   SpoolTerminate();
  112.   WinTerminate(hab);                  
  113.   return(0);
  114. }
  115.  
  116. MRESULT EXPENTRY wpMain (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  117. {
  118.   HPS hpsPrn;
  119.   POINTL pt;
  120.   FONTMETRICS fm;
  121.   LONG lprnMaxAscender, lprnMaxBaselineExt;
  122.   SHORT i;
  123.   USHORT usJobId;
  124.   BOOL fExceed;
  125.   RECTL rclUpdate, rclClient;
  126.   POINTL  aptl[LENGTH_STRING+1];
  127.  
  128.   switch( msg )
  129.   {
  130.     case WM_CREATE:
  131.       InitOut();
  132.       return (MRESULT)FALSE;
  133.  
  134.     case WM_ERASEBACKGROUND:
  135.       return (MRESULT)TRUE;
  136.  
  137.     case WM_COMMAND:
  138.       switch (SHORT1FROMMP(mp1))
  139.       {
  140.         case MI_SETUP:
  141.           /* display Printer setup dialog box */
  142.           WinDlgBox(HWND_DESKTOP, hwnd, dpQueryQInfo, (HMODULE)0, DB_QUERYPRINT, NULL);
  143.           InitOut();
  144.           WinInvalidateRegion(hwnd, NULLHANDLE, FALSE);
  145.           SpoolEndSetup(); 
  146.           break;
  147.  
  148.         case MI_PRINT:
  149.           /* print text displayed in the application window */
  150.           WinSetPointer(HWND_DESKTOP, hptrWait);
  151.           fExceed = FALSE;
  152.  
  153.           /* obtain an information printer presentation space to query */
  154.           /* necessary font metrics of the selected font */
  155.           SpoolBeginInfo(&hpsPrn, 0UL, FALSE);
  156.           GpiQueryFontMetrics (hpsPrn, sizeof(FONTMETRICS), &fm);
  157.           lprnMaxAscender=fm.lMaxAscender;
  158.           lprnMaxBaselineExt=fm.lMaxBaselineExt;
  159.           SpoolEndInfo();
  160.  
  161.           /* change the application title bar text to inform user of spooling action */
  162.           WinSetWindowText(hwndFrame, (PSZ)"Spooling ...");
  163.  
  164.           /* begin a print job and obtain a handle to the printer presentation space */
  165.           SpoolBeginSpool("Printer Capabilities", "Printer Caps", "COP=1", 0UL, &hpsPrn, FALSE);
  166.  
  167.           /* check if the lines are within page boundaries */
  168.           for (i=0; i < NUM_OF_LINES && !fExceed; i++)
  169.           {
  170.             GpiQueryCharStringPosAt(hpsPrn, &ptlStart, 0L, (LONG)strlen(szOut[i]), (PSZ)szOut[i], NULL, aptl);
  171.             if (aptl[strlen(szOut[i])].x > alCaps[CAPS_WIDTH])
  172.               fExceed = TRUE;
  173.           }
  174.  
  175.           /* inform user if line(s) exceed page boundaries */
  176.           if (fExceed)
  177.             if (WinMessageBox(HWND_DESKTOP, hwnd, "Line(s) will exceed right margin of selected form. Print anyway?",
  178.                               (PCH) "Hello/2", ID_MESSAGEBOX, MB_YESNO | MB_APPLMODAL | MB_MOVEABLE | MB_ICONQUESTION) == MBID_NO)
  179.             {
  180.               /* the user wants to abort the job */
  181.               SpoolAbortSpool();
  182.               WinSetPointer(HWND_DESKTOP, hptrSystem);
  183.               WinSetWindowText(hwndFrame, "Hello queue/2");
  184.               break;
  185.             }
  186.  
  187.           /* print lines */
  188.           pt.x = 0; pt.y = hcInfoDef.yPels-lprnMaxAscender;
  189.           for (i=0; i < NUM_OF_LINES; i++)
  190.           {
  191.             /* form feed */
  192.             if (i%alCaps[CAPS_HEIGHT_IN_CHARS]==0 && i>0)
  193.             {
  194.               SpoolNewFrame();
  195.               pt.y = hcInfoDef.yPels-lprnMaxAscender;
  196.             }
  197.             GpiCharStringAt(hpsPrn, &pt, (LONG)strlen(szOut[i]), (PSZ)szOut[i]);
  198.             pt.y-=lprnMaxBaselineExt;
  199.           }
  200.  
  201.           /* end print job */
  202.           SpoolEndSpool(&usJobId);
  203.  
  204.           /* restore arrow pointer an set old application title bar text */
  205.           WinSetPointer(HWND_DESKTOP, hptrSystem);
  206.           WinSetWindowText(hwndFrame, (PSZ)"Hello queue/2");
  207.           break;
  208.  
  209.         case MI_INFO:
  210.           /* display product information dialog box */
  211.           WinDlgBox( HWND_DESKTOP, hwndFrame, (PFNWP)dpProdInfo, (HMODULE)0, DB_PRODINFO, (PBOOL)&fIni);           /* initialization data          */
  212.           break;
  213.  
  214.         default:
  215.           return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  216.       }
  217.       break;
  218.  
  219.       case WM_PAINT:
  220.       {
  221.         HPS hps;
  222.         /* paint client window */
  223.         /* begin drawing */
  224.         hps = WinBeginPaint( hwnd, (HPS)NULL, &rclUpdate);
  225.  
  226.         /* paint invalid rectangle first */
  227.         WinFillRect (hps, &rclUpdate, SYSCLR_WINDOW);
  228.  
  229.         /* draw lines */
  230.         WinQueryWindowRect(hwnd, &rclClient);
  231.         pt.x = 0; pt.y = rclClient.yTop-lMaxAscender;
  232.         for (i=0; i < NUM_OF_LINES; i++)
  233.         {
  234.           GpiCharStringAt(hps, &pt, (LONG)strlen(szOut[i]), (PSZ)szOut[i]);
  235.           pt.y-=lMaxBaselineExt;
  236.         }
  237.  
  238.       WinEndPaint(hps);               /* drawing is complete */
  239.       }
  240.       break;
  241.       
  242.     case WM_CLOSE:
  243.       WinPostMsg( hwnd, WM_QUIT, NULL, NULL);     /* cause termination */
  244.       break;
  245.  
  246.     default:
  247.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  248.   }
  249.   return (MRESULT) FALSE;
  250. }
  251.  
  252. MRESULT EXPENTRY dpQueryQInfo( HWND hwndDlg, ULONG msg, MPARAM mp1, MPARAM mp2 )
  253. {
  254.   static BOOL fVisitedJobProp;
  255.   HWND hwndListBox;
  256.  
  257.   switch (msg)
  258.   {
  259.     case WM_INITDLG:
  260.       fVisitedJobProp = FALSE;
  261.       hwndListBox = WinWindowFromID(hwndDlg, LB_QUEUES);
  262.       /* fill list box and select default printer */
  263.       SpoolBeginSetup(hwndListBox);
  264.       break;
  265.  
  266.     case WM_CONTROL:
  267.       /* list box item is double-clicked */
  268.       if (SHORT2FROMMP(mp1) == LN_ENTER && SHORT1FROMMP(mp1) == LB_QUEUES)
  269.         WinPostMsg(hwndDlg, WM_COMMAND, MPFROM2SHORT(DID_OK, 0), NULL);
  270.       break;
  271.      
  272.     case WM_COMMAND:                    /* posted by push button or key */
  273.       switch(SHORT1FROMMP(mp1))       /* extract the command value */
  274.       {
  275.         case PB_JOBPROP:
  276.           /* Job properties button has been chosen, display Jop properties dialog box */
  277.           if (!SpoolJobProp())
  278.             WinAlarm(HWND_DESKTOP, WA_ERROR);
  279.           fVisitedJobProp = TRUE;
  280.           break;
  281.         case DID_OK:            /* the OK push button or key */
  282.           /* set the currently hilited printer the default */
  283.           SpoolSetDef();
  284.           /* close dialog */
  285.           WinDismissDlg(hwndDlg, TRUE);
  286.           break;
  287.         case DID_CANCEL:        /* the Cancel pushbutton or Escape key */
  288.           WinDismissDlg(hwndDlg, fVisitedJobProp ? TRUE : FALSE);   /* removes the dialog window */
  289.           break;
  290.         default:
  291.           break;
  292.       }
  293.       break;
  294.     default:
  295.       return WinDefDlgProc( hwndDlg, msg, mp1, mp2 );
  296.   }
  297.   return (MRESULT) FALSE;
  298. }
  299.  
  300. MRESULT EXPENTRY dpProdInfo(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  301. {
  302.   LONG lLogoDisplayTime;
  303.   ULONG ulTimerId;
  304.  
  305.   switch(msg)
  306.   {
  307.     case WM_INITDLG:
  308.       if (*((PBOOL)mp2))  
  309.       {
  310.         lLogoDisplayTime = PrfQueryProfileInt(HINI_USERPROFILE, "PM_ControlPanel", "LogoDisplayTime", -1L);
  311.         switch(lLogoDisplayTime)
  312.         {  
  313.           case 0: WinDismissDlg(hwnd, TRUE); break; /* logo is not displayed */
  314.           case -1: break; /* indefinite logo display */
  315.           default: ulTimerId = WinStartTimer(hab, hwnd, TI_LOGO, lLogoDisplayTime); break;
  316.         }
  317.       }
  318.       break;
  319.  
  320.     case WM_TIMER:
  321.       if (SHORT1FROMMP(mp1) == TI_LOGO)
  322.         WinPostMsg(hwnd, WM_COMMAND, NULL, NULL);
  323.       break;
  324.  
  325.     case WM_COMMAND:
  326.       /* OK has been pressed or timer messages has arrived */
  327.       WinStopTimer(hab, hwnd, ulTimerId);
  328.       WinDismissDlg(hwnd, TRUE);
  329.       break;
  330.  
  331.     default:
  332.       return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  333.   }
  334.   return (MRESULT)0;
  335. }
  336.  
  337. void InitOut(void)
  338. {
  339.   sprintf(szOut[0], "Queue information:");
  340.   sprintf(szOut[1], "  Queue name: %s", prqInfoDef.pszName);
  341.   sprintf(szOut[2], "  Default queue processor: %s", prqInfoDef.pszPrProc);
  342.   sprintf(szOut[3], "  Queue description: %s", prqInfoDef.pszComment);
  343.   sprintf(szOut[4], "  Default device driver: %s", prqInfoDef.pszDriverName);
  344.   sprintf(szOut[5], "  Print devices connected to queue: %s", prqInfoDef.pszPrinters);
  345.   sprintf(szOut[6], "");
  346.   sprintf(szOut[7], "Default form description: ");
  347.   sprintf(szOut[8], "  Form name: %s", hcInfoDef.szFormname);
  348.   sprintf(szOut[9], "  Width (left-to-right): %ld milimeters", hcInfoDef.cx);
  349.   sprintf(szOut[10], "  Height (top-to-bottom): %ld milimeters", hcInfoDef.cy);
  350.   sprintf(szOut[11], "  Number of pels between left and right clip limits: %ld", hcInfoDef.xPels);
  351.   sprintf(szOut[12], "  Number of pels between bottom and top clip limits: %ld", hcInfoDef.yPels);
  352.   sprintf(szOut[13], "");  
  353.   sprintf(szOut[14], "Device capabilities: ");
  354.   sprintf(szOut[15], "  Media width: %ld pels", alCaps[CAPS_WIDTH]);
  355.   sprintf(szOut[16], "  Media height: %ld pels", alCaps[CAPS_HEIGHT]);
  356.   sprintf(szOut[17], "  Media width: %ld chars", alCaps[CAPS_WIDTH_IN_CHARS]);
  357.   sprintf(szOut[18], "  Media height: %ld chars", alCaps[CAPS_HEIGHT_IN_CHARS]);
  358.   sprintf(szOut[19], "  Char height: %ld pels", alCaps[CAPS_CHAR_HEIGHT]);
  359.   sprintf(szOut[20], "  Number of device specific fonts: %ld", alCaps[CAPS_DEVICE_FONTS]);
  360.   sprintf(szOut[21], "  Number of distinct colors available on the device: %ld", alCaps[CAPS_PHYS_COLORS]);
  361.   sprintf(szOut[22], "  Horizontal resolution: %ld dpi", alCaps[CAPS_HORIZONTAL_FONT_RES]);
  362.   sprintf(szOut[23], "  Vertical resolution: %ld dpi", alCaps[CAPS_VERTICAL_FONT_RES]);
  363. }
  364.