home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / printq14.zip / HELLOQ1 / HELLO.C next >
Text File  |  1994-04-25  |  16KB  |  433 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_PRINT_BITMAP:
  210.           {
  211.             HDC hdcPrnMem, hdcMem;
  212.             HPS hpsPrnMem, hpsMem, hpsToPrint;
  213.             SIZEL sizl = {0, 0};
  214.             BITMAPINFOHEADER2 bmih;
  215.             LONG alBitmapStuff[2];
  216.             SWP swp;
  217.             HBITMAP hbmToPrint;
  218.             POINTL apointl[3];
  219.  
  220.             /* print desktop window as bitmap */
  221.             WinSetPointer(HWND_DESKTOP, hptrWait);
  222.             fExceed = FALSE;
  223.  
  224.             /* change the application title bar text to inform user of spooling action */
  225.             WinSetWindowText(hwndFrame, (PSZ)"Spooling ...");
  226.  
  227.             /* begin a print job and obtain a handle to the printer presentation space */
  228.             SpoolBeginSpool("Bitmap", "Bitmap", "COP=1", 0UL, &hpsPrn, FALSE);
  229.  
  230.             /* get device context and presentation space compatible with printer */
  231.             /* the imported PrintQ variable hdcPrn contains a handle to the printer dc */
  232.             hdcPrnMem = DevOpenDC(hab, OD_MEMORY, "*", 0, NULL, hdcPrn);
  233.             hpsPrnMem = GpiCreatePS(hab, hdcPrnMem, &sizl, PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
  234.  
  235.             /* get device context and presentation space compatible with display */
  236.             hdcMem = DevOpenDC(hab, OD_MEMORY, "*", 0, NULL, NULLHANDLE);
  237.             hpsMem = GpiCreatePS(hab, hdcMem, &sizl, PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
  238.  
  239.             /* get PS for the frame window */
  240.             hpsToPrint = WinGetPS(HWND_DESKTOP);
  241.  
  242.             WinQueryWindowPos(HWND_DESKTOP, &swp);
  243.             bmih.cbFix = sizeof(BITMAPINFOHEADER2);
  244.             bmih.cx = swp.cx;
  245.             bmih.cy = swp.cy;
  246.             GpiQueryDeviceBitmapFormats(hpsToPrint, 2L, alBitmapStuff);
  247.             bmih.cPlanes = (USHORT) alBitmapStuff[0];
  248.             bmih.cBitCount = (USHORT) alBitmapStuff[1];
  249.  
  250.             hbmToPrint = GpiCreateBitmap(hpsMem, &bmih, 0, NULL, NULL);
  251.             GpiSetBitmap(hpsMem, hbmToPrint);
  252.             apointl[0].x = 0;
  253.             apointl[0].y = 0;
  254.             apointl[1].x = swp.cx;
  255.             apointl[1].y = swp.cy;
  256.             apointl[2].x = 0;
  257.             apointl[2].y = 0;
  258.             GpiBitBlt(hpsMem, hpsToPrint, 3L, apointl, ROP_SRCCOPY, BBO_IGNORE);
  259.             GpiSetBitmap(hpsMem, NULLHANDLE);
  260.             GpiSetBitmap(hpsPrnMem, hbmToPrint);
  261.             GpiBitBlt(hpsPrn, hpsPrnMem, 3L, apointl, ROP_SRCCOPY, BBO_IGNORE);
  262.             GpiSetBitmap(hpsMem, NULLHANDLE);
  263.             GpiDeleteBitmap(hbmToPrint);
  264.             GpiDestroyPS(hpsMem);
  265.             GpiDestroyPS(hpsPrnMem);
  266.             DevCloseDC(hdcMem);
  267.             DevCloseDC(hdcPrnMem);
  268.            
  269.             /* end print job */
  270.             SpoolEndSpool(&usJobId);
  271.  
  272.             /* restore arrow pointer an set old application title bar text */
  273.             WinSetPointer(HWND_DESKTOP, hptrSystem);
  274.             WinSetWindowText(hwndFrame, (PSZ)"Hello queue/2");
  275.           }
  276.           break;
  277.  
  278.         case MI_INFO:
  279.           /* display product information dialog box */
  280.           WinDlgBox( HWND_DESKTOP, hwndFrame, (PFNWP)dpProdInfo, (HMODULE)0, DB_PRODINFO, (PBOOL)&fIni);           /* initialization data          */
  281.           break;
  282.  
  283.         default:
  284.           return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  285.       }
  286.       break;
  287.  
  288.       case WM_PAINT:
  289.       {
  290.         HPS hps;
  291.         /* paint client window */
  292.         /* begin drawing */
  293.         hps = WinBeginPaint( hwnd, (HPS)NULL, &rclUpdate);
  294.  
  295.         /* paint invalid rectangle first */
  296.         WinFillRect (hps, &rclUpdate, SYSCLR_WINDOW);
  297.  
  298.         /* draw lines */
  299.         WinQueryWindowRect(hwnd, &rclClient);
  300.         pt.x = 0; pt.y = rclClient.yTop-lMaxAscender;
  301.         for (i=0; i < NUM_OF_LINES; i++)
  302.         {
  303.           GpiCharStringAt(hps, &pt, (LONG)strlen(szOut[i]), (PSZ)szOut[i]);
  304.           pt.y-=lMaxBaselineExt;
  305.         }
  306.  
  307.       WinEndPaint(hps);               /* drawing is complete */
  308.       }
  309.       break;
  310.       
  311.     case WM_CLOSE:
  312.       WinPostMsg( hwnd, WM_QUIT, NULL, NULL);     /* cause termination */
  313.       break;
  314.  
  315.     default:
  316.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  317.   }
  318.   return (MRESULT) FALSE;
  319. }
  320.  
  321. MRESULT EXPENTRY dpQueryQInfo( HWND hwndDlg, ULONG msg, MPARAM mp1, MPARAM mp2 )
  322. {
  323.   static BOOL fVisitedJobProp;
  324.   HWND hwndListBox;
  325.  
  326.   switch (msg)
  327.   {
  328.     case WM_INITDLG:
  329.       fVisitedJobProp = FALSE;
  330.       hwndListBox = WinWindowFromID(hwndDlg, LB_QUEUES);
  331.       /* fill list box and select default printer */
  332.       SpoolBeginSetup(hwndListBox);
  333.       break;
  334.  
  335.     case WM_CONTROL:
  336.       /* list box item is double-clicked */
  337.       if (SHORT2FROMMP(mp1) == LN_ENTER && SHORT1FROMMP(mp1) == LB_QUEUES)
  338.         WinPostMsg(hwndDlg, WM_COMMAND, MPFROM2SHORT(DID_OK, 0), NULL);
  339.       break;
  340.      
  341.     case WM_COMMAND:                    /* posted by push button or key */
  342.       switch(SHORT1FROMMP(mp1))       /* extract the command value */
  343.       {
  344.         case PB_JOBPROP:
  345.           /* Job properties button has been chosen, display Jop properties dialog box */
  346.           if (!SpoolJobProp())
  347.             WinAlarm(HWND_DESKTOP, WA_ERROR);
  348.           fVisitedJobProp = TRUE;
  349.           break;
  350.         case DID_OK:            /* the OK push button or key */
  351.           /* set the currently hilited printer the default */
  352.           SpoolSetDef();
  353.           /* close dialog */
  354.           WinDismissDlg(hwndDlg, TRUE);
  355.           break;
  356.         case DID_CANCEL:        /* the Cancel pushbutton or Escape key */
  357.           WinDismissDlg(hwndDlg, fVisitedJobProp ? TRUE : FALSE);   /* removes the dialog window */
  358.           break;
  359.         default:
  360.           break;
  361.       }
  362.       break;
  363.     default:
  364.       return WinDefDlgProc( hwndDlg, msg, mp1, mp2 );
  365.   }
  366.   return (MRESULT) FALSE;
  367. }
  368.  
  369. MRESULT EXPENTRY dpProdInfo(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  370. {
  371.   LONG lLogoDisplayTime;
  372.   ULONG ulTimerId;
  373.  
  374.   switch(msg)
  375.   {
  376.     case WM_INITDLG:
  377.       if (*((PBOOL)mp2))  
  378.       {
  379.         lLogoDisplayTime = PrfQueryProfileInt(HINI_USERPROFILE, "PM_ControlPanel", "LogoDisplayTime", -1L);
  380.         switch(lLogoDisplayTime)
  381.         {  
  382.           case 0: WinDismissDlg(hwnd, TRUE); break; /* logo is not displayed */
  383.           case -1: break; /* indefinite logo display */
  384.           default: ulTimerId = WinStartTimer(hab, hwnd, TI_LOGO, lLogoDisplayTime); break;
  385.         }
  386.       }
  387.       break;
  388.  
  389.     case WM_TIMER:
  390.       if (SHORT1FROMMP(mp1) == TI_LOGO)
  391.         WinPostMsg(hwnd, WM_COMMAND, NULL, NULL);
  392.       break;
  393.  
  394.     case WM_COMMAND:
  395.       /* OK has been pressed or timer messages has arrived */
  396.       WinStopTimer(hab, hwnd, ulTimerId);
  397.       WinDismissDlg(hwnd, TRUE);
  398.       break;
  399.  
  400.     default:
  401.       return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  402.   }
  403.   return (MRESULT)0;
  404. }
  405.  
  406. void InitOut(void)
  407. {
  408.   sprintf(szOut[0], "Queue information:");
  409.   sprintf(szOut[1], "  Queue name: %s", prqInfoDef.pszName);
  410.   sprintf(szOut[2], "  Default queue processor: %s", prqInfoDef.pszPrProc);
  411.   sprintf(szOut[3], "  Queue description: %s", prqInfoDef.pszComment);
  412.   sprintf(szOut[4], "  Default device driver: %s", prqInfoDef.pszDriverName);
  413.   sprintf(szOut[5], "  Print devices connected to queue: %s", prqInfoDef.pszPrinters);
  414.   sprintf(szOut[6], "");
  415.   sprintf(szOut[7], "Default form description: ");
  416.   sprintf(szOut[8], "  Form name: %s", hcInfoDef.szFormname);
  417.   sprintf(szOut[9], "  Width (left-to-right): %ld milimeters", hcInfoDef.cx);
  418.   sprintf(szOut[10], "  Height (top-to-bottom): %ld milimeters", hcInfoDef.cy);
  419.   sprintf(szOut[11], "  Number of pels between left and right clip limits: %ld", hcInfoDef.xPels);
  420.   sprintf(szOut[12], "  Number of pels between bottom and top clip limits: %ld", hcInfoDef.yPels);
  421.   sprintf(szOut[13], "");  
  422.   sprintf(szOut[14], "Device capabilities: ");
  423.   sprintf(szOut[15], "  Media width: %ld pels", alCaps[CAPS_WIDTH]);
  424.   sprintf(szOut[16], "  Media height: %ld pels", alCaps[CAPS_HEIGHT]);
  425.   sprintf(szOut[17], "  Media width: %ld chars", alCaps[CAPS_WIDTH_IN_CHARS]);
  426.   sprintf(szOut[18], "  Media height: %ld chars", alCaps[CAPS_HEIGHT_IN_CHARS]);
  427.   sprintf(szOut[19], "  Char height: %ld pels", alCaps[CAPS_CHAR_HEIGHT]);
  428.   sprintf(szOut[20], "  Number of device specific fonts: %ld", alCaps[CAPS_DEVICE_FONTS]);
  429.   sprintf(szOut[21], "  Number of distinct colors available on the device: %ld", alCaps[CAPS_PHYS_COLORS]);
  430.   sprintf(szOut[22], "  Horizontal resolution: %ld dpi", alCaps[CAPS_HORIZONTAL_FONT_RES]);
  431.   sprintf(szOut[23], "  Vertical resolution: %ld dpi", alCaps[CAPS_VERTICAL_FONT_RES]);
  432. }
  433.