2007-07-23 00:15:18 +00:00
#region - - - License - - -
/ * Copyright ( c ) 2007 Stefanos Apostolopoulos
* See license . txt for license info
* /
#endregion
using System ;
using System.Collections.Generic ;
using System.Text ;
using System.Windows.Forms ;
using System.Drawing ;
using System.Diagnostics ;
namespace OpenTK.Platform.X11
{
sealed class X11GLControl : IGLControl
{
2007-08-05 13:42:31 +00:00
WindowInfo info = new WindowInfo ( ) ;
2007-08-21 10:48:32 +00:00
//DisplayMode mode;
2007-07-23 00:15:18 +00:00
private Type xplatui ;
2007-09-09 15:10:21 +00:00
IGLContext glContext ;
2007-07-23 00:15:18 +00:00
private bool disposed ;
2007-08-04 12:09:58 +00:00
private bool fullscreen ;
2007-07-23 00:15:18 +00:00
#region - - - Contructors - - -
2007-08-14 16:57:25 +00:00
2007-09-09 11:52:09 +00:00
#region public X11GLControl ( UserControl c )
2007-08-21 10:48:32 +00:00
public X11GLControl ( UserControl c )
2007-07-23 00:15:18 +00:00
{
2007-08-09 12:06:09 +00:00
Debug . WriteLine ( "Creating opengl control (X11GLControl driver)" ) ;
Debug . Indent ( ) ;
2007-08-14 16:57:25 +00:00
2007-08-21 10:48:32 +00:00
Utilities . ThrowOnX11Error = true ;
2007-08-20 12:25:48 +00:00
2007-08-09 12:06:09 +00:00
if ( c = = null /* || c.TopLevelControl == null*/ )
{
throw new ArgumentException ( "UserControl c may not be null." ) ;
}
2007-07-23 00:15:18 +00:00
2007-08-20 12:25:48 +00:00
c . HandleCreated + = new EventHandler ( c_HandleCreated ) ;
c . HandleDestroyed + = new EventHandler ( c_HandleDestroyed ) ;
2007-08-14 16:57:25 +00:00
2007-07-23 00:15:18 +00:00
xplatui = Type . GetType ( "System.Windows.Forms.XplatUIX11, System.Windows.Forms" ) ;
2007-09-09 11:52:09 +00:00
if ( xplatui = = null )
throw new ApplicationException ( "Could not get System.Windows.Forms.XplatUIX11 through reflection. Unsupported platform or Mono runtime version, aborting." ) ;
info . Display = ( IntPtr ) xplatui . GetField ( "DisplayHandle" ,
System . Reflection . BindingFlags . Static | System . Reflection . BindingFlags . NonPublic ) . GetValue ( null ) ;
info . RootWindow = ( IntPtr ) xplatui . GetField ( "RootWindow" ,
System . Reflection . BindingFlags . Static | System . Reflection . BindingFlags . NonPublic ) . GetValue ( null ) ;
info . Screen = ( int ) xplatui . GetField ( "ScreenNo" ,
System . Reflection . BindingFlags . Static | System . Reflection . BindingFlags . NonPublic ) . GetValue ( null ) ;
Debug . Print ( "Data read from System.Windows.Forms.XplatUIX11: {0}" , info . ToString ( ) ) ;
2007-07-23 00:15:18 +00:00
2007-09-09 11:52:09 +00:00
//this.mode = mode;
2007-09-09 15:10:21 +00:00
glContext = new GLContext ( null , info ) ;
2007-09-09 11:52:09 +00:00
//glContext.PrepareContext(info);
2007-09-09 15:10:21 +00:00
info . VisualInfo = ( glContext . Info as X11 . WindowInfo ) . VisualInfo ;
2007-08-21 10:48:32 +00:00
2007-09-09 11:52:09 +00:00
Debug . Print ( "Setting XplatUIX11.CustomVisual" ) ;
xplatui . GetField ( "CustomVisual" , System . Reflection . BindingFlags . Static | System . Reflection . BindingFlags . NonPublic )
. SetValue ( null , info . VisualInfo . visual ) ;
2007-08-20 12:25:48 +00:00
2007-09-09 11:52:09 +00:00
Debug . Print ( "Setting XplatUIX11.CustomColormap" ) ;
xplatui . GetField ( "CustomColormap" , System . Reflection . BindingFlags . Static | System . Reflection . BindingFlags . NonPublic )
. SetValue ( null , API . CreateColormap ( info . Display , info . RootWindow , info . VisualInfo . visual , 0 ) ) ;
2007-08-20 14:12:57 +00:00
Debug . Unindent ( ) ;
2007-08-20 12:25:48 +00:00
}
2007-07-23 00:15:18 +00:00
2007-09-09 11:52:09 +00:00
#endregion
#region void c_HandleCreated ( object sender , EventArgs e )
2007-08-20 12:25:48 +00:00
void c_HandleCreated ( object sender , EventArgs e )
{
UserControl c = ( sender as UserControl ) ;
Debug . Print ( "GLControl handle created, creating X11GLContext." ) ;
Debug . Indent ( ) ;
try
{
2007-09-09 15:10:21 +00:00
( glContext . Info as X11 . WindowInfo ) . Handle = info . Handle = ( sender as UserControl ) . Handle ;
glContext . CreateContext ( true , null ) ;
2007-08-20 14:12:57 +00:00
glContext . MakeCurrent ( ) ;
2007-07-23 00:15:18 +00:00
}
2007-08-20 12:25:48 +00:00
catch ( ApplicationException expt )
{
Debug . Print ( expt . ToString ( ) ) ;
throw ;
}
finally
{
Debug . Unindent ( ) ;
}
}
2007-09-09 11:52:09 +00:00
#endregion
#region void c_HandleDestroyed ( object sender , EventArgs e )
2007-08-20 12:25:48 +00:00
void c_HandleDestroyed ( object sender , EventArgs e )
{
Debug . Print ( "X11GLControl handle destroyed, disposing X11GLContext." ) ;
glContext . Dispose ( ) ;
2007-07-23 00:15:18 +00:00
}
2007-09-09 11:52:09 +00:00
#endregion
2007-08-21 10:48:32 +00:00
#region private IntPtr FindColormap ( )
2007-08-20 12:25:48 +00:00
/// <summary>
/// Finds a colormap suitable for use with the GLControl.
/// </summary>
/// <returns>A pointer to the colormap</returns>
/// <remarks>
/// If the visual of the GLControl matches the default visual, the function returns
/// the default colormap (i.e. the colormap of the root window). Otherwise, it creates
/// a new, private colormap.
/// </remarks>
private IntPtr FindColormap ( )
{
if ( info . VisualInfo . visual = = Functions . XDefaultVisual ( info . Display , info . Screen ) )
{
return Functions . XDefaultColormap ( info . Display , info . Screen ) ;
}
2007-09-09 15:10:21 +00:00
return API . CreateColormap ( info . Display , info . RootWindow ,
( glContext . Info as X11 . WindowInfo ) . VisualInfo . visual , 0 /*AllocNone*/ ) ;
2007-08-20 12:25:48 +00:00
}
2007-07-23 00:15:18 +00:00
#endregion
2007-08-21 10:48:32 +00:00
#endregion
2007-07-23 00:15:18 +00:00
#region - - - IGLControl Members - - -
public event CreateEvent Create ;
private void OnCreate ( object sender , EventArgs e )
{
if ( this . Create ! = null )
this . Create ( sender , e ) ;
}
2007-08-04 12:09:58 +00:00
#region public bool IsIdle
2007-07-23 00:15:18 +00:00
public bool IsIdle
{
get
{
return API . Pending ( info . Display ) = = 0 ;
}
}
2007-08-04 12:09:58 +00:00
#endregion
#region public bool Fullscreen
2007-07-23 00:15:18 +00:00
public bool Fullscreen
{
get
{
2007-08-04 12:09:58 +00:00
return fullscreen ;
2007-07-23 00:15:18 +00:00
}
set
{
2007-08-05 13:51:07 +00:00
//throw new Exception("The method or operation is not implemented.");
fullscreen = false ;
2007-07-23 00:15:18 +00:00
}
}
2007-08-04 12:09:58 +00:00
#endregion
#region public IGLContext Context
public IGLContext Context
2007-07-23 00:15:18 +00:00
{
get
{
return glContext ;
}
}
2007-08-04 12:09:58 +00:00
#endregion
2007-07-23 00:15:18 +00:00
#endregion
#region - - - IDisposable Members - - -
public void Dispose ( )
{
this . Dispose ( true ) ;
GC . SuppressFinalize ( this ) ;
}
private void Dispose ( bool manuallyCalled )
{
if ( ! disposed )
{
// Clean unmanaged resources:
2007-08-04 12:09:58 +00:00
// Nothing
2007-07-23 00:15:18 +00:00
if ( manuallyCalled )
{
// Clean managed resources, too
glContext . Dispose ( ) ;
}
}
disposed = true ;
}
~ X11GLControl ( )
{
this . Dispose ( false ) ;
}
#endregion
}
}