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

  1. #include "FiveWin.ch"
  2. #include "Constant.ch"
  3. #include "Set.ch"
  4.  
  5. #define EM_GETSEL  (WM_USER+0)
  6. #define EM_SETSEL  (WM_USER+1)
  7.  
  8. #define COLOR_WINDOW         5
  9. #define COLOR_WINDOWTEXT     8
  10.  
  11. //----------------------------------------------------------------------------//
  12.  
  13. CLASS TGet FROM TControl
  14.  
  15.    DATA   oGet
  16.    DATA   nClrFocusText, nClrFocusPane
  17.  
  18.    METHOD New( nRow, nCol, bSetGet, oWnd, nWidth, nHeight, cPict, bValid,;
  19.                nClrFore, nClrBack, oFont, lDesign, oCursor, lPixel,;
  20.                cMsg, lUpdate ) CONSTRUCTOR
  21.  
  22.    METHOD ReDefine( nId, bSetGet, oWnd, nHelpId, cPict, bValid,;
  23.                     nClrFore, nClrBack, oFont, oCursor, cMsg,;
  24.                     lUpdate ) CONSTRUCTOR
  25.  
  26.    METHOD cToChar() INLINE Super:cToChar( "EDIT" )
  27.  
  28.    METHOD Default()
  29.  
  30.    METHOD cGenPrg()
  31.  
  32.    METHOD GotFocus() INLINE  Super:GotFocus(),;
  33.                              ::oGet:SetFocus(),;
  34.                              SetWindowText( ::hWnd, ::oGet:buffer ), nil
  35.  
  36.    METHOD HandleEvent( nMsg, nWParam, nLParam )
  37.    METHOD Init( hDlg )
  38.  
  39.    METHOD KeyDown( nKey, nLData )
  40.    METHOD KeyChar( nKey )
  41.  
  42.    METHOD LostFocus()
  43.  
  44.    METHOD MouseMove( nRow, nCol, nKeyFlags )
  45.  
  46.    METHOD cText( cText ) SETGET
  47.  
  48.    METHOD Refresh() INLINE ::oGet:SetFocus(),;
  49.                            ::oGet:UpdateBuffer(),;
  50.                            SetWindowText( ::hWnd, ::oGet:buffer )
  51.  
  52.    METHOD Save() INLINE ::oGet:Assign()
  53.  
  54.    METHOD GetSel()
  55.    METHOD GetSelPos( @nStart, @nEnd )
  56.    METHOD GetDelSel( nStart, nEnd )
  57.  
  58.    METHOD EditUpdate()
  59.  
  60.    METHOD lValid()
  61.  
  62. ENDCLASS
  63.  
  64. //----------------------------------------------------------------------------//
  65.  
  66. METHOD New( nRow, nCol, bSetGet, oWnd, nWidth, nHeight, cPict, bValid,;
  67.             nClrFore, nClrBack, oFont, lDesign, oCursor, lPixel, cMsg,;
  68.             lUpdate ) CLASS TGet
  69.  
  70.    DEFAULT nClrFore := GetSysColor( COLOR_WINDOWTEXT ),;
  71.            nClrBack := GetSysColor( COLOR_WINDOW ),;
  72.            oWnd     := GetWndDefault(),;
  73.            nHeight  := If( oFont != nil, oFont:nHeight, 12 ),;
  74.            lDesign  := .f., lPixel := .f., lUpdate := .f.
  75.  
  76.    ::cCaption = If( cPict == nil, cValToChar( Eval( bSetGet ) ), ;
  77.                                   Transform( Eval( bSetGet ), cPict ) )
  78.    ::nTop     = nRow * If( lPixel, 1, GET_CHARPIX_H )     //13
  79.    ::nLeft    = nCol * If( lPixel, 1, GET_CHARPIX_W )     // 8
  80.    ::nBottom  = ::nTop + nHeight - 1
  81.    ::nRight   = ::nLeft + If( nWidth == nil, ( 1 + Len( ::cCaption ) ) * 3.5, ;
  82.                                                nWidth - 1 )
  83.    ::oWnd      = oWnd
  84.    ::nStyle    = nOR( WS_CHILD, WS_VISIBLE, WS_BORDER, WS_TABSTOP, ES_LEFT,;
  85.                       ES_AUTOHSCROLL, If( lDesign, WS_THICKFRAME, 0 ) )
  86.    ::nId       = ::GetNewId()
  87.    ::bSetGet   = bSetGet
  88.    ::oGet      = GetNew( 5000, 5000, bSetGet,, cPict )
  89.    ::bValid    = bValid
  90.    ::lDrag     = lDesign
  91.    ::lCaptured = .f.
  92.    ::oFont     = oFont
  93.    ::oCursor   = oCursor
  94.    ::cMsg      = cMsg
  95.    ::lUpdate   = lUpdate
  96.  
  97.    ::SetColor( nClrFore, nClrBack )
  98.  
  99.    ::oGet:SetFocus()
  100.    ::cCaption = ::oGet:Buffer
  101.    ::oGet:KillFocus()
  102.  
  103.    if oWnd:lVisible
  104.       ::Create( "EDIT" )
  105.       ::Default()
  106.       oWnd:AddControl( Self )
  107.    else
  108.       oWnd:DefControl( Self )
  109.    endif
  110.  
  111. return nil
  112.  
  113. //----------------------------------------------------------------------------//
  114.  
  115. METHOD ReDefine( nId, bSetGet, oWnd, nHelpId, cPict, bValid, nClrFore,;
  116.                  nClrBack, oFont, oCursor, cMsg, lUpdate ) CLASS TGet
  117.  
  118.    DEFAULT nClrFore := GetSysColor( COLOR_WINDOWTEXT ),;
  119.            nClrBack := GetSysColor( COLOR_WINDOW ),;
  120.            lUpdate  := .f.
  121.  
  122.    ::nId       = nId
  123.    ::oWnd      = oWnd
  124.    ::nHelpId   = nHelpId
  125.    ::bSetGet   = bSetGet
  126.    ::oGet      = GetNew( 5000, 5000, bSetGet,, cPict )
  127.    ::bValid    = bValid
  128.    ::lDrag     = .f.
  129.    ::lCaptured = .f.
  130.    ::oFont     = oFont
  131.    ::oCursor   = oCursor
  132.    ::cMsg      = cMsg
  133.    ::lUpdate   = lUpdate
  134.  
  135.    ::SetColor( nClrFore, nClrBack )
  136.  
  137.    oWnd:DefControl( Self )
  138.  
  139. return nil
  140.  
  141. //----------------------------------------------------------------------------//
  142.  
  143. METHOD Init( hDlg ) CLASS TGet
  144.  
  145.    Super:Init( hDlg )
  146.    ::oGet:SetFocus()
  147.    SetWindowText( ::hWnd, ::oGet:buffer )
  148.    ::oGet:KillFocus()
  149.  
  150. return nil
  151.  
  152. //---------------------------------------------------------------------------//
  153.  // Actualiza texto y y posición del cursor del EDIT en función del oGet.
  154.  // Updates the text and the EDIT cursor position depending the oGet
  155.  
  156. METHOD EditUpdate() CLASS TGet
  157.  
  158.    if ::oGet:HasFocus
  159.       SetWindowText( ::hWnd, ::oGet:buffer )
  160.    endif
  161.  
  162.    CallWindowProc( ::nOldProc, ::hWnd, EM_SETSEL, 0, ;
  163.                    nMakeLong( ::oGet:pos - 1, ::oGet:pos - 1 ) )
  164.  
  165. return nil
  166.  
  167. //---------------------------------------------------------------------------//
  168.  
  169. METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TGet
  170.  
  171.    static nCaretCol := 0
  172.    local nHi, nLo
  173.  
  174.    do case
  175.       case nMsg == WM_PAINT
  176.            return nil
  177.  
  178.       case nMsg == WM_LBUTTONDOWN
  179.            if ::lDrag
  180.               return Super:HandleEvent( nMsg, nWParam, nLParam )
  181.            else
  182.               CallWindowProc( ::nOldProc, ::hWnd, nMsg, nWParam, nLParam )
  183.               nCaretCol = GetCaretPos()[ 1 ]
  184.               return 1
  185.            endif
  186.  
  187.       case nMsg == WM_LBUTTONUP
  188.            if ::lDrag
  189.               return Super:HandleEvent( nMsg, nWParam, nLParam )
  190.            else
  191.               ::GetSelPos( @nLo, @nHi )
  192.               ::oGet:pos = If( nCaretCol <= GetCaretPos()[ 1 ], nHi, nLo ) + 1
  193.            endif
  194.                    
  195.       case nMsg == WM_KEYUP
  196.            // Paste from clipboard
  197.            if nWParam == VK_INSERT .and. GetKeyState( VK_SHIFT )
  198.                ::oGet:buffer = GetWindowText( ::hWnd )
  199.                ::oGet:pos    = nHiWord( ::SendMsg( EM_GETSEL ) ) + 1
  200.            elseif nWParam == VK_UP .or. nWParam == VK_DOWN
  201.                return 1  // We have not processed the key and we don't let
  202.                          // the edit to do it
  203.            endif
  204.  
  205.        endcase
  206.  
  207. return Super:HandleEvent( nMsg, nWParam, nLParam )
  208.  
  209. //----------------------------------------------------------------------------//
  210.  
  211. METHOD cText( uVal ) CLASS TGet
  212.  
  213.    if PCount() == 1      // OJO Con Objects 2.0 PCount() es PCount() + 1
  214.       ::oGet:VarPut( uVal )
  215.       ::Refresh()
  216.    endif
  217.  
  218. return GetWindowText( ::hWnd )
  219.  
  220. //----------------------------------------------------------------------------//
  221.  
  222. METHOD GetSel() CLASS TGet
  223.  
  224.    local n      := ::SendMsg( EM_GETSEL )
  225.    local nStart := nLoWord( n )
  226.    local nEnd   := nHiWord( n )
  227.  
  228. return If( nStart != nEnd, SubStr( ::cText, nStart, nEnd - nStart + 1 ), "" )
  229.  
  230. //----------------------------------------------------------------------------//
  231.  
  232. METHOD GetSelPos( nStart, nEnd ) CLASS TGet
  233.  
  234.    local n := ::SendMsg( EM_GETSEL )
  235.    nStart  := nLoWord( n )
  236.    nEnd    := nHiWord( n )
  237.  
  238. return nil
  239.  
  240. //----------------------------------------------------------------------------//
  241.  
  242. METHOD GetDelSel( nStart, nEnd ) CLASS TGet
  243.  
  244.    ::oGet:buffer = Left( ::oGet:buffer, Min( nEnd, nStart ) ) ;
  245.                    + Right( ::oGet:buffer, ;
  246.                             Len( ::oGet:buffer ) - Max( nEnd, nStart ) ) ;
  247.                    + Space( Abs( nStart - nEnd ) )
  248.  
  249.    ::oGet:Assign()
  250.    ::oGet:Reset()
  251.    ::oGet:pos := Min( nStart, nEnd ) + 1
  252.  
  253. return nil
  254.  
  255. //---------------------------------------------------------------------------//
  256.  
  257. METHOD MouseMove( nRow, nCol, nKeyFlags ) CLASS TGet
  258.  
  259.    if ::lDrag
  260.       return Super:MouseMove( nRow, nCol, nKeyFlags )
  261.    else
  262.       ::SetMsg( ::cMsg )
  263.       if ::oCursor != nil
  264.          SetCursor( ::oCursor:hCursor )
  265.       else
  266.          CursorIBeam()
  267.       endif
  268.    endif
  269.  
  270. return nil      // We want standard Get behavior !!!
  271.  
  272. //---------------------------------------------------------------------------//
  273.  
  274. METHOD Default() CLASS TGet
  275.  
  276.    if ::oFont != nil
  277.       ::SetFont( ::oFont )
  278.    else
  279.       ::SetFont( ::oWnd:oFont )
  280.    endif
  281.  
  282. return nil
  283.  
  284. //---------------------------------------------------------------------------//
  285.  
  286. METHOD cGenPrg() CLASS TGet
  287.  
  288.    local cCode := ""
  289.  
  290.    cCode += CRLF + "   @ " + Str( ::nTop, 3 ) + ", " + Str( ::nLeft, 3 ) + ;
  291.             " GET oGet SIZE " + Str( ::nRight - ::nLeft + 1, 3 ) + ;
  292.             ", " + Str( ::nBottom - ::nTop + 1, 3 ) + ;
  293.             " PIXEL OF oDlg " + CRLF
  294.  
  295. return cCode
  296.  
  297. //---------------------------------------------------------------------------//
  298.  
  299. METHOD KeyDown( nKey, nLData ) CLASS TGet
  300.  
  301.    local nHi, nLo
  302.  
  303.    do case
  304.       case nKey == VK_F1
  305.            return Super:KeyDown( nKey )
  306.  
  307.       case nKey == VK_UP .or. nKey == VK_DOWN
  308.            ::oWnd:FocusNext( ::hWnd, nKey == VK_UP )
  309.            return 1  // We have not processed the key and we
  310.                        // don't let the edit to do it
  311.  
  312.       case nKey == VK_LEFT
  313.            if GetKeyState( VK_CONTROL )
  314.               ::oGet:wordLeft()
  315.            else
  316.               ::oGet:left()
  317.            endif
  318.            CallWindowProc( ::nOldProc, ::hWnd, EM_SETSEL, 0, ;
  319.                            nMakeLong( ::oGet:pos - 1, ::oGet:pos - 1 ) )
  320.            return 0
  321.  
  322.       case nKey == VK_RIGHT
  323.            if GetKeyState( VK_CONTROL )
  324.               ::oGet:wordRight()
  325.            else
  326.               ::oGet:right()
  327.            endif
  328.            CallWindowProc( ::nOldProc, ::hWnd, EM_SETSEL, 0, ;
  329.                            nMakeLong( ::oGet:pos - 1, ::oGet:pos - 1 ) )
  330.            return 0
  331.  
  332.       case nKey == VK_DELETE
  333.            ::GetSelPos( @nLo, @nHi )
  334.  
  335.              // Deletes selection
  336.            if nHi != nLo
  337.               ::GetDelSel( nLo, nHi )
  338.               if GetKeyState( VK_SHIFT )
  339.                  CallWindowProc( ::nOldProc, ::hWnd, WM_KEYDOWN, nKey, nLData )
  340.               endif
  341.            else
  342.               ::oGet:Delete()
  343.            endif
  344.            ::EditUpdate()
  345.            return 1
  346.  
  347.       case nKey == VK_HOME
  348.            ::oGet:Home()
  349.  
  350.       case nKey == VK_END
  351.            ::oGet:End()
  352.            ::EditUpdate()
  353.            return 1
  354.  
  355.       otherwise
  356.            return Super:KeyDown( nKey, nLData )
  357.    endcase
  358.  
  359. return nil
  360.  
  361. //---------------------------------------------------------------------------//
  362.  
  363. METHOD KeyChar( nKey ) CLASS TGet
  364.  
  365.    local nHi, nLo
  366.  
  367.     do case
  368.        case nKey == VK_BACK
  369.             ::oGet:BackSpace()
  370.             ::EditUpdate()
  371.  
  372.        case nKey == VK_ESCAPE
  373.             MessageBeep( -1 )
  374.             return 0
  375.  
  376.        case nKey == VK_RETURN .or. nKey == VK_TAB
  377.             ::oWnd:GoNextCtrl( ::hWnd )
  378.             return 0
  379.  
  380.        case nKey >= 32 .or. nKey < 256
  381.             ::GetSelPos( @nLo, @nHi )
  382.             // Borrar selección.
  383.             if nHi != nLo
  384.                ::GetDelSel( nLo, nHi )
  385.                ::EditUpdate()
  386.             endif
  387.             ::oGet:Insert( Chr( nKey ) )
  388.             if ::oGet:Rejected()
  389.                MessageBeep( -1 )
  390.             else
  391.                ::EditUpdate()
  392.             endif
  393.             if ::oGet:TypeOut .and. ! Set( _SET_CONFIRM )
  394.                ::oWnd:GoNextCtrl( ::hWnd )
  395.             endif
  396.  
  397.        otherwise
  398.             return Super:KeyChar( nKey )
  399.     endcase
  400.  
  401. return 1
  402.  
  403. //---------------------------------------------------------------------------//
  404.  
  405. METHOD lValid() CLASS TGet
  406.  
  407.    if ::oGet:BadDate
  408.       MsgAlert( "Invalid date" )
  409.       return .f.
  410.    endif
  411.  
  412. return Super:lValid()
  413.  
  414. //---------------------------------------------------------------------------//
  415.  
  416. METHOD LostFocus() CLASS TGet
  417.  
  418.    Super:LostFocus()
  419.  
  420.    ::oGet:SetFocus()
  421.  
  422.    if ! ::oGet:BadDate
  423.       ::oGet:Assign()     // for adjust numbers
  424.       ::oGet:UpdateBuffer()
  425.    endif
  426.  
  427.    SetWindowText( ::hWnd, ::oGet:buffer )
  428.  
  429.    if ! ::oGet:BadDate
  430.       ::oGet:KillFocus()
  431.    else
  432.       ::oGet:Pos = 1
  433.    endif
  434.  
  435. return nil
  436.  
  437. //----------------------------------------------------------------------------//
  438.