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

  1. /*
  2.  * quoter_p.c
  3.  *
  4.  * Painting routines for the Quoteriser
  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 "quoter.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.  
  36. void ThreadPaint(THREADINFO *pti)
  37. /*
  38.  * Thread used to draw the contents of the client window. It creates a
  39.  * window procedure PaintWinProc() to handle calls from the main window
  40.  */
  41. {
  42.     HMQ hmq;
  43.     QMSG qmsg;
  44.  
  45.     habPaint = WinInitialize(0);
  46.     hmq = WinCreateMsgQueue(habPaint, 0);
  47.     WinRegisterClass(habPaint, "QPAINT", (PFNWP)PaintWinProc, 0, 0);
  48.  
  49.     /* create the paint window */
  50.     hwndPaint = WinCreateWindow(HWND_OBJECT, "QPAINT", "", 0, 0, 0, 0, 0,
  51.                     HWND_OBJECT, HWND_BOTTOM, 0, pti, NULL);
  52.  
  53.     /* go */
  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 the screen contents to the clipboard in the format specified in mp1
  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), NULL);
  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.  
  143. char **PaintTypesetArray(THREADINFO *pti, char **papszTypeset, char *pszString, long lSize)
  144. /*
  145.  * Set up the typeset array ready for passing to TypesetMetafile() or TypesetASCII().
  146.  *
  147.  * THREADINFO *pti    - the THREADINFO structure of the calling thread
  148.  * char **papszTypeset    - the typeset array (output)
  149.  * char *pszString    - some space belonging to the calling thread we can use
  150.  * long lSize        - size of the space pointed to by pszString
  151.  *
  152.  * Returns        - papszTypeset
  153.  */
  154. {
  155.     int    i;
  156.     BOOL    bPrintAuthor, bPrintSource;
  157.  
  158.     /* initialise typeset array to be empty */
  159.     for (i = 0; i < 3; i++)
  160.         papszTypeset[i] = NULL;
  161.  
  162.     /* fill the typeset array according to current display mode */
  163.     switch (pti->pps->iMode) {
  164.         case Q_SEARCHING:
  165.             WinLoadString(habPaint, 0, IDS_SEARCHING, lSize, pszString);
  166.             papszTypeset[0] = pszString;
  167.             break;
  168.  
  169.         case Q_QUOTE:
  170.             if (pti->pps->pszText != NULL)
  171.                 papszTypeset[0] = pti->pps->pszText;
  172.             bPrintAuthor = strlen(pti->pps->szAuthorCode) > 0;
  173.             bPrintSource = strlen(pti->pps->pqi->szSource) > 0;
  174.             if (bPrintAuthor || bPrintSource)  {
  175.                 strcpy(pszString, "<p>- ");
  176.                 if (bPrintAuthor) {
  177.                     strcat(pszString, pti->pps->pai->szGivenName);
  178.                     if ((strlen(pti->pps->pai->szSurname) > 0) &&
  179.                             (strlen(pti->pps->pai->szGivenName) > 0))
  180.                         strcat(pszString, " ");
  181.                     strcat(pszString, pti->pps->pai->szSurname);
  182.                 }
  183.                 if (bPrintAuthor && bPrintSource)
  184.                     strcat(pszString, ", ");
  185.                 if (bPrintSource) {
  186.                     strcat(pszString, "<i>");
  187.                     strcat(pszString, pti->pps->pqi->szSource);
  188.                     strcat(pszString, "</i>");
  189.                 }
  190.                 papszTypeset[1] = pszString;
  191.             }
  192.             break;
  193.  
  194.         case Q_AUTHOR:
  195.             strcpy(pszString, "<b>");
  196.             strcat(pszString, pti->pps->pai->szGivenName);
  197.             if ((strlen(pti->pps->pai->szSurname) > 0) &&
  198.                 (strlen(pti->pps->pai->szGivenName) > 0))
  199.                 strcat(pszString, " ");
  200.             strcat(pszString, pti->pps->pai->szSurname);
  201.             strcat(pszString, "</b> (");
  202.             strcat(pszString, pti->pps->pai->szBirthYear);
  203.             strcat(pszString, " - ");
  204.             strcat(pszString, pti->pps->pai->szDeathYear);
  205.             strcat(pszString, ")<br><br>");
  206.             papszTypeset[0] = pszString;
  207.             if (pti->pps->pszDesc != NULL)
  208.                 papszTypeset[1] = pti->pps->pszDesc;
  209.             break;
  210.     }
  211.  
  212.     return (papszTypeset);
  213. }
  214.