home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 16 / CD_ASCQ_16_0994.iso / news / 4611 / fw16d.ins / SOURCE / CLASSES / CONTROL.PRG < prev    next >
Text File  |  1994-06-12  |  10KB  |  362 lines

  1. #include "FiveWin.ch"
  2.  
  3. #define LTGRAY_BRUSH       1
  4. #define GRAY_BRUSH         2
  5.  
  6. #define WM_ERASEBKGND     20
  7. #define WM_LBUTTONDBLCLK 515  // 0x203
  8. #define WM_CTLCOLOR       25  // 0x19       // Don't remove Color Control
  9. #define WM_MENUSELECT    287
  10.  
  11. #define GW_HWNDNEXT        2
  12. #define GW_CHILD           5
  13.  
  14. #define SC_CLOSE       61536   // 0xF060
  15. #define SC_NEXT        61504
  16. #define WM_SYSCOMMAND    274   // 0x0112
  17.  
  18. static nId := 100
  19. static nPoint
  20. static nMRow, nMCol
  21.  
  22. //----------------------------------------------------------------------------//
  23.  
  24. CLASS TControl FROM TWindow
  25.  
  26.    DATA   bSetGet
  27.    DATA   cCaption
  28.    DATA   lCaptured, lDrag, lUpdate
  29.  
  30.    METHOD Change() VIRTUAL
  31.  
  32.    METHOD Click() VIRTUAL
  33.  
  34.    METHOD cToChar( cCtrlClass )
  35.    METHOD Init( hDlg )
  36.  
  37.    METHOD Default()
  38.  
  39.    METHOD GetNewId() INLINE ++nId
  40.  
  41.    METHOD GotFocus() INLINE Super:GotFocus(), ::SetMsg( ::cMsg )
  42.  
  43.    METHOD LostFocus() INLINE Super:LostFocus(), ::SetMsg()
  44.  
  45.    METHOD End()
  46.  
  47.    METHOD HandleEvent( nMsg, nWParam, nLParam )
  48.  
  49.    MESSAGE SetFocus METHOD _SetFocus()
  50.  
  51.    METHOD Colors()
  52.    METHOD DrawItem( nPStruct ) VIRTUAL
  53.    METHOD Paint()              VIRTUAL
  54.    METHOD FillMeasure()        VIRTUAL
  55.  
  56.    METHOD Set3DLook() INLINE Ctl3DLook( ::hWnd )
  57.  
  58.    METHOD VarPut( uVal ) INLINE  If( ValType( ::bSetGet ) == "B",;
  59.                                  Eval( ::bSetGet, uVal ),)
  60.  
  61.    METHOD VarGet() INLINE If( ValType( ::bSetGet ) == "B", Eval( ::bSetGet ),)
  62.  
  63.    METHOD LButtonDown( nRow, nCol, nKeyFlags )
  64.  
  65.    METHOD LButtonUp( nRow, nCol, nKeyFlags )
  66.  
  67.    METHOD MouseMove( nRow, nCol, nKeyFlags )
  68.  
  69.    METHOD KeyChar( nKey, nFlags )
  70.  
  71. ENDCLASS
  72.  
  73. //----------------------------------------------------------------------------//
  74.  
  75. METHOD cToChar( cCtrlClass ) CLASS TControl
  76.  
  77.    local n := GetDlgBaseUnits()
  78.  
  79.    DEFAULT cCtrlClass := ::ClassName(),;
  80.            ::cCaption := "",;
  81.            ::nId      := ::GetNewId(),;
  82.            ::nStyle   := nOR( WS_CHILD, WS_VISIBLE, WS_TABSTOP )
  83.  
  84. return cCtrl2Chr( Int( 2 * 8 * ::nTop    / nHiWord( n ) ),;
  85.                   Int( 2 * 4 * ::nLeft   / nLoWord( n ) ),;
  86.                   Int( 2 * 8 * ::nBottom / nHiWord( n ) ),;
  87.                   Int( 2 * 4 * ::nRight  / nLoWord( n ) ),;
  88.                   ::nId, ::nStyle, cCtrlClass, ::cCaption )
  89.  
  90. // return cCtrl2Chr( ::nTop, ::nLeft, ::nBottom, ::nRight,;
  91. //                   ::nId, ::nStyle, cCtrlClass, ::cCaption )
  92.  
  93. //----------------------------------------------------------------------------//
  94.  
  95. METHOD Init( hDlg ) CLASS TControl
  96.  
  97.    local oRect
  98.  
  99.    DEFAULT ::lActive := .t., ::lDrag := .f., ::lCaptured := .f.
  100.  
  101.    if( ( ::hWnd := GetDlgItem( hDlg, ::nId ) ) != 0 )
  102.       oRect         = ::GetRect()
  103.       ::nTop        = oRect:nTop
  104.       ::nLeft       = oRect:nLeft
  105.       ::nBottom     = oRect:nBottom
  106.       ::nRight      = oRect:nRight
  107.  
  108.       If( ::lActive, ::Enable(), ::Disable() )
  109.  
  110.       ::Link()
  111.  
  112.       if ::oFont != nil
  113.          ::SetFont( ::oFont )
  114.       else
  115.          ::SetFont( ::oWnd:oFont )
  116.       endif
  117.  
  118.    else
  119.      #define NOVALID_CONTROLID   1
  120.      Eval( ErrorBlock(), _FWGenError( NOVALID_CONTROLID, "No: " + ;
  121.                                       Str( ::nId, 6 ) ) )
  122.    endif
  123.  
  124. return nil
  125.  
  126. //----------------------------------------------------------------------------//
  127.  
  128. METHOD _SetFocus() CLASS TControl
  129.  
  130.    local hCtrlNext
  131.  
  132.    if ::lWhen()
  133.       SetFocus( ::hWnd )
  134.    else
  135.       hCtrlNext = GetWindow( ::hWnd, GW_HWNDNEXT )
  136.       if GetParent( hCtrlNext ) != ::oWnd:hWnd
  137.          hCtrlNext = GetWindow( ::oWnd:hWnd, GW_CHILD )
  138.       endif
  139.       SetFocus( hCtrlNext )
  140.    endif
  141.  
  142. return nil
  143.  
  144. //----------------------------------------------------------------------------//
  145.  
  146. METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TControl
  147.  
  148.    local nDlgCode
  149.  
  150.    do case
  151.       case nMsg == WM_PAINT          // Standard Controls must use default
  152.            return nil                // painting procedures
  153.  
  154.       case nMsg == WM_COMMAND
  155.            return Super:Command( nWParam, nLParam )
  156.  
  157.       case nMsg == WM_CLICK
  158.            ::Click()
  159.  
  160.       case nMsg == WM_CHANGE
  161.            ::Change()
  162.  
  163.       case nMsg == WM_COLOR
  164.            ::hDC = nWParam
  165.            return ::Colors()
  166.  
  167.       case nMsg == WM_DESTROY
  168.            ::Release()
  169.  
  170.       case nMsg == FW_MEASURE
  171.            return ::FillMeasure( nLParam )
  172.  
  173.       case nMsg == WM_MENUSELECT
  174.            return Super:HandleEvent( nMsg, nWParam, nLParam )
  175.  
  176.       case nMsg == FW_DRAW
  177.            return ::DrawItem( nLParam )
  178.  
  179.       case nMsg == WM_GETDLGCODE
  180.            nDlgCode = ::GetDlgCode()
  181.            if ! Empty( nDlgCode )
  182.               return nDlgCode
  183.            endif
  184.  
  185.       case nMsg == WM_KEYDOWN
  186.            return ::KeyDown( nWParam, nLParam )
  187.  
  188.       case nMsg == WM_CHAR
  189.            return ::KeyChar( nWParam, nLParam )
  190.  
  191.       case nMsg == WM_KILLFOCUS
  192.            ::LostFocus()
  193.            if GetParent( nWParam ) == ::oWnd:hWnd .and. ! ::oWnd:lValidating
  194.               PostMessage( ::hWnd, FW_LOSTFOCUS )
  195.            endif
  196.  
  197.       case nMsg == FW_LOSTFOCUS
  198.            if ! ::oWnd:lValidating
  199.               ::oWnd:lValidating = .t.
  200.               if ! ::lValid()
  201.                  SetFocus( ::hWnd )
  202.               endif
  203.               ::oWnd:lValidating = .f.
  204.            endif
  205.  
  206.       case nMsg == WM_MOUSEMOVE
  207.            return ::MouseMove( nHiWord( nLParam ), nLoWord( nLParam ), nWParam )
  208.  
  209.       case nMsg == WM_SETFOCUS
  210.            ::oWnd:nResult := Self
  211.            ::GotFocus()
  212.  
  213.       case nMsg == WM_LBUTTONDOWN
  214.            return ::LButtonDown( nHiWord( nLParam ), nLoWord( nLParam ), nWParam )
  215.  
  216.       case nMsg == WM_LBUTTONUP
  217.            return ::LButtonUp( nHiWord( nLParam ), nLoWord( nLParam ), nWParam )
  218.  
  219.       case nMsg == WM_RBUTTONDOWN
  220.            return ::RButtonDown( nHiWord( nLParam ), nLoWord( nLParam ), nWParam )
  221.  
  222.       case nMsg == WM_LBUTTONDBLCLK
  223.            return ::LDblClick( nHiWord( nLParam ), nLoWord( nLParam ), nWParam )
  224.  
  225.       case nMsg == WM_CTLCOLOR
  226.            return Super:HandleEvent( nMsg, nWParam, nLParam )
  227.  
  228.       case nMsg == WM_ERASEBKGND
  229.            if ::oBrush != nil
  230.               FillRect( nWParam, GetClientRect( ::hWnd ), ::oBrush:hBrush )
  231.               return 1
  232.            endif
  233.  
  234.       case nMsg == WM_VSCROLL
  235.            return ::VScroll( nWParam, nLParam )
  236.  
  237.       case nMsg == WM_HSCROLL
  238.            return ::HScroll( nWParam, nLParam )
  239.    endcase
  240.  
  241. return nil
  242.  
  243. //----------------------------------------------------------------------------//
  244.  
  245. METHOD Colors() CLASS TControl
  246.  
  247.    DEFAULT ::nClrText := GetTextColor( ::hDC ),;
  248.            ::nClrPane := GetBkColor( ::hDC ),;
  249.            ::oBrush   := TBrush():New( ,::nClrPane,)
  250.  
  251.    SetTextColor( ::hDC, ::nClrText )
  252.    SetBkColor( ::hDC,   ::nClrPane )
  253.  
  254. return ::oBrush:hBrush
  255.  
  256. //----------------------------------------------------------------------------//
  257.  
  258. METHOD Default() CLASS TControl
  259.  
  260.    ::lDrag     = .f.
  261.    ::lCaptured = .f.
  262.  
  263. return nil
  264.  
  265. //----------------------------------------------------------------------------//
  266.  
  267. METHOD LButtonDown( nRow, nCol, nKeyFlags ) CLASS TControl
  268.  
  269.    if ::lDrag
  270.       if ! ::lCaptured
  271.          ::Capture()
  272.          ::lCaptured = .t.
  273.          nMRow  = nRow
  274.          nMCol  = nCol
  275.          nPoint = 0
  276.          CtrlDrawFocus( ::hWnd )
  277.       endif
  278.       return 0
  279.    endif
  280.  
  281. return nil
  282.  
  283. //----------------------------------------------------------------------------//
  284.  
  285. METHOD LButtonUp( nRow, nCol, nKeyFlags ) CLASS TControl
  286.  
  287.    if ::lDrag
  288.       if ::lCaptured
  289.          ReleaseCapture()
  290.          ::lCaptured = .f.
  291.          if nPoint != 0
  292.             CtrlDrawFocus( ::hWnd, nLoWord( nPoint ) - nMRow,;
  293.                                    nHiWord( nPoint ) - nMCol )
  294.             ::Move( ::nTop + nRow - nMRow, ::nLeft + nCol - nMCol )
  295.             ::oWnd:Refresh( .t. )
  296.          else
  297.             CtrlDrawFocus( ::hWnd, 0, 0 )
  298.          endif
  299.       endif
  300.       return 0
  301.    endif
  302.  
  303. return nil
  304.  
  305. //----------------------------------------------------------------------------//
  306.  
  307. METHOD MouseMove( nRow, nCol, nKeyFlags ) CLASS TControl
  308.  
  309.    if ::lDrag
  310.       if ::lCaptured
  311.          CursorCatch()
  312.          if nPoint != 0
  313.             CtrlDrawFocus( ::hWnd, nLoWord( nPoint ) - nMRow,;
  314.                                    nHiWord( nPoint ) - nMCol )
  315.          else
  316.             CtrlDrawFocus( ::hWnd, 0, 0 )
  317.          endif
  318.          nPoint = nMakeLong( nRow, nCol )
  319.          CtrlDrawFocus( ::hWnd, nLoWord( nPoint ) - nMRow,;
  320.                                 nHiWord( nPoint ) - nMCol )
  321.       else
  322.          CursorSize()
  323.       endif
  324.       return 0
  325.    else
  326.       ::SetMsg( ::cMsg )
  327.       return Super:MouseMove( nRow, nCol, nKeyFlags )
  328.    endif
  329.  
  330. return 0
  331.  
  332. //----------------------------------------------------------------------------//
  333.  
  334. METHOD End() CLASS TControl
  335.  
  336.    local nAt := AScan( ::oWnd:aControls, { | oCtrl | oCtrl:hWnd == Self:hWnd } )
  337.  
  338.    if nAt != 0
  339.       ADel( ::oWnd:aControls, nAt )
  340.       ASize( ::oWnd:aControls, Len( ::oWnd:aControls ) - 1 )
  341.    endif
  342.  
  343. return Super:End()
  344.  
  345. //----------------------------------------------------------------------------//
  346.  
  347. METHOD KeyChar( nKey, nFlags ) CLASS TControl
  348.  
  349.    do case
  350.       case nKey == VK_TAB .and. GetKeyState( VK_SHIFT )
  351.            ::oWnd:GoPrevCtrl( ::hWnd )
  352.            return 0    // We don't want API default behavior
  353.  
  354.       case nKey == VK_TAB
  355.            ::oWnd:GoNextCtrl( ::hWnd )
  356.            return 0    // We don't want API default behavior
  357.    endcase
  358.  
  359. return nil             // We want API default behavior
  360.  
  361. //----------------------------------------------------------------------------//
  362.