function DisplaySettings::OnWake()
{
   $displayDriver = "OpenGL";    
   CreateResolutionList("OpenGL");
   DisplayWindowMode();
   ShowDisplayDriver();
}

function CreateResolutionList(%driver)
{
   // clear the buttons first
   
   for(%i=1;%i<=20;%i++)
   {
      %control = "ds_res" @ %i;
      %control.SetText("");            
   }
   
   %list = getResolutionList(%driver);
   %num = 1;
      
   %count = getFieldCount(%list);   
   
   for(%i=0;%i<%count;%i++)
   {   
      // we only have 20 buttons for resolution in the display page, 19 because it starts at 0
      
      if(%i == 19)
         break;

      %field = getField(%list, %i);
      
      // let's exclude some smaller resolutions
      
      %res1 = getWord(%field, 0);
      %res2 = getWord(%field, 1);
      %color = getWord(%field, 2);

      if(%res1 >= 1024 && %color $= 32)
      {      
         if(%res2 == 600 || %res2 >= 768)
         {
            %control = "ds_res" @ %num;
            
            %control.SetText(%res1 SPC %res2);      
            %control.command = "SetResolution(" @ "\"" @ %field @ "\"" @ "," @ %num @ ");";
            
            if(%field $= $selectedRes)
            {
               $CurrentDSControl = %num;
               %control.setProfile(Gui24Red);
            }            
            %num++;
         }
      }         
   }   
}

// this doesn't actually change the resolution, just sets the value for when they hit apply

function SetResolution(%res, %num)
{
   if($CurrentDSControl !$= "")
   {
      %control = "ds_res" @ $CurrentDSControl;
      %control.setProfile(Gui24Yellow);
   }
   $SelectedRes = %res;      
   
   %control = "ds_res" @ %num;
   %control.SetProfile(Gui24Red);
   $CurrentDSControl = %num;   
}

function ShowResolution()
{
   for(%i=1;%i<=20;%i++)
   {
      %control = "ds_res" @ %i;      
   }

   if($CurrentDSControl !$= "")
   {
      %control = "ds_res" @ $CurrentDSControl;
      %control.setBitmap(expandfilename("~/data/images/gui/transparent.png"));
   }
   $SelectedRes = %res;      
   
   %control = "ds_res" @ %num;
   %control.setProfile(Gui24Red);
   $CurrentDSControl = %num;   
}

function ChangeDisplayDriver()
{
   if($displayDriver $= "OpenGL")    
   {
      $displayDriver = "D3D";
      $OldDisplayDriver = "OpenGL";
      %display = "D3D";
   }
   else
   {
      $displayDriver = "OpenGL";   
      $OldDisplayDriver = "D3D";
      %display = "OpenGL";
   }
   
   displaydriver.Text = %display;
}

function ShowDisplayDriver()
{
   if($displayDriver $= "opengl")
      %display = "OpenGL";
   else
      %display = "D3D";
      
   displaydriver.Text = %display;
}

function ChangeResolution()
{
   %width = getWord($selectedRes, 0);
   %height = getWord($selectedRes, 1);
   %color = getWord($selectedRes, 2);
          
   setScreenMode(%width, %height, %color, $fullscreen);
   
   $ChangeResolution = true;

   SaveResolution();
}

function ChangeWindowMode()
{
   if($fullscreen $= false)
   {
      $fullscreen = true;
      $OldFullScreen = false;
      windowsmode.setText("Full Screen");
   }
   else
   {
      $fullscreen = false;
      $OldFullScreen = true;
      windowsmode.setText("Window");
   }
}

function DisplayWindowMode()
{
   if($fullscreen $= true)
      windowsmode.setText("Full Screen");
   else
      windowsmode.setText("Window");
}

function SaveResolution()
{
   %fo = new fileobject();
  
   %file = "~/resolution.sav";
 
   %fo.OpenForWrite(%file);   
   
   %fo.WriteLine("fullscreen " @ $fullscreen);              
   %fo.WriteLine("resolution " @ $selectedRes);              
   %fo.WriteLine("currentdscontrol " @ $CurrentDSControl);              
      
   %fo.close();   
}

function CloseDisplaySettings()
{
   canvas.PopDialog(DisplaySettings);
   canvas.Popdialog(options);
   
   if($ChangeResolution $= true)
   {
      $ChangeResolution = "";
      $OldFullScreen = $fullscreen;     
      $OldDisplayDriver = $displayDriver; 
   }
   else
   {
      // put the globals back to whatever default they were
            
      $fullscreen = $OldFullScreen;     
      $displayDriver = $OldDisplayDriver;       
   }

}