00001 #region License
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #endregion
00027
00028 using System;
00029 using System.ComponentModel;
00030 using System.Drawing;
00031 using OpenTK.Graphics;
00032 using OpenTK.Input;
00033 using OpenTK.Platform;
00034
00035 namespace OpenTK
00036 {
00037
00041 public class NativeWindow : INativeWindow
00042 {
00043 #region --- Fields ---
00044
00045 private readonly GameWindowFlags options;
00046
00047 private readonly DisplayDevice device;
00048
00049 private readonly INativeWindow implementation;
00050
00051 private bool disposed, events;
00052
00053 #endregion
00054
00055 #region --- Contructors ---
00056
00058 public NativeWindow()
00059 : this(640, 480, "OpenTK Native Window", GameWindowFlags.Default, GraphicsMode.Default, DisplayDevice.Default) { }
00060
00061
00062
00072 public NativeWindow(int width, int height, string title, GameWindowFlags options, GraphicsMode mode, DisplayDevice device)
00073 : this(device.Bounds.Left + (device.Bounds.Width - width) / 2,
00074 device.Bounds.Top + (device.Bounds.Height - height) / 2,
00075 width, height, title, options, mode, device) { }
00076
00088 public NativeWindow(int x, int y, int width, int height, string title, GameWindowFlags options, GraphicsMode mode, DisplayDevice device)
00089 {
00090
00091 if (width < 1)
00092 throw new ArgumentOutOfRangeException("width", "Must be greater than zero.");
00093 if (height < 1)
00094 throw new ArgumentOutOfRangeException("height", "Must be greater than zero.");
00095 if (mode == null)
00096 throw new ArgumentNullException("mode");
00097 if (device == null)
00098 throw new ArgumentNullException("device");
00099
00100 this.options = options;
00101 this.device = device;
00102
00103 implementation = Factory.Default.CreateNativeWindow(x, y, width, height, title, mode, options, this.device);
00104
00105 if ((options & GameWindowFlags.Fullscreen) != 0)
00106 {
00107 this.device.ChangeResolution(width, height, mode.ColorFormat.BitsPerPixel, 0);
00108 WindowState = WindowState.Fullscreen;
00109 }
00110 }
00111
00112 #endregion
00113
00114 #region --- INativeWindow Members ---
00115
00116 #region Methods
00117
00118 #region Close
00119
00123 public void Close()
00124 {
00125 EnsureUndisposed();
00126 implementation.Close();
00127 }
00128
00129 #endregion
00130
00131 #region PointToClient
00132
00142 public Point PointToClient(Point point)
00143 {
00144 return implementation.PointToClient(point);
00145 }
00146
00147 #endregion
00148
00149 #region PointToScreen
00150
00160 public Point PointToScreen(Point point)
00161 {
00162
00163
00164 Point trans = PointToClient(Point.Empty);
00165 point.X -= trans.X;
00166 point.Y -= trans.Y;
00167 return point;
00168 }
00169
00170 #endregion
00171
00172 #region ProcessEvents
00173
00177 public void ProcessEvents()
00178 {
00179 ProcessEvents(false);
00180 }
00181
00182 #endregion
00183
00184 #endregion
00185
00186 #region Properties
00187
00188 #region Bounds
00189
00194 public Rectangle Bounds
00195 {
00196 get
00197 {
00198 EnsureUndisposed();
00199 return implementation.Bounds;
00200 }
00201 set
00202 {
00203 EnsureUndisposed();
00204 implementation.Bounds = value;
00205 }
00206 }
00207
00208 #endregion
00209
00210 #region ClientRectangle
00211
00216 public Rectangle ClientRectangle
00217 {
00218 get
00219 {
00220 EnsureUndisposed();
00221 return implementation.ClientRectangle;
00222 }
00223 set
00224 {
00225 EnsureUndisposed();
00226 implementation.ClientRectangle = value;
00227 }
00228 }
00229
00230 #endregion
00231
00232 #region ClientSize
00233
00237 public Size ClientSize
00238 {
00239 get
00240 {
00241 EnsureUndisposed();
00242 return implementation.ClientSize;
00243 }
00244 set
00245 {
00246 EnsureUndisposed();
00247 implementation.ClientSize = value;
00248 }
00249 }
00250
00251 #endregion
00252
00253 #region Exists
00254
00258 public bool Exists
00259 {
00260 get
00261 {
00262 return IsDisposed ? false : implementation.Exists;
00263 }
00264 }
00265
00266 #endregion
00267
00268 #region Focused
00269
00273 public bool Focused
00274 {
00275 get
00276 {
00277 EnsureUndisposed();
00278 return implementation.Focused;
00279 }
00280 }
00281
00282 #endregion
00283
00284 #region Height
00285
00289 public int Height
00290 {
00291 get
00292 {
00293 EnsureUndisposed();
00294 return implementation.Height;
00295 }
00296 set
00297 {
00298 EnsureUndisposed();
00299 implementation.Height = value;
00300 }
00301 }
00302
00303 #endregion
00304
00305 #region Icon
00306
00310 public Icon Icon
00311 {
00312 get
00313 {
00314 EnsureUndisposed();
00315 return implementation.Icon;
00316 }
00317 set
00318 {
00319 EnsureUndisposed();
00320 implementation.Icon = value;
00321 }
00322 }
00323
00324 #endregion
00325
00326 #region InputDriver
00327
00331 [Obsolete]
00332 public IInputDriver InputDriver
00333 {
00334 get
00335 {
00336 EnsureUndisposed();
00337 return implementation.InputDriver;
00338 }
00339 }
00340
00341 #endregion
00342
00343 #region Location
00344
00348 public Point Location
00349 {
00350 get
00351 {
00352 EnsureUndisposed();
00353 return implementation.Location;
00354 }
00355 set
00356 {
00357 EnsureUndisposed();
00358 implementation.Location = value;
00359 }
00360 }
00361
00362 #endregion
00363
00364 #region Size
00365
00369 public Size Size
00370 {
00371 get
00372 {
00373 EnsureUndisposed();
00374 return implementation.Size;
00375 }
00376 set
00377 {
00378 EnsureUndisposed();
00379 implementation.Size = value;
00380 }
00381 }
00382
00383 #endregion
00384
00385 #region Title
00386
00390 public string Title
00391 {
00392 get
00393 {
00394 EnsureUndisposed();
00395 return implementation.Title;
00396 }
00397 set
00398 {
00399 EnsureUndisposed();
00400 implementation.Title = value;
00401 }
00402 }
00403
00404 #endregion
00405
00406 #region Visible
00407
00411 public bool Visible
00412 {
00413 get
00414 {
00415 EnsureUndisposed();
00416 return implementation.Visible;
00417 }
00418 set
00419 {
00420 EnsureUndisposed();
00421 implementation.Visible = value;
00422 }
00423 }
00424
00425 #endregion
00426
00427 #region Width
00428
00432 public int Width
00433 {
00434 get
00435 {
00436 EnsureUndisposed();
00437 return implementation.Width;
00438 }
00439 set
00440 {
00441 EnsureUndisposed();
00442 implementation.Width = value;
00443 }
00444 }
00445
00446 #endregion
00447
00448 #region WindowBorder
00449
00453 public WindowBorder WindowBorder
00454 {
00455 get
00456 {
00457 return implementation.WindowBorder;
00458 }
00459 set
00460 {
00461 implementation.WindowBorder = value;
00462 }
00463 }
00464
00465 #endregion
00466
00467 #region WindowInfo
00468
00472 public IWindowInfo WindowInfo
00473 {
00474 get
00475 {
00476 EnsureUndisposed();
00477 return implementation.WindowInfo;
00478 }
00479 }
00480
00481 #endregion
00482
00483 #region WindowState
00484
00488 public virtual WindowState WindowState
00489 {
00490 get
00491 {
00492 return implementation.WindowState;
00493 }
00494 set
00495 {
00496 implementation.WindowState = value;
00497 }
00498 }
00499
00500 #endregion
00501
00502 #region X
00503
00507 public int X
00508 {
00509 get
00510 {
00511 EnsureUndisposed();
00512 return implementation.X;
00513 }
00514 set
00515 {
00516 EnsureUndisposed();
00517 implementation.X = value;
00518 }
00519 }
00520
00521 #endregion
00522
00523 #region Y
00524
00528 public int Y
00529 {
00530 get
00531 {
00532 EnsureUndisposed();
00533 return implementation.Y;
00534 }
00535 set
00536 {
00537 EnsureUndisposed();
00538 implementation.Y = value;
00539 }
00540 }
00541
00542 #endregion
00543
00544 #endregion
00545
00546 #region Events
00547
00551 public event EventHandler<EventArgs> Closed;
00552
00556 public event EventHandler<CancelEventArgs> Closing;
00557
00561 public event EventHandler<EventArgs> Disposed;
00562
00566 public event EventHandler<EventArgs> FocusedChanged;
00567
00571 public event EventHandler<EventArgs> IconChanged;
00572
00576 public event EventHandler<KeyPressEventArgs> KeyPress;
00577
00581 public event EventHandler<EventArgs> Move;
00582
00586 public event EventHandler<EventArgs> MouseEnter;
00587
00591 public event EventHandler<EventArgs> MouseLeave;
00592
00596 public event EventHandler<EventArgs> Resize;
00597
00601 public event EventHandler<EventArgs> TitleChanged;
00602
00606 public event EventHandler<EventArgs> VisibleChanged;
00607
00611 public event EventHandler<EventArgs> WindowBorderChanged;
00612
00616 public event EventHandler<EventArgs> WindowStateChanged;
00617
00618 #endregion
00619
00620 #endregion
00621
00622 #region --- IDisposable Members ---
00623
00624 #region Dispose
00625
00629 public virtual void Dispose()
00630 {
00631 if (!IsDisposed)
00632 {
00633 if ((options & GameWindowFlags.Fullscreen) != 0)
00634 {
00635
00636 device.RestoreResolution();
00637 }
00638 implementation.Dispose();
00639 GC.SuppressFinalize(this);
00640
00641 IsDisposed = true;
00642 }
00643 }
00644
00645 #endregion
00646
00647 #endregion
00648
00649 #region --- Protected Members ---
00650
00651 #region Methods
00652
00653 #region EnsureUndisposed
00654
00661 protected void EnsureUndisposed()
00662 {
00663 if (IsDisposed) throw new ObjectDisposedException(GetType().Name);
00664 }
00665
00666 #endregion
00667
00668 #region IsDisposed
00669
00674 protected bool IsDisposed
00675 {
00676 get { return disposed; }
00677 set { disposed = value; }
00678 }
00679
00680 #endregion
00681
00682 #region OnClosed
00683
00688 protected virtual void OnClosed(EventArgs e)
00689 {
00690 if (Closed != null) Closed(this, e);
00691 }
00692
00693 #endregion
00694
00695 #region OnClosing
00696
00703 protected virtual void OnClosing(CancelEventArgs e)
00704 {
00705 if (Closing != null) Closing(this, e);
00706 }
00707
00708 #endregion
00709
00710 #region OnDisposed
00711
00716 protected virtual void OnDisposed(EventArgs e)
00717 {
00718 if (Disposed != null) Disposed(this, e);
00719 }
00720
00721 #endregion
00722
00723 #region OnFocusedChanged
00724
00729 protected virtual void OnFocusedChanged(EventArgs e)
00730 {
00731 if (FocusedChanged != null) FocusedChanged(this, e);
00732 }
00733
00734 #endregion
00735
00736 #region OnIconChanged
00737
00742 protected virtual void OnIconChanged(EventArgs e)
00743 {
00744 if (IconChanged != null) IconChanged(this, e);
00745 }
00746
00747 #endregion
00748
00749 #region OnKeyPress
00750
00755 protected virtual void OnKeyPress(KeyPressEventArgs e)
00756 {
00757 if (KeyPress != null) KeyPress(this, e);
00758 }
00759
00760 #endregion
00761
00762 #region OnMove
00763
00768 protected virtual void OnMove(EventArgs e)
00769 {
00770 if (Move != null) Move(this, e);
00771 }
00772
00773 #endregion
00774
00775 #region OnMouseEnter
00776
00781 protected virtual void OnMouseEnter(EventArgs e)
00782 {
00783 if (MouseEnter != null) MouseEnter(this, e);
00784 }
00785
00786 #endregion
00787
00788 #region OnMouseLeave
00789
00794 protected virtual void OnMouseLeave(EventArgs e)
00795 {
00796 if (MouseLeave != null) MouseLeave(this, e);
00797 }
00798
00799 #endregion
00800
00801 #region OnResize
00802
00807 protected virtual void OnResize(EventArgs e)
00808 {
00809 if (Resize != null) Resize(this, e);
00810 }
00811
00812 #endregion
00813
00814 #region OnTitleChanged
00815
00820 protected virtual void OnTitleChanged(EventArgs e)
00821 {
00822 if (TitleChanged != null) TitleChanged(this, e);
00823 }
00824
00825 #endregion
00826
00827 #region OnVisibleChanged
00828
00833 protected virtual void OnVisibleChanged(EventArgs e)
00834 {
00835 if (VisibleChanged != null) VisibleChanged(this, e);
00836 }
00837
00838 #endregion
00839
00840 #region OnWindowBorderChanged
00841
00846 protected virtual void OnWindowBorderChanged(EventArgs e)
00847 {
00848 if (WindowBorderChanged != null) WindowBorderChanged(this, e);
00849 }
00850
00851 #endregion
00852
00853 #region OnWindowStateChanged
00854
00859 protected virtual void OnWindowStateChanged(EventArgs e)
00860 {
00861 if (WindowStateChanged != null) WindowStateChanged(this, e);
00862 }
00863
00864 #endregion
00865
00866 #region ProcessEvents
00867
00872 protected void ProcessEvents(bool retainEvents)
00873 {
00874 EnsureUndisposed();
00875 if (!retainEvents && !events) Events = true;
00876 implementation.ProcessEvents();
00877 }
00878
00879 #endregion
00880
00881 #endregion
00882
00883 #endregion
00884
00885 #region --- Private Members ---
00886
00887 #region Methods
00888
00889 #region OnClosedInternal
00890
00891 private void OnClosedInternal(object sender, EventArgs e)
00892 {
00893 OnClosed(e);
00894 Events = false;
00895 }
00896
00897 #endregion
00898
00899 #region OnClosingInternal
00900
00901 private void OnClosingInternal(object sender, CancelEventArgs e) { OnClosing(e); }
00902
00903 #endregion
00904
00905 #region OnDisposedInternal
00906
00907 private void OnDisposedInternal(object sender, EventArgs e) { OnDisposed(e); }
00908
00909 #endregion
00910
00911 #region OnFocusedChangedInternal
00912
00913 private void OnFocusedChangedInternal(object sender, EventArgs e) { OnFocusedChanged(e); }
00914
00915 #endregion
00916
00917 #region OnIconChangedInternal
00918
00919 private void OnIconChangedInternal(object sender, EventArgs e) { OnIconChanged(e); }
00920
00921 #endregion
00922
00923 #region OnKeyPressInternal
00924
00925 private void OnKeyPressInternal(object sender, KeyPressEventArgs e) { OnKeyPress(e); }
00926
00927 #endregion
00928
00929 #region OnMouseEnterInternal
00930
00931 private void OnMouseEnterInternal(object sender, EventArgs e) { OnMouseEnter(e); }
00932
00933 #endregion
00934
00935 #region OnMouseLeaveInternal
00936
00937 private void OnMouseLeaveInternal(object sender, EventArgs e) { OnMouseLeave(e); }
00938
00939 #endregion
00940
00941 #region OnMoveInternal
00942
00943 private void OnMoveInternal(object sender, EventArgs e) { OnMove(e); }
00944
00945 #endregion
00946
00947 #region OnResizeInternal
00948
00949 private void OnResizeInternal(object sender, EventArgs e) { OnResize(e); }
00950
00951 #endregion
00952
00953 #region OnTitleChangedInternal
00954
00955 private void OnTitleChangedInternal(object sender, EventArgs e) { OnTitleChanged(e); }
00956
00957 #endregion
00958
00959 #region OnVisibleChangedInternal
00960
00961 private void OnVisibleChangedInternal(object sender, EventArgs e) { OnVisibleChanged(e); }
00962
00963 #endregion
00964
00965 #region OnWindowBorderChangedInternal
00966
00967 private void OnWindowBorderChangedInternal(object sender, EventArgs e) { OnWindowBorderChanged(e); }
00968
00969 #endregion
00970
00971 #region OnWindowStateChangedInternal
00972
00973 private void OnWindowStateChangedInternal(object sender, EventArgs e) { OnWindowStateChanged(e); }
00974
00975 #endregion
00976
00977 #endregion
00978
00979 #region Properties
00980
00981 #region Events
00982
00983 private bool Events
00984 {
00985 set
00986 {
00987 if (value)
00988 {
00989 if (events)
00990 {
00991 throw new InvalidOperationException("Event propagation is already enabled.");
00992 }
00993 implementation.Closed += OnClosedInternal;
00994 implementation.Closing += OnClosingInternal;
00995 implementation.Disposed += OnDisposedInternal;
00996 implementation.FocusedChanged += OnFocusedChangedInternal;
00997 implementation.IconChanged += OnIconChangedInternal;
00998 implementation.KeyPress += OnKeyPressInternal;
00999 implementation.MouseEnter += OnMouseEnterInternal;
01000 implementation.MouseLeave += OnMouseLeaveInternal;
01001 implementation.Move += OnMoveInternal;
01002 implementation.Resize += OnResizeInternal;
01003 implementation.TitleChanged += OnTitleChangedInternal;
01004 implementation.VisibleChanged += OnVisibleChangedInternal;
01005 implementation.WindowBorderChanged += OnWindowBorderChangedInternal;
01006 implementation.WindowStateChanged += OnWindowStateChangedInternal;
01007 events = true;
01008 }
01009 else if (events)
01010 {
01011 implementation.Closed -= OnClosedInternal;
01012 implementation.Closing -= OnClosingInternal;
01013 implementation.Disposed -= OnDisposedInternal;
01014 implementation.FocusedChanged -= OnFocusedChangedInternal;
01015 implementation.IconChanged -= OnIconChangedInternal;
01016 implementation.KeyPress -= OnKeyPressInternal;
01017 implementation.MouseEnter -= OnMouseEnterInternal;
01018 implementation.MouseLeave -= OnMouseLeaveInternal;
01019 implementation.Move -= OnMoveInternal;
01020 implementation.Resize -= OnResizeInternal;
01021 implementation.TitleChanged -= OnTitleChangedInternal;
01022 implementation.VisibleChanged -= OnVisibleChangedInternal;
01023 implementation.WindowBorderChanged -= OnWindowBorderChangedInternal;
01024 implementation.WindowStateChanged -= OnWindowStateChangedInternal;
01025 events = false;
01026 }
01027 else
01028 {
01029 throw new InvalidOperationException("Event propagation is already disabled.");
01030 }
01031 }
01032 }
01033
01034 #endregion
01035
01036 #endregion
01037
01038 #endregion
01039 }
01040
01041 }