home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mltcol.zip / EDIT.C next >
C/C++ Source or Header  |  1993-05-27  |  11KB  |  280 lines

  1. #define     INCL_GPIPRIMITIVES
  2. #define     INCL_WIN
  3. #include    <os2.h>
  4.  
  5. #include    <stdlib.h>
  6. #include    <stdio.h>
  7. #include    <string.h>
  8. #include    <ctype.h>
  9.  
  10. #include    "edit.h"
  11.  
  12. /*----------------------------------------------------------------------*/
  13. /*  Prototype for edit class only.                                      */
  14. /*----------------------------------------------------------------------*/
  15. USHORT   EntryFldCharProc ( HWND    hwnd, SUBCLASSCTRLDATA  *pSubCls,
  16.                                USHORT  key );
  17.  
  18. void     EntryFldChar     ( HWND, USHORT, MPARAM, MPARAM );
  19.  
  20. extern   PFNWP       pfnEntryWndProc;
  21.  
  22. MRESULT EXPENTRY EditWndProc( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  23. {
  24.     switch ( msg )  {
  25.  
  26.     case WM_CREATE :
  27.         EntryFldCreate ( hwnd, ( CREATESTRUCT * ) mp2 );
  28.         pfnEntryWndProc  ( hwnd, msg, mp1, mp2 );
  29.         if ( WinQueryWindowTextLength ( hwnd ) )
  30.             WinSetWindowText ( hwnd, "" );
  31.         return ( MRESULT ) FALSE;
  32.  
  33.     case WM_CHAR                  :
  34.         EntryFldChar ( hwnd, msg, mp1, mp2 );
  35.         return ( MRESULT ) TRUE;
  36.  
  37.     /*------------------------------------------------------------------*/
  38.     /*  Release all the memory used to process this window.             */
  39.     /*------------------------------------------------------------------*/
  40.     case WM_DESTROY         :
  41.         EntryFldDestroy ( hwnd );
  42.         return ( MRESULT ) FALSE;
  43.  
  44.     case  WM_PAINT          :
  45.         pfnEntryWndProc ( hwnd, msg, mp1, mp2 );
  46.         EntryFldPaint ( hwnd );
  47.         break;
  48.     }                                   /* Switch : msg                 */
  49.     return   pfnEntryWndProc ( hwnd, msg, mp1, mp2 );
  50. }
  51.  
  52. /*----------------------------------------------------------------------*/
  53. /*  EntryFldChar                                                     */
  54. /*                                                                      */
  55. /*      The big kahuna. This is what a entry field is all about. Make   */
  56. /*      sure only worthy characters are allowed to be entered and put   */
  57. /*      in the separating characters appropriatly.                      */
  58. /*----------------------------------------------------------------------*/
  59. void  EntryFldChar ( HWND    hwnd, USHORT  msg, MPARAM  mp1, MPARAM  mp2 ){
  60.  
  61.     SUBCLASSCTRLDATA  *pSubCls;
  62.     USHORT                usFlags, vkey, key;
  63.     UCHAR                 scancode;
  64.  
  65.     pSubCls  = WinQueryWindowPtr ( hwnd, QWL_USER );
  66.  
  67.     usFlags  = SHORT1FROMMP ( mp1 );
  68.     vkey     = SHORT2FROMMP ( mp2 );
  69.     scancode = (UCHAR)   CHAR4FROMMP ( mp1 );
  70.  
  71.     if ( usFlags & KC_KEYUP || ! ( usFlags & KC_CHAR  ) )  {
  72.         pfnEntryWndProc ( hwnd, msg, mp1, mp2 );
  73.         return;
  74.     }
  75.  
  76.     switch ( vkey )  {
  77.     case    VK_TAB          :
  78.     case    VK_NEWLINE      :
  79.     case    VK_ENTER        :
  80.     case    VK_BACKSPACE    :
  81.     case    VK_DELETE       :
  82.     case    VK_BACKTAB      :
  83.     case    VK_ESC          :
  84.         pfnEntryWndProc ( hwnd, msg, mp1, mp2 );
  85.         return;
  86.     }
  87.  
  88.     if ( usFlags & KC_SHIFT )  {
  89.         switch ( scancode )  {
  90.         case  0x47   :                  /*  Home    */
  91.         case  0x4B   :                  /*  Left    */
  92.         case  0x4D   :                  /*  Right   */
  93.         case  0x4F   :                  /*  End     */
  94.             pfnEntryWndProc ( hwnd, msg, mp1, mp2 );
  95.             return;
  96.         }
  97.     }
  98.  
  99.     if ( key = EntryFldCharProc ( hwnd, pSubCls,
  100.                                     (USHORT) CHAR1FROMMP((ULONG) mp2) ))  {
  101.  
  102.         ((UCHAR *) &mp2)[0] = ( UCHAR ) key;
  103.         pfnEntryWndProc ( hwnd, msg, mp1, mp2 );
  104.  
  105.     }
  106. }
  107.  
  108. /*----------------------------------------------------------------------*/
  109. /*    EntryFldCharProc                                               */
  110. /*                                                                      */
  111. /*      This function handles the code specific to real characters      */
  112. /*      All virtual and editing characters have already been thrown     */
  113. /*      away. Just validate each character and change the case if needed*/
  114. /*----------------------------------------------------------------------*/
  115. USHORT  EntryFldCharProc ( HWND    hwnd, SUBCLASSCTRLDATA  *pSubCls,
  116.                               USHORT  key )  {
  117.  
  118.     BOOL          fValid = FALSE;
  119.  
  120.     /*------------------------------------------------------------------*/
  121.     /*  Check the char entered against the base class for the entry     */
  122.     /*  field.                                                          */
  123.     /*------------------------------------------------------------------*/
  124.     switch ( pSubCls->ucFldType )  {
  125.     case   '9'   :
  126.         fValid = isdigit ( key );
  127.         break;
  128.  
  129.     case   'A'   :
  130.         key    = toupper ( key );
  131.     case   'a'   :
  132.         fValid = isalpha ( key );
  133.         break;
  134.  
  135.     case   'N'   :
  136.         key = toupper ( key );
  137.     case   'n'   :
  138.         fValid = isalnum ( key );
  139.         break;
  140.  
  141.     case   'X'   :
  142.         fValid = ( isalnum ( key ) || key == ' ');
  143.         break;
  144.     }                           /* Switch : Field Type          */
  145.     if ( ! fValid )  {
  146.         WinAlarm ( HWND_DESKTOP, WA_NOTE );
  147.         return  0;
  148.     }
  149.     else  {
  150.         return  key;
  151.     }
  152. }
  153.  
  154. /*----------------------------------------------------------------------*/
  155. /*  EntryFldCreate                                                   */
  156. /*                                                                      */
  157. /*      Initialize the Entry field control structure with the standard  */
  158. /*      values. Required field, solid line, no character list, Black    */
  159. /*----------------------------------------------------------------------*/
  160. SUBCLASSCTRLDATA  *EntryFldCreate ( HWND  hwnd, CREATESTRUCT *pCr )  {
  161.  
  162.     SUBCLASSCTRLDATA  *pSubCls;
  163.     ULONG                 ulStyle;
  164.  
  165.     /*--------------------------------------------------------------*/
  166.     /*  Default Conditions                                          */
  167.     /*      Line Type       :   Solid   No Error                    */
  168.     /*      Char List       :   None                                */
  169.     /*      Line Style      :   2       Required                    */
  170.     /*      Field Type      :   Defined in .DLG                     */
  171.     /*      Line Color      :   Black                               */
  172.     /*--------------------------------------------------------------*/
  173.  
  174.     pSubCls  = malloc  ( sizeof ( SUBCLASSCTRLDATA ) );
  175.     if ( pSubCls == NULL )
  176.         return  NULL;
  177.  
  178.     memset ( pSubCls, 0, sizeof ( SUBCLASSCTRLDATA ) );
  179.  
  180.     WinSetWindowPtr ( hwnd, QWL_USER, pSubCls );
  181.  
  182.     pSubCls->fValidState  = TRUE;
  183.     pSubCls->ulLineType   = LINETYPE_SOLID;
  184.     pSubCls->ulLineColor  = SYSCLR_WINDOWFRAME;
  185.     pSubCls->usLineStyle  = ES_REQUIRED;
  186.     pSubCls->ulFlags      = 0;
  187.  
  188.     if ( pCr != NULL )  {
  189.         pSubCls->ucFldType = pCr->pszClass[0];
  190.     }
  191.     /*------------------------------------------------------------------*/
  192.     /*  If there is not a margin create one and while we are here turn  */
  193.     /*  on auto scrolling                                               */
  194.     /*------------------------------------------------------------------*/
  195.     ulStyle = WinQueryWindowULong ( hwnd, QWL_STYLE );
  196.     ulStyle |= ES_MARGIN | ES_AUTOSCROLL;
  197.  
  198.     /*------------------------------------------------------------------*/
  199.     /*  Right Justify the entry field if it is a numeric field          */
  200.     /*------------------------------------------------------------------*/
  201.     if ( pSubCls->ucFldType == '9' )  {
  202.         ulStyle |= ES_RIGHT;
  203.     }
  204.     WinSetWindowULong  ( hwnd, QWL_STYLE, ulStyle );
  205.  
  206.     return  pSubCls;
  207. }
  208.  
  209. /*----------------------------------------------------------------------*/
  210. /*  EntryFldDestroy                                                  */
  211. /*                                                                      */
  212. /*      Release all memory used by this window.                         */
  213. /*----------------------------------------------------------------------*/
  214. void    EntryFldDestroy ( HWND   hwnd )  {
  215.  
  216.     SUBCLASSCTRLDATA  *pSubCls;
  217.  
  218.     pSubCls  = WinQueryWindowPtr ( hwnd, QWL_USER );
  219.  
  220.     free ( pSubCls );
  221.     WinSetWindowPtr ( hwnd, QWL_USER, NULL );
  222. }
  223.  
  224. /*----------------------------------------------------------------------*/
  225. /*  Get out your brushes and paint the window.                          */
  226. /*  Put a border around the window as required either error border,     */
  227. /*  required border or default border.                                  */
  228. /*----------------------------------------------------------------------*/
  229. void   EntryFldPaint ( HWND hwnd )  {
  230.  
  231.     SUBCLASSCTRLDATA  *pSubCls;
  232.     HPS                   hps;
  233.     SWP                   swp;
  234.     RECTL                 rcl;
  235.     POINTL                aptl[4];
  236.  
  237.     pSubCls = WinQueryWindowPtr ( hwnd, QWL_USER );
  238.  
  239.     hps = WinGetPS ( WinQueryWindow ( hwnd, QW_PARENT, 0 ) );
  240.  
  241.     WinQueryWindowPos ( hwnd, &swp );
  242.                                                         /*--------------*/
  243.     aptl[2].x = aptl[3].x = swp.x;                      /* Left         */
  244.     aptl[0].x = aptl[1].x = swp.cx +  swp.x - 1;        /* Right        */
  245.                                                         /*--------------*/
  246.     aptl[0].y = aptl[3].y = swp.y;                      /* Bottom       */
  247.     aptl[1].y = aptl[2].y = swp.cy + swp.y - 1;         /* Top          */
  248.                                                         /*--------------*/
  249.     GpiSetLineType ( hps, LINETYPE_SOLID );
  250.     GpiMove        ( hps, &aptl[3] );
  251.     GpiSetColor    ( hps, SYSCLR_DIALOGBACKGROUND );
  252.     GpiPolyLine    ( hps, 4L, aptl );
  253.  
  254.     GpiSetLineType ( hps, pSubCls->ulLineType );
  255.     GpiMove ( hps, &aptl[3] );
  256.  
  257.     switch   ( pSubCls->usLineStyle )   {
  258.     case        ES_OPTIONAL          :  /*  Optional Border         */
  259.     case        ES_REQUIRED          :  /*  Required Border         */
  260.         GpiSetColor    ( hps, pSubCls->ulLineColor );
  261.         break;
  262.     case        ES_OPTIONAL_RDONLY   :  /*  Invisible Optional Border  */
  263.     case        ES_REQUIRED_RDONLY   :  /*  Invisible Required Border  */
  264.         GpiSetColor    ( hps, SYSCLR_DIALOGBACKGROUND );
  265.         break;
  266.     }
  267.     GpiPolyLine  ( hps, 4L, aptl );
  268.     WinReleasePS ( hps );
  269.  
  270.     WinQueryWindowRect ( hwnd, &rcl );
  271.     WinValidateRect    ( hwnd, &rcl, FALSE );
  272.     rcl.xLeft     += 2;
  273.     rcl.xRight    -= 2;
  274.     rcl.yTop      -= 2;
  275.     rcl.yBottom   += 2;
  276.     WinInvalidateRect  ( hwnd, &rcl, FALSE );
  277.     WinShowWindow      ( hwnd, TRUE );
  278.  
  279. }
  280.