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

  1. #include "FiveWin.ch"
  2. #include "Constant.ch"
  3.  
  4. #define CB_ADDSTRING     ( WM_USER +  3 )
  5. #define CB_DELETESTRING  ( WM_USER +  4 )
  6. #define CB_GETCURSEL     ( WM_USER +  7 )
  7. #define CB_INSERTSTRING  ( WM_USER + 10 )
  8. #define CB_RESETCONTENT  ( WM_USER + 11 )
  9. #define CB_SETCURSEL     ( WM_USER + 14 )
  10. #define CB_ERR                       -1
  11.  
  12. #define COLOR_WINDOW       5
  13. #define COLOR_WINDOWTEXT   8
  14.  
  15. //----------------------------------------------------------------------------//
  16.  
  17. CLASS TComboBox FROM TControl
  18.  
  19.    DATA   aItems
  20.    DATA   bChange
  21.    DATA   nAt
  22.  
  23.    METHOD New( nRow, nCol, bSetGet, aItems, nWidth, nHeight, oWnd, nHelpId,;
  24.                bChange, bValid, nClrText, nClrBack, lPixel, oFont,;
  25.                cMsg, lUpdate ) CONSTRUCTOR
  26.  
  27.    METHOD ReDefine( nId, bSetGet, aItems, oWnd, nHelpId, bValid, ;
  28.                bChange, nClrText, nClrBack, cMsg, lUpdate ) CONSTRUCTOR
  29.  
  30.    METHOD cToChar() INLINE  Super:cToChar( "COMBOBOX" )
  31.  
  32.    METHOD Init( hDlg ) INLINE Super:Init( hDlg ),;
  33.                               ::Default()
  34.  
  35.    METHOD Change()
  36.  
  37.    METHOD Default()
  38.  
  39.    METHOD LostFocus()
  40.  
  41.    METHOD MouseMove( nRow, nCol, nKeyFlags )
  42.  
  43.    METHOD Refresh() INLINE  ::Set( Eval( ::bSetGet ) ), Super:Refresh()
  44.  
  45.    METHOD Select( nItem ) INLINE ::SendMsg( CB_SETCURSEL, nItem - 1, 0 )
  46.  
  47.    METHOD Set( cNewItem )
  48.  
  49.    METHOD SetItems( aItems ) INLINE ::Reset(), ::aItems := aItems,;
  50.                                     ::Default(), ::Change()
  51.  
  52.    METHOD Add( cItem, nAt )
  53.    METHOD Modify( cItem, nAt )
  54.    METHOD Insert( cItem, nAt )
  55.    METHOD Del( nAt )
  56.  
  57.    METHOD Reset() INLINE  Eval( ::bSetGet, "" ), ;
  58.                           ASize( ::aItems, 0 ) , ;
  59.                           ::nAt := 0, ::SendMsg( CB_RESETCONTENT ),;
  60.                           ::Change()
  61.  
  62. ENDCLASS
  63.  
  64. //----------------------------------------------------------------------------//
  65.  
  66. METHOD New( nRow, nCol, bSetGet, aItems, nWidth, nHeight, oWnd, nHelpId,;
  67.             bChange, bValid, nClrFore, nClrBack, lPixel, oFont,;
  68.             cMsg, lUpdate ) CLASS TComboBox
  69.  
  70.    if nClrFore == nil
  71.       nClrBack := GetSysColor( COLOR_WINDOW )
  72.    endif
  73.  
  74.    DEFAULT aItems   := {}, nWidth := 40, nHeight := 60,;
  75.            nClrFore := GetSysColor( COLOR_WINDOWTEXT ),;
  76.            lPixel   := .f., lUpdate := .f.
  77.  
  78.    ::cCaption  = ""
  79.    ::nTop      = nRow * If( lPixel, 1, CMB_CHARPIX_H )  // 14
  80.    ::nLeft     = nCol * If( lPixel, 1, CMB_CHARPIX_W )  // 8 
  81.    ::nBottom   = ::nTop  + nHeight - 1
  82.    ::nRight    = ::nLeft + nWidth  - 1
  83.    ::nAt       = 1
  84.    ::aItems    = aItems
  85.    ::bChange   = bChange
  86.    ::bSetGet   = bSetGet
  87.    ::oWnd      = oWnd
  88.    ::oFont     = oFont
  89.    ::nStyle    = nOR( LBS_NOTIFY, WS_TABSTOP, CBS_DROPDOWNLIST,;
  90.                       LBS_DISABLENOSCROLL, WS_CHILD, WS_VISIBLE, WS_BORDER,;
  91.                       WS_VSCROLL )
  92.    ::nId       = ::GetNewId()
  93.    ::nHelpId   = nHelpId
  94.    ::bValid    = bValid
  95.    ::lDrag     = .f.
  96.    ::lCaptured = .f.
  97.    ::cMsg      = cMsg
  98.    ::lUpdate   = lUpdate
  99.  
  100.    ::SetColor( nClrFore, nClrBack )
  101.  
  102.    if oWnd:lVisible
  103.       ::Create( "COMBOBOX" )
  104.       ::Default()
  105.       oWnd:AddControl( Self )
  106.    else
  107.       oWnd:DefControl( Self )
  108.    endif
  109.  
  110. return nil
  111.  
  112. //----------------------------------------------------------------------------//
  113.  
  114. METHOD ReDefine( nId, bSetGet, aItems, oWnd, nHelpId, bValid, ;
  115.                  bChange, nClrFore, nClrBack, cMsg, lUpdate ) CLASS TComboBox
  116.  
  117.    if nClrFore == nil
  118.       nClrBack := GetSysColor( COLOR_WINDOW )
  119.    endif
  120.  
  121.    DEFAULT aItems   := {},;
  122.            nClrFore := GetSysColor( COLOR_WINDOWTEXT ),;
  123.            lUpdate  := .f.
  124.  
  125.    ::nId       = nId
  126.    ::hWnd      = 0
  127.    ::aItems    = aItems
  128.    ::bChange   = bChange
  129.    ::bSetGet   = bSetGet
  130.    ::oWnd      = oWnd
  131.    ::nHelpId   = nHelpId
  132.    ::bValid    = bValid
  133.    ::nAt       = 1
  134.    ::lDrag     = .f.
  135.    ::lCaptured = .f.
  136.    ::cMsg      = cMsg
  137.    ::lUpdate   = lUpdate
  138.  
  139.    ::SetColor( nClrFore, nClrBack )
  140.  
  141.    oWnd:DefControl( Self )
  142.  
  143. return nil
  144.  
  145. //----------------------------------------------------------------------------//
  146.  
  147. METHOD Change() CLASS TComboBox
  148.  
  149.    ::nAt = ::SendMsg( CB_GETCURSEL ) + 1
  150.  
  151.    if ::nAt != 0 .and. ::nAt <= Len( ::aItems )
  152.       Eval( ::bSetGet, ::aItems[ ::nAt ] )
  153.    endif
  154.  
  155.    if ::bChange != nil
  156.       Eval( ::bChange, Self )
  157.    endif
  158.  
  159. return nil
  160.  
  161. //----------------------------------------------------------------------------//
  162.  
  163. METHOD Set( cNewItem ) CLASS TComboBox
  164.  
  165.    local nAt := AScan( ::aItems,;
  166.                        { | cItem | Upper( AllTrim( cItem ) ) == ;
  167.                                    Upper( AllTrim( cNewItem ) ) } )
  168.    if nAt != 0
  169.       ::Select( nAt )
  170.    endif
  171.  
  172. return nil
  173.  
  174. //----------------------------------------------------------------------------//
  175.  
  176. METHOD LostFocus() CLASS TComboBox
  177.  
  178.    local nAt := ::SendMsg( CB_GETCURSEL )
  179.  
  180.    Super:LostFocus()
  181.    if nAt != CB_ERR
  182.       ::nAt = nAt
  183.       Eval( ::bSetGet, ::aItems[ nAt + 1 ] )
  184.    endif
  185.  
  186. return nil
  187.  
  188. //----------------------------------------------------------------------------//
  189.  
  190. METHOD Add( cItem, nAt ) CLASS TComboBox
  191.  
  192.    DEFAULT nAt := 0
  193.  
  194.    if nAt == 0
  195.       AAdd( ::aItems, cItem )
  196.    else
  197.       ASize( ::aItems, Len( ::aItems ) + 1 )
  198.       AIns( ::aItems, nAt )
  199.       ::aItems[ nAt ] = cItem
  200.    endif
  201.  
  202.    ::SendMsg( CB_ADDSTRING, nAt, cItem )
  203.  
  204. return nil
  205.  
  206. //----------------------------------------------------------------------------//
  207.  
  208. METHOD Modify( cItem, nAt ) CLASS TComboBox
  209.  
  210.    DEFAULT nAt := 0
  211.  
  212.    if nAt != 0
  213.       ::aItems[ nAt ] = cItem
  214.       ::SendMsg( CB_DELETESTRING, nAt - 1 )
  215.       ::SendMsg( CB_INSERTSTRING, nAt - 1, cItem )
  216.    endif
  217.  
  218. return nil
  219.  
  220. //----------------------------------------------------------------------------//
  221.  
  222. METHOD Insert( cItem, nAt ) CLASS TComboBox
  223.  
  224.    DEFAULT nAt := 0
  225.  
  226.    if nAt != 0
  227.       ASize( ::aItems, Len( ::aItems ) + 1 )
  228.       AIns( ::aItems, nAt )
  229.       ::aItems[ nAt ] = cItem
  230.       ::SendMsg( CB_INSERTSTRING, nAt - 1, cItem )
  231.    endif
  232.  
  233. return nil
  234.  
  235. //----------------------------------------------------------------------------//
  236.  
  237. METHOD Del( nAt ) CLASS TComboBox
  238.  
  239.    DEFAULT nAt := 0
  240.  
  241.    if nAt != 0
  242.       ADel( ::aItems, nAt )
  243.       ASize( ::aItems, Len( ::aItems ) - 1 )
  244.       ::SendMsg( CB_DELETESTRING, nAt - 1 )
  245.    endif
  246.  
  247. return nil
  248.  
  249. //----------------------------------------------------------------------------//
  250.  
  251. METHOD Default() CLASS TComboBox
  252.  
  253.    local cStart := Eval( ::bSetGet )
  254.  
  255.    if cStart == nil
  256.       Eval( ::bSetGet, If( Len( ::aItems ) > 0, ::aItems[ 1 ], "" ) )
  257.       cStart = If( Len( ::aItems ) > 0, ::aItems[ 1 ], "" )
  258.    endif
  259.  
  260.    AEval( ::aItems, { | cItem, nAt | ::SendMsg( CB_ADDSTRING, nAt, cItem ) } )
  261.  
  262.    ::nAt = AScan( ::aItems, { | cItem | Upper( AllTrim( cItem ) ) == ;
  263.                                         Upper( AllTrim( cStart ) ) } )
  264.    ::nAt = If( ::nAt > 0, ::nAt, 1 )
  265.    ::Select( ::nAt )
  266.  
  267.    if ::oFont != nil
  268.       ::SetFont( ::oFont )
  269.    else
  270.       ::SetFont( ::oWnd:oFont )
  271.    endif
  272.  
  273. return nil
  274.  
  275. //----------------------------------------------------------------------------//
  276.  
  277. METHOD MouseMove( nRow, nCol, nKeyFlags ) CLASS TComboBox
  278.  
  279.    local nResult := Super:MouseMove( nRow, nCol, nKeyFlags )
  280.  
  281. return If( ::lDrag, nResult, nil )    // We want standard behavior !!!
  282.  
  283. //----------------------------------------------------------------------------//
  284.