Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a scaled bitmap using wxhaskell

The imageviewer example shows how to display an image in a ScrolledWindow.

What if I want to display the image in the available space, scaling the bitmap as needed?

My google-fu failed me on this one.

edit: I thought I had something with scrolledWindowSetScale, but it looks like it's not going to help here.

like image 698
David V. Avatar asked Nov 04 '22 15:11

David V.


1 Answers

Some people pointed me to functions in wxCore, so I could find a solution that works.

The function that does the drawing in the original example is:

onPaint vbitmap dc viewArea
  = do mbBitmap <- get vbitmap value
       case mbBitmap of
         Nothing -> return () 
         Just bm -> drawBitmap dc bm pointZero False []

using dcSetUserScale from wxCore, I was able to modify it to scale that way:

( sw is the scrolledWindow )

onPaint sw img dc viewArea = do
  mimg <- get img value
  case mimg of
    Nothing -> return ()
    Just bm -> do
      bsize <- get bm size
      vsize <- get sw size
      let scale = calcScale bsize vsize
      dcSetUserScale dc scale scale
      drawBitmap dc bm pointZero False []

calcScale :: Size -> Size -> Double
calcScale (Size bw bh) (Size vw vh) = min scalew scaleh
  where scalew = fromIntegral vw / fromIntegral bw
        scaleh = fromIntegral vh / fromIntegral bh
like image 97
David V. Avatar answered Nov 09 '22 07:11

David V.