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

  1. #include "FiveWin.ch"
  2. #include "Constant.ch"
  3.  
  4. #define LTGRAY_BRUSH     1
  5. #define GRAY_BRUSH       2
  6.  
  7. #define WM_ACTIVATE      6
  8. #define WM_NCACTIVATE  134  // 0x0086
  9. #define WM_CTLCOLOR     25  // 0x19       // Don't remove Color Control
  10. #define WM_ERASEBKGND   20  // 0x0014     // or controls will not shown
  11.                                           // colors !!!
  12. #define WM_GETFONT      49  // 0x0031
  13. #define WM_DRAWITEM     43  // 0x002B
  14. #define WM_MEASUREITEM  44  // 0x002C
  15.  
  16. #define WM_VBXFIREEVENT  864       // 0x0360
  17.  
  18. #define CBN_SELCHANGE      1
  19.  
  20. #define BM_SETSTYLE        WM_USER + 4
  21.  
  22. static lRegistered := .f.
  23.  
  24. //----------------------------------------------------------------------------//
  25.  
  26. CLASS TDialog FROM TWindow
  27.  
  28.    DATA   cResName
  29.    DATA   hResources
  30.    DATA   lCentered, lModal, lModify, lVbx
  31.  
  32.    METHOD New( nTop, nLeft, nBottom, nRight, cCaption, cResName, hResources,;
  33.                lVbx, nStyle, nClrText, nClrBack, oBrush, oWnd, oIco ) CONSTRUCTOR
  34.  
  35.    METHOD Define( nTop, nLeft, nBottom, nRight, cCaption, nStyle,;
  36.                   nClrText, nClrPane, oBrush ) CONSTRUCTOR
  37.  
  38.    METHOD Activate( bClicked, bMoved, bPainted, lCentered, bValid, lModal,;
  39.                     bInit, bRClicked, bWhen )
  40.  
  41.    METHOD Command( nWParam, nLParam )
  42.    METHOD cToChar( hActiveWnd )
  43.    METHOD DefControl( oControl )
  44.  
  45.    METHOD Release() INLINE If( ! ::lModal, Super:Release(), )
  46.    METHOD End( nResult )
  47.    METHOD FocusNext( hCtrlFocus, lPrevius )
  48.    METHOD GetItem( nId ) INLINE  GetDlgItem( ::hWnd, nId )
  49.  
  50.    METHOD HandleEvent( nMsg, nWParam, nLParam )
  51.  
  52.    METHOD Init()
  53.  
  54. ENDCLASS
  55.  
  56. //----------------------------------------------------------------------------//
  57.  
  58. METHOD New( nTop, nLeft, nBottom, nRight, cCaption, cResName, hResources,;
  59.             lVbx, nStyle, nClrText, nClrBack, oBrush, oWnd, lPixels,;
  60.             oIco ) CLASS TDialog
  61.  
  62.    DEFAULT hResources := GetResources(), lVbx := .f.,;
  63.            nStyle     := nOR( DS_MODALFRAME, WS_POPUP, WS_CAPTION, WS_SYSMENU ),;
  64.            nClrText   := CLR_BLACK, nClrBack := CLR_LIGHTGRAY,;
  65.            lPixels    := .f., nTop := 0, nLeft := 0, nBottom := 10, nRight := 10
  66.  
  67.    ::aControls  = {}
  68.    ::cResName   = cResName
  69.    ::cCaption   = cCaption
  70.    ::hResources = hResources
  71.    ::lModify    = .t.
  72.    ::lVbx       = lVbx
  73.    ::lVisible   = .f.
  74.    ::nResult    = 0
  75.    ::nStyle     = nStyle
  76.    ::oWnd       = oWnd
  77.    ::oIcon      = oIco
  78.  
  79.    ::SetColor( nClrText, nClrBack, oBrush )
  80.  
  81.    if lPixels  // New PIXELS Clausule
  82.       ::nTop       = nTop
  83.       ::nLeft      = nLeft
  84.       ::nBottom    = nBottom
  85.       ::nRight     = nRight
  86.    else
  87.       // Compatibility
  88.       ::nTop    := int( nTop    * DLG_CHARPIX_H )        //15.1
  89.       ::nLeft   := int( nLeft   * DLG_CHARPIX_W )      // 7.9
  90.       ::nBottom := int( nBottom * DLG_CHARPIX_H  )         // 15.1
  91.       ::nRight  := int( nRight  * DLG_CHARPIX_W  )         // 7.9
  92.    endif
  93.  
  94.    if lVbx
  95.       VbxInit()
  96.    endif
  97.  
  98.    if ! lRegistered
  99.       ::Register( nOr( CS_VREDRAW, CS_HREDRAW, CS_GLOBALCLASS ) )
  100.       lRegistered = .t.
  101.    endif
  102.  
  103.    SetWndDefault( Self )          //  Set Default DEFINEd Window
  104.  
  105. return nil
  106.  
  107. //----------------------------------------------------------------------------//
  108.  
  109. METHOD Activate( bLClicked, bMoved, bPainted, lCentered, ;
  110.                  bValid, lModal, bInit, bRClicked, bWhen ) CLASS TDialog
  111.  
  112.    static nDlgCount := 0
  113.  
  114.    local hActiveWnd, hWnd
  115.  
  116.    DEFAULT lCentered := .f., lModal := .t., ::hWnd := 0
  117.  
  118.    ++nDlgCount
  119.  
  120.    hActiveWnd = If( ::oWnd != nil, ::oWnd:hWnd,;
  121.                 If( nDlgCount > 1 .or. lWRunning(),;
  122.                     GetActiveWindow(), GetWndApp() ) )
  123.  
  124.    ::lCentered   = lCentered
  125.    ::lModal      = lModal
  126.    ::bLClicked   = bLClicked
  127.    ::bRClicked   = bRClicked
  128.    ::bWhen       = bWhen
  129.    ::bValid      = bValid
  130.    ::bInit       = bInit
  131.    ::bPainted    = bPainted
  132.    ::bMoved      = bMoved
  133.    ::nResult     = nil
  134.    ::lValidating = .f.
  135.    ::lVisible    = .t.
  136.  
  137.    if ::bWhen != nil
  138.       if ! Eval( ::bWhen, Self )
  139.           ::nResult  = IDCANCEL
  140.           ::lVisible = .f.
  141.           return nil             // <<---------- Warning: Exiting!
  142.       endif
  143.    endif
  144.  
  145.    if lModal
  146.       ::nResult = if( ! Empty( ::cResName ),;
  147.                       DialogBox( ::hResources, ::cResName,;
  148.                                       hActiveWnd, Self ),;
  149.                       DialogBoxIndirect( GetInstance(), ::cToChar( hActiveWnd ),;
  150.                                       hActiveWnd, Self ) )
  151.       if ::nResult == -1
  152.          CreateDlgError( Self )
  153.       endif
  154.  
  155.    else
  156.       if ( Len( ::aControls ) > 0 .and. CanRegDialog() ) .or. ;
  157.            Len( ::aControls ) == 0
  158.  
  159.          ::hWnd = if( ! Empty( ::cResName ),;
  160.                        CreateDlg( ::hResources, ::cResName,;
  161.                                   hActiveWnd, Self ),;
  162.                        CreateDlgIndirect( GetInstance(), ::cToChar( hActiveWnd ),;
  163.                                   hActiveWnd, Self ) )
  164.  
  165.          if ::hWnd == 0
  166.             CreateDlgError( Self )
  167.          endif
  168.  
  169.          ::Link( .f. )
  170.  
  171.          if Len( ::aControls ) > 0 .and. ! RegDialog( ::hWnd )
  172.             ::SendMsg( WM_CLOSE )
  173.             MsgAlert( "Not possible to create more non-modal Dialogs" )
  174.          endif
  175.  
  176.          ShowWindow( ::hWnd )
  177.  
  178.       else
  179.  
  180.          MsgAlert( "Not possible to create more non-modal Dialogs" )
  181.  
  182.       endif
  183.  
  184.    endif
  185.  
  186.    nDlgCount--
  187.  
  188.    if ::lModal
  189.       ::lVisible = .f.
  190.    endif
  191.  
  192. return nil
  193.  
  194. //---------------------------------------------------------------------------//
  195.  
  196. METHOD DefControl( oCtrl ) CLASS TDialog
  197.  
  198.    DEFAULT oCtrl:nId := oCtrl:GetNewId()
  199.  
  200.    if AScan( ::aControls, { | o | o:nId == oCtrl:nId } ) > 0
  201.       #define DUPLICATED_CONTROLID  2
  202.       Eval( ErrorBlock(), _FWGenError( DUPLICATED_CONTROLID, ;
  203.                           "No: " + Str( oCtrl:nId, 6 ) ) )
  204.    else
  205.       AAdd( ::aControls, oCtrl )
  206.    endif
  207.  
  208. return nil
  209.  
  210. //----------------------------------------------------------------------------//
  211.  
  212. METHOD FocusNext( hCtrlFocus, lPrevius ) CLASS TDialog
  213.  
  214.    local hCtrlNext := NextDlgTab( ::hWnd, hCtrlFocus, lPrevius )
  215.  
  216.    if hCtrlNext != hCtrlFocus
  217.       SetFocus( hCtrlNext )
  218.  
  219.       // DLGC_BUTTON || DLGC_DEFPUSHBUTTON = 8224
  220.       if SendMessage( hCtrlNext, WM_GETDLGCODE, 0, 0 ) = 8224
  221.          SendMessage( hCtrlNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, 1 )
  222.       endif
  223.    else
  224.       MessageBeep( -1 )
  225.    endif
  226.  
  227. return nil
  228.  
  229. //----------------------------------------------------------------------------//
  230.  
  231. METHOD Command( nWParam, nLParam ) CLASS TDialog
  232.  
  233.    do case
  234.       case ::oPopup != nil
  235.            ::oPopup:Command( nWParam )
  236.            ::oPopup = nil
  237.  
  238.       case nLParam == 0 .and. ::oMenu != nil
  239.            ::oMenu:Command( nWParam )
  240.  
  241.       case nWParam > 0
  242.            do case
  243.               case nHiWord( nLParam ) == BN_CLICKED
  244.  
  245.                    if nLoWord( nLParam ) > 0 .and. nWParam != IDCANCEL
  246.  
  247.                       if ::nResult != nil  // latest control which had focus
  248.  
  249.                          ::nResult:LostFocus()    // updates related variable
  250.  
  251.                          // There is a pending Valid, it is not a clicked button
  252.                          if ::nResult:nID != nWParam .and. !::nResult:lValid()
  253.                             return nil
  254.                          endif
  255.  
  256.                       endif
  257.  
  258.                       if AScan( ::aControls, { |o| o:nID == nWParam } ) > 0
  259.                          SendMessage( nLoWord( nLParam ), WM_CLICK, 0, 0 )
  260.                       elseif nWParam == IDOK
  261.                          ::End( IDOK )
  262.                       endif
  263.  
  264.                    else
  265.  
  266.                       if nWParam == IDOK
  267.  
  268.                          ::FocusNext( GetFocus(), .f. )
  269.  
  270.                       elseif nLoWord( nLParam ) > 0 .and. ; // There is a control for IDCANCEL
  271.                              AScan( ::aControls, { |o| o:nID == nWParam } ) > 0
  272.                             SendMessage( nLoWord( nLParam ), WM_CLICK, 0, 0 )
  273.                       else
  274.                          ::End( IDCANCEL )
  275.                       endif
  276.  
  277.                    endif
  278.  
  279.                    return nil
  280.  
  281.               case nHiWord( nLParam ) == CBN_SELCHANGE
  282.                    SendMessage( nLoWord( nLParam ), WM_CHANGE, 0, 0 )
  283.  
  284.            endcase
  285.    endcase
  286.  
  287. return nil
  288.  
  289. //----------------------------------------------------------------------------//
  290.  
  291. METHOD cToChar( hActiveWnd ) CLASS TDialog
  292.  
  293.    local cResult
  294.    local aControls := ::aControls
  295.    local n     := GetDlgBaseUnits()
  296.    local aRect := GetWndRect( hActiveWnd )
  297.  
  298.    DEFAULT ::cCaption := ""
  299.  
  300.    cResult = cDlg2Chr( Len( aControls ),;
  301.                        int( 8 * ( ::nTop  - aRect[ 1 ]   ) / nHiWord( n ) ),;
  302.                        int( 4 * ( ::nLeft - aRect[ 2 ]   ) / nLoWord( n ) ),;
  303.                        int( 8 * ( ::nBottom - aRect[ 1 ] ) / nHiWord( n ) ),;
  304.                        int( 4 * ( ::nRight  - aRect[ 2 ] ) / nLoWord( n ) ),;
  305.                        ::cCaption, ::nStyle )
  306.  
  307.    for n = 1 to Len( aControls )
  308.       cResult += aControls[ n ]:cToChar()
  309.    next
  310.  
  311. return cResult
  312.  
  313. //----------------------------------------------------------------------------//
  314.  
  315. METHOD Define( nTop, nLeft, nBottom, nRight, cCaption, nStyle, lVbx,;
  316.                nClrText, nClrBack, oBrush ) CLASS TDialog
  317.  
  318.    DEFAULT lVbx     := .f.,;
  319.            nClrText := CLR_BLACK, nClrBack := CLR_LIGHTGRAY
  320.  
  321.    ::hWnd     = 0
  322.    ::nTop     = nTop
  323.    ::nLeft    = nLeft
  324.    ::nBottom  = nBottom
  325.    ::nRight   = nRight
  326.    ::cCaption = cCaption
  327.    ::nStyle   = nStyle
  328.    ::lVbx     = lVbx
  329.  
  330.    ::SetColor( nClrText, nClrBack, oBrush )
  331.  
  332. return nil
  333.  
  334. //----------------------------------------------------------------------------//
  335.  
  336. METHOD End( nResult ) CLASS TDialog
  337.  
  338.    DEFAULT nResult := 2              // Cancel
  339.  
  340.    if ! ::lModal
  341.       PostMessage( ::hWnd, WM_CLOSE, nResult )
  342.    else
  343.       if ValType( ::bValid ) == "B"
  344.          if ! Eval( ::bValid, Self )
  345.             return .f.
  346.          endif
  347.       endif
  348.       ::nResult = nResult
  349.       EndDialog( ::hWnd, nResult )
  350.    endif
  351.  
  352. return .t.
  353.  
  354. //----------------------------------------------------------------------------//
  355.  
  356. METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TDialog
  357.  
  358.    do case
  359.       case nMsg == WM_INITDIALOG
  360.            return ::Init()
  361.  
  362.       case nMsg == WM_CLOSE
  363.            if ! ::lModal
  364.               if ValType( ::bValid ) == "B"
  365.                  if ! Eval( ::bValid, Self )
  366.                     return nil
  367.                  endif
  368.               endif
  369.  
  370.                ::nResult = nWParam
  371.  
  372.               ::lVisible = .f.
  373.               DestroyWindow( ::hWnd )
  374.               return .t.
  375.            endif
  376.            
  377.       case nMsg == WM_COMMAND
  378.            ::Command( nWParam, nLParam )
  379.  
  380.       case nMsg == WM_CTLCOLOR
  381.            return SendMessage( nLoWord( nLParam ),;   // Handle Child
  382.                                WM_COLOR,;             // FiveWin Message
  383.                                nWParam )              // Child hDC
  384.  
  385.       case nMsg == WM_DESTROY
  386.            if ! ::lModal
  387.               ::Release()
  388.               return .t.
  389.            endif
  390.  
  391.       case nMsg == WM_VBXFIREEVENT
  392.            // MsgInfo( "Vbx Fire Event" )
  393.  
  394.       case nMsg == WM_MEASUREITEM
  395.            return Super:HandleEvent( nMsg, nWParam, nLParam )
  396.  
  397.       case nMsg == WM_DRAWITEM
  398.            return Super:HandleEvent( nMsg, nWParam, nLParam )
  399.  
  400.       case nMsg == WM_LBUTTONDOWN
  401.            return ::LButtonDown( nHiWord( nLParam ), nLoWord( nLParam ), nWParam )
  402.  
  403.       case nMsg == WM_RBUTTONDOWN
  404.            return ::RButtonDown( nHiWord( nLParam ), nLoWord( nLParam ), nWParam )
  405.  
  406.       case nMsg == WM_VSCROLL
  407.            ::VScroll( nWParam, nLParam )
  408.  
  409.       case nMsg == WM_HSCROLL
  410.            ::HScroll( nWParam, nLParam )
  411.  
  412.       case nMsg == WM_ERASEBKGND
  413.            if ::oBrush != nil
  414.               FillRect( nWParam, GetClientRect( ::hWnd ), ::oBrush:hBrush )
  415.               return .t.
  416.            endif
  417.  
  418.       case nMsg == WM_PAINT
  419.            ::BeginPaint()
  420.            ::Paint()
  421.            ::EndPaint()
  422.            return 0
  423.  
  424.       case nMsg == WM_DDE_ACK
  425.            DdeAck( nWParam, nLParam, ::hWnd )
  426.  
  427.       case nMsg == WM_QUERYENDSESSION
  428.            return ! ::End()
  429.    endcase
  430.  
  431. return nil
  432.  
  433. //----------------------------------------------------------------------------//
  434. // Conection with Borland's VBX DLL
  435.  
  436. DLL STATIC FUNCTION VbxInitDialog( hWnd AS WORD, hInstance AS WORD,;
  437.                        cResName AS STRING ) AS BOOL PASCAL LIB "BIVBX.DLL"
  438.  
  439. DLL STATIC FUNCTION VbxInit() AS BOOL PASCAL LIB "BIVBX.DLL"
  440.  
  441. //----------------------------------------------------------------------------//
  442.  
  443. static function CreateDlgError( Self )
  444.  
  445.    #define CANNOTCREATE_DIALOG 3
  446.    Eval( ErrorBlock(), ;
  447.         _FwGenError( CANNOTCREATE_DIALOG, CHR(13)+CHR(10) + ;
  448.                      If( !Empty( ::cResName ), "Resource: " + ::cResName,;
  449.                "Title: " + If( Empty( ::cCaption ), "", ::cCaption ) ) ) )
  450. return nil
  451.  
  452. //----------------------------------------------------------------------------//
  453.  
  454. METHOD Init() CLASS TDialog
  455.  
  456.    if ::lVbx
  457.       if ! VbxInitDialog( ::hWnd, ::hResources, ::cResName )
  458.          MsgAlert( "Error on VBX's initialization" )
  459.       endif
  460.    endif
  461.  
  462.    if ::oFont == nil
  463.       ::oFont = TFont()
  464.       ::oFont:hFont = ::SendMsg( WM_GETFONT )
  465.    endif
  466.  
  467.    AEval( ::aControls, { | oCtrl | oCtrl:Init( ::hWnd ) } )
  468.  
  469.    if ::lCentered
  470.       WndCenter( ::hWnd )
  471.    endif
  472.  
  473.    if ::cCaption != nil
  474.       ::cTitle = ::cCaption
  475.    endif
  476.  
  477.    if ::bInit != nil
  478.       Eval( ::bInit, Self )
  479.    endif
  480.  
  481. return .t.                              // .t. for focus
  482.  
  483. //----------------------------------------------------------------------------//
  484.