home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / contro.zip / SHADOW.C < prev    next >
Text File  |  1994-12-18  |  7KB  |  221 lines

  1. #define INCL_WIN
  2. #define INCL_GPI
  3.  
  4. /* Presentation Manager include file  */
  5. #include <os2.h>        
  6.  
  7. /*
  8.  * Include C library routine header files
  9.  */
  10.  
  11. #include <stdlib.h>    /* Miscellaneous function declarations       */
  12. #include <stdio.h>    /* Declarations for standard I/O routines */
  13. #include <string.h>    /* String function declarations          */
  14. #include <memory.h>    /* Declarations for memory manipulation      */
  15.  
  16. /*
  17.  * Include C GPF API User Control header files 
  18.  */
  19. #include "GpfUC.h"
  20.  
  21. /* function prototypes of everything in this file */
  22. PUCC APIENTRY RegisterShadow( VOID );
  23. MRESULT EXPENTRY fnwpShadow(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  24. VOID Paint(HWND, HPS, PSZ);
  25.  
  26. #define EXTRAWORDS (4 * sizeof(PVOID))
  27. /*************************************************/
  28. PUCC APIENTRY RegisterShadow( VOID )
  29. {
  30. HAB    hab;
  31. static UCC Ucc;
  32.  
  33.   hab = WinInitialize(0);
  34.   /**********************************************/
  35.   /*    Register the window class "Shadow".      */
  36.   /**********************************************/
  37.   WinRegisterClass(        /* Register window class    */
  38.       hab,            /* Anchor block handle      */
  39.       "SHADOW",         /* Window class name (Upper)*/
  40.       fnwpShadow,        /* Address window procedure */
  41.       CS_SAVEBITS      |     /* Class style          */
  42.       CS_HITTEST      |     /* Class style          */
  43.       CS_SIZEREDRAW   |     /* Class style          */
  44.       CS_SYNCPAINT,     /* Class style      */
  45.       EXTRAWORDS        /* Extra window words, instance-specific */
  46.       );
  47.   Ucc.Capability = GPF_CAPS_TEXT | GPF_CAPS_PRESPARAMS;
  48.  
  49.   Ucc.WsStyle     = WS_VISIBLE;
  50.   /* set some arbitrary default size, used by the TRACKRECT */
  51.   Ucc.Cx     = 120;
  52.   Ucc.Cy     = 32;
  53.  
  54.   /* return this structure to Gpf so it can start placement and */
  55.   /* open the default dialog. The default dialog will return the text. */
  56.   return (&Ucc);
  57.  }
  58.  
  59.  
  60. #define EXTRA_SHADOW    8
  61. /******************************************************/
  62. MRESULT EXPENTRY fnwpShadow(
  63.          HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  64. {
  65. HPS    hps;            /* Presentation Space handle     */
  66. RECTL    rcl;            /* Window rectangle          */
  67. ULONG    BorderColor,Color;
  68. ULONG    BackGroundColor;
  69. PSZ    pShadow;        /* we will allocate memory for this */
  70. PCREATESTRUCT pCrst;        /* data type(s) defined by OS/2 PM  */
  71.  
  72.  
  73.   /**********************************************/
  74.   /* Initialize Control    Parameters            */
  75.   /**********************************************/
  76.   pShadow = (PVOID) WinQueryWindowULong(hwnd,0);
  77.   if (!pShadow)
  78.       {
  79.        if (msg != WM_CREATE)
  80.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  81.       }
  82.  
  83.   switch(msg)
  84.   {
  85.     /*
  86.      * Query Dialog Code
  87.      */
  88.     case WM_QUERYDLGCODE:
  89.         return( (MRESULT) DLGC_STATIC ); /* Static item */
  90.     break;
  91.  
  92.     /*
  93.      * HitTest
  94.      */
  95.     case WM_HITTEST:
  96.         return( (MRESULT) HT_TRANSPARENT);
  97.     break;
  98.  
  99.     /*
  100.      * Let frame control erase background for us
  101.      */
  102.     case WM_ERASEBACKGROUND:
  103.         /* The client window is cleared to SYSCLR_WINDOW. */
  104.         return (MRESULT)(FALSE); /*  No background windows. */
  105.  
  106.     /*
  107.      * The window needs painting
  108.      */
  109.     case WM_PAINT:
  110.         /* Set Default BackGroundColor and ForeGroundColor */            BackGroundColor = CLR_PALEGRAY;
  111.         /* Query and Save Presentation Parameters BackGroundColor */
  112.         if (WinQueryPresParam(hwnd,
  113.                       PP_BACKGROUNDCOLOR,0,
  114.                       0,4L,&Color,0))
  115.             BackGroundColor = Color;
  116.         /* Query and Save Presentation Parameters BG Color Index */
  117.         if (WinQueryPresParam(hwnd,0,
  118.                       PP_BACKGROUNDCOLORINDEX,
  119.                       0,4L,&Color,QPF_ID1COLORINDEX))
  120.             BackGroundColor = Color;
  121.         /* Set Default BorderColor         */
  122.         /* BorderColor = BackGroundColor;    */
  123.         BorderColor = CLR_BLACK;
  124.         /* Query and Save Presentation Parameters BackGroundColor */
  125.         if (WinQueryPresParam(hwnd,
  126.                       PP_BORDERCOLOR,0,
  127.                       0,4L,&Color,0))
  128.             BorderColor = Color;
  129.         /* Query and Save Presentation Parameters BG Color Index */
  130.         if (WinQueryPresParam(
  131.             hwnd,0,
  132.             PP_BORDERCOLORINDEX,
  133.             0,4L,&Color,QPF_ID1COLORINDEX))
  134.                 BorderColor = Color;
  135.         hps = WinBeginPaint( hwnd, (HPS)0, (PRECTL)&rcl );
  136.         WinFillRect(hps,&rcl,BackGroundColor);
  137.         Paint(hwnd,hps, pShadow);    /* do detailed painting */
  138.         WinEndPaint( hps );         /* Release cache PS   */
  139.         break;
  140.  
  141.     /*
  142.      * The application has asked for the control to be created.
  143.      */
  144.     case WM_CREATE:
  145.         pCrst= (PCREATESTRUCT) mp2;
  146.         pShadow = (PSZ)malloc(64);           /* Set Pointer */
  147.         strncpy(pShadow, pCrst->pszText, 63);
  148.         *(pShadow+63)= 0;    /* defensive programming, make PSZ */
  149.         WinSetWindowPtr(hwnd,0,pShadow);
  150.         return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  151.  
  152.     /*
  153.      * The application has asked for the control to be destroyed.
  154.      */
  155.     case WM_DESTROY:
  156.         WinSetWindowULong(hwnd,EXTRA_SHADOW,(ULONG)0);
  157.         free(pShadow);         /* release pointer/mem  */
  158.         break;
  159.  
  160.  
  161.     /*
  162.      * All other messages are passed to the default procedure.
  163.      */
  164.     default:
  165.     /* Pass all other messages to the default window procedure */
  166.         return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  167.   }
  168.   return ((MRESULT) FALSE);
  169. }
  170. /* End of window procedure - WinProc: fnwpShadow                  */
  171.  
  172.  
  173. #define MY_FONT_ID 10    /* any old number will do */
  174.  
  175. /***********************************************************/
  176. VOID Paint(HWND hwnd, HPS hps, PSZ psz) {
  177. HAB    hab;
  178. RECTL    rect;
  179. SIZEF    sizef;
  180. SHORT    usLen, usCharWidth;
  181. static FATTRS    Font = {
  182.         sizeof(FATTRS),0,0L,
  183.         "Tms Rmn",0,0,0L,0L,0,FATTR_FONTUSE_NOMIX};
  184.  
  185.     hab= WinQueryAnchorBlock(hwnd);
  186.  
  187.     WinQueryWindowRect(hwnd, &rect );
  188.     WinFillRect(hps, &rect, CLR_PALEGRAY);
  189.     WinInflateRect(hab,&rect,-1,-1);
  190.     GpiSetCharMode(hps,CM_MODE2);
  191.     usLen= max(1,strlen(psz));    /* make sure this is never zero  */
  192.     /* the character width will probably be too small (based on "W") */
  193.     usCharWidth= (rect.xRight - rect.xLeft)/usLen;
  194.     /* so we will guess at using ~150% of the min value */
  195.     sizef.cx = MAKEFIXED(11*usCharWidth/6,0);
  196.     /* make character height as tall as box sized by user */
  197.     /* if no descenders are used, text will float above box bottom */
  198.     sizef.cy = MAKEFIXED((rect.yTop-rect.yBottom),0);
  199.     GpiSetCharBox ( hps, &sizef );    /* set the 'cell' width */
  200.     /* we will always load, then unload, the required font */
  201.     GpiCreateLogFont(hps,(PSTR8)"EmptyName",MY_FONT_ID,&Font);
  202.     GpiSetCharSet(hps,MY_FONT_ID);
  203.     /* now draw text in three different places, slightly offset from */
  204.     /* each other and vary the foreground color. Slick. */
  205.     WinOffsetRect(hab,&rect,-1,+1);
  206.     /* first, shadow on left (sunny) side */
  207.     WinDrawText(hps,usLen,psz,&rect,CLR_WHITE,CLR_PALEGRAY,
  208.         DT_CENTER | DT_BOTTOM);
  209.     WinOffsetRect(hab,&rect,+2,-2);
  210.     /* next, dark shadow on right side */
  211.     WinDrawText(hps,usLen,psz,&rect,CLR_BLACK,CLR_PALEGRAY,
  212.         DT_CENTER | DT_BOTTOM);
  213.     WinOffsetRect(hab,&rect,-1,+1);
  214.     /* superimpose the character bodies */
  215.     WinDrawText(hps,usLen,psz,&rect,CLR_PALEGRAY,CLR_PALEGRAY,
  216.         DT_CENTER | DT_BOTTOM);
  217.  
  218.     GpiSetCharSet(hps,0L);
  219.     GpiDeleteSetId(hps, MY_FONT_ID);
  220. }
  221.