-- originally Euman's code, with C.K. Lester's patch, 
-- and some re-arrangement.

without warning
include win32lib.ew

-- set up calls to two gdi graphics functions
constant 
  zSetViewportExtEx = registerw32Function( gdi32, "SetViewportExtEx", {C_LONG, C_INT, C_INT, C_INT}, C_INT ),
  zSetWindowExtEx = registerw32Function( gdi32, "SetWindowExtEx", {C_LONG, C_INT, C_INT, C_POINTER}, C_INT )

constant
  Win = create( Window, "Mapping Mode Scaling Demo", NULL, 25, 25, 300, 200, 0 )

atom WinDC, void

constant  
  zMM_ANISOTROPIC = 8

-- onOpen[Win]
procedure onOpen_Win( atom self, atom event, sequence parms )
  atom oldMapMode
  sequence InitExt
    WinDC = getDC( Win )
    InitExt = getClientRect( Win )
    oldMapMode = w32Func( xSetMapMode, {WinDC, zMM_ANISOTROPIC} )
    void = w32Func( zSetWindowExtEx, {WinDC, InitExt[3]-InitExt[1], InitExt[4]-InitExt[2], 0} )
    setPenColor( Win, BrightRed )
end procedure
setHandler( Win, w32HOpen, routine_id( "onOpen_Win" ) )

-- onResize[Win], -- onGotFocus[Win]
procedure onResize_Win( atom self, atom event, sequence parms )
  sequence CurExt
    CurExt = getClientRect( Win )
    repaintRect( Win, CurExt[1], CurExt[2], CurExt[3], CurExt[4] )
    if w32Func( zSetViewportExtEx, {WinDC, CurExt[3]-CurExt[1], CurExt[4]-CurExt[2], 0} ) then
      drawEllipse( Win, True, CurExt[1], CurExt[2], CurExt[3], CurExt[4] )
    end if
end procedure
setHandler( Win, {w32HResize, w32HGotFocus}, routine_id( "onResize_Win" ) )

-- onClose[Win]
procedure onClose_Win( atom self, atom event, sequence parms )
  releaseDC( Win )
end procedure
setHandler( Win, w32HClose, routine_id( "onClose_Win" ) )


WinMain( Win, Normal )

