2007-08-10 11:27:13 +02:00
#region - - - License - - -
/ * Copyright ( c ) 2006 , 2007 Stefanos Apostolopoulos
* See license . txt for license info
* /
#endregion
using System ;
2007-08-05 15:42:31 +02:00
using System.Collections.Generic ;
using System.Text ;
2007-09-09 13:52:09 +02:00
using System.Windows.Forms ;
2007-08-05 15:42:31 +02:00
namespace OpenTK.Platform.X11
{
/// <summary>
2007-09-09 13:52:09 +02:00
/// Describes a Windows.Form.Control, Windows.Forms.NativeWindow or OpenTK.GameWindow on the X11 platform.
2007-08-05 15:42:31 +02:00
/// This class supports OpenTK, and is not intended for use by OpenTK programs.
/// </summary>
2007-09-21 22:07:30 +02:00
public sealed class WindowInfo : IMutableWindowInfo
2007-08-05 15:42:31 +02:00
{
2007-09-21 22:07:30 +02:00
private IntPtr handle , topLevelWindow ;
private IntPtr rootWindow , display ;
2007-09-09 13:52:09 +02:00
private int screen ;
2007-09-25 18:45:12 +02:00
private X11 . WindowInfo parent ;
2007-09-09 13:52:09 +02:00
private XVisualInfo visinfo ;
2007-09-21 22:07:30 +02:00
private static Type xplatui ;
2007-09-25 18:45:12 +02:00
private EventMask eventMask ;
2007-09-09 13:52:09 +02:00
2007-08-21 11:01:24 +02:00
public WindowInfo ( )
2007-08-10 11:27:13 +02:00
{
2007-09-21 22:07:30 +02:00
//visinfo = new XVisualInfo();
}
public WindowInfo ( IWindowInfo info )
{
this . CopyInfoFrom ( info ) ;
2007-08-07 22:32:26 +02:00
}
2007-08-21 11:01:24 +02:00
2007-09-09 13:52:09 +02:00
public WindowInfo ( Control control )
2007-08-10 11:27:13 +02:00
{
2007-09-21 22:07:30 +02:00
if ( control = = null )
throw new ArgumentException ( "Control cannot be null." ) ;
this . CopyInfoFromXPlatUI ( ) ;
this . Handle = control . Handle ;
this . Parent = control . Parent ! = null ? new WindowInfo ( control . Parent ) : this . Parent ;
this . TopLevelWindow = control . TopLevelControl ! = null ? control . TopLevelControl . Handle : IntPtr . Zero ;
2007-08-05 15:42:31 +02:00
}
2007-09-09 13:52:09 +02:00
public WindowInfo ( NativeWindow window )
{
2007-09-21 22:07:30 +02:00
if ( window = = null )
throw new ArgumentException ( "NativeWindow cannot be null." ) ;
this . CopyInfoFromXPlatUI ( ) ;
this . Handle = window . Handle ;
this . Parent = null ;
this . TopLevelWindow = IntPtr . Zero ;
2007-09-09 13:52:09 +02:00
}
public WindowInfo ( GameWindow window )
{
2007-09-21 22:07:30 +02:00
if ( window = = null )
throw new ArgumentException ( "GameWindow cannot be null." ) ;
2007-09-09 13:52:09 +02:00
2007-09-21 22:07:30 +02:00
this . CopyInfoFrom ( window . WindowInfo ) ;
2007-09-09 13:52:09 +02:00
}
#region - - - IWindowInfo Members - - -
2007-09-09 17:10:21 +02:00
public IntPtr Handle { get { return handle ; } internal set { handle = value ; } }
public IWindowInfo Parent { get { return parent ; } internal set { parent = value as WindowInfo ; } }
public IWindowInfo GetInfoFrom ( Control control )
2007-09-09 13:52:09 +02:00
{
if ( control = = null )
2007-09-21 22:07:30 +02:00
throw new ArgumentException ( "Control cannot be null." ) ;
return new WindowInfo ( control ) ;
2007-09-09 13:52:09 +02:00
}
2007-09-09 17:10:21 +02:00
public IWindowInfo GetInfoFrom ( NativeWindow window )
2007-09-09 13:52:09 +02:00
{
if ( window = = null )
2007-09-21 22:07:30 +02:00
throw new ArgumentException ( "NativeWindow cannot be null." ) ;
2007-09-09 13:52:09 +02:00
2007-09-21 22:07:30 +02:00
return new WindowInfo ( window ) ;
2007-09-09 13:52:09 +02:00
}
2007-09-09 17:10:21 +02:00
public IWindowInfo GetInfoFrom ( GameWindow window )
2007-09-09 13:52:09 +02:00
{
if ( window = = null )
throw new ArgumentException ( "GameWindow cannot be null." ) ;
2007-09-25 18:45:12 +02:00
return this . GetInfoFrom ( window . WindowInfo ) ;
2007-09-09 13:52:09 +02:00
}
2007-09-09 17:10:21 +02:00
public IWindowInfo GetInfoFrom ( IWindowInfo info )
2007-09-09 13:52:09 +02:00
{
if ( info = = null )
throw new ArgumentException ( "WindowInfo cannot be null" ) ;
2007-09-09 17:10:21 +02:00
return info ;
}
2007-09-09 13:52:09 +02:00
2007-09-21 22:07:30 +02:00
#endregion
#region - - - IMutableWindowInfo Members - - -
2007-09-09 17:10:21 +02:00
public void CopyInfoFrom ( IWindowInfo info )
{
2007-09-25 18:45:12 +02:00
if ( info = = null )
throw new ArgumentException ( "IWindowInfo info cannot be null." ) ;
2007-09-09 13:52:09 +02:00
this . Handle = info . Handle ;
this . Parent = info . Parent ;
2007-09-27 02:40:28 +02:00
// Work around Mono 1.2.4 gmcs bug (resolved in 1.2.5)
// X11.WindowInfo winfo = info as X11.WindowInfo ?? (X11.WindowInfo)(info as Platform.WindowInfo);
X11 . WindowInfo winfo = info as X11 . WindowInfo ;
if ( winfo = = null )
winfo = ( X11 . WindowInfo ) ( info as Platform . WindowInfo ) ;
2007-09-09 17:10:21 +02:00
2007-09-09 13:52:09 +02:00
this . RootWindow = winfo . RootWindow ;
this . TopLevelWindow = winfo . TopLevelWindow ;
this . Display = winfo . Display ;
this . Screen = winfo . Screen ;
this . VisualInfo = winfo . VisualInfo ;
2007-09-25 18:45:12 +02:00
this . EventMask = winfo . EventMask ;
2007-09-09 13:52:09 +02:00
}
#endregion
2007-08-05 15:42:31 +02:00
2007-09-09 13:52:09 +02:00
public IntPtr RootWindow { get { return rootWindow ; } internal set { rootWindow = value ; } }
public IntPtr TopLevelWindow { get { return topLevelWindow ; } internal set { topLevelWindow = value ; } }
2007-09-25 18:45:12 +02:00
public IntPtr ParentHandle { get { return parent . Handle ; } internal set { parent . Handle = value ; } }
2007-09-09 13:52:09 +02:00
public IntPtr Display { get { return display ; } internal set { display = value ; } }
public int Screen { get { return screen ; } internal set { screen = value ; } }
public XVisualInfo VisualInfo { get { return visinfo ; } internal set { visinfo = value ; } }
2007-09-25 18:45:12 +02:00
public EventMask EventMask { get { return eventMask ; } internal set { eventMask = value ; } }
2007-08-05 18:44:31 +02:00
public override string ToString ( )
{
2007-08-05 21:04:06 +02:00
return String . Format ( "X11.WindowInfo: Display {0}, Screen {1}, Handle {2}, Parent: ({3})" ,
2007-08-05 18:54:14 +02:00
this . Display , this . Screen , this . Handle , this . Parent ! = null ? this . Parent . ToString ( ) : "null" ) ;
2007-08-05 18:44:31 +02:00
}
2007-09-21 22:07:30 +02:00
private void CopyInfoFromXPlatUI ( )
{
xplatui = Type . GetType ( "System.Windows.Forms.XplatUIX11, System.Windows.Forms" ) ;
if ( xplatui = = null )
throw new ApplicationException ( "Could not get System.Windows.Forms.XplatUIX11 through reflection. Unsupported platform or Mono runtime version, aborting." ) ;
this . Display = ( IntPtr ) xplatui . GetField ( "DisplayHandle" ,
System . Reflection . BindingFlags . Static | System . Reflection . BindingFlags . NonPublic ) . GetValue ( null ) ;
this . RootWindow = ( IntPtr ) xplatui . GetField ( "RootWindow" ,
System . Reflection . BindingFlags . Static | System . Reflection . BindingFlags . NonPublic ) . GetValue ( null ) ;
this . Screen = ( int ) xplatui . GetField ( "ScreenNo" ,
System . Reflection . BindingFlags . Static | System . Reflection . BindingFlags . NonPublic ) . GetValue ( null ) ;
2007-09-25 18:45:12 +02:00
2007-09-21 22:07:30 +02:00
}
2007-08-05 15:42:31 +02:00
}
}