'====================================================================
' Owner-draw button demo, modal dialog as main.
'
' Adapted from an example in the Microsoft PSDK 
' using bitmaps from: examples\GL\NeHe\data\
'====================================================================

$ filename "Ownerdrawn.exe"
'uses rtl32
'uses rtl64

'% review

uses dialogs


type DRAWITEMSTRUCT
  uint  CtlType
  uint  CtlID
  uint  itemID
  uint  itemAction
  uint  itemState
  sys   hwndItem
  sys   hDC
  RECT  rcItem
  dword itemData
end type


% ID_Button=100

'====================================================================

function DialogProc( sys hDlg, uint uMsg, sys wParam, lParam ) as int callback

  static sys hBmp1, hBmp2
  static sys hdcMem

  select case uMsg

    case WM_INITDIALOG
       hBmp1 = LoadImage( null, "mask1.bmp" , IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)
       if hBmp1 = null then mbox "mask1.bmp missing!"

       hBmp2 = LoadImage( null, "mask2.bmp" , IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)
       if hBmp2 = null then mbox "mask2.bmp missing!"

       return true

    case WM_DRAWITEM    
       DRAWITEMSTRUCT lpdis at lParam

       hdcMem = CreateCompatibleDC( lpdis.hDC )

       if (lpdis.itemState and ODS_SELECTED) then
         SelectObject( hdcMem, hBmp2 )
       else
         SelectObject( hdcMem, hBmp1)
       end if

       StretchBlt( lpdis.hDC,
                   lpdis.rcItem.left,
                   lpdis.rcItem.top,
                   lpdis.rcItem.right - lpdis.rcItem.left,
                   lpdis.rcItem.bottom - lpdis.rcItem.top,
                   hdcMem,
                   0,
                   0,
                   128,
                   128,
                   SRCCOPY )

       ' Return hDC member of DRAWITEMSTRUCT to default state.
       ' This is ESSENTIAL for proper button operation.
       DeleteDC( hdcMem )

       return true

    case WM_COMMAND
       if loword(wParam) = IDCANCEL then
         ' This will allow the user to close the
         ' dialog by pressing the Escape key.
         EndDialog( hDlg, null )
       end if

       if hiword(wParam) = BN_CLICKED then
         select case loword(wParam)
           case ID_Button
#ifdef review          
   printl "Click..."
#endif   
         end select

         return true
       end if

    case WM_CLOSE
       DeleteObject( hBmp1 )
       DeleteObject( hBmp2 )

       EndDialog( hDlg, null )

  end select

  return 0
end function

'====================================================================

Dialog( 0, 0, 100, 75, "Owner Draw Button Demo",
        WS_OVERLAPPED or WS_SYSMENU or DS_CENTER )

'Control(  "", ID_Button, "BUTTON", BS_OWNERDRAW or WS_DLGFRAME, 35, 20, 20, 20 )
PushButton( "", ID_Button, 35, 20, 20, 20, BS_OWNERDRAW or WS_DLGFRAME )

CreateModalDialog( 0, @DialogProc, 0 )

'====================================================================