home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / quot210s.zip / src / qotd_p.c < prev    next >
C/C++ Source or Header  |  1998-06-07  |  6KB  |  196 lines

  1. /*
  2.  * qotd_p.c
  3.  *
  4.  * Window painting routines for the Quoteriser's Quote-of-the-Day.
  5.  *
  6.  *      Created: 27th February, 1997 (as paint.c)
  7.  * Version 1.00: 9th April, 1997 (as paint.c)
  8.  * Version 2.00: 21st December, 1997
  9.  *
  10.  * (C) 1997 Nicholas Paul Sheppard
  11.  *
  12.  * This file is distributed under the GNU General Public License. See the
  13.  * file copying.txt for details.
  14.  */
  15.  
  16. #define INCL_GPI
  17. #define INCL_WIN
  18. #include <os2.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include "qotd.h"
  22. #include "types.h"
  23. #include "threads.h"
  24. #include "typeset.h"
  25. #include "pmutil.h"
  26.  
  27.  
  28. HAB habPaint;
  29. HWND hwndPaint;
  30.  
  31. /* internal function prototypes */
  32. char **PaintTypesetArray(THREADINFO *, char **, char *, long);
  33. MRESULT EXPENTRY PaintWinProc(HWND, ULONG, MPARAM, MPARAM);
  34.  
  35. void ThreadPaint(THREADINFO *pti)
  36. /*
  37.  * Thread used to draw the contents of the client window. It creates a
  38.  * window procedure PaintWinProc() to handle calls from the main window
  39.  */
  40. {
  41.     HMQ hmq;
  42.     QMSG qmsg;
  43.  
  44.     habPaint = WinInitialize(0);
  45.     hmq = WinCreateMsgQueue(habPaint, 0);
  46.     WinRegisterClass(habPaint, "QPAINT", (PFNWP)PaintWinProc, 0, 0);
  47.  
  48.     /* create the paint window */
  49.     hwndPaint = WinCreateWindow(HWND_OBJECT, "QPAINT", "", 0, 0, 0, 0, 0,
  50.                     HWND_OBJECT, HWND_BOTTOM, 0, pti, NULL);
  51.  
  52.     /* go */
  53.     WinPostMsg(pti->hwndCaller, QM_START, NULL, NULL);
  54.     while (WinGetMsg(habPaint, &qmsg, (HWND)NULL, 0, 0))
  55.         WinDispatchMsg(habPaint, &qmsg);
  56.  
  57.     /* clean up */
  58.     WinDestroyWindow(hwndPaint);
  59.     WinDestroyMsgQueue(hmq);
  60.     WinTerminate(habPaint);
  61. }
  62.  
  63.  
  64. MRESULT EXPENTRY PaintWinProc(HWND hwnd, ULONG message, MPARAM mp1, MPARAM mp2)
  65. /*
  66.  * Window procedure for the paint thread. As well as the usual window messages,
  67.  * it supports the following:
  68.  *
  69.  * QM_PAINT    - re-paint the screen
  70.  * QM_TYPESET    - typeset the current string (into a metafile for later painting)
  71.  * QM_COPY    - copy then current display to the clipboard
  72.  */
  73. {
  74.     static THREADINFO    *pti;
  75.     static HPS        hps;
  76.     static HMF        hmf;
  77.     static RECTL        rectlMetafile;
  78.     char            szString[210], *apszTypeset[3], *pszTemp;
  79.     void            *pClipboard;
  80.     HMF            hmfTemp;
  81.  
  82.     switch (message) {
  83.         case WM_CREATE:
  84.             pti = (THREADINFO *)PVOIDFROMMP(mp1);
  85.             hps = WinGetPS(pti->hwndCaller);
  86.             hmf = NULLHANDLE;
  87.             return (FALSE);
  88.  
  89.         case QM_PAINT:
  90.             hps = WinBeginPaint(pti->hwndCaller, hps, NULL);
  91.             GpiResetPS(hps, GRES_ALL);
  92.             ScrollMetafile(hps, NULLHANDLE, hwndVertScroll, hmf);
  93.             WinEndPaint(hps);
  94.             return (FALSE);
  95.  
  96.         case QM_TYPESET:
  97.             if (hmf != NULLHANDLE)
  98.                 GpiDeleteMetaFile(hmf);
  99.             PaintTypesetArray(pti, apszTypeset, szString, sizeof(szString));
  100.             hmf = TypesetMetafile(habPaint, pti->hwndCaller, apszTypeset, &rectlMetafile, &(prf.fattrs));
  101.             ScrollSetGeometry(pti->hwndCaller, NULLHANDLE, hwndVertScroll, &rectlMetafile);
  102.             WinSendMsg(hwndVertScroll, SBM_SETPOS, MPFROMSHORT(0), 0);
  103.             return (FALSE);
  104.  
  105.         case QM_COPY:
  106.             switch (SHORT1FROMMP(mp1)) {
  107.                 case CF_TEXT:
  108.                     PaintTypesetArray(pti, apszTypeset, szString, sizeof(szString));
  109.                     pszTemp = TypesetASCII(apszTypeset, 0);
  110.                     if ((pszTemp != NULL) && WinOpenClipbrd(habPaint)) {
  111.                         WinEmptyClipbrd(habPaint);
  112.                         DosAllocSharedMem(&pClipboard, 0, strlen(pszTemp) + 1, PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE);
  113.                         strcpy((char *)pClipboard, pszTemp);
  114.                         WinSetClipbrdData(habPaint, (ULONG)pClipboard, CF_TEXT, CFI_POINTER);
  115.                         WinCloseClipbrd(habPaint);
  116.                     }
  117.                     free(pszTemp);
  118.                     return (FALSE);
  119.  
  120.                 case CF_METAFILE:
  121.                     if (WinOpenClipbrd(habPaint)) {
  122.                         hmfTemp = GpiCopyMetaFile(hmf);
  123.                         WinEmptyClipbrd(habPaint);
  124.                         WinSetClipbrdData(habPaint, (ULONG)hmfTemp, CF_METAFILE, CFI_HANDLE);
  125.                         WinCloseClipbrd(habPaint);
  126.                     }
  127.                     return (FALSE);
  128.             }
  129.             return (FALSE);
  130.  
  131.         case WM_DESTROY:
  132.             WinReleasePS(hps);
  133.             return (FALSE);
  134.  
  135.         default:
  136.             return (WinDefWindowProc(hwnd, message, mp1, mp2));
  137.     }
  138.  
  139.     return (FALSE);
  140. }
  141.  
  142. char **PaintTypesetArray(THREADINFO *pti, char **papszTypeset, char *pszString, long lSize)
  143. /*
  144.  * Set up the typeset array ready for passing to TypesetMetafile() or TypesetASCII().
  145.  *
  146.  * THREADINFO *pti    - the THREADINFO structure of the calling thread
  147.  * char **papszTypeset    - the typeset array (output)
  148.  * char *pszString    - some space belonging to the calling thread we can use
  149.  * long lSize        - size of the space pointed to by pszString
  150.  *
  151.  * Returns        - papszTypeset
  152.  */
  153. {
  154.     int    i;
  155.     BOOL    bPrintAuthor, bPrintSource;
  156.  
  157.     /* initialise typeset array to be empty */
  158.     for (i = 0; i < 3; i++)
  159.         papszTypeset[i] = NULL;
  160.  
  161.     /* fill the typeset array according to current display mode */
  162.     switch (pti->pps->iMode) {
  163.         case Q_SEARCHING:
  164.             WinLoadString(habPaint, 0, IDS_SEARCHING, lSize, pszString);
  165.             papszTypeset[0] = pszString;
  166.             break;
  167.  
  168.         case Q_QUOTE:
  169.             if (pti->pps->pszText != NULL)
  170.                 papszTypeset[0] = pti->pps->pszText;
  171.             bPrintAuthor = strlen(pti->pps->szAuthorCode) > 0;
  172.             bPrintSource = strlen(pti->pps->pqi->szSource) > 0;
  173.             if (bPrintAuthor || bPrintSource)  {
  174.                 strcpy(pszString, "<p>- ");
  175.                 if (bPrintAuthor) {
  176.                     strcat(pszString, pti->pps->pai->szGivenName);
  177.                     if ((strlen(pti->pps->pai->szSurname) > 0) &&
  178.                             (strlen(pti->pps->pai->szGivenName) > 0))
  179.                         strcat(pszString, " ");
  180.                     strcat(pszString, pti->pps->pai->szSurname);
  181.                 }
  182.                 if (bPrintAuthor && bPrintSource)
  183.                     strcat(pszString, ", ");
  184.                 if (bPrintSource) {
  185.                     strcat(pszString, "<i>");
  186.                     strcat(pszString, pti->pps->pqi->szSource);
  187.                     strcat(pszString, "</i>");
  188.                 }
  189.                 papszTypeset[1] = pszString;
  190.             }
  191.             break;
  192.     }
  193.  
  194.     return (papszTypeset);
  195. }
  196.