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

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