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

  1. /* hello.c: source code file for Hello queue/2 */
  2. /* Version 1.2 MT, (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;                           /* screen font metric information */
  21. LONG lMaxBaselineExt;                        /* screen font metric information */
  22. BOOL fPrintThreadStarted = FALSE;            /* flag to indicate if a print    */
  23.                                              /* job has been started in a      */
  24.                                              /* secondary thread               */
  25. unsigned long tid;                           /* print thread id                */
  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.   /* main message loop */
  101.   while(WinGetMsg(hab, &qmsg, (HWND)NULL, 0, 0))
  102.     WinDispatchMsg(hab, &qmsg);
  103.  
  104.   /* terminate application */
  105.   WinDestroyWindow(hwndFrame);        
  106.   WinDestroyMsgQueue(hmq);   
  107.          
  108.   /* de-register from PRINTQ and Presentation Manager */
  109.   SpoolTerminate();
  110.   WinTerminate(hab);                  
  111.   return(0);
  112. }
  113.  
  114. MRESULT EXPENTRY wpMain (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  115. {
  116.   RECTL rclUpdate, rclClient;
  117.   SHORT i;
  118.   POINTL pt;
  119.   CHAR szJobInfo[128];
  120.  
  121.   switch( msg )
  122.   {
  123.     case WM_CREATE:
  124.       InitOut();
  125.       return (MRESULT)FALSE;
  126.  
  127.     case WM_ERASEBACKGROUND:
  128.       return (MRESULT)TRUE;
  129.  
  130.     case WM_INITMENU:
  131.       /* disable Print and Printer setup menu items while there is a print job */
  132.       WinEnableMenuItem (HWNDFROMMP(mp2), MI_SETUP, !fPrintThreadStarted);
  133.       WinEnableMenuItem (HWNDFROMMP(mp2), MI_PRINT, !fPrintThreadStarted);
  134.       /* enable Abort print job after the job has been started */
  135.       /* a job is started when SpoolBeginSpool has been called */
  136.       WinEnableMenuItem (HWNDFROMMP(mp2), MI_ABORT, SpoolIsJobStarted());
  137.       return (MRESULT)(0);
  138.  
  139.     case WMP_JOB_DONE:
  140.       /* print job has ended */
  141.       fPrintThreadStarted = FALSE;
  142.  
  143.       /* give the user information about the print job */
  144.       if (LONGFROMMP(mp2))
  145.         sprintf(szJobInfo, "Print job with id %ld has been spooled.", LONGFROMMP(mp1));
  146.       else
  147.         sprintf(szJobInfo, "Print job has been aborted.");
  148.       WinMessageBox(HWND_DESKTOP, hwnd, szJobInfo,
  149.                     (PCH) "Hello/2", ID_MESSAGEBOX, MB_OK | MB_APPLMODAL | MB_MOVEABLE | MB_INFORMATION);
  150.  
  151.       /* restore old window title */
  152.       WinSetWindowText(hwndFrame, (PSZ)"Hello queue/2");
  153.       break;
  154.  
  155.     case WM_COMMAND:
  156.       switch (SHORT1FROMMP(mp1))
  157.       {
  158.         case MI_SETUP:
  159.           /* display Printer setup dialog box */
  160.           WinDlgBox(HWND_DESKTOP, hwnd, dpQueryQInfo, (HMODULE)0, DB_QUERYPRINT, NULL);
  161.           InitOut();
  162.           WinInvalidateRegion(hwnd, NULLHANDLE, FALSE);
  163.           SpoolEndSetup();
  164.           break;
  165.  
  166.         case MI_ABORT:
  167.           /* abort current print job */
  168.           if (!SpoolAbortSpool())
  169.             WinMessageBox(HWND_DESKTOP, hwnd, "Unable to abort print job.",
  170.                               (PCH) "Hello/2", ID_MESSAGEBOX, MB_OK | MB_APPLMODAL | MB_MOVEABLE | MB_ERROR);
  171.           break;
  172.  
  173.         case MI_PRINT:
  174.           /* print text displayed in the application window */
  175.           /* create print thread */
  176.           fPrintThreadStarted = TRUE;
  177.           tid = _beginthread(PrintInfo, NULL, STACK_SIZE, NULL);
  178.           if (tid == -1)
  179.           {
  180.             WinMessageBox(HWND_DESKTOP, hwnd, "Unable to create print thread.",
  181.                               (PCH) "Hello/2", ID_MESSAGEBOX, MB_OK | MB_APPLMODAL | MB_MOVEABLE | MB_ERROR);
  182.             fPrintThreadStarted = FALSE;
  183.           }
  184.           else
  185.             /* print thread was successfully created */
  186.             WinSetWindowText(hwndFrame, (PSZ)"Printing in secondary thread is in progress...");
  187.           break;
  188.  
  189.         case MI_INFO:
  190.           /* display product information dialog box */
  191.           WinDlgBox( HWND_DESKTOP, hwndFrame, (PFNWP)dpProdInfo, (HMODULE)0, DB_PRODINFO, (PBOOL)&fIni);           /* initialization data          */
  192.           break;
  193.  
  194.         default:
  195.           return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  196.       }
  197.       break;
  198.  
  199.       case WM_PAINT:
  200.       {
  201.         HPS hps;
  202.         /* paint client window */
  203.         /* begin drawing */
  204.         hps = WinBeginPaint( hwnd, (HPS)NULL, &rclUpdate);
  205.  
  206.         /* paint invalid rectangle first */
  207.         WinFillRect (hps, &rclUpdate, SYSCLR_WINDOW);
  208.  
  209.         /* draw lines */
  210.         WinQueryWindowRect(hwnd, &rclClient);
  211.         pt.x = 0; pt.y = rclClient.yTop-lMaxAscender;
  212.         for (i=0; i < NUM_OF_LINES; i++)
  213.         {
  214.           GpiCharStringAt(hps, &pt, (LONG)strlen(szOut[i]), (PSZ)szOut[i]);
  215.           pt.y-=lMaxBaselineExt;
  216.         }
  217.  
  218.       WinEndPaint(hps);               /* drawing is complete */
  219.       }
  220.       break;
  221.       
  222.     case WM_CLOSE:
  223.       WinPostMsg( hwnd, WM_QUIT, NULL, NULL);     /* cause termination */
  224.       break;
  225.     
  226.     case WM_DESTROY:
  227.       /* if a job has been started it is aborted */
  228.       if (SpoolIsJobStarted())
  229.         SpoolAbortSpool();
  230.       /* we have to wait until the print thread terminates (if there is one) */
  231.       DosWaitThread(&tid, DCWW_WAIT);
  232.       break;
  233.  
  234.     default:
  235.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  236.   }
  237.   return (MRESULT) FALSE;
  238. }
  239.  
  240. MRESULT EXPENTRY dpQueryQInfo( HWND hwndDlg, ULONG msg, MPARAM mp1, MPARAM mp2 )
  241. {
  242.   static BOOL fVisitedJobProp;
  243.   HWND hwndListBox;
  244.  
  245.   switch (msg)
  246.   {
  247.     case WM_INITDLG:
  248.       fVisitedJobProp = FALSE;
  249.       hwndListBox = WinWindowFromID(hwndDlg, LB_QUEUES);
  250.       /* fill list box and select default printer */
  251.       SpoolBeginSetup(hwndListBox);
  252.       break;
  253.  
  254.     case WM_CONTROL:
  255.       /* list box item is double-clicked */
  256.       if (SHORT2FROMMP(mp1) == LN_ENTER && SHORT1FROMMP(mp1) == LB_QUEUES)
  257.         WinPostMsg(hwndDlg, WM_COMMAND, MPFROM2SHORT(DID_OK, 0), NULL);
  258.       break;
  259.      
  260.     case WM_COMMAND:                    /* posted by push button or key */
  261.       switch(SHORT1FROMMP(mp1))       /* extract the command value */
  262.       {
  263.         case PB_JOBPROP:
  264.           /* Job properties button has been chosen, display Jop properties dialog box */
  265.           if (!SpoolJobProp())
  266.             WinAlarm(HWND_DESKTOP, WA_ERROR);
  267.           fVisitedJobProp = TRUE;
  268.           break;
  269.         case DID_OK:            /* the OK push button or key */
  270.           /* set the currently hilited printer the default */
  271.           SpoolSetDef();
  272.           /* close dialog */
  273.           WinDismissDlg(hwndDlg, TRUE);
  274.           break;
  275.         case DID_CANCEL:        /* the Cancel pushbutton or Escape key */
  276.           WinDismissDlg(hwndDlg, fVisitedJobProp ? TRUE : FALSE);   /* removes the dialog window */
  277.           break;
  278.         default:
  279.           break;
  280.       }
  281.       break;
  282.     default:
  283.       return WinDefDlgProc( hwndDlg, msg, mp1, mp2 );
  284.   }
  285.   return (MRESULT) FALSE;
  286. }
  287.  
  288. MRESULT EXPENTRY dpProdInfo(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  289. {
  290.   LONG lLogoDisplayTime;
  291.   ULONG ulTimerId;
  292.  
  293.   switch(msg)
  294.   {
  295.     case WM_INITDLG:
  296.       if (*((PBOOL)mp2))  
  297.       {
  298.         lLogoDisplayTime = PrfQueryProfileInt(HINI_USERPROFILE, "PM_ControlPanel", "LogoDisplayTime", -1L);
  299.         switch(lLogoDisplayTime)
  300.         {  
  301.           case 0: WinDismissDlg(hwnd, TRUE); break; /* logo is not displayed */
  302.           case -1: break; /* indefinite logo display */
  303.           default: ulTimerId = WinStartTimer(hab, hwnd, TI_LOGO, lLogoDisplayTime); break;
  304.         }
  305.       }
  306.       break;
  307.  
  308.     case WM_TIMER:
  309.       if (SHORT1FROMMP(mp1) == TI_LOGO)
  310.         WinPostMsg(hwnd, WM_COMMAND, NULL, NULL);
  311.       break;
  312.  
  313.     case WM_COMMAND:
  314.       /* OK has been pressed or timer messages has arrived */
  315.       WinStopTimer(hab, hwnd, ulTimerId);
  316.       WinDismissDlg(hwnd, TRUE);
  317.       break;
  318.  
  319.     default:
  320.       return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  321.   }
  322.   return (MRESULT)0;
  323. }
  324.  
  325. void InitOut(void)
  326. {
  327.   sprintf(szOut[0], "Queue information:");
  328.   sprintf(szOut[1], "  Queue name: %s", prqInfoDef.pszName);
  329.   sprintf(szOut[2], "  Default queue processor: %s", prqInfoDef.pszPrProc);
  330.   sprintf(szOut[3], "  Queue description: %s", prqInfoDef.pszComment);
  331.   sprintf(szOut[4], "  Default device driver: %s", prqInfoDef.pszDriverName);
  332.   sprintf(szOut[5], "  Print devices connected to queue: %s", prqInfoDef.pszPrinters);
  333.   sprintf(szOut[6], "");
  334.   sprintf(szOut[7], "Default form description: ");
  335.   sprintf(szOut[8], "  Form name: %s", hcInfoDef.szFormname);
  336.   sprintf(szOut[9], "  Width (left-to-right): %ld milimeters", hcInfoDef.cx);
  337.   sprintf(szOut[10], "  Height (top-to-bottom): %ld milimeters", hcInfoDef.cy);
  338.   sprintf(szOut[11], "  Number of pels between left and right clip limits: %ld", hcInfoDef.xPels);
  339.   sprintf(szOut[12], "  Number of pels between bottom and top clip limits: %ld", hcInfoDef.yPels);
  340.   sprintf(szOut[13], "");  
  341.   sprintf(szOut[14], "Device capabilities: ");
  342.   sprintf(szOut[15], "  Media width: %ld pels", alCaps[CAPS_WIDTH]);
  343.   sprintf(szOut[16], "  Media height: %ld pels", alCaps[CAPS_HEIGHT]);
  344.   sprintf(szOut[17], "  Media width: %ld chars", alCaps[CAPS_WIDTH_IN_CHARS]);
  345.   sprintf(szOut[18], "  Media height: %ld chars", alCaps[CAPS_HEIGHT_IN_CHARS]);
  346.   sprintf(szOut[19], "  Char height: %ld pels", alCaps[CAPS_CHAR_HEIGHT]);
  347.   sprintf(szOut[20], "  Number of device specific fonts: %ld", alCaps[CAPS_DEVICE_FONTS]);
  348.   sprintf(szOut[21], "  Number of distinct colors available on the device: %ld", alCaps[CAPS_PHYS_COLORS]);
  349.   sprintf(szOut[22], "  Horizontal resolution: %ld dpi", alCaps[CAPS_HORIZONTAL_FONT_RES]);
  350.   sprintf(szOut[23], "  Vertical resolution: %ld dpi", alCaps[CAPS_VERTICAL_FONT_RES]);
  351. }
  352.  
  353. void PrintInfo(void *arg)
  354. {
  355.   HPS hpsPrn;
  356.   FONTMETRICS fm;
  357.   LONG lprnMaxAscender, lprnMaxBaselineExt;
  358.   SHORT i;
  359.   POINTL pt;
  360.   USHORT usJobId = 0;
  361.  
  362.   /* begin a print job and obtain a handle to the printer presentation space */
  363.   SpoolBeginSpool("Printer Capabilities", "Printer Caps", "COP=1", 0UL, &hpsPrn, FALSE);
  364.  
  365.   /* obtain necessary font metrics of the selected font */
  366.   GpiQueryFontMetrics (hpsPrn, sizeof(FONTMETRICS), &fm);
  367.   lprnMaxAscender=fm.lMaxAscender;
  368.   lprnMaxBaselineExt=fm.lMaxBaselineExt;
  369.  
  370.   /* print lines */
  371.   pt.x = 0; pt.y = hcInfoDef.yPels-lprnMaxAscender;
  372.   for (i=0; i < NUM_OF_LINES; i++)
  373.   {
  374.     /* has the job been aborted from the primary thread? */
  375.     if (!SpoolIsJobStarted())
  376.     {
  377.       /* job has been aborted, so we quit as well */
  378.       while (!WinPostMsg(hwndFrame, WMP_JOB_DONE, MPFROMLONG(usJobId), MPFROMLONG(FALSE)))
  379.         DosSleep(BACKOFF_TIME);
  380.       _endthread();
  381.     }
  382.     /* form feed */
  383.     if (i%alCaps[CAPS_HEIGHT_IN_CHARS]==0 && i>0)
  384.     {
  385.       SpoolNewFrame();
  386.       pt.y = hcInfoDef.yPels-lprnMaxAscender;
  387.     }
  388.     GpiCharStringAt(hpsPrn, &pt, (LONG)strlen(szOut[i]), (PSZ)szOut[i]);
  389.     pt.y-=lprnMaxBaselineExt;
  390.     /* nap attack after spooling one line */
  391.     DosSleep (1000UL);
  392.   }
  393.   /* end print job */
  394.   SpoolEndSpool(&usJobId);
  395.   /* make sure that the Job done message is successfully posted */
  396.   /* mp1 contains the job id */
  397.   /* mp2 contains an indicator if the job was successful */
  398.   while (!WinPostMsg(hwndFrame, WMP_JOB_DONE, MPFROMLONG(usJobId), MPFROMLONG(TRUE)))
  399.     DosSleep(BACKOFF_TIME);
  400.   _endthread();
  401. }
  402.  
  403.