-- App Display
----------------------------------------------------------
-- Last update: May 07, 2008 by Renaud
-- KNOWN ISSUES:
-- Here we are using both buddy api's old calls, and using the resolution xtra for mac to get the screen res

-- If ever a window refocus issue comes up where the buttons dont mouse over anymore...it has to do with 
-- (the stage).titlebarOptions.maximizebox = false, it seems that setting that property to false as I do the minimize
-- function would kill the mouse overs....setting this to false on new, somehow magically fixed the problem


----------------------------------------------------------
-- PUBLIC FUNCTIONS
-- 
-- isModeAvailable me, mode
-- setDisplayMode me, mode
-- getDisplayMode me
-- delete me
-- minize me
-- maximize me
-- hideVistaButton me
-- getWindowRect me

-- createWindow me, windowName, fileName, loc
-- moveWindows me
-- destroyWindow me, windowName, time


property pOriginalScreenSettings -- [height,width,depth,refreshrate]
property pDisplayMode -- #fullScreen, #windowed, #widescreen
property pNativeRes -- the res the art was created in
property pAvailableDisplayModes -- the available modes

property pWindowRect -- holds the working rect of the window

property pWindowsPendingDestruction

property pActiveDisplayMode -- 

on new me, nativeRes
  
  -- new
  (the stage).titlebarOptions.minimizebox = true
  (the stage).titlebarOptions.maximizebox = false
  (the stage).resizable = false
  
  
  pWindowsPendingDestruction = []
  
  pNativeRes = nativeRes
  
  if pNativeRes = point(640,480) then
  else if pNativeRes = point(800,600) then
  else if pNativeRes = point(1024,768) then
  else
    alerT("Invalid native resolution")
    return void
  end if
  
  -- get screen information
  if isMac() then
    pMacResXtra = new(Xtra "Resolution", "AARESX-64258-26244-89139") 
    var = pMacResXtra.get_current_display_mode()
    pOriginalScreenSettings = [var[1],var[2],var[3],0]
    pMacResXtra = void
  else
    pOriginalScreenSettings = [0,0,0,0] -- height, width, depth, refresh
    pOriginalScreenSettings[1] = integer(baScreenInfo( "width" )) -- baScreenInfo("width")
    pOriginalScreenSettings[2] = integer(baScreenInfo( "height" )) -- baScreenInfo("height")
    pOriginalScreenSettings[3] = integer(baScreenInfo( "depth" )) -- baScreenInfo("depth")
    pOriginalScreenSettings[4] = integer(baScreenInfo( "refresh" )) -- baScreenInfo("refresh") 
  end if
  
  -- determine available display modes
  pAvailableDisplayModes = [#fullscreen,#windowed]
  
  pWindowRect = duplicate(pNativeRes)
  
  return me
end

on isModeAvailable me, mode
  if pAvailableDisplayModes.getPos(mode) > 0 then
    return true
  else
    return false
  end if
end

on setDisplayMode me, mode
  --alert("setDisplayMode")
  
  if NOT(pDisplayMode=mode) then
    
    if me.isModeAvailable(mode) then
      pDisplayMode = mode
    else
      pDisplayMode = #windowed
    end if
    
    case pDisplayMode of
      #fullscreen:
        result = me.setToFullScreen() 
        if result = false then
          pDisplayMode = #windowed
        end if
        timeout().new("hideVistaButton", 500, #hideVistaButton, me)
      #windowed:
        me.setToWindowed()
      otherwise:
        alert("passed in invalid display mode")
    end case
  end if
end

on getDisplayMode me
  return pDisplayMode
end

on delete me
  if isMac() then
    baSetDisplay( pOriginalScreenSettings[1] , pOriginalScreenSettings[2] , pOriginalScreenSettings[3] , "temp" , false )
    baHideTaskBar( false )
    

    
  else
    OK = baSetDisplayEx( pOriginalScreenSettings[1] , pOriginalScreenSettings[2] , pOriginalScreenSettings[3] , pOriginalScreenSettings[4], "temp" , false )
    baHideTaskBar( false )
    baRefreshDesktop( false )
  end if
end

on minimize me
  
  if isMac() then
    _player.appMinimize()
  else
    case pDisplayMode of
      #fullscreen,#widescreen:
        baSetDisplayEx( pOriginalScreenSettings[1] , pOriginalScreenSettings[2] , pOriginalScreenSettings[3] , pOriginalScreenSettings[4], "temp" , false )
        _player.appMinimize()
        baHideTaskBar( false )
      otherwise:
    end case
  end if
  
end

on maximize me
  
  
  case pDisplayMode of
    #fullscreen:
      repeat with a = 1 to _player.windowlist.count
        _player.windowlist[a].restore()
      end repeat
      me.setToFullScreen()
      timeout().new("hideVistaButton", 500, #hideVistaButton, me)
      
  end case
  
end


on hideVistaButton me
  (the stage).rect = pWindowRect
  if _movie.timeoutList.getPos(timeOut("hideVistaButton")) > 0 then
    timeout("hideVistaButton").forget()
  end if
end


---------------------------------------------------------------------------------
-- PRIVATE FUNCTIONS
---------------------------------------------------------------------------------
on setToFullScreen me
  
  
  if isMac() then
    
    if baSetDisplay(pNativeRes[1] , pNativeRes[2] , pOriginalScreenSettings[3] , "temp" , false ) = 0 then
      pWindowRect = rect(0, 0, pNativeRes[1], pNativeRes[2])
      (the stage).titlebaroptions.visible = false
      (the stage).rect = rect(0, 0, pNativeRes[1], pNativeRes[2])
      baHideTaskBar( 2 )
      pActiveDisplayMode = #fullScreen
    else
      alert("Couldn't change monitor resolution")
    end if
 
    
  else
    
    if baSetDisplayEx( pNativeRes[1] , pNativeRes[2] , 32 , pOriginalScreenSettings[4], "test" , false ) = 0 then
      workingRes = pNativeRes
      theDepth = 32
    else if baSetDisplayEx( pNativeRes[1] , pNativeRes[2] , pOriginalScreenSettings[3] , pOriginalScreenSettings[4], "test" , false ) = 0 then
      workingRes = pNativeRes
      theDepth = pOriginalScreenSettings[3]
    else if baSetDisplayEx( 800 , 600 , 32 , pOriginalScreenSettings[4], "test" , false ) = 0 then
      workingRes = point(800,600)
      theDepth = 32
    else if baSetDisplayEx( 800 , 600 , pOriginalScreenSettings[3] , pOriginalScreenSettings[4], "test" , false ) = 0 then
      workingRes = point(800,600)
      theDepth = pOriginalScreenSettings[3]
    else if baSetDisplayEx( 640 , 480 , pOriginalScreenSettings[3] , pOriginalScreenSettings[4], "test" , false ) = 0 then
      workingRes = point(640,480)
      theDepth = pOriginalScreenSettings[3]
    end if
    
    if NOT(workingRes=void) then
      
      pWindowRect = rect(0, 0, workingRes[1], workingRes[2])
      
      baHideTaskBar( true )
      OK = baWindowToFront( baWinHandle() )
      Result = baSetDisplayEx( workingRes[1] , workingRes[2] , theDepth , pOriginalScreenSettings[4], "temp" , false )
      (the stage).titlebaroptions.visible = false
      (the stage).rect = rect(0, 0, workingRes[1], workingRes[2])
      --(the stage).resizable = false
      baHideTaskBar( true )
      OK = baWindowToFront( baWinHandle() )
      _movie.stage.drawRect = rect(0, 0, workingRes[1], workingRes[2])
      pActiveDisplayMode = #fullScreen
    else
      alerT("coudln't find a working resolution for this PC")
      return false
    end if
    
  end if
  
  return true
end


on setToWindowed me, newDims
  if pActiveDisplayMode = #windowed then
    -- dont do this again
    return
  end if
  
  -- make our window fit in the desktop
  if newDims = VOID then
    
    if pNativeRes[1]+50 < pOriginalScreenSettings[1] then
      windowedRes = pNativeRes
    else
      if 800+50 < pOriginalScreenSettings[1] then
        windowedRes = point(800,600)
      else
        if 640 + 50 < pOriginalScreenSettings[1] then
          windowedRes = point(640,480)
        else
          windowedRes = point(400,300)
        end if
      end if
    end if
    
  else
    
    windowedRes = newDims
    
  end if
  
  
  if isMac() then
    
    pWindowRect = rect(50, 50, windowedRes[1]+50, windowedRes[2]+50)
    baSetDisplay( pOriginalScreenSettings[1] , pOriginalScreenSettings[2] , pOriginalScreenSettings[3] , "temp" , false )
    (the stage).titlebaroptions.visible = true
    (the stage).titlebarOptions.minimizebox = true
    (the stage).titlebarOptions.maximizebox = false
    (the stage).rect = rect(50, 50, windowedRes[1]+50, windowedRes[2]+50)
    (the stage).resizable = false
    baHideTaskBar( false )
    pActiveDisplayMode = #windowed
    
  else
    
    if baSetDisplayEx( pOriginalScreenSettings[1] , pOriginalScreenSettings[2] , 32 , pOriginalScreenSettings[4], "test" , false ) = 0 then
      theDepth = 32
    else
      theDepth = pOriginalScreenSettings[3]
    end if
    pWindowRect = rect(50, 50, windowedRes[1]+50, windowedRes[2]+50)
    OK = baSetDisplayEx( pOriginalScreenSettings[1] , pOriginalScreenSettings[2] , theDepth , pOriginalScreenSettings[4], "temp" , false )
    (the stage).titlebaroptions.visible = true
    (the stage).titlebarOptions.minimizebox = true
    (the stage).titlebarOptions.maximizebox = false
    (the stage).rect = rect(50, 50, windowedRes[1]+50, windowedRes[2]+50)
    (the stage).resizable = false
    _movie.stage.drawRect = rect(0, 0, windowedRes[1], windowedRes[2])
    baHideTaskBar( false )
    pActiveDisplayMode = #windowed
    
  end if
  
end

on getWindowRect me
  return pWindowRect
end


--------------------------------------------------------------------
-- MIAW Stuff
--------------------------------------------------------------------
on createWindow me, windowName, fileName, loc
  
  if loc=void then
    loc = point(0,0)
  end if
  
  -- We use the stageRight and the stageTop
  
  if 1 then
    -- window loc (need to know where this window goes, could be a menu)
    window().new(windowName)
    window(windowName).fileName = fileName
    
    
    --width = window(windowName).sourceRect[3] - window(windowName).sourceRect[1]
    --height = window(windowName).sourceRect[4] - window(windowName).sourceRect[2]
    window(windowName).title = windowName
    window(windowName).resizable = false
    window(windowName).titlebarOptions.visible = false
    window(windowName).appearanceOptions.border = #none
    window(windowName).type = #tool
    window(windowName).open()
  end if

  
  me.moveWindows()
  
end

on moveWindows me
  -- have all windows line up with the first
  rect = the stage.rect --_player.windowList[1].rect
  loc = point(rect[1],rect[2])
  repeat with a = 1 to _player.windowList.count
    
    if NOT(_player.windowList[a]=the stage) then
      
      winRect = _player.windowList[a].rect
      
      width = winRect[3] - winRect[1]
      height = winRect[4] - winRect[2]
      
      _player.windowList[a].rect = rect(loc[1],loc[2],loc[1]+width,loc[2]+height)
    end if
    
  end repeat
end

-- the reason we may want to wait to destroy the window, is in case the window
-- we want to destroy just made a tell call, the stage needs time to respond
-- to the miaw before the miaw destroys, otherwise you get a crash
on destroyWindow me, windowName, time
  if time=void then -- can't destroy window right away
    if NOT(window(windowName)=void) then
      -- window(windowName).close()
      window(windowName).forget()
    end if
  else
    pWindowsPendingDestruction.append(windowName)
    timeout().new("timeToDestroy", time, #timeToDestroy, me)
  end if
end



----------------------------------------------------------------------
-- PROTECTED
----------------------------------------------------------------------
on timeToDestroy me
  if _movie.timeoutList.getPos(timeOut("timeToDestroy")) > 0 then
    timeout("timeToDestroy").forget()
  end if
  if pWindowsPendingDestruction.count > 0 then
    windowName = pWindowsPendingDestruction[1]
    pWindowsPendingDestruction.deleteAt(1)
    if NOT(window(windowName)=void) then
      -- window(windowName).close()
      window(windowName).forget()
    end if
  end if
end

